2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token
=
57 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant
{
63 /* floating-point literal */
67 /* %<__FUNCTION__%> */
69 /* %<__PRETTY_FUNCTION__%> */
77 /* %<typeid%> operator */
79 /* non-constant compound literals */
87 /* an array reference */
93 /* the address of a label */
107 /* calls to overloaded operators */
111 /* a comma operator */
113 /* a call to a constructor */
115 /* a transaction expression */
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error
{
125 /* is not a class or namespace */
127 /* is not a class, namespace, or enumeration */
131 /* The various kinds of required token */
132 enum required_token
{
134 RT_SEMICOLON
, /* ';' */
135 RT_OPEN_PAREN
, /* '(' */
136 RT_CLOSE_BRACE
, /* '}' */
137 RT_OPEN_BRACE
, /* '{' */
138 RT_CLOSE_SQUARE
, /* ']' */
139 RT_OPEN_SQUARE
, /* '[' */
143 RT_GREATER
, /* '>' */
145 RT_ELLIPSIS
, /* '...' */
149 RT_COLON_SCOPE
, /* ':' or '::' */
150 RT_CLOSE_PAREN
, /* ')' */
151 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
152 RT_PRAGMA_EOL
, /* end of line */
153 RT_NAME
, /* identifier */
155 /* The type is CPP_KEYWORD */
157 RT_DELETE
, /* delete */
158 RT_RETURN
, /* return */
159 RT_WHILE
, /* while */
160 RT_EXTERN
, /* extern */
161 RT_STATIC_ASSERT
, /* static_assert */
162 RT_DECLTYPE
, /* decltype */
163 RT_OPERATOR
, /* operator */
164 RT_CLASS
, /* class */
165 RT_TEMPLATE
, /* template */
166 RT_NAMESPACE
, /* namespace */
167 RT_USING
, /* using */
170 RT_CATCH
, /* catch */
171 RT_THROW
, /* throw */
172 RT_LABEL
, /* __label__ */
173 RT_AT_TRY
, /* @try */
174 RT_AT_SYNCHRONIZED
, /* @synchronized */
175 RT_AT_THROW
, /* @throw */
177 RT_SELECT
, /* selection-statement */
178 RT_ITERATION
, /* iteration-statement */
179 RT_JUMP
, /* jump-statement */
180 RT_CLASS_KEY
, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
195 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
197 saved (parser
->in_type_id_in_expr_p
)
198 { parser
->in_type_id_in_expr_p
= set
; }
199 ~type_id_in_expr_sentinel ()
200 { parser
->in_type_id_in_expr_p
= saved
; }
205 static cp_lexer
*cp_lexer_new_main
207 static cp_lexer
*cp_lexer_new_from_tokens
208 (cp_token_cache
*tokens
);
209 static void cp_lexer_destroy
211 static int cp_lexer_saving_tokens
213 static cp_token
*cp_lexer_token_at
214 (cp_lexer
*, cp_token_position
);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer
*, cp_token
*);
217 static inline cp_token
*cp_lexer_peek_token
219 static cp_token
*cp_lexer_peek_nth_token
220 (cp_lexer
*, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer
*, enum cpp_ttype
);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer
*, enum rid
);
227 static cp_token
*cp_lexer_consume_token
229 static void cp_lexer_purge_token
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer
*, cp_token_position
);
233 static void cp_lexer_save_tokens
235 static void cp_lexer_commit_tokens
237 static void cp_lexer_rollback_tokens
239 static void cp_lexer_print_token
240 (FILE *, cp_token
*);
241 static inline bool cp_lexer_debugging_p
243 static void cp_lexer_start_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
245 static void cp_lexer_stop_debugging
246 (cp_lexer
*) ATTRIBUTE_UNUSED
;
248 static cp_token_cache
*cp_token_cache_new
249 (cp_token
*, cp_token
*);
251 static void cp_parser_initial_pragma
254 static bool cp_parser_omp_declare_reduction_exprs
256 static void cp_finalize_oacc_routine
257 (cp_parser
*, tree
, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream
;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand
;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
279 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
280 cp_token
*start_token
, unsigned num
,
281 cp_token
*curr_token
)
283 unsigned i
, nprinted
;
287 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
293 num
= buffer
->length ();
295 if (start_token
== NULL
)
296 start_token
= buffer
->address ();
298 if (start_token
> buffer
->address ())
300 cp_lexer_print_token (file
, &(*buffer
)[0]);
301 fprintf (file
, " ... ");
306 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
308 if (token
== start_token
)
315 if (token
== curr_token
)
316 fprintf (file
, "[[");
318 cp_lexer_print_token (file
, token
);
320 if (token
== curr_token
)
321 fprintf (file
, "]]");
327 case CPP_CLOSE_BRACE
:
337 if (i
== num
&& i
< buffer
->length ())
339 fprintf (file
, " ... ");
340 cp_lexer_print_token (file
, &buffer
->last ());
343 fprintf (file
, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
350 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
352 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
356 debug (vec
<cp_token
, va_gc
> &ref
)
358 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> *ptr
)
367 fprintf (stderr
, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
375 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
379 fprintf (file
, "%s: ", desc
);
380 print_node_brief (file
, "", t
, 0);
385 /* Dump parser context C to FILE. */
388 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
390 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
392 print_node_brief (file
, "", c
->object_type
, 0);
393 fprintf (file
, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
403 cp_parser_context
*c
;
405 fprintf (file
, "Parsing context stack:\n");
406 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
408 fprintf (file
, "\t#%u: ", i
);
409 cp_debug_print_context (file
, c
);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
420 fprintf (file
, "%s: true\n", desc
);
424 /* Print an unparsed function entry UF to FILE. */
427 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
430 cp_default_arg_entry
*default_arg_fn
;
433 fprintf (file
, "\tFunctions with default args:\n");
435 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
438 fprintf (file
, "\t\tClass type: ");
439 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
440 fprintf (file
, "\t\tDeclaration: ");
441 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
442 fprintf (file
, "\n");
445 fprintf (file
, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
449 print_node_brief (file
, "", fn
, 0);
452 fprintf (file
, "\n");
454 fprintf (file
, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
458 print_node_brief (file
, "", fn
, 0);
461 fprintf (file
, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
468 cp_debug_print_unparsed_queues (FILE *file
,
469 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
472 cp_unparsed_functions_entry
*uf
;
474 fprintf (file
, "Unparsed functions\n");
475 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
477 fprintf (file
, "#%u:\n", i
);
478 cp_debug_print_unparsed_function (file
, uf
);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
489 cp_token
*next_token
, *first_token
, *start_token
;
494 next_token
= parser
->lexer
->next_token
;
495 first_token
= parser
->lexer
->buffer
->address ();
496 start_token
= (next_token
> first_token
+ window_size
/ 2)
497 ? next_token
- window_size
/ 2
499 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
508 cp_debug_parser (FILE *file
, cp_parser
*parser
)
510 const size_t window_size
= 20;
512 expanded_location eloc
;
517 fprintf (file
, "Parser state\n\n");
518 fprintf (file
, "Number of tokens: %u\n",
519 vec_safe_length (parser
->lexer
->buffer
));
520 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
521 cp_debug_print_tree_if_set (file
, "Object scope",
522 parser
->object_scope
);
523 cp_debug_print_tree_if_set (file
, "Qualifying scope",
524 parser
->qualifying_scope
);
525 cp_debug_print_context_stack (file
, parser
->context
);
526 cp_debug_print_flag (file
, "Allow GNU extensions",
527 parser
->allow_gnu_extensions_p
);
528 cp_debug_print_flag (file
, "'>' token is greater-than",
529 parser
->greater_than_is_operator_p
);
530 cp_debug_print_flag (file
, "Default args allowed in current "
531 "parameter list", parser
->default_arg_ok_p
);
532 cp_debug_print_flag (file
, "Parsing integral constant-expression",
533 parser
->integral_constant_expression_p
);
534 cp_debug_print_flag (file
, "Allow non-constant expression in current "
535 "constant-expression",
536 parser
->allow_non_integral_constant_expression_p
);
537 cp_debug_print_flag (file
, "Seen non-constant expression",
538 parser
->non_integral_constant_expression_p
);
539 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
541 parser
->local_variables_forbidden_p
);
542 cp_debug_print_flag (file
, "In unbraced linkage specification",
543 parser
->in_unbraced_linkage_specification_p
);
544 cp_debug_print_flag (file
, "Parsing a declarator",
545 parser
->in_declarator_p
);
546 cp_debug_print_flag (file
, "In template argument list",
547 parser
->in_template_argument_list_p
);
548 cp_debug_print_flag (file
, "Parsing an iteration statement",
549 parser
->in_statement
& IN_ITERATION_STMT
);
550 cp_debug_print_flag (file
, "Parsing a switch statement",
551 parser
->in_statement
& IN_SWITCH_STMT
);
552 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
553 parser
->in_statement
& IN_OMP_BLOCK
);
554 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
555 parser
->in_statement
& IN_OMP_FOR
);
556 cp_debug_print_flag (file
, "Parsing an if statement",
557 parser
->in_statement
& IN_IF_STMT
);
558 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
559 "context", parser
->in_type_id_in_expr_p
);
560 cp_debug_print_flag (file
, "String expressions should be translated "
561 "to execution character set",
562 parser
->translate_strings_p
);
563 cp_debug_print_flag (file
, "Parsing function body outside of a "
564 "local class", parser
->in_function_body
);
565 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
566 parser
->colon_corrects_to_scope_p
);
567 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
568 parser
->colon_doesnt_start_class_def_p
);
569 if (parser
->type_definition_forbidden_message
)
570 fprintf (file
, "Error message for forbidden type definitions: %s\n",
571 parser
->type_definition_forbidden_message
);
572 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
573 fprintf (file
, "Number of class definitions in progress: %u\n",
574 parser
->num_classes_being_defined
);
575 fprintf (file
, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser
->num_template_parameter_lists
);
577 cp_debug_parser_tokens (file
, parser
, window_size
);
578 token
= parser
->lexer
->next_token
;
579 fprintf (file
, "Next token to parse:\n");
580 fprintf (file
, "\tToken: ");
581 cp_lexer_print_token (file
, token
);
582 eloc
= expand_location (token
->location
);
583 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
584 fprintf (file
, "\tLine: %d\n", eloc
.line
);
585 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
589 debug (cp_parser
&ref
)
591 cp_debug_parser (stderr
, &ref
);
595 debug (cp_parser
*ptr
)
600 fprintf (stderr
, "<nil>\n");
603 /* Allocate memory for a new lexer object and return it. */
606 cp_lexer_alloc (void)
610 c_common_no_more_pch ();
612 /* Allocate the memory. */
613 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
615 /* Initially we are not debugging. */
616 lexer
->debugging_p
= false;
618 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
620 /* Create the buffer. */
621 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 cp_lexer_new_main (void)
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token
);
641 lexer
= cp_lexer_alloc ();
643 /* Put the first token in the buffer. */
644 lexer
->buffer
->quick_push (token
);
646 /* Get the remaining tokens from the preprocessor. */
647 while (token
.type
!= CPP_EOF
)
649 cp_lexer_get_preprocessor_token (lexer
, &token
);
650 vec_safe_push (lexer
->buffer
, token
);
653 lexer
->last_token
= lexer
->buffer
->address ()
654 + lexer
->buffer
->length ()
656 lexer
->next_token
= lexer
->buffer
->length ()
657 ? lexer
->buffer
->address ()
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
664 gcc_assert (!lexer
->next_token
->purged_p
);
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
674 cp_token
*first
= cache
->first
;
675 cp_token
*last
= cache
->last
;
676 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
678 /* We do not own the buffer. */
679 lexer
->buffer
= NULL
;
680 lexer
->next_token
= first
== last
? &eof_token
: first
;
681 lexer
->last_token
= last
;
683 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
685 /* Initially we are not debugging. */
686 lexer
->debugging_p
= false;
688 gcc_assert (!lexer
->next_token
->purged_p
);
692 /* Frees all resources associated with LEXER. */
695 cp_lexer_destroy (cp_lexer
*lexer
)
697 vec_free (lexer
->buffer
);
698 lexer
->saved_tokens
.release ();
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
707 #define LEXER_DEBUGGING_ENABLED_P false
709 /* Returns nonzero if debugging information should be output. */
712 cp_lexer_debugging_p (cp_lexer
*lexer
)
714 if (!LEXER_DEBUGGING_ENABLED_P
)
717 return lexer
->debugging_p
;
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
724 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
726 return lexer
->next_token
- previous_p
;
729 static inline cp_token
*
730 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
736 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
738 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer
*lexer
)
744 if (lexer
->next_token
== &eof_token
)
745 return lexer
->last_token
- 1;
747 return cp_lexer_token_position (lexer
, true);
750 static inline cp_token
*
751 cp_lexer_previous_token (cp_lexer
*lexer
)
753 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
755 /* Skip past purged tokens. */
758 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
762 return cp_lexer_token_at (lexer
, tp
);
765 /* nonzero if we are presently saving tokens. */
768 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
770 return lexer
->saved_tokens
.length () != 0;
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
779 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
781 static int is_extern_c
= 0;
783 /* Get a new token from the preprocessor. */
785 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
786 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
787 token
->keyword
= RID_MAX
;
788 token
->purged_p
= false;
789 token
->error_reported
= false;
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c
+= pending_lang_change
;
795 pending_lang_change
= 0;
796 token
->implicit_extern_c
= is_extern_c
> 0;
798 /* Check to see if this token is a keyword. */
799 if (token
->type
== CPP_NAME
)
801 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
803 /* Mark this token as a keyword. */
804 token
->type
= CPP_KEYWORD
;
805 /* Record which keyword. */
806 token
->keyword
= C_RID_CODE (token
->u
.value
);
810 if (warn_cxx11_compat
811 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
812 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
814 /* Warn about the C++0x keyword (but still treat it as
816 warning (OPT_Wc__11_compat
,
817 "identifier %qE is a keyword in C++11",
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
825 token
->keyword
= RID_MAX
;
828 else if (token
->type
== CPP_AT_NAME
)
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token
->type
= CPP_KEYWORD
;
832 switch (C_RID_CODE (token
->u
.value
))
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
842 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
843 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
844 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
845 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
846 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
847 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
848 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
849 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
850 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
855 /* Update the globals input_location and the input file stack from TOKEN. */
857 cp_lexer_set_source_position_from_token (cp_token
*token
)
859 if (token
->type
!= CPP_EOF
)
861 input_location
= token
->location
;
865 /* Update the globals input_location and the input file stack from LEXER. */
867 cp_lexer_set_source_position (cp_lexer
*lexer
)
869 cp_token
*token
= cp_lexer_peek_token (lexer
);
870 cp_lexer_set_source_position_from_token (token
);
873 /* Return a pointer to the next token in the token stream, but do not
876 static inline cp_token
*
877 cp_lexer_peek_token (cp_lexer
*lexer
)
879 if (cp_lexer_debugging_p (lexer
))
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
882 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
883 putc ('\n', cp_lexer_debug_stream
);
885 return lexer
->next_token
;
888 /* Return true if the next token has the indicated TYPE. */
891 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
893 return cp_lexer_peek_token (lexer
)->type
== type
;
896 /* Return true if the next token does not have the indicated TYPE. */
899 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
901 return !cp_lexer_next_token_is (lexer
, type
);
904 /* Return true if the next token is the indicated KEYWORD. */
907 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
909 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
913 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
915 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
919 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
921 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
924 /* Return true if KEYWORD can start a decl-specifier. */
927 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
934 /* Storage classes. */
940 /* Elaborated type specifiers. */
946 /* Simple type specifiers. */
960 /* GNU extensions. */
963 /* C++0x extensions. */
965 case RID_UNDERLYING_TYPE
:
970 if (keyword
>= RID_FIRST_INT_N
971 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
972 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
978 /* Return true if the next token is a keyword for a decl-specifier. */
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
985 token
= cp_lexer_peek_token (lexer
);
986 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
989 /* Returns TRUE iff the token T begins a decltype type. */
992 token_is_decltype (cp_token
*t
)
994 return (t
->keyword
== RID_DECLTYPE
995 || t
->type
== CPP_DECLTYPE
);
998 /* Returns TRUE iff the next token begins a decltype type. */
1001 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1003 cp_token
*t
= cp_lexer_peek_token (lexer
);
1004 return token_is_decltype (t
);
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1011 saved_checks_value (struct tree_check
*check_value
)
1013 /* Perform any access checks that were deferred. */
1014 vec
<deferred_access_check
, va_gc
> *checks
;
1015 deferred_access_check
*chk
;
1016 checks
= check_value
->checks
;
1020 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1021 perform_or_defer_access_check (chk
->binfo
,
1023 chk
->diag_decl
, tf_warning_or_error
);
1025 /* Return the stored value. */
1026 return check_value
->value
;
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1036 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1040 /* N is 1-based, not zero-based. */
1043 if (cp_lexer_debugging_p (lexer
))
1044 fprintf (cp_lexer_debug_stream
,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1048 token
= lexer
->next_token
;
1049 gcc_assert (!n
|| token
!= &eof_token
);
1053 if (token
== lexer
->last_token
)
1059 if (!token
->purged_p
)
1063 if (cp_lexer_debugging_p (lexer
))
1065 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1066 putc ('\n', cp_lexer_debug_stream
);
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1076 cp_lexer_consume_token (cp_lexer
* lexer
)
1078 cp_token
*token
= lexer
->next_token
;
1080 gcc_assert (token
!= &eof_token
);
1081 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1085 lexer
->next_token
++;
1086 if (lexer
->next_token
== lexer
->last_token
)
1088 lexer
->next_token
= &eof_token
;
1093 while (lexer
->next_token
->purged_p
);
1095 cp_lexer_set_source_position_from_token (token
);
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer
))
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1101 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1102 putc ('\n', cp_lexer_debug_stream
);
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1113 cp_lexer_purge_token (cp_lexer
*lexer
)
1115 cp_token
*tok
= lexer
->next_token
;
1117 gcc_assert (tok
!= &eof_token
);
1118 tok
->purged_p
= true;
1119 tok
->location
= UNKNOWN_LOCATION
;
1120 tok
->u
.value
= NULL_TREE
;
1121 tok
->keyword
= RID_MAX
;
1126 if (tok
== lexer
->last_token
)
1132 while (tok
->purged_p
);
1133 lexer
->next_token
= tok
;
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1141 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1143 cp_token
*peek
= lexer
->next_token
;
1145 if (peek
== &eof_token
)
1146 peek
= lexer
->last_token
;
1148 gcc_assert (tok
< peek
);
1150 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1152 tok
->purged_p
= true;
1153 tok
->location
= UNKNOWN_LOCATION
;
1154 tok
->u
.value
= NULL_TREE
;
1155 tok
->keyword
= RID_MAX
;
1159 /* Begin saving tokens. All tokens consumed after this point will be
1163 cp_lexer_save_tokens (cp_lexer
* lexer
)
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer
))
1167 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1169 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1172 /* Commit to the portion of the token stream most recently saved. */
1175 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer
))
1179 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1181 lexer
->saved_tokens
.pop ();
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1188 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer
))
1192 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1194 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1202 struct saved_token_sentinel
1207 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1209 len
= lexer
->saved_tokens
.length ();
1210 cp_lexer_save_tokens (lexer
);
1214 cp_lexer_rollback_tokens (lexer
);
1217 ~saved_token_sentinel()
1220 cp_lexer_commit_tokens (lexer
);
1221 gcc_assert (lexer
->saved_tokens
.length () == len
);
1225 /* Print a representation of the TOKEN on the STREAM. */
1228 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names
[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1239 /* C++ parser token types - see "Manifest constants", above. */
1242 "NESTED_NAME_SPECIFIER",
1245 /* For some tokens, print the associated data. */
1246 switch (token
->type
)
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token
->u
.value
))
1255 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1262 case CPP_UTF8STRING
:
1263 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1267 print_generic_expr (stream
, token
->u
.value
);
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token
->type
< ARRAY_SIZE(token_names
))
1274 fputs (token_names
[token
->type
], stream
);
1276 fprintf (stream
, "[%d]", token
->type
);
1282 debug (cp_token
&ref
)
1284 cp_lexer_print_token (stderr
, &ref
);
1285 fprintf (stderr
, "\n");
1289 debug (cp_token
*ptr
)
1294 fprintf (stderr
, "<nil>\n");
1298 /* Start emitting debugging information. */
1301 cp_lexer_start_debugging (cp_lexer
* lexer
)
1303 if (!LEXER_DEBUGGING_ENABLED_P
)
1304 fatal_error (input_location
,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1307 lexer
->debugging_p
= true;
1308 cp_lexer_debug_stream
= stderr
;
1311 /* Stop emitting debugging information. */
1314 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1316 if (!LEXER_DEBUGGING_ENABLED_P
)
1317 fatal_error (input_location
,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1320 lexer
->debugging_p
= false;
1321 cp_lexer_debug_stream
= NULL
;
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1326 static cp_token_cache
*
1327 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1329 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1330 cache
->first
= first
;
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1339 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1341 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser
->omp_declare_simd
= NULL
;
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1353 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1355 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1357 if (fndecl
== error_mark_node
)
1359 parser
->omp_declare_simd
= NULL
;
1362 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1364 cp_ensure_no_omp_declare_simd (parser
);
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1374 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1376 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1378 error_at (parser
->oacc_routine
->loc
,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser
->oacc_routine
= NULL
;
1385 /* Decl-specifiers. */
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1390 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1392 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1402 static cp_declarator
*make_call_declarator
1403 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1404 static cp_declarator
*make_array_declarator
1405 (cp_declarator
*, tree
);
1406 static cp_declarator
*make_pointer_declarator
1407 (cp_cv_quals
, cp_declarator
*, tree
);
1408 static cp_declarator
*make_reference_declarator
1409 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1410 static cp_declarator
*make_ptrmem_declarator
1411 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1413 /* An erroneous declarator. */
1414 static cp_declarator
*cp_error_declarator
;
1416 /* The obstack on which declarators and related data structures are
1418 static struct obstack declarator_obstack
;
1420 /* Alloc BYTES from the declarator memory pool. */
1422 static inline void *
1423 alloc_declarator (size_t bytes
)
1425 return obstack_alloc (&declarator_obstack
, bytes
);
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1431 static cp_declarator
*
1432 make_declarator (cp_declarator_kind kind
)
1434 cp_declarator
*declarator
;
1436 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1437 declarator
->kind
= kind
;
1438 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1439 declarator
->attributes
= NULL_TREE
;
1440 declarator
->std_attributes
= NULL_TREE
;
1441 declarator
->declarator
= NULL
;
1442 declarator
->parameter_pack_p
= false;
1443 declarator
->id_loc
= UNKNOWN_LOCATION
;
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1454 static cp_declarator
*
1455 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1456 special_function_kind sfk
, location_t id_location
)
1458 cp_declarator
*declarator
;
1460 /* It is valid to write:
1462 class C { void f(); };
1466 The standard is not clear about whether `typedef const C D' is
1467 legal; as of 2002-09-15 the committee is considering that
1468 question. EDG 3.0 allows that syntax. Therefore, we do as
1470 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1471 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1473 gcc_assert (identifier_p (unqualified_name
)
1474 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1475 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1477 declarator
= make_declarator (cdk_id
);
1478 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1479 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1480 declarator
->u
.id
.sfk
= sfk
;
1481 declarator
->id_loc
= id_location
;
1486 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1487 of modifiers such as const or volatile to apply to the pointer
1488 type, represented as identifiers. ATTRIBUTES represent the attributes that
1489 appertain to the pointer or reference. */
1492 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1495 cp_declarator
*declarator
;
1497 declarator
= make_declarator (cdk_pointer
);
1498 declarator
->declarator
= target
;
1499 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1500 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1503 declarator
->id_loc
= target
->id_loc
;
1504 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1505 target
->parameter_pack_p
= false;
1508 declarator
->parameter_pack_p
= false;
1510 declarator
->std_attributes
= attributes
;
1515 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1516 represent the attributes that appertain to the pointer or
1520 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1521 bool rvalue_ref
, tree attributes
)
1523 cp_declarator
*declarator
;
1525 declarator
= make_declarator (cdk_reference
);
1526 declarator
->declarator
= target
;
1527 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1528 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1531 declarator
->id_loc
= target
->id_loc
;
1532 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1533 target
->parameter_pack_p
= false;
1536 declarator
->parameter_pack_p
= false;
1538 declarator
->std_attributes
= attributes
;
1543 /* Like make_pointer_declarator -- but for a pointer to a non-static
1544 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1545 appertain to the pointer or reference. */
1548 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1549 cp_declarator
*pointee
,
1552 cp_declarator
*declarator
;
1554 declarator
= make_declarator (cdk_ptrmem
);
1555 declarator
->declarator
= pointee
;
1556 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1557 declarator
->u
.pointer
.class_type
= class_type
;
1561 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1562 pointee
->parameter_pack_p
= false;
1565 declarator
->parameter_pack_p
= false;
1567 declarator
->std_attributes
= attributes
;
1572 /* Make a declarator for the function given by TARGET, with the
1573 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1574 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1575 indicates what exceptions can be thrown. */
1578 make_call_declarator (cp_declarator
*target
,
1580 cp_cv_quals cv_qualifiers
,
1581 cp_virt_specifiers virt_specifiers
,
1582 cp_ref_qualifier ref_qualifier
,
1584 tree exception_specification
,
1585 tree late_return_type
,
1586 tree requires_clause
)
1588 cp_declarator
*declarator
;
1590 declarator
= make_declarator (cdk_function
);
1591 declarator
->declarator
= target
;
1592 declarator
->u
.function
.parameters
= parms
;
1593 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1594 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1595 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1596 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1597 declarator
->u
.function
.exception_specification
= exception_specification
;
1598 declarator
->u
.function
.late_return_type
= late_return_type
;
1599 declarator
->u
.function
.requires_clause
= requires_clause
;
1602 declarator
->id_loc
= target
->id_loc
;
1603 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1604 target
->parameter_pack_p
= false;
1607 declarator
->parameter_pack_p
= false;
1612 /* Make a declarator for an array of BOUNDS elements, each of which is
1613 defined by ELEMENT. */
1616 make_array_declarator (cp_declarator
*element
, tree bounds
)
1618 cp_declarator
*declarator
;
1620 declarator
= make_declarator (cdk_array
);
1621 declarator
->declarator
= element
;
1622 declarator
->u
.array
.bounds
= bounds
;
1625 declarator
->id_loc
= element
->id_loc
;
1626 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1627 element
->parameter_pack_p
= false;
1630 declarator
->parameter_pack_p
= false;
1635 /* Determine whether the declarator we've seen so far can be a
1636 parameter pack, when followed by an ellipsis. */
1638 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1640 if (declarator
&& declarator
->parameter_pack_p
)
1641 /* We already saw an ellipsis. */
1644 /* Search for a declarator name, or any other declarator that goes
1645 after the point where the ellipsis could appear in a parameter
1646 pack. If we find any of these, then this declarator can not be
1647 made into a parameter pack. */
1649 while (declarator
&& !found
)
1651 switch ((int)declarator
->kind
)
1663 declarator
= declarator
->declarator
;
1671 cp_parameter_declarator
*no_parameters
;
1673 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1674 DECLARATOR and DEFAULT_ARGUMENT. */
1676 cp_parameter_declarator
*
1677 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1678 cp_declarator
*declarator
,
1679 tree default_argument
,
1681 bool template_parameter_pack_p
= false)
1683 cp_parameter_declarator
*parameter
;
1685 parameter
= ((cp_parameter_declarator
*)
1686 alloc_declarator (sizeof (cp_parameter_declarator
)));
1687 parameter
->next
= NULL
;
1688 if (decl_specifiers
)
1689 parameter
->decl_specifiers
= *decl_specifiers
;
1691 clear_decl_specs (¶meter
->decl_specifiers
);
1692 parameter
->declarator
= declarator
;
1693 parameter
->default_argument
= default_argument
;
1694 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1695 parameter
->loc
= loc
;
1700 /* Returns true iff DECLARATOR is a declaration for a function. */
1703 function_declarator_p (const cp_declarator
*declarator
)
1707 if (declarator
->kind
== cdk_function
1708 && declarator
->declarator
->kind
== cdk_id
)
1710 if (declarator
->kind
== cdk_id
1711 || declarator
->kind
== cdk_decomp
1712 || declarator
->kind
== cdk_error
)
1714 declarator
= declarator
->declarator
;
1724 A cp_parser parses the token stream as specified by the C++
1725 grammar. Its job is purely parsing, not semantic analysis. For
1726 example, the parser breaks the token stream into declarators,
1727 expressions, statements, and other similar syntactic constructs.
1728 It does not check that the types of the expressions on either side
1729 of an assignment-statement are compatible, or that a function is
1730 not declared with a parameter of type `void'.
1732 The parser invokes routines elsewhere in the compiler to perform
1733 semantic analysis and to build up the abstract syntax tree for the
1736 The parser (and the template instantiation code, which is, in a
1737 way, a close relative of parsing) are the only parts of the
1738 compiler that should be calling push_scope and pop_scope, or
1739 related functions. The parser (and template instantiation code)
1740 keeps track of what scope is presently active; everything else
1741 should simply honor that. (The code that generates static
1742 initializers may also need to set the scope, in order to check
1743 access control correctly when emitting the initializers.)
1748 The parser is of the standard recursive-descent variety. Upcoming
1749 tokens in the token stream are examined in order to determine which
1750 production to use when parsing a non-terminal. Some C++ constructs
1751 require arbitrary look ahead to disambiguate. For example, it is
1752 impossible, in the general case, to tell whether a statement is an
1753 expression or declaration without scanning the entire statement.
1754 Therefore, the parser is capable of "parsing tentatively." When the
1755 parser is not sure what construct comes next, it enters this mode.
1756 Then, while we attempt to parse the construct, the parser queues up
1757 error messages, rather than issuing them immediately, and saves the
1758 tokens it consumes. If the construct is parsed successfully, the
1759 parser "commits", i.e., it issues any queued error messages and
1760 the tokens that were being preserved are permanently discarded.
1761 If, however, the construct is not parsed successfully, the parser
1762 rolls back its state completely so that it can resume parsing using
1763 a different alternative.
1768 The performance of the parser could probably be improved substantially.
1769 We could often eliminate the need to parse tentatively by looking ahead
1770 a little bit. In some places, this approach might not entirely eliminate
1771 the need to parse tentatively, but it might still speed up the average
1774 /* Flags that are passed to some parsing functions. These values can
1775 be bitwise-ored together. */
1780 CP_PARSER_FLAGS_NONE
= 0x0,
1781 /* The construct is optional. If it is not present, then no error
1782 should be issued. */
1783 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1784 /* When parsing a type-specifier, treat user-defined type-names
1785 as non-type identifiers. */
1786 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1787 /* When parsing a type-specifier, do not try to parse a class-specifier
1788 or enum-specifier. */
1789 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1790 /* When parsing a decl-specifier-seq, only allow type-specifier or
1792 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1793 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1794 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags
;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT
,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED
,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1820 PREC_LOGICAL_OR_EXPRESSION
,
1821 PREC_LOGICAL_AND_EXPRESSION
,
1822 PREC_INCLUSIVE_OR_EXPRESSION
,
1823 PREC_EXCLUSIVE_OR_EXPRESSION
,
1824 PREC_AND_EXPRESSION
,
1825 PREC_EQUALITY_EXPRESSION
,
1826 PREC_RELATIONAL_EXPRESSION
,
1827 PREC_SHIFT_EXPRESSION
,
1828 PREC_ADDITIVE_EXPRESSION
,
1829 PREC_MULTIPLICATIVE_EXPRESSION
,
1831 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type
;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type
;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec
;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type
;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type
;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec
;
1859 /* Location of the binary operation we are parsing. */
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1871 /* Constructors and destructors. */
1873 static cp_parser_context
*cp_parser_context_new
1874 (cp_parser_context
*);
1876 /* Class variables. */
1878 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1884 static const cp_parser_binary_operations_map_node binops
[] = {
1885 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1886 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1888 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1889 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1890 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1892 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1893 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1895 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1896 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1898 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1899 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1900 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1901 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1903 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1904 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1906 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1908 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1910 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1912 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1914 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context
*
1928 cp_parser_context_new (cp_parser_context
* next
)
1930 cp_parser_context
*context
;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list
!= NULL
)
1935 /* Pull the first entry from the free list. */
1936 context
= cp_parser_context_free_list
;
1937 cp_parser_context_free_list
= context
->next
;
1938 memset (context
, 0, sizeof (*context
));
1941 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1943 /* No errors have occurred yet in this context. */
1944 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context
->object_type
= next
->object_type
;
1952 /* Thread the stack. */
1953 context
->next
= next
;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1971 push_unparsed_function_queues (cp_parser
*parser
)
1973 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1974 vec_safe_push (parser
->unparsed_queues
, e
);
1978 pop_unparsed_function_queues (cp_parser
*parser
)
1980 release_tree_vector (unparsed_funs_with_definitions
);
1981 parser
->unparsed_queues
->pop ();
1986 /* Constructors and destructors. */
1988 static cp_parser
*cp_parser_new
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser
*, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2011 static tree cp_parser_userdef_string_literal
2013 static cp_expr cp_parser_userdef_numeric_literal
2016 /* Basic concepts [gram.basic] */
2018 static void cp_parser_translation_unit (cp_parser
*);
2020 /* Expressions [gram.expr] */
2022 static cp_expr cp_parser_primary_expression
2023 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2024 static cp_expr cp_parser_id_expression
2025 (cp_parser
*, bool, bool, bool *, bool, bool);
2026 static cp_expr cp_parser_unqualified_id
2027 (cp_parser
*, bool, bool, bool, bool);
2028 static tree cp_parser_nested_name_specifier_opt
2029 (cp_parser
*, bool, bool, bool, bool, bool = false);
2030 static tree cp_parser_nested_name_specifier
2031 (cp_parser
*, bool, bool, bool, bool);
2032 static tree cp_parser_qualifying_entity
2033 (cp_parser
*, bool, bool, bool, bool, bool);
2034 static cp_expr cp_parser_postfix_expression
2035 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2036 static tree cp_parser_postfix_open_square_expression
2037 (cp_parser
*, tree
, bool, bool);
2038 static tree cp_parser_postfix_dot_deref_expression
2039 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2040 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2041 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser
*, tree
, tree
*, tree
*);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2051 static tree cp_parser_has_attribute_expression
2053 static tree cp_parser_new_expression
2055 static vec
<tree
, va_gc
> *cp_parser_new_placement
2057 static tree cp_parser_new_type_id
2058 (cp_parser
*, tree
*);
2059 static cp_declarator
*cp_parser_new_declarator_opt
2061 static cp_declarator
*cp_parser_direct_new_declarator
2063 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2065 static tree cp_parser_delete_expression
2067 static cp_expr cp_parser_cast_expression
2068 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2069 static cp_expr cp_parser_binary_expression
2070 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2071 static tree cp_parser_question_colon_clause
2072 (cp_parser
*, cp_expr
);
2073 static cp_expr cp_parser_assignment_expression
2074 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2075 static enum tree_code cp_parser_assignment_operator_opt
2077 static cp_expr cp_parser_expression
2078 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2079 static cp_expr cp_parser_constant_expression
2080 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2081 static cp_expr cp_parser_builtin_offsetof
2083 static cp_expr cp_parser_lambda_expression
2085 static void cp_parser_lambda_introducer
2086 (cp_parser
*, tree
);
2087 static bool cp_parser_lambda_declarator_opt
2088 (cp_parser
*, tree
);
2089 static void cp_parser_lambda_body
2090 (cp_parser
*, tree
);
2092 /* Statements [gram.stmt.stmt] */
2094 static void cp_parser_statement
2095 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2096 static void cp_parser_label_for_labeled_statement
2097 (cp_parser
*, tree
);
2098 static tree cp_parser_expression_statement
2099 (cp_parser
*, tree
);
2100 static tree cp_parser_compound_statement
2101 (cp_parser
*, tree
, int, bool);
2102 static void cp_parser_statement_seq_opt
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_selection_statement
2105 (cp_parser
*, bool *, vec
<tree
> *);
2106 static tree cp_parser_condition
2108 static tree cp_parser_iteration_statement
2109 (cp_parser
*, bool *, bool, unsigned short);
2110 static bool cp_parser_init_statement
2111 (cp_parser
*, tree
*decl
);
2112 static tree cp_parser_for
2113 (cp_parser
*, bool, unsigned short);
2114 static tree cp_parser_c_for
2115 (cp_parser
*, tree
, tree
, bool, unsigned short);
2116 static tree cp_parser_range_for
2117 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2118 static void do_range_for_auto_deduction
2120 static tree cp_parser_perform_range_for_lookup
2121 (tree
, tree
*, tree
*);
2122 static tree cp_parser_range_for_member_function
2124 static tree cp_parser_jump_statement
2126 static void cp_parser_declaration_statement
2129 static tree cp_parser_implicitly_scoped_statement
2130 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2131 static void cp_parser_already_scoped_statement
2132 (cp_parser
*, bool *, const token_indent_info
&);
2134 /* Declarations [gram.dcl.dcl] */
2136 static void cp_parser_declaration_seq_opt
2138 static void cp_parser_declaration
2140 static void cp_parser_toplevel_declaration
2142 static void cp_parser_block_declaration
2143 (cp_parser
*, bool);
2144 static void cp_parser_simple_declaration
2145 (cp_parser
*, bool, tree
*);
2146 static void cp_parser_decl_specifier_seq
2147 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2148 static tree cp_parser_storage_class_specifier_opt
2150 static tree cp_parser_function_specifier_opt
2151 (cp_parser
*, cp_decl_specifier_seq
*);
2152 static tree cp_parser_type_specifier
2153 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2155 static tree cp_parser_simple_type_specifier
2156 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2157 static tree cp_parser_type_name
2158 (cp_parser
*, bool);
2159 static tree cp_parser_type_name
2161 static tree cp_parser_nonclass_name
2162 (cp_parser
* parser
);
2163 static tree cp_parser_elaborated_type_specifier
2164 (cp_parser
*, bool, bool);
2165 static tree cp_parser_enum_specifier
2167 static void cp_parser_enumerator_list
2168 (cp_parser
*, tree
);
2169 static void cp_parser_enumerator_definition
2170 (cp_parser
*, tree
);
2171 static tree cp_parser_namespace_name
2173 static void cp_parser_namespace_definition
2175 static void cp_parser_namespace_body
2177 static tree cp_parser_qualified_namespace_specifier
2179 static void cp_parser_namespace_alias_definition
2181 static bool cp_parser_using_declaration
2182 (cp_parser
*, bool);
2183 static void cp_parser_using_directive
2185 static tree cp_parser_alias_declaration
2187 static void cp_parser_asm_definition
2189 static void cp_parser_linkage_specification
2191 static void cp_parser_static_assert
2192 (cp_parser
*, bool);
2193 static tree cp_parser_decltype
2195 static tree cp_parser_decomposition_declaration
2196 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2198 /* Declarators [gram.dcl.decl] */
2200 static tree cp_parser_init_declarator
2201 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2202 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2203 static cp_declarator
*cp_parser_declarator
2204 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2205 static cp_declarator
*cp_parser_direct_declarator
2206 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2207 static enum tree_code cp_parser_ptr_operator
2208 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2209 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2211 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2213 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2215 static tree cp_parser_tx_qualifier_opt
2217 static tree cp_parser_late_return_type_opt
2218 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2219 static tree cp_parser_declarator_id
2220 (cp_parser
*, bool);
2221 static tree cp_parser_type_id
2222 (cp_parser
*, location_t
* = NULL
);
2223 static tree cp_parser_template_type_arg
2225 static tree
cp_parser_trailing_type_id (cp_parser
*);
2226 static tree cp_parser_type_id_1
2227 (cp_parser
*, bool, bool, location_t
*);
2228 static void cp_parser_type_specifier_seq
2229 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2230 static tree cp_parser_parameter_declaration_clause
2232 static tree cp_parser_parameter_declaration_list
2234 static cp_parameter_declarator
*cp_parser_parameter_declaration
2235 (cp_parser
*, bool, bool *);
2236 static tree cp_parser_default_argument
2237 (cp_parser
*, bool);
2238 static void cp_parser_function_body
2239 (cp_parser
*, bool);
2240 static tree cp_parser_initializer
2241 (cp_parser
*, bool *, bool *, bool = false);
2242 static cp_expr cp_parser_initializer_clause
2243 (cp_parser
*, bool *);
2244 static cp_expr cp_parser_braced_list
2245 (cp_parser
*, bool*);
2246 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2247 (cp_parser
*, bool *);
2249 static void cp_parser_ctor_initializer_opt_and_function_body
2250 (cp_parser
*, bool);
2252 static tree cp_parser_late_parsing_omp_declare_simd
2253 (cp_parser
*, tree
);
2255 static tree cp_parser_late_parsing_oacc_routine
2256 (cp_parser
*, tree
);
2258 static tree synthesize_implicit_template_parm
2259 (cp_parser
*, tree
);
2260 static tree finish_fully_implicit_template
2261 (cp_parser
*, tree
);
2262 static void abort_fully_implicit_template
2265 /* Classes [gram.class] */
2267 static tree cp_parser_class_name
2268 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2269 static tree cp_parser_class_specifier
2271 static tree cp_parser_class_head
2272 (cp_parser
*, bool *);
2273 static enum tag_types cp_parser_class_key
2275 static void cp_parser_type_parameter_key
2276 (cp_parser
* parser
);
2277 static void cp_parser_member_specification_opt
2279 static void cp_parser_member_declaration
2281 static tree cp_parser_pure_specifier
2283 static tree cp_parser_constant_initializer
2286 /* Derived classes [gram.class.derived] */
2288 static tree cp_parser_base_clause
2290 static tree cp_parser_base_specifier
2293 /* Special member functions [gram.special] */
2295 static tree cp_parser_conversion_function_id
2297 static tree cp_parser_conversion_type_id
2299 static cp_declarator
*cp_parser_conversion_declarator_opt
2301 static void cp_parser_ctor_initializer_opt
2303 static void cp_parser_mem_initializer_list
2305 static tree cp_parser_mem_initializer
2307 static tree cp_parser_mem_initializer_id
2310 /* Overloading [gram.over] */
2312 static cp_expr cp_parser_operator_function_id
2314 static cp_expr cp_parser_operator
2315 (cp_parser
*, location_t
);
2317 /* Templates [gram.temp] */
2319 static void cp_parser_template_declaration
2320 (cp_parser
*, bool);
2321 static tree cp_parser_template_parameter_list
2323 static tree cp_parser_template_parameter
2324 (cp_parser
*, bool *, bool *);
2325 static tree cp_parser_type_parameter
2326 (cp_parser
*, bool *);
2327 static tree cp_parser_template_id
2328 (cp_parser
*, bool, bool, enum tag_types
, bool);
2329 static tree cp_parser_template_name
2330 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2331 static tree cp_parser_template_argument_list
2333 static tree cp_parser_template_argument
2335 static void cp_parser_explicit_instantiation
2337 static void cp_parser_explicit_specialization
2340 /* Exception handling [gram.exception] */
2342 static tree cp_parser_try_block
2344 static void cp_parser_function_try_block
2346 static void cp_parser_handler_seq
2348 static void cp_parser_handler
2350 static tree cp_parser_exception_declaration
2352 static tree cp_parser_throw_expression
2354 static tree cp_parser_exception_specification_opt
2356 static tree cp_parser_type_id_list
2359 /* GNU Extensions */
2361 static tree cp_parser_asm_specification_opt
2363 static tree cp_parser_asm_operand_list
2365 static tree cp_parser_asm_clobber_list
2367 static tree cp_parser_asm_label_list
2369 static bool cp_next_tokens_can_be_attribute_p
2371 static bool cp_next_tokens_can_be_gnu_attribute_p
2373 static bool cp_next_tokens_can_be_std_attribute_p
2375 static bool cp_nth_tokens_can_be_std_attribute_p
2376 (cp_parser
*, size_t);
2377 static bool cp_nth_tokens_can_be_gnu_attribute_p
2378 (cp_parser
*, size_t);
2379 static bool cp_nth_tokens_can_be_attribute_p
2380 (cp_parser
*, size_t);
2381 static tree cp_parser_attributes_opt
2383 static tree cp_parser_gnu_attributes_opt
2385 static tree cp_parser_gnu_attribute_list
2386 (cp_parser
*, bool = false);
2387 static tree cp_parser_std_attribute
2388 (cp_parser
*, tree
);
2389 static tree cp_parser_std_attribute_spec
2391 static tree cp_parser_std_attribute_spec_seq
2393 static size_t cp_parser_skip_attributes_opt
2394 (cp_parser
*, size_t);
2395 static bool cp_parser_extension_opt
2396 (cp_parser
*, int *);
2397 static void cp_parser_label_declaration
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2404 static tree cp_parser_requires_clause_opt
2406 static tree cp_parser_requires_expression
2408 static tree cp_parser_requirement_parameter_list
2410 static tree cp_parser_requirement_body
2412 static tree cp_parser_requirement_list
2414 static tree cp_parser_requirement
2416 static tree cp_parser_simple_requirement
2418 static tree cp_parser_compound_requirement
2420 static tree cp_parser_type_requirement
2422 static tree cp_parser_nested_requirement
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser
*, cp_token
*);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser
*, enum rid
);
2431 static void cp_parser_function_transaction
2432 (cp_parser
*, enum rid
);
2433 static tree cp_parser_transaction_cancel
2436 enum pragma_context
{
2443 static bool cp_parser_pragma
2444 (cp_parser
*, enum pragma_context
, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2450 static tree cp_parser_objc_message_args
2452 static tree cp_parser_objc_message_expression
2454 static cp_expr cp_parser_objc_encode_expression
2456 static tree cp_parser_objc_defs_expression
2458 static tree cp_parser_objc_protocol_expression
2460 static tree cp_parser_objc_selector_expression
2462 static cp_expr cp_parser_objc_expression
2464 static bool cp_parser_objc_selector_p
2466 static tree cp_parser_objc_selector
2468 static tree cp_parser_objc_protocol_refs_opt
2470 static void cp_parser_objc_declaration
2471 (cp_parser
*, tree
);
2472 static tree cp_parser_objc_statement
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser
*, tree
*);
2476 static void cp_parser_objc_at_property_declaration
2478 static void cp_parser_objc_at_synthesize_declaration
2480 static void cp_parser_objc_at_dynamic_declaration
2482 static tree cp_parser_objc_struct_declaration
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser
*, tree
, location_t
);
2491 static tree cp_parser_maybe_treat_template_as_class
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser
*, cp_declarator
*, location_t
);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2497 static cp_expr cp_parser_simple_cast_expression
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser
*, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser
*, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser
*, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser
*, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec
<deferred_access_check
, va_gc
> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser
*, tree
);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2517 static tree cp_parser_save_nsdmi
2519 static tree cp_parser_enclosed_template_argument_list
2521 static void cp_parser_save_default_args
2522 (cp_parser
*, tree
);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser
*, tree
);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser
*, tree
, tree
, tree
);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser
*, tree
);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser
*, tree
);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser
*, enum rid
);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser
*, enum rid
);
2535 static bool cp_parser_declares_only_class_p
2537 static void cp_parser_set_storage_class
2538 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq
*decl_specs
,
2543 cp_decl_spec ds
, cp_token
*);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq
*);
2546 static void cp_parser_required_error
2547 (cp_parser
*, required_token
, bool, location_t
);
2548 static cp_token
*cp_parser_require
2549 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2550 static cp_token
*cp_parser_require_keyword
2551 (cp_parser
*, enum rid
, required_token
);
2552 static bool cp_parser_token_starts_function_definition_p
2554 static bool cp_parser_next_token_starts_class_definition_p
2556 static bool cp_parser_next_token_ends_template_argument_p
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser
*, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2564 static void cp_parser_check_class_key
2565 (enum tag_types
, tree type
);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type
, location_t location
);
2568 static bool cp_parser_optional_template_keyword
2570 static void cp_parser_pre_parsed_nested_name_specifier
2572 static bool cp_parser_cache_group
2573 (cp_parser
*, enum cpp_ttype
, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser
*parser
, bool nsdmi
);
2576 static void cp_parser_parse_tentatively
2578 static void cp_parser_commit_to_tentative_parse
2580 static void cp_parser_commit_to_topmost_tentative_parse
2582 static void cp_parser_abort_tentative_parse
2584 static bool cp_parser_parse_definitely
2586 static inline bool cp_parser_parsing_tentatively
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2590 static void cp_parser_error
2591 (cp_parser
*, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2594 static bool cp_parser_simulate_error
2596 static bool cp_parser_check_type_definition
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator
*, tree
, location_t type_location
);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser
*, non_integral_constant
);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser
*, tree
, location_t
);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser
*, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2614 static void cp_parser_skip_to_end_of_block_or_statement
2616 static bool cp_parser_skip_to_closing_brace
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser
*, cp_token
*);
2622 static bool cp_parser_error_occurred
2624 static bool cp_parser_allow_gnu_extensions_p
2626 static bool cp_parser_is_pure_string_literal
2628 static bool cp_parser_is_string_literal
2630 static bool cp_parser_is_keyword
2631 (cp_token
*, enum rid
);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser
*, tree
, location_t location
);
2634 static cp_declarator
* cp_parser_make_indirect_declarator
2635 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2636 static bool cp_parser_compound_literal_p
2638 static bool cp_parser_array_designator_p
2640 static bool cp_parser_init_statement_p
2642 static bool cp_parser_skip_to_closing_square_bracket
2645 /* Concept-related syntactic transformations */
2647 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2648 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand
;
2657 ++c_inhibit_evaluation_warnings
;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings
;
2663 --cp_unevaluated_operand
;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2672 cp_parser_parsing_tentatively (cp_parser
* parser
)
2674 return parser
->context
->next
!= NULL
;
2677 /* Returns nonzero if TOKEN is a string literal. */
2680 cp_parser_is_pure_string_literal (cp_token
* token
)
2682 return (token
->type
== CPP_STRING
||
2683 token
->type
== CPP_STRING16
||
2684 token
->type
== CPP_STRING32
||
2685 token
->type
== CPP_WSTRING
||
2686 token
->type
== CPP_UTF8STRING
);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2693 cp_parser_is_string_literal (cp_token
* token
)
2695 return (cp_parser_is_pure_string_literal (token
) ||
2696 token
->type
== CPP_STRING_USERDEF
||
2697 token
->type
== CPP_STRING16_USERDEF
||
2698 token
->type
== CPP_STRING32_USERDEF
||
2699 token
->type
== CPP_WSTRING_USERDEF
||
2700 token
->type
== CPP_UTF8STRING_USERDEF
);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2708 return token
->keyword
== keyword
;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token
*token
)
2717 if (token
->type
!= CPP_PRAGMA
)
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2735 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2736 location_t
*out_loc
)
2738 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2739 if (token2
->type
!= tok1_kind
)
2741 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2742 if (token3
->type
!= tok1_kind
)
2744 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2745 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2748 /* It must be at the start of the line. */
2749 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2750 if (LOCATION_COLUMN (start_loc
) != 1)
2753 /* We have a conflict marker. Construct a location of the form:
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc
= get_finish (token4
->location
);
2758 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2767 get_matching_symbol (required_token token_desc
)
2774 case RT_CLOSE_BRACE
:
2776 case RT_CLOSE_PAREN
:
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc
)
2790 return CPP_SEMICOLON
;
2792 return CPP_OPEN_PAREN
;
2793 case RT_CLOSE_BRACE
:
2794 return CPP_CLOSE_BRACE
;
2796 return CPP_OPEN_BRACE
;
2797 case RT_CLOSE_SQUARE
:
2798 return CPP_CLOSE_SQUARE
;
2799 case RT_OPEN_SQUARE
:
2800 return CPP_OPEN_SQUARE
;
2805 case RT_CLOSE_PAREN
:
2806 return CPP_CLOSE_PAREN
;
2809 /* Use CPP_EOF as a "no completions possible" code. */
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2834 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2835 required_token missing_token_desc
,
2836 location_t matching_location
)
2838 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token
);
2843 if (token
->type
== CPP_PRAGMA
)
2845 error_at (token
->location
,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser
, token
);
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token
->type
== CPP_LSHIFT
2853 || token
->type
== CPP_RSHIFT
2854 || token
->type
== CPP_EQ_EQ
)
2857 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2859 error_at (loc
, "version control conflict marker in file");
2860 expanded_location token_exploc
= expand_location (token
->location
);
2861 /* Consume tokens until the end of the source line. */
2864 cp_lexer_consume_token (parser
->lexer
);
2865 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2868 expanded_location next_exploc
= expand_location (next
->location
);
2869 if (next_exploc
.file
!= token_exploc
.file
)
2871 if (next_exploc
.line
!= token_exploc
.line
)
2878 gcc_rich_location
richloc (input_location
);
2880 bool added_matching_location
= false;
2882 if (missing_token_desc
!= RT_NONE
)
2884 /* Potentially supply a fix-it hint, suggesting to add the
2885 missing token immediately after the *previous* token.
2886 This may move the primary location within richloc. */
2887 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2888 location_t prev_token_loc
2889 = cp_lexer_previous_token (parser
->lexer
)->location
;
2890 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2892 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2893 Attempt to consolidate diagnostics by printing it as a
2894 secondary range within the main diagnostic. */
2895 if (matching_location
!= UNKNOWN_LOCATION
)
2896 added_matching_location
2897 = richloc
.add_location_if_nearby (matching_location
);
2900 /* Actually emit the error. */
2901 c_parse_error (gmsgid
,
2902 /* Because c_parser_error does not understand
2903 CPP_KEYWORD, keywords are treated like
2905 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2906 token
->u
.value
, token
->flags
, &richloc
);
2908 if (missing_token_desc
!= RT_NONE
)
2910 /* If we weren't able to consolidate matching_location, then
2911 print it as a secondary diagnostic. */
2912 if (matching_location
!= UNKNOWN_LOCATION
2913 && !added_matching_location
)
2914 inform (matching_location
, "to match this %qs",
2915 get_matching_symbol (missing_token_desc
));
2919 /* If not parsing tentatively, issue a diagnostic of the form
2920 FILE:LINE: MESSAGE before TOKEN
2921 where TOKEN is the next token in the input stream. MESSAGE
2922 (specified by the caller) is usually of the form "expected
2926 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2928 if (!cp_parser_simulate_error (parser
))
2929 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2932 /* Issue an error about name-lookup failing. NAME is the
2933 IDENTIFIER_NODE DECL is the result of
2934 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2935 the thing that we hoped to find. */
2938 cp_parser_name_lookup_error (cp_parser
* parser
,
2941 name_lookup_error desired
,
2942 location_t location
)
2944 /* If name lookup completely failed, tell the user that NAME was not
2946 if (decl
== error_mark_node
)
2948 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2949 error_at (location
, "%<%E::%E%> has not been declared",
2950 parser
->scope
, name
);
2951 else if (parser
->scope
== global_namespace
)
2952 error_at (location
, "%<::%E%> has not been declared", name
);
2953 else if (parser
->object_scope
2954 && !CLASS_TYPE_P (parser
->object_scope
))
2955 error_at (location
, "request for member %qE in non-class type %qT",
2956 name
, parser
->object_scope
);
2957 else if (parser
->object_scope
)
2958 error_at (location
, "%<%T::%E%> has not been declared",
2959 parser
->object_scope
, name
);
2961 error_at (location
, "%qE has not been declared", name
);
2963 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2968 error_at (location
, "%<%E::%E%> is not a type",
2969 parser
->scope
, name
);
2972 error_at (location
, "%<%E::%E%> is not a class or namespace",
2973 parser
->scope
, name
);
2977 "%<%E::%E%> is not a class, namespace, or enumeration",
2978 parser
->scope
, name
);
2985 else if (parser
->scope
== global_namespace
)
2990 error_at (location
, "%<::%E%> is not a type", name
);
2993 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2997 "%<::%E%> is not a class, namespace, or enumeration",
3009 error_at (location
, "%qE is not a type", name
);
3012 error_at (location
, "%qE is not a class or namespace", name
);
3016 "%qE is not a class, namespace, or enumeration", name
);
3024 /* If we are parsing tentatively, remember that an error has occurred
3025 during this tentative parse. Returns true if the error was
3026 simulated; false if a message should be issued by the caller. */
3029 cp_parser_simulate_error (cp_parser
* parser
)
3031 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3033 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3039 /* This function is called when a type is defined. If type
3040 definitions are forbidden at this point, an error message is
3044 cp_parser_check_type_definition (cp_parser
* parser
)
3046 /* If types are forbidden here, issue a message. */
3047 if (parser
->type_definition_forbidden_message
)
3049 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3050 in the message need to be interpreted. */
3051 error (parser
->type_definition_forbidden_message
);
3057 /* This function is called when the DECLARATOR is processed. The TYPE
3058 was a type defined in the decl-specifiers. If it is invalid to
3059 define a type in the decl-specifiers for DECLARATOR, an error is
3060 issued. TYPE_LOCATION is the location of TYPE and is used
3061 for error reporting. */
3064 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3065 tree type
, location_t type_location
)
3067 /* [dcl.fct] forbids type definitions in return types.
3068 Unfortunately, it's not easy to know whether or not we are
3069 processing a return type until after the fact. */
3071 && (declarator
->kind
== cdk_pointer
3072 || declarator
->kind
== cdk_reference
3073 || declarator
->kind
== cdk_ptrmem
))
3074 declarator
= declarator
->declarator
;
3076 && declarator
->kind
== cdk_function
)
3078 error_at (type_location
,
3079 "new types may not be defined in a return type");
3080 inform (type_location
,
3081 "(perhaps a semicolon is missing after the definition of %qT)",
3086 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3087 "<" in any valid C++ program. If the next token is indeed "<",
3088 issue a message warning the user about what appears to be an
3089 invalid attempt to form a template-id. LOCATION is the location
3090 of the type-specifier (TYPE) */
3093 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3095 enum tag_types tag_type
,
3096 location_t location
)
3098 cp_token_position start
= 0;
3100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3102 if (TREE_CODE (type
) == TYPE_DECL
)
3103 type
= TREE_TYPE (type
);
3104 if (TYPE_P (type
) && !template_placeholder_p (type
))
3105 error_at (location
, "%qT is not a template", type
);
3106 else if (identifier_p (type
))
3108 if (tag_type
!= none_type
)
3109 error_at (location
, "%qE is not a class template", type
);
3111 error_at (location
, "%qE is not a template", type
);
3114 error_at (location
, "invalid template-id");
3115 /* Remember the location of the invalid "<". */
3116 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3117 start
= cp_lexer_token_position (parser
->lexer
, true);
3118 /* Consume the "<". */
3119 cp_lexer_consume_token (parser
->lexer
);
3120 /* Parse the template arguments. */
3121 cp_parser_enclosed_template_argument_list (parser
);
3122 /* Permanently remove the invalid template arguments so that
3123 this error message is not issued again. */
3125 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3129 /* If parsing an integral constant-expression, issue an error message
3130 about the fact that THING appeared and return true. Otherwise,
3131 return false. In either case, set
3132 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3135 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3136 non_integral_constant thing
)
3138 parser
->non_integral_constant_expression_p
= true;
3139 if (parser
->integral_constant_expression_p
)
3141 if (!parser
->allow_non_integral_constant_expression_p
)
3143 const char *msg
= NULL
;
3147 pedwarn (input_location
, OPT_Wpedantic
,
3148 "ISO C++ forbids using a floating-point literal "
3149 "in a constant-expression");
3152 error ("a cast to a type other than an integral or "
3153 "enumeration type cannot appear in a "
3154 "constant-expression");
3157 error ("%<typeid%> operator "
3158 "cannot appear in a constant-expression");
3161 error ("non-constant compound literals "
3162 "cannot appear in a constant-expression");
3165 error ("a function call "
3166 "cannot appear in a constant-expression");
3169 error ("an increment "
3170 "cannot appear in a constant-expression");
3173 error ("an decrement "
3174 "cannot appear in a constant-expression");
3177 error ("an array reference "
3178 "cannot appear in a constant-expression");
3180 case NIC_ADDR_LABEL
:
3181 error ("the address of a label "
3182 "cannot appear in a constant-expression");
3184 case NIC_OVERLOADED
:
3185 error ("calls to overloaded operators "
3186 "cannot appear in a constant-expression");
3188 case NIC_ASSIGNMENT
:
3189 error ("an assignment cannot appear in a constant-expression");
3192 error ("a comma operator "
3193 "cannot appear in a constant-expression");
3195 case NIC_CONSTRUCTOR
:
3196 error ("a call to a constructor "
3197 "cannot appear in a constant-expression");
3199 case NIC_TRANSACTION
:
3200 error ("a transaction expression "
3201 "cannot appear in a constant-expression");
3207 msg
= "__FUNCTION__";
3209 case NIC_PRETTY_FUNC
:
3210 msg
= "__PRETTY_FUNCTION__";
3230 case NIC_PREINCREMENT
:
3233 case NIC_PREDECREMENT
:
3246 error ("%qs cannot appear in a constant-expression", msg
);
3253 /* Emit a diagnostic for an invalid type name. This function commits
3254 to the current active tentative parse, if any. (Otherwise, the
3255 problematic construct might be encountered again later, resulting
3256 in duplicate error messages.) LOCATION is the location of ID. */
3259 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3260 location_t location
)
3262 tree decl
, ambiguous_decls
;
3263 cp_parser_commit_to_tentative_parse (parser
);
3264 /* Try to lookup the identifier. */
3265 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3266 /*is_template=*/false,
3267 /*is_namespace=*/false,
3268 /*check_dependency=*/true,
3269 &ambiguous_decls
, location
);
3270 if (ambiguous_decls
)
3271 /* If the lookup was ambiguous, an error will already have
3274 /* If the lookup found a template-name, it means that the user forgot
3275 to specify an argument list. Emit a useful error message. */
3276 if (DECL_TYPE_TEMPLATE_P (decl
))
3278 auto_diagnostic_group d
;
3280 "invalid use of template-name %qE without an argument list",
3282 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3283 inform (location
, "class template argument deduction is only available "
3284 "with -std=c++17 or -std=gnu++17");
3285 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3287 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3288 error_at (location
, "invalid use of destructor %qD as a type", id
);
3289 else if (TREE_CODE (decl
) == TYPE_DECL
)
3290 /* Something like 'unsigned A a;' */
3291 error_at (location
, "invalid combination of multiple type-specifiers");
3292 else if (!parser
->scope
)
3294 /* Issue an error message. */
3295 auto_diagnostic_group d
;
3297 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3298 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3299 if (const char *suggestion
= hint
.suggestion ())
3301 gcc_rich_location
richloc (location
);
3302 richloc
.add_fixit_replace (suggestion
);
3304 "%qE does not name a type; did you mean %qs?",
3308 error_at (location
, "%qE does not name a type", id
);
3309 /* If we're in a template class, it's possible that the user was
3310 referring to a type from a base class. For example:
3312 template <typename T> struct A { typedef T X; };
3313 template <typename T> struct B : public A<T> { X x; };
3315 The user should have said "typename A<T>::X". */
3316 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3317 inform (location
, "C++11 %<constexpr%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3320 inform (location
, "C++11 %<noexcept%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect
< cxx11
3323 && TREE_CODE (id
) == IDENTIFIER_NODE
3324 && id_equal (id
, "thread_local"))
3325 inform (location
, "C++11 %<thread_local%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3328 inform (location
, "%<concept%> only available with -fconcepts");
3329 else if (processing_template_decl
&& current_class_type
3330 && TYPE_BINFO (current_class_type
))
3334 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3338 tree base_type
= BINFO_TYPE (b
);
3339 if (CLASS_TYPE_P (base_type
)
3340 && dependent_type_p (base_type
))
3343 /* Go from a particular instantiation of the
3344 template (which will have an empty TYPE_FIELDs),
3345 to the main version. */
3346 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3347 for (field
= TYPE_FIELDS (base_type
);
3349 field
= DECL_CHAIN (field
))
3350 if (TREE_CODE (field
) == TYPE_DECL
3351 && DECL_NAME (field
) == id
)
3354 "(perhaps %<typename %T::%E%> was intended)",
3355 BINFO_TYPE (b
), id
);
3364 /* Here we diagnose qualified-ids where the scope is actually correct,
3365 but the identifier does not resolve to a valid type name. */
3366 else if (parser
->scope
!= error_mark_node
)
3368 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3370 auto_diagnostic_group d
;
3372 if (decl
== error_mark_node
)
3373 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3375 const char *suggestion
= hint
.suggestion ();
3376 gcc_rich_location
richloc (location_of (id
));
3378 richloc
.add_fixit_replace (suggestion
);
3379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3383 "%qE in namespace %qE does not name a template"
3384 " type; did you mean %qs?",
3385 id
, parser
->scope
, suggestion
);
3388 "%qE in namespace %qE does not name a template type",
3391 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3395 "%qE in namespace %qE does not name a template"
3396 " type; did you mean %qs?",
3397 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3400 "%qE in namespace %qE does not name a template"
3402 TREE_OPERAND (id
, 0), parser
->scope
);
3408 "%qE in namespace %qE does not name a type"
3409 "; did you mean %qs?",
3410 id
, parser
->scope
, suggestion
);
3413 "%qE in namespace %qE does not name a type",
3417 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3419 else if (CLASS_TYPE_P (parser
->scope
)
3420 && constructor_name_p (id
, parser
->scope
))
3423 auto_diagnostic_group d
;
3424 error_at (location
, "%<%T::%E%> names the constructor, not"
3425 " the type", parser
->scope
, id
);
3426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3427 error_at (location
, "and %qT has no template constructors",
3430 else if (TYPE_P (parser
->scope
)
3431 && dependent_scope_p (parser
->scope
))
3433 gcc_rich_location
richloc (location
);
3434 richloc
.add_fixit_insert_before ("typename ");
3435 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3437 "need %<typename%> before %<%T::%D::%E%> because "
3438 "%<%T::%D%> is a dependent scope",
3439 TYPE_CONTEXT (parser
->scope
),
3440 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3442 TYPE_CONTEXT (parser
->scope
),
3443 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3445 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3446 "%qT is a dependent scope",
3447 parser
->scope
, id
, parser
->scope
);
3449 else if (TYPE_P (parser
->scope
))
3451 auto_diagnostic_group d
;
3452 if (!COMPLETE_TYPE_P (parser
->scope
))
3453 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3455 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3456 error_at (location_of (id
),
3457 "%qE in %q#T does not name a template type",
3459 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3460 error_at (location_of (id
),
3461 "%qE in %q#T does not name a template type",
3462 TREE_OPERAND (id
, 0), parser
->scope
);
3464 error_at (location_of (id
),
3465 "%qE in %q#T does not name a type",
3468 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3475 /* Check for a common situation where a type-name should be present,
3476 but is not, and issue a sensible error message. Returns true if an
3477 invalid type-name was detected.
3479 The situation handled by this function are variable declarations of the
3480 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3481 Usually, `ID' should name a type, but if we got here it means that it
3482 does not. We try to emit the best possible error message depending on
3483 how exactly the id-expression looks like. */
3486 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3489 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3491 /* Avoid duplicate error about ambiguous lookup. */
3492 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3494 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3495 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3499 cp_parser_parse_tentatively (parser
);
3500 id
= cp_parser_id_expression (parser
,
3501 /*template_keyword_p=*/false,
3502 /*check_dependency_p=*/true,
3503 /*template_p=*/NULL
,
3504 /*declarator_p=*/false,
3505 /*optional_p=*/false);
3506 /* If the next token is a (, this is a function with no explicit return
3507 type, i.e. constructor, destructor or conversion op. */
3508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3509 || TREE_CODE (id
) == TYPE_DECL
)
3511 cp_parser_abort_tentative_parse (parser
);
3514 if (!cp_parser_parse_definitely (parser
))
3517 /* Emit a diagnostic for the invalid type. */
3518 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3520 /* If we aren't in the middle of a declarator (i.e. in a
3521 parameter-declaration-clause), skip to the end of the declaration;
3522 there's no point in trying to process it. */
3523 if (!parser
->in_declarator_p
)
3524 cp_parser_skip_to_end_of_block_or_statement (parser
);
3528 /* Consume tokens up to, and including, the next non-nested closing `)'.
3529 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3530 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3531 found an unnested token of that type. */
3534 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3539 unsigned paren_depth
= 0;
3540 unsigned brace_depth
= 0;
3541 unsigned square_depth
= 0;
3542 unsigned condop_depth
= 0;
3544 if (recovering
&& or_ttype
== CPP_EOF
3545 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3550 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3552 /* Have we found what we're looking for before the closing paren? */
3553 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3554 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3557 switch (token
->type
)
3560 case CPP_PRAGMA_EOL
:
3561 /* If we've run out of tokens, then there is no closing `)'. */
3564 /* This is good for lambda expression capture-lists. */
3565 case CPP_OPEN_SQUARE
:
3568 case CPP_CLOSE_SQUARE
:
3569 if (!square_depth
--)
3574 /* This matches the processing in skip_to_end_of_statement. */
3579 case CPP_OPEN_BRACE
:
3582 case CPP_CLOSE_BRACE
:
3587 case CPP_OPEN_PAREN
:
3592 case CPP_CLOSE_PAREN
:
3593 if (!brace_depth
&& !paren_depth
--)
3596 cp_lexer_consume_token (parser
->lexer
);
3602 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3607 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3615 /* Consume the token. */
3616 cp_lexer_consume_token (parser
->lexer
);
3620 /* Consume tokens up to, and including, the next non-nested closing `)'.
3621 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3622 are doing error recovery. Returns -1 if OR_COMMA is true and we
3623 found an unnested token of that type. */
3626 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3631 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3632 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3633 ttype
, consume_paren
);
3636 /* Consume tokens until we reach the end of the current statement.
3637 Normally, that will be just before consuming a `;'. However, if a
3638 non-nested `}' comes first, then we stop before consuming that. */
3641 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3643 unsigned nesting_depth
= 0;
3645 /* Unwind generic function template scope if necessary. */
3646 if (parser
->fully_implicit_function_template_p
)
3647 abort_fully_implicit_template (parser
);
3651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3653 switch (token
->type
)
3656 case CPP_PRAGMA_EOL
:
3657 /* If we've run out of tokens, stop. */
3661 /* If the next token is a `;', we have reached the end of the
3667 case CPP_CLOSE_BRACE
:
3668 /* If this is a non-nested '}', stop before consuming it.
3669 That way, when confronted with something like:
3673 we stop before consuming the closing '}', even though we
3674 have not yet reached a `;'. */
3675 if (nesting_depth
== 0)
3678 /* If it is the closing '}' for a block that we have
3679 scanned, stop -- but only after consuming the token.
3685 we will stop after the body of the erroneously declared
3686 function, but before consuming the following `typedef'
3688 if (--nesting_depth
== 0)
3690 cp_lexer_consume_token (parser
->lexer
);
3695 case CPP_OPEN_BRACE
:
3703 /* Consume the token. */
3704 cp_lexer_consume_token (parser
->lexer
);
3708 /* This function is called at the end of a statement or declaration.
3709 If the next token is a semicolon, it is consumed; otherwise, error
3710 recovery is attempted. */
3713 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3715 /* Look for the trailing `;'. */
3716 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3718 /* If there is additional (erroneous) input, skip to the end of
3720 cp_parser_skip_to_end_of_statement (parser
);
3721 /* If the next token is now a `;', consume it. */
3722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3723 cp_lexer_consume_token (parser
->lexer
);
3727 /* Skip tokens until we have consumed an entire block, or until we
3728 have consumed a non-nested `;'. */
3731 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3733 int nesting_depth
= 0;
3735 /* Unwind generic function template scope if necessary. */
3736 if (parser
->fully_implicit_function_template_p
)
3737 abort_fully_implicit_template (parser
);
3739 while (nesting_depth
>= 0)
3741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3743 switch (token
->type
)
3746 case CPP_PRAGMA_EOL
:
3747 /* If we've run out of tokens, stop. */
3751 /* Stop if this is an unnested ';'. */
3756 case CPP_CLOSE_BRACE
:
3757 /* Stop if this is an unnested '}', or closes the outermost
3760 if (nesting_depth
< 0)
3766 case CPP_OPEN_BRACE
:
3775 /* Consume the token. */
3776 cp_lexer_consume_token (parser
->lexer
);
3780 /* Skip tokens until a non-nested closing curly brace is the next
3781 token, or there are no more tokens. Return true in the first case,
3785 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3787 unsigned nesting_depth
= 0;
3791 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3793 switch (token
->type
)
3796 case CPP_PRAGMA_EOL
:
3797 /* If we've run out of tokens, stop. */
3800 case CPP_CLOSE_BRACE
:
3801 /* If the next token is a non-nested `}', then we have reached
3802 the end of the current block. */
3803 if (nesting_depth
-- == 0)
3807 case CPP_OPEN_BRACE
:
3808 /* If it the next token is a `{', then we are entering a new
3809 block. Consume the entire block. */
3817 /* Consume the token. */
3818 cp_lexer_consume_token (parser
->lexer
);
3822 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3823 parameter is the PRAGMA token, allowing us to purge the entire pragma
3827 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3831 parser
->lexer
->in_pragma
= false;
3834 token
= cp_lexer_consume_token (parser
->lexer
);
3835 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3837 /* Ensure that the pragma is not parsed again. */
3838 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3841 /* Require pragma end of line, resyncing with it as necessary. The
3842 arguments are as for cp_parser_skip_to_pragma_eol. */
3845 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3847 parser
->lexer
->in_pragma
= false;
3848 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3849 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3852 /* This is a simple wrapper around make_typename_type. When the id is
3853 an unresolved identifier node, we can provide a superior diagnostic
3854 using cp_parser_diagnose_invalid_type_name. */
3857 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3858 location_t id_location
)
3861 if (identifier_p (id
))
3863 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3864 /*complain=*/tf_none
);
3865 if (result
== error_mark_node
)
3866 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3869 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3872 /* This is a wrapper around the
3873 make_{pointer,ptrmem,reference}_declarator functions that decides
3874 which one to call based on the CODE and CLASS_TYPE arguments. The
3875 CODE argument should be one of the values returned by
3876 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3877 appertain to the pointer or reference. */
3879 static cp_declarator
*
3880 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3881 cp_cv_quals cv_qualifiers
,
3882 cp_declarator
*target
,
3885 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3886 return cp_error_declarator
;
3888 if (code
== INDIRECT_REF
)
3889 if (class_type
== NULL_TREE
)
3890 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3892 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3893 target
, attributes
);
3894 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3895 return make_reference_declarator (cv_qualifiers
, target
,
3897 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3898 return make_reference_declarator (cv_qualifiers
, target
,
3903 /* Create a new C++ parser. */
3906 cp_parser_new (void)
3912 /* cp_lexer_new_main is called before doing GC allocation because
3913 cp_lexer_new_main might load a PCH file. */
3914 lexer
= cp_lexer_new_main ();
3916 /* Initialize the binops_by_token so that we can get the tree
3917 directly from the token. */
3918 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3919 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3921 parser
= ggc_cleared_alloc
<cp_parser
> ();
3922 parser
->lexer
= lexer
;
3923 parser
->context
= cp_parser_context_new (NULL
);
3925 /* For now, we always accept GNU extensions. */
3926 parser
->allow_gnu_extensions_p
= 1;
3928 /* The `>' token is a greater-than operator, not the end of a
3930 parser
->greater_than_is_operator_p
= true;
3932 parser
->default_arg_ok_p
= true;
3934 /* We are not parsing a constant-expression. */
3935 parser
->integral_constant_expression_p
= false;
3936 parser
->allow_non_integral_constant_expression_p
= false;
3937 parser
->non_integral_constant_expression_p
= false;
3939 /* Local variable names are not forbidden. */
3940 parser
->local_variables_forbidden_p
= false;
3942 /* We are not processing an `extern "C"' declaration. */
3943 parser
->in_unbraced_linkage_specification_p
= false;
3945 /* We are not processing a declarator. */
3946 parser
->in_declarator_p
= false;
3948 /* We are not processing a template-argument-list. */
3949 parser
->in_template_argument_list_p
= false;
3951 /* We are not in an iteration statement. */
3952 parser
->in_statement
= 0;
3954 /* We are not in a switch statement. */
3955 parser
->in_switch_statement_p
= false;
3957 /* We are not parsing a type-id inside an expression. */
3958 parser
->in_type_id_in_expr_p
= false;
3960 /* String literals should be translated to the execution character set. */
3961 parser
->translate_strings_p
= true;
3963 /* We are not parsing a function body. */
3964 parser
->in_function_body
= false;
3966 /* We can correct until told otherwise. */
3967 parser
->colon_corrects_to_scope_p
= true;
3969 /* The unparsed function queue is empty. */
3970 push_unparsed_function_queues (parser
);
3972 /* There are no classes being defined. */
3973 parser
->num_classes_being_defined
= 0;
3975 /* No template parameters apply. */
3976 parser
->num_template_parameter_lists
= 0;
3978 /* Special parsing data structures. */
3979 parser
->omp_declare_simd
= NULL
;
3980 parser
->oacc_routine
= NULL
;
3982 /* Not declaring an implicit function template. */
3983 parser
->auto_is_implicit_function_template_parm_p
= false;
3984 parser
->fully_implicit_function_template_p
= false;
3985 parser
->implicit_template_parms
= 0;
3986 parser
->implicit_template_scope
= 0;
3988 /* Allow constrained-type-specifiers. */
3989 parser
->prevent_constrained_type_specifiers
= 0;
3991 /* We haven't yet seen an 'extern "C"'. */
3992 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3997 /* Create a cp_lexer structure which will emit the tokens in CACHE
3998 and push it onto the parser's lexer stack. This is used for delayed
3999 parsing of in-class method bodies and default arguments, and should
4000 not be confused with tentative parsing. */
4002 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4004 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4005 lexer
->next
= parser
->lexer
;
4006 parser
->lexer
= lexer
;
4008 /* Move the current source position to that of the first token in the
4010 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4013 /* Pop the top lexer off the parser stack. This is never used for the
4014 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4016 cp_parser_pop_lexer (cp_parser
*parser
)
4018 cp_lexer
*lexer
= parser
->lexer
;
4019 parser
->lexer
= lexer
->next
;
4020 cp_lexer_destroy (lexer
);
4022 /* Put the current source position back where it was before this
4023 lexer was pushed. */
4024 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4027 /* Lexical conventions [gram.lex] */
4029 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4033 cp_parser_identifier (cp_parser
* parser
)
4037 /* Look for the identifier. */
4038 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4039 /* Return the value. */
4041 return cp_expr (token
->u
.value
, token
->location
);
4043 return error_mark_node
;
4046 /* Parse a sequence of adjacent string constants. Returns a
4047 TREE_STRING representing the combined, nul-terminated string
4048 constant. If TRANSLATE is true, translate the string to the
4049 execution character set. If WIDE_OK is true, a wide string is
4052 C++98 [lex.string] says that if a narrow string literal token is
4053 adjacent to a wide string literal token, the behavior is undefined.
4054 However, C99 6.4.5p4 says that this results in a wide string literal.
4055 We follow C99 here, for consistency with the C front end.
4057 This code is largely lifted from lex_string() in c-lex.c.
4059 FUTURE: ObjC++ will need to handle @-strings here. */
4061 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4062 bool lookup_udlit
= true)
4066 struct obstack str_ob
;
4067 struct obstack loc_ob
;
4068 cpp_string str
, istr
, *strs
;
4070 enum cpp_ttype type
, curr_type
;
4071 int have_suffix_p
= 0;
4073 tree suffix_id
= NULL_TREE
;
4074 bool curr_tok_is_userdef_p
= false;
4076 tok
= cp_lexer_peek_token (parser
->lexer
);
4077 if (!cp_parser_is_string_literal (tok
))
4079 cp_parser_error (parser
, "expected string-literal");
4080 return error_mark_node
;
4083 location_t loc
= tok
->location
;
4085 if (cpp_userdef_string_p (tok
->type
))
4087 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4088 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4089 curr_tok_is_userdef_p
= true;
4093 string_tree
= tok
->u
.value
;
4094 curr_type
= tok
->type
;
4098 /* Try to avoid the overhead of creating and destroying an obstack
4099 for the common case of just one string. */
4100 if (!cp_parser_is_string_literal
4101 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4103 cp_lexer_consume_token (parser
->lexer
);
4105 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4106 str
.len
= TREE_STRING_LENGTH (string_tree
);
4109 if (curr_tok_is_userdef_p
)
4111 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4113 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4116 curr_type
= tok
->type
;
4122 location_t last_tok_loc
= tok
->location
;
4123 gcc_obstack_init (&str_ob
);
4124 gcc_obstack_init (&loc_ob
);
4129 cp_lexer_consume_token (parser
->lexer
);
4131 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4132 str
.len
= TREE_STRING_LENGTH (string_tree
);
4134 if (curr_tok_is_userdef_p
)
4136 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4137 if (have_suffix_p
== 0)
4139 suffix_id
= curr_suffix_id
;
4142 else if (have_suffix_p
== 1
4143 && curr_suffix_id
!= suffix_id
)
4145 error ("inconsistent user-defined literal suffixes"
4146 " %qD and %qD in string literal",
4147 suffix_id
, curr_suffix_id
);
4150 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4153 curr_type
= tok
->type
;
4155 if (type
!= curr_type
)
4157 if (type
== CPP_STRING
)
4159 else if (curr_type
!= CPP_STRING
)
4161 rich_location
rich_loc (line_table
, tok
->location
);
4162 rich_loc
.add_range (last_tok_loc
);
4163 error_at (&rich_loc
,
4164 "unsupported non-standard concatenation "
4165 "of string literals");
4169 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4170 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4172 last_tok_loc
= tok
->location
;
4174 tok
= cp_lexer_peek_token (parser
->lexer
);
4175 if (cpp_userdef_string_p (tok
->type
))
4177 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4178 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4179 curr_tok_is_userdef_p
= true;
4183 string_tree
= tok
->u
.value
;
4184 curr_type
= tok
->type
;
4185 curr_tok_is_userdef_p
= false;
4188 while (cp_parser_is_string_literal (tok
));
4190 /* A string literal built by concatenation has its caret=start at
4191 the start of the initial string, and its finish at the finish of
4192 the final string literal. */
4193 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4195 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4198 if (type
!= CPP_STRING
&& !wide_ok
)
4200 cp_parser_error (parser
, "a wide string is invalid in this context");
4204 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4205 (parse_in
, strs
, count
, &istr
, type
))
4207 value
= build_string (istr
.len
, (const char *)istr
.text
);
4208 free (CONST_CAST (unsigned char *, istr
.text
));
4211 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4212 gcc_assert (g_string_concat_db
);
4213 g_string_concat_db
->record_string_concatenation (count
, locs
);
4220 case CPP_UTF8STRING
:
4221 TREE_TYPE (value
) = char_array_type_node
;
4224 TREE_TYPE (value
) = char16_array_type_node
;
4227 TREE_TYPE (value
) = char32_array_type_node
;
4230 TREE_TYPE (value
) = wchar_array_type_node
;
4234 value
= fix_string_type (value
);
4238 tree literal
= build_userdef_literal (suffix_id
, value
,
4239 OT_NONE
, NULL_TREE
);
4241 value
= cp_parser_userdef_string_literal (literal
);
4247 /* cpp_interpret_string has issued an error. */
4248 value
= error_mark_node
;
4252 obstack_free (&str_ob
, 0);
4253 obstack_free (&loc_ob
, 0);
4256 return cp_expr (value
, loc
);
4259 /* Look up a literal operator with the name and the exact arguments. */
4262 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4264 tree decl
= lookup_name (name
);
4265 if (!decl
|| !is_overloaded_fn (decl
))
4266 return error_mark_node
;
4268 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4272 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4278 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4279 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4281 tree tparm
= TREE_VALUE (parmtypes
);
4282 tree targ
= TREE_TYPE ((*args
)[ix
]);
4283 bool ptr
= TYPE_PTR_P (tparm
);
4284 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4285 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4287 || !same_type_p (TREE_TYPE (tparm
),
4293 && ix
== vec_safe_length (args
)
4294 /* May be this should be sufficient_parms_p instead,
4295 depending on how exactly should user-defined literals
4296 work in presence of default arguments on the literal
4297 operator parameters. */
4298 && parmtypes
== void_list_node
)
4303 return error_mark_node
;
4306 /* Parse a user-defined char constant. Returns a call to a user-defined
4307 literal operator taking the character as an argument. */
4310 cp_parser_userdef_char_literal (cp_parser
*parser
)
4312 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4313 tree literal
= token
->u
.value
;
4314 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4315 tree value
= USERDEF_LITERAL_VALUE (literal
);
4316 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4319 /* Build up a call to the user-defined operator */
4320 /* Lookup the name we got back from the id-expression. */
4321 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4322 vec_safe_push (args
, value
);
4323 decl
= lookup_literal_operator (name
, args
);
4324 if (!decl
|| decl
== error_mark_node
)
4326 error ("unable to find character literal operator %qD with %qT argument",
4327 name
, TREE_TYPE (value
));
4328 release_tree_vector (args
);
4329 return error_mark_node
;
4331 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4332 release_tree_vector (args
);
4336 /* A subroutine of cp_parser_userdef_numeric_literal to
4337 create a char... template parameter pack from a string node. */
4340 make_char_string_pack (tree value
)
4343 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4344 const char *str
= TREE_STRING_POINTER (value
);
4345 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4346 tree argvec
= make_tree_vec (1);
4348 /* Fill in CHARVEC with all of the parameters. */
4349 charvec
= make_tree_vec (len
);
4350 for (i
= 0; i
< len
; ++i
)
4352 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4353 cpp_string in
= { 3, s
};
4354 cpp_string out
= { 0, 0 };
4355 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4357 gcc_assert (out
.len
== 2);
4358 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4362 /* Build the argument packs. */
4363 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4365 TREE_VEC_ELT (argvec
, 0) = argpack
;
4370 /* A subroutine of cp_parser_userdef_numeric_literal to
4371 create a char... template parameter pack from a string node. */
4374 make_string_pack (tree value
)
4377 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4378 const unsigned char *str
4379 = (const unsigned char *) TREE_STRING_POINTER (value
);
4380 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4381 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4382 tree argvec
= make_tree_vec (2);
4384 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4385 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4387 /* First template parm is character type. */
4388 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4390 /* Fill in CHARVEC with all of the parameters. */
4391 charvec
= make_tree_vec (len
);
4392 for (int i
= 0; i
< len
; ++i
)
4393 TREE_VEC_ELT (charvec
, i
)
4394 = double_int_to_tree (str_char_type_node
,
4395 double_int::from_buffer (str
+ i
* sz
, sz
));
4397 /* Build the argument packs. */
4398 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4400 TREE_VEC_ELT (argvec
, 1) = argpack
;
4405 /* Parse a user-defined numeric constant. returns a call to a user-defined
4406 literal operator. */
4409 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4411 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4412 tree literal
= token
->u
.value
;
4413 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4414 tree value
= USERDEF_LITERAL_VALUE (literal
);
4415 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4416 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4417 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4419 vec
<tree
, va_gc
> *args
;
4421 /* Look for a literal operator taking the exact type of numeric argument
4422 as the literal value. */
4423 args
= make_tree_vector ();
4424 vec_safe_push (args
, value
);
4425 decl
= lookup_literal_operator (name
, args
);
4426 if (decl
&& decl
!= error_mark_node
)
4428 result
= finish_call_expr (decl
, &args
, false, true,
4429 tf_warning_or_error
);
4431 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4433 warning_at (token
->location
, OPT_Woverflow
,
4434 "integer literal exceeds range of %qT type",
4435 long_long_unsigned_type_node
);
4440 warning_at (token
->location
, OPT_Woverflow
,
4441 "floating literal exceeds range of %qT type",
4442 long_double_type_node
);
4443 else if (overflow
< 0)
4444 warning_at (token
->location
, OPT_Woverflow
,
4445 "floating literal truncated to zero");
4448 release_tree_vector (args
);
4451 release_tree_vector (args
);
4453 /* If the numeric argument didn't work, look for a raw literal
4454 operator taking a const char* argument consisting of the number
4455 in string format. */
4456 args
= make_tree_vector ();
4457 vec_safe_push (args
, num_string
);
4458 decl
= lookup_literal_operator (name
, args
);
4459 if (decl
&& decl
!= error_mark_node
)
4461 result
= finish_call_expr (decl
, &args
, false, true,
4462 tf_warning_or_error
);
4463 release_tree_vector (args
);
4466 release_tree_vector (args
);
4468 /* If the raw literal didn't work, look for a non-type template
4469 function with parameter pack char.... Call the function with
4470 template parameter characters representing the number. */
4471 args
= make_tree_vector ();
4472 decl
= lookup_literal_operator (name
, args
);
4473 if (decl
&& decl
!= error_mark_node
)
4475 tree tmpl_args
= make_char_string_pack (num_string
);
4476 if (tmpl_args
== NULL_TREE
)
4478 error ("failed to translate literal to execution character set %qT",
4480 return error_mark_node
;
4482 decl
= lookup_template_function (decl
, tmpl_args
);
4483 result
= finish_call_expr (decl
, &args
, false, true,
4484 tf_warning_or_error
);
4485 release_tree_vector (args
);
4489 release_tree_vector (args
);
4491 /* In C++14 the standard library defines complex number suffixes that
4492 conflict with GNU extensions. Prefer them if <complex> is #included. */
4493 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4494 bool i14
= (cxx_dialect
> cxx11
4495 && (id_equal (suffix_id
, "i")
4496 || id_equal (suffix_id
, "if")
4497 || id_equal (suffix_id
, "il")));
4498 diagnostic_t kind
= DK_ERROR
;
4503 tree cxlit
= lookup_qualified_name (std_node
,
4504 get_identifier ("complex_literals"),
4506 if (cxlit
== error_mark_node
)
4508 /* No <complex>, so pedwarn and use GNU semantics. */
4510 opt
= OPT_Wpedantic
;
4515 = emit_diagnostic (kind
, input_location
, opt
,
4516 "unable to find numeric literal operator %qD", name
);
4519 /* Don't inform either. */;
4522 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4523 "(from <complex>) to enable the C++14 user-defined literal "
4526 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4527 "GNU built-in suffix");
4530 inform (token
->location
, "use -fext-numeric-literals "
4531 "to enable more built-in suffixes");
4533 if (kind
== DK_ERROR
)
4534 value
= error_mark_node
;
4537 /* Use the built-in semantics. */
4539 if (id_equal (suffix_id
, "i"))
4541 if (TREE_CODE (value
) == INTEGER_CST
)
4542 type
= integer_type_node
;
4544 type
= double_type_node
;
4546 else if (id_equal (suffix_id
, "if"))
4547 type
= float_type_node
;
4548 else /* if (id_equal (suffix_id, "il")) */
4549 type
= long_double_type_node
;
4551 value
= build_complex (build_complex_type (type
),
4552 fold_convert (type
, integer_zero_node
),
4553 fold_convert (type
, value
));
4556 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4557 /* Avoid repeated diagnostics. */
4558 token
->u
.value
= value
;
4562 /* Parse a user-defined string constant. Returns a call to a user-defined
4563 literal operator taking a character pointer and the length of the string
4567 cp_parser_userdef_string_literal (tree literal
)
4569 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4570 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4571 tree value
= USERDEF_LITERAL_VALUE (literal
);
4572 int len
= TREE_STRING_LENGTH (value
)
4573 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4576 /* Build up a call to the user-defined operator. */
4577 /* Lookup the name we got back from the id-expression. */
4578 releasing_vec rargs
;
4579 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4580 vec_safe_push (args
, value
);
4581 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4582 decl
= lookup_literal_operator (name
, args
);
4584 if (decl
&& decl
!= error_mark_node
)
4585 return finish_call_expr (decl
, &args
, false, true,
4586 tf_warning_or_error
);
4588 /* Look for a suitable template function, either (C++20) with a single
4589 parameter of class type, or (N3599) with typename parameter CharT and
4590 parameter pack CharT... */
4592 decl
= lookup_literal_operator (name
, args
);
4593 if (decl
&& decl
!= error_mark_node
)
4595 /* Use resolve_nondeduced_context to try to choose one form of template
4597 tree tmpl_args
= make_tree_vec (1);
4598 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4599 decl
= lookup_template_function (decl
, tmpl_args
);
4600 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4605 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4606 res
= resolve_nondeduced_context (decl
, tf_none
);
4610 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4611 TREE_OPERAND (decl
, 1) = tmpl_args
;
4612 return finish_call_expr (decl
, &args
, false, true,
4613 tf_warning_or_error
);
4616 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4617 name
, TREE_TYPE (value
), size_type_node
);
4618 return error_mark_node
;
4622 /* Basic concepts [gram.basic] */
4624 /* Parse a translation-unit.
4627 declaration-seq [opt] */
4630 cp_parser_translation_unit (cp_parser
* parser
)
4632 gcc_checking_assert (!cp_error_declarator
);
4634 /* Create the declarator obstack. */
4635 gcc_obstack_init (&declarator_obstack
);
4636 /* Create the error declarator. */
4637 cp_error_declarator
= make_declarator (cdk_error
);
4638 /* Create the empty parameter list. */
4639 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4641 /* Remember where the base of the declarator obstack lies. */
4642 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4644 bool implicit_extern_c
= false;
4648 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4650 /* If we're entering or exiting a region that's implicitly
4651 extern "C", modify the lang context appropriately. */
4652 if (implicit_extern_c
4653 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4655 implicit_extern_c
= !implicit_extern_c
;
4656 if (implicit_extern_c
)
4657 push_lang_context (lang_name_c
);
4659 pop_lang_context ();
4662 if (token
->type
== CPP_EOF
)
4665 if (token
->type
== CPP_CLOSE_BRACE
)
4667 cp_parser_error (parser
, "expected declaration");
4668 cp_lexer_consume_token (parser
->lexer
);
4669 /* If the next token is now a `;', consume it. */
4670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4671 cp_lexer_consume_token (parser
->lexer
);
4674 cp_parser_toplevel_declaration (parser
);
4677 /* Get rid of the token array; we don't need it any more. */
4678 cp_lexer_destroy (parser
->lexer
);
4679 parser
->lexer
= NULL
;
4681 /* The EOF should have reset this. */
4682 gcc_checking_assert (!implicit_extern_c
);
4684 /* Make sure the declarator obstack was fully cleaned up. */
4685 gcc_assert (obstack_next_free (&declarator_obstack
)
4686 == declarator_obstack_base
);
4689 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4690 decltype context. */
4692 static inline tsubst_flags_t
4693 complain_flags (bool decltype_p
)
4695 tsubst_flags_t complain
= tf_warning_or_error
;
4697 complain
|= tf_decltype
;
4701 /* We're about to parse a collection of statements. If we're currently
4702 parsing tentatively, set up a firewall so that any nested
4703 cp_parser_commit_to_tentative_parse won't affect the current context. */
4705 static cp_token_position
4706 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4711 cp_parser_parse_tentatively (parser
);
4712 cp_parser_commit_to_topmost_tentative_parse (parser
);
4713 return cp_lexer_token_position (parser
->lexer
, false);
4716 /* We've finished parsing the collection of statements. Wrap up the
4717 firewall and replace the relevant tokens with the parsed form. */
4720 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4726 /* Finish the firewall level. */
4727 cp_parser_parse_definitely (parser
);
4728 /* And remember the result of the parse for when we try again. */
4729 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4730 token
->type
= CPP_PREPARSED_EXPR
;
4731 token
->u
.value
= expr
;
4732 token
->keyword
= RID_MAX
;
4733 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4736 /* Like the above functions, but let the user modify the tokens. Used by
4737 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4738 later parses, so it makes sense to localize the effects of
4739 cp_parser_commit_to_tentative_parse. */
4741 struct tentative_firewall
4746 tentative_firewall (cp_parser
*p
): parser(p
)
4748 /* If we're currently parsing tentatively, start a committed level as a
4749 firewall and then an inner tentative parse. */
4750 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4752 cp_parser_parse_tentatively (parser
);
4753 cp_parser_commit_to_topmost_tentative_parse (parser
);
4754 cp_parser_parse_tentatively (parser
);
4758 ~tentative_firewall()
4762 /* Finish the inner tentative parse and the firewall, propagating any
4763 uncommitted error state to the outer tentative parse. */
4764 bool err
= cp_parser_error_occurred (parser
);
4765 cp_parser_parse_definitely (parser
);
4766 cp_parser_parse_definitely (parser
);
4768 cp_parser_simulate_error (parser
);
4773 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4774 This class is for tracking such a matching pair of symbols.
4775 In particular, it tracks the location of the first token,
4776 so that if the second token is missing, we can highlight the
4777 location of the first token when notifying the user about the
4780 template <typename traits_t
>
4784 /* token_pair's ctor. */
4785 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4787 /* If the next token is the opening symbol for this pair, consume it and
4789 Otherwise, issue an error and return false.
4790 In either case, record the location of the opening token. */
4792 bool require_open (cp_parser
*parser
)
4794 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4795 return cp_parser_require (parser
, traits_t::open_token_type
,
4796 traits_t::required_token_open
);
4799 /* Consume the next token from PARSER, recording its location as
4800 that of the opening token within the pair. */
4802 cp_token
* consume_open (cp_parser
*parser
)
4804 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4805 gcc_assert (tok
->type
== traits_t::open_token_type
);
4806 m_open_loc
= tok
->location
;
4810 /* If the next token is the closing symbol for this pair, consume it
4812 Otherwise, issue an error, highlighting the location of the
4813 corresponding opening token, and return NULL. */
4815 cp_token
*require_close (cp_parser
*parser
) const
4817 return cp_parser_require (parser
, traits_t::close_token_type
,
4818 traits_t::required_token_close
,
4823 location_t m_open_loc
;
4826 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4828 struct matching_paren_traits
4830 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4831 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4832 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4833 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4836 /* "matching_parens" is a token_pair<T> class for tracking matching
4837 pairs of parentheses. */
4839 typedef token_pair
<matching_paren_traits
> matching_parens
;
4841 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4843 struct matching_brace_traits
4845 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4846 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4847 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4848 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4851 /* "matching_braces" is a token_pair<T> class for tracking matching
4854 typedef token_pair
<matching_brace_traits
> matching_braces
;
4857 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4858 enclosing parentheses. */
4861 cp_parser_statement_expr (cp_parser
*parser
)
4863 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4865 /* Consume the '('. */
4866 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4867 matching_parens parens
;
4868 parens
.consume_open (parser
);
4869 /* Start the statement-expression. */
4870 tree expr
= begin_stmt_expr ();
4871 /* Parse the compound-statement. */
4872 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4874 expr
= finish_stmt_expr (expr
, false);
4875 /* Consume the ')'. */
4876 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4877 if (!parens
.require_close (parser
))
4878 cp_parser_skip_to_end_of_statement (parser
);
4880 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4881 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4882 return cp_expr (expr
, combined_loc
);
4885 /* Expressions [gram.expr] */
4887 /* Parse a fold-operator.
4890 - * / % ^ & | = < > << >>
4891 = -= *= /= %= ^= &= |= <<= >>=
4892 == != <= >= && || , .* ->*
4894 This returns the tree code corresponding to the matched operator
4895 as an int. When the current token matches a compound assignment
4896 opertor, the resulting tree code is the negative value of the
4897 non-assignment operator. */
4900 cp_parser_fold_operator (cp_token
*token
)
4902 switch (token
->type
)
4904 case CPP_PLUS
: return PLUS_EXPR
;
4905 case CPP_MINUS
: return MINUS_EXPR
;
4906 case CPP_MULT
: return MULT_EXPR
;
4907 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4908 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4909 case CPP_XOR
: return BIT_XOR_EXPR
;
4910 case CPP_AND
: return BIT_AND_EXPR
;
4911 case CPP_OR
: return BIT_IOR_EXPR
;
4912 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4913 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4915 case CPP_EQ
: return -NOP_EXPR
;
4916 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4917 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4918 case CPP_MULT_EQ
: return -MULT_EXPR
;
4919 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4920 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4921 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4922 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4923 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4924 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4925 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4927 case CPP_EQ_EQ
: return EQ_EXPR
;
4928 case CPP_NOT_EQ
: return NE_EXPR
;
4929 case CPP_LESS
: return LT_EXPR
;
4930 case CPP_GREATER
: return GT_EXPR
;
4931 case CPP_LESS_EQ
: return LE_EXPR
;
4932 case CPP_GREATER_EQ
: return GE_EXPR
;
4934 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4935 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4937 case CPP_COMMA
: return COMPOUND_EXPR
;
4939 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4940 case CPP_DEREF_STAR
: return MEMBER_REF
;
4942 default: return ERROR_MARK
;
4946 /* Returns true if CODE indicates a binary expression, which is not allowed in
4947 the LHS of a fold-expression. More codes will need to be added to use this
4948 function in other contexts. */
4951 is_binary_op (tree_code code
)
4956 case POINTER_PLUS_EXPR
:
4959 case TRUNC_DIV_EXPR
:
4960 case TRUNC_MOD_EXPR
:
4976 case TRUTH_ANDIF_EXPR
:
4977 case TRUTH_ORIF_EXPR
:
4990 /* If the next token is a suitable fold operator, consume it and return as
4991 the function above. */
4994 cp_parser_fold_operator (cp_parser
*parser
)
4996 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4997 int code
= cp_parser_fold_operator (token
);
4998 if (code
!= ERROR_MARK
)
4999 cp_lexer_consume_token (parser
->lexer
);
5003 /* Parse a fold-expression.
5006 ( ... folding-operator cast-expression)
5007 ( cast-expression folding-operator ... )
5008 ( cast-expression folding operator ... folding-operator cast-expression)
5010 Note that the '(' and ')' are matched in primary expression. */
5013 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5020 cp_lexer_consume_token (parser
->lexer
);
5021 int op
= cp_parser_fold_operator (parser
);
5022 if (op
== ERROR_MARK
)
5024 cp_parser_error (parser
, "expected binary operator");
5025 return error_mark_node
;
5028 tree expr
= cp_parser_cast_expression (parser
, false, false,
5030 if (expr
== error_mark_node
)
5031 return error_mark_node
;
5032 return finish_left_unary_fold_expr (expr
, op
);
5035 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5036 int op
= cp_parser_fold_operator (parser
);
5037 if (op
== ERROR_MARK
)
5039 cp_parser_error (parser
, "expected binary operator");
5040 return error_mark_node
;
5043 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5045 cp_parser_error (parser
, "expected ...");
5046 return error_mark_node
;
5048 cp_lexer_consume_token (parser
->lexer
);
5050 /* The operands of a fold-expression are cast-expressions, so binary or
5051 conditional expressions are not allowed. We check this here to avoid
5052 tentative parsing. */
5053 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5054 /* OK, the expression was parenthesized. */;
5055 else if (is_binary_op (TREE_CODE (expr1
)))
5056 error_at (location_of (expr1
),
5057 "binary expression in operand of fold-expression");
5058 else if (TREE_CODE (expr1
) == COND_EXPR
5059 || (REFERENCE_REF_P (expr1
)
5060 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5061 error_at (location_of (expr1
),
5062 "conditional expression in operand of fold-expression");
5065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5066 return finish_right_unary_fold_expr (expr1
, op
);
5068 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5070 cp_parser_error (parser
, "mismatched operator in fold-expression");
5071 return error_mark_node
;
5073 cp_lexer_consume_token (parser
->lexer
);
5075 // Binary left or right fold.
5076 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5077 if (expr2
== error_mark_node
)
5078 return error_mark_node
;
5079 return finish_binary_fold_expr (expr1
, expr2
, op
);
5082 /* Parse a primary-expression.
5089 lambda-expression (C++11)
5094 ( compound-statement )
5095 __builtin_va_arg ( assignment-expression , type-id )
5096 __builtin_offsetof ( type-id , offsetof-expression )
5099 __has_nothrow_assign ( type-id )
5100 __has_nothrow_constructor ( type-id )
5101 __has_nothrow_copy ( type-id )
5102 __has_trivial_assign ( type-id )
5103 __has_trivial_constructor ( type-id )
5104 __has_trivial_copy ( type-id )
5105 __has_trivial_destructor ( type-id )
5106 __has_virtual_destructor ( type-id )
5107 __is_abstract ( type-id )
5108 __is_base_of ( type-id , type-id )
5109 __is_class ( type-id )
5110 __is_empty ( type-id )
5111 __is_enum ( type-id )
5112 __is_final ( type-id )
5113 __is_literal_type ( type-id )
5114 __is_pod ( type-id )
5115 __is_polymorphic ( type-id )
5116 __is_std_layout ( type-id )
5117 __is_trivial ( type-id )
5118 __is_union ( type-id )
5120 Objective-C++ Extension:
5128 ADDRESS_P is true iff this expression was immediately preceded by
5129 "&" and therefore might denote a pointer-to-member. CAST_P is true
5130 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5131 true iff this expression is a template argument.
5133 Returns a representation of the expression. Upon return, *IDK
5134 indicates what kind of id-expression (if any) was present. */
5137 cp_parser_primary_expression (cp_parser
*parser
,
5140 bool template_arg_p
,
5144 cp_token
*token
= NULL
;
5146 /* Assume the primary expression is not an id-expression. */
5147 *idk
= CP_ID_KIND_NONE
;
5149 /* Peek at the next token. */
5150 token
= cp_lexer_peek_token (parser
->lexer
);
5151 switch ((int) token
->type
)
5160 user-defined-literal */
5167 case CPP_PREPARSED_EXPR
:
5168 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5169 return cp_parser_userdef_numeric_literal (parser
);
5170 token
= cp_lexer_consume_token (parser
->lexer
);
5171 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5173 error_at (token
->location
,
5174 "fixed-point types not supported in C++");
5175 return error_mark_node
;
5177 /* Floating-point literals are only allowed in an integral
5178 constant expression if they are cast to an integral or
5179 enumeration type. */
5180 if (TREE_CODE (token
->u
.value
) == REAL_CST
5181 && parser
->integral_constant_expression_p
5184 /* CAST_P will be set even in invalid code like "int(2.7 +
5185 ...)". Therefore, we have to check that the next token
5186 is sure to end the cast. */
5189 cp_token
*next_token
;
5191 next_token
= cp_lexer_peek_token (parser
->lexer
);
5192 if (/* The comma at the end of an
5193 enumerator-definition. */
5194 next_token
->type
!= CPP_COMMA
5195 /* The curly brace at the end of an enum-specifier. */
5196 && next_token
->type
!= CPP_CLOSE_BRACE
5197 /* The end of a statement. */
5198 && next_token
->type
!= CPP_SEMICOLON
5199 /* The end of the cast-expression. */
5200 && next_token
->type
!= CPP_CLOSE_PAREN
5201 /* The end of an array bound. */
5202 && next_token
->type
!= CPP_CLOSE_SQUARE
5203 /* The closing ">" in a template-argument-list. */
5204 && (next_token
->type
!= CPP_GREATER
5205 || parser
->greater_than_is_operator_p
)
5206 /* C++0x only: A ">>" treated like two ">" tokens,
5207 in a template-argument-list. */
5208 && (next_token
->type
!= CPP_RSHIFT
5209 || (cxx_dialect
== cxx98
)
5210 || parser
->greater_than_is_operator_p
))
5214 /* If we are within a cast, then the constraint that the
5215 cast is to an integral or enumeration type will be
5216 checked at that point. If we are not within a cast, then
5217 this code is invalid. */
5219 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5221 return cp_expr (token
->u
.value
, token
->location
);
5223 case CPP_CHAR_USERDEF
:
5224 case CPP_CHAR16_USERDEF
:
5225 case CPP_CHAR32_USERDEF
:
5226 case CPP_WCHAR_USERDEF
:
5227 case CPP_UTF8CHAR_USERDEF
:
5228 return cp_parser_userdef_char_literal (parser
);
5234 case CPP_UTF8STRING
:
5235 case CPP_STRING_USERDEF
:
5236 case CPP_STRING16_USERDEF
:
5237 case CPP_STRING32_USERDEF
:
5238 case CPP_WSTRING_USERDEF
:
5239 case CPP_UTF8STRING_USERDEF
:
5240 /* ??? Should wide strings be allowed when parser->translate_strings_p
5241 is false (i.e. in attributes)? If not, we can kill the third
5242 argument to cp_parser_string_literal. */
5243 return cp_parser_string_literal (parser
,
5244 parser
->translate_strings_p
,
5247 case CPP_OPEN_PAREN
:
5248 /* If we see `( { ' then we are looking at the beginning of
5249 a GNU statement-expression. */
5250 if (cp_parser_allow_gnu_extensions_p (parser
)
5251 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5253 /* Statement-expressions are not allowed by the standard. */
5254 pedwarn (token
->location
, OPT_Wpedantic
,
5255 "ISO C++ forbids braced-groups within expressions");
5257 /* And they're not allowed outside of a function-body; you
5258 cannot, for example, write:
5260 int i = ({ int j = 3; j + 1; });
5262 at class or namespace scope. */
5263 if (!parser
->in_function_body
5264 || parser
->in_template_argument_list_p
)
5266 error_at (token
->location
,
5267 "statement-expressions are not allowed outside "
5268 "functions nor in template-argument lists");
5269 cp_parser_skip_to_end_of_block_or_statement (parser
);
5270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5271 cp_lexer_consume_token (parser
->lexer
);
5272 return error_mark_node
;
5275 return cp_parser_statement_expr (parser
);
5277 /* Otherwise it's a normal parenthesized expression. */
5280 bool saved_greater_than_is_operator_p
;
5282 location_t open_paren_loc
= token
->location
;
5284 /* Consume the `('. */
5285 matching_parens parens
;
5286 parens
.consume_open (parser
);
5287 /* Within a parenthesized expression, a `>' token is always
5288 the greater-than operator. */
5289 saved_greater_than_is_operator_p
5290 = parser
->greater_than_is_operator_p
;
5291 parser
->greater_than_is_operator_p
= true;
5293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5294 /* Left fold expression. */
5297 /* Parse the parenthesized expression. */
5298 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5300 token
= cp_lexer_peek_token (parser
->lexer
);
5301 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5303 expr
= cp_parser_fold_expression (parser
, expr
);
5304 if (expr
!= error_mark_node
5305 && cxx_dialect
< cxx17
5306 && !in_system_header_at (input_location
))
5307 pedwarn (input_location
, 0, "fold-expressions only available "
5308 "with -std=c++17 or -std=gnu++17");
5311 /* Let the front end know that this expression was
5312 enclosed in parentheses. This matters in case, for
5313 example, the expression is of the form `A::B', since
5314 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5316 expr
= finish_parenthesized_expr (expr
);
5318 /* DR 705: Wrapping an unqualified name in parentheses
5319 suppresses arg-dependent lookup. We want to pass back
5320 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5321 (c++/37862), but none of the others. */
5322 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5323 *idk
= CP_ID_KIND_NONE
;
5325 /* The `>' token might be the end of a template-id or
5326 template-parameter-list now. */
5327 parser
->greater_than_is_operator_p
5328 = saved_greater_than_is_operator_p
;
5330 /* Consume the `)'. */
5331 token
= cp_lexer_peek_token (parser
->lexer
);
5332 location_t close_paren_loc
= token
->location
;
5333 expr
.set_range (open_paren_loc
, close_paren_loc
);
5334 if (!parens
.require_close (parser
)
5335 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5336 cp_parser_skip_to_end_of_statement (parser
);
5341 case CPP_OPEN_SQUARE
:
5343 if (c_dialect_objc ())
5345 /* We might have an Objective-C++ message. */
5346 cp_parser_parse_tentatively (parser
);
5347 tree msg
= cp_parser_objc_message_expression (parser
);
5348 /* If that works out, we're done ... */
5349 if (cp_parser_parse_definitely (parser
))
5351 /* ... else, fall though to see if it's a lambda. */
5353 cp_expr lam
= cp_parser_lambda_expression (parser
);
5354 /* Don't warn about a failed tentative parse. */
5355 if (cp_parser_error_occurred (parser
))
5356 return error_mark_node
;
5357 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5361 case CPP_OBJC_STRING
:
5362 if (c_dialect_objc ())
5363 /* We have an Objective-C++ string literal. */
5364 return cp_parser_objc_expression (parser
);
5365 cp_parser_error (parser
, "expected primary-expression");
5366 return error_mark_node
;
5369 switch (token
->keyword
)
5371 /* These two are the boolean literals. */
5373 cp_lexer_consume_token (parser
->lexer
);
5374 return cp_expr (boolean_true_node
, token
->location
);
5376 cp_lexer_consume_token (parser
->lexer
);
5377 return cp_expr (boolean_false_node
, token
->location
);
5379 /* The `__null' literal. */
5381 cp_lexer_consume_token (parser
->lexer
);
5382 return cp_expr (null_node
, token
->location
);
5384 /* The `nullptr' literal. */
5386 cp_lexer_consume_token (parser
->lexer
);
5387 return cp_expr (nullptr_node
, token
->location
);
5389 /* Recognize the `this' keyword. */
5391 cp_lexer_consume_token (parser
->lexer
);
5392 if (parser
->local_variables_forbidden_p
)
5394 error_at (token
->location
,
5395 "%<this%> may not be used in this context");
5396 return error_mark_node
;
5398 /* Pointers cannot appear in constant-expressions. */
5399 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5400 return error_mark_node
;
5401 return cp_expr (finish_this_expr (), token
->location
);
5403 /* The `operator' keyword can be the beginning of an
5408 case RID_FUNCTION_NAME
:
5409 case RID_PRETTY_FUNCTION_NAME
:
5410 case RID_C99_FUNCTION_NAME
:
5412 non_integral_constant name
;
5414 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5415 __func__ are the names of variables -- but they are
5416 treated specially. Therefore, they are handled here,
5417 rather than relying on the generic id-expression logic
5418 below. Grammatically, these names are id-expressions.
5420 Consume the token. */
5421 token
= cp_lexer_consume_token (parser
->lexer
);
5423 switch (token
->keyword
)
5425 case RID_FUNCTION_NAME
:
5426 name
= NIC_FUNC_NAME
;
5428 case RID_PRETTY_FUNCTION_NAME
:
5429 name
= NIC_PRETTY_FUNC
;
5431 case RID_C99_FUNCTION_NAME
:
5432 name
= NIC_C99_FUNC
;
5438 if (cp_parser_non_integral_constant_expression (parser
, name
))
5439 return error_mark_node
;
5441 /* Look up the name. */
5442 return finish_fname (token
->u
.value
);
5449 location_t type_location
;
5450 location_t start_loc
5451 = cp_lexer_peek_token (parser
->lexer
)->location
;
5452 /* The `__builtin_va_arg' construct is used to handle
5453 `va_arg'. Consume the `__builtin_va_arg' token. */
5454 cp_lexer_consume_token (parser
->lexer
);
5455 /* Look for the opening `('. */
5456 matching_parens parens
;
5457 parens
.require_open (parser
);
5458 /* Now, parse the assignment-expression. */
5459 expression
= cp_parser_assignment_expression (parser
);
5460 /* Look for the `,'. */
5461 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5462 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5463 /* Parse the type-id. */
5465 type_id_in_expr_sentinel
s (parser
);
5466 type
= cp_parser_type_id (parser
);
5468 /* Look for the closing `)'. */
5469 location_t finish_loc
5470 = cp_lexer_peek_token (parser
->lexer
)->location
;
5471 parens
.require_close (parser
);
5472 /* Using `va_arg' in a constant-expression is not
5474 if (cp_parser_non_integral_constant_expression (parser
,
5476 return error_mark_node
;
5477 /* Construct a location of the form:
5478 __builtin_va_arg (v, int)
5479 ~~~~~~~~~~~~~~~~~~~~~^~~~
5480 with the caret at the type, ranging from the start of the
5481 "__builtin_va_arg" token to the close paren. */
5482 location_t combined_loc
5483 = make_location (type_location
, start_loc
, finish_loc
);
5484 return build_x_va_arg (combined_loc
, expression
, type
);
5488 return cp_parser_builtin_offsetof (parser
);
5490 case RID_HAS_NOTHROW_ASSIGN
:
5491 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5492 case RID_HAS_NOTHROW_COPY
:
5493 case RID_HAS_TRIVIAL_ASSIGN
:
5494 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5495 case RID_HAS_TRIVIAL_COPY
:
5496 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5497 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5498 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5499 case RID_IS_ABSTRACT
:
5500 case RID_IS_AGGREGATE
:
5501 case RID_IS_BASE_OF
:
5506 case RID_IS_LITERAL_TYPE
:
5508 case RID_IS_POLYMORPHIC
:
5509 case RID_IS_SAME_AS
:
5510 case RID_IS_STD_LAYOUT
:
5511 case RID_IS_TRIVIAL
:
5512 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5513 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5514 case RID_IS_TRIVIALLY_COPYABLE
:
5516 case RID_IS_ASSIGNABLE
:
5517 case RID_IS_CONSTRUCTIBLE
:
5518 return cp_parser_trait_expr (parser
, token
->keyword
);
5522 return cp_parser_requires_expression (parser
);
5524 /* Objective-C++ expressions. */
5526 case RID_AT_PROTOCOL
:
5527 case RID_AT_SELECTOR
:
5528 return cp_parser_objc_expression (parser
);
5531 if (parser
->in_function_body
5532 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5535 error_at (token
->location
,
5536 "a template declaration cannot appear at block scope");
5537 cp_parser_skip_to_end_of_block_or_statement (parser
);
5538 return error_mark_node
;
5542 cp_parser_error (parser
, "expected primary-expression");
5543 return error_mark_node
;
5546 /* An id-expression can start with either an identifier, a
5547 `::' as the beginning of a qualified-id, or the "operator"
5551 case CPP_TEMPLATE_ID
:
5552 case CPP_NESTED_NAME_SPECIFIER
:
5555 cp_expr id_expression
;
5557 const char *error_msg
;
5560 cp_token
*id_expr_token
;
5562 /* Parse the id-expression. */
5564 = cp_parser_id_expression (parser
,
5565 /*template_keyword_p=*/false,
5566 /*check_dependency_p=*/true,
5568 /*declarator_p=*/false,
5569 /*optional_p=*/false);
5570 if (id_expression
== error_mark_node
)
5571 return error_mark_node
;
5572 id_expr_token
= token
;
5573 token
= cp_lexer_peek_token (parser
->lexer
);
5574 done
= (token
->type
!= CPP_OPEN_SQUARE
5575 && token
->type
!= CPP_OPEN_PAREN
5576 && token
->type
!= CPP_DOT
5577 && token
->type
!= CPP_DEREF
5578 && token
->type
!= CPP_PLUS_PLUS
5579 && token
->type
!= CPP_MINUS_MINUS
);
5580 /* If we have a template-id, then no further lookup is
5581 required. If the template-id was for a template-class, we
5582 will sometimes have a TYPE_DECL at this point. */
5583 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5584 || TREE_CODE (id_expression
) == TYPE_DECL
)
5585 decl
= id_expression
;
5586 /* Look up the name. */
5589 tree ambiguous_decls
;
5591 /* If we already know that this lookup is ambiguous, then
5592 we've already issued an error message; there's no reason
5594 if (id_expr_token
->type
== CPP_NAME
5595 && id_expr_token
->error_reported
)
5597 cp_parser_simulate_error (parser
);
5598 return error_mark_node
;
5601 decl
= cp_parser_lookup_name (parser
, id_expression
,
5604 /*is_namespace=*/false,
5605 /*check_dependency=*/true,
5607 id_expression
.get_location ());
5608 /* If the lookup was ambiguous, an error will already have
5610 if (ambiguous_decls
)
5611 return error_mark_node
;
5613 /* In Objective-C++, we may have an Objective-C 2.0
5614 dot-syntax for classes here. */
5615 if (c_dialect_objc ()
5616 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5617 && TREE_CODE (decl
) == TYPE_DECL
5618 && objc_is_class_name (decl
))
5621 cp_lexer_consume_token (parser
->lexer
);
5622 component
= cp_parser_identifier (parser
);
5623 if (component
== error_mark_node
)
5624 return error_mark_node
;
5626 tree result
= objc_build_class_component_ref (id_expression
,
5628 /* Build a location of the form:
5631 with caret at the start of the component name (at
5632 input_location), ranging from the start of the id_expression
5633 to the end of the component name. */
5634 location_t combined_loc
5635 = make_location (input_location
, id_expression
.get_start (),
5636 get_finish (input_location
));
5637 protected_set_expr_location (result
, combined_loc
);
5641 /* In Objective-C++, an instance variable (ivar) may be preferred
5642 to whatever cp_parser_lookup_name() found.
5643 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5644 rest of c-family, we have to do a little extra work to preserve
5645 any location information in cp_expr "decl". Given that
5646 objc_lookup_ivar is implemented in "c-family" and "objc", we
5647 have a trip through the pure "tree" type, rather than cp_expr.
5648 Naively copying it back to "decl" would implicitly give the
5649 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5650 store an EXPR_LOCATION. Hence we only update "decl" (and
5651 hence its location_t) if we get back a different tree node. */
5652 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5654 if (decl_tree
!= decl
.get_value ())
5655 decl
= cp_expr (decl_tree
);
5657 /* If name lookup gives us a SCOPE_REF, then the
5658 qualifying scope was dependent. */
5659 if (TREE_CODE (decl
) == SCOPE_REF
)
5661 /* At this point, we do not know if DECL is a valid
5662 integral constant expression. We assume that it is
5663 in fact such an expression, so that code like:
5665 template <int N> struct A {
5669 is accepted. At template-instantiation time, we
5670 will check that B<N>::i is actually a constant. */
5673 /* Check to see if DECL is a local variable in a context
5674 where that is forbidden. */
5675 if (parser
->local_variables_forbidden_p
5676 && local_variable_p (decl
))
5678 error_at (id_expression
.get_location (),
5679 "local variable %qD may not appear in this context",
5681 return error_mark_node
;
5685 decl
= (finish_id_expression
5686 (id_expression
, decl
, parser
->scope
,
5688 parser
->integral_constant_expression_p
,
5689 parser
->allow_non_integral_constant_expression_p
,
5690 &parser
->non_integral_constant_expression_p
,
5691 template_p
, done
, address_p
,
5694 id_expression
.get_location ()));
5696 cp_parser_error (parser
, error_msg
);
5697 decl
.set_location (id_expression
.get_location ());
5698 decl
.set_range (id_expr_token
->location
, id_expression
.get_finish ());
5702 /* Anything else is an error. */
5704 cp_parser_error (parser
, "expected primary-expression");
5705 return error_mark_node
;
5709 static inline cp_expr
5710 cp_parser_primary_expression (cp_parser
*parser
,
5713 bool template_arg_p
,
5716 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5717 /*decltype*/false, idk
);
5720 /* Parse an id-expression.
5727 :: [opt] nested-name-specifier template [opt] unqualified-id
5729 :: operator-function-id
5732 Return a representation of the unqualified portion of the
5733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5734 a `::' or nested-name-specifier.
5736 Often, if the id-expression was a qualified-id, the caller will
5737 want to make a SCOPE_REF to represent the qualified-id. This
5738 function does not do this in order to avoid wastefully creating
5739 SCOPE_REFs when they are not required.
5741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5745 uninstantiated templates.
5747 If *TEMPLATE_P is non-NULL, it is set to true iff the
5748 `template' keyword is used to explicitly indicate that the entity
5749 named is a template.
5751 If DECLARATOR_P is true, the id-expression is appearing as part of
5752 a declarator, rather than as part of an expression. */
5755 cp_parser_id_expression (cp_parser
*parser
,
5756 bool template_keyword_p
,
5757 bool check_dependency_p
,
5762 bool global_scope_p
;
5763 bool nested_name_specifier_p
;
5765 /* Assume the `template' keyword was not used. */
5767 *template_p
= template_keyword_p
;
5769 /* Look for the optional `::' operator. */
5771 = (!template_keyword_p
5772 && (cp_parser_global_scope_opt (parser
,
5773 /*current_scope_valid_p=*/false)
5776 /* Look for the optional nested-name-specifier. */
5777 nested_name_specifier_p
5778 = (cp_parser_nested_name_specifier_opt (parser
,
5779 /*typename_keyword_p=*/false,
5786 /* If there is a nested-name-specifier, then we are looking at
5787 the first qualified-id production. */
5788 if (nested_name_specifier_p
)
5791 tree saved_object_scope
;
5792 tree saved_qualifying_scope
;
5793 cp_expr unqualified_id
;
5796 /* See if the next token is the `template' keyword. */
5798 template_p
= &is_template
;
5799 *template_p
= cp_parser_optional_template_keyword (parser
);
5800 /* Name lookup we do during the processing of the
5801 unqualified-id might obliterate SCOPE. */
5802 saved_scope
= parser
->scope
;
5803 saved_object_scope
= parser
->object_scope
;
5804 saved_qualifying_scope
= parser
->qualifying_scope
;
5805 /* Process the final unqualified-id. */
5806 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5809 /*optional_p=*/false);
5810 /* Restore the SAVED_SCOPE for our caller. */
5811 parser
->scope
= saved_scope
;
5812 parser
->object_scope
= saved_object_scope
;
5813 parser
->qualifying_scope
= saved_qualifying_scope
;
5815 return unqualified_id
;
5817 /* Otherwise, if we are in global scope, then we are looking at one
5818 of the other qualified-id productions. */
5819 else if (global_scope_p
)
5824 /* Peek at the next token. */
5825 token
= cp_lexer_peek_token (parser
->lexer
);
5827 /* If it's an identifier, and the next token is not a "<", then
5828 we can avoid the template-id case. This is an optimization
5829 for this common case. */
5830 if (token
->type
== CPP_NAME
5831 && !cp_parser_nth_token_starts_template_argument_list_p
5833 return cp_parser_identifier (parser
);
5835 cp_parser_parse_tentatively (parser
);
5836 /* Try a template-id. */
5837 id
= cp_parser_template_id (parser
,
5838 /*template_keyword_p=*/false,
5839 /*check_dependency_p=*/true,
5842 /* If that worked, we're done. */
5843 if (cp_parser_parse_definitely (parser
))
5846 /* Peek at the next token. (Changes in the token buffer may
5847 have invalidated the pointer obtained above.) */
5848 token
= cp_lexer_peek_token (parser
->lexer
);
5850 switch (token
->type
)
5853 return cp_parser_identifier (parser
);
5856 if (token
->keyword
== RID_OPERATOR
)
5857 return cp_parser_operator_function_id (parser
);
5861 cp_parser_error (parser
, "expected id-expression");
5862 return error_mark_node
;
5866 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5867 /*check_dependency_p=*/true,
5872 /* Parse an unqualified-id.
5876 operator-function-id
5877 conversion-function-id
5881 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5882 keyword, in a construct like `A::template ...'.
5884 Returns a representation of unqualified-id. For the `identifier'
5885 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5886 production a BIT_NOT_EXPR is returned; the operand of the
5887 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5888 other productions, see the documentation accompanying the
5889 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5890 names are looked up in uninstantiated templates. If DECLARATOR_P
5891 is true, the unqualified-id is appearing as part of a declarator,
5892 rather than as part of an expression. */
5895 cp_parser_unqualified_id (cp_parser
* parser
,
5896 bool template_keyword_p
,
5897 bool check_dependency_p
,
5903 /* Peek at the next token. */
5904 token
= cp_lexer_peek_token (parser
->lexer
);
5906 switch ((int) token
->type
)
5912 /* We don't know yet whether or not this will be a
5914 cp_parser_parse_tentatively (parser
);
5915 /* Try a template-id. */
5916 id
= cp_parser_template_id (parser
, template_keyword_p
,
5920 /* If it worked, we're done. */
5921 if (cp_parser_parse_definitely (parser
))
5923 /* Otherwise, it's an ordinary identifier. */
5924 return cp_parser_identifier (parser
);
5927 case CPP_TEMPLATE_ID
:
5928 return cp_parser_template_id (parser
, template_keyword_p
,
5936 tree qualifying_scope
;
5941 /* Consume the `~' token. */
5942 cp_lexer_consume_token (parser
->lexer
);
5943 /* Parse the class-name. The standard, as written, seems to
5946 template <typename T> struct S { ~S (); };
5947 template <typename T> S<T>::~S() {}
5949 is invalid, since `~' must be followed by a class-name, but
5950 `S<T>' is dependent, and so not known to be a class.
5951 That's not right; we need to look in uninstantiated
5952 templates. A further complication arises from:
5954 template <typename T> void f(T t) {
5958 Here, it is not possible to look up `T' in the scope of `T'
5959 itself. We must look in both the current scope, and the
5960 scope of the containing complete expression.
5962 Yet another issue is:
5971 The standard does not seem to say that the `S' in `~S'
5972 should refer to the type `S' and not the data member
5975 /* DR 244 says that we look up the name after the "~" in the
5976 same scope as we looked up the qualifying name. That idea
5977 isn't fully worked out; it's more complicated than that. */
5978 scope
= parser
->scope
;
5979 object_scope
= parser
->object_scope
;
5980 qualifying_scope
= parser
->qualifying_scope
;
5982 /* Check for invalid scopes. */
5983 if (scope
== error_mark_node
)
5985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5986 cp_lexer_consume_token (parser
->lexer
);
5987 return error_mark_node
;
5989 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5991 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5992 error_at (token
->location
,
5993 "scope %qT before %<~%> is not a class-name",
5995 cp_parser_simulate_error (parser
);
5996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5997 cp_lexer_consume_token (parser
->lexer
);
5998 return error_mark_node
;
6000 gcc_assert (!scope
|| TYPE_P (scope
));
6002 /* If the name is of the form "X::~X" it's OK even if X is a
6004 token
= cp_lexer_peek_token (parser
->lexer
);
6006 && token
->type
== CPP_NAME
6007 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6009 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6010 || (CLASS_TYPE_P (scope
)
6011 && constructor_name_p (token
->u
.value
, scope
))))
6013 cp_lexer_consume_token (parser
->lexer
);
6014 return build_nt (BIT_NOT_EXPR
, scope
);
6017 /* ~auto means the destructor of whatever the object is. */
6018 if (cp_parser_is_keyword (token
, RID_AUTO
))
6020 if (cxx_dialect
< cxx14
)
6021 pedwarn (input_location
, 0,
6022 "%<~auto%> only available with "
6023 "-std=c++14 or -std=gnu++14");
6024 cp_lexer_consume_token (parser
->lexer
);
6025 return build_nt (BIT_NOT_EXPR
, make_auto ());
6028 /* If there was an explicit qualification (S::~T), first look
6029 in the scope given by the qualification (i.e., S).
6031 Note: in the calls to cp_parser_class_name below we pass
6032 typename_type so that lookup finds the injected-class-name
6033 rather than the constructor. */
6035 type_decl
= NULL_TREE
;
6038 cp_parser_parse_tentatively (parser
);
6039 type_decl
= cp_parser_class_name (parser
,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6046 if (cp_parser_parse_definitely (parser
))
6049 /* In "N::S::~S", look in "N" as well. */
6050 if (!done
&& scope
&& qualifying_scope
)
6052 cp_parser_parse_tentatively (parser
);
6053 parser
->scope
= qualifying_scope
;
6054 parser
->object_scope
= NULL_TREE
;
6055 parser
->qualifying_scope
= NULL_TREE
;
6057 = cp_parser_class_name (parser
,
6058 /*typename_keyword_p=*/false,
6059 /*template_keyword_p=*/false,
6061 /*check_dependency=*/false,
6062 /*class_head_p=*/false,
6064 if (cp_parser_parse_definitely (parser
))
6067 /* In "p->S::~T", look in the scope given by "*p" as well. */
6068 else if (!done
&& object_scope
)
6070 cp_parser_parse_tentatively (parser
);
6071 parser
->scope
= object_scope
;
6072 parser
->object_scope
= NULL_TREE
;
6073 parser
->qualifying_scope
= NULL_TREE
;
6075 = cp_parser_class_name (parser
,
6076 /*typename_keyword_p=*/false,
6077 /*template_keyword_p=*/false,
6079 /*check_dependency=*/false,
6080 /*class_head_p=*/false,
6082 if (cp_parser_parse_definitely (parser
))
6085 /* Look in the surrounding context. */
6088 parser
->scope
= NULL_TREE
;
6089 parser
->object_scope
= NULL_TREE
;
6090 parser
->qualifying_scope
= NULL_TREE
;
6091 if (processing_template_decl
)
6092 cp_parser_parse_tentatively (parser
);
6094 = cp_parser_class_name (parser
,
6095 /*typename_keyword_p=*/false,
6096 /*template_keyword_p=*/false,
6098 /*check_dependency=*/false,
6099 /*class_head_p=*/false,
6101 if (processing_template_decl
6102 && ! cp_parser_parse_definitely (parser
))
6104 /* We couldn't find a type with this name. If we're parsing
6105 tentatively, fail and try something else. */
6106 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6108 cp_parser_simulate_error (parser
);
6109 return error_mark_node
;
6111 /* Otherwise, accept it and check for a match at instantiation
6113 type_decl
= cp_parser_identifier (parser
);
6114 if (type_decl
!= error_mark_node
)
6115 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6119 /* If an error occurred, assume that the name of the
6120 destructor is the same as the name of the qualifying
6121 class. That allows us to keep parsing after running
6122 into ill-formed destructor names. */
6123 if (type_decl
== error_mark_node
&& scope
)
6124 return build_nt (BIT_NOT_EXPR
, scope
);
6125 else if (type_decl
== error_mark_node
)
6126 return error_mark_node
;
6128 /* Check that destructor name and scope match. */
6129 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6131 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6132 error_at (token
->location
,
6133 "declaration of %<~%T%> as member of %qT",
6135 cp_parser_simulate_error (parser
);
6136 return error_mark_node
;
6141 A typedef-name that names a class shall not be used as the
6142 identifier in the declarator for a destructor declaration. */
6144 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6145 && !DECL_SELF_REFERENCE_P (type_decl
)
6146 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6147 error_at (token
->location
,
6148 "typedef-name %qD used as destructor declarator",
6151 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6155 if (token
->keyword
== RID_OPERATOR
)
6159 /* This could be a template-id, so we try that first. */
6160 cp_parser_parse_tentatively (parser
);
6161 /* Try a template-id. */
6162 id
= cp_parser_template_id (parser
, template_keyword_p
,
6163 /*check_dependency_p=*/true,
6166 /* If that worked, we're done. */
6167 if (cp_parser_parse_definitely (parser
))
6169 /* We still don't know whether we're looking at an
6170 operator-function-id or a conversion-function-id. */
6171 cp_parser_parse_tentatively (parser
);
6172 /* Try an operator-function-id. */
6173 id
= cp_parser_operator_function_id (parser
);
6174 /* If that didn't work, try a conversion-function-id. */
6175 if (!cp_parser_parse_definitely (parser
))
6176 id
= cp_parser_conversion_function_id (parser
);
6185 cp_parser_error (parser
, "expected unqualified-id");
6186 return error_mark_node
;
6190 /* Parse an (optional) nested-name-specifier.
6192 nested-name-specifier: [C++98]
6193 class-or-namespace-name :: nested-name-specifier [opt]
6194 class-or-namespace-name :: template nested-name-specifier [opt]
6196 nested-name-specifier: [C++0x]
6199 nested-name-specifier identifier ::
6200 nested-name-specifier template [opt] simple-template-id ::
6202 PARSER->SCOPE should be set appropriately before this function is
6203 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6204 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6207 Sets PARSER->SCOPE to the class (TYPE) or namespace
6208 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6209 it unchanged if there is no nested-name-specifier. Returns the new
6210 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6212 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6213 part of a declaration and/or decl-specifier. */
6216 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6217 bool typename_keyword_p
,
6218 bool check_dependency_p
,
6220 bool is_declaration
,
6221 bool template_keyword_p
/* = false */)
6223 bool success
= false;
6224 cp_token_position start
= 0;
6227 /* Remember where the nested-name-specifier starts. */
6228 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6230 start
= cp_lexer_token_position (parser
->lexer
, false);
6231 push_deferring_access_checks (dk_deferred
);
6238 tree saved_qualifying_scope
;
6240 /* Spot cases that cannot be the beginning of a
6241 nested-name-specifier. */
6242 token
= cp_lexer_peek_token (parser
->lexer
);
6244 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6245 the already parsed nested-name-specifier. */
6246 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6248 /* Grab the nested-name-specifier and continue the loop. */
6249 cp_parser_pre_parsed_nested_name_specifier (parser
);
6250 /* If we originally encountered this nested-name-specifier
6251 with IS_DECLARATION set to false, we will not have
6252 resolved TYPENAME_TYPEs, so we must do so here. */
6254 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6256 new_scope
= resolve_typename_type (parser
->scope
,
6257 /*only_current_p=*/false);
6258 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6259 parser
->scope
= new_scope
;
6265 /* Spot cases that cannot be the beginning of a
6266 nested-name-specifier. On the second and subsequent times
6267 through the loop, we look for the `template' keyword. */
6268 if (success
&& token
->keyword
== RID_TEMPLATE
)
6270 /* A template-id can start a nested-name-specifier. */
6271 else if (token
->type
== CPP_TEMPLATE_ID
)
6273 /* DR 743: decltype can be used in a nested-name-specifier. */
6274 else if (token_is_decltype (token
))
6278 /* If the next token is not an identifier, then it is
6279 definitely not a type-name or namespace-name. */
6280 if (token
->type
!= CPP_NAME
)
6282 /* If the following token is neither a `<' (to begin a
6283 template-id), nor a `::', then we are not looking at a
6284 nested-name-specifier. */
6285 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6287 if (token
->type
== CPP_COLON
6288 && parser
->colon_corrects_to_scope_p
6289 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6291 gcc_rich_location
richloc (token
->location
);
6292 richloc
.add_fixit_replace ("::");
6294 "found %<:%> in nested-name-specifier, "
6296 token
->type
= CPP_SCOPE
;
6299 if (token
->type
!= CPP_SCOPE
6300 && !cp_parser_nth_token_starts_template_argument_list_p
6305 /* The nested-name-specifier is optional, so we parse
6307 cp_parser_parse_tentatively (parser
);
6309 /* Look for the optional `template' keyword, if this isn't the
6310 first time through the loop. */
6312 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6314 /* Save the old scope since the name lookup we are about to do
6315 might destroy it. */
6316 old_scope
= parser
->scope
;
6317 saved_qualifying_scope
= parser
->qualifying_scope
;
6318 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6319 look up names in "X<T>::I" in order to determine that "Y" is
6320 a template. So, if we have a typename at this point, we make
6321 an effort to look through it. */
6323 && !typename_keyword_p
6325 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6326 parser
->scope
= resolve_typename_type (parser
->scope
,
6327 /*only_current_p=*/false);
6328 /* Parse the qualifying entity. */
6330 = cp_parser_qualifying_entity (parser
,
6336 /* Look for the `::' token. */
6337 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6339 /* If we found what we wanted, we keep going; otherwise, we're
6341 if (!cp_parser_parse_definitely (parser
))
6343 bool error_p
= false;
6345 /* Restore the OLD_SCOPE since it was valid before the
6346 failed attempt at finding the last
6347 class-or-namespace-name. */
6348 parser
->scope
= old_scope
;
6349 parser
->qualifying_scope
= saved_qualifying_scope
;
6351 /* If the next token is a decltype, and the one after that is a
6352 `::', then the decltype has failed to resolve to a class or
6353 enumeration type. Give this error even when parsing
6354 tentatively since it can't possibly be valid--and we're going
6355 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6356 won't get another chance.*/
6357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6358 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6361 token
= cp_lexer_consume_token (parser
->lexer
);
6362 error_at (token
->location
, "decltype evaluates to %qT, "
6363 "which is not a class or enumeration type",
6364 token
->u
.tree_check_value
->value
);
6365 parser
->scope
= error_mark_node
;
6369 cp_lexer_consume_token (parser
->lexer
);
6372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6373 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6375 /* If we have a non-type template-id followed by ::, it can't
6376 possibly be valid. */
6377 token
= cp_lexer_peek_token (parser
->lexer
);
6378 tree tid
= token
->u
.tree_check_value
->value
;
6379 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6380 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6382 tree tmpl
= NULL_TREE
;
6383 if (is_overloaded_fn (tid
))
6385 tree fns
= get_fns (tid
);
6386 if (OVL_SINGLE_P (fns
))
6387 tmpl
= OVL_FIRST (fns
);
6388 error_at (token
->location
, "function template-id %qD "
6389 "in nested-name-specifier", tid
);
6393 /* Variable template. */
6394 tmpl
= TREE_OPERAND (tid
, 0);
6395 gcc_assert (variable_template_p (tmpl
));
6396 error_at (token
->location
, "variable template-id %qD "
6397 "in nested-name-specifier", tid
);
6400 inform (DECL_SOURCE_LOCATION (tmpl
),
6401 "%qD declared here", tmpl
);
6403 parser
->scope
= error_mark_node
;
6407 cp_lexer_consume_token (parser
->lexer
);
6408 cp_lexer_consume_token (parser
->lexer
);
6412 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6414 /* If the next token is an identifier, and the one after
6415 that is a `::', then any valid interpretation would have
6416 found a class-or-namespace-name. */
6417 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6418 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6420 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6423 token
= cp_lexer_consume_token (parser
->lexer
);
6426 if (!token
->error_reported
)
6429 tree ambiguous_decls
;
6431 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6433 /*is_template=*/false,
6434 /*is_namespace=*/false,
6435 /*check_dependency=*/true,
6438 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6439 error_at (token
->location
,
6440 "%qD used without template arguments",
6442 else if (ambiguous_decls
)
6444 // cp_parser_lookup_name has the same diagnostic,
6445 // thus make sure to emit it at most once.
6446 if (cp_parser_uncommitted_to_tentative_parse_p
6449 error_at (token
->location
,
6450 "reference to %qD is ambiguous",
6452 print_candidates (ambiguous_decls
);
6454 decl
= error_mark_node
;
6458 if (cxx_dialect
!= cxx98
)
6459 cp_parser_name_lookup_error
6460 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6463 cp_parser_name_lookup_error
6464 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6468 parser
->scope
= error_mark_node
;
6470 /* Treat this as a successful nested-name-specifier
6475 If the name found is not a class-name (clause
6476 _class_) or namespace-name (_namespace.def_), the
6477 program is ill-formed. */
6480 cp_lexer_consume_token (parser
->lexer
);
6484 /* We've found one valid nested-name-specifier. */
6486 /* Name lookup always gives us a DECL. */
6487 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6488 new_scope
= TREE_TYPE (new_scope
);
6489 /* Uses of "template" must be followed by actual templates. */
6490 if (template_keyword_p
6491 && !(CLASS_TYPE_P (new_scope
)
6492 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6493 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6494 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6495 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6496 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6497 == TEMPLATE_ID_EXPR
)))
6498 permerror (input_location
, TYPE_P (new_scope
)
6499 ? G_("%qT is not a template")
6500 : G_("%qD is not a template"),
6502 /* If it is a class scope, try to complete it; we are about to
6503 be looking up names inside the class. */
6504 if (TYPE_P (new_scope
)
6505 /* Since checking types for dependency can be expensive,
6506 avoid doing it if the type is already complete. */
6507 && !COMPLETE_TYPE_P (new_scope
)
6508 /* Do not try to complete dependent types. */
6509 && !dependent_type_p (new_scope
))
6511 new_scope
= complete_type (new_scope
);
6512 /* If it is a typedef to current class, use the current
6513 class instead, as the typedef won't have any names inside
6515 if (!COMPLETE_TYPE_P (new_scope
)
6516 && currently_open_class (new_scope
))
6517 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6519 /* Make sure we look in the right scope the next time through
6521 parser
->scope
= new_scope
;
6524 /* If parsing tentatively, replace the sequence of tokens that makes
6525 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6526 token. That way, should we re-parse the token stream, we will
6527 not have to repeat the effort required to do the parse, nor will
6528 we issue duplicate error messages. */
6529 if (success
&& start
)
6533 token
= cp_lexer_token_at (parser
->lexer
, start
);
6534 /* Reset the contents of the START token. */
6535 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6536 /* Retrieve any deferred checks. Do not pop this access checks yet
6537 so the memory will not be reclaimed during token replacing below. */
6538 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6539 token
->u
.tree_check_value
->value
= parser
->scope
;
6540 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6541 token
->u
.tree_check_value
->qualifying_scope
=
6542 parser
->qualifying_scope
;
6543 token
->keyword
= RID_MAX
;
6545 /* Purge all subsequent tokens. */
6546 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6550 pop_to_parent_deferring_access_checks ();
6552 return success
? parser
->scope
: NULL_TREE
;
6555 /* Parse a nested-name-specifier. See
6556 cp_parser_nested_name_specifier_opt for details. This function
6557 behaves identically, except that it will an issue an error if no
6558 nested-name-specifier is present. */
6561 cp_parser_nested_name_specifier (cp_parser
*parser
,
6562 bool typename_keyword_p
,
6563 bool check_dependency_p
,
6565 bool is_declaration
)
6569 /* Look for the nested-name-specifier. */
6570 scope
= cp_parser_nested_name_specifier_opt (parser
,
6575 /* If it was not present, issue an error message. */
6578 cp_parser_error (parser
, "expected nested-name-specifier");
6579 parser
->scope
= NULL_TREE
;
6585 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6586 this is either a class-name or a namespace-name (which corresponds
6587 to the class-or-namespace-name production in the grammar). For
6588 C++0x, it can also be a type-name that refers to an enumeration
6589 type or a simple-template-id.
6591 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6592 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6593 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6594 TYPE_P is TRUE iff the next name should be taken as a class-name,
6595 even the same name is declared to be another entity in the same
6598 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6599 specified by the class-or-namespace-name. If neither is found the
6600 ERROR_MARK_NODE is returned. */
6603 cp_parser_qualifying_entity (cp_parser
*parser
,
6604 bool typename_keyword_p
,
6605 bool template_keyword_p
,
6606 bool check_dependency_p
,
6608 bool is_declaration
)
6611 tree saved_qualifying_scope
;
6612 tree saved_object_scope
;
6615 bool successful_parse_p
;
6617 /* DR 743: decltype can appear in a nested-name-specifier. */
6618 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6620 scope
= cp_parser_decltype (parser
);
6621 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6622 && !MAYBE_CLASS_TYPE_P (scope
))
6624 cp_parser_simulate_error (parser
);
6625 return error_mark_node
;
6627 if (TYPE_NAME (scope
))
6628 scope
= TYPE_NAME (scope
);
6632 /* Before we try to parse the class-name, we must save away the
6633 current PARSER->SCOPE since cp_parser_class_name will destroy
6635 saved_scope
= parser
->scope
;
6636 saved_qualifying_scope
= parser
->qualifying_scope
;
6637 saved_object_scope
= parser
->object_scope
;
6638 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6639 there is no need to look for a namespace-name. */
6640 only_class_p
= template_keyword_p
6641 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6643 cp_parser_parse_tentatively (parser
);
6644 scope
= cp_parser_class_name (parser
,
6647 type_p
? class_type
: none_type
,
6649 /*class_head_p=*/false,
6651 /*enum_ok=*/cxx_dialect
> cxx98
);
6652 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6653 /* If that didn't work, try for a namespace-name. */
6654 if (!only_class_p
&& !successful_parse_p
)
6656 /* Restore the saved scope. */
6657 parser
->scope
= saved_scope
;
6658 parser
->qualifying_scope
= saved_qualifying_scope
;
6659 parser
->object_scope
= saved_object_scope
;
6660 /* If we are not looking at an identifier followed by the scope
6661 resolution operator, then this is not part of a
6662 nested-name-specifier. (Note that this function is only used
6663 to parse the components of a nested-name-specifier.) */
6664 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6665 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6666 return error_mark_node
;
6667 scope
= cp_parser_namespace_name (parser
);
6673 /* Return true if we are looking at a compound-literal, false otherwise. */
6676 cp_parser_compound_literal_p (cp_parser
*parser
)
6678 cp_lexer_save_tokens (parser
->lexer
);
6680 /* Skip tokens until the next token is a closing parenthesis.
6681 If we find the closing `)', and the next token is a `{', then
6682 we are looking at a compound-literal. */
6683 bool compound_literal_p
6684 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6685 /*consume_paren=*/true)
6686 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6688 /* Roll back the tokens we skipped. */
6689 cp_lexer_rollback_tokens (parser
->lexer
);
6691 return compound_literal_p
;
6694 /* Return true if EXPR is the integer constant zero or a complex constant
6695 of zero, without any folding, but ignoring location wrappers. */
6698 literal_integer_zerop (const_tree expr
)
6700 return (location_wrapper_p (expr
)
6701 && integer_zerop (TREE_OPERAND (expr
, 0)));
6704 /* Parse a postfix-expression.
6708 postfix-expression [ expression ]
6709 postfix-expression ( expression-list [opt] )
6710 simple-type-specifier ( expression-list [opt] )
6711 typename :: [opt] nested-name-specifier identifier
6712 ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier template [opt] template-id
6714 ( expression-list [opt] )
6715 postfix-expression . template [opt] id-expression
6716 postfix-expression -> template [opt] id-expression
6717 postfix-expression . pseudo-destructor-name
6718 postfix-expression -> pseudo-destructor-name
6719 postfix-expression ++
6720 postfix-expression --
6721 dynamic_cast < type-id > ( expression )
6722 static_cast < type-id > ( expression )
6723 reinterpret_cast < type-id > ( expression )
6724 const_cast < type-id > ( expression )
6725 typeid ( expression )
6731 ( type-id ) { initializer-list , [opt] }
6733 This extension is a GNU version of the C99 compound-literal
6734 construct. (The C99 grammar uses `type-name' instead of `type-id',
6735 but they are essentially the same concept.)
6737 If ADDRESS_P is true, the postfix expression is the operand of the
6738 `&' operator. CAST_P is true if this expression is the target of a
6741 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6742 class member access expressions [expr.ref].
6744 Returns a representation of the expression. */
6747 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6748 bool member_access_only_p
, bool decltype_p
,
6749 cp_id_kind
* pidk_return
)
6754 cp_id_kind idk
= CP_ID_KIND_NONE
;
6755 cp_expr postfix_expression
= NULL_TREE
;
6756 bool is_member_access
= false;
6758 /* Peek at the next token. */
6759 token
= cp_lexer_peek_token (parser
->lexer
);
6760 loc
= token
->location
;
6761 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6763 /* Some of the productions are determined by keywords. */
6764 keyword
= token
->keyword
;
6774 const char *saved_message
;
6775 bool saved_in_type_id_in_expr_p
;
6777 /* All of these can be handled in the same way from the point
6778 of view of parsing. Begin by consuming the token
6779 identifying the cast. */
6780 cp_lexer_consume_token (parser
->lexer
);
6782 /* New types cannot be defined in the cast. */
6783 saved_message
= parser
->type_definition_forbidden_message
;
6784 parser
->type_definition_forbidden_message
6785 = G_("types may not be defined in casts");
6787 /* Look for the opening `<'. */
6788 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6789 /* Parse the type to which we are casting. */
6790 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6791 parser
->in_type_id_in_expr_p
= true;
6792 type
= cp_parser_type_id (parser
);
6793 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6794 /* Look for the closing `>'. */
6795 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6796 /* Restore the old message. */
6797 parser
->type_definition_forbidden_message
= saved_message
;
6799 bool saved_greater_than_is_operator_p
6800 = parser
->greater_than_is_operator_p
;
6801 parser
->greater_than_is_operator_p
= true;
6803 /* And the expression which is being cast. */
6804 matching_parens parens
;
6805 parens
.require_open (parser
);
6806 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6807 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6809 location_t end_loc
= close_paren
?
6810 close_paren
->location
: UNKNOWN_LOCATION
;
6812 parser
->greater_than_is_operator_p
6813 = saved_greater_than_is_operator_p
;
6815 /* Only type conversions to integral or enumeration types
6816 can be used in constant-expressions. */
6817 if (!cast_valid_in_integral_constant_expression_p (type
)
6818 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6820 postfix_expression
= error_mark_node
;
6828 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6832 = build_static_cast (type
, expression
, tf_warning_or_error
);
6836 = build_reinterpret_cast (type
, expression
,
6837 tf_warning_or_error
);
6841 = build_const_cast (type
, expression
, tf_warning_or_error
);
6847 /* Construct a location e.g. :
6848 reinterpret_cast <int *> (expr)
6849 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6850 ranging from the start of the "*_cast" token to the final closing
6851 paren, with the caret at the start. */
6852 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6853 postfix_expression
.set_location (cp_cast_loc
);
6860 const char *saved_message
;
6861 bool saved_in_type_id_in_expr_p
;
6863 /* Consume the `typeid' token. */
6864 cp_lexer_consume_token (parser
->lexer
);
6865 /* Look for the `(' token. */
6866 matching_parens parens
;
6867 parens
.require_open (parser
);
6868 /* Types cannot be defined in a `typeid' expression. */
6869 saved_message
= parser
->type_definition_forbidden_message
;
6870 parser
->type_definition_forbidden_message
6871 = G_("types may not be defined in a %<typeid%> expression");
6872 /* We can't be sure yet whether we're looking at a type-id or an
6874 cp_parser_parse_tentatively (parser
);
6875 /* Try a type-id first. */
6876 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6877 parser
->in_type_id_in_expr_p
= true;
6878 type
= cp_parser_type_id (parser
);
6879 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6880 /* Look for the `)' token. Otherwise, we can't be sure that
6881 we're not looking at an expression: consider `typeid (int
6882 (3))', for example. */
6883 cp_token
*close_paren
= parens
.require_close (parser
);
6884 /* If all went well, simply lookup the type-id. */
6885 if (cp_parser_parse_definitely (parser
))
6886 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6887 /* Otherwise, fall back to the expression variant. */
6892 /* Look for an expression. */
6893 expression
= cp_parser_expression (parser
, & idk
);
6894 /* Compute its typeid. */
6895 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6896 /* Look for the `)' token. */
6897 close_paren
= parens
.require_close (parser
);
6899 /* Restore the saved message. */
6900 parser
->type_definition_forbidden_message
= saved_message
;
6901 /* `typeid' may not appear in an integral constant expression. */
6902 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6903 postfix_expression
= error_mark_node
;
6905 /* Construct a location e.g. :
6908 ranging from the start of the "typeid" token to the final closing
6909 paren, with the caret at the start. */
6912 location_t typeid_loc
6913 = make_location (start_loc
, start_loc
, close_paren
->location
);
6914 postfix_expression
.set_location (typeid_loc
);
6915 postfix_expression
.maybe_add_location_wrapper ();
6923 /* The syntax permitted here is the same permitted for an
6924 elaborated-type-specifier. */
6925 ++parser
->prevent_constrained_type_specifiers
;
6926 type
= cp_parser_elaborated_type_specifier (parser
,
6927 /*is_friend=*/false,
6928 /*is_declaration=*/false);
6929 --parser
->prevent_constrained_type_specifiers
;
6930 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6935 case RID_BUILTIN_SHUFFLE
:
6936 case RID_BUILTIN_LAUNDER
:
6938 vec
<tree
, va_gc
> *vec
;
6942 cp_lexer_consume_token (parser
->lexer
);
6943 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6944 /*cast_p=*/false, /*allow_expansion_p=*/true,
6945 /*non_constant_p=*/NULL
);
6948 postfix_expression
= error_mark_node
;
6952 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6958 if (vec
->length () == 1)
6960 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6963 error_at (loc
, "wrong number of arguments to "
6964 "%<__builtin_addressof%>");
6965 postfix_expression
= error_mark_node
;
6969 case RID_BUILTIN_LAUNDER
:
6970 if (vec
->length () == 1)
6971 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6972 tf_warning_or_error
);
6975 error_at (loc
, "wrong number of arguments to "
6976 "%<__builtin_launder%>");
6977 postfix_expression
= error_mark_node
;
6981 case RID_BUILTIN_SHUFFLE
:
6982 if (vec
->length () == 2)
6984 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6985 (*vec
)[1], tf_warning_or_error
);
6986 else if (vec
->length () == 3)
6988 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6989 (*vec
)[2], tf_warning_or_error
);
6992 error_at (loc
, "wrong number of arguments to "
6993 "%<__builtin_shuffle%>");
6994 postfix_expression
= error_mark_node
;
7008 /* If the next thing is a simple-type-specifier, we may be
7009 looking at a functional cast. We could also be looking at
7010 an id-expression. So, we try the functional cast, and if
7011 that doesn't work we fall back to the primary-expression. */
7012 cp_parser_parse_tentatively (parser
);
7013 /* Look for the simple-type-specifier. */
7014 ++parser
->prevent_constrained_type_specifiers
;
7015 type
= cp_parser_simple_type_specifier (parser
,
7016 /*decl_specs=*/NULL
,
7017 CP_PARSER_FLAGS_NONE
);
7018 --parser
->prevent_constrained_type_specifiers
;
7019 /* Parse the cast itself. */
7020 if (!cp_parser_error_occurred (parser
))
7022 = cp_parser_functional_cast (parser
, type
);
7023 /* If that worked, we're done. */
7024 if (cp_parser_parse_definitely (parser
))
7027 /* If the functional-cast didn't work out, try a
7028 compound-literal. */
7029 if (cp_parser_allow_gnu_extensions_p (parser
)
7030 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7032 cp_expr initializer
= NULL_TREE
;
7034 cp_parser_parse_tentatively (parser
);
7036 matching_parens parens
;
7037 parens
.consume_open (parser
);
7039 /* Avoid calling cp_parser_type_id pointlessly, see comment
7040 in cp_parser_cast_expression about c++/29234. */
7041 if (!cp_parser_compound_literal_p (parser
))
7042 cp_parser_simulate_error (parser
);
7045 /* Parse the type. */
7046 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7047 parser
->in_type_id_in_expr_p
= true;
7048 type
= cp_parser_type_id (parser
);
7049 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7050 parens
.require_close (parser
);
7053 /* If things aren't going well, there's no need to
7055 if (!cp_parser_error_occurred (parser
))
7057 bool non_constant_p
;
7058 /* Parse the brace-enclosed initializer list. */
7059 initializer
= cp_parser_braced_list (parser
,
7062 /* If that worked, we're definitely looking at a
7063 compound-literal expression. */
7064 if (cp_parser_parse_definitely (parser
))
7066 /* Warn the user that a compound literal is not
7067 allowed in standard C++. */
7068 pedwarn (input_location
, OPT_Wpedantic
,
7069 "ISO C++ forbids compound-literals");
7070 /* For simplicity, we disallow compound literals in
7071 constant-expressions. We could
7072 allow compound literals of integer type, whose
7073 initializer was a constant, in constant
7074 expressions. Permitting that usage, as a further
7075 extension, would not change the meaning of any
7076 currently accepted programs. (Of course, as
7077 compound literals are not part of ISO C++, the
7078 standard has nothing to say.) */
7079 if (cp_parser_non_integral_constant_expression (parser
,
7082 postfix_expression
= error_mark_node
;
7085 /* Form the representation of the compound-literal. */
7087 = finish_compound_literal (type
, initializer
,
7088 tf_warning_or_error
, fcl_c99
);
7089 postfix_expression
.set_location (initializer
.get_location ());
7094 /* It must be a primary-expression. */
7096 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7097 /*template_arg_p=*/false,
7104 /* Note that we don't need to worry about calling build_cplus_new on a
7105 class-valued CALL_EXPR in decltype when it isn't the end of the
7106 postfix-expression; unary_complex_lvalue will take care of that for
7109 /* Keep looping until the postfix-expression is complete. */
7112 if (idk
== CP_ID_KIND_UNQUALIFIED
7113 && identifier_p (postfix_expression
)
7114 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7115 /* It is not a Koenig lookup function call. */
7117 = unqualified_name_lookup_error (postfix_expression
);
7119 /* Peek at the next token. */
7120 token
= cp_lexer_peek_token (parser
->lexer
);
7122 switch (token
->type
)
7124 case CPP_OPEN_SQUARE
:
7125 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7127 cp_parser_error (parser
,
7128 "two consecutive %<[%> shall "
7129 "only introduce an attribute");
7130 return error_mark_node
;
7133 = cp_parser_postfix_open_square_expression (parser
,
7137 postfix_expression
.set_range (start_loc
,
7138 postfix_expression
.get_location ());
7140 idk
= CP_ID_KIND_NONE
;
7141 is_member_access
= false;
7144 case CPP_OPEN_PAREN
:
7145 /* postfix-expression ( expression-list [opt] ) */
7148 bool is_builtin_constant_p
;
7149 bool saved_integral_constant_expression_p
= false;
7150 bool saved_non_integral_constant_expression_p
= false;
7151 tsubst_flags_t complain
= complain_flags (decltype_p
);
7152 vec
<tree
, va_gc
> *args
;
7153 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7155 is_member_access
= false;
7157 is_builtin_constant_p
7158 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7159 if (is_builtin_constant_p
)
7161 /* The whole point of __builtin_constant_p is to allow
7162 non-constant expressions to appear as arguments. */
7163 saved_integral_constant_expression_p
7164 = parser
->integral_constant_expression_p
;
7165 saved_non_integral_constant_expression_p
7166 = parser
->non_integral_constant_expression_p
;
7167 parser
->integral_constant_expression_p
= false;
7169 args
= (cp_parser_parenthesized_expression_list
7171 /*cast_p=*/false, /*allow_expansion_p=*/true,
7172 /*non_constant_p=*/NULL
,
7173 /*close_paren_loc=*/&close_paren_loc
,
7174 /*wrap_locations_p=*/true));
7175 if (is_builtin_constant_p
)
7177 parser
->integral_constant_expression_p
7178 = saved_integral_constant_expression_p
;
7179 parser
->non_integral_constant_expression_p
7180 = saved_non_integral_constant_expression_p
;
7185 postfix_expression
= error_mark_node
;
7189 /* Function calls are not permitted in
7190 constant-expressions. */
7191 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7192 && cp_parser_non_integral_constant_expression (parser
,
7195 postfix_expression
= error_mark_node
;
7196 release_tree_vector (args
);
7201 if (idk
== CP_ID_KIND_UNQUALIFIED
7202 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7204 if (identifier_p (postfix_expression
)
7205 /* In C++2A, we may need to perform ADL for a template
7207 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7208 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7210 if (!args
->is_empty ())
7213 if (!any_type_dependent_arguments_p (args
))
7215 = perform_koenig_lookup (postfix_expression
, args
,
7220 = unqualified_fn_lookup_error (postfix_expression
);
7222 /* We do not perform argument-dependent lookup if
7223 normal lookup finds a non-function, in accordance
7224 with the expected resolution of DR 218. */
7225 else if (!args
->is_empty ()
7226 && is_overloaded_fn (postfix_expression
))
7228 tree fn
= get_first_fn (postfix_expression
);
7229 fn
= STRIP_TEMPLATE (fn
);
7231 /* Do not do argument dependent lookup if regular
7232 lookup finds a member function or a block-scope
7233 function declaration. [basic.lookup.argdep]/3 */
7234 if (!DECL_FUNCTION_MEMBER_P (fn
)
7235 && !DECL_LOCAL_FUNCTION_P (fn
))
7238 if (!any_type_dependent_arguments_p (args
))
7240 = perform_koenig_lookup (postfix_expression
, args
,
7246 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7248 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7249 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7251 if (processing_template_decl
7252 && (type_dependent_object_expression_p (instance
)
7253 || (!BASELINK_P (fn
)
7254 && TREE_CODE (fn
) != FIELD_DECL
)
7255 || type_dependent_expression_p (fn
)
7256 || any_type_dependent_arguments_p (args
)))
7258 maybe_generic_this_capture (instance
, fn
);
7260 = build_min_nt_call_vec (postfix_expression
, args
);
7261 release_tree_vector (args
);
7265 if (BASELINK_P (fn
))
7268 = (build_new_method_call
7269 (instance
, fn
, &args
, NULL_TREE
,
7270 (idk
== CP_ID_KIND_QUALIFIED
7271 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7278 = finish_call_expr (postfix_expression
, &args
,
7279 /*disallow_virtual=*/false,
7283 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7284 || TREE_CODE (postfix_expression
) == MEMBER_REF
7285 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7286 postfix_expression
= (build_offset_ref_call_from_tree
7287 (postfix_expression
, &args
,
7289 else if (idk
== CP_ID_KIND_QUALIFIED
)
7290 /* A call to a static class member, or a namespace-scope
7293 = finish_call_expr (postfix_expression
, &args
,
7294 /*disallow_virtual=*/true,
7298 /* All other function calls. */
7300 = finish_call_expr (postfix_expression
, &args
,
7301 /*disallow_virtual=*/false,
7305 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7307 location_t combined_loc
= make_location (token
->location
,
7310 postfix_expression
.set_location (combined_loc
);
7313 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7314 idk
= CP_ID_KIND_NONE
;
7316 release_tree_vector (args
);
7322 /* postfix-expression . template [opt] id-expression
7323 postfix-expression . pseudo-destructor-name
7324 postfix-expression -> template [opt] id-expression
7325 postfix-expression -> pseudo-destructor-name */
7327 /* Consume the `.' or `->' operator. */
7328 cp_lexer_consume_token (parser
->lexer
);
7331 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7335 is_member_access
= true;
7339 /* postfix-expression ++ */
7340 /* Consume the `++' token. */
7341 cp_lexer_consume_token (parser
->lexer
);
7342 /* Generate a representation for the complete expression. */
7344 = finish_increment_expr (postfix_expression
,
7345 POSTINCREMENT_EXPR
);
7346 /* Increments may not appear in constant-expressions. */
7347 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7348 postfix_expression
= error_mark_node
;
7349 idk
= CP_ID_KIND_NONE
;
7350 is_member_access
= false;
7353 case CPP_MINUS_MINUS
:
7354 /* postfix-expression -- */
7355 /* Consume the `--' token. */
7356 cp_lexer_consume_token (parser
->lexer
);
7357 /* Generate a representation for the complete expression. */
7359 = finish_increment_expr (postfix_expression
,
7360 POSTDECREMENT_EXPR
);
7361 /* Decrements may not appear in constant-expressions. */
7362 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7363 postfix_expression
= error_mark_node
;
7364 idk
= CP_ID_KIND_NONE
;
7365 is_member_access
= false;
7369 if (pidk_return
!= NULL
)
7370 * pidk_return
= idk
;
7371 if (member_access_only_p
)
7372 return is_member_access
7373 ? postfix_expression
7374 : cp_expr (error_mark_node
);
7376 return postfix_expression
;
7380 /* We should never get here. */
7382 return error_mark_node
;
7385 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7386 by cp_parser_builtin_offsetof. We're looking for
7388 postfix-expression [ expression ]
7389 postfix-expression [ braced-init-list ] (C++11)
7391 FOR_OFFSETOF is set if we're being called in that context, which
7392 changes how we deal with integer constant expressions. */
7395 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7396 tree postfix_expression
,
7400 tree index
= NULL_TREE
;
7401 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7402 bool saved_greater_than_is_operator_p
;
7404 /* Consume the `[' token. */
7405 cp_lexer_consume_token (parser
->lexer
);
7407 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7408 parser
->greater_than_is_operator_p
= true;
7410 /* Parse the index expression. */
7411 /* ??? For offsetof, there is a question of what to allow here. If
7412 offsetof is not being used in an integral constant expression context,
7413 then we *could* get the right answer by computing the value at runtime.
7414 If we are in an integral constant expression context, then we might
7415 could accept any constant expression; hard to say without analysis.
7416 Rather than open the barn door too wide right away, allow only integer
7417 constant expressions here. */
7419 index
= cp_parser_constant_expression (parser
);
7422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7424 bool expr_nonconst_p
;
7425 cp_lexer_set_source_position (parser
->lexer
);
7426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7427 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7430 index
= cp_parser_expression (parser
);
7433 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7435 /* Look for the closing `]'. */
7436 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7438 /* Build the ARRAY_REF. */
7439 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7442 /* When not doing offsetof, array references are not permitted in
7443 constant-expressions. */
7445 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7446 postfix_expression
= error_mark_node
;
7448 return postfix_expression
;
7451 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7452 dereference of incomplete type, returns true if error_mark_node should
7453 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7454 and *DEPENDENT_P. */
7457 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7460 /* In a template, be permissive by treating an object expression
7461 of incomplete type as dependent (after a pedwarn). */
7462 diagnostic_t kind
= (processing_template_decl
7463 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7465 switch (TREE_CODE (*postfix_expression
))
7468 case REINTERPRET_CAST_EXPR
:
7469 case CONST_CAST_EXPR
:
7470 case STATIC_CAST_EXPR
:
7471 case DYNAMIC_CAST_EXPR
:
7472 case IMPLICIT_CONV_EXPR
:
7473 case VIEW_CONVERT_EXPR
:
7474 case NON_LVALUE_EXPR
:
7478 /* Don't emit any diagnostic for OVERLOADs. */
7482 /* Avoid clobbering e.g. DECLs. */
7483 if (!EXPR_P (*postfix_expression
))
7488 if (kind
== DK_IGNORED
)
7491 location_t exploc
= location_of (*postfix_expression
);
7492 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7493 if (!MAYBE_CLASS_TYPE_P (*scope
))
7495 if (kind
== DK_ERROR
)
7496 *scope
= *postfix_expression
= error_mark_node
;
7497 else if (processing_template_decl
)
7499 *dependent_p
= true;
7500 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7505 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7506 by cp_parser_builtin_offsetof. We're looking for
7508 postfix-expression . template [opt] id-expression
7509 postfix-expression . pseudo-destructor-name
7510 postfix-expression -> template [opt] id-expression
7511 postfix-expression -> pseudo-destructor-name
7513 FOR_OFFSETOF is set if we're being called in that context. That sorta
7514 limits what of the above we'll actually accept, but nevermind.
7515 TOKEN_TYPE is the "." or "->" token, which will already have been
7516 removed from the stream. */
7519 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7520 enum cpp_ttype token_type
,
7521 cp_expr postfix_expression
,
7522 bool for_offsetof
, cp_id_kind
*idk
,
7523 location_t location
)
7527 bool pseudo_destructor_p
;
7528 tree scope
= NULL_TREE
;
7529 location_t start_loc
= postfix_expression
.get_start ();
7531 /* If this is a `->' operator, dereference the pointer. */
7532 if (token_type
== CPP_DEREF
)
7533 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7534 tf_warning_or_error
);
7535 /* Check to see whether or not the expression is type-dependent and
7536 not the current instantiation. */
7537 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7538 /* The identifier following the `->' or `.' is not qualified. */
7539 parser
->scope
= NULL_TREE
;
7540 parser
->qualifying_scope
= NULL_TREE
;
7541 parser
->object_scope
= NULL_TREE
;
7542 *idk
= CP_ID_KIND_NONE
;
7544 /* Enter the scope corresponding to the type of the object
7545 given by the POSTFIX_EXPRESSION. */
7548 scope
= TREE_TYPE (postfix_expression
);
7549 /* According to the standard, no expression should ever have
7550 reference type. Unfortunately, we do not currently match
7551 the standard in this respect in that our internal representation
7552 of an expression may have reference type even when the standard
7553 says it does not. Therefore, we have to manually obtain the
7554 underlying type here. */
7555 scope
= non_reference (scope
);
7556 /* The type of the POSTFIX_EXPRESSION must be complete. */
7557 /* Unlike the object expression in other contexts, *this is not
7558 required to be of complete type for purposes of class member
7559 access (5.2.5) outside the member function body. */
7560 if (postfix_expression
!= current_class_ref
7561 && scope
!= error_mark_node
7562 && !currently_open_class (scope
))
7564 scope
= complete_type (scope
);
7565 if (!COMPLETE_TYPE_P (scope
)
7566 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7568 return error_mark_node
;
7573 /* Let the name lookup machinery know that we are processing a
7574 class member access expression. */
7575 parser
->context
->object_type
= scope
;
7576 /* If something went wrong, we want to be able to discern that case,
7577 as opposed to the case where there was no SCOPE due to the type
7578 of expression being dependent. */
7580 scope
= error_mark_node
;
7581 /* If the SCOPE was erroneous, make the various semantic analysis
7582 functions exit quickly -- and without issuing additional error
7584 if (scope
== error_mark_node
)
7585 postfix_expression
= error_mark_node
;
7590 /* Tell cp_parser_lookup_name that there was an object, even though it's
7592 parser
->context
->object_type
= unknown_type_node
;
7594 /* Assume this expression is not a pseudo-destructor access. */
7595 pseudo_destructor_p
= false;
7597 /* If the SCOPE is a scalar type, then, if this is a valid program,
7598 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7599 is type dependent, it can be pseudo-destructor-name or something else.
7600 Try to parse it as pseudo-destructor-name first. */
7601 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7606 cp_parser_parse_tentatively (parser
);
7607 /* Parse the pseudo-destructor-name. */
7609 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7612 && (cp_parser_error_occurred (parser
)
7613 || !SCALAR_TYPE_P (type
)))
7614 cp_parser_abort_tentative_parse (parser
);
7615 else if (cp_parser_parse_definitely (parser
))
7617 pseudo_destructor_p
= true;
7619 = finish_pseudo_destructor_expr (postfix_expression
,
7624 if (!pseudo_destructor_p
)
7626 /* If the SCOPE is not a scalar type, we are looking at an
7627 ordinary class member access expression, rather than a
7628 pseudo-destructor-name. */
7630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7631 /* Parse the id-expression. */
7632 name
= (cp_parser_id_expression
7634 cp_parser_optional_template_keyword (parser
),
7635 /*check_dependency_p=*/true,
7637 /*declarator_p=*/false,
7638 /*optional_p=*/false));
7639 /* In general, build a SCOPE_REF if the member name is qualified.
7640 However, if the name was not dependent and has already been
7641 resolved; there is no need to build the SCOPE_REF. For example;
7643 struct X { void f(); };
7644 template <typename T> void f(T* t) { t->X::f(); }
7646 Even though "t" is dependent, "X::f" is not and has been resolved
7647 to a BASELINK; there is no need to include scope information. */
7649 /* But we do need to remember that there was an explicit scope for
7650 virtual function calls. */
7652 *idk
= CP_ID_KIND_QUALIFIED
;
7654 /* If the name is a template-id that names a type, we will get a
7655 TYPE_DECL here. That is invalid code. */
7656 if (TREE_CODE (name
) == TYPE_DECL
)
7658 error_at (token
->location
, "invalid use of %qD", name
);
7659 postfix_expression
= error_mark_node
;
7663 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7665 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7667 error_at (token
->location
, "%<%D::%D%> is not a class member",
7668 parser
->scope
, name
);
7669 postfix_expression
= error_mark_node
;
7672 name
= build_qualified_name (/*type=*/NULL_TREE
,
7676 parser
->scope
= NULL_TREE
;
7677 parser
->qualifying_scope
= NULL_TREE
;
7678 parser
->object_scope
= NULL_TREE
;
7680 if (parser
->scope
&& name
&& BASELINK_P (name
))
7681 adjust_result_of_qualified_name_lookup
7682 (name
, parser
->scope
, scope
);
7684 = finish_class_member_access_expr (postfix_expression
, name
,
7686 tf_warning_or_error
);
7687 /* Build a location e.g.:
7690 where the caret is at the deref token, ranging from
7691 the start of postfix_expression to the end of the access expr. */
7693 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7694 location_t combined_loc
7695 = make_location (input_location
, start_loc
, end_loc
);
7696 protected_set_expr_location (postfix_expression
, combined_loc
);
7700 /* We no longer need to look up names in the scope of the object on
7701 the left-hand side of the `.' or `->' operator. */
7702 parser
->context
->object_type
= NULL_TREE
;
7704 /* Outside of offsetof, these operators may not appear in
7705 constant-expressions. */
7707 && (cp_parser_non_integral_constant_expression
7708 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7709 postfix_expression
= error_mark_node
;
7711 return postfix_expression
;
7714 /* Parse a parenthesized expression-list.
7717 assignment-expression
7718 expression-list, assignment-expression
7723 identifier, expression-list
7725 CAST_P is true if this expression is the target of a cast.
7727 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7730 WRAP_LOCATIONS_P is true if expressions within this list for which
7731 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7732 their source locations.
7734 Returns a vector of trees. Each element is a representation of an
7735 assignment-expression. NULL is returned if the ( and or ) are
7736 missing. An empty, but allocated, vector is returned on no
7737 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7738 if we are parsing an attribute list for an attribute that wants a
7739 plain identifier argument, normal_attr for an attribute that wants
7740 an expression, or non_attr if we aren't parsing an attribute list. If
7741 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7742 not all of the expressions in the list were constant.
7743 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7744 will be written to with the location of the closing parenthesis. If
7745 an error occurs, it may or may not be written to. */
7747 static vec
<tree
, va_gc
> *
7748 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7749 int is_attribute_list
,
7751 bool allow_expansion_p
,
7752 bool *non_constant_p
,
7753 location_t
*close_paren_loc
,
7754 bool wrap_locations_p
)
7756 vec
<tree
, va_gc
> *expression_list
;
7757 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7758 tree identifier
= NULL_TREE
;
7759 bool saved_greater_than_is_operator_p
;
7761 /* Assume all the expressions will be constant. */
7763 *non_constant_p
= false;
7765 matching_parens parens
;
7766 if (!parens
.require_open (parser
))
7769 expression_list
= make_tree_vector ();
7771 /* Within a parenthesized expression, a `>' token is always
7772 the greater-than operator. */
7773 saved_greater_than_is_operator_p
7774 = parser
->greater_than_is_operator_p
;
7775 parser
->greater_than_is_operator_p
= true;
7777 cp_expr
expr (NULL_TREE
);
7779 /* Consume expressions until there are no more. */
7780 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7783 /* At the beginning of attribute lists, check to see if the
7784 next token is an identifier. */
7785 if (is_attribute_list
== id_attr
7786 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7790 /* Consume the identifier. */
7791 token
= cp_lexer_consume_token (parser
->lexer
);
7792 /* Save the identifier. */
7793 identifier
= token
->u
.value
;
7797 bool expr_non_constant_p
;
7799 /* Parse the next assignment-expression. */
7800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7802 /* A braced-init-list. */
7803 cp_lexer_set_source_position (parser
->lexer
);
7804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7805 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7806 if (non_constant_p
&& expr_non_constant_p
)
7807 *non_constant_p
= true;
7809 else if (non_constant_p
)
7811 expr
= (cp_parser_constant_expression
7812 (parser
, /*allow_non_constant_p=*/true,
7813 &expr_non_constant_p
));
7814 if (expr_non_constant_p
)
7815 *non_constant_p
= true;
7818 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7822 expr
= instantiate_non_dependent_expr (expr
);
7824 /* If we have an ellipsis, then this is an expression
7826 if (allow_expansion_p
7827 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7829 /* Consume the `...'. */
7830 cp_lexer_consume_token (parser
->lexer
);
7832 /* Build the argument pack. */
7833 expr
= make_pack_expansion (expr
);
7836 if (wrap_locations_p
)
7837 expr
.maybe_add_location_wrapper ();
7839 /* Add it to the list. We add error_mark_node
7840 expressions to the list, so that we can still tell if
7841 the correct form for a parenthesized expression-list
7842 is found. That gives better errors. */
7843 vec_safe_push (expression_list
, expr
.get_value ());
7845 if (expr
== error_mark_node
)
7849 /* After the first item, attribute lists look the same as
7850 expression lists. */
7851 is_attribute_list
= non_attr
;
7854 /* If the next token isn't a `,', then we are done. */
7855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7858 /* Otherwise, consume the `,' and keep going. */
7859 cp_lexer_consume_token (parser
->lexer
);
7862 if (close_paren_loc
)
7863 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7865 if (!parens
.require_close (parser
))
7870 /* We try and resync to an unnested comma, as that will give the
7871 user better diagnostics. */
7872 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7873 /*recovering=*/true,
7875 /*consume_paren=*/true);
7880 parser
->greater_than_is_operator_p
7881 = saved_greater_than_is_operator_p
;
7886 parser
->greater_than_is_operator_p
7887 = saved_greater_than_is_operator_p
;
7890 vec_safe_insert (expression_list
, 0, identifier
);
7892 return expression_list
;
7895 /* Parse a pseudo-destructor-name.
7897 pseudo-destructor-name:
7898 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7899 :: [opt] nested-name-specifier template template-id :: ~ type-name
7900 :: [opt] nested-name-specifier [opt] ~ type-name
7902 If either of the first two productions is used, sets *SCOPE to the
7903 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7904 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7905 or ERROR_MARK_NODE if the parse fails. */
7908 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7913 bool nested_name_specifier_p
;
7916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7917 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7918 && !type_dependent_expression_p (object
))
7920 if (cxx_dialect
< cxx14
)
7921 pedwarn (input_location
, 0,
7922 "%<~auto%> only available with "
7923 "-std=c++14 or -std=gnu++14");
7924 cp_lexer_consume_token (parser
->lexer
);
7925 cp_lexer_consume_token (parser
->lexer
);
7927 *type
= TREE_TYPE (object
);
7931 /* Assume that things will not work out. */
7932 *type
= error_mark_node
;
7934 /* Look for the optional `::' operator. */
7935 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7936 /* Look for the optional nested-name-specifier. */
7937 nested_name_specifier_p
7938 = (cp_parser_nested_name_specifier_opt (parser
,
7939 /*typename_keyword_p=*/false,
7940 /*check_dependency_p=*/true,
7942 /*is_declaration=*/false)
7944 /* Now, if we saw a nested-name-specifier, we might be doing the
7945 second production. */
7946 if (nested_name_specifier_p
7947 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7949 /* Consume the `template' keyword. */
7950 cp_lexer_consume_token (parser
->lexer
);
7951 /* Parse the template-id. */
7952 cp_parser_template_id (parser
,
7953 /*template_keyword_p=*/true,
7954 /*check_dependency_p=*/false,
7956 /*is_declaration=*/true);
7957 /* Look for the `::' token. */
7958 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7960 /* If the next token is not a `~', then there might be some
7961 additional qualification. */
7962 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7964 /* At this point, we're looking for "type-name :: ~". The type-name
7965 must not be a class-name, since this is a pseudo-destructor. So,
7966 it must be either an enum-name, or a typedef-name -- both of which
7967 are just identifiers. So, we peek ahead to check that the "::"
7968 and "~" tokens are present; if they are not, then we can avoid
7969 calling type_name. */
7970 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7971 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7972 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7974 cp_parser_error (parser
, "non-scalar type");
7978 /* Look for the type-name. */
7979 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7980 if (*scope
== error_mark_node
)
7983 /* Look for the `::' token. */
7984 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7989 /* Look for the `~'. */
7990 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7992 /* Once we see the ~, this has to be a pseudo-destructor. */
7993 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7994 cp_parser_commit_to_topmost_tentative_parse (parser
);
7996 /* Look for the type-name again. We are not responsible for
7997 checking that it matches the first type-name. */
7998 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8001 /* Parse a unary-expression.
8007 unary-operator cast-expression
8008 sizeof unary-expression
8010 alignof ( type-id ) [C++0x]
8017 __extension__ cast-expression
8018 __alignof__ unary-expression
8019 __alignof__ ( type-id )
8020 alignof unary-expression [C++0x]
8021 __real__ cast-expression
8022 __imag__ cast-expression
8024 sizeof ( type-id ) { initializer-list , [opt] }
8025 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8026 __alignof__ ( type-id ) { initializer-list , [opt] }
8028 ADDRESS_P is true iff the unary-expression is appearing as the
8029 operand of the `&' operator. CAST_P is true if this expression is
8030 the target of a cast.
8032 Returns a representation of the expression. */
8035 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8036 bool address_p
, bool cast_p
, bool decltype_p
)
8039 enum tree_code unary_operator
;
8041 /* Peek at the next token. */
8042 token
= cp_lexer_peek_token (parser
->lexer
);
8043 /* Some keywords give away the kind of expression. */
8044 if (token
->type
== CPP_KEYWORD
)
8046 enum rid keyword
= token
->keyword
;
8055 location_t start_loc
= token
->location
;
8057 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8058 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8060 /* Consume the token. */
8061 cp_lexer_consume_token (parser
->lexer
);
8062 /* Parse the operand. */
8063 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8065 if (TYPE_P (operand
))
8066 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8074 pedwarn (token
->location
, OPT_Wpedantic
,
8075 "ISO C++ does not allow %<alignof%> "
8078 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8084 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8086 if (!processing_template_decl
&& TYPE_P (operand
))
8088 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8089 build1 (NOP_EXPR
, operand
,
8091 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8094 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8095 TREE_SIDE_EFFECTS (ret
) = 0;
8096 TREE_READONLY (ret
) = 1;
8100 /* Construct a location e.g. :
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser
->lexer
)->location
;
8107 location_t compound_loc
8108 = make_location (start_loc
, start_loc
, finish_loc
);
8110 cp_expr
ret_expr (ret
);
8111 ret_expr
.set_location (compound_loc
);
8112 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8116 case RID_BUILTIN_HAS_ATTRIBUTE
:
8117 return cp_parser_has_attribute_expression (parser
);
8120 return cp_parser_new_expression (parser
);
8123 return cp_parser_delete_expression (parser
);
8127 /* The saved value of the PEDANTIC flag. */
8131 /* Save away the PEDANTIC flag. */
8132 cp_parser_extension_opt (parser
, &saved_pedantic
);
8133 /* Parse the cast-expression. */
8134 expr
= cp_parser_simple_cast_expression (parser
);
8135 /* Restore the PEDANTIC flag. */
8136 pedantic
= saved_pedantic
;
8146 /* Consume the `__real__' or `__imag__' token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 /* Parse the cast-expression. */
8149 expression
= cp_parser_simple_cast_expression (parser
);
8150 /* Create the complete representation. */
8151 return build_x_unary_op (token
->location
,
8152 (keyword
== RID_REALPART
8153 ? REALPART_EXPR
: IMAGPART_EXPR
),
8155 tf_warning_or_error
);
8159 case RID_TRANSACTION_ATOMIC
:
8160 case RID_TRANSACTION_RELAXED
:
8161 return cp_parser_transaction_expression (parser
, keyword
);
8166 const char *saved_message
;
8167 bool saved_integral_constant_expression_p
;
8168 bool saved_non_integral_constant_expression_p
;
8169 bool saved_greater_than_is_operator_p
;
8171 location_t start_loc
= token
->location
;
8173 cp_lexer_consume_token (parser
->lexer
);
8174 matching_parens parens
;
8175 parens
.require_open (parser
);
8177 saved_message
= parser
->type_definition_forbidden_message
;
8178 parser
->type_definition_forbidden_message
8179 = G_("types may not be defined in %<noexcept%> expressions");
8181 saved_integral_constant_expression_p
8182 = parser
->integral_constant_expression_p
;
8183 saved_non_integral_constant_expression_p
8184 = parser
->non_integral_constant_expression_p
;
8185 parser
->integral_constant_expression_p
= false;
8187 saved_greater_than_is_operator_p
8188 = parser
->greater_than_is_operator_p
;
8189 parser
->greater_than_is_operator_p
= true;
8191 ++cp_unevaluated_operand
;
8192 ++c_inhibit_evaluation_warnings
;
8193 ++cp_noexcept_operand
;
8194 expr
= cp_parser_expression (parser
);
8195 --cp_noexcept_operand
;
8196 --c_inhibit_evaluation_warnings
;
8197 --cp_unevaluated_operand
;
8199 parser
->greater_than_is_operator_p
8200 = saved_greater_than_is_operator_p
;
8202 parser
->integral_constant_expression_p
8203 = saved_integral_constant_expression_p
;
8204 parser
->non_integral_constant_expression_p
8205 = saved_non_integral_constant_expression_p
;
8207 parser
->type_definition_forbidden_message
= saved_message
;
8209 location_t finish_loc
8210 = cp_lexer_peek_token (parser
->lexer
)->location
;
8211 parens
.require_close (parser
);
8213 /* Construct a location of the form:
8216 with start == caret, finishing at the close-paren. */
8217 location_t noexcept_loc
8218 = make_location (start_loc
, start_loc
, finish_loc
);
8220 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8229 /* Look for the `:: new' and `:: delete', which also signal the
8230 beginning of a new-expression, or delete-expression,
8231 respectively. If the next token is `::', then it might be one of
8233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8237 /* See if the token after the `::' is one of the keywords in
8238 which we're interested. */
8239 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8240 /* If it's `new', we have a new-expression. */
8241 if (keyword
== RID_NEW
)
8242 return cp_parser_new_expression (parser
);
8243 /* Similarly, for `delete'. */
8244 else if (keyword
== RID_DELETE
)
8245 return cp_parser_delete_expression (parser
);
8248 /* Look for a unary operator. */
8249 unary_operator
= cp_parser_unary_operator (token
);
8250 /* The `++' and `--' operators can be handled similarly, even though
8251 they are not technically unary-operators in the grammar. */
8252 if (unary_operator
== ERROR_MARK
)
8254 if (token
->type
== CPP_PLUS_PLUS
)
8255 unary_operator
= PREINCREMENT_EXPR
;
8256 else if (token
->type
== CPP_MINUS_MINUS
)
8257 unary_operator
= PREDECREMENT_EXPR
;
8258 /* Handle the GNU address-of-label extension. */
8259 else if (cp_parser_allow_gnu_extensions_p (parser
)
8260 && token
->type
== CPP_AND_AND
)
8264 location_t start_loc
= token
->location
;
8266 /* Consume the '&&' token. */
8267 cp_lexer_consume_token (parser
->lexer
);
8268 /* Look for the identifier. */
8269 location_t finish_loc
8270 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8271 identifier
= cp_parser_identifier (parser
);
8272 /* Construct a location of the form:
8275 with caret==start at the "&&", finish at the end of the label. */
8276 location_t combined_loc
8277 = make_location (start_loc
, start_loc
, finish_loc
);
8278 /* Create an expression representing the address. */
8279 expression
= finish_label_address_expr (identifier
, combined_loc
);
8280 if (cp_parser_non_integral_constant_expression (parser
,
8282 expression
= error_mark_node
;
8286 if (unary_operator
!= ERROR_MARK
)
8288 cp_expr cast_expression
;
8289 cp_expr expression
= error_mark_node
;
8290 non_integral_constant non_constant_p
= NIC_NONE
;
8291 location_t loc
= token
->location
;
8292 tsubst_flags_t complain
= complain_flags (decltype_p
);
8294 /* Consume the operator token. */
8295 token
= cp_lexer_consume_token (parser
->lexer
);
8296 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8298 /* Parse the cast-expression. */
8300 = cp_parser_cast_expression (parser
,
8301 unary_operator
== ADDR_EXPR
,
8307 OP_TOKEN CAST_EXPRESSION
8308 ^~~~~~~~~~~~~~~~~~~~~~~~~
8309 with start==caret at the operator token, and
8310 extending to the end of the cast_expression. */
8311 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8313 /* Now, build an appropriate representation. */
8314 switch (unary_operator
)
8317 non_constant_p
= NIC_STAR
;
8318 expression
= build_x_indirect_ref (loc
, cast_expression
,
8321 /* TODO: build_x_indirect_ref does not always honor the
8322 location, so ensure it is set. */
8323 expression
.set_location (loc
);
8327 non_constant_p
= NIC_ADDR
;
8330 expression
= build_x_unary_op (loc
, unary_operator
,
8333 /* TODO: build_x_unary_op does not always honor the location,
8334 so ensure it is set. */
8335 expression
.set_location (loc
);
8338 case PREINCREMENT_EXPR
:
8339 case PREDECREMENT_EXPR
:
8340 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8341 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8344 /* Immediately fold negation of a constant, unless the constant is 0
8345 (since -0 == 0) or it would overflow. */
8346 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8347 && CONSTANT_CLASS_P (cast_expression
)
8348 && !integer_zerop (cast_expression
)
8349 && !TREE_OVERFLOW (cast_expression
))
8351 tree folded
= fold_build1 (unary_operator
,
8352 TREE_TYPE (cast_expression
),
8354 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8356 expression
= cp_expr (folded
, loc
);
8361 case UNARY_PLUS_EXPR
:
8362 case TRUTH_NOT_EXPR
:
8363 expression
= finish_unary_op_expr (loc
, unary_operator
,
8364 cast_expression
, complain
);
8371 if (non_constant_p
!= NIC_NONE
8372 && cp_parser_non_integral_constant_expression (parser
,
8374 expression
= error_mark_node
;
8379 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8380 /*member_access_only_p=*/false,
8385 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8386 unary-operator, the corresponding tree code is returned. */
8388 static enum tree_code
8389 cp_parser_unary_operator (cp_token
* token
)
8391 switch (token
->type
)
8394 return INDIRECT_REF
;
8400 return UNARY_PLUS_EXPR
;
8406 return TRUTH_NOT_EXPR
;
8409 return BIT_NOT_EXPR
;
8416 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8417 Returns a representation of the expression. */
8420 cp_parser_has_attribute_expression (cp_parser
*parser
)
8422 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8424 /* Consume the __builtin_has_attribute token. */
8425 cp_lexer_consume_token (parser
->lexer
);
8427 matching_parens parens
;
8428 if (!parens
.require_open (parser
))
8429 return error_mark_node
;
8431 /* Types cannot be defined in a `sizeof' expression. Save away the
8433 const char *saved_message
= parser
->type_definition_forbidden_message
;
8434 /* And create the new one. */
8435 const int kwd
= RID_BUILTIN_HAS_ATTRIBUTE
;
8436 char *tmp
= concat ("types may not be defined in %<",
8437 IDENTIFIER_POINTER (ridpointers
[kwd
]),
8438 "%> expressions", NULL
);
8439 parser
->type_definition_forbidden_message
= tmp
;
8441 /* The restrictions on constant-expressions do not apply inside
8442 sizeof expressions. */
8443 bool saved_integral_constant_expression_p
8444 = parser
->integral_constant_expression_p
;
8445 bool saved_non_integral_constant_expression_p
8446 = parser
->non_integral_constant_expression_p
;
8447 parser
->integral_constant_expression_p
= false;
8449 /* Do not actually evaluate the expression. */
8450 ++cp_unevaluated_operand
;
8451 ++c_inhibit_evaluation_warnings
;
8453 tree oper
= NULL_TREE
;
8455 /* We can't be sure yet whether we're looking at a type-id or an
8457 cp_parser_parse_tentatively (parser
);
8459 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8460 parser
->in_type_id_in_expr_p
= true;
8461 /* Look for the type-id. */
8462 oper
= cp_parser_type_id (parser
);
8463 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8465 cp_parser_parse_definitely (parser
);
8467 /* If the type-id production did not work out, then we must be
8468 looking at the unary-expression production. */
8469 if (!oper
|| oper
== error_mark_node
)
8470 oper
= cp_parser_unary_expression (parser
);
8472 /* Go back to evaluating expressions. */
8473 --cp_unevaluated_operand
;
8474 --c_inhibit_evaluation_warnings
;
8476 /* Free the message we created. */
8478 /* And restore the old one. */
8479 parser
->type_definition_forbidden_message
= saved_message
;
8480 parser
->integral_constant_expression_p
8481 = saved_integral_constant_expression_p
;
8482 parser
->non_integral_constant_expression_p
8483 = saved_non_integral_constant_expression_p
;
8485 /* Consume the comma if it's there. */
8486 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8488 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8489 /*consume_paren=*/true);
8490 return error_mark_node
;
8493 /* Parse the attribute specification. */
8495 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8496 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8498 if (oper
!= error_mark_node
)
8500 /* Fold constant expressions used in attributes first. */
8501 cp_check_const_attributes (attr
);
8503 /* Finally, see if OPER has been declared with ATTR. */
8504 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8507 parens
.require_close (parser
);
8511 error_at (atloc
, "expected identifier");
8512 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8515 /* Construct a location e.g. :
8516 __builtin_has_attribute (oper, attr)
8517 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8518 with start == caret at the start of the built-in token,
8519 and with the endpoint at the final closing paren. */
8520 location_t finish_loc
8521 = cp_lexer_previous_token (parser
->lexer
)->location
;
8522 location_t compound_loc
8523 = make_location (start_loc
, start_loc
, finish_loc
);
8525 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8526 ret_expr
.set_location (compound_loc
);
8527 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8531 /* Parse a new-expression.
8534 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8535 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8537 Returns a representation of the expression. */
8540 cp_parser_new_expression (cp_parser
* parser
)
8542 bool global_scope_p
;
8543 vec
<tree
, va_gc
> *placement
;
8545 vec
<tree
, va_gc
> *initializer
;
8546 tree nelts
= NULL_TREE
;
8549 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8551 /* Look for the optional `::' operator. */
8553 = (cp_parser_global_scope_opt (parser
,
8554 /*current_scope_valid_p=*/false)
8556 /* Look for the `new' operator. */
8557 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8558 /* There's no easy way to tell a new-placement from the
8559 `( type-id )' construct. */
8560 cp_parser_parse_tentatively (parser
);
8561 /* Look for a new-placement. */
8562 placement
= cp_parser_new_placement (parser
);
8563 /* If that didn't work out, there's no new-placement. */
8564 if (!cp_parser_parse_definitely (parser
))
8566 if (placement
!= NULL
)
8567 release_tree_vector (placement
);
8571 /* If the next token is a `(', then we have a parenthesized
8573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8576 const char *saved_message
= parser
->type_definition_forbidden_message
;
8578 /* Consume the `('. */
8579 matching_parens parens
;
8580 parens
.consume_open (parser
);
8582 /* Parse the type-id. */
8583 parser
->type_definition_forbidden_message
8584 = G_("types may not be defined in a new-expression");
8586 type_id_in_expr_sentinel
s (parser
);
8587 type
= cp_parser_type_id (parser
);
8589 parser
->type_definition_forbidden_message
= saved_message
;
8591 /* Look for the closing `)'. */
8592 parens
.require_close (parser
);
8593 token
= cp_lexer_peek_token (parser
->lexer
);
8594 /* There should not be a direct-new-declarator in this production,
8595 but GCC used to allowed this, so we check and emit a sensible error
8596 message for this case. */
8597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8599 error_at (token
->location
,
8600 "array bound forbidden after parenthesized type-id");
8601 inform (token
->location
,
8602 "try removing the parentheses around the type-id");
8603 cp_parser_direct_new_declarator (parser
);
8606 /* Otherwise, there must be a new-type-id. */
8608 type
= cp_parser_new_type_id (parser
, &nelts
);
8610 /* If the next token is a `(' or '{', then we have a new-initializer. */
8611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8612 if (token
->type
== CPP_OPEN_PAREN
8613 || token
->type
== CPP_OPEN_BRACE
)
8614 initializer
= cp_parser_new_initializer (parser
);
8618 /* A new-expression may not appear in an integral constant
8620 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8621 ret
= error_mark_node
;
8622 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8623 of a new-type-id or type-id of a new-expression, the new-expression shall
8624 contain a new-initializer of the form ( assignment-expression )".
8625 Additionally, consistently with the spirit of DR 1467, we want to accept
8626 'new auto { 2 }' too. */
8627 else if ((ret
= type_uses_auto (type
))
8628 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8629 && (vec_safe_length (initializer
) != 1
8630 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8631 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8633 error_at (token
->location
,
8634 "initialization of new-expression for type %<auto%> "
8635 "requires exactly one element");
8636 ret
= error_mark_node
;
8640 /* Construct a location e.g.:
8643 with caret == start at the start of the "new" token, and the end
8644 at the end of the final token we consumed. */
8645 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8646 location_t end_loc
= get_finish (end_tok
->location
);
8647 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8649 /* Create a representation of the new-expression. */
8650 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8651 tf_warning_or_error
);
8652 protected_set_expr_location (ret
, combined_loc
);
8655 if (placement
!= NULL
)
8656 release_tree_vector (placement
);
8657 if (initializer
!= NULL
)
8658 release_tree_vector (initializer
);
8663 /* Parse a new-placement.
8668 Returns the same representation as for an expression-list. */
8670 static vec
<tree
, va_gc
> *
8671 cp_parser_new_placement (cp_parser
* parser
)
8673 vec
<tree
, va_gc
> *expression_list
;
8675 /* Parse the expression-list. */
8676 expression_list
= (cp_parser_parenthesized_expression_list
8677 (parser
, non_attr
, /*cast_p=*/false,
8678 /*allow_expansion_p=*/true,
8679 /*non_constant_p=*/NULL
));
8681 if (expression_list
&& expression_list
->is_empty ())
8682 error ("expected expression-list or type-id");
8684 return expression_list
;
8687 /* Parse a new-type-id.
8690 type-specifier-seq new-declarator [opt]
8692 Returns the TYPE allocated. If the new-type-id indicates an array
8693 type, *NELTS is set to the number of elements in the last array
8694 bound; the TYPE will not include the last array bound. */
8697 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8699 cp_decl_specifier_seq type_specifier_seq
;
8700 cp_declarator
*new_declarator
;
8701 cp_declarator
*declarator
;
8702 cp_declarator
*outer_declarator
;
8703 const char *saved_message
;
8705 /* The type-specifier sequence must not contain type definitions.
8706 (It cannot contain declarations of new types either, but if they
8707 are not definitions we will catch that because they are not
8709 saved_message
= parser
->type_definition_forbidden_message
;
8710 parser
->type_definition_forbidden_message
8711 = G_("types may not be defined in a new-type-id");
8712 /* Parse the type-specifier-seq. */
8713 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8714 /*is_trailing_return=*/false,
8715 &type_specifier_seq
);
8716 /* Restore the old message. */
8717 parser
->type_definition_forbidden_message
= saved_message
;
8719 if (type_specifier_seq
.type
== error_mark_node
)
8720 return error_mark_node
;
8722 /* Parse the new-declarator. */
8723 new_declarator
= cp_parser_new_declarator_opt (parser
);
8725 /* Determine the number of elements in the last array dimension, if
8728 /* Skip down to the last array dimension. */
8729 declarator
= new_declarator
;
8730 outer_declarator
= NULL
;
8731 while (declarator
&& (declarator
->kind
== cdk_pointer
8732 || declarator
->kind
== cdk_ptrmem
))
8734 outer_declarator
= declarator
;
8735 declarator
= declarator
->declarator
;
8738 && declarator
->kind
== cdk_array
8739 && declarator
->declarator
8740 && declarator
->declarator
->kind
== cdk_array
)
8742 outer_declarator
= declarator
;
8743 declarator
= declarator
->declarator
;
8746 if (declarator
&& declarator
->kind
== cdk_array
)
8748 *nelts
= declarator
->u
.array
.bounds
;
8749 if (*nelts
== error_mark_node
)
8750 *nelts
= integer_one_node
;
8752 if (outer_declarator
)
8753 outer_declarator
->declarator
= declarator
->declarator
;
8755 new_declarator
= NULL
;
8758 return groktypename (&type_specifier_seq
, new_declarator
, false);
8761 /* Parse an (optional) new-declarator.
8764 ptr-operator new-declarator [opt]
8765 direct-new-declarator
8767 Returns the declarator. */
8769 static cp_declarator
*
8770 cp_parser_new_declarator_opt (cp_parser
* parser
)
8772 enum tree_code code
;
8773 tree type
, std_attributes
= NULL_TREE
;
8774 cp_cv_quals cv_quals
;
8776 /* We don't know if there's a ptr-operator next, or not. */
8777 cp_parser_parse_tentatively (parser
);
8778 /* Look for a ptr-operator. */
8779 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8780 /* If that worked, look for more new-declarators. */
8781 if (cp_parser_parse_definitely (parser
))
8783 cp_declarator
*declarator
;
8785 /* Parse another optional declarator. */
8786 declarator
= cp_parser_new_declarator_opt (parser
);
8788 declarator
= cp_parser_make_indirect_declarator
8789 (code
, type
, cv_quals
, declarator
, std_attributes
);
8794 /* If the next token is a `[', there is a direct-new-declarator. */
8795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8796 return cp_parser_direct_new_declarator (parser
);
8801 /* Parse a direct-new-declarator.
8803 direct-new-declarator:
8805 direct-new-declarator [constant-expression]
8809 static cp_declarator
*
8810 cp_parser_direct_new_declarator (cp_parser
* parser
)
8812 cp_declarator
*declarator
= NULL
;
8819 /* Look for the opening `['. */
8820 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8822 token
= cp_lexer_peek_token (parser
->lexer
);
8823 expression
= cp_parser_expression (parser
);
8824 /* The standard requires that the expression have integral
8825 type. DR 74 adds enumeration types. We believe that the
8826 real intent is that these expressions be handled like the
8827 expression in a `switch' condition, which also allows
8828 classes with a single conversion to integral or
8829 enumeration type. */
8830 if (!processing_template_decl
)
8833 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8838 error_at (token
->location
,
8839 "expression in new-declarator must have integral "
8840 "or enumeration type");
8841 expression
= error_mark_node
;
8845 /* Look for the closing `]'. */
8846 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8848 /* Add this bound to the declarator. */
8849 declarator
= make_array_declarator (declarator
, expression
);
8851 /* If the next token is not a `[', then there are no more
8853 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8860 /* Parse a new-initializer.
8863 ( expression-list [opt] )
8866 Returns a representation of the expression-list. */
8868 static vec
<tree
, va_gc
> *
8869 cp_parser_new_initializer (cp_parser
* parser
)
8871 vec
<tree
, va_gc
> *expression_list
;
8873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8876 bool expr_non_constant_p
;
8877 cp_lexer_set_source_position (parser
->lexer
);
8878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8879 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8880 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8881 expression_list
= make_tree_vector_single (t
);
8884 expression_list
= (cp_parser_parenthesized_expression_list
8885 (parser
, non_attr
, /*cast_p=*/false,
8886 /*allow_expansion_p=*/true,
8887 /*non_constant_p=*/NULL
));
8889 return expression_list
;
8892 /* Parse a delete-expression.
8895 :: [opt] delete cast-expression
8896 :: [opt] delete [ ] cast-expression
8898 Returns a representation of the expression. */
8901 cp_parser_delete_expression (cp_parser
* parser
)
8903 bool global_scope_p
;
8907 /* Look for the optional `::' operator. */
8909 = (cp_parser_global_scope_opt (parser
,
8910 /*current_scope_valid_p=*/false)
8912 /* Look for the `delete' keyword. */
8913 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8914 /* See if the array syntax is in use. */
8915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8917 /* Consume the `[' token. */
8918 cp_lexer_consume_token (parser
->lexer
);
8919 /* Look for the `]' token. */
8920 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8921 /* Remember that this is the `[]' construct. */
8927 /* Parse the cast-expression. */
8928 expression
= cp_parser_simple_cast_expression (parser
);
8930 /* A delete-expression may not appear in an integral constant
8932 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8933 return error_mark_node
;
8935 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8936 tf_warning_or_error
);
8939 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8940 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8944 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8946 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8947 switch (token
->type
)
8953 case CPP_CLOSE_SQUARE
:
8954 case CPP_CLOSE_PAREN
:
8955 case CPP_CLOSE_BRACE
:
8956 case CPP_OPEN_BRACE
:
8960 case CPP_DEREF_STAR
:
8968 case CPP_GREATER_EQ
:
8989 case CPP_OPEN_PAREN
:
8990 /* In ((type ()) () the last () isn't a valid cast-expression,
8991 so the whole must be parsed as postfix-expression. */
8992 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8995 case CPP_OPEN_SQUARE
:
8996 /* '[' may start a primary-expression in obj-c++ and in C++11,
8997 as a lambda-expression, eg, '(void)[]{}'. */
8998 if (cxx_dialect
>= cxx11
)
9000 return c_dialect_objc ();
9003 case CPP_MINUS_MINUS
:
9004 /* '++' and '--' may or may not start a cast-expression:
9006 struct T { void operator++(int); };
9007 void f() { (T())++; }
9020 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9021 in the order: const_cast, static_cast, reinterpret_cast.
9023 Don't suggest dynamic_cast.
9025 Return the first legal cast kind found, or NULL otherwise. */
9028 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9032 /* Reuse the parser logic by attempting to build the various kinds of
9033 cast, with "complain" disabled.
9034 Identify the first such cast that is valid. */
9036 /* Don't attempt to run such logic within template processing. */
9037 if (processing_template_decl
)
9040 /* First try const_cast. */
9041 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9042 if (trial
!= error_mark_node
)
9043 return "const_cast";
9045 /* If that fails, try static_cast. */
9046 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9047 if (trial
!= error_mark_node
)
9048 return "static_cast";
9050 /* Finally, try reinterpret_cast. */
9051 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9052 if (trial
!= error_mark_node
)
9053 return "reinterpret_cast";
9055 /* No such cast possible. */
9059 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9060 suggesting how to convert a C-style cast of the form:
9064 to a C++-style cast.
9066 The primary range of RICHLOC is asssumed to be that of the original
9067 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9068 of the parens in the C-style cast. */
9071 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9072 location_t close_paren_loc
, tree orig_expr
,
9075 /* This function is non-trivial, so bail out now if the warning isn't
9076 going to be emitted. */
9077 if (!warn_old_style_cast
)
9080 /* Try to find a legal C++ cast, trying them in order:
9081 const_cast, static_cast, reinterpret_cast. */
9082 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9083 if (!cast_suggestion
)
9086 /* Replace the open paren with "CAST_SUGGESTION<". */
9088 pp_printf (&pp
, "%s<", cast_suggestion
);
9089 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9091 /* Replace the close paren with "> (". */
9092 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9094 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9095 rich_loc
->add_fixit_insert_after (")");
9099 /* Parse a cast-expression.
9103 ( type-id ) cast-expression
9105 ADDRESS_P is true iff the unary-expression is appearing as the
9106 operand of the `&' operator. CAST_P is true if this expression is
9107 the target of a cast.
9109 Returns a representation of the expression. */
9112 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9113 bool decltype_p
, cp_id_kind
* pidk
)
9115 /* If it's a `(', then we might be looking at a cast. */
9116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9118 tree type
= NULL_TREE
;
9119 cp_expr
expr (NULL_TREE
);
9120 int cast_expression
= 0;
9121 const char *saved_message
;
9123 /* There's no way to know yet whether or not this is a cast.
9124 For example, `(int (3))' is a unary-expression, while `(int)
9125 3' is a cast. So, we resort to parsing tentatively. */
9126 cp_parser_parse_tentatively (parser
);
9127 /* Types may not be defined in a cast. */
9128 saved_message
= parser
->type_definition_forbidden_message
;
9129 parser
->type_definition_forbidden_message
9130 = G_("types may not be defined in casts");
9131 /* Consume the `('. */
9132 matching_parens parens
;
9133 cp_token
*open_paren
= parens
.consume_open (parser
);
9134 location_t open_paren_loc
= open_paren
->location
;
9135 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9137 /* A very tricky bit is that `(struct S) { 3 }' is a
9138 compound-literal (which we permit in C++ as an extension).
9139 But, that construct is not a cast-expression -- it is a
9140 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9141 is legal; if the compound-literal were a cast-expression,
9142 you'd need an extra set of parentheses.) But, if we parse
9143 the type-id, and it happens to be a class-specifier, then we
9144 will commit to the parse at that point, because we cannot
9145 undo the action that is done when creating a new class. So,
9146 then we cannot back up and do a postfix-expression.
9148 Another tricky case is the following (c++/29234):
9150 struct S { void operator () (); };
9157 As a type-id we parse the parenthesized S()() as a function
9158 returning a function, groktypename complains and we cannot
9159 back up in this case either.
9161 Therefore, we scan ahead to the closing `)', and check to see
9162 if the tokens after the `)' can start a cast-expression. Otherwise
9163 we are dealing with an unary-expression, a postfix-expression
9166 Yet another tricky case, in C++11, is the following (c++/54891):
9170 The issue is that usually, besides the case of lambda-expressions,
9171 the parenthesized type-id cannot be followed by '[', and, eg, we
9172 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9173 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9174 we don't commit, we try a cast-expression, then an unary-expression.
9176 Save tokens so that we can put them back. */
9177 cp_lexer_save_tokens (parser
->lexer
);
9179 /* We may be looking at a cast-expression. */
9180 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9181 /*consume_paren=*/true))
9183 = cp_parser_tokens_start_cast_expression (parser
);
9185 /* Roll back the tokens we skipped. */
9186 cp_lexer_rollback_tokens (parser
->lexer
);
9187 /* If we aren't looking at a cast-expression, simulate an error so
9188 that the call to cp_parser_error_occurred below returns true. */
9189 if (!cast_expression
)
9190 cp_parser_simulate_error (parser
);
9193 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9194 parser
->in_type_id_in_expr_p
= true;
9195 /* Look for the type-id. */
9196 type
= cp_parser_type_id (parser
);
9197 /* Look for the closing `)'. */
9198 cp_token
*close_paren
= parens
.require_close (parser
);
9200 close_paren_loc
= close_paren
->location
;
9201 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9204 /* Restore the saved message. */
9205 parser
->type_definition_forbidden_message
= saved_message
;
9207 /* At this point this can only be either a cast or a
9208 parenthesized ctor such as `(T ())' that looks like a cast to
9209 function returning T. */
9210 if (!cp_parser_error_occurred (parser
))
9212 /* Only commit if the cast-expression doesn't start with
9213 '++', '--', or '[' in C++11. */
9214 if (cast_expression
> 0)
9215 cp_parser_commit_to_topmost_tentative_parse (parser
);
9217 expr
= cp_parser_cast_expression (parser
,
9218 /*address_p=*/false,
9220 /*decltype_p=*/false,
9223 if (cp_parser_parse_definitely (parser
))
9225 /* Warn about old-style casts, if so requested. */
9226 if (warn_old_style_cast
9227 && !in_system_header_at (input_location
)
9228 && !VOID_TYPE_P (type
)
9229 && current_lang_name
!= lang_name_c
)
9231 gcc_rich_location
rich_loc (input_location
);
9232 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9234 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9235 "use of old-style cast to %q#T", type
);
9238 /* Only type conversions to integral or enumeration types
9239 can be used in constant-expressions. */
9240 if (!cast_valid_in_integral_constant_expression_p (type
)
9241 && cp_parser_non_integral_constant_expression (parser
,
9243 return error_mark_node
;
9245 /* Perform the cast. */
9249 with start==caret at the open paren, extending to the
9251 location_t cast_loc
= make_location (open_paren_loc
,
9253 expr
.get_finish ());
9254 expr
= build_c_cast (cast_loc
, type
, expr
);
9259 cp_parser_abort_tentative_parse (parser
);
9262 /* If we get here, then it's not a cast, so it must be a
9263 unary-expression. */
9264 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9265 cast_p
, decltype_p
);
9268 /* Parse a binary expression of the general form:
9272 pm-expression .* cast-expression
9273 pm-expression ->* cast-expression
9275 multiplicative-expression:
9277 multiplicative-expression * pm-expression
9278 multiplicative-expression / pm-expression
9279 multiplicative-expression % pm-expression
9281 additive-expression:
9282 multiplicative-expression
9283 additive-expression + multiplicative-expression
9284 additive-expression - multiplicative-expression
9288 shift-expression << additive-expression
9289 shift-expression >> additive-expression
9291 relational-expression:
9293 relational-expression < shift-expression
9294 relational-expression > shift-expression
9295 relational-expression <= shift-expression
9296 relational-expression >= shift-expression
9300 relational-expression:
9301 relational-expression <? shift-expression
9302 relational-expression >? shift-expression
9304 equality-expression:
9305 relational-expression
9306 equality-expression == relational-expression
9307 equality-expression != relational-expression
9311 and-expression & equality-expression
9313 exclusive-or-expression:
9315 exclusive-or-expression ^ and-expression
9317 inclusive-or-expression:
9318 exclusive-or-expression
9319 inclusive-or-expression | exclusive-or-expression
9321 logical-and-expression:
9322 inclusive-or-expression
9323 logical-and-expression && inclusive-or-expression
9325 logical-or-expression:
9326 logical-and-expression
9327 logical-or-expression || logical-and-expression
9329 All these are implemented with a single function like:
9332 simple-cast-expression
9333 binary-expression <token> binary-expression
9335 CAST_P is true if this expression is the target of a cast.
9337 The binops_by_token map is used to get the tree codes for each <token> type.
9338 binary-expressions are associated according to a precedence table. */
9340 #define TOKEN_PRECEDENCE(token) \
9341 (((token->type == CPP_GREATER \
9342 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9343 && !parser->greater_than_is_operator_p) \
9344 ? PREC_NOT_OPERATOR \
9345 : binops_by_token[token->type].prec)
9348 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9349 bool no_toplevel_fold_p
,
9351 enum cp_parser_prec prec
,
9354 cp_parser_expression_stack stack
;
9355 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9356 cp_parser_expression_stack_entry current
;
9359 enum tree_code rhs_type
;
9360 enum cp_parser_prec new_prec
, lookahead_prec
;
9363 /* Parse the first expression. */
9364 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9365 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9366 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9367 cast_p
, decltype_p
, pidk
);
9368 current
.prec
= prec
;
9370 if (cp_parser_error_occurred (parser
))
9371 return error_mark_node
;
9375 /* Get an operator token. */
9376 token
= cp_lexer_peek_token (parser
->lexer
);
9378 if (warn_cxx11_compat
9379 && token
->type
== CPP_RSHIFT
9380 && !parser
->greater_than_is_operator_p
)
9382 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9383 "%<>>%> operator is treated"
9384 " as two right angle brackets in C++11"))
9385 inform (token
->location
,
9386 "suggest parentheses around %<>>%> expression");
9389 new_prec
= TOKEN_PRECEDENCE (token
);
9390 if (new_prec
!= PREC_NOT_OPERATOR
9391 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9392 /* This is a fold-expression; handle it later. */
9393 new_prec
= PREC_NOT_OPERATOR
;
9395 /* Popping an entry off the stack means we completed a subexpression:
9396 - either we found a token which is not an operator (`>' where it is not
9397 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9398 will happen repeatedly;
9399 - or, we found an operator which has lower priority. This is the case
9400 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9402 if (new_prec
<= current
.prec
)
9411 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9412 current
.loc
= token
->location
;
9414 /* We used the operator token. */
9415 cp_lexer_consume_token (parser
->lexer
);
9417 /* For "false && x" or "true || x", x will never be executed;
9418 disable warnings while evaluating it. */
9419 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9420 c_inhibit_evaluation_warnings
+=
9421 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9422 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9423 c_inhibit_evaluation_warnings
+=
9424 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9426 /* Extract another operand. It may be the RHS of this expression
9427 or the LHS of a new, higher priority expression. */
9428 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9429 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9430 rhs
= cp_parser_simple_cast_expression (parser
);
9432 /* Get another operator token. Look up its precedence to avoid
9433 building a useless (immediately popped) stack entry for common
9434 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9435 token
= cp_lexer_peek_token (parser
->lexer
);
9436 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9437 if (lookahead_prec
!= PREC_NOT_OPERATOR
9438 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9439 lookahead_prec
= PREC_NOT_OPERATOR
;
9440 if (lookahead_prec
> new_prec
)
9442 /* ... and prepare to parse the RHS of the new, higher priority
9443 expression. Since precedence levels on the stack are
9444 monotonically increasing, we do not have to care about
9449 current
.lhs_type
= rhs_type
;
9450 current
.prec
= new_prec
;
9451 new_prec
= lookahead_prec
;
9455 lookahead_prec
= new_prec
;
9456 /* If the stack is not empty, we have parsed into LHS the right side
9457 (`4' in the example above) of an expression we had suspended.
9458 We can use the information on the stack to recover the LHS (`3')
9459 from the stack together with the tree code (`MULT_EXPR'), and
9460 the precedence of the higher level subexpression
9461 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9462 which will be used to actually build the additive expression. */
9464 rhs_type
= current
.lhs_type
;
9469 /* Undo the disabling of warnings done above. */
9470 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9471 c_inhibit_evaluation_warnings
-=
9472 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9473 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9474 c_inhibit_evaluation_warnings
-=
9475 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9477 if (warn_logical_not_paren
9478 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9479 && current
.lhs_type
== TRUTH_NOT_EXPR
9480 /* Avoid warning for !!x == y. */
9481 && (TREE_CODE (current
.lhs
) != NE_EXPR
9482 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9483 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9484 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9485 /* Avoid warning for !b == y where b is boolean. */
9486 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9487 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9489 /* Avoid warning for !!b == y where b is boolean. */
9490 && (!DECL_P (current
.lhs
)
9491 || TREE_TYPE (current
.lhs
) == NULL_TREE
9492 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9493 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9494 current
.lhs
, maybe_constant_value (rhs
));
9498 location_t combined_loc
= make_location (current
.loc
,
9499 current
.lhs
.get_start (),
9502 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9503 ERROR_MARK for everything that is not a binary expression.
9504 This makes warn_about_parentheses miss some warnings that
9505 involve unary operators. For unary expressions we should
9506 pass the correct tree_code unless the unary expression was
9507 surrounded by parentheses.
9509 if (no_toplevel_fold_p
9510 && lookahead_prec
<= current
.prec
9513 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9514 current
.lhs
= error_mark_node
;
9518 = build_min (current
.tree_type
,
9519 TREE_CODE_CLASS (current
.tree_type
)
9521 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9522 current
.lhs
.get_value (), rhs
.get_value ());
9523 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9528 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9529 current
.lhs
, current
.lhs_type
,
9530 rhs
, rhs_type
, &overload
,
9531 complain_flags (decltype_p
));
9532 /* TODO: build_x_binary_op doesn't always honor the location. */
9533 current
.lhs
.set_location (combined_loc
);
9535 current
.lhs_type
= current
.tree_type
;
9537 /* If the binary operator required the use of an overloaded operator,
9538 then this expression cannot be an integral constant-expression.
9539 An overloaded operator can be used even if both operands are
9540 otherwise permissible in an integral constant-expression if at
9541 least one of the operands is of enumeration type. */
9544 && cp_parser_non_integral_constant_expression (parser
,
9546 return error_mark_node
;
9553 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9554 bool no_toplevel_fold_p
,
9555 enum cp_parser_prec prec
,
9558 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9559 /*decltype*/false, prec
, pidk
);
9562 /* Parse the `? expression : assignment-expression' part of a
9563 conditional-expression. The LOGICAL_OR_EXPR is the
9564 logical-or-expression that started the conditional-expression.
9565 Returns a representation of the entire conditional-expression.
9567 This routine is used by cp_parser_assignment_expression.
9569 ? expression : assignment-expression
9573 ? : assignment-expression */
9576 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9578 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9579 cp_expr assignment_expr
;
9580 struct cp_token
*token
;
9581 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9583 /* Consume the `?' token. */
9584 cp_lexer_consume_token (parser
->lexer
);
9585 token
= cp_lexer_peek_token (parser
->lexer
);
9586 if (cp_parser_allow_gnu_extensions_p (parser
)
9587 && token
->type
== CPP_COLON
)
9589 pedwarn (token
->location
, OPT_Wpedantic
,
9590 "ISO C++ does not allow ?: with omitted middle operand");
9591 /* Implicit true clause. */
9593 c_inhibit_evaluation_warnings
+=
9594 folded_logical_or_expr
== truthvalue_true_node
;
9595 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9599 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9600 parser
->colon_corrects_to_scope_p
= false;
9601 /* Parse the expression. */
9602 c_inhibit_evaluation_warnings
+=
9603 folded_logical_or_expr
== truthvalue_false_node
;
9604 expr
= cp_parser_expression (parser
);
9605 c_inhibit_evaluation_warnings
+=
9606 ((folded_logical_or_expr
== truthvalue_true_node
)
9607 - (folded_logical_or_expr
== truthvalue_false_node
));
9608 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9611 /* The next token should be a `:'. */
9612 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9613 /* Parse the assignment-expression. */
9614 assignment_expr
= cp_parser_assignment_expression (parser
);
9615 c_inhibit_evaluation_warnings
-=
9616 folded_logical_or_expr
== truthvalue_true_node
;
9619 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9620 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9621 with the caret at the "?", ranging from the start of
9622 the logical_or_expr to the end of the assignment_expr. */
9623 loc
= make_location (loc
,
9624 logical_or_expr
.get_start (),
9625 assignment_expr
.get_finish ());
9627 /* Build the conditional-expression. */
9628 return build_x_conditional_expr (loc
, logical_or_expr
,
9631 tf_warning_or_error
);
9634 /* Parse an assignment-expression.
9636 assignment-expression:
9637 conditional-expression
9638 logical-or-expression assignment-operator assignment_expression
9641 CAST_P is true if this expression is the target of a cast.
9642 DECLTYPE_P is true if this expression is the operand of decltype.
9644 Returns a representation for the expression. */
9647 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9648 bool cast_p
, bool decltype_p
)
9652 /* If the next token is the `throw' keyword, then we're looking at
9653 a throw-expression. */
9654 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9655 expr
= cp_parser_throw_expression (parser
);
9656 /* Otherwise, it must be that we are looking at a
9657 logical-or-expression. */
9660 /* Parse the binary expressions (logical-or-expression). */
9661 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9663 PREC_NOT_OPERATOR
, pidk
);
9664 /* If the next token is a `?' then we're actually looking at a
9665 conditional-expression. */
9666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9667 return cp_parser_question_colon_clause (parser
, expr
);
9670 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9672 /* If it's an assignment-operator, we're using the second
9674 enum tree_code assignment_operator
9675 = cp_parser_assignment_operator_opt (parser
);
9676 if (assignment_operator
!= ERROR_MARK
)
9678 bool non_constant_p
;
9680 /* Parse the right-hand side of the assignment. */
9681 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9684 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9685 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9687 /* An assignment may not appear in a
9688 constant-expression. */
9689 if (cp_parser_non_integral_constant_expression (parser
,
9691 return error_mark_node
;
9692 /* Build the assignment expression. Its default
9696 is the location of the '=' token as the
9697 caret, ranging from the start of the lhs to the
9699 loc
= make_location (loc
,
9702 expr
= build_x_modify_expr (loc
, expr
,
9703 assignment_operator
,
9705 complain_flags (decltype_p
));
9706 /* TODO: build_x_modify_expr doesn't honor the location,
9707 so we must set it here. */
9708 expr
.set_location (loc
);
9716 /* Parse an (optional) assignment-operator.
9718 assignment-operator: one of
9719 = *= /= %= += -= >>= <<= &= ^= |=
9723 assignment-operator: one of
9726 If the next token is an assignment operator, the corresponding tree
9727 code is returned, and the token is consumed. For example, for
9728 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9729 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9730 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9731 operator, ERROR_MARK is returned. */
9733 static enum tree_code
9734 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9739 /* Peek at the next token. */
9740 token
= cp_lexer_peek_token (parser
->lexer
);
9742 switch (token
->type
)
9753 op
= TRUNC_DIV_EXPR
;
9757 op
= TRUNC_MOD_EXPR
;
9789 /* Nothing else is an assignment operator. */
9793 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9794 if (op
!= ERROR_MARK
9795 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9798 /* If it was an assignment operator, consume it. */
9799 if (op
!= ERROR_MARK
)
9800 cp_lexer_consume_token (parser
->lexer
);
9805 /* Parse an expression.
9808 assignment-expression
9809 expression , assignment-expression
9811 CAST_P is true if this expression is the target of a cast.
9812 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9813 except possibly parenthesized or on the RHS of a comma (N3276).
9815 Returns a representation of the expression. */
9818 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9819 bool cast_p
, bool decltype_p
)
9821 cp_expr expression
= NULL_TREE
;
9822 location_t loc
= UNKNOWN_LOCATION
;
9826 cp_expr assignment_expression
;
9828 /* Parse the next assignment-expression. */
9829 assignment_expression
9830 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9832 /* We don't create a temporary for a call that is the immediate operand
9833 of decltype or on the RHS of a comma. But when we see a comma, we
9834 need to create a temporary for a call on the LHS. */
9835 if (decltype_p
&& !processing_template_decl
9836 && TREE_CODE (assignment_expression
) == CALL_EXPR
9837 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9838 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9839 assignment_expression
9840 = build_cplus_new (TREE_TYPE (assignment_expression
),
9841 assignment_expression
, tf_warning_or_error
);
9843 /* If this is the first assignment-expression, we can just
9846 expression
= assignment_expression
;
9849 /* Create a location with caret at the comma, ranging
9850 from the start of the LHS to the end of the RHS. */
9851 loc
= make_location (loc
,
9852 expression
.get_start (),
9853 assignment_expression
.get_finish ());
9854 expression
= build_x_compound_expr (loc
, expression
,
9855 assignment_expression
,
9856 complain_flags (decltype_p
));
9857 expression
.set_location (loc
);
9859 /* If the next token is not a comma, or we're in a fold-expression, then
9860 we are done with the expression. */
9861 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9862 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9864 /* Consume the `,'. */
9865 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9866 cp_lexer_consume_token (parser
->lexer
);
9867 /* A comma operator cannot appear in a constant-expression. */
9868 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9869 expression
= error_mark_node
;
9875 /* Parse a constant-expression.
9877 constant-expression:
9878 conditional-expression
9880 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9881 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9882 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9883 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9884 only parse a conditional-expression, otherwise parse an
9885 assignment-expression. See below for rationale. */
9888 cp_parser_constant_expression (cp_parser
* parser
,
9889 bool allow_non_constant_p
,
9890 bool *non_constant_p
,
9893 bool saved_integral_constant_expression_p
;
9894 bool saved_allow_non_integral_constant_expression_p
;
9895 bool saved_non_integral_constant_expression_p
;
9898 /* It might seem that we could simply parse the
9899 conditional-expression, and then check to see if it were
9900 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9901 one that the compiler can figure out is constant, possibly after
9902 doing some simplifications or optimizations. The standard has a
9903 precise definition of constant-expression, and we must honor
9904 that, even though it is somewhat more restrictive.
9910 is not a legal declaration, because `(2, 3)' is not a
9911 constant-expression. The `,' operator is forbidden in a
9912 constant-expression. However, GCC's constant-folding machinery
9913 will fold this operation to an INTEGER_CST for `3'. */
9915 /* Save the old settings. */
9916 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9917 saved_allow_non_integral_constant_expression_p
9918 = parser
->allow_non_integral_constant_expression_p
;
9919 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9920 /* We are now parsing a constant-expression. */
9921 parser
->integral_constant_expression_p
= true;
9922 parser
->allow_non_integral_constant_expression_p
9923 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9924 parser
->non_integral_constant_expression_p
= false;
9925 /* Although the grammar says "conditional-expression", when not STRICT_P,
9926 we parse an "assignment-expression", which also permits
9927 "throw-expression" and the use of assignment operators. In the case
9928 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9929 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9930 actually essential that we look for an assignment-expression.
9931 For example, cp_parser_initializer_clauses uses this function to
9932 determine whether a particular assignment-expression is in fact
9936 /* Parse the binary expressions (logical-or-expression). */
9937 expression
= cp_parser_binary_expression (parser
, false, false, false,
9938 PREC_NOT_OPERATOR
, NULL
);
9939 /* If the next token is a `?' then we're actually looking at
9940 a conditional-expression; otherwise we're done. */
9941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9942 expression
= cp_parser_question_colon_clause (parser
, expression
);
9945 expression
= cp_parser_assignment_expression (parser
);
9946 /* Restore the old settings. */
9947 parser
->integral_constant_expression_p
9948 = saved_integral_constant_expression_p
;
9949 parser
->allow_non_integral_constant_expression_p
9950 = saved_allow_non_integral_constant_expression_p
;
9951 if (cxx_dialect
>= cxx11
)
9953 /* Require an rvalue constant expression here; that's what our
9954 callers expect. Reference constant expressions are handled
9955 separately in e.g. cp_parser_template_argument. */
9956 tree decay
= expression
;
9957 if (TREE_TYPE (expression
)
9958 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9959 decay
= build_address (expression
);
9960 bool is_const
= potential_rvalue_constant_expression (decay
);
9961 parser
->non_integral_constant_expression_p
= !is_const
;
9962 if (!is_const
&& !allow_non_constant_p
)
9963 require_potential_rvalue_constant_expression (decay
);
9965 if (allow_non_constant_p
)
9966 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9967 parser
->non_integral_constant_expression_p
9968 = saved_non_integral_constant_expression_p
;
9973 /* Parse __builtin_offsetof.
9975 offsetof-expression:
9976 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9978 offsetof-member-designator:
9980 | offsetof-member-designator "." id-expression
9981 | offsetof-member-designator "[" expression "]"
9982 | offsetof-member-designator "->" id-expression */
9985 cp_parser_builtin_offsetof (cp_parser
*parser
)
9987 int save_ice_p
, save_non_ice_p
;
9992 location_t finish_loc
;
9994 /* We're about to accept non-integral-constant things, but will
9995 definitely yield an integral constant expression. Save and
9996 restore these values around our local parsing. */
9997 save_ice_p
= parser
->integral_constant_expression_p
;
9998 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10000 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10002 /* Consume the "__builtin_offsetof" token. */
10003 cp_lexer_consume_token (parser
->lexer
);
10004 /* Consume the opening `('. */
10005 matching_parens parens
;
10006 parens
.require_open (parser
);
10007 /* Parse the type-id. */
10008 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10010 const char *saved_message
= parser
->type_definition_forbidden_message
;
10011 parser
->type_definition_forbidden_message
10012 = G_("types may not be defined within __builtin_offsetof");
10013 type
= cp_parser_type_id (parser
);
10014 parser
->type_definition_forbidden_message
= saved_message
;
10016 /* Look for the `,'. */
10017 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10018 token
= cp_lexer_peek_token (parser
->lexer
);
10020 /* Build the (type *)null that begins the traditional offsetof macro. */
10022 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10023 tf_warning_or_error
);
10025 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10026 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10027 true, &dummy
, token
->location
);
10030 token
= cp_lexer_peek_token (parser
->lexer
);
10031 switch (token
->type
)
10033 case CPP_OPEN_SQUARE
:
10034 /* offsetof-member-designator "[" expression "]" */
10035 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10040 /* offsetof-member-designator "->" identifier */
10041 expr
= grok_array_decl (token
->location
, expr
,
10042 integer_zero_node
, false);
10046 /* offsetof-member-designator "." identifier */
10047 cp_lexer_consume_token (parser
->lexer
);
10048 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10049 expr
, true, &dummy
,
10053 case CPP_CLOSE_PAREN
:
10054 /* Consume the ")" token. */
10055 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10056 cp_lexer_consume_token (parser
->lexer
);
10060 /* Error. We know the following require will fail, but
10061 that gives the proper error message. */
10062 parens
.require_close (parser
);
10063 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10064 expr
= error_mark_node
;
10070 /* Make a location of the form:
10071 __builtin_offsetof (struct s, f)
10072 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10073 with caret at the type-id, ranging from the start of the
10074 "_builtin_offsetof" token to the close paren. */
10075 loc
= make_location (loc
, start_loc
, finish_loc
);
10076 /* The result will be an INTEGER_CST, so we need to explicitly
10077 preserve the location. */
10078 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10081 parser
->integral_constant_expression_p
= save_ice_p
;
10082 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10084 expr
= expr
.maybe_add_location_wrapper ();
10088 /* Parse a trait expression.
10090 Returns a representation of the expression, the underlying type
10091 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10094 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10096 cp_trait_kind kind
;
10097 tree type1
, type2
= NULL_TREE
;
10098 bool binary
= false;
10099 bool variadic
= false;
10103 case RID_HAS_NOTHROW_ASSIGN
:
10104 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10106 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10107 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10109 case RID_HAS_NOTHROW_COPY
:
10110 kind
= CPTK_HAS_NOTHROW_COPY
;
10112 case RID_HAS_TRIVIAL_ASSIGN
:
10113 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10115 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10116 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10118 case RID_HAS_TRIVIAL_COPY
:
10119 kind
= CPTK_HAS_TRIVIAL_COPY
;
10121 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10122 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10125 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10127 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10128 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10130 case RID_IS_ABSTRACT
:
10131 kind
= CPTK_IS_ABSTRACT
;
10133 case RID_IS_AGGREGATE
:
10134 kind
= CPTK_IS_AGGREGATE
;
10136 case RID_IS_BASE_OF
:
10137 kind
= CPTK_IS_BASE_OF
;
10141 kind
= CPTK_IS_CLASS
;
10144 kind
= CPTK_IS_EMPTY
;
10147 kind
= CPTK_IS_ENUM
;
10150 kind
= CPTK_IS_FINAL
;
10152 case RID_IS_LITERAL_TYPE
:
10153 kind
= CPTK_IS_LITERAL_TYPE
;
10156 kind
= CPTK_IS_POD
;
10158 case RID_IS_POLYMORPHIC
:
10159 kind
= CPTK_IS_POLYMORPHIC
;
10161 case RID_IS_SAME_AS
:
10162 kind
= CPTK_IS_SAME_AS
;
10165 case RID_IS_STD_LAYOUT
:
10166 kind
= CPTK_IS_STD_LAYOUT
;
10168 case RID_IS_TRIVIAL
:
10169 kind
= CPTK_IS_TRIVIAL
;
10171 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10172 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10175 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10176 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10179 case RID_IS_TRIVIALLY_COPYABLE
:
10180 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10183 kind
= CPTK_IS_UNION
;
10185 case RID_UNDERLYING_TYPE
:
10186 kind
= CPTK_UNDERLYING_TYPE
;
10191 case RID_DIRECT_BASES
:
10192 kind
= CPTK_DIRECT_BASES
;
10194 case RID_IS_ASSIGNABLE
:
10195 kind
= CPTK_IS_ASSIGNABLE
;
10198 case RID_IS_CONSTRUCTIBLE
:
10199 kind
= CPTK_IS_CONSTRUCTIBLE
;
10203 gcc_unreachable ();
10206 /* Get location of initial token. */
10207 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10209 /* Consume the token. */
10210 cp_lexer_consume_token (parser
->lexer
);
10212 matching_parens parens
;
10213 parens
.require_open (parser
);
10216 type_id_in_expr_sentinel
s (parser
);
10217 type1
= cp_parser_type_id (parser
);
10220 if (type1
== error_mark_node
)
10221 return error_mark_node
;
10225 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10228 type_id_in_expr_sentinel
s (parser
);
10229 type2
= cp_parser_type_id (parser
);
10232 if (type2
== error_mark_node
)
10233 return error_mark_node
;
10237 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10239 cp_lexer_consume_token (parser
->lexer
);
10240 tree elt
= cp_parser_type_id (parser
);
10241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10243 cp_lexer_consume_token (parser
->lexer
);
10244 elt
= make_pack_expansion (elt
);
10246 if (elt
== error_mark_node
)
10247 return error_mark_node
;
10248 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10252 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10253 parens
.require_close (parser
);
10255 /* Construct a location of the form:
10256 __is_trivially_copyable(_Tp)
10257 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10258 with start == caret, finishing at the close-paren. */
10259 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10261 /* Complete the trait expression, which may mean either processing
10262 the trait expr now or saving it for template instantiation. */
10265 case CPTK_UNDERLYING_TYPE
:
10266 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10268 return cp_expr (finish_bases (type1
, false), trait_loc
);
10269 case CPTK_DIRECT_BASES
:
10270 return cp_expr (finish_bases (type1
, true), trait_loc
);
10272 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10276 /* Parse a lambda expression.
10279 lambda-introducer lambda-declarator [opt] compound-statement
10281 Returns a representation of the expression. */
10284 cp_parser_lambda_expression (cp_parser
* parser
)
10286 tree lambda_expr
= build_lambda_expr ();
10289 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10290 cp_token_position start
= 0;
10292 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10294 if (cxx_dialect
>= cxx2a
)
10295 /* C++20 allows lambdas in unevaluated context. */;
10296 else if (cp_unevaluated_operand
)
10298 if (!token
->error_reported
)
10300 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10301 "lambda-expression in unevaluated context"
10302 " only available with -std=c++2a or -std=gnu++2a");
10303 token
->error_reported
= true;
10307 else if (parser
->in_template_argument_list_p
)
10309 if (!token
->error_reported
)
10311 error_at (token
->location
, "lambda-expression in template-argument"
10312 " only available with -std=c++2a or -std=gnu++2a");
10313 token
->error_reported
= true;
10318 /* We may be in the middle of deferred access check. Disable
10320 push_deferring_access_checks (dk_no_deferred
);
10322 cp_parser_lambda_introducer (parser
, lambda_expr
);
10323 if (cp_parser_error_occurred (parser
))
10324 return error_mark_node
;
10326 type
= begin_lambda_type (lambda_expr
);
10327 if (type
== error_mark_node
)
10328 return error_mark_node
;
10330 record_lambda_scope (lambda_expr
);
10332 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10333 determine_visibility (TYPE_NAME (type
));
10335 /* Now that we've started the type, add the capture fields for any
10336 explicit captures. */
10337 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10340 /* Inside the class, surrounding template-parameter-lists do not apply. */
10341 unsigned int saved_num_template_parameter_lists
10342 = parser
->num_template_parameter_lists
;
10343 unsigned char in_statement
= parser
->in_statement
;
10344 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10345 bool fully_implicit_function_template_p
10346 = parser
->fully_implicit_function_template_p
;
10347 tree implicit_template_parms
= parser
->implicit_template_parms
;
10348 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10349 bool auto_is_implicit_function_template_parm_p
10350 = parser
->auto_is_implicit_function_template_parm_p
;
10352 parser
->num_template_parameter_lists
= 0;
10353 parser
->in_statement
= 0;
10354 parser
->in_switch_statement_p
= false;
10355 parser
->fully_implicit_function_template_p
= false;
10356 parser
->implicit_template_parms
= 0;
10357 parser
->implicit_template_scope
= 0;
10358 parser
->auto_is_implicit_function_template_parm_p
= false;
10360 /* By virtue of defining a local class, a lambda expression has access to
10361 the private variables of enclosing classes. */
10363 if (cp_parser_start_tentative_firewall (parser
))
10366 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10368 if (ok
&& cp_parser_error_occurred (parser
))
10373 cp_parser_lambda_body (parser
, lambda_expr
);
10375 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10377 if (cp_parser_skip_to_closing_brace (parser
))
10378 cp_lexer_consume_token (parser
->lexer
);
10381 /* The capture list was built up in reverse order; fix that now. */
10382 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10383 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10386 maybe_add_lambda_conv_op (type
);
10388 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10390 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10391 parser
->in_statement
= in_statement
;
10392 parser
->in_switch_statement_p
= in_switch_statement_p
;
10393 parser
->fully_implicit_function_template_p
10394 = fully_implicit_function_template_p
;
10395 parser
->implicit_template_parms
= implicit_template_parms
;
10396 parser
->implicit_template_scope
= implicit_template_scope
;
10397 parser
->auto_is_implicit_function_template_parm_p
10398 = auto_is_implicit_function_template_parm_p
;
10401 /* This field is only used during parsing of the lambda. */
10402 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10404 /* This lambda shouldn't have any proxies left at this point. */
10405 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10406 /* And now that we're done, push proxies for an enclosing lambda. */
10407 insert_pending_capture_proxies ();
10409 /* Update the lambda expression to a range. */
10410 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10411 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10413 end_tok
->location
);
10416 lambda_expr
= build_lambda_object (lambda_expr
);
10418 lambda_expr
= error_mark_node
;
10420 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10422 pop_deferring_access_checks ();
10424 return lambda_expr
;
10427 /* Parse the beginning of a lambda expression.
10430 [ lambda-capture [opt] ]
10432 LAMBDA_EXPR is the current representation of the lambda expression. */
10435 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10437 /* Need commas after the first capture. */
10440 /* Eat the leading `['. */
10441 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10443 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10445 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10446 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10447 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10448 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10450 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10452 cp_lexer_consume_token (parser
->lexer
);
10455 if (!(at_function_scope_p () || parsing_nsdmi ()))
10456 error ("non-local lambda expression cannot have a capture-default");
10459 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10461 cp_token
* capture_token
;
10463 tree capture_init_expr
;
10464 cp_id_kind idk
= CP_ID_KIND_NONE
;
10465 bool explicit_init_p
= false;
10467 enum capture_kind_type
10472 enum capture_kind_type capture_kind
= BY_COPY
;
10474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10476 error ("expected end of capture-list");
10483 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10485 /* Possibly capture `this'. */
10486 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10488 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10489 if (cxx_dialect
< cxx2a
10490 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10491 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10492 "with by-copy capture default");
10493 cp_lexer_consume_token (parser
->lexer
);
10494 add_capture (lambda_expr
,
10495 /*id=*/this_identifier
,
10496 /*initializer=*/finish_this_expr (),
10497 /*by_reference_p=*/true,
10502 /* Possibly capture `*this'. */
10503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10504 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10506 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10507 if (cxx_dialect
< cxx17
)
10508 pedwarn (loc
, 0, "%<*this%> capture only available with "
10509 "-std=c++17 or -std=gnu++17");
10510 cp_lexer_consume_token (parser
->lexer
);
10511 cp_lexer_consume_token (parser
->lexer
);
10512 add_capture (lambda_expr
,
10513 /*id=*/this_identifier
,
10514 /*initializer=*/finish_this_expr (),
10515 /*by_reference_p=*/false,
10520 bool init_pack_expansion
= false;
10521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10523 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10524 if (cxx_dialect
< cxx2a
)
10525 pedwarn (loc
, 0, "pack init-capture only available with "
10526 "-std=c++2a or -std=gnu++2a");
10527 cp_lexer_consume_token (parser
->lexer
);
10528 init_pack_expansion
= true;
10531 /* Remember whether we want to capture as a reference or not. */
10532 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10534 capture_kind
= BY_REFERENCE
;
10535 cp_lexer_consume_token (parser
->lexer
);
10538 /* Get the identifier. */
10539 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10540 capture_id
= cp_parser_identifier (parser
);
10542 if (capture_id
== error_mark_node
)
10543 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10544 delimiters, but I modified this to stop on unnested ']' as well. It
10545 was already changed to stop on unnested '}', so the
10546 "closing_parenthesis" name is no more misleading with my change. */
10548 cp_parser_skip_to_closing_parenthesis (parser
,
10549 /*recovering=*/true,
10551 /*consume_paren=*/true);
10555 /* Find the initializer for this capture. */
10556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10557 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10558 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10560 bool direct
, non_constant
;
10561 /* An explicit initializer exists. */
10562 if (cxx_dialect
< cxx14
)
10563 pedwarn (input_location
, 0,
10564 "lambda capture initializers "
10565 "only available with -std=c++14 or -std=gnu++14");
10566 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10567 &non_constant
, true);
10568 explicit_init_p
= true;
10569 if (capture_init_expr
== NULL_TREE
)
10571 error ("empty initializer for lambda init-capture");
10572 capture_init_expr
= error_mark_node
;
10574 if (init_pack_expansion
)
10575 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10579 const char* error_msg
;
10581 /* Turn the identifier into an id-expression. */
10583 = cp_parser_lookup_name_simple (parser
, capture_id
,
10584 capture_token
->location
);
10586 if (capture_init_expr
== error_mark_node
)
10588 unqualified_name_lookup_error (capture_id
);
10591 else if (!VAR_P (capture_init_expr
)
10592 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10594 error_at (capture_token
->location
,
10595 "capture of non-variable %qE",
10596 capture_init_expr
);
10597 if (DECL_P (capture_init_expr
))
10598 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10599 "%q#D declared here", capture_init_expr
);
10602 if (VAR_P (capture_init_expr
)
10603 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10605 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10606 "%qD with non-automatic storage duration",
10607 capture_init_expr
))
10608 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10609 "%q#D declared here", capture_init_expr
);
10614 = finish_id_expression
10619 /*integral_constant_expression_p=*/false,
10620 /*allow_non_integral_constant_expression_p=*/false,
10621 /*non_integral_constant_expression_p=*/NULL
,
10622 /*template_p=*/false,
10624 /*address_p=*/false,
10625 /*template_arg_p=*/false,
10627 capture_token
->location
);
10629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10631 cp_lexer_consume_token (parser
->lexer
);
10632 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10636 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10637 && !explicit_init_p
)
10639 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10640 && capture_kind
== BY_COPY
)
10641 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10642 "of %qD redundant with by-copy capture default",
10644 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10645 && capture_kind
== BY_REFERENCE
)
10646 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10647 "capture of %qD redundant with by-reference capture "
10648 "default", capture_id
);
10651 add_capture (lambda_expr
,
10654 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10657 /* If there is any qualification still in effect, clear it
10658 now; we will be starting fresh with the next capture. */
10659 parser
->scope
= NULL_TREE
;
10660 parser
->qualifying_scope
= NULL_TREE
;
10661 parser
->object_scope
= NULL_TREE
;
10664 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10667 /* Parse the (optional) middle of a lambda expression.
10670 < template-parameter-list [opt] >
10671 ( parameter-declaration-clause [opt] )
10672 attribute-specifier [opt]
10673 decl-specifier-seq [opt]
10674 exception-specification [opt]
10675 lambda-return-type-clause [opt]
10677 LAMBDA_EXPR is the current representation of the lambda expression. */
10680 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10682 /* 5.1.1.4 of the standard says:
10683 If a lambda-expression does not include a lambda-declarator, it is as if
10684 the lambda-declarator were ().
10685 This means an empty parameter list, no attributes, and no exception
10687 tree param_list
= void_list_node
;
10688 tree attributes
= NULL_TREE
;
10689 tree exception_spec
= NULL_TREE
;
10690 tree template_param_list
= NULL_TREE
;
10691 tree tx_qual
= NULL_TREE
;
10692 tree return_type
= NULL_TREE
;
10693 cp_decl_specifier_seq lambda_specs
;
10694 clear_decl_specs (&lambda_specs
);
10696 /* The template-parameter-list is optional, but must begin with
10697 an opening angle if present. */
10698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10700 if (cxx_dialect
< cxx14
)
10701 pedwarn (parser
->lexer
->next_token
->location
, 0,
10702 "lambda templates are only available with "
10703 "-std=c++14 or -std=gnu++14");
10704 else if (cxx_dialect
< cxx2a
)
10705 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10706 "lambda templates are only available with "
10707 "-std=c++2a or -std=gnu++2a");
10709 cp_lexer_consume_token (parser
->lexer
);
10711 template_param_list
= cp_parser_template_parameter_list (parser
);
10713 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10715 /* We just processed one more parameter list. */
10716 ++parser
->num_template_parameter_lists
;
10719 /* The parameter-declaration-clause is optional (unless
10720 template-parameter-list was given), but must begin with an
10721 opening parenthesis if present. */
10722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10724 matching_parens parens
;
10725 parens
.consume_open (parser
);
10727 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10729 /* Parse parameters. */
10730 param_list
= cp_parser_parameter_declaration_clause (parser
);
10732 /* Default arguments shall not be specified in the
10733 parameter-declaration-clause of a lambda-declarator. */
10734 if (cxx_dialect
< cxx14
)
10735 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10736 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10737 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10738 "default argument specified for lambda parameter");
10740 parens
.require_close (parser
);
10742 /* In the decl-specifier-seq of the lambda-declarator, each
10743 decl-specifier shall either be mutable or constexpr. */
10744 int declares_class_or_enum
;
10745 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10746 cp_parser_decl_specifier_seq (parser
,
10747 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10748 &lambda_specs
, &declares_class_or_enum
);
10749 if (lambda_specs
.storage_class
== sc_mutable
)
10751 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10752 if (lambda_specs
.conflicting_specifiers_p
)
10753 error_at (lambda_specs
.locations
[ds_storage_class
],
10754 "duplicate %<mutable%>");
10757 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10759 /* Parse optional exception specification. */
10760 exception_spec
= cp_parser_exception_specification_opt (parser
);
10762 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10764 /* Parse optional trailing return type. */
10765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10767 cp_lexer_consume_token (parser
->lexer
);
10768 return_type
= cp_parser_trailing_type_id (parser
);
10771 /* The function parameters must be in scope all the way until after the
10772 trailing-return-type in case of decltype. */
10773 pop_bindings_and_leave_scope ();
10775 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10776 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10778 /* Create the function call operator.
10780 Messing with declarators like this is no uglier than building up the
10781 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10784 cp_decl_specifier_seq return_type_specs
;
10785 cp_declarator
* declarator
;
10790 clear_decl_specs (&return_type_specs
);
10791 return_type_specs
.type
= make_auto ();
10793 if (lambda_specs
.locations
[ds_constexpr
])
10795 if (cxx_dialect
>= cxx17
)
10796 return_type_specs
.locations
[ds_constexpr
]
10797 = lambda_specs
.locations
[ds_constexpr
];
10799 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10800 "lambda only available with -std=c++17 or -std=gnu++17");
10803 p
= obstack_alloc (&declarator_obstack
, 0);
10805 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10806 LAMBDA_EXPR_LOCATION (lambda_expr
));
10808 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10809 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10810 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10811 VIRT_SPEC_UNSPECIFIED
,
10816 /*requires_clause*/NULL_TREE
);
10817 declarator
->std_attributes
= attributes
;
10819 fco
= grokmethod (&return_type_specs
,
10822 if (fco
!= error_mark_node
)
10824 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10825 DECL_ARTIFICIAL (fco
) = 1;
10826 /* Give the object parameter a different name. */
10827 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10828 DECL_LAMBDA_FUNCTION (fco
) = 1;
10830 if (template_param_list
)
10832 fco
= finish_member_template_decl (fco
);
10833 finish_template_decl (template_param_list
);
10834 --parser
->num_template_parameter_lists
;
10836 else if (parser
->fully_implicit_function_template_p
)
10837 fco
= finish_fully_implicit_template (parser
, fco
);
10839 finish_member_declaration (fco
);
10841 obstack_free (&declarator_obstack
, p
);
10843 return (fco
!= error_mark_node
);
10847 /* Parse the body of a lambda expression, which is simply
10851 but which requires special handling.
10852 LAMBDA_EXPR is the current representation of the lambda expression. */
10855 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10857 bool nested
= (current_function_decl
!= NULL_TREE
);
10858 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10859 bool in_function_body
= parser
->in_function_body
;
10861 /* The body of a lambda-expression is not a subexpression of the enclosing
10866 push_function_context ();
10868 /* Still increment function_depth so that we don't GC in the
10869 middle of an expression. */
10872 vec
<tree
> omp_privatization_save
;
10873 save_omp_privatization_clauses (omp_privatization_save
);
10874 /* Clear this in case we're in the middle of a default argument. */
10875 parser
->local_variables_forbidden_p
= false;
10876 parser
->in_function_body
= true;
10879 local_specialization_stack
s (lss_copy
);
10880 tree fco
= lambda_function (lambda_expr
);
10881 tree body
= start_lambda_function (fco
, lambda_expr
);
10882 matching_braces braces
;
10884 if (braces
.require_open (parser
))
10886 tree compound_stmt
= begin_compound_stmt (0);
10888 /* Originally C++11 required us to peek for 'return expr'; and
10889 process it specially here to deduce the return type. N3638
10890 removed the need for that. */
10892 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10893 cp_parser_label_declaration (parser
);
10894 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10895 braces
.require_close (parser
);
10897 finish_compound_stmt (compound_stmt
);
10900 finish_lambda_function (body
);
10903 restore_omp_privatization_clauses (omp_privatization_save
);
10904 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10905 parser
->in_function_body
= in_function_body
;
10907 pop_function_context();
10912 /* Statements [gram.stmt.stmt] */
10914 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10917 add_debug_begin_stmt (location_t loc
)
10919 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10921 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10922 /* A concept is never expanded normally. */
10925 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10926 SET_EXPR_LOCATION (stmt
, loc
);
10930 /* Parse a statement.
10934 expression-statement
10936 selection-statement
10937 iteration-statement
10939 declaration-statement
10946 attribute-specifier-seq (opt) expression-statement
10947 attribute-specifier-seq (opt) compound-statement
10948 attribute-specifier-seq (opt) selection-statement
10949 attribute-specifier-seq (opt) iteration-statement
10950 attribute-specifier-seq (opt) jump-statement
10951 declaration-statement
10952 attribute-specifier-seq (opt) try-block
10955 expression-statement
10963 IN_COMPOUND is true when the statement is nested inside a
10964 cp_parser_compound_statement; this matters for certain pragmas.
10966 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10967 is a (possibly labeled) if statement which is not enclosed in braces
10968 and has an else clause. This is used to implement -Wparentheses.
10970 CHAIN is a vector of if-else-if conditions. */
10973 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10974 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10975 location_t
*loc_after_labels
)
10977 tree statement
, std_attrs
= NULL_TREE
;
10979 location_t statement_location
, attrs_location
;
10984 /* There is no statement yet. */
10985 statement
= NULL_TREE
;
10987 saved_token_sentinel
saved_tokens (parser
->lexer
);
10988 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10989 if (c_dialect_objc ())
10990 /* In obj-c++, seeing '[[' might be the either the beginning of
10991 c++11 attributes, or a nested objc-message-expression. So
10992 let's parse the c++11 attributes tentatively. */
10993 cp_parser_parse_tentatively (parser
);
10994 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10995 if (c_dialect_objc ())
10997 if (!cp_parser_parse_definitely (parser
))
10998 std_attrs
= NULL_TREE
;
11001 /* Peek at the next token. */
11002 token
= cp_lexer_peek_token (parser
->lexer
);
11003 /* Remember the location of the first token in the statement. */
11004 cp_token
*statement_token
= token
;
11005 statement_location
= token
->location
;
11006 add_debug_begin_stmt (statement_location
);
11007 /* If this is a keyword, then that will often determine what kind of
11008 statement we have. */
11009 if (token
->type
== CPP_KEYWORD
)
11011 enum rid keyword
= token
->keyword
;
11017 /* Looks like a labeled-statement with a case label.
11018 Parse the label, and then use tail recursion to parse
11020 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11021 in_compound
= false;
11026 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11027 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11033 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11034 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11041 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11042 statement
= cp_parser_jump_statement (parser
);
11045 /* Objective-C++ exception-handling constructs. */
11048 case RID_AT_FINALLY
:
11049 case RID_AT_SYNCHRONIZED
:
11051 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11052 statement
= cp_parser_objc_statement (parser
);
11056 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11057 statement
= cp_parser_try_block (parser
);
11060 case RID_NAMESPACE
:
11061 /* This must be a namespace alias definition. */
11062 if (std_attrs
!= NULL_TREE
)
11064 /* Attributes should be parsed as part of the the
11065 declaration, so let's un-parse them. */
11066 saved_tokens
.rollback();
11067 std_attrs
= NULL_TREE
;
11069 cp_parser_declaration_statement (parser
);
11072 case RID_TRANSACTION_ATOMIC
:
11073 case RID_TRANSACTION_RELAXED
:
11074 case RID_SYNCHRONIZED
:
11075 case RID_ATOMIC_NOEXCEPT
:
11076 case RID_ATOMIC_CANCEL
:
11077 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11078 statement
= cp_parser_transaction (parser
, token
);
11080 case RID_TRANSACTION_CANCEL
:
11081 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11082 statement
= cp_parser_transaction_cancel (parser
);
11086 /* It might be a keyword like `int' that can start a
11087 declaration-statement. */
11091 else if (token
->type
== CPP_NAME
)
11093 /* If the next token is a `:', then we are looking at a
11094 labeled-statement. */
11095 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11096 if (token
->type
== CPP_COLON
)
11098 /* Looks like a labeled-statement with an ordinary label.
11099 Parse the label, and then use tail recursion to parse
11102 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11103 in_compound
= false;
11107 /* Anything that starts with a `{' must be a compound-statement. */
11108 else if (token
->type
== CPP_OPEN_BRACE
)
11109 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11110 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11111 a statement all its own. */
11112 else if (token
->type
== CPP_PRAGMA
)
11114 /* Only certain OpenMP pragmas are attached to statements, and thus
11115 are considered statements themselves. All others are not. In
11116 the context of a compound, accept the pragma as a "statement" and
11117 return so that we can check for a close brace. Otherwise we
11118 require a real statement and must go back and read one. */
11120 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11121 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11125 else if (token
->type
== CPP_EOF
)
11127 cp_parser_error (parser
, "expected statement");
11131 /* Everything else must be a declaration-statement or an
11132 expression-statement. Try for the declaration-statement
11133 first, unless we are looking at a `;', in which case we know that
11134 we have an expression-statement. */
11137 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11139 if (std_attrs
!= NULL_TREE
)
11140 /* Attributes should be parsed as part of the declaration,
11141 so let's un-parse them. */
11142 saved_tokens
.rollback();
11144 cp_parser_parse_tentatively (parser
);
11145 /* Try to parse the declaration-statement. */
11146 cp_parser_declaration_statement (parser
);
11147 /* If that worked, we're done. */
11148 if (cp_parser_parse_definitely (parser
))
11150 /* It didn't work, restore the post-attribute position. */
11152 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11154 /* All preceding labels have been parsed at this point. */
11155 if (loc_after_labels
!= NULL
)
11156 *loc_after_labels
= statement_location
;
11158 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11160 /* Look for an expression-statement instead. */
11161 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11163 /* Handle [[fallthrough]];. */
11164 if (attribute_fallthrough_p (std_attrs
))
11166 /* The next token after the fallthrough attribute is ';'. */
11167 if (statement
== NULL_TREE
)
11169 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11170 statement
= build_call_expr_internal_loc (statement_location
,
11172 void_type_node
, 0);
11173 finish_expr_stmt (statement
);
11176 warning_at (statement_location
, OPT_Wattributes
,
11177 "%<fallthrough%> attribute not followed by %<;%>");
11178 std_attrs
= NULL_TREE
;
11182 /* Set the line number for the statement. */
11183 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11184 SET_EXPR_LOCATION (statement
, statement_location
);
11186 /* Allow "[[fallthrough]];", but warn otherwise. */
11187 if (std_attrs
!= NULL_TREE
)
11188 warning_at (attrs_location
,
11190 "attributes at the beginning of statement are ignored");
11193 /* Append ATTR to attribute list ATTRS. */
11196 attr_chainon (tree attrs
, tree attr
)
11198 if (attrs
== error_mark_node
)
11199 return error_mark_node
;
11200 if (attr
== error_mark_node
)
11201 return error_mark_node
;
11202 return chainon (attrs
, attr
);
11205 /* Parse the label for a labeled-statement, i.e.
11208 case constant-expression :
11212 case constant-expression ... constant-expression : statement
11214 When a label is parsed without errors, the label is added to the
11215 parse tree by the finish_* functions, so this function doesn't
11216 have to return the label. */
11219 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11222 tree label
= NULL_TREE
;
11223 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11225 /* The next token should be an identifier. */
11226 token
= cp_lexer_peek_token (parser
->lexer
);
11227 if (token
->type
!= CPP_NAME
11228 && token
->type
!= CPP_KEYWORD
)
11230 cp_parser_error (parser
, "expected labeled-statement");
11234 /* Remember whether this case or a user-defined label is allowed to fall
11236 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11238 parser
->colon_corrects_to_scope_p
= false;
11239 switch (token
->keyword
)
11243 tree expr
, expr_hi
;
11244 cp_token
*ellipsis
;
11246 /* Consume the `case' token. */
11247 cp_lexer_consume_token (parser
->lexer
);
11248 /* Parse the constant-expression. */
11249 expr
= cp_parser_constant_expression (parser
);
11250 if (check_for_bare_parameter_packs (expr
))
11251 expr
= error_mark_node
;
11253 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11254 if (ellipsis
->type
== CPP_ELLIPSIS
)
11256 /* Consume the `...' token. */
11257 cp_lexer_consume_token (parser
->lexer
);
11258 expr_hi
= cp_parser_constant_expression (parser
);
11259 if (check_for_bare_parameter_packs (expr_hi
))
11260 expr_hi
= error_mark_node
;
11262 /* We don't need to emit warnings here, as the common code
11263 will do this for us. */
11266 expr_hi
= NULL_TREE
;
11268 if (parser
->in_switch_statement_p
)
11270 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11271 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11273 label
= CASE_LABEL (l
);
11274 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11278 error_at (token
->location
,
11279 "case label %qE not within a switch statement",
11285 /* Consume the `default' token. */
11286 cp_lexer_consume_token (parser
->lexer
);
11288 if (parser
->in_switch_statement_p
)
11290 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11291 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11293 label
= CASE_LABEL (l
);
11294 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11298 error_at (token
->location
, "case label not within a switch statement");
11302 /* Anything else must be an ordinary label. */
11303 label
= finish_label_stmt (cp_parser_identifier (parser
));
11304 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11305 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11309 /* Require the `:' token. */
11310 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11312 /* An ordinary label may optionally be followed by attributes.
11313 However, this is only permitted if the attributes are then
11314 followed by a semicolon. This is because, for backward
11315 compatibility, when parsing
11316 lab: __attribute__ ((unused)) int i;
11317 we want the attribute to attach to "i", not "lab". */
11318 if (label
!= NULL_TREE
11319 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11322 cp_parser_parse_tentatively (parser
);
11323 attrs
= cp_parser_gnu_attributes_opt (parser
);
11324 if (attrs
== NULL_TREE
11325 /* And fallthrough always binds to the expression-statement. */
11326 || attribute_fallthrough_p (attrs
)
11327 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11328 cp_parser_abort_tentative_parse (parser
);
11329 else if (!cp_parser_parse_definitely (parser
))
11332 attributes
= attr_chainon (attributes
, attrs
);
11335 if (attributes
!= NULL_TREE
)
11336 cplus_decl_attributes (&label
, attributes
, 0);
11338 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11341 /* Parse an expression-statement.
11343 expression-statement:
11346 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11347 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11348 indicates whether this expression-statement is part of an
11349 expression statement. */
11352 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11354 tree statement
= NULL_TREE
;
11355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11356 location_t loc
= token
->location
;
11358 /* There might be attribute fallthrough. */
11359 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11361 /* If the next token is a ';', then there is no expression
11363 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11365 statement
= cp_parser_expression (parser
);
11366 if (statement
== error_mark_node
11367 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11369 cp_parser_skip_to_end_of_block_or_statement (parser
);
11370 return error_mark_node
;
11374 /* Handle [[fallthrough]];. */
11375 if (attribute_fallthrough_p (attr
))
11377 /* The next token after the fallthrough attribute is ';'. */
11378 if (statement
== NULL_TREE
)
11379 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11380 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11381 void_type_node
, 0);
11383 warning_at (loc
, OPT_Wattributes
,
11384 "%<fallthrough%> attribute not followed by %<;%>");
11388 /* Allow "[[fallthrough]];", but warn otherwise. */
11389 if (attr
!= NULL_TREE
)
11390 warning_at (loc
, OPT_Wattributes
,
11391 "attributes at the beginning of statement are ignored");
11393 /* Give a helpful message for "A<T>::type t;" and the like. */
11394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11395 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11397 if (TREE_CODE (statement
) == SCOPE_REF
)
11398 error_at (token
->location
, "need %<typename%> before %qE because "
11399 "%qT is a dependent scope",
11400 statement
, TREE_OPERAND (statement
, 0));
11401 else if (is_overloaded_fn (statement
)
11402 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11405 tree fn
= get_first_fn (statement
);
11406 error_at (token
->location
,
11407 "%<%T::%D%> names the constructor, not the type",
11408 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11412 /* Consume the final `;'. */
11413 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11415 if (in_statement_expr
11416 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11417 /* This is the final expression statement of a statement
11419 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11420 else if (statement
)
11421 statement
= finish_expr_stmt (statement
);
11426 /* Parse a compound-statement.
11428 compound-statement:
11429 { statement-seq [opt] }
11433 compound-statement:
11434 { label-declaration-seq [opt] statement-seq [opt] }
11436 label-declaration-seq:
11438 label-declaration-seq label-declaration
11440 Returns a tree representing the statement. */
11443 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11444 int bcs_flags
, bool function_body
)
11446 tree compound_stmt
;
11447 matching_braces braces
;
11449 /* Consume the `{'. */
11450 if (!braces
.require_open (parser
))
11451 return error_mark_node
;
11452 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11453 && !function_body
&& cxx_dialect
< cxx14
)
11454 pedwarn (input_location
, OPT_Wpedantic
,
11455 "compound-statement in %<constexpr%> function");
11456 /* Begin the compound-statement. */
11457 compound_stmt
= begin_compound_stmt (bcs_flags
);
11458 /* If the next keyword is `__label__' we have a label declaration. */
11459 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11460 cp_parser_label_declaration (parser
);
11461 /* Parse an (optional) statement-seq. */
11462 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11463 /* Finish the compound-statement. */
11464 finish_compound_stmt (compound_stmt
);
11465 /* Consume the `}'. */
11466 braces
.require_close (parser
);
11468 return compound_stmt
;
11471 /* Parse an (optional) statement-seq.
11475 statement-seq [opt] statement */
11478 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11480 /* Scan statements until there aren't any more. */
11483 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11485 /* If we are looking at a `}', then we have run out of
11486 statements; the same is true if we have reached the end
11487 of file, or have stumbled upon a stray '@end'. */
11488 if (token
->type
== CPP_CLOSE_BRACE
11489 || token
->type
== CPP_EOF
11490 || token
->type
== CPP_PRAGMA_EOL
11491 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11494 /* If we are in a compound statement and find 'else' then
11495 something went wrong. */
11496 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11498 if (parser
->in_statement
& IN_IF_STMT
)
11502 token
= cp_lexer_consume_token (parser
->lexer
);
11503 error_at (token
->location
, "%<else%> without a previous %<if%>");
11507 /* Parse the statement. */
11508 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11512 /* Return true if this is the C++20 version of range-based-for with
11516 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11520 /* Save tokens so that we can put them back. */
11521 cp_lexer_save_tokens (parser
->lexer
);
11523 /* There has to be an unnested ; followed by an unnested :. */
11524 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11525 /*recovering=*/false,
11527 /*consume_paren=*/false) != -1)
11530 /* We found the semicolon, eat it now. */
11531 cp_lexer_consume_token (parser
->lexer
);
11533 /* Now look for ':' that is not nested in () or {}. */
11534 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11535 /*recovering=*/false,
11537 /*consume_paren=*/false) == -1);
11540 /* Roll back the tokens we skipped. */
11541 cp_lexer_rollback_tokens (parser
->lexer
);
11546 /* Return true if we're looking at (init; cond), false otherwise. */
11549 cp_parser_init_statement_p (cp_parser
*parser
)
11551 /* Save tokens so that we can put them back. */
11552 cp_lexer_save_tokens (parser
->lexer
);
11554 /* Look for ';' that is not nested in () or {}. */
11555 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11556 /*recovering=*/false,
11558 /*consume_paren=*/false);
11560 /* Roll back the tokens we skipped. */
11561 cp_lexer_rollback_tokens (parser
->lexer
);
11566 /* Parse a selection-statement.
11568 selection-statement:
11569 if ( init-statement [opt] condition ) statement
11570 if ( init-statement [opt] condition ) statement else statement
11571 switch ( init-statement [opt] condition ) statement
11573 Returns the new IF_STMT or SWITCH_STMT.
11575 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11576 is a (possibly labeled) if statement which is not enclosed in
11577 braces and has an else clause. This is used to implement
11580 CHAIN is a vector of if-else-if conditions. This is used to implement
11581 -Wduplicated-cond. */
11584 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11589 token_indent_info guard_tinfo
;
11594 /* Peek at the next token. */
11595 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11596 guard_tinfo
= get_token_indent_info (token
);
11598 /* See what kind of keyword it is. */
11599 keyword
= token
->keyword
;
11609 if (keyword
== RID_IF
11610 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11614 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11615 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11616 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11617 "with -std=c++17 or -std=gnu++17");
11620 /* Look for the `('. */
11621 matching_parens parens
;
11622 if (!parens
.require_open (parser
))
11624 cp_parser_skip_to_end_of_statement (parser
);
11625 return error_mark_node
;
11628 /* Begin the selection-statement. */
11629 if (keyword
== RID_IF
)
11631 statement
= begin_if_stmt ();
11632 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11635 statement
= begin_switch_stmt ();
11637 /* Parse the optional init-statement. */
11638 if (cp_parser_init_statement_p (parser
))
11641 if (cxx_dialect
< cxx17
)
11642 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11643 "init-statement in selection statements only available "
11644 "with -std=c++17 or -std=gnu++17");
11645 cp_parser_init_statement (parser
, &decl
);
11648 /* Parse the condition. */
11649 condition
= cp_parser_condition (parser
);
11650 /* Look for the `)'. */
11651 if (!parens
.require_close (parser
))
11652 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11653 /*consume_paren=*/true);
11655 if (keyword
== RID_IF
)
11658 unsigned char in_statement
;
11660 /* Add the condition. */
11661 condition
= finish_if_stmt_cond (condition
, statement
);
11663 if (warn_duplicated_cond
)
11664 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11667 /* Parse the then-clause. */
11668 in_statement
= parser
->in_statement
;
11669 parser
->in_statement
|= IN_IF_STMT
;
11671 /* Outside a template, the non-selected branch of a constexpr
11672 if is a 'discarded statement', i.e. unevaluated. */
11673 bool was_discarded
= in_discarded_stmt
;
11674 bool discard_then
= (cx
&& !processing_template_decl
11675 && integer_zerop (condition
));
11678 in_discarded_stmt
= true;
11679 ++c_inhibit_evaluation_warnings
;
11682 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11685 parser
->in_statement
= in_statement
;
11687 finish_then_clause (statement
);
11691 THEN_CLAUSE (statement
) = NULL_TREE
;
11692 in_discarded_stmt
= was_discarded
;
11693 --c_inhibit_evaluation_warnings
;
11696 /* If the next token is `else', parse the else-clause. */
11697 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11700 bool discard_else
= (cx
&& !processing_template_decl
11701 && integer_nonzerop (condition
));
11704 in_discarded_stmt
= true;
11705 ++c_inhibit_evaluation_warnings
;
11709 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11710 /* Consume the `else' keyword. */
11711 cp_lexer_consume_token (parser
->lexer
);
11712 if (warn_duplicated_cond
)
11714 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11718 /* We've got "if (COND) else if (COND2)". Start
11719 the condition chain and add COND as the first
11721 chain
= new vec
<tree
> ();
11722 if (!CONSTANT_CLASS_P (condition
)
11723 && !TREE_SIDE_EFFECTS (condition
))
11725 /* Wrap it in a NOP_EXPR so that we can set the
11726 location of the condition. */
11727 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11729 SET_EXPR_LOCATION (e
, token
->location
);
11730 chain
->safe_push (e
);
11733 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11736 /* This is if-else without subsequent if. Zap the
11737 condition chain; we would have already warned at
11743 begin_else_clause (statement
);
11744 /* Parse the else-clause. */
11745 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11746 guard_tinfo
, chain
);
11748 finish_else_clause (statement
);
11750 /* If we are currently parsing a then-clause, then
11751 IF_P will not be NULL. We set it to true to
11752 indicate that this if statement has an else clause.
11753 This may trigger the Wparentheses warning below
11754 when we get back up to the parent if statement. */
11760 ELSE_CLAUSE (statement
) = NULL_TREE
;
11761 in_discarded_stmt
= was_discarded
;
11762 --c_inhibit_evaluation_warnings
;
11767 /* This if statement does not have an else clause. If
11768 NESTED_IF is true, then the then-clause has an if
11769 statement which does have an else clause. We warn
11770 about the potential ambiguity. */
11772 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11773 "suggest explicit braces to avoid ambiguous"
11775 if (warn_duplicated_cond
)
11777 /* We don't need the condition chain anymore. */
11783 /* Now we're all done with the if-statement. */
11784 finish_if_stmt (statement
);
11788 bool in_switch_statement_p
;
11789 unsigned char in_statement
;
11791 /* Add the condition. */
11792 finish_switch_cond (condition
, statement
);
11794 /* Parse the body of the switch-statement. */
11795 in_switch_statement_p
= parser
->in_switch_statement_p
;
11796 in_statement
= parser
->in_statement
;
11797 parser
->in_switch_statement_p
= true;
11798 parser
->in_statement
|= IN_SWITCH_STMT
;
11799 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11801 parser
->in_switch_statement_p
= in_switch_statement_p
;
11802 parser
->in_statement
= in_statement
;
11804 /* Now we're all done with the switch-statement. */
11805 finish_switch_stmt (statement
);
11813 cp_parser_error (parser
, "expected selection-statement");
11814 return error_mark_node
;
11818 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11819 If we have seen at least one decl-specifier, and the next token
11820 is not a parenthesis, then we must be looking at a declaration.
11821 (After "int (" we might be looking at a functional cast.) */
11824 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11825 bool any_specifiers_p
)
11827 if (any_specifiers_p
11828 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11829 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11830 && !cp_parser_error_occurred (parser
))
11831 cp_parser_commit_to_tentative_parse (parser
);
11834 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11835 The declarator shall not specify a function or an array. Returns
11836 TRUE if the declarator is valid, FALSE otherwise. */
11839 cp_parser_check_condition_declarator (cp_parser
* parser
,
11840 cp_declarator
*declarator
,
11843 if (declarator
== cp_error_declarator
11844 || function_declarator_p (declarator
)
11845 || declarator
->kind
== cdk_array
)
11847 if (declarator
== cp_error_declarator
)
11848 /* Already complained. */;
11849 else if (declarator
->kind
== cdk_array
)
11850 error_at (loc
, "condition declares an array");
11852 error_at (loc
, "condition declares a function");
11853 if (parser
->fully_implicit_function_template_p
)
11854 abort_fully_implicit_template (parser
);
11855 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11856 /*or_comma=*/false,
11857 /*consume_paren=*/false);
11864 /* Parse a condition.
11868 type-specifier-seq declarator = initializer-clause
11869 type-specifier-seq declarator braced-init-list
11874 type-specifier-seq declarator asm-specification [opt]
11875 attributes [opt] = assignment-expression
11877 Returns the expression that should be tested. */
11880 cp_parser_condition (cp_parser
* parser
)
11882 cp_decl_specifier_seq type_specifiers
;
11883 const char *saved_message
;
11884 int declares_class_or_enum
;
11886 /* Try the declaration first. */
11887 cp_parser_parse_tentatively (parser
);
11888 /* New types are not allowed in the type-specifier-seq for a
11890 saved_message
= parser
->type_definition_forbidden_message
;
11891 parser
->type_definition_forbidden_message
11892 = G_("types may not be defined in conditions");
11893 /* Parse the type-specifier-seq. */
11894 cp_parser_decl_specifier_seq (parser
,
11895 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11897 &declares_class_or_enum
);
11898 /* Restore the saved message. */
11899 parser
->type_definition_forbidden_message
= saved_message
;
11901 cp_parser_maybe_commit_to_declaration (parser
,
11902 type_specifiers
.any_specifiers_p
);
11904 /* If all is well, we might be looking at a declaration. */
11905 if (!cp_parser_error_occurred (parser
))
11908 tree asm_specification
;
11910 cp_declarator
*declarator
;
11911 tree initializer
= NULL_TREE
;
11912 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11914 /* Parse the declarator. */
11915 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11916 /*ctor_dtor_or_conv_p=*/NULL
,
11917 /*parenthesized_p=*/NULL
,
11918 /*member_p=*/false,
11919 /*friend_p=*/false);
11920 /* Parse the attributes. */
11921 attributes
= cp_parser_attributes_opt (parser
);
11922 /* Parse the asm-specification. */
11923 asm_specification
= cp_parser_asm_specification_opt (parser
);
11924 /* If the next token is not an `=' or '{', then we might still be
11925 looking at an expression. For example:
11929 looks like a decl-specifier-seq and a declarator -- but then
11930 there is no `=', so this is an expression. */
11931 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11932 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11933 cp_parser_simulate_error (parser
);
11935 /* If we did see an `=' or '{', then we are looking at a declaration
11937 if (cp_parser_parse_definitely (parser
))
11940 bool non_constant_p
= false;
11941 int flags
= LOOKUP_ONLYCONVERTING
;
11943 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11944 return error_mark_node
;
11946 /* Create the declaration. */
11947 decl
= start_decl (declarator
, &type_specifiers
,
11948 /*initialized_p=*/true,
11949 attributes
, /*prefix_attributes=*/NULL_TREE
,
11952 /* Parse the initializer. */
11953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11955 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11956 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11959 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11961 /* Consume the `='. */
11962 cp_lexer_consume_token (parser
->lexer
);
11963 initializer
= cp_parser_initializer_clause (parser
,
11968 cp_parser_error (parser
, "expected initializer");
11969 initializer
= error_mark_node
;
11971 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11974 /* Process the initializer. */
11975 cp_finish_decl (decl
,
11976 initializer
, !non_constant_p
,
11981 pop_scope (pushed_scope
);
11983 return convert_from_reference (decl
);
11986 /* If we didn't even get past the declarator successfully, we are
11987 definitely not looking at a declaration. */
11989 cp_parser_abort_tentative_parse (parser
);
11991 /* Otherwise, we are looking at an expression. */
11992 return cp_parser_expression (parser
);
11995 /* Parses a for-statement or range-for-statement until the closing ')',
11999 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12001 tree init
, scope
, decl
;
12004 /* Begin the for-statement. */
12005 scope
= begin_for_scope (&init
);
12007 /* Parse the initialization. */
12008 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12011 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12014 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12018 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12019 unsigned short unroll
)
12021 /* Normal for loop */
12022 tree condition
= NULL_TREE
;
12023 tree expression
= NULL_TREE
;
12026 stmt
= begin_for_stmt (scope
, init
);
12027 /* The init-statement has already been parsed in
12028 cp_parser_init_statement, so no work is needed here. */
12029 finish_init_stmt (stmt
);
12031 /* If there's a condition, process it. */
12032 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12033 condition
= cp_parser_condition (parser
);
12036 cp_parser_error (parser
, "missing loop condition in loop with "
12037 "%<GCC ivdep%> pragma");
12038 condition
= error_mark_node
;
12042 cp_parser_error (parser
, "missing loop condition in loop with "
12043 "%<GCC unroll%> pragma");
12044 condition
= error_mark_node
;
12046 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12047 /* Look for the `;'. */
12048 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12050 /* If there's an expression, process it. */
12051 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12052 expression
= cp_parser_expression (parser
);
12053 finish_for_expr (expression
, stmt
);
12058 /* Tries to parse a range-based for-statement:
12061 decl-specifier-seq declarator : expression
12063 The decl-specifier-seq declarator and the `:' are already parsed by
12064 cp_parser_init_statement. If processing_template_decl it returns a
12065 newly created RANGE_FOR_STMT; if not, it is converted to a
12066 regular FOR_STMT. */
12069 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12070 bool ivdep
, unsigned short unroll
, bool is_omp
)
12072 tree stmt
, range_expr
;
12073 auto_vec
<cxx_binding
*, 16> bindings
;
12074 auto_vec
<tree
, 16> names
;
12075 tree decomp_first_name
= NULL_TREE
;
12076 unsigned int decomp_cnt
= 0;
12078 /* Get the range declaration momentarily out of the way so that
12079 the range expression doesn't clash with it. */
12080 if (range_decl
!= error_mark_node
)
12082 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12084 tree v
= DECL_VALUE_EXPR (range_decl
);
12085 /* For decomposition declaration get all of the corresponding
12086 declarations out of the way. */
12087 if (TREE_CODE (v
) == ARRAY_REF
12088 && VAR_P (TREE_OPERAND (v
, 0))
12089 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12091 tree d
= range_decl
;
12092 range_decl
= TREE_OPERAND (v
, 0);
12093 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12094 decomp_first_name
= d
;
12095 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12097 tree name
= DECL_NAME (d
);
12098 names
.safe_push (name
);
12099 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12100 IDENTIFIER_BINDING (name
)
12101 = IDENTIFIER_BINDING (name
)->previous
;
12105 if (names
.is_empty ())
12107 tree name
= DECL_NAME (range_decl
);
12108 names
.safe_push (name
);
12109 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12110 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12116 bool expr_non_constant_p
;
12117 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12120 range_expr
= cp_parser_expression (parser
);
12122 /* Put the range declaration(s) back into scope. */
12123 for (unsigned int i
= 0; i
< names
.length (); i
++)
12125 cxx_binding
*binding
= bindings
[i
];
12126 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12127 IDENTIFIER_BINDING (names
[i
]) = binding
;
12130 /* finish_omp_for has its own code for the following, so just
12131 return the range_expr instead. */
12135 /* If in template, STMT is converted to a normal for-statement
12136 at instantiation. If not, it is done just ahead. */
12137 if (processing_template_decl
)
12139 if (check_for_bare_parameter_packs (range_expr
))
12140 range_expr
= error_mark_node
;
12141 stmt
= begin_range_for_stmt (scope
, init
);
12143 RANGE_FOR_IVDEP (stmt
) = 1;
12145 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12146 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12147 if (!type_dependent_expression_p (range_expr
)
12148 /* do_auto_deduction doesn't mess with template init-lists. */
12149 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12150 do_range_for_auto_deduction (range_decl
, range_expr
);
12154 stmt
= begin_for_stmt (scope
, init
);
12155 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12156 decomp_first_name
, decomp_cnt
, ivdep
,
12162 /* Subroutine of cp_convert_range_for: given the initializer expression,
12163 builds up the range temporary. */
12166 build_range_temp (tree range_expr
)
12168 tree range_type
, range_temp
;
12170 /* Find out the type deduced by the declaration
12171 `auto &&__range = range_expr'. */
12172 range_type
= cp_build_reference_type (make_auto (), true);
12173 range_type
= do_auto_deduction (range_type
, range_expr
,
12174 type_uses_auto (range_type
));
12176 /* Create the __range variable. */
12177 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12179 TREE_USED (range_temp
) = 1;
12180 DECL_ARTIFICIAL (range_temp
) = 1;
12185 /* Used by cp_parser_range_for in template context: we aren't going to
12186 do a full conversion yet, but we still need to resolve auto in the
12187 type of the for-range-declaration if present. This is basically
12188 a shortcut version of cp_convert_range_for. */
12191 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12193 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12196 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12197 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12198 iter_type
= (cp_parser_perform_range_for_lookup
12199 (range_temp
, &begin_dummy
, &end_dummy
));
12202 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12204 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12206 tf_warning_or_error
);
12207 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12208 iter_decl
, auto_node
);
12213 /* Converts a range-based for-statement into a normal
12214 for-statement, as per the definition.
12216 for (RANGE_DECL : RANGE_EXPR)
12219 should be equivalent to:
12222 auto &&__range = RANGE_EXPR;
12223 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12227 RANGE_DECL = *__begin;
12232 If RANGE_EXPR is an array:
12233 BEGIN_EXPR = __range
12234 END_EXPR = __range + ARRAY_SIZE(__range)
12235 Else if RANGE_EXPR has a member 'begin' or 'end':
12236 BEGIN_EXPR = __range.begin()
12237 END_EXPR = __range.end()
12239 BEGIN_EXPR = begin(__range)
12240 END_EXPR = end(__range);
12242 If __range has a member 'begin' but not 'end', or vice versa, we must
12243 still use the second alternative (it will surely fail, however).
12244 When calling begin()/end() in the third alternative we must use
12245 argument dependent lookup, but always considering 'std' as an associated
12249 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12250 tree decomp_first_name
, unsigned int decomp_cnt
,
12251 bool ivdep
, unsigned short unroll
)
12254 tree iter_type
, begin_expr
, end_expr
;
12255 tree condition
, expression
;
12257 range_expr
= mark_lvalue_use (range_expr
);
12259 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12260 /* If an error happened previously do nothing or else a lot of
12261 unhelpful errors would be issued. */
12262 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12267 if (VAR_P (range_expr
)
12268 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12269 /* Can't bind a reference to an array of runtime bound. */
12270 range_temp
= range_expr
;
12273 range_temp
= build_range_temp (range_expr
);
12274 pushdecl (range_temp
);
12275 cp_finish_decl (range_temp
, range_expr
,
12276 /*is_constant_init*/false, NULL_TREE
,
12277 LOOKUP_ONLYCONVERTING
);
12278 range_temp
= convert_from_reference (range_temp
);
12280 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12281 &begin_expr
, &end_expr
);
12284 /* The new for initialization statement. */
12285 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12287 TREE_USED (begin
) = 1;
12288 DECL_ARTIFICIAL (begin
) = 1;
12290 cp_finish_decl (begin
, begin_expr
,
12291 /*is_constant_init*/false, NULL_TREE
,
12292 LOOKUP_ONLYCONVERTING
);
12294 if (cxx_dialect
>= cxx17
)
12295 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12296 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12297 TREE_USED (end
) = 1;
12298 DECL_ARTIFICIAL (end
) = 1;
12300 cp_finish_decl (end
, end_expr
,
12301 /*is_constant_init*/false, NULL_TREE
,
12302 LOOKUP_ONLYCONVERTING
);
12304 finish_init_stmt (statement
);
12306 /* The new for condition. */
12307 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12310 NULL
, tf_warning_or_error
);
12311 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12313 /* The new increment expression. */
12314 expression
= finish_unary_op_expr (input_location
,
12315 PREINCREMENT_EXPR
, begin
,
12316 tf_warning_or_error
);
12317 finish_for_expr (expression
, statement
);
12319 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12320 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12322 /* The declaration is initialized with *__begin inside the loop body. */
12323 cp_finish_decl (range_decl
,
12324 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12325 tf_warning_or_error
),
12326 /*is_constant_init*/false, NULL_TREE
,
12327 LOOKUP_ONLYCONVERTING
);
12328 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12329 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12334 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12335 We need to solve both at the same time because the method used
12336 depends on the existence of members begin or end.
12337 Returns the type deduced for the iterator expression. */
12340 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12342 if (error_operand_p (range
))
12344 *begin
= *end
= error_mark_node
;
12345 return error_mark_node
;
12348 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12350 error ("range-based %<for%> expression of type %qT "
12351 "has incomplete type", TREE_TYPE (range
));
12352 *begin
= *end
= error_mark_node
;
12353 return error_mark_node
;
12355 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12357 /* If RANGE is an array, we will use pointer arithmetic. */
12358 *begin
= decay_conversion (range
, tf_warning_or_error
);
12359 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12361 array_type_nelts_top (TREE_TYPE (range
)),
12363 return TREE_TYPE (*begin
);
12367 /* If it is not an array, we must do a bit of magic. */
12368 tree id_begin
, id_end
;
12369 tree member_begin
, member_end
;
12371 *begin
= *end
= error_mark_node
;
12373 id_begin
= get_identifier ("begin");
12374 id_end
= get_identifier ("end");
12375 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12376 /*protect=*/2, /*want_type=*/false,
12377 tf_warning_or_error
);
12378 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12379 /*protect=*/2, /*want_type=*/false,
12380 tf_warning_or_error
);
12382 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12384 /* Use the member functions. */
12385 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12386 *end
= cp_parser_range_for_member_function (range
, id_end
);
12390 /* Use global functions with ADL. */
12391 vec
<tree
, va_gc
> *vec
;
12392 vec
= make_tree_vector ();
12394 vec_safe_push (vec
, range
);
12396 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12397 tf_warning_or_error
);
12398 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12399 tf_warning_or_error
);
12400 member_end
= perform_koenig_lookup (id_end
, vec
,
12401 tf_warning_or_error
);
12402 *end
= finish_call_expr (member_end
, &vec
, false, true,
12403 tf_warning_or_error
);
12405 release_tree_vector (vec
);
12408 /* Last common checks. */
12409 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12411 /* If one of the expressions is an error do no more checks. */
12412 *begin
= *end
= error_mark_node
;
12413 return error_mark_node
;
12415 else if (type_dependent_expression_p (*begin
)
12416 || type_dependent_expression_p (*end
))
12417 /* Can happen, when, eg, in a template context, Koenig lookup
12418 can't resolve begin/end (c++/58503). */
12422 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12423 /* The unqualified type of the __begin and __end temporaries should
12424 be the same, as required by the multiple auto declaration. */
12425 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12427 if (cxx_dialect
>= cxx17
12428 && (build_x_binary_op (input_location
, NE_EXPR
,
12429 *begin
, ERROR_MARK
,
12432 != error_mark_node
))
12433 /* P0184R0 allows __begin and __end to have different types,
12434 but make sure they are comparable so we can give a better
12437 error ("inconsistent begin/end types in range-based %<for%> "
12438 "statement: %qT and %qT",
12439 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12446 /* Helper function for cp_parser_perform_range_for_lookup.
12447 Builds a tree for RANGE.IDENTIFIER(). */
12450 cp_parser_range_for_member_function (tree range
, tree identifier
)
12453 vec
<tree
, va_gc
> *vec
;
12455 member
= finish_class_member_access_expr (range
, identifier
,
12456 false, tf_warning_or_error
);
12457 if (member
== error_mark_node
)
12458 return error_mark_node
;
12460 vec
= make_tree_vector ();
12461 res
= finish_call_expr (member
, &vec
,
12462 /*disallow_virtual=*/false,
12463 /*koenig_p=*/false,
12464 tf_warning_or_error
);
12465 release_tree_vector (vec
);
12469 /* Parse an iteration-statement.
12471 iteration-statement:
12472 while ( condition ) statement
12473 do statement while ( expression ) ;
12474 for ( init-statement condition [opt] ; expression [opt] )
12477 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12480 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12481 unsigned short unroll
)
12486 unsigned char in_statement
;
12487 token_indent_info guard_tinfo
;
12489 /* Peek at the next token. */
12490 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12492 return error_mark_node
;
12494 guard_tinfo
= get_token_indent_info (token
);
12496 /* Remember whether or not we are already within an iteration
12498 in_statement
= parser
->in_statement
;
12500 /* See what kind of keyword it is. */
12501 keyword
= token
->keyword
;
12508 /* Begin the while-statement. */
12509 statement
= begin_while_stmt ();
12510 /* Look for the `('. */
12511 matching_parens parens
;
12512 parens
.require_open (parser
);
12513 /* Parse the condition. */
12514 condition
= cp_parser_condition (parser
);
12515 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12516 /* Look for the `)'. */
12517 parens
.require_close (parser
);
12518 /* Parse the dependent statement. */
12519 parser
->in_statement
= IN_ITERATION_STMT
;
12520 bool prev
= note_iteration_stmt_body_start ();
12521 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12522 note_iteration_stmt_body_end (prev
);
12523 parser
->in_statement
= in_statement
;
12524 /* We're done with the while-statement. */
12525 finish_while_stmt (statement
);
12533 /* Begin the do-statement. */
12534 statement
= begin_do_stmt ();
12535 /* Parse the body of the do-statement. */
12536 parser
->in_statement
= IN_ITERATION_STMT
;
12537 bool prev
= note_iteration_stmt_body_start ();
12538 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12539 note_iteration_stmt_body_end (prev
);
12540 parser
->in_statement
= in_statement
;
12541 finish_do_body (statement
);
12542 /* Look for the `while' keyword. */
12543 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12544 /* Look for the `('. */
12545 matching_parens parens
;
12546 parens
.require_open (parser
);
12547 /* Parse the expression. */
12548 expression
= cp_parser_expression (parser
);
12549 /* We're done with the do-statement. */
12550 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12551 /* Look for the `)'. */
12552 parens
.require_close (parser
);
12553 /* Look for the `;'. */
12554 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12560 /* Look for the `('. */
12561 matching_parens parens
;
12562 parens
.require_open (parser
);
12564 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12566 /* Look for the `)'. */
12567 parens
.require_close (parser
);
12569 /* Parse the body of the for-statement. */
12570 parser
->in_statement
= IN_ITERATION_STMT
;
12571 bool prev
= note_iteration_stmt_body_start ();
12572 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12573 note_iteration_stmt_body_end (prev
);
12574 parser
->in_statement
= in_statement
;
12576 /* We're done with the for-statement. */
12577 finish_for_stmt (statement
);
12582 cp_parser_error (parser
, "expected iteration-statement");
12583 statement
= error_mark_node
;
12590 /* Parse a init-statement or the declarator of a range-based-for.
12591 Returns true if a range-based-for declaration is seen.
12594 expression-statement
12595 simple-declaration */
12598 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12600 /* If the next token is a `;', then we have an empty
12601 expression-statement. Grammatically, this is also a
12602 simple-declaration, but an invalid one, because it does not
12603 declare anything. Therefore, if we did not handle this case
12604 specially, we would issue an error message about an invalid
12606 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12608 bool is_range_for
= false;
12609 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12611 /* Try to parse the init-statement. */
12612 if (cp_parser_range_based_for_with_init_p (parser
))
12615 cp_parser_parse_tentatively (parser
);
12616 /* Parse the declaration. */
12617 cp_parser_simple_declaration (parser
,
12618 /*function_definition_allowed_p=*/false,
12620 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12621 if (!cp_parser_parse_definitely (parser
))
12622 /* That didn't work, try to parse it as an expression-statement. */
12623 cp_parser_expression_statement (parser
, NULL_TREE
);
12625 if (cxx_dialect
< cxx2a
)
12627 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12628 "range-based %<for%> loops with initializer only "
12629 "available with -std=c++2a or -std=gnu++2a");
12630 *decl
= error_mark_node
;
12634 /* A colon is used in range-based for. */
12635 parser
->colon_corrects_to_scope_p
= false;
12637 /* We're going to speculatively look for a declaration, falling back
12638 to an expression, if necessary. */
12639 cp_parser_parse_tentatively (parser
);
12640 /* Parse the declaration. */
12641 cp_parser_simple_declaration (parser
,
12642 /*function_definition_allowed_p=*/false,
12644 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12647 /* It is a range-for, consume the ':'. */
12648 cp_lexer_consume_token (parser
->lexer
);
12649 is_range_for
= true;
12650 if (cxx_dialect
< cxx11
)
12651 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12652 "range-based %<for%> loops only available with "
12653 "-std=c++11 or -std=gnu++11");
12656 /* The ';' is not consumed yet because we told
12657 cp_parser_simple_declaration not to. */
12658 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12660 if (cp_parser_parse_definitely (parser
))
12661 return is_range_for
;
12662 /* If the tentative parse failed, then we shall need to look for an
12663 expression-statement. */
12665 /* If we are here, it is an expression-statement. */
12666 cp_parser_expression_statement (parser
, NULL_TREE
);
12670 /* Parse a jump-statement.
12675 return expression [opt] ;
12676 return braced-init-list ;
12682 goto * expression ;
12684 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12687 cp_parser_jump_statement (cp_parser
* parser
)
12689 tree statement
= error_mark_node
;
12692 unsigned char in_statement
;
12694 /* Peek at the next token. */
12695 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12697 return error_mark_node
;
12699 /* See what kind of keyword it is. */
12700 keyword
= token
->keyword
;
12704 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12705 switch (in_statement
)
12708 error_at (token
->location
, "break statement not within loop or switch");
12711 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12712 || in_statement
== IN_ITERATION_STMT
);
12713 statement
= finish_break_stmt ();
12714 if (in_statement
== IN_ITERATION_STMT
)
12715 break_maybe_infinite_loop ();
12718 error_at (token
->location
, "invalid exit from OpenMP structured block");
12721 error_at (token
->location
, "break statement used with OpenMP for loop");
12724 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12728 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12731 error_at (token
->location
, "continue statement not within a loop");
12733 /* Fall through. */
12734 case IN_ITERATION_STMT
:
12736 statement
= finish_continue_stmt ();
12739 error_at (token
->location
, "invalid exit from OpenMP structured block");
12742 gcc_unreachable ();
12744 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12750 bool expr_non_constant_p
;
12752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12754 cp_lexer_set_source_position (parser
->lexer
);
12755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12756 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12758 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12759 expr
= cp_parser_expression (parser
);
12761 /* If the next token is a `;', then there is no
12764 /* Build the return-statement. */
12765 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12766 /* Don't deduce from a discarded return statement. */;
12768 statement
= finish_return_stmt (expr
);
12769 /* Look for the final `;'. */
12770 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12775 if (parser
->in_function_body
12776 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12778 error ("%<goto%> in %<constexpr%> function");
12779 cp_function_chain
->invalid_constexpr
= true;
12782 /* Create the goto-statement. */
12783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12785 /* Issue a warning about this use of a GNU extension. */
12786 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12787 /* Consume the '*' token. */
12788 cp_lexer_consume_token (parser
->lexer
);
12789 /* Parse the dependent expression. */
12790 finish_goto_stmt (cp_parser_expression (parser
));
12793 finish_goto_stmt (cp_parser_identifier (parser
));
12794 /* Look for the final `;'. */
12795 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12799 cp_parser_error (parser
, "expected jump-statement");
12806 /* Parse a declaration-statement.
12808 declaration-statement:
12809 block-declaration */
12812 cp_parser_declaration_statement (cp_parser
* parser
)
12816 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12817 p
= obstack_alloc (&declarator_obstack
, 0);
12819 /* Parse the block-declaration. */
12820 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12822 /* Free any declarators allocated. */
12823 obstack_free (&declarator_obstack
, p
);
12826 /* Some dependent statements (like `if (cond) statement'), are
12827 implicitly in their own scope. In other words, if the statement is
12828 a single statement (as opposed to a compound-statement), it is
12829 none-the-less treated as if it were enclosed in braces. Any
12830 declarations appearing in the dependent statement are out of scope
12831 after control passes that point. This function parses a statement,
12832 but ensures that is in its own scope, even if it is not a
12833 compound-statement.
12835 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12836 is a (possibly labeled) if statement which is not enclosed in
12837 braces and has an else clause. This is used to implement
12840 CHAIN is a vector of if-else-if conditions. This is used to implement
12843 Returns the new statement. */
12846 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12847 const token_indent_info
&guard_tinfo
,
12851 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12852 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12853 token_indent_info body_tinfo
12854 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12859 /* Mark if () ; with a special NOP_EXPR. */
12860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12862 cp_lexer_consume_token (parser
->lexer
);
12863 statement
= add_stmt (build_empty_stmt (body_loc
));
12865 if (guard_tinfo
.keyword
== RID_IF
12866 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12867 warning_at (body_loc
, OPT_Wempty_body
,
12868 "suggest braces around empty body in an %<if%> statement");
12869 else if (guard_tinfo
.keyword
== RID_ELSE
)
12870 warning_at (body_loc
, OPT_Wempty_body
,
12871 "suggest braces around empty body in an %<else%> statement");
12873 /* if a compound is opened, we simply parse the statement directly. */
12874 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12875 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12876 /* If the token is not a `{', then we must take special action. */
12879 /* Create a compound-statement. */
12880 statement
= begin_compound_stmt (0);
12881 /* Parse the dependent-statement. */
12882 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12883 &body_loc_after_labels
);
12884 /* Finish the dummy compound-statement. */
12885 finish_compound_stmt (statement
);
12888 token_indent_info next_tinfo
12889 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12890 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12892 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12893 && next_tinfo
.type
!= CPP_SEMICOLON
)
12894 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12895 guard_tinfo
.location
, guard_tinfo
.keyword
);
12897 /* Return the statement. */
12901 /* For some dependent statements (like `while (cond) statement'), we
12902 have already created a scope. Therefore, even if the dependent
12903 statement is a compound-statement, we do not want to create another
12907 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12908 const token_indent_info
&guard_tinfo
)
12910 /* If the token is a `{', then we must take special action. */
12911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12913 token_indent_info body_tinfo
12914 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12915 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12917 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12918 &loc_after_labels
);
12919 token_indent_info next_tinfo
12920 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12921 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12923 if (loc_after_labels
!= UNKNOWN_LOCATION
12924 && next_tinfo
.type
!= CPP_SEMICOLON
)
12925 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12926 guard_tinfo
.location
,
12927 guard_tinfo
.keyword
);
12931 /* Avoid calling cp_parser_compound_statement, so that we
12932 don't create a new scope. Do everything else by hand. */
12933 matching_braces braces
;
12934 braces
.require_open (parser
);
12935 /* If the next keyword is `__label__' we have a label declaration. */
12936 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12937 cp_parser_label_declaration (parser
);
12938 /* Parse an (optional) statement-seq. */
12939 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12940 braces
.require_close (parser
);
12944 /* Declarations [gram.dcl.dcl] */
12946 /* Parse an optional declaration-sequence.
12950 declaration-seq declaration */
12953 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12957 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12959 if (token
->type
== CPP_CLOSE_BRACE
12960 || token
->type
== CPP_EOF
)
12963 cp_parser_toplevel_declaration (parser
);
12967 /* Parse a declaration.
12971 function-definition
12972 template-declaration
12973 explicit-instantiation
12974 explicit-specialization
12975 linkage-specification
12976 namespace-definition
12984 __extension__ declaration */
12987 cp_parser_declaration (cp_parser
* parser
)
12991 int saved_pedantic
;
12993 tree attributes
= NULL_TREE
;
12995 /* Check for the `__extension__' keyword. */
12996 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12998 /* Parse the qualified declaration. */
12999 cp_parser_declaration (parser
);
13000 /* Restore the PEDANTIC flag. */
13001 pedantic
= saved_pedantic
;
13006 /* Try to figure out what kind of declaration is present. */
13007 token1
= *cp_lexer_peek_token (parser
->lexer
);
13009 if (token1
.type
!= CPP_EOF
)
13010 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13013 token2
.type
= CPP_EOF
;
13014 token2
.keyword
= RID_MAX
;
13017 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13018 p
= obstack_alloc (&declarator_obstack
, 0);
13020 /* If the next token is `extern' and the following token is a string
13021 literal, then we have a linkage specification. */
13022 if (token1
.keyword
== RID_EXTERN
13023 && cp_parser_is_pure_string_literal (&token2
))
13024 cp_parser_linkage_specification (parser
);
13025 /* If the next token is `template', then we have either a template
13026 declaration, an explicit instantiation, or an explicit
13028 else if (token1
.keyword
== RID_TEMPLATE
)
13030 /* `template <>' indicates a template specialization. */
13031 if (token2
.type
== CPP_LESS
13032 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13033 cp_parser_explicit_specialization (parser
);
13034 /* `template <' indicates a template declaration. */
13035 else if (token2
.type
== CPP_LESS
)
13036 cp_parser_template_declaration (parser
, /*member_p=*/false);
13037 /* Anything else must be an explicit instantiation. */
13039 cp_parser_explicit_instantiation (parser
);
13041 /* If the next token is `export', then we have a template
13043 else if (token1
.keyword
== RID_EXPORT
)
13044 cp_parser_template_declaration (parser
, /*member_p=*/false);
13045 /* If the next token is `extern', 'static' or 'inline' and the one
13046 after that is `template', we have a GNU extended explicit
13047 instantiation directive. */
13048 else if (cp_parser_allow_gnu_extensions_p (parser
)
13049 && (token1
.keyword
== RID_EXTERN
13050 || token1
.keyword
== RID_STATIC
13051 || token1
.keyword
== RID_INLINE
)
13052 && token2
.keyword
== RID_TEMPLATE
)
13053 cp_parser_explicit_instantiation (parser
);
13054 /* If the next token is `namespace', check for a named or unnamed
13055 namespace definition. */
13056 else if (token1
.keyword
== RID_NAMESPACE
13057 && (/* A named namespace definition. */
13058 (token2
.type
== CPP_NAME
13059 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13061 || (token2
.type
== CPP_OPEN_SQUARE
13062 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13063 == CPP_OPEN_SQUARE
)
13064 /* An unnamed namespace definition. */
13065 || token2
.type
== CPP_OPEN_BRACE
13066 || token2
.keyword
== RID_ATTRIBUTE
))
13067 cp_parser_namespace_definition (parser
);
13068 /* An inline (associated) namespace definition. */
13069 else if (token1
.keyword
== RID_INLINE
13070 && token2
.keyword
== RID_NAMESPACE
)
13071 cp_parser_namespace_definition (parser
);
13072 /* Objective-C++ declaration/definition. */
13073 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13074 cp_parser_objc_declaration (parser
, NULL_TREE
);
13075 else if (c_dialect_objc ()
13076 && token1
.keyword
== RID_ATTRIBUTE
13077 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13078 cp_parser_objc_declaration (parser
, attributes
);
13079 /* At this point we may have a template declared by a concept
13081 else if (flag_concepts
13082 && cp_parser_template_declaration_after_export (parser
,
13083 /*member_p=*/false))
13086 /* Try to parse a block-declaration, or a function-definition. */
13087 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13089 /* Free any declarators allocated. */
13090 obstack_free (&declarator_obstack
, p
);
13093 /* Parse a namespace-scope declaration. */
13096 cp_parser_toplevel_declaration (cp_parser
* parser
)
13098 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13100 if (token
->type
== CPP_PRAGMA
)
13101 /* A top-level declaration can consist solely of a #pragma. A
13102 nested declaration cannot, so this is done here and not in
13103 cp_parser_declaration. (A #pragma at block scope is
13104 handled in cp_parser_statement.) */
13105 cp_parser_pragma (parser
, pragma_external
, NULL
);
13106 else if (token
->type
== CPP_SEMICOLON
)
13108 /* A declaration consisting of a single semicolon is
13109 invalid. Allow it unless we're being pedantic. */
13110 cp_lexer_consume_token (parser
->lexer
);
13111 if (!in_system_header_at (input_location
))
13112 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13115 /* Parse the declaration itself. */
13116 cp_parser_declaration (parser
);
13119 /* Parse a block-declaration.
13124 namespace-alias-definition
13131 __extension__ block-declaration
13136 static_assert-declaration
13138 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13139 part of a declaration-statement. */
13142 cp_parser_block_declaration (cp_parser
*parser
,
13146 int saved_pedantic
;
13148 /* Check for the `__extension__' keyword. */
13149 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13151 /* Parse the qualified declaration. */
13152 cp_parser_block_declaration (parser
, statement_p
);
13153 /* Restore the PEDANTIC flag. */
13154 pedantic
= saved_pedantic
;
13159 /* Peek at the next token to figure out which kind of declaration is
13161 token1
= cp_lexer_peek_token (parser
->lexer
);
13163 /* If the next keyword is `asm', we have an asm-definition. */
13164 if (token1
->keyword
== RID_ASM
)
13167 cp_parser_commit_to_tentative_parse (parser
);
13168 cp_parser_asm_definition (parser
);
13170 /* If the next keyword is `namespace', we have a
13171 namespace-alias-definition. */
13172 else if (token1
->keyword
== RID_NAMESPACE
)
13173 cp_parser_namespace_alias_definition (parser
);
13174 /* If the next keyword is `using', we have a
13175 using-declaration, a using-directive, or an alias-declaration. */
13176 else if (token1
->keyword
== RID_USING
)
13181 cp_parser_commit_to_tentative_parse (parser
);
13182 /* If the token after `using' is `namespace', then we have a
13183 using-directive. */
13184 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13185 if (token2
->keyword
== RID_NAMESPACE
)
13186 cp_parser_using_directive (parser
);
13187 /* If the second token after 'using' is '=', then we have an
13188 alias-declaration. */
13189 else if (cxx_dialect
>= cxx11
13190 && token2
->type
== CPP_NAME
13191 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13192 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13193 cp_parser_alias_declaration (parser
);
13194 /* Otherwise, it's a using-declaration. */
13196 cp_parser_using_declaration (parser
,
13197 /*access_declaration_p=*/false);
13199 /* If the next keyword is `__label__' we have a misplaced label
13201 else if (token1
->keyword
== RID_LABEL
)
13203 cp_lexer_consume_token (parser
->lexer
);
13204 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13205 cp_parser_skip_to_end_of_statement (parser
);
13206 /* If the next token is now a `;', consume it. */
13207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13208 cp_lexer_consume_token (parser
->lexer
);
13210 /* If the next token is `static_assert' we have a static assertion. */
13211 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13212 cp_parser_static_assert (parser
, /*member_p=*/false);
13213 /* Anything else must be a simple-declaration. */
13215 cp_parser_simple_declaration (parser
, !statement_p
,
13216 /*maybe_range_for_decl*/NULL
);
13219 /* Parse a simple-declaration.
13221 simple-declaration:
13222 decl-specifier-seq [opt] init-declarator-list [opt] ;
13223 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13224 brace-or-equal-initializer ;
13226 init-declarator-list:
13228 init-declarator-list , init-declarator
13230 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13231 function-definition as a simple-declaration.
13233 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13234 parsed declaration if it is an uninitialized single declarator not followed
13235 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13236 if present, will not be consumed. */
13239 cp_parser_simple_declaration (cp_parser
* parser
,
13240 bool function_definition_allowed_p
,
13241 tree
*maybe_range_for_decl
)
13243 cp_decl_specifier_seq decl_specifiers
;
13244 int declares_class_or_enum
;
13245 bool saw_declarator
;
13246 location_t comma_loc
= UNKNOWN_LOCATION
;
13247 location_t init_loc
= UNKNOWN_LOCATION
;
13249 if (maybe_range_for_decl
)
13250 *maybe_range_for_decl
= NULL_TREE
;
13252 /* Defer access checks until we know what is being declared; the
13253 checks for names appearing in the decl-specifier-seq should be
13254 done as if we were in the scope of the thing being declared. */
13255 push_deferring_access_checks (dk_deferred
);
13257 /* Parse the decl-specifier-seq. We have to keep track of whether
13258 or not the decl-specifier-seq declares a named class or
13259 enumeration type, since that is the only case in which the
13260 init-declarator-list is allowed to be empty.
13264 In a simple-declaration, the optional init-declarator-list can be
13265 omitted only when declaring a class or enumeration, that is when
13266 the decl-specifier-seq contains either a class-specifier, an
13267 elaborated-type-specifier, or an enum-specifier. */
13268 cp_parser_decl_specifier_seq (parser
,
13269 CP_PARSER_FLAGS_OPTIONAL
,
13271 &declares_class_or_enum
);
13272 /* We no longer need to defer access checks. */
13273 stop_deferring_access_checks ();
13275 /* In a block scope, a valid declaration must always have a
13276 decl-specifier-seq. By not trying to parse declarators, we can
13277 resolve the declaration/expression ambiguity more quickly. */
13278 if (!function_definition_allowed_p
13279 && !decl_specifiers
.any_specifiers_p
)
13281 cp_parser_error (parser
, "expected declaration");
13285 /* If the next two tokens are both identifiers, the code is
13286 erroneous. The usual cause of this situation is code like:
13290 where "T" should name a type -- but does not. */
13291 if (!decl_specifiers
.any_type_specifiers_p
13292 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13294 /* If parsing tentatively, we should commit; we really are
13295 looking at a declaration. */
13296 cp_parser_commit_to_tentative_parse (parser
);
13301 cp_parser_maybe_commit_to_declaration (parser
,
13302 decl_specifiers
.any_specifiers_p
);
13304 /* Look for C++17 decomposition declaration. */
13305 for (size_t n
= 1; ; n
++)
13306 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13307 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13309 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13310 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13311 && decl_specifiers
.any_specifiers_p
)
13314 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13315 maybe_range_for_decl
,
13318 /* The next token should be either a `,' or a `;'. */
13319 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13320 /* If it's a `;', we are done. */
13321 if (token
->type
== CPP_SEMICOLON
)
13323 else if (maybe_range_for_decl
)
13325 if (*maybe_range_for_decl
== NULL_TREE
)
13326 *maybe_range_for_decl
= error_mark_node
;
13329 /* Anything else is an error. */
13332 /* If we have already issued an error message we don't need
13333 to issue another one. */
13334 if ((decl
!= error_mark_node
13335 && DECL_INITIAL (decl
) != error_mark_node
)
13336 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13337 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13338 /* Skip tokens until we reach the end of the statement. */
13339 cp_parser_skip_to_end_of_statement (parser
);
13340 /* If the next token is now a `;', consume it. */
13341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13342 cp_lexer_consume_token (parser
->lexer
);
13350 bool auto_specifier_p
;
13351 /* NULL_TREE if both variable and function declaration are allowed,
13352 error_mark_node if function declaration are not allowed and
13353 a FUNCTION_DECL that should be diagnosed if it is followed by
13354 variable declarations. */
13355 tree auto_function_declaration
;
13357 last_type
= NULL_TREE
;
13359 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13360 auto_function_declaration
= NULL_TREE
;
13362 /* Keep going until we hit the `;' at the end of the simple
13364 saw_declarator
= false;
13365 while (cp_lexer_next_token_is_not (parser
->lexer
,
13369 bool function_definition_p
;
13371 tree auto_result
= NULL_TREE
;
13373 if (saw_declarator
)
13375 /* If we are processing next declarator, comma is expected */
13376 token
= cp_lexer_peek_token (parser
->lexer
);
13377 gcc_assert (token
->type
== CPP_COMMA
);
13378 cp_lexer_consume_token (parser
->lexer
);
13379 if (maybe_range_for_decl
)
13381 *maybe_range_for_decl
= error_mark_node
;
13382 if (comma_loc
== UNKNOWN_LOCATION
)
13383 comma_loc
= token
->location
;
13387 saw_declarator
= true;
13389 /* Parse the init-declarator. */
13390 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13392 function_definition_allowed_p
,
13393 /*member_p=*/false,
13394 declares_class_or_enum
,
13395 &function_definition_p
,
13396 maybe_range_for_decl
,
13399 /* If an error occurred while parsing tentatively, exit quickly.
13400 (That usually happens when in the body of a function; each
13401 statement is treated as a declaration-statement until proven
13403 if (cp_parser_error_occurred (parser
))
13406 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13408 /* If the init-declarator-list contains more than one
13409 init-declarator, they shall all form declarations of
13411 if (auto_function_declaration
== NULL_TREE
)
13412 auto_function_declaration
13413 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13414 else if (TREE_CODE (decl
) == FUNCTION_DECL
13415 || auto_function_declaration
!= error_mark_node
)
13417 error_at (decl_specifiers
.locations
[ds_type_spec
],
13418 "non-variable %qD in declaration with more than one "
13419 "declarator with placeholder type",
13420 TREE_CODE (decl
) == FUNCTION_DECL
13421 ? decl
: auto_function_declaration
);
13422 auto_function_declaration
= error_mark_node
;
13427 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13430 && last_type
!= error_mark_node
13431 && !same_type_p (auto_result
, last_type
))
13433 /* If the list of declarators contains more than one declarator,
13434 the type of each declared variable is determined as described
13435 above. If the type deduced for the template parameter U is not
13436 the same in each deduction, the program is ill-formed. */
13437 error_at (decl_specifiers
.locations
[ds_type_spec
],
13438 "inconsistent deduction for %qT: %qT and then %qT",
13439 decl_specifiers
.type
, last_type
, auto_result
);
13440 last_type
= error_mark_node
;
13443 last_type
= auto_result
;
13446 /* Handle function definitions specially. */
13447 if (function_definition_p
)
13449 /* If the next token is a `,', then we are probably
13450 processing something like:
13454 which is erroneous. */
13455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13458 error_at (token
->location
,
13460 " declarations and function-definitions is forbidden");
13462 /* Otherwise, we're done with the list of declarators. */
13465 pop_deferring_access_checks ();
13469 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13470 *maybe_range_for_decl
= decl
;
13471 /* The next token should be either a `,' or a `;'. */
13472 token
= cp_lexer_peek_token (parser
->lexer
);
13473 /* If it's a `,', there are more declarators to come. */
13474 if (token
->type
== CPP_COMMA
)
13475 /* will be consumed next time around */;
13476 /* If it's a `;', we are done. */
13477 else if (token
->type
== CPP_SEMICOLON
)
13479 else if (maybe_range_for_decl
)
13481 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13482 permerror (decl_specifiers
.locations
[ds_type_spec
],
13483 "types may not be defined in a for-range-declaration");
13486 /* Anything else is an error. */
13489 /* If we have already issued an error message we don't need
13490 to issue another one. */
13491 if ((decl
!= error_mark_node
13492 && DECL_INITIAL (decl
) != error_mark_node
)
13493 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13494 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13495 /* Skip tokens until we reach the end of the statement. */
13496 cp_parser_skip_to_end_of_statement (parser
);
13497 /* If the next token is now a `;', consume it. */
13498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13499 cp_lexer_consume_token (parser
->lexer
);
13502 /* After the first time around, a function-definition is not
13503 allowed -- even if it was OK at first. For example:
13508 function_definition_allowed_p
= false;
13511 /* Issue an error message if no declarators are present, and the
13512 decl-specifier-seq does not itself declare a class or
13513 enumeration: [dcl.dcl]/3. */
13514 if (!saw_declarator
)
13516 if (cp_parser_declares_only_class_p (parser
))
13518 if (!declares_class_or_enum
13519 && decl_specifiers
.type
13520 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13521 /* Ensure an error is issued anyway when finish_decltype_type,
13522 called via cp_parser_decl_specifier_seq, returns a class or
13523 an enumeration (c++/51786). */
13524 decl_specifiers
.type
= NULL_TREE
;
13525 shadow_tag (&decl_specifiers
);
13527 /* Perform any deferred access checks. */
13528 perform_deferred_access_checks (tf_warning_or_error
);
13531 /* Consume the `;'. */
13533 if (!maybe_range_for_decl
)
13534 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13535 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13537 if (init_loc
!= UNKNOWN_LOCATION
)
13538 error_at (init_loc
, "initializer in range-based %<for%> loop");
13539 if (comma_loc
!= UNKNOWN_LOCATION
)
13540 error_at (comma_loc
,
13541 "multiple declarations in range-based %<for%> loop");
13545 pop_deferring_access_checks ();
13548 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13549 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13553 cp_parser_decomposition_declaration (cp_parser
*parser
,
13554 cp_decl_specifier_seq
*decl_specifiers
,
13555 tree
*maybe_range_for_decl
,
13556 location_t
*init_loc
)
13558 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13559 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13560 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13562 /* Parse the identifier-list. */
13563 auto_vec
<cp_expr
, 10> v
;
13564 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13567 cp_expr e
= cp_parser_identifier (parser
);
13568 if (e
.get_value () == error_mark_node
)
13571 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13573 cp_lexer_consume_token (parser
->lexer
);
13576 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13577 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13579 end_loc
= UNKNOWN_LOCATION
;
13580 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13583 cp_lexer_consume_token (parser
->lexer
);
13586 cp_parser_skip_to_end_of_statement (parser
);
13587 return error_mark_node
;
13591 if (cxx_dialect
< cxx17
)
13592 pedwarn (loc
, 0, "structured bindings only available with "
13593 "-std=c++17 or -std=gnu++17");
13596 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13597 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13598 declarator
->id_loc
= loc
;
13599 if (ref_qual
!= REF_QUAL_NONE
)
13600 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13601 ref_qual
== REF_QUAL_RVALUE
,
13603 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13604 NULL_TREE
, decl_specifiers
->attributes
,
13606 tree orig_decl
= decl
;
13610 cp_decl_specifier_seq decl_specs
;
13611 clear_decl_specs (&decl_specs
);
13612 decl_specs
.type
= make_auto ();
13614 FOR_EACH_VEC_ELT (v
, i
, e
)
13617 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13618 sfk_none
, e
.get_location ());
13621 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13622 declarator
->id_loc
= e
.get_location ();
13624 tree elt_pushed_scope
;
13625 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13626 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13627 if (decl2
== error_mark_node
)
13628 decl
= error_mark_node
;
13629 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13631 /* Ensure we've diagnosed redeclaration if we aren't creating
13633 gcc_assert (errorcount
);
13634 decl
= error_mark_node
;
13638 if (elt_pushed_scope
)
13639 pop_scope (elt_pushed_scope
);
13644 error_at (loc
, "empty structured binding declaration");
13645 decl
= error_mark_node
;
13648 if (maybe_range_for_decl
== NULL
13649 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13651 bool non_constant_p
= false, is_direct_init
= false;
13652 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13653 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13655 if (initializer
== NULL_TREE
13656 || (TREE_CODE (initializer
) == TREE_LIST
13657 && TREE_CHAIN (initializer
))
13659 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13660 && CONSTRUCTOR_NELTS (initializer
) != 1))
13662 error_at (loc
, "invalid initializer for structured binding "
13664 initializer
= error_mark_node
;
13667 if (decl
!= error_mark_node
)
13669 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13670 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13671 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13672 cp_finish_decomp (decl
, prev
, v
.length ());
13675 else if (decl
!= error_mark_node
)
13677 *maybe_range_for_decl
= prev
;
13678 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13679 the underlying DECL. */
13680 cp_finish_decomp (decl
, prev
, v
.length ());
13684 pop_scope (pushed_scope
);
13686 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13688 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13689 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13695 /* Parse a decl-specifier-seq.
13697 decl-specifier-seq:
13698 decl-specifier-seq [opt] decl-specifier
13699 decl-specifier attribute-specifier-seq [opt] (C++11)
13702 storage-class-specifier
13713 Concepts Extension:
13718 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13720 The parser flags FLAGS is used to control type-specifier parsing.
13722 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13725 1: one of the decl-specifiers is an elaborated-type-specifier
13726 (i.e., a type declaration)
13727 2: one of the decl-specifiers is an enum-specifier or a
13728 class-specifier (i.e., a type definition)
13733 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13734 cp_parser_flags flags
,
13735 cp_decl_specifier_seq
*decl_specs
,
13736 int* declares_class_or_enum
)
13738 bool constructor_possible_p
= !parser
->in_declarator_p
;
13739 bool found_decl_spec
= false;
13740 cp_token
*start_token
= NULL
;
13743 /* Clear DECL_SPECS. */
13744 clear_decl_specs (decl_specs
);
13746 /* Assume no class or enumeration type is declared. */
13747 *declares_class_or_enum
= 0;
13749 /* Keep reading specifiers until there are no more to read. */
13752 bool constructor_p
;
13756 /* Peek at the next token. */
13757 token
= cp_lexer_peek_token (parser
->lexer
);
13759 /* Save the first token of the decl spec list for error
13762 start_token
= token
;
13763 /* Handle attributes. */
13764 if (cp_next_tokens_can_be_attribute_p (parser
))
13766 /* Parse the attributes. */
13767 tree attrs
= cp_parser_attributes_opt (parser
);
13769 /* In a sequence of declaration specifiers, c++11 attributes
13770 appertain to the type that precede them. In that case
13773 The attribute-specifier-seq affects the type only for
13774 the declaration it appears in, not other declarations
13775 involving the same type.
13777 But for now let's force the user to position the
13778 attribute either at the beginning of the declaration or
13779 after the declarator-id, which would clearly mean that it
13780 applies to the declarator. */
13781 if (cxx11_attribute_p (attrs
))
13783 if (!found_decl_spec
)
13784 /* The c++11 attribute is at the beginning of the
13785 declaration. It appertains to the entity being
13789 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13791 /* This is an attribute following a
13792 class-specifier. */
13793 if (decl_specs
->type_definition_p
)
13794 warn_misplaced_attr_for_class_type (token
->location
,
13800 decl_specs
->std_attributes
13801 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13802 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13803 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13809 decl_specs
->attributes
13810 = attr_chainon (decl_specs
->attributes
, attrs
);
13811 if (decl_specs
->locations
[ds_attribute
] == 0)
13812 decl_specs
->locations
[ds_attribute
] = token
->location
;
13815 /* Assume we will find a decl-specifier keyword. */
13816 found_decl_spec
= true;
13817 /* If the next token is an appropriate keyword, we can simply
13818 add it to the list. */
13819 switch (token
->keyword
)
13825 if (!at_class_scope_p ())
13827 gcc_rich_location
richloc (token
->location
);
13828 richloc
.add_fixit_remove ();
13829 error_at (&richloc
, "%<friend%> used outside of class");
13830 cp_lexer_purge_token (parser
->lexer
);
13835 /* Consume the token. */
13836 cp_lexer_consume_token (parser
->lexer
);
13840 case RID_CONSTEXPR
:
13842 cp_lexer_consume_token (parser
->lexer
);
13847 cp_lexer_consume_token (parser
->lexer
);
13850 /* function-specifier:
13857 cp_parser_function_specifier_opt (parser
, decl_specs
);
13864 /* Consume the token. */
13865 cp_lexer_consume_token (parser
->lexer
);
13866 /* A constructor declarator cannot appear in a typedef. */
13867 constructor_possible_p
= false;
13868 /* The "typedef" keyword can only occur in a declaration; we
13869 may as well commit at this point. */
13870 cp_parser_commit_to_tentative_parse (parser
);
13872 if (decl_specs
->storage_class
!= sc_none
)
13873 decl_specs
->conflicting_specifiers_p
= true;
13876 /* storage-class-specifier:
13886 if (cxx_dialect
== cxx98
)
13888 /* Consume the token. */
13889 cp_lexer_consume_token (parser
->lexer
);
13891 /* Complain about `auto' as a storage specifier, if
13892 we're complaining about C++0x compatibility. */
13893 gcc_rich_location
richloc (token
->location
);
13894 richloc
.add_fixit_remove ();
13895 warning_at (&richloc
, OPT_Wc__11_compat
,
13896 "%<auto%> changes meaning in C++11; "
13897 "please remove it");
13899 /* Set the storage class anyway. */
13900 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13904 /* C++0x auto type-specifier. */
13905 found_decl_spec
= false;
13912 /* Consume the token. */
13913 cp_lexer_consume_token (parser
->lexer
);
13914 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13918 /* Consume the token. */
13920 cp_lexer_consume_token (parser
->lexer
);
13924 /* We did not yet find a decl-specifier yet. */
13925 found_decl_spec
= false;
13929 if (found_decl_spec
13930 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13931 && token
->keyword
!= RID_CONSTEXPR
)
13932 error ("decl-specifier invalid in condition");
13934 if (found_decl_spec
13935 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13936 && token
->keyword
!= RID_MUTABLE
13937 && token
->keyword
!= RID_CONSTEXPR
)
13938 error_at (token
->location
, "%qD invalid in lambda",
13939 ridpointers
[token
->keyword
]);
13942 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13944 /* Constructors are a special case. The `S' in `S()' is not a
13945 decl-specifier; it is the beginning of the declarator. */
13947 = (!found_decl_spec
13948 && constructor_possible_p
13949 && (cp_parser_constructor_declarator_p
13950 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13952 /* If we don't have a DECL_SPEC yet, then we must be looking at
13953 a type-specifier. */
13954 if (!found_decl_spec
&& !constructor_p
)
13956 int decl_spec_declares_class_or_enum
;
13957 bool is_cv_qualifier
;
13961 = cp_parser_type_specifier (parser
, flags
,
13963 /*is_declaration=*/true,
13964 &decl_spec_declares_class_or_enum
,
13966 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13968 /* If this type-specifier referenced a user-defined type
13969 (a typedef, class-name, etc.), then we can't allow any
13970 more such type-specifiers henceforth.
13974 The longest sequence of decl-specifiers that could
13975 possibly be a type name is taken as the
13976 decl-specifier-seq of a declaration. The sequence shall
13977 be self-consistent as described below.
13981 As a general rule, at most one type-specifier is allowed
13982 in the complete decl-specifier-seq of a declaration. The
13983 only exceptions are the following:
13985 -- const or volatile can be combined with any other
13988 -- signed or unsigned can be combined with char, long,
13996 void g (const int Pc);
13998 Here, Pc is *not* part of the decl-specifier seq; it's
13999 the declarator. Therefore, once we see a type-specifier
14000 (other than a cv-qualifier), we forbid any additional
14001 user-defined types. We *do* still allow things like `int
14002 int' to be considered a decl-specifier-seq, and issue the
14003 error message later. */
14004 if (type_spec
&& !is_cv_qualifier
)
14005 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14006 /* A constructor declarator cannot follow a type-specifier. */
14009 constructor_possible_p
= false;
14010 found_decl_spec
= true;
14011 if (!is_cv_qualifier
)
14012 decl_specs
->any_type_specifiers_p
= true;
14014 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14015 error_at (token
->location
, "type-specifier invalid in lambda");
14019 /* If we still do not have a DECL_SPEC, then there are no more
14020 decl-specifiers. */
14021 if (!found_decl_spec
)
14024 decl_specs
->any_specifiers_p
= true;
14025 /* After we see one decl-specifier, further decl-specifiers are
14026 always optional. */
14027 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14030 /* Don't allow a friend specifier with a class definition. */
14031 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14032 && (*declares_class_or_enum
& 2))
14033 error_at (decl_specs
->locations
[ds_friend
],
14034 "class definition may not be declared a friend");
14037 /* Parse an (optional) storage-class-specifier.
14039 storage-class-specifier:
14048 storage-class-specifier:
14051 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14054 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14056 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14059 if (cxx_dialect
!= cxx98
)
14061 /* Fall through for C++98. */
14062 gcc_fallthrough ();
14069 /* Consume the token. */
14070 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14077 /* Parse an (optional) function-specifier.
14079 function-specifier:
14085 explicit(constant-expression)
14087 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14088 Updates DECL_SPECS, if it is non-NULL. */
14091 cp_parser_function_specifier_opt (cp_parser
* parser
,
14092 cp_decl_specifier_seq
*decl_specs
)
14094 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14095 switch (token
->keyword
)
14098 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14102 /* 14.5.2.3 [temp.mem]
14104 A member function template shall not be virtual. */
14105 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14106 && current_class_type
)
14107 error_at (token
->location
, "templates may not be %<virtual%>");
14109 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14114 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14115 /* If we see '(', it's C++20 explicit(bool). */
14117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14119 matching_parens parens
;
14120 parens
.consume_open (parser
);
14122 /* New types are not allowed in an explicit-specifier. */
14123 const char *saved_message
14124 = parser
->type_definition_forbidden_message
;
14125 parser
->type_definition_forbidden_message
14126 = G_("types may not be defined in explicit-specifier");
14128 if (cxx_dialect
< cxx2a
)
14129 pedwarn (token
->location
, 0,
14130 "%<explicit(bool)%> only available with -std=c++2a "
14131 "or -std=gnu++2a");
14133 /* Parse the constant-expression. */
14134 expr
= cp_parser_constant_expression (parser
);
14136 /* Restore the saved message. */
14137 parser
->type_definition_forbidden_message
= saved_message
;
14138 parens
.require_close (parser
);
14141 /* The explicit-specifier explicit without a constant-expression is
14142 equivalent to the explicit-specifier explicit(true). */
14143 expr
= boolean_true_node
;
14146 "the constant-expression, if supplied, shall be a contextually
14147 converted constant expression of type bool." */
14148 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14149 /* We could evaluate it -- mark the decl as appropriate. */
14150 if (expr
== boolean_true_node
)
14151 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14152 else if (expr
== boolean_false_node
)
14153 /* Don't mark the decl as explicit. */;
14154 else if (decl_specs
)
14155 /* The expression was value-dependent. Remember it so that we can
14156 substitute it later. */
14157 decl_specs
->explicit_specifier
= expr
;
14165 /* Consume the token. */
14166 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14169 /* Parse a linkage-specification.
14171 linkage-specification:
14172 extern string-literal { declaration-seq [opt] }
14173 extern string-literal declaration */
14176 cp_parser_linkage_specification (cp_parser
* parser
)
14180 /* Look for the `extern' keyword. */
14181 cp_token
*extern_token
14182 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14184 /* Look for the string-literal. */
14185 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14186 linkage
= cp_parser_string_literal (parser
, false, false);
14188 /* Transform the literal into an identifier. If the literal is a
14189 wide-character string, or contains embedded NULs, then we can't
14190 handle it as the user wants. */
14191 if (strlen (TREE_STRING_POINTER (linkage
))
14192 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14194 cp_parser_error (parser
, "invalid linkage-specification");
14195 /* Assume C++ linkage. */
14196 linkage
= lang_name_cplusplus
;
14199 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14201 /* We're now using the new linkage. */
14202 push_lang_context (linkage
);
14204 /* Preserve the location of the the innermost linkage specification,
14205 tracking the locations of nested specifications via a local. */
14206 location_t saved_location
14207 = parser
->innermost_linkage_specification_location
;
14208 /* Construct a location ranging from the start of the "extern" to
14209 the end of the string-literal, with the caret at the start, e.g.:
14213 parser
->innermost_linkage_specification_location
14214 = make_location (extern_token
->location
,
14215 extern_token
->location
,
14216 get_finish (string_token
->location
));
14218 /* If the next token is a `{', then we're using the first
14220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14222 cp_ensure_no_omp_declare_simd (parser
);
14223 cp_ensure_no_oacc_routine (parser
);
14225 /* Consume the `{' token. */
14226 matching_braces braces
;
14227 braces
.consume_open (parser
);
14228 /* Parse the declarations. */
14229 cp_parser_declaration_seq_opt (parser
);
14230 /* Look for the closing `}'. */
14231 braces
.require_close (parser
);
14233 /* Otherwise, there's just one declaration. */
14236 bool saved_in_unbraced_linkage_specification_p
;
14238 saved_in_unbraced_linkage_specification_p
14239 = parser
->in_unbraced_linkage_specification_p
;
14240 parser
->in_unbraced_linkage_specification_p
= true;
14241 cp_parser_declaration (parser
);
14242 parser
->in_unbraced_linkage_specification_p
14243 = saved_in_unbraced_linkage_specification_p
;
14246 /* We're done with the linkage-specification. */
14247 pop_lang_context ();
14249 /* Restore location of parent linkage specification, if any. */
14250 parser
->innermost_linkage_specification_location
= saved_location
;
14253 /* Parse a static_assert-declaration.
14255 static_assert-declaration:
14256 static_assert ( constant-expression , string-literal ) ;
14257 static_assert ( constant-expression ) ; (C++17)
14259 If MEMBER_P, this static_assert is a class member. */
14262 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14265 location_t token_loc
;
14269 /* Peek at the `static_assert' token so we can keep track of exactly
14270 where the static assertion started. */
14271 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14273 /* Look for the `static_assert' keyword. */
14274 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14278 /* We know we are in a static assertion; commit to any tentative
14280 if (cp_parser_parsing_tentatively (parser
))
14281 cp_parser_commit_to_tentative_parse (parser
);
14283 /* Parse the `(' starting the static assertion condition. */
14284 matching_parens parens
;
14285 parens
.require_open (parser
);
14287 /* Parse the constant-expression. Allow a non-constant expression
14288 here in order to give better diagnostics in finish_static_assert. */
14290 cp_parser_constant_expression (parser
,
14291 /*allow_non_constant_p=*/true,
14292 /*non_constant_p=*/&dummy
);
14294 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14296 if (cxx_dialect
< cxx17
)
14297 pedwarn (input_location
, OPT_Wpedantic
,
14298 "static_assert without a message "
14299 "only available with -std=c++17 or -std=gnu++17");
14301 cp_lexer_consume_token (parser
->lexer
);
14302 message
= build_string (1, "");
14303 TREE_TYPE (message
) = char_array_type_node
;
14304 fix_string_type (message
);
14308 /* Parse the separating `,'. */
14309 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14311 /* Parse the string-literal message. */
14312 message
= cp_parser_string_literal (parser
,
14313 /*translate=*/false,
14316 /* A `)' completes the static assertion. */
14317 if (!parens
.require_close (parser
))
14318 cp_parser_skip_to_closing_parenthesis (parser
,
14319 /*recovering=*/true,
14320 /*or_comma=*/false,
14321 /*consume_paren=*/true);
14324 /* A semicolon terminates the declaration. */
14325 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14327 /* Get the location for the static assertion. Use that of the
14328 condition if available, otherwise, use that of the "static_assert"
14330 location_t assert_loc
= condition
.get_location ();
14331 if (assert_loc
== UNKNOWN_LOCATION
)
14332 assert_loc
= token_loc
;
14334 /* Complete the static assertion, which may mean either processing
14335 the static assert now or saving it for template instantiation. */
14336 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14339 /* Parse the expression in decltype ( expression ). */
14342 cp_parser_decltype_expr (cp_parser
*parser
,
14343 bool &id_expression_or_member_access_p
)
14345 cp_token
*id_expr_start_token
;
14348 /* Since we're going to preserve any side-effects from this parse, set up a
14349 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14350 in the expression. */
14351 tentative_firewall
firewall (parser
);
14353 /* First, try parsing an id-expression. */
14354 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14355 cp_parser_parse_tentatively (parser
);
14356 expr
= cp_parser_id_expression (parser
,
14357 /*template_keyword_p=*/false,
14358 /*check_dependency_p=*/true,
14359 /*template_p=*/NULL
,
14360 /*declarator_p=*/false,
14361 /*optional_p=*/false);
14363 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14365 bool non_integral_constant_expression_p
= false;
14366 tree id_expression
= expr
;
14368 const char *error_msg
;
14370 if (identifier_p (expr
))
14371 /* Lookup the name we got back from the id-expression. */
14372 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14373 id_expr_start_token
->location
);
14375 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14376 /* A template without args is not a complete id-expression. */
14377 expr
= error_mark_node
;
14380 && expr
!= error_mark_node
14381 && TREE_CODE (expr
) != TYPE_DECL
14382 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14383 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14384 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14386 /* Complete lookup of the id-expression. */
14387 expr
= (finish_id_expression
14388 (id_expression
, expr
, parser
->scope
, &idk
,
14389 /*integral_constant_expression_p=*/false,
14390 /*allow_non_integral_constant_expression_p=*/true,
14391 &non_integral_constant_expression_p
,
14392 /*template_p=*/false,
14394 /*address_p=*/false,
14395 /*template_arg_p=*/false,
14397 id_expr_start_token
->location
));
14399 if (expr
== error_mark_node
)
14400 /* We found an id-expression, but it was something that we
14401 should not have found. This is an error, not something
14402 we can recover from, so note that we found an
14403 id-expression and we'll recover as gracefully as
14405 id_expression_or_member_access_p
= true;
14409 && expr
!= error_mark_node
14410 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14411 /* We have an id-expression. */
14412 id_expression_or_member_access_p
= true;
14415 if (!id_expression_or_member_access_p
)
14417 /* Abort the id-expression parse. */
14418 cp_parser_abort_tentative_parse (parser
);
14420 /* Parsing tentatively, again. */
14421 cp_parser_parse_tentatively (parser
);
14423 /* Parse a class member access. */
14424 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14425 /*cast_p=*/false, /*decltype*/true,
14426 /*member_access_only_p=*/true, NULL
);
14429 && expr
!= error_mark_node
14430 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14431 /* We have an id-expression. */
14432 id_expression_or_member_access_p
= true;
14435 if (id_expression_or_member_access_p
)
14436 /* We have parsed the complete id-expression or member access. */
14437 cp_parser_parse_definitely (parser
);
14440 /* Abort our attempt to parse an id-expression or member access
14442 cp_parser_abort_tentative_parse (parser
);
14444 /* Commit to the tentative_firewall so we get syntax errors. */
14445 cp_parser_commit_to_tentative_parse (parser
);
14447 /* Parse a full expression. */
14448 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14449 /*decltype_p=*/true);
14455 /* Parse a `decltype' type. Returns the type.
14457 simple-type-specifier:
14458 decltype ( expression )
14460 decltype ( auto ) */
14463 cp_parser_decltype (cp_parser
*parser
)
14465 bool id_expression_or_member_access_p
= false;
14466 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14468 if (start_token
->type
== CPP_DECLTYPE
)
14470 /* Already parsed. */
14471 cp_lexer_consume_token (parser
->lexer
);
14472 return saved_checks_value (start_token
->u
.tree_check_value
);
14475 /* Look for the `decltype' token. */
14476 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14477 return error_mark_node
;
14479 /* Parse the opening `('. */
14480 matching_parens parens
;
14481 if (!parens
.require_open (parser
))
14482 return error_mark_node
;
14484 push_deferring_access_checks (dk_deferred
);
14486 tree expr
= NULL_TREE
;
14488 if (cxx_dialect
>= cxx14
14489 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14490 /* decltype (auto) */
14491 cp_lexer_consume_token (parser
->lexer
);
14494 /* decltype (expression) */
14496 /* Types cannot be defined in a `decltype' expression. Save away the
14497 old message and set the new one. */
14498 const char *saved_message
= parser
->type_definition_forbidden_message
;
14499 parser
->type_definition_forbidden_message
14500 = G_("types may not be defined in %<decltype%> expressions");
14502 /* The restrictions on constant-expressions do not apply inside
14503 decltype expressions. */
14504 bool saved_integral_constant_expression_p
14505 = parser
->integral_constant_expression_p
;
14506 bool saved_non_integral_constant_expression_p
14507 = parser
->non_integral_constant_expression_p
;
14508 parser
->integral_constant_expression_p
= false;
14510 /* Within a parenthesized expression, a `>' token is always
14511 the greater-than operator. */
14512 bool saved_greater_than_is_operator_p
14513 = parser
->greater_than_is_operator_p
;
14514 parser
->greater_than_is_operator_p
= true;
14516 /* Do not actually evaluate the expression. */
14517 ++cp_unevaluated_operand
;
14519 /* Do not warn about problems with the expression. */
14520 ++c_inhibit_evaluation_warnings
;
14522 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14524 /* Go back to evaluating expressions. */
14525 --cp_unevaluated_operand
;
14526 --c_inhibit_evaluation_warnings
;
14528 /* The `>' token might be the end of a template-id or
14529 template-parameter-list now. */
14530 parser
->greater_than_is_operator_p
14531 = saved_greater_than_is_operator_p
;
14533 /* Restore the old message and the integral constant expression
14535 parser
->type_definition_forbidden_message
= saved_message
;
14536 parser
->integral_constant_expression_p
14537 = saved_integral_constant_expression_p
;
14538 parser
->non_integral_constant_expression_p
14539 = saved_non_integral_constant_expression_p
;
14542 /* Parse to the closing `)'. */
14543 if (!parens
.require_close (parser
))
14545 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14546 /*consume_paren=*/true);
14547 pop_deferring_access_checks ();
14548 return error_mark_node
;
14554 expr
= make_decltype_auto ();
14555 AUTO_IS_DECLTYPE (expr
) = true;
14558 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14559 tf_warning_or_error
);
14561 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14563 start_token
->type
= CPP_DECLTYPE
;
14564 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14565 start_token
->u
.tree_check_value
->value
= expr
;
14566 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14567 start_token
->keyword
= RID_MAX
;
14568 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14570 pop_to_parent_deferring_access_checks ();
14575 /* Special member functions [gram.special] */
14577 /* Parse a conversion-function-id.
14579 conversion-function-id:
14580 operator conversion-type-id
14582 Returns an IDENTIFIER_NODE representing the operator. */
14585 cp_parser_conversion_function_id (cp_parser
* parser
)
14589 tree saved_qualifying_scope
;
14590 tree saved_object_scope
;
14591 tree pushed_scope
= NULL_TREE
;
14593 /* Look for the `operator' token. */
14594 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14595 return error_mark_node
;
14596 /* When we parse the conversion-type-id, the current scope will be
14597 reset. However, we need that information in able to look up the
14598 conversion function later, so we save it here. */
14599 saved_scope
= parser
->scope
;
14600 saved_qualifying_scope
= parser
->qualifying_scope
;
14601 saved_object_scope
= parser
->object_scope
;
14602 /* We must enter the scope of the class so that the names of
14603 entities declared within the class are available in the
14604 conversion-type-id. For example, consider:
14611 S::operator I() { ... }
14613 In order to see that `I' is a type-name in the definition, we
14614 must be in the scope of `S'. */
14616 pushed_scope
= push_scope (saved_scope
);
14617 /* Parse the conversion-type-id. */
14618 type
= cp_parser_conversion_type_id (parser
);
14619 /* Leave the scope of the class, if any. */
14621 pop_scope (pushed_scope
);
14622 /* Restore the saved scope. */
14623 parser
->scope
= saved_scope
;
14624 parser
->qualifying_scope
= saved_qualifying_scope
;
14625 parser
->object_scope
= saved_object_scope
;
14626 /* If the TYPE is invalid, indicate failure. */
14627 if (type
== error_mark_node
)
14628 return error_mark_node
;
14629 return make_conv_op_name (type
);
14632 /* Parse a conversion-type-id:
14634 conversion-type-id:
14635 type-specifier-seq conversion-declarator [opt]
14637 Returns the TYPE specified. */
14640 cp_parser_conversion_type_id (cp_parser
* parser
)
14643 cp_decl_specifier_seq type_specifiers
;
14644 cp_declarator
*declarator
;
14645 tree type_specified
;
14646 const char *saved_message
;
14648 /* Parse the attributes. */
14649 attributes
= cp_parser_attributes_opt (parser
);
14651 saved_message
= parser
->type_definition_forbidden_message
;
14652 parser
->type_definition_forbidden_message
14653 = G_("types may not be defined in a conversion-type-id");
14655 /* Parse the type-specifiers. */
14656 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14657 /*is_trailing_return=*/false,
14660 parser
->type_definition_forbidden_message
= saved_message
;
14662 /* If that didn't work, stop. */
14663 if (type_specifiers
.type
== error_mark_node
)
14664 return error_mark_node
;
14665 /* Parse the conversion-declarator. */
14666 declarator
= cp_parser_conversion_declarator_opt (parser
);
14668 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14669 /*initialized=*/0, &attributes
);
14671 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14673 /* Don't give this error when parsing tentatively. This happens to
14674 work because we always parse this definitively once. */
14675 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14676 && type_uses_auto (type_specified
))
14678 if (cxx_dialect
< cxx14
)
14680 error ("invalid use of %<auto%> in conversion operator");
14681 return error_mark_node
;
14683 else if (template_parm_scope_p ())
14684 warning (0, "use of %<auto%> in member template "
14685 "conversion operator can never be deduced");
14688 return type_specified
;
14691 /* Parse an (optional) conversion-declarator.
14693 conversion-declarator:
14694 ptr-operator conversion-declarator [opt]
14698 static cp_declarator
*
14699 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14701 enum tree_code code
;
14702 tree class_type
, std_attributes
= NULL_TREE
;
14703 cp_cv_quals cv_quals
;
14705 /* We don't know if there's a ptr-operator next, or not. */
14706 cp_parser_parse_tentatively (parser
);
14707 /* Try the ptr-operator. */
14708 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14710 /* If it worked, look for more conversion-declarators. */
14711 if (cp_parser_parse_definitely (parser
))
14713 cp_declarator
*declarator
;
14715 /* Parse another optional declarator. */
14716 declarator
= cp_parser_conversion_declarator_opt (parser
);
14718 declarator
= cp_parser_make_indirect_declarator
14719 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14727 /* Parse an (optional) ctor-initializer.
14730 : mem-initializer-list */
14733 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14735 /* If the next token is not a `:', then there is no
14736 ctor-initializer. */
14737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14739 /* Do default initialization of any bases and members. */
14740 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14741 finish_mem_initializers (NULL_TREE
);
14745 /* Consume the `:' token. */
14746 cp_lexer_consume_token (parser
->lexer
);
14747 /* And the mem-initializer-list. */
14748 cp_parser_mem_initializer_list (parser
);
14751 /* Parse a mem-initializer-list.
14753 mem-initializer-list:
14754 mem-initializer ... [opt]
14755 mem-initializer ... [opt] , mem-initializer-list */
14758 cp_parser_mem_initializer_list (cp_parser
* parser
)
14760 tree mem_initializer_list
= NULL_TREE
;
14761 tree target_ctor
= error_mark_node
;
14762 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14764 /* Let the semantic analysis code know that we are starting the
14765 mem-initializer-list. */
14766 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14767 error_at (token
->location
,
14768 "only constructors take member initializers");
14770 /* Loop through the list. */
14773 tree mem_initializer
;
14775 token
= cp_lexer_peek_token (parser
->lexer
);
14776 /* Parse the mem-initializer. */
14777 mem_initializer
= cp_parser_mem_initializer (parser
);
14778 /* If the next token is a `...', we're expanding member initializers. */
14779 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14781 || (mem_initializer
!= error_mark_node
14782 && check_for_bare_parameter_packs (TREE_PURPOSE
14783 (mem_initializer
))))
14785 /* Consume the `...'. */
14787 cp_lexer_consume_token (parser
->lexer
);
14789 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14790 can be expanded but members cannot. */
14791 if (mem_initializer
!= error_mark_node
14792 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14794 error_at (token
->location
,
14795 "cannot expand initializer for member %qD",
14796 TREE_PURPOSE (mem_initializer
));
14797 mem_initializer
= error_mark_node
;
14800 /* Construct the pack expansion type. */
14801 if (mem_initializer
!= error_mark_node
)
14802 mem_initializer
= make_pack_expansion (mem_initializer
);
14804 if (target_ctor
!= error_mark_node
14805 && mem_initializer
!= error_mark_node
)
14807 error ("mem-initializer for %qD follows constructor delegation",
14808 TREE_PURPOSE (mem_initializer
));
14809 mem_initializer
= error_mark_node
;
14811 /* Look for a target constructor. */
14812 if (mem_initializer
!= error_mark_node
14813 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14814 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14816 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14817 if (mem_initializer_list
)
14819 error ("constructor delegation follows mem-initializer for %qD",
14820 TREE_PURPOSE (mem_initializer_list
));
14821 mem_initializer
= error_mark_node
;
14823 target_ctor
= mem_initializer
;
14825 /* Add it to the list, unless it was erroneous. */
14826 if (mem_initializer
!= error_mark_node
)
14828 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14829 mem_initializer_list
= mem_initializer
;
14831 /* If the next token is not a `,', we're done. */
14832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14834 /* Consume the `,' token. */
14835 cp_lexer_consume_token (parser
->lexer
);
14838 /* Perform semantic analysis. */
14839 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14840 finish_mem_initializers (mem_initializer_list
);
14843 /* Parse a mem-initializer.
14846 mem-initializer-id ( expression-list [opt] )
14847 mem-initializer-id braced-init-list
14852 ( expression-list [opt] )
14854 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14855 class) or FIELD_DECL (for a non-static data member) to initialize;
14856 the TREE_VALUE is the expression-list. An empty initialization
14857 list is represented by void_list_node. */
14860 cp_parser_mem_initializer (cp_parser
* parser
)
14862 tree mem_initializer_id
;
14863 tree expression_list
;
14865 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14867 /* Find out what is being initialized. */
14868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14870 permerror (token
->location
,
14871 "anachronistic old-style base class initializer");
14872 mem_initializer_id
= NULL_TREE
;
14876 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14877 if (mem_initializer_id
== error_mark_node
)
14878 return mem_initializer_id
;
14880 member
= expand_member_init (mem_initializer_id
);
14881 if (member
&& !DECL_P (member
))
14882 in_base_initializer
= 1;
14884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14886 bool expr_non_constant_p
;
14887 cp_lexer_set_source_position (parser
->lexer
);
14888 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14889 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14890 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14891 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14895 vec
<tree
, va_gc
> *vec
;
14896 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14898 /*allow_expansion_p=*/true,
14899 /*non_constant_p=*/NULL
);
14901 return error_mark_node
;
14902 expression_list
= build_tree_list_vec (vec
);
14903 release_tree_vector (vec
);
14906 if (expression_list
== error_mark_node
)
14907 return error_mark_node
;
14908 if (!expression_list
)
14909 expression_list
= void_type_node
;
14911 in_base_initializer
= 0;
14913 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14916 /* Parse a mem-initializer-id.
14918 mem-initializer-id:
14919 :: [opt] nested-name-specifier [opt] class-name
14920 decltype-specifier (C++11)
14923 Returns a TYPE indicating the class to be initialized for the first
14924 production (and the second in C++11). Returns an IDENTIFIER_NODE
14925 indicating the data member to be initialized for the last production. */
14928 cp_parser_mem_initializer_id (cp_parser
* parser
)
14930 bool global_scope_p
;
14931 bool nested_name_specifier_p
;
14932 bool template_p
= false;
14935 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14937 /* `typename' is not allowed in this context ([temp.res]). */
14938 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14940 error_at (token
->location
,
14941 "keyword %<typename%> not allowed in this context (a qualified "
14942 "member initializer is implicitly a type)");
14943 cp_lexer_consume_token (parser
->lexer
);
14945 /* Look for the optional `::' operator. */
14947 = (cp_parser_global_scope_opt (parser
,
14948 /*current_scope_valid_p=*/false)
14950 /* Look for the optional nested-name-specifier. The simplest way to
14955 The keyword `typename' is not permitted in a base-specifier or
14956 mem-initializer; in these contexts a qualified name that
14957 depends on a template-parameter is implicitly assumed to be a
14960 is to assume that we have seen the `typename' keyword at this
14962 nested_name_specifier_p
14963 = (cp_parser_nested_name_specifier_opt (parser
,
14964 /*typename_keyword_p=*/true,
14965 /*check_dependency_p=*/true,
14967 /*is_declaration=*/true)
14969 if (nested_name_specifier_p
)
14970 template_p
= cp_parser_optional_template_keyword (parser
);
14971 /* If there is a `::' operator or a nested-name-specifier, then we
14972 are definitely looking for a class-name. */
14973 if (global_scope_p
|| nested_name_specifier_p
)
14974 return cp_parser_class_name (parser
,
14975 /*typename_keyword_p=*/true,
14976 /*template_keyword_p=*/template_p
,
14978 /*check_dependency_p=*/true,
14979 /*class_head_p=*/false,
14980 /*is_declaration=*/true);
14981 /* Otherwise, we could also be looking for an ordinary identifier. */
14982 cp_parser_parse_tentatively (parser
);
14983 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14984 /* Try a decltype-specifier. */
14985 id
= cp_parser_decltype (parser
);
14987 /* Otherwise, try a class-name. */
14988 id
= cp_parser_class_name (parser
,
14989 /*typename_keyword_p=*/true,
14990 /*template_keyword_p=*/false,
14992 /*check_dependency_p=*/true,
14993 /*class_head_p=*/false,
14994 /*is_declaration=*/true);
14995 /* If we found one, we're done. */
14996 if (cp_parser_parse_definitely (parser
))
14998 /* Otherwise, look for an ordinary identifier. */
14999 return cp_parser_identifier (parser
);
15002 /* Overloading [gram.over] */
15004 /* Parse an operator-function-id.
15006 operator-function-id:
15009 Returns an IDENTIFIER_NODE for the operator which is a
15010 human-readable spelling of the identifier, e.g., `operator +'. */
15013 cp_parser_operator_function_id (cp_parser
* parser
)
15015 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15016 /* Look for the `operator' keyword. */
15017 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15018 return error_mark_node
;
15019 /* And then the name of the operator itself. */
15020 return cp_parser_operator (parser
, start_loc
);
15023 /* Return an identifier node for a user-defined literal operator.
15024 The suffix identifier is chained to the operator name identifier. */
15027 cp_literal_operator_id (const char* name
)
15030 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15031 + strlen (name
) + 10);
15032 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15033 identifier
= get_identifier (buffer
);
15038 /* Parse an operator.
15041 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15042 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15043 || ++ -- , ->* -> () []
15050 Returns an IDENTIFIER_NODE for the operator which is a
15051 human-readable spelling of the identifier, e.g., `operator +'. */
15054 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15056 tree id
= NULL_TREE
;
15060 /* Peek at the next token. */
15061 token
= cp_lexer_peek_token (parser
->lexer
);
15063 location_t end_loc
= token
->location
;
15065 /* Figure out which operator we have. */
15066 enum tree_code op
= ERROR_MARK
;
15067 bool assop
= false;
15068 bool consumed
= false;
15069 switch (token
->type
)
15073 /* The keyword should be either `new' or `delete'. */
15074 if (token
->keyword
== RID_NEW
)
15076 else if (token
->keyword
== RID_DELETE
)
15081 /* Consume the `new' or `delete' token. */
15082 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15084 /* Peek at the next token. */
15085 token
= cp_lexer_peek_token (parser
->lexer
);
15086 /* If it's a `[' token then this is the array variant of the
15088 if (token
->type
== CPP_OPEN_SQUARE
)
15090 /* Consume the `[' token. */
15091 cp_lexer_consume_token (parser
->lexer
);
15092 /* Look for the `]' token. */
15093 if (cp_token
*close_token
15094 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15095 end_loc
= close_token
->location
;
15096 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15115 op
= TRUNC_DIV_EXPR
;
15119 op
= TRUNC_MOD_EXPR
;
15139 op
= TRUTH_NOT_EXPR
;
15172 op
= TRUNC_DIV_EXPR
;
15177 op
= TRUNC_MOD_EXPR
;
15203 case CPP_LSHIFT_EQ
:
15208 case CPP_RSHIFT_EQ
:
15225 case CPP_GREATER_EQ
:
15230 op
= TRUTH_ANDIF_EXPR
;
15234 op
= TRUTH_ORIF_EXPR
;
15237 case CPP_PLUS_PLUS
:
15238 op
= POSTINCREMENT_EXPR
;
15241 case CPP_MINUS_MINUS
:
15242 op
= PREDECREMENT_EXPR
;
15246 op
= COMPOUND_EXPR
;
15249 case CPP_DEREF_STAR
:
15254 op
= COMPONENT_REF
;
15257 case CPP_OPEN_PAREN
:
15259 /* Consume the `('. */
15260 matching_parens parens
;
15261 parens
.consume_open (parser
);
15262 /* Look for the matching `)'. */
15263 token
= parens
.require_close (parser
);
15265 end_loc
= token
->location
;
15271 case CPP_OPEN_SQUARE
:
15272 /* Consume the `['. */
15273 cp_lexer_consume_token (parser
->lexer
);
15274 /* Look for the matching `]'. */
15275 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15277 end_loc
= token
->location
;
15282 case CPP_UTF8STRING
:
15283 case CPP_UTF8STRING_USERDEF
:
15290 case CPP_STRING_USERDEF
:
15291 case CPP_WSTRING_USERDEF
:
15292 case CPP_STRING16_USERDEF
:
15293 case CPP_STRING32_USERDEF
:
15299 if (cxx_dialect
== cxx98
)
15300 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15302 /* Consume the string. */
15303 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15304 /*wide_ok=*/true, /*lookup_udlit=*/false);
15305 if (str
== error_mark_node
)
15306 return error_mark_node
;
15307 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15309 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15310 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15311 end_loc
= str
.get_location ();
15316 /* Look for the suffix identifier. */
15317 token
= cp_lexer_peek_token (parser
->lexer
);
15318 if (token
->type
== CPP_NAME
)
15320 id
= cp_parser_identifier (parser
);
15321 end_loc
= token
->location
;
15323 else if (token
->type
== CPP_KEYWORD
)
15325 error ("unexpected keyword;"
15326 " remove space between quotes and suffix identifier");
15327 return error_mark_node
;
15331 error ("expected suffix identifier");
15332 return error_mark_node
;
15335 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15336 (TREE_TYPE (TREE_TYPE (string_tree
))));
15337 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15340 error ("expected empty string after %<operator%> keyword");
15341 return error_mark_node
;
15343 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15346 error ("invalid encoding prefix in literal operator");
15347 return error_mark_node
;
15349 if (id
!= error_mark_node
)
15351 const char *name
= IDENTIFIER_POINTER (id
);
15352 id
= cp_literal_operator_id (name
);
15354 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15355 return cp_expr (id
, start_loc
);
15359 /* Anything else is an error. */
15363 /* If we have selected an identifier, we need to consume the
15365 if (op
!= ERROR_MARK
)
15367 id
= ovl_op_identifier (assop
, op
);
15369 cp_lexer_consume_token (parser
->lexer
);
15371 /* Otherwise, no valid operator name was present. */
15374 cp_parser_error (parser
, "expected operator");
15375 id
= error_mark_node
;
15378 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15379 return cp_expr (id
, start_loc
);
15382 /* Parse a template-declaration.
15384 template-declaration:
15385 export [opt] template < template-parameter-list > declaration
15387 If MEMBER_P is TRUE, this template-declaration occurs within a
15390 The grammar rule given by the standard isn't correct. What
15391 is really meant is:
15393 template-declaration:
15394 export [opt] template-parameter-list-seq
15395 decl-specifier-seq [opt] init-declarator [opt] ;
15396 export [opt] template-parameter-list-seq
15397 function-definition
15399 template-parameter-list-seq:
15400 template-parameter-list-seq [opt]
15401 template < template-parameter-list >
15403 Concept Extensions:
15405 template-parameter-list-seq:
15406 template < template-parameter-list > requires-clause [opt]
15409 requires logical-or-expression */
15412 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15414 /* Check for `export'. */
15415 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15417 /* Consume the `export' token. */
15418 cp_lexer_consume_token (parser
->lexer
);
15419 /* Warn that we do not support `export'. */
15420 warning (0, "keyword %<export%> not implemented, and will be ignored");
15423 cp_parser_template_declaration_after_export (parser
, member_p
);
15426 /* Parse a template-parameter-list.
15428 template-parameter-list:
15430 template-parameter-list , template-parameter
15432 Returns a TREE_LIST. Each node represents a template parameter.
15433 The nodes are connected via their TREE_CHAINs. */
15436 cp_parser_template_parameter_list (cp_parser
* parser
)
15438 tree parameter_list
= NULL_TREE
;
15440 begin_template_parm_list ();
15442 /* The loop below parses the template parms. We first need to know
15443 the total number of template parms to be able to compute proper
15444 canonical types of each dependent type. So after the loop, when
15445 we know the total number of template parms,
15446 end_template_parm_list computes the proper canonical types and
15447 fixes up the dependent types accordingly. */
15452 bool is_parameter_pack
;
15453 location_t parm_loc
;
15455 /* Parse the template-parameter. */
15456 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15457 parameter
= cp_parser_template_parameter (parser
,
15459 &is_parameter_pack
);
15460 /* Add it to the list. */
15461 if (parameter
!= error_mark_node
)
15462 parameter_list
= process_template_parm (parameter_list
,
15466 is_parameter_pack
);
15469 tree err_parm
= build_tree_list (parameter
, parameter
);
15470 parameter_list
= chainon (parameter_list
, err_parm
);
15473 /* If the next token is not a `,', we're done. */
15474 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15476 /* Otherwise, consume the `,' token. */
15477 cp_lexer_consume_token (parser
->lexer
);
15480 return end_template_parm_list (parameter_list
);
15483 /* Parse a introduction-list.
15486 introduced-parameter
15487 introduction-list , introduced-parameter
15489 introduced-parameter:
15490 ...[opt] identifier
15492 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15493 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15494 WILDCARD_DECL will also have DECL_NAME set and token location in
15495 DECL_SOURCE_LOCATION. */
15498 cp_parser_introduction_list (cp_parser
*parser
)
15500 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15504 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15506 cp_lexer_consume_token (parser
->lexer
);
15508 tree identifier
= cp_parser_identifier (parser
);
15509 if (identifier
== error_mark_node
)
15512 /* Build placeholder. */
15513 tree parm
= build_nt (WILDCARD_DECL
);
15514 DECL_SOURCE_LOCATION (parm
)
15515 = cp_lexer_peek_token (parser
->lexer
)->location
;
15516 DECL_NAME (parm
) = identifier
;
15517 WILDCARD_PACK_P (parm
) = is_pack
;
15518 vec_safe_push (introduction_vec
, parm
);
15520 /* If the next token is not a `,', we're done. */
15521 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15523 /* Otherwise, consume the `,' token. */
15524 cp_lexer_consume_token (parser
->lexer
);
15527 /* Convert the vec into a TREE_VEC. */
15528 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15531 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15532 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15534 release_tree_vector (introduction_vec
);
15535 return introduction_list
;
15538 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15539 is an abstract declarator. */
15541 static inline cp_declarator
*
15542 get_id_declarator (cp_declarator
*declarator
)
15544 cp_declarator
*d
= declarator
;
15545 while (d
&& d
->kind
!= cdk_id
)
15550 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15551 is an abstract declarator. */
15554 get_unqualified_id (cp_declarator
*declarator
)
15556 declarator
= get_id_declarator (declarator
);
15558 return declarator
->u
.id
.unqualified_name
;
15563 /* Returns true if DECL represents a constrained-parameter. */
15566 is_constrained_parameter (tree decl
)
15569 && TREE_CODE (decl
) == TYPE_DECL
15570 && CONSTRAINED_PARM_CONCEPT (decl
)
15571 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15574 /* Returns true if PARM declares a constrained-parameter. */
15577 is_constrained_parameter (cp_parameter_declarator
*parm
)
15579 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15582 /* Check that the type parameter is only a declarator-id, and that its
15583 type is not cv-qualified. */
15586 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15587 cp_parameter_declarator
*parm
)
15589 if (!parm
->declarator
)
15592 if (parm
->declarator
->kind
!= cdk_id
)
15594 cp_parser_error (parser
, "invalid constrained type parameter");
15598 /* Don't allow cv-qualified type parameters. */
15599 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15600 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15602 cp_parser_error (parser
, "cv-qualified type parameter");
15609 /* Finish parsing/processing a template type parameter and checking
15610 various restrictions. */
15613 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15615 cp_parameter_declarator
* parmdecl
)
15617 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15618 return finish_template_type_parm (class_type_node
, id
);
15620 return error_mark_node
;
15624 finish_constrained_template_template_parm (tree proto
, tree id
)
15626 /* FIXME: This should probably be copied, and we may need to adjust
15627 the template parameter depths. */
15628 tree saved_parms
= current_template_parms
;
15629 begin_template_parm_list ();
15630 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15631 end_template_parm_list ();
15633 tree parm
= finish_template_template_parm (class_type_node
, id
);
15634 current_template_parms
= saved_parms
;
15639 /* Finish parsing/processing a template template parameter by borrowing
15640 the template parameter list from the prototype parameter. */
15643 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15646 cp_parameter_declarator
*parmdecl
)
15648 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15649 return error_mark_node
;
15650 return finish_constrained_template_template_parm (proto
, id
);
15653 /* Create a new non-type template parameter from the given PARM
15657 constrained_non_type_template_parm (bool *is_non_type
,
15658 cp_parameter_declarator
*parm
)
15660 *is_non_type
= true;
15661 cp_declarator
*decl
= parm
->declarator
;
15662 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15663 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15664 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15667 /* Build a constrained template parameter based on the PARMDECL
15668 declarator. The type of PARMDECL is the constrained type, which
15669 refers to the prototype template parameter that ultimately
15670 specifies the type of the declared parameter. */
15673 finish_constrained_parameter (cp_parser
*parser
,
15674 cp_parameter_declarator
*parmdecl
,
15676 bool *is_parameter_pack
)
15678 tree decl
= parmdecl
->decl_specifiers
.type
;
15679 tree id
= get_unqualified_id (parmdecl
->declarator
);
15680 tree def
= parmdecl
->default_argument
;
15681 tree proto
= DECL_INITIAL (decl
);
15683 /* A template parameter constrained by a variadic concept shall also
15684 be declared as a template parameter pack. */
15685 bool is_variadic
= template_parameter_pack_p (proto
);
15686 if (is_variadic
&& !*is_parameter_pack
)
15687 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15689 /* Build the parameter. Return an error if the declarator was invalid. */
15691 if (TREE_CODE (proto
) == TYPE_DECL
)
15692 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15693 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15694 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15697 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15698 if (parm
== error_mark_node
)
15699 return error_mark_node
;
15701 /* Finish the parameter decl and create a node attaching the
15702 default argument and constraint. */
15703 parm
= build_tree_list (def
, parm
);
15704 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15709 /* Returns true if the parsed type actually represents the declaration
15710 of a type template-parameter. */
15713 declares_constrained_type_template_parameter (tree type
)
15715 return (is_constrained_parameter (type
)
15716 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15720 /* Returns true if the parsed type actually represents the declaration of
15721 a template template-parameter. */
15724 declares_constrained_template_template_parameter (tree type
)
15726 return (is_constrained_parameter (type
)
15727 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15730 /* Parse a default argument for a type template-parameter.
15731 Note that diagnostics are handled in cp_parser_template_parameter. */
15734 cp_parser_default_type_template_argument (cp_parser
*parser
)
15736 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15738 /* Consume the `=' token. */
15739 cp_lexer_consume_token (parser
->lexer
);
15741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15743 /* Parse the default-argument. */
15744 push_deferring_access_checks (dk_no_deferred
);
15745 tree default_argument
= cp_parser_type_id (parser
);
15746 pop_deferring_access_checks ();
15748 if (flag_concepts
&& type_uses_auto (default_argument
))
15750 error_at (token
->location
,
15751 "invalid use of %<auto%> in default template argument");
15752 return error_mark_node
;
15755 return default_argument
;
15758 /* Parse a default argument for a template template-parameter. */
15761 cp_parser_default_template_template_argument (cp_parser
*parser
)
15763 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15767 /* Consume the `='. */
15768 cp_lexer_consume_token (parser
->lexer
);
15769 /* Parse the id-expression. */
15770 push_deferring_access_checks (dk_no_deferred
);
15771 /* save token before parsing the id-expression, for error
15773 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15774 tree default_argument
15775 = cp_parser_id_expression (parser
,
15776 /*template_keyword_p=*/false,
15777 /*check_dependency_p=*/true,
15778 /*template_p=*/&is_template
,
15779 /*declarator_p=*/false,
15780 /*optional_p=*/false);
15781 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15782 /* If the id-expression was a template-id that refers to
15783 a template-class, we already have the declaration here,
15784 so no further lookup is needed. */
15787 /* Look up the name. */
15789 = cp_parser_lookup_name (parser
, default_argument
,
15791 /*is_template=*/is_template
,
15792 /*is_namespace=*/false,
15793 /*check_dependency=*/true,
15794 /*ambiguous_decls=*/NULL
,
15796 /* See if the default argument is valid. */
15797 default_argument
= check_template_template_default_arg (default_argument
);
15798 pop_deferring_access_checks ();
15799 return default_argument
;
15802 /* Parse a template-parameter.
15804 template-parameter:
15806 parameter-declaration
15808 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15809 the parameter. The TREE_PURPOSE is the default value, if any.
15810 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15811 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15812 set to true iff this parameter is a parameter pack. */
15815 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15816 bool *is_parameter_pack
)
15819 cp_parameter_declarator
*parameter_declarator
;
15822 /* Assume it is a type parameter or a template parameter. */
15823 *is_non_type
= false;
15824 /* Assume it not a parameter pack. */
15825 *is_parameter_pack
= false;
15826 /* Peek at the next token. */
15827 token
= cp_lexer_peek_token (parser
->lexer
);
15828 /* If it is `template', we have a type-parameter. */
15829 if (token
->keyword
== RID_TEMPLATE
)
15830 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15831 /* If it is `class' or `typename' we do not know yet whether it is a
15832 type parameter or a non-type parameter. Consider:
15834 template <typename T, typename T::X X> ...
15838 template <class C, class D*> ...
15840 Here, the first parameter is a type parameter, and the second is
15841 a non-type parameter. We can tell by looking at the token after
15842 the identifier -- if it is a `,', `=', or `>' then we have a type
15844 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15846 /* Peek at the token after `class' or `typename'. */
15847 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15848 /* If it's an ellipsis, we have a template type parameter
15850 if (token
->type
== CPP_ELLIPSIS
)
15851 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15852 /* If it's an identifier, skip it. */
15853 if (token
->type
== CPP_NAME
)
15854 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15855 /* Now, see if the token looks like the end of a template
15857 if (token
->type
== CPP_COMMA
15858 || token
->type
== CPP_EQ
15859 || token
->type
== CPP_GREATER
)
15860 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15863 /* Otherwise, it is a non-type parameter or a constrained parameter.
15867 When parsing a default template-argument for a non-type
15868 template-parameter, the first non-nested `>' is taken as the end
15869 of the template parameter-list rather than a greater-than
15871 parameter_declarator
15872 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15873 /*parenthesized_p=*/NULL
);
15875 if (!parameter_declarator
)
15876 return error_mark_node
;
15878 /* If the parameter declaration is marked as a parameter pack, set
15879 *IS_PARAMETER_PACK to notify the caller. */
15880 if (parameter_declarator
->template_parameter_pack_p
)
15881 *is_parameter_pack
= true;
15883 if (parameter_declarator
->default_argument
)
15885 /* Can happen in some cases of erroneous input (c++/34892). */
15886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15887 /* Consume the `...' for better error recovery. */
15888 cp_lexer_consume_token (parser
->lexer
);
15891 // The parameter may have been constrained.
15892 if (is_constrained_parameter (parameter_declarator
))
15893 return finish_constrained_parameter (parser
,
15894 parameter_declarator
,
15896 is_parameter_pack
);
15898 // Now we're sure that the parameter is a non-type parameter.
15899 *is_non_type
= true;
15901 parm
= grokdeclarator (parameter_declarator
->declarator
,
15902 ¶meter_declarator
->decl_specifiers
,
15903 TPARM
, /*initialized=*/0,
15904 /*attrlist=*/NULL
);
15905 if (parm
== error_mark_node
)
15906 return error_mark_node
;
15908 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15911 /* Parse a type-parameter.
15914 class identifier [opt]
15915 class identifier [opt] = type-id
15916 typename identifier [opt]
15917 typename identifier [opt] = type-id
15918 template < template-parameter-list > class identifier [opt]
15919 template < template-parameter-list > class identifier [opt]
15922 GNU Extension (variadic templates):
15925 class ... identifier [opt]
15926 typename ... identifier [opt]
15928 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15929 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15930 the declaration of the parameter.
15932 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15935 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15940 /* Look for a keyword to tell us what kind of parameter this is. */
15941 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15943 return error_mark_node
;
15945 switch (token
->keyword
)
15951 tree default_argument
;
15953 /* If the next token is an ellipsis, we have a template
15955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15957 /* Consume the `...' token. */
15958 cp_lexer_consume_token (parser
->lexer
);
15959 maybe_warn_variadic_templates ();
15961 *is_parameter_pack
= true;
15964 /* If the next token is an identifier, then it names the
15966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15967 identifier
= cp_parser_identifier (parser
);
15969 identifier
= NULL_TREE
;
15971 /* Create the parameter. */
15972 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15974 /* If the next token is an `=', we have a default argument. */
15975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15978 = cp_parser_default_type_template_argument (parser
);
15980 /* Template parameter packs cannot have default
15982 if (*is_parameter_pack
)
15985 error_at (token
->location
,
15986 "template parameter pack %qD cannot have a "
15987 "default argument", identifier
);
15989 error_at (token
->location
,
15990 "template parameter packs cannot have "
15991 "default arguments");
15992 default_argument
= NULL_TREE
;
15994 else if (check_for_bare_parameter_packs (default_argument
))
15995 default_argument
= error_mark_node
;
15998 default_argument
= NULL_TREE
;
16000 /* Create the combined representation of the parameter and the
16001 default argument. */
16002 parameter
= build_tree_list (default_argument
, parameter
);
16009 tree default_argument
;
16011 /* Look for the `<'. */
16012 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16013 /* Parse the template-parameter-list. */
16014 cp_parser_template_parameter_list (parser
);
16015 /* Look for the `>'. */
16016 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16018 // If template requirements are present, parse them.
16021 tree reqs
= get_shorthand_constraints (current_template_parms
);
16022 if (tree r
= cp_parser_requires_clause_opt (parser
))
16023 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16024 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16027 /* Look for the `class' or 'typename' keywords. */
16028 cp_parser_type_parameter_key (parser
);
16029 /* If the next token is an ellipsis, we have a template
16031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16033 /* Consume the `...' token. */
16034 cp_lexer_consume_token (parser
->lexer
);
16035 maybe_warn_variadic_templates ();
16037 *is_parameter_pack
= true;
16039 /* If the next token is an `=', then there is a
16040 default-argument. If the next token is a `>', we are at
16041 the end of the parameter-list. If the next token is a `,',
16042 then we are at the end of this parameter. */
16043 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16044 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16045 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16047 identifier
= cp_parser_identifier (parser
);
16048 /* Treat invalid names as if the parameter were nameless. */
16049 if (identifier
== error_mark_node
)
16050 identifier
= NULL_TREE
;
16053 identifier
= NULL_TREE
;
16055 /* Create the template parameter. */
16056 parameter
= finish_template_template_parm (class_type_node
,
16059 /* If the next token is an `=', then there is a
16060 default-argument. */
16061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16064 = cp_parser_default_template_template_argument (parser
);
16066 /* Template parameter packs cannot have default
16068 if (*is_parameter_pack
)
16071 error_at (token
->location
,
16072 "template parameter pack %qD cannot "
16073 "have a default argument",
16076 error_at (token
->location
, "template parameter packs cannot "
16077 "have default arguments");
16078 default_argument
= NULL_TREE
;
16082 default_argument
= NULL_TREE
;
16084 /* Create the combined representation of the parameter and the
16085 default argument. */
16086 parameter
= build_tree_list (default_argument
, parameter
);
16091 gcc_unreachable ();
16098 /* Parse a template-id.
16101 template-name < template-argument-list [opt] >
16103 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16104 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16105 returned. Otherwise, if the template-name names a function, or set
16106 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16107 names a class, returns a TYPE_DECL for the specialization.
16109 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16110 uninstantiated templates. */
16113 cp_parser_template_id (cp_parser
*parser
,
16114 bool template_keyword_p
,
16115 bool check_dependency_p
,
16116 enum tag_types tag_type
,
16117 bool is_declaration
)
16122 cp_token_position start_of_id
= 0;
16123 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16124 bool is_identifier
;
16126 /* If the next token corresponds to a template-id, there is no need
16128 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16129 if (token
->type
== CPP_TEMPLATE_ID
)
16131 cp_lexer_consume_token (parser
->lexer
);
16132 return saved_checks_value (token
->u
.tree_check_value
);
16135 /* Avoid performing name lookup if there is no possibility of
16136 finding a template-id. */
16137 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16138 || (token
->type
== CPP_NAME
16139 && !cp_parser_nth_token_starts_template_argument_list_p
16142 cp_parser_error (parser
, "expected template-id");
16143 return error_mark_node
;
16146 /* Remember where the template-id starts. */
16147 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16148 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16150 push_deferring_access_checks (dk_deferred
);
16152 /* Parse the template-name. */
16153 is_identifier
= false;
16154 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16155 check_dependency_p
,
16159 if (templ
== error_mark_node
|| is_identifier
)
16161 pop_deferring_access_checks ();
16165 /* Since we're going to preserve any side-effects from this parse, set up a
16166 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16167 in the template arguments. */
16168 tentative_firewall
firewall (parser
);
16170 /* If we find the sequence `[:' after a template-name, it's probably
16171 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16172 parse correctly the argument list. */
16173 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16174 == CPP_OPEN_SQUARE
)
16175 && next_token
->flags
& DIGRAPH
16176 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16178 && !(next_token_2
->flags
& PREV_WHITE
))
16180 cp_parser_parse_tentatively (parser
);
16181 /* Change `:' into `::'. */
16182 next_token_2
->type
= CPP_SCOPE
;
16183 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16185 cp_lexer_consume_token (parser
->lexer
);
16187 /* Parse the arguments. */
16188 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16189 if (!cp_parser_parse_definitely (parser
))
16191 /* If we couldn't parse an argument list, then we revert our changes
16192 and return simply an error. Maybe this is not a template-id
16194 next_token_2
->type
= CPP_COLON
;
16195 cp_parser_error (parser
, "expected %<<%>");
16196 pop_deferring_access_checks ();
16197 return error_mark_node
;
16199 /* Otherwise, emit an error about the invalid digraph, but continue
16200 parsing because we got our argument list. */
16201 if (permerror (next_token
->location
,
16202 "%<<::%> cannot begin a template-argument list"))
16204 static bool hint
= false;
16205 inform (next_token
->location
,
16206 "%<<:%> is an alternate spelling for %<[%>."
16207 " Insert whitespace between %<<%> and %<::%>");
16208 if (!hint
&& !flag_permissive
)
16210 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16211 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16212 "accept your code)");
16219 /* Look for the `<' that starts the template-argument-list. */
16220 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16222 pop_deferring_access_checks ();
16223 return error_mark_node
;
16225 /* Parse the arguments. */
16226 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16228 if ((cxx_dialect
> cxx17
)
16229 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16230 && !template_keyword_p
16231 && (cp_parser_error_occurred (parser
)
16232 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16234 /* This didn't go well. */
16235 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16237 /* C++2A says that "function-name < a;" is now ill-formed. */
16238 if (cp_parser_error_occurred (parser
))
16240 error_at (token
->location
, "invalid template-argument-list");
16241 inform (token
->location
, "function name as the left hand "
16242 "operand of %<<%> is ill-formed in C++2a; wrap the "
16243 "function name in %<()%>");
16246 /* We expect "f<targs>" to be followed by "(args)". */
16247 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16248 "expected %<(%> after template-argument-list");
16250 /* Purge all subsequent tokens. */
16251 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16254 cp_parser_simulate_error (parser
);
16255 pop_deferring_access_checks ();
16256 return error_mark_node
;
16260 /* Set the location to be of the form:
16261 template-name < template-argument-list [opt] >
16262 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16263 with caret == start at the start of the template-name,
16264 ranging until the closing '>'. */
16265 location_t finish_loc
16266 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16267 location_t combined_loc
16268 = make_location (token
->location
, token
->location
, finish_loc
);
16270 /* Check for concepts autos where they don't belong. We could
16271 identify types in some cases of idnetifier TEMPL, looking ahead
16272 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16273 types. We reject them in functions, but if what we have is an
16274 identifier, even with none_type we can't conclude it's NOT a
16275 type, we have to wait for template substitution. */
16276 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16277 template_id
= error_mark_node
;
16278 /* Build a representation of the specialization. */
16279 else if (identifier_p (templ
))
16280 template_id
= build_min_nt_loc (combined_loc
,
16283 else if (DECL_TYPE_TEMPLATE_P (templ
)
16284 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16286 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16287 template (rather than some instantiation thereof) only if
16288 is not nested within some other construct. For example, in
16289 "template <typename T> void f(T) { A<T>::", A<T> is just an
16290 instantiation of A. */
16291 bool entering_scope
16292 = (template_parm_scope_p ()
16293 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16295 = finish_template_type (templ
, arguments
, entering_scope
);
16297 /* A template-like identifier may be a partial concept id. */
16298 else if (flag_concepts
16299 && (template_id
= (cp_parser_maybe_partial_concept_id
16300 (parser
, templ
, arguments
))))
16301 return template_id
;
16302 else if (variable_template_p (templ
))
16304 template_id
= lookup_template_variable (templ
, arguments
);
16305 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16306 SET_EXPR_LOCATION (template_id
, combined_loc
);
16310 /* If it's not a class-template or a template-template, it should be
16311 a function-template. */
16312 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16313 || TREE_CODE (templ
) == OVERLOAD
16314 || TREE_CODE (templ
) == FUNCTION_DECL
16315 || BASELINK_P (templ
)));
16317 template_id
= lookup_template_function (templ
, arguments
);
16318 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16319 SET_EXPR_LOCATION (template_id
, combined_loc
);
16322 /* If parsing tentatively, replace the sequence of tokens that makes
16323 up the template-id with a CPP_TEMPLATE_ID token. That way,
16324 should we re-parse the token stream, we will not have to repeat
16325 the effort required to do the parse, nor will we issue duplicate
16326 error messages about problems during instantiation of the
16329 /* Don't do this if we had a parse error in a declarator; re-parsing
16330 might succeed if a name changes meaning (60361). */
16331 && !(cp_parser_error_occurred (parser
)
16332 && cp_parser_parsing_tentatively (parser
)
16333 && parser
->in_declarator_p
))
16335 /* Reset the contents of the START_OF_ID token. */
16336 token
->type
= CPP_TEMPLATE_ID
;
16337 token
->location
= combined_loc
;
16339 /* Retrieve any deferred checks. Do not pop this access checks yet
16340 so the memory will not be reclaimed during token replacing below. */
16341 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16342 token
->u
.tree_check_value
->value
= template_id
;
16343 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16344 token
->keyword
= RID_MAX
;
16346 /* Purge all subsequent tokens. */
16347 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16349 /* ??? Can we actually assume that, if template_id ==
16350 error_mark_node, we will have issued a diagnostic to the
16351 user, as opposed to simply marking the tentative parse as
16353 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16354 error_at (token
->location
, "parse error in template argument list");
16357 pop_to_parent_deferring_access_checks ();
16358 return template_id
;
16361 /* Parse a template-name.
16366 The standard should actually say:
16370 operator-function-id
16372 A defect report has been filed about this issue.
16374 A conversion-function-id cannot be a template name because they cannot
16375 be part of a template-id. In fact, looking at this code:
16377 a.operator K<int>()
16379 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16380 It is impossible to call a templated conversion-function-id with an
16381 explicit argument list, since the only allowed template parameter is
16382 the type to which it is converting.
16384 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16385 `template' keyword, in a construction like:
16389 In that case `f' is taken to be a template-name, even though there
16390 is no way of knowing for sure.
16392 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16393 name refers to a set of overloaded functions, at least one of which
16394 is a template, or an IDENTIFIER_NODE with the name of the template,
16395 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16396 names are looked up inside uninstantiated templates. */
16399 cp_parser_template_name (cp_parser
* parser
,
16400 bool template_keyword_p
,
16401 bool check_dependency_p
,
16402 bool is_declaration
,
16403 enum tag_types tag_type
,
16404 bool *is_identifier
)
16408 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16410 /* If the next token is `operator', then we have either an
16411 operator-function-id or a conversion-function-id. */
16412 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16414 /* We don't know whether we're looking at an
16415 operator-function-id or a conversion-function-id. */
16416 cp_parser_parse_tentatively (parser
);
16417 /* Try an operator-function-id. */
16418 identifier
= cp_parser_operator_function_id (parser
);
16419 /* If that didn't work, try a conversion-function-id. */
16420 if (!cp_parser_parse_definitely (parser
))
16422 cp_parser_error (parser
, "expected template-name");
16423 return error_mark_node
;
16426 /* Look for the identifier. */
16428 identifier
= cp_parser_identifier (parser
);
16430 /* If we didn't find an identifier, we don't have a template-id. */
16431 if (identifier
== error_mark_node
)
16432 return error_mark_node
;
16434 /* If the name immediately followed the `template' keyword, then it
16435 is a template-name. However, if the next token is not `<', then
16436 we do not treat it as a template-name, since it is not being used
16437 as part of a template-id. This enables us to handle constructs
16440 template <typename T> struct S { S(); };
16441 template <typename T> S<T>::S();
16443 correctly. We would treat `S' as a template -- if it were `S<T>'
16444 -- but we do not if there is no `<'. */
16446 if (processing_template_decl
16447 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16449 /* In a declaration, in a dependent context, we pretend that the
16450 "template" keyword was present in order to improve error
16451 recovery. For example, given:
16453 template <typename T> void f(T::X<int>);
16455 we want to treat "X<int>" as a template-id. */
16457 && !template_keyword_p
16458 && parser
->scope
&& TYPE_P (parser
->scope
)
16459 && check_dependency_p
16460 && dependent_scope_p (parser
->scope
)
16461 /* Do not do this for dtors (or ctors), since they never
16462 need the template keyword before their name. */
16463 && !constructor_name_p (identifier
, parser
->scope
))
16465 cp_token_position start
= 0;
16467 /* Explain what went wrong. */
16468 error_at (token
->location
, "non-template %qD used as template",
16470 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16471 parser
->scope
, identifier
);
16472 /* If parsing tentatively, find the location of the "<" token. */
16473 if (cp_parser_simulate_error (parser
))
16474 start
= cp_lexer_token_position (parser
->lexer
, true);
16475 /* Parse the template arguments so that we can issue error
16476 messages about them. */
16477 cp_lexer_consume_token (parser
->lexer
);
16478 cp_parser_enclosed_template_argument_list (parser
);
16479 /* Skip tokens until we find a good place from which to
16480 continue parsing. */
16481 cp_parser_skip_to_closing_parenthesis (parser
,
16482 /*recovering=*/true,
16484 /*consume_paren=*/false);
16485 /* If parsing tentatively, permanently remove the
16486 template argument list. That will prevent duplicate
16487 error messages from being issued about the missing
16488 "template" keyword. */
16490 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16492 *is_identifier
= true;
16493 parser
->context
->object_type
= NULL_TREE
;
16497 /* If the "template" keyword is present, then there is generally
16498 no point in doing name-lookup, so we just return IDENTIFIER.
16499 But, if the qualifying scope is non-dependent then we can
16500 (and must) do name-lookup normally. */
16501 if (template_keyword_p
)
16503 tree scope
= (parser
->scope
? parser
->scope
16504 : parser
->context
->object_type
);
16505 if (scope
&& TYPE_P (scope
)
16506 && (!CLASS_TYPE_P (scope
)
16507 || (check_dependency_p
&& dependent_type_p (scope
))))
16509 /* We're optimizing away the call to cp_parser_lookup_name, but
16510 we still need to do this. */
16511 parser
->context
->object_type
= NULL_TREE
;
16517 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16518 const bool scoped_p
= ((parser
->scope
? parser
->scope
16519 : parser
->context
->object_type
) != NULL_TREE
);
16521 /* Look up the name. */
16522 decl
= cp_parser_lookup_name (parser
, identifier
,
16524 /*is_template=*/true,
16525 /*is_namespace=*/false,
16526 check_dependency_p
,
16527 /*ambiguous_decls=*/NULL
,
16530 decl
= strip_using_decl (decl
);
16532 /* If DECL is a template, then the name was a template-name. */
16533 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16535 if (TREE_DEPRECATED (decl
)
16536 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16537 warn_deprecated_use (decl
, NULL_TREE
);
16541 /* The standard does not explicitly indicate whether a name that
16542 names a set of overloaded declarations, some of which are
16543 templates, is a template-name. However, such a name should
16544 be a template-name; otherwise, there is no way to form a
16545 template-id for the overloaded templates. */
16546 bool found
= false;
16548 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16549 !found
&& iter
; ++iter
)
16550 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16554 && (cxx_dialect
> cxx17
)
16556 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
16558 /* [temp.names] says "A name is also considered to refer to a template
16559 if it is an unqualified-id followed by a < and name lookup finds
16560 either one or more functions or finds nothing." */
16562 /* The "more functions" case. Just use the OVERLOAD as normally.
16563 We don't use is_overloaded_fn here to avoid considering
16565 if (TREE_CODE (decl
) == OVERLOAD
16566 /* Name lookup found one function. */
16567 || TREE_CODE (decl
) == FUNCTION_DECL
)
16569 /* Name lookup found nothing. */
16570 else if (decl
== error_mark_node
)
16576 /* The name does not name a template. */
16577 cp_parser_error (parser
, "expected template-name");
16578 return error_mark_node
;
16585 /* Parse a template-argument-list.
16587 template-argument-list:
16588 template-argument ... [opt]
16589 template-argument-list , template-argument ... [opt]
16591 Returns a TREE_VEC containing the arguments. */
16594 cp_parser_template_argument_list (cp_parser
* parser
)
16596 tree fixed_args
[10];
16597 unsigned n_args
= 0;
16598 unsigned alloced
= 10;
16599 tree
*arg_ary
= fixed_args
;
16601 bool saved_in_template_argument_list_p
;
16603 bool saved_non_ice_p
;
16605 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16606 parser
->in_template_argument_list_p
= true;
16607 /* Even if the template-id appears in an integral
16608 constant-expression, the contents of the argument list do
16610 saved_ice_p
= parser
->integral_constant_expression_p
;
16611 parser
->integral_constant_expression_p
= false;
16612 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16613 parser
->non_integral_constant_expression_p
= false;
16615 /* Parse the arguments. */
16621 /* Consume the comma. */
16622 cp_lexer_consume_token (parser
->lexer
);
16624 /* Parse the template-argument. */
16625 argument
= cp_parser_template_argument (parser
);
16627 /* If the next token is an ellipsis, we're expanding a template
16629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16631 if (argument
== error_mark_node
)
16633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16634 error_at (token
->location
,
16635 "expected parameter pack before %<...%>");
16637 /* Consume the `...' token. */
16638 cp_lexer_consume_token (parser
->lexer
);
16640 /* Make the argument into a TYPE_PACK_EXPANSION or
16641 EXPR_PACK_EXPANSION. */
16642 argument
= make_pack_expansion (argument
);
16645 if (n_args
== alloced
)
16649 if (arg_ary
== fixed_args
)
16651 arg_ary
= XNEWVEC (tree
, alloced
);
16652 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16655 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16657 arg_ary
[n_args
++] = argument
;
16659 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16661 vec
= make_tree_vec (n_args
);
16664 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16666 if (arg_ary
!= fixed_args
)
16668 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16669 parser
->integral_constant_expression_p
= saved_ice_p
;
16670 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16672 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16676 /* Parse a template-argument.
16679 assignment-expression
16683 The representation is that of an assignment-expression, type-id, or
16684 id-expression -- except that the qualified id-expression is
16685 evaluated, so that the value returned is either a DECL or an
16688 Although the standard says "assignment-expression", it forbids
16689 throw-expressions or assignments in the template argument.
16690 Therefore, we use "conditional-expression" instead. */
16693 cp_parser_template_argument (cp_parser
* parser
)
16698 bool maybe_type_id
= false;
16699 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16700 location_t loc
= 0;
16703 /* There's really no way to know what we're looking at, so we just
16704 try each alternative in order.
16708 In a template-argument, an ambiguity between a type-id and an
16709 expression is resolved to a type-id, regardless of the form of
16710 the corresponding template-parameter.
16712 Therefore, we try a type-id first. */
16713 cp_parser_parse_tentatively (parser
);
16714 argument
= cp_parser_template_type_arg (parser
);
16715 /* If there was no error parsing the type-id but the next token is a
16716 '>>', our behavior depends on which dialect of C++ we're
16717 parsing. In C++98, we probably found a typo for '> >'. But there
16718 are type-id which are also valid expressions. For instance:
16720 struct X { int operator >> (int); };
16721 template <int V> struct Foo {};
16724 Here 'X()' is a valid type-id of a function type, but the user just
16725 wanted to write the expression "X() >> 5". Thus, we remember that we
16726 found a valid type-id, but we still try to parse the argument as an
16727 expression to see what happens.
16729 In C++0x, the '>>' will be considered two separate '>'
16731 if (!cp_parser_error_occurred (parser
)
16732 && cxx_dialect
== cxx98
16733 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16735 maybe_type_id
= true;
16736 cp_parser_abort_tentative_parse (parser
);
16740 /* If the next token isn't a `,' or a `>', then this argument wasn't
16741 really finished. This means that the argument is not a valid
16743 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16744 cp_parser_error (parser
, "expected template-argument");
16745 /* If that worked, we're done. */
16746 if (cp_parser_parse_definitely (parser
))
16749 /* We're still not sure what the argument will be. */
16750 cp_parser_parse_tentatively (parser
);
16751 /* Try a template. */
16752 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16753 argument
= cp_parser_id_expression (parser
,
16754 /*template_keyword_p=*/false,
16755 /*check_dependency_p=*/true,
16757 /*declarator_p=*/false,
16758 /*optional_p=*/false);
16759 /* If the next token isn't a `,' or a `>', then this argument wasn't
16760 really finished. */
16761 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16762 cp_parser_error (parser
, "expected template-argument");
16763 if (!cp_parser_error_occurred (parser
))
16765 /* Figure out what is being referred to. If the id-expression
16766 was for a class template specialization, then we will have a
16767 TYPE_DECL at this point. There is no need to do name lookup
16768 at this point in that case. */
16769 if (TREE_CODE (argument
) != TYPE_DECL
)
16770 argument
= cp_parser_lookup_name (parser
, argument
,
16772 /*is_template=*/template_p
,
16773 /*is_namespace=*/false,
16774 /*check_dependency=*/true,
16775 /*ambiguous_decls=*/NULL
,
16776 argument_start_token
->location
);
16777 /* Handle a constrained-type-specifier for a non-type template
16779 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16781 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16782 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16783 cp_parser_error (parser
, "expected template-name");
16785 if (cp_parser_parse_definitely (parser
))
16787 if (TREE_DEPRECATED (argument
))
16788 warn_deprecated_use (argument
, NULL_TREE
);
16791 /* It must be a non-type argument. In C++17 any constant-expression is
16793 if (cxx_dialect
> cxx14
)
16796 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16798 -- an integral constant-expression of integral or enumeration
16801 -- the name of a non-type template-parameter; or
16803 -- the name of an object or function with external linkage...
16805 -- the address of an object or function with external linkage...
16807 -- a pointer to member... */
16808 /* Look for a non-type template parameter. */
16809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16811 cp_parser_parse_tentatively (parser
);
16812 argument
= cp_parser_primary_expression (parser
,
16813 /*address_p=*/false,
16815 /*template_arg_p=*/true,
16817 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16818 || !cp_parser_next_token_ends_template_argument_p (parser
))
16819 cp_parser_simulate_error (parser
);
16820 if (cp_parser_parse_definitely (parser
))
16824 /* If the next token is "&", the argument must be the address of an
16825 object or function with external linkage. */
16826 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16829 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16830 cp_lexer_consume_token (parser
->lexer
);
16832 /* See if we might have an id-expression. */
16833 token
= cp_lexer_peek_token (parser
->lexer
);
16834 if (token
->type
== CPP_NAME
16835 || token
->keyword
== RID_OPERATOR
16836 || token
->type
== CPP_SCOPE
16837 || token
->type
== CPP_TEMPLATE_ID
16838 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16840 cp_parser_parse_tentatively (parser
);
16841 argument
= cp_parser_primary_expression (parser
,
16844 /*template_arg_p=*/true,
16846 if (cp_parser_error_occurred (parser
)
16847 || !cp_parser_next_token_ends_template_argument_p (parser
))
16848 cp_parser_abort_tentative_parse (parser
);
16853 if (INDIRECT_REF_P (argument
))
16855 /* Strip the dereference temporarily. */
16856 gcc_assert (REFERENCE_REF_P (argument
));
16857 argument
= TREE_OPERAND (argument
, 0);
16860 /* If we're in a template, we represent a qualified-id referring
16861 to a static data member as a SCOPE_REF even if the scope isn't
16862 dependent so that we can check access control later. */
16864 if (TREE_CODE (probe
) == SCOPE_REF
)
16865 probe
= TREE_OPERAND (probe
, 1);
16868 /* A variable without external linkage might still be a
16869 valid constant-expression, so no error is issued here
16870 if the external-linkage check fails. */
16871 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16872 cp_parser_simulate_error (parser
);
16874 else if (is_overloaded_fn (argument
))
16875 /* All overloaded functions are allowed; if the external
16876 linkage test does not pass, an error will be issued
16880 && (TREE_CODE (argument
) == OFFSET_REF
16881 || TREE_CODE (argument
) == SCOPE_REF
))
16882 /* A pointer-to-member. */
16884 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16887 cp_parser_simulate_error (parser
);
16889 if (cp_parser_parse_definitely (parser
))
16892 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16893 tf_warning_or_error
);
16895 argument
= convert_from_reference (argument
);
16900 /* If the argument started with "&", there are no other valid
16901 alternatives at this point. */
16904 cp_parser_error (parser
, "invalid non-type template argument");
16905 return error_mark_node
;
16909 /* If the argument wasn't successfully parsed as a type-id followed
16910 by '>>', the argument can only be a constant expression now.
16911 Otherwise, we try parsing the constant-expression tentatively,
16912 because the argument could really be a type-id. */
16914 cp_parser_parse_tentatively (parser
);
16916 if (cxx_dialect
<= cxx14
)
16917 argument
= cp_parser_constant_expression (parser
);
16920 /* With C++17 generalized non-type template arguments we need to handle
16921 lvalue constant expressions, too. */
16922 argument
= cp_parser_assignment_expression (parser
);
16923 require_potential_constant_expression (argument
);
16926 if (!maybe_type_id
)
16928 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16929 cp_parser_error (parser
, "expected template-argument");
16930 if (cp_parser_parse_definitely (parser
))
16932 /* We did our best to parse the argument as a non type-id, but that
16933 was the only alternative that matched (albeit with a '>' after
16934 it). We can assume it's just a typo from the user, and a
16935 diagnostic will then be issued. */
16936 return cp_parser_template_type_arg (parser
);
16939 /* Parse an explicit-instantiation.
16941 explicit-instantiation:
16942 template declaration
16944 Although the standard says `declaration', what it really means is:
16946 explicit-instantiation:
16947 template decl-specifier-seq [opt] declarator [opt] ;
16949 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16950 supposed to be allowed. A defect report has been filed about this
16955 explicit-instantiation:
16956 storage-class-specifier template
16957 decl-specifier-seq [opt] declarator [opt] ;
16958 function-specifier template
16959 decl-specifier-seq [opt] declarator [opt] ; */
16962 cp_parser_explicit_instantiation (cp_parser
* parser
)
16964 int declares_class_or_enum
;
16965 cp_decl_specifier_seq decl_specifiers
;
16966 tree extension_specifier
= NULL_TREE
;
16968 timevar_push (TV_TEMPLATE_INST
);
16970 /* Look for an (optional) storage-class-specifier or
16971 function-specifier. */
16972 if (cp_parser_allow_gnu_extensions_p (parser
))
16974 extension_specifier
16975 = cp_parser_storage_class_specifier_opt (parser
);
16976 if (!extension_specifier
)
16977 extension_specifier
16978 = cp_parser_function_specifier_opt (parser
,
16979 /*decl_specs=*/NULL
);
16982 /* Look for the `template' keyword. */
16983 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16984 /* Let the front end know that we are processing an explicit
16986 begin_explicit_instantiation ();
16987 /* [temp.explicit] says that we are supposed to ignore access
16988 control while processing explicit instantiation directives. */
16989 push_deferring_access_checks (dk_no_check
);
16990 /* Parse a decl-specifier-seq. */
16991 cp_parser_decl_specifier_seq (parser
,
16992 CP_PARSER_FLAGS_OPTIONAL
,
16994 &declares_class_or_enum
);
16995 /* If there was exactly one decl-specifier, and it declared a class,
16996 and there's no declarator, then we have an explicit type
16998 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17002 type
= check_tag_decl (&decl_specifiers
,
17003 /*explicit_type_instantiation_p=*/true);
17004 /* Turn access control back on for names used during
17005 template instantiation. */
17006 pop_deferring_access_checks ();
17008 do_type_instantiation (type
, extension_specifier
,
17009 /*complain=*/tf_error
);
17013 cp_declarator
*declarator
;
17016 /* Parse the declarator. */
17018 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17019 /*ctor_dtor_or_conv_p=*/NULL
,
17020 /*parenthesized_p=*/NULL
,
17021 /*member_p=*/false,
17022 /*friend_p=*/false);
17023 if (declares_class_or_enum
& 2)
17024 cp_parser_check_for_definition_in_return_type (declarator
,
17025 decl_specifiers
.type
,
17026 decl_specifiers
.locations
[ds_type_spec
]);
17027 if (declarator
!= cp_error_declarator
)
17029 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17030 permerror (decl_specifiers
.locations
[ds_inline
],
17031 "explicit instantiation shall not use"
17032 " %<inline%> specifier");
17033 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17034 permerror (decl_specifiers
.locations
[ds_constexpr
],
17035 "explicit instantiation shall not use"
17036 " %<constexpr%> specifier");
17038 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17039 NORMAL
, 0, &decl_specifiers
.attributes
);
17040 /* Turn access control back on for names used during
17041 template instantiation. */
17042 pop_deferring_access_checks ();
17043 /* Do the explicit instantiation. */
17044 do_decl_instantiation (decl
, extension_specifier
);
17048 pop_deferring_access_checks ();
17049 /* Skip the body of the explicit instantiation. */
17050 cp_parser_skip_to_end_of_statement (parser
);
17053 /* We're done with the instantiation. */
17054 end_explicit_instantiation ();
17056 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17058 timevar_pop (TV_TEMPLATE_INST
);
17061 /* Parse an explicit-specialization.
17063 explicit-specialization:
17064 template < > declaration
17066 Although the standard says `declaration', what it really means is:
17068 explicit-specialization:
17069 template <> decl-specifier [opt] init-declarator [opt] ;
17070 template <> function-definition
17071 template <> explicit-specialization
17072 template <> template-declaration */
17075 cp_parser_explicit_specialization (cp_parser
* parser
)
17077 bool need_lang_pop
;
17078 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17080 /* Look for the `template' keyword. */
17081 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17082 /* Look for the `<'. */
17083 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17084 /* Look for the `>'. */
17085 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17086 /* We have processed another parameter list. */
17087 ++parser
->num_template_parameter_lists
;
17090 A template ... explicit specialization ... shall not have C
17092 if (current_lang_name
== lang_name_c
)
17094 error_at (token
->location
, "template specialization with C linkage");
17095 maybe_show_extern_c_location ();
17096 /* Give it C++ linkage to avoid confusing other parts of the
17098 push_lang_context (lang_name_cplusplus
);
17099 need_lang_pop
= true;
17102 need_lang_pop
= false;
17103 /* Let the front end know that we are beginning a specialization. */
17104 if (!begin_specialization ())
17106 end_specialization ();
17110 /* If the next keyword is `template', we need to figure out whether
17111 or not we're looking a template-declaration. */
17112 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17114 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17115 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17116 cp_parser_template_declaration_after_export (parser
,
17117 /*member_p=*/false);
17119 cp_parser_explicit_specialization (parser
);
17122 /* Parse the dependent declaration. */
17123 cp_parser_single_declaration (parser
,
17125 /*member_p=*/false,
17126 /*explicit_specialization_p=*/true,
17127 /*friend_p=*/NULL
);
17128 /* We're done with the specialization. */
17129 end_specialization ();
17130 /* For the erroneous case of a template with C linkage, we pushed an
17131 implicit C++ linkage scope; exit that scope now. */
17133 pop_lang_context ();
17134 /* We're done with this parameter list. */
17135 --parser
->num_template_parameter_lists
;
17138 /* Parse a type-specifier.
17141 simple-type-specifier
17144 elaborated-type-specifier
17152 Returns a representation of the type-specifier. For a
17153 class-specifier, enum-specifier, or elaborated-type-specifier, a
17154 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17156 The parser flags FLAGS is used to control type-specifier parsing.
17158 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17159 in a decl-specifier-seq.
17161 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17162 class-specifier, enum-specifier, or elaborated-type-specifier, then
17163 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17164 if a type is declared; 2 if it is defined. Otherwise, it is set to
17167 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17168 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17169 is set to FALSE. */
17172 cp_parser_type_specifier (cp_parser
* parser
,
17173 cp_parser_flags flags
,
17174 cp_decl_specifier_seq
*decl_specs
,
17175 bool is_declaration
,
17176 int* declares_class_or_enum
,
17177 bool* is_cv_qualifier
)
17179 tree type_spec
= NULL_TREE
;
17182 cp_decl_spec ds
= ds_last
;
17184 /* Assume this type-specifier does not declare a new type. */
17185 if (declares_class_or_enum
)
17186 *declares_class_or_enum
= 0;
17187 /* And that it does not specify a cv-qualifier. */
17188 if (is_cv_qualifier
)
17189 *is_cv_qualifier
= false;
17190 /* Peek at the next token. */
17191 token
= cp_lexer_peek_token (parser
->lexer
);
17193 /* If we're looking at a keyword, we can use that to guide the
17194 production we choose. */
17195 keyword
= token
->keyword
;
17199 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17200 goto elaborated_type_specifier
;
17202 /* Look for the enum-specifier. */
17203 type_spec
= cp_parser_enum_specifier (parser
);
17204 /* If that worked, we're done. */
17207 if (declares_class_or_enum
)
17208 *declares_class_or_enum
= 2;
17210 cp_parser_set_decl_spec_type (decl_specs
,
17213 /*type_definition_p=*/true);
17217 goto elaborated_type_specifier
;
17219 /* Any of these indicate either a class-specifier, or an
17220 elaborated-type-specifier. */
17224 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17225 goto elaborated_type_specifier
;
17227 /* Parse tentatively so that we can back up if we don't find a
17228 class-specifier. */
17229 cp_parser_parse_tentatively (parser
);
17230 /* Look for the class-specifier. */
17231 type_spec
= cp_parser_class_specifier (parser
);
17232 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17233 /* If that worked, we're done. */
17234 if (cp_parser_parse_definitely (parser
))
17236 if (declares_class_or_enum
)
17237 *declares_class_or_enum
= 2;
17239 cp_parser_set_decl_spec_type (decl_specs
,
17242 /*type_definition_p=*/true);
17246 /* Fall through. */
17247 elaborated_type_specifier
:
17248 /* We're declaring (not defining) a class or enum. */
17249 if (declares_class_or_enum
)
17250 *declares_class_or_enum
= 1;
17252 /* Fall through. */
17254 /* Look for an elaborated-type-specifier. */
17256 = (cp_parser_elaborated_type_specifier
17258 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17261 cp_parser_set_decl_spec_type (decl_specs
,
17264 /*type_definition_p=*/false);
17269 if (is_cv_qualifier
)
17270 *is_cv_qualifier
= true;
17275 if (is_cv_qualifier
)
17276 *is_cv_qualifier
= true;
17281 if (is_cv_qualifier
)
17282 *is_cv_qualifier
= true;
17286 /* The `__complex__' keyword is a GNU extension. */
17294 /* Handle simple keywords. */
17299 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17300 decl_specs
->any_specifiers_p
= true;
17302 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17305 /* If we do not already have a type-specifier, assume we are looking
17306 at a simple-type-specifier. */
17307 type_spec
= cp_parser_simple_type_specifier (parser
,
17311 /* If we didn't find a type-specifier, and a type-specifier was not
17312 optional in this context, issue an error message. */
17313 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17315 cp_parser_error (parser
, "expected type specifier");
17316 return error_mark_node
;
17322 /* Parse a simple-type-specifier.
17324 simple-type-specifier:
17325 :: [opt] nested-name-specifier [opt] type-name
17326 :: [opt] nested-name-specifier template template-id
17341 simple-type-specifier:
17343 decltype ( expression )
17346 __underlying_type ( type-id )
17350 nested-name-specifier(opt) template-name
17354 simple-type-specifier:
17356 __typeof__ unary-expression
17357 __typeof__ ( type-id )
17358 __typeof__ ( type-id ) { initializer-list , [opt] }
17360 Concepts Extension:
17362 simple-type-specifier:
17363 constrained-type-specifier
17365 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17366 appropriately updated. */
17369 cp_parser_simple_type_specifier (cp_parser
* parser
,
17370 cp_decl_specifier_seq
*decl_specs
,
17371 cp_parser_flags flags
)
17373 tree type
= NULL_TREE
;
17377 /* Peek at the next token. */
17378 token
= cp_lexer_peek_token (parser
->lexer
);
17380 /* If we're looking at a keyword, things are easy. */
17381 switch (token
->keyword
)
17385 decl_specs
->explicit_char_p
= true;
17386 type
= char_type_node
;
17389 type
= char16_type_node
;
17392 type
= char32_type_node
;
17395 type
= wchar_type_node
;
17398 type
= boolean_type_node
;
17401 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17402 type
= short_integer_type_node
;
17406 decl_specs
->explicit_int_p
= true;
17407 type
= integer_type_node
;
17413 idx
= token
->keyword
- RID_INT_N_0
;
17414 if (! int_n_enabled_p
[idx
])
17418 decl_specs
->explicit_intN_p
= true;
17419 decl_specs
->int_n_idx
= idx
;
17421 type
= int_n_trees
[idx
].signed_type
;
17425 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17426 type
= long_integer_type_node
;
17429 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17430 type
= integer_type_node
;
17433 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17434 type
= unsigned_type_node
;
17437 type
= float_type_node
;
17440 type
= double_type_node
;
17443 type
= void_type_node
;
17447 maybe_warn_cpp0x (CPP0X_AUTO
);
17448 if (parser
->auto_is_implicit_function_template_parm_p
)
17450 /* The 'auto' might be the placeholder return type for a function decl
17451 with trailing return type. */
17452 bool have_trailing_return_fn_decl
= false;
17454 cp_parser_parse_tentatively (parser
);
17455 cp_lexer_consume_token (parser
->lexer
);
17456 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17457 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17458 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17459 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17463 cp_lexer_consume_token (parser
->lexer
);
17464 cp_parser_skip_to_closing_parenthesis (parser
,
17465 /*recovering*/false,
17467 /*consume_paren*/true);
17471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17473 have_trailing_return_fn_decl
= true;
17477 cp_lexer_consume_token (parser
->lexer
);
17479 cp_parser_abort_tentative_parse (parser
);
17481 if (have_trailing_return_fn_decl
)
17483 type
= make_auto ();
17487 if (cxx_dialect
>= cxx14
)
17489 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17490 type
= TREE_TYPE (type
);
17493 type
= error_mark_node
;
17495 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17497 if (cxx_dialect
< cxx14
)
17498 error_at (token
->location
,
17499 "use of %<auto%> in lambda parameter declaration "
17500 "only available with "
17501 "-std=c++14 or -std=gnu++14");
17503 else if (cxx_dialect
< cxx14
)
17504 error_at (token
->location
,
17505 "use of %<auto%> in parameter declaration "
17506 "only available with "
17507 "-std=c++14 or -std=gnu++14");
17508 else if (!flag_concepts
)
17509 pedwarn (token
->location
, 0,
17510 "use of %<auto%> in parameter declaration "
17511 "only available with -fconcepts");
17514 type
= make_auto ();
17518 /* Since DR 743, decltype can either be a simple-type-specifier by
17519 itself or begin a nested-name-specifier. Parsing it will replace
17520 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17521 handling below decide what to do. */
17522 cp_parser_decltype (parser
);
17523 cp_lexer_set_token_position (parser
->lexer
, token
);
17527 /* Consume the `typeof' token. */
17528 cp_lexer_consume_token (parser
->lexer
);
17529 /* Parse the operand to `typeof'. */
17530 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17531 /* If it is not already a TYPE, take its type. */
17532 if (!TYPE_P (type
))
17533 type
= finish_typeof (type
);
17536 cp_parser_set_decl_spec_type (decl_specs
, type
,
17538 /*type_definition_p=*/false);
17542 case RID_UNDERLYING_TYPE
:
17543 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17545 cp_parser_set_decl_spec_type (decl_specs
, type
,
17547 /*type_definition_p=*/false);
17552 case RID_DIRECT_BASES
:
17553 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17555 cp_parser_set_decl_spec_type (decl_specs
, type
,
17557 /*type_definition_p=*/false);
17563 /* If token is an already-parsed decltype not followed by ::,
17564 it's a simple-type-specifier. */
17565 if (token
->type
== CPP_DECLTYPE
17566 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17568 type
= saved_checks_value (token
->u
.tree_check_value
);
17571 cp_parser_set_decl_spec_type (decl_specs
, type
,
17573 /*type_definition_p=*/false);
17574 /* Remember that we are handling a decltype in order to
17575 implement the resolution of DR 1510 when the argument
17576 isn't instantiation dependent. */
17577 decl_specs
->decltype_p
= true;
17579 cp_lexer_consume_token (parser
->lexer
);
17583 /* If the type-specifier was for a built-in type, we're done. */
17586 /* Record the type. */
17588 && (token
->keyword
!= RID_SIGNED
17589 && token
->keyword
!= RID_UNSIGNED
17590 && token
->keyword
!= RID_SHORT
17591 && token
->keyword
!= RID_LONG
))
17592 cp_parser_set_decl_spec_type (decl_specs
,
17595 /*type_definition_p=*/false);
17597 decl_specs
->any_specifiers_p
= true;
17599 /* Consume the token. */
17600 cp_lexer_consume_token (parser
->lexer
);
17602 if (type
== error_mark_node
)
17603 return error_mark_node
;
17605 /* There is no valid C++ program where a non-template type is
17606 followed by a "<". That usually indicates that the user thought
17607 that the type was a template. */
17608 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17611 return TYPE_NAME (type
);
17614 /* The type-specifier must be a user-defined type. */
17615 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17620 /* Don't gobble tokens or issue error messages if this is an
17621 optional type-specifier. */
17622 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17623 cp_parser_parse_tentatively (parser
);
17625 token
= cp_lexer_peek_token (parser
->lexer
);
17627 /* Look for the optional `::' operator. */
17629 = (cp_parser_global_scope_opt (parser
,
17630 /*current_scope_valid_p=*/false)
17632 /* Look for the nested-name specifier. */
17634 = (cp_parser_nested_name_specifier_opt (parser
,
17635 /*typename_keyword_p=*/false,
17636 /*check_dependency_p=*/true,
17638 /*is_declaration=*/false)
17640 /* If we have seen a nested-name-specifier, and the next token
17641 is `template', then we are using the template-id production. */
17643 && cp_parser_optional_template_keyword (parser
))
17645 /* Look for the template-id. */
17646 type
= cp_parser_template_id (parser
,
17647 /*template_keyword_p=*/true,
17648 /*check_dependency_p=*/true,
17650 /*is_declaration=*/false);
17651 /* If the template-id did not name a type, we are out of
17653 if (TREE_CODE (type
) != TYPE_DECL
)
17655 cp_parser_error (parser
, "expected template-id for type");
17659 /* Otherwise, look for a type-name. */
17661 type
= cp_parser_type_name (parser
);
17662 /* Keep track of all name-lookups performed in class scopes. */
17666 && TREE_CODE (type
) == TYPE_DECL
17667 && identifier_p (DECL_NAME (type
)))
17668 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17669 /* If it didn't work out, we don't have a TYPE. */
17670 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17671 && !cp_parser_parse_definitely (parser
))
17673 if (!type
&& cxx_dialect
>= cxx17
)
17675 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17676 cp_parser_parse_tentatively (parser
);
17678 cp_parser_global_scope_opt (parser
,
17679 /*current_scope_valid_p=*/false);
17680 cp_parser_nested_name_specifier_opt (parser
,
17681 /*typename_keyword_p=*/false,
17682 /*check_dependency_p=*/true,
17684 /*is_declaration=*/false);
17685 tree name
= cp_parser_identifier (parser
);
17686 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17687 && parser
->scope
!= error_mark_node
)
17689 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17691 /*is_template=*/false,
17692 /*is_namespace=*/false,
17693 /*check_dependency=*/true,
17694 /*ambiguous_decls=*/NULL
,
17696 if (tmpl
&& tmpl
!= error_mark_node
17697 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17698 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17699 type
= make_template_placeholder (tmpl
);
17702 type
= error_mark_node
;
17703 if (!cp_parser_simulate_error (parser
))
17704 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17705 NLE_TYPE
, token
->location
);
17709 type
= error_mark_node
;
17711 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17712 && !cp_parser_parse_definitely (parser
))
17715 if (type
&& decl_specs
)
17716 cp_parser_set_decl_spec_type (decl_specs
, type
,
17718 /*type_definition_p=*/false);
17721 /* If we didn't get a type-name, issue an error message. */
17722 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17724 cp_parser_error (parser
, "expected type-name");
17725 return error_mark_node
;
17728 if (type
&& type
!= error_mark_node
)
17730 /* See if TYPE is an Objective-C type, and if so, parse and
17731 accept any protocol references following it. Do this before
17732 the cp_parser_check_for_invalid_template_id() call, because
17733 Objective-C types can be followed by '<...>' which would
17734 enclose protocol names rather than template arguments, and so
17735 everything is fine. */
17736 if (c_dialect_objc () && !parser
->scope
17737 && (objc_is_id (type
) || objc_is_class_name (type
)))
17739 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17740 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17742 /* Clobber the "unqualified" type previously entered into
17743 DECL_SPECS with the new, improved protocol-qualified version. */
17745 decl_specs
->type
= qual_type
;
17750 /* There is no valid C++ program where a non-template type is
17751 followed by a "<". That usually indicates that the user
17752 thought that the type was a template. */
17753 cp_parser_check_for_invalid_template_id (parser
, type
,
17761 /* Parse a type-name.
17767 simple-template-id [in c++0x]
17784 Returns a TYPE_DECL for the type. */
17787 cp_parser_type_name (cp_parser
* parser
)
17789 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17794 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17798 /* We can't know yet whether it is a class-name or not. */
17799 cp_parser_parse_tentatively (parser
);
17800 /* Try a class-name. */
17801 type_decl
= cp_parser_class_name (parser
,
17802 typename_keyword_p
,
17803 /*template_keyword_p=*/false,
17805 /*check_dependency_p=*/true,
17806 /*class_head_p=*/false,
17807 /*is_declaration=*/false);
17808 /* If it's not a class-name, keep looking. */
17809 if (!cp_parser_parse_definitely (parser
))
17811 if (cxx_dialect
< cxx11
)
17812 /* It must be a typedef-name or an enum-name. */
17813 return cp_parser_nonclass_name (parser
);
17815 cp_parser_parse_tentatively (parser
);
17816 /* It is either a simple-template-id representing an
17817 instantiation of an alias template... */
17818 type_decl
= cp_parser_template_id (parser
,
17819 /*template_keyword_p=*/false,
17820 /*check_dependency_p=*/true,
17822 /*is_declaration=*/false);
17823 /* Note that this must be an instantiation of an alias template
17824 because [temp.names]/6 says:
17826 A template-id that names an alias template specialization
17829 Whereas [temp.names]/7 says:
17831 A simple-template-id that names a class template
17832 specialization is a class-name.
17834 With concepts, this could also be a partial-concept-id that
17835 declares a non-type template parameter. */
17836 if (type_decl
!= NULL_TREE
17837 && TREE_CODE (type_decl
) == TYPE_DECL
17838 && TYPE_DECL_ALIAS_P (type_decl
))
17839 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17840 else if (is_constrained_parameter (type_decl
))
17841 /* Don't do anything. */ ;
17843 cp_parser_simulate_error (parser
);
17845 if (!cp_parser_parse_definitely (parser
))
17846 /* ... Or a typedef-name or an enum-name. */
17847 return cp_parser_nonclass_name (parser
);
17853 /* Check if DECL and ARGS can form a constrained-type-specifier.
17854 If ARGS is non-null, we try to form a concept check of the
17855 form DECL<?, ARGS> where ? is a wildcard that matches any
17856 kind of template argument. If ARGS is NULL, then we try to
17857 form a concept check of the form DECL<?>. */
17860 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17861 tree decl
, tree args
)
17863 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17865 /* If we a constrained-type-specifier cannot be deduced. */
17866 if (parser
->prevent_constrained_type_specifiers
)
17869 /* A constrained type specifier can only be found in an
17870 overload set or as a reference to a template declaration.
17872 FIXME: This might be masking a bug. It's possible that
17873 that the deduction below is causing template specializations
17874 to be formed with the wildcard as an argument. */
17875 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17878 /* Try to build a call expression that evaluates the
17879 concept. This can fail if the overload set refers
17880 only to non-templates. */
17881 tree placeholder
= build_nt (WILDCARD_DECL
);
17882 tree check
= build_concept_check (decl
, placeholder
, args
);
17883 if (check
== error_mark_node
)
17886 /* Deduce the checked constraint and the prototype parameter.
17888 FIXME: In certain cases, failure to deduce should be a
17889 diagnosable error. */
17892 if (!deduce_constrained_parameter (check
, conc
, proto
))
17895 /* In template parameter scope, this results in a constrained
17896 parameter. Return a descriptor of that parm. */
17897 if (processing_template_parmlist
)
17898 return build_constrained_parameter (conc
, proto
, args
);
17900 /* In a parameter-declaration-clause, constrained-type
17901 specifiers result in invented template parameters. */
17902 if (parser
->auto_is_implicit_function_template_parm_p
)
17904 tree x
= build_constrained_parameter (conc
, proto
, args
);
17905 return synthesize_implicit_template_parm (parser
, x
);
17909 /* Otherwise, we're in a context where the constrained
17910 type name is deduced and the constraint applies
17911 after deduction. */
17912 return make_constrained_auto (conc
, args
);
17918 /* If DECL refers to a concept, return a TYPE_DECL representing
17919 the result of using the constrained type specifier in the
17920 current context. DECL refers to a concept if
17922 - it is an overload set containing a function concept taking a single
17925 - it is a variable concept taking a single type argument. */
17928 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17931 && (TREE_CODE (decl
) == OVERLOAD
17932 || BASELINK_P (decl
)
17933 || variable_concept_p (decl
)))
17934 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17939 /* Check if DECL and ARGS form a partial-concept-id. If so,
17940 assign ID to the resulting constrained placeholder.
17942 Returns true if the partial-concept-id designates a placeholder
17943 and false otherwise. Note that *id is set to NULL_TREE in
17947 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17949 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17952 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17964 Returns a TYPE_DECL for the type. */
17967 cp_parser_nonclass_name (cp_parser
* parser
)
17972 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17973 identifier
= cp_parser_identifier (parser
);
17974 if (identifier
== error_mark_node
)
17975 return error_mark_node
;
17977 /* Look up the type-name. */
17978 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17980 type_decl
= strip_using_decl (type_decl
);
17982 /* If we found an overload set, then it may refer to a concept-name. */
17983 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17986 if (TREE_CODE (type_decl
) != TYPE_DECL
17987 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17989 /* See if this is an Objective-C type. */
17990 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17991 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17993 type_decl
= TYPE_NAME (type
);
17996 /* Issue an error if we did not find a type-name. */
17997 if (TREE_CODE (type_decl
) != TYPE_DECL
17998 /* In Objective-C, we have the complication that class names are
17999 normally type names and start declarations (eg, the
18000 "NSObject" in "NSObject *object;"), but can be used in an
18001 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18002 is an expression. So, a classname followed by a dot is not a
18003 valid type-name. */
18004 || (objc_is_class_name (TREE_TYPE (type_decl
))
18005 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18007 if (!cp_parser_simulate_error (parser
))
18008 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18009 NLE_TYPE
, token
->location
);
18010 return error_mark_node
;
18012 /* Remember that the name was used in the definition of the
18013 current class so that we can check later to see if the
18014 meaning would have been different after the class was
18015 entirely defined. */
18016 else if (type_decl
!= error_mark_node
18018 maybe_note_name_used_in_class (identifier
, type_decl
);
18023 /* Parse an elaborated-type-specifier. Note that the grammar given
18024 here incorporates the resolution to DR68.
18026 elaborated-type-specifier:
18027 class-key :: [opt] nested-name-specifier [opt] identifier
18028 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18029 enum-key :: [opt] nested-name-specifier [opt] identifier
18030 typename :: [opt] nested-name-specifier identifier
18031 typename :: [opt] nested-name-specifier template [opt]
18036 elaborated-type-specifier:
18037 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18038 class-key attributes :: [opt] nested-name-specifier [opt]
18039 template [opt] template-id
18040 enum attributes :: [opt] nested-name-specifier [opt] identifier
18042 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18043 declared `friend'. If IS_DECLARATION is TRUE, then this
18044 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18045 something is being declared.
18047 Returns the TYPE specified. */
18050 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18052 bool is_declaration
)
18054 enum tag_types tag_type
;
18056 tree type
= NULL_TREE
;
18057 tree attributes
= NULL_TREE
;
18059 cp_token
*token
= NULL
;
18061 /* See if we're looking at the `enum' keyword. */
18062 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18064 /* Consume the `enum' token. */
18065 cp_lexer_consume_token (parser
->lexer
);
18066 /* Remember that it's an enumeration type. */
18067 tag_type
= enum_type
;
18068 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18069 enums) is used here. */
18070 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18071 if (cp_parser_is_keyword (token
, RID_CLASS
)
18072 || cp_parser_is_keyword (token
, RID_STRUCT
))
18074 gcc_rich_location
richloc (token
->location
);
18075 richloc
.add_range (input_location
);
18076 richloc
.add_fixit_remove ();
18077 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18078 "a scoped enum must not use the %qD keyword",
18080 /* Consume the `struct' or `class' and parse it anyway. */
18081 cp_lexer_consume_token (parser
->lexer
);
18083 /* Parse the attributes. */
18084 attributes
= cp_parser_attributes_opt (parser
);
18086 /* Or, it might be `typename'. */
18087 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18090 /* Consume the `typename' token. */
18091 cp_lexer_consume_token (parser
->lexer
);
18092 /* Remember that it's a `typename' type. */
18093 tag_type
= typename_type
;
18095 /* Otherwise it must be a class-key. */
18098 tag_type
= cp_parser_class_key (parser
);
18099 if (tag_type
== none_type
)
18100 return error_mark_node
;
18101 /* Parse the attributes. */
18102 attributes
= cp_parser_attributes_opt (parser
);
18105 /* Look for the `::' operator. */
18106 globalscope
= cp_parser_global_scope_opt (parser
,
18107 /*current_scope_valid_p=*/false);
18108 /* Look for the nested-name-specifier. */
18109 tree nested_name_specifier
;
18110 if (tag_type
== typename_type
&& !globalscope
)
18112 nested_name_specifier
18113 = cp_parser_nested_name_specifier (parser
,
18114 /*typename_keyword_p=*/true,
18115 /*check_dependency_p=*/true,
18118 if (!nested_name_specifier
)
18119 return error_mark_node
;
18122 /* Even though `typename' is not present, the proposed resolution
18123 to Core Issue 180 says that in `class A<T>::B', `B' should be
18124 considered a type-name, even if `A<T>' is dependent. */
18125 nested_name_specifier
18126 = cp_parser_nested_name_specifier_opt (parser
,
18127 /*typename_keyword_p=*/true,
18128 /*check_dependency_p=*/true,
18131 /* For everything but enumeration types, consider a template-id.
18132 For an enumeration type, consider only a plain identifier. */
18133 if (tag_type
!= enum_type
)
18135 bool template_p
= false;
18138 /* Allow the `template' keyword. */
18139 template_p
= cp_parser_optional_template_keyword (parser
);
18140 /* If we didn't see `template', we don't know if there's a
18141 template-id or not. */
18143 cp_parser_parse_tentatively (parser
);
18144 /* The `template' keyword must follow a nested-name-specifier. */
18145 else if (!nested_name_specifier
)
18147 cp_parser_error (parser
, "%<template%> must follow a nested-"
18149 return error_mark_node
;
18152 /* Parse the template-id. */
18153 token
= cp_lexer_peek_token (parser
->lexer
);
18154 decl
= cp_parser_template_id (parser
, template_p
,
18155 /*check_dependency_p=*/true,
18158 /* If we didn't find a template-id, look for an ordinary
18160 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18162 /* We can get here when cp_parser_template_id, called by
18163 cp_parser_class_name with tag_type == none_type, succeeds
18164 and caches a BASELINK. Then, when called again here,
18165 instead of failing and returning an error_mark_node
18166 returns it (see template/typename17.C in C++11).
18167 ??? Could we diagnose this earlier? */
18168 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18170 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18171 type
= error_mark_node
;
18173 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18174 in effect, then we must assume that, upon instantiation, the
18175 template will correspond to a class. */
18176 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18177 && tag_type
== typename_type
)
18178 type
= make_typename_type (parser
->scope
, decl
,
18180 /*complain=*/tf_error
);
18181 /* If the `typename' keyword is in effect and DECL is not a type
18182 decl, then type is non existent. */
18183 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18185 else if (TREE_CODE (decl
) == TYPE_DECL
)
18187 type
= check_elaborated_type_specifier (tag_type
, decl
,
18188 /*allow_template_p=*/true);
18190 /* If the next token is a semicolon, this must be a specialization,
18191 instantiation, or friend declaration. Check the scope while we
18192 still know whether or not we had a nested-name-specifier. */
18193 if (type
!= error_mark_node
18194 && !nested_name_specifier
&& !is_friend
18195 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18196 check_unqualified_spec_or_inst (type
, token
->location
);
18198 else if (decl
== error_mark_node
)
18199 type
= error_mark_node
;
18204 token
= cp_lexer_peek_token (parser
->lexer
);
18205 identifier
= cp_parser_identifier (parser
);
18207 if (identifier
== error_mark_node
)
18209 parser
->scope
= NULL_TREE
;
18210 return error_mark_node
;
18213 /* For a `typename', we needn't call xref_tag. */
18214 if (tag_type
== typename_type
18215 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18216 return cp_parser_make_typename_type (parser
, identifier
,
18219 /* Template parameter lists apply only if we are not within a
18220 function parameter list. */
18221 bool template_parm_lists_apply
18222 = parser
->num_template_parameter_lists
;
18223 if (template_parm_lists_apply
)
18224 for (cp_binding_level
*s
= current_binding_level
;
18225 s
&& s
->kind
!= sk_template_parms
;
18226 s
= s
->level_chain
)
18227 if (s
->kind
== sk_function_parms
)
18228 template_parm_lists_apply
= false;
18230 /* Look up a qualified name in the usual way. */
18234 tree ambiguous_decls
;
18236 decl
= cp_parser_lookup_name (parser
, identifier
,
18238 /*is_template=*/false,
18239 /*is_namespace=*/false,
18240 /*check_dependency=*/true,
18244 /* If the lookup was ambiguous, an error will already have been
18246 if (ambiguous_decls
)
18247 return error_mark_node
;
18249 /* If we are parsing friend declaration, DECL may be a
18250 TEMPLATE_DECL tree node here. However, we need to check
18251 whether this TEMPLATE_DECL results in valid code. Consider
18252 the following example:
18255 template <class T> class C {};
18258 template <class T> friend class N::C; // #1, valid code
18260 template <class T> class Y {
18261 friend class N::C; // #2, invalid code
18264 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18265 name lookup of `N::C'. We see that friend declaration must
18266 be template for the code to be valid. Note that
18267 processing_template_decl does not work here since it is
18268 always 1 for the above two cases. */
18270 decl
= (cp_parser_maybe_treat_template_as_class
18271 (decl
, /*tag_name_p=*/is_friend
18272 && template_parm_lists_apply
));
18274 if (TREE_CODE (decl
) != TYPE_DECL
)
18276 cp_parser_diagnose_invalid_type_name (parser
,
18279 return error_mark_node
;
18282 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18284 bool allow_template
= (template_parm_lists_apply
18285 || DECL_SELF_REFERENCE_P (decl
));
18286 type
= check_elaborated_type_specifier (tag_type
, decl
,
18289 if (type
== error_mark_node
)
18290 return error_mark_node
;
18293 /* Forward declarations of nested types, such as
18298 are invalid unless all components preceding the final '::'
18299 are complete. If all enclosing types are complete, these
18300 declarations become merely pointless.
18302 Invalid forward declarations of nested types are errors
18303 caught elsewhere in parsing. Those that are pointless arrive
18306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18307 && !is_friend
&& !processing_explicit_instantiation
)
18308 warning (0, "declaration %qD does not declare anything", decl
);
18310 type
= TREE_TYPE (decl
);
18314 /* An elaborated-type-specifier sometimes introduces a new type and
18315 sometimes names an existing type. Normally, the rule is that it
18316 introduces a new type only if there is not an existing type of
18317 the same name already in scope. For example, given:
18320 void f() { struct S s; }
18322 the `struct S' in the body of `f' is the same `struct S' as in
18323 the global scope; the existing definition is used. However, if
18324 there were no global declaration, this would introduce a new
18325 local class named `S'.
18327 An exception to this rule applies to the following code:
18329 namespace N { struct S; }
18331 Here, the elaborated-type-specifier names a new type
18332 unconditionally; even if there is already an `S' in the
18333 containing scope this declaration names a new type.
18334 This exception only applies if the elaborated-type-specifier
18335 forms the complete declaration:
18339 A declaration consisting solely of `class-key identifier ;' is
18340 either a redeclaration of the name in the current scope or a
18341 forward declaration of the identifier as a class name. It
18342 introduces the name into the current scope.
18344 We are in this situation precisely when the next token is a `;'.
18346 An exception to the exception is that a `friend' declaration does
18347 *not* name a new type; i.e., given:
18349 struct S { friend struct T; };
18351 `T' is not a new type in the scope of `S'.
18353 Also, `new struct S' or `sizeof (struct S)' never results in the
18354 definition of a new type; a new type can only be declared in a
18355 declaration context. */
18361 /* Friends have special name lookup rules. */
18362 ts
= ts_within_enclosing_non_class
;
18363 else if (is_declaration
18364 && cp_lexer_next_token_is (parser
->lexer
,
18366 /* This is a `class-key identifier ;' */
18372 (template_parm_lists_apply
18373 && (cp_parser_next_token_starts_class_definition_p (parser
)
18374 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18375 /* An unqualified name was used to reference this type, so
18376 there were no qualifying templates. */
18377 if (template_parm_lists_apply
18378 && !cp_parser_check_template_parameters (parser
,
18379 /*num_templates=*/0,
18380 /*template_id*/false,
18382 /*declarator=*/NULL
))
18383 return error_mark_node
;
18384 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18388 if (type
== error_mark_node
)
18389 return error_mark_node
;
18391 /* Allow attributes on forward declarations of classes. */
18394 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18395 warning (OPT_Wattributes
,
18396 "attributes ignored on uninstantiated type");
18397 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18398 && ! processing_explicit_instantiation
)
18399 warning (OPT_Wattributes
,
18400 "attributes ignored on template instantiation");
18401 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18402 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18404 warning (OPT_Wattributes
,
18405 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18408 if (tag_type
!= enum_type
)
18410 /* Indicate whether this class was declared as a `class' or as a
18412 if (CLASS_TYPE_P (type
))
18413 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18414 cp_parser_check_class_key (tag_type
, type
);
18417 /* A "<" cannot follow an elaborated type specifier. If that
18418 happens, the user was probably trying to form a template-id. */
18419 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18425 /* Parse an enum-specifier.
18428 enum-head { enumerator-list [opt] }
18429 enum-head { enumerator-list , } [C++0x]
18432 enum-key identifier [opt] enum-base [opt]
18433 enum-key nested-name-specifier identifier enum-base [opt]
18438 enum struct [C++0x]
18441 : type-specifier-seq
18443 opaque-enum-specifier:
18444 enum-key identifier enum-base [opt] ;
18447 enum-key attributes[opt] identifier [opt] enum-base [opt]
18448 { enumerator-list [opt] }attributes[opt]
18449 enum-key attributes[opt] identifier [opt] enum-base [opt]
18450 { enumerator-list, }attributes[opt] [C++0x]
18452 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18453 if the token stream isn't an enum-specifier after all. */
18456 cp_parser_enum_specifier (cp_parser
* parser
)
18459 tree type
= NULL_TREE
;
18461 tree nested_name_specifier
= NULL_TREE
;
18463 bool scoped_enum_p
= false;
18464 bool has_underlying_type
= false;
18465 bool nested_being_defined
= false;
18466 bool new_value_list
= false;
18467 bool is_new_type
= false;
18468 bool is_unnamed
= false;
18469 tree underlying_type
= NULL_TREE
;
18470 cp_token
*type_start_token
= NULL
;
18471 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18473 parser
->colon_corrects_to_scope_p
= false;
18475 /* Parse tentatively so that we can back up if we don't find a
18477 cp_parser_parse_tentatively (parser
);
18479 /* Caller guarantees that the current token is 'enum', an identifier
18480 possibly follows, and the token after that is an opening brace.
18481 If we don't have an identifier, fabricate an anonymous name for
18482 the enumeration being defined. */
18483 cp_lexer_consume_token (parser
->lexer
);
18485 /* Parse the "class" or "struct", which indicates a scoped
18486 enumeration type in C++0x. */
18487 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18488 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18490 if (cxx_dialect
< cxx11
)
18491 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18493 /* Consume the `struct' or `class' token. */
18494 cp_lexer_consume_token (parser
->lexer
);
18496 scoped_enum_p
= true;
18499 attributes
= cp_parser_attributes_opt (parser
);
18501 /* Clear the qualification. */
18502 parser
->scope
= NULL_TREE
;
18503 parser
->qualifying_scope
= NULL_TREE
;
18504 parser
->object_scope
= NULL_TREE
;
18506 /* Figure out in what scope the declaration is being placed. */
18507 prev_scope
= current_scope ();
18509 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18511 push_deferring_access_checks (dk_no_check
);
18512 nested_name_specifier
18513 = cp_parser_nested_name_specifier_opt (parser
,
18514 /*typename_keyword_p=*/true,
18515 /*check_dependency_p=*/false,
18517 /*is_declaration=*/false);
18519 if (nested_name_specifier
)
18523 identifier
= cp_parser_identifier (parser
);
18524 name
= cp_parser_lookup_name (parser
, identifier
,
18526 /*is_template=*/false,
18527 /*is_namespace=*/false,
18528 /*check_dependency=*/true,
18529 /*ambiguous_decls=*/NULL
,
18531 if (name
&& name
!= error_mark_node
)
18533 type
= TREE_TYPE (name
);
18534 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18536 /* Are template enums allowed in ISO? */
18537 if (template_parm_scope_p ())
18538 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18539 "%qD is an enumeration template", name
);
18540 /* ignore a typename reference, for it will be solved by name
18545 else if (nested_name_specifier
== error_mark_node
)
18546 /* We already issued an error. */;
18549 error_at (type_start_token
->location
,
18550 "%qD does not name an enumeration in %qT",
18551 identifier
, nested_name_specifier
);
18552 nested_name_specifier
= error_mark_node
;
18557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18558 identifier
= cp_parser_identifier (parser
);
18561 identifier
= make_anon_name ();
18564 error_at (type_start_token
->location
,
18565 "unnamed scoped enum is not allowed");
18568 pop_deferring_access_checks ();
18570 /* Check for the `:' that denotes a specified underlying type in C++0x.
18571 Note that a ':' could also indicate a bitfield width, however. */
18572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18574 cp_decl_specifier_seq type_specifiers
;
18576 /* Consume the `:'. */
18577 cp_lexer_consume_token (parser
->lexer
);
18579 /* Parse the type-specifier-seq. */
18580 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18581 /*is_trailing_return=*/false,
18584 /* At this point this is surely not elaborated type specifier. */
18585 if (!cp_parser_parse_definitely (parser
))
18588 if (cxx_dialect
< cxx11
)
18589 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18591 has_underlying_type
= true;
18593 /* If that didn't work, stop. */
18594 if (type_specifiers
.type
!= error_mark_node
)
18596 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18597 /*initialized=*/0, NULL
);
18598 if (underlying_type
== error_mark_node
18599 || check_for_bare_parameter_packs (underlying_type
))
18600 underlying_type
= NULL_TREE
;
18604 /* Look for the `{' but don't consume it yet. */
18605 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18607 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18609 cp_parser_error (parser
, "expected %<{%>");
18610 if (has_underlying_type
)
18616 /* An opaque-enum-specifier must have a ';' here. */
18617 if ((scoped_enum_p
|| underlying_type
)
18618 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18620 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18621 if (has_underlying_type
)
18629 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18632 if (nested_name_specifier
)
18634 if (CLASS_TYPE_P (nested_name_specifier
))
18636 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18637 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18638 push_scope (nested_name_specifier
);
18640 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18642 push_nested_namespace (nested_name_specifier
);
18646 /* Issue an error message if type-definitions are forbidden here. */
18647 if (!cp_parser_check_type_definition (parser
))
18648 type
= error_mark_node
;
18650 /* Create the new type. We do this before consuming the opening
18651 brace so the enum will be recorded as being on the line of its
18652 tag (or the 'enum' keyword, if there is no tag). */
18653 type
= start_enum (identifier
, type
, underlying_type
,
18654 attributes
, scoped_enum_p
, &is_new_type
);
18656 /* If the next token is not '{' it is an opaque-enum-specifier or an
18657 elaborated-type-specifier. */
18658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18660 timevar_push (TV_PARSE_ENUM
);
18661 if (nested_name_specifier
18662 && nested_name_specifier
!= error_mark_node
)
18664 /* The following catches invalid code such as:
18665 enum class S<int>::E { A, B, C }; */
18666 if (!processing_specialization
18667 && CLASS_TYPE_P (nested_name_specifier
)
18668 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18669 error_at (type_start_token
->location
, "cannot add an enumerator "
18670 "list to a template instantiation");
18672 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18674 error_at (type_start_token
->location
,
18675 "%<%T::%E%> has not been declared",
18676 TYPE_CONTEXT (nested_name_specifier
),
18677 nested_name_specifier
);
18678 type
= error_mark_node
;
18680 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18681 && !CLASS_TYPE_P (nested_name_specifier
))
18683 error_at (type_start_token
->location
, "nested name specifier "
18684 "%qT for enum declaration does not name a class "
18685 "or namespace", nested_name_specifier
);
18686 type
= error_mark_node
;
18688 /* If that scope does not contain the scope in which the
18689 class was originally declared, the program is invalid. */
18690 else if (prev_scope
&& !is_ancestor (prev_scope
,
18691 nested_name_specifier
))
18693 if (at_namespace_scope_p ())
18694 error_at (type_start_token
->location
,
18695 "declaration of %qD in namespace %qD which does not "
18697 type
, prev_scope
, nested_name_specifier
);
18699 error_at (type_start_token
->location
,
18700 "declaration of %qD in %qD which does not "
18702 type
, prev_scope
, nested_name_specifier
);
18703 type
= error_mark_node
;
18705 /* If that scope is the scope where the declaration is being placed
18706 the program is invalid. */
18707 else if (CLASS_TYPE_P (nested_name_specifier
)
18708 && CLASS_TYPE_P (prev_scope
)
18709 && same_type_p (nested_name_specifier
, prev_scope
))
18711 permerror (type_start_token
->location
,
18712 "extra qualification not allowed");
18713 nested_name_specifier
= NULL_TREE
;
18718 begin_scope (sk_scoped_enum
, type
);
18720 /* Consume the opening brace. */
18721 matching_braces braces
;
18722 braces
.consume_open (parser
);
18724 if (type
== error_mark_node
)
18725 ; /* Nothing to add */
18726 else if (OPAQUE_ENUM_P (type
)
18727 || (cxx_dialect
> cxx98
&& processing_specialization
))
18729 new_value_list
= true;
18730 SET_OPAQUE_ENUM_P (type
, false);
18731 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18735 error_at (type_start_token
->location
,
18736 "multiple definition of %q#T", type
);
18737 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18738 "previous definition here");
18739 type
= error_mark_node
;
18742 if (type
== error_mark_node
)
18743 cp_parser_skip_to_end_of_block_or_statement (parser
);
18744 /* If the next token is not '}', then there are some enumerators. */
18745 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18747 if (is_unnamed
&& !scoped_enum_p
)
18748 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18749 "ISO C++ forbids empty unnamed enum");
18752 cp_parser_enumerator_list (parser
, type
);
18754 /* Consume the final '}'. */
18755 braces
.require_close (parser
);
18759 timevar_pop (TV_PARSE_ENUM
);
18763 /* If a ';' follows, then it is an opaque-enum-specifier
18764 and additional restrictions apply. */
18765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18768 error_at (type_start_token
->location
,
18769 "opaque-enum-specifier without name");
18770 else if (nested_name_specifier
)
18771 error_at (type_start_token
->location
,
18772 "opaque-enum-specifier must use a simple identifier");
18776 /* Look for trailing attributes to apply to this enumeration, and
18777 apply them if appropriate. */
18778 if (cp_parser_allow_gnu_extensions_p (parser
))
18780 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18781 cplus_decl_attributes (&type
,
18783 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18786 /* Finish up the enumeration. */
18787 if (type
!= error_mark_node
)
18789 if (new_value_list
)
18790 finish_enum_value_list (type
);
18792 finish_enum (type
);
18795 if (nested_name_specifier
)
18797 if (CLASS_TYPE_P (nested_name_specifier
))
18799 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18800 pop_scope (nested_name_specifier
);
18802 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18804 pop_nested_namespace (nested_name_specifier
);
18808 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18812 /* Parse an enumerator-list. The enumerators all have the indicated
18816 enumerator-definition
18817 enumerator-list , enumerator-definition */
18820 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18824 /* Parse an enumerator-definition. */
18825 cp_parser_enumerator_definition (parser
, type
);
18827 /* If the next token is not a ',', we've reached the end of
18829 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18831 /* Otherwise, consume the `,' and keep going. */
18832 cp_lexer_consume_token (parser
->lexer
);
18833 /* If the next token is a `}', there is a trailing comma. */
18834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18836 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18837 pedwarn (input_location
, OPT_Wpedantic
,
18838 "comma at end of enumerator list");
18844 /* Parse an enumerator-definition. The enumerator has the indicated
18847 enumerator-definition:
18849 enumerator = constant-expression
18856 enumerator-definition:
18857 enumerator attributes [opt]
18858 enumerator attributes [opt] = constant-expression */
18861 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18867 /* Save the input location because we are interested in the location
18868 of the identifier and not the location of the explicit value. */
18869 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18871 /* Look for the identifier. */
18872 identifier
= cp_parser_identifier (parser
);
18873 if (identifier
== error_mark_node
)
18876 /* Parse any specified attributes. */
18877 tree attrs
= cp_parser_attributes_opt (parser
);
18879 /* If the next token is an '=', then there is an explicit value. */
18880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18882 /* Consume the `=' token. */
18883 cp_lexer_consume_token (parser
->lexer
);
18884 /* Parse the value. */
18885 value
= cp_parser_constant_expression (parser
);
18890 /* If we are processing a template, make sure the initializer of the
18891 enumerator doesn't contain any bare template parameter pack. */
18892 if (check_for_bare_parameter_packs (value
))
18893 value
= error_mark_node
;
18895 /* Create the enumerator. */
18896 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18899 /* Parse a namespace-name.
18902 original-namespace-name
18905 Returns the NAMESPACE_DECL for the namespace. */
18908 cp_parser_namespace_name (cp_parser
* parser
)
18911 tree namespace_decl
;
18913 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18915 /* Get the name of the namespace. */
18916 identifier
= cp_parser_identifier (parser
);
18917 if (identifier
== error_mark_node
)
18918 return error_mark_node
;
18920 /* Look up the identifier in the currently active scope. Look only
18921 for namespaces, due to:
18923 [basic.lookup.udir]
18925 When looking up a namespace-name in a using-directive or alias
18926 definition, only namespace names are considered.
18930 [basic.lookup.qual]
18932 During the lookup of a name preceding the :: scope resolution
18933 operator, object, function, and enumerator names are ignored.
18935 (Note that cp_parser_qualifying_entity only calls this
18936 function if the token after the name is the scope resolution
18938 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18940 /*is_template=*/false,
18941 /*is_namespace=*/true,
18942 /*check_dependency=*/true,
18943 /*ambiguous_decls=*/NULL
,
18945 /* If it's not a namespace, issue an error. */
18946 if (namespace_decl
== error_mark_node
18947 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18949 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18951 auto_diagnostic_group d
;
18953 if (namespace_decl
== error_mark_node
18954 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18955 hint
= suggest_alternative_in_explicit_scope (token
->location
,
18958 if (const char *suggestion
= hint
.suggestion ())
18960 gcc_rich_location
richloc (token
->location
);
18961 richloc
.add_fixit_replace (suggestion
);
18962 error_at (&richloc
,
18963 "%qD is not a namespace-name; did you mean %qs?",
18964 identifier
, suggestion
);
18967 error_at (token
->location
, "%qD is not a namespace-name",
18971 cp_parser_error (parser
, "expected namespace-name");
18972 namespace_decl
= error_mark_node
;
18975 return namespace_decl
;
18978 /* Parse a namespace-definition.
18980 namespace-definition:
18981 named-namespace-definition
18982 unnamed-namespace-definition
18984 named-namespace-definition:
18985 original-namespace-definition
18986 extension-namespace-definition
18988 original-namespace-definition:
18989 namespace identifier { namespace-body }
18991 extension-namespace-definition:
18992 namespace original-namespace-name { namespace-body }
18994 unnamed-namespace-definition:
18995 namespace { namespace-body } */
18998 cp_parser_namespace_definition (cp_parser
* parser
)
19001 int nested_definition_count
= 0;
19003 cp_ensure_no_omp_declare_simd (parser
);
19004 cp_ensure_no_oacc_routine (parser
);
19006 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19010 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19011 cp_lexer_consume_token (parser
->lexer
);
19014 /* Look for the `namespace' keyword. */
19016 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19018 /* Parse any specified attributes before the identifier. */
19019 tree attribs
= cp_parser_attributes_opt (parser
);
19023 identifier
= NULL_TREE
;
19025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19027 identifier
= cp_parser_identifier (parser
);
19029 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19030 pedwarn (input_location
, OPT_Wpedantic
,
19031 "standard attributes on namespaces must precede "
19032 "the namespace name");
19034 /* Parse any attributes specified after the identifier. */
19035 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19038 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19041 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19042 pedwarn (input_location
, OPT_Wpedantic
,
19043 "nested namespace definitions only available with "
19044 "-std=c++17 or -std=gnu++17");
19046 /* Nested namespace names can create new namespaces (unlike
19047 other qualified-ids). */
19048 if (int count
= identifier
? push_namespace (identifier
) : 0)
19049 nested_definition_count
+= count
;
19051 cp_parser_error (parser
, "nested namespace name required");
19052 cp_lexer_consume_token (parser
->lexer
);
19055 if (nested_definition_count
&& !identifier
)
19056 cp_parser_error (parser
, "namespace name required");
19058 if (nested_definition_count
&& attribs
)
19059 error_at (token
->location
,
19060 "a nested namespace definition cannot have attributes");
19061 if (nested_definition_count
&& is_inline
)
19062 error_at (token
->location
,
19063 "a nested namespace definition cannot be inline");
19065 /* Start the namespace. */
19066 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19068 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19070 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19072 /* Look for the `{' to validate starting the namespace. */
19073 matching_braces braces
;
19074 if (braces
.require_open (parser
))
19076 /* Parse the body of the namespace. */
19077 cp_parser_namespace_body (parser
);
19079 /* Look for the final `}'. */
19080 braces
.require_close (parser
);
19083 if (has_visibility
)
19084 pop_visibility (1);
19086 /* Pop the nested namespace definitions. */
19087 while (nested_definition_count
--)
19091 /* Parse a namespace-body.
19094 declaration-seq [opt] */
19097 cp_parser_namespace_body (cp_parser
* parser
)
19099 cp_parser_declaration_seq_opt (parser
);
19102 /* Parse a namespace-alias-definition.
19104 namespace-alias-definition:
19105 namespace identifier = qualified-namespace-specifier ; */
19108 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19111 tree namespace_specifier
;
19113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19115 /* Look for the `namespace' keyword. */
19116 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19117 /* Look for the identifier. */
19118 identifier
= cp_parser_identifier (parser
);
19119 if (identifier
== error_mark_node
)
19121 /* Look for the `=' token. */
19122 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19123 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19125 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19126 /* Skip the definition. */
19127 cp_lexer_consume_token (parser
->lexer
);
19128 if (cp_parser_skip_to_closing_brace (parser
))
19129 cp_lexer_consume_token (parser
->lexer
);
19132 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19133 /* Look for the qualified-namespace-specifier. */
19134 namespace_specifier
19135 = cp_parser_qualified_namespace_specifier (parser
);
19136 /* Look for the `;' token. */
19137 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19139 /* Register the alias in the symbol table. */
19140 do_namespace_alias (identifier
, namespace_specifier
);
19143 /* Parse a qualified-namespace-specifier.
19145 qualified-namespace-specifier:
19146 :: [opt] nested-name-specifier [opt] namespace-name
19148 Returns a NAMESPACE_DECL corresponding to the specified
19152 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19154 /* Look for the optional `::'. */
19155 cp_parser_global_scope_opt (parser
,
19156 /*current_scope_valid_p=*/false);
19158 /* Look for the optional nested-name-specifier. */
19159 cp_parser_nested_name_specifier_opt (parser
,
19160 /*typename_keyword_p=*/false,
19161 /*check_dependency_p=*/true,
19163 /*is_declaration=*/true);
19165 return cp_parser_namespace_name (parser
);
19168 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19169 access declaration.
19172 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19173 using :: unqualified-id ;
19175 access-declaration:
19181 cp_parser_using_declaration (cp_parser
* parser
,
19182 bool access_declaration_p
)
19185 bool typename_p
= false;
19186 bool global_scope_p
;
19190 int oldcount
= errorcount
;
19191 cp_token
*diag_token
= NULL
;
19193 if (access_declaration_p
)
19195 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19196 cp_parser_parse_tentatively (parser
);
19200 /* Look for the `using' keyword. */
19201 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19204 /* Peek at the next token. */
19205 token
= cp_lexer_peek_token (parser
->lexer
);
19206 /* See if it's `typename'. */
19207 if (token
->keyword
== RID_TYPENAME
)
19209 /* Remember that we've seen it. */
19211 /* Consume the `typename' token. */
19212 cp_lexer_consume_token (parser
->lexer
);
19216 /* Look for the optional global scope qualification. */
19218 = (cp_parser_global_scope_opt (parser
,
19219 /*current_scope_valid_p=*/false)
19222 /* If we saw `typename', or didn't see `::', then there must be a
19223 nested-name-specifier present. */
19224 if (typename_p
|| !global_scope_p
)
19226 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19227 /*check_dependency_p=*/true,
19229 /*is_declaration=*/true);
19230 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19232 cp_parser_skip_to_end_of_block_or_statement (parser
);
19236 /* Otherwise, we could be in either of the two productions. In that
19237 case, treat the nested-name-specifier as optional. */
19239 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19240 /*typename_keyword_p=*/false,
19241 /*check_dependency_p=*/true,
19243 /*is_declaration=*/true);
19245 qscope
= global_namespace
;
19246 else if (UNSCOPED_ENUM_P (qscope
))
19247 qscope
= CP_TYPE_CONTEXT (qscope
);
19249 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19250 /* Something has already gone wrong; there's no need to parse
19251 further. Since an error has occurred, the return value of
19252 cp_parser_parse_definitely will be false, as required. */
19253 return cp_parser_parse_definitely (parser
);
19255 token
= cp_lexer_peek_token (parser
->lexer
);
19256 /* Parse the unqualified-id. */
19257 identifier
= cp_parser_unqualified_id (parser
,
19258 /*template_keyword_p=*/false,
19259 /*check_dependency_p=*/true,
19260 /*declarator_p=*/true,
19261 /*optional_p=*/false);
19263 if (access_declaration_p
)
19265 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19266 cp_parser_simulate_error (parser
);
19267 if (!cp_parser_parse_definitely (parser
))
19270 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19272 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19273 if (cxx_dialect
< cxx17
19274 && !in_system_header_at (ell
->location
))
19275 pedwarn (ell
->location
, 0,
19276 "pack expansion in using-declaration only available "
19277 "with -std=c++17 or -std=gnu++17");
19278 qscope
= make_pack_expansion (qscope
);
19281 /* The function we call to handle a using-declaration is different
19282 depending on what scope we are in. */
19283 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19285 else if (!identifier_p (identifier
)
19286 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19287 /* [namespace.udecl]
19289 A using declaration shall not name a template-id. */
19290 error_at (token
->location
,
19291 "a template-id may not appear in a using-declaration");
19294 if (at_class_scope_p ())
19296 /* Create the USING_DECL. */
19297 decl
= do_class_using_decl (qscope
, identifier
);
19299 if (decl
&& typename_p
)
19300 USING_DECL_TYPENAME_P (decl
) = 1;
19302 if (check_for_bare_parameter_packs (decl
))
19304 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19308 /* Add it to the list of members in this class. */
19309 finish_member_declaration (decl
);
19313 decl
= cp_parser_lookup_name_simple (parser
,
19316 if (decl
== error_mark_node
)
19317 cp_parser_name_lookup_error (parser
, identifier
,
19320 else if (check_for_bare_parameter_packs (decl
))
19322 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19325 else if (!at_namespace_scope_p ())
19326 finish_local_using_decl (decl
, qscope
, identifier
);
19328 finish_namespace_using_decl (decl
, qscope
, identifier
);
19332 if (!access_declaration_p
19333 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19335 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19336 if (cxx_dialect
< cxx17
)
19337 pedwarn (comma
->location
, 0,
19338 "comma-separated list in using-declaration only available "
19339 "with -std=c++17 or -std=gnu++17");
19343 /* Look for the final `;'. */
19344 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19346 if (access_declaration_p
&& errorcount
== oldcount
)
19347 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19348 "access declarations are deprecated "
19349 "in favour of using-declarations; "
19350 "suggestion: add the %<using%> keyword");
19355 /* Parse an alias-declaration.
19358 using identifier attribute-specifier-seq [opt] = type-id */
19361 cp_parser_alias_declaration (cp_parser
* parser
)
19363 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19364 location_t id_location
, type_location
;
19365 cp_declarator
*declarator
;
19366 cp_decl_specifier_seq decl_specs
;
19368 const char *saved_message
= NULL
;
19370 /* Look for the `using' keyword. */
19371 cp_token
*using_token
19372 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19373 if (using_token
== NULL
)
19374 return error_mark_node
;
19376 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19377 id
= cp_parser_identifier (parser
);
19378 if (id
== error_mark_node
)
19379 return error_mark_node
;
19381 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19382 attributes
= cp_parser_attributes_opt (parser
);
19383 if (attributes
== error_mark_node
)
19384 return error_mark_node
;
19386 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19388 if (cp_parser_error_occurred (parser
))
19389 return error_mark_node
;
19391 cp_parser_commit_to_tentative_parse (parser
);
19393 /* Now we are going to parse the type-id of the declaration. */
19398 "A type-specifier-seq shall not define a class or enumeration
19399 unless it appears in the type-id of an alias-declaration (7.1.3) that
19400 is not the declaration of a template-declaration."
19402 In other words, if we currently are in an alias template, the
19403 type-id should not define a type.
19405 So let's set parser->type_definition_forbidden_message in that
19406 case; cp_parser_check_type_definition (called by
19407 cp_parser_class_specifier) will then emit an error if a type is
19408 defined in the type-id. */
19409 if (parser
->num_template_parameter_lists
)
19411 saved_message
= parser
->type_definition_forbidden_message
;
19412 parser
->type_definition_forbidden_message
=
19413 G_("types may not be defined in alias template declarations");
19416 type
= cp_parser_type_id (parser
, &type_location
);
19418 /* Restore the error message if need be. */
19419 if (parser
->num_template_parameter_lists
)
19420 parser
->type_definition_forbidden_message
= saved_message
;
19422 if (type
== error_mark_node
19423 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19425 cp_parser_skip_to_end_of_block_or_statement (parser
);
19426 return error_mark_node
;
19429 /* A typedef-name can also be introduced by an alias-declaration. The
19430 identifier following the using keyword becomes a typedef-name. It has
19431 the same semantics as if it were introduced by the typedef
19432 specifier. In particular, it does not define a new type and it shall
19433 not appear in the type-id. */
19435 clear_decl_specs (&decl_specs
);
19436 decl_specs
.type
= type
;
19437 if (attributes
!= NULL_TREE
)
19439 decl_specs
.attributes
= attributes
;
19440 set_and_check_decl_spec_loc (&decl_specs
,
19444 set_and_check_decl_spec_loc (&decl_specs
,
19447 set_and_check_decl_spec_loc (&decl_specs
,
19450 decl_specs
.locations
[ds_type_spec
] = type_location
;
19452 if (parser
->num_template_parameter_lists
19453 && !cp_parser_check_template_parameters (parser
,
19454 /*num_templates=*/0,
19455 /*template_id*/false,
19457 /*declarator=*/NULL
))
19458 return error_mark_node
;
19460 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19462 member_p
= at_class_scope_p ();
19464 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19465 NULL_TREE
, attributes
);
19467 decl
= start_decl (declarator
, &decl_specs
, 0,
19468 attributes
, NULL_TREE
, &pushed_scope
);
19469 if (decl
== error_mark_node
)
19472 // Attach constraints to the alias declaration.
19473 if (flag_concepts
&& current_template_parms
)
19475 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19476 tree constr
= build_constraints (reqs
, NULL_TREE
);
19477 set_constraints (decl
, constr
);
19480 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19483 pop_scope (pushed_scope
);
19485 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19486 added into the symbol table; otherwise, return the TYPE_DECL. */
19487 if (DECL_LANG_SPECIFIC (decl
)
19488 && DECL_TEMPLATE_INFO (decl
)
19489 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19491 decl
= DECL_TI_TEMPLATE (decl
);
19493 check_member_template (decl
);
19499 /* Parse a using-directive.
19502 using namespace :: [opt] nested-name-specifier [opt]
19503 namespace-name ; */
19506 cp_parser_using_directive (cp_parser
* parser
)
19508 tree namespace_decl
;
19511 /* Look for the `using' keyword. */
19512 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19513 /* And the `namespace' keyword. */
19514 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19515 /* Look for the optional `::' operator. */
19516 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19517 /* And the optional nested-name-specifier. */
19518 cp_parser_nested_name_specifier_opt (parser
,
19519 /*typename_keyword_p=*/false,
19520 /*check_dependency_p=*/true,
19522 /*is_declaration=*/true);
19523 /* Get the namespace being used. */
19524 namespace_decl
= cp_parser_namespace_name (parser
);
19525 /* And any specified attributes. */
19526 attribs
= cp_parser_attributes_opt (parser
);
19528 /* Update the symbol table. */
19529 if (namespace_bindings_p ())
19530 finish_namespace_using_directive (namespace_decl
, attribs
);
19532 finish_local_using_directive (namespace_decl
, attribs
);
19534 /* Look for the final `;'. */
19535 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19538 /* Parse an asm-definition.
19541 asm ( string-literal ) ;
19546 asm volatile [opt] ( string-literal ) ;
19547 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19548 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19549 : asm-operand-list [opt] ) ;
19550 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19551 : asm-operand-list [opt]
19552 : asm-clobber-list [opt] ) ;
19553 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19554 : asm-clobber-list [opt]
19555 : asm-goto-list ) ; */
19558 cp_parser_asm_definition (cp_parser
* parser
)
19561 tree outputs
= NULL_TREE
;
19562 tree inputs
= NULL_TREE
;
19563 tree clobbers
= NULL_TREE
;
19564 tree labels
= NULL_TREE
;
19566 bool volatile_p
= false;
19567 bool extended_p
= false;
19568 bool invalid_inputs_p
= false;
19569 bool invalid_outputs_p
= false;
19570 bool goto_p
= false;
19571 required_token missing
= RT_NONE
;
19573 /* Look for the `asm' keyword. */
19574 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19576 if (parser
->in_function_body
19577 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19579 error ("%<asm%> in %<constexpr%> function");
19580 cp_function_chain
->invalid_constexpr
= true;
19583 /* See if the next token is `volatile'. */
19584 if (cp_parser_allow_gnu_extensions_p (parser
)
19585 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19587 /* Remember that we saw the `volatile' keyword. */
19589 /* Consume the token. */
19590 cp_lexer_consume_token (parser
->lexer
);
19592 if (cp_parser_allow_gnu_extensions_p (parser
)
19593 && parser
->in_function_body
19594 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19596 /* Remember that we saw the `goto' keyword. */
19598 /* Consume the token. */
19599 cp_lexer_consume_token (parser
->lexer
);
19601 /* Look for the opening `('. */
19602 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19604 /* Look for the string. */
19605 string
= cp_parser_string_literal (parser
, false, false);
19606 if (string
== error_mark_node
)
19608 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19609 /*consume_paren=*/true);
19613 /* If we're allowing GNU extensions, check for the extended assembly
19614 syntax. Unfortunately, the `:' tokens need not be separated by
19615 a space in C, and so, for compatibility, we tolerate that here
19616 too. Doing that means that we have to treat the `::' operator as
19618 if (cp_parser_allow_gnu_extensions_p (parser
)
19619 && parser
->in_function_body
19620 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19621 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19623 bool inputs_p
= false;
19624 bool clobbers_p
= false;
19625 bool labels_p
= false;
19627 /* The extended syntax was used. */
19630 /* Look for outputs. */
19631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19633 /* Consume the `:'. */
19634 cp_lexer_consume_token (parser
->lexer
);
19635 /* Parse the output-operands. */
19636 if (cp_lexer_next_token_is_not (parser
->lexer
,
19638 && cp_lexer_next_token_is_not (parser
->lexer
,
19640 && cp_lexer_next_token_is_not (parser
->lexer
,
19644 outputs
= cp_parser_asm_operand_list (parser
);
19645 if (outputs
== error_mark_node
)
19646 invalid_outputs_p
= true;
19649 /* If the next token is `::', there are no outputs, and the
19650 next token is the beginning of the inputs. */
19651 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19652 /* The inputs are coming next. */
19655 /* Look for inputs. */
19657 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19659 /* Consume the `:' or `::'. */
19660 cp_lexer_consume_token (parser
->lexer
);
19661 /* Parse the output-operands. */
19662 if (cp_lexer_next_token_is_not (parser
->lexer
,
19664 && cp_lexer_next_token_is_not (parser
->lexer
,
19666 && cp_lexer_next_token_is_not (parser
->lexer
,
19669 inputs
= cp_parser_asm_operand_list (parser
);
19670 if (inputs
== error_mark_node
)
19671 invalid_inputs_p
= true;
19674 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19675 /* The clobbers are coming next. */
19678 /* Look for clobbers. */
19680 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19683 /* Consume the `:' or `::'. */
19684 cp_lexer_consume_token (parser
->lexer
);
19685 /* Parse the clobbers. */
19686 if (cp_lexer_next_token_is_not (parser
->lexer
,
19688 && cp_lexer_next_token_is_not (parser
->lexer
,
19690 clobbers
= cp_parser_asm_clobber_list (parser
);
19693 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19694 /* The labels are coming next. */
19697 /* Look for labels. */
19699 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19702 /* Consume the `:' or `::'. */
19703 cp_lexer_consume_token (parser
->lexer
);
19704 /* Parse the labels. */
19705 labels
= cp_parser_asm_label_list (parser
);
19708 if (goto_p
&& !labels_p
)
19709 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19712 missing
= RT_COLON_SCOPE
;
19714 /* Look for the closing `)'. */
19715 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19716 missing
? missing
: RT_CLOSE_PAREN
))
19717 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19718 /*consume_paren=*/true);
19719 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19721 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19723 /* Create the ASM_EXPR. */
19724 if (parser
->in_function_body
)
19726 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19727 inputs
, clobbers
, labels
);
19728 /* If the extended syntax was not used, mark the ASM_EXPR. */
19731 tree temp
= asm_stmt
;
19732 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19733 temp
= TREE_OPERAND (temp
, 0);
19735 ASM_INPUT_P (temp
) = 1;
19739 symtab
->finalize_toplevel_asm (string
);
19743 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19744 type that comes from the decl-specifier-seq. */
19747 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19749 for (cp_declarator
*d
= declarator
; d
;)
19759 if (TYPE_PTRMEMFUNC_P (type
))
19760 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19761 type
= TREE_TYPE (type
);
19769 /* Declarators [gram.dcl.decl] */
19771 /* Parse an init-declarator.
19774 declarator initializer [opt]
19779 declarator asm-specification [opt] attributes [opt] initializer [opt]
19781 function-definition:
19782 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19784 decl-specifier-seq [opt] declarator function-try-block
19788 function-definition:
19789 __extension__ function-definition
19793 function-definition:
19794 decl-specifier-seq [opt] declarator function-transaction-block
19796 The DECL_SPECIFIERS apply to this declarator. Returns a
19797 representation of the entity declared. If MEMBER_P is TRUE, then
19798 this declarator appears in a class scope. The new DECL created by
19799 this declarator is returned.
19801 The CHECKS are access checks that should be performed once we know
19802 what entity is being declared (and, therefore, what classes have
19805 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19806 for a function-definition here as well. If the declarator is a
19807 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19808 be TRUE upon return. By that point, the function-definition will
19809 have been completely parsed.
19811 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19814 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19815 parsed declaration if it is an uninitialized single declarator not followed
19816 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19817 if present, will not be consumed. If returned, this declarator will be
19818 created with SD_INITIALIZED but will not call cp_finish_decl.
19820 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19821 and there is an initializer, the pointed location_t is set to the
19822 location of the '=' or `(', or '{' in C++11 token introducing the
19826 cp_parser_init_declarator (cp_parser
* parser
,
19827 cp_decl_specifier_seq
*decl_specifiers
,
19828 vec
<deferred_access_check
, va_gc
> *checks
,
19829 bool function_definition_allowed_p
,
19831 int declares_class_or_enum
,
19832 bool* function_definition_p
,
19833 tree
* maybe_range_for_decl
,
19834 location_t
* init_loc
,
19837 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19838 *attributes_start_token
= NULL
;
19839 cp_declarator
*declarator
;
19840 tree prefix_attributes
;
19841 tree attributes
= NULL
;
19842 tree asm_specification
;
19844 tree decl
= NULL_TREE
;
19846 int is_initialized
;
19847 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19848 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19850 enum cpp_ttype initialization_kind
;
19851 bool is_direct_init
= false;
19852 bool is_non_constant_init
;
19853 int ctor_dtor_or_conv_p
;
19854 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19855 tree pushed_scope
= NULL_TREE
;
19856 bool range_for_decl_p
= false;
19857 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19858 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19860 /* Gather the attributes that were provided with the
19861 decl-specifiers. */
19862 prefix_attributes
= decl_specifiers
->attributes
;
19864 /* Assume that this is not the declarator for a function
19866 if (function_definition_p
)
19867 *function_definition_p
= false;
19869 /* Default arguments are only permitted for function parameters. */
19870 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19871 parser
->default_arg_ok_p
= false;
19873 /* Defer access checks while parsing the declarator; we cannot know
19874 what names are accessible until we know what is being
19876 resume_deferring_access_checks ();
19878 token
= cp_lexer_peek_token (parser
->lexer
);
19880 /* Parse the declarator. */
19882 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19883 &ctor_dtor_or_conv_p
,
19884 /*parenthesized_p=*/NULL
,
19885 member_p
, friend_p
);
19886 /* Gather up the deferred checks. */
19887 stop_deferring_access_checks ();
19889 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19891 /* If the DECLARATOR was erroneous, there's no need to go
19893 if (declarator
== cp_error_declarator
)
19894 return error_mark_node
;
19896 /* Check that the number of template-parameter-lists is OK. */
19897 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19899 return error_mark_node
;
19901 if (declares_class_or_enum
& 2)
19902 cp_parser_check_for_definition_in_return_type (declarator
,
19903 decl_specifiers
->type
,
19904 decl_specifiers
->locations
[ds_type_spec
]);
19906 /* Figure out what scope the entity declared by the DECLARATOR is
19907 located in. `grokdeclarator' sometimes changes the scope, so
19908 we compute it now. */
19909 scope
= get_scope_of_declarator (declarator
);
19911 /* Perform any lookups in the declared type which were thought to be
19912 dependent, but are not in the scope of the declarator. */
19913 decl_specifiers
->type
19914 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19916 /* If we're allowing GNU extensions, look for an
19917 asm-specification. */
19918 if (cp_parser_allow_gnu_extensions_p (parser
))
19920 /* Look for an asm-specification. */
19921 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19922 asm_specification
= cp_parser_asm_specification_opt (parser
);
19925 asm_specification
= NULL_TREE
;
19927 /* Look for attributes. */
19928 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19929 attributes
= cp_parser_attributes_opt (parser
);
19931 /* Peek at the next token. */
19932 token
= cp_lexer_peek_token (parser
->lexer
);
19934 bool bogus_implicit_tmpl
= false;
19936 if (function_declarator_p (declarator
))
19938 /* Handle C++17 deduction guides. */
19939 if (!decl_specifiers
->type
19940 && ctor_dtor_or_conv_p
<= 0
19941 && cxx_dialect
>= cxx17
)
19943 cp_declarator
*id
= get_id_declarator (declarator
);
19944 tree name
= id
->u
.id
.unqualified_name
;
19945 parser
->scope
= id
->u
.id
.qualifying_scope
;
19946 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19948 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19949 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19951 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19952 id
->u
.id
.sfk
= sfk_deduction_guide
;
19953 ctor_dtor_or_conv_p
= 1;
19957 /* Check to see if the token indicates the start of a
19958 function-definition. */
19959 if (cp_parser_token_starts_function_definition_p (token
))
19961 if (!function_definition_allowed_p
)
19963 /* If a function-definition should not appear here, issue an
19965 cp_parser_error (parser
,
19966 "a function-definition is not allowed here");
19967 return error_mark_node
;
19970 location_t func_brace_location
19971 = cp_lexer_peek_token (parser
->lexer
)->location
;
19973 /* Neither attributes nor an asm-specification are allowed
19974 on a function-definition. */
19975 if (asm_specification
)
19976 error_at (asm_spec_start_token
->location
,
19977 "an asm-specification is not allowed "
19978 "on a function-definition");
19980 error_at (attributes_start_token
->location
,
19981 "attributes are not allowed "
19982 "on a function-definition");
19983 /* This is a function-definition. */
19984 *function_definition_p
= true;
19986 /* Parse the function definition. */
19988 decl
= cp_parser_save_member_function_body (parser
,
19991 prefix_attributes
);
19994 (cp_parser_function_definition_from_specifiers_and_declarator
19995 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19997 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19999 /* This is where the prologue starts... */
20000 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20001 = func_brace_location
;
20007 else if (parser
->fully_implicit_function_template_p
)
20009 /* A non-template declaration involving a function parameter list
20010 containing an implicit template parameter will be made into a
20011 template. If the resulting declaration is not going to be an
20012 actual function then finish the template scope here to prevent it.
20013 An error message will be issued once we have a decl to talk about.
20015 FIXME probably we should do type deduction rather than create an
20016 implicit template, but the standard currently doesn't allow it. */
20017 bogus_implicit_tmpl
= true;
20018 finish_fully_implicit_template (parser
, NULL_TREE
);
20023 Only in function declarations for constructors, destructors, type
20024 conversions, and deduction guides can the decl-specifier-seq be omitted.
20026 We explicitly postpone this check past the point where we handle
20027 function-definitions because we tolerate function-definitions
20028 that are missing their return types in some modes. */
20029 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20031 cp_parser_error (parser
,
20032 "expected constructor, destructor, or type conversion");
20033 return error_mark_node
;
20036 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20037 if (token
->type
== CPP_EQ
20038 || token
->type
== CPP_OPEN_PAREN
20039 || token
->type
== CPP_OPEN_BRACE
)
20041 is_initialized
= SD_INITIALIZED
;
20042 initialization_kind
= token
->type
;
20043 if (maybe_range_for_decl
)
20044 *maybe_range_for_decl
= error_mark_node
;
20045 tmp_init_loc
= token
->location
;
20046 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20047 *init_loc
= tmp_init_loc
;
20049 if (token
->type
== CPP_EQ
20050 && function_declarator_p (declarator
))
20052 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20053 if (t2
->keyword
== RID_DEFAULT
)
20054 is_initialized
= SD_DEFAULTED
;
20055 else if (t2
->keyword
== RID_DELETE
)
20056 is_initialized
= SD_DELETED
;
20061 /* If the init-declarator isn't initialized and isn't followed by a
20062 `,' or `;', it's not a valid init-declarator. */
20063 if (token
->type
!= CPP_COMMA
20064 && token
->type
!= CPP_SEMICOLON
)
20066 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20067 range_for_decl_p
= true;
20070 if (!maybe_range_for_decl
)
20071 cp_parser_error (parser
, "expected initializer");
20072 return error_mark_node
;
20075 is_initialized
= SD_UNINITIALIZED
;
20076 initialization_kind
= CPP_EOF
;
20079 /* Because start_decl has side-effects, we should only call it if we
20080 know we're going ahead. By this point, we know that we cannot
20081 possibly be looking at any other construct. */
20082 cp_parser_commit_to_tentative_parse (parser
);
20084 /* Enter the newly declared entry in the symbol table. If we're
20085 processing a declaration in a class-specifier, we wait until
20086 after processing the initializer. */
20089 if (parser
->in_unbraced_linkage_specification_p
)
20090 decl_specifiers
->storage_class
= sc_extern
;
20091 decl
= start_decl (declarator
, decl_specifiers
,
20092 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20093 attributes
, prefix_attributes
, &pushed_scope
);
20094 cp_finalize_omp_declare_simd (parser
, decl
);
20095 cp_finalize_oacc_routine (parser
, decl
, false);
20096 /* Adjust location of decl if declarator->id_loc is more appropriate:
20097 set, and decl wasn't merged with another decl, in which case its
20098 location would be different from input_location, and more accurate. */
20100 && declarator
->id_loc
!= UNKNOWN_LOCATION
20101 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20102 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20105 /* Enter the SCOPE. That way unqualified names appearing in the
20106 initializer will be looked up in SCOPE. */
20107 pushed_scope
= push_scope (scope
);
20109 /* Perform deferred access control checks, now that we know in which
20110 SCOPE the declared entity resides. */
20111 if (!member_p
&& decl
)
20113 tree saved_current_function_decl
= NULL_TREE
;
20115 /* If the entity being declared is a function, pretend that we
20116 are in its scope. If it is a `friend', it may have access to
20117 things that would not otherwise be accessible. */
20118 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20120 saved_current_function_decl
= current_function_decl
;
20121 current_function_decl
= decl
;
20124 /* Perform access checks for template parameters. */
20125 cp_parser_perform_template_parameter_access_checks (checks
);
20127 /* Perform the access control checks for the declarator and the
20128 decl-specifiers. */
20129 perform_deferred_access_checks (tf_warning_or_error
);
20131 /* Restore the saved value. */
20132 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20133 current_function_decl
= saved_current_function_decl
;
20136 /* Parse the initializer. */
20137 initializer
= NULL_TREE
;
20138 is_direct_init
= false;
20139 is_non_constant_init
= true;
20140 if (is_initialized
)
20142 if (function_declarator_p (declarator
))
20144 if (initialization_kind
== CPP_EQ
)
20145 initializer
= cp_parser_pure_specifier (parser
);
20148 /* If the declaration was erroneous, we don't really
20149 know what the user intended, so just silently
20150 consume the initializer. */
20151 if (decl
!= error_mark_node
)
20152 error_at (tmp_init_loc
, "initializer provided for function");
20153 cp_parser_skip_to_closing_parenthesis (parser
,
20154 /*recovering=*/true,
20155 /*or_comma=*/false,
20156 /*consume_paren=*/true);
20161 /* We want to record the extra mangling scope for in-class
20162 initializers of class members and initializers of static data
20163 member templates. The former involves deferring
20164 parsing of the initializer until end of class as with default
20165 arguments. So right here we only handle the latter. */
20166 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20167 start_lambda_scope (decl
);
20168 initializer
= cp_parser_initializer (parser
,
20170 &is_non_constant_init
);
20171 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20172 finish_lambda_scope ();
20173 if (initializer
== error_mark_node
)
20174 cp_parser_skip_to_end_of_statement (parser
);
20178 /* The old parser allows attributes to appear after a parenthesized
20179 initializer. Mark Mitchell proposed removing this functionality
20180 on the GCC mailing lists on 2002-08-13. This parser accepts the
20181 attributes -- but ignores them. Made a permerror in GCC 8. */
20182 if (cp_parser_allow_gnu_extensions_p (parser
)
20183 && initialization_kind
== CPP_OPEN_PAREN
20184 && cp_parser_attributes_opt (parser
)
20185 && permerror (input_location
,
20186 "attributes after parenthesized initializer ignored"))
20189 if (flag_permissive
&& !hint
)
20192 inform (input_location
,
20193 "this flexibility is deprecated and will be removed");
20197 /* And now complain about a non-function implicit template. */
20198 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20199 error_at (DECL_SOURCE_LOCATION (decl
),
20200 "non-function %qD declared as implicit template", decl
);
20202 /* For an in-class declaration, use `grokfield' to create the
20208 pop_scope (pushed_scope
);
20209 pushed_scope
= NULL_TREE
;
20211 decl
= grokfield (declarator
, decl_specifiers
,
20212 initializer
, !is_non_constant_init
,
20213 /*asmspec=*/NULL_TREE
,
20214 attr_chainon (attributes
, prefix_attributes
));
20215 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20216 cp_parser_save_default_args (parser
, decl
);
20217 cp_finalize_omp_declare_simd (parser
, decl
);
20218 cp_finalize_oacc_routine (parser
, decl
, false);
20221 /* Finish processing the declaration. But, skip member
20223 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20225 cp_finish_decl (decl
,
20226 initializer
, !is_non_constant_init
,
20228 /* If the initializer is in parentheses, then this is
20229 a direct-initialization, which means that an
20230 `explicit' constructor is OK. Otherwise, an
20231 `explicit' constructor cannot be used. */
20232 ((is_direct_init
|| !is_initialized
)
20233 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20235 else if ((cxx_dialect
!= cxx98
) && friend_p
20236 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20237 /* Core issue #226 (C++0x only): A default template-argument
20238 shall not be specified in a friend class template
20240 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20241 /*is_partial=*/false, /*is_friend_decl=*/1);
20243 if (!friend_p
&& pushed_scope
)
20244 pop_scope (pushed_scope
);
20246 if (function_declarator_p (declarator
)
20247 && parser
->fully_implicit_function_template_p
)
20250 decl
= finish_fully_implicit_template (parser
, decl
);
20252 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20255 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20256 && type_uses_auto (decl_specifiers
->type
))
20257 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20262 /* Parse a declarator.
20266 ptr-operator declarator
20268 abstract-declarator:
20269 ptr-operator abstract-declarator [opt]
20270 direct-abstract-declarator
20275 attributes [opt] direct-declarator
20276 attributes [opt] ptr-operator declarator
20278 abstract-declarator:
20279 attributes [opt] ptr-operator abstract-declarator [opt]
20280 attributes [opt] direct-abstract-declarator
20282 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20283 detect constructors, destructors, deduction guides, or conversion operators.
20284 It is set to -1 if the declarator is a name, and +1 if it is a
20285 function. Otherwise it is set to zero. Usually you just want to
20286 test for >0, but internally the negative value is used.
20288 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20289 a decl-specifier-seq unless it declares a constructor, destructor,
20290 or conversion. It might seem that we could check this condition in
20291 semantic analysis, rather than parsing, but that makes it difficult
20292 to handle something like `f()'. We want to notice that there are
20293 no decl-specifiers, and therefore realize that this is an
20294 expression, not a declaration.)
20296 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20297 the declarator is a direct-declarator of the form "(...)".
20299 MEMBER_P is true iff this declarator is a member-declarator.
20301 FRIEND_P is true iff this declarator is a friend. */
20303 static cp_declarator
*
20304 cp_parser_declarator (cp_parser
* parser
,
20305 cp_parser_declarator_kind dcl_kind
,
20306 int* ctor_dtor_or_conv_p
,
20307 bool* parenthesized_p
,
20308 bool member_p
, bool friend_p
)
20310 cp_declarator
*declarator
;
20311 enum tree_code code
;
20312 cp_cv_quals cv_quals
;
20314 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20316 /* Assume this is not a constructor, destructor, or type-conversion
20318 if (ctor_dtor_or_conv_p
)
20319 *ctor_dtor_or_conv_p
= 0;
20321 if (cp_parser_allow_gnu_extensions_p (parser
))
20322 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20324 /* Check for the ptr-operator production. */
20325 cp_parser_parse_tentatively (parser
);
20326 /* Parse the ptr-operator. */
20327 code
= cp_parser_ptr_operator (parser
,
20332 /* If that worked, then we have a ptr-operator. */
20333 if (cp_parser_parse_definitely (parser
))
20335 /* If a ptr-operator was found, then this declarator was not
20337 if (parenthesized_p
)
20338 *parenthesized_p
= true;
20339 /* The dependent declarator is optional if we are parsing an
20340 abstract-declarator. */
20341 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20342 cp_parser_parse_tentatively (parser
);
20344 /* Parse the dependent declarator. */
20345 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20346 /*ctor_dtor_or_conv_p=*/NULL
,
20347 /*parenthesized_p=*/NULL
,
20348 /*member_p=*/false,
20351 /* If we are parsing an abstract-declarator, we must handle the
20352 case where the dependent declarator is absent. */
20353 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20354 && !cp_parser_parse_definitely (parser
))
20357 declarator
= cp_parser_make_indirect_declarator
20358 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20360 /* Everything else is a direct-declarator. */
20363 if (parenthesized_p
)
20364 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20366 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20367 ctor_dtor_or_conv_p
,
20368 member_p
, friend_p
);
20371 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20372 declarator
->attributes
= gnu_attributes
;
20376 /* Parse a direct-declarator or direct-abstract-declarator.
20380 direct-declarator ( parameter-declaration-clause )
20381 cv-qualifier-seq [opt]
20382 ref-qualifier [opt]
20383 exception-specification [opt]
20384 direct-declarator [ constant-expression [opt] ]
20387 direct-abstract-declarator:
20388 direct-abstract-declarator [opt]
20389 ( parameter-declaration-clause )
20390 cv-qualifier-seq [opt]
20391 ref-qualifier [opt]
20392 exception-specification [opt]
20393 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20394 ( abstract-declarator )
20396 Returns a representation of the declarator. DCL_KIND is
20397 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20398 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20399 we are parsing a direct-declarator. It is
20400 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20401 of ambiguity we prefer an abstract declarator, as per
20402 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20403 as for cp_parser_declarator. */
20405 static cp_declarator
*
20406 cp_parser_direct_declarator (cp_parser
* parser
,
20407 cp_parser_declarator_kind dcl_kind
,
20408 int* ctor_dtor_or_conv_p
,
20409 bool member_p
, bool friend_p
)
20412 cp_declarator
*declarator
= NULL
;
20413 tree scope
= NULL_TREE
;
20414 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20415 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20417 tree pushed_scope
= NULL_TREE
;
20418 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20422 /* Peek at the next token. */
20423 token
= cp_lexer_peek_token (parser
->lexer
);
20424 if (token
->type
== CPP_OPEN_PAREN
)
20426 /* This is either a parameter-declaration-clause, or a
20427 parenthesized declarator. When we know we are parsing a
20428 named declarator, it must be a parenthesized declarator
20429 if FIRST is true. For instance, `(int)' is a
20430 parameter-declaration-clause, with an omitted
20431 direct-abstract-declarator. But `((*))', is a
20432 parenthesized abstract declarator. Finally, when T is a
20433 template parameter `(T)' is a
20434 parameter-declaration-clause, and not a parenthesized
20437 We first try and parse a parameter-declaration-clause,
20438 and then try a nested declarator (if FIRST is true).
20440 It is not an error for it not to be a
20441 parameter-declaration-clause, even when FIRST is
20447 The first is the declaration of a function while the
20448 second is the definition of a variable, including its
20451 Having seen only the parenthesis, we cannot know which of
20452 these two alternatives should be selected. Even more
20453 complex are examples like:
20458 The former is a function-declaration; the latter is a
20459 variable initialization.
20461 Thus again, we try a parameter-declaration-clause, and if
20462 that fails, we back out and return. */
20464 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20467 bool is_declarator
= false;
20471 /* In a member-declarator, the only valid interpretation
20472 of a parenthesis is the start of a
20473 parameter-declaration-clause. (It is invalid to
20474 initialize a static data member with a parenthesized
20475 initializer; only the "=" form of initialization is
20478 cp_parser_parse_tentatively (parser
);
20480 /* Consume the `('. */
20481 matching_parens parens
;
20482 parens
.consume_open (parser
);
20485 /* If this is going to be an abstract declarator, we're
20486 in a declarator and we can't have default args. */
20487 parser
->default_arg_ok_p
= false;
20488 parser
->in_declarator_p
= true;
20491 begin_scope (sk_function_parms
, NULL_TREE
);
20493 /* Parse the parameter-declaration-clause. */
20494 params
= cp_parser_parameter_declaration_clause (parser
);
20496 /* Consume the `)'. */
20497 parens
.require_close (parser
);
20499 /* If all went well, parse the cv-qualifier-seq,
20500 ref-qualifier and the exception-specification. */
20501 if (member_p
|| cp_parser_parse_definitely (parser
))
20503 cp_cv_quals cv_quals
;
20504 cp_virt_specifiers virt_specifiers
;
20505 cp_ref_qualifier ref_qual
;
20506 tree exception_specification
;
20509 bool memfn
= (member_p
|| (pushed_scope
20510 && CLASS_TYPE_P (pushed_scope
)));
20512 is_declarator
= true;
20514 if (ctor_dtor_or_conv_p
)
20515 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20518 /* Parse the cv-qualifier-seq. */
20519 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20520 /* Parse the ref-qualifier. */
20521 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20522 /* Parse the tx-qualifier. */
20523 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20524 /* And the exception-specification. */
20525 exception_specification
20526 = cp_parser_exception_specification_opt (parser
);
20528 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20530 /* In here, we handle cases where attribute is used after
20531 the function declaration. For example:
20532 void func (int x) __attribute__((vector(..))); */
20533 tree gnu_attrs
= NULL_TREE
;
20534 tree requires_clause
= NULL_TREE
;
20535 late_return
= (cp_parser_late_return_type_opt
20536 (parser
, declarator
, requires_clause
,
20537 memfn
? cv_quals
: -1));
20539 /* Parse the virt-specifier-seq. */
20540 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20542 /* Create the function-declarator. */
20543 declarator
= make_call_declarator (declarator
,
20549 exception_specification
,
20552 declarator
->std_attributes
= attrs
;
20553 declarator
->attributes
= gnu_attrs
;
20554 /* Any subsequent parameter lists are to do with
20555 return type, so are not those of the declared
20557 parser
->default_arg_ok_p
= false;
20560 /* Remove the function parms from scope. */
20561 pop_bindings_and_leave_scope ();
20564 /* Repeat the main loop. */
20568 /* If this is the first, we can try a parenthesized
20572 bool saved_in_type_id_in_expr_p
;
20574 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20575 parser
->in_declarator_p
= saved_in_declarator_p
;
20577 open_paren
= token
;
20578 /* Consume the `('. */
20579 matching_parens parens
;
20580 parens
.consume_open (parser
);
20581 /* Parse the nested declarator. */
20582 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20583 parser
->in_type_id_in_expr_p
= true;
20585 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20586 /*parenthesized_p=*/NULL
,
20587 member_p
, friend_p
);
20588 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20590 /* Expect a `)'. */
20591 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20592 if (!parens
.require_close (parser
))
20593 declarator
= cp_error_declarator
;
20594 if (declarator
== cp_error_declarator
)
20597 goto handle_declarator
;
20599 /* Otherwise, we must be done. */
20603 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20604 && token
->type
== CPP_OPEN_SQUARE
20605 && !cp_next_tokens_can_be_attribute_p (parser
))
20607 /* Parse an array-declarator. */
20608 tree bounds
, attrs
;
20610 if (ctor_dtor_or_conv_p
)
20611 *ctor_dtor_or_conv_p
= 0;
20615 parser
->default_arg_ok_p
= false;
20616 parser
->in_declarator_p
= true;
20617 /* Consume the `['. */
20618 cp_lexer_consume_token (parser
->lexer
);
20619 /* Peek at the next token. */
20620 token
= cp_lexer_peek_token (parser
->lexer
);
20621 /* If the next token is `]', then there is no
20622 constant-expression. */
20623 if (token
->type
!= CPP_CLOSE_SQUARE
)
20625 bool non_constant_p
;
20627 = cp_parser_constant_expression (parser
,
20628 /*allow_non_constant=*/true,
20630 if (!non_constant_p
)
20632 else if (error_operand_p (bounds
))
20633 /* Already gave an error. */;
20634 else if (!parser
->in_function_body
20635 || current_binding_level
->kind
== sk_function_parms
)
20637 /* Normally, the array bound must be an integral constant
20638 expression. However, as an extension, we allow VLAs
20639 in function scopes as long as they aren't part of a
20640 parameter declaration. */
20641 cp_parser_error (parser
,
20642 "array bound is not an integer constant");
20643 bounds
= error_mark_node
;
20645 else if (processing_template_decl
20646 && !type_dependent_expression_p (bounds
))
20648 /* Remember this wasn't a constant-expression. */
20649 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20650 TREE_SIDE_EFFECTS (bounds
) = 1;
20654 bounds
= NULL_TREE
;
20655 /* Look for the closing `]'. */
20656 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20658 declarator
= cp_error_declarator
;
20662 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20663 declarator
= make_array_declarator (declarator
, bounds
);
20664 declarator
->std_attributes
= attrs
;
20666 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20669 tree qualifying_scope
;
20670 tree unqualified_name
;
20672 special_function_kind sfk
;
20674 bool pack_expansion_p
= false;
20675 cp_token
*declarator_id_start_token
;
20677 /* Parse a declarator-id */
20678 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20681 cp_parser_parse_tentatively (parser
);
20683 /* If we see an ellipsis, we should be looking at a
20685 if (token
->type
== CPP_ELLIPSIS
)
20687 /* Consume the `...' */
20688 cp_lexer_consume_token (parser
->lexer
);
20690 pack_expansion_p
= true;
20694 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20696 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20697 qualifying_scope
= parser
->scope
;
20702 if (!unqualified_name
&& pack_expansion_p
)
20704 /* Check whether an error occurred. */
20705 okay
= !cp_parser_error_occurred (parser
);
20707 /* We already consumed the ellipsis to mark a
20708 parameter pack, but we have no way to report it,
20709 so abort the tentative parse. We will be exiting
20710 immediately anyway. */
20711 cp_parser_abort_tentative_parse (parser
);
20714 okay
= cp_parser_parse_definitely (parser
);
20717 unqualified_name
= error_mark_node
;
20718 else if (unqualified_name
20719 && (qualifying_scope
20720 || (!identifier_p (unqualified_name
))))
20722 cp_parser_error (parser
, "expected unqualified-id");
20723 unqualified_name
= error_mark_node
;
20727 if (!unqualified_name
)
20729 if (unqualified_name
== error_mark_node
)
20731 declarator
= cp_error_declarator
;
20732 pack_expansion_p
= false;
20733 declarator
->parameter_pack_p
= false;
20737 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20739 if (qualifying_scope
&& at_namespace_scope_p ()
20740 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20742 /* In the declaration of a member of a template class
20743 outside of the class itself, the SCOPE will sometimes
20744 be a TYPENAME_TYPE. For example, given:
20746 template <typename T>
20747 int S<T>::R::i = 3;
20749 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20750 this context, we must resolve S<T>::R to an ordinary
20751 type, rather than a typename type.
20753 The reason we normally avoid resolving TYPENAME_TYPEs
20754 is that a specialization of `S' might render
20755 `S<T>::R' not a type. However, if `S' is
20756 specialized, then this `i' will not be used, so there
20757 is no harm in resolving the types here. */
20760 /* Resolve the TYPENAME_TYPE. */
20761 type
= resolve_typename_type (qualifying_scope
,
20762 /*only_current_p=*/false);
20763 /* If that failed, the declarator is invalid. */
20764 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20766 if (typedef_variant_p (type
))
20767 error_at (declarator_id_start_token
->location
,
20768 "cannot define member of dependent typedef "
20771 error_at (declarator_id_start_token
->location
,
20772 "%<%T::%E%> is not a type",
20773 TYPE_CONTEXT (qualifying_scope
),
20774 TYPE_IDENTIFIER (qualifying_scope
));
20776 qualifying_scope
= type
;
20781 if (unqualified_name
)
20785 if (qualifying_scope
20786 && CLASS_TYPE_P (qualifying_scope
))
20787 class_type
= qualifying_scope
;
20789 class_type
= current_class_type
;
20791 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20793 tree name_type
= TREE_TYPE (unqualified_name
);
20795 if (!class_type
|| !same_type_p (name_type
, class_type
))
20797 /* We do not attempt to print the declarator
20798 here because we do not have enough
20799 information about its original syntactic
20801 cp_parser_error (parser
, "invalid declarator");
20802 declarator
= cp_error_declarator
;
20805 else if (qualifying_scope
20806 && CLASSTYPE_USE_TEMPLATE (name_type
))
20808 error_at (declarator_id_start_token
->location
,
20809 "invalid use of constructor as a template");
20810 inform (declarator_id_start_token
->location
,
20811 "use %<%T::%D%> instead of %<%T::%D%> to "
20812 "name the constructor in a qualified name",
20814 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20815 class_type
, name_type
);
20816 declarator
= cp_error_declarator
;
20819 unqualified_name
= constructor_name (class_type
);
20824 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20825 sfk
= sfk_destructor
;
20826 else if (identifier_p (unqualified_name
)
20827 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20828 sfk
= sfk_conversion
;
20829 else if (/* There's no way to declare a constructor
20830 for an unnamed type, even if the type
20831 got a name for linkage purposes. */
20832 !TYPE_WAS_UNNAMED (class_type
)
20833 /* Handle correctly (c++/19200):
20847 friend void N::S();
20849 && (!friend_p
|| class_type
== qualifying_scope
)
20850 && constructor_name_p (unqualified_name
,
20852 sfk
= sfk_constructor
;
20853 else if (is_overloaded_fn (unqualified_name
)
20854 && DECL_CONSTRUCTOR_P (get_first_fn
20855 (unqualified_name
)))
20856 sfk
= sfk_constructor
;
20858 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20859 *ctor_dtor_or_conv_p
= -1;
20862 declarator
= make_id_declarator (qualifying_scope
,
20864 sfk
, token
->location
);
20865 declarator
->std_attributes
= attrs
;
20866 declarator
->parameter_pack_p
= pack_expansion_p
;
20868 if (pack_expansion_p
)
20869 maybe_warn_variadic_templates ();
20872 handle_declarator
:;
20873 scope
= get_scope_of_declarator (declarator
);
20876 /* Any names that appear after the declarator-id for a
20877 member are looked up in the containing scope. */
20878 if (at_function_scope_p ())
20880 /* But declarations with qualified-ids can't appear in a
20882 cp_parser_error (parser
, "qualified-id in declaration");
20883 declarator
= cp_error_declarator
;
20886 pushed_scope
= push_scope (scope
);
20888 parser
->in_declarator_p
= true;
20889 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20890 || (declarator
&& declarator
->kind
== cdk_id
))
20891 /* Default args are only allowed on function
20893 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20895 parser
->default_arg_ok_p
= false;
20904 /* For an abstract declarator, we might wind up with nothing at this
20905 point. That's an error; the declarator is not optional. */
20907 cp_parser_error (parser
, "expected declarator");
20908 else if (open_paren
)
20910 /* Record overly parenthesized declarator so we can give a
20911 diagnostic about confusing decl/expr disambiguation. */
20912 if (declarator
->kind
== cdk_array
)
20914 /* If the open and close parens are on different lines, this
20915 is probably a formatting thing, so ignore. */
20916 expanded_location open
= expand_location (open_paren
->location
);
20917 expanded_location close
= expand_location (close_paren
->location
);
20918 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20922 declarator
->parenthesized
= open_paren
->location
;
20925 /* If we entered a scope, we must exit it now. */
20927 pop_scope (pushed_scope
);
20929 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20930 parser
->in_declarator_p
= saved_in_declarator_p
;
20935 /* Parse a ptr-operator.
20938 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20939 * cv-qualifier-seq [opt]
20941 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20942 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20947 & cv-qualifier-seq [opt]
20949 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20950 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20951 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20952 filled in with the TYPE containing the member. *CV_QUALS is
20953 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20954 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20955 Note that the tree codes returned by this function have nothing
20956 to do with the types of trees that will be eventually be created
20957 to represent the pointer or reference type being parsed. They are
20958 just constants with suggestive names. */
20959 static enum tree_code
20960 cp_parser_ptr_operator (cp_parser
* parser
,
20962 cp_cv_quals
*cv_quals
,
20965 enum tree_code code
= ERROR_MARK
;
20967 tree attrs
= NULL_TREE
;
20969 /* Assume that it's not a pointer-to-member. */
20971 /* And that there are no cv-qualifiers. */
20972 *cv_quals
= TYPE_UNQUALIFIED
;
20974 /* Peek at the next token. */
20975 token
= cp_lexer_peek_token (parser
->lexer
);
20977 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20978 if (token
->type
== CPP_MULT
)
20979 code
= INDIRECT_REF
;
20980 else if (token
->type
== CPP_AND
)
20982 else if ((cxx_dialect
!= cxx98
) &&
20983 token
->type
== CPP_AND_AND
) /* C++0x only */
20984 code
= NON_LVALUE_EXPR
;
20986 if (code
!= ERROR_MARK
)
20988 /* Consume the `*', `&' or `&&'. */
20989 cp_lexer_consume_token (parser
->lexer
);
20991 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20992 `&', if we are allowing GNU extensions. (The only qualifier
20993 that can legally appear after `&' is `restrict', but that is
20994 enforced during semantic analysis. */
20995 if (code
== INDIRECT_REF
20996 || cp_parser_allow_gnu_extensions_p (parser
))
20997 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20999 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21000 if (attributes
!= NULL
)
21001 *attributes
= attrs
;
21005 /* Try the pointer-to-member case. */
21006 cp_parser_parse_tentatively (parser
);
21007 /* Look for the optional `::' operator. */
21008 cp_parser_global_scope_opt (parser
,
21009 /*current_scope_valid_p=*/false);
21010 /* Look for the nested-name specifier. */
21011 token
= cp_lexer_peek_token (parser
->lexer
);
21012 cp_parser_nested_name_specifier (parser
,
21013 /*typename_keyword_p=*/false,
21014 /*check_dependency_p=*/true,
21016 /*is_declaration=*/false);
21017 /* If we found it, and the next token is a `*', then we are
21018 indeed looking at a pointer-to-member operator. */
21019 if (!cp_parser_error_occurred (parser
)
21020 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21022 /* Indicate that the `*' operator was used. */
21023 code
= INDIRECT_REF
;
21025 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21026 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21027 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21028 error_at (token
->location
, "cannot form pointer to member of "
21029 "non-class %q#T", parser
->scope
);
21032 /* The type of which the member is a member is given by the
21034 *type
= parser
->scope
;
21035 /* The next name will not be qualified. */
21036 parser
->scope
= NULL_TREE
;
21037 parser
->qualifying_scope
= NULL_TREE
;
21038 parser
->object_scope
= NULL_TREE
;
21039 /* Look for optional c++11 attributes. */
21040 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21041 if (attributes
!= NULL
)
21042 *attributes
= attrs
;
21043 /* Look for the optional cv-qualifier-seq. */
21044 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21047 /* If that didn't work we don't have a ptr-operator. */
21048 if (!cp_parser_parse_definitely (parser
))
21049 cp_parser_error (parser
, "expected ptr-operator");
21055 /* Parse an (optional) cv-qualifier-seq.
21058 cv-qualifier cv-qualifier-seq [opt]
21069 Returns a bitmask representing the cv-qualifiers. */
21072 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21074 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21079 cp_cv_quals cv_qualifier
;
21081 /* Peek at the next token. */
21082 token
= cp_lexer_peek_token (parser
->lexer
);
21083 /* See if it's a cv-qualifier. */
21084 switch (token
->keyword
)
21087 cv_qualifier
= TYPE_QUAL_CONST
;
21091 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21095 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21099 cv_qualifier
= TYPE_UNQUALIFIED
;
21106 if (cv_quals
& cv_qualifier
)
21108 gcc_rich_location
richloc (token
->location
);
21109 richloc
.add_fixit_remove ();
21110 error_at (&richloc
, "duplicate cv-qualifier");
21111 cp_lexer_purge_token (parser
->lexer
);
21115 cp_lexer_consume_token (parser
->lexer
);
21116 cv_quals
|= cv_qualifier
;
21123 /* Parse an (optional) ref-qualifier
21129 Returns cp_ref_qualifier representing ref-qualifier. */
21131 static cp_ref_qualifier
21132 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21134 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21136 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21137 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21142 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21145 switch (token
->type
)
21148 curr_ref_qual
= REF_QUAL_LVALUE
;
21152 curr_ref_qual
= REF_QUAL_RVALUE
;
21156 curr_ref_qual
= REF_QUAL_NONE
;
21160 if (!curr_ref_qual
)
21164 error_at (token
->location
, "multiple ref-qualifiers");
21165 cp_lexer_purge_token (parser
->lexer
);
21169 ref_qual
= curr_ref_qual
;
21170 cp_lexer_consume_token (parser
->lexer
);
21177 /* Parse an optional tx-qualifier.
21181 transaction_safe_dynamic */
21184 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21186 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21187 if (token
->type
== CPP_NAME
)
21189 tree name
= token
->u
.value
;
21190 const char *p
= IDENTIFIER_POINTER (name
);
21191 const int len
= strlen ("transaction_safe");
21192 if (!strncmp (p
, "transaction_safe", len
))
21196 || !strcmp (p
, "_dynamic"))
21198 cp_lexer_consume_token (parser
->lexer
);
21201 error ("%qE requires %<-fgnu-tm%>", name
);
21212 /* Parse an (optional) virt-specifier-seq.
21214 virt-specifier-seq:
21215 virt-specifier virt-specifier-seq [opt]
21221 Returns a bitmask representing the virt-specifiers. */
21223 static cp_virt_specifiers
21224 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21226 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21231 cp_virt_specifiers virt_specifier
;
21233 /* Peek at the next token. */
21234 token
= cp_lexer_peek_token (parser
->lexer
);
21235 /* See if it's a virt-specifier-qualifier. */
21236 if (token
->type
!= CPP_NAME
)
21238 if (id_equal (token
->u
.value
, "override"))
21240 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21241 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21243 else if (id_equal (token
->u
.value
, "final"))
21245 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21246 virt_specifier
= VIRT_SPEC_FINAL
;
21248 else if (id_equal (token
->u
.value
, "__final"))
21250 virt_specifier
= VIRT_SPEC_FINAL
;
21255 if (virt_specifiers
& virt_specifier
)
21257 gcc_rich_location
richloc (token
->location
);
21258 richloc
.add_fixit_remove ();
21259 error_at (&richloc
, "duplicate virt-specifier");
21260 cp_lexer_purge_token (parser
->lexer
);
21264 cp_lexer_consume_token (parser
->lexer
);
21265 virt_specifiers
|= virt_specifier
;
21268 return virt_specifiers
;
21271 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21272 is in scope even though it isn't real. */
21275 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21279 if (current_class_ptr
)
21281 /* We don't clear this between NSDMIs. Is it already what we want? */
21282 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21283 if (DECL_P (current_class_ptr
)
21284 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21285 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21286 && cp_type_quals (type
) == quals
)
21290 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21291 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21292 current_class_ptr
= NULL_TREE
;
21294 = cp_build_fold_indirect_ref (this_parm
);
21295 current_class_ptr
= this_parm
;
21298 /* Return true iff our current scope is a non-static data member
21302 parsing_nsdmi (void)
21304 /* We recognize NSDMI context by the context-less 'this' pointer set up
21305 by the function above. */
21306 if (current_class_ptr
21307 && TREE_CODE (current_class_ptr
) == PARM_DECL
21308 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21313 /* Parse a late-specified return type, if any. This is not a separate
21314 non-terminal, but part of a function declarator, which looks like
21316 -> trailing-type-specifier-seq abstract-declarator(opt)
21318 Returns the type indicated by the type-id.
21320 In addition to this, parse any queued up #pragma omp declare simd
21321 clauses, and #pragma acc routine clauses.
21323 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21327 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21328 tree
& requires_clause
, cp_cv_quals quals
)
21331 tree type
= NULL_TREE
;
21332 bool declare_simd_p
= (parser
->omp_declare_simd
21334 && declarator
->kind
== cdk_id
);
21336 bool oacc_routine_p
= (parser
->oacc_routine
21338 && declarator
->kind
== cdk_id
);
21340 /* Peek at the next token. */
21341 token
= cp_lexer_peek_token (parser
->lexer
);
21342 /* A late-specified return type is indicated by an initial '->'. */
21343 if (token
->type
!= CPP_DEREF
21344 && token
->keyword
!= RID_REQUIRES
21345 && !(token
->type
== CPP_NAME
21346 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21347 && !(declare_simd_p
|| oacc_routine_p
))
21350 tree save_ccp
= current_class_ptr
;
21351 tree save_ccr
= current_class_ref
;
21354 /* DR 1207: 'this' is in scope in the trailing return type. */
21355 inject_this_parameter (current_class_type
, quals
);
21358 if (token
->type
== CPP_DEREF
)
21360 /* Consume the ->. */
21361 cp_lexer_consume_token (parser
->lexer
);
21363 type
= cp_parser_trailing_type_id (parser
);
21366 /* Function declarations may be followed by a trailing
21367 requires-clause. */
21368 requires_clause
= cp_parser_requires_clause_opt (parser
);
21370 if (declare_simd_p
)
21371 declarator
->attributes
21372 = cp_parser_late_parsing_omp_declare_simd (parser
,
21373 declarator
->attributes
);
21374 if (oacc_routine_p
)
21375 declarator
->attributes
21376 = cp_parser_late_parsing_oacc_routine (parser
,
21377 declarator
->attributes
);
21381 current_class_ptr
= save_ccp
;
21382 current_class_ref
= save_ccr
;
21388 /* Parse a declarator-id.
21392 :: [opt] nested-name-specifier [opt] type-name
21394 In the `id-expression' case, the value returned is as for
21395 cp_parser_id_expression if the id-expression was an unqualified-id.
21396 If the id-expression was a qualified-id, then a SCOPE_REF is
21397 returned. The first operand is the scope (either a NAMESPACE_DECL
21398 or TREE_TYPE), but the second is still just a representation of an
21402 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21405 /* The expression must be an id-expression. Assume that qualified
21406 names are the names of types so that:
21409 int S<T>::R::i = 3;
21411 will work; we must treat `S<T>::R' as the name of a type.
21412 Similarly, assume that qualified names are templates, where
21416 int S<T>::R<T>::i = 3;
21419 id
= cp_parser_id_expression (parser
,
21420 /*template_keyword_p=*/false,
21421 /*check_dependency_p=*/false,
21422 /*template_p=*/NULL
,
21423 /*declarator_p=*/true,
21425 if (id
&& BASELINK_P (id
))
21426 id
= BASELINK_FUNCTIONS (id
);
21430 /* Parse a type-id.
21433 type-specifier-seq abstract-declarator [opt]
21435 Returns the TYPE specified. */
21438 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21439 bool is_trailing_return
, location_t
* type_location
)
21441 cp_decl_specifier_seq type_specifier_seq
;
21442 cp_declarator
*abstract_declarator
;
21444 /* Parse the type-specifier-seq. */
21445 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21446 is_trailing_return
,
21447 &type_specifier_seq
);
21449 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21451 if (is_template_arg
&& type_specifier_seq
.type
21452 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21453 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21454 /* A bare template name as a template argument is a template template
21455 argument, not a placeholder, so fail parsing it as a type argument. */
21457 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21458 cp_parser_simulate_error (parser
);
21459 return error_mark_node
;
21461 if (type_specifier_seq
.type
== error_mark_node
)
21462 return error_mark_node
;
21464 /* There might or might not be an abstract declarator. */
21465 cp_parser_parse_tentatively (parser
);
21466 /* Look for the declarator. */
21467 abstract_declarator
21468 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21469 /*parenthesized_p=*/NULL
,
21470 /*member_p=*/false,
21471 /*friend_p=*/false);
21472 /* Check to see if there really was a declarator. */
21473 if (!cp_parser_parse_definitely (parser
))
21474 abstract_declarator
= NULL
;
21476 if (type_specifier_seq
.type
21477 /* The concepts TS allows 'auto' as a type-id. */
21478 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21479 /* None of the valid uses of 'auto' in C++14 involve the type-id
21480 nonterminal, but it is valid in a trailing-return-type. */
21481 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21482 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21484 /* A type-id with type 'auto' is only ok if the abstract declarator
21485 is a function declarator with a late-specified return type.
21487 A type-id with 'auto' is also valid in a trailing-return-type
21488 in a compound-requirement. */
21489 if (abstract_declarator
21490 && abstract_declarator
->kind
== cdk_function
21491 && abstract_declarator
->u
.function
.late_return_type
)
21493 else if (parser
->in_result_type_constraint_p
)
21497 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21498 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21500 error_at (loc
, "missing template arguments after %qT",
21502 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21506 error_at (loc
, "invalid use of %qT", auto_node
);
21507 return error_mark_node
;
21511 return groktypename (&type_specifier_seq
, abstract_declarator
,
21516 cp_parser_type_id (cp_parser
*parser
, location_t
* type_location
)
21518 return cp_parser_type_id_1 (parser
, false, false, type_location
);
21522 cp_parser_template_type_arg (cp_parser
*parser
)
21525 const char *saved_message
= parser
->type_definition_forbidden_message
;
21526 parser
->type_definition_forbidden_message
21527 = G_("types may not be defined in template arguments");
21528 r
= cp_parser_type_id_1 (parser
, true, false, NULL
);
21529 parser
->type_definition_forbidden_message
= saved_message
;
21530 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21532 error ("invalid use of %<auto%> in template argument");
21533 r
= error_mark_node
;
21539 cp_parser_trailing_type_id (cp_parser
*parser
)
21541 return cp_parser_type_id_1 (parser
, false, true, NULL
);
21544 /* Parse a type-specifier-seq.
21546 type-specifier-seq:
21547 type-specifier type-specifier-seq [opt]
21551 type-specifier-seq:
21552 attributes type-specifier-seq [opt]
21554 If IS_DECLARATION is true, we are at the start of a "condition" or
21555 exception-declaration, so we might be followed by a declarator-id.
21557 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21558 i.e. we've just seen "->".
21560 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21563 cp_parser_type_specifier_seq (cp_parser
* parser
,
21564 bool is_declaration
,
21565 bool is_trailing_return
,
21566 cp_decl_specifier_seq
*type_specifier_seq
)
21568 bool seen_type_specifier
= false;
21569 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21570 cp_token
*start_token
= NULL
;
21572 /* Clear the TYPE_SPECIFIER_SEQ. */
21573 clear_decl_specs (type_specifier_seq
);
21575 /* In the context of a trailing return type, enum E { } is an
21576 elaborated-type-specifier followed by a function-body, not an
21578 if (is_trailing_return
)
21579 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21581 /* Parse the type-specifiers and attributes. */
21584 tree type_specifier
;
21585 bool is_cv_qualifier
;
21587 /* Check for attributes first. */
21588 if (cp_next_tokens_can_be_attribute_p (parser
))
21590 type_specifier_seq
->attributes
21591 = attr_chainon (type_specifier_seq
->attributes
,
21592 cp_parser_attributes_opt (parser
));
21596 /* record the token of the beginning of the type specifier seq,
21597 for error reporting purposes*/
21599 start_token
= cp_lexer_peek_token (parser
->lexer
);
21601 /* Look for the type-specifier. */
21602 type_specifier
= cp_parser_type_specifier (parser
,
21604 type_specifier_seq
,
21605 /*is_declaration=*/false,
21608 if (!type_specifier
)
21610 /* If the first type-specifier could not be found, this is not a
21611 type-specifier-seq at all. */
21612 if (!seen_type_specifier
)
21614 /* Set in_declarator_p to avoid skipping to the semicolon. */
21615 int in_decl
= parser
->in_declarator_p
;
21616 parser
->in_declarator_p
= true;
21618 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21619 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21620 cp_parser_error (parser
, "expected type-specifier");
21622 parser
->in_declarator_p
= in_decl
;
21624 type_specifier_seq
->type
= error_mark_node
;
21627 /* If subsequent type-specifiers could not be found, the
21628 type-specifier-seq is complete. */
21632 seen_type_specifier
= true;
21633 /* The standard says that a condition can be:
21635 type-specifier-seq declarator = assignment-expression
21642 we should treat the "S" as a declarator, not as a
21643 type-specifier. The standard doesn't say that explicitly for
21644 type-specifier-seq, but it does say that for
21645 decl-specifier-seq in an ordinary declaration. Perhaps it
21646 would be clearer just to allow a decl-specifier-seq here, and
21647 then add a semantic restriction that if any decl-specifiers
21648 that are not type-specifiers appear, the program is invalid. */
21649 if (is_declaration
&& !is_cv_qualifier
)
21650 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21654 /* Return whether the function currently being declared has an associated
21655 template parameter list. */
21658 function_being_declared_is_template_p (cp_parser
* parser
)
21660 if (!current_template_parms
|| processing_template_parmlist
)
21663 if (parser
->implicit_template_scope
)
21666 if (at_class_scope_p ()
21667 && TYPE_BEING_DEFINED (current_class_type
))
21668 return parser
->num_template_parameter_lists
!= 0;
21670 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21671 (current_class_type
));
21674 /* Parse a parameter-declaration-clause.
21676 parameter-declaration-clause:
21677 parameter-declaration-list [opt] ... [opt]
21678 parameter-declaration-list , ...
21680 Returns a representation for the parameter declarations. A return
21681 value of NULL indicates a parameter-declaration-clause consisting
21682 only of an ellipsis. */
21685 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21691 temp_override
<bool> cleanup
21692 (parser
->auto_is_implicit_function_template_parm_p
);
21694 if (!processing_specialization
21695 && !processing_template_parmlist
21696 && !processing_explicit_instantiation
21697 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21698 actual function or a random abstract declarator. */
21699 && parser
->default_arg_ok_p
)
21700 if (!current_function_decl
21701 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21702 parser
->auto_is_implicit_function_template_parm_p
= true;
21704 /* Peek at the next token. */
21705 token
= cp_lexer_peek_token (parser
->lexer
);
21706 /* Check for trivial parameter-declaration-clauses. */
21707 if (token
->type
== CPP_ELLIPSIS
)
21709 /* Consume the `...' token. */
21710 cp_lexer_consume_token (parser
->lexer
);
21713 else if (token
->type
== CPP_CLOSE_PAREN
)
21714 /* There are no parameters. */
21715 return void_list_node
;
21716 /* Check for `(void)', too, which is a special case. */
21717 else if (token
->keyword
== RID_VOID
21718 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21719 == CPP_CLOSE_PAREN
))
21721 /* Consume the `void' token. */
21722 cp_lexer_consume_token (parser
->lexer
);
21723 /* There are no parameters. */
21724 return void_list_node
;
21727 /* Parse the parameter-declaration-list. */
21728 parameters
= cp_parser_parameter_declaration_list (parser
);
21729 /* If a parse error occurred while parsing the
21730 parameter-declaration-list, then the entire
21731 parameter-declaration-clause is erroneous. */
21732 if (parameters
== error_mark_node
)
21735 /* Peek at the next token. */
21736 token
= cp_lexer_peek_token (parser
->lexer
);
21737 /* If it's a `,', the clause should terminate with an ellipsis. */
21738 if (token
->type
== CPP_COMMA
)
21740 /* Consume the `,'. */
21741 cp_lexer_consume_token (parser
->lexer
);
21742 /* Expect an ellipsis. */
21744 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21746 /* It might also be `...' if the optional trailing `,' was
21748 else if (token
->type
== CPP_ELLIPSIS
)
21750 /* Consume the `...' token. */
21751 cp_lexer_consume_token (parser
->lexer
);
21752 /* And remember that we saw it. */
21756 ellipsis_p
= false;
21758 /* Finish the parameter list. */
21760 parameters
= chainon (parameters
, void_list_node
);
21765 /* Parse a parameter-declaration-list.
21767 parameter-declaration-list:
21768 parameter-declaration
21769 parameter-declaration-list , parameter-declaration
21771 Returns a representation of the parameter-declaration-list, as for
21772 cp_parser_parameter_declaration_clause. However, the
21773 `void_list_node' is never appended to the list. */
21776 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21778 tree parameters
= NULL_TREE
;
21779 tree
*tail
= ¶meters
;
21780 bool saved_in_unbraced_linkage_specification_p
;
21783 /* The special considerations that apply to a function within an
21784 unbraced linkage specifications do not apply to the parameters
21785 to the function. */
21786 saved_in_unbraced_linkage_specification_p
21787 = parser
->in_unbraced_linkage_specification_p
;
21788 parser
->in_unbraced_linkage_specification_p
= false;
21790 /* Look for more parameters. */
21793 cp_parameter_declarator
*parameter
;
21794 tree decl
= error_mark_node
;
21795 bool parenthesized_p
= false;
21797 /* Parse the parameter. */
21799 = cp_parser_parameter_declaration (parser
,
21800 /*template_parm_p=*/false,
21803 /* We don't know yet if the enclosing context is deprecated, so wait
21804 and warn in grokparms if appropriate. */
21805 deprecated_state
= DEPRECATED_SUPPRESS
;
21809 decl
= grokdeclarator (parameter
->declarator
,
21810 ¶meter
->decl_specifiers
,
21812 parameter
->default_argument
!= NULL_TREE
,
21813 ¶meter
->decl_specifiers
.attributes
);
21814 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21815 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21818 deprecated_state
= DEPRECATED_NORMAL
;
21820 /* If a parse error occurred parsing the parameter declaration,
21821 then the entire parameter-declaration-list is erroneous. */
21822 if (decl
== error_mark_node
)
21824 parameters
= error_mark_node
;
21828 if (parameter
->decl_specifiers
.attributes
)
21829 cplus_decl_attributes (&decl
,
21830 parameter
->decl_specifiers
.attributes
,
21832 if (DECL_NAME (decl
))
21833 decl
= pushdecl (decl
);
21835 if (decl
!= error_mark_node
)
21837 retrofit_lang_decl (decl
);
21838 DECL_PARM_INDEX (decl
) = ++index
;
21839 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21842 /* Add the new parameter to the list. */
21843 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21844 tail
= &TREE_CHAIN (*tail
);
21846 /* Peek at the next token. */
21847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21848 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21849 /* These are for Objective-C++ */
21850 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21851 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21852 /* The parameter-declaration-list is complete. */
21854 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21858 /* Peek at the next token. */
21859 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21860 /* If it's an ellipsis, then the list is complete. */
21861 if (token
->type
== CPP_ELLIPSIS
)
21863 /* Otherwise, there must be more parameters. Consume the
21865 cp_lexer_consume_token (parser
->lexer
);
21866 /* When parsing something like:
21868 int i(float f, double d)
21870 we can tell after seeing the declaration for "f" that we
21871 are not looking at an initialization of a variable "i",
21872 but rather at the declaration of a function "i".
21874 Due to the fact that the parsing of template arguments
21875 (as specified to a template-id) requires backtracking we
21876 cannot use this technique when inside a template argument
21878 if (!parser
->in_template_argument_list_p
21879 && !parser
->in_type_id_in_expr_p
21880 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21881 /* However, a parameter-declaration of the form
21882 "float(f)" (which is a valid declaration of a
21883 parameter "f") can also be interpreted as an
21884 expression (the conversion of "f" to "float"). */
21885 && !parenthesized_p
)
21886 cp_parser_commit_to_tentative_parse (parser
);
21890 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21891 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21892 cp_parser_skip_to_closing_parenthesis (parser
,
21893 /*recovering=*/true,
21894 /*or_comma=*/false,
21895 /*consume_paren=*/false);
21900 parser
->in_unbraced_linkage_specification_p
21901 = saved_in_unbraced_linkage_specification_p
;
21903 /* Reset implicit_template_scope if we are about to leave the function
21904 parameter list that introduced it. Note that for out-of-line member
21905 definitions, there will be one or more class scopes before we get to
21906 the template parameter scope. */
21908 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21909 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21911 while (maybe_its
->kind
== sk_class
)
21912 maybe_its
= maybe_its
->level_chain
;
21913 if (maybe_its
== its
)
21915 parser
->implicit_template_parms
= 0;
21916 parser
->implicit_template_scope
= 0;
21923 /* Parse a parameter declaration.
21925 parameter-declaration:
21926 decl-specifier-seq ... [opt] declarator
21927 decl-specifier-seq declarator = assignment-expression
21928 decl-specifier-seq ... [opt] abstract-declarator [opt]
21929 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21931 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21932 declares a template parameter. (In that case, a non-nested `>'
21933 token encountered during the parsing of the assignment-expression
21934 is not interpreted as a greater-than operator.)
21936 Returns a representation of the parameter, or NULL if an error
21937 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21938 true iff the declarator is of the form "(p)". */
21940 static cp_parameter_declarator
*
21941 cp_parser_parameter_declaration (cp_parser
*parser
,
21942 bool template_parm_p
,
21943 bool *parenthesized_p
)
21945 int declares_class_or_enum
;
21946 cp_decl_specifier_seq decl_specifiers
;
21947 cp_declarator
*declarator
;
21948 tree default_argument
;
21949 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21950 const char *saved_message
;
21951 bool template_parameter_pack_p
= false;
21953 /* In a template parameter, `>' is not an operator.
21957 When parsing a default template-argument for a non-type
21958 template-parameter, the first non-nested `>' is taken as the end
21959 of the template parameter-list rather than a greater-than
21962 /* Type definitions may not appear in parameter types. */
21963 saved_message
= parser
->type_definition_forbidden_message
;
21964 parser
->type_definition_forbidden_message
21965 = G_("types may not be defined in parameter types");
21967 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21968 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21969 (current_template_parms
)) : 0);
21971 /* Parse the declaration-specifiers. */
21972 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21973 cp_parser_decl_specifier_seq (parser
,
21974 CP_PARSER_FLAGS_NONE
,
21976 &declares_class_or_enum
);
21978 /* Complain about missing 'typename' or other invalid type names. */
21979 if (!decl_specifiers
.any_type_specifiers_p
21980 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21981 decl_specifiers
.type
= error_mark_node
;
21983 /* If an error occurred, there's no reason to attempt to parse the
21984 rest of the declaration. */
21985 if (cp_parser_error_occurred (parser
))
21987 parser
->type_definition_forbidden_message
= saved_message
;
21991 /* Peek at the next token. */
21992 token
= cp_lexer_peek_token (parser
->lexer
);
21994 /* If the next token is a `)', `,', `=', `>', or `...', then there
21995 is no declarator. However, when variadic templates are enabled,
21996 there may be a declarator following `...'. */
21997 if (token
->type
== CPP_CLOSE_PAREN
21998 || token
->type
== CPP_COMMA
21999 || token
->type
== CPP_EQ
22000 || token
->type
== CPP_GREATER
)
22003 if (parenthesized_p
)
22004 *parenthesized_p
= false;
22006 /* Otherwise, there should be a declarator. */
22009 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22010 parser
->default_arg_ok_p
= false;
22012 /* After seeing a decl-specifier-seq, if the next token is not a
22013 "(", there is no possibility that the code is a valid
22014 expression. Therefore, if parsing tentatively, we commit at
22016 if (!parser
->in_template_argument_list_p
22017 /* In an expression context, having seen:
22021 we cannot be sure whether we are looking at a
22022 function-type (taking a "char" as a parameter) or a cast
22023 of some object of type "char" to "int". */
22024 && !parser
->in_type_id_in_expr_p
22025 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22026 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22027 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22028 cp_parser_commit_to_tentative_parse (parser
);
22029 /* Parse the declarator. */
22030 declarator_token_start
= token
;
22031 declarator
= cp_parser_declarator (parser
,
22032 CP_PARSER_DECLARATOR_EITHER
,
22033 /*ctor_dtor_or_conv_p=*/NULL
,
22035 /*member_p=*/false,
22036 /*friend_p=*/false);
22037 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22038 /* After the declarator, allow more attributes. */
22039 decl_specifiers
.attributes
22040 = attr_chainon (decl_specifiers
.attributes
,
22041 cp_parser_attributes_opt (parser
));
22043 /* If the declarator is a template parameter pack, remember that and
22044 clear the flag in the declarator itself so we don't get errors
22045 from grokdeclarator. */
22046 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22048 declarator
->parameter_pack_p
= false;
22049 template_parameter_pack_p
= true;
22053 /* If the next token is an ellipsis, and we have not seen a declarator
22054 name, and if either the type of the declarator contains parameter
22055 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22056 for, eg, abbreviated integral type names), then we actually have a
22057 parameter pack expansion expression. Otherwise, leave the ellipsis
22058 for a C-style variadic function. */
22059 token
= cp_lexer_peek_token (parser
->lexer
);
22061 /* If a function parameter pack was specified and an implicit template
22062 parameter was introduced during cp_parser_parameter_declaration,
22063 change any implicit parameters introduced into packs. */
22064 if (parser
->implicit_template_parms
22065 && ((token
->type
== CPP_ELLIPSIS
22066 && declarator_can_be_parameter_pack (declarator
))
22067 || (declarator
&& declarator
->parameter_pack_p
)))
22069 int latest_template_parm_idx
= TREE_VEC_LENGTH
22070 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22072 if (latest_template_parm_idx
!= template_parm_idx
)
22073 decl_specifiers
.type
= convert_generic_types_to_packs
22074 (decl_specifiers
.type
,
22075 template_parm_idx
, latest_template_parm_idx
);
22078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22080 tree type
= decl_specifiers
.type
;
22082 if (type
&& DECL_P (type
))
22083 type
= TREE_TYPE (type
);
22086 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22087 && (template_parm_p
|| uses_parameter_packs (type
)))
22088 || (!type
&& template_parm_p
))
22089 && declarator_can_be_parameter_pack (declarator
))
22091 /* Consume the `...'. */
22092 cp_lexer_consume_token (parser
->lexer
);
22093 maybe_warn_variadic_templates ();
22095 /* Build a pack expansion type */
22096 if (template_parm_p
)
22097 template_parameter_pack_p
= true;
22098 else if (declarator
)
22099 declarator
->parameter_pack_p
= true;
22101 decl_specifiers
.type
= make_pack_expansion (type
);
22105 /* The restriction on defining new types applies only to the type
22106 of the parameter, not to the default argument. */
22107 parser
->type_definition_forbidden_message
= saved_message
;
22109 /* If the next token is `=', then process a default argument. */
22110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22112 tree type
= decl_specifiers
.type
;
22113 token
= cp_lexer_peek_token (parser
->lexer
);
22114 /* If we are defining a class, then the tokens that make up the
22115 default argument must be saved and processed later. */
22116 if (!template_parm_p
&& at_class_scope_p ()
22117 && TYPE_BEING_DEFINED (current_class_type
)
22118 && !LAMBDA_TYPE_P (current_class_type
))
22119 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22121 // A constrained-type-specifier may declare a type template-parameter.
22122 else if (declares_constrained_type_template_parameter (type
))
22124 = cp_parser_default_type_template_argument (parser
);
22126 // A constrained-type-specifier may declare a template-template-parameter.
22127 else if (declares_constrained_template_template_parameter (type
))
22129 = cp_parser_default_template_template_argument (parser
);
22131 /* Outside of a class definition, we can just parse the
22132 assignment-expression. */
22135 = cp_parser_default_argument (parser
, template_parm_p
);
22137 if (!parser
->default_arg_ok_p
)
22139 permerror (token
->location
,
22140 "default arguments are only "
22141 "permitted for function parameters");
22143 else if ((declarator
&& declarator
->parameter_pack_p
)
22144 || template_parameter_pack_p
22145 || (decl_specifiers
.type
22146 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22148 /* Find the name of the parameter pack. */
22149 cp_declarator
*id_declarator
= declarator
;
22150 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22151 id_declarator
= id_declarator
->declarator
;
22153 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22154 error_at (declarator_token_start
->location
,
22156 ? G_("template parameter pack %qD "
22157 "cannot have a default argument")
22158 : G_("parameter pack %qD cannot have "
22159 "a default argument"),
22160 id_declarator
->u
.id
.unqualified_name
);
22162 error_at (declarator_token_start
->location
,
22164 ? G_("template parameter pack cannot have "
22165 "a default argument")
22166 : G_("parameter pack cannot have a "
22167 "default argument"));
22169 default_argument
= NULL_TREE
;
22173 default_argument
= NULL_TREE
;
22175 /* Generate a location for the parameter, ranging from the start of the
22176 initial token to the end of the final token (using input_location for
22177 the latter, set up by cp_lexer_set_source_position_from_token when
22180 If we have a identifier, then use it for the caret location, e.g.
22182 extern int callee (int one, int (*two)(int, int), float three);
22183 ~~~~~~^~~~~~~~~~~~~~
22185 otherwise, reuse the start location for the caret location e.g.:
22187 extern int callee (int one, int (*)(int, int), float three);
22191 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22192 ? declarator
->id_loc
22193 : decl_spec_token_start
->location
);
22194 location_t param_loc
= make_location (caret_loc
,
22195 decl_spec_token_start
->location
,
22198 return make_parameter_declarator (&decl_specifiers
,
22202 template_parameter_pack_p
);
22205 /* Parse a default argument and return it.
22207 TEMPLATE_PARM_P is true if this is a default argument for a
22208 non-type template parameter. */
22210 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22212 tree default_argument
= NULL_TREE
;
22213 bool saved_greater_than_is_operator_p
;
22214 bool saved_local_variables_forbidden_p
;
22215 bool non_constant_p
, is_direct_init
;
22217 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22219 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22220 parser
->greater_than_is_operator_p
= !template_parm_p
;
22221 /* Local variable names (and the `this' keyword) may not
22222 appear in a default argument. */
22223 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22224 parser
->local_variables_forbidden_p
= true;
22225 /* Parse the assignment-expression. */
22226 if (template_parm_p
)
22227 push_deferring_access_checks (dk_no_deferred
);
22228 tree saved_class_ptr
= NULL_TREE
;
22229 tree saved_class_ref
= NULL_TREE
;
22230 /* The "this" pointer is not valid in a default argument. */
22233 saved_class_ptr
= current_class_ptr
;
22234 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22235 saved_class_ref
= current_class_ref
;
22236 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22239 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22240 /* Restore the "this" pointer. */
22243 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22244 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22246 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22247 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22248 if (template_parm_p
)
22249 pop_deferring_access_checks ();
22250 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22251 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22253 return default_argument
;
22256 /* Parse a function-body.
22259 compound_statement */
22262 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22264 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22265 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22269 /* Parse a ctor-initializer-opt followed by a function-body. Return
22270 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22271 is true we are parsing a function-try-block. */
22274 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22275 bool in_function_try_block
)
22278 const bool check_body_p
=
22279 DECL_CONSTRUCTOR_P (current_function_decl
)
22280 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22283 /* Begin the function body. */
22284 body
= begin_function_body ();
22285 /* Parse the optional ctor-initializer. */
22286 cp_parser_ctor_initializer_opt (parser
);
22288 /* If we're parsing a constexpr constructor definition, we need
22289 to check that the constructor body is indeed empty. However,
22290 before we get to cp_parser_function_body lot of junk has been
22291 generated, so we can't just check that we have an empty block.
22292 Rather we take a snapshot of the outermost block, and check whether
22293 cp_parser_function_body changed its state. */
22296 list
= cur_stmt_list
;
22297 if (STATEMENT_LIST_TAIL (list
))
22298 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22300 /* Parse the function-body. */
22301 cp_parser_function_body (parser
, in_function_try_block
);
22303 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22304 /* Finish the function body. */
22305 finish_function_body (body
);
22308 /* Parse an initializer.
22311 = initializer-clause
22312 ( expression-list )
22314 Returns an expression representing the initializer. If no
22315 initializer is present, NULL_TREE is returned.
22317 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22318 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22319 set to TRUE if there is no initializer present. If there is an
22320 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22321 is set to true; otherwise it is set to false. */
22324 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22325 bool* non_constant_p
, bool subexpression_p
)
22330 /* Peek at the next token. */
22331 token
= cp_lexer_peek_token (parser
->lexer
);
22333 /* Let our caller know whether or not this initializer was
22335 *is_direct_init
= (token
->type
!= CPP_EQ
);
22336 /* Assume that the initializer is constant. */
22337 *non_constant_p
= false;
22339 if (token
->type
== CPP_EQ
)
22341 /* Consume the `='. */
22342 cp_lexer_consume_token (parser
->lexer
);
22343 /* Parse the initializer-clause. */
22344 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22346 else if (token
->type
== CPP_OPEN_PAREN
)
22348 vec
<tree
, va_gc
> *vec
;
22349 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22351 /*allow_expansion_p=*/true,
22354 return error_mark_node
;
22355 init
= build_tree_list_vec (vec
);
22356 release_tree_vector (vec
);
22358 else if (token
->type
== CPP_OPEN_BRACE
)
22360 cp_lexer_set_source_position (parser
->lexer
);
22361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22362 init
= cp_parser_braced_list (parser
, non_constant_p
);
22363 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22367 /* Anything else is an error. */
22368 cp_parser_error (parser
, "expected initializer");
22369 init
= error_mark_node
;
22372 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22373 init
= error_mark_node
;
22378 /* Parse an initializer-clause.
22380 initializer-clause:
22381 assignment-expression
22384 Returns an expression representing the initializer.
22386 If the `assignment-expression' production is used the value
22387 returned is simply a representation for the expression.
22389 Otherwise, calls cp_parser_braced_list. */
22392 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22394 cp_expr initializer
;
22396 /* Assume the expression is constant. */
22397 *non_constant_p
= false;
22399 /* If it is not a `{', then we are looking at an
22400 assignment-expression. */
22401 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22404 = cp_parser_constant_expression (parser
,
22405 /*allow_non_constant_p=*/true,
22409 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22411 return initializer
;
22414 /* Parse a brace-enclosed initializer list.
22417 { initializer-list , [opt] }
22418 { designated-initializer-list , [opt] }
22421 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22422 the elements of the initializer-list (or NULL, if the last
22423 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22424 NULL_TREE. There is no way to detect whether or not the optional
22425 trailing `,' was provided. NON_CONSTANT_P is as for
22426 cp_parser_initializer. */
22429 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22432 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22434 /* Consume the `{' token. */
22435 matching_braces braces
;
22436 braces
.require_open (parser
);
22437 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22438 initializer
= make_node (CONSTRUCTOR
);
22439 /* If it's not a `}', then there is a non-trivial initializer. */
22440 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22442 /* Parse the initializer list. */
22443 CONSTRUCTOR_ELTS (initializer
)
22444 = cp_parser_initializer_list (parser
, non_constant_p
);
22445 /* A trailing `,' token is allowed. */
22446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22447 cp_lexer_consume_token (parser
->lexer
);
22450 *non_constant_p
= false;
22451 /* Now, there should be a trailing `}'. */
22452 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22453 braces
.require_close (parser
);
22454 TREE_TYPE (initializer
) = init_list_type_node
;
22456 cp_expr
result (initializer
);
22457 /* Build a location of the form:
22460 with caret==start at the open brace, finish at the close brace. */
22461 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22462 result
.set_location (combined_loc
);
22466 /* Consume tokens up to, and including, the next non-nested closing `]'.
22467 Returns true iff we found a closing `]'. */
22470 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22472 unsigned square_depth
= 0;
22476 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22478 switch (token
->type
)
22481 case CPP_PRAGMA_EOL
:
22482 /* If we've run out of tokens, then there is no closing `]'. */
22485 case CPP_OPEN_SQUARE
:
22489 case CPP_CLOSE_SQUARE
:
22490 if (!square_depth
--)
22492 cp_lexer_consume_token (parser
->lexer
);
22501 /* Consume the token. */
22502 cp_lexer_consume_token (parser
->lexer
);
22506 /* Return true if we are looking at an array-designator, false otherwise. */
22509 cp_parser_array_designator_p (cp_parser
*parser
)
22511 /* Consume the `['. */
22512 cp_lexer_consume_token (parser
->lexer
);
22514 cp_lexer_save_tokens (parser
->lexer
);
22516 /* Skip tokens until the next token is a closing square bracket.
22517 If we find the closing `]', and the next token is a `=', then
22518 we are looking at an array designator. */
22519 bool array_designator_p
22520 = (cp_parser_skip_to_closing_square_bracket (parser
)
22521 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22523 /* Roll back the tokens we skipped. */
22524 cp_lexer_rollback_tokens (parser
->lexer
);
22526 return array_designator_p
;
22529 /* Parse an initializer-list.
22532 initializer-clause ... [opt]
22533 initializer-list , initializer-clause ... [opt]
22537 designated-initializer-list:
22538 designated-initializer-clause
22539 designated-initializer-list , designated-initializer-clause
22541 designated-initializer-clause:
22542 designator brace-or-equal-initializer
22550 designation initializer-clause ...[opt]
22551 initializer-list , designation initializer-clause ...[opt]
22556 [ constant-expression ] =
22558 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22559 for the initializer. If the INDEX of the elt is non-NULL, it is the
22560 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22561 as for cp_parser_initializer. */
22563 static vec
<constructor_elt
, va_gc
> *
22564 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22566 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22567 bool first_p
= true;
22568 tree first_designator
= NULL_TREE
;
22570 /* Assume all of the expressions are constant. */
22571 *non_constant_p
= false;
22573 /* Parse the rest of the list. */
22579 bool clause_non_constant_p
;
22580 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22582 /* Handle the C++2A syntax, '. id ='. */
22583 if ((cxx_dialect
>= cxx2a
22584 || cp_parser_allow_gnu_extensions_p (parser
))
22585 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22586 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22587 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22588 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22589 == CPP_OPEN_BRACE
)))
22591 if (cxx_dialect
< cxx2a
)
22592 pedwarn (loc
, OPT_Wpedantic
,
22593 "C++ designated initializers only available with "
22594 "-std=c++2a or -std=gnu++2a");
22595 /* Consume the `.'. */
22596 cp_lexer_consume_token (parser
->lexer
);
22597 /* Consume the identifier. */
22598 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22600 /* Consume the `='. */
22601 cp_lexer_consume_token (parser
->lexer
);
22603 /* Also, if the next token is an identifier and the following one is a
22604 colon, we are looking at the GNU designated-initializer
22606 else if (cp_parser_allow_gnu_extensions_p (parser
)
22607 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22608 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22611 /* Warn the user that they are using an extension. */
22612 pedwarn (loc
, OPT_Wpedantic
,
22613 "ISO C++ does not allow GNU designated initializers");
22614 /* Consume the identifier. */
22615 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22616 /* Consume the `:'. */
22617 cp_lexer_consume_token (parser
->lexer
);
22619 /* Also handle C99 array designators, '[ const ] ='. */
22620 else if (cp_parser_allow_gnu_extensions_p (parser
)
22621 && !c_dialect_objc ()
22622 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22624 /* In C++11, [ could start a lambda-introducer. */
22625 bool non_const
= false;
22627 cp_parser_parse_tentatively (parser
);
22629 if (!cp_parser_array_designator_p (parser
))
22631 cp_parser_simulate_error (parser
);
22632 designator
= NULL_TREE
;
22636 designator
= cp_parser_constant_expression (parser
, true,
22638 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22639 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22642 if (!cp_parser_parse_definitely (parser
))
22643 designator
= NULL_TREE
;
22645 && (!require_potential_rvalue_constant_expression
22647 designator
= NULL_TREE
;
22649 /* Warn the user that they are using an extension. */
22650 pedwarn (loc
, OPT_Wpedantic
,
22651 "ISO C++ does not allow C99 designated initializers");
22654 designator
= NULL_TREE
;
22658 first_designator
= designator
;
22661 else if (cxx_dialect
>= cxx2a
22662 && first_designator
!= error_mark_node
22663 && (!first_designator
!= !designator
))
22665 error_at (loc
, "either all initializer clauses should be designated "
22666 "or none of them should be");
22667 first_designator
= error_mark_node
;
22669 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22670 first_designator
= designator
;
22672 /* Parse the initializer. */
22673 initializer
= cp_parser_initializer_clause (parser
,
22674 &clause_non_constant_p
);
22675 /* If any clause is non-constant, so is the entire initializer. */
22676 if (clause_non_constant_p
)
22677 *non_constant_p
= true;
22679 /* If we have an ellipsis, this is an initializer pack
22681 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22683 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22685 /* Consume the `...'. */
22686 cp_lexer_consume_token (parser
->lexer
);
22688 if (designator
&& cxx_dialect
>= cxx2a
)
22690 "%<...%> not allowed in designated initializer list");
22692 /* Turn the initializer into an initializer expansion. */
22693 initializer
= make_pack_expansion (initializer
);
22696 /* Add it to the vector. */
22697 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22699 /* If the next token is not a comma, we have reached the end of
22701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22704 /* Peek at the next token. */
22705 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22706 /* If the next token is a `}', then we're still done. An
22707 initializer-clause can have a trailing `,' after the
22708 initializer-list and before the closing `}'. */
22709 if (token
->type
== CPP_CLOSE_BRACE
)
22712 /* Consume the `,' token. */
22713 cp_lexer_consume_token (parser
->lexer
);
22716 /* The same identifier shall not appear in multiple designators
22717 of a designated-initializer-list. */
22718 if (first_designator
)
22721 tree designator
, val
;
22722 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22723 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22725 if (IDENTIFIER_MARKED (designator
))
22727 error_at (cp_expr_loc_or_loc (val
, input_location
),
22728 "%<.%s%> designator used multiple times in "
22729 "the same initializer list",
22730 IDENTIFIER_POINTER (designator
));
22731 (*v
)[i
].index
= error_mark_node
;
22734 IDENTIFIER_MARKED (designator
) = 1;
22736 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22737 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22738 IDENTIFIER_MARKED (designator
) = 0;
22744 /* Classes [gram.class] */
22746 /* Parse a class-name.
22752 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22753 to indicate that names looked up in dependent types should be
22754 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22755 keyword has been used to indicate that the name that appears next
22756 is a template. TAG_TYPE indicates the explicit tag given before
22757 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22758 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22759 is the class being defined in a class-head. If ENUM_OK is TRUE,
22760 enum-names are also accepted.
22762 Returns the TYPE_DECL representing the class. */
22765 cp_parser_class_name (cp_parser
*parser
,
22766 bool typename_keyword_p
,
22767 bool template_keyword_p
,
22768 enum tag_types tag_type
,
22769 bool check_dependency_p
,
22771 bool is_declaration
,
22778 tree identifier
= NULL_TREE
;
22780 /* All class-names start with an identifier. */
22781 token
= cp_lexer_peek_token (parser
->lexer
);
22782 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22784 cp_parser_error (parser
, "expected class-name");
22785 return error_mark_node
;
22788 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22789 to a template-id, so we save it here. */
22790 scope
= parser
->scope
;
22791 if (scope
== error_mark_node
)
22792 return error_mark_node
;
22794 /* Any name names a type if we're following the `typename' keyword
22795 in a qualified name where the enclosing scope is type-dependent. */
22796 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22797 && dependent_type_p (scope
));
22798 /* Handle the common case (an identifier, but not a template-id)
22800 if (token
->type
== CPP_NAME
22801 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22803 cp_token
*identifier_token
;
22806 /* Look for the identifier. */
22807 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22808 ambiguous_p
= identifier_token
->error_reported
;
22809 identifier
= cp_parser_identifier (parser
);
22810 /* If the next token isn't an identifier, we are certainly not
22811 looking at a class-name. */
22812 if (identifier
== error_mark_node
)
22813 decl
= error_mark_node
;
22814 /* If we know this is a type-name, there's no need to look it
22816 else if (typename_p
)
22820 tree ambiguous_decls
;
22821 /* If we already know that this lookup is ambiguous, then
22822 we've already issued an error message; there's no reason
22826 cp_parser_simulate_error (parser
);
22827 return error_mark_node
;
22829 /* If the next token is a `::', then the name must be a type
22832 [basic.lookup.qual]
22834 During the lookup for a name preceding the :: scope
22835 resolution operator, object, function, and enumerator
22836 names are ignored. */
22837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22838 tag_type
= scope_type
;
22839 /* Look up the name. */
22840 decl
= cp_parser_lookup_name (parser
, identifier
,
22842 /*is_template=*/false,
22843 /*is_namespace=*/false,
22844 check_dependency_p
,
22846 identifier_token
->location
);
22847 if (ambiguous_decls
)
22849 if (cp_parser_parsing_tentatively (parser
))
22850 cp_parser_simulate_error (parser
);
22851 return error_mark_node
;
22857 /* Try a template-id. */
22858 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22859 check_dependency_p
,
22862 if (decl
== error_mark_node
)
22863 return error_mark_node
;
22866 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22868 /* If this is a typename, create a TYPENAME_TYPE. */
22869 if (typename_p
&& decl
!= error_mark_node
)
22871 decl
= make_typename_type (scope
, decl
, typename_type
,
22872 /*complain=*/tf_error
);
22873 if (decl
!= error_mark_node
)
22874 decl
= TYPE_NAME (decl
);
22877 decl
= strip_using_decl (decl
);
22879 /* Check to see that it is really the name of a class. */
22880 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22881 && identifier_p (TREE_OPERAND (decl
, 0))
22882 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22883 /* Situations like this:
22885 template <typename T> struct A {
22886 typename T::template X<int>::I i;
22889 are problematic. Is `T::template X<int>' a class-name? The
22890 standard does not seem to be definitive, but there is no other
22891 valid interpretation of the following `::'. Therefore, those
22892 names are considered class-names. */
22894 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22895 if (decl
!= error_mark_node
)
22896 decl
= TYPE_NAME (decl
);
22898 else if (TREE_CODE (decl
) != TYPE_DECL
22899 || TREE_TYPE (decl
) == error_mark_node
22900 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22901 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22902 /* In Objective-C 2.0, a classname followed by '.' starts a
22903 dot-syntax expression, and it's not a type-name. */
22904 || (c_dialect_objc ()
22905 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22906 && objc_is_class_name (decl
)))
22907 decl
= error_mark_node
;
22909 if (decl
== error_mark_node
)
22910 cp_parser_error (parser
, "expected class-name");
22911 else if (identifier
&& !parser
->scope
)
22912 maybe_note_name_used_in_class (identifier
, decl
);
22917 /* Parse a class-specifier.
22920 class-head { member-specification [opt] }
22922 Returns the TREE_TYPE representing the class. */
22925 cp_parser_class_specifier_1 (cp_parser
* parser
)
22928 tree attributes
= NULL_TREE
;
22929 bool nested_name_specifier_p
;
22930 unsigned saved_num_template_parameter_lists
;
22931 bool saved_in_function_body
;
22932 unsigned char in_statement
;
22933 bool in_switch_statement_p
;
22934 bool saved_in_unbraced_linkage_specification_p
;
22935 tree old_scope
= NULL_TREE
;
22936 tree scope
= NULL_TREE
;
22937 cp_token
*closing_brace
;
22939 push_deferring_access_checks (dk_no_deferred
);
22941 /* Parse the class-head. */
22942 type
= cp_parser_class_head (parser
,
22943 &nested_name_specifier_p
);
22944 /* If the class-head was a semantic disaster, skip the entire body
22948 cp_parser_skip_to_end_of_block_or_statement (parser
);
22949 pop_deferring_access_checks ();
22950 return error_mark_node
;
22953 /* Look for the `{'. */
22954 matching_braces braces
;
22955 if (!braces
.require_open (parser
))
22957 pop_deferring_access_checks ();
22958 return error_mark_node
;
22961 cp_ensure_no_omp_declare_simd (parser
);
22962 cp_ensure_no_oacc_routine (parser
);
22964 /* Issue an error message if type-definitions are forbidden here. */
22965 cp_parser_check_type_definition (parser
);
22966 /* Remember that we are defining one more class. */
22967 ++parser
->num_classes_being_defined
;
22968 /* Inside the class, surrounding template-parameter-lists do not
22970 saved_num_template_parameter_lists
22971 = parser
->num_template_parameter_lists
;
22972 parser
->num_template_parameter_lists
= 0;
22973 /* We are not in a function body. */
22974 saved_in_function_body
= parser
->in_function_body
;
22975 parser
->in_function_body
= false;
22976 /* Or in a loop. */
22977 in_statement
= parser
->in_statement
;
22978 parser
->in_statement
= 0;
22979 /* Or in a switch. */
22980 in_switch_statement_p
= parser
->in_switch_statement_p
;
22981 parser
->in_switch_statement_p
= false;
22982 /* We are not immediately inside an extern "lang" block. */
22983 saved_in_unbraced_linkage_specification_p
22984 = parser
->in_unbraced_linkage_specification_p
;
22985 parser
->in_unbraced_linkage_specification_p
= false;
22987 // Associate constraints with the type.
22989 type
= associate_classtype_constraints (type
);
22991 /* Start the class. */
22992 if (nested_name_specifier_p
)
22994 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22995 old_scope
= push_inner_scope (scope
);
22997 type
= begin_class_definition (type
);
22999 if (type
== error_mark_node
)
23000 /* If the type is erroneous, skip the entire body of the class. */
23001 cp_parser_skip_to_closing_brace (parser
);
23003 /* Parse the member-specification. */
23004 cp_parser_member_specification_opt (parser
);
23006 /* Look for the trailing `}'. */
23007 closing_brace
= braces
.require_close (parser
);
23008 /* Look for trailing attributes to apply to this class. */
23009 if (cp_parser_allow_gnu_extensions_p (parser
))
23010 attributes
= cp_parser_gnu_attributes_opt (parser
);
23011 if (type
!= error_mark_node
)
23012 type
= finish_struct (type
, attributes
);
23013 if (nested_name_specifier_p
)
23014 pop_inner_scope (old_scope
, scope
);
23016 /* We've finished a type definition. Check for the common syntax
23017 error of forgetting a semicolon after the definition. We need to
23018 be careful, as we can't just check for not-a-semicolon and be done
23019 with it; the user might have typed:
23021 class X { } c = ...;
23022 class X { } *p = ...;
23024 and so forth. Instead, enumerate all the possible tokens that
23025 might follow this production; if we don't see one of them, then
23026 complain and silently insert the semicolon. */
23028 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23029 bool want_semicolon
= true;
23031 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23032 /* Don't try to parse c++11 attributes here. As per the
23033 grammar, that should be a task for
23034 cp_parser_decl_specifier_seq. */
23035 want_semicolon
= false;
23037 switch (token
->type
)
23040 case CPP_SEMICOLON
:
23043 case CPP_OPEN_PAREN
:
23044 case CPP_CLOSE_PAREN
:
23046 want_semicolon
= false;
23049 /* While it's legal for type qualifiers and storage class
23050 specifiers to follow type definitions in the grammar, only
23051 compiler testsuites contain code like that. Assume that if
23052 we see such code, then what we're really seeing is a case
23056 const <type> var = ...;
23061 static <type> func (...) ...
23063 i.e. the qualifier or specifier applies to the next
23064 declaration. To do so, however, we need to look ahead one
23065 more token to see if *that* token is a type specifier.
23067 This code could be improved to handle:
23070 static const <type> var = ...; */
23072 if (keyword_is_decl_specifier (token
->keyword
))
23074 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23076 /* Handling user-defined types here would be nice, but very
23079 = (lookahead
->type
== CPP_KEYWORD
23080 && keyword_begins_type_specifier (lookahead
->keyword
));
23087 /* If we don't have a type, then something is very wrong and we
23088 shouldn't try to do anything clever. Likewise for not seeing the
23090 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23092 /* Locate the closing brace. */
23093 cp_token_position prev
23094 = cp_lexer_previous_token_position (parser
->lexer
);
23095 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23096 location_t loc
= prev_token
->location
;
23098 /* We want to suggest insertion of a ';' immediately *after* the
23099 closing brace, so, if we can, offset the location by 1 column. */
23100 location_t next_loc
= loc
;
23101 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23102 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23104 rich_location
richloc (line_table
, next_loc
);
23106 /* If we successfully offset the location, suggest the fix-it. */
23107 if (next_loc
!= loc
)
23108 richloc
.add_fixit_insert_before (next_loc
, ";");
23110 if (CLASSTYPE_DECLARED_CLASS (type
))
23111 error_at (&richloc
,
23112 "expected %<;%> after class definition");
23113 else if (TREE_CODE (type
) == RECORD_TYPE
)
23114 error_at (&richloc
,
23115 "expected %<;%> after struct definition");
23116 else if (TREE_CODE (type
) == UNION_TYPE
)
23117 error_at (&richloc
,
23118 "expected %<;%> after union definition");
23120 gcc_unreachable ();
23122 /* Unget one token and smash it to look as though we encountered
23123 a semicolon in the input stream. */
23124 cp_lexer_set_token_position (parser
->lexer
, prev
);
23125 token
= cp_lexer_peek_token (parser
->lexer
);
23126 token
->type
= CPP_SEMICOLON
;
23127 token
->keyword
= RID_MAX
;
23131 /* If this class is not itself within the scope of another class,
23132 then we need to parse the bodies of all of the queued function
23133 definitions. Note that the queued functions defined in a class
23134 are not always processed immediately following the
23135 class-specifier for that class. Consider:
23138 struct B { void f() { sizeof (A); } };
23141 If `f' were processed before the processing of `A' were
23142 completed, there would be no way to compute the size of `A'.
23143 Note that the nesting we are interested in here is lexical --
23144 not the semantic nesting given by TYPE_CONTEXT. In particular,
23147 struct A { struct B; };
23148 struct A::B { void f() { } };
23150 there is no need to delay the parsing of `A::B::f'. */
23151 if (--parser
->num_classes_being_defined
== 0)
23154 tree class_type
= NULL_TREE
;
23155 tree pushed_scope
= NULL_TREE
;
23157 cp_default_arg_entry
*e
;
23158 tree save_ccp
, save_ccr
;
23160 if (any_erroneous_template_args_p (type
))
23162 /* Skip default arguments, NSDMIs, etc, in order to improve
23163 error recovery (c++/71169, c++/71832). */
23164 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23165 vec_safe_truncate (unparsed_nsdmis
, 0);
23166 vec_safe_truncate (unparsed_classes
, 0);
23167 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23170 /* In a first pass, parse default arguments to the functions.
23171 Then, in a second pass, parse the bodies of the functions.
23172 This two-phased approach handles cases like:
23180 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23183 /* If there are default arguments that have not yet been processed,
23184 take care of them now. */
23185 if (class_type
!= e
->class_type
)
23188 pop_scope (pushed_scope
);
23189 class_type
= e
->class_type
;
23190 pushed_scope
= push_scope (class_type
);
23192 /* Make sure that any template parameters are in scope. */
23193 maybe_begin_member_template_processing (decl
);
23194 /* Parse the default argument expressions. */
23195 cp_parser_late_parsing_default_args (parser
, decl
);
23196 /* Remove any template parameters from the symbol table. */
23197 maybe_end_member_template_processing ();
23199 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23200 /* Now parse any NSDMIs. */
23201 save_ccp
= current_class_ptr
;
23202 save_ccr
= current_class_ref
;
23203 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23205 if (class_type
!= DECL_CONTEXT (decl
))
23208 pop_scope (pushed_scope
);
23209 class_type
= DECL_CONTEXT (decl
);
23210 pushed_scope
= push_scope (class_type
);
23212 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23213 cp_parser_late_parsing_nsdmi (parser
, decl
);
23215 vec_safe_truncate (unparsed_nsdmis
, 0);
23216 current_class_ptr
= save_ccp
;
23217 current_class_ref
= save_ccr
;
23219 pop_scope (pushed_scope
);
23221 /* Now do some post-NSDMI bookkeeping. */
23222 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23223 after_nsdmi_defaulted_late_checks (class_type
);
23224 vec_safe_truncate (unparsed_classes
, 0);
23225 after_nsdmi_defaulted_late_checks (type
);
23227 /* Now parse the body of the functions. */
23230 /* OpenMP UDRs need to be parsed before all other functions. */
23231 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23232 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23233 cp_parser_late_parsing_for_member (parser
, decl
);
23234 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23235 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23236 cp_parser_late_parsing_for_member (parser
, decl
);
23239 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23240 cp_parser_late_parsing_for_member (parser
, decl
);
23241 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23244 vec_safe_push (unparsed_classes
, type
);
23246 /* Put back any saved access checks. */
23247 pop_deferring_access_checks ();
23249 /* Restore saved state. */
23250 parser
->in_switch_statement_p
= in_switch_statement_p
;
23251 parser
->in_statement
= in_statement
;
23252 parser
->in_function_body
= saved_in_function_body
;
23253 parser
->num_template_parameter_lists
23254 = saved_num_template_parameter_lists
;
23255 parser
->in_unbraced_linkage_specification_p
23256 = saved_in_unbraced_linkage_specification_p
;
23262 cp_parser_class_specifier (cp_parser
* parser
)
23265 timevar_push (TV_PARSE_STRUCT
);
23266 ret
= cp_parser_class_specifier_1 (parser
);
23267 timevar_pop (TV_PARSE_STRUCT
);
23271 /* Parse a class-head.
23274 class-key identifier [opt] base-clause [opt]
23275 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23276 class-key nested-name-specifier [opt] template-id
23279 class-virt-specifier:
23283 class-key attributes identifier [opt] base-clause [opt]
23284 class-key attributes nested-name-specifier identifier base-clause [opt]
23285 class-key attributes nested-name-specifier [opt] template-id
23288 Upon return BASES is initialized to the list of base classes (or
23289 NULL, if there are none) in the same form returned by
23290 cp_parser_base_clause.
23292 Returns the TYPE of the indicated class. Sets
23293 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23294 involving a nested-name-specifier was used, and FALSE otherwise.
23296 Returns error_mark_node if this is not a class-head.
23298 Returns NULL_TREE if the class-head is syntactically valid, but
23299 semantically invalid in a way that means we should skip the entire
23300 body of the class. */
23303 cp_parser_class_head (cp_parser
* parser
,
23304 bool* nested_name_specifier_p
)
23306 tree nested_name_specifier
;
23307 enum tag_types class_key
;
23308 tree id
= NULL_TREE
;
23309 tree type
= NULL_TREE
;
23312 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23313 bool template_id_p
= false;
23314 bool qualified_p
= false;
23315 bool invalid_nested_name_p
= false;
23316 bool invalid_explicit_specialization_p
= false;
23317 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23318 tree pushed_scope
= NULL_TREE
;
23319 unsigned num_templates
;
23320 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23321 /* Assume no nested-name-specifier will be present. */
23322 *nested_name_specifier_p
= false;
23323 /* Assume no template parameter lists will be used in defining the
23326 parser
->colon_corrects_to_scope_p
= false;
23328 /* Look for the class-key. */
23329 class_key
= cp_parser_class_key (parser
);
23330 if (class_key
== none_type
)
23331 return error_mark_node
;
23333 location_t class_head_start_location
= input_location
;
23335 /* Parse the attributes. */
23336 attributes
= cp_parser_attributes_opt (parser
);
23338 /* If the next token is `::', that is invalid -- but sometimes
23339 people do try to write:
23343 Handle this gracefully by accepting the extra qualifier, and then
23344 issuing an error about it later if this really is a
23345 class-head. If it turns out just to be an elaborated type
23346 specifier, remain silent. */
23347 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23348 qualified_p
= true;
23350 push_deferring_access_checks (dk_no_check
);
23352 /* Determine the name of the class. Begin by looking for an
23353 optional nested-name-specifier. */
23354 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23355 nested_name_specifier
23356 = cp_parser_nested_name_specifier_opt (parser
,
23357 /*typename_keyword_p=*/false,
23358 /*check_dependency_p=*/false,
23360 /*is_declaration=*/false);
23361 /* If there was a nested-name-specifier, then there *must* be an
23364 cp_token
*bad_template_keyword
= NULL
;
23366 if (nested_name_specifier
)
23368 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23369 /* Although the grammar says `identifier', it really means
23370 `class-name' or `template-name'. You are only allowed to
23371 define a class that has already been declared with this
23374 The proposed resolution for Core Issue 180 says that wherever
23375 you see `class T::X' you should treat `X' as a type-name.
23377 It is OK to define an inaccessible class; for example:
23379 class A { class B; };
23382 We do not know if we will see a class-name, or a
23383 template-name. We look for a class-name first, in case the
23384 class-name is a template-id; if we looked for the
23385 template-name first we would stop after the template-name. */
23386 cp_parser_parse_tentatively (parser
);
23387 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23388 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23389 type
= cp_parser_class_name (parser
,
23390 /*typename_keyword_p=*/false,
23391 /*template_keyword_p=*/false,
23393 /*check_dependency_p=*/false,
23394 /*class_head_p=*/true,
23395 /*is_declaration=*/false);
23396 /* If that didn't work, ignore the nested-name-specifier. */
23397 if (!cp_parser_parse_definitely (parser
))
23399 invalid_nested_name_p
= true;
23400 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23401 id
= cp_parser_identifier (parser
);
23402 if (id
== error_mark_node
)
23405 /* If we could not find a corresponding TYPE, treat this
23406 declaration like an unqualified declaration. */
23407 if (type
== error_mark_node
)
23408 nested_name_specifier
= NULL_TREE
;
23409 /* Otherwise, count the number of templates used in TYPE and its
23410 containing scopes. */
23412 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23414 /* Otherwise, the identifier is optional. */
23417 /* We don't know whether what comes next is a template-id,
23418 an identifier, or nothing at all. */
23419 cp_parser_parse_tentatively (parser
);
23420 /* Check for a template-id. */
23421 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23422 id
= cp_parser_template_id (parser
,
23423 /*template_keyword_p=*/false,
23424 /*check_dependency_p=*/true,
23426 /*is_declaration=*/true);
23427 /* If that didn't work, it could still be an identifier. */
23428 if (!cp_parser_parse_definitely (parser
))
23430 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23432 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23433 id
= cp_parser_identifier (parser
);
23440 template_id_p
= true;
23445 pop_deferring_access_checks ();
23449 cp_parser_check_for_invalid_template_id (parser
, id
,
23451 type_start_token
->location
);
23453 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23455 /* If it's not a `:' or a `{' then we can't really be looking at a
23456 class-head, since a class-head only appears as part of a
23457 class-specifier. We have to detect this situation before calling
23458 xref_tag, since that has irreversible side-effects. */
23459 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23461 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23462 type
= error_mark_node
;
23466 /* At this point, we're going ahead with the class-specifier, even
23467 if some other problem occurs. */
23468 cp_parser_commit_to_tentative_parse (parser
);
23469 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23471 cp_parser_error (parser
,
23472 "cannot specify %<override%> for a class");
23473 type
= error_mark_node
;
23476 /* Issue the error about the overly-qualified name now. */
23479 cp_parser_error (parser
,
23480 "global qualification of class name is invalid");
23481 type
= error_mark_node
;
23484 else if (invalid_nested_name_p
)
23486 cp_parser_error (parser
,
23487 "qualified name does not name a class");
23488 type
= error_mark_node
;
23491 else if (nested_name_specifier
)
23495 if (bad_template_keyword
)
23496 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23497 keyword template shall not appear at the top level. */
23498 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23499 "keyword %<template%> not allowed in class-head-name");
23501 /* Reject typedef-names in class heads. */
23502 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23504 error_at (type_start_token
->location
,
23505 "invalid class name in declaration of %qD",
23511 /* Figure out in what scope the declaration is being placed. */
23512 scope
= current_scope ();
23513 /* If that scope does not contain the scope in which the
23514 class was originally declared, the program is invalid. */
23515 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23517 if (at_namespace_scope_p ())
23518 error_at (type_start_token
->location
,
23519 "declaration of %qD in namespace %qD which does not "
23521 type
, scope
, nested_name_specifier
);
23523 error_at (type_start_token
->location
,
23524 "declaration of %qD in %qD which does not enclose %qD",
23525 type
, scope
, nested_name_specifier
);
23531 A declarator-id shall not be qualified except for the
23532 definition of a ... nested class outside of its class
23533 ... [or] the definition or explicit instantiation of a
23534 class member of a namespace outside of its namespace. */
23535 if (scope
== nested_name_specifier
)
23537 permerror (nested_name_specifier_token_start
->location
,
23538 "extra qualification not allowed");
23539 nested_name_specifier
= NULL_TREE
;
23543 /* An explicit-specialization must be preceded by "template <>". If
23544 it is not, try to recover gracefully. */
23545 if (at_namespace_scope_p ()
23546 && parser
->num_template_parameter_lists
== 0
23547 && !processing_template_parmlist
23550 /* Build a location of this form:
23551 struct typename <ARGS>
23552 ^~~~~~~~~~~~~~~~~~~~~~
23553 with caret==start at the start token, and
23554 finishing at the end of the type. */
23555 location_t reported_loc
23556 = make_location (class_head_start_location
,
23557 class_head_start_location
,
23558 get_finish (type_start_token
->location
));
23559 rich_location
richloc (line_table
, reported_loc
);
23560 richloc
.add_fixit_insert_before (class_head_start_location
,
23562 error_at (&richloc
,
23563 "an explicit specialization must be preceded by"
23564 " %<template <>%>");
23565 invalid_explicit_specialization_p
= true;
23566 /* Take the same action that would have been taken by
23567 cp_parser_explicit_specialization. */
23568 ++parser
->num_template_parameter_lists
;
23569 begin_specialization ();
23571 /* There must be no "return" statements between this point and the
23572 end of this function; set "type "to the correct return value and
23573 use "goto done;" to return. */
23574 /* Make sure that the right number of template parameters were
23576 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23578 type_start_token
->location
,
23579 /*declarator=*/NULL
))
23581 /* If something went wrong, there is no point in even trying to
23582 process the class-definition. */
23587 /* Look up the type. */
23590 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23591 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23592 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23594 error_at (type_start_token
->location
,
23595 "function template %qD redeclared as a class template", id
);
23596 type
= error_mark_node
;
23600 type
= TREE_TYPE (id
);
23601 type
= maybe_process_partial_specialization (type
);
23603 /* Check the scope while we still know whether or not we had a
23604 nested-name-specifier. */
23605 if (type
!= error_mark_node
)
23606 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23608 if (nested_name_specifier
)
23609 pushed_scope
= push_scope (nested_name_specifier
);
23611 else if (nested_name_specifier
)
23617 template <typename T> struct S { struct T };
23618 template <typename T> struct S<T>::T { };
23620 we will get a TYPENAME_TYPE when processing the definition of
23621 `S::T'. We need to resolve it to the actual type before we
23622 try to define it. */
23623 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23625 class_type
= resolve_typename_type (TREE_TYPE (type
),
23626 /*only_current_p=*/false);
23627 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23628 type
= TYPE_NAME (class_type
);
23631 cp_parser_error (parser
, "could not resolve typename type");
23632 type
= error_mark_node
;
23636 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23637 == error_mark_node
)
23643 class_type
= current_class_type
;
23644 /* Enter the scope indicated by the nested-name-specifier. */
23645 pushed_scope
= push_scope (nested_name_specifier
);
23646 /* Get the canonical version of this type. */
23647 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23648 /* Call push_template_decl if it seems like we should be defining a
23649 template either from the template headers or the type we're
23650 defining, so that we diagnose both extra and missing headers. */
23651 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23652 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23653 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23655 type
= push_template_decl (type
);
23656 if (type
== error_mark_node
)
23663 type
= TREE_TYPE (type
);
23664 *nested_name_specifier_p
= true;
23666 else /* The name is not a nested name. */
23668 /* If the class was unnamed, create a dummy name. */
23670 id
= make_anon_name ();
23671 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23672 ? ts_within_enclosing_non_class
23674 type
= xref_tag (class_key
, id
, tag_scope
,
23675 parser
->num_template_parameter_lists
);
23678 /* Indicate whether this class was declared as a `class' or as a
23680 if (TREE_CODE (type
) == RECORD_TYPE
)
23681 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23682 cp_parser_check_class_key (class_key
, type
);
23684 /* If this type was already complete, and we see another definition,
23685 that's an error. */
23686 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23688 error_at (type_start_token
->location
, "redefinition of %q#T",
23690 inform (location_of (type
), "previous definition of %q#T",
23695 else if (type
== error_mark_node
)
23700 /* Apply attributes now, before any use of the class as a template
23701 argument in its base list. */
23702 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23703 fixup_attribute_variants (type
);
23706 /* We will have entered the scope containing the class; the names of
23707 base classes should be looked up in that context. For example:
23709 struct A { struct B {}; struct C; };
23710 struct A::C : B {};
23714 /* Get the list of base-classes, if there is one. */
23715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23717 /* PR59482: enter the class scope so that base-specifiers are looked
23721 bases
= cp_parser_base_clause (parser
);
23722 /* PR59482: get out of the previously pushed class scope so that the
23723 subsequent pops pop the right thing. */
23730 /* If we're really defining a class, process the base classes.
23731 If they're invalid, fail. */
23732 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23733 xref_basetypes (type
, bases
);
23736 /* Leave the scope given by the nested-name-specifier. We will
23737 enter the class scope itself while processing the members. */
23739 pop_scope (pushed_scope
);
23741 if (invalid_explicit_specialization_p
)
23743 end_specialization ();
23744 --parser
->num_template_parameter_lists
;
23748 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23749 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23750 CLASSTYPE_FINAL (type
) = 1;
23752 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23756 /* Parse a class-key.
23763 Returns the kind of class-key specified, or none_type to indicate
23766 static enum tag_types
23767 cp_parser_class_key (cp_parser
* parser
)
23770 enum tag_types tag_type
;
23772 /* Look for the class-key. */
23773 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23777 /* Check to see if the TOKEN is a class-key. */
23778 tag_type
= cp_parser_token_is_class_key (token
);
23780 cp_parser_error (parser
, "expected class-key");
23784 /* Parse a type-parameter-key.
23786 type-parameter-key:
23792 cp_parser_type_parameter_key (cp_parser
* parser
)
23794 /* Look for the type-parameter-key. */
23795 enum tag_types tag_type
= none_type
;
23796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23797 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23799 cp_lexer_consume_token (parser
->lexer
);
23800 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23801 /* typename is not allowed in a template template parameter
23802 by the standard until C++17. */
23803 pedwarn (token
->location
, OPT_Wpedantic
,
23804 "ISO C++ forbids typename key in template template parameter;"
23805 " use -std=c++17 or -std=gnu++17");
23808 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23813 /* Parse an (optional) member-specification.
23815 member-specification:
23816 member-declaration member-specification [opt]
23817 access-specifier : member-specification [opt] */
23820 cp_parser_member_specification_opt (cp_parser
* parser
)
23827 /* Peek at the next token. */
23828 token
= cp_lexer_peek_token (parser
->lexer
);
23829 /* If it's a `}', or EOF then we've seen all the members. */
23830 if (token
->type
== CPP_CLOSE_BRACE
23831 || token
->type
== CPP_EOF
23832 || token
->type
== CPP_PRAGMA_EOL
)
23835 /* See if this token is a keyword. */
23836 keyword
= token
->keyword
;
23840 case RID_PROTECTED
:
23842 /* Consume the access-specifier. */
23843 cp_lexer_consume_token (parser
->lexer
);
23844 /* Remember which access-specifier is active. */
23845 current_access_specifier
= token
->u
.value
;
23846 /* Look for the `:'. */
23847 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23851 /* Accept #pragmas at class scope. */
23852 if (token
->type
== CPP_PRAGMA
)
23854 cp_parser_pragma (parser
, pragma_member
, NULL
);
23858 /* Otherwise, the next construction must be a
23859 member-declaration. */
23860 cp_parser_member_declaration (parser
);
23865 /* Parse a member-declaration.
23867 member-declaration:
23868 decl-specifier-seq [opt] member-declarator-list [opt] ;
23869 function-definition ; [opt]
23870 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23872 template-declaration
23875 member-declarator-list:
23877 member-declarator-list , member-declarator
23880 declarator pure-specifier [opt]
23881 declarator constant-initializer [opt]
23882 identifier [opt] : constant-expression
23886 member-declaration:
23887 __extension__ member-declaration
23890 declarator attributes [opt] pure-specifier [opt]
23891 declarator attributes [opt] constant-initializer [opt]
23892 identifier [opt] attributes [opt] : constant-expression
23896 member-declaration:
23897 static_assert-declaration */
23900 cp_parser_member_declaration (cp_parser
* parser
)
23902 cp_decl_specifier_seq decl_specifiers
;
23903 tree prefix_attributes
;
23905 int declares_class_or_enum
;
23907 cp_token
*token
= NULL
;
23908 cp_token
*decl_spec_token_start
= NULL
;
23909 cp_token
*initializer_token_start
= NULL
;
23910 int saved_pedantic
;
23911 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23913 /* Check for the `__extension__' keyword. */
23914 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23917 cp_parser_member_declaration (parser
);
23918 /* Restore the old value of the PEDANTIC flag. */
23919 pedantic
= saved_pedantic
;
23924 /* Check for a template-declaration. */
23925 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23927 /* An explicit specialization here is an error condition, and we
23928 expect the specialization handler to detect and report this. */
23929 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23930 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23931 cp_parser_explicit_specialization (parser
);
23933 cp_parser_template_declaration (parser
, /*member_p=*/true);
23937 /* Check for a template introduction. */
23938 else if (cp_parser_template_declaration_after_export (parser
, true))
23941 /* Check for a using-declaration. */
23942 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23944 if (cxx_dialect
< cxx11
)
23946 /* Parse the using-declaration. */
23947 cp_parser_using_declaration (parser
,
23948 /*access_declaration_p=*/false);
23954 bool alias_decl_expected
;
23955 cp_parser_parse_tentatively (parser
);
23956 decl
= cp_parser_alias_declaration (parser
);
23957 /* Note that if we actually see the '=' token after the
23958 identifier, cp_parser_alias_declaration commits the
23959 tentative parse. In that case, we really expect an
23960 alias-declaration. Otherwise, we expect a using
23962 alias_decl_expected
=
23963 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23964 cp_parser_parse_definitely (parser
);
23966 if (alias_decl_expected
)
23967 finish_member_declaration (decl
);
23969 cp_parser_using_declaration (parser
,
23970 /*access_declaration_p=*/false);
23975 /* Check for @defs. */
23976 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23979 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23980 ivar
= ivar_chains
;
23984 ivar
= TREE_CHAIN (member
);
23985 TREE_CHAIN (member
) = NULL_TREE
;
23986 finish_member_declaration (member
);
23991 /* If the next token is `static_assert' we have a static assertion. */
23992 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23994 cp_parser_static_assert (parser
, /*member_p=*/true);
23998 parser
->colon_corrects_to_scope_p
= false;
24000 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24003 /* Parse the decl-specifier-seq. */
24004 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24005 cp_parser_decl_specifier_seq (parser
,
24006 CP_PARSER_FLAGS_OPTIONAL
,
24008 &declares_class_or_enum
);
24009 /* Check for an invalid type-name. */
24010 if (!decl_specifiers
.any_type_specifiers_p
24011 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24013 /* If there is no declarator, then the decl-specifier-seq should
24015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24017 /* If there was no decl-specifier-seq, and the next token is a
24018 `;', then we have something like:
24024 Each member-declaration shall declare at least one member
24025 name of the class. */
24026 if (!decl_specifiers
.any_specifiers_p
)
24028 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24029 if (!in_system_header_at (token
->location
))
24031 gcc_rich_location
richloc (token
->location
);
24032 richloc
.add_fixit_remove ();
24033 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24040 /* See if this declaration is a friend. */
24041 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24042 /* If there were decl-specifiers, check to see if there was
24043 a class-declaration. */
24044 type
= check_tag_decl (&decl_specifiers
,
24045 /*explicit_type_instantiation_p=*/false);
24046 /* Nested classes have already been added to the class, but
24047 a `friend' needs to be explicitly registered. */
24050 /* If the `friend' keyword was present, the friend must
24051 be introduced with a class-key. */
24052 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24053 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24054 "in C++03 a class-key must be used "
24055 "when declaring a friend");
24058 template <typename T> struct A {
24059 friend struct A<T>::B;
24062 A<T>::B will be represented by a TYPENAME_TYPE, and
24063 therefore not recognized by check_tag_decl. */
24066 type
= decl_specifiers
.type
;
24067 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24068 type
= TREE_TYPE (type
);
24070 if (!type
|| !TYPE_P (type
))
24071 error_at (decl_spec_token_start
->location
,
24072 "friend declaration does not name a class or "
24075 make_friend_class (current_class_type
, type
,
24076 /*complain=*/true);
24078 /* If there is no TYPE, an error message will already have
24080 else if (!type
|| type
== error_mark_node
)
24082 /* An anonymous aggregate has to be handled specially; such
24083 a declaration really declares a data member (with a
24084 particular type), as opposed to a nested class. */
24085 else if (ANON_AGGR_TYPE_P (type
))
24088 if (decl_specifiers
.storage_class
!= sc_none
)
24089 error_at (decl_spec_token_start
->location
,
24090 "a storage class on an anonymous aggregate "
24091 "in class scope is not allowed");
24093 /* Remove constructors and such from TYPE, now that we
24094 know it is an anonymous aggregate. */
24095 fixup_anonymous_aggr (type
);
24096 /* And make the corresponding data member. */
24097 decl
= build_decl (decl_spec_token_start
->location
,
24098 FIELD_DECL
, NULL_TREE
, type
);
24099 /* Add it to the class. */
24100 finish_member_declaration (decl
);
24103 cp_parser_check_access_in_redeclaration
24105 decl_spec_token_start
->location
);
24110 bool assume_semicolon
= false;
24112 /* Clear attributes from the decl_specifiers but keep them
24113 around as prefix attributes that apply them to the entity
24115 prefix_attributes
= decl_specifiers
.attributes
;
24116 decl_specifiers
.attributes
= NULL_TREE
;
24118 /* See if these declarations will be friends. */
24119 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24121 /* Keep going until we hit the `;' at the end of the
24123 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24125 tree attributes
= NULL_TREE
;
24126 tree first_attribute
;
24128 bool named_bitfld
= false;
24130 /* Peek at the next token. */
24131 token
= cp_lexer_peek_token (parser
->lexer
);
24133 /* The following code wants to know early if it is a bit-field
24134 or some other declaration. Attributes can appear before
24135 the `:' token. Skip over them without consuming any tokens
24136 to peek if they are followed by `:'. */
24137 if (cp_next_tokens_can_be_attribute_p (parser
)
24138 || (token
->type
== CPP_NAME
24139 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24140 && (named_bitfld
= true)))
24143 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24144 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24147 /* Check for a bitfield declaration. */
24148 if (token
->type
== CPP_COLON
24149 || (token
->type
== CPP_NAME
24150 && token
== cp_lexer_peek_token (parser
->lexer
)
24151 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24152 && (named_bitfld
= true)))
24156 tree late_attributes
= NULL_TREE
;
24157 location_t id_location
24158 = cp_lexer_peek_token (parser
->lexer
)->location
;
24161 identifier
= cp_parser_identifier (parser
);
24163 identifier
= NULL_TREE
;
24165 /* Look for attributes that apply to the bitfield. */
24166 attributes
= cp_parser_attributes_opt (parser
);
24168 /* Consume the `:' token. */
24169 cp_lexer_consume_token (parser
->lexer
);
24171 /* Get the width of the bitfield. */
24172 width
= cp_parser_constant_expression (parser
, false, NULL
,
24173 cxx_dialect
>= cxx11
);
24175 /* In C++2A and as extension for C++11 and above we allow
24176 default member initializers for bit-fields. */
24177 initializer
= NULL_TREE
;
24178 if (cxx_dialect
>= cxx11
24179 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24180 || cp_lexer_next_token_is (parser
->lexer
,
24184 = cp_lexer_peek_token (parser
->lexer
)->location
;
24185 if (cxx_dialect
< cxx2a
24186 && !in_system_header_at (loc
)
24187 && identifier
!= NULL_TREE
)
24189 "default member initializers for bit-fields "
24190 "only available with -std=c++2a or "
24193 initializer
= cp_parser_save_nsdmi (parser
);
24194 if (identifier
== NULL_TREE
)
24196 error_at (loc
, "default member initializer for "
24197 "unnamed bit-field");
24198 initializer
= NULL_TREE
;
24203 /* Look for attributes that apply to the bitfield after
24204 the `:' token and width. This is where GCC used to
24205 parse attributes in the past, pedwarn if there is
24206 a std attribute. */
24207 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24208 pedwarn (input_location
, OPT_Wpedantic
,
24209 "ISO C++ allows bit-field attributes only "
24210 "before the %<:%> token");
24212 late_attributes
= cp_parser_attributes_opt (parser
);
24215 attributes
= attr_chainon (attributes
, late_attributes
);
24217 /* Remember which attributes are prefix attributes and
24219 first_attribute
= attributes
;
24220 /* Combine the attributes. */
24221 attributes
= attr_chainon (prefix_attributes
, attributes
);
24223 /* Create the bitfield declaration. */
24224 decl
= grokbitfield (identifier
24225 ? make_id_declarator (NULL_TREE
,
24231 width
, initializer
,
24236 cp_declarator
*declarator
;
24237 tree asm_specification
;
24238 int ctor_dtor_or_conv_p
;
24240 /* Parse the declarator. */
24242 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24243 &ctor_dtor_or_conv_p
,
24244 /*parenthesized_p=*/NULL
,
24248 /* If something went wrong parsing the declarator, make sure
24249 that we at least consume some tokens. */
24250 if (declarator
== cp_error_declarator
)
24252 /* Skip to the end of the statement. */
24253 cp_parser_skip_to_end_of_statement (parser
);
24254 /* If the next token is not a semicolon, that is
24255 probably because we just skipped over the body of
24256 a function. So, we consume a semicolon if
24257 present, but do not issue an error message if it
24259 if (cp_lexer_next_token_is (parser
->lexer
,
24261 cp_lexer_consume_token (parser
->lexer
);
24265 if (declares_class_or_enum
& 2)
24266 cp_parser_check_for_definition_in_return_type
24267 (declarator
, decl_specifiers
.type
,
24268 decl_specifiers
.locations
[ds_type_spec
]);
24270 /* Look for an asm-specification. */
24271 asm_specification
= cp_parser_asm_specification_opt (parser
);
24272 /* Look for attributes that apply to the declaration. */
24273 attributes
= cp_parser_attributes_opt (parser
);
24274 /* Remember which attributes are prefix attributes and
24276 first_attribute
= attributes
;
24277 /* Combine the attributes. */
24278 attributes
= attr_chainon (prefix_attributes
, attributes
);
24280 /* If it's an `=', then we have a constant-initializer or a
24281 pure-specifier. It is not correct to parse the
24282 initializer before registering the member declaration
24283 since the member declaration should be in scope while
24284 its initializer is processed. However, the rest of the
24285 front end does not yet provide an interface that allows
24286 us to handle this correctly. */
24287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24291 A pure-specifier shall be used only in the declaration of
24292 a virtual function.
24294 A member-declarator can contain a constant-initializer
24295 only if it declares a static member of integral or
24298 Therefore, if the DECLARATOR is for a function, we look
24299 for a pure-specifier; otherwise, we look for a
24300 constant-initializer. When we call `grokfield', it will
24301 perform more stringent semantics checks. */
24302 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24303 if (function_declarator_p (declarator
)
24304 || (decl_specifiers
.type
24305 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24306 && declarator
->kind
== cdk_id
24307 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24308 == FUNCTION_TYPE
)))
24309 initializer
= cp_parser_pure_specifier (parser
);
24310 else if (decl_specifiers
.storage_class
!= sc_static
)
24311 initializer
= cp_parser_save_nsdmi (parser
);
24312 else if (cxx_dialect
>= cxx11
)
24315 /* Don't require a constant rvalue in C++11, since we
24316 might want a reference constant. We'll enforce
24317 constancy later. */
24318 cp_lexer_consume_token (parser
->lexer
);
24319 /* Parse the initializer. */
24320 initializer
= cp_parser_initializer_clause (parser
,
24324 /* Parse the initializer. */
24325 initializer
= cp_parser_constant_initializer (parser
);
24327 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24328 && !function_declarator_p (declarator
))
24331 if (decl_specifiers
.storage_class
!= sc_static
)
24332 initializer
= cp_parser_save_nsdmi (parser
);
24334 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24336 /* Otherwise, there is no initializer. */
24338 initializer
= NULL_TREE
;
24340 /* See if we are probably looking at a function
24341 definition. We are certainly not looking at a
24342 member-declarator. Calling `grokfield' has
24343 side-effects, so we must not do it unless we are sure
24344 that we are looking at a member-declarator. */
24345 if (cp_parser_token_starts_function_definition_p
24346 (cp_lexer_peek_token (parser
->lexer
)))
24348 /* The grammar does not allow a pure-specifier to be
24349 used when a member function is defined. (It is
24350 possible that this fact is an oversight in the
24351 standard, since a pure function may be defined
24352 outside of the class-specifier. */
24353 if (initializer
&& initializer_token_start
)
24354 error_at (initializer_token_start
->location
,
24355 "pure-specifier on function-definition");
24356 decl
= cp_parser_save_member_function_body (parser
,
24360 if (parser
->fully_implicit_function_template_p
)
24361 decl
= finish_fully_implicit_template (parser
, decl
);
24362 /* If the member was not a friend, declare it here. */
24364 finish_member_declaration (decl
);
24365 /* Peek at the next token. */
24366 token
= cp_lexer_peek_token (parser
->lexer
);
24367 /* If the next token is a semicolon, consume it. */
24368 if (token
->type
== CPP_SEMICOLON
)
24370 location_t semicolon_loc
24371 = cp_lexer_consume_token (parser
->lexer
)->location
;
24372 gcc_rich_location
richloc (semicolon_loc
);
24373 richloc
.add_fixit_remove ();
24374 warning_at (&richloc
, OPT_Wextra_semi
,
24375 "extra %<;%> after in-class "
24376 "function definition");
24381 if (declarator
->kind
== cdk_function
)
24382 declarator
->id_loc
= token
->location
;
24383 /* Create the declaration. */
24384 decl
= grokfield (declarator
, &decl_specifiers
,
24385 initializer
, /*init_const_expr_p=*/true,
24386 asm_specification
, attributes
);
24387 if (parser
->fully_implicit_function_template_p
)
24390 finish_fully_implicit_template (parser
, 0);
24392 decl
= finish_fully_implicit_template (parser
, decl
);
24396 cp_finalize_omp_declare_simd (parser
, decl
);
24397 cp_finalize_oacc_routine (parser
, decl
, false);
24399 /* Reset PREFIX_ATTRIBUTES. */
24400 if (attributes
!= error_mark_node
)
24402 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24403 attributes
= TREE_CHAIN (attributes
);
24405 TREE_CHAIN (attributes
) = NULL_TREE
;
24408 /* If there is any qualification still in effect, clear it
24409 now; we will be starting fresh with the next declarator. */
24410 parser
->scope
= NULL_TREE
;
24411 parser
->qualifying_scope
= NULL_TREE
;
24412 parser
->object_scope
= NULL_TREE
;
24413 /* If it's a `,', then there are more declarators. */
24414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24416 cp_lexer_consume_token (parser
->lexer
);
24417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24419 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24420 gcc_rich_location
richloc (token
->location
);
24421 richloc
.add_fixit_remove ();
24422 error_at (&richloc
, "stray %<,%> at end of "
24423 "member declaration");
24426 /* If the next token isn't a `;', then we have a parse error. */
24427 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24430 /* The next token might be a ways away from where the
24431 actual semicolon is missing. Find the previous token
24432 and use that for our error position. */
24433 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24434 gcc_rich_location
richloc (token
->location
);
24435 richloc
.add_fixit_insert_after (";");
24436 error_at (&richloc
, "expected %<;%> at end of "
24437 "member declaration");
24439 /* Assume that the user meant to provide a semicolon. If
24440 we were to cp_parser_skip_to_end_of_statement, we might
24441 skip to a semicolon inside a member function definition
24442 and issue nonsensical error messages. */
24443 assume_semicolon
= true;
24448 /* Add DECL to the list of members. */
24450 /* Explicitly include, eg, NSDMIs, for better error
24451 recovery (c++/58650). */
24452 || !DECL_DECLARES_FUNCTION_P (decl
))
24453 finish_member_declaration (decl
);
24455 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24456 cp_parser_save_default_args (parser
, decl
);
24457 else if (TREE_CODE (decl
) == FIELD_DECL
24458 && DECL_INITIAL (decl
))
24459 /* Add DECL to the queue of NSDMI to be parsed later. */
24460 vec_safe_push (unparsed_nsdmis
, decl
);
24463 if (assume_semicolon
)
24468 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24470 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24473 /* Parse a pure-specifier.
24478 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24479 Otherwise, ERROR_MARK_NODE is returned. */
24482 cp_parser_pure_specifier (cp_parser
* parser
)
24486 /* Look for the `=' token. */
24487 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24488 return error_mark_node
;
24489 /* Look for the `0' token. */
24490 token
= cp_lexer_peek_token (parser
->lexer
);
24492 if (token
->type
== CPP_EOF
24493 || token
->type
== CPP_PRAGMA_EOL
)
24494 return error_mark_node
;
24496 cp_lexer_consume_token (parser
->lexer
);
24498 /* Accept = default or = delete in c++0x mode. */
24499 if (token
->keyword
== RID_DEFAULT
24500 || token
->keyword
== RID_DELETE
)
24502 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24503 return token
->u
.value
;
24506 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24507 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24509 cp_parser_error (parser
,
24510 "invalid pure specifier (only %<= 0%> is allowed)");
24511 cp_parser_skip_to_end_of_statement (parser
);
24512 return error_mark_node
;
24514 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24516 error_at (token
->location
, "templates may not be %<virtual%>");
24517 return error_mark_node
;
24520 return integer_zero_node
;
24523 /* Parse a constant-initializer.
24525 constant-initializer:
24526 = constant-expression
24528 Returns a representation of the constant-expression. */
24531 cp_parser_constant_initializer (cp_parser
* parser
)
24533 /* Look for the `=' token. */
24534 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24535 return error_mark_node
;
24537 /* It is invalid to write:
24539 struct S { static const int i = { 7 }; };
24542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24544 cp_parser_error (parser
,
24545 "a brace-enclosed initializer is not allowed here");
24546 /* Consume the opening brace. */
24547 matching_braces braces
;
24548 braces
.consume_open (parser
);
24549 /* Skip the initializer. */
24550 cp_parser_skip_to_closing_brace (parser
);
24551 /* Look for the trailing `}'. */
24552 braces
.require_close (parser
);
24554 return error_mark_node
;
24557 return cp_parser_constant_expression (parser
);
24560 /* Derived classes [gram.class.derived] */
24562 /* Parse a base-clause.
24565 : base-specifier-list
24567 base-specifier-list:
24568 base-specifier ... [opt]
24569 base-specifier-list , base-specifier ... [opt]
24571 Returns a TREE_LIST representing the base-classes, in the order in
24572 which they were declared. The representation of each node is as
24573 described by cp_parser_base_specifier.
24575 In the case that no bases are specified, this function will return
24576 NULL_TREE, not ERROR_MARK_NODE. */
24579 cp_parser_base_clause (cp_parser
* parser
)
24581 tree bases
= NULL_TREE
;
24583 /* Look for the `:' that begins the list. */
24584 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24586 /* Scan the base-specifier-list. */
24591 bool pack_expansion_p
= false;
24593 /* Look for the base-specifier. */
24594 base
= cp_parser_base_specifier (parser
);
24595 /* Look for the (optional) ellipsis. */
24596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24598 /* Consume the `...'. */
24599 cp_lexer_consume_token (parser
->lexer
);
24601 pack_expansion_p
= true;
24604 /* Add BASE to the front of the list. */
24605 if (base
&& base
!= error_mark_node
)
24607 if (pack_expansion_p
)
24608 /* Make this a pack expansion type. */
24609 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24611 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24613 TREE_CHAIN (base
) = bases
;
24617 /* Peek at the next token. */
24618 token
= cp_lexer_peek_token (parser
->lexer
);
24619 /* If it's not a comma, then the list is complete. */
24620 if (token
->type
!= CPP_COMMA
)
24622 /* Consume the `,'. */
24623 cp_lexer_consume_token (parser
->lexer
);
24626 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24627 base class had a qualified name. However, the next name that
24628 appears is certainly not qualified. */
24629 parser
->scope
= NULL_TREE
;
24630 parser
->qualifying_scope
= NULL_TREE
;
24631 parser
->object_scope
= NULL_TREE
;
24633 return nreverse (bases
);
24636 /* Parse a base-specifier.
24639 :: [opt] nested-name-specifier [opt] class-name
24640 virtual access-specifier [opt] :: [opt] nested-name-specifier
24642 access-specifier virtual [opt] :: [opt] nested-name-specifier
24645 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24646 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24647 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24648 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24651 cp_parser_base_specifier (cp_parser
* parser
)
24655 bool virtual_p
= false;
24656 bool duplicate_virtual_error_issued_p
= false;
24657 bool duplicate_access_error_issued_p
= false;
24658 bool class_scope_p
, template_p
;
24659 tree access
= access_default_node
;
24662 /* Process the optional `virtual' and `access-specifier'. */
24665 /* Peek at the next token. */
24666 token
= cp_lexer_peek_token (parser
->lexer
);
24667 /* Process `virtual'. */
24668 switch (token
->keyword
)
24671 /* If `virtual' appears more than once, issue an error. */
24672 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24674 cp_parser_error (parser
,
24675 "%<virtual%> specified more than once in base-specifier");
24676 duplicate_virtual_error_issued_p
= true;
24681 /* Consume the `virtual' token. */
24682 cp_lexer_consume_token (parser
->lexer
);
24687 case RID_PROTECTED
:
24689 /* If more than one access specifier appears, issue an
24691 if (access
!= access_default_node
24692 && !duplicate_access_error_issued_p
)
24694 cp_parser_error (parser
,
24695 "more than one access specifier in base-specifier");
24696 duplicate_access_error_issued_p
= true;
24699 access
= ridpointers
[(int) token
->keyword
];
24701 /* Consume the access-specifier. */
24702 cp_lexer_consume_token (parser
->lexer
);
24711 /* It is not uncommon to see programs mechanically, erroneously, use
24712 the 'typename' keyword to denote (dependent) qualified types
24713 as base classes. */
24714 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24716 token
= cp_lexer_peek_token (parser
->lexer
);
24717 if (!processing_template_decl
)
24718 error_at (token
->location
,
24719 "keyword %<typename%> not allowed outside of templates");
24721 error_at (token
->location
,
24722 "keyword %<typename%> not allowed in this context "
24723 "(the base class is implicitly a type)");
24724 cp_lexer_consume_token (parser
->lexer
);
24727 /* Look for the optional `::' operator. */
24728 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24729 /* Look for the nested-name-specifier. The simplest way to
24734 The keyword `typename' is not permitted in a base-specifier or
24735 mem-initializer; in these contexts a qualified name that
24736 depends on a template-parameter is implicitly assumed to be a
24739 is to pretend that we have seen the `typename' keyword at this
24741 cp_parser_nested_name_specifier_opt (parser
,
24742 /*typename_keyword_p=*/true,
24743 /*check_dependency_p=*/true,
24745 /*is_declaration=*/true);
24746 /* If the base class is given by a qualified name, assume that names
24747 we see are type names or templates, as appropriate. */
24748 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24749 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24752 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24753 /* DR 950 allows decltype as a base-specifier. */
24754 type
= cp_parser_decltype (parser
);
24757 /* Otherwise, look for the class-name. */
24758 type
= cp_parser_class_name (parser
,
24762 /*check_dependency_p=*/true,
24763 /*class_head_p=*/false,
24764 /*is_declaration=*/true);
24765 type
= TREE_TYPE (type
);
24768 if (type
== error_mark_node
)
24769 return error_mark_node
;
24771 return finish_base_specifier (type
, access
, virtual_p
);
24774 /* Exception handling [gram.exception] */
24776 /* Parse an (optional) noexcept-specification.
24778 noexcept-specification:
24779 noexcept ( constant-expression ) [opt]
24781 If no noexcept-specification is present, returns NULL_TREE.
24782 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24783 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24784 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24785 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24786 in which case a boolean condition is returned instead. */
24789 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24790 bool require_constexpr
,
24791 bool* consumed_expr
,
24795 const char *saved_message
;
24797 /* Peek at the next token. */
24798 token
= cp_lexer_peek_token (parser
->lexer
);
24800 /* Is it a noexcept-specification? */
24801 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24804 cp_lexer_consume_token (parser
->lexer
);
24806 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24808 matching_parens parens
;
24809 parens
.consume_open (parser
);
24811 tree save_ccp
= current_class_ptr
;
24812 tree save_ccr
= current_class_ref
;
24814 if (current_class_type
)
24815 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
24817 if (require_constexpr
)
24819 /* Types may not be defined in an exception-specification. */
24820 saved_message
= parser
->type_definition_forbidden_message
;
24821 parser
->type_definition_forbidden_message
24822 = G_("types may not be defined in an exception-specification");
24824 expr
= cp_parser_constant_expression (parser
);
24826 /* Restore the saved message. */
24827 parser
->type_definition_forbidden_message
= saved_message
;
24831 expr
= cp_parser_expression (parser
);
24832 *consumed_expr
= true;
24835 parens
.require_close (parser
);
24837 current_class_ptr
= save_ccp
;
24838 current_class_ref
= save_ccr
;
24842 expr
= boolean_true_node
;
24843 if (!require_constexpr
)
24844 *consumed_expr
= false;
24847 /* We cannot build a noexcept-spec right away because this will check
24848 that expr is a constexpr. */
24850 return build_noexcept_spec (expr
, tf_warning_or_error
);
24858 /* Parse an (optional) exception-specification.
24860 exception-specification:
24861 throw ( type-id-list [opt] )
24863 Returns a TREE_LIST representing the exception-specification. The
24864 TREE_VALUE of each node is a type. */
24867 cp_parser_exception_specification_opt (cp_parser
* parser
)
24871 const char *saved_message
;
24873 /* Peek at the next token. */
24874 token
= cp_lexer_peek_token (parser
->lexer
);
24876 /* Is it a noexcept-specification? */
24877 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24879 if (type_id_list
!= NULL_TREE
)
24880 return type_id_list
;
24882 /* If it's not `throw', then there's no exception-specification. */
24883 if (!cp_parser_is_keyword (token
, RID_THROW
))
24886 location_t loc
= token
->location
;
24888 /* Consume the `throw'. */
24889 cp_lexer_consume_token (parser
->lexer
);
24891 /* Look for the `('. */
24892 matching_parens parens
;
24893 parens
.require_open (parser
);
24895 /* Peek at the next token. */
24896 token
= cp_lexer_peek_token (parser
->lexer
);
24897 /* If it's not a `)', then there is a type-id-list. */
24898 if (token
->type
!= CPP_CLOSE_PAREN
)
24900 /* Types may not be defined in an exception-specification. */
24901 saved_message
= parser
->type_definition_forbidden_message
;
24902 parser
->type_definition_forbidden_message
24903 = G_("types may not be defined in an exception-specification");
24904 /* Parse the type-id-list. */
24905 type_id_list
= cp_parser_type_id_list (parser
);
24906 /* Restore the saved message. */
24907 parser
->type_definition_forbidden_message
= saved_message
;
24909 if (cxx_dialect
>= cxx17
)
24911 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24913 type_id_list
= NULL_TREE
;
24915 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24916 warning_at (loc
, OPT_Wdeprecated
,
24917 "dynamic exception specifications are deprecated in "
24920 /* In C++17, throw() is equivalent to noexcept (true). throw()
24921 is deprecated in C++11 and above as well, but is still widely used,
24922 so don't warn about it yet. */
24923 else if (cxx_dialect
>= cxx17
)
24924 type_id_list
= noexcept_true_spec
;
24926 type_id_list
= empty_except_spec
;
24928 /* Look for the `)'. */
24929 parens
.require_close (parser
);
24931 return type_id_list
;
24934 /* Parse an (optional) type-id-list.
24938 type-id-list , type-id ... [opt]
24940 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24941 in the order that the types were presented. */
24944 cp_parser_type_id_list (cp_parser
* parser
)
24946 tree types
= NULL_TREE
;
24953 token
= cp_lexer_peek_token (parser
->lexer
);
24955 /* Get the next type-id. */
24956 type
= cp_parser_type_id (parser
);
24957 /* Check for invalid 'auto'. */
24958 if (flag_concepts
&& type_uses_auto (type
))
24960 error_at (token
->location
,
24961 "invalid use of %<auto%> in exception-specification");
24962 type
= error_mark_node
;
24964 /* Parse the optional ellipsis. */
24965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24967 /* Consume the `...'. */
24968 cp_lexer_consume_token (parser
->lexer
);
24970 /* Turn the type into a pack expansion expression. */
24971 type
= make_pack_expansion (type
);
24973 /* Add it to the list. */
24974 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24975 /* Peek at the next token. */
24976 token
= cp_lexer_peek_token (parser
->lexer
);
24977 /* If it is not a `,', we are done. */
24978 if (token
->type
!= CPP_COMMA
)
24980 /* Consume the `,'. */
24981 cp_lexer_consume_token (parser
->lexer
);
24984 return nreverse (types
);
24987 /* Parse a try-block.
24990 try compound-statement handler-seq */
24993 cp_parser_try_block (cp_parser
* parser
)
24997 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24998 if (parser
->in_function_body
24999 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
25000 error ("%<try%> in %<constexpr%> function");
25002 try_block
= begin_try_block ();
25003 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25004 finish_try_block (try_block
);
25005 cp_parser_handler_seq (parser
);
25006 finish_handler_sequence (try_block
);
25011 /* Parse a function-try-block.
25013 function-try-block:
25014 try ctor-initializer [opt] function-body handler-seq */
25017 cp_parser_function_try_block (cp_parser
* parser
)
25019 tree compound_stmt
;
25022 /* Look for the `try' keyword. */
25023 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25025 /* Let the rest of the front end know where we are. */
25026 try_block
= begin_function_try_block (&compound_stmt
);
25027 /* Parse the function-body. */
25028 cp_parser_ctor_initializer_opt_and_function_body
25029 (parser
, /*in_function_try_block=*/true);
25030 /* We're done with the `try' part. */
25031 finish_function_try_block (try_block
);
25032 /* Parse the handlers. */
25033 cp_parser_handler_seq (parser
);
25034 /* We're done with the handlers. */
25035 finish_function_handler_sequence (try_block
, compound_stmt
);
25038 /* Parse a handler-seq.
25041 handler handler-seq [opt] */
25044 cp_parser_handler_seq (cp_parser
* parser
)
25050 /* Parse the handler. */
25051 cp_parser_handler (parser
);
25052 /* Peek at the next token. */
25053 token
= cp_lexer_peek_token (parser
->lexer
);
25054 /* If it's not `catch' then there are no more handlers. */
25055 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25060 /* Parse a handler.
25063 catch ( exception-declaration ) compound-statement */
25066 cp_parser_handler (cp_parser
* parser
)
25071 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25072 handler
= begin_handler ();
25073 matching_parens parens
;
25074 parens
.require_open (parser
);
25075 declaration
= cp_parser_exception_declaration (parser
);
25076 finish_handler_parms (declaration
, handler
);
25077 parens
.require_close (parser
);
25078 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25079 finish_handler (handler
);
25082 /* Parse an exception-declaration.
25084 exception-declaration:
25085 type-specifier-seq declarator
25086 type-specifier-seq abstract-declarator
25090 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25091 ellipsis variant is used. */
25094 cp_parser_exception_declaration (cp_parser
* parser
)
25096 cp_decl_specifier_seq type_specifiers
;
25097 cp_declarator
*declarator
;
25098 const char *saved_message
;
25100 /* If it's an ellipsis, it's easy to handle. */
25101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25103 /* Consume the `...' token. */
25104 cp_lexer_consume_token (parser
->lexer
);
25108 /* Types may not be defined in exception-declarations. */
25109 saved_message
= parser
->type_definition_forbidden_message
;
25110 parser
->type_definition_forbidden_message
25111 = G_("types may not be defined in exception-declarations");
25113 /* Parse the type-specifier-seq. */
25114 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
25115 /*is_trailing_return=*/false,
25117 /* If it's a `)', then there is no declarator. */
25118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25121 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25122 /*ctor_dtor_or_conv_p=*/NULL
,
25123 /*parenthesized_p=*/NULL
,
25124 /*member_p=*/false,
25125 /*friend_p=*/false);
25127 /* Restore the saved message. */
25128 parser
->type_definition_forbidden_message
= saved_message
;
25130 if (!type_specifiers
.any_specifiers_p
)
25131 return error_mark_node
;
25133 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25136 /* Parse a throw-expression.
25139 throw assignment-expression [opt]
25141 Returns a THROW_EXPR representing the throw-expression. */
25144 cp_parser_throw_expression (cp_parser
* parser
)
25149 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25150 token
= cp_lexer_peek_token (parser
->lexer
);
25151 /* Figure out whether or not there is an assignment-expression
25152 following the "throw" keyword. */
25153 if (token
->type
== CPP_COMMA
25154 || token
->type
== CPP_SEMICOLON
25155 || token
->type
== CPP_CLOSE_PAREN
25156 || token
->type
== CPP_CLOSE_SQUARE
25157 || token
->type
== CPP_CLOSE_BRACE
25158 || token
->type
== CPP_COLON
)
25159 expression
= NULL_TREE
;
25161 expression
= cp_parser_assignment_expression (parser
);
25163 return build_throw (expression
);
25166 /* GNU Extensions */
25168 /* Parse an (optional) asm-specification.
25171 asm ( string-literal )
25173 If the asm-specification is present, returns a STRING_CST
25174 corresponding to the string-literal. Otherwise, returns
25178 cp_parser_asm_specification_opt (cp_parser
* parser
)
25181 tree asm_specification
;
25183 /* Peek at the next token. */
25184 token
= cp_lexer_peek_token (parser
->lexer
);
25185 /* If the next token isn't the `asm' keyword, then there's no
25186 asm-specification. */
25187 if (!cp_parser_is_keyword (token
, RID_ASM
))
25190 /* Consume the `asm' token. */
25191 cp_lexer_consume_token (parser
->lexer
);
25192 /* Look for the `('. */
25193 matching_parens parens
;
25194 parens
.require_open (parser
);
25196 /* Look for the string-literal. */
25197 asm_specification
= cp_parser_string_literal (parser
, false, false);
25199 /* Look for the `)'. */
25200 parens
.require_close (parser
);
25202 return asm_specification
;
25205 /* Parse an asm-operand-list.
25209 asm-operand-list , asm-operand
25212 string-literal ( expression )
25213 [ string-literal ] string-literal ( expression )
25215 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25216 each node is the expression. The TREE_PURPOSE is itself a
25217 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25218 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25219 is a STRING_CST for the string literal before the parenthesis. Returns
25220 ERROR_MARK_NODE if any of the operands are invalid. */
25223 cp_parser_asm_operand_list (cp_parser
* parser
)
25225 tree asm_operands
= NULL_TREE
;
25226 bool invalid_operands
= false;
25230 tree string_literal
;
25234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25236 /* Consume the `[' token. */
25237 cp_lexer_consume_token (parser
->lexer
);
25238 /* Read the operand name. */
25239 name
= cp_parser_identifier (parser
);
25240 if (name
!= error_mark_node
)
25241 name
= build_string (IDENTIFIER_LENGTH (name
),
25242 IDENTIFIER_POINTER (name
));
25243 /* Look for the closing `]'. */
25244 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25248 /* Look for the string-literal. */
25249 string_literal
= cp_parser_string_literal (parser
, false, false);
25251 /* Look for the `('. */
25252 matching_parens parens
;
25253 parens
.require_open (parser
);
25254 /* Parse the expression. */
25255 expression
= cp_parser_expression (parser
);
25256 /* Look for the `)'. */
25257 parens
.require_close (parser
);
25259 if (name
== error_mark_node
25260 || string_literal
== error_mark_node
25261 || expression
== error_mark_node
)
25262 invalid_operands
= true;
25264 /* Add this operand to the list. */
25265 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25268 /* If the next token is not a `,', there are no more
25270 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25272 /* Consume the `,'. */
25273 cp_lexer_consume_token (parser
->lexer
);
25276 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25279 /* Parse an asm-clobber-list.
25283 asm-clobber-list , string-literal
25285 Returns a TREE_LIST, indicating the clobbers in the order that they
25286 appeared. The TREE_VALUE of each node is a STRING_CST. */
25289 cp_parser_asm_clobber_list (cp_parser
* parser
)
25291 tree clobbers
= NULL_TREE
;
25295 tree string_literal
;
25297 /* Look for the string literal. */
25298 string_literal
= cp_parser_string_literal (parser
, false, false);
25299 /* Add it to the list. */
25300 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25301 /* If the next token is not a `,', then the list is
25303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25305 /* Consume the `,' token. */
25306 cp_lexer_consume_token (parser
->lexer
);
25312 /* Parse an asm-label-list.
25316 asm-label-list , identifier
25318 Returns a TREE_LIST, indicating the labels in the order that they
25319 appeared. The TREE_VALUE of each node is a label. */
25322 cp_parser_asm_label_list (cp_parser
* parser
)
25324 tree labels
= NULL_TREE
;
25328 tree identifier
, label
, name
;
25330 /* Look for the identifier. */
25331 identifier
= cp_parser_identifier (parser
);
25332 if (!error_operand_p (identifier
))
25334 label
= lookup_label (identifier
);
25335 if (TREE_CODE (label
) == LABEL_DECL
)
25337 TREE_USED (label
) = 1;
25338 check_goto (label
);
25339 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25340 IDENTIFIER_POINTER (identifier
));
25341 labels
= tree_cons (name
, label
, labels
);
25344 /* If the next token is not a `,', then the list is
25346 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25348 /* Consume the `,' token. */
25349 cp_lexer_consume_token (parser
->lexer
);
25352 return nreverse (labels
);
25355 /* Return TRUE iff the next tokens in the stream are possibly the
25356 beginning of a GNU extension attribute. */
25359 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25361 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25364 /* Return TRUE iff the next tokens in the stream are possibly the
25365 beginning of a standard C++-11 attribute specifier. */
25368 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25370 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25373 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25374 beginning of a standard C++-11 attribute specifier. */
25377 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25379 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25381 return (cxx_dialect
>= cxx11
25382 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25383 || (token
->type
== CPP_OPEN_SQUARE
25384 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25385 && token
->type
== CPP_OPEN_SQUARE
)));
25388 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25389 beginning of a GNU extension attribute. */
25392 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25394 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25396 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25399 /* Return true iff the next tokens can be the beginning of either a
25400 GNU attribute list, or a standard C++11 attribute sequence. */
25403 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25405 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25406 || cp_next_tokens_can_be_std_attribute_p (parser
));
25409 /* Return true iff the next Nth tokens can be the beginning of either
25410 a GNU attribute list, or a standard C++11 attribute sequence. */
25413 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25415 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25416 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25419 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25420 of GNU attributes, or return NULL. */
25423 cp_parser_attributes_opt (cp_parser
*parser
)
25425 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25426 return cp_parser_gnu_attributes_opt (parser
);
25427 return cp_parser_std_attribute_spec_seq (parser
);
25430 /* Parse an (optional) series of attributes.
25433 attributes attribute
25436 __attribute__ (( attribute-list [opt] ))
25438 The return value is as for cp_parser_gnu_attribute_list. */
25441 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25443 tree attributes
= NULL_TREE
;
25445 temp_override
<bool> cleanup
25446 (parser
->auto_is_implicit_function_template_parm_p
, false);
25451 tree attribute_list
;
25454 /* Peek at the next token. */
25455 token
= cp_lexer_peek_token (parser
->lexer
);
25456 /* If it's not `__attribute__', then we're done. */
25457 if (token
->keyword
!= RID_ATTRIBUTE
)
25460 /* Consume the `__attribute__' keyword. */
25461 cp_lexer_consume_token (parser
->lexer
);
25462 /* Look for the two `(' tokens. */
25463 matching_parens outer_parens
;
25464 outer_parens
.require_open (parser
);
25465 matching_parens inner_parens
;
25466 inner_parens
.require_open (parser
);
25468 /* Peek at the next token. */
25469 token
= cp_lexer_peek_token (parser
->lexer
);
25470 if (token
->type
!= CPP_CLOSE_PAREN
)
25471 /* Parse the attribute-list. */
25472 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25474 /* If the next token is a `)', then there is no attribute
25476 attribute_list
= NULL
;
25478 /* Look for the two `)' tokens. */
25479 if (!inner_parens
.require_close (parser
))
25481 if (!outer_parens
.require_close (parser
))
25484 cp_parser_skip_to_end_of_statement (parser
);
25486 /* Add these new attributes to the list. */
25487 attributes
= attr_chainon (attributes
, attribute_list
);
25493 /* Parse a GNU attribute-list.
25497 attribute-list , attribute
25501 identifier ( identifier )
25502 identifier ( identifier , expression-list )
25503 identifier ( expression-list )
25505 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25506 to an attribute. The TREE_PURPOSE of each node is the identifier
25507 indicating which attribute is in use. The TREE_VALUE represents
25508 the arguments, if any. */
25511 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25513 tree attribute_list
= NULL_TREE
;
25514 bool save_translate_strings_p
= parser
->translate_strings_p
;
25516 parser
->translate_strings_p
= false;
25523 /* Look for the identifier. We also allow keywords here; for
25524 example `__attribute__ ((const))' is legal. */
25525 token
= cp_lexer_peek_token (parser
->lexer
);
25526 if (token
->type
== CPP_NAME
25527 || token
->type
== CPP_KEYWORD
)
25529 tree arguments
= NULL_TREE
;
25531 /* Consume the token, but save it since we need it for the
25532 SIMD enabled function parsing. */
25533 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25535 /* Save away the identifier that indicates which attribute
25537 identifier
= (token
->type
== CPP_KEYWORD
)
25538 /* For keywords, use the canonical spelling, not the
25539 parsed identifier. */
25540 ? ridpointers
[(int) token
->keyword
]
25541 : id_token
->u
.value
;
25543 identifier
= canonicalize_attr_name (identifier
);
25544 attribute
= build_tree_list (identifier
, NULL_TREE
);
25546 /* Peek at the next token. */
25547 token
= cp_lexer_peek_token (parser
->lexer
);
25548 /* If it's an `(', then parse the attribute arguments. */
25549 if (token
->type
== CPP_OPEN_PAREN
)
25551 vec
<tree
, va_gc
> *vec
;
25552 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25553 ? id_attr
: normal_attr
);
25554 vec
= cp_parser_parenthesized_expression_list
25555 (parser
, attr_flag
, /*cast_p=*/false,
25556 /*allow_expansion_p=*/false,
25557 /*non_constant_p=*/NULL
);
25559 arguments
= error_mark_node
;
25562 arguments
= build_tree_list_vec (vec
);
25563 release_tree_vector (vec
);
25565 /* Save the arguments away. */
25566 TREE_VALUE (attribute
) = arguments
;
25569 if (arguments
!= error_mark_node
)
25571 /* Add this attribute to the list. */
25572 TREE_CHAIN (attribute
) = attribute_list
;
25573 attribute_list
= attribute
;
25576 token
= cp_lexer_peek_token (parser
->lexer
);
25578 /* Unless EXACTLY_ONE is set look for more attributes.
25579 If the next token isn't a `,', we're done. */
25580 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25583 /* Consume the comma and keep going. */
25584 cp_lexer_consume_token (parser
->lexer
);
25586 parser
->translate_strings_p
= save_translate_strings_p
;
25588 /* We built up the list in reverse order. */
25589 return nreverse (attribute_list
);
25592 /* Parse a standard C++11 attribute.
25594 The returned representation is a TREE_LIST which TREE_PURPOSE is
25595 the scoped name of the attribute, and the TREE_VALUE is its
25598 Note that the scoped name of the attribute is itself a TREE_LIST
25599 which TREE_PURPOSE is the namespace of the attribute, and
25600 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25601 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25602 and which TREE_PURPOSE is directly the attribute name.
25604 Clients of the attribute code should use get_attribute_namespace
25605 and get_attribute_name to get the actual namespace and name of
25606 attributes, regardless of their being GNU or C++11 attributes.
25609 attribute-token attribute-argument-clause [opt]
25613 attribute-scoped-token
25615 attribute-scoped-token:
25616 attribute-namespace :: identifier
25618 attribute-namespace:
25621 attribute-argument-clause:
25622 ( balanced-token-seq )
25624 balanced-token-seq:
25625 balanced-token [opt]
25626 balanced-token-seq balanced-token
25629 ( balanced-token-seq )
25630 [ balanced-token-seq ]
25631 { balanced-token-seq }. */
25634 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25636 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25639 temp_override
<bool> cleanup
25640 (parser
->auto_is_implicit_function_template_parm_p
, false);
25642 /* First, parse name of the attribute, a.k.a attribute-token. */
25644 token
= cp_lexer_peek_token (parser
->lexer
);
25645 if (token
->type
== CPP_NAME
)
25646 attr_id
= token
->u
.value
;
25647 else if (token
->type
== CPP_KEYWORD
)
25648 attr_id
= ridpointers
[(int) token
->keyword
];
25649 else if (token
->flags
& NAMED_OP
)
25650 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25652 if (attr_id
== NULL_TREE
)
25655 cp_lexer_consume_token (parser
->lexer
);
25657 token
= cp_lexer_peek_token (parser
->lexer
);
25658 if (token
->type
== CPP_SCOPE
)
25660 /* We are seeing a scoped attribute token. */
25662 cp_lexer_consume_token (parser
->lexer
);
25664 error_at (token
->location
, "attribute using prefix used together "
25665 "with scoped attribute token");
25668 token
= cp_lexer_consume_token (parser
->lexer
);
25669 if (token
->type
== CPP_NAME
)
25670 attr_id
= token
->u
.value
;
25671 else if (token
->type
== CPP_KEYWORD
)
25672 attr_id
= ridpointers
[(int) token
->keyword
];
25673 else if (token
->flags
& NAMED_OP
)
25674 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25677 error_at (token
->location
,
25678 "expected an identifier for the attribute name");
25679 return error_mark_node
;
25682 attr_ns
= canonicalize_attr_name (attr_ns
);
25683 attr_id
= canonicalize_attr_name (attr_id
);
25684 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25686 token
= cp_lexer_peek_token (parser
->lexer
);
25690 attr_ns
= canonicalize_attr_name (attr_ns
);
25691 attr_id
= canonicalize_attr_name (attr_id
);
25692 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25697 attr_id
= canonicalize_attr_name (attr_id
);
25698 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25700 /* C++11 noreturn attribute is equivalent to GNU's. */
25701 if (is_attribute_p ("noreturn", attr_id
))
25702 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25703 /* C++14 deprecated attribute is equivalent to GNU's. */
25704 else if (is_attribute_p ("deprecated", attr_id
))
25705 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25706 /* C++17 fallthrough attribute is equivalent to GNU's. */
25707 else if (is_attribute_p ("fallthrough", attr_id
))
25708 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25709 /* Transactional Memory TS optimize_for_synchronized attribute is
25710 equivalent to GNU transaction_callable. */
25711 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25712 TREE_PURPOSE (attribute
)
25713 = get_identifier ("transaction_callable");
25714 /* Transactional Memory attributes are GNU attributes. */
25715 else if (tm_attr_to_mask (attr_id
))
25716 TREE_PURPOSE (attribute
) = attr_id
;
25719 /* Now parse the optional argument clause of the attribute. */
25721 if (token
->type
!= CPP_OPEN_PAREN
)
25725 vec
<tree
, va_gc
> *vec
;
25726 int attr_flag
= normal_attr
;
25728 if (attr_ns
== gnu_identifier
25729 && attribute_takes_identifier_p (attr_id
))
25730 /* A GNU attribute that takes an identifier in parameter. */
25731 attr_flag
= id_attr
;
25733 vec
= cp_parser_parenthesized_expression_list
25734 (parser
, attr_flag
, /*cast_p=*/false,
25735 /*allow_expansion_p=*/true,
25736 /*non_constant_p=*/NULL
);
25738 arguments
= error_mark_node
;
25741 arguments
= build_tree_list_vec (vec
);
25742 release_tree_vector (vec
);
25745 if (arguments
== error_mark_node
)
25746 attribute
= error_mark_node
;
25748 TREE_VALUE (attribute
) = arguments
;
25754 /* Check that the attribute ATTRIBUTE appears at most once in the
25755 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25756 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25757 isn't implemented yet in GCC. */
25760 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25764 tree name
= get_attribute_name (attribute
);
25765 if (is_attribute_p ("noreturn", name
)
25766 && lookup_attribute ("noreturn", attributes
))
25767 error ("attribute %<noreturn%> can appear at most once "
25768 "in an attribute-list");
25769 else if (is_attribute_p ("deprecated", name
)
25770 && lookup_attribute ("deprecated", attributes
))
25771 error ("attribute %<deprecated%> can appear at most once "
25772 "in an attribute-list");
25776 /* Parse a list of standard C++-11 attributes.
25780 attribute-list , attribute[opt]
25782 attribute-list , attribute ...
25786 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25788 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25789 cp_token
*token
= NULL
;
25793 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25794 if (attribute
== error_mark_node
)
25796 if (attribute
!= NULL_TREE
)
25798 cp_parser_check_std_attribute (attributes
, attribute
);
25799 TREE_CHAIN (attribute
) = attributes
;
25800 attributes
= attribute
;
25802 token
= cp_lexer_peek_token (parser
->lexer
);
25803 if (token
->type
== CPP_ELLIPSIS
)
25805 cp_lexer_consume_token (parser
->lexer
);
25806 if (attribute
== NULL_TREE
)
25807 error_at (token
->location
,
25808 "expected attribute before %<...%>");
25811 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25812 if (pack
== error_mark_node
)
25813 return error_mark_node
;
25814 TREE_VALUE (attribute
) = pack
;
25816 token
= cp_lexer_peek_token (parser
->lexer
);
25818 if (token
->type
!= CPP_COMMA
)
25820 cp_lexer_consume_token (parser
->lexer
);
25822 attributes
= nreverse (attributes
);
25826 /* Parse a standard C++-11 attribute specifier.
25828 attribute-specifier:
25829 [ [ attribute-using-prefix [opt] attribute-list ] ]
25830 alignment-specifier
25832 attribute-using-prefix:
25833 using attribute-namespace :
25835 alignment-specifier:
25836 alignas ( type-id ... [opt] )
25837 alignas ( alignment-expression ... [opt] ). */
25840 cp_parser_std_attribute_spec (cp_parser
*parser
)
25842 tree attributes
= NULL_TREE
;
25843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25845 if (token
->type
== CPP_OPEN_SQUARE
25846 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25848 tree attr_ns
= NULL_TREE
;
25850 cp_lexer_consume_token (parser
->lexer
);
25851 cp_lexer_consume_token (parser
->lexer
);
25853 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25855 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25856 if (token
->type
== CPP_NAME
)
25857 attr_ns
= token
->u
.value
;
25858 else if (token
->type
== CPP_KEYWORD
)
25859 attr_ns
= ridpointers
[(int) token
->keyword
];
25860 else if (token
->flags
& NAMED_OP
)
25861 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25864 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25866 if (cxx_dialect
< cxx17
25867 && !in_system_header_at (input_location
))
25868 pedwarn (input_location
, 0,
25869 "attribute using prefix only available "
25870 "with -std=c++17 or -std=gnu++17");
25872 cp_lexer_consume_token (parser
->lexer
);
25873 cp_lexer_consume_token (parser
->lexer
);
25874 cp_lexer_consume_token (parser
->lexer
);
25877 attr_ns
= NULL_TREE
;
25880 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25882 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25883 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25884 cp_parser_skip_to_end_of_statement (parser
);
25886 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25887 when we are sure that we have actually parsed them. */
25888 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25894 /* Look for an alignment-specifier. */
25896 token
= cp_lexer_peek_token (parser
->lexer
);
25898 if (token
->type
!= CPP_KEYWORD
25899 || token
->keyword
!= RID_ALIGNAS
)
25902 cp_lexer_consume_token (parser
->lexer
);
25903 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25905 matching_parens parens
;
25906 if (!parens
.require_open (parser
))
25907 return error_mark_node
;
25909 cp_parser_parse_tentatively (parser
);
25910 alignas_expr
= cp_parser_type_id (parser
);
25912 if (!cp_parser_parse_definitely (parser
))
25914 alignas_expr
= cp_parser_assignment_expression (parser
);
25915 if (alignas_expr
== error_mark_node
)
25916 cp_parser_skip_to_end_of_statement (parser
);
25917 if (alignas_expr
== NULL_TREE
25918 || alignas_expr
== error_mark_node
)
25919 return alignas_expr
;
25922 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25923 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25925 /* Handle alignas (pack...). */
25926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25928 cp_lexer_consume_token (parser
->lexer
);
25929 alignas_expr
= make_pack_expansion (alignas_expr
);
25932 /* Something went wrong, so don't build the attribute. */
25933 if (alignas_expr
== error_mark_node
)
25934 return error_mark_node
;
25936 if (!parens
.require_close (parser
))
25937 return error_mark_node
;
25939 /* Build the C++-11 representation of an 'aligned'
25942 = build_tree_list (build_tree_list (gnu_identifier
,
25943 aligned_identifier
), alignas_expr
);
25949 /* Parse a standard C++-11 attribute-specifier-seq.
25951 attribute-specifier-seq:
25952 attribute-specifier-seq [opt] attribute-specifier
25956 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25958 tree attr_specs
= NULL_TREE
;
25959 tree attr_last
= NULL_TREE
;
25963 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25964 if (attr_spec
== NULL_TREE
)
25966 if (attr_spec
== error_mark_node
)
25967 return error_mark_node
;
25970 TREE_CHAIN (attr_last
) = attr_spec
;
25972 attr_specs
= attr_last
= attr_spec
;
25973 attr_last
= tree_last (attr_last
);
25979 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25980 return index of the first token after balanced-token, or N on failure. */
25983 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25986 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25988 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25991 case CPP_PRAGMA_EOL
:
25992 /* Ran out of tokens. */
25994 case CPP_OPEN_PAREN
:
25997 case CPP_OPEN_BRACE
:
26000 case CPP_OPEN_SQUARE
:
26003 case CPP_CLOSE_PAREN
:
26006 case CPP_CLOSE_BRACE
:
26009 case CPP_CLOSE_SQUARE
:
26015 while (nparens
|| nbraces
|| nsquares
);
26019 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26020 return index of the first token after the GNU attribute tokens, or N on
26024 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26028 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26029 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26030 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26033 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26036 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26043 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26044 next token), return index of the first token after the standard C++11
26045 attribute tokens, or N on failure. */
26048 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26052 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26053 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26055 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26058 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26062 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26063 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26065 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26076 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26077 as the next token), return index of the first token after the attribute
26078 tokens, or N on failure. */
26081 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26083 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26084 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26085 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26088 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26089 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26090 current value of the PEDANTIC flag, regardless of whether or not
26091 the `__extension__' keyword is present. The caller is responsible
26092 for restoring the value of the PEDANTIC flag. */
26095 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26097 /* Save the old value of the PEDANTIC flag. */
26098 *saved_pedantic
= pedantic
;
26100 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26102 /* Consume the `__extension__' token. */
26103 cp_lexer_consume_token (parser
->lexer
);
26104 /* We're not being pedantic while the `__extension__' keyword is
26114 /* Parse a label declaration.
26117 __label__ label-declarator-seq ;
26119 label-declarator-seq:
26120 identifier , label-declarator-seq
26124 cp_parser_label_declaration (cp_parser
* parser
)
26126 /* Look for the `__label__' keyword. */
26127 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26133 /* Look for an identifier. */
26134 identifier
= cp_parser_identifier (parser
);
26135 /* If we failed, stop. */
26136 if (identifier
== error_mark_node
)
26138 /* Declare it as a label. */
26139 finish_label_decl (identifier
);
26140 /* If the next token is a `;', stop. */
26141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26143 /* Look for the `,' separating the label declarations. */
26144 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26147 /* Look for the final `;'. */
26148 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26151 // -------------------------------------------------------------------------- //
26154 // Parse a requires clause.
26156 // requires-clause:
26157 // 'requires' logical-or-expression
26159 // The required logical-or-expression must be a constant expression. Note
26160 // that we don't check that the expression is constepxr here. We defer until
26161 // we analyze constraints and then, we only check atomic constraints.
26163 cp_parser_requires_clause (cp_parser
*parser
)
26165 // Parse the requires clause so that it is not automatically folded.
26166 ++processing_template_decl
;
26167 tree expr
= cp_parser_binary_expression (parser
, false, false,
26168 PREC_NOT_OPERATOR
, NULL
);
26169 if (check_for_bare_parameter_packs (expr
))
26170 expr
= error_mark_node
;
26171 --processing_template_decl
;
26175 // Optionally parse a requires clause:
26177 cp_parser_requires_clause_opt (cp_parser
*parser
)
26179 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26180 if (tok
->keyword
!= RID_REQUIRES
)
26182 if (!flag_concepts
&& tok
->type
== CPP_NAME
26183 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26185 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26186 "%<requires%> only available with -fconcepts");
26187 /* Parse and discard the requires-clause. */
26188 cp_lexer_consume_token (parser
->lexer
);
26189 cp_parser_requires_clause (parser
);
26193 cp_lexer_consume_token (parser
->lexer
);
26194 return cp_parser_requires_clause (parser
);
26198 /*---------------------------------------------------------------------------
26199 Requires expressions
26200 ---------------------------------------------------------------------------*/
26202 /* Parse a requires expression
26204 requirement-expression:
26205 'requires' requirement-parameter-list [opt] requirement-body */
26207 cp_parser_requires_expression (cp_parser
*parser
)
26209 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26210 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26212 /* A requires-expression shall appear only within a concept
26213 definition or a requires-clause.
26215 TODO: Implement this diagnostic correctly. */
26216 if (!processing_template_decl
)
26218 error_at (loc
, "a requires expression cannot appear outside a template");
26219 cp_parser_skip_to_end_of_statement (parser
);
26220 return error_mark_node
;
26225 /* Local parameters are delared as variables within the scope
26226 of the expression. They are not visible past the end of
26227 the expression. Expressions within the requires-expression
26228 are unevaluated. */
26229 struct scope_sentinel
26233 ++cp_unevaluated_operand
;
26234 begin_scope (sk_block
, NULL_TREE
);
26239 pop_bindings_and_leave_scope ();
26240 --cp_unevaluated_operand
;
26244 /* Parse the optional parameter list. */
26245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26247 parms
= cp_parser_requirement_parameter_list (parser
);
26248 if (parms
== error_mark_node
)
26249 return error_mark_node
;
26254 /* Parse the requirement body. */
26255 reqs
= cp_parser_requirement_body (parser
);
26256 if (reqs
== error_mark_node
)
26257 return error_mark_node
;
26260 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26262 grokparms (parms
, &parms
);
26263 return finish_requires_expr (parms
, reqs
);
26266 /* Parse a parameterized requirement.
26268 requirement-parameter-list:
26269 '(' parameter-declaration-clause ')' */
26271 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26273 matching_parens parens
;
26274 if (!parens
.require_open (parser
))
26275 return error_mark_node
;
26277 tree parms
= cp_parser_parameter_declaration_clause (parser
);
26279 if (!parens
.require_close (parser
))
26280 return error_mark_node
;
26285 /* Parse the body of a requirement.
26288 '{' requirement-list '}' */
26290 cp_parser_requirement_body (cp_parser
*parser
)
26292 matching_braces braces
;
26293 if (!braces
.require_open (parser
))
26294 return error_mark_node
;
26296 tree reqs
= cp_parser_requirement_list (parser
);
26298 if (!braces
.require_close (parser
))
26299 return error_mark_node
;
26304 /* Parse a list of requirements.
26308 requirement-list ';' requirement[opt] */
26310 cp_parser_requirement_list (cp_parser
*parser
)
26312 tree result
= NULL_TREE
;
26315 tree req
= cp_parser_requirement (parser
);
26316 if (req
== error_mark_node
)
26317 return error_mark_node
;
26319 result
= tree_cons (NULL_TREE
, req
, result
);
26321 /* If we see a semi-colon, consume it. */
26322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26323 cp_lexer_consume_token (parser
->lexer
);
26325 /* Stop processing at the end of the list. */
26326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26330 /* Reverse the order of requirements so they are analyzed in
26331 declaration order. */
26332 return nreverse (result
);
26335 /* Parse a syntactic requirement or type requirement.
26339 compound-requirement
26341 nested-requirement */
26343 cp_parser_requirement (cp_parser
*parser
)
26345 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26346 return cp_parser_compound_requirement (parser
);
26347 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26348 return cp_parser_type_requirement (parser
);
26349 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26350 return cp_parser_nested_requirement (parser
);
26352 return cp_parser_simple_requirement (parser
);
26355 /* Parse a simple requirement.
26357 simple-requirement:
26360 cp_parser_simple_requirement (cp_parser
*parser
)
26362 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26363 if (!expr
|| expr
== error_mark_node
)
26364 return error_mark_node
;
26366 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26367 return error_mark_node
;
26369 return finish_simple_requirement (expr
);
26372 /* Parse a type requirement
26375 nested-name-specifier [opt] required-type-name ';'
26377 required-type-name:
26379 'template' [opt] simple-template-id */
26381 cp_parser_type_requirement (cp_parser
*parser
)
26383 cp_lexer_consume_token (parser
->lexer
);
26385 // Save the scope before parsing name specifiers.
26386 tree saved_scope
= parser
->scope
;
26387 tree saved_object_scope
= parser
->object_scope
;
26388 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26389 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26390 cp_parser_nested_name_specifier_opt (parser
,
26391 /*typename_keyword_p=*/true,
26392 /*check_dependency_p=*/false,
26394 /*is_declaration=*/false);
26397 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26399 cp_lexer_consume_token (parser
->lexer
);
26400 type
= cp_parser_template_id (parser
,
26401 /*template_keyword_p=*/true,
26402 /*check_dependency=*/false,
26403 /*tag_type=*/none_type
,
26404 /*is_declaration=*/false);
26405 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26406 /*complain=*/tf_error
);
26409 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26411 if (TREE_CODE (type
) == TYPE_DECL
)
26412 type
= TREE_TYPE (type
);
26414 parser
->scope
= saved_scope
;
26415 parser
->object_scope
= saved_object_scope
;
26416 parser
->qualifying_scope
= saved_qualifying_scope
;
26418 if (type
== error_mark_node
)
26419 cp_parser_skip_to_end_of_statement (parser
);
26421 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26422 return error_mark_node
;
26423 if (type
== error_mark_node
)
26424 return error_mark_node
;
26426 return finish_type_requirement (type
);
26429 /* Parse a compound requirement
26431 compound-requirement:
26432 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26434 cp_parser_compound_requirement (cp_parser
*parser
)
26436 /* Parse an expression enclosed in '{ }'s. */
26437 matching_braces braces
;
26438 if (!braces
.require_open (parser
))
26439 return error_mark_node
;
26441 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26442 if (!expr
|| expr
== error_mark_node
)
26443 return error_mark_node
;
26445 if (!braces
.require_close (parser
))
26446 return error_mark_node
;
26448 /* Parse the optional noexcept. */
26449 bool noexcept_p
= false;
26450 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26452 cp_lexer_consume_token (parser
->lexer
);
26456 /* Parse the optional trailing return type. */
26457 tree type
= NULL_TREE
;
26458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26460 cp_lexer_consume_token (parser
->lexer
);
26461 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26462 parser
->in_result_type_constraint_p
= true;
26463 type
= cp_parser_trailing_type_id (parser
);
26464 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26465 if (type
== error_mark_node
)
26466 return error_mark_node
;
26469 return finish_compound_requirement (expr
, type
, noexcept_p
);
26472 /* Parse a nested requirement. This is the same as a requires clause.
26474 nested-requirement:
26477 cp_parser_nested_requirement (cp_parser
*parser
)
26479 cp_lexer_consume_token (parser
->lexer
);
26480 tree req
= cp_parser_requires_clause (parser
);
26481 if (req
== error_mark_node
)
26482 return error_mark_node
;
26483 return finish_nested_requirement (req
);
26486 /* Support Functions */
26488 /* Return the appropriate prefer_type argument for lookup_name_real based on
26489 tag_type and template_mem_access. */
26492 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26494 /* DR 141: When looking in the current enclosing context for a template-name
26495 after -> or ., only consider class templates. */
26496 if (template_mem_access
)
26500 case none_type
: return 0; // No preference.
26501 case scope_type
: return 1; // Type or namespace.
26502 default: return 2; // Type only.
26506 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26507 NAME should have one of the representations used for an
26508 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26509 is returned. If PARSER->SCOPE is a dependent type, then a
26510 SCOPE_REF is returned.
26512 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26513 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26514 was formed. Abstractly, such entities should not be passed to this
26515 function, because they do not need to be looked up, but it is
26516 simpler to check for this special case here, rather than at the
26519 In cases not explicitly covered above, this function returns a
26520 DECL, OVERLOAD, or baselink representing the result of the lookup.
26521 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26524 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26525 (e.g., "struct") that was used. In that case bindings that do not
26526 refer to types are ignored.
26528 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26531 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26534 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26537 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26538 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26539 NULL_TREE otherwise. */
26542 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26543 enum tag_types tag_type
,
26546 bool check_dependency
,
26547 tree
*ambiguous_decls
,
26548 location_t name_location
)
26551 tree object_type
= parser
->context
->object_type
;
26553 /* Assume that the lookup will be unambiguous. */
26554 if (ambiguous_decls
)
26555 *ambiguous_decls
= NULL_TREE
;
26557 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26558 no longer valid. Note that if we are parsing tentatively, and
26559 the parse fails, OBJECT_TYPE will be automatically restored. */
26560 parser
->context
->object_type
= NULL_TREE
;
26562 if (name
== error_mark_node
)
26563 return error_mark_node
;
26565 /* A template-id has already been resolved; there is no lookup to
26567 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26569 if (BASELINK_P (name
))
26571 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26572 == TEMPLATE_ID_EXPR
);
26576 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26577 it should already have been checked to make sure that the name
26578 used matches the type being destroyed. */
26579 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26583 /* Figure out to which type this destructor applies. */
26585 type
= parser
->scope
;
26586 else if (object_type
)
26587 type
= object_type
;
26589 type
= current_class_type
;
26590 /* If that's not a class type, there is no destructor. */
26591 if (!type
|| !CLASS_TYPE_P (type
))
26592 return error_mark_node
;
26594 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26595 lazily_declare_fn (sfk_destructor
, type
);
26597 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26600 return error_mark_node
;
26603 /* By this point, the NAME should be an ordinary identifier. If
26604 the id-expression was a qualified name, the qualifying scope is
26605 stored in PARSER->SCOPE at this point. */
26606 gcc_assert (identifier_p (name
));
26608 /* Perform the lookup. */
26613 if (parser
->scope
== error_mark_node
)
26614 return error_mark_node
;
26616 /* If the SCOPE is dependent, the lookup must be deferred until
26617 the template is instantiated -- unless we are explicitly
26618 looking up names in uninstantiated templates. Even then, we
26619 cannot look up the name if the scope is not a class type; it
26620 might, for example, be a template type parameter. */
26621 dependent_p
= (TYPE_P (parser
->scope
)
26622 && dependent_scope_p (parser
->scope
));
26623 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26625 /* Defer lookup. */
26626 decl
= error_mark_node
;
26629 tree pushed_scope
= NULL_TREE
;
26631 /* If PARSER->SCOPE is a dependent type, then it must be a
26632 class type, and we must not be checking dependencies;
26633 otherwise, we would have processed this lookup above. So
26634 that PARSER->SCOPE is not considered a dependent base by
26635 lookup_member, we must enter the scope here. */
26637 pushed_scope
= push_scope (parser
->scope
);
26639 /* If the PARSER->SCOPE is a template specialization, it
26640 may be instantiated during name lookup. In that case,
26641 errors may be issued. Even if we rollback the current
26642 tentative parse, those errors are valid. */
26643 decl
= lookup_qualified_name (parser
->scope
, name
,
26644 prefer_type_arg (tag_type
),
26645 /*complain=*/true);
26647 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26648 lookup result and the nested-name-specifier nominates a class C:
26649 * if the name specified after the nested-name-specifier, when
26650 looked up in C, is the injected-class-name of C (Clause 9), or
26651 * if the name specified after the nested-name-specifier is the
26652 same as the identifier or the simple-template-id's template-
26653 name in the last component of the nested-name-specifier,
26654 the name is instead considered to name the constructor of
26655 class C. [ Note: for example, the constructor is not an
26656 acceptable lookup result in an elaborated-type-specifier so
26657 the constructor would not be used in place of the
26658 injected-class-name. --end note ] Such a constructor name
26659 shall be used only in the declarator-id of a declaration that
26660 names a constructor or in a using-declaration. */
26661 if (tag_type
== none_type
26662 && DECL_SELF_REFERENCE_P (decl
)
26663 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26664 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26665 prefer_type_arg (tag_type
),
26666 /*complain=*/true);
26668 /* If we have a single function from a using decl, pull it out. */
26669 if (TREE_CODE (decl
) == OVERLOAD
26670 && !really_overloaded_fn (decl
))
26671 decl
= OVL_FUNCTION (decl
);
26674 pop_scope (pushed_scope
);
26677 /* If the scope is a dependent type and either we deferred lookup or
26678 we did lookup but didn't find the name, rememeber the name. */
26679 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26680 && dependent_type_p (parser
->scope
))
26686 /* The resolution to Core Issue 180 says that `struct
26687 A::B' should be considered a type-name, even if `A'
26689 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26690 /*complain=*/tf_error
);
26691 if (type
!= error_mark_node
)
26692 decl
= TYPE_NAME (type
);
26694 else if (is_template
26695 && (cp_parser_next_token_ends_template_argument_p (parser
)
26696 || cp_lexer_next_token_is (parser
->lexer
,
26698 decl
= make_unbound_class_template (parser
->scope
,
26700 /*complain=*/tf_error
);
26702 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26703 parser
->scope
, name
,
26706 parser
->qualifying_scope
= parser
->scope
;
26707 parser
->object_scope
= NULL_TREE
;
26709 else if (object_type
)
26711 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26712 OBJECT_TYPE is not a class. */
26713 if (CLASS_TYPE_P (object_type
))
26714 /* If the OBJECT_TYPE is a template specialization, it may
26715 be instantiated during name lookup. In that case, errors
26716 may be issued. Even if we rollback the current tentative
26717 parse, those errors are valid. */
26718 decl
= lookup_member (object_type
,
26721 prefer_type_arg (tag_type
),
26722 tf_warning_or_error
);
26727 /* Look it up in the enclosing context. DR 141: When looking for a
26728 template-name after -> or ., only consider class templates. */
26729 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26731 /*block_p=*/true, is_namespace
, 0);
26732 if (object_type
== unknown_type_node
)
26733 /* The object is type-dependent, so we can't look anything up; we used
26734 this to get the DR 141 behavior. */
26735 object_type
= NULL_TREE
;
26736 parser
->object_scope
= object_type
;
26737 parser
->qualifying_scope
= NULL_TREE
;
26741 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26743 /*block_p=*/true, is_namespace
, 0);
26744 parser
->qualifying_scope
= NULL_TREE
;
26745 parser
->object_scope
= NULL_TREE
;
26748 /* If the lookup failed, let our caller know. */
26749 if (!decl
|| decl
== error_mark_node
)
26750 return error_mark_node
;
26752 /* Pull out the template from an injected-class-name (or multiple). */
26754 decl
= maybe_get_template_decl_from_type_decl (decl
);
26756 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26757 if (TREE_CODE (decl
) == TREE_LIST
)
26759 if (ambiguous_decls
)
26760 *ambiguous_decls
= decl
;
26761 /* The error message we have to print is too complicated for
26762 cp_parser_error, so we incorporate its actions directly. */
26763 if (!cp_parser_simulate_error (parser
))
26765 error_at (name_location
, "reference to %qD is ambiguous",
26767 print_candidates (decl
);
26769 return error_mark_node
;
26772 gcc_assert (DECL_P (decl
)
26773 || TREE_CODE (decl
) == OVERLOAD
26774 || TREE_CODE (decl
) == SCOPE_REF
26775 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26776 || BASELINK_P (decl
));
26778 /* If we have resolved the name of a member declaration, check to
26779 see if the declaration is accessible. When the name resolves to
26780 set of overloaded functions, accessibility is checked when
26781 overload resolution is done.
26783 During an explicit instantiation, access is not checked at all,
26784 as per [temp.explicit]. */
26786 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26788 maybe_record_typedef_use (decl
);
26790 return cp_expr (decl
, name_location
);
26793 /* Like cp_parser_lookup_name, but for use in the typical case where
26794 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26795 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26798 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26800 return cp_parser_lookup_name (parser
, name
,
26802 /*is_template=*/false,
26803 /*is_namespace=*/false,
26804 /*check_dependency=*/true,
26805 /*ambiguous_decls=*/NULL
,
26809 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26810 the current context, return the TYPE_DECL. If TAG_NAME_P is
26811 true, the DECL indicates the class being defined in a class-head,
26812 or declared in an elaborated-type-specifier.
26814 Otherwise, return DECL. */
26817 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26819 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26820 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26823 template <typename T> struct B;
26826 template <typename T> struct A::B {};
26828 Similarly, in an elaborated-type-specifier:
26830 namespace N { struct X{}; }
26833 template <typename T> friend struct N::X;
26836 However, if the DECL refers to a class type, and we are in
26837 the scope of the class, then the name lookup automatically
26838 finds the TYPE_DECL created by build_self_reference rather
26839 than a TEMPLATE_DECL. For example, in:
26841 template <class T> struct S {
26845 there is no need to handle such case. */
26847 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26848 return DECL_TEMPLATE_RESULT (decl
);
26853 /* If too many, or too few, template-parameter lists apply to the
26854 declarator, issue an error message. Returns TRUE if all went well,
26855 and FALSE otherwise. */
26858 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26859 cp_declarator
*declarator
,
26860 location_t declarator_location
)
26862 switch (declarator
->kind
)
26866 unsigned num_templates
= 0;
26867 tree scope
= declarator
->u
.id
.qualifying_scope
;
26868 bool template_id_p
= false;
26871 num_templates
= num_template_headers_for_class (scope
);
26872 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26873 == TEMPLATE_ID_EXPR
)
26875 /* If the DECLARATOR has the form `X<y>' then it uses one
26876 additional level of template parameters. */
26878 template_id_p
= true;
26881 return cp_parser_check_template_parameters
26882 (parser
, num_templates
, template_id_p
, declarator_location
,
26889 case cdk_reference
:
26891 return (cp_parser_check_declarator_template_parameters
26892 (parser
, declarator
->declarator
, declarator_location
));
26899 gcc_unreachable ();
26904 /* NUM_TEMPLATES were used in the current declaration. If that is
26905 invalid, return FALSE and issue an error messages. Otherwise,
26906 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26907 declarator and we can print more accurate diagnostics. */
26910 cp_parser_check_template_parameters (cp_parser
* parser
,
26911 unsigned num_templates
,
26912 bool template_id_p
,
26913 location_t location
,
26914 cp_declarator
*declarator
)
26916 /* If there are the same number of template classes and parameter
26917 lists, that's OK. */
26918 if (parser
->num_template_parameter_lists
== num_templates
)
26920 /* If there are more, but only one more, and the name ends in an identifier,
26921 then we are declaring a primary template. That's OK too. */
26923 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26925 /* If there are more template classes than parameter lists, we have
26928 template <class T> void S<T>::R<T>::f (); */
26929 if (parser
->num_template_parameter_lists
< num_templates
)
26931 if (declarator
&& !current_function_decl
)
26932 error_at (location
, "specializing member %<%T::%E%> "
26933 "requires %<template<>%> syntax",
26934 declarator
->u
.id
.qualifying_scope
,
26935 declarator
->u
.id
.unqualified_name
);
26936 else if (declarator
)
26937 error_at (location
, "invalid declaration of %<%T::%E%>",
26938 declarator
->u
.id
.qualifying_scope
,
26939 declarator
->u
.id
.unqualified_name
);
26941 error_at (location
, "too few template-parameter-lists");
26944 /* Otherwise, there are too many template parameter lists. We have
26947 template <class T> template <class U> void S::f(); */
26948 error_at (location
, "too many template-parameter-lists");
26952 /* Parse an optional `::' token indicating that the following name is
26953 from the global namespace. If so, PARSER->SCOPE is set to the
26954 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26955 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26956 Returns the new value of PARSER->SCOPE, if the `::' token is
26957 present, and NULL_TREE otherwise. */
26960 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26964 /* Peek at the next token. */
26965 token
= cp_lexer_peek_token (parser
->lexer
);
26966 /* If we're looking at a `::' token then we're starting from the
26967 global namespace, not our current location. */
26968 if (token
->type
== CPP_SCOPE
)
26970 /* Consume the `::' token. */
26971 cp_lexer_consume_token (parser
->lexer
);
26972 /* Set the SCOPE so that we know where to start the lookup. */
26973 parser
->scope
= global_namespace
;
26974 parser
->qualifying_scope
= global_namespace
;
26975 parser
->object_scope
= NULL_TREE
;
26977 return parser
->scope
;
26979 else if (!current_scope_valid_p
)
26981 parser
->scope
= NULL_TREE
;
26982 parser
->qualifying_scope
= NULL_TREE
;
26983 parser
->object_scope
= NULL_TREE
;
26989 /* Returns TRUE if the upcoming token sequence is the start of a
26990 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26991 declarator is preceded by the `friend' specifier. */
26994 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26996 bool constructor_p
;
26997 bool outside_class_specifier_p
;
26998 tree nested_name_specifier
;
26999 cp_token
*next_token
;
27001 /* The common case is that this is not a constructor declarator, so
27002 try to avoid doing lots of work if at all possible. It's not
27003 valid declare a constructor at function scope. */
27004 if (parser
->in_function_body
)
27006 /* And only certain tokens can begin a constructor declarator. */
27007 next_token
= cp_lexer_peek_token (parser
->lexer
);
27008 if (next_token
->type
!= CPP_NAME
27009 && next_token
->type
!= CPP_SCOPE
27010 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27011 && next_token
->type
!= CPP_TEMPLATE_ID
)
27014 /* Parse tentatively; we are going to roll back all of the tokens
27016 cp_parser_parse_tentatively (parser
);
27017 /* Assume that we are looking at a constructor declarator. */
27018 constructor_p
= true;
27020 /* Look for the optional `::' operator. */
27021 cp_parser_global_scope_opt (parser
,
27022 /*current_scope_valid_p=*/false);
27023 /* Look for the nested-name-specifier. */
27024 nested_name_specifier
27025 = (cp_parser_nested_name_specifier_opt (parser
,
27026 /*typename_keyword_p=*/false,
27027 /*check_dependency_p=*/false,
27029 /*is_declaration=*/false));
27031 outside_class_specifier_p
= (!at_class_scope_p ()
27032 || !TYPE_BEING_DEFINED (current_class_type
)
27035 /* Outside of a class-specifier, there must be a
27036 nested-name-specifier. Except in C++17 mode, where we
27037 might be declaring a guiding declaration. */
27038 if (!nested_name_specifier
&& outside_class_specifier_p
27039 && cxx_dialect
< cxx17
)
27040 constructor_p
= false;
27041 else if (nested_name_specifier
== error_mark_node
)
27042 constructor_p
= false;
27044 /* If we have a class scope, this is easy; DR 147 says that S::S always
27045 names the constructor, and no other qualified name could. */
27046 if (constructor_p
&& nested_name_specifier
27047 && CLASS_TYPE_P (nested_name_specifier
))
27049 tree id
= cp_parser_unqualified_id (parser
,
27050 /*template_keyword_p=*/false,
27051 /*check_dependency_p=*/false,
27052 /*declarator_p=*/true,
27053 /*optional_p=*/false);
27054 if (is_overloaded_fn (id
))
27055 id
= DECL_NAME (get_first_fn (id
));
27056 if (!constructor_name_p (id
, nested_name_specifier
))
27057 constructor_p
= false;
27059 /* If we still think that this might be a constructor-declarator,
27060 look for a class-name. */
27061 else if (constructor_p
)
27065 template <typename T> struct S {
27069 we must recognize that the nested `S' names a class. */
27070 if (cxx_dialect
>= cxx17
)
27071 cp_parser_parse_tentatively (parser
);
27074 type_decl
= cp_parser_class_name (parser
,
27075 /*typename_keyword_p=*/false,
27076 /*template_keyword_p=*/false,
27078 /*check_dependency_p=*/false,
27079 /*class_head_p=*/false,
27080 /*is_declaration=*/false);
27082 if (cxx_dialect
>= cxx17
27083 && !cp_parser_parse_definitely (parser
))
27085 type_decl
= NULL_TREE
;
27086 tree tmpl
= cp_parser_template_name (parser
,
27087 /*template_keyword*/false,
27088 /*check_dependency_p*/false,
27089 /*is_declaration*/false,
27091 /*is_identifier*/NULL
);
27092 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27093 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27094 /* It's a deduction guide, return true. */;
27096 cp_parser_simulate_error (parser
);
27099 /* If there was no class-name, then this is not a constructor.
27100 Otherwise, if we are in a class-specifier and we aren't
27101 handling a friend declaration, check that its type matches
27102 current_class_type (c++/38313). Note: error_mark_node
27103 is left alone for error recovery purposes. */
27104 constructor_p
= (!cp_parser_error_occurred (parser
)
27105 && (outside_class_specifier_p
27106 || type_decl
== NULL_TREE
27107 || type_decl
== error_mark_node
27108 || same_type_p (current_class_type
,
27109 TREE_TYPE (type_decl
))));
27111 /* If we're still considering a constructor, we have to see a `(',
27112 to begin the parameter-declaration-clause, followed by either a
27113 `)', an `...', or a decl-specifier. We need to check for a
27114 type-specifier to avoid being fooled into thinking that:
27118 is a constructor. (It is actually a function named `f' that
27119 takes one parameter (of type `int') and returns a value of type
27122 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27123 constructor_p
= false;
27126 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27127 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27128 /* A parameter declaration begins with a decl-specifier,
27129 which is either the "attribute" keyword, a storage class
27130 specifier, or (usually) a type-specifier. */
27131 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27134 tree pushed_scope
= NULL_TREE
;
27135 unsigned saved_num_template_parameter_lists
;
27137 /* Names appearing in the type-specifier should be looked up
27138 in the scope of the class. */
27139 if (current_class_type
)
27141 else if (type_decl
)
27143 type
= TREE_TYPE (type_decl
);
27144 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27146 type
= resolve_typename_type (type
,
27147 /*only_current_p=*/false);
27148 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27150 cp_parser_abort_tentative_parse (parser
);
27154 pushed_scope
= push_scope (type
);
27157 /* Inside the constructor parameter list, surrounding
27158 template-parameter-lists do not apply. */
27159 saved_num_template_parameter_lists
27160 = parser
->num_template_parameter_lists
;
27161 parser
->num_template_parameter_lists
= 0;
27163 /* Look for the type-specifier. */
27164 cp_parser_type_specifier (parser
,
27165 CP_PARSER_FLAGS_NONE
,
27166 /*decl_specs=*/NULL
,
27167 /*is_declarator=*/true,
27168 /*declares_class_or_enum=*/NULL
,
27169 /*is_cv_qualifier=*/NULL
);
27171 parser
->num_template_parameter_lists
27172 = saved_num_template_parameter_lists
;
27174 /* Leave the scope of the class. */
27176 pop_scope (pushed_scope
);
27178 constructor_p
= !cp_parser_error_occurred (parser
);
27182 /* We did not really want to consume any tokens. */
27183 cp_parser_abort_tentative_parse (parser
);
27185 return constructor_p
;
27188 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27189 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27190 they must be performed once we are in the scope of the function.
27192 Returns the function defined. */
27195 cp_parser_function_definition_from_specifiers_and_declarator
27196 (cp_parser
* parser
,
27197 cp_decl_specifier_seq
*decl_specifiers
,
27199 const cp_declarator
*declarator
)
27204 /* Begin the function-definition. */
27205 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27207 /* The things we're about to see are not directly qualified by any
27208 template headers we've seen thus far. */
27209 reset_specialization ();
27211 /* If there were names looked up in the decl-specifier-seq that we
27212 did not check, check them now. We must wait until we are in the
27213 scope of the function to perform the checks, since the function
27214 might be a friend. */
27215 perform_deferred_access_checks (tf_warning_or_error
);
27219 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27220 parser
->omp_declare_simd
= NULL
;
27221 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27222 parser
->oacc_routine
= NULL
;
27227 /* Skip the entire function. */
27228 cp_parser_skip_to_end_of_block_or_statement (parser
);
27229 fn
= error_mark_node
;
27231 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27233 /* Seen already, skip it. An error message has already been output. */
27234 cp_parser_skip_to_end_of_block_or_statement (parser
);
27235 fn
= current_function_decl
;
27236 current_function_decl
= NULL_TREE
;
27237 /* If this is a function from a class, pop the nested class. */
27238 if (current_class_name
)
27239 pop_nested_class ();
27244 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27245 tv
= TV_PARSE_INLINE
;
27247 tv
= TV_PARSE_FUNC
;
27249 fn
= cp_parser_function_definition_after_declarator (parser
,
27250 /*inline_p=*/false);
27257 /* Parse the part of a function-definition that follows the
27258 declarator. INLINE_P is TRUE iff this function is an inline
27259 function defined within a class-specifier.
27261 Returns the function defined. */
27264 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27268 bool saved_in_unbraced_linkage_specification_p
;
27269 bool saved_in_function_body
;
27270 unsigned saved_num_template_parameter_lists
;
27272 bool fully_implicit_function_template_p
27273 = parser
->fully_implicit_function_template_p
;
27274 parser
->fully_implicit_function_template_p
= false;
27275 tree implicit_template_parms
27276 = parser
->implicit_template_parms
;
27277 parser
->implicit_template_parms
= 0;
27278 cp_binding_level
* implicit_template_scope
27279 = parser
->implicit_template_scope
;
27280 parser
->implicit_template_scope
= 0;
27282 saved_in_function_body
= parser
->in_function_body
;
27283 parser
->in_function_body
= true;
27284 /* If the next token is `return', then the code may be trying to
27285 make use of the "named return value" extension that G++ used to
27287 token
= cp_lexer_peek_token (parser
->lexer
);
27288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27290 /* Consume the `return' keyword. */
27291 cp_lexer_consume_token (parser
->lexer
);
27292 /* Look for the identifier that indicates what value is to be
27294 cp_parser_identifier (parser
);
27295 /* Issue an error message. */
27296 error_at (token
->location
,
27297 "named return values are no longer supported");
27298 /* Skip tokens until we reach the start of the function body. */
27301 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27302 if (token
->type
== CPP_OPEN_BRACE
27303 || token
->type
== CPP_EOF
27304 || token
->type
== CPP_PRAGMA_EOL
)
27306 cp_lexer_consume_token (parser
->lexer
);
27309 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27310 anything declared inside `f'. */
27311 saved_in_unbraced_linkage_specification_p
27312 = parser
->in_unbraced_linkage_specification_p
;
27313 parser
->in_unbraced_linkage_specification_p
= false;
27314 /* Inside the function, surrounding template-parameter-lists do not
27316 saved_num_template_parameter_lists
27317 = parser
->num_template_parameter_lists
;
27318 parser
->num_template_parameter_lists
= 0;
27320 /* If the next token is `try', `__transaction_atomic', or
27321 `__transaction_relaxed`, then we are looking at either function-try-block
27322 or function-transaction-block. Note that all of these include the
27324 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27325 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27326 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27327 RID_TRANSACTION_RELAXED
))
27328 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27329 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27330 cp_parser_function_try_block (parser
);
27332 cp_parser_ctor_initializer_opt_and_function_body
27333 (parser
, /*in_function_try_block=*/false);
27335 /* Finish the function. */
27336 fn
= finish_function (inline_p
);
27337 /* Generate code for it, if necessary. */
27338 expand_or_defer_fn (fn
);
27339 /* Restore the saved values. */
27340 parser
->in_unbraced_linkage_specification_p
27341 = saved_in_unbraced_linkage_specification_p
;
27342 parser
->num_template_parameter_lists
27343 = saved_num_template_parameter_lists
;
27344 parser
->in_function_body
= saved_in_function_body
;
27346 parser
->fully_implicit_function_template_p
27347 = fully_implicit_function_template_p
;
27348 parser
->implicit_template_parms
27349 = implicit_template_parms
;
27350 parser
->implicit_template_scope
27351 = implicit_template_scope
;
27353 if (parser
->fully_implicit_function_template_p
)
27354 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27359 /* Parse a template-declaration body (following argument list). */
27362 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27363 tree parameter_list
,
27366 tree decl
= NULL_TREE
;
27367 bool friend_p
= false;
27369 /* We just processed one more parameter list. */
27370 ++parser
->num_template_parameter_lists
;
27372 /* Get the deferred access checks from the parameter list. These
27373 will be checked once we know what is being declared, as for a
27374 member template the checks must be performed in the scope of the
27375 class containing the member. */
27376 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27378 /* Tentatively parse for a new template parameter list, which can either be
27379 the template keyword or a template introduction. */
27380 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27382 else if (cxx_dialect
>= cxx11
27383 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27384 decl
= cp_parser_alias_declaration (parser
);
27387 /* There are no access checks when parsing a template, as we do not
27388 know if a specialization will be a friend. */
27389 push_deferring_access_checks (dk_no_check
);
27390 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27391 decl
= cp_parser_single_declaration (parser
,
27394 /*explicit_specialization_p=*/false,
27396 pop_deferring_access_checks ();
27398 /* If this is a member template declaration, let the front
27400 if (member_p
&& !friend_p
&& decl
)
27402 if (TREE_CODE (decl
) == TYPE_DECL
)
27403 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27405 decl
= finish_member_template_decl (decl
);
27407 else if (friend_p
&& decl
27408 && DECL_DECLARES_TYPE_P (decl
))
27409 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27410 /*complain=*/true);
27412 /* We are done with the current parameter list. */
27413 --parser
->num_template_parameter_lists
;
27415 pop_deferring_access_checks ();
27418 finish_template_decl (parameter_list
);
27420 /* Check the template arguments for a literal operator template. */
27422 && DECL_DECLARES_FUNCTION_P (decl
)
27423 && UDLIT_OPER_P (DECL_NAME (decl
)))
27426 if (parameter_list
== NULL_TREE
)
27430 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27431 if (num_parms
== 1)
27433 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27434 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27435 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27436 /* OK, C++20 string literal operator template. We don't need
27437 to warn in lower dialects here because we will have already
27438 warned about the template parameter. */;
27439 else if (TREE_TYPE (parm
) != char_type_node
27440 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27443 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27445 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27446 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27447 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27448 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27449 if (parm
== error_mark_node
27450 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27451 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27454 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27455 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27456 "ISO C++ did not adopt string literal operator templa"
27457 "tes taking an argument pack of characters");
27464 if (cxx_dialect
> cxx17
)
27465 error ("literal operator template %qD has invalid parameter list;"
27466 " Expected non-type template parameter pack <char...> "
27467 " or single non-type parameter of class type",
27470 error ("literal operator template %qD has invalid parameter list."
27471 " Expected non-type template parameter pack <char...>",
27476 /* Register member declarations. */
27477 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27478 finish_member_declaration (decl
);
27479 /* If DECL is a function template, we must return to parse it later.
27480 (Even though there is no definition, there might be default
27481 arguments that need handling.) */
27482 if (member_p
&& decl
27483 && DECL_DECLARES_FUNCTION_P (decl
))
27484 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27487 /* Parse a template introduction header for a template-declaration. Returns
27488 false if tentative parse fails. */
27491 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27493 cp_parser_parse_tentatively (parser
);
27495 tree saved_scope
= parser
->scope
;
27496 tree saved_object_scope
= parser
->object_scope
;
27497 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27499 /* Look for the optional `::' operator. */
27500 cp_parser_global_scope_opt (parser
,
27501 /*current_scope_valid_p=*/false);
27502 /* Look for the nested-name-specifier. */
27503 cp_parser_nested_name_specifier_opt (parser
,
27504 /*typename_keyword_p=*/false,
27505 /*check_dependency_p=*/true,
27507 /*is_declaration=*/false);
27509 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27510 tree concept_name
= cp_parser_identifier (parser
);
27512 /* Look up the concept for which we will be matching
27513 template parameters. */
27514 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27516 parser
->scope
= saved_scope
;
27517 parser
->object_scope
= saved_object_scope
;
27518 parser
->qualifying_scope
= saved_qualifying_scope
;
27520 if (concept_name
== error_mark_node
)
27521 cp_parser_simulate_error (parser
);
27523 /* Look for opening brace for introduction. */
27524 matching_braces braces
;
27525 braces
.require_open (parser
);
27527 if (!cp_parser_parse_definitely (parser
))
27530 push_deferring_access_checks (dk_deferred
);
27532 /* Build vector of placeholder parameters and grab
27533 matching identifiers. */
27534 tree introduction_list
= cp_parser_introduction_list (parser
);
27536 /* Look for closing brace for introduction. */
27537 if (!braces
.require_close (parser
))
27540 /* The introduction-list shall not be empty. */
27541 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27544 /* In cp_parser_introduction_list we have already issued an error. */
27548 if (tmpl_decl
== error_mark_node
)
27550 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27555 /* Build and associate the constraint. */
27556 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27557 if (parms
&& parms
!= error_mark_node
)
27559 cp_parser_template_declaration_after_parameters (parser
, parms
,
27564 error_at (token
->location
, "no matching concept for template-introduction");
27568 /* Parse a normal template-declaration following the template keyword. */
27571 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27573 tree parameter_list
;
27574 bool need_lang_pop
;
27575 location_t location
= input_location
;
27577 /* Look for the `<' token. */
27578 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27580 if (at_class_scope_p () && current_function_decl
)
27582 /* 14.5.2.2 [temp.mem]
27584 A local class shall not have member templates. */
27585 error_at (location
,
27586 "invalid declaration of member template in local class");
27587 cp_parser_skip_to_end_of_block_or_statement (parser
);
27592 A template ... shall not have C linkage. */
27593 if (current_lang_name
== lang_name_c
)
27595 error_at (location
, "template with C linkage");
27596 maybe_show_extern_c_location ();
27597 /* Give it C++ linkage to avoid confusing other parts of the
27599 push_lang_context (lang_name_cplusplus
);
27600 need_lang_pop
= true;
27603 need_lang_pop
= false;
27605 /* We cannot perform access checks on the template parameter
27606 declarations until we know what is being declared, just as we
27607 cannot check the decl-specifier list. */
27608 push_deferring_access_checks (dk_deferred
);
27610 /* If the next token is `>', then we have an invalid
27611 specialization. Rather than complain about an invalid template
27612 parameter, issue an error message here. */
27613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27615 cp_parser_error (parser
, "invalid explicit specialization");
27616 begin_specialization ();
27617 parameter_list
= NULL_TREE
;
27621 /* Parse the template parameters. */
27622 parameter_list
= cp_parser_template_parameter_list (parser
);
27625 /* Look for the `>'. */
27626 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27628 /* Manage template requirements */
27631 tree reqs
= get_shorthand_constraints (current_template_parms
);
27632 if (tree r
= cp_parser_requires_clause_opt (parser
))
27633 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27634 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27637 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27640 /* For the erroneous case of a template with C linkage, we pushed an
27641 implicit C++ linkage scope; exit that scope now. */
27643 pop_lang_context ();
27646 /* Parse a template-declaration, assuming that the `export' (and
27647 `extern') keywords, if present, has already been scanned. MEMBER_P
27648 is as for cp_parser_template_declaration. */
27651 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27653 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27655 cp_lexer_consume_token (parser
->lexer
);
27656 cp_parser_explicit_template_declaration (parser
, member_p
);
27659 else if (flag_concepts
)
27660 return cp_parser_template_introduction (parser
, member_p
);
27665 /* Perform the deferred access checks from a template-parameter-list.
27666 CHECKS is a TREE_LIST of access checks, as returned by
27667 get_deferred_access_checks. */
27670 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27672 ++processing_template_parmlist
;
27673 perform_access_checks (checks
, tf_warning_or_error
);
27674 --processing_template_parmlist
;
27677 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27678 `function-definition' sequence that follows a template header.
27679 If MEMBER_P is true, this declaration appears in a class scope.
27681 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27682 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27685 cp_parser_single_declaration (cp_parser
* parser
,
27686 vec
<deferred_access_check
, va_gc
> *checks
,
27688 bool explicit_specialization_p
,
27691 int declares_class_or_enum
;
27692 tree decl
= NULL_TREE
;
27693 cp_decl_specifier_seq decl_specifiers
;
27694 bool function_definition_p
= false;
27695 cp_token
*decl_spec_token_start
;
27697 /* This function is only used when processing a template
27699 gcc_assert (innermost_scope_kind () == sk_template_parms
27700 || innermost_scope_kind () == sk_template_spec
);
27702 /* Defer access checks until we know what is being declared. */
27703 push_deferring_access_checks (dk_deferred
);
27705 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27707 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27708 cp_parser_decl_specifier_seq (parser
,
27709 CP_PARSER_FLAGS_OPTIONAL
,
27711 &declares_class_or_enum
);
27713 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27715 /* There are no template typedefs. */
27716 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27718 error_at (decl_spec_token_start
->location
,
27719 "template declaration of %<typedef%>");
27720 decl
= error_mark_node
;
27723 /* Gather up the access checks that occurred the
27724 decl-specifier-seq. */
27725 stop_deferring_access_checks ();
27727 /* Check for the declaration of a template class. */
27728 if (declares_class_or_enum
)
27730 if (cp_parser_declares_only_class_p (parser
)
27731 || (declares_class_or_enum
& 2))
27733 // If this is a declaration, but not a definition, associate
27734 // any constraints with the type declaration. Constraints
27735 // are associated with definitions in cp_parser_class_specifier.
27736 if (declares_class_or_enum
== 1)
27737 associate_classtype_constraints (decl_specifiers
.type
);
27739 decl
= shadow_tag (&decl_specifiers
);
27744 friend template <typename T> struct A<T>::B;
27747 A<T>::B will be represented by a TYPENAME_TYPE, and
27748 therefore not recognized by shadow_tag. */
27749 if (friend_p
&& *friend_p
27751 && decl_specifiers
.type
27752 && TYPE_P (decl_specifiers
.type
))
27753 decl
= decl_specifiers
.type
;
27755 if (decl
&& decl
!= error_mark_node
)
27756 decl
= TYPE_NAME (decl
);
27758 decl
= error_mark_node
;
27760 /* Perform access checks for template parameters. */
27761 cp_parser_perform_template_parameter_access_checks (checks
);
27763 /* Give a helpful diagnostic for
27764 template <class T> struct A { } a;
27765 if we aren't already recovering from an error. */
27766 if (!cp_parser_declares_only_class_p (parser
)
27769 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27770 "a class template declaration must not declare "
27772 cp_parser_skip_to_end_of_block_or_statement (parser
);
27778 /* Complain about missing 'typename' or other invalid type names. */
27779 if (!decl_specifiers
.any_type_specifiers_p
27780 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27782 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27783 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27784 the rest of this declaration. */
27785 decl
= error_mark_node
;
27789 /* If it's not a template class, try for a template function. If
27790 the next token is a `;', then this declaration does not declare
27791 anything. But, if there were errors in the decl-specifiers, then
27792 the error might well have come from an attempted class-specifier.
27793 In that case, there's no need to warn about a missing declarator. */
27795 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27796 || decl_specifiers
.type
!= error_mark_node
))
27798 decl
= cp_parser_init_declarator (parser
,
27801 /*function_definition_allowed_p=*/true,
27803 declares_class_or_enum
,
27804 &function_definition_p
,
27807 /* 7.1.1-1 [dcl.stc]
27809 A storage-class-specifier shall not be specified in an explicit
27810 specialization... */
27812 && explicit_specialization_p
27813 && decl_specifiers
.storage_class
!= sc_none
)
27815 error_at (decl_spec_token_start
->location
,
27816 "explicit template specialization cannot have a storage class");
27817 decl
= error_mark_node
;
27820 if (decl
&& VAR_P (decl
))
27821 check_template_variable (decl
);
27824 /* Look for a trailing `;' after the declaration. */
27825 if (!function_definition_p
27826 && (decl
== error_mark_node
27827 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27828 cp_parser_skip_to_end_of_block_or_statement (parser
);
27831 pop_deferring_access_checks ();
27833 /* Clear any current qualification; whatever comes next is the start
27834 of something new. */
27835 parser
->scope
= NULL_TREE
;
27836 parser
->qualifying_scope
= NULL_TREE
;
27837 parser
->object_scope
= NULL_TREE
;
27842 /* Parse a cast-expression that is not the operand of a unary "&". */
27845 cp_parser_simple_cast_expression (cp_parser
*parser
)
27847 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27848 /*cast_p=*/false, /*decltype*/false, NULL
);
27851 /* Parse a functional cast to TYPE. Returns an expression
27852 representing the cast. */
27855 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27857 vec
<tree
, va_gc
> *vec
;
27858 tree expression_list
;
27862 location_t start_loc
= input_location
;
27865 type
= error_mark_node
;
27867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27869 cp_lexer_set_source_position (parser
->lexer
);
27870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27871 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27872 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27873 if (TREE_CODE (type
) == TYPE_DECL
)
27874 type
= TREE_TYPE (type
);
27876 cast
= finish_compound_literal (type
, expression_list
,
27877 tf_warning_or_error
, fcl_functional
);
27878 /* Create a location of the form:
27881 with caret == start at the start of the type name,
27882 finishing at the closing brace. */
27883 location_t finish_loc
27884 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27885 location_t combined_loc
= make_location (start_loc
, start_loc
,
27887 cast
.set_location (combined_loc
);
27892 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27894 /*allow_expansion_p=*/true,
27895 /*non_constant_p=*/NULL
);
27897 expression_list
= error_mark_node
;
27900 expression_list
= build_tree_list_vec (vec
);
27901 release_tree_vector (vec
);
27904 cast
= build_functional_cast (type
, expression_list
,
27905 tf_warning_or_error
);
27906 /* [expr.const]/1: In an integral constant expression "only type
27907 conversions to integral or enumeration type can be used". */
27908 if (TREE_CODE (type
) == TYPE_DECL
)
27909 type
= TREE_TYPE (type
);
27910 if (cast
!= error_mark_node
27911 && !cast_valid_in_integral_constant_expression_p (type
)
27912 && cp_parser_non_integral_constant_expression (parser
,
27914 return error_mark_node
;
27916 /* Create a location of the form:
27919 with caret == start at the start of the type name,
27920 finishing at the closing paren. */
27921 location_t finish_loc
27922 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27923 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27924 cast
.set_location (combined_loc
);
27928 /* Save the tokens that make up the body of a member function defined
27929 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27930 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27931 specifiers applied to the declaration. Returns the FUNCTION_DECL
27932 for the member function. */
27935 cp_parser_save_member_function_body (cp_parser
* parser
,
27936 cp_decl_specifier_seq
*decl_specifiers
,
27937 cp_declarator
*declarator
,
27943 bool function_try_block
= false;
27945 /* Create the FUNCTION_DECL. */
27946 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27947 cp_finalize_omp_declare_simd (parser
, fn
);
27948 cp_finalize_oacc_routine (parser
, fn
, true);
27949 /* If something went badly wrong, bail out now. */
27950 if (fn
== error_mark_node
)
27952 /* If there's a function-body, skip it. */
27953 if (cp_parser_token_starts_function_definition_p
27954 (cp_lexer_peek_token (parser
->lexer
)))
27955 cp_parser_skip_to_end_of_block_or_statement (parser
);
27956 return error_mark_node
;
27959 /* Remember it, if there default args to post process. */
27960 cp_parser_save_default_args (parser
, fn
);
27962 /* Save away the tokens that make up the body of the
27964 first
= parser
->lexer
->next_token
;
27966 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27967 cp_lexer_consume_token (parser
->lexer
);
27968 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27969 RID_TRANSACTION_ATOMIC
))
27971 cp_lexer_consume_token (parser
->lexer
);
27972 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27974 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27975 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27976 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27977 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27978 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27980 cp_lexer_consume_token (parser
->lexer
);
27981 cp_lexer_consume_token (parser
->lexer
);
27982 cp_lexer_consume_token (parser
->lexer
);
27983 cp_lexer_consume_token (parser
->lexer
);
27984 cp_lexer_consume_token (parser
->lexer
);
27987 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27988 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27990 cp_lexer_consume_token (parser
->lexer
);
27991 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27996 /* Handle function try blocks. */
27997 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27999 cp_lexer_consume_token (parser
->lexer
);
28000 function_try_block
= true;
28002 /* We can have braced-init-list mem-initializers before the fn body. */
28003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28005 cp_lexer_consume_token (parser
->lexer
);
28006 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28008 /* cache_group will stop after an un-nested { } pair, too. */
28009 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28012 /* variadic mem-inits have ... after the ')'. */
28013 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28014 cp_lexer_consume_token (parser
->lexer
);
28017 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28018 /* Handle function try blocks. */
28019 if (function_try_block
)
28020 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28021 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28022 last
= parser
->lexer
->next_token
;
28024 /* Save away the inline definition; we will process it when the
28025 class is complete. */
28026 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28027 DECL_PENDING_INLINE_P (fn
) = 1;
28029 /* We need to know that this was defined in the class, so that
28030 friend templates are handled correctly. */
28031 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28033 /* Add FN to the queue of functions to be parsed later. */
28034 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28039 /* Save the tokens that make up the in-class initializer for a non-static
28040 data member. Returns a DEFAULT_ARG. */
28043 cp_parser_save_nsdmi (cp_parser
* parser
)
28045 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28048 /* Parse a template-argument-list, as well as the trailing ">" (but
28049 not the opening "<"). See cp_parser_template_argument_list for the
28053 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28057 tree saved_qualifying_scope
;
28058 tree saved_object_scope
;
28059 bool saved_greater_than_is_operator_p
;
28063 When parsing a template-id, the first non-nested `>' is taken as
28064 the end of the template-argument-list rather than a greater-than
28066 saved_greater_than_is_operator_p
28067 = parser
->greater_than_is_operator_p
;
28068 parser
->greater_than_is_operator_p
= false;
28069 /* Parsing the argument list may modify SCOPE, so we save it
28071 saved_scope
= parser
->scope
;
28072 saved_qualifying_scope
= parser
->qualifying_scope
;
28073 saved_object_scope
= parser
->object_scope
;
28074 /* We need to evaluate the template arguments, even though this
28075 template-id may be nested within a "sizeof". */
28077 /* Parse the template-argument-list itself. */
28078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28079 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28080 arguments
= NULL_TREE
;
28082 arguments
= cp_parser_template_argument_list (parser
);
28083 /* Look for the `>' that ends the template-argument-list. If we find
28084 a '>>' instead, it's probably just a typo. */
28085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28087 if (cxx_dialect
!= cxx98
)
28089 /* In C++0x, a `>>' in a template argument list or cast
28090 expression is considered to be two separate `>'
28091 tokens. So, change the current token to a `>', but don't
28092 consume it: it will be consumed later when the outer
28093 template argument list (or cast expression) is parsed.
28094 Note that this replacement of `>' for `>>' is necessary
28095 even if we are parsing tentatively: in the tentative
28096 case, after calling
28097 cp_parser_enclosed_template_argument_list we will always
28098 throw away all of the template arguments and the first
28099 closing `>', either because the template argument list
28100 was erroneous or because we are replacing those tokens
28101 with a CPP_TEMPLATE_ID token. The second `>' (which will
28102 not have been thrown away) is needed either to close an
28103 outer template argument list or to complete a new-style
28105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28106 token
->type
= CPP_GREATER
;
28108 else if (!saved_greater_than_is_operator_p
)
28110 /* If we're in a nested template argument list, the '>>' has
28111 to be a typo for '> >'. We emit the error message, but we
28112 continue parsing and we push a '>' as next token, so that
28113 the argument list will be parsed correctly. Note that the
28114 global source location is still on the token before the
28115 '>>', so we need to say explicitly where we want it. */
28116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28117 gcc_rich_location
richloc (token
->location
);
28118 richloc
.add_fixit_replace ("> >");
28119 error_at (&richloc
, "%<>>%> should be %<> >%> "
28120 "within a nested template argument list");
28122 token
->type
= CPP_GREATER
;
28126 /* If this is not a nested template argument list, the '>>'
28127 is a typo for '>'. Emit an error message and continue.
28128 Same deal about the token location, but here we can get it
28129 right by consuming the '>>' before issuing the diagnostic. */
28130 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28131 error_at (token
->location
,
28132 "spurious %<>>%>, use %<>%> to terminate "
28133 "a template argument list");
28137 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28138 /* The `>' token might be a greater-than operator again now. */
28139 parser
->greater_than_is_operator_p
28140 = saved_greater_than_is_operator_p
;
28141 /* Restore the SAVED_SCOPE. */
28142 parser
->scope
= saved_scope
;
28143 parser
->qualifying_scope
= saved_qualifying_scope
;
28144 parser
->object_scope
= saved_object_scope
;
28149 /* MEMBER_FUNCTION is a member function, or a friend. If default
28150 arguments, or the body of the function have not yet been parsed,
28154 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28156 timevar_push (TV_PARSE_INMETH
);
28157 /* If this member is a template, get the underlying
28159 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28160 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28162 /* There should not be any class definitions in progress at this
28163 point; the bodies of members are only parsed outside of all class
28165 gcc_assert (parser
->num_classes_being_defined
== 0);
28166 /* While we're parsing the member functions we might encounter more
28167 classes. We want to handle them right away, but we don't want
28168 them getting mixed up with functions that are currently in the
28170 push_unparsed_function_queues (parser
);
28172 /* Make sure that any template parameters are in scope. */
28173 maybe_begin_member_template_processing (member_function
);
28175 /* If the body of the function has not yet been parsed, parse it
28177 if (DECL_PENDING_INLINE_P (member_function
))
28179 tree function_scope
;
28180 cp_token_cache
*tokens
;
28182 /* The function is no longer pending; we are processing it. */
28183 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28184 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28185 DECL_PENDING_INLINE_P (member_function
) = 0;
28187 /* If this is a local class, enter the scope of the containing
28189 function_scope
= current_function_decl
;
28190 if (function_scope
)
28191 push_function_context ();
28193 /* Push the body of the function onto the lexer stack. */
28194 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28196 /* Let the front end know that we going to be defining this
28198 start_preparsed_function (member_function
, NULL_TREE
,
28199 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28201 /* Don't do access checking if it is a templated function. */
28202 if (processing_template_decl
)
28203 push_deferring_access_checks (dk_no_check
);
28205 /* #pragma omp declare reduction needs special parsing. */
28206 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28208 parser
->lexer
->in_pragma
= true;
28209 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28210 finish_function (/*inline_p=*/true);
28211 cp_check_omp_declare_reduction (member_function
);
28214 /* Now, parse the body of the function. */
28215 cp_parser_function_definition_after_declarator (parser
,
28216 /*inline_p=*/true);
28218 if (processing_template_decl
)
28219 pop_deferring_access_checks ();
28221 /* Leave the scope of the containing function. */
28222 if (function_scope
)
28223 pop_function_context ();
28224 cp_parser_pop_lexer (parser
);
28227 /* Remove any template parameters from the symbol table. */
28228 maybe_end_member_template_processing ();
28230 /* Restore the queue. */
28231 pop_unparsed_function_queues (parser
);
28232 timevar_pop (TV_PARSE_INMETH
);
28235 /* If DECL contains any default args, remember it on the unparsed
28236 functions queue. */
28239 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28243 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28245 probe
= TREE_CHAIN (probe
))
28246 if (TREE_PURPOSE (probe
))
28248 cp_default_arg_entry entry
= {current_class_type
, decl
};
28249 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28254 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28255 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28256 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28257 from the parameter-type-list. */
28260 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28261 tree default_arg
, tree parmtype
)
28263 cp_token_cache
*tokens
;
28267 if (default_arg
== error_mark_node
)
28268 return error_mark_node
;
28270 /* Push the saved tokens for the default argument onto the parser's
28272 tokens
= DEFARG_TOKENS (default_arg
);
28273 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28275 start_lambda_scope (decl
);
28277 /* Parse the default argument. */
28278 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28279 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28280 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28282 finish_lambda_scope ();
28284 if (parsed_arg
== error_mark_node
)
28285 cp_parser_skip_to_end_of_statement (parser
);
28287 if (!processing_template_decl
)
28289 /* In a non-template class, check conversions now. In a template,
28290 we'll wait and instantiate these as needed. */
28291 if (TREE_CODE (decl
) == PARM_DECL
)
28292 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28293 tf_warning_or_error
);
28294 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28295 parsed_arg
= error_mark_node
;
28297 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28300 /* If the token stream has not been completely used up, then
28301 there was extra junk after the end of the default
28303 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28305 if (TREE_CODE (decl
) == PARM_DECL
)
28306 cp_parser_error (parser
, "expected %<,%>");
28308 cp_parser_error (parser
, "expected %<;%>");
28311 /* Revert to the main lexer. */
28312 cp_parser_pop_lexer (parser
);
28317 /* FIELD is a non-static data member with an initializer which we saved for
28318 later; parse it now. */
28321 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28325 maybe_begin_member_template_processing (field
);
28327 push_unparsed_function_queues (parser
);
28328 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28329 DECL_INITIAL (field
),
28331 pop_unparsed_function_queues (parser
);
28333 maybe_end_member_template_processing ();
28335 DECL_INITIAL (field
) = def
;
28338 /* FN is a FUNCTION_DECL which may contains a parameter with an
28339 unparsed DEFAULT_ARG. Parse the default args now. This function
28340 assumes that the current scope is the scope in which the default
28341 argument should be processed. */
28344 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28346 bool saved_local_variables_forbidden_p
;
28347 tree parm
, parmdecl
;
28349 /* While we're parsing the default args, we might (due to the
28350 statement expression extension) encounter more classes. We want
28351 to handle them right away, but we don't want them getting mixed
28352 up with default args that are currently in the queue. */
28353 push_unparsed_function_queues (parser
);
28355 /* Local variable names (and the `this' keyword) may not appear
28356 in a default argument. */
28357 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28358 parser
->local_variables_forbidden_p
= true;
28360 push_defarg_context (fn
);
28362 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28363 parmdecl
= DECL_ARGUMENTS (fn
);
28364 parm
&& parm
!= void_list_node
;
28365 parm
= TREE_CHAIN (parm
),
28366 parmdecl
= DECL_CHAIN (parmdecl
))
28368 tree default_arg
= TREE_PURPOSE (parm
);
28370 vec
<tree
, va_gc
> *insts
;
28377 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28378 /* This can happen for a friend declaration for a function
28379 already declared with default arguments. */
28383 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28385 TREE_VALUE (parm
));
28386 TREE_PURPOSE (parm
) = parsed_arg
;
28388 /* Update any instantiations we've already created. */
28389 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28390 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28391 TREE_PURPOSE (copy
) = parsed_arg
;
28394 pop_defarg_context ();
28396 /* Make sure no default arg is missing. */
28397 check_default_args (fn
);
28399 /* Restore the state of local_variables_forbidden_p. */
28400 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28402 /* Restore the queue. */
28403 pop_unparsed_function_queues (parser
);
28406 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28408 sizeof ... ( identifier )
28410 where the 'sizeof' token has already been consumed. */
28413 cp_parser_sizeof_pack (cp_parser
*parser
)
28415 /* Consume the `...'. */
28416 cp_lexer_consume_token (parser
->lexer
);
28417 maybe_warn_variadic_templates ();
28419 matching_parens parens
;
28420 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28422 parens
.consume_open (parser
);
28424 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28425 "%<sizeof...%> argument must be surrounded by parentheses");
28427 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28428 tree name
= cp_parser_identifier (parser
);
28429 if (name
== error_mark_node
)
28430 return error_mark_node
;
28431 /* The name is not qualified. */
28432 parser
->scope
= NULL_TREE
;
28433 parser
->qualifying_scope
= NULL_TREE
;
28434 parser
->object_scope
= NULL_TREE
;
28435 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28436 if (expr
== error_mark_node
)
28437 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28439 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28440 expr
= TREE_TYPE (expr
);
28441 else if (TREE_CODE (expr
) == CONST_DECL
)
28442 expr
= DECL_INITIAL (expr
);
28443 expr
= make_pack_expansion (expr
);
28444 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28447 parens
.require_close (parser
);
28452 /* Parse the operand of `sizeof' (or a similar operator). Returns
28453 either a TYPE or an expression, depending on the form of the
28454 input. The KEYWORD indicates which kind of expression we have
28458 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28460 tree expr
= NULL_TREE
;
28461 const char *saved_message
;
28463 bool saved_integral_constant_expression_p
;
28464 bool saved_non_integral_constant_expression_p
;
28466 /* If it's a `...', then we are computing the length of a parameter
28468 if (keyword
== RID_SIZEOF
28469 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28470 return cp_parser_sizeof_pack (parser
);
28472 /* Types cannot be defined in a `sizeof' expression. Save away the
28474 saved_message
= parser
->type_definition_forbidden_message
;
28475 /* And create the new one. */
28476 tmp
= concat ("types may not be defined in %<",
28477 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28478 "%> expressions", NULL
);
28479 parser
->type_definition_forbidden_message
= tmp
;
28481 /* The restrictions on constant-expressions do not apply inside
28482 sizeof expressions. */
28483 saved_integral_constant_expression_p
28484 = parser
->integral_constant_expression_p
;
28485 saved_non_integral_constant_expression_p
28486 = parser
->non_integral_constant_expression_p
;
28487 parser
->integral_constant_expression_p
= false;
28489 /* Do not actually evaluate the expression. */
28490 ++cp_unevaluated_operand
;
28491 ++c_inhibit_evaluation_warnings
;
28492 /* If it's a `(', then we might be looking at the type-id
28494 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28496 tree type
= NULL_TREE
;
28498 /* We can't be sure yet whether we're looking at a type-id or an
28500 cp_parser_parse_tentatively (parser
);
28502 matching_parens parens
;
28503 parens
.consume_open (parser
);
28505 /* Note: as a GNU Extension, compound literals are considered
28506 postfix-expressions as they are in C99, so they are valid
28507 arguments to sizeof. See comment in cp_parser_cast_expression
28509 if (cp_parser_compound_literal_p (parser
))
28510 cp_parser_simulate_error (parser
);
28513 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28514 parser
->in_type_id_in_expr_p
= true;
28515 /* Look for the type-id. */
28516 type
= cp_parser_type_id (parser
);
28517 /* Look for the closing `)'. */
28518 parens
.require_close (parser
);
28519 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28522 /* If all went well, then we're done. */
28523 if (cp_parser_parse_definitely (parser
))
28527 /* If the type-id production did not work out, then we must be
28528 looking at the unary-expression production. */
28530 expr
= cp_parser_unary_expression (parser
);
28532 /* Go back to evaluating expressions. */
28533 --cp_unevaluated_operand
;
28534 --c_inhibit_evaluation_warnings
;
28536 /* Free the message we created. */
28538 /* And restore the old one. */
28539 parser
->type_definition_forbidden_message
= saved_message
;
28540 parser
->integral_constant_expression_p
28541 = saved_integral_constant_expression_p
;
28542 parser
->non_integral_constant_expression_p
28543 = saved_non_integral_constant_expression_p
;
28548 /* If the current declaration has no declarator, return true. */
28551 cp_parser_declares_only_class_p (cp_parser
*parser
)
28553 /* If the next token is a `;' or a `,' then there is no
28555 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28556 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28559 /* Update the DECL_SPECS to reflect the storage class indicated by
28563 cp_parser_set_storage_class (cp_parser
*parser
,
28564 cp_decl_specifier_seq
*decl_specs
,
28568 cp_storage_class storage_class
;
28570 if (parser
->in_unbraced_linkage_specification_p
)
28572 error_at (token
->location
, "invalid use of %qD in linkage specification",
28573 ridpointers
[keyword
]);
28576 else if (decl_specs
->storage_class
!= sc_none
)
28578 decl_specs
->conflicting_specifiers_p
= true;
28582 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28583 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28584 && decl_specs
->gnu_thread_keyword_p
)
28586 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28587 "%<__thread%> before %qD", ridpointers
[keyword
]);
28593 storage_class
= sc_auto
;
28596 storage_class
= sc_register
;
28599 storage_class
= sc_static
;
28602 storage_class
= sc_extern
;
28605 storage_class
= sc_mutable
;
28608 gcc_unreachable ();
28610 decl_specs
->storage_class
= storage_class
;
28611 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28613 /* A storage class specifier cannot be applied alongside a typedef
28614 specifier. If there is a typedef specifier present then set
28615 conflicting_specifiers_p which will trigger an error later
28616 on in grokdeclarator. */
28617 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28618 decl_specs
->conflicting_specifiers_p
= true;
28621 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28622 is true, the type is a class or enum definition. */
28625 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28628 bool type_definition_p
)
28630 decl_specs
->any_specifiers_p
= true;
28632 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28633 (with, for example, in "typedef int wchar_t;") we remember that
28634 this is what happened. In system headers, we ignore these
28635 declarations so that G++ can work with system headers that are not
28637 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28638 && !type_definition_p
28639 && (type_spec
== boolean_type_node
28640 || type_spec
== char16_type_node
28641 || type_spec
== char32_type_node
28642 || type_spec
== wchar_type_node
)
28643 && (decl_specs
->type
28644 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28645 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28646 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28647 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28649 decl_specs
->redefined_builtin_type
= type_spec
;
28650 set_and_check_decl_spec_loc (decl_specs
,
28651 ds_redefined_builtin_type_spec
,
28653 if (!decl_specs
->type
)
28655 decl_specs
->type
= type_spec
;
28656 decl_specs
->type_definition_p
= false;
28657 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28660 else if (decl_specs
->type
)
28661 decl_specs
->multiple_types_p
= true;
28664 decl_specs
->type
= type_spec
;
28665 decl_specs
->type_definition_p
= type_definition_p
;
28666 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28667 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28671 /* True iff TOKEN is the GNU keyword __thread. */
28674 token_is__thread (cp_token
*token
)
28676 gcc_assert (token
->keyword
== RID_THREAD
);
28677 return id_equal (token
->u
.value
, "__thread");
28680 /* Set the location for a declarator specifier and check if it is
28683 DECL_SPECS is the sequence of declarator specifiers onto which to
28686 DS is the single declarator specifier to set which location is to
28687 be set onto the existing sequence of declarators.
28689 LOCATION is the location for the declarator specifier to
28693 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28694 cp_decl_spec ds
, cp_token
*token
)
28696 gcc_assert (ds
< ds_last
);
28698 if (decl_specs
== NULL
)
28701 location_t location
= token
->location
;
28703 if (decl_specs
->locations
[ds
] == 0)
28705 decl_specs
->locations
[ds
] = location
;
28706 if (ds
== ds_thread
)
28707 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28713 if (decl_specs
->locations
[ds_long_long
] != 0)
28714 error_at (location
,
28715 "%<long long long%> is too long for GCC");
28718 decl_specs
->locations
[ds_long_long
] = location
;
28719 pedwarn_cxx98 (location
,
28721 "ISO C++ 1998 does not support %<long long%>");
28724 else if (ds
== ds_thread
)
28726 bool gnu
= token_is__thread (token
);
28727 gcc_rich_location
richloc (location
);
28728 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28730 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28731 error_at (&richloc
,
28732 "both %<__thread%> and %<thread_local%> specified");
28736 richloc
.add_fixit_remove ();
28737 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28742 static const char *const decl_spec_names
[] = {
28759 gcc_rich_location
richloc (location
);
28760 richloc
.add_fixit_remove ();
28761 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28766 /* Return true iff the declarator specifier DS is present in the
28767 sequence of declarator specifiers DECL_SPECS. */
28770 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28773 gcc_assert (ds
< ds_last
);
28775 if (decl_specs
== NULL
)
28778 return decl_specs
->locations
[ds
] != 0;
28781 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28782 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28785 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28787 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28790 /* Issue an error message indicating that TOKEN_DESC was expected.
28791 If KEYWORD is true, it indicated this function is called by
28792 cp_parser_require_keword and the required token can only be
28793 a indicated keyword.
28795 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28796 within any error as the location of an "opening" token matching
28797 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28798 RT_CLOSE_PAREN). */
28801 cp_parser_required_error (cp_parser
*parser
,
28802 required_token token_desc
,
28804 location_t matching_location
)
28806 if (cp_parser_simulate_error (parser
))
28809 const char *gmsgid
= NULL
;
28810 switch (token_desc
)
28813 gmsgid
= G_("expected %<new%>");
28816 gmsgid
= G_("expected %<delete%>");
28819 gmsgid
= G_("expected %<return%>");
28822 gmsgid
= G_("expected %<while%>");
28825 gmsgid
= G_("expected %<extern%>");
28827 case RT_STATIC_ASSERT
:
28828 gmsgid
= G_("expected %<static_assert%>");
28831 gmsgid
= G_("expected %<decltype%>");
28834 gmsgid
= G_("expected %<operator%>");
28837 gmsgid
= G_("expected %<class%>");
28840 gmsgid
= G_("expected %<template%>");
28843 gmsgid
= G_("expected %<namespace%>");
28846 gmsgid
= G_("expected %<using%>");
28849 gmsgid
= G_("expected %<asm%>");
28852 gmsgid
= G_("expected %<try%>");
28855 gmsgid
= G_("expected %<catch%>");
28858 gmsgid
= G_("expected %<throw%>");
28861 gmsgid
= G_("expected %<__label__%>");
28864 gmsgid
= G_("expected %<@try%>");
28866 case RT_AT_SYNCHRONIZED
:
28867 gmsgid
= G_("expected %<@synchronized%>");
28870 gmsgid
= G_("expected %<@throw%>");
28872 case RT_TRANSACTION_ATOMIC
:
28873 gmsgid
= G_("expected %<__transaction_atomic%>");
28875 case RT_TRANSACTION_RELAXED
:
28876 gmsgid
= G_("expected %<__transaction_relaxed%>");
28882 if (!gmsgid
&& !keyword
)
28884 switch (token_desc
)
28887 gmsgid
= G_("expected %<;%>");
28889 case RT_OPEN_PAREN
:
28890 gmsgid
= G_("expected %<(%>");
28892 case RT_CLOSE_BRACE
:
28893 gmsgid
= G_("expected %<}%>");
28895 case RT_OPEN_BRACE
:
28896 gmsgid
= G_("expected %<{%>");
28898 case RT_CLOSE_SQUARE
:
28899 gmsgid
= G_("expected %<]%>");
28901 case RT_OPEN_SQUARE
:
28902 gmsgid
= G_("expected %<[%>");
28905 gmsgid
= G_("expected %<,%>");
28908 gmsgid
= G_("expected %<::%>");
28911 gmsgid
= G_("expected %<<%>");
28914 gmsgid
= G_("expected %<>%>");
28917 gmsgid
= G_("expected %<=%>");
28920 gmsgid
= G_("expected %<...%>");
28923 gmsgid
= G_("expected %<*%>");
28926 gmsgid
= G_("expected %<~%>");
28929 gmsgid
= G_("expected %<:%>");
28931 case RT_COLON_SCOPE
:
28932 gmsgid
= G_("expected %<:%> or %<::%>");
28934 case RT_CLOSE_PAREN
:
28935 gmsgid
= G_("expected %<)%>");
28937 case RT_COMMA_CLOSE_PAREN
:
28938 gmsgid
= G_("expected %<,%> or %<)%>");
28940 case RT_PRAGMA_EOL
:
28941 gmsgid
= G_("expected end of line");
28944 gmsgid
= G_("expected identifier");
28947 gmsgid
= G_("expected selection-statement");
28950 gmsgid
= G_("expected iteration-statement");
28953 gmsgid
= G_("expected jump-statement");
28956 gmsgid
= G_("expected class-key");
28958 case RT_CLASS_TYPENAME_TEMPLATE
:
28959 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28962 gcc_unreachable ();
28967 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28971 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28972 issue an error message indicating that TOKEN_DESC was expected.
28974 Returns the token consumed, if the token had the appropriate type.
28975 Otherwise, returns NULL.
28977 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28978 within any error as the location of an "opening" token matching
28979 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28980 RT_CLOSE_PAREN). */
28983 cp_parser_require (cp_parser
* parser
,
28984 enum cpp_ttype type
,
28985 required_token token_desc
,
28986 location_t matching_location
)
28988 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28989 return cp_lexer_consume_token (parser
->lexer
);
28992 /* Output the MESSAGE -- unless we're parsing tentatively. */
28993 if (!cp_parser_simulate_error (parser
))
28994 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28995 matching_location
);
29000 /* An error message is produced if the next token is not '>'.
29001 All further tokens are skipped until the desired token is
29002 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29005 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29007 /* Current level of '< ... >'. */
29008 unsigned level
= 0;
29009 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29010 unsigned nesting_depth
= 0;
29012 /* Are we ready, yet? If not, issue error message. */
29013 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29016 /* Skip tokens until the desired token is found. */
29019 /* Peek at the next token. */
29020 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29023 if (!nesting_depth
)
29028 if (cxx_dialect
== cxx98
)
29029 /* C++0x views the `>>' operator as two `>' tokens, but
29032 else if (!nesting_depth
&& level
-- == 0)
29034 /* We've hit a `>>' where the first `>' closes the
29035 template argument list, and the second `>' is
29036 spurious. Just consume the `>>' and stop; we've
29037 already produced at least one error. */
29038 cp_lexer_consume_token (parser
->lexer
);
29041 /* Fall through for C++0x, so we handle the second `>' in
29043 gcc_fallthrough ();
29046 if (!nesting_depth
&& level
-- == 0)
29048 /* We've reached the token we want, consume it and stop. */
29049 cp_lexer_consume_token (parser
->lexer
);
29054 case CPP_OPEN_PAREN
:
29055 case CPP_OPEN_SQUARE
:
29059 case CPP_CLOSE_PAREN
:
29060 case CPP_CLOSE_SQUARE
:
29061 if (nesting_depth
-- == 0)
29066 case CPP_PRAGMA_EOL
:
29067 case CPP_SEMICOLON
:
29068 case CPP_OPEN_BRACE
:
29069 case CPP_CLOSE_BRACE
:
29070 /* The '>' was probably forgotten, don't look further. */
29077 /* Consume this token. */
29078 cp_lexer_consume_token (parser
->lexer
);
29082 /* If the next token is the indicated keyword, consume it. Otherwise,
29083 issue an error message indicating that TOKEN_DESC was expected.
29085 Returns the token consumed, if the token had the appropriate type.
29086 Otherwise, returns NULL. */
29089 cp_parser_require_keyword (cp_parser
* parser
,
29091 required_token token_desc
)
29093 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29095 if (token
&& token
->keyword
!= keyword
)
29097 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29105 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29106 function-definition. */
29109 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29111 return (/* An ordinary function-body begins with an `{'. */
29112 token
->type
== CPP_OPEN_BRACE
29113 /* A ctor-initializer begins with a `:'. */
29114 || token
->type
== CPP_COLON
29115 /* A function-try-block begins with `try'. */
29116 || token
->keyword
== RID_TRY
29117 /* A function-transaction-block begins with `__transaction_atomic'
29118 or `__transaction_relaxed'. */
29119 || token
->keyword
== RID_TRANSACTION_ATOMIC
29120 || token
->keyword
== RID_TRANSACTION_RELAXED
29121 /* The named return value extension begins with `return'. */
29122 || token
->keyword
== RID_RETURN
);
29125 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29129 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29133 token
= cp_lexer_peek_token (parser
->lexer
);
29134 return (token
->type
== CPP_OPEN_BRACE
29135 || (token
->type
== CPP_COLON
29136 && !parser
->colon_doesnt_start_class_def_p
));
29139 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29140 C++0x) ending a template-argument. */
29143 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29147 token
= cp_lexer_peek_token (parser
->lexer
);
29148 return (token
->type
== CPP_COMMA
29149 || token
->type
== CPP_GREATER
29150 || token
->type
== CPP_ELLIPSIS
29151 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29154 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29155 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29158 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29163 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29164 if (token
->type
== CPP_LESS
)
29166 /* Check for the sequence `<::' in the original code. It would be lexed as
29167 `[:', where `[' is a digraph, and there is no whitespace before
29169 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29172 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29173 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29179 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29180 or none_type otherwise. */
29182 static enum tag_types
29183 cp_parser_token_is_class_key (cp_token
* token
)
29185 switch (token
->keyword
)
29190 return record_type
;
29199 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29200 or none_type otherwise or if the token is null. */
29202 static enum tag_types
29203 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29208 switch (token
->keyword
)
29213 return typename_type
;
29220 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29223 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29225 if (type
== error_mark_node
)
29227 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29229 if (permerror (input_location
, "%qs tag used in naming %q#T",
29230 class_key
== union_type
? "union"
29231 : class_key
== record_type
? "struct" : "class",
29233 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29234 "%q#T was previously declared here", type
);
29238 /* Issue an error message if DECL is redeclared with different
29239 access than its original declaration [class.access.spec/3].
29240 This applies to nested classes, nested class templates and
29241 enumerations [class.mem/1]. */
29244 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29247 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29248 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29251 if ((TREE_PRIVATE (decl
)
29252 != (current_access_specifier
== access_private_node
))
29253 || (TREE_PROTECTED (decl
)
29254 != (current_access_specifier
== access_protected_node
)))
29255 error_at (location
, "%qD redeclared with different access", decl
);
29258 /* Look for the `template' keyword, as a syntactic disambiguator.
29259 Return TRUE iff it is present, in which case it will be
29263 cp_parser_optional_template_keyword (cp_parser
*parser
)
29265 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29267 /* In C++98 the `template' keyword can only be used within templates;
29268 outside templates the parser can always figure out what is a
29269 template and what is not. In C++11, per the resolution of DR 468,
29270 `template' is allowed in cases where it is not strictly necessary. */
29271 if (!processing_template_decl
29272 && pedantic
&& cxx_dialect
== cxx98
)
29274 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29275 pedwarn (token
->location
, OPT_Wpedantic
,
29276 "in C++98 %<template%> (as a disambiguator) is only "
29277 "allowed within templates");
29278 /* If this part of the token stream is rescanned, the same
29279 error message would be generated. So, we purge the token
29280 from the stream. */
29281 cp_lexer_purge_token (parser
->lexer
);
29286 /* Consume the `template' keyword. */
29287 cp_lexer_consume_token (parser
->lexer
);
29294 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29295 set PARSER->SCOPE, and perform other related actions. */
29298 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29300 struct tree_check
*check_value
;
29302 /* Get the stored value. */
29303 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29304 /* Set the scope from the stored value. */
29305 parser
->scope
= saved_checks_value (check_value
);
29306 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29307 parser
->object_scope
= NULL_TREE
;
29310 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29311 encounter the end of a block before what we were looking for. */
29314 cp_parser_cache_group (cp_parser
*parser
,
29315 enum cpp_ttype end
,
29320 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29322 /* Abort a parenthesized expression if we encounter a semicolon. */
29323 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29324 && token
->type
== CPP_SEMICOLON
)
29326 /* If we've reached the end of the file, stop. */
29327 if (token
->type
== CPP_EOF
29328 || (end
!= CPP_PRAGMA_EOL
29329 && token
->type
== CPP_PRAGMA_EOL
))
29331 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29332 /* We've hit the end of an enclosing block, so there's been some
29333 kind of syntax error. */
29336 /* Consume the token. */
29337 cp_lexer_consume_token (parser
->lexer
);
29338 /* See if it starts a new group. */
29339 if (token
->type
== CPP_OPEN_BRACE
)
29341 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29342 /* In theory this should probably check end == '}', but
29343 cp_parser_save_member_function_body needs it to exit
29344 after either '}' or ')' when called with ')'. */
29348 else if (token
->type
== CPP_OPEN_PAREN
)
29350 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29351 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29354 else if (token
->type
== CPP_PRAGMA
)
29355 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29356 else if (token
->type
== end
)
29361 /* Like above, for caching a default argument or NSDMI. Both of these are
29362 terminated by a non-nested comma, but it can be unclear whether or not a
29363 comma is nested in a template argument list unless we do more parsing.
29364 In order to handle this ambiguity, when we encounter a ',' after a '<'
29365 we try to parse what follows as a parameter-declaration-list (in the
29366 case of a default argument) or a member-declarator (in the case of an
29367 NSDMI). If that succeeds, then we stop caching. */
29370 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29372 unsigned depth
= 0;
29373 int maybe_template_id
= 0;
29374 cp_token
*first_token
;
29376 tree default_argument
;
29378 /* Add tokens until we have processed the entire default
29379 argument. We add the range [first_token, token). */
29380 first_token
= cp_lexer_peek_token (parser
->lexer
);
29381 if (first_token
->type
== CPP_OPEN_BRACE
)
29383 /* For list-initialization, this is straightforward. */
29384 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29385 token
= cp_lexer_peek_token (parser
->lexer
);
29391 /* Peek at the next token. */
29392 token
= cp_lexer_peek_token (parser
->lexer
);
29393 /* What we do depends on what token we have. */
29394 switch (token
->type
)
29396 /* In valid code, a default argument must be
29397 immediately followed by a `,' `)', or `...'. */
29399 if (depth
== 0 && maybe_template_id
)
29401 /* If we've seen a '<', we might be in a
29402 template-argument-list. Until Core issue 325 is
29403 resolved, we don't know how this situation ought
29404 to be handled, so try to DTRT. We check whether
29405 what comes after the comma is a valid parameter
29406 declaration list. If it is, then the comma ends
29407 the default argument; otherwise the default
29408 argument continues. */
29409 bool error
= false;
29412 /* Set ITALP so cp_parser_parameter_declaration_list
29413 doesn't decide to commit to this parse. */
29414 bool saved_italp
= parser
->in_template_argument_list_p
;
29415 parser
->in_template_argument_list_p
= true;
29417 cp_parser_parse_tentatively (parser
);
29421 /* Parse declarators until we reach a non-comma or
29422 somthing that cannot be an initializer.
29423 Just checking whether we're looking at a single
29424 declarator is insufficient. Consider:
29425 int var = tuple<T,U>::x;
29426 The template parameter 'U' looks exactly like a
29430 int ctor_dtor_or_conv_p
;
29431 cp_lexer_consume_token (parser
->lexer
);
29432 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29433 &ctor_dtor_or_conv_p
,
29434 /*parenthesized_p=*/NULL
,
29436 /*friend_p=*/false);
29437 peek
= cp_lexer_peek_token (parser
->lexer
);
29438 if (cp_parser_error_occurred (parser
))
29441 while (peek
->type
== CPP_COMMA
);
29442 /* If we met an '=' or ';' then the original comma
29443 was the end of the NSDMI. Otherwise assume
29444 we're still in the NSDMI. */
29445 error
= (peek
->type
!= CPP_EQ
29446 && peek
->type
!= CPP_SEMICOLON
);
29450 cp_lexer_consume_token (parser
->lexer
);
29451 begin_scope (sk_function_parms
, NULL_TREE
);
29452 if (cp_parser_parameter_declaration_list (parser
)
29453 == error_mark_node
)
29455 pop_bindings_and_leave_scope ();
29457 if (!cp_parser_error_occurred (parser
) && !error
)
29459 cp_parser_abort_tentative_parse (parser
);
29461 parser
->in_template_argument_list_p
= saved_italp
;
29465 case CPP_CLOSE_PAREN
:
29467 /* If we run into a non-nested `;', `}', or `]',
29468 then the code is invalid -- but the default
29469 argument is certainly over. */
29470 case CPP_SEMICOLON
:
29471 case CPP_CLOSE_BRACE
:
29472 case CPP_CLOSE_SQUARE
:
29474 /* Handle correctly int n = sizeof ... ( p ); */
29475 && token
->type
!= CPP_ELLIPSIS
)
29477 /* Update DEPTH, if necessary. */
29478 else if (token
->type
== CPP_CLOSE_PAREN
29479 || token
->type
== CPP_CLOSE_BRACE
29480 || token
->type
== CPP_CLOSE_SQUARE
)
29484 case CPP_OPEN_PAREN
:
29485 case CPP_OPEN_SQUARE
:
29486 case CPP_OPEN_BRACE
:
29492 /* This might be the comparison operator, or it might
29493 start a template argument list. */
29494 ++maybe_template_id
;
29498 if (cxx_dialect
== cxx98
)
29500 /* Fall through for C++0x, which treats the `>>'
29501 operator like two `>' tokens in certain
29503 gcc_fallthrough ();
29508 /* This might be an operator, or it might close a
29509 template argument list. But if a previous '<'
29510 started a template argument list, this will have
29511 closed it, so we can't be in one anymore. */
29512 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29513 if (maybe_template_id
< 0)
29514 maybe_template_id
= 0;
29518 /* If we run out of tokens, issue an error message. */
29520 case CPP_PRAGMA_EOL
:
29521 error_at (token
->location
, "file ends in default argument");
29522 return error_mark_node
;
29526 /* In these cases, we should look for template-ids.
29527 For example, if the default argument is
29528 `X<int, double>()', we need to do name lookup to
29529 figure out whether or not `X' is a template; if
29530 so, the `,' does not end the default argument.
29532 That is not yet done. */
29539 /* If we've reached the end, stop. */
29543 /* Add the token to the token block. */
29544 token
= cp_lexer_consume_token (parser
->lexer
);
29547 /* Create a DEFAULT_ARG to represent the unparsed default
29549 default_argument
= make_node (DEFAULT_ARG
);
29550 DEFARG_TOKENS (default_argument
)
29551 = cp_token_cache_new (first_token
, token
);
29552 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29554 return default_argument
;
29557 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29560 defarg_location (tree default_argument
)
29562 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29563 location_t start
= tokens
->first
->location
;
29564 location_t end
= tokens
->last
->location
;
29565 return make_location (start
, start
, end
);
29568 /* Begin parsing tentatively. We always save tokens while parsing
29569 tentatively so that if the tentative parsing fails we can restore the
29573 cp_parser_parse_tentatively (cp_parser
* parser
)
29575 /* Enter a new parsing context. */
29576 parser
->context
= cp_parser_context_new (parser
->context
);
29577 /* Begin saving tokens. */
29578 cp_lexer_save_tokens (parser
->lexer
);
29579 /* In order to avoid repetitive access control error messages,
29580 access checks are queued up until we are no longer parsing
29582 push_deferring_access_checks (dk_deferred
);
29585 /* Commit to the currently active tentative parse. */
29588 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29590 cp_parser_context
*context
;
29593 /* Mark all of the levels as committed. */
29594 lexer
= parser
->lexer
;
29595 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29597 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29599 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29600 while (!cp_lexer_saving_tokens (lexer
))
29601 lexer
= lexer
->next
;
29602 cp_lexer_commit_tokens (lexer
);
29606 /* Commit to the topmost currently active tentative parse.
29608 Note that this function shouldn't be called when there are
29609 irreversible side-effects while in a tentative state. For
29610 example, we shouldn't create a permanent entry in the symbol
29611 table, or issue an error message that might not apply if the
29612 tentative parse is aborted. */
29615 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29617 cp_parser_context
*context
= parser
->context
;
29618 cp_lexer
*lexer
= parser
->lexer
;
29622 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29624 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29626 while (!cp_lexer_saving_tokens (lexer
))
29627 lexer
= lexer
->next
;
29628 cp_lexer_commit_tokens (lexer
);
29632 /* Abort the currently active tentative parse. All consumed tokens
29633 will be rolled back, and no diagnostics will be issued. */
29636 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29638 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29639 || errorcount
> 0);
29640 cp_parser_simulate_error (parser
);
29641 /* Now, pretend that we want to see if the construct was
29642 successfully parsed. */
29643 cp_parser_parse_definitely (parser
);
29646 /* Stop parsing tentatively. If a parse error has occurred, restore the
29647 token stream. Otherwise, commit to the tokens we have consumed.
29648 Returns true if no error occurred; false otherwise. */
29651 cp_parser_parse_definitely (cp_parser
* parser
)
29653 bool error_occurred
;
29654 cp_parser_context
*context
;
29656 /* Remember whether or not an error occurred, since we are about to
29657 destroy that information. */
29658 error_occurred
= cp_parser_error_occurred (parser
);
29659 /* Remove the topmost context from the stack. */
29660 context
= parser
->context
;
29661 parser
->context
= context
->next
;
29662 /* If no parse errors occurred, commit to the tentative parse. */
29663 if (!error_occurred
)
29665 /* Commit to the tokens read tentatively, unless that was
29667 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29668 cp_lexer_commit_tokens (parser
->lexer
);
29670 pop_to_parent_deferring_access_checks ();
29672 /* Otherwise, if errors occurred, roll back our state so that things
29673 are just as they were before we began the tentative parse. */
29676 cp_lexer_rollback_tokens (parser
->lexer
);
29677 pop_deferring_access_checks ();
29679 /* Add the context to the front of the free list. */
29680 context
->next
= cp_parser_context_free_list
;
29681 cp_parser_context_free_list
= context
;
29683 return !error_occurred
;
29686 /* Returns true if we are parsing tentatively and are not committed to
29687 this tentative parse. */
29690 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29692 return (cp_parser_parsing_tentatively (parser
)
29693 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29696 /* Returns nonzero iff an error has occurred during the most recent
29697 tentative parse. */
29700 cp_parser_error_occurred (cp_parser
* parser
)
29702 return (cp_parser_parsing_tentatively (parser
)
29703 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29706 /* Returns nonzero if GNU extensions are allowed. */
29709 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29711 return parser
->allow_gnu_extensions_p
;
29714 /* Objective-C++ Productions */
29717 /* Parse an Objective-C expression, which feeds into a primary-expression
29721 objc-message-expression
29722 objc-string-literal
29723 objc-encode-expression
29724 objc-protocol-expression
29725 objc-selector-expression
29727 Returns a tree representation of the expression. */
29730 cp_parser_objc_expression (cp_parser
* parser
)
29732 /* Try to figure out what kind of declaration is present. */
29733 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29737 case CPP_OPEN_SQUARE
:
29738 return cp_parser_objc_message_expression (parser
);
29740 case CPP_OBJC_STRING
:
29741 kwd
= cp_lexer_consume_token (parser
->lexer
);
29742 return objc_build_string_object (kwd
->u
.value
);
29745 switch (kwd
->keyword
)
29747 case RID_AT_ENCODE
:
29748 return cp_parser_objc_encode_expression (parser
);
29750 case RID_AT_PROTOCOL
:
29751 return cp_parser_objc_protocol_expression (parser
);
29753 case RID_AT_SELECTOR
:
29754 return cp_parser_objc_selector_expression (parser
);
29761 error_at (kwd
->location
,
29762 "misplaced %<@%D%> Objective-C++ construct",
29764 cp_parser_skip_to_end_of_block_or_statement (parser
);
29767 return error_mark_node
;
29770 /* Parse an Objective-C message expression.
29772 objc-message-expression:
29773 [ objc-message-receiver objc-message-args ]
29775 Returns a representation of an Objective-C message. */
29778 cp_parser_objc_message_expression (cp_parser
* parser
)
29780 tree receiver
, messageargs
;
29782 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29783 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29784 receiver
= cp_parser_objc_message_receiver (parser
);
29785 messageargs
= cp_parser_objc_message_args (parser
);
29786 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29787 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29789 tree result
= objc_build_message_expr (receiver
, messageargs
);
29791 /* Construct a location e.g.
29794 ranging from the '[' to the ']', with the caret at the start. */
29795 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29796 protected_set_expr_location (result
, combined_loc
);
29801 /* Parse an objc-message-receiver.
29803 objc-message-receiver:
29805 simple-type-specifier
29807 Returns a representation of the type or expression. */
29810 cp_parser_objc_message_receiver (cp_parser
* parser
)
29814 /* An Objective-C message receiver may be either (1) a type
29815 or (2) an expression. */
29816 cp_parser_parse_tentatively (parser
);
29817 rcv
= cp_parser_expression (parser
);
29819 /* If that worked out, fine. */
29820 if (cp_parser_parse_definitely (parser
))
29823 cp_parser_parse_tentatively (parser
);
29824 rcv
= cp_parser_simple_type_specifier (parser
,
29825 /*decl_specs=*/NULL
,
29826 CP_PARSER_FLAGS_NONE
);
29828 if (cp_parser_parse_definitely (parser
))
29829 return objc_get_class_reference (rcv
);
29831 cp_parser_error (parser
, "objective-c++ message receiver expected");
29832 return error_mark_node
;
29835 /* Parse the arguments and selectors comprising an Objective-C message.
29840 objc-selector-args , objc-comma-args
29842 objc-selector-args:
29843 objc-selector [opt] : assignment-expression
29844 objc-selector-args objc-selector [opt] : assignment-expression
29847 assignment-expression
29848 objc-comma-args , assignment-expression
29850 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29851 selector arguments and TREE_VALUE containing a list of comma
29855 cp_parser_objc_message_args (cp_parser
* parser
)
29857 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29858 bool maybe_unary_selector_p
= true;
29859 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29861 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29863 tree selector
= NULL_TREE
, arg
;
29865 if (token
->type
!= CPP_COLON
)
29866 selector
= cp_parser_objc_selector (parser
);
29868 /* Detect if we have a unary selector. */
29869 if (maybe_unary_selector_p
29870 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29871 return build_tree_list (selector
, NULL_TREE
);
29873 maybe_unary_selector_p
= false;
29874 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29875 arg
= cp_parser_assignment_expression (parser
);
29878 = chainon (sel_args
,
29879 build_tree_list (selector
, arg
));
29881 token
= cp_lexer_peek_token (parser
->lexer
);
29884 /* Handle non-selector arguments, if any. */
29885 while (token
->type
== CPP_COMMA
)
29889 cp_lexer_consume_token (parser
->lexer
);
29890 arg
= cp_parser_assignment_expression (parser
);
29893 = chainon (addl_args
,
29894 build_tree_list (NULL_TREE
, arg
));
29896 token
= cp_lexer_peek_token (parser
->lexer
);
29899 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29901 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29902 return build_tree_list (error_mark_node
, error_mark_node
);
29905 return build_tree_list (sel_args
, addl_args
);
29908 /* Parse an Objective-C encode expression.
29910 objc-encode-expression:
29911 @encode objc-typename
29913 Returns an encoded representation of the type argument. */
29916 cp_parser_objc_encode_expression (cp_parser
* parser
)
29920 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29922 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29923 matching_parens parens
;
29924 parens
.require_open (parser
);
29925 token
= cp_lexer_peek_token (parser
->lexer
);
29926 type
= complete_type (cp_parser_type_id (parser
));
29927 parens
.require_close (parser
);
29931 error_at (token
->location
,
29932 "%<@encode%> must specify a type as an argument");
29933 return error_mark_node
;
29936 /* This happens if we find @encode(T) (where T is a template
29937 typename or something dependent on a template typename) when
29938 parsing a template. In that case, we can't compile it
29939 immediately, but we rather create an AT_ENCODE_EXPR which will
29940 need to be instantiated when the template is used.
29942 if (dependent_type_p (type
))
29944 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29945 TREE_READONLY (value
) = 1;
29950 /* Build a location of the form:
29953 with caret==start at the @ token, finishing at the close paren. */
29954 location_t combined_loc
29955 = make_location (start_loc
, start_loc
,
29956 cp_lexer_previous_token (parser
->lexer
)->location
);
29958 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29961 /* Parse an Objective-C @defs expression. */
29964 cp_parser_objc_defs_expression (cp_parser
*parser
)
29968 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29969 matching_parens parens
;
29970 parens
.require_open (parser
);
29971 name
= cp_parser_identifier (parser
);
29972 parens
.require_close (parser
);
29974 return objc_get_class_ivars (name
);
29977 /* Parse an Objective-C protocol expression.
29979 objc-protocol-expression:
29980 @protocol ( identifier )
29982 Returns a representation of the protocol expression. */
29985 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29988 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29990 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29991 matching_parens parens
;
29992 parens
.require_open (parser
);
29993 proto
= cp_parser_identifier (parser
);
29994 parens
.require_close (parser
);
29996 /* Build a location of the form:
29999 with caret==start at the @ token, finishing at the close paren. */
30000 location_t combined_loc
30001 = make_location (start_loc
, start_loc
,
30002 cp_lexer_previous_token (parser
->lexer
)->location
);
30003 tree result
= objc_build_protocol_expr (proto
);
30004 protected_set_expr_location (result
, combined_loc
);
30008 /* Parse an Objective-C selector expression.
30010 objc-selector-expression:
30011 @selector ( objc-method-signature )
30013 objc-method-signature:
30019 objc-selector-seq objc-selector :
30021 Returns a representation of the method selector. */
30024 cp_parser_objc_selector_expression (cp_parser
* parser
)
30026 tree sel_seq
= NULL_TREE
;
30027 bool maybe_unary_selector_p
= true;
30029 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30031 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30032 matching_parens parens
;
30033 parens
.require_open (parser
);
30034 token
= cp_lexer_peek_token (parser
->lexer
);
30036 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30037 || token
->type
== CPP_SCOPE
)
30039 tree selector
= NULL_TREE
;
30041 if (token
->type
!= CPP_COLON
30042 || token
->type
== CPP_SCOPE
)
30043 selector
= cp_parser_objc_selector (parser
);
30045 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30046 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30048 /* Detect if we have a unary selector. */
30049 if (maybe_unary_selector_p
)
30051 sel_seq
= selector
;
30052 goto finish_selector
;
30056 cp_parser_error (parser
, "expected %<:%>");
30059 maybe_unary_selector_p
= false;
30060 token
= cp_lexer_consume_token (parser
->lexer
);
30062 if (token
->type
== CPP_SCOPE
)
30065 = chainon (sel_seq
,
30066 build_tree_list (selector
, NULL_TREE
));
30068 = chainon (sel_seq
,
30069 build_tree_list (NULL_TREE
, NULL_TREE
));
30073 = chainon (sel_seq
,
30074 build_tree_list (selector
, NULL_TREE
));
30076 token
= cp_lexer_peek_token (parser
->lexer
);
30080 parens
.require_close (parser
);
30083 /* Build a location of the form:
30086 with caret==start at the @ token, finishing at the close paren. */
30087 location_t combined_loc
30088 = make_location (loc
, loc
,
30089 cp_lexer_previous_token (parser
->lexer
)->location
);
30090 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30091 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30092 protected_set_expr_location (result
, combined_loc
);
30096 /* Parse a list of identifiers.
30098 objc-identifier-list:
30100 objc-identifier-list , identifier
30102 Returns a TREE_LIST of identifier nodes. */
30105 cp_parser_objc_identifier_list (cp_parser
* parser
)
30111 identifier
= cp_parser_identifier (parser
);
30112 if (identifier
== error_mark_node
)
30113 return error_mark_node
;
30115 list
= build_tree_list (NULL_TREE
, identifier
);
30116 sep
= cp_lexer_peek_token (parser
->lexer
);
30118 while (sep
->type
== CPP_COMMA
)
30120 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30121 identifier
= cp_parser_identifier (parser
);
30122 if (identifier
== error_mark_node
)
30125 list
= chainon (list
, build_tree_list (NULL_TREE
,
30127 sep
= cp_lexer_peek_token (parser
->lexer
);
30133 /* Parse an Objective-C alias declaration.
30135 objc-alias-declaration:
30136 @compatibility_alias identifier identifier ;
30138 This function registers the alias mapping with the Objective-C front end.
30139 It returns nothing. */
30142 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30146 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30147 alias
= cp_parser_identifier (parser
);
30148 orig
= cp_parser_identifier (parser
);
30149 objc_declare_alias (alias
, orig
);
30150 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30153 /* Parse an Objective-C class forward-declaration.
30155 objc-class-declaration:
30156 @class objc-identifier-list ;
30158 The function registers the forward declarations with the Objective-C
30159 front end. It returns nothing. */
30162 cp_parser_objc_class_declaration (cp_parser
* parser
)
30164 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30169 id
= cp_parser_identifier (parser
);
30170 if (id
== error_mark_node
)
30173 objc_declare_class (id
);
30175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30176 cp_lexer_consume_token (parser
->lexer
);
30180 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30183 /* Parse a list of Objective-C protocol references.
30185 objc-protocol-refs-opt:
30186 objc-protocol-refs [opt]
30188 objc-protocol-refs:
30189 < objc-identifier-list >
30191 Returns a TREE_LIST of identifiers, if any. */
30194 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30196 tree protorefs
= NULL_TREE
;
30198 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30200 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30201 protorefs
= cp_parser_objc_identifier_list (parser
);
30202 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30208 /* Parse a Objective-C visibility specification. */
30211 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30213 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30215 switch (vis
->keyword
)
30217 case RID_AT_PRIVATE
:
30218 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30220 case RID_AT_PROTECTED
:
30221 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30223 case RID_AT_PUBLIC
:
30224 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30226 case RID_AT_PACKAGE
:
30227 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30233 /* Eat '@private'/'@protected'/'@public'. */
30234 cp_lexer_consume_token (parser
->lexer
);
30237 /* Parse an Objective-C method type. Return 'true' if it is a class
30238 (+) method, and 'false' if it is an instance (-) method. */
30241 cp_parser_objc_method_type (cp_parser
* parser
)
30243 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30249 /* Parse an Objective-C protocol qualifier. */
30252 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30254 tree quals
= NULL_TREE
, node
;
30255 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30257 node
= token
->u
.value
;
30259 while (node
&& identifier_p (node
)
30260 && (node
== ridpointers
[(int) RID_IN
]
30261 || node
== ridpointers
[(int) RID_OUT
]
30262 || node
== ridpointers
[(int) RID_INOUT
]
30263 || node
== ridpointers
[(int) RID_BYCOPY
]
30264 || node
== ridpointers
[(int) RID_BYREF
]
30265 || node
== ridpointers
[(int) RID_ONEWAY
]))
30267 quals
= tree_cons (NULL_TREE
, node
, quals
);
30268 cp_lexer_consume_token (parser
->lexer
);
30269 token
= cp_lexer_peek_token (parser
->lexer
);
30270 node
= token
->u
.value
;
30276 /* Parse an Objective-C typename. */
30279 cp_parser_objc_typename (cp_parser
* parser
)
30281 tree type_name
= NULL_TREE
;
30283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30285 tree proto_quals
, cp_type
= NULL_TREE
;
30287 matching_parens parens
;
30288 parens
.consume_open (parser
); /* Eat '('. */
30289 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30291 /* An ObjC type name may consist of just protocol qualifiers, in which
30292 case the type shall default to 'id'. */
30293 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30295 cp_type
= cp_parser_type_id (parser
);
30297 /* If the type could not be parsed, an error has already
30298 been produced. For error recovery, behave as if it had
30299 not been specified, which will use the default type
30301 if (cp_type
== error_mark_node
)
30303 cp_type
= NULL_TREE
;
30304 /* We need to skip to the closing parenthesis as
30305 cp_parser_type_id() does not seem to do it for
30307 cp_parser_skip_to_closing_parenthesis (parser
,
30308 /*recovering=*/true,
30309 /*or_comma=*/false,
30310 /*consume_paren=*/false);
30314 parens
.require_close (parser
);
30315 type_name
= build_tree_list (proto_quals
, cp_type
);
30321 /* Check to see if TYPE refers to an Objective-C selector name. */
30324 cp_parser_objc_selector_p (enum cpp_ttype type
)
30326 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30327 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30328 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30329 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30330 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30333 /* Parse an Objective-C selector. */
30336 cp_parser_objc_selector (cp_parser
* parser
)
30338 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30340 if (!cp_parser_objc_selector_p (token
->type
))
30342 error_at (token
->location
, "invalid Objective-C++ selector name");
30343 return error_mark_node
;
30346 /* C++ operator names are allowed to appear in ObjC selectors. */
30347 switch (token
->type
)
30349 case CPP_AND_AND
: return get_identifier ("and");
30350 case CPP_AND_EQ
: return get_identifier ("and_eq");
30351 case CPP_AND
: return get_identifier ("bitand");
30352 case CPP_OR
: return get_identifier ("bitor");
30353 case CPP_COMPL
: return get_identifier ("compl");
30354 case CPP_NOT
: return get_identifier ("not");
30355 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30356 case CPP_OR_OR
: return get_identifier ("or");
30357 case CPP_OR_EQ
: return get_identifier ("or_eq");
30358 case CPP_XOR
: return get_identifier ("xor");
30359 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30360 default: return token
->u
.value
;
30364 /* Parse an Objective-C params list. */
30367 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30369 tree params
= NULL_TREE
;
30370 bool maybe_unary_selector_p
= true;
30371 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30373 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30375 tree selector
= NULL_TREE
, type_name
, identifier
;
30376 tree parm_attr
= NULL_TREE
;
30378 if (token
->keyword
== RID_ATTRIBUTE
)
30381 if (token
->type
!= CPP_COLON
)
30382 selector
= cp_parser_objc_selector (parser
);
30384 /* Detect if we have a unary selector. */
30385 if (maybe_unary_selector_p
30386 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30388 params
= selector
; /* Might be followed by attributes. */
30392 maybe_unary_selector_p
= false;
30393 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30395 /* Something went quite wrong. There should be a colon
30396 here, but there is not. Stop parsing parameters. */
30399 type_name
= cp_parser_objc_typename (parser
);
30400 /* New ObjC allows attributes on parameters too. */
30401 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30402 parm_attr
= cp_parser_attributes_opt (parser
);
30403 identifier
= cp_parser_identifier (parser
);
30407 objc_build_keyword_decl (selector
,
30412 token
= cp_lexer_peek_token (parser
->lexer
);
30415 if (params
== NULL_TREE
)
30417 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30418 return error_mark_node
;
30421 /* We allow tail attributes for the method. */
30422 if (token
->keyword
== RID_ATTRIBUTE
)
30424 *attributes
= cp_parser_attributes_opt (parser
);
30425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30426 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30428 cp_parser_error (parser
,
30429 "method attributes must be specified at the end");
30430 return error_mark_node
;
30433 if (params
== NULL_TREE
)
30435 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30436 return error_mark_node
;
30441 /* Parse the non-keyword Objective-C params. */
30444 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30447 tree params
= make_node (TREE_LIST
);
30448 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30449 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30451 while (token
->type
== CPP_COMMA
)
30453 cp_parameter_declarator
*parmdecl
;
30456 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30457 token
= cp_lexer_peek_token (parser
->lexer
);
30459 if (token
->type
== CPP_ELLIPSIS
)
30461 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30463 token
= cp_lexer_peek_token (parser
->lexer
);
30467 /* TODO: parse attributes for tail parameters. */
30468 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30469 parm
= grokdeclarator (parmdecl
->declarator
,
30470 &parmdecl
->decl_specifiers
,
30471 PARM
, /*initialized=*/0,
30472 /*attrlist=*/NULL
);
30474 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30475 token
= cp_lexer_peek_token (parser
->lexer
);
30478 /* We allow tail attributes for the method. */
30479 if (token
->keyword
== RID_ATTRIBUTE
)
30481 if (*attributes
== NULL_TREE
)
30483 *attributes
= cp_parser_attributes_opt (parser
);
30484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30485 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30489 /* We have an error, but parse the attributes, so that we can
30491 *attributes
= cp_parser_attributes_opt (parser
);
30493 cp_parser_error (parser
,
30494 "method attributes must be specified at the end");
30495 return error_mark_node
;
30501 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30504 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30506 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30508 /* If the next token is `extern' and the following token is a string
30509 literal, then we have a linkage specification. */
30510 if (token
->keyword
== RID_EXTERN
30511 && cp_parser_is_pure_string_literal
30512 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30513 cp_parser_linkage_specification (parser
);
30514 /* Handle #pragma, if any. */
30515 else if (token
->type
== CPP_PRAGMA
)
30516 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30517 /* Allow stray semicolons. */
30518 else if (token
->type
== CPP_SEMICOLON
)
30519 cp_lexer_consume_token (parser
->lexer
);
30520 /* Mark methods as optional or required, when building protocols. */
30521 else if (token
->keyword
== RID_AT_OPTIONAL
)
30523 cp_lexer_consume_token (parser
->lexer
);
30524 objc_set_method_opt (true);
30526 else if (token
->keyword
== RID_AT_REQUIRED
)
30528 cp_lexer_consume_token (parser
->lexer
);
30529 objc_set_method_opt (false);
30531 else if (token
->keyword
== RID_NAMESPACE
)
30532 cp_parser_namespace_definition (parser
);
30533 /* Other stray characters must generate errors. */
30534 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30536 cp_lexer_consume_token (parser
->lexer
);
30537 error ("stray %qs between Objective-C++ methods",
30538 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30540 /* Finally, try to parse a block-declaration, or a function-definition. */
30542 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30545 /* Parse a method signature. */
30548 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30550 tree rettype
, kwdparms
, optparms
;
30551 bool ellipsis
= false;
30552 bool is_class_method
;
30554 is_class_method
= cp_parser_objc_method_type (parser
);
30555 rettype
= cp_parser_objc_typename (parser
);
30556 *attributes
= NULL_TREE
;
30557 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30558 if (kwdparms
== error_mark_node
)
30559 return error_mark_node
;
30560 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30561 if (optparms
== error_mark_node
)
30562 return error_mark_node
;
30564 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30568 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30571 cp_lexer_save_tokens (parser
->lexer
);
30572 tattr
= cp_parser_attributes_opt (parser
);
30573 gcc_assert (tattr
) ;
30575 /* If the attributes are followed by a method introducer, this is not allowed.
30576 Dump the attributes and flag the situation. */
30577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30578 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30581 /* Otherwise, the attributes introduce some interstitial code, possibly so
30582 rewind to allow that check. */
30583 cp_lexer_rollback_tokens (parser
->lexer
);
30587 /* Parse an Objective-C method prototype list. */
30590 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30592 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30594 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30596 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30598 tree attributes
, sig
;
30599 bool is_class_method
;
30600 if (token
->type
== CPP_PLUS
)
30601 is_class_method
= true;
30603 is_class_method
= false;
30604 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30605 if (sig
== error_mark_node
)
30607 cp_parser_skip_to_end_of_block_or_statement (parser
);
30608 token
= cp_lexer_peek_token (parser
->lexer
);
30611 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30612 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30614 else if (token
->keyword
== RID_AT_PROPERTY
)
30615 cp_parser_objc_at_property_declaration (parser
);
30616 else if (token
->keyword
== RID_ATTRIBUTE
30617 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30618 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30620 "prefix attributes are ignored for methods");
30622 /* Allow for interspersed non-ObjC++ code. */
30623 cp_parser_objc_interstitial_code (parser
);
30625 token
= cp_lexer_peek_token (parser
->lexer
);
30628 if (token
->type
!= CPP_EOF
)
30629 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30631 cp_parser_error (parser
, "expected %<@end%>");
30633 objc_finish_interface ();
30636 /* Parse an Objective-C method definition list. */
30639 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30641 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30643 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30647 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30650 tree sig
, attribute
;
30651 bool is_class_method
;
30652 if (token
->type
== CPP_PLUS
)
30653 is_class_method
= true;
30655 is_class_method
= false;
30656 push_deferring_access_checks (dk_deferred
);
30657 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30658 if (sig
== error_mark_node
)
30660 cp_parser_skip_to_end_of_block_or_statement (parser
);
30661 token
= cp_lexer_peek_token (parser
->lexer
);
30664 objc_start_method_definition (is_class_method
, sig
, attribute
,
30667 /* For historical reasons, we accept an optional semicolon. */
30668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30669 cp_lexer_consume_token (parser
->lexer
);
30671 ptk
= cp_lexer_peek_token (parser
->lexer
);
30672 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30673 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30675 perform_deferred_access_checks (tf_warning_or_error
);
30676 stop_deferring_access_checks ();
30677 meth
= cp_parser_function_definition_after_declarator (parser
,
30679 pop_deferring_access_checks ();
30680 objc_finish_method_definition (meth
);
30683 /* The following case will be removed once @synthesize is
30684 completely implemented. */
30685 else if (token
->keyword
== RID_AT_PROPERTY
)
30686 cp_parser_objc_at_property_declaration (parser
);
30687 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30688 cp_parser_objc_at_synthesize_declaration (parser
);
30689 else if (token
->keyword
== RID_AT_DYNAMIC
)
30690 cp_parser_objc_at_dynamic_declaration (parser
);
30691 else if (token
->keyword
== RID_ATTRIBUTE
30692 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30693 warning_at (token
->location
, OPT_Wattributes
,
30694 "prefix attributes are ignored for methods");
30696 /* Allow for interspersed non-ObjC++ code. */
30697 cp_parser_objc_interstitial_code (parser
);
30699 token
= cp_lexer_peek_token (parser
->lexer
);
30702 if (token
->type
!= CPP_EOF
)
30703 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30705 cp_parser_error (parser
, "expected %<@end%>");
30707 objc_finish_implementation ();
30710 /* Parse Objective-C ivars. */
30713 cp_parser_objc_class_ivars (cp_parser
* parser
)
30715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30717 if (token
->type
!= CPP_OPEN_BRACE
)
30718 return; /* No ivars specified. */
30720 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30721 token
= cp_lexer_peek_token (parser
->lexer
);
30723 while (token
->type
!= CPP_CLOSE_BRACE
30724 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30726 cp_decl_specifier_seq declspecs
;
30727 int decl_class_or_enum_p
;
30728 tree prefix_attributes
;
30730 cp_parser_objc_visibility_spec (parser
);
30732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30735 cp_parser_decl_specifier_seq (parser
,
30736 CP_PARSER_FLAGS_OPTIONAL
,
30738 &decl_class_or_enum_p
);
30740 /* auto, register, static, extern, mutable. */
30741 if (declspecs
.storage_class
!= sc_none
)
30743 cp_parser_error (parser
, "invalid type for instance variable");
30744 declspecs
.storage_class
= sc_none
;
30747 /* thread_local. */
30748 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30750 cp_parser_error (parser
, "invalid type for instance variable");
30751 declspecs
.locations
[ds_thread
] = 0;
30755 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30757 cp_parser_error (parser
, "invalid type for instance variable");
30758 declspecs
.locations
[ds_typedef
] = 0;
30761 prefix_attributes
= declspecs
.attributes
;
30762 declspecs
.attributes
= NULL_TREE
;
30764 /* Keep going until we hit the `;' at the end of the
30766 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30768 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30769 cp_declarator
*declarator
= NULL
;
30770 int ctor_dtor_or_conv_p
;
30772 /* Check for a (possibly unnamed) bitfield declaration. */
30773 token
= cp_lexer_peek_token (parser
->lexer
);
30774 if (token
->type
== CPP_COLON
)
30777 if (token
->type
== CPP_NAME
30778 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30781 /* Get the name of the bitfield. */
30782 declarator
= make_id_declarator (NULL_TREE
,
30783 cp_parser_identifier (parser
),
30784 sfk_none
, token
->location
);
30787 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30788 /* Get the width of the bitfield. */
30790 = cp_parser_constant_expression (parser
);
30794 /* Parse the declarator. */
30796 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30797 &ctor_dtor_or_conv_p
,
30798 /*parenthesized_p=*/NULL
,
30799 /*member_p=*/false,
30800 /*friend_p=*/false);
30803 /* Look for attributes that apply to the ivar. */
30804 attributes
= cp_parser_attributes_opt (parser
);
30805 /* Remember which attributes are prefix attributes and
30807 first_attribute
= attributes
;
30808 /* Combine the attributes. */
30809 attributes
= attr_chainon (prefix_attributes
, attributes
);
30812 /* Create the bitfield declaration. */
30813 decl
= grokbitfield (declarator
, &declspecs
,
30814 width
, NULL_TREE
, attributes
);
30816 decl
= grokfield (declarator
, &declspecs
,
30817 NULL_TREE
, /*init_const_expr_p=*/false,
30818 NULL_TREE
, attributes
);
30820 /* Add the instance variable. */
30821 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30822 objc_add_instance_variable (decl
);
30824 /* Reset PREFIX_ATTRIBUTES. */
30825 if (attributes
!= error_mark_node
)
30827 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30828 attributes
= TREE_CHAIN (attributes
);
30830 TREE_CHAIN (attributes
) = NULL_TREE
;
30833 token
= cp_lexer_peek_token (parser
->lexer
);
30835 if (token
->type
== CPP_COMMA
)
30837 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30843 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30844 token
= cp_lexer_peek_token (parser
->lexer
);
30847 if (token
->keyword
== RID_AT_END
)
30848 cp_parser_error (parser
, "expected %<}%>");
30850 /* Do not consume the RID_AT_END, so it will be read again as terminating
30851 the @interface of @implementation. */
30852 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30853 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30855 /* For historical reasons, we accept an optional semicolon. */
30856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30857 cp_lexer_consume_token (parser
->lexer
);
30860 /* Parse an Objective-C protocol declaration. */
30863 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30865 tree proto
, protorefs
;
30868 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30869 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30871 tok
= cp_lexer_peek_token (parser
->lexer
);
30872 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30873 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30877 /* See if we have a forward declaration or a definition. */
30878 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30880 /* Try a forward declaration first. */
30881 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30887 id
= cp_parser_identifier (parser
);
30888 if (id
== error_mark_node
)
30891 objc_declare_protocol (id
, attributes
);
30893 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30894 cp_lexer_consume_token (parser
->lexer
);
30898 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30901 /* Ok, we got a full-fledged definition (or at least should). */
30904 proto
= cp_parser_identifier (parser
);
30905 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30906 objc_start_protocol (proto
, protorefs
, attributes
);
30907 cp_parser_objc_method_prototype_list (parser
);
30911 /* Parse an Objective-C superclass or category. */
30914 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30917 tree
*categ
, bool *is_class_extension
)
30919 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30921 *super
= *categ
= NULL_TREE
;
30922 *is_class_extension
= false;
30923 if (next
->type
== CPP_COLON
)
30925 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30926 *super
= cp_parser_identifier (parser
);
30928 else if (next
->type
== CPP_OPEN_PAREN
)
30930 matching_parens parens
;
30931 parens
.consume_open (parser
); /* Eat '('. */
30933 /* If there is no category name, and this is an @interface, we
30934 have a class extension. */
30935 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30937 *categ
= NULL_TREE
;
30938 *is_class_extension
= true;
30941 *categ
= cp_parser_identifier (parser
);
30943 parens
.require_close (parser
);
30947 /* Parse an Objective-C class interface. */
30950 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30952 tree name
, super
, categ
, protos
;
30953 bool is_class_extension
;
30955 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30956 name
= cp_parser_identifier (parser
);
30957 if (name
== error_mark_node
)
30959 /* It's hard to recover because even if valid @interface stuff
30960 is to follow, we can't compile it (or validate it) if we
30961 don't even know which class it refers to. Let's assume this
30962 was a stray '@interface' token in the stream and skip it.
30966 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30967 &is_class_extension
);
30968 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30970 /* We have either a class or a category on our hands. */
30971 if (categ
|| is_class_extension
)
30972 objc_start_category_interface (name
, categ
, protos
, attributes
);
30975 objc_start_class_interface (name
, super
, protos
, attributes
);
30976 /* Handle instance variable declarations, if any. */
30977 cp_parser_objc_class_ivars (parser
);
30978 objc_continue_interface ();
30981 cp_parser_objc_method_prototype_list (parser
);
30984 /* Parse an Objective-C class implementation. */
30987 cp_parser_objc_class_implementation (cp_parser
* parser
)
30989 tree name
, super
, categ
;
30990 bool is_class_extension
;
30992 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30993 name
= cp_parser_identifier (parser
);
30994 if (name
== error_mark_node
)
30996 /* It's hard to recover because even if valid @implementation
30997 stuff is to follow, we can't compile it (or validate it) if
30998 we don't even know which class it refers to. Let's assume
30999 this was a stray '@implementation' token in the stream and
31004 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31005 &is_class_extension
);
31007 /* We have either a class or a category on our hands. */
31009 objc_start_category_implementation (name
, categ
);
31012 objc_start_class_implementation (name
, super
);
31013 /* Handle instance variable declarations, if any. */
31014 cp_parser_objc_class_ivars (parser
);
31015 objc_continue_implementation ();
31018 cp_parser_objc_method_definition_list (parser
);
31021 /* Consume the @end token and finish off the implementation. */
31024 cp_parser_objc_end_implementation (cp_parser
* parser
)
31026 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31027 objc_finish_implementation ();
31030 /* Parse an Objective-C declaration. */
31033 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31035 /* Try to figure out what kind of declaration is present. */
31036 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31039 switch (kwd
->keyword
)
31044 error_at (kwd
->location
, "attributes may not be specified before"
31045 " the %<@%D%> Objective-C++ keyword",
31049 case RID_AT_IMPLEMENTATION
:
31050 warning_at (kwd
->location
, OPT_Wattributes
,
31051 "prefix attributes are ignored before %<@%D%>",
31058 switch (kwd
->keyword
)
31061 cp_parser_objc_alias_declaration (parser
);
31064 cp_parser_objc_class_declaration (parser
);
31066 case RID_AT_PROTOCOL
:
31067 cp_parser_objc_protocol_declaration (parser
, attributes
);
31069 case RID_AT_INTERFACE
:
31070 cp_parser_objc_class_interface (parser
, attributes
);
31072 case RID_AT_IMPLEMENTATION
:
31073 cp_parser_objc_class_implementation (parser
);
31076 cp_parser_objc_end_implementation (parser
);
31079 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31081 cp_parser_skip_to_end_of_block_or_statement (parser
);
31085 /* Parse an Objective-C try-catch-finally statement.
31087 objc-try-catch-finally-stmt:
31088 @try compound-statement objc-catch-clause-seq [opt]
31089 objc-finally-clause [opt]
31091 objc-catch-clause-seq:
31092 objc-catch-clause objc-catch-clause-seq [opt]
31095 @catch ( objc-exception-declaration ) compound-statement
31097 objc-finally-clause:
31098 @finally compound-statement
31100 objc-exception-declaration:
31101 parameter-declaration
31104 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31108 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31109 for C. Keep them in sync. */
31112 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31114 location_t location
;
31117 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31118 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31119 objc_maybe_warn_exceptions (location
);
31120 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31121 node, lest it get absorbed into the surrounding block. */
31122 stmt
= push_stmt_list ();
31123 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31124 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31126 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31128 cp_parameter_declarator
*parm
;
31129 tree parameter_declaration
= error_mark_node
;
31130 bool seen_open_paren
= false;
31131 matching_parens parens
;
31133 cp_lexer_consume_token (parser
->lexer
);
31134 if (parens
.require_open (parser
))
31135 seen_open_paren
= true;
31136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31138 /* We have "@catch (...)" (where the '...' are literally
31139 what is in the code). Skip the '...'.
31140 parameter_declaration is set to NULL_TREE, and
31141 objc_being_catch_clauses() knows that that means
31143 cp_lexer_consume_token (parser
->lexer
);
31144 parameter_declaration
= NULL_TREE
;
31148 /* We have "@catch (NSException *exception)" or something
31149 like that. Parse the parameter declaration. */
31150 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
31152 parameter_declaration
= error_mark_node
;
31154 parameter_declaration
= grokdeclarator (parm
->declarator
,
31155 &parm
->decl_specifiers
,
31156 PARM
, /*initialized=*/0,
31157 /*attrlist=*/NULL
);
31159 if (seen_open_paren
)
31160 parens
.require_close (parser
);
31163 /* If there was no open parenthesis, we are recovering from
31164 an error, and we are trying to figure out what mistake
31165 the user has made. */
31167 /* If there is an immediate closing parenthesis, the user
31168 probably forgot the opening one (ie, they typed "@catch
31169 NSException *e)". Parse the closing parenthesis and keep
31171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31172 cp_lexer_consume_token (parser
->lexer
);
31174 /* If these is no immediate closing parenthesis, the user
31175 probably doesn't know that parenthesis are required at
31176 all (ie, they typed "@catch NSException *e"). So, just
31177 forget about the closing parenthesis and keep going. */
31179 objc_begin_catch_clause (parameter_declaration
);
31180 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31181 objc_finish_catch_clause ();
31183 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31185 cp_lexer_consume_token (parser
->lexer
);
31186 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31187 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31188 node, lest it get absorbed into the surrounding block. */
31189 stmt
= push_stmt_list ();
31190 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31191 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31194 return objc_finish_try_stmt ();
31197 /* Parse an Objective-C synchronized statement.
31199 objc-synchronized-stmt:
31200 @synchronized ( expression ) compound-statement
31202 Returns NULL_TREE. */
31205 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31207 location_t location
;
31210 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31212 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31213 objc_maybe_warn_exceptions (location
);
31214 matching_parens parens
;
31215 parens
.require_open (parser
);
31216 lock
= cp_parser_expression (parser
);
31217 parens
.require_close (parser
);
31219 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31220 node, lest it get absorbed into the surrounding block. */
31221 stmt
= push_stmt_list ();
31222 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31224 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31227 /* Parse an Objective-C throw statement.
31230 @throw assignment-expression [opt] ;
31232 Returns a constructed '@throw' statement. */
31235 cp_parser_objc_throw_statement (cp_parser
*parser
)
31237 tree expr
= NULL_TREE
;
31238 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31240 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31243 expr
= cp_parser_expression (parser
);
31245 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31247 return objc_build_throw_stmt (loc
, expr
);
31250 /* Parse an Objective-C statement. */
31253 cp_parser_objc_statement (cp_parser
* parser
)
31255 /* Try to figure out what kind of declaration is present. */
31256 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31258 switch (kwd
->keyword
)
31261 return cp_parser_objc_try_catch_finally_statement (parser
);
31262 case RID_AT_SYNCHRONIZED
:
31263 return cp_parser_objc_synchronized_statement (parser
);
31265 return cp_parser_objc_throw_statement (parser
);
31267 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31269 cp_parser_skip_to_end_of_block_or_statement (parser
);
31272 return error_mark_node
;
31275 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31276 look ahead to see if an objc keyword follows the attributes. This
31277 is to detect the use of prefix attributes on ObjC @interface and
31281 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31283 cp_lexer_save_tokens (parser
->lexer
);
31284 *attrib
= cp_parser_attributes_opt (parser
);
31285 gcc_assert (*attrib
);
31286 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31288 cp_lexer_commit_tokens (parser
->lexer
);
31291 cp_lexer_rollback_tokens (parser
->lexer
);
31295 /* This routine is a minimal replacement for
31296 c_parser_struct_declaration () used when parsing the list of
31297 types/names or ObjC++ properties. For example, when parsing the
31300 @property (readonly) int a, b, c;
31302 this function is responsible for parsing "int a, int b, int c" and
31303 returning the declarations as CHAIN of DECLs.
31305 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31306 similar parsing. */
31308 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31310 tree decls
= NULL_TREE
;
31311 cp_decl_specifier_seq declspecs
;
31312 int decl_class_or_enum_p
;
31313 tree prefix_attributes
;
31315 cp_parser_decl_specifier_seq (parser
,
31316 CP_PARSER_FLAGS_NONE
,
31318 &decl_class_or_enum_p
);
31320 if (declspecs
.type
== error_mark_node
)
31321 return error_mark_node
;
31323 /* auto, register, static, extern, mutable. */
31324 if (declspecs
.storage_class
!= sc_none
)
31326 cp_parser_error (parser
, "invalid type for property");
31327 declspecs
.storage_class
= sc_none
;
31330 /* thread_local. */
31331 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31333 cp_parser_error (parser
, "invalid type for property");
31334 declspecs
.locations
[ds_thread
] = 0;
31338 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31340 cp_parser_error (parser
, "invalid type for property");
31341 declspecs
.locations
[ds_typedef
] = 0;
31344 prefix_attributes
= declspecs
.attributes
;
31345 declspecs
.attributes
= NULL_TREE
;
31347 /* Keep going until we hit the `;' at the end of the declaration. */
31348 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31350 tree attributes
, first_attribute
, decl
;
31351 cp_declarator
*declarator
;
31354 /* Parse the declarator. */
31355 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31356 NULL
, NULL
, false, false);
31358 /* Look for attributes that apply to the ivar. */
31359 attributes
= cp_parser_attributes_opt (parser
);
31360 /* Remember which attributes are prefix attributes and
31362 first_attribute
= attributes
;
31363 /* Combine the attributes. */
31364 attributes
= attr_chainon (prefix_attributes
, attributes
);
31366 decl
= grokfield (declarator
, &declspecs
,
31367 NULL_TREE
, /*init_const_expr_p=*/false,
31368 NULL_TREE
, attributes
);
31370 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31371 return error_mark_node
;
31373 /* Reset PREFIX_ATTRIBUTES. */
31374 if (attributes
!= error_mark_node
)
31376 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31377 attributes
= TREE_CHAIN (attributes
);
31379 TREE_CHAIN (attributes
) = NULL_TREE
;
31382 DECL_CHAIN (decl
) = decls
;
31385 token
= cp_lexer_peek_token (parser
->lexer
);
31386 if (token
->type
== CPP_COMMA
)
31388 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31397 /* Parse an Objective-C @property declaration. The syntax is:
31399 objc-property-declaration:
31400 '@property' objc-property-attributes[opt] struct-declaration ;
31402 objc-property-attributes:
31403 '(' objc-property-attribute-list ')'
31405 objc-property-attribute-list:
31406 objc-property-attribute
31407 objc-property-attribute-list, objc-property-attribute
31409 objc-property-attribute
31410 'getter' = identifier
31411 'setter' = identifier
31420 @property NSString *name;
31421 @property (readonly) id object;
31422 @property (retain, nonatomic, getter=getTheName) id name;
31423 @property int a, b, c;
31425 PS: This function is identical to
31426 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31428 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31430 /* The following variables hold the attributes of the properties as
31431 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31432 seen. When we see an attribute, we set them to 'true' (if they
31433 are boolean properties) or to the identifier (if they have an
31434 argument, ie, for getter and setter). Note that here we only
31435 parse the list of attributes, check the syntax and accumulate the
31436 attributes that we find. objc_add_property_declaration() will
31437 then process the information. */
31438 bool property_assign
= false;
31439 bool property_copy
= false;
31440 tree property_getter_ident
= NULL_TREE
;
31441 bool property_nonatomic
= false;
31442 bool property_readonly
= false;
31443 bool property_readwrite
= false;
31444 bool property_retain
= false;
31445 tree property_setter_ident
= NULL_TREE
;
31447 /* 'properties' is the list of properties that we read. Usually a
31448 single one, but maybe more (eg, in "@property int a, b, c;" there
31453 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31455 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31457 /* Parse the optional attribute list... */
31458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31461 matching_parens parens
;
31462 parens
.consume_open (parser
);
31466 bool syntax_error
= false;
31467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31470 if (token
->type
!= CPP_NAME
)
31472 cp_parser_error (parser
, "expected identifier");
31475 keyword
= C_RID_CODE (token
->u
.value
);
31476 cp_lexer_consume_token (parser
->lexer
);
31479 case RID_ASSIGN
: property_assign
= true; break;
31480 case RID_COPY
: property_copy
= true; break;
31481 case RID_NONATOMIC
: property_nonatomic
= true; break;
31482 case RID_READONLY
: property_readonly
= true; break;
31483 case RID_READWRITE
: property_readwrite
= true; break;
31484 case RID_RETAIN
: property_retain
= true; break;
31488 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31490 if (keyword
== RID_GETTER
)
31491 cp_parser_error (parser
,
31492 "missing %<=%> (after %<getter%> attribute)");
31494 cp_parser_error (parser
,
31495 "missing %<=%> (after %<setter%> attribute)");
31496 syntax_error
= true;
31499 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31500 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31502 cp_parser_error (parser
, "expected identifier");
31503 syntax_error
= true;
31506 if (keyword
== RID_SETTER
)
31508 if (property_setter_ident
!= NULL_TREE
)
31510 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31511 cp_lexer_consume_token (parser
->lexer
);
31514 property_setter_ident
= cp_parser_objc_selector (parser
);
31515 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31516 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31518 cp_lexer_consume_token (parser
->lexer
);
31522 if (property_getter_ident
!= NULL_TREE
)
31524 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31525 cp_lexer_consume_token (parser
->lexer
);
31528 property_getter_ident
= cp_parser_objc_selector (parser
);
31532 cp_parser_error (parser
, "unknown property attribute");
31533 syntax_error
= true;
31540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31541 cp_lexer_consume_token (parser
->lexer
);
31546 /* FIXME: "@property (setter, assign);" will generate a spurious
31547 "error: expected ‘)’ before ‘,’ token". This is because
31548 cp_parser_require, unlike the C counterpart, will produce an
31549 error even if we are in error recovery. */
31550 if (!parens
.require_close (parser
))
31552 cp_parser_skip_to_closing_parenthesis (parser
,
31553 /*recovering=*/true,
31554 /*or_comma=*/false,
31555 /*consume_paren=*/true);
31559 /* ... and the property declaration(s). */
31560 properties
= cp_parser_objc_struct_declaration (parser
);
31562 if (properties
== error_mark_node
)
31564 cp_parser_skip_to_end_of_statement (parser
);
31565 /* If the next token is now a `;', consume it. */
31566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31567 cp_lexer_consume_token (parser
->lexer
);
31571 if (properties
== NULL_TREE
)
31572 cp_parser_error (parser
, "expected identifier");
31575 /* Comma-separated properties are chained together in
31576 reverse order; add them one by one. */
31577 properties
= nreverse (properties
);
31579 for (; properties
; properties
= TREE_CHAIN (properties
))
31580 objc_add_property_declaration (loc
, copy_node (properties
),
31581 property_readonly
, property_readwrite
,
31582 property_assign
, property_retain
,
31583 property_copy
, property_nonatomic
,
31584 property_getter_ident
, property_setter_ident
);
31587 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31590 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31592 objc-synthesize-declaration:
31593 @synthesize objc-synthesize-identifier-list ;
31595 objc-synthesize-identifier-list:
31596 objc-synthesize-identifier
31597 objc-synthesize-identifier-list, objc-synthesize-identifier
31599 objc-synthesize-identifier
31601 identifier = identifier
31604 @synthesize MyProperty;
31605 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31607 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31608 for C. Keep them in sync.
31611 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31613 tree list
= NULL_TREE
;
31615 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31617 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31620 tree property
, ivar
;
31621 property
= cp_parser_identifier (parser
);
31622 if (property
== error_mark_node
)
31624 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31629 cp_lexer_consume_token (parser
->lexer
);
31630 ivar
= cp_parser_identifier (parser
);
31631 if (ivar
== error_mark_node
)
31633 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31639 list
= chainon (list
, build_tree_list (ivar
, property
));
31640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31641 cp_lexer_consume_token (parser
->lexer
);
31645 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31646 objc_add_synthesize_declaration (loc
, list
);
31649 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31651 objc-dynamic-declaration:
31652 @dynamic identifier-list ;
31655 @dynamic MyProperty;
31656 @dynamic MyProperty, AnotherProperty;
31658 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31659 for C. Keep them in sync.
31662 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31664 tree list
= NULL_TREE
;
31666 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31668 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31672 property
= cp_parser_identifier (parser
);
31673 if (property
== error_mark_node
)
31675 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31678 list
= chainon (list
, build_tree_list (NULL
, property
));
31679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31680 cp_lexer_consume_token (parser
->lexer
);
31684 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31685 objc_add_dynamic_declaration (loc
, list
);
31689 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31691 /* Returns name of the next clause.
31692 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31693 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31694 returned and the token is consumed. */
31696 static pragma_omp_clause
31697 cp_parser_omp_clause_name (cp_parser
*parser
)
31699 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31701 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31702 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31703 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31704 result
= PRAGMA_OMP_CLAUSE_IF
;
31705 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31706 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31707 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31708 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31709 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31710 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31711 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31712 result
= PRAGMA_OMP_CLAUSE_FOR
;
31713 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31715 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31716 const char *p
= IDENTIFIER_POINTER (id
);
31721 if (!strcmp ("aligned", p
))
31722 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31723 else if (!strcmp ("async", p
))
31724 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31727 if (!strcmp ("collapse", p
))
31728 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31729 else if (!strcmp ("copy", p
))
31730 result
= PRAGMA_OACC_CLAUSE_COPY
;
31731 else if (!strcmp ("copyin", p
))
31732 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31733 else if (!strcmp ("copyout", p
))
31734 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31735 else if (!strcmp ("copyprivate", p
))
31736 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31737 else if (!strcmp ("create", p
))
31738 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31741 if (!strcmp ("defaultmap", p
))
31742 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31743 else if (!strcmp ("depend", p
))
31744 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31745 else if (!strcmp ("device", p
))
31746 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31747 else if (!strcmp ("deviceptr", p
))
31748 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31749 else if (!strcmp ("device_resident", p
))
31750 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31751 else if (!strcmp ("dist_schedule", p
))
31752 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31755 if (!strcmp ("final", p
))
31756 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31757 else if (!strcmp ("finalize", p
))
31758 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31759 else if (!strcmp ("firstprivate", p
))
31760 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31761 else if (!strcmp ("from", p
))
31762 result
= PRAGMA_OMP_CLAUSE_FROM
;
31765 if (!strcmp ("gang", p
))
31766 result
= PRAGMA_OACC_CLAUSE_GANG
;
31767 else if (!strcmp ("grainsize", p
))
31768 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31771 if (!strcmp ("hint", p
))
31772 result
= PRAGMA_OMP_CLAUSE_HINT
;
31773 else if (!strcmp ("host", p
))
31774 result
= PRAGMA_OACC_CLAUSE_HOST
;
31777 if (!strcmp ("if_present", p
))
31778 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31779 else if (!strcmp ("in_reduction", p
))
31780 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
31781 else if (!strcmp ("inbranch", p
))
31782 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31783 else if (!strcmp ("independent", p
))
31784 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31785 else if (!strcmp ("is_device_ptr", p
))
31786 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31789 if (!strcmp ("lastprivate", p
))
31790 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31791 else if (!strcmp ("linear", p
))
31792 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31793 else if (!strcmp ("link", p
))
31794 result
= PRAGMA_OMP_CLAUSE_LINK
;
31797 if (!strcmp ("map", p
))
31798 result
= PRAGMA_OMP_CLAUSE_MAP
;
31799 else if (!strcmp ("mergeable", p
))
31800 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31803 if (!strcmp ("nogroup", p
))
31804 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31805 else if (!strcmp ("nontemporal", p
))
31806 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
31807 else if (!strcmp ("notinbranch", p
))
31808 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31809 else if (!strcmp ("nowait", p
))
31810 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31811 else if (!strcmp ("num_gangs", p
))
31812 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31813 else if (!strcmp ("num_tasks", p
))
31814 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31815 else if (!strcmp ("num_teams", p
))
31816 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31817 else if (!strcmp ("num_threads", p
))
31818 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31819 else if (!strcmp ("num_workers", p
))
31820 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31823 if (!strcmp ("ordered", p
))
31824 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31827 if (!strcmp ("parallel", p
))
31828 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31829 else if (!strcmp ("present", p
))
31830 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31831 else if (!strcmp ("present_or_copy", p
)
31832 || !strcmp ("pcopy", p
))
31833 result
= PRAGMA_OACC_CLAUSE_COPY
;
31834 else if (!strcmp ("present_or_copyin", p
)
31835 || !strcmp ("pcopyin", p
))
31836 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31837 else if (!strcmp ("present_or_copyout", p
)
31838 || !strcmp ("pcopyout", p
))
31839 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31840 else if (!strcmp ("present_or_create", p
)
31841 || !strcmp ("pcreate", p
))
31842 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31843 else if (!strcmp ("priority", p
))
31844 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31845 else if (!strcmp ("proc_bind", p
))
31846 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31849 if (!strcmp ("reduction", p
))
31850 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31853 if (!strcmp ("safelen", p
))
31854 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31855 else if (!strcmp ("schedule", p
))
31856 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31857 else if (!strcmp ("sections", p
))
31858 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31859 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31860 result
= PRAGMA_OACC_CLAUSE_HOST
;
31861 else if (!strcmp ("seq", p
))
31862 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31863 else if (!strcmp ("shared", p
))
31864 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31865 else if (!strcmp ("simd", p
))
31866 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31867 else if (!strcmp ("simdlen", p
))
31868 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31871 if (!strcmp ("task_reduction", p
))
31872 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
31873 else if (!strcmp ("taskgroup", p
))
31874 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31875 else if (!strcmp ("thread_limit", p
))
31876 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31877 else if (!strcmp ("threads", p
))
31878 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31879 else if (!strcmp ("tile", p
))
31880 result
= PRAGMA_OACC_CLAUSE_TILE
;
31881 else if (!strcmp ("to", p
))
31882 result
= PRAGMA_OMP_CLAUSE_TO
;
31885 if (!strcmp ("uniform", p
))
31886 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31887 else if (!strcmp ("untied", p
))
31888 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31889 else if (!strcmp ("use_device", p
))
31890 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31891 else if (!strcmp ("use_device_ptr", p
))
31892 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31895 if (!strcmp ("vector", p
))
31896 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31897 else if (!strcmp ("vector_length", p
))
31898 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31901 if (!strcmp ("wait", p
))
31902 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31903 else if (!strcmp ("worker", p
))
31904 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31909 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31910 cp_lexer_consume_token (parser
->lexer
);
31915 /* Validate that a clause of the given type does not already exist. */
31918 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31919 const char *name
, location_t location
)
31923 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31924 if (OMP_CLAUSE_CODE (c
) == code
)
31926 error_at (location
, "too many %qs clauses", name
);
31934 variable-list , identifier
31936 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31937 colon). An opening parenthesis will have been consumed by the caller.
31939 If KIND is nonzero, create the appropriate node and install the decl
31940 in OMP_CLAUSE_DECL and add the node to the head of the list.
31942 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31943 return the list created.
31945 COLON can be NULL if only closing parenthesis should end the list,
31946 or pointer to bool which will receive false if the list is terminated
31947 by closing parenthesis or true if the list is terminated by colon. */
31950 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31951 tree list
, bool *colon
)
31954 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31957 parser
->colon_corrects_to_scope_p
= false;
31964 if (kind
== OMP_CLAUSE_DEPEND
)
31965 cp_parser_parse_tentatively (parser
);
31966 token
= cp_lexer_peek_token (parser
->lexer
);
31968 && current_class_ptr
31969 && cp_parser_is_keyword (token
, RID_THIS
))
31971 decl
= finish_this_expr ();
31972 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31973 || CONVERT_EXPR_P (decl
))
31974 decl
= TREE_OPERAND (decl
, 0);
31975 cp_lexer_consume_token (parser
->lexer
);
31979 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31980 /*check_dependency_p=*/true,
31981 /*template_p=*/NULL
,
31982 /*declarator_p=*/false,
31983 /*optional_p=*/false);
31984 if (name
== error_mark_node
)
31986 if (kind
== OMP_CLAUSE_DEPEND
31987 && cp_parser_simulate_error (parser
))
31988 goto depend_lvalue
;
31992 if (identifier_p (name
))
31993 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31996 if (decl
== error_mark_node
)
31998 if (kind
== OMP_CLAUSE_DEPEND
31999 && cp_parser_simulate_error (parser
))
32000 goto depend_lvalue
;
32001 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32005 if (decl
== error_mark_node
)
32007 else if (kind
!= 0)
32011 case OMP_CLAUSE__CACHE_
:
32012 /* The OpenACC cache directive explicitly only allows "array
32013 elements or subarrays". */
32014 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32016 error_at (token
->location
, "expected %<[%>");
32017 decl
= error_mark_node
;
32021 case OMP_CLAUSE_MAP
:
32022 case OMP_CLAUSE_FROM
:
32023 case OMP_CLAUSE_TO
:
32024 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32027 = cp_lexer_peek_token (parser
->lexer
)->location
;
32028 cp_id_kind idk
= CP_ID_KIND_NONE
;
32029 cp_lexer_consume_token (parser
->lexer
);
32030 decl
= convert_from_reference (decl
);
32032 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32037 case OMP_CLAUSE_DEPEND
:
32038 case OMP_CLAUSE_REDUCTION
:
32039 case OMP_CLAUSE_IN_REDUCTION
:
32040 case OMP_CLAUSE_TASK_REDUCTION
:
32041 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32043 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32045 parser
->colon_corrects_to_scope_p
= false;
32046 cp_lexer_consume_token (parser
->lexer
);
32047 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32048 low_bound
= cp_parser_expression (parser
);
32050 parser
->colon_corrects_to_scope_p
32051 = saved_colon_corrects_to_scope_p
;
32052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32053 length
= integer_one_node
;
32056 /* Look for `:'. */
32057 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32059 if (kind
== OMP_CLAUSE_DEPEND
32060 && cp_parser_simulate_error (parser
))
32061 goto depend_lvalue
;
32064 if (kind
== OMP_CLAUSE_DEPEND
)
32065 cp_parser_commit_to_tentative_parse (parser
);
32066 if (!cp_lexer_next_token_is (parser
->lexer
,
32068 length
= cp_parser_expression (parser
);
32070 /* Look for the closing `]'. */
32071 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32074 if (kind
== OMP_CLAUSE_DEPEND
32075 && cp_parser_simulate_error (parser
))
32076 goto depend_lvalue
;
32080 decl
= tree_cons (low_bound
, length
, decl
);
32087 if (kind
== OMP_CLAUSE_DEPEND
)
32089 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32090 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32091 && cp_parser_simulate_error (parser
))
32094 cp_parser_abort_tentative_parse (parser
);
32095 decl
= cp_parser_assignment_expression (parser
, NULL
,
32099 cp_parser_parse_definitely (parser
);
32102 tree u
= build_omp_clause (token
->location
, kind
);
32103 OMP_CLAUSE_DECL (u
) = decl
;
32104 OMP_CLAUSE_CHAIN (u
) = list
;
32108 list
= tree_cons (decl
, NULL_TREE
, list
);
32111 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32113 cp_lexer_consume_token (parser
->lexer
);
32117 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32119 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32122 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32126 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32130 /* Try to resync to an unnested comma. Copied from
32131 cp_parser_parenthesized_expression_list. */
32134 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32135 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32136 /*recovering=*/true,
32138 /*consume_paren=*/true);
32146 /* Similarly, but expect leading and trailing parenthesis. This is a very
32147 common case for omp clauses. */
32150 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32152 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32153 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32158 copy ( variable-list )
32159 copyin ( variable-list )
32160 copyout ( variable-list )
32161 create ( variable-list )
32162 delete ( variable-list )
32163 present ( variable-list ) */
32166 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32169 enum gomp_map_kind kind
;
32172 case PRAGMA_OACC_CLAUSE_COPY
:
32173 kind
= GOMP_MAP_TOFROM
;
32175 case PRAGMA_OACC_CLAUSE_COPYIN
:
32176 kind
= GOMP_MAP_TO
;
32178 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32179 kind
= GOMP_MAP_FROM
;
32181 case PRAGMA_OACC_CLAUSE_CREATE
:
32182 kind
= GOMP_MAP_ALLOC
;
32184 case PRAGMA_OACC_CLAUSE_DELETE
:
32185 kind
= GOMP_MAP_RELEASE
;
32187 case PRAGMA_OACC_CLAUSE_DEVICE
:
32188 kind
= GOMP_MAP_FORCE_TO
;
32190 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32191 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32193 case PRAGMA_OACC_CLAUSE_HOST
:
32194 kind
= GOMP_MAP_FORCE_FROM
;
32196 case PRAGMA_OACC_CLAUSE_LINK
:
32197 kind
= GOMP_MAP_LINK
;
32199 case PRAGMA_OACC_CLAUSE_PRESENT
:
32200 kind
= GOMP_MAP_FORCE_PRESENT
;
32203 gcc_unreachable ();
32206 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32208 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32209 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32215 deviceptr ( variable-list ) */
32218 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32220 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32223 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32224 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32225 variable-list must only allow for pointer variables. */
32226 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32227 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32229 tree v
= TREE_PURPOSE (t
);
32230 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32231 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32232 OMP_CLAUSE_DECL (u
) = v
;
32233 OMP_CLAUSE_CHAIN (u
) = list
;
32248 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32249 enum omp_clause_code code
,
32250 tree list
, location_t location
)
32252 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32253 tree c
= build_omp_clause (location
, code
);
32254 OMP_CLAUSE_CHAIN (c
) = list
;
32259 num_gangs ( expression )
32260 num_workers ( expression )
32261 vector_length ( expression ) */
32264 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32265 const char *str
, tree list
)
32267 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32269 matching_parens parens
;
32270 if (!parens
.require_open (parser
))
32273 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32275 if (t
== error_mark_node
32276 || !parens
.require_close (parser
))
32278 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32279 /*or_comma=*/false,
32280 /*consume_paren=*/true);
32284 check_no_duplicate_clause (list
, code
, str
, loc
);
32286 tree c
= build_omp_clause (loc
, code
);
32287 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32288 OMP_CLAUSE_CHAIN (c
) = list
;
32294 gang [( gang-arg-list )]
32295 worker [( [num:] int-expr )]
32296 vector [( [length:] int-expr )]
32298 where gang-arg is one of:
32303 and size-expr may be:
32310 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32311 const char *str
, tree list
)
32313 const char *id
= "num";
32314 cp_lexer
*lexer
= parser
->lexer
;
32315 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32316 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32318 if (kind
== OMP_CLAUSE_VECTOR
)
32321 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32323 matching_parens parens
;
32324 parens
.consume_open (parser
);
32328 cp_token
*next
= cp_lexer_peek_token (lexer
);
32331 /* Gang static argument. */
32332 if (kind
== OMP_CLAUSE_GANG
32333 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32335 cp_lexer_consume_token (lexer
);
32337 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32338 goto cleanup_error
;
32341 if (ops
[idx
] != NULL
)
32343 cp_parser_error (parser
, "too many %<static%> arguments");
32344 goto cleanup_error
;
32347 /* Check for the '*' argument. */
32348 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32349 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32350 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32353 cp_lexer_consume_token (lexer
);
32354 ops
[idx
] = integer_minus_one_node
;
32356 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32358 cp_lexer_consume_token (lexer
);
32364 /* Worker num: argument and vector length: arguments. */
32365 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32366 && id_equal (next
->u
.value
, id
)
32367 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32369 cp_lexer_consume_token (lexer
); /* id */
32370 cp_lexer_consume_token (lexer
); /* ':' */
32373 /* Now collect the actual argument. */
32374 if (ops
[idx
] != NULL_TREE
)
32376 cp_parser_error (parser
, "unexpected argument");
32377 goto cleanup_error
;
32380 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32382 if (expr
== error_mark_node
)
32383 goto cleanup_error
;
32385 mark_exp_read (expr
);
32388 if (kind
== OMP_CLAUSE_GANG
32389 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32391 cp_lexer_consume_token (lexer
);
32398 if (!parens
.require_close (parser
))
32399 goto cleanup_error
;
32402 check_no_duplicate_clause (list
, kind
, str
, loc
);
32404 c
= build_omp_clause (loc
, kind
);
32407 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32409 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32410 OMP_CLAUSE_CHAIN (c
) = list
;
32415 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32420 tile ( size-expr-list ) */
32423 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32425 tree c
, expr
= error_mark_node
;
32426 tree tile
= NULL_TREE
;
32428 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32429 so, but the spec authors never considered such a case and have
32430 differing opinions on what it might mean, including 'not
32432 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32433 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32436 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32441 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32445 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32446 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32448 cp_lexer_consume_token (parser
->lexer
);
32449 expr
= integer_zero_node
;
32452 expr
= cp_parser_constant_expression (parser
);
32454 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32456 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32458 /* Consume the trailing ')'. */
32459 cp_lexer_consume_token (parser
->lexer
);
32461 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32462 tile
= nreverse (tile
);
32463 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32464 OMP_CLAUSE_CHAIN (c
) = list
;
32469 Parse wait clause or directive parameters. */
32472 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32474 vec
<tree
, va_gc
> *args
;
32477 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32479 /*allow_expansion_p=*/true,
32480 /*non_constant_p=*/NULL
);
32482 if (args
== NULL
|| args
->length () == 0)
32484 cp_parser_error (parser
, "expected integer expression before ')'");
32486 release_tree_vector (args
);
32490 args_tree
= build_tree_list_vec (args
);
32492 release_tree_vector (args
);
32494 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32496 tree targ
= TREE_VALUE (t
);
32498 if (targ
!= error_mark_node
)
32500 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32501 error ("%<wait%> expression must be integral");
32504 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32506 targ
= mark_rvalue_use (targ
);
32507 OMP_CLAUSE_DECL (c
) = targ
;
32508 OMP_CLAUSE_CHAIN (c
) = list
;
32518 wait ( int-expr-list ) */
32521 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32523 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32525 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32528 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32534 collapse ( constant-expression ) */
32537 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32543 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32544 matching_parens parens
;
32545 if (!parens
.require_open (parser
))
32548 num
= cp_parser_constant_expression (parser
);
32550 if (!parens
.require_close (parser
))
32551 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32552 /*or_comma=*/false,
32553 /*consume_paren=*/true);
32555 if (num
== error_mark_node
)
32557 num
= fold_non_dependent_expr (num
);
32558 if (!tree_fits_shwi_p (num
)
32559 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32560 || (n
= tree_to_shwi (num
)) <= 0
32563 error_at (loc
, "collapse argument needs positive constant integer expression");
32567 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32568 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32569 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32570 OMP_CLAUSE_CHAIN (c
) = list
;
32571 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32577 default ( none | shared )
32580 default ( none | present ) */
32583 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32584 location_t location
, bool is_oacc
)
32586 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32589 matching_parens parens
;
32590 if (!parens
.require_open (parser
))
32592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32594 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32595 const char *p
= IDENTIFIER_POINTER (id
);
32600 if (strcmp ("none", p
) != 0)
32602 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32606 if (strcmp ("present", p
) != 0 || !is_oacc
)
32608 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32612 if (strcmp ("shared", p
) != 0 || is_oacc
)
32614 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32621 cp_lexer_consume_token (parser
->lexer
);
32627 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32629 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32632 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32633 || !parens
.require_close (parser
))
32634 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32635 /*or_comma=*/false,
32636 /*consume_paren=*/true);
32638 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32641 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32642 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32643 OMP_CLAUSE_CHAIN (c
) = list
;
32644 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32650 final ( expression ) */
32653 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32657 matching_parens parens
;
32658 if (!parens
.require_open (parser
))
32661 t
= cp_parser_assignment_expression (parser
);
32663 if (t
== error_mark_node
32664 || !parens
.require_close (parser
))
32665 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32666 /*or_comma=*/false,
32667 /*consume_paren=*/true);
32669 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32671 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32672 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32673 OMP_CLAUSE_CHAIN (c
) = list
;
32682 if ( directive-name-modifier : expression )
32684 directive-name-modifier:
32685 parallel | task | taskloop | target data | target | target update
32686 | target enter data | target exit data
32689 directive-name-modifier:
32690 ... | simd | cancel */
32693 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32697 enum tree_code if_modifier
= ERROR_MARK
;
32699 matching_parens parens
;
32700 if (!parens
.require_open (parser
))
32703 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32705 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32706 const char *p
= IDENTIFIER_POINTER (id
);
32709 if (strcmp ("cancel", p
) == 0)
32710 if_modifier
= VOID_CST
;
32711 else if (strcmp ("parallel", p
) == 0)
32712 if_modifier
= OMP_PARALLEL
;
32713 else if (strcmp ("simd", p
) == 0)
32714 if_modifier
= OMP_SIMD
;
32715 else if (strcmp ("task", p
) == 0)
32716 if_modifier
= OMP_TASK
;
32717 else if (strcmp ("taskloop", p
) == 0)
32718 if_modifier
= OMP_TASKLOOP
;
32719 else if (strcmp ("target", p
) == 0)
32721 if_modifier
= OMP_TARGET
;
32722 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32724 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32725 p
= IDENTIFIER_POINTER (id
);
32726 if (strcmp ("data", p
) == 0)
32727 if_modifier
= OMP_TARGET_DATA
;
32728 else if (strcmp ("update", p
) == 0)
32729 if_modifier
= OMP_TARGET_UPDATE
;
32730 else if (strcmp ("enter", p
) == 0)
32731 if_modifier
= OMP_TARGET_ENTER_DATA
;
32732 else if (strcmp ("exit", p
) == 0)
32733 if_modifier
= OMP_TARGET_EXIT_DATA
;
32734 if (if_modifier
!= OMP_TARGET
)
32739 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32740 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32742 if_modifier
= ERROR_MARK
;
32744 if (if_modifier
== OMP_TARGET_ENTER_DATA
32745 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32747 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32749 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32750 p
= IDENTIFIER_POINTER (id
);
32751 if (strcmp ("data", p
) == 0)
32757 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32758 error_at (loc
, "expected %<data%>");
32759 if_modifier
= ERROR_MARK
;
32764 if (if_modifier
!= ERROR_MARK
)
32766 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32769 cp_lexer_consume_token (parser
->lexer
);
32776 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32777 error_at (loc
, "expected %<:%>");
32779 if_modifier
= ERROR_MARK
;
32784 t
= cp_parser_assignment_expression (parser
);
32786 if (t
== error_mark_node
32787 || !parens
.require_close (parser
))
32788 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32789 /*or_comma=*/false,
32790 /*consume_paren=*/true);
32792 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32793 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32795 if (if_modifier
!= ERROR_MARK
32796 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32798 const char *p
= NULL
;
32799 switch (if_modifier
)
32801 case VOID_CST
: p
= "cancel"; break;
32802 case OMP_PARALLEL
: p
= "parallel"; break;
32803 case OMP_SIMD
: p
= "simd"; break;
32804 case OMP_TASK
: p
= "task"; break;
32805 case OMP_TASKLOOP
: p
= "taskloop"; break;
32806 case OMP_TARGET_DATA
: p
= "target data"; break;
32807 case OMP_TARGET
: p
= "target"; break;
32808 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32809 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32810 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32811 default: gcc_unreachable ();
32813 error_at (location
, "too many %<if%> clauses with %qs modifier",
32817 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32820 error_at (location
, "too many %<if%> clauses");
32822 error_at (location
, "too many %<if%> clauses without modifier");
32825 else if (if_modifier
== ERROR_MARK
32826 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32828 error_at (location
, "if any %<if%> clause has modifier, then all "
32829 "%<if%> clauses have to use modifier");
32834 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32835 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32836 OMP_CLAUSE_IF_EXPR (c
) = t
;
32837 OMP_CLAUSE_CHAIN (c
) = list
;
32846 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32847 tree list
, location_t location
)
32851 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32854 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32855 OMP_CLAUSE_CHAIN (c
) = list
;
32863 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32864 tree list
, location_t location
)
32868 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32870 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32871 OMP_CLAUSE_CHAIN (c
) = list
;
32876 num_threads ( expression ) */
32879 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32880 location_t location
)
32884 matching_parens parens
;
32885 if (!parens
.require_open (parser
))
32888 t
= cp_parser_assignment_expression (parser
);
32890 if (t
== error_mark_node
32891 || !parens
.require_close (parser
))
32892 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32893 /*or_comma=*/false,
32894 /*consume_paren=*/true);
32896 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32897 "num_threads", location
);
32899 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32900 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32901 OMP_CLAUSE_CHAIN (c
) = list
;
32907 num_tasks ( expression ) */
32910 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32911 location_t location
)
32915 matching_parens parens
;
32916 if (!parens
.require_open (parser
))
32919 t
= cp_parser_assignment_expression (parser
);
32921 if (t
== error_mark_node
32922 || !parens
.require_close (parser
))
32923 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32924 /*or_comma=*/false,
32925 /*consume_paren=*/true);
32927 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32928 "num_tasks", location
);
32930 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32931 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32932 OMP_CLAUSE_CHAIN (c
) = list
;
32938 grainsize ( expression ) */
32941 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32942 location_t location
)
32946 matching_parens parens
;
32947 if (!parens
.require_open (parser
))
32950 t
= cp_parser_assignment_expression (parser
);
32952 if (t
== error_mark_node
32953 || !parens
.require_close (parser
))
32954 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32955 /*or_comma=*/false,
32956 /*consume_paren=*/true);
32958 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32959 "grainsize", location
);
32961 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32962 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32963 OMP_CLAUSE_CHAIN (c
) = list
;
32969 priority ( expression ) */
32972 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32973 location_t location
)
32977 matching_parens parens
;
32978 if (!parens
.require_open (parser
))
32981 t
= cp_parser_assignment_expression (parser
);
32983 if (t
== error_mark_node
32984 || !parens
.require_close (parser
))
32985 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32986 /*or_comma=*/false,
32987 /*consume_paren=*/true);
32989 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32990 "priority", location
);
32992 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32993 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32994 OMP_CLAUSE_CHAIN (c
) = list
;
33000 hint ( expression ) */
33003 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33007 matching_parens parens
;
33008 if (!parens
.require_open (parser
))
33011 t
= cp_parser_assignment_expression (parser
);
33013 if (t
== error_mark_node
33014 || !parens
.require_close (parser
))
33015 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33016 /*or_comma=*/false,
33017 /*consume_paren=*/true);
33019 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33021 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33022 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33023 OMP_CLAUSE_CHAIN (c
) = list
;
33029 defaultmap ( tofrom : scalar )
33032 defaultmap ( implicit-behavior [ : variable-category ] ) */
33035 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33036 location_t location
)
33040 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33041 enum omp_clause_defaultmap_kind category
33042 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33044 matching_parens parens
;
33045 if (!parens
.require_open (parser
))
33048 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33050 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33053 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33054 "%<tofrom%>, %<firstprivate%>, %<none%> "
33060 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33061 p
= IDENTIFIER_POINTER (id
);
33067 if (strcmp ("alloc", p
) == 0)
33068 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33070 goto invalid_behavior
;
33074 if (strcmp ("default", p
) == 0)
33075 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33077 goto invalid_behavior
;
33081 if (strcmp ("firstprivate", p
) == 0)
33082 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33083 else if (strcmp ("from", p
) == 0)
33084 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33086 goto invalid_behavior
;
33090 if (strcmp ("none", p
) == 0)
33091 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33093 goto invalid_behavior
;
33097 if (strcmp ("tofrom", p
) == 0)
33098 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33099 else if (strcmp ("to", p
) == 0)
33100 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33102 goto invalid_behavior
;
33106 goto invalid_behavior
;
33108 cp_lexer_consume_token (parser
->lexer
);
33110 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33112 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33115 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33118 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33122 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33123 p
= IDENTIFIER_POINTER (id
);
33128 if (strcmp ("aggregate", p
) == 0)
33129 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33131 goto invalid_category
;
33135 if (strcmp ("pointer", p
) == 0)
33136 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33138 goto invalid_category
;
33142 if (strcmp ("scalar", p
) == 0)
33143 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33145 goto invalid_category
;
33149 goto invalid_category
;
33152 cp_lexer_consume_token (parser
->lexer
);
33154 if (!parens
.require_close (parser
))
33157 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33158 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33159 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33160 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33161 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33162 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33164 enum omp_clause_defaultmap_kind cat
= category
;
33165 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33166 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33167 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33171 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33174 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33177 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33180 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33184 gcc_unreachable ();
33187 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33190 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33195 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33196 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33197 OMP_CLAUSE_CHAIN (c
) = list
;
33201 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33202 /*or_comma=*/false,
33203 /*consume_paren=*/true);
33211 ordered ( constant-expression ) */
33214 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33215 tree list
, location_t location
)
33217 tree c
, num
= NULL_TREE
;
33220 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33221 "ordered", location
);
33223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33225 matching_parens parens
;
33226 parens
.consume_open (parser
);
33228 num
= cp_parser_constant_expression (parser
);
33230 if (!parens
.require_close (parser
))
33231 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33232 /*or_comma=*/false,
33233 /*consume_paren=*/true);
33235 if (num
== error_mark_node
)
33237 num
= fold_non_dependent_expr (num
);
33238 if (!tree_fits_shwi_p (num
)
33239 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33240 || (n
= tree_to_shwi (num
)) <= 0
33243 error_at (location
,
33244 "ordered argument needs positive constant integer "
33250 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33251 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33252 OMP_CLAUSE_CHAIN (c
) = list
;
33257 reduction ( reduction-operator : variable-list )
33259 reduction-operator:
33260 One of: + * - & ^ | && ||
33264 reduction-operator:
33265 One of: + * - & ^ | && || min max
33269 reduction-operator:
33270 One of: + * - & ^ | && ||
33274 reduction ( reduction-modifier, reduction-operator : variable-list )
33275 in_reduction ( reduction-operator : variable-list )
33276 task_reduction ( reduction-operator : variable-list ) */
33279 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33280 bool is_omp
, tree list
)
33282 enum tree_code code
= ERROR_MARK
;
33283 tree nlist
, c
, id
= NULL_TREE
;
33285 bool inscan
= false;
33287 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33290 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33292 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33293 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33295 cp_lexer_consume_token (parser
->lexer
);
33296 cp_lexer_consume_token (parser
->lexer
);
33298 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33299 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33301 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33302 const char *p
= IDENTIFIER_POINTER (id
);
33303 if (strcmp (p
, "task") == 0)
33305 else if (strcmp (p
, "inscan") == 0)
33308 sorry ("%<inscan%> modifier on %<reduction%> clause "
33309 "not supported yet");
33311 if (task
|| inscan
)
33313 cp_lexer_consume_token (parser
->lexer
);
33314 cp_lexer_consume_token (parser
->lexer
);
33319 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33321 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33322 case CPP_MULT
: code
= MULT_EXPR
; break;
33323 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33324 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33325 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33326 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33327 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33328 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33332 if (code
!= ERROR_MARK
)
33333 cp_lexer_consume_token (parser
->lexer
);
33336 bool saved_colon_corrects_to_scope_p
;
33337 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33338 parser
->colon_corrects_to_scope_p
= false;
33339 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33340 /*check_dependency_p=*/true,
33341 /*template_p=*/NULL
,
33342 /*declarator_p=*/false,
33343 /*optional_p=*/false);
33344 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33345 if (identifier_p (id
))
33347 const char *p
= IDENTIFIER_POINTER (id
);
33349 if (strcmp (p
, "min") == 0)
33351 else if (strcmp (p
, "max") == 0)
33353 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33355 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33357 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33359 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33360 code
= BIT_AND_EXPR
;
33361 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33362 code
= BIT_IOR_EXPR
;
33363 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33364 code
= BIT_XOR_EXPR
;
33365 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33366 code
= TRUTH_ANDIF_EXPR
;
33367 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33368 code
= TRUTH_ORIF_EXPR
;
33369 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33370 tree scope
= parser
->scope
;
33372 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33373 parser
->scope
= NULL_TREE
;
33374 parser
->qualifying_scope
= NULL_TREE
;
33375 parser
->object_scope
= NULL_TREE
;
33379 error ("invalid reduction-identifier");
33381 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33382 /*or_comma=*/false,
33383 /*consume_paren=*/true);
33388 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33391 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33393 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33395 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33397 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33399 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33400 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33407 schedule ( schedule-kind )
33408 schedule ( schedule-kind , expression )
33411 static | dynamic | guided | runtime | auto
33414 schedule ( schedule-modifier : schedule-kind )
33415 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33423 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33426 int modifiers
= 0, nmodifiers
= 0;
33428 matching_parens parens
;
33429 if (!parens
.require_open (parser
))
33432 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33434 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33436 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33437 const char *p
= IDENTIFIER_POINTER (id
);
33438 if (strcmp ("simd", p
) == 0)
33439 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33440 else if (strcmp ("monotonic", p
) == 0)
33441 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33442 else if (strcmp ("nonmonotonic", p
) == 0)
33443 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33446 cp_lexer_consume_token (parser
->lexer
);
33447 if (nmodifiers
++ == 0
33448 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33449 cp_lexer_consume_token (parser
->lexer
);
33452 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33459 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33460 const char *p
= IDENTIFIER_POINTER (id
);
33465 if (strcmp ("dynamic", p
) != 0)
33467 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33471 if (strcmp ("guided", p
) != 0)
33473 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33477 if (strcmp ("runtime", p
) != 0)
33479 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33486 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33487 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33488 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33489 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33492 cp_lexer_consume_token (parser
->lexer
);
33494 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33495 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33496 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33497 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33499 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33507 cp_lexer_consume_token (parser
->lexer
);
33509 token
= cp_lexer_peek_token (parser
->lexer
);
33510 t
= cp_parser_assignment_expression (parser
);
33512 if (t
== error_mark_node
)
33514 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33515 error_at (token
->location
, "schedule %<runtime%> does not take "
33516 "a %<chunk_size%> parameter");
33517 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33518 error_at (token
->location
, "schedule %<auto%> does not take "
33519 "a %<chunk_size%> parameter");
33521 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33523 if (!parens
.require_close (parser
))
33526 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33529 OMP_CLAUSE_SCHEDULE_KIND (c
)
33530 = (enum omp_clause_schedule_kind
)
33531 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33533 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33534 OMP_CLAUSE_CHAIN (c
) = list
;
33538 cp_parser_error (parser
, "invalid schedule kind");
33540 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33541 /*or_comma=*/false,
33542 /*consume_paren=*/true);
33550 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33551 tree list
, location_t location
)
33555 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33557 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33558 OMP_CLAUSE_CHAIN (c
) = list
;
33567 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33568 tree list
, location_t location
)
33570 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33571 tree c
= build_omp_clause (location
, code
);
33572 OMP_CLAUSE_CHAIN (c
) = list
;
33583 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33584 enum omp_clause_code code
,
33585 tree list
, location_t location
)
33587 tree c
= build_omp_clause (location
, code
);
33588 OMP_CLAUSE_CHAIN (c
) = list
;
33596 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33597 tree list
, location_t location
)
33599 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33600 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33601 OMP_CLAUSE_CHAIN (c
) = list
;
33610 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33611 enum omp_clause_code code
,
33612 tree list
, location_t location
)
33614 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33615 tree c
= build_omp_clause (location
, code
);
33616 OMP_CLAUSE_CHAIN (c
) = list
;
33621 num_teams ( expression ) */
33624 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33625 location_t location
)
33629 matching_parens parens
;
33630 if (!parens
.require_open (parser
))
33633 t
= cp_parser_assignment_expression (parser
);
33635 if (t
== error_mark_node
33636 || !parens
.require_close (parser
))
33637 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33638 /*or_comma=*/false,
33639 /*consume_paren=*/true);
33641 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33642 "num_teams", location
);
33644 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33645 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33646 OMP_CLAUSE_CHAIN (c
) = list
;
33652 thread_limit ( expression ) */
33655 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33656 location_t location
)
33660 matching_parens parens
;
33661 if (!parens
.require_open (parser
))
33664 t
= cp_parser_assignment_expression (parser
);
33666 if (t
== error_mark_node
33667 || !parens
.require_close (parser
))
33668 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33669 /*or_comma=*/false,
33670 /*consume_paren=*/true);
33672 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33673 "thread_limit", location
);
33675 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33676 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33677 OMP_CLAUSE_CHAIN (c
) = list
;
33683 aligned ( variable-list )
33684 aligned ( variable-list : constant-expression ) */
33687 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33689 tree nlist
, c
, alignment
= NULL_TREE
;
33692 matching_parens parens
;
33693 if (!parens
.require_open (parser
))
33696 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33701 alignment
= cp_parser_constant_expression (parser
);
33703 if (!parens
.require_close (parser
))
33704 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33705 /*or_comma=*/false,
33706 /*consume_paren=*/true);
33708 if (alignment
== error_mark_node
)
33709 alignment
= NULL_TREE
;
33712 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33713 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33719 lastprivate ( variable-list )
33722 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33725 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
33727 bool conditional
= false;
33729 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33733 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
33735 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33736 const char *p
= IDENTIFIER_POINTER (id
);
33738 if (strcmp ("conditional", p
) == 0)
33740 conditional
= true;
33741 cp_lexer_consume_token (parser
->lexer
);
33742 cp_lexer_consume_token (parser
->lexer
);
33746 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
33750 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33751 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
33756 linear ( variable-list )
33757 linear ( variable-list : expression )
33760 linear ( modifier ( variable-list ) )
33761 linear ( modifier ( variable-list ) : expression ) */
33764 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33767 tree nlist
, c
, step
= integer_one_node
;
33769 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33771 matching_parens parens
;
33772 if (!parens
.require_open (parser
))
33775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33777 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33778 const char *p
= IDENTIFIER_POINTER (id
);
33780 if (strcmp ("ref", p
) == 0)
33781 kind
= OMP_CLAUSE_LINEAR_REF
;
33782 else if (strcmp ("val", p
) == 0)
33783 kind
= OMP_CLAUSE_LINEAR_VAL
;
33784 else if (strcmp ("uval", p
) == 0)
33785 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33786 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33787 cp_lexer_consume_token (parser
->lexer
);
33789 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33792 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33793 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33797 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33798 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33800 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33801 else if (!parens
.require_close (parser
))
33802 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33803 /*or_comma=*/false,
33804 /*consume_paren=*/true);
33811 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33812 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33815 cp_parser_parse_tentatively (parser
);
33816 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33817 /*check_dependency_p=*/true,
33818 /*template_p=*/NULL
,
33819 /*declarator_p=*/false,
33820 /*optional_p=*/false);
33821 if (step
!= error_mark_node
)
33822 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33823 if (step
== error_mark_node
)
33826 cp_parser_abort_tentative_parse (parser
);
33828 else if (!cp_parser_parse_definitely (parser
))
33832 step
= cp_parser_assignment_expression (parser
);
33834 if (!parens
.require_close (parser
))
33835 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33836 /*or_comma=*/false,
33837 /*consume_paren=*/true);
33839 if (step
== error_mark_node
)
33843 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33845 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33846 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33853 safelen ( constant-expression ) */
33856 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33857 location_t location
)
33861 matching_parens parens
;
33862 if (!parens
.require_open (parser
))
33865 t
= cp_parser_constant_expression (parser
);
33867 if (t
== error_mark_node
33868 || !parens
.require_close (parser
))
33869 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33870 /*or_comma=*/false,
33871 /*consume_paren=*/true);
33873 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33875 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33876 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33877 OMP_CLAUSE_CHAIN (c
) = list
;
33883 simdlen ( constant-expression ) */
33886 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33887 location_t location
)
33891 matching_parens parens
;
33892 if (!parens
.require_open (parser
))
33895 t
= cp_parser_constant_expression (parser
);
33897 if (t
== error_mark_node
33898 || !parens
.require_close (parser
))
33899 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33900 /*or_comma=*/false,
33901 /*consume_paren=*/true);
33903 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33905 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33906 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33907 OMP_CLAUSE_CHAIN (c
) = list
;
33914 identifier [+/- integer]
33915 vec , identifier [+/- integer]
33919 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33924 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33926 cp_parser_error (parser
, "expected identifier");
33930 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33932 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33933 tree t
, identifier
= cp_parser_identifier (parser
);
33934 tree addend
= NULL
;
33936 if (identifier
== error_mark_node
)
33937 t
= error_mark_node
;
33940 t
= cp_parser_lookup_name_simple
33941 (parser
, identifier
,
33942 cp_lexer_peek_token (parser
->lexer
)->location
);
33943 if (t
== error_mark_node
)
33944 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33951 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33953 addend
= integer_zero_node
;
33954 goto add_to_vector
;
33956 cp_lexer_consume_token (parser
->lexer
);
33958 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33960 cp_parser_error (parser
, "expected integer");
33964 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33965 if (TREE_CODE (addend
) != INTEGER_CST
)
33967 cp_parser_error (parser
, "expected integer");
33970 cp_lexer_consume_token (parser
->lexer
);
33973 if (t
!= error_mark_node
)
33975 vec
= tree_cons (addend
, t
, vec
);
33977 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33980 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33983 cp_lexer_consume_token (parser
->lexer
);
33986 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33988 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33989 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33990 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33991 OMP_CLAUSE_CHAIN (u
) = list
;
33998 iterators ( iterators-definition )
34000 iterators-definition:
34002 iterator-specifier , iterators-definition
34004 iterator-specifier:
34005 identifier = range-specification
34006 iterator-type identifier = range-specification
34008 range-specification:
34010 begin : end : step */
34013 cp_parser_omp_iterators (cp_parser
*parser
)
34015 tree ret
= NULL_TREE
, *last
= &ret
;
34016 cp_lexer_consume_token (parser
->lexer
);
34018 matching_parens parens
;
34019 if (!parens
.require_open (parser
))
34020 return error_mark_node
;
34022 bool saved_colon_corrects_to_scope_p
34023 = parser
->colon_corrects_to_scope_p
;
34024 bool saved_colon_doesnt_start_class_def_p
34025 = parser
->colon_doesnt_start_class_def_p
;
34030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34031 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34032 iter_type
= integer_type_node
;
34035 const char *saved_message
34036 = parser
->type_definition_forbidden_message
;
34037 parser
->type_definition_forbidden_message
34038 = G_("types may not be defined in iterator type");
34040 iter_type
= cp_parser_type_id (parser
);
34042 parser
->type_definition_forbidden_message
= saved_message
;
34045 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34046 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34048 cp_parser_error (parser
, "expected identifier");
34052 tree id
= cp_parser_identifier (parser
);
34053 if (id
== error_mark_node
)
34056 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34059 parser
->colon_corrects_to_scope_p
= false;
34060 parser
->colon_doesnt_start_class_def_p
= true;
34061 tree begin
= cp_parser_assignment_expression (parser
);
34063 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34066 tree end
= cp_parser_assignment_expression (parser
);
34068 tree step
= integer_one_node
;
34069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34071 cp_lexer_consume_token (parser
->lexer
);
34072 step
= cp_parser_assignment_expression (parser
);
34075 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34076 DECL_ARTIFICIAL (iter_var
) = 1;
34077 DECL_CONTEXT (iter_var
) = current_function_decl
;
34078 pushdecl (iter_var
);
34080 *last
= make_tree_vec (6);
34081 TREE_VEC_ELT (*last
, 0) = iter_var
;
34082 TREE_VEC_ELT (*last
, 1) = begin
;
34083 TREE_VEC_ELT (*last
, 2) = end
;
34084 TREE_VEC_ELT (*last
, 3) = step
;
34085 last
= &TREE_CHAIN (*last
);
34087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34089 cp_lexer_consume_token (parser
->lexer
);
34096 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34097 parser
->colon_doesnt_start_class_def_p
34098 = saved_colon_doesnt_start_class_def_p
;
34100 if (!parens
.require_close (parser
))
34101 cp_parser_skip_to_closing_parenthesis (parser
,
34102 /*recovering=*/true,
34103 /*or_comma=*/false,
34104 /*consume_paren=*/true);
34106 return ret
? ret
: error_mark_node
;
34110 depend ( depend-kind : variable-list )
34118 depend ( sink : vec )
34121 depend ( depend-modifier , depend-kind: variable-list )
34124 in | out | inout | mutexinoutset | depobj
34127 iterator ( iterators-definition ) */
34130 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34132 tree nlist
, c
, iterators
= NULL_TREE
;
34133 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34135 matching_parens parens
;
34136 if (!parens
.require_open (parser
))
34141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34144 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34145 const char *p
= IDENTIFIER_POINTER (id
);
34147 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34149 begin_scope (sk_omp
, NULL
);
34150 iterators
= cp_parser_omp_iterators (parser
);
34151 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34154 if (strcmp ("in", p
) == 0)
34155 kind
= OMP_CLAUSE_DEPEND_IN
;
34156 else if (strcmp ("inout", p
) == 0)
34157 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34158 else if (strcmp ("mutexinoutset", p
) == 0)
34159 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34160 else if (strcmp ("out", p
) == 0)
34161 kind
= OMP_CLAUSE_DEPEND_OUT
;
34162 else if (strcmp ("depobj", p
) == 0)
34163 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34164 else if (strcmp ("sink", p
) == 0)
34165 kind
= OMP_CLAUSE_DEPEND_SINK
;
34166 else if (strcmp ("source", p
) == 0)
34167 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34174 cp_lexer_consume_token (parser
->lexer
);
34177 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34179 poplevel (0, 1, 0);
34180 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34181 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34182 iterators
= NULL_TREE
;
34185 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34187 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34188 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34189 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34190 OMP_CLAUSE_CHAIN (c
) = list
;
34191 if (!parens
.require_close (parser
))
34192 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34193 /*or_comma=*/false,
34194 /*consume_paren=*/true);
34198 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34201 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34202 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34205 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34210 tree block
= poplevel (1, 1, 0);
34211 if (iterators
== error_mark_node
)
34212 iterators
= NULL_TREE
;
34214 TREE_VEC_ELT (iterators
, 5) = block
;
34217 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34219 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34221 OMP_CLAUSE_DECL (c
)
34222 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34228 cp_parser_error (parser
, "invalid depend kind");
34231 poplevel (0, 1, 0);
34232 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34233 /*or_comma=*/false,
34234 /*consume_paren=*/true);
34239 map ( map-kind : variable-list )
34240 map ( variable-list )
34243 alloc | to | from | tofrom
34247 alloc | to | from | tofrom | release | delete
34249 map ( always [,] map-kind: variable-list ) */
34252 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34255 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34256 bool always
= false;
34258 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34263 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34264 const char *p
= IDENTIFIER_POINTER (id
);
34266 if (strcmp ("always", p
) == 0)
34269 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34271 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34272 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34274 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34278 cp_lexer_consume_token (parser
->lexer
);
34280 cp_lexer_consume_token (parser
->lexer
);
34285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34286 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34288 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34289 const char *p
= IDENTIFIER_POINTER (id
);
34291 if (strcmp ("alloc", p
) == 0)
34292 kind
= GOMP_MAP_ALLOC
;
34293 else if (strcmp ("to", p
) == 0)
34294 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34295 else if (strcmp ("from", p
) == 0)
34296 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34297 else if (strcmp ("tofrom", p
) == 0)
34298 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34299 else if (strcmp ("release", p
) == 0)
34300 kind
= GOMP_MAP_RELEASE
;
34303 cp_parser_error (parser
, "invalid map kind");
34304 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34305 /*or_comma=*/false,
34306 /*consume_paren=*/true);
34309 cp_lexer_consume_token (parser
->lexer
);
34310 cp_lexer_consume_token (parser
->lexer
);
34312 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34313 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34315 kind
= GOMP_MAP_DELETE
;
34316 cp_lexer_consume_token (parser
->lexer
);
34317 cp_lexer_consume_token (parser
->lexer
);
34320 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34323 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34324 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34330 device ( expression ) */
34333 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34334 location_t location
)
34338 matching_parens parens
;
34339 if (!parens
.require_open (parser
))
34342 t
= cp_parser_assignment_expression (parser
);
34344 if (t
== error_mark_node
34345 || !parens
.require_close (parser
))
34346 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34347 /*or_comma=*/false,
34348 /*consume_paren=*/true);
34350 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34351 "device", location
);
34353 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34354 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34355 OMP_CLAUSE_CHAIN (c
) = list
;
34361 dist_schedule ( static )
34362 dist_schedule ( static , expression ) */
34365 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34366 location_t location
)
34370 matching_parens parens
;
34371 if (!parens
.require_open (parser
))
34374 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34376 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34378 cp_lexer_consume_token (parser
->lexer
);
34380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34382 cp_lexer_consume_token (parser
->lexer
);
34384 t
= cp_parser_assignment_expression (parser
);
34386 if (t
== error_mark_node
)
34388 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34390 if (!parens
.require_close (parser
))
34393 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34396 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34398 OMP_CLAUSE_CHAIN (c
) = list
;
34402 cp_parser_error (parser
, "invalid dist_schedule kind");
34404 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34405 /*or_comma=*/false,
34406 /*consume_paren=*/true);
34411 proc_bind ( proc-bind-kind )
34414 master | close | spread */
34417 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34418 location_t location
)
34421 enum omp_clause_proc_bind_kind kind
;
34423 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34428 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34429 const char *p
= IDENTIFIER_POINTER (id
);
34431 if (strcmp ("master", p
) == 0)
34432 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34433 else if (strcmp ("close", p
) == 0)
34434 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34435 else if (strcmp ("spread", p
) == 0)
34436 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34443 cp_lexer_consume_token (parser
->lexer
);
34444 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34447 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34448 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34450 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34451 OMP_CLAUSE_CHAIN (c
) = list
;
34455 cp_parser_error (parser
, "invalid depend kind");
34457 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34458 /*or_comma=*/false,
34459 /*consume_paren=*/true);
34464 async [( int-expr )] */
34467 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34470 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34472 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34474 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34476 matching_parens parens
;
34477 parens
.consume_open (parser
);
34479 t
= cp_parser_expression (parser
);
34480 if (t
== error_mark_node
34481 || !parens
.require_close (parser
))
34482 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34483 /*or_comma=*/false,
34484 /*consume_paren=*/true);
34487 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34489 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34490 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34491 OMP_CLAUSE_CHAIN (c
) = list
;
34497 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34498 is a bitmask in MASK. Return the list of clauses found. */
34501 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34502 const char *where
, cp_token
*pragma_tok
,
34503 bool finish_p
= true)
34505 tree clauses
= NULL
;
34508 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34511 pragma_omp_clause c_kind
;
34512 omp_clause_code code
;
34513 const char *c_name
;
34514 tree prev
= clauses
;
34516 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34517 cp_lexer_consume_token (parser
->lexer
);
34519 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34520 c_kind
= cp_parser_omp_clause_name (parser
);
34524 case PRAGMA_OACC_CLAUSE_ASYNC
:
34525 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34528 case PRAGMA_OACC_CLAUSE_AUTO
:
34529 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34533 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34534 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34535 c_name
= "collapse";
34537 case PRAGMA_OACC_CLAUSE_COPY
:
34538 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34541 case PRAGMA_OACC_CLAUSE_COPYIN
:
34542 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34545 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34546 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34547 c_name
= "copyout";
34549 case PRAGMA_OACC_CLAUSE_CREATE
:
34550 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34553 case PRAGMA_OACC_CLAUSE_DELETE
:
34554 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34557 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34558 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34559 c_name
= "default";
34561 case PRAGMA_OACC_CLAUSE_DEVICE
:
34562 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34565 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34566 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34567 c_name
= "deviceptr";
34569 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34570 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34571 c_name
= "device_resident";
34573 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34574 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34576 c_name
= "finalize";
34578 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34579 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34581 c_name
= "firstprivate";
34583 case PRAGMA_OACC_CLAUSE_GANG
:
34585 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34588 case PRAGMA_OACC_CLAUSE_HOST
:
34589 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34592 case PRAGMA_OACC_CLAUSE_IF
:
34593 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34596 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34597 clauses
= cp_parser_oacc_simple_clause (parser
,
34598 OMP_CLAUSE_IF_PRESENT
,
34600 c_name
= "if_present";
34602 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34603 clauses
= cp_parser_oacc_simple_clause (parser
,
34604 OMP_CLAUSE_INDEPENDENT
,
34606 c_name
= "independent";
34608 case PRAGMA_OACC_CLAUSE_LINK
:
34609 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34612 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34613 code
= OMP_CLAUSE_NUM_GANGS
;
34614 c_name
= "num_gangs";
34615 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34618 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34619 c_name
= "num_workers";
34620 code
= OMP_CLAUSE_NUM_WORKERS
;
34621 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34624 case PRAGMA_OACC_CLAUSE_PRESENT
:
34625 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34626 c_name
= "present";
34628 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34629 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34631 c_name
= "private";
34633 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34635 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34637 c_name
= "reduction";
34639 case PRAGMA_OACC_CLAUSE_SEQ
:
34640 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34644 case PRAGMA_OACC_CLAUSE_TILE
:
34645 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34648 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34649 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34651 c_name
= "use_device";
34653 case PRAGMA_OACC_CLAUSE_VECTOR
:
34655 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34658 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34659 c_name
= "vector_length";
34660 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34661 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34664 case PRAGMA_OACC_CLAUSE_WAIT
:
34665 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34668 case PRAGMA_OACC_CLAUSE_WORKER
:
34670 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34674 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34680 if (((mask
>> c_kind
) & 1) == 0)
34682 /* Remove the invalid clause(s) from the list to avoid
34683 confusing the rest of the compiler. */
34685 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34690 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34693 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34698 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34699 is a bitmask in MASK. Return the list of clauses found; the result
34700 of clause default goes in *pdefault. */
34703 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34704 const char *where
, cp_token
*pragma_tok
,
34705 bool finish_p
= true)
34707 tree clauses
= NULL
;
34709 cp_token
*token
= NULL
;
34711 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34713 pragma_omp_clause c_kind
;
34714 const char *c_name
;
34715 tree prev
= clauses
;
34717 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34718 cp_lexer_consume_token (parser
->lexer
);
34720 token
= cp_lexer_peek_token (parser
->lexer
);
34721 c_kind
= cp_parser_omp_clause_name (parser
);
34725 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
34726 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
34728 c_name
= "collapse";
34730 case PRAGMA_OMP_CLAUSE_COPYIN
:
34731 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
34734 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
34735 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
34737 c_name
= "copyprivate";
34739 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34740 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
34741 token
->location
, false);
34742 c_name
= "default";
34744 case PRAGMA_OMP_CLAUSE_FINAL
:
34745 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
34748 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
34749 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34751 c_name
= "firstprivate";
34753 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34754 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34756 c_name
= "grainsize";
34758 case PRAGMA_OMP_CLAUSE_HINT
:
34759 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34763 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34764 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34766 c_name
= "defaultmap";
34768 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34769 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34771 c_name
= "use_device_ptr";
34773 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34774 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34776 c_name
= "is_device_ptr";
34778 case PRAGMA_OMP_CLAUSE_IF
:
34779 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34783 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
34785 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
34787 c_name
= "in_reduction";
34789 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34790 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
34791 c_name
= "lastprivate";
34793 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34794 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34796 c_name
= "mergeable";
34798 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34799 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34802 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34803 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34805 c_name
= "num_tasks";
34807 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34808 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34810 c_name
= "num_threads";
34812 case PRAGMA_OMP_CLAUSE_ORDERED
:
34813 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34815 c_name
= "ordered";
34817 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34818 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34820 c_name
= "priority";
34822 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34823 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34825 c_name
= "private";
34827 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34829 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34831 c_name
= "reduction";
34833 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34834 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34836 c_name
= "schedule";
34838 case PRAGMA_OMP_CLAUSE_SHARED
:
34839 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34843 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
34845 = cp_parser_omp_clause_reduction (parser
,
34846 OMP_CLAUSE_TASK_REDUCTION
,
34848 c_name
= "task_reduction";
34850 case PRAGMA_OMP_CLAUSE_UNTIED
:
34851 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34855 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34856 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34857 clauses
, token
->location
);
34858 c_name
= "inbranch";
34860 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
34861 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
34863 c_name
= "nontemporal";
34865 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34866 clauses
= cp_parser_omp_clause_branch (parser
,
34867 OMP_CLAUSE_NOTINBRANCH
,
34868 clauses
, token
->location
);
34869 c_name
= "notinbranch";
34871 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34872 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34873 clauses
, token
->location
);
34874 c_name
= "parallel";
34878 error_at (token
->location
, "%qs must be the first clause of %qs",
34883 case PRAGMA_OMP_CLAUSE_FOR
:
34884 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34885 clauses
, token
->location
);
34888 goto clause_not_first
;
34890 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34891 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34892 clauses
, token
->location
);
34893 c_name
= "sections";
34895 goto clause_not_first
;
34897 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34898 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34899 clauses
, token
->location
);
34900 c_name
= "taskgroup";
34902 goto clause_not_first
;
34904 case PRAGMA_OMP_CLAUSE_LINK
:
34905 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34908 case PRAGMA_OMP_CLAUSE_TO
:
34909 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34910 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34913 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34916 case PRAGMA_OMP_CLAUSE_FROM
:
34917 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34920 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34921 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34923 c_name
= "uniform";
34925 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34926 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34928 c_name
= "num_teams";
34930 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34931 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34933 c_name
= "thread_limit";
34935 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34936 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34937 c_name
= "aligned";
34939 case PRAGMA_OMP_CLAUSE_LINEAR
:
34941 bool declare_simd
= false;
34942 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34943 declare_simd
= true;
34944 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34948 case PRAGMA_OMP_CLAUSE_DEPEND
:
34949 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34953 case PRAGMA_OMP_CLAUSE_MAP
:
34954 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34957 case PRAGMA_OMP_CLAUSE_DEVICE
:
34958 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34962 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34963 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34965 c_name
= "dist_schedule";
34967 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34968 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34970 c_name
= "proc_bind";
34972 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34973 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34975 c_name
= "safelen";
34977 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34978 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34980 c_name
= "simdlen";
34982 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34983 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34985 c_name
= "nogroup";
34987 case PRAGMA_OMP_CLAUSE_THREADS
:
34989 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34990 clauses
, token
->location
);
34991 c_name
= "threads";
34993 case PRAGMA_OMP_CLAUSE_SIMD
:
34995 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34996 clauses
, token
->location
);
35000 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35006 if (((mask
>> c_kind
) & 1) == 0)
35008 /* Remove the invalid clause(s) from the list to avoid
35009 confusing the rest of the compiler. */
35011 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35015 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35018 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35019 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35021 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35030 In practice, we're also interested in adding the statement to an
35031 outer node. So it is convenient if we work around the fact that
35032 cp_parser_statement calls add_stmt. */
35035 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35037 unsigned save
= parser
->in_statement
;
35039 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35040 This preserves the "not within loop or switch" style error messages
35041 for nonsense cases like
35047 if (parser
->in_statement
)
35048 parser
->in_statement
= IN_OMP_BLOCK
;
35054 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35056 parser
->in_statement
= save
;
35060 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35062 tree stmt
= begin_omp_structured_block ();
35063 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35065 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35067 cp_parser_end_omp_structured_block (parser
, save
);
35068 return finish_omp_structured_block (stmt
);
35072 # pragma omp atomic new-line
35076 x binop= expr | x++ | ++x | x-- | --x
35078 +, *, -, /, &, ^, |, <<, >>
35080 where x is an lvalue expression with scalar type.
35083 # pragma omp atomic new-line
35086 # pragma omp atomic read new-line
35089 # pragma omp atomic write new-line
35092 # pragma omp atomic update new-line
35095 # pragma omp atomic capture new-line
35098 # pragma omp atomic capture new-line
35106 expression-stmt | x = x binop expr
35108 v = expression-stmt
35110 { v = x; update-stmt; } | { update-stmt; v = x; }
35114 expression-stmt | x = x binop expr | x = expr binop x
35118 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35120 where x and v are lvalue expressions with scalar type. */
35123 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35125 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35126 tree rhs1
= NULL_TREE
, orig_lhs
;
35127 location_t loc
= pragma_tok
->location
;
35128 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35129 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35130 bool structured_block
= false;
35132 tree clauses
= NULL_TREE
;
35134 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35136 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35137 cp_lexer_consume_token (parser
->lexer
);
35141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35143 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35144 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35145 const char *p
= IDENTIFIER_POINTER (id
);
35146 enum tree_code new_code
= ERROR_MARK
;
35147 enum omp_memory_order new_memory_order
35148 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35150 if (!strcmp (p
, "read"))
35151 new_code
= OMP_ATOMIC_READ
;
35152 else if (!strcmp (p
, "write"))
35153 new_code
= NOP_EXPR
;
35154 else if (!strcmp (p
, "update"))
35155 new_code
= OMP_ATOMIC
;
35156 else if (!strcmp (p
, "capture"))
35157 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35158 else if (!strcmp (p
, "seq_cst"))
35159 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35160 else if (!strcmp (p
, "acq_rel"))
35161 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35162 else if (!strcmp (p
, "release"))
35163 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35164 else if (!strcmp (p
, "acquire"))
35165 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35166 else if (!strcmp (p
, "relaxed"))
35167 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35168 else if (!strcmp (p
, "hint"))
35170 cp_lexer_consume_token (parser
->lexer
);
35171 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35177 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35178 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35179 "%<release%>, %<relaxed%> or %<hint%> clause");
35183 if (new_code
!= ERROR_MARK
)
35185 if (code
!= ERROR_MARK
)
35186 error_at (cloc
, "too many atomic clauses");
35190 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35192 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35193 error_at (cloc
, "too many memory order clauses");
35195 memory_order
= new_memory_order
;
35197 cp_lexer_consume_token (parser
->lexer
);
35203 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35205 if (code
== ERROR_MARK
)
35207 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35210 = (enum omp_requires
) (omp_requires_mask
35211 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35212 switch ((enum omp_memory_order
)
35213 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35215 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35216 case OMP_MEMORY_ORDER_RELAXED
:
35217 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35219 case OMP_MEMORY_ORDER_SEQ_CST
:
35220 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35222 case OMP_MEMORY_ORDER_ACQ_REL
:
35225 case OMP_ATOMIC_READ
:
35226 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35228 case NOP_EXPR
: /* atomic write */
35230 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35233 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35238 gcc_unreachable ();
35244 case OMP_ATOMIC_READ
:
35245 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35246 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35248 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35249 "%<acq_rel%> or %<release%> clauses");
35250 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35253 case NOP_EXPR
: /* atomic write */
35254 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35255 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35257 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35258 "%<acq_rel%> or %<acquire%> clauses");
35259 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35263 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35264 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35266 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35267 "%<acq_rel%> or %<acquire%> clauses");
35268 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35277 case OMP_ATOMIC_READ
:
35278 case NOP_EXPR
: /* atomic write */
35279 v
= cp_parser_unary_expression (parser
);
35280 if (v
== error_mark_node
)
35282 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35284 if (code
== NOP_EXPR
)
35285 lhs
= cp_parser_expression (parser
);
35287 lhs
= cp_parser_unary_expression (parser
);
35288 if (lhs
== error_mark_node
)
35290 if (code
== NOP_EXPR
)
35292 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35300 case OMP_ATOMIC_CAPTURE_NEW
:
35301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35303 cp_lexer_consume_token (parser
->lexer
);
35304 structured_block
= true;
35308 v
= cp_parser_unary_expression (parser
);
35309 if (v
== error_mark_node
)
35311 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35319 lhs
= cp_parser_unary_expression (parser
);
35321 switch (TREE_CODE (lhs
))
35326 case POSTINCREMENT_EXPR
:
35327 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35328 code
= OMP_ATOMIC_CAPTURE_OLD
;
35330 case PREINCREMENT_EXPR
:
35331 lhs
= TREE_OPERAND (lhs
, 0);
35332 opcode
= PLUS_EXPR
;
35333 rhs
= integer_one_node
;
35336 case POSTDECREMENT_EXPR
:
35337 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35338 code
= OMP_ATOMIC_CAPTURE_OLD
;
35340 case PREDECREMENT_EXPR
:
35341 lhs
= TREE_OPERAND (lhs
, 0);
35342 opcode
= MINUS_EXPR
;
35343 rhs
= integer_one_node
;
35346 case COMPOUND_EXPR
:
35347 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35348 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35349 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35350 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35351 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35352 (TREE_OPERAND (lhs
, 1), 0), 0)))
35354 /* Undo effects of boolean_increment for post {in,de}crement. */
35355 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35358 if (TREE_CODE (lhs
) == MODIFY_EXPR
35359 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35361 /* Undo effects of boolean_increment. */
35362 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35364 /* This is pre or post increment. */
35365 rhs
= TREE_OPERAND (lhs
, 1);
35366 lhs
= TREE_OPERAND (lhs
, 0);
35368 if (code
== OMP_ATOMIC_CAPTURE_NEW
35369 && !structured_block
35370 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35371 code
= OMP_ATOMIC_CAPTURE_OLD
;
35377 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35380 opcode
= MULT_EXPR
;
35383 opcode
= TRUNC_DIV_EXPR
;
35386 opcode
= PLUS_EXPR
;
35389 opcode
= MINUS_EXPR
;
35391 case CPP_LSHIFT_EQ
:
35392 opcode
= LSHIFT_EXPR
;
35394 case CPP_RSHIFT_EQ
:
35395 opcode
= RSHIFT_EXPR
;
35398 opcode
= BIT_AND_EXPR
;
35401 opcode
= BIT_IOR_EXPR
;
35404 opcode
= BIT_XOR_EXPR
;
35407 enum cp_parser_prec oprec
;
35409 cp_lexer_consume_token (parser
->lexer
);
35410 cp_parser_parse_tentatively (parser
);
35411 rhs1
= cp_parser_simple_cast_expression (parser
);
35412 if (rhs1
== error_mark_node
)
35414 cp_parser_abort_tentative_parse (parser
);
35415 cp_parser_simple_cast_expression (parser
);
35418 token
= cp_lexer_peek_token (parser
->lexer
);
35419 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35421 cp_parser_abort_tentative_parse (parser
);
35422 cp_parser_parse_tentatively (parser
);
35423 rhs
= cp_parser_binary_expression (parser
, false, true,
35424 PREC_NOT_OPERATOR
, NULL
);
35425 if (rhs
== error_mark_node
)
35427 cp_parser_abort_tentative_parse (parser
);
35428 cp_parser_binary_expression (parser
, false, true,
35429 PREC_NOT_OPERATOR
, NULL
);
35432 switch (TREE_CODE (rhs
))
35435 case TRUNC_DIV_EXPR
:
35444 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35446 if (cp_parser_parse_definitely (parser
))
35448 opcode
= TREE_CODE (rhs
);
35449 rhs1
= TREE_OPERAND (rhs
, 0);
35450 rhs
= TREE_OPERAND (rhs
, 1);
35460 cp_parser_abort_tentative_parse (parser
);
35461 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35463 rhs
= cp_parser_expression (parser
);
35464 if (rhs
== error_mark_node
)
35470 cp_parser_error (parser
,
35471 "invalid form of %<#pragma omp atomic%>");
35474 if (!cp_parser_parse_definitely (parser
))
35476 switch (token
->type
)
35478 case CPP_SEMICOLON
:
35479 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35481 code
= OMP_ATOMIC_CAPTURE_OLD
;
35486 cp_lexer_consume_token (parser
->lexer
);
35489 else if (structured_block
)
35496 cp_parser_error (parser
,
35497 "invalid form of %<#pragma omp atomic%>");
35500 opcode
= MULT_EXPR
;
35503 opcode
= TRUNC_DIV_EXPR
;
35506 opcode
= PLUS_EXPR
;
35509 opcode
= MINUS_EXPR
;
35512 opcode
= LSHIFT_EXPR
;
35515 opcode
= RSHIFT_EXPR
;
35518 opcode
= BIT_AND_EXPR
;
35521 opcode
= BIT_IOR_EXPR
;
35524 opcode
= BIT_XOR_EXPR
;
35527 cp_parser_error (parser
,
35528 "invalid operator for %<#pragma omp atomic%>");
35531 oprec
= TOKEN_PRECEDENCE (token
);
35532 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35533 if (commutative_tree_code (opcode
))
35534 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35535 cp_lexer_consume_token (parser
->lexer
);
35536 rhs
= cp_parser_binary_expression (parser
, false, false,
35538 if (rhs
== error_mark_node
)
35543 cp_parser_error (parser
,
35544 "invalid operator for %<#pragma omp atomic%>");
35547 cp_lexer_consume_token (parser
->lexer
);
35549 rhs
= cp_parser_expression (parser
);
35550 if (rhs
== error_mark_node
)
35555 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35557 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35559 v
= cp_parser_unary_expression (parser
);
35560 if (v
== error_mark_node
)
35562 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35564 lhs1
= cp_parser_unary_expression (parser
);
35565 if (lhs1
== error_mark_node
)
35568 if (structured_block
)
35570 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35571 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35574 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35575 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35576 rhs1
, clauses
, memory_order
);
35577 if (!structured_block
)
35578 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35582 cp_parser_skip_to_end_of_block_or_statement (parser
);
35583 if (structured_block
)
35585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35586 cp_lexer_consume_token (parser
->lexer
);
35587 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35589 cp_parser_skip_to_end_of_block_or_statement (parser
);
35590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35591 cp_lexer_consume_token (parser
->lexer
);
35598 # pragma omp barrier new-line */
35601 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35603 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35604 finish_omp_barrier ();
35608 # pragma omp critical [(name)] new-line
35612 # pragma omp critical [(name) [hint(expression)]] new-line
35613 structured-block */
35615 #define OMP_CRITICAL_CLAUSE_MASK \
35616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35619 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35621 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35625 matching_parens parens
;
35626 parens
.consume_open (parser
);
35628 name
= cp_parser_identifier (parser
);
35630 if (name
== error_mark_node
35631 || !parens
.require_close (parser
))
35632 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35633 /*or_comma=*/false,
35634 /*consume_paren=*/true);
35635 if (name
== error_mark_node
)
35638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35639 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35640 cp_lexer_consume_token (parser
->lexer
);
35642 clauses
= cp_parser_omp_all_clauses (parser
,
35643 OMP_CRITICAL_CLAUSE_MASK
,
35644 "#pragma omp critical", pragma_tok
);
35647 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35649 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35650 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35654 # pragma omp depobj ( depobj ) depobj-clause new-line
35657 depend (dependence-type : locator)
35659 update (dependence-type)
35668 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
35670 location_t loc
= pragma_tok
->location
;
35671 matching_parens parens
;
35672 if (!parens
.require_open (parser
))
35674 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35678 tree depobj
= cp_parser_assignment_expression (parser
);
35680 if (!parens
.require_close (parser
))
35681 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35682 /*or_comma=*/false,
35683 /*consume_paren=*/true);
35685 tree clause
= NULL_TREE
;
35686 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35687 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35690 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35691 const char *p
= IDENTIFIER_POINTER (id
);
35693 cp_lexer_consume_token (parser
->lexer
);
35694 if (!strcmp ("depend", p
))
35696 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
35698 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
35700 clause
= error_mark_node
;
35702 else if (!strcmp ("destroy", p
))
35703 kind
= OMP_CLAUSE_DEPEND_LAST
;
35704 else if (!strcmp ("update", p
))
35706 matching_parens c_parens
;
35707 if (c_parens
.require_open (parser
))
35710 = cp_lexer_peek_token (parser
->lexer
)->location
;
35711 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35713 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35714 const char *p2
= IDENTIFIER_POINTER (id2
);
35716 cp_lexer_consume_token (parser
->lexer
);
35717 if (!strcmp ("in", p2
))
35718 kind
= OMP_CLAUSE_DEPEND_IN
;
35719 else if (!strcmp ("out", p2
))
35720 kind
= OMP_CLAUSE_DEPEND_OUT
;
35721 else if (!strcmp ("inout", p2
))
35722 kind
= OMP_CLAUSE_DEPEND_INOUT
;
35723 else if (!strcmp ("mutexinoutset", p2
))
35724 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
35726 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35728 clause
= error_mark_node
;
35729 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
35730 "%<mutexinoutset%>");
35732 if (!c_parens
.require_close (parser
))
35733 cp_parser_skip_to_closing_parenthesis (parser
,
35734 /*recovering=*/true,
35735 /*or_comma=*/false,
35736 /*consume_paren=*/true);
35739 clause
= error_mark_node
;
35742 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35744 clause
= error_mark_node
;
35745 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
35747 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35749 finish_omp_depobj (loc
, depobj
, kind
, clause
);
35754 # pragma omp flush flush-vars[opt] new-line
35760 # pragma omp flush memory-order-clause new-line */
35763 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
35765 enum memmodel mo
= MEMMODEL_LAST
;
35766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35768 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35769 const char *p
= IDENTIFIER_POINTER (id
);
35770 if (!strcmp (p
, "acq_rel"))
35771 mo
= MEMMODEL_ACQ_REL
;
35772 else if (!strcmp (p
, "release"))
35773 mo
= MEMMODEL_RELEASE
;
35774 else if (!strcmp (p
, "acquire"))
35775 mo
= MEMMODEL_ACQUIRE
;
35777 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35778 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35779 cp_lexer_consume_token (parser
->lexer
);
35781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35783 if (mo
!= MEMMODEL_LAST
)
35784 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35785 "%<flush%> list specified together with memory order "
35787 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35789 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35791 finish_omp_flush (mo
);
35794 /* Helper function, to parse omp for increment expression. */
35797 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
35799 tree cond
= cp_parser_binary_expression (parser
, false, true,
35800 PREC_NOT_OPERATOR
, NULL
);
35801 if (cond
== error_mark_node
35802 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35804 cp_parser_skip_to_end_of_statement (parser
);
35805 return error_mark_node
;
35808 switch (TREE_CODE (cond
))
35816 if (code
!= OACC_LOOP
)
35818 gcc_fallthrough ();
35820 return error_mark_node
;
35823 /* If decl is an iterator, preserve LHS and RHS of the relational
35824 expr until finish_omp_for. */
35826 && (type_dependent_expression_p (decl
)
35827 || CLASS_TYPE_P (TREE_TYPE (decl
))))
35830 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
35832 TREE_OPERAND (cond
, 0), ERROR_MARK
,
35833 TREE_OPERAND (cond
, 1), ERROR_MARK
,
35834 /*overload=*/NULL
, tf_warning_or_error
);
35837 /* Helper function, to parse omp for increment expression. */
35840 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
35842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35848 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35850 op
= (token
->type
== CPP_PLUS_PLUS
35851 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
35852 cp_lexer_consume_token (parser
->lexer
);
35853 lhs
= cp_parser_simple_cast_expression (parser
);
35855 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35856 return error_mark_node
;
35857 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35860 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
35862 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35863 return error_mark_node
;
35865 token
= cp_lexer_peek_token (parser
->lexer
);
35866 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35868 op
= (token
->type
== CPP_PLUS_PLUS
35869 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
35870 cp_lexer_consume_token (parser
->lexer
);
35871 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35874 op
= cp_parser_assignment_operator_opt (parser
);
35875 if (op
== ERROR_MARK
)
35876 return error_mark_node
;
35878 if (op
!= NOP_EXPR
)
35880 rhs
= cp_parser_assignment_expression (parser
);
35881 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
35882 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35885 lhs
= cp_parser_binary_expression (parser
, false, false,
35886 PREC_ADDITIVE_EXPRESSION
, NULL
);
35887 token
= cp_lexer_peek_token (parser
->lexer
);
35888 decl_first
= (lhs
== decl
35889 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
35892 if (token
->type
!= CPP_PLUS
35893 && token
->type
!= CPP_MINUS
)
35894 return error_mark_node
;
35898 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
35899 cp_lexer_consume_token (parser
->lexer
);
35900 rhs
= cp_parser_binary_expression (parser
, false, false,
35901 PREC_ADDITIVE_EXPRESSION
, NULL
);
35902 token
= cp_lexer_peek_token (parser
->lexer
);
35903 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
35905 if (lhs
== NULL_TREE
)
35907 if (op
== PLUS_EXPR
)
35910 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
35911 tf_warning_or_error
);
35914 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
35915 ERROR_MARK
, NULL
, tf_warning_or_error
);
35918 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
35923 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
35924 || op
== MINUS_EXPR
)
35925 return error_mark_node
;
35926 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
35929 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
35931 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35934 /* Parse the initialization statement of an OpenMP for loop.
35936 Return true if the resulting construct should have an
35937 OMP_CLAUSE_PRIVATE added to it. */
35940 cp_parser_omp_for_loop_init (cp_parser
*parser
,
35941 tree
&this_pre_body
,
35942 vec
<tree
, va_gc
> *&for_block
,
35948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35951 tree add_private_clause
= NULL_TREE
;
35953 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35957 integer-type var = lb
35958 random-access-iterator-type var = lb
35959 pointer-type var = lb
35961 cp_decl_specifier_seq type_specifiers
;
35963 /* First, try to parse as an initialized declaration. See
35964 cp_parser_condition, from whence the bulk of this is copied. */
35966 cp_parser_parse_tentatively (parser
);
35967 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
35968 /*is_trailing_return=*/false,
35970 if (cp_parser_parse_definitely (parser
))
35972 /* If parsing a type specifier seq succeeded, then this
35973 MUST be a initialized declaration. */
35974 tree asm_specification
, attributes
;
35975 cp_declarator
*declarator
;
35977 declarator
= cp_parser_declarator (parser
,
35978 CP_PARSER_DECLARATOR_NAMED
,
35979 /*ctor_dtor_or_conv_p=*/NULL
,
35980 /*parenthesized_p=*/NULL
,
35981 /*member_p=*/false,
35982 /*friend_p=*/false);
35983 attributes
= cp_parser_attributes_opt (parser
);
35984 asm_specification
= cp_parser_asm_specification_opt (parser
);
35986 if (declarator
== cp_error_declarator
)
35987 cp_parser_skip_to_end_of_statement (parser
);
35991 tree pushed_scope
, auto_node
;
35993 decl
= start_decl (declarator
, &type_specifiers
,
35994 SD_INITIALIZED
, attributes
,
35995 /*prefix_attributes=*/NULL_TREE
,
35998 auto_node
= type_uses_auto (TREE_TYPE (decl
));
35999 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36001 if (cp_lexer_next_token_is (parser
->lexer
,
36003 error ("parenthesized initialization is not allowed in "
36004 "OpenMP %<for%> loop");
36006 /* Trigger an error. */
36007 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36009 init
= error_mark_node
;
36010 cp_parser_skip_to_end_of_statement (parser
);
36012 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36013 || type_dependent_expression_p (decl
)
36016 bool is_direct_init
, is_non_constant_init
;
36018 init
= cp_parser_initializer (parser
,
36020 &is_non_constant_init
);
36025 = do_auto_deduction (TREE_TYPE (decl
), init
,
36028 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36029 && !type_dependent_expression_p (decl
))
36033 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36035 LOOKUP_ONLYCONVERTING
);
36037 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36039 vec_safe_push (for_block
, this_pre_body
);
36044 init
= pop_stmt_list (this_pre_body
);
36045 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36047 tree_stmt_iterator i
= tsi_start (init
);
36048 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36049 while (!tsi_end_p (i
))
36051 tree t
= tsi_stmt (i
);
36052 if (TREE_CODE (t
) == DECL_EXPR
36053 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36056 vec_safe_push (for_block
, t
);
36061 if (tsi_one_before_end_p (i
))
36063 tree t
= tsi_stmt (i
);
36065 free_stmt_list (init
);
36070 this_pre_body
= NULL_TREE
;
36075 cp_lexer_consume_token (parser
->lexer
);
36076 init
= cp_parser_assignment_expression (parser
);
36079 if (TYPE_REF_P (TREE_TYPE (decl
)))
36080 init
= error_mark_node
;
36082 cp_finish_decl (decl
, NULL_TREE
,
36083 /*init_const_expr_p=*/false,
36085 LOOKUP_ONLYCONVERTING
);
36089 pop_scope (pushed_scope
);
36095 /* If parsing a type specifier sequence failed, then
36096 this MUST be a simple expression. */
36097 cp_parser_parse_tentatively (parser
);
36098 decl
= cp_parser_primary_expression (parser
, false, false,
36100 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36101 if (!cp_parser_error_occurred (parser
)
36103 && (TREE_CODE (decl
) == COMPONENT_REF
36104 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36106 cp_parser_abort_tentative_parse (parser
);
36107 cp_parser_parse_tentatively (parser
);
36108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36109 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36110 /*check_dependency_p=*/true,
36111 /*template_p=*/NULL
,
36112 /*declarator_p=*/false,
36113 /*optional_p=*/false);
36114 if (name
!= error_mark_node
36115 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36117 decl
= cp_parser_lookup_name_simple (parser
, name
,
36119 if (TREE_CODE (decl
) == FIELD_DECL
)
36120 add_private_clause
= omp_privatize_field (decl
, false);
36122 cp_parser_abort_tentative_parse (parser
);
36123 cp_parser_parse_tentatively (parser
);
36124 decl
= cp_parser_primary_expression (parser
, false, false,
36127 if (!cp_parser_error_occurred (parser
)
36130 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36134 cp_parser_parse_definitely (parser
);
36135 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36136 rhs
= cp_parser_assignment_expression (parser
);
36138 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36141 tf_warning_or_error
));
36142 if (!add_private_clause
)
36143 add_private_clause
= decl
;
36148 cp_parser_abort_tentative_parse (parser
);
36149 init
= cp_parser_expression (parser
);
36152 if (TREE_CODE (init
) == MODIFY_EXPR
36153 || TREE_CODE (init
) == MODOP_EXPR
)
36154 real_decl
= TREE_OPERAND (init
, 0);
36158 return add_private_clause
;
36161 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36164 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36165 tree
&decl
, tree
&orig_decl
, tree
&init
,
36166 tree
&orig_init
, tree
&cond
, tree
&incr
)
36168 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36169 tree iter_type
, begin_expr
, end_expr
;
36171 if (processing_template_decl
)
36173 if (check_for_bare_parameter_packs (init
))
36174 init
= error_mark_node
;
36175 if (!type_dependent_expression_p (init
)
36176 /* do_auto_deduction doesn't mess with template init-lists. */
36177 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36180 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36182 tree v
= DECL_VALUE_EXPR (decl
);
36183 if (TREE_CODE (v
) == ARRAY_REF
36184 && VAR_P (TREE_OPERAND (v
, 0))
36185 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36186 d
= TREE_OPERAND (v
, 0);
36188 do_range_for_auto_deduction (d
, init
);
36190 cond
= global_namespace
;
36194 this_pre_body
= pop_stmt_list (this_pre_body
);
36198 init
= mark_lvalue_use (init
);
36200 if (decl
== error_mark_node
|| init
== error_mark_node
)
36201 /* If an error happened previously do nothing or else a lot of
36202 unhelpful errors would be issued. */
36203 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36209 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36210 /* Can't bind a reference to an array of runtime bound. */
36214 range_temp
= build_range_temp (init
);
36215 DECL_NAME (range_temp
) = NULL_TREE
;
36216 pushdecl (range_temp
);
36217 cp_finish_decl (range_temp
, init
,
36218 /*is_constant_init*/false, NULL_TREE
,
36219 LOOKUP_ONLYCONVERTING
);
36220 range_temp_decl
= range_temp
;
36221 range_temp
= convert_from_reference (range_temp
);
36223 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36224 &begin_expr
, &end_expr
);
36227 tree end_iter_type
= iter_type
;
36228 if (cxx_dialect
>= cxx17
)
36229 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36230 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36231 TREE_USED (end
) = 1;
36232 DECL_ARTIFICIAL (end
) = 1;
36234 cp_finish_decl (end
, end_expr
,
36235 /*is_constant_init*/false, NULL_TREE
,
36236 LOOKUP_ONLYCONVERTING
);
36238 /* The new for initialization statement. */
36239 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36240 TREE_USED (begin
) = 1;
36241 DECL_ARTIFICIAL (begin
) = 1;
36244 if (CLASS_TYPE_P (iter_type
))
36249 begin_expr
= NULL_TREE
;
36251 cp_finish_decl (begin
, begin_expr
,
36252 /*is_constant_init*/false, NULL_TREE
,
36253 LOOKUP_ONLYCONVERTING
);
36255 /* The new for condition. */
36256 if (CLASS_TYPE_P (iter_type
))
36257 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36259 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36262 NULL
, tf_warning_or_error
);
36264 /* The new increment expression. */
36265 if (CLASS_TYPE_P (iter_type
))
36266 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36268 incr
= finish_unary_op_expr (input_location
,
36269 PREINCREMENT_EXPR
, begin
,
36270 tf_warning_or_error
);
36276 vec_safe_push (for_block
, this_pre_body
);
36277 this_pre_body
= NULL_TREE
;
36280 tree decomp_first_name
= NULL_TREE
;
36281 unsigned decomp_cnt
= 0;
36282 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36284 tree v
= DECL_VALUE_EXPR (orig_decl
);
36285 if (TREE_CODE (v
) == ARRAY_REF
36286 && VAR_P (TREE_OPERAND (v
, 0))
36287 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36289 tree d
= orig_decl
;
36290 orig_decl
= TREE_OPERAND (v
, 0);
36291 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36292 decomp_first_name
= d
;
36296 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36299 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36301 if (!error_operand_p (t
))
36302 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36306 tree v
= make_tree_vec (decomp_cnt
+ 3);
36307 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36308 TREE_VEC_ELT (v
, 1) = end
;
36309 TREE_VEC_ELT (v
, 2) = orig_decl
;
36310 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36312 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36313 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36315 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36318 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36319 inside of the collapsed body. */
36322 cp_finish_omp_range_for (tree orig
, tree begin
)
36324 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36325 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36326 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36327 tree decomp_first_name
= NULL_TREE
;
36328 unsigned int decomp_cnt
= 0;
36330 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36332 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36333 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36334 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36337 /* The declaration is initialized with *__begin inside the loop body. */
36338 cp_finish_decl (decl
,
36339 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36340 tf_warning_or_error
),
36341 /*is_constant_init*/false, NULL_TREE
,
36342 LOOKUP_ONLYCONVERTING
);
36343 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36344 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36347 /* Parse the restricted form of the for statement allowed by OpenMP. */
36350 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36351 tree
*cclauses
, bool *if_p
)
36353 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36355 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36356 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36357 location_t loc_first
;
36358 bool collapse_err
= false;
36359 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36360 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36361 auto_vec
<tree
, 4> orig_inits
;
36362 bool tiling
= false;
36364 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36365 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36366 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36367 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36370 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36372 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36373 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36376 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36379 if (ordered
&& ordered
< collapse
)
36381 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36382 "%<ordered%> clause parameter is less than %<collapse%>");
36383 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36384 = build_int_cst (NULL_TREE
, collapse
);
36385 ordered
= collapse
;
36389 for (tree
*pc
= &clauses
; *pc
; )
36390 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36392 error_at (OMP_CLAUSE_LOCATION (*pc
),
36393 "%<linear%> clause may not be specified together "
36394 "with %<ordered%> clause with a parameter");
36395 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36398 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36401 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36402 count
= ordered
? ordered
: collapse
;
36404 declv
= make_tree_vec (count
);
36405 initv
= make_tree_vec (count
);
36406 condv
= make_tree_vec (count
);
36407 incrv
= make_tree_vec (count
);
36408 orig_declv
= NULL_TREE
;
36410 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36412 for (i
= 0; i
< count
; i
++)
36414 int bracecount
= 0;
36415 tree add_private_clause
= NULL_TREE
;
36418 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36421 cp_parser_error (parser
, "for statement expected");
36424 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36426 matching_parens parens
;
36427 if (!parens
.require_open (parser
))
36430 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36431 this_pre_body
= push_stmt_list ();
36433 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36435 /* Save tokens so that we can put them back. */
36436 cp_lexer_save_tokens (parser
->lexer
);
36438 /* Look for ':' that is not nested in () or {}. */
36440 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36441 /*recovering=*/false,
36446 /* Roll back the tokens we skipped. */
36447 cp_lexer_rollback_tokens (parser
->lexer
);
36451 bool saved_colon_corrects_to_scope_p
36452 = parser
->colon_corrects_to_scope_p
;
36454 /* A colon is used in range-based for. */
36455 parser
->colon_corrects_to_scope_p
= false;
36457 /* Parse the declaration. */
36458 cp_parser_simple_declaration (parser
,
36459 /*function_definition_allowed_p=*/
36461 parser
->colon_corrects_to_scope_p
36462 = saved_colon_corrects_to_scope_p
;
36464 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36466 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36469 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36470 orig_decl
, init
, orig_init
,
36477 pre_body
= push_stmt_list ();
36479 add_stmt (this_pre_body
);
36480 pre_body
= pop_stmt_list (pre_body
);
36483 pre_body
= this_pre_body
;
36487 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36488 "%<ordered%> clause with parameter on "
36489 "range-based %<for%> loop");
36491 goto parse_close_paren
;
36496 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36497 init
, orig_init
, decl
, real_decl
);
36499 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36502 this_pre_body
= pop_stmt_list (this_pre_body
);
36506 pre_body
= push_stmt_list ();
36508 add_stmt (this_pre_body
);
36509 pre_body
= pop_stmt_list (pre_body
);
36512 pre_body
= this_pre_body
;
36517 if (cclauses
!= NULL
36518 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36519 && real_decl
!= NULL_TREE
)
36522 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36523 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36524 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36526 error_at (loc
, "iteration variable %qD"
36527 " should not be firstprivate", real_decl
);
36528 *c
= OMP_CLAUSE_CHAIN (*c
);
36530 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36531 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36533 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36535 *c
= OMP_CLAUSE_CHAIN (*c
);
36536 if (code
== OMP_SIMD
)
36538 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36539 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36543 OMP_CLAUSE_CHAIN (l
) = clauses
;
36546 add_private_clause
= NULL_TREE
;
36550 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36551 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36552 add_private_clause
= NULL_TREE
;
36553 c
= &OMP_CLAUSE_CHAIN (*c
);
36557 if (add_private_clause
)
36560 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36562 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36563 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36564 && OMP_CLAUSE_DECL (c
) == decl
)
36566 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36567 && OMP_CLAUSE_DECL (c
) == decl
)
36568 error_at (loc
, "iteration variable %qD "
36569 "should not be firstprivate",
36571 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36572 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36573 && OMP_CLAUSE_DECL (c
) == decl
)
36574 error_at (loc
, "iteration variable %qD should not be reduction",
36579 if (code
!= OMP_SIMD
)
36580 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36581 else if (collapse
== 1)
36582 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36584 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36585 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36586 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36589 OMP_CLAUSE_CHAIN (c
) = clauses
;
36591 /* For linear, signal that we need to fill up
36592 the so far unknown linear step. */
36593 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36594 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36600 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36601 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36602 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36605 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36607 /* If decl is an iterator, preserve the operator on decl
36608 until finish_omp_for. */
36610 && ((processing_template_decl
36611 && (TREE_TYPE (real_decl
) == NULL_TREE
36612 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36613 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36614 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36616 incr
= cp_parser_expression (parser
);
36617 if (!EXPR_HAS_LOCATION (incr
))
36618 protected_set_expr_location (incr
, input_location
);
36622 if (!parens
.require_close (parser
))
36623 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36624 /*or_comma=*/false,
36625 /*consume_paren=*/true);
36627 TREE_VEC_ELT (declv
, i
) = decl
;
36628 TREE_VEC_ELT (initv
, i
) = init
;
36629 TREE_VEC_ELT (condv
, i
) = cond
;
36630 TREE_VEC_ELT (incrv
, i
) = incr
;
36633 orig_inits
.safe_grow_cleared (i
+ 1);
36634 orig_inits
[i
] = orig_init
;
36639 orig_declv
= copy_node (declv
);
36640 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36642 else if (orig_declv
)
36643 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36645 if (i
== count
- 1)
36648 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36649 in between the collapsed for loops to be still considered perfectly
36650 nested. Hopefully the final version clarifies this.
36651 For now handle (multiple) {'s and empty statements. */
36652 cp_parser_parse_tentatively (parser
);
36655 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36657 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36659 cp_lexer_consume_token (parser
->lexer
);
36662 else if (bracecount
36663 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36664 cp_lexer_consume_token (parser
->lexer
);
36667 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36668 error_at (loc
, "not enough for loops to collapse");
36669 collapse_err
= true;
36670 cp_parser_abort_tentative_parse (parser
);
36678 cp_parser_parse_definitely (parser
);
36679 nbraces
+= bracecount
;
36686 /* Note that we saved the original contents of this flag when we entered
36687 the structured block, and so we don't need to re-save it here. */
36688 parser
->in_statement
= IN_OMP_FOR
;
36690 /* Note that the grammar doesn't call for a structured block here,
36691 though the loop as a whole is a structured block. */
36694 body
= begin_omp_structured_block ();
36695 for (i
= 0; i
< count
; i
++)
36696 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
36697 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
36698 TREE_VEC_ELT (declv
, i
));
36701 body
= push_stmt_list ();
36702 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36704 body
= finish_omp_structured_block (body
);
36706 body
= pop_stmt_list (body
);
36708 if (declv
== NULL_TREE
)
36711 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
36712 incrv
, body
, pre_body
, &orig_inits
, clauses
);
36716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36718 cp_lexer_consume_token (parser
->lexer
);
36721 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36722 cp_lexer_consume_token (parser
->lexer
);
36727 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36728 "collapsed loops not perfectly nested");
36730 collapse_err
= true;
36731 cp_parser_statement_seq_opt (parser
, NULL
);
36732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
36737 while (!for_block
->is_empty ())
36739 tree t
= for_block
->pop ();
36740 if (TREE_CODE (t
) == STATEMENT_LIST
)
36741 add_stmt (pop_stmt_list (t
));
36745 release_tree_vector (for_block
);
36750 /* Helper function for OpenMP parsing, split clauses and call
36751 finish_omp_clauses on each of the set of clauses afterwards. */
36754 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
36755 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
36758 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
36759 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
36761 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
36765 #pragma omp simd simd-clause[optseq] new-line
36768 #define OMP_SIMD_CLAUSE_MASK \
36769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36781 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36782 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36785 tree clauses
, sb
, ret
;
36787 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36789 strcat (p_name
, " simd");
36790 mask
|= OMP_SIMD_CLAUSE_MASK
;
36792 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36796 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
36797 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
36798 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
36799 OMP_CLAUSE_ORDERED
);
36800 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
36802 error_at (OMP_CLAUSE_LOCATION (c
),
36803 "%<ordered%> clause with parameter may not be specified "
36804 "on %qs construct", p_name
);
36805 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
36809 keep_next_level (true);
36810 sb
= begin_omp_structured_block ();
36811 save
= cp_parser_begin_omp_structured_block (parser
);
36813 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
36815 cp_parser_end_omp_structured_block (parser
, save
);
36816 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36822 #pragma omp for for-clause[optseq] new-line
36826 #pragma omp for simd for-simd-clause[optseq] new-line
36829 #define OMP_FOR_CLAUSE_MASK \
36830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36841 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
36842 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36845 tree clauses
, sb
, ret
;
36847 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36849 strcat (p_name
, " for");
36850 mask
|= OMP_FOR_CLAUSE_MASK
;
36851 /* parallel for{, simd} disallows nowait clause, but for
36852 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36853 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
36854 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
36855 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36856 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36857 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
36859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36861 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36862 const char *p
= IDENTIFIER_POINTER (id
);
36864 if (strcmp (p
, "simd") == 0)
36866 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36867 if (cclauses
== NULL
)
36868 cclauses
= cclauses_buf
;
36870 cp_lexer_consume_token (parser
->lexer
);
36871 if (!flag_openmp
) /* flag_openmp_simd */
36872 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36874 sb
= begin_omp_structured_block ();
36875 save
= cp_parser_begin_omp_structured_block (parser
);
36876 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36878 cp_parser_end_omp_structured_block (parser
, save
);
36879 tree body
= finish_omp_structured_block (sb
);
36882 ret
= make_node (OMP_FOR
);
36883 TREE_TYPE (ret
) = void_type_node
;
36884 OMP_FOR_BODY (ret
) = body
;
36885 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36886 SET_EXPR_LOCATION (ret
, loc
);
36891 if (!flag_openmp
) /* flag_openmp_simd */
36893 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36897 /* Composite distribute parallel for disallows linear clause. */
36898 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36899 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
36901 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36905 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
36906 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36909 keep_next_level (true);
36910 sb
= begin_omp_structured_block ();
36911 save
= cp_parser_begin_omp_structured_block (parser
);
36913 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
36915 cp_parser_end_omp_structured_block (parser
, save
);
36916 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36921 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
36922 omp_clause_mask
, tree
*, bool *);
36925 # pragma omp master new-line
36926 structured-block */
36929 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
36930 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36933 tree clauses
, sb
, ret
;
36935 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36937 strcat (p_name
, " master");
36939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36941 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36942 const char *p
= IDENTIFIER_POINTER (id
);
36944 if (strcmp (p
, "taskloop") == 0)
36946 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36947 if (cclauses
== NULL
)
36948 cclauses
= cclauses_buf
;
36950 cp_lexer_consume_token (parser
->lexer
);
36951 if (!flag_openmp
) /* flag_openmp_simd */
36952 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36954 sb
= begin_omp_structured_block ();
36955 save
= cp_parser_begin_omp_structured_block (parser
);
36956 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36958 cp_parser_end_omp_structured_block (parser
, save
);
36959 tree body
= finish_omp_structured_block (sb
);
36962 return c_finish_omp_master (loc
, body
);
36965 if (!flag_openmp
) /* flag_openmp_simd */
36967 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36973 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36975 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
36978 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36980 return c_finish_omp_master (loc
,
36981 cp_parser_omp_structured_block (parser
, if_p
));
36985 # pragma omp ordered new-line
36989 # pragma omp ordered ordered-clauses new-line
36990 structured-block */
36992 #define OMP_ORDERED_CLAUSE_MASK \
36993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36996 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36997 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37000 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37001 enum pragma_context context
, bool *if_p
)
37003 location_t loc
= pragma_tok
->location
;
37005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37007 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37008 const char *p
= IDENTIFIER_POINTER (id
);
37010 if (strcmp (p
, "depend") == 0)
37012 if (!flag_openmp
) /* flag_openmp_simd */
37014 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37017 if (context
== pragma_stmt
)
37019 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37020 "%<depend%> clause may only be used in compound "
37022 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37026 = cp_parser_omp_all_clauses (parser
,
37027 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37028 "#pragma omp ordered", pragma_tok
);
37029 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37035 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37036 "#pragma omp ordered", pragma_tok
);
37038 if (!flag_openmp
/* flag_openmp_simd */
37039 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37042 c_finish_omp_ordered (loc
, clauses
,
37043 cp_parser_omp_structured_block (parser
, if_p
));
37050 { section-sequence }
37053 section-directive[opt] structured-block
37054 section-sequence section-directive structured-block */
37057 cp_parser_omp_sections_scope (cp_parser
*parser
)
37059 tree stmt
, substmt
;
37060 bool error_suppress
= false;
37063 matching_braces braces
;
37064 if (!braces
.require_open (parser
))
37067 stmt
= push_stmt_list ();
37069 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37070 != PRAGMA_OMP_SECTION
)
37072 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37073 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37074 add_stmt (substmt
);
37079 tok
= cp_lexer_peek_token (parser
->lexer
);
37080 if (tok
->type
== CPP_CLOSE_BRACE
)
37082 if (tok
->type
== CPP_EOF
)
37085 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37087 cp_lexer_consume_token (parser
->lexer
);
37088 cp_parser_require_pragma_eol (parser
, tok
);
37089 error_suppress
= false;
37091 else if (!error_suppress
)
37093 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37094 error_suppress
= true;
37097 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37098 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37099 add_stmt (substmt
);
37101 braces
.require_close (parser
);
37103 substmt
= pop_stmt_list (stmt
);
37105 stmt
= make_node (OMP_SECTIONS
);
37106 TREE_TYPE (stmt
) = void_type_node
;
37107 OMP_SECTIONS_BODY (stmt
) = substmt
;
37114 # pragma omp sections sections-clause[optseq] newline
37117 #define OMP_SECTIONS_CLAUSE_MASK \
37118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37125 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37126 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37129 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37131 strcat (p_name
, " sections");
37132 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37134 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37136 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37140 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37141 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37144 ret
= cp_parser_omp_sections_scope (parser
);
37146 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37152 # pragma omp parallel parallel-clause[optseq] new-line
37154 # pragma omp parallel for parallel-for-clause[optseq] new-line
37156 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37160 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37161 structured-block */
37163 #define OMP_PARALLEL_CLAUSE_MASK \
37164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37175 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37176 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37179 tree stmt
, clauses
, block
;
37181 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37183 strcat (p_name
, " parallel");
37184 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37185 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37186 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37187 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37188 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37190 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37192 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37193 if (cclauses
== NULL
)
37194 cclauses
= cclauses_buf
;
37196 cp_lexer_consume_token (parser
->lexer
);
37197 if (!flag_openmp
) /* flag_openmp_simd */
37198 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37200 block
= begin_omp_parallel ();
37201 save
= cp_parser_begin_omp_structured_block (parser
);
37202 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37204 cp_parser_end_omp_structured_block (parser
, save
);
37205 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37207 if (ret
== NULL_TREE
)
37209 OMP_PARALLEL_COMBINED (stmt
) = 1;
37212 /* When combined with distribute, parallel has to be followed by for.
37213 #pragma omp target parallel is allowed though. */
37215 && (mask
& (OMP_CLAUSE_MASK_1
37216 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37218 error_at (loc
, "expected %<for%> after %qs", p_name
);
37219 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37222 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37224 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37225 const char *p
= IDENTIFIER_POINTER (id
);
37226 if (strcmp (p
, "master") == 0)
37228 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37229 cclauses
= cclauses_buf
;
37231 cp_lexer_consume_token (parser
->lexer
);
37232 block
= begin_omp_parallel ();
37233 save
= cp_parser_begin_omp_structured_block (parser
);
37234 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37236 cp_parser_end_omp_structured_block (parser
, save
);
37237 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37239 OMP_PARALLEL_COMBINED (stmt
) = 1;
37240 if (ret
== NULL_TREE
)
37244 else if (!flag_openmp
) /* flag_openmp_simd */
37246 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37249 else if (strcmp (p
, "sections") == 0)
37251 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37252 cclauses
= cclauses_buf
;
37254 cp_lexer_consume_token (parser
->lexer
);
37255 block
= begin_omp_parallel ();
37256 save
= cp_parser_begin_omp_structured_block (parser
);
37257 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37258 cp_parser_end_omp_structured_block (parser
, save
);
37259 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37261 OMP_PARALLEL_COMBINED (stmt
) = 1;
37265 else if (!flag_openmp
) /* flag_openmp_simd */
37267 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37271 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37275 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37276 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37279 block
= begin_omp_parallel ();
37280 save
= cp_parser_begin_omp_structured_block (parser
);
37281 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37282 cp_parser_end_omp_structured_block (parser
, save
);
37283 stmt
= finish_omp_parallel (clauses
, block
);
37288 # pragma omp single single-clause[optseq] new-line
37289 structured-block */
37291 #define OMP_SINGLE_CLAUSE_MASK \
37292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37298 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37300 tree stmt
= make_node (OMP_SINGLE
);
37301 TREE_TYPE (stmt
) = void_type_node
;
37302 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37304 OMP_SINGLE_CLAUSES (stmt
)
37305 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37306 "#pragma omp single", pragma_tok
);
37307 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37309 return add_stmt (stmt
);
37313 # pragma omp task task-clause[optseq] new-line
37314 structured-block */
37316 #define OMP_TASK_CLAUSE_MASK \
37317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37330 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37332 tree clauses
, block
;
37335 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37336 "#pragma omp task", pragma_tok
);
37337 block
= begin_omp_task ();
37338 save
= cp_parser_begin_omp_structured_block (parser
);
37339 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37340 cp_parser_end_omp_structured_block (parser
, save
);
37341 return finish_omp_task (clauses
, block
);
37345 # pragma omp taskwait new-line
37348 # pragma omp taskwait taskwait-clause[opt] new-line */
37350 #define OMP_TASKWAIT_CLAUSE_MASK \
37351 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37354 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37357 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37358 "#pragma omp taskwait", pragma_tok
);
37362 tree stmt
= make_node (OMP_TASK
);
37363 TREE_TYPE (stmt
) = void_node
;
37364 OMP_TASK_CLAUSES (stmt
) = clauses
;
37365 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37366 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37370 finish_omp_taskwait ();
37374 # pragma omp taskyield new-line */
37377 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37379 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37380 finish_omp_taskyield ();
37384 # pragma omp taskgroup new-line
37388 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37390 #define OMP_TASKGROUP_CLAUSE_MASK \
37391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37394 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37397 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37398 "#pragma omp taskgroup", pragma_tok
);
37399 return c_finish_omp_taskgroup (input_location
,
37400 cp_parser_omp_structured_block (parser
,
37407 # pragma omp threadprivate (variable-list) */
37410 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37414 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37415 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37417 finish_omp_threadprivate (vars
);
37421 # pragma omp cancel cancel-clause[optseq] new-line */
37423 #define OMP_CANCEL_CLAUSE_MASK \
37424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37431 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37433 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37434 "#pragma omp cancel", pragma_tok
);
37435 finish_omp_cancel (clauses
);
37439 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37441 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37448 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37449 enum pragma_context context
)
37452 bool point_seen
= false;
37454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37456 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37457 const char *p
= IDENTIFIER_POINTER (id
);
37459 if (strcmp (p
, "point") == 0)
37461 cp_lexer_consume_token (parser
->lexer
);
37467 cp_parser_error (parser
, "expected %<point%>");
37468 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37472 if (context
!= pragma_compound
)
37474 if (context
== pragma_stmt
)
37475 error_at (pragma_tok
->location
,
37476 "%<#pragma %s%> may only be used in compound statements",
37477 "omp cancellation point");
37479 cp_parser_error (parser
, "expected declaration specifiers");
37480 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37484 clauses
= cp_parser_omp_all_clauses (parser
,
37485 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37486 "#pragma omp cancellation point",
37488 finish_omp_cancellation_point (clauses
);
37492 #pragma omp distribute distribute-clause[optseq] new-line
37495 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37503 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37504 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37507 tree clauses
, sb
, ret
;
37509 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37511 strcat (p_name
, " distribute");
37512 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37516 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37517 const char *p
= IDENTIFIER_POINTER (id
);
37519 bool parallel
= false;
37521 if (strcmp (p
, "simd") == 0)
37524 parallel
= strcmp (p
, "parallel") == 0;
37525 if (parallel
|| simd
)
37527 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37528 if (cclauses
== NULL
)
37529 cclauses
= cclauses_buf
;
37530 cp_lexer_consume_token (parser
->lexer
);
37531 if (!flag_openmp
) /* flag_openmp_simd */
37534 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37537 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37540 sb
= begin_omp_structured_block ();
37541 save
= cp_parser_begin_omp_structured_block (parser
);
37543 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37546 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37548 cp_parser_end_omp_structured_block (parser
, save
);
37549 tree body
= finish_omp_structured_block (sb
);
37552 ret
= make_node (OMP_DISTRIBUTE
);
37553 TREE_TYPE (ret
) = void_type_node
;
37554 OMP_FOR_BODY (ret
) = body
;
37555 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37556 SET_EXPR_LOCATION (ret
, loc
);
37561 if (!flag_openmp
) /* flag_openmp_simd */
37563 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37567 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37571 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37572 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37575 keep_next_level (true);
37576 sb
= begin_omp_structured_block ();
37577 save
= cp_parser_begin_omp_structured_block (parser
);
37579 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37581 cp_parser_end_omp_structured_block (parser
, save
);
37582 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37588 # pragma omp teams teams-clause[optseq] new-line
37589 structured-block */
37591 #define OMP_TEAMS_CLAUSE_MASK \
37592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37601 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37602 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37605 tree clauses
, sb
, ret
;
37607 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37609 strcat (p_name
, " teams");
37610 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37614 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37615 const char *p
= IDENTIFIER_POINTER (id
);
37616 if (strcmp (p
, "distribute") == 0)
37618 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37619 if (cclauses
== NULL
)
37620 cclauses
= cclauses_buf
;
37622 cp_lexer_consume_token (parser
->lexer
);
37623 if (!flag_openmp
) /* flag_openmp_simd */
37624 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37626 keep_next_level (true);
37627 sb
= begin_omp_structured_block ();
37628 save
= cp_parser_begin_omp_structured_block (parser
);
37629 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37631 cp_parser_end_omp_structured_block (parser
, save
);
37632 tree body
= finish_omp_structured_block (sb
);
37635 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37636 ret
= make_node (OMP_TEAMS
);
37637 TREE_TYPE (ret
) = void_type_node
;
37638 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37639 OMP_TEAMS_BODY (ret
) = body
;
37640 OMP_TEAMS_COMBINED (ret
) = 1;
37641 SET_EXPR_LOCATION (ret
, loc
);
37642 return add_stmt (ret
);
37645 if (!flag_openmp
) /* flag_openmp_simd */
37647 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37651 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37655 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
37656 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37659 tree stmt
= make_node (OMP_TEAMS
);
37660 TREE_TYPE (stmt
) = void_type_node
;
37661 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
37662 keep_next_level (true);
37663 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37664 SET_EXPR_LOCATION (stmt
, loc
);
37666 return add_stmt (stmt
);
37670 # pragma omp target data target-data-clause[optseq] new-line
37671 structured-block */
37673 #define OMP_TARGET_DATA_CLAUSE_MASK \
37674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37680 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37683 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
37684 "#pragma omp target data", pragma_tok
);
37686 for (tree
*pc
= &clauses
; *pc
;)
37688 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37689 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37692 case GOMP_MAP_ALWAYS_TO
:
37693 case GOMP_MAP_FROM
:
37694 case GOMP_MAP_ALWAYS_FROM
:
37695 case GOMP_MAP_TOFROM
:
37696 case GOMP_MAP_ALWAYS_TOFROM
:
37697 case GOMP_MAP_ALLOC
:
37700 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37701 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37702 case GOMP_MAP_ALWAYS_POINTER
:
37706 error_at (OMP_CLAUSE_LOCATION (*pc
),
37707 "%<#pragma omp target data%> with map-type other "
37708 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37709 "on %<map%> clause");
37710 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37713 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
37715 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37721 error_at (pragma_tok
->location
,
37722 "%<#pragma omp target data%> must contain at least "
37723 "one %<map%> or %<use_device_ptr%> clause");
37727 tree stmt
= make_node (OMP_TARGET_DATA
);
37728 TREE_TYPE (stmt
) = void_type_node
;
37729 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
37731 keep_next_level (true);
37732 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37734 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37735 return add_stmt (stmt
);
37739 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37740 structured-block */
37742 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37750 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37751 enum pragma_context context
)
37753 bool data_seen
= false;
37754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37756 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37757 const char *p
= IDENTIFIER_POINTER (id
);
37759 if (strcmp (p
, "data") == 0)
37761 cp_lexer_consume_token (parser
->lexer
);
37767 cp_parser_error (parser
, "expected %<data%>");
37768 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37772 if (context
== pragma_stmt
)
37774 error_at (pragma_tok
->location
,
37775 "%<#pragma %s%> may only be used in compound statements",
37776 "omp target enter data");
37777 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37782 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
37783 "#pragma omp target enter data", pragma_tok
);
37785 for (tree
*pc
= &clauses
; *pc
;)
37787 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37788 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37791 case GOMP_MAP_ALWAYS_TO
:
37792 case GOMP_MAP_ALLOC
:
37795 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37796 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37797 case GOMP_MAP_ALWAYS_POINTER
:
37801 error_at (OMP_CLAUSE_LOCATION (*pc
),
37802 "%<#pragma omp target enter data%> with map-type other "
37803 "than %<to%> or %<alloc%> on %<map%> clause");
37804 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37807 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37813 error_at (pragma_tok
->location
,
37814 "%<#pragma omp target enter data%> must contain at least "
37815 "one %<map%> clause");
37819 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
37820 TREE_TYPE (stmt
) = void_type_node
;
37821 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
37822 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37823 return add_stmt (stmt
);
37827 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37828 structured-block */
37830 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37838 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37839 enum pragma_context context
)
37841 bool data_seen
= false;
37842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37844 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37845 const char *p
= IDENTIFIER_POINTER (id
);
37847 if (strcmp (p
, "data") == 0)
37849 cp_lexer_consume_token (parser
->lexer
);
37855 cp_parser_error (parser
, "expected %<data%>");
37856 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37860 if (context
== pragma_stmt
)
37862 error_at (pragma_tok
->location
,
37863 "%<#pragma %s%> may only be used in compound statements",
37864 "omp target exit data");
37865 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37870 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
37871 "#pragma omp target exit data", pragma_tok
);
37873 for (tree
*pc
= &clauses
; *pc
;)
37875 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37876 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37878 case GOMP_MAP_FROM
:
37879 case GOMP_MAP_ALWAYS_FROM
:
37880 case GOMP_MAP_RELEASE
:
37881 case GOMP_MAP_DELETE
:
37884 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37885 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37886 case GOMP_MAP_ALWAYS_POINTER
:
37890 error_at (OMP_CLAUSE_LOCATION (*pc
),
37891 "%<#pragma omp target exit data%> with map-type other "
37892 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37894 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37897 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37903 error_at (pragma_tok
->location
,
37904 "%<#pragma omp target exit data%> must contain at least "
37905 "one %<map%> clause");
37909 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
37910 TREE_TYPE (stmt
) = void_type_node
;
37911 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
37912 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37913 return add_stmt (stmt
);
37917 # pragma omp target update target-update-clause[optseq] new-line */
37919 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37928 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
37929 enum pragma_context context
)
37931 if (context
== pragma_stmt
)
37933 error_at (pragma_tok
->location
,
37934 "%<#pragma %s%> may only be used in compound statements",
37935 "omp target update");
37936 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37941 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
37942 "#pragma omp target update", pragma_tok
);
37943 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
37944 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
37946 error_at (pragma_tok
->location
,
37947 "%<#pragma omp target update%> must contain at least one "
37948 "%<from%> or %<to%> clauses");
37952 tree stmt
= make_node (OMP_TARGET_UPDATE
);
37953 TREE_TYPE (stmt
) = void_type_node
;
37954 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
37955 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37961 # pragma omp target target-clause[optseq] new-line
37962 structured-block */
37964 #define OMP_TARGET_CLAUSE_MASK \
37965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37976 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
37977 enum pragma_context context
, bool *if_p
)
37979 tree
*pc
= NULL
, stmt
;
37983 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
37985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37987 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37988 const char *p
= IDENTIFIER_POINTER (id
);
37989 enum tree_code ccode
= ERROR_MARK
;
37991 if (strcmp (p
, "teams") == 0)
37993 else if (strcmp (p
, "parallel") == 0)
37994 ccode
= OMP_PARALLEL
;
37995 else if (strcmp (p
, "simd") == 0)
37997 if (ccode
!= ERROR_MARK
)
37999 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38000 char p_name
[sizeof ("#pragma omp target teams distribute "
38001 "parallel for simd")];
38003 cp_lexer_consume_token (parser
->lexer
);
38004 strcpy (p_name
, "#pragma omp target");
38005 if (!flag_openmp
) /* flag_openmp_simd */
38011 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38012 OMP_TARGET_CLAUSE_MASK
,
38016 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38017 OMP_TARGET_CLAUSE_MASK
,
38021 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38022 OMP_TARGET_CLAUSE_MASK
,
38026 gcc_unreachable ();
38028 return stmt
!= NULL_TREE
;
38030 keep_next_level (true);
38031 tree sb
= begin_omp_structured_block (), ret
;
38032 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38036 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38037 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38041 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38042 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38046 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38047 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38051 gcc_unreachable ();
38053 cp_parser_end_omp_structured_block (parser
, save
);
38054 tree body
= finish_omp_structured_block (sb
);
38055 if (ret
== NULL_TREE
)
38057 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38059 /* For combined target teams, ensure the num_teams and
38060 thread_limit clause expressions are evaluated on the host,
38061 before entering the target construct. */
38063 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38064 c
; c
= OMP_CLAUSE_CHAIN (c
))
38065 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38066 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38067 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38069 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38070 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38071 if (expr
== error_mark_node
)
38073 tree tmp
= TARGET_EXPR_SLOT (expr
);
38075 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38076 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38077 OMP_CLAUSE_FIRSTPRIVATE
);
38078 OMP_CLAUSE_DECL (tc
) = tmp
;
38079 OMP_CLAUSE_CHAIN (tc
)
38080 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38081 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38084 tree stmt
= make_node (OMP_TARGET
);
38085 TREE_TYPE (stmt
) = void_type_node
;
38086 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38087 OMP_TARGET_BODY (stmt
) = body
;
38088 OMP_TARGET_COMBINED (stmt
) = 1;
38089 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38091 pc
= &OMP_TARGET_CLAUSES (stmt
);
38092 goto check_clauses
;
38094 else if (!flag_openmp
) /* flag_openmp_simd */
38096 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38099 else if (strcmp (p
, "data") == 0)
38101 cp_lexer_consume_token (parser
->lexer
);
38102 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38105 else if (strcmp (p
, "enter") == 0)
38107 cp_lexer_consume_token (parser
->lexer
);
38108 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38111 else if (strcmp (p
, "exit") == 0)
38113 cp_lexer_consume_token (parser
->lexer
);
38114 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38117 else if (strcmp (p
, "update") == 0)
38119 cp_lexer_consume_token (parser
->lexer
);
38120 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38123 if (!flag_openmp
) /* flag_openmp_simd */
38125 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38129 stmt
= make_node (OMP_TARGET
);
38130 TREE_TYPE (stmt
) = void_type_node
;
38132 OMP_TARGET_CLAUSES (stmt
)
38133 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38134 "#pragma omp target", pragma_tok
);
38135 pc
= &OMP_TARGET_CLAUSES (stmt
);
38136 keep_next_level (true);
38137 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38139 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38145 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38146 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38149 case GOMP_MAP_ALWAYS_TO
:
38150 case GOMP_MAP_FROM
:
38151 case GOMP_MAP_ALWAYS_FROM
:
38152 case GOMP_MAP_TOFROM
:
38153 case GOMP_MAP_ALWAYS_TOFROM
:
38154 case GOMP_MAP_ALLOC
:
38155 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38156 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38157 case GOMP_MAP_ALWAYS_POINTER
:
38160 error_at (OMP_CLAUSE_LOCATION (*pc
),
38161 "%<#pragma omp target%> with map-type other "
38162 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38163 "on %<map%> clause");
38164 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38167 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38173 # pragma acc cache (variable-list) new-line
38177 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38179 tree stmt
, clauses
;
38181 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38182 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38184 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38186 stmt
= make_node (OACC_CACHE
);
38187 TREE_TYPE (stmt
) = void_type_node
;
38188 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38189 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38196 # pragma acc data oacc-data-clause[optseq] new-line
38197 structured-block */
38199 #define OACC_DATA_CLAUSE_MASK \
38200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38209 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38211 tree stmt
, clauses
, block
;
38214 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38215 "#pragma acc data", pragma_tok
);
38217 block
= begin_omp_parallel ();
38218 save
= cp_parser_begin_omp_structured_block (parser
);
38219 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38220 cp_parser_end_omp_structured_block (parser
, save
);
38221 stmt
= finish_oacc_data (clauses
, block
);
38226 # pragma acc host_data <clauses> new-line
38227 structured-block */
38229 #define OACC_HOST_DATA_CLAUSE_MASK \
38230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38233 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38235 tree stmt
, clauses
, block
;
38238 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38239 "#pragma acc host_data", pragma_tok
);
38241 block
= begin_omp_parallel ();
38242 save
= cp_parser_begin_omp_structured_block (parser
);
38243 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38244 cp_parser_end_omp_structured_block (parser
, save
);
38245 stmt
= finish_oacc_host_data (clauses
, block
);
38250 # pragma acc declare oacc-data-clause[optseq] new-line
38253 #define OACC_DECLARE_CLAUSE_MASK \
38254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38264 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38266 tree clauses
, stmt
;
38267 bool error
= false;
38269 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38270 "#pragma acc declare", pragma_tok
, true);
38273 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38275 error_at (pragma_tok
->location
,
38276 "no valid clauses specified in %<#pragma acc declare%>");
38280 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38282 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38283 tree decl
= OMP_CLAUSE_DECL (t
);
38284 if (!DECL_P (decl
))
38286 error_at (loc
, "array section in %<#pragma acc declare%>");
38290 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38291 switch (OMP_CLAUSE_MAP_KIND (t
))
38293 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38294 case GOMP_MAP_ALLOC
:
38296 case GOMP_MAP_FORCE_DEVICEPTR
:
38297 case GOMP_MAP_DEVICE_RESIDENT
:
38300 case GOMP_MAP_LINK
:
38301 if (!global_bindings_p ()
38302 && (TREE_STATIC (decl
)
38303 || !DECL_EXTERNAL (decl
)))
38306 "%qD must be a global variable in "
38307 "%<#pragma acc declare link%>",
38315 if (global_bindings_p ())
38317 error_at (loc
, "invalid OpenACC clause at file scope");
38321 if (DECL_EXTERNAL (decl
))
38324 "invalid use of %<extern%> variable %qD "
38325 "in %<#pragma acc declare%>", decl
);
38329 else if (TREE_PUBLIC (decl
))
38332 "invalid use of %<global%> variable %qD "
38333 "in %<#pragma acc declare%>", decl
);
38340 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38341 || lookup_attribute ("omp declare target link",
38342 DECL_ATTRIBUTES (decl
)))
38344 error_at (loc
, "variable %qD used more than once with "
38345 "%<#pragma acc declare%>", decl
);
38354 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38355 id
= get_identifier ("omp declare target link");
38357 id
= get_identifier ("omp declare target");
38359 DECL_ATTRIBUTES (decl
)
38360 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38361 if (global_bindings_p ())
38363 symtab_node
*node
= symtab_node::get (decl
);
38366 node
->offloadable
= 1;
38367 if (ENABLE_OFFLOADING
)
38369 g
->have_offload
= true;
38370 if (is_a
<varpool_node
*> (node
))
38371 vec_safe_push (offload_vars
, decl
);
38378 if (error
|| global_bindings_p ())
38381 stmt
= make_node (OACC_DECLARE
);
38382 TREE_TYPE (stmt
) = void_type_node
;
38383 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38384 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38392 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38396 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38398 LOC is the location of the #pragma token.
38401 #define OACC_ENTER_DATA_CLAUSE_MASK \
38402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38408 #define OACC_EXIT_DATA_CLAUSE_MASK \
38409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38417 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38420 location_t loc
= pragma_tok
->location
;
38421 tree stmt
, clauses
;
38422 const char *p
= "";
38424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38425 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38427 if (strcmp (p
, "data") != 0)
38429 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38430 enter
? "enter" : "exit");
38431 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38435 cp_lexer_consume_token (parser
->lexer
);
38438 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38439 "#pragma acc enter data", pragma_tok
);
38441 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38442 "#pragma acc exit data", pragma_tok
);
38444 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38446 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38447 enter
? "enter" : "exit");
38451 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38452 TREE_TYPE (stmt
) = void_type_node
;
38453 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38454 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38460 # pragma acc loop oacc-loop-clause[optseq] new-line
38461 structured-block */
38463 #define OACC_LOOP_CLAUSE_MASK \
38464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38476 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38477 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38479 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38481 strcat (p_name
, " loop");
38482 mask
|= OACC_LOOP_CLAUSE_MASK
;
38484 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38488 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38490 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38492 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38495 tree block
= begin_omp_structured_block ();
38496 int save
= cp_parser_begin_omp_structured_block (parser
);
38497 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38498 cp_parser_end_omp_structured_block (parser
, save
);
38499 add_stmt (finish_omp_structured_block (block
));
38505 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38510 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38514 #define OACC_KERNELS_CLAUSE_MASK \
38515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38529 #define OACC_PARALLEL_CLAUSE_MASK \
38530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38548 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38549 char *p_name
, bool *if_p
)
38551 omp_clause_mask mask
;
38552 enum tree_code code
;
38553 switch (cp_parser_pragma_kind (pragma_tok
))
38555 case PRAGMA_OACC_KERNELS
:
38556 strcat (p_name
, " kernels");
38557 mask
= OACC_KERNELS_CLAUSE_MASK
;
38558 code
= OACC_KERNELS
;
38560 case PRAGMA_OACC_PARALLEL
:
38561 strcat (p_name
, " parallel");
38562 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38563 code
= OACC_PARALLEL
;
38566 gcc_unreachable ();
38569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38572 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38573 if (strcmp (p
, "loop") == 0)
38575 cp_lexer_consume_token (parser
->lexer
);
38576 tree block
= begin_omp_parallel ();
38578 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
38580 return finish_omp_construct (code
, block
, clauses
);
38584 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38586 tree block
= begin_omp_parallel ();
38587 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38588 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38589 cp_parser_end_omp_structured_block (parser
, save
);
38590 return finish_omp_construct (code
, block
, clauses
);
38594 # pragma acc update oacc-update-clause[optseq] new-line
38597 #define OACC_UPDATE_CLAUSE_MASK \
38598 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38606 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38608 tree stmt
, clauses
;
38610 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38611 "#pragma acc update", pragma_tok
);
38613 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38615 error_at (pragma_tok
->location
,
38616 "%<#pragma acc update%> must contain at least one "
38617 "%<device%> or %<host%> or %<self%> clause");
38621 stmt
= make_node (OACC_UPDATE
);
38622 TREE_TYPE (stmt
) = void_type_node
;
38623 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38624 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38630 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38632 LOC is the location of the #pragma token.
38635 #define OACC_WAIT_CLAUSE_MASK \
38636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38639 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38641 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38642 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38644 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38645 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
38647 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
38648 "#pragma acc wait", pragma_tok
);
38650 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
38651 stmt
= finish_expr_stmt (stmt
);
38657 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38659 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38668 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38669 enum pragma_context context
)
38671 bool first_p
= parser
->omp_declare_simd
== NULL
;
38672 cp_omp_declare_simd_data data
;
38675 data
.error_seen
= false;
38676 data
.fndecl_seen
= false;
38677 data
.tokens
= vNULL
;
38678 data
.clauses
= NULL_TREE
;
38679 /* It is safe to take the address of a local variable; it will only be
38680 used while this scope is live. */
38681 parser
->omp_declare_simd
= &data
;
38684 /* Store away all pragma tokens. */
38685 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38687 cp_lexer_consume_token (parser
->lexer
);
38688 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38689 parser
->omp_declare_simd
->error_seen
= true;
38690 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38691 struct cp_token_cache
*cp
38692 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38693 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
38697 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38698 cp_parser_pragma (parser
, context
, NULL
);
38701 case pragma_external
:
38702 cp_parser_declaration (parser
);
38704 case pragma_member
:
38705 cp_parser_member_declaration (parser
);
38707 case pragma_objc_icode
:
38708 cp_parser_block_declaration (parser
, /*statement_p=*/false);
38711 cp_parser_declaration_statement (parser
);
38714 if (parser
->omp_declare_simd
38715 && !parser
->omp_declare_simd
->error_seen
38716 && !parser
->omp_declare_simd
->fndecl_seen
)
38717 error_at (pragma_tok
->location
,
38718 "%<#pragma omp declare simd%> not immediately followed by "
38719 "function declaration or definition");
38720 data
.tokens
.release ();
38721 parser
->omp_declare_simd
= NULL
;
38725 /* Finalize #pragma omp declare simd clauses after direct declarator has
38726 been parsed, and put that into "omp declare simd" attribute. */
38729 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
38731 struct cp_token_cache
*ce
;
38732 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
38735 if (!data
->error_seen
&& data
->fndecl_seen
)
38737 error ("%<#pragma omp declare simd%> not immediately followed by "
38738 "a single function declaration or definition");
38739 data
->error_seen
= true;
38741 if (data
->error_seen
)
38744 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
38748 cp_parser_push_lexer_for_tokens (parser
, ce
);
38749 parser
->lexer
->in_pragma
= true;
38750 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38751 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38752 cp_lexer_consume_token (parser
->lexer
);
38753 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
38754 "#pragma omp declare simd", pragma_tok
);
38755 cp_parser_pop_lexer (parser
);
38757 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
38758 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
38759 TREE_CHAIN (c
) = attrs
;
38760 if (processing_template_decl
)
38761 ATTR_IS_DEPENDENT (c
) = 1;
38765 data
->fndecl_seen
= true;
38771 # pragma omp declare target new-line
38772 declarations and definitions
38773 # pragma omp end declare target new-line
38776 # pragma omp declare target ( extended-list ) new-line
38778 # pragma omp declare target declare-target-clauses[seq] new-line */
38780 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38785 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38787 tree clauses
= NULL_TREE
;
38788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38790 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
38791 "#pragma omp declare target", pragma_tok
);
38792 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38794 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
38796 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38797 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38801 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38802 scope_chain
->omp_declare_target_attribute
++;
38805 if (scope_chain
->omp_declare_target_attribute
)
38806 error_at (pragma_tok
->location
,
38807 "%<#pragma omp declare target%> with clauses in between "
38808 "%<#pragma omp declare target%> without clauses and "
38809 "%<#pragma omp end declare target%>");
38810 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
38812 tree t
= OMP_CLAUSE_DECL (c
), id
;
38813 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
38814 tree at2
= lookup_attribute ("omp declare target link",
38815 DECL_ATTRIBUTES (t
));
38816 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
38818 id
= get_identifier ("omp declare target link");
38819 std::swap (at1
, at2
);
38822 id
= get_identifier ("omp declare target");
38825 error_at (OMP_CLAUSE_LOCATION (c
),
38826 "%qD specified both in declare target %<link%> and %<to%>"
38832 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
38833 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
38836 symtab_node
*node
= symtab_node::get (t
);
38839 node
->offloadable
= 1;
38840 if (ENABLE_OFFLOADING
)
38842 g
->have_offload
= true;
38843 if (is_a
<varpool_node
*> (node
))
38844 vec_safe_push (offload_vars
, t
);
38852 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38854 const char *p
= "";
38855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38857 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38858 p
= IDENTIFIER_POINTER (id
);
38860 if (strcmp (p
, "declare") == 0)
38862 cp_lexer_consume_token (parser
->lexer
);
38864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38866 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38867 p
= IDENTIFIER_POINTER (id
);
38869 if (strcmp (p
, "target") == 0)
38870 cp_lexer_consume_token (parser
->lexer
);
38873 cp_parser_error (parser
, "expected %<target%>");
38874 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38880 cp_parser_error (parser
, "expected %<declare%>");
38881 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38884 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38885 if (!scope_chain
->omp_declare_target_attribute
)
38886 error_at (pragma_tok
->location
,
38887 "%<#pragma omp end declare target%> without corresponding "
38888 "%<#pragma omp declare target%>");
38890 scope_chain
->omp_declare_target_attribute
--;
38893 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38894 expression and optional initializer clause of
38895 #pragma omp declare reduction. We store the expression(s) as
38896 either 3, 6 or 7 special statements inside of the artificial function's
38897 body. The first two statements are DECL_EXPRs for the artificial
38898 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38899 expression that uses those variables.
38900 If there was any INITIALIZER clause, this is followed by further statements,
38901 the fourth and fifth statements are DECL_EXPRs for the artificial
38902 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38903 constructor variant (first token after open paren is not omp_priv),
38904 then the sixth statement is a statement with the function call expression
38905 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38906 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38907 to initialize the OMP_PRIV artificial variable and there is seventh
38908 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38911 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
38913 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
38914 gcc_assert (TYPE_REF_P (type
));
38915 type
= TREE_TYPE (type
);
38916 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
38917 DECL_ARTIFICIAL (omp_out
) = 1;
38918 pushdecl (omp_out
);
38919 add_decl_expr (omp_out
);
38920 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
38921 DECL_ARTIFICIAL (omp_in
) = 1;
38923 add_decl_expr (omp_in
);
38925 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
38927 keep_next_level (true);
38928 tree block
= begin_omp_structured_block ();
38929 combiner
= cp_parser_expression (parser
);
38930 finish_expr_stmt (combiner
);
38931 block
= finish_omp_structured_block (block
);
38934 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38937 const char *p
= "";
38938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38940 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38941 p
= IDENTIFIER_POINTER (id
);
38944 if (strcmp (p
, "initializer") == 0)
38946 cp_lexer_consume_token (parser
->lexer
);
38947 matching_parens parens
;
38948 if (!parens
.require_open (parser
))
38952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38954 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38955 p
= IDENTIFIER_POINTER (id
);
38958 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
38959 DECL_ARTIFICIAL (omp_priv
) = 1;
38960 pushdecl (omp_priv
);
38961 add_decl_expr (omp_priv
);
38962 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
38963 DECL_ARTIFICIAL (omp_orig
) = 1;
38964 pushdecl (omp_orig
);
38965 add_decl_expr (omp_orig
);
38967 keep_next_level (true);
38968 block
= begin_omp_structured_block ();
38971 if (strcmp (p
, "omp_priv") == 0)
38973 bool is_direct_init
, is_non_constant_init
;
38975 cp_lexer_consume_token (parser
->lexer
);
38976 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
38978 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
38979 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
38981 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
38982 == CPP_CLOSE_PAREN
))
38984 finish_omp_structured_block (block
);
38985 error ("invalid initializer clause");
38988 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
38989 &is_non_constant_init
);
38990 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
38991 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
38995 cp_parser_parse_tentatively (parser
);
38996 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
38997 /*check_dependency_p=*/true,
38998 /*template_p=*/NULL
,
38999 /*declarator_p=*/false,
39000 /*optional_p=*/false);
39001 vec
<tree
, va_gc
> *args
;
39002 if (fn_name
== error_mark_node
39003 || cp_parser_error_occurred (parser
)
39004 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39005 || ((args
= cp_parser_parenthesized_expression_list
39006 (parser
, non_attr
, /*cast_p=*/false,
39007 /*allow_expansion_p=*/true,
39008 /*non_constant_p=*/NULL
)),
39009 cp_parser_error_occurred (parser
)))
39011 finish_omp_structured_block (block
);
39012 cp_parser_abort_tentative_parse (parser
);
39013 cp_parser_error (parser
, "expected id-expression (arguments)");
39018 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39019 if (arg
== omp_priv
39020 || (TREE_CODE (arg
) == ADDR_EXPR
39021 && TREE_OPERAND (arg
, 0) == omp_priv
))
39023 cp_parser_abort_tentative_parse (parser
);
39024 if (arg
== NULL_TREE
)
39025 error ("one of the initializer call arguments should be %<omp_priv%>"
39026 " or %<&omp_priv%>");
39027 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39029 finish_expr_stmt (initializer
);
39032 block
= finish_omp_structured_block (block
);
39033 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39037 add_decl_expr (omp_orig
);
39039 if (!parens
.require_close (parser
))
39043 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39044 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39051 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39052 initializer-clause[opt] new-line
39054 initializer-clause:
39055 initializer (omp_priv initializer)
39056 initializer (function-name (argument-list)) */
39059 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39060 enum pragma_context
)
39062 auto_vec
<tree
> types
;
39063 enum tree_code reduc_code
= ERROR_MARK
;
39064 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39066 cp_token
*first_token
;
39067 cp_token_cache
*cp
;
39071 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39072 p
= obstack_alloc (&declarator_obstack
, 0);
39074 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39077 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39080 reduc_code
= PLUS_EXPR
;
39083 reduc_code
= MULT_EXPR
;
39086 reduc_code
= MINUS_EXPR
;
39089 reduc_code
= BIT_AND_EXPR
;
39092 reduc_code
= BIT_XOR_EXPR
;
39095 reduc_code
= BIT_IOR_EXPR
;
39098 reduc_code
= TRUTH_ANDIF_EXPR
;
39101 reduc_code
= TRUTH_ORIF_EXPR
;
39104 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39107 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39108 "%<|%>, %<&&%>, %<||%> or identifier");
39112 if (reduc_code
!= ERROR_MARK
)
39113 cp_lexer_consume_token (parser
->lexer
);
39115 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39116 if (reduc_id
== error_mark_node
)
39119 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39122 /* Types may not be defined in declare reduction type list. */
39123 const char *saved_message
;
39124 saved_message
= parser
->type_definition_forbidden_message
;
39125 parser
->type_definition_forbidden_message
39126 = G_("types may not be defined in declare reduction type list");
39127 bool saved_colon_corrects_to_scope_p
;
39128 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39129 parser
->colon_corrects_to_scope_p
= false;
39130 bool saved_colon_doesnt_start_class_def_p
;
39131 saved_colon_doesnt_start_class_def_p
39132 = parser
->colon_doesnt_start_class_def_p
;
39133 parser
->colon_doesnt_start_class_def_p
= true;
39137 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39138 type
= cp_parser_type_id (parser
);
39139 if (type
== error_mark_node
)
39141 else if (ARITHMETIC_TYPE_P (type
)
39142 && (orig_reduc_id
== NULL_TREE
39143 || (TREE_CODE (type
) != COMPLEX_TYPE
39144 && (id_equal (orig_reduc_id
, "min")
39145 || id_equal (orig_reduc_id
, "max")))))
39146 error_at (loc
, "predeclared arithmetic type %qT in "
39147 "%<#pragma omp declare reduction%>", type
);
39148 else if (TREE_CODE (type
) == FUNCTION_TYPE
39149 || TREE_CODE (type
) == METHOD_TYPE
39150 || TREE_CODE (type
) == ARRAY_TYPE
)
39151 error_at (loc
, "function or array type %qT in "
39152 "%<#pragma omp declare reduction%>", type
);
39153 else if (TYPE_REF_P (type
))
39154 error_at (loc
, "reference type %qT in "
39155 "%<#pragma omp declare reduction%>", type
);
39156 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39157 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39158 "%<#pragma omp declare reduction%>", type
);
39160 types
.safe_push (type
);
39162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39163 cp_lexer_consume_token (parser
->lexer
);
39168 /* Restore the saved message. */
39169 parser
->type_definition_forbidden_message
= saved_message
;
39170 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39171 parser
->colon_doesnt_start_class_def_p
39172 = saved_colon_doesnt_start_class_def_p
;
39174 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39175 || types
.is_empty ())
39178 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39182 first_token
= cp_lexer_peek_token (parser
->lexer
);
39185 FOR_EACH_VEC_ELT (types
, i
, type
)
39188 = build_function_type_list (void_type_node
,
39189 cp_build_reference_type (type
, false),
39191 tree this_reduc_id
= reduc_id
;
39192 if (!dependent_type_p (type
))
39193 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39194 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39195 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39196 DECL_ARTIFICIAL (fndecl
) = 1;
39197 DECL_EXTERNAL (fndecl
) = 1;
39198 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39199 DECL_IGNORED_P (fndecl
) = 1;
39200 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39201 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39202 DECL_ATTRIBUTES (fndecl
)
39203 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39204 DECL_ATTRIBUTES (fndecl
));
39205 if (processing_template_decl
)
39206 fndecl
= push_template_decl (fndecl
);
39207 bool block_scope
= false;
39208 tree block
= NULL_TREE
;
39209 if (current_function_decl
)
39211 block_scope
= true;
39212 DECL_CONTEXT (fndecl
) = global_namespace
;
39213 if (!processing_template_decl
)
39216 else if (current_class_type
)
39220 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39221 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39222 cp_lexer_consume_token (parser
->lexer
);
39223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39225 cp
= cp_token_cache_new (first_token
,
39226 cp_lexer_peek_nth_token (parser
->lexer
,
39229 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39230 finish_member_declaration (fndecl
);
39231 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39232 DECL_PENDING_INLINE_P (fndecl
) = 1;
39233 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39238 DECL_CONTEXT (fndecl
) = current_namespace
;
39242 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39244 block
= begin_omp_structured_block ();
39247 cp_parser_push_lexer_for_tokens (parser
, cp
);
39248 parser
->lexer
->in_pragma
= true;
39250 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39253 finish_function (/*inline_p=*/false);
39255 DECL_CONTEXT (fndecl
) = current_function_decl
;
39257 cp_parser_pop_lexer (parser
);
39261 cp_parser_pop_lexer (parser
);
39263 finish_function (/*inline_p=*/false);
39266 DECL_CONTEXT (fndecl
) = current_function_decl
;
39267 block
= finish_omp_structured_block (block
);
39268 if (TREE_CODE (block
) == BIND_EXPR
)
39269 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39270 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39271 DECL_SAVED_TREE (fndecl
) = block
;
39272 if (processing_template_decl
)
39273 add_decl_expr (fndecl
);
39275 cp_check_omp_declare_reduction (fndecl
);
39276 if (cp
== NULL
&& types
.length () > 1)
39277 cp
= cp_token_cache_new (first_token
,
39278 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39279 if (errs
!= errorcount
)
39283 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39286 /* Free any declarators allocated. */
39287 obstack_free (&declarator_obstack
, p
);
39291 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39292 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39293 initializer-clause[opt] new-line
39294 #pragma omp declare target new-line */
39297 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39298 enum pragma_context context
)
39300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39302 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39303 const char *p
= IDENTIFIER_POINTER (id
);
39305 if (strcmp (p
, "simd") == 0)
39307 cp_lexer_consume_token (parser
->lexer
);
39308 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39312 cp_ensure_no_omp_declare_simd (parser
);
39313 if (strcmp (p
, "reduction") == 0)
39315 cp_lexer_consume_token (parser
->lexer
);
39316 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39320 if (!flag_openmp
) /* flag_openmp_simd */
39322 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39325 if (strcmp (p
, "target") == 0)
39327 cp_lexer_consume_token (parser
->lexer
);
39328 cp_parser_omp_declare_target (parser
, pragma_tok
);
39332 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39334 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39339 #pragma omp requires clauses[optseq] new-line */
39342 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39345 enum omp_requires new_req
= (enum omp_requires
) 0;
39347 location_t loc
= pragma_tok
->location
;
39348 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39350 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39351 cp_lexer_consume_token (parser
->lexer
);
39355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39357 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39358 const char *p
= IDENTIFIER_POINTER (id
);
39359 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39360 enum omp_requires this_req
= (enum omp_requires
) 0;
39362 if (!strcmp (p
, "unified_address"))
39363 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39364 else if (!strcmp (p
, "unified_shared_memory"))
39365 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39366 else if (!strcmp (p
, "dynamic_allocators"))
39367 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39368 else if (!strcmp (p
, "reverse_offload"))
39369 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39370 else if (!strcmp (p
, "atomic_default_mem_order"))
39372 cp_lexer_consume_token (parser
->lexer
);
39374 matching_parens parens
;
39375 if (parens
.require_open (parser
))
39377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39379 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39380 p
= IDENTIFIER_POINTER (id
);
39382 if (!strcmp (p
, "seq_cst"))
39384 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39385 else if (!strcmp (p
, "relaxed"))
39387 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39388 else if (!strcmp (p
, "acq_rel"))
39390 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39394 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39395 "expected %<seq_cst%>, %<relaxed%> or "
39397 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39399 cp_lexer_consume_token (parser
->lexer
);
39402 cp_lexer_consume_token (parser
->lexer
);
39404 if (!parens
.require_close (parser
))
39405 cp_parser_skip_to_closing_parenthesis (parser
,
39406 /*recovering=*/true,
39407 /*or_comma=*/false,
39413 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39421 error_at (cloc
, "expected %<unified_address%>, "
39422 "%<unified_shared_memory%>, "
39423 "%<dynamic_allocators%>, "
39424 "%<reverse_offload%> "
39425 "or %<atomic_default_mem_order%> clause");
39426 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39430 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39431 "supported yet", p
);
39433 cp_lexer_consume_token (parser
->lexer
);
39436 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39438 if ((this_req
& new_req
) != 0)
39439 error_at (cloc
, "too many %qs clauses", p
);
39440 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39441 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39442 error_at (cloc
, "%qs clause used lexically after first "
39443 "target construct or offloading API", p
);
39445 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39447 error_at (cloc
, "too many %qs clauses",
39448 "atomic_default_mem_order");
39449 this_req
= (enum omp_requires
) 0;
39451 else if ((omp_requires_mask
39452 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39454 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39455 " clause in a single compilation unit");
39457 = (enum omp_requires
)
39459 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39461 else if ((omp_requires_mask
39462 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39463 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39464 "lexically after first %<atomic%> construct "
39465 "without memory order clause");
39466 new_req
= (enum omp_requires
) (new_req
| this_req
);
39468 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39474 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39477 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39483 #pragma omp taskloop taskloop-clause[optseq] new-line
39486 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39489 #define OMP_TASKLOOP_CLAUSE_MASK \
39490 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39508 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39509 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39512 tree clauses
, sb
, ret
;
39514 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39516 strcat (p_name
, " taskloop");
39517 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39518 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39520 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39521 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39523 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39525 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39526 const char *p
= IDENTIFIER_POINTER (id
);
39528 if (strcmp (p
, "simd") == 0)
39530 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39531 if (cclauses
== NULL
)
39532 cclauses
= cclauses_buf
;
39534 cp_lexer_consume_token (parser
->lexer
);
39535 if (!flag_openmp
) /* flag_openmp_simd */
39536 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39538 sb
= begin_omp_structured_block ();
39539 save
= cp_parser_begin_omp_structured_block (parser
);
39540 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39542 cp_parser_end_omp_structured_block (parser
, save
);
39543 tree body
= finish_omp_structured_block (sb
);
39546 ret
= make_node (OMP_TASKLOOP
);
39547 TREE_TYPE (ret
) = void_type_node
;
39548 OMP_FOR_BODY (ret
) = body
;
39549 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39550 SET_EXPR_LOCATION (ret
, loc
);
39555 if (!flag_openmp
) /* flag_openmp_simd */
39557 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39561 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39565 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39566 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39569 keep_next_level (true);
39570 sb
= begin_omp_structured_block ();
39571 save
= cp_parser_begin_omp_structured_block (parser
);
39573 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39576 cp_parser_end_omp_structured_block (parser
, save
);
39577 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39584 # pragma acc routine oacc-routine-clause[optseq] new-line
39585 function-definition
39587 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39590 #define OACC_ROUTINE_CLAUSE_MASK \
39591 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39597 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39598 component, which must resolve to a declared namespace-scope
39599 function. The clauses are either processed directly (for a named
39600 function), or defered until the immediatley following declaration
39604 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39605 enum pragma_context context
)
39607 gcc_checking_assert (context
== pragma_external
);
39608 /* The checking for "another pragma following this one" in the "no optional
39609 '( name )'" case makes sure that we dont re-enter. */
39610 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39612 cp_oacc_routine_data data
;
39613 data
.error_seen
= false;
39614 data
.fndecl_seen
= false;
39615 data
.tokens
= vNULL
;
39616 data
.clauses
= NULL_TREE
;
39617 data
.loc
= pragma_tok
->location
;
39618 /* It is safe to take the address of a local variable; it will only be
39619 used while this scope is live. */
39620 parser
->oacc_routine
= &data
;
39622 /* Look for optional '( name )'. */
39623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39625 matching_parens parens
;
39626 parens
.consume_open (parser
); /* '(' */
39628 /* We parse the name as an id-expression. If it resolves to
39629 anything other than a non-overloaded function at namespace
39630 scope, it's an error. */
39631 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39632 tree name
= cp_parser_id_expression (parser
,
39633 /*template_keyword_p=*/false,
39634 /*check_dependency_p=*/false,
39635 /*template_p=*/NULL
,
39636 /*declarator_p=*/false,
39637 /*optional_p=*/false);
39638 tree decl
= (identifier_p (name
)
39639 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39641 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39642 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39644 if (decl
== error_mark_node
39645 || !parens
.require_close (parser
))
39647 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39648 parser
->oacc_routine
= NULL
;
39653 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39654 "#pragma acc routine",
39655 cp_lexer_peek_token (parser
->lexer
));
39657 if (decl
&& is_overloaded_fn (decl
)
39658 && (TREE_CODE (decl
) != FUNCTION_DECL
39659 || DECL_FUNCTION_TEMPLATE_P (decl
)))
39661 error_at (name_loc
,
39662 "%<#pragma acc routine%> names a set of overloads");
39663 parser
->oacc_routine
= NULL
;
39667 /* Perhaps we should use the same rule as declarations in different
39669 if (!DECL_NAMESPACE_SCOPE_P (decl
))
39671 error_at (name_loc
,
39672 "%qD does not refer to a namespace scope function", decl
);
39673 parser
->oacc_routine
= NULL
;
39677 if (TREE_CODE (decl
) != FUNCTION_DECL
)
39679 error_at (name_loc
, "%qD does not refer to a function", decl
);
39680 parser
->oacc_routine
= NULL
;
39684 cp_finalize_oacc_routine (parser
, decl
, false);
39685 parser
->oacc_routine
= NULL
;
39687 else /* No optional '( name )'. */
39689 /* Store away all pragma tokens. */
39690 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39691 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39692 cp_lexer_consume_token (parser
->lexer
);
39693 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39694 parser
->oacc_routine
->error_seen
= true;
39695 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39696 struct cp_token_cache
*cp
39697 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39698 parser
->oacc_routine
->tokens
.safe_push (cp
);
39700 /* Emit a helpful diagnostic if there's another pragma following this
39702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39704 cp_ensure_no_oacc_routine (parser
);
39705 data
.tokens
.release ();
39706 /* ..., and then just keep going. */
39710 /* We only have to consider the pragma_external case here. */
39711 cp_parser_declaration (parser
);
39712 if (parser
->oacc_routine
39713 && !parser
->oacc_routine
->fndecl_seen
)
39714 cp_ensure_no_oacc_routine (parser
);
39716 parser
->oacc_routine
= NULL
;
39717 data
.tokens
.release ();
39721 /* Finalize #pragma acc routine clauses after direct declarator has
39725 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
39727 struct cp_token_cache
*ce
;
39728 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
39730 if (!data
->error_seen
&& data
->fndecl_seen
)
39732 error_at (data
->loc
,
39733 "%<#pragma acc routine%> not immediately followed by "
39734 "a single function declaration or definition");
39735 data
->error_seen
= true;
39737 if (data
->error_seen
)
39740 gcc_checking_assert (data
->tokens
.length () == 1);
39741 ce
= data
->tokens
[0];
39743 cp_parser_push_lexer_for_tokens (parser
, ce
);
39744 parser
->lexer
->in_pragma
= true;
39745 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39747 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39748 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
39749 parser
->oacc_routine
->clauses
39750 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39751 "#pragma acc routine", pragma_tok
);
39752 cp_parser_pop_lexer (parser
);
39753 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39759 /* Apply any saved OpenACC routine clauses to a just-parsed
39763 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
39765 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
39767 /* Keep going if we're in error reporting mode. */
39768 if (parser
->oacc_routine
->error_seen
39769 || fndecl
== error_mark_node
)
39772 if (parser
->oacc_routine
->fndecl_seen
)
39774 error_at (parser
->oacc_routine
->loc
,
39775 "%<#pragma acc routine%> not immediately followed by"
39776 " a single function declaration or definition");
39777 parser
->oacc_routine
= NULL
;
39780 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
39782 cp_ensure_no_oacc_routine (parser
);
39786 if (oacc_get_fn_attrib (fndecl
))
39788 error_at (parser
->oacc_routine
->loc
,
39789 "%<#pragma acc routine%> already applied to %qD", fndecl
);
39790 parser
->oacc_routine
= NULL
;
39794 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
39796 error_at (parser
->oacc_routine
->loc
,
39798 ? G_("%<#pragma acc routine%> must be applied before use")
39799 : G_("%<#pragma acc routine%> must be applied before "
39801 parser
->oacc_routine
= NULL
;
39805 /* Process the routine's dimension clauses. */
39806 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
39807 oacc_replace_fn_attrib (fndecl
, dims
);
39809 /* Add an "omp declare target" attribute. */
39810 DECL_ATTRIBUTES (fndecl
)
39811 = tree_cons (get_identifier ("omp declare target"),
39812 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
39814 /* Don't unset parser->oacc_routine here: we may still need it to
39815 diagnose wrong usage. But, remember that we've used this "#pragma acc
39817 parser
->oacc_routine
->fndecl_seen
= true;
39821 /* Main entry point to OpenMP statement pragmas. */
39824 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39827 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
39828 omp_clause_mask
mask (0);
39830 switch (cp_parser_pragma_kind (pragma_tok
))
39832 case PRAGMA_OACC_ATOMIC
:
39833 cp_parser_omp_atomic (parser
, pragma_tok
);
39835 case PRAGMA_OACC_CACHE
:
39836 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
39838 case PRAGMA_OACC_DATA
:
39839 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
39841 case PRAGMA_OACC_ENTER_DATA
:
39842 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
39844 case PRAGMA_OACC_EXIT_DATA
:
39845 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
39847 case PRAGMA_OACC_HOST_DATA
:
39848 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
39850 case PRAGMA_OACC_KERNELS
:
39851 case PRAGMA_OACC_PARALLEL
:
39852 strcpy (p_name
, "#pragma acc");
39853 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
39856 case PRAGMA_OACC_LOOP
:
39857 strcpy (p_name
, "#pragma acc");
39858 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39861 case PRAGMA_OACC_UPDATE
:
39862 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
39864 case PRAGMA_OACC_WAIT
:
39865 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
39867 case PRAGMA_OMP_ATOMIC
:
39868 cp_parser_omp_atomic (parser
, pragma_tok
);
39870 case PRAGMA_OMP_CRITICAL
:
39871 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
39873 case PRAGMA_OMP_DISTRIBUTE
:
39874 strcpy (p_name
, "#pragma omp");
39875 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
39878 case PRAGMA_OMP_FOR
:
39879 strcpy (p_name
, "#pragma omp");
39880 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
39883 case PRAGMA_OMP_MASTER
:
39884 strcpy (p_name
, "#pragma omp");
39885 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
39888 case PRAGMA_OMP_PARALLEL
:
39889 strcpy (p_name
, "#pragma omp");
39890 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
39893 case PRAGMA_OMP_SECTIONS
:
39894 strcpy (p_name
, "#pragma omp");
39895 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
39897 case PRAGMA_OMP_SIMD
:
39898 strcpy (p_name
, "#pragma omp");
39899 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
39902 case PRAGMA_OMP_SINGLE
:
39903 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
39905 case PRAGMA_OMP_TASK
:
39906 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
39908 case PRAGMA_OMP_TASKGROUP
:
39909 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
39911 case PRAGMA_OMP_TASKLOOP
:
39912 strcpy (p_name
, "#pragma omp");
39913 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39916 case PRAGMA_OMP_TEAMS
:
39917 strcpy (p_name
, "#pragma omp");
39918 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
39922 gcc_unreachable ();
39925 protected_set_expr_location (stmt
, pragma_tok
->location
);
39928 /* Transactional Memory parsing routines. */
39930 /* Parse a transaction attribute.
39936 We use this instead of cp_parser_attributes_opt for transactions to avoid
39937 the pedwarn in C++98 mode. */
39940 cp_parser_txn_attribute_opt (cp_parser
*parser
)
39943 tree attr_name
, attr
= NULL
;
39945 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
39946 return cp_parser_attributes_opt (parser
);
39948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
39950 cp_lexer_consume_token (parser
->lexer
);
39951 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
39954 token
= cp_lexer_peek_token (parser
->lexer
);
39955 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
39957 token
= cp_lexer_consume_token (parser
->lexer
);
39959 attr_name
= (token
->type
== CPP_KEYWORD
39960 /* For keywords, use the canonical spelling,
39961 not the parsed identifier. */
39962 ? ridpointers
[(int) token
->keyword
]
39964 attr
= build_tree_list (attr_name
, NULL_TREE
);
39967 cp_parser_error (parser
, "expected identifier");
39969 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39971 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39975 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39977 transaction-statement:
39978 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39980 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39984 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
39986 unsigned char old_in
= parser
->in_transaction
;
39987 unsigned char this_in
= 1, new_in
;
39988 enum rid keyword
= token
->keyword
;
39989 tree stmt
, attrs
, noex
;
39991 cp_lexer_consume_token (parser
->lexer
);
39993 if (keyword
== RID_TRANSACTION_RELAXED
39994 || keyword
== RID_SYNCHRONIZED
)
39995 this_in
|= TM_STMT_ATTR_RELAXED
;
39998 attrs
= cp_parser_txn_attribute_opt (parser
);
40000 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40003 /* Parse a noexcept specification. */
40004 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40005 noex
= boolean_true_node
;
40006 else if (keyword
== RID_ATOMIC_CANCEL
)
40008 /* cancel-and-throw is unimplemented. */
40009 sorry ("atomic_cancel");
40013 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40015 /* Keep track if we're in the lexical scope of an outer transaction. */
40016 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40018 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40020 parser
->in_transaction
= new_in
;
40021 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40022 parser
->in_transaction
= old_in
;
40024 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40029 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40031 transaction-expression:
40032 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40033 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40037 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40039 unsigned char old_in
= parser
->in_transaction
;
40040 unsigned char this_in
= 1;
40044 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40046 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40047 || keyword
== RID_TRANSACTION_RELAXED
);
40051 keyword
== RID_TRANSACTION_RELAXED
40052 ? G_("%<__transaction_relaxed%> without transactional memory "
40054 : G_("%<__transaction_atomic%> without transactional memory "
40055 "support enabled"));
40057 token
= cp_parser_require_keyword (parser
, keyword
,
40058 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40059 : RT_TRANSACTION_RELAXED
));
40060 gcc_assert (token
!= NULL
);
40062 if (keyword
== RID_TRANSACTION_RELAXED
)
40063 this_in
|= TM_STMT_ATTR_RELAXED
;
40065 /* Set this early. This might mean that we allow transaction_cancel in
40066 an expression that we find out later actually has to be a constexpr.
40067 However, we expect that cxx_constant_value will be able to deal with
40068 this; also, if the noexcept has no constexpr, then what we parse next
40069 really is a transaction's body. */
40070 parser
->in_transaction
= this_in
;
40072 /* Parse a noexcept specification. */
40073 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40076 if (!noex
|| !noex_expr
40077 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40079 matching_parens parens
;
40080 parens
.require_open (parser
);
40082 expr
= cp_parser_expression (parser
);
40083 expr
= finish_parenthesized_expr (expr
);
40085 parens
.require_close (parser
);
40089 /* The only expression that is available got parsed for the noexcept
40090 already. noexcept is true then. */
40092 noex
= boolean_true_node
;
40095 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40096 parser
->in_transaction
= old_in
;
40098 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40099 return error_mark_node
;
40101 return (flag_tm
? expr
: error_mark_node
);
40104 /* Parse a function-transaction-block.
40106 function-transaction-block:
40107 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40109 __transaction_atomic txn-attribute[opt] function-try-block
40110 __transaction_relaxed ctor-initializer[opt] function-body
40111 __transaction_relaxed function-try-block
40115 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40117 unsigned char old_in
= parser
->in_transaction
;
40118 unsigned char new_in
= 1;
40119 tree compound_stmt
, stmt
, attrs
;
40122 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40123 || keyword
== RID_TRANSACTION_RELAXED
);
40124 token
= cp_parser_require_keyword (parser
, keyword
,
40125 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40126 : RT_TRANSACTION_RELAXED
));
40127 gcc_assert (token
!= NULL
);
40129 if (keyword
== RID_TRANSACTION_RELAXED
)
40130 new_in
|= TM_STMT_ATTR_RELAXED
;
40133 attrs
= cp_parser_txn_attribute_opt (parser
);
40135 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40138 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40140 parser
->in_transaction
= new_in
;
40142 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40143 cp_parser_function_try_block (parser
);
40145 cp_parser_ctor_initializer_opt_and_function_body
40146 (parser
, /*in_function_try_block=*/false);
40148 parser
->in_transaction
= old_in
;
40150 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40153 /* Parse a __transaction_cancel statement.
40156 __transaction_cancel txn-attribute[opt] ;
40157 __transaction_cancel txn-attribute[opt] throw-expression ;
40159 ??? Cancel and throw is not yet implemented. */
40162 cp_parser_transaction_cancel (cp_parser
*parser
)
40165 bool is_outer
= false;
40168 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40169 RT_TRANSACTION_CANCEL
);
40170 gcc_assert (token
!= NULL
);
40172 attrs
= cp_parser_txn_attribute_opt (parser
);
40174 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40176 /* ??? Parse cancel-and-throw here. */
40178 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40182 error_at (token
->location
, "%<__transaction_cancel%> without "
40183 "transactional memory support enabled");
40184 return error_mark_node
;
40186 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40188 error_at (token
->location
, "%<__transaction_cancel%> within a "
40189 "%<__transaction_relaxed%>");
40190 return error_mark_node
;
40194 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40195 && !is_tm_may_cancel_outer (current_function_decl
))
40197 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40198 "within outer %<__transaction_atomic%>");
40199 error_at (token
->location
,
40200 " or a %<transaction_may_cancel_outer%> function");
40201 return error_mark_node
;
40204 else if (parser
->in_transaction
== 0)
40206 error_at (token
->location
, "%<__transaction_cancel%> not within "
40207 "%<__transaction_atomic%>");
40208 return error_mark_node
;
40211 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40219 static GTY (()) cp_parser
*the_parser
;
40222 /* Special handling for the first token or line in the file. The first
40223 thing in the file might be #pragma GCC pch_preprocess, which loads a
40224 PCH file, which is a GC collection point. So we need to handle this
40225 first pragma without benefit of an existing lexer structure.
40227 Always returns one token to the caller in *FIRST_TOKEN. This is
40228 either the true first token of the file, or the first token after
40229 the initial pragma. */
40232 cp_parser_initial_pragma (cp_token
*first_token
)
40236 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40237 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40240 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40241 if (first_token
->type
== CPP_STRING
)
40243 name
= first_token
->u
.value
;
40245 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40246 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40247 error_at (first_token
->location
,
40248 "junk at end of %<#pragma GCC pch_preprocess%>");
40251 error_at (first_token
->location
, "expected string literal");
40253 /* Skip to the end of the pragma. */
40254 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40255 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40257 /* Now actually load the PCH file. */
40259 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40261 /* Read one more token to return to our caller. We have to do this
40262 after reading the PCH file in, since its pointers have to be
40264 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40267 /* Parse a pragma GCC ivdep. */
40270 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40272 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40276 /* Parse a pragma GCC unroll. */
40278 static unsigned short
40279 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40281 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40282 tree expr
= cp_parser_constant_expression (parser
);
40283 unsigned short unroll
;
40284 expr
= maybe_constant_value (expr
);
40285 HOST_WIDE_INT lunroll
= 0;
40286 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40287 || TREE_CODE (expr
) != INTEGER_CST
40288 || (lunroll
= tree_to_shwi (expr
)) < 0
40289 || lunroll
>= USHRT_MAX
)
40291 error_at (location
, "%<#pragma GCC unroll%> requires an"
40292 " assignment-expression that evaluates to a non-negative"
40293 " integral constant less than %u", USHRT_MAX
);
40298 unroll
= (unsigned short)lunroll
;
40302 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40306 /* Normal parsing of a pragma token. Here we can (and must) use the
40310 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40312 cp_token
*pragma_tok
;
40317 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40318 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40319 parser
->lexer
->in_pragma
= true;
40321 id
= cp_parser_pragma_kind (pragma_tok
);
40322 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40323 cp_ensure_no_omp_declare_simd (parser
);
40326 case PRAGMA_GCC_PCH_PREPROCESS
:
40327 error_at (pragma_tok
->location
,
40328 "%<#pragma GCC pch_preprocess%> must be first");
40331 case PRAGMA_OMP_BARRIER
:
40334 case pragma_compound
:
40335 cp_parser_omp_barrier (parser
, pragma_tok
);
40338 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40339 "used in compound statements", "omp barrier");
40346 case PRAGMA_OMP_DEPOBJ
:
40349 case pragma_compound
:
40350 cp_parser_omp_depobj (parser
, pragma_tok
);
40353 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40354 "used in compound statements", "omp depobj");
40361 case PRAGMA_OMP_FLUSH
:
40364 case pragma_compound
:
40365 cp_parser_omp_flush (parser
, pragma_tok
);
40368 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40369 "used in compound statements", "omp flush");
40376 case PRAGMA_OMP_TASKWAIT
:
40379 case pragma_compound
:
40380 cp_parser_omp_taskwait (parser
, pragma_tok
);
40383 error_at (pragma_tok
->location
,
40384 "%<#pragma %s%> may only be used in compound statements",
40392 case PRAGMA_OMP_TASKYIELD
:
40395 case pragma_compound
:
40396 cp_parser_omp_taskyield (parser
, pragma_tok
);
40399 error_at (pragma_tok
->location
,
40400 "%<#pragma %s%> may only be used in compound statements",
40408 case PRAGMA_OMP_CANCEL
:
40411 case pragma_compound
:
40412 cp_parser_omp_cancel (parser
, pragma_tok
);
40415 error_at (pragma_tok
->location
,
40416 "%<#pragma %s%> may only be used in compound statements",
40424 case PRAGMA_OMP_CANCELLATION_POINT
:
40425 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40428 case PRAGMA_OMP_THREADPRIVATE
:
40429 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40432 case PRAGMA_OMP_DECLARE
:
40433 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40435 case PRAGMA_OACC_DECLARE
:
40436 cp_parser_oacc_declare (parser
, pragma_tok
);
40439 case PRAGMA_OACC_ENTER_DATA
:
40440 if (context
== pragma_stmt
)
40442 error_at (pragma_tok
->location
,
40443 "%<#pragma %s%> may only be used in compound statements",
40447 else if (context
!= pragma_compound
)
40449 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40452 case PRAGMA_OACC_EXIT_DATA
:
40453 if (context
== pragma_stmt
)
40455 error_at (pragma_tok
->location
,
40456 "%<#pragma %s%> may only be used in compound statements",
40460 else if (context
!= pragma_compound
)
40462 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40465 case PRAGMA_OACC_ROUTINE
:
40466 if (context
!= pragma_external
)
40468 error_at (pragma_tok
->location
,
40469 "%<#pragma acc routine%> must be at file scope");
40472 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40475 case PRAGMA_OACC_UPDATE
:
40476 if (context
== pragma_stmt
)
40478 error_at (pragma_tok
->location
,
40479 "%<#pragma %s%> may only be used in compound statements",
40483 else if (context
!= pragma_compound
)
40485 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40488 case PRAGMA_OACC_WAIT
:
40489 if (context
== pragma_stmt
)
40491 error_at (pragma_tok
->location
,
40492 "%<#pragma %s%> may only be used in compound statements",
40496 else if (context
!= pragma_compound
)
40498 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40501 case PRAGMA_OACC_ATOMIC
:
40502 case PRAGMA_OACC_CACHE
:
40503 case PRAGMA_OACC_DATA
:
40504 case PRAGMA_OACC_HOST_DATA
:
40505 case PRAGMA_OACC_KERNELS
:
40506 case PRAGMA_OACC_PARALLEL
:
40507 case PRAGMA_OACC_LOOP
:
40508 case PRAGMA_OMP_ATOMIC
:
40509 case PRAGMA_OMP_CRITICAL
:
40510 case PRAGMA_OMP_DISTRIBUTE
:
40511 case PRAGMA_OMP_FOR
:
40512 case PRAGMA_OMP_MASTER
:
40513 case PRAGMA_OMP_PARALLEL
:
40514 case PRAGMA_OMP_SECTIONS
:
40515 case PRAGMA_OMP_SIMD
:
40516 case PRAGMA_OMP_SINGLE
:
40517 case PRAGMA_OMP_TASK
:
40518 case PRAGMA_OMP_TASKGROUP
:
40519 case PRAGMA_OMP_TASKLOOP
:
40520 case PRAGMA_OMP_TEAMS
:
40521 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40523 stmt
= push_omp_privatization_clauses (false);
40524 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40525 pop_omp_privatization_clauses (stmt
);
40528 case PRAGMA_OMP_REQUIRES
:
40529 return cp_parser_omp_requires (parser
, pragma_tok
);
40531 case PRAGMA_OMP_ORDERED
:
40532 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40534 stmt
= push_omp_privatization_clauses (false);
40535 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40536 pop_omp_privatization_clauses (stmt
);
40539 case PRAGMA_OMP_TARGET
:
40540 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40542 stmt
= push_omp_privatization_clauses (false);
40543 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40544 pop_omp_privatization_clauses (stmt
);
40547 case PRAGMA_OMP_END_DECLARE_TARGET
:
40548 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40551 case PRAGMA_OMP_SECTION
:
40552 error_at (pragma_tok
->location
,
40553 "%<#pragma omp section%> may only be used in "
40554 "%<#pragma omp sections%> construct");
40559 if (context
== pragma_external
)
40561 error_at (pragma_tok
->location
,
40562 "%<#pragma GCC ivdep%> must be inside a function");
40565 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40566 unsigned short unroll
;
40567 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40568 if (tok
->type
== CPP_PRAGMA
40569 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40571 tok
= cp_lexer_consume_token (parser
->lexer
);
40572 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40573 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40577 if (tok
->type
!= CPP_KEYWORD
40578 || (tok
->keyword
!= RID_FOR
40579 && tok
->keyword
!= RID_WHILE
40580 && tok
->keyword
!= RID_DO
))
40582 cp_parser_error (parser
, "for, while or do statement expected");
40585 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40589 case PRAGMA_UNROLL
:
40591 if (context
== pragma_external
)
40593 error_at (pragma_tok
->location
,
40594 "%<#pragma GCC unroll%> must be inside a function");
40597 const unsigned short unroll
40598 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40600 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40601 if (tok
->type
== CPP_PRAGMA
40602 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40604 tok
= cp_lexer_consume_token (parser
->lexer
);
40605 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40606 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40610 if (tok
->type
!= CPP_KEYWORD
40611 || (tok
->keyword
!= RID_FOR
40612 && tok
->keyword
!= RID_WHILE
40613 && tok
->keyword
!= RID_DO
))
40615 cp_parser_error (parser
, "for, while or do statement expected");
40618 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40623 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40624 c_invoke_pragma_handler (id
);
40628 cp_parser_error (parser
, "expected declaration specifiers");
40632 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40636 /* The interface the pragma parsers have to the lexer. */
40639 pragma_lex (tree
*value
, location_t
*loc
)
40641 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40642 enum cpp_ttype ret
= tok
->type
;
40644 *value
= tok
->u
.value
;
40646 *loc
= tok
->location
;
40648 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
40650 else if (ret
== CPP_STRING
)
40651 *value
= cp_parser_string_literal (the_parser
, false, false);
40654 if (ret
== CPP_KEYWORD
)
40656 cp_lexer_consume_token (the_parser
->lexer
);
40663 /* External interface. */
40665 /* Parse one entire translation unit. */
40668 c_parse_file (void)
40670 static bool already_called
= false;
40672 if (already_called
)
40673 fatal_error (input_location
,
40674 "inter-module optimizations not implemented for C++");
40675 already_called
= true;
40677 the_parser
= cp_parser_new ();
40678 push_deferring_access_checks (flag_access_control
40679 ? dk_no_deferred
: dk_no_check
);
40680 cp_parser_translation_unit (the_parser
);
40683 finish_translation_unit ();
40686 /* Create an identifier for a generic parameter type (a synthesized
40687 template parameter implied by `auto' or a concept identifier). */
40689 static GTY(()) int generic_parm_count
;
40691 make_generic_type_name ()
40694 sprintf (buf
, "auto:%d", ++generic_parm_count
);
40695 return get_identifier (buf
);
40698 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40699 (creating a new template parameter list if necessary). Returns the newly
40700 created template type parm. */
40703 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
40705 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
40707 /* Before committing to modifying any scope, if we're in an
40708 implicit template scope, and we're trying to synthesize a
40709 constrained parameter, try to find a previous parameter with
40710 the same name. This is the same-type rule for abbreviated
40711 function templates.
40713 NOTE: We can generate implicit parameters when tentatively
40714 parsing a nested name specifier, only to reject that parse
40715 later. However, matching the same template-id as part of a
40716 direct-declarator should generate an identical template
40717 parameter, so this rule will merge them. */
40718 if (parser
->implicit_template_scope
&& constr
)
40720 tree t
= parser
->implicit_template_parms
;
40723 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
40725 tree d
= TREE_VALUE (t
);
40726 if (TREE_CODE (d
) == PARM_DECL
)
40727 /* Return the TEMPLATE_PARM_INDEX. */
40728 d
= DECL_INITIAL (d
);
40731 t
= TREE_CHAIN (t
);
40735 /* We are either continuing a function template that already contains implicit
40736 template parameters, creating a new fully-implicit function template, or
40737 extending an existing explicit function template with implicit template
40740 cp_binding_level
*const entry_scope
= current_binding_level
;
40742 bool become_template
= false;
40743 cp_binding_level
*parent_scope
= 0;
40745 if (parser
->implicit_template_scope
)
40747 gcc_assert (parser
->implicit_template_parms
);
40749 current_binding_level
= parser
->implicit_template_scope
;
40753 /* Roll back to the existing template parameter scope (in the case of
40754 extending an explicit function template) or introduce a new template
40755 parameter scope ahead of the function parameter scope (or class scope
40756 in the case of out-of-line member definitions). The function scope is
40757 added back after template parameter synthesis below. */
40759 cp_binding_level
*scope
= entry_scope
;
40761 while (scope
->kind
== sk_function_parms
)
40763 parent_scope
= scope
;
40764 scope
= scope
->level_chain
;
40766 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
40768 /* If not defining a class, then any class scope is a scope level in
40769 an out-of-line member definition. In this case simply wind back
40770 beyond the first such scope to inject the template parameter list.
40771 Otherwise wind back to the class being defined. The latter can
40772 occur in class member friend declarations such as:
40778 friend void A::foo (auto);
40781 The template parameter list synthesized for the friend declaration
40782 must be injected in the scope of 'B'. This can also occur in
40783 erroneous cases such as:
40789 void B::foo (auto) {}
40792 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40793 but, nevertheless, the template parameter list synthesized for the
40794 declarator should be injected into the scope of 'A' as if the
40795 ill-formed template was specified explicitly. */
40797 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
40799 parent_scope
= scope
;
40800 scope
= scope
->level_chain
;
40804 current_binding_level
= scope
;
40806 if (scope
->kind
!= sk_template_parms
40807 || !function_being_declared_is_template_p (parser
))
40809 /* Introduce a new template parameter list for implicit template
40812 become_template
= true;
40814 parser
->implicit_template_scope
40815 = begin_scope (sk_template_parms
, NULL
);
40817 ++processing_template_decl
;
40819 parser
->fully_implicit_function_template_p
= true;
40820 ++parser
->num_template_parameter_lists
;
40824 /* Synthesize implicit template parameters at the end of the explicit
40825 template parameter list. */
40827 gcc_assert (current_template_parms
);
40829 parser
->implicit_template_scope
= scope
;
40831 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40832 parser
->implicit_template_parms
40833 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
40837 /* Synthesize a new template parameter and track the current template
40838 parameter chain with implicit_template_parms. */
40840 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
40841 tree synth_id
= make_generic_type_name ();
40842 tree synth_tmpl_parm
;
40843 bool non_type
= false;
40845 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
40847 = finish_template_type_parm (class_type_node
, synth_id
);
40848 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
40850 = finish_constrained_template_template_parm (proto
, synth_id
);
40853 synth_tmpl_parm
= copy_decl (proto
);
40854 DECL_NAME (synth_tmpl_parm
) = synth_id
;
40858 // Attach the constraint to the parm before processing.
40859 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
40860 TREE_TYPE (node
) = constr
;
40862 = process_template_parm (parser
->implicit_template_parms
,
40865 /*non_type=*/non_type
,
40866 /*param_pack=*/false);
40868 // Chain the new parameter to the list of implicit parameters.
40869 if (parser
->implicit_template_parms
)
40870 parser
->implicit_template_parms
40871 = TREE_CHAIN (parser
->implicit_template_parms
);
40873 parser
->implicit_template_parms
= new_parm
;
40875 tree new_decl
= get_local_decls ();
40877 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40878 new_decl
= DECL_INITIAL (new_decl
);
40880 /* If creating a fully implicit function template, start the new implicit
40881 template parameter list with this synthesized type, otherwise grow the
40882 current template parameter list. */
40884 if (become_template
)
40886 parent_scope
->level_chain
= current_binding_level
;
40888 tree new_parms
= make_tree_vec (1);
40889 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
40890 current_template_parms
= tree_cons (size_int (processing_template_decl
),
40891 new_parms
, current_template_parms
);
40895 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40896 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
40897 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
40898 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
40901 // If the new parameter was constrained, we need to add that to the
40902 // constraints in the template parameter list.
40903 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
40905 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
40906 reqs
= conjoin_constraints (reqs
, req
);
40907 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
40910 current_binding_level
= entry_scope
;
40915 /* Finish the declaration of a fully implicit function template. Such a
40916 template has no explicit template parameter list so has not been through the
40917 normal template head and tail processing. synthesize_implicit_template_parm
40918 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40919 provided if the declaration is a class member such that its template
40920 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40921 form is returned. Otherwise NULL_TREE is returned. */
40924 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
40926 gcc_assert (parser
->fully_implicit_function_template_p
);
40928 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
40929 && DECL_VIRTUAL_P (member_decl_opt
))
40931 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
40932 "implicit templates may not be %<virtual%>");
40933 DECL_VIRTUAL_P (member_decl_opt
) = false;
40936 if (member_decl_opt
)
40937 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
40938 end_template_decl ();
40940 parser
->fully_implicit_function_template_p
= false;
40941 parser
->implicit_template_parms
= 0;
40942 parser
->implicit_template_scope
= 0;
40943 --parser
->num_template_parameter_lists
;
40945 return member_decl_opt
;
40948 /* Like finish_fully_implicit_template, but to be used in error
40949 recovery, rearranging scopes so that we restore the state we had
40950 before synthesize_implicit_template_parm inserted the implement
40951 template parms scope. */
40954 abort_fully_implicit_template (cp_parser
*parser
)
40956 cp_binding_level
*return_to_scope
= current_binding_level
;
40958 if (parser
->implicit_template_scope
40959 && return_to_scope
!= parser
->implicit_template_scope
)
40961 cp_binding_level
*child
= return_to_scope
;
40962 for (cp_binding_level
*scope
= child
->level_chain
;
40963 scope
!= parser
->implicit_template_scope
;
40964 scope
= child
->level_chain
)
40966 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
40967 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
40968 current_binding_level
= parser
->implicit_template_scope
;
40971 return_to_scope
= return_to_scope
->level_chain
;
40973 finish_fully_implicit_template (parser
, NULL
);
40975 gcc_assert (current_binding_level
== return_to_scope
);
40978 /* Helper function for diagnostics that have complained about things
40979 being used with 'extern "C"' linkage.
40981 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40984 maybe_show_extern_c_location (void)
40986 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
40987 inform (the_parser
->innermost_linkage_specification_location
,
40988 "%<extern \"C\"%> linkage started here");
40991 #include "gt-cp-parser.h"