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_new_expression
2053 static vec
<tree
, va_gc
> *cp_parser_new_placement
2055 static tree cp_parser_new_type_id
2056 (cp_parser
*, tree
*);
2057 static cp_declarator
*cp_parser_new_declarator_opt
2059 static cp_declarator
*cp_parser_direct_new_declarator
2061 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2063 static tree cp_parser_delete_expression
2065 static cp_expr cp_parser_cast_expression
2066 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2067 static cp_expr cp_parser_binary_expression
2068 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2069 static tree cp_parser_question_colon_clause
2070 (cp_parser
*, cp_expr
);
2071 static cp_expr cp_parser_assignment_expression
2072 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2075 static cp_expr cp_parser_expression
2076 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2079 static cp_expr cp_parser_builtin_offsetof
2081 static cp_expr cp_parser_lambda_expression
2083 static void cp_parser_lambda_introducer
2084 (cp_parser
*, tree
);
2085 static bool cp_parser_lambda_declarator_opt
2086 (cp_parser
*, tree
);
2087 static void cp_parser_lambda_body
2088 (cp_parser
*, tree
);
2090 /* Statements [gram.stmt.stmt] */
2092 static void cp_parser_statement
2093 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser
*, tree
);
2096 static tree cp_parser_expression_statement
2097 (cp_parser
*, tree
);
2098 static tree cp_parser_compound_statement
2099 (cp_parser
*, tree
, int, bool);
2100 static void cp_parser_statement_seq_opt
2101 (cp_parser
*, tree
);
2102 static tree cp_parser_selection_statement
2103 (cp_parser
*, bool *, vec
<tree
> *);
2104 static tree cp_parser_condition
2106 static tree cp_parser_iteration_statement
2107 (cp_parser
*, bool *, bool, unsigned short);
2108 static bool cp_parser_init_statement
2109 (cp_parser
*, tree
*decl
);
2110 static tree cp_parser_for
2111 (cp_parser
*, bool, unsigned short);
2112 static tree cp_parser_c_for
2113 (cp_parser
*, tree
, tree
, bool, unsigned short);
2114 static tree cp_parser_range_for
2115 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2116 static void do_range_for_auto_deduction
2118 static tree cp_parser_perform_range_for_lookup
2119 (tree
, tree
*, tree
*);
2120 static tree cp_parser_range_for_member_function
2122 static tree cp_parser_jump_statement
2124 static void cp_parser_declaration_statement
2127 static tree cp_parser_implicitly_scoped_statement
2128 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2129 static void cp_parser_already_scoped_statement
2130 (cp_parser
*, bool *, const token_indent_info
&);
2132 /* Declarations [gram.dcl.dcl] */
2134 static void cp_parser_declaration_seq_opt
2136 static void cp_parser_declaration
2138 static void cp_parser_toplevel_declaration
2140 static void cp_parser_block_declaration
2141 (cp_parser
*, bool);
2142 static void cp_parser_simple_declaration
2143 (cp_parser
*, bool, tree
*);
2144 static void cp_parser_decl_specifier_seq
2145 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2146 static tree cp_parser_storage_class_specifier_opt
2148 static tree cp_parser_function_specifier_opt
2149 (cp_parser
*, cp_decl_specifier_seq
*);
2150 static tree cp_parser_type_specifier
2151 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2153 static tree cp_parser_simple_type_specifier
2154 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2155 static tree cp_parser_type_name
2156 (cp_parser
*, bool);
2157 static tree cp_parser_type_name
2159 static tree cp_parser_nonclass_name
2160 (cp_parser
* parser
);
2161 static tree cp_parser_elaborated_type_specifier
2162 (cp_parser
*, bool, bool);
2163 static tree cp_parser_enum_specifier
2165 static void cp_parser_enumerator_list
2166 (cp_parser
*, tree
);
2167 static void cp_parser_enumerator_definition
2168 (cp_parser
*, tree
);
2169 static tree cp_parser_namespace_name
2171 static void cp_parser_namespace_definition
2173 static void cp_parser_namespace_body
2175 static tree cp_parser_qualified_namespace_specifier
2177 static void cp_parser_namespace_alias_definition
2179 static bool cp_parser_using_declaration
2180 (cp_parser
*, bool);
2181 static void cp_parser_using_directive
2183 static tree cp_parser_alias_declaration
2185 static void cp_parser_asm_definition
2187 static void cp_parser_linkage_specification
2189 static void cp_parser_static_assert
2190 (cp_parser
*, bool);
2191 static tree cp_parser_decltype
2193 static tree cp_parser_decomposition_declaration
2194 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2196 /* Declarators [gram.dcl.decl] */
2198 static tree cp_parser_init_declarator
2199 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2200 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2201 static cp_declarator
*cp_parser_declarator
2202 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2203 static cp_declarator
*cp_parser_direct_declarator
2204 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2205 static enum tree_code cp_parser_ptr_operator
2206 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2207 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2209 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2211 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2213 static tree cp_parser_tx_qualifier_opt
2215 static tree cp_parser_late_return_type_opt
2216 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2217 static tree cp_parser_declarator_id
2218 (cp_parser
*, bool);
2219 static tree cp_parser_type_id
2220 (cp_parser
*, location_t
* = NULL
);
2221 static tree cp_parser_template_type_arg
2223 static tree
cp_parser_trailing_type_id (cp_parser
*);
2224 static tree cp_parser_type_id_1
2225 (cp_parser
*, bool, bool, location_t
*);
2226 static void cp_parser_type_specifier_seq
2227 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2228 static tree cp_parser_parameter_declaration_clause
2230 static tree cp_parser_parameter_declaration_list
2232 static cp_parameter_declarator
*cp_parser_parameter_declaration
2233 (cp_parser
*, bool, bool *);
2234 static tree cp_parser_default_argument
2235 (cp_parser
*, bool);
2236 static void cp_parser_function_body
2237 (cp_parser
*, bool);
2238 static tree cp_parser_initializer
2239 (cp_parser
*, bool *, bool *, bool = false);
2240 static cp_expr cp_parser_initializer_clause
2241 (cp_parser
*, bool *);
2242 static cp_expr cp_parser_braced_list
2243 (cp_parser
*, bool*);
2244 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2245 (cp_parser
*, bool *);
2247 static void cp_parser_ctor_initializer_opt_and_function_body
2248 (cp_parser
*, bool);
2250 static tree cp_parser_late_parsing_omp_declare_simd
2251 (cp_parser
*, tree
);
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser
*, tree
);
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser
*, tree
);
2258 static tree finish_fully_implicit_template
2259 (cp_parser
*, tree
);
2260 static void abort_fully_implicit_template
2263 /* Classes [gram.class] */
2265 static tree cp_parser_class_name
2266 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2267 static tree cp_parser_class_specifier
2269 static tree cp_parser_class_head
2270 (cp_parser
*, bool *);
2271 static enum tag_types cp_parser_class_key
2273 static void cp_parser_type_parameter_key
2274 (cp_parser
* parser
);
2275 static void cp_parser_member_specification_opt
2277 static void cp_parser_member_declaration
2279 static tree cp_parser_pure_specifier
2281 static tree cp_parser_constant_initializer
2284 /* Derived classes [gram.class.derived] */
2286 static tree cp_parser_base_clause
2288 static tree cp_parser_base_specifier
2291 /* Special member functions [gram.special] */
2293 static tree cp_parser_conversion_function_id
2295 static tree cp_parser_conversion_type_id
2297 static cp_declarator
*cp_parser_conversion_declarator_opt
2299 static void cp_parser_ctor_initializer_opt
2301 static void cp_parser_mem_initializer_list
2303 static tree cp_parser_mem_initializer
2305 static tree cp_parser_mem_initializer_id
2308 /* Overloading [gram.over] */
2310 static cp_expr cp_parser_operator_function_id
2312 static cp_expr cp_parser_operator
2315 /* Templates [gram.temp] */
2317 static void cp_parser_template_declaration
2318 (cp_parser
*, bool);
2319 static tree cp_parser_template_parameter_list
2321 static tree cp_parser_template_parameter
2322 (cp_parser
*, bool *, bool *);
2323 static tree cp_parser_type_parameter
2324 (cp_parser
*, bool *);
2325 static tree cp_parser_template_id
2326 (cp_parser
*, bool, bool, enum tag_types
, bool);
2327 static tree cp_parser_template_name
2328 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2329 static tree cp_parser_template_argument_list
2331 static tree cp_parser_template_argument
2333 static void cp_parser_explicit_instantiation
2335 static void cp_parser_explicit_specialization
2338 /* Exception handling [gram.exception] */
2340 static tree cp_parser_try_block
2342 static void cp_parser_function_try_block
2344 static void cp_parser_handler_seq
2346 static void cp_parser_handler
2348 static tree cp_parser_exception_declaration
2350 static tree cp_parser_throw_expression
2352 static tree cp_parser_exception_specification_opt
2354 static tree cp_parser_type_id_list
2357 /* GNU Extensions */
2359 static tree cp_parser_asm_specification_opt
2361 static tree cp_parser_asm_operand_list
2363 static tree cp_parser_asm_clobber_list
2365 static tree cp_parser_asm_label_list
2367 static bool cp_next_tokens_can_be_attribute_p
2369 static bool cp_next_tokens_can_be_gnu_attribute_p
2371 static bool cp_next_tokens_can_be_std_attribute_p
2373 static bool cp_nth_tokens_can_be_std_attribute_p
2374 (cp_parser
*, size_t);
2375 static bool cp_nth_tokens_can_be_gnu_attribute_p
2376 (cp_parser
*, size_t);
2377 static bool cp_nth_tokens_can_be_attribute_p
2378 (cp_parser
*, size_t);
2379 static tree cp_parser_attributes_opt
2381 static tree cp_parser_gnu_attributes_opt
2383 static tree cp_parser_gnu_attribute_list
2385 static tree cp_parser_std_attribute
2386 (cp_parser
*, tree
);
2387 static tree cp_parser_std_attribute_spec
2389 static tree cp_parser_std_attribute_spec_seq
2391 static size_t cp_parser_skip_attributes_opt
2392 (cp_parser
*, size_t);
2393 static bool cp_parser_extension_opt
2394 (cp_parser
*, int *);
2395 static void cp_parser_label_declaration
2398 /* Concept Extensions */
2400 static tree cp_parser_requires_clause
2402 static tree cp_parser_requires_clause_opt
2404 static tree cp_parser_requires_expression
2406 static tree cp_parser_requirement_parameter_list
2408 static tree cp_parser_requirement_body
2410 static tree cp_parser_requirement_list
2412 static tree cp_parser_requirement
2414 static tree cp_parser_simple_requirement
2416 static tree cp_parser_compound_requirement
2418 static tree cp_parser_type_requirement
2420 static tree cp_parser_nested_requirement
2423 /* Transactional Memory Extensions */
2425 static tree cp_parser_transaction
2426 (cp_parser
*, cp_token
*);
2427 static tree cp_parser_transaction_expression
2428 (cp_parser
*, enum rid
);
2429 static void cp_parser_function_transaction
2430 (cp_parser
*, enum rid
);
2431 static tree cp_parser_transaction_cancel
2434 enum pragma_context
{
2441 static bool cp_parser_pragma
2442 (cp_parser
*, enum pragma_context
, bool *);
2444 /* Objective-C++ Productions */
2446 static tree cp_parser_objc_message_receiver
2448 static tree cp_parser_objc_message_args
2450 static tree cp_parser_objc_message_expression
2452 static cp_expr cp_parser_objc_encode_expression
2454 static tree cp_parser_objc_defs_expression
2456 static tree cp_parser_objc_protocol_expression
2458 static tree cp_parser_objc_selector_expression
2460 static cp_expr cp_parser_objc_expression
2462 static bool cp_parser_objc_selector_p
2464 static tree cp_parser_objc_selector
2466 static tree cp_parser_objc_protocol_refs_opt
2468 static void cp_parser_objc_declaration
2469 (cp_parser
*, tree
);
2470 static tree cp_parser_objc_statement
2472 static bool cp_parser_objc_valid_prefix_attributes
2473 (cp_parser
*, tree
*);
2474 static void cp_parser_objc_at_property_declaration
2476 static void cp_parser_objc_at_synthesize_declaration
2478 static void cp_parser_objc_at_dynamic_declaration
2480 static tree cp_parser_objc_struct_declaration
2483 /* Utility Routines */
2485 static cp_expr cp_parser_lookup_name
2486 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2487 static tree cp_parser_lookup_name_simple
2488 (cp_parser
*, tree
, location_t
);
2489 static tree cp_parser_maybe_treat_template_as_class
2491 static bool cp_parser_check_declarator_template_parameters
2492 (cp_parser
*, cp_declarator
*, location_t
);
2493 static bool cp_parser_check_template_parameters
2494 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2495 static cp_expr cp_parser_simple_cast_expression
2497 static tree cp_parser_global_scope_opt
2498 (cp_parser
*, bool);
2499 static bool cp_parser_constructor_declarator_p
2500 (cp_parser
*, bool);
2501 static tree cp_parser_function_definition_from_specifiers_and_declarator
2502 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2503 static tree cp_parser_function_definition_after_declarator
2504 (cp_parser
*, bool);
2505 static bool cp_parser_template_declaration_after_export
2506 (cp_parser
*, bool);
2507 static void cp_parser_perform_template_parameter_access_checks
2508 (vec
<deferred_access_check
, va_gc
> *);
2509 static tree cp_parser_single_declaration
2510 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2511 static cp_expr cp_parser_functional_cast
2512 (cp_parser
*, tree
);
2513 static tree cp_parser_save_member_function_body
2514 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2515 static tree cp_parser_save_nsdmi
2517 static tree cp_parser_enclosed_template_argument_list
2519 static void cp_parser_save_default_args
2520 (cp_parser
*, tree
);
2521 static void cp_parser_late_parsing_for_member
2522 (cp_parser
*, tree
);
2523 static tree cp_parser_late_parse_one_default_arg
2524 (cp_parser
*, tree
, tree
, tree
);
2525 static void cp_parser_late_parsing_nsdmi
2526 (cp_parser
*, tree
);
2527 static void cp_parser_late_parsing_default_args
2528 (cp_parser
*, tree
);
2529 static tree cp_parser_sizeof_operand
2530 (cp_parser
*, enum rid
);
2531 static cp_expr cp_parser_trait_expr
2532 (cp_parser
*, enum rid
);
2533 static bool cp_parser_declares_only_class_p
2535 static void cp_parser_set_storage_class
2536 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2537 static void cp_parser_set_decl_spec_type
2538 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2539 static void set_and_check_decl_spec_loc
2540 (cp_decl_specifier_seq
*decl_specs
,
2541 cp_decl_spec ds
, cp_token
*);
2542 static bool cp_parser_friend_p
2543 (const cp_decl_specifier_seq
*);
2544 static void cp_parser_required_error
2545 (cp_parser
*, required_token
, bool, location_t
);
2546 static cp_token
*cp_parser_require
2547 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2548 static cp_token
*cp_parser_require_keyword
2549 (cp_parser
*, enum rid
, required_token
);
2550 static bool cp_parser_token_starts_function_definition_p
2552 static bool cp_parser_next_token_starts_class_definition_p
2554 static bool cp_parser_next_token_ends_template_argument_p
2556 static bool cp_parser_nth_token_starts_template_argument_list_p
2557 (cp_parser
*, size_t);
2558 static enum tag_types cp_parser_token_is_class_key
2560 static enum tag_types cp_parser_token_is_type_parameter_key
2562 static void cp_parser_check_class_key
2563 (enum tag_types
, tree type
);
2564 static void cp_parser_check_access_in_redeclaration
2565 (tree type
, location_t location
);
2566 static bool cp_parser_optional_template_keyword
2568 static void cp_parser_pre_parsed_nested_name_specifier
2570 static bool cp_parser_cache_group
2571 (cp_parser
*, enum cpp_ttype
, unsigned);
2572 static tree cp_parser_cache_defarg
2573 (cp_parser
*parser
, bool nsdmi
);
2574 static void cp_parser_parse_tentatively
2576 static void cp_parser_commit_to_tentative_parse
2578 static void cp_parser_commit_to_topmost_tentative_parse
2580 static void cp_parser_abort_tentative_parse
2582 static bool cp_parser_parse_definitely
2584 static inline bool cp_parser_parsing_tentatively
2586 static bool cp_parser_uncommitted_to_tentative_parse_p
2588 static void cp_parser_error
2589 (cp_parser
*, const char *);
2590 static void cp_parser_name_lookup_error
2591 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2592 static bool cp_parser_simulate_error
2594 static bool cp_parser_check_type_definition
2596 static void cp_parser_check_for_definition_in_return_type
2597 (cp_declarator
*, tree
, location_t type_location
);
2598 static void cp_parser_check_for_invalid_template_id
2599 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2600 static bool cp_parser_non_integral_constant_expression
2601 (cp_parser
*, non_integral_constant
);
2602 static void cp_parser_diagnose_invalid_type_name
2603 (cp_parser
*, tree
, location_t
);
2604 static bool cp_parser_parse_and_diagnose_invalid_type_name
2606 static int cp_parser_skip_to_closing_parenthesis
2607 (cp_parser
*, bool, bool, bool);
2608 static void cp_parser_skip_to_end_of_statement
2610 static void cp_parser_consume_semicolon_at_end_of_statement
2612 static void cp_parser_skip_to_end_of_block_or_statement
2614 static bool cp_parser_skip_to_closing_brace
2616 static void cp_parser_skip_to_end_of_template_parameter_list
2618 static void cp_parser_skip_to_pragma_eol
2619 (cp_parser
*, cp_token
*);
2620 static bool cp_parser_error_occurred
2622 static bool cp_parser_allow_gnu_extensions_p
2624 static bool cp_parser_is_pure_string_literal
2626 static bool cp_parser_is_string_literal
2628 static bool cp_parser_is_keyword
2629 (cp_token
*, enum rid
);
2630 static tree cp_parser_make_typename_type
2631 (cp_parser
*, tree
, location_t location
);
2632 static cp_declarator
* cp_parser_make_indirect_declarator
2633 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2634 static bool cp_parser_compound_literal_p
2636 static bool cp_parser_array_designator_p
2638 static bool cp_parser_init_statement_p
2640 static bool cp_parser_skip_to_closing_square_bracket
2643 /* Concept-related syntactic transformations */
2645 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2646 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2648 // -------------------------------------------------------------------------- //
2649 // Unevaluated Operand Guard
2651 // Implementation of an RAII helper for unevaluated operand parsing.
2652 cp_unevaluated::cp_unevaluated ()
2654 ++cp_unevaluated_operand
;
2655 ++c_inhibit_evaluation_warnings
;
2658 cp_unevaluated::~cp_unevaluated ()
2660 --c_inhibit_evaluation_warnings
;
2661 --cp_unevaluated_operand
;
2664 // -------------------------------------------------------------------------- //
2665 // Tentative Parsing
2667 /* Returns nonzero if we are parsing tentatively. */
2670 cp_parser_parsing_tentatively (cp_parser
* parser
)
2672 return parser
->context
->next
!= NULL
;
2675 /* Returns nonzero if TOKEN is a string literal. */
2678 cp_parser_is_pure_string_literal (cp_token
* token
)
2680 return (token
->type
== CPP_STRING
||
2681 token
->type
== CPP_STRING16
||
2682 token
->type
== CPP_STRING32
||
2683 token
->type
== CPP_WSTRING
||
2684 token
->type
== CPP_UTF8STRING
);
2687 /* Returns nonzero if TOKEN is a string literal
2688 of a user-defined string literal. */
2691 cp_parser_is_string_literal (cp_token
* token
)
2693 return (cp_parser_is_pure_string_literal (token
) ||
2694 token
->type
== CPP_STRING_USERDEF
||
2695 token
->type
== CPP_STRING16_USERDEF
||
2696 token
->type
== CPP_STRING32_USERDEF
||
2697 token
->type
== CPP_WSTRING_USERDEF
||
2698 token
->type
== CPP_UTF8STRING_USERDEF
);
2701 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2704 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2706 return token
->keyword
== keyword
;
2709 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 static enum pragma_kind
2713 cp_parser_pragma_kind (cp_token
*token
)
2715 if (token
->type
!= CPP_PRAGMA
)
2717 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2718 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2721 /* Helper function for cp_parser_error.
2722 Having peeked a token of kind TOK1_KIND that might signify
2723 a conflict marker, peek successor tokens to determine
2724 if we actually do have a conflict marker.
2725 Specifically, we consider a run of 7 '<', '=' or '>' characters
2726 at the start of a line as a conflict marker.
2727 These come through the lexer as three pairs and a single,
2728 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2729 If it returns true, *OUT_LOC is written to with the location/range
2733 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2734 location_t
*out_loc
)
2736 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2737 if (token2
->type
!= tok1_kind
)
2739 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2740 if (token3
->type
!= tok1_kind
)
2742 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2743 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2746 /* It must be at the start of the line. */
2747 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2748 if (LOCATION_COLUMN (start_loc
) != 1)
2751 /* We have a conflict marker. Construct a location of the form:
2754 with start == caret, finishing at the end of the marker. */
2755 location_t finish_loc
= get_finish (token4
->location
);
2756 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2761 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2765 get_matching_symbol (required_token token_desc
)
2772 case RT_CLOSE_BRACE
:
2774 case RT_CLOSE_PAREN
:
2779 /* Attempt to convert TOKEN_DESC from a required_token to an
2780 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2782 static enum cpp_ttype
2783 get_required_cpp_ttype (required_token token_desc
)
2788 return CPP_SEMICOLON
;
2790 return CPP_OPEN_PAREN
;
2791 case RT_CLOSE_BRACE
:
2792 return CPP_CLOSE_BRACE
;
2794 return CPP_OPEN_BRACE
;
2795 case RT_CLOSE_SQUARE
:
2796 return CPP_CLOSE_SQUARE
;
2797 case RT_OPEN_SQUARE
:
2798 return CPP_OPEN_SQUARE
;
2803 case RT_CLOSE_PAREN
:
2804 return CPP_CLOSE_PAREN
;
2807 /* Use CPP_EOF as a "no completions possible" code. */
2813 /* Subroutine of cp_parser_error and cp_parser_required_error.
2815 Issue a diagnostic of the form
2816 FILE:LINE: MESSAGE before TOKEN
2817 where TOKEN is the next token in the input stream. MESSAGE
2818 (specified by the caller) is usually of the form "expected
2821 This bypasses the check for tentative passing, and potentially
2822 adds material needed by cp_parser_required_error.
2824 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2825 suggesting insertion of the missing token.
2827 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2828 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2832 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2833 required_token missing_token_desc
,
2834 location_t matching_location
)
2836 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2837 /* This diagnostic makes more sense if it is tagged to the line
2838 of the token we just peeked at. */
2839 cp_lexer_set_source_position_from_token (token
);
2841 if (token
->type
== CPP_PRAGMA
)
2843 error_at (token
->location
,
2844 "%<#pragma%> is not allowed here");
2845 cp_parser_skip_to_pragma_eol (parser
, token
);
2849 /* If this is actually a conflict marker, report it as such. */
2850 if (token
->type
== CPP_LSHIFT
2851 || token
->type
== CPP_RSHIFT
2852 || token
->type
== CPP_EQ_EQ
)
2855 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2857 error_at (loc
, "version control conflict marker in file");
2858 expanded_location token_exploc
= expand_location (token
->location
);
2859 /* Consume tokens until the end of the source line. */
2862 cp_lexer_consume_token (parser
->lexer
);
2863 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2866 expanded_location next_exploc
= expand_location (next
->location
);
2867 if (next_exploc
.file
!= token_exploc
.file
)
2869 if (next_exploc
.line
!= token_exploc
.line
)
2876 gcc_rich_location
richloc (input_location
);
2878 bool added_matching_location
= false;
2880 if (missing_token_desc
!= RT_NONE
)
2882 /* Potentially supply a fix-it hint, suggesting to add the
2883 missing token immediately after the *previous* token.
2884 This may move the primary location within richloc. */
2885 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2886 location_t prev_token_loc
2887 = cp_lexer_previous_token (parser
->lexer
)->location
;
2888 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2890 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2891 Attempt to consolidate diagnostics by printing it as a
2892 secondary range within the main diagnostic. */
2893 if (matching_location
!= UNKNOWN_LOCATION
)
2894 added_matching_location
2895 = richloc
.add_location_if_nearby (matching_location
);
2898 /* Actually emit the error. */
2899 c_parse_error (gmsgid
,
2900 /* Because c_parser_error does not understand
2901 CPP_KEYWORD, keywords are treated like
2903 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2904 token
->u
.value
, token
->flags
, &richloc
);
2906 if (missing_token_desc
!= RT_NONE
)
2908 /* If we weren't able to consolidate matching_location, then
2909 print it as a secondary diagnostic. */
2910 if (matching_location
!= UNKNOWN_LOCATION
2911 && !added_matching_location
)
2912 inform (matching_location
, "to match this %qs",
2913 get_matching_symbol (missing_token_desc
));
2917 /* If not parsing tentatively, issue a diagnostic of the form
2918 FILE:LINE: MESSAGE before TOKEN
2919 where TOKEN is the next token in the input stream. MESSAGE
2920 (specified by the caller) is usually of the form "expected
2924 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2926 if (!cp_parser_simulate_error (parser
))
2927 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2930 /* Issue an error about name-lookup failing. NAME is the
2931 IDENTIFIER_NODE DECL is the result of
2932 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2933 the thing that we hoped to find. */
2936 cp_parser_name_lookup_error (cp_parser
* parser
,
2939 name_lookup_error desired
,
2940 location_t location
)
2942 /* If name lookup completely failed, tell the user that NAME was not
2944 if (decl
== error_mark_node
)
2946 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2947 error_at (location
, "%<%E::%E%> has not been declared",
2948 parser
->scope
, name
);
2949 else if (parser
->scope
== global_namespace
)
2950 error_at (location
, "%<::%E%> has not been declared", name
);
2951 else if (parser
->object_scope
2952 && !CLASS_TYPE_P (parser
->object_scope
))
2953 error_at (location
, "request for member %qE in non-class type %qT",
2954 name
, parser
->object_scope
);
2955 else if (parser
->object_scope
)
2956 error_at (location
, "%<%T::%E%> has not been declared",
2957 parser
->object_scope
, name
);
2959 error_at (location
, "%qE has not been declared", name
);
2961 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2966 error_at (location
, "%<%E::%E%> is not a type",
2967 parser
->scope
, name
);
2970 error_at (location
, "%<%E::%E%> is not a class or namespace",
2971 parser
->scope
, name
);
2975 "%<%E::%E%> is not a class, namespace, or enumeration",
2976 parser
->scope
, name
);
2983 else if (parser
->scope
== global_namespace
)
2988 error_at (location
, "%<::%E%> is not a type", name
);
2991 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2995 "%<::%E%> is not a class, namespace, or enumeration",
3007 error_at (location
, "%qE is not a type", name
);
3010 error_at (location
, "%qE is not a class or namespace", name
);
3014 "%qE is not a class, namespace, or enumeration", name
);
3022 /* If we are parsing tentatively, remember that an error has occurred
3023 during this tentative parse. Returns true if the error was
3024 simulated; false if a message should be issued by the caller. */
3027 cp_parser_simulate_error (cp_parser
* parser
)
3029 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3031 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3037 /* This function is called when a type is defined. If type
3038 definitions are forbidden at this point, an error message is
3042 cp_parser_check_type_definition (cp_parser
* parser
)
3044 /* If types are forbidden here, issue a message. */
3045 if (parser
->type_definition_forbidden_message
)
3047 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3048 in the message need to be interpreted. */
3049 error (parser
->type_definition_forbidden_message
);
3055 /* This function is called when the DECLARATOR is processed. The TYPE
3056 was a type defined in the decl-specifiers. If it is invalid to
3057 define a type in the decl-specifiers for DECLARATOR, an error is
3058 issued. TYPE_LOCATION is the location of TYPE and is used
3059 for error reporting. */
3062 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3063 tree type
, location_t type_location
)
3065 /* [dcl.fct] forbids type definitions in return types.
3066 Unfortunately, it's not easy to know whether or not we are
3067 processing a return type until after the fact. */
3069 && (declarator
->kind
== cdk_pointer
3070 || declarator
->kind
== cdk_reference
3071 || declarator
->kind
== cdk_ptrmem
))
3072 declarator
= declarator
->declarator
;
3074 && declarator
->kind
== cdk_function
)
3076 error_at (type_location
,
3077 "new types may not be defined in a return type");
3078 inform (type_location
,
3079 "(perhaps a semicolon is missing after the definition of %qT)",
3084 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3085 "<" in any valid C++ program. If the next token is indeed "<",
3086 issue a message warning the user about what appears to be an
3087 invalid attempt to form a template-id. LOCATION is the location
3088 of the type-specifier (TYPE) */
3091 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3093 enum tag_types tag_type
,
3094 location_t location
)
3096 cp_token_position start
= 0;
3098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3100 if (TREE_CODE (type
) == TYPE_DECL
)
3101 type
= TREE_TYPE (type
);
3102 if (TYPE_P (type
) && !template_placeholder_p (type
))
3103 error_at (location
, "%qT is not a template", type
);
3104 else if (identifier_p (type
))
3106 if (tag_type
!= none_type
)
3107 error_at (location
, "%qE is not a class template", type
);
3109 error_at (location
, "%qE is not a template", type
);
3112 error_at (location
, "invalid template-id");
3113 /* Remember the location of the invalid "<". */
3114 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3115 start
= cp_lexer_token_position (parser
->lexer
, true);
3116 /* Consume the "<". */
3117 cp_lexer_consume_token (parser
->lexer
);
3118 /* Parse the template arguments. */
3119 cp_parser_enclosed_template_argument_list (parser
);
3120 /* Permanently remove the invalid template arguments so that
3121 this error message is not issued again. */
3123 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3127 /* If parsing an integral constant-expression, issue an error message
3128 about the fact that THING appeared and return true. Otherwise,
3129 return false. In either case, set
3130 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3133 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3134 non_integral_constant thing
)
3136 parser
->non_integral_constant_expression_p
= true;
3137 if (parser
->integral_constant_expression_p
)
3139 if (!parser
->allow_non_integral_constant_expression_p
)
3141 const char *msg
= NULL
;
3145 pedwarn (input_location
, OPT_Wpedantic
,
3146 "ISO C++ forbids using a floating-point literal "
3147 "in a constant-expression");
3150 error ("a cast to a type other than an integral or "
3151 "enumeration type cannot appear in a "
3152 "constant-expression");
3155 error ("%<typeid%> operator "
3156 "cannot appear in a constant-expression");
3159 error ("non-constant compound literals "
3160 "cannot appear in a constant-expression");
3163 error ("a function call "
3164 "cannot appear in a constant-expression");
3167 error ("an increment "
3168 "cannot appear in a constant-expression");
3171 error ("an decrement "
3172 "cannot appear in a constant-expression");
3175 error ("an array reference "
3176 "cannot appear in a constant-expression");
3178 case NIC_ADDR_LABEL
:
3179 error ("the address of a label "
3180 "cannot appear in a constant-expression");
3182 case NIC_OVERLOADED
:
3183 error ("calls to overloaded operators "
3184 "cannot appear in a constant-expression");
3186 case NIC_ASSIGNMENT
:
3187 error ("an assignment cannot appear in a constant-expression");
3190 error ("a comma operator "
3191 "cannot appear in a constant-expression");
3193 case NIC_CONSTRUCTOR
:
3194 error ("a call to a constructor "
3195 "cannot appear in a constant-expression");
3197 case NIC_TRANSACTION
:
3198 error ("a transaction expression "
3199 "cannot appear in a constant-expression");
3205 msg
= "__FUNCTION__";
3207 case NIC_PRETTY_FUNC
:
3208 msg
= "__PRETTY_FUNCTION__";
3228 case NIC_PREINCREMENT
:
3231 case NIC_PREDECREMENT
:
3244 error ("%qs cannot appear in a constant-expression", msg
);
3251 /* Emit a diagnostic for an invalid type name. This function commits
3252 to the current active tentative parse, if any. (Otherwise, the
3253 problematic construct might be encountered again later, resulting
3254 in duplicate error messages.) LOCATION is the location of ID. */
3257 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3258 location_t location
)
3260 tree decl
, ambiguous_decls
;
3261 cp_parser_commit_to_tentative_parse (parser
);
3262 /* Try to lookup the identifier. */
3263 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3264 /*is_template=*/false,
3265 /*is_namespace=*/false,
3266 /*check_dependency=*/true,
3267 &ambiguous_decls
, location
);
3268 if (ambiguous_decls
)
3269 /* If the lookup was ambiguous, an error will already have
3272 /* If the lookup found a template-name, it means that the user forgot
3273 to specify an argument list. Emit a useful error message. */
3274 if (DECL_TYPE_TEMPLATE_P (decl
))
3276 auto_diagnostic_group d
;
3278 "invalid use of template-name %qE without an argument list",
3280 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3281 inform (location
, "class template argument deduction is only available "
3282 "with -std=c++17 or -std=gnu++17");
3283 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3285 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3286 error_at (location
, "invalid use of destructor %qD as a type", id
);
3287 else if (TREE_CODE (decl
) == TYPE_DECL
)
3288 /* Something like 'unsigned A a;' */
3289 error_at (location
, "invalid combination of multiple type-specifiers");
3290 else if (!parser
->scope
)
3292 /* Issue an error message. */
3293 auto_diagnostic_group d
;
3295 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3296 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3297 if (const char *suggestion
= hint
.suggestion ())
3299 gcc_rich_location
richloc (location
);
3300 richloc
.add_fixit_replace (suggestion
);
3302 "%qE does not name a type; did you mean %qs?",
3306 error_at (location
, "%qE does not name a type", id
);
3307 /* If we're in a template class, it's possible that the user was
3308 referring to a type from a base class. For example:
3310 template <typename T> struct A { typedef T X; };
3311 template <typename T> struct B : public A<T> { X x; };
3313 The user should have said "typename A<T>::X". */
3314 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3315 inform (location
, "C++11 %<constexpr%> only available with "
3316 "-std=c++11 or -std=gnu++11");
3317 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3318 inform (location
, "C++11 %<noexcept%> only available with "
3319 "-std=c++11 or -std=gnu++11");
3320 else if (cxx_dialect
< cxx11
3321 && TREE_CODE (id
) == IDENTIFIER_NODE
3322 && id_equal (id
, "thread_local"))
3323 inform (location
, "C++11 %<thread_local%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3326 inform (location
, "%<concept%> only available with -fconcepts");
3327 else if (processing_template_decl
&& current_class_type
3328 && TYPE_BINFO (current_class_type
))
3332 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3336 tree base_type
= BINFO_TYPE (b
);
3337 if (CLASS_TYPE_P (base_type
)
3338 && dependent_type_p (base_type
))
3341 /* Go from a particular instantiation of the
3342 template (which will have an empty TYPE_FIELDs),
3343 to the main version. */
3344 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3345 for (field
= TYPE_FIELDS (base_type
);
3347 field
= DECL_CHAIN (field
))
3348 if (TREE_CODE (field
) == TYPE_DECL
3349 && DECL_NAME (field
) == id
)
3352 "(perhaps %<typename %T::%E%> was intended)",
3353 BINFO_TYPE (b
), id
);
3362 /* Here we diagnose qualified-ids where the scope is actually correct,
3363 but the identifier does not resolve to a valid type name. */
3364 else if (parser
->scope
!= error_mark_node
)
3366 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3368 auto_diagnostic_group d
;
3370 if (decl
== error_mark_node
)
3371 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3373 const char *suggestion
= hint
.suggestion ();
3374 gcc_rich_location
richloc (location_of (id
));
3376 richloc
.add_fixit_replace (suggestion
);
3377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3381 "%qE in namespace %qE does not name a template"
3382 " type; did you mean %qs?",
3383 id
, parser
->scope
, suggestion
);
3386 "%qE in namespace %qE does not name a template type",
3389 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3393 "%qE in namespace %qE does not name a template"
3394 " type; did you mean %qs?",
3395 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3398 "%qE in namespace %qE does not name a template"
3400 TREE_OPERAND (id
, 0), parser
->scope
);
3406 "%qE in namespace %qE does not name a type"
3407 "; did you mean %qs?",
3408 id
, parser
->scope
, suggestion
);
3411 "%qE in namespace %qE does not name a type",
3415 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3417 else if (CLASS_TYPE_P (parser
->scope
)
3418 && constructor_name_p (id
, parser
->scope
))
3421 auto_diagnostic_group d
;
3422 error_at (location
, "%<%T::%E%> names the constructor, not"
3423 " the type", parser
->scope
, id
);
3424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3425 error_at (location
, "and %qT has no template constructors",
3428 else if (TYPE_P (parser
->scope
)
3429 && dependent_scope_p (parser
->scope
))
3431 gcc_rich_location
richloc (location
);
3432 richloc
.add_fixit_insert_before ("typename ");
3433 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3435 "need %<typename%> before %<%T::%D::%E%> because "
3436 "%<%T::%D%> is a dependent scope",
3437 TYPE_CONTEXT (parser
->scope
),
3438 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3440 TYPE_CONTEXT (parser
->scope
),
3441 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3443 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3444 "%qT is a dependent scope",
3445 parser
->scope
, id
, parser
->scope
);
3447 else if (TYPE_P (parser
->scope
))
3449 auto_diagnostic_group d
;
3450 if (!COMPLETE_TYPE_P (parser
->scope
))
3451 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3453 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3454 error_at (location_of (id
),
3455 "%qE in %q#T does not name a template type",
3457 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3458 error_at (location_of (id
),
3459 "%qE in %q#T does not name a template type",
3460 TREE_OPERAND (id
, 0), parser
->scope
);
3462 error_at (location_of (id
),
3463 "%qE in %q#T does not name a type",
3466 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3473 /* Check for a common situation where a type-name should be present,
3474 but is not, and issue a sensible error message. Returns true if an
3475 invalid type-name was detected.
3477 The situation handled by this function are variable declarations of the
3478 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3479 Usually, `ID' should name a type, but if we got here it means that it
3480 does not. We try to emit the best possible error message depending on
3481 how exactly the id-expression looks like. */
3484 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3487 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3489 /* Avoid duplicate error about ambiguous lookup. */
3490 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3492 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3493 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3497 cp_parser_parse_tentatively (parser
);
3498 id
= cp_parser_id_expression (parser
,
3499 /*template_keyword_p=*/false,
3500 /*check_dependency_p=*/true,
3501 /*template_p=*/NULL
,
3502 /*declarator_p=*/false,
3503 /*optional_p=*/false);
3504 /* If the next token is a (, this is a function with no explicit return
3505 type, i.e. constructor, destructor or conversion op. */
3506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3507 || TREE_CODE (id
) == TYPE_DECL
)
3509 cp_parser_abort_tentative_parse (parser
);
3512 if (!cp_parser_parse_definitely (parser
))
3515 /* Emit a diagnostic for the invalid type. */
3516 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3518 /* If we aren't in the middle of a declarator (i.e. in a
3519 parameter-declaration-clause), skip to the end of the declaration;
3520 there's no point in trying to process it. */
3521 if (!parser
->in_declarator_p
)
3522 cp_parser_skip_to_end_of_block_or_statement (parser
);
3526 /* Consume tokens up to, and including, the next non-nested closing `)'.
3527 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3528 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3529 found an unnested token of that type. */
3532 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3537 unsigned paren_depth
= 0;
3538 unsigned brace_depth
= 0;
3539 unsigned square_depth
= 0;
3540 unsigned condop_depth
= 0;
3542 if (recovering
&& or_ttype
== CPP_EOF
3543 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3548 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3550 /* Have we found what we're looking for before the closing paren? */
3551 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3552 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3555 switch (token
->type
)
3558 case CPP_PRAGMA_EOL
:
3559 /* If we've run out of tokens, then there is no closing `)'. */
3562 /* This is good for lambda expression capture-lists. */
3563 case CPP_OPEN_SQUARE
:
3566 case CPP_CLOSE_SQUARE
:
3567 if (!square_depth
--)
3572 /* This matches the processing in skip_to_end_of_statement. */
3577 case CPP_OPEN_BRACE
:
3580 case CPP_CLOSE_BRACE
:
3585 case CPP_OPEN_PAREN
:
3590 case CPP_CLOSE_PAREN
:
3591 if (!brace_depth
&& !paren_depth
--)
3594 cp_lexer_consume_token (parser
->lexer
);
3600 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3605 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3613 /* Consume the token. */
3614 cp_lexer_consume_token (parser
->lexer
);
3618 /* Consume tokens up to, and including, the next non-nested closing `)'.
3619 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3620 are doing error recovery. Returns -1 if OR_COMMA is true and we
3621 found an unnested token of that type. */
3624 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3629 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3630 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3631 ttype
, consume_paren
);
3634 /* Consume tokens until we reach the end of the current statement.
3635 Normally, that will be just before consuming a `;'. However, if a
3636 non-nested `}' comes first, then we stop before consuming that. */
3639 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3641 unsigned nesting_depth
= 0;
3643 /* Unwind generic function template scope if necessary. */
3644 if (parser
->fully_implicit_function_template_p
)
3645 abort_fully_implicit_template (parser
);
3649 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3651 switch (token
->type
)
3654 case CPP_PRAGMA_EOL
:
3655 /* If we've run out of tokens, stop. */
3659 /* If the next token is a `;', we have reached the end of the
3665 case CPP_CLOSE_BRACE
:
3666 /* If this is a non-nested '}', stop before consuming it.
3667 That way, when confronted with something like:
3671 we stop before consuming the closing '}', even though we
3672 have not yet reached a `;'. */
3673 if (nesting_depth
== 0)
3676 /* If it is the closing '}' for a block that we have
3677 scanned, stop -- but only after consuming the token.
3683 we will stop after the body of the erroneously declared
3684 function, but before consuming the following `typedef'
3686 if (--nesting_depth
== 0)
3688 cp_lexer_consume_token (parser
->lexer
);
3693 case CPP_OPEN_BRACE
:
3701 /* Consume the token. */
3702 cp_lexer_consume_token (parser
->lexer
);
3706 /* This function is called at the end of a statement or declaration.
3707 If the next token is a semicolon, it is consumed; otherwise, error
3708 recovery is attempted. */
3711 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3713 /* Look for the trailing `;'. */
3714 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3716 /* If there is additional (erroneous) input, skip to the end of
3718 cp_parser_skip_to_end_of_statement (parser
);
3719 /* If the next token is now a `;', consume it. */
3720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3721 cp_lexer_consume_token (parser
->lexer
);
3725 /* Skip tokens until we have consumed an entire block, or until we
3726 have consumed a non-nested `;'. */
3729 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3731 int nesting_depth
= 0;
3733 /* Unwind generic function template scope if necessary. */
3734 if (parser
->fully_implicit_function_template_p
)
3735 abort_fully_implicit_template (parser
);
3737 while (nesting_depth
>= 0)
3739 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3741 switch (token
->type
)
3744 case CPP_PRAGMA_EOL
:
3745 /* If we've run out of tokens, stop. */
3749 /* Stop if this is an unnested ';'. */
3754 case CPP_CLOSE_BRACE
:
3755 /* Stop if this is an unnested '}', or closes the outermost
3758 if (nesting_depth
< 0)
3764 case CPP_OPEN_BRACE
:
3773 /* Consume the token. */
3774 cp_lexer_consume_token (parser
->lexer
);
3778 /* Skip tokens until a non-nested closing curly brace is the next
3779 token, or there are no more tokens. Return true in the first case,
3783 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3785 unsigned nesting_depth
= 0;
3789 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3791 switch (token
->type
)
3794 case CPP_PRAGMA_EOL
:
3795 /* If we've run out of tokens, stop. */
3798 case CPP_CLOSE_BRACE
:
3799 /* If the next token is a non-nested `}', then we have reached
3800 the end of the current block. */
3801 if (nesting_depth
-- == 0)
3805 case CPP_OPEN_BRACE
:
3806 /* If it the next token is a `{', then we are entering a new
3807 block. Consume the entire block. */
3815 /* Consume the token. */
3816 cp_lexer_consume_token (parser
->lexer
);
3820 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3821 parameter is the PRAGMA token, allowing us to purge the entire pragma
3825 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3829 parser
->lexer
->in_pragma
= false;
3832 token
= cp_lexer_consume_token (parser
->lexer
);
3833 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3835 /* Ensure that the pragma is not parsed again. */
3836 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3839 /* Require pragma end of line, resyncing with it as necessary. The
3840 arguments are as for cp_parser_skip_to_pragma_eol. */
3843 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3845 parser
->lexer
->in_pragma
= false;
3846 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3847 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3850 /* This is a simple wrapper around make_typename_type. When the id is
3851 an unresolved identifier node, we can provide a superior diagnostic
3852 using cp_parser_diagnose_invalid_type_name. */
3855 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3856 location_t id_location
)
3859 if (identifier_p (id
))
3861 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3862 /*complain=*/tf_none
);
3863 if (result
== error_mark_node
)
3864 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3867 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3870 /* This is a wrapper around the
3871 make_{pointer,ptrmem,reference}_declarator functions that decides
3872 which one to call based on the CODE and CLASS_TYPE arguments. The
3873 CODE argument should be one of the values returned by
3874 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3875 appertain to the pointer or reference. */
3877 static cp_declarator
*
3878 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3879 cp_cv_quals cv_qualifiers
,
3880 cp_declarator
*target
,
3883 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3884 return cp_error_declarator
;
3886 if (code
== INDIRECT_REF
)
3887 if (class_type
== NULL_TREE
)
3888 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3890 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3891 target
, attributes
);
3892 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3893 return make_reference_declarator (cv_qualifiers
, target
,
3895 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3896 return make_reference_declarator (cv_qualifiers
, target
,
3901 /* Create a new C++ parser. */
3904 cp_parser_new (void)
3910 /* cp_lexer_new_main is called before doing GC allocation because
3911 cp_lexer_new_main might load a PCH file. */
3912 lexer
= cp_lexer_new_main ();
3914 /* Initialize the binops_by_token so that we can get the tree
3915 directly from the token. */
3916 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3917 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3919 parser
= ggc_cleared_alloc
<cp_parser
> ();
3920 parser
->lexer
= lexer
;
3921 parser
->context
= cp_parser_context_new (NULL
);
3923 /* For now, we always accept GNU extensions. */
3924 parser
->allow_gnu_extensions_p
= 1;
3926 /* The `>' token is a greater-than operator, not the end of a
3928 parser
->greater_than_is_operator_p
= true;
3930 parser
->default_arg_ok_p
= true;
3932 /* We are not parsing a constant-expression. */
3933 parser
->integral_constant_expression_p
= false;
3934 parser
->allow_non_integral_constant_expression_p
= false;
3935 parser
->non_integral_constant_expression_p
= false;
3937 /* Local variable names are not forbidden. */
3938 parser
->local_variables_forbidden_p
= false;
3940 /* We are not processing an `extern "C"' declaration. */
3941 parser
->in_unbraced_linkage_specification_p
= false;
3943 /* We are not processing a declarator. */
3944 parser
->in_declarator_p
= false;
3946 /* We are not processing a template-argument-list. */
3947 parser
->in_template_argument_list_p
= false;
3949 /* We are not in an iteration statement. */
3950 parser
->in_statement
= 0;
3952 /* We are not in a switch statement. */
3953 parser
->in_switch_statement_p
= false;
3955 /* We are not parsing a type-id inside an expression. */
3956 parser
->in_type_id_in_expr_p
= false;
3958 /* String literals should be translated to the execution character set. */
3959 parser
->translate_strings_p
= true;
3961 /* We are not parsing a function body. */
3962 parser
->in_function_body
= false;
3964 /* We can correct until told otherwise. */
3965 parser
->colon_corrects_to_scope_p
= true;
3967 /* The unparsed function queue is empty. */
3968 push_unparsed_function_queues (parser
);
3970 /* There are no classes being defined. */
3971 parser
->num_classes_being_defined
= 0;
3973 /* No template parameters apply. */
3974 parser
->num_template_parameter_lists
= 0;
3976 /* Special parsing data structures. */
3977 parser
->omp_declare_simd
= NULL
;
3978 parser
->oacc_routine
= NULL
;
3980 /* Not declaring an implicit function template. */
3981 parser
->auto_is_implicit_function_template_parm_p
= false;
3982 parser
->fully_implicit_function_template_p
= false;
3983 parser
->implicit_template_parms
= 0;
3984 parser
->implicit_template_scope
= 0;
3986 /* Allow constrained-type-specifiers. */
3987 parser
->prevent_constrained_type_specifiers
= 0;
3989 /* We haven't yet seen an 'extern "C"'. */
3990 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3995 /* Create a cp_lexer structure which will emit the tokens in CACHE
3996 and push it onto the parser's lexer stack. This is used for delayed
3997 parsing of in-class method bodies and default arguments, and should
3998 not be confused with tentative parsing. */
4000 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4002 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4003 lexer
->next
= parser
->lexer
;
4004 parser
->lexer
= lexer
;
4006 /* Move the current source position to that of the first token in the
4008 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4011 /* Pop the top lexer off the parser stack. This is never used for the
4012 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4014 cp_parser_pop_lexer (cp_parser
*parser
)
4016 cp_lexer
*lexer
= parser
->lexer
;
4017 parser
->lexer
= lexer
->next
;
4018 cp_lexer_destroy (lexer
);
4020 /* Put the current source position back where it was before this
4021 lexer was pushed. */
4022 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4025 /* Lexical conventions [gram.lex] */
4027 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4031 cp_parser_identifier (cp_parser
* parser
)
4035 /* Look for the identifier. */
4036 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4037 /* Return the value. */
4039 return cp_expr (token
->u
.value
, token
->location
);
4041 return error_mark_node
;
4044 /* Parse a sequence of adjacent string constants. Returns a
4045 TREE_STRING representing the combined, nul-terminated string
4046 constant. If TRANSLATE is true, translate the string to the
4047 execution character set. If WIDE_OK is true, a wide string is
4050 C++98 [lex.string] says that if a narrow string literal token is
4051 adjacent to a wide string literal token, the behavior is undefined.
4052 However, C99 6.4.5p4 says that this results in a wide string literal.
4053 We follow C99 here, for consistency with the C front end.
4055 This code is largely lifted from lex_string() in c-lex.c.
4057 FUTURE: ObjC++ will need to handle @-strings here. */
4059 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4060 bool lookup_udlit
= true)
4064 struct obstack str_ob
;
4065 struct obstack loc_ob
;
4066 cpp_string str
, istr
, *strs
;
4068 enum cpp_ttype type
, curr_type
;
4069 int have_suffix_p
= 0;
4071 tree suffix_id
= NULL_TREE
;
4072 bool curr_tok_is_userdef_p
= false;
4074 tok
= cp_lexer_peek_token (parser
->lexer
);
4075 if (!cp_parser_is_string_literal (tok
))
4077 cp_parser_error (parser
, "expected string-literal");
4078 return error_mark_node
;
4081 location_t loc
= tok
->location
;
4083 if (cpp_userdef_string_p (tok
->type
))
4085 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4086 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4087 curr_tok_is_userdef_p
= true;
4091 string_tree
= tok
->u
.value
;
4092 curr_type
= tok
->type
;
4096 /* Try to avoid the overhead of creating and destroying an obstack
4097 for the common case of just one string. */
4098 if (!cp_parser_is_string_literal
4099 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4101 cp_lexer_consume_token (parser
->lexer
);
4103 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4104 str
.len
= TREE_STRING_LENGTH (string_tree
);
4107 if (curr_tok_is_userdef_p
)
4109 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4111 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4114 curr_type
= tok
->type
;
4120 location_t last_tok_loc
= tok
->location
;
4121 gcc_obstack_init (&str_ob
);
4122 gcc_obstack_init (&loc_ob
);
4127 cp_lexer_consume_token (parser
->lexer
);
4129 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4130 str
.len
= TREE_STRING_LENGTH (string_tree
);
4132 if (curr_tok_is_userdef_p
)
4134 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4135 if (have_suffix_p
== 0)
4137 suffix_id
= curr_suffix_id
;
4140 else if (have_suffix_p
== 1
4141 && curr_suffix_id
!= suffix_id
)
4143 error ("inconsistent user-defined literal suffixes"
4144 " %qD and %qD in string literal",
4145 suffix_id
, curr_suffix_id
);
4148 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4151 curr_type
= tok
->type
;
4153 if (type
!= curr_type
)
4155 if (type
== CPP_STRING
)
4157 else if (curr_type
!= CPP_STRING
)
4159 rich_location
rich_loc (line_table
, tok
->location
);
4160 rich_loc
.add_range (last_tok_loc
);
4161 error_at (&rich_loc
,
4162 "unsupported non-standard concatenation "
4163 "of string literals");
4167 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4168 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4170 last_tok_loc
= tok
->location
;
4172 tok
= cp_lexer_peek_token (parser
->lexer
);
4173 if (cpp_userdef_string_p (tok
->type
))
4175 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4176 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4177 curr_tok_is_userdef_p
= true;
4181 string_tree
= tok
->u
.value
;
4182 curr_type
= tok
->type
;
4183 curr_tok_is_userdef_p
= false;
4186 while (cp_parser_is_string_literal (tok
));
4188 /* A string literal built by concatenation has its caret=start at
4189 the start of the initial string, and its finish at the finish of
4190 the final string literal. */
4191 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4193 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4196 if (type
!= CPP_STRING
&& !wide_ok
)
4198 cp_parser_error (parser
, "a wide string is invalid in this context");
4202 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4203 (parse_in
, strs
, count
, &istr
, type
))
4205 value
= build_string (istr
.len
, (const char *)istr
.text
);
4206 free (CONST_CAST (unsigned char *, istr
.text
));
4209 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4210 gcc_assert (g_string_concat_db
);
4211 g_string_concat_db
->record_string_concatenation (count
, locs
);
4218 case CPP_UTF8STRING
:
4219 TREE_TYPE (value
) = char_array_type_node
;
4222 TREE_TYPE (value
) = char16_array_type_node
;
4225 TREE_TYPE (value
) = char32_array_type_node
;
4228 TREE_TYPE (value
) = wchar_array_type_node
;
4232 value
= fix_string_type (value
);
4236 tree literal
= build_userdef_literal (suffix_id
, value
,
4237 OT_NONE
, NULL_TREE
);
4239 value
= cp_parser_userdef_string_literal (literal
);
4245 /* cpp_interpret_string has issued an error. */
4246 value
= error_mark_node
;
4250 obstack_free (&str_ob
, 0);
4251 obstack_free (&loc_ob
, 0);
4254 return cp_expr (value
, loc
);
4257 /* Look up a literal operator with the name and the exact arguments. */
4260 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4262 tree decl
= lookup_name (name
);
4263 if (!decl
|| !is_overloaded_fn (decl
))
4264 return error_mark_node
;
4266 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4270 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4276 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4277 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4279 tree tparm
= TREE_VALUE (parmtypes
);
4280 tree targ
= TREE_TYPE ((*args
)[ix
]);
4281 bool ptr
= TYPE_PTR_P (tparm
);
4282 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4283 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4285 || !same_type_p (TREE_TYPE (tparm
),
4291 && ix
== vec_safe_length (args
)
4292 /* May be this should be sufficient_parms_p instead,
4293 depending on how exactly should user-defined literals
4294 work in presence of default arguments on the literal
4295 operator parameters. */
4296 && parmtypes
== void_list_node
)
4301 return error_mark_node
;
4304 /* Parse a user-defined char constant. Returns a call to a user-defined
4305 literal operator taking the character as an argument. */
4308 cp_parser_userdef_char_literal (cp_parser
*parser
)
4310 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4311 tree literal
= token
->u
.value
;
4312 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4313 tree value
= USERDEF_LITERAL_VALUE (literal
);
4314 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4317 /* Build up a call to the user-defined operator */
4318 /* Lookup the name we got back from the id-expression. */
4319 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4320 vec_safe_push (args
, value
);
4321 decl
= lookup_literal_operator (name
, args
);
4322 if (!decl
|| decl
== error_mark_node
)
4324 error ("unable to find character literal operator %qD with %qT argument",
4325 name
, TREE_TYPE (value
));
4326 release_tree_vector (args
);
4327 return error_mark_node
;
4329 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4330 release_tree_vector (args
);
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4338 make_char_string_pack (tree value
)
4341 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4342 const char *str
= TREE_STRING_POINTER (value
);
4343 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4344 tree argvec
= make_tree_vec (1);
4346 /* Fill in CHARVEC with all of the parameters. */
4347 charvec
= make_tree_vec (len
);
4348 for (i
= 0; i
< len
; ++i
)
4350 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4351 cpp_string in
= { 3, s
};
4352 cpp_string out
= { 0, 0 };
4353 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4355 gcc_assert (out
.len
== 2);
4356 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4360 /* Build the argument packs. */
4361 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4363 TREE_VEC_ELT (argvec
, 0) = argpack
;
4368 /* A subroutine of cp_parser_userdef_numeric_literal to
4369 create a char... template parameter pack from a string node. */
4372 make_string_pack (tree value
)
4375 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4376 const unsigned char *str
4377 = (const unsigned char *) TREE_STRING_POINTER (value
);
4378 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4379 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4380 tree argvec
= make_tree_vec (2);
4382 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4383 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4385 /* First template parm is character type. */
4386 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4388 /* Fill in CHARVEC with all of the parameters. */
4389 charvec
= make_tree_vec (len
);
4390 for (int i
= 0; i
< len
; ++i
)
4391 TREE_VEC_ELT (charvec
, i
)
4392 = double_int_to_tree (str_char_type_node
,
4393 double_int::from_buffer (str
+ i
* sz
, sz
));
4395 /* Build the argument packs. */
4396 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4398 TREE_VEC_ELT (argvec
, 1) = argpack
;
4403 /* Parse a user-defined numeric constant. returns a call to a user-defined
4404 literal operator. */
4407 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4409 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4410 tree literal
= token
->u
.value
;
4411 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4412 tree value
= USERDEF_LITERAL_VALUE (literal
);
4413 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4414 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4415 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4417 vec
<tree
, va_gc
> *args
;
4419 /* Look for a literal operator taking the exact type of numeric argument
4420 as the literal value. */
4421 args
= make_tree_vector ();
4422 vec_safe_push (args
, value
);
4423 decl
= lookup_literal_operator (name
, args
);
4424 if (decl
&& decl
!= error_mark_node
)
4426 result
= finish_call_expr (decl
, &args
, false, true,
4427 tf_warning_or_error
);
4429 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4431 warning_at (token
->location
, OPT_Woverflow
,
4432 "integer literal exceeds range of %qT type",
4433 long_long_unsigned_type_node
);
4438 warning_at (token
->location
, OPT_Woverflow
,
4439 "floating literal exceeds range of %qT type",
4440 long_double_type_node
);
4441 else if (overflow
< 0)
4442 warning_at (token
->location
, OPT_Woverflow
,
4443 "floating literal truncated to zero");
4446 release_tree_vector (args
);
4449 release_tree_vector (args
);
4451 /* If the numeric argument didn't work, look for a raw literal
4452 operator taking a const char* argument consisting of the number
4453 in string format. */
4454 args
= make_tree_vector ();
4455 vec_safe_push (args
, num_string
);
4456 decl
= lookup_literal_operator (name
, args
);
4457 if (decl
&& decl
!= error_mark_node
)
4459 result
= finish_call_expr (decl
, &args
, false, true,
4460 tf_warning_or_error
);
4461 release_tree_vector (args
);
4464 release_tree_vector (args
);
4466 /* If the raw literal didn't work, look for a non-type template
4467 function with parameter pack char.... Call the function with
4468 template parameter characters representing the number. */
4469 args
= make_tree_vector ();
4470 decl
= lookup_literal_operator (name
, args
);
4471 if (decl
&& decl
!= error_mark_node
)
4473 tree tmpl_args
= make_char_string_pack (num_string
);
4474 if (tmpl_args
== NULL_TREE
)
4476 error ("failed to translate literal to execution character set %qT",
4478 return error_mark_node
;
4480 decl
= lookup_template_function (decl
, tmpl_args
);
4481 result
= finish_call_expr (decl
, &args
, false, true,
4482 tf_warning_or_error
);
4483 release_tree_vector (args
);
4487 release_tree_vector (args
);
4489 /* In C++14 the standard library defines complex number suffixes that
4490 conflict with GNU extensions. Prefer them if <complex> is #included. */
4491 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4492 bool i14
= (cxx_dialect
> cxx11
4493 && (id_equal (suffix_id
, "i")
4494 || id_equal (suffix_id
, "if")
4495 || id_equal (suffix_id
, "il")));
4496 diagnostic_t kind
= DK_ERROR
;
4501 tree cxlit
= lookup_qualified_name (std_node
,
4502 get_identifier ("complex_literals"),
4504 if (cxlit
== error_mark_node
)
4506 /* No <complex>, so pedwarn and use GNU semantics. */
4508 opt
= OPT_Wpedantic
;
4513 = emit_diagnostic (kind
, input_location
, opt
,
4514 "unable to find numeric literal operator %qD", name
);
4517 /* Don't inform either. */;
4520 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4521 "(from <complex>) to enable the C++14 user-defined literal "
4524 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4525 "GNU built-in suffix");
4528 inform (token
->location
, "use -fext-numeric-literals "
4529 "to enable more built-in suffixes");
4531 if (kind
== DK_ERROR
)
4532 value
= error_mark_node
;
4535 /* Use the built-in semantics. */
4537 if (id_equal (suffix_id
, "i"))
4539 if (TREE_CODE (value
) == INTEGER_CST
)
4540 type
= integer_type_node
;
4542 type
= double_type_node
;
4544 else if (id_equal (suffix_id
, "if"))
4545 type
= float_type_node
;
4546 else /* if (id_equal (suffix_id, "il")) */
4547 type
= long_double_type_node
;
4549 value
= build_complex (build_complex_type (type
),
4550 fold_convert (type
, integer_zero_node
),
4551 fold_convert (type
, value
));
4554 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4555 /* Avoid repeated diagnostics. */
4556 token
->u
.value
= value
;
4560 /* Parse a user-defined string constant. Returns a call to a user-defined
4561 literal operator taking a character pointer and the length of the string
4565 cp_parser_userdef_string_literal (tree literal
)
4567 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4568 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4569 tree value
= USERDEF_LITERAL_VALUE (literal
);
4570 int len
= TREE_STRING_LENGTH (value
)
4571 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4574 /* Build up a call to the user-defined operator. */
4575 /* Lookup the name we got back from the id-expression. */
4576 releasing_vec rargs
;
4577 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4578 vec_safe_push (args
, value
);
4579 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4580 decl
= lookup_literal_operator (name
, args
);
4582 if (decl
&& decl
!= error_mark_node
)
4583 return finish_call_expr (decl
, &args
, false, true,
4584 tf_warning_or_error
);
4586 /* Look for a suitable template function, either (C++20) with a single
4587 parameter of class type, or (N3599) with typename parameter CharT and
4588 parameter pack CharT... */
4590 decl
= lookup_literal_operator (name
, args
);
4591 if (decl
&& decl
!= error_mark_node
)
4593 /* Use resolve_nondeduced_context to try to choose one form of template
4595 tree tmpl_args
= make_tree_vec (1);
4596 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4597 decl
= lookup_template_function (decl
, tmpl_args
);
4598 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4603 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4604 res
= resolve_nondeduced_context (decl
, tf_none
);
4608 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4609 TREE_OPERAND (decl
, 1) = tmpl_args
;
4610 return finish_call_expr (decl
, &args
, false, true,
4611 tf_warning_or_error
);
4614 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4615 name
, TREE_TYPE (value
), size_type_node
);
4616 return error_mark_node
;
4620 /* Basic concepts [gram.basic] */
4622 /* Parse a translation-unit.
4625 declaration-seq [opt] */
4628 cp_parser_translation_unit (cp_parser
* parser
)
4630 gcc_checking_assert (!cp_error_declarator
);
4632 /* Create the declarator obstack. */
4633 gcc_obstack_init (&declarator_obstack
);
4634 /* Create the error declarator. */
4635 cp_error_declarator
= make_declarator (cdk_error
);
4636 /* Create the empty parameter list. */
4637 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4639 /* Remember where the base of the declarator obstack lies. */
4640 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4642 bool implicit_extern_c
= false;
4646 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4648 /* If we're entering or exiting a region that's implicitly
4649 extern "C", modify the lang context appropriately. */
4650 if (implicit_extern_c
4651 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4653 implicit_extern_c
= !implicit_extern_c
;
4654 if (implicit_extern_c
)
4655 push_lang_context (lang_name_c
);
4657 pop_lang_context ();
4660 if (token
->type
== CPP_EOF
)
4663 if (token
->type
== CPP_CLOSE_BRACE
)
4665 cp_parser_error (parser
, "expected declaration");
4666 cp_lexer_consume_token (parser
->lexer
);
4667 /* If the next token is now a `;', consume it. */
4668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4669 cp_lexer_consume_token (parser
->lexer
);
4672 cp_parser_toplevel_declaration (parser
);
4675 /* Get rid of the token array; we don't need it any more. */
4676 cp_lexer_destroy (parser
->lexer
);
4677 parser
->lexer
= NULL
;
4679 /* The EOF should have reset this. */
4680 gcc_checking_assert (!implicit_extern_c
);
4682 /* Make sure the declarator obstack was fully cleaned up. */
4683 gcc_assert (obstack_next_free (&declarator_obstack
)
4684 == declarator_obstack_base
);
4687 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4688 decltype context. */
4690 static inline tsubst_flags_t
4691 complain_flags (bool decltype_p
)
4693 tsubst_flags_t complain
= tf_warning_or_error
;
4695 complain
|= tf_decltype
;
4699 /* We're about to parse a collection of statements. If we're currently
4700 parsing tentatively, set up a firewall so that any nested
4701 cp_parser_commit_to_tentative_parse won't affect the current context. */
4703 static cp_token_position
4704 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4706 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4709 cp_parser_parse_tentatively (parser
);
4710 cp_parser_commit_to_topmost_tentative_parse (parser
);
4711 return cp_lexer_token_position (parser
->lexer
, false);
4714 /* We've finished parsing the collection of statements. Wrap up the
4715 firewall and replace the relevant tokens with the parsed form. */
4718 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4724 /* Finish the firewall level. */
4725 cp_parser_parse_definitely (parser
);
4726 /* And remember the result of the parse for when we try again. */
4727 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4728 token
->type
= CPP_PREPARSED_EXPR
;
4729 token
->u
.value
= expr
;
4730 token
->keyword
= RID_MAX
;
4731 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4734 /* Like the above functions, but let the user modify the tokens. Used by
4735 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4736 later parses, so it makes sense to localize the effects of
4737 cp_parser_commit_to_tentative_parse. */
4739 struct tentative_firewall
4744 tentative_firewall (cp_parser
*p
): parser(p
)
4746 /* If we're currently parsing tentatively, start a committed level as a
4747 firewall and then an inner tentative parse. */
4748 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4750 cp_parser_parse_tentatively (parser
);
4751 cp_parser_commit_to_topmost_tentative_parse (parser
);
4752 cp_parser_parse_tentatively (parser
);
4756 ~tentative_firewall()
4760 /* Finish the inner tentative parse and the firewall, propagating any
4761 uncommitted error state to the outer tentative parse. */
4762 bool err
= cp_parser_error_occurred (parser
);
4763 cp_parser_parse_definitely (parser
);
4764 cp_parser_parse_definitely (parser
);
4766 cp_parser_simulate_error (parser
);
4771 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4772 This class is for tracking such a matching pair of symbols.
4773 In particular, it tracks the location of the first token,
4774 so that if the second token is missing, we can highlight the
4775 location of the first token when notifying the user about the
4778 template <typename traits_t
>
4782 /* token_pair's ctor. */
4783 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4785 /* If the next token is the opening symbol for this pair, consume it and
4787 Otherwise, issue an error and return false.
4788 In either case, record the location of the opening token. */
4790 bool require_open (cp_parser
*parser
)
4792 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4793 return cp_parser_require (parser
, traits_t::open_token_type
,
4794 traits_t::required_token_open
);
4797 /* Consume the next token from PARSER, recording its location as
4798 that of the opening token within the pair. */
4800 cp_token
* consume_open (cp_parser
*parser
)
4802 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4803 gcc_assert (tok
->type
== traits_t::open_token_type
);
4804 m_open_loc
= tok
->location
;
4808 /* If the next token is the closing symbol for this pair, consume it
4810 Otherwise, issue an error, highlighting the location of the
4811 corresponding opening token, and return NULL. */
4813 cp_token
*require_close (cp_parser
*parser
) const
4815 return cp_parser_require (parser
, traits_t::close_token_type
,
4816 traits_t::required_token_close
,
4821 location_t m_open_loc
;
4824 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4826 struct matching_paren_traits
4828 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4829 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4830 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4831 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4834 /* "matching_parens" is a token_pair<T> class for tracking matching
4835 pairs of parentheses. */
4837 typedef token_pair
<matching_paren_traits
> matching_parens
;
4839 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4841 struct matching_brace_traits
4843 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4844 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4845 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4846 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4849 /* "matching_braces" is a token_pair<T> class for tracking matching
4852 typedef token_pair
<matching_brace_traits
> matching_braces
;
4855 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4856 enclosing parentheses. */
4859 cp_parser_statement_expr (cp_parser
*parser
)
4861 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4863 /* Consume the '('. */
4864 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4865 matching_parens parens
;
4866 parens
.consume_open (parser
);
4867 /* Start the statement-expression. */
4868 tree expr
= begin_stmt_expr ();
4869 /* Parse the compound-statement. */
4870 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4872 expr
= finish_stmt_expr (expr
, false);
4873 /* Consume the ')'. */
4874 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4875 if (!parens
.require_close (parser
))
4876 cp_parser_skip_to_end_of_statement (parser
);
4878 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4879 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4880 return cp_expr (expr
, combined_loc
);
4883 /* Expressions [gram.expr] */
4885 /* Parse a fold-operator.
4888 - * / % ^ & | = < > << >>
4889 = -= *= /= %= ^= &= |= <<= >>=
4890 == != <= >= && || , .* ->*
4892 This returns the tree code corresponding to the matched operator
4893 as an int. When the current token matches a compound assignment
4894 opertor, the resulting tree code is the negative value of the
4895 non-assignment operator. */
4898 cp_parser_fold_operator (cp_token
*token
)
4900 switch (token
->type
)
4902 case CPP_PLUS
: return PLUS_EXPR
;
4903 case CPP_MINUS
: return MINUS_EXPR
;
4904 case CPP_MULT
: return MULT_EXPR
;
4905 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4906 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4907 case CPP_XOR
: return BIT_XOR_EXPR
;
4908 case CPP_AND
: return BIT_AND_EXPR
;
4909 case CPP_OR
: return BIT_IOR_EXPR
;
4910 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4911 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4913 case CPP_EQ
: return -NOP_EXPR
;
4914 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4915 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4916 case CPP_MULT_EQ
: return -MULT_EXPR
;
4917 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4918 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4919 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4920 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4921 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4922 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4923 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4925 case CPP_EQ_EQ
: return EQ_EXPR
;
4926 case CPP_NOT_EQ
: return NE_EXPR
;
4927 case CPP_LESS
: return LT_EXPR
;
4928 case CPP_GREATER
: return GT_EXPR
;
4929 case CPP_LESS_EQ
: return LE_EXPR
;
4930 case CPP_GREATER_EQ
: return GE_EXPR
;
4932 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4933 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4935 case CPP_COMMA
: return COMPOUND_EXPR
;
4937 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4938 case CPP_DEREF_STAR
: return MEMBER_REF
;
4940 default: return ERROR_MARK
;
4944 /* Returns true if CODE indicates a binary expression, which is not allowed in
4945 the LHS of a fold-expression. More codes will need to be added to use this
4946 function in other contexts. */
4949 is_binary_op (tree_code code
)
4954 case POINTER_PLUS_EXPR
:
4957 case TRUNC_DIV_EXPR
:
4958 case TRUNC_MOD_EXPR
:
4974 case TRUTH_ANDIF_EXPR
:
4975 case TRUTH_ORIF_EXPR
:
4988 /* If the next token is a suitable fold operator, consume it and return as
4989 the function above. */
4992 cp_parser_fold_operator (cp_parser
*parser
)
4994 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4995 int code
= cp_parser_fold_operator (token
);
4996 if (code
!= ERROR_MARK
)
4997 cp_lexer_consume_token (parser
->lexer
);
5001 /* Parse a fold-expression.
5004 ( ... folding-operator cast-expression)
5005 ( cast-expression folding-operator ... )
5006 ( cast-expression folding operator ... folding-operator cast-expression)
5008 Note that the '(' and ')' are matched in primary expression. */
5011 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5018 cp_lexer_consume_token (parser
->lexer
);
5019 int op
= cp_parser_fold_operator (parser
);
5020 if (op
== ERROR_MARK
)
5022 cp_parser_error (parser
, "expected binary operator");
5023 return error_mark_node
;
5026 tree expr
= cp_parser_cast_expression (parser
, false, false,
5028 if (expr
== error_mark_node
)
5029 return error_mark_node
;
5030 return finish_left_unary_fold_expr (expr
, op
);
5033 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5034 int op
= cp_parser_fold_operator (parser
);
5035 if (op
== ERROR_MARK
)
5037 cp_parser_error (parser
, "expected binary operator");
5038 return error_mark_node
;
5041 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5043 cp_parser_error (parser
, "expected ...");
5044 return error_mark_node
;
5046 cp_lexer_consume_token (parser
->lexer
);
5048 /* The operands of a fold-expression are cast-expressions, so binary or
5049 conditional expressions are not allowed. We check this here to avoid
5050 tentative parsing. */
5051 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5052 /* OK, the expression was parenthesized. */;
5053 else if (is_binary_op (TREE_CODE (expr1
)))
5054 error_at (location_of (expr1
),
5055 "binary expression in operand of fold-expression");
5056 else if (TREE_CODE (expr1
) == COND_EXPR
5057 || (REFERENCE_REF_P (expr1
)
5058 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5059 error_at (location_of (expr1
),
5060 "conditional expression in operand of fold-expression");
5063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5064 return finish_right_unary_fold_expr (expr1
, op
);
5066 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5068 cp_parser_error (parser
, "mismatched operator in fold-expression");
5069 return error_mark_node
;
5071 cp_lexer_consume_token (parser
->lexer
);
5073 // Binary left or right fold.
5074 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5075 if (expr2
== error_mark_node
)
5076 return error_mark_node
;
5077 return finish_binary_fold_expr (expr1
, expr2
, op
);
5080 /* Parse a primary-expression.
5087 lambda-expression (C++11)
5092 ( compound-statement )
5093 __builtin_va_arg ( assignment-expression , type-id )
5094 __builtin_offsetof ( type-id , offsetof-expression )
5097 __has_nothrow_assign ( type-id )
5098 __has_nothrow_constructor ( type-id )
5099 __has_nothrow_copy ( type-id )
5100 __has_trivial_assign ( type-id )
5101 __has_trivial_constructor ( type-id )
5102 __has_trivial_copy ( type-id )
5103 __has_trivial_destructor ( type-id )
5104 __has_virtual_destructor ( type-id )
5105 __is_abstract ( type-id )
5106 __is_base_of ( type-id , type-id )
5107 __is_class ( type-id )
5108 __is_empty ( type-id )
5109 __is_enum ( type-id )
5110 __is_final ( type-id )
5111 __is_literal_type ( type-id )
5112 __is_pod ( type-id )
5113 __is_polymorphic ( type-id )
5114 __is_std_layout ( type-id )
5115 __is_trivial ( type-id )
5116 __is_union ( type-id )
5118 Objective-C++ Extension:
5126 ADDRESS_P is true iff this expression was immediately preceded by
5127 "&" and therefore might denote a pointer-to-member. CAST_P is true
5128 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5129 true iff this expression is a template argument.
5131 Returns a representation of the expression. Upon return, *IDK
5132 indicates what kind of id-expression (if any) was present. */
5135 cp_parser_primary_expression (cp_parser
*parser
,
5138 bool template_arg_p
,
5142 cp_token
*token
= NULL
;
5144 /* Assume the primary expression is not an id-expression. */
5145 *idk
= CP_ID_KIND_NONE
;
5147 /* Peek at the next token. */
5148 token
= cp_lexer_peek_token (parser
->lexer
);
5149 switch ((int) token
->type
)
5158 user-defined-literal */
5165 case CPP_PREPARSED_EXPR
:
5166 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5167 return cp_parser_userdef_numeric_literal (parser
);
5168 token
= cp_lexer_consume_token (parser
->lexer
);
5169 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5171 error_at (token
->location
,
5172 "fixed-point types not supported in C++");
5173 return error_mark_node
;
5175 /* Floating-point literals are only allowed in an integral
5176 constant expression if they are cast to an integral or
5177 enumeration type. */
5178 if (TREE_CODE (token
->u
.value
) == REAL_CST
5179 && parser
->integral_constant_expression_p
5182 /* CAST_P will be set even in invalid code like "int(2.7 +
5183 ...)". Therefore, we have to check that the next token
5184 is sure to end the cast. */
5187 cp_token
*next_token
;
5189 next_token
= cp_lexer_peek_token (parser
->lexer
);
5190 if (/* The comma at the end of an
5191 enumerator-definition. */
5192 next_token
->type
!= CPP_COMMA
5193 /* The curly brace at the end of an enum-specifier. */
5194 && next_token
->type
!= CPP_CLOSE_BRACE
5195 /* The end of a statement. */
5196 && next_token
->type
!= CPP_SEMICOLON
5197 /* The end of the cast-expression. */
5198 && next_token
->type
!= CPP_CLOSE_PAREN
5199 /* The end of an array bound. */
5200 && next_token
->type
!= CPP_CLOSE_SQUARE
5201 /* The closing ">" in a template-argument-list. */
5202 && (next_token
->type
!= CPP_GREATER
5203 || parser
->greater_than_is_operator_p
)
5204 /* C++0x only: A ">>" treated like two ">" tokens,
5205 in a template-argument-list. */
5206 && (next_token
->type
!= CPP_RSHIFT
5207 || (cxx_dialect
== cxx98
)
5208 || parser
->greater_than_is_operator_p
))
5212 /* If we are within a cast, then the constraint that the
5213 cast is to an integral or enumeration type will be
5214 checked at that point. If we are not within a cast, then
5215 this code is invalid. */
5217 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5219 return cp_expr (token
->u
.value
, token
->location
);
5221 case CPP_CHAR_USERDEF
:
5222 case CPP_CHAR16_USERDEF
:
5223 case CPP_CHAR32_USERDEF
:
5224 case CPP_WCHAR_USERDEF
:
5225 case CPP_UTF8CHAR_USERDEF
:
5226 return cp_parser_userdef_char_literal (parser
);
5232 case CPP_UTF8STRING
:
5233 case CPP_STRING_USERDEF
:
5234 case CPP_STRING16_USERDEF
:
5235 case CPP_STRING32_USERDEF
:
5236 case CPP_WSTRING_USERDEF
:
5237 case CPP_UTF8STRING_USERDEF
:
5238 /* ??? Should wide strings be allowed when parser->translate_strings_p
5239 is false (i.e. in attributes)? If not, we can kill the third
5240 argument to cp_parser_string_literal. */
5241 return cp_parser_string_literal (parser
,
5242 parser
->translate_strings_p
,
5245 case CPP_OPEN_PAREN
:
5246 /* If we see `( { ' then we are looking at the beginning of
5247 a GNU statement-expression. */
5248 if (cp_parser_allow_gnu_extensions_p (parser
)
5249 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5251 /* Statement-expressions are not allowed by the standard. */
5252 pedwarn (token
->location
, OPT_Wpedantic
,
5253 "ISO C++ forbids braced-groups within expressions");
5255 /* And they're not allowed outside of a function-body; you
5256 cannot, for example, write:
5258 int i = ({ int j = 3; j + 1; });
5260 at class or namespace scope. */
5261 if (!parser
->in_function_body
5262 || parser
->in_template_argument_list_p
)
5264 error_at (token
->location
,
5265 "statement-expressions are not allowed outside "
5266 "functions nor in template-argument lists");
5267 cp_parser_skip_to_end_of_block_or_statement (parser
);
5268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5269 cp_lexer_consume_token (parser
->lexer
);
5270 return error_mark_node
;
5273 return cp_parser_statement_expr (parser
);
5275 /* Otherwise it's a normal parenthesized expression. */
5278 bool saved_greater_than_is_operator_p
;
5280 location_t open_paren_loc
= token
->location
;
5282 /* Consume the `('. */
5283 matching_parens parens
;
5284 parens
.consume_open (parser
);
5285 /* Within a parenthesized expression, a `>' token is always
5286 the greater-than operator. */
5287 saved_greater_than_is_operator_p
5288 = parser
->greater_than_is_operator_p
;
5289 parser
->greater_than_is_operator_p
= true;
5291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5292 /* Left fold expression. */
5295 /* Parse the parenthesized expression. */
5296 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5298 token
= cp_lexer_peek_token (parser
->lexer
);
5299 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5301 expr
= cp_parser_fold_expression (parser
, expr
);
5302 if (expr
!= error_mark_node
5303 && cxx_dialect
< cxx17
5304 && !in_system_header_at (input_location
))
5305 pedwarn (input_location
, 0, "fold-expressions only available "
5306 "with -std=c++17 or -std=gnu++17");
5309 /* Let the front end know that this expression was
5310 enclosed in parentheses. This matters in case, for
5311 example, the expression is of the form `A::B', since
5312 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5314 expr
= finish_parenthesized_expr (expr
);
5316 /* DR 705: Wrapping an unqualified name in parentheses
5317 suppresses arg-dependent lookup. We want to pass back
5318 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5319 (c++/37862), but none of the others. */
5320 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5321 *idk
= CP_ID_KIND_NONE
;
5323 /* The `>' token might be the end of a template-id or
5324 template-parameter-list now. */
5325 parser
->greater_than_is_operator_p
5326 = saved_greater_than_is_operator_p
;
5328 /* Consume the `)'. */
5329 token
= cp_lexer_peek_token (parser
->lexer
);
5330 location_t close_paren_loc
= token
->location
;
5331 expr
.set_range (open_paren_loc
, close_paren_loc
);
5332 if (!parens
.require_close (parser
)
5333 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5334 cp_parser_skip_to_end_of_statement (parser
);
5339 case CPP_OPEN_SQUARE
:
5341 if (c_dialect_objc ())
5343 /* We might have an Objective-C++ message. */
5344 cp_parser_parse_tentatively (parser
);
5345 tree msg
= cp_parser_objc_message_expression (parser
);
5346 /* If that works out, we're done ... */
5347 if (cp_parser_parse_definitely (parser
))
5349 /* ... else, fall though to see if it's a lambda. */
5351 cp_expr lam
= cp_parser_lambda_expression (parser
);
5352 /* Don't warn about a failed tentative parse. */
5353 if (cp_parser_error_occurred (parser
))
5354 return error_mark_node
;
5355 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5359 case CPP_OBJC_STRING
:
5360 if (c_dialect_objc ())
5361 /* We have an Objective-C++ string literal. */
5362 return cp_parser_objc_expression (parser
);
5363 cp_parser_error (parser
, "expected primary-expression");
5364 return error_mark_node
;
5367 switch (token
->keyword
)
5369 /* These two are the boolean literals. */
5371 cp_lexer_consume_token (parser
->lexer
);
5372 return cp_expr (boolean_true_node
, token
->location
);
5374 cp_lexer_consume_token (parser
->lexer
);
5375 return cp_expr (boolean_false_node
, token
->location
);
5377 /* The `__null' literal. */
5379 cp_lexer_consume_token (parser
->lexer
);
5380 return cp_expr (null_node
, token
->location
);
5382 /* The `nullptr' literal. */
5384 cp_lexer_consume_token (parser
->lexer
);
5385 return cp_expr (nullptr_node
, token
->location
);
5387 /* Recognize the `this' keyword. */
5389 cp_lexer_consume_token (parser
->lexer
);
5390 if (parser
->local_variables_forbidden_p
)
5392 error_at (token
->location
,
5393 "%<this%> may not be used in this context");
5394 return error_mark_node
;
5396 /* Pointers cannot appear in constant-expressions. */
5397 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5398 return error_mark_node
;
5399 return cp_expr (finish_this_expr (), token
->location
);
5401 /* The `operator' keyword can be the beginning of an
5406 case RID_FUNCTION_NAME
:
5407 case RID_PRETTY_FUNCTION_NAME
:
5408 case RID_C99_FUNCTION_NAME
:
5410 non_integral_constant name
;
5412 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5413 __func__ are the names of variables -- but they are
5414 treated specially. Therefore, they are handled here,
5415 rather than relying on the generic id-expression logic
5416 below. Grammatically, these names are id-expressions.
5418 Consume the token. */
5419 token
= cp_lexer_consume_token (parser
->lexer
);
5421 switch (token
->keyword
)
5423 case RID_FUNCTION_NAME
:
5424 name
= NIC_FUNC_NAME
;
5426 case RID_PRETTY_FUNCTION_NAME
:
5427 name
= NIC_PRETTY_FUNC
;
5429 case RID_C99_FUNCTION_NAME
:
5430 name
= NIC_C99_FUNC
;
5436 if (cp_parser_non_integral_constant_expression (parser
, name
))
5437 return error_mark_node
;
5439 /* Look up the name. */
5440 return finish_fname (token
->u
.value
);
5447 location_t type_location
;
5448 location_t start_loc
5449 = cp_lexer_peek_token (parser
->lexer
)->location
;
5450 /* The `__builtin_va_arg' construct is used to handle
5451 `va_arg'. Consume the `__builtin_va_arg' token. */
5452 cp_lexer_consume_token (parser
->lexer
);
5453 /* Look for the opening `('. */
5454 matching_parens parens
;
5455 parens
.require_open (parser
);
5456 /* Now, parse the assignment-expression. */
5457 expression
= cp_parser_assignment_expression (parser
);
5458 /* Look for the `,'. */
5459 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5460 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5461 /* Parse the type-id. */
5463 type_id_in_expr_sentinel
s (parser
);
5464 type
= cp_parser_type_id (parser
);
5466 /* Look for the closing `)'. */
5467 location_t finish_loc
5468 = cp_lexer_peek_token (parser
->lexer
)->location
;
5469 parens
.require_close (parser
);
5470 /* Using `va_arg' in a constant-expression is not
5472 if (cp_parser_non_integral_constant_expression (parser
,
5474 return error_mark_node
;
5475 /* Construct a location of the form:
5476 __builtin_va_arg (v, int)
5477 ~~~~~~~~~~~~~~~~~~~~~^~~~
5478 with the caret at the type, ranging from the start of the
5479 "__builtin_va_arg" token to the close paren. */
5480 location_t combined_loc
5481 = make_location (type_location
, start_loc
, finish_loc
);
5482 return build_x_va_arg (combined_loc
, expression
, type
);
5486 return cp_parser_builtin_offsetof (parser
);
5488 case RID_HAS_NOTHROW_ASSIGN
:
5489 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5490 case RID_HAS_NOTHROW_COPY
:
5491 case RID_HAS_TRIVIAL_ASSIGN
:
5492 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5493 case RID_HAS_TRIVIAL_COPY
:
5494 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5495 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5496 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5497 case RID_IS_ABSTRACT
:
5498 case RID_IS_AGGREGATE
:
5499 case RID_IS_BASE_OF
:
5504 case RID_IS_LITERAL_TYPE
:
5506 case RID_IS_POLYMORPHIC
:
5507 case RID_IS_SAME_AS
:
5508 case RID_IS_STD_LAYOUT
:
5509 case RID_IS_TRIVIAL
:
5510 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5511 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5512 case RID_IS_TRIVIALLY_COPYABLE
:
5514 case RID_IS_ASSIGNABLE
:
5515 case RID_IS_CONSTRUCTIBLE
:
5516 return cp_parser_trait_expr (parser
, token
->keyword
);
5520 return cp_parser_requires_expression (parser
);
5522 /* Objective-C++ expressions. */
5524 case RID_AT_PROTOCOL
:
5525 case RID_AT_SELECTOR
:
5526 return cp_parser_objc_expression (parser
);
5529 if (parser
->in_function_body
5530 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5533 error_at (token
->location
,
5534 "a template declaration cannot appear at block scope");
5535 cp_parser_skip_to_end_of_block_or_statement (parser
);
5536 return error_mark_node
;
5540 cp_parser_error (parser
, "expected primary-expression");
5541 return error_mark_node
;
5544 /* An id-expression can start with either an identifier, a
5545 `::' as the beginning of a qualified-id, or the "operator"
5549 case CPP_TEMPLATE_ID
:
5550 case CPP_NESTED_NAME_SPECIFIER
:
5553 cp_expr id_expression
;
5555 const char *error_msg
;
5558 cp_token
*id_expr_token
;
5560 /* Parse the id-expression. */
5562 = cp_parser_id_expression (parser
,
5563 /*template_keyword_p=*/false,
5564 /*check_dependency_p=*/true,
5566 /*declarator_p=*/false,
5567 /*optional_p=*/false);
5568 if (id_expression
== error_mark_node
)
5569 return error_mark_node
;
5570 id_expr_token
= token
;
5571 token
= cp_lexer_peek_token (parser
->lexer
);
5572 done
= (token
->type
!= CPP_OPEN_SQUARE
5573 && token
->type
!= CPP_OPEN_PAREN
5574 && token
->type
!= CPP_DOT
5575 && token
->type
!= CPP_DEREF
5576 && token
->type
!= CPP_PLUS_PLUS
5577 && token
->type
!= CPP_MINUS_MINUS
);
5578 /* If we have a template-id, then no further lookup is
5579 required. If the template-id was for a template-class, we
5580 will sometimes have a TYPE_DECL at this point. */
5581 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5582 || TREE_CODE (id_expression
) == TYPE_DECL
)
5583 decl
= id_expression
;
5584 /* Look up the name. */
5587 tree ambiguous_decls
;
5589 /* If we already know that this lookup is ambiguous, then
5590 we've already issued an error message; there's no reason
5592 if (id_expr_token
->type
== CPP_NAME
5593 && id_expr_token
->error_reported
)
5595 cp_parser_simulate_error (parser
);
5596 return error_mark_node
;
5599 decl
= cp_parser_lookup_name (parser
, id_expression
,
5602 /*is_namespace=*/false,
5603 /*check_dependency=*/true,
5605 id_expr_token
->location
);
5606 /* If the lookup was ambiguous, an error will already have
5608 if (ambiguous_decls
)
5609 return error_mark_node
;
5611 /* In Objective-C++, we may have an Objective-C 2.0
5612 dot-syntax for classes here. */
5613 if (c_dialect_objc ()
5614 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5615 && TREE_CODE (decl
) == TYPE_DECL
5616 && objc_is_class_name (decl
))
5619 cp_lexer_consume_token (parser
->lexer
);
5620 component
= cp_parser_identifier (parser
);
5621 if (component
== error_mark_node
)
5622 return error_mark_node
;
5624 tree result
= objc_build_class_component_ref (id_expression
,
5626 /* Build a location of the form:
5629 with caret at the start of the component name (at
5630 input_location), ranging from the start of the id_expression
5631 to the end of the component name. */
5632 location_t combined_loc
5633 = make_location (input_location
, id_expression
.get_start (),
5634 get_finish (input_location
));
5635 protected_set_expr_location (result
, combined_loc
);
5639 /* In Objective-C++, an instance variable (ivar) may be preferred
5640 to whatever cp_parser_lookup_name() found.
5641 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5642 rest of c-family, we have to do a little extra work to preserve
5643 any location information in cp_expr "decl". Given that
5644 objc_lookup_ivar is implemented in "c-family" and "objc", we
5645 have a trip through the pure "tree" type, rather than cp_expr.
5646 Naively copying it back to "decl" would implicitly give the
5647 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5648 store an EXPR_LOCATION. Hence we only update "decl" (and
5649 hence its location_t) if we get back a different tree node. */
5650 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5652 if (decl_tree
!= decl
.get_value ())
5653 decl
= cp_expr (decl_tree
);
5655 /* If name lookup gives us a SCOPE_REF, then the
5656 qualifying scope was dependent. */
5657 if (TREE_CODE (decl
) == SCOPE_REF
)
5659 /* At this point, we do not know if DECL is a valid
5660 integral constant expression. We assume that it is
5661 in fact such an expression, so that code like:
5663 template <int N> struct A {
5667 is accepted. At template-instantiation time, we
5668 will check that B<N>::i is actually a constant. */
5671 /* Check to see if DECL is a local variable in a context
5672 where that is forbidden. */
5673 if (parser
->local_variables_forbidden_p
5674 && local_variable_p (decl
))
5676 error_at (id_expr_token
->location
,
5677 "local variable %qD may not appear in this context",
5679 return error_mark_node
;
5683 decl
= (finish_id_expression
5684 (id_expression
, decl
, parser
->scope
,
5686 parser
->integral_constant_expression_p
,
5687 parser
->allow_non_integral_constant_expression_p
,
5688 &parser
->non_integral_constant_expression_p
,
5689 template_p
, done
, address_p
,
5692 id_expression
.get_location ()));
5694 cp_parser_error (parser
, error_msg
);
5695 decl
.set_location (id_expr_token
->location
);
5699 /* Anything else is an error. */
5701 cp_parser_error (parser
, "expected primary-expression");
5702 return error_mark_node
;
5706 static inline cp_expr
5707 cp_parser_primary_expression (cp_parser
*parser
,
5710 bool template_arg_p
,
5713 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5714 /*decltype*/false, idk
);
5717 /* Parse an id-expression.
5724 :: [opt] nested-name-specifier template [opt] unqualified-id
5726 :: operator-function-id
5729 Return a representation of the unqualified portion of the
5730 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5731 a `::' or nested-name-specifier.
5733 Often, if the id-expression was a qualified-id, the caller will
5734 want to make a SCOPE_REF to represent the qualified-id. This
5735 function does not do this in order to avoid wastefully creating
5736 SCOPE_REFs when they are not required.
5738 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5741 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5742 uninstantiated templates.
5744 If *TEMPLATE_P is non-NULL, it is set to true iff the
5745 `template' keyword is used to explicitly indicate that the entity
5746 named is a template.
5748 If DECLARATOR_P is true, the id-expression is appearing as part of
5749 a declarator, rather than as part of an expression. */
5752 cp_parser_id_expression (cp_parser
*parser
,
5753 bool template_keyword_p
,
5754 bool check_dependency_p
,
5759 bool global_scope_p
;
5760 bool nested_name_specifier_p
;
5762 /* Assume the `template' keyword was not used. */
5764 *template_p
= template_keyword_p
;
5766 /* Look for the optional `::' operator. */
5768 = (!template_keyword_p
5769 && (cp_parser_global_scope_opt (parser
,
5770 /*current_scope_valid_p=*/false)
5773 /* Look for the optional nested-name-specifier. */
5774 nested_name_specifier_p
5775 = (cp_parser_nested_name_specifier_opt (parser
,
5776 /*typename_keyword_p=*/false,
5783 /* If there is a nested-name-specifier, then we are looking at
5784 the first qualified-id production. */
5785 if (nested_name_specifier_p
)
5788 tree saved_object_scope
;
5789 tree saved_qualifying_scope
;
5790 cp_expr unqualified_id
;
5793 /* See if the next token is the `template' keyword. */
5795 template_p
= &is_template
;
5796 *template_p
= cp_parser_optional_template_keyword (parser
);
5797 /* Name lookup we do during the processing of the
5798 unqualified-id might obliterate SCOPE. */
5799 saved_scope
= parser
->scope
;
5800 saved_object_scope
= parser
->object_scope
;
5801 saved_qualifying_scope
= parser
->qualifying_scope
;
5802 /* Process the final unqualified-id. */
5803 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5806 /*optional_p=*/false);
5807 /* Restore the SAVED_SCOPE for our caller. */
5808 parser
->scope
= saved_scope
;
5809 parser
->object_scope
= saved_object_scope
;
5810 parser
->qualifying_scope
= saved_qualifying_scope
;
5812 return unqualified_id
;
5814 /* Otherwise, if we are in global scope, then we are looking at one
5815 of the other qualified-id productions. */
5816 else if (global_scope_p
)
5821 /* Peek at the next token. */
5822 token
= cp_lexer_peek_token (parser
->lexer
);
5824 /* If it's an identifier, and the next token is not a "<", then
5825 we can avoid the template-id case. This is an optimization
5826 for this common case. */
5827 if (token
->type
== CPP_NAME
5828 && !cp_parser_nth_token_starts_template_argument_list_p
5830 return cp_parser_identifier (parser
);
5832 cp_parser_parse_tentatively (parser
);
5833 /* Try a template-id. */
5834 id
= cp_parser_template_id (parser
,
5835 /*template_keyword_p=*/false,
5836 /*check_dependency_p=*/true,
5839 /* If that worked, we're done. */
5840 if (cp_parser_parse_definitely (parser
))
5843 /* Peek at the next token. (Changes in the token buffer may
5844 have invalidated the pointer obtained above.) */
5845 token
= cp_lexer_peek_token (parser
->lexer
);
5847 switch (token
->type
)
5850 return cp_parser_identifier (parser
);
5853 if (token
->keyword
== RID_OPERATOR
)
5854 return cp_parser_operator_function_id (parser
);
5858 cp_parser_error (parser
, "expected id-expression");
5859 return error_mark_node
;
5863 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5864 /*check_dependency_p=*/true,
5869 /* Parse an unqualified-id.
5873 operator-function-id
5874 conversion-function-id
5878 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5879 keyword, in a construct like `A::template ...'.
5881 Returns a representation of unqualified-id. For the `identifier'
5882 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5883 production a BIT_NOT_EXPR is returned; the operand of the
5884 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5885 other productions, see the documentation accompanying the
5886 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5887 names are looked up in uninstantiated templates. If DECLARATOR_P
5888 is true, the unqualified-id is appearing as part of a declarator,
5889 rather than as part of an expression. */
5892 cp_parser_unqualified_id (cp_parser
* parser
,
5893 bool template_keyword_p
,
5894 bool check_dependency_p
,
5900 /* Peek at the next token. */
5901 token
= cp_lexer_peek_token (parser
->lexer
);
5903 switch ((int) token
->type
)
5909 /* We don't know yet whether or not this will be a
5911 cp_parser_parse_tentatively (parser
);
5912 /* Try a template-id. */
5913 id
= cp_parser_template_id (parser
, template_keyword_p
,
5917 /* If it worked, we're done. */
5918 if (cp_parser_parse_definitely (parser
))
5920 /* Otherwise, it's an ordinary identifier. */
5921 return cp_parser_identifier (parser
);
5924 case CPP_TEMPLATE_ID
:
5925 return cp_parser_template_id (parser
, template_keyword_p
,
5933 tree qualifying_scope
;
5938 /* Consume the `~' token. */
5939 cp_lexer_consume_token (parser
->lexer
);
5940 /* Parse the class-name. The standard, as written, seems to
5943 template <typename T> struct S { ~S (); };
5944 template <typename T> S<T>::~S() {}
5946 is invalid, since `~' must be followed by a class-name, but
5947 `S<T>' is dependent, and so not known to be a class.
5948 That's not right; we need to look in uninstantiated
5949 templates. A further complication arises from:
5951 template <typename T> void f(T t) {
5955 Here, it is not possible to look up `T' in the scope of `T'
5956 itself. We must look in both the current scope, and the
5957 scope of the containing complete expression.
5959 Yet another issue is:
5968 The standard does not seem to say that the `S' in `~S'
5969 should refer to the type `S' and not the data member
5972 /* DR 244 says that we look up the name after the "~" in the
5973 same scope as we looked up the qualifying name. That idea
5974 isn't fully worked out; it's more complicated than that. */
5975 scope
= parser
->scope
;
5976 object_scope
= parser
->object_scope
;
5977 qualifying_scope
= parser
->qualifying_scope
;
5979 /* Check for invalid scopes. */
5980 if (scope
== error_mark_node
)
5982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5983 cp_lexer_consume_token (parser
->lexer
);
5984 return error_mark_node
;
5986 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5988 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5989 error_at (token
->location
,
5990 "scope %qT before %<~%> is not a class-name",
5992 cp_parser_simulate_error (parser
);
5993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5994 cp_lexer_consume_token (parser
->lexer
);
5995 return error_mark_node
;
5997 gcc_assert (!scope
|| TYPE_P (scope
));
5999 /* If the name is of the form "X::~X" it's OK even if X is a
6001 token
= cp_lexer_peek_token (parser
->lexer
);
6003 && token
->type
== CPP_NAME
6004 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6006 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6007 || (CLASS_TYPE_P (scope
)
6008 && constructor_name_p (token
->u
.value
, scope
))))
6010 cp_lexer_consume_token (parser
->lexer
);
6011 return build_nt (BIT_NOT_EXPR
, scope
);
6014 /* ~auto means the destructor of whatever the object is. */
6015 if (cp_parser_is_keyword (token
, RID_AUTO
))
6017 if (cxx_dialect
< cxx14
)
6018 pedwarn (input_location
, 0,
6019 "%<~auto%> only available with "
6020 "-std=c++14 or -std=gnu++14");
6021 cp_lexer_consume_token (parser
->lexer
);
6022 return build_nt (BIT_NOT_EXPR
, make_auto ());
6025 /* If there was an explicit qualification (S::~T), first look
6026 in the scope given by the qualification (i.e., S).
6028 Note: in the calls to cp_parser_class_name below we pass
6029 typename_type so that lookup finds the injected-class-name
6030 rather than the constructor. */
6032 type_decl
= NULL_TREE
;
6035 cp_parser_parse_tentatively (parser
);
6036 type_decl
= cp_parser_class_name (parser
,
6037 /*typename_keyword_p=*/false,
6038 /*template_keyword_p=*/false,
6040 /*check_dependency=*/false,
6041 /*class_head_p=*/false,
6043 if (cp_parser_parse_definitely (parser
))
6046 /* In "N::S::~S", look in "N" as well. */
6047 if (!done
&& scope
&& qualifying_scope
)
6049 cp_parser_parse_tentatively (parser
);
6050 parser
->scope
= qualifying_scope
;
6051 parser
->object_scope
= NULL_TREE
;
6052 parser
->qualifying_scope
= NULL_TREE
;
6054 = cp_parser_class_name (parser
,
6055 /*typename_keyword_p=*/false,
6056 /*template_keyword_p=*/false,
6058 /*check_dependency=*/false,
6059 /*class_head_p=*/false,
6061 if (cp_parser_parse_definitely (parser
))
6064 /* In "p->S::~T", look in the scope given by "*p" as well. */
6065 else if (!done
&& object_scope
)
6067 cp_parser_parse_tentatively (parser
);
6068 parser
->scope
= object_scope
;
6069 parser
->object_scope
= NULL_TREE
;
6070 parser
->qualifying_scope
= NULL_TREE
;
6072 = cp_parser_class_name (parser
,
6073 /*typename_keyword_p=*/false,
6074 /*template_keyword_p=*/false,
6076 /*check_dependency=*/false,
6077 /*class_head_p=*/false,
6079 if (cp_parser_parse_definitely (parser
))
6082 /* Look in the surrounding context. */
6085 parser
->scope
= NULL_TREE
;
6086 parser
->object_scope
= NULL_TREE
;
6087 parser
->qualifying_scope
= NULL_TREE
;
6088 if (processing_template_decl
)
6089 cp_parser_parse_tentatively (parser
);
6091 = cp_parser_class_name (parser
,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6098 if (processing_template_decl
6099 && ! cp_parser_parse_definitely (parser
))
6101 /* We couldn't find a type with this name. If we're parsing
6102 tentatively, fail and try something else. */
6103 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6105 cp_parser_simulate_error (parser
);
6106 return error_mark_node
;
6108 /* Otherwise, accept it and check for a match at instantiation
6110 type_decl
= cp_parser_identifier (parser
);
6111 if (type_decl
!= error_mark_node
)
6112 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6116 /* If an error occurred, assume that the name of the
6117 destructor is the same as the name of the qualifying
6118 class. That allows us to keep parsing after running
6119 into ill-formed destructor names. */
6120 if (type_decl
== error_mark_node
&& scope
)
6121 return build_nt (BIT_NOT_EXPR
, scope
);
6122 else if (type_decl
== error_mark_node
)
6123 return error_mark_node
;
6125 /* Check that destructor name and scope match. */
6126 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6128 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6129 error_at (token
->location
,
6130 "declaration of %<~%T%> as member of %qT",
6132 cp_parser_simulate_error (parser
);
6133 return error_mark_node
;
6138 A typedef-name that names a class shall not be used as the
6139 identifier in the declarator for a destructor declaration. */
6141 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6142 && !DECL_SELF_REFERENCE_P (type_decl
)
6143 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6144 error_at (token
->location
,
6145 "typedef-name %qD used as destructor declarator",
6148 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6152 if (token
->keyword
== RID_OPERATOR
)
6156 /* This could be a template-id, so we try that first. */
6157 cp_parser_parse_tentatively (parser
);
6158 /* Try a template-id. */
6159 id
= cp_parser_template_id (parser
, template_keyword_p
,
6160 /*check_dependency_p=*/true,
6163 /* If that worked, we're done. */
6164 if (cp_parser_parse_definitely (parser
))
6166 /* We still don't know whether we're looking at an
6167 operator-function-id or a conversion-function-id. */
6168 cp_parser_parse_tentatively (parser
);
6169 /* Try an operator-function-id. */
6170 id
= cp_parser_operator_function_id (parser
);
6171 /* If that didn't work, try a conversion-function-id. */
6172 if (!cp_parser_parse_definitely (parser
))
6173 id
= cp_parser_conversion_function_id (parser
);
6182 cp_parser_error (parser
, "expected unqualified-id");
6183 return error_mark_node
;
6187 /* Parse an (optional) nested-name-specifier.
6189 nested-name-specifier: [C++98]
6190 class-or-namespace-name :: nested-name-specifier [opt]
6191 class-or-namespace-name :: template nested-name-specifier [opt]
6193 nested-name-specifier: [C++0x]
6196 nested-name-specifier identifier ::
6197 nested-name-specifier template [opt] simple-template-id ::
6199 PARSER->SCOPE should be set appropriately before this function is
6200 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6201 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6204 Sets PARSER->SCOPE to the class (TYPE) or namespace
6205 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6206 it unchanged if there is no nested-name-specifier. Returns the new
6207 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6209 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6210 part of a declaration and/or decl-specifier. */
6213 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6214 bool typename_keyword_p
,
6215 bool check_dependency_p
,
6217 bool is_declaration
,
6218 bool template_keyword_p
/* = false */)
6220 bool success
= false;
6221 cp_token_position start
= 0;
6224 /* Remember where the nested-name-specifier starts. */
6225 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6227 start
= cp_lexer_token_position (parser
->lexer
, false);
6228 push_deferring_access_checks (dk_deferred
);
6235 tree saved_qualifying_scope
;
6237 /* Spot cases that cannot be the beginning of a
6238 nested-name-specifier. */
6239 token
= cp_lexer_peek_token (parser
->lexer
);
6241 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6242 the already parsed nested-name-specifier. */
6243 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6245 /* Grab the nested-name-specifier and continue the loop. */
6246 cp_parser_pre_parsed_nested_name_specifier (parser
);
6247 /* If we originally encountered this nested-name-specifier
6248 with IS_DECLARATION set to false, we will not have
6249 resolved TYPENAME_TYPEs, so we must do so here. */
6251 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6253 new_scope
= resolve_typename_type (parser
->scope
,
6254 /*only_current_p=*/false);
6255 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6256 parser
->scope
= new_scope
;
6262 /* Spot cases that cannot be the beginning of a
6263 nested-name-specifier. On the second and subsequent times
6264 through the loop, we look for the `template' keyword. */
6265 if (success
&& token
->keyword
== RID_TEMPLATE
)
6267 /* A template-id can start a nested-name-specifier. */
6268 else if (token
->type
== CPP_TEMPLATE_ID
)
6270 /* DR 743: decltype can be used in a nested-name-specifier. */
6271 else if (token_is_decltype (token
))
6275 /* If the next token is not an identifier, then it is
6276 definitely not a type-name or namespace-name. */
6277 if (token
->type
!= CPP_NAME
)
6279 /* If the following token is neither a `<' (to begin a
6280 template-id), nor a `::', then we are not looking at a
6281 nested-name-specifier. */
6282 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6284 if (token
->type
== CPP_COLON
6285 && parser
->colon_corrects_to_scope_p
6286 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6288 gcc_rich_location
richloc (token
->location
);
6289 richloc
.add_fixit_replace ("::");
6291 "found %<:%> in nested-name-specifier, "
6293 token
->type
= CPP_SCOPE
;
6296 if (token
->type
!= CPP_SCOPE
6297 && !cp_parser_nth_token_starts_template_argument_list_p
6302 /* The nested-name-specifier is optional, so we parse
6304 cp_parser_parse_tentatively (parser
);
6306 /* Look for the optional `template' keyword, if this isn't the
6307 first time through the loop. */
6309 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6311 /* Save the old scope since the name lookup we are about to do
6312 might destroy it. */
6313 old_scope
= parser
->scope
;
6314 saved_qualifying_scope
= parser
->qualifying_scope
;
6315 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6316 look up names in "X<T>::I" in order to determine that "Y" is
6317 a template. So, if we have a typename at this point, we make
6318 an effort to look through it. */
6320 && !typename_keyword_p
6322 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6323 parser
->scope
= resolve_typename_type (parser
->scope
,
6324 /*only_current_p=*/false);
6325 /* Parse the qualifying entity. */
6327 = cp_parser_qualifying_entity (parser
,
6333 /* Look for the `::' token. */
6334 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6336 /* If we found what we wanted, we keep going; otherwise, we're
6338 if (!cp_parser_parse_definitely (parser
))
6340 bool error_p
= false;
6342 /* Restore the OLD_SCOPE since it was valid before the
6343 failed attempt at finding the last
6344 class-or-namespace-name. */
6345 parser
->scope
= old_scope
;
6346 parser
->qualifying_scope
= saved_qualifying_scope
;
6348 /* If the next token is a decltype, and the one after that is a
6349 `::', then the decltype has failed to resolve to a class or
6350 enumeration type. Give this error even when parsing
6351 tentatively since it can't possibly be valid--and we're going
6352 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6353 won't get another chance.*/
6354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6355 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6358 token
= cp_lexer_consume_token (parser
->lexer
);
6359 error_at (token
->location
, "decltype evaluates to %qT, "
6360 "which is not a class or enumeration type",
6361 token
->u
.tree_check_value
->value
);
6362 parser
->scope
= error_mark_node
;
6366 cp_lexer_consume_token (parser
->lexer
);
6369 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6370 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6372 /* If we have a non-type template-id followed by ::, it can't
6373 possibly be valid. */
6374 token
= cp_lexer_peek_token (parser
->lexer
);
6375 tree tid
= token
->u
.tree_check_value
->value
;
6376 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6377 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6379 tree tmpl
= NULL_TREE
;
6380 if (is_overloaded_fn (tid
))
6382 tree fns
= get_fns (tid
);
6383 if (OVL_SINGLE_P (fns
))
6384 tmpl
= OVL_FIRST (fns
);
6385 error_at (token
->location
, "function template-id %qD "
6386 "in nested-name-specifier", tid
);
6390 /* Variable template. */
6391 tmpl
= TREE_OPERAND (tid
, 0);
6392 gcc_assert (variable_template_p (tmpl
));
6393 error_at (token
->location
, "variable template-id %qD "
6394 "in nested-name-specifier", tid
);
6397 inform (DECL_SOURCE_LOCATION (tmpl
),
6398 "%qD declared here", tmpl
);
6400 parser
->scope
= error_mark_node
;
6404 cp_lexer_consume_token (parser
->lexer
);
6405 cp_lexer_consume_token (parser
->lexer
);
6409 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6411 /* If the next token is an identifier, and the one after
6412 that is a `::', then any valid interpretation would have
6413 found a class-or-namespace-name. */
6414 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6415 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6417 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6420 token
= cp_lexer_consume_token (parser
->lexer
);
6423 if (!token
->error_reported
)
6426 tree ambiguous_decls
;
6428 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6430 /*is_template=*/false,
6431 /*is_namespace=*/false,
6432 /*check_dependency=*/true,
6435 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6436 error_at (token
->location
,
6437 "%qD used without template arguments",
6439 else if (ambiguous_decls
)
6441 // cp_parser_lookup_name has the same diagnostic,
6442 // thus make sure to emit it at most once.
6443 if (cp_parser_uncommitted_to_tentative_parse_p
6446 error_at (token
->location
,
6447 "reference to %qD is ambiguous",
6449 print_candidates (ambiguous_decls
);
6451 decl
= error_mark_node
;
6455 if (cxx_dialect
!= cxx98
)
6456 cp_parser_name_lookup_error
6457 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6460 cp_parser_name_lookup_error
6461 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6465 parser
->scope
= error_mark_node
;
6467 /* Treat this as a successful nested-name-specifier
6472 If the name found is not a class-name (clause
6473 _class_) or namespace-name (_namespace.def_), the
6474 program is ill-formed. */
6477 cp_lexer_consume_token (parser
->lexer
);
6481 /* We've found one valid nested-name-specifier. */
6483 /* Name lookup always gives us a DECL. */
6484 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6485 new_scope
= TREE_TYPE (new_scope
);
6486 /* Uses of "template" must be followed by actual templates. */
6487 if (template_keyword_p
6488 && !(CLASS_TYPE_P (new_scope
)
6489 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6490 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6491 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6492 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6493 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6494 == TEMPLATE_ID_EXPR
)))
6495 permerror (input_location
, TYPE_P (new_scope
)
6496 ? G_("%qT is not a template")
6497 : G_("%qD is not a template"),
6499 /* If it is a class scope, try to complete it; we are about to
6500 be looking up names inside the class. */
6501 if (TYPE_P (new_scope
)
6502 /* Since checking types for dependency can be expensive,
6503 avoid doing it if the type is already complete. */
6504 && !COMPLETE_TYPE_P (new_scope
)
6505 /* Do not try to complete dependent types. */
6506 && !dependent_type_p (new_scope
))
6508 new_scope
= complete_type (new_scope
);
6509 /* If it is a typedef to current class, use the current
6510 class instead, as the typedef won't have any names inside
6512 if (!COMPLETE_TYPE_P (new_scope
)
6513 && currently_open_class (new_scope
))
6514 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6516 /* Make sure we look in the right scope the next time through
6518 parser
->scope
= new_scope
;
6521 /* If parsing tentatively, replace the sequence of tokens that makes
6522 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6523 token. That way, should we re-parse the token stream, we will
6524 not have to repeat the effort required to do the parse, nor will
6525 we issue duplicate error messages. */
6526 if (success
&& start
)
6530 token
= cp_lexer_token_at (parser
->lexer
, start
);
6531 /* Reset the contents of the START token. */
6532 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6533 /* Retrieve any deferred checks. Do not pop this access checks yet
6534 so the memory will not be reclaimed during token replacing below. */
6535 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6536 token
->u
.tree_check_value
->value
= parser
->scope
;
6537 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6538 token
->u
.tree_check_value
->qualifying_scope
=
6539 parser
->qualifying_scope
;
6540 token
->keyword
= RID_MAX
;
6542 /* Purge all subsequent tokens. */
6543 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6547 pop_to_parent_deferring_access_checks ();
6549 return success
? parser
->scope
: NULL_TREE
;
6552 /* Parse a nested-name-specifier. See
6553 cp_parser_nested_name_specifier_opt for details. This function
6554 behaves identically, except that it will an issue an error if no
6555 nested-name-specifier is present. */
6558 cp_parser_nested_name_specifier (cp_parser
*parser
,
6559 bool typename_keyword_p
,
6560 bool check_dependency_p
,
6562 bool is_declaration
)
6566 /* Look for the nested-name-specifier. */
6567 scope
= cp_parser_nested_name_specifier_opt (parser
,
6572 /* If it was not present, issue an error message. */
6575 cp_parser_error (parser
, "expected nested-name-specifier");
6576 parser
->scope
= NULL_TREE
;
6582 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6583 this is either a class-name or a namespace-name (which corresponds
6584 to the class-or-namespace-name production in the grammar). For
6585 C++0x, it can also be a type-name that refers to an enumeration
6586 type or a simple-template-id.
6588 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6589 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6590 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6591 TYPE_P is TRUE iff the next name should be taken as a class-name,
6592 even the same name is declared to be another entity in the same
6595 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6596 specified by the class-or-namespace-name. If neither is found the
6597 ERROR_MARK_NODE is returned. */
6600 cp_parser_qualifying_entity (cp_parser
*parser
,
6601 bool typename_keyword_p
,
6602 bool template_keyword_p
,
6603 bool check_dependency_p
,
6605 bool is_declaration
)
6608 tree saved_qualifying_scope
;
6609 tree saved_object_scope
;
6612 bool successful_parse_p
;
6614 /* DR 743: decltype can appear in a nested-name-specifier. */
6615 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6617 scope
= cp_parser_decltype (parser
);
6618 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6619 && !MAYBE_CLASS_TYPE_P (scope
))
6621 cp_parser_simulate_error (parser
);
6622 return error_mark_node
;
6624 if (TYPE_NAME (scope
))
6625 scope
= TYPE_NAME (scope
);
6629 /* Before we try to parse the class-name, we must save away the
6630 current PARSER->SCOPE since cp_parser_class_name will destroy
6632 saved_scope
= parser
->scope
;
6633 saved_qualifying_scope
= parser
->qualifying_scope
;
6634 saved_object_scope
= parser
->object_scope
;
6635 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6636 there is no need to look for a namespace-name. */
6637 only_class_p
= template_keyword_p
6638 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6640 cp_parser_parse_tentatively (parser
);
6641 scope
= cp_parser_class_name (parser
,
6644 type_p
? class_type
: none_type
,
6646 /*class_head_p=*/false,
6648 /*enum_ok=*/cxx_dialect
> cxx98
);
6649 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6650 /* If that didn't work, try for a namespace-name. */
6651 if (!only_class_p
&& !successful_parse_p
)
6653 /* Restore the saved scope. */
6654 parser
->scope
= saved_scope
;
6655 parser
->qualifying_scope
= saved_qualifying_scope
;
6656 parser
->object_scope
= saved_object_scope
;
6657 /* If we are not looking at an identifier followed by the scope
6658 resolution operator, then this is not part of a
6659 nested-name-specifier. (Note that this function is only used
6660 to parse the components of a nested-name-specifier.) */
6661 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6662 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6663 return error_mark_node
;
6664 scope
= cp_parser_namespace_name (parser
);
6670 /* Return true if we are looking at a compound-literal, false otherwise. */
6673 cp_parser_compound_literal_p (cp_parser
*parser
)
6675 cp_lexer_save_tokens (parser
->lexer
);
6677 /* Skip tokens until the next token is a closing parenthesis.
6678 If we find the closing `)', and the next token is a `{', then
6679 we are looking at a compound-literal. */
6680 bool compound_literal_p
6681 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6682 /*consume_paren=*/true)
6683 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6685 /* Roll back the tokens we skipped. */
6686 cp_lexer_rollback_tokens (parser
->lexer
);
6688 return compound_literal_p
;
6691 /* Return true if EXPR is the integer constant zero or a complex constant
6692 of zero, without any folding, but ignoring location wrappers. */
6695 literal_integer_zerop (const_tree expr
)
6697 return (location_wrapper_p (expr
)
6698 && integer_zerop (TREE_OPERAND (expr
, 0)));
6701 /* Parse a postfix-expression.
6705 postfix-expression [ expression ]
6706 postfix-expression ( expression-list [opt] )
6707 simple-type-specifier ( expression-list [opt] )
6708 typename :: [opt] nested-name-specifier identifier
6709 ( expression-list [opt] )
6710 typename :: [opt] nested-name-specifier template [opt] template-id
6711 ( expression-list [opt] )
6712 postfix-expression . template [opt] id-expression
6713 postfix-expression -> template [opt] id-expression
6714 postfix-expression . pseudo-destructor-name
6715 postfix-expression -> pseudo-destructor-name
6716 postfix-expression ++
6717 postfix-expression --
6718 dynamic_cast < type-id > ( expression )
6719 static_cast < type-id > ( expression )
6720 reinterpret_cast < type-id > ( expression )
6721 const_cast < type-id > ( expression )
6722 typeid ( expression )
6728 ( type-id ) { initializer-list , [opt] }
6730 This extension is a GNU version of the C99 compound-literal
6731 construct. (The C99 grammar uses `type-name' instead of `type-id',
6732 but they are essentially the same concept.)
6734 If ADDRESS_P is true, the postfix expression is the operand of the
6735 `&' operator. CAST_P is true if this expression is the target of a
6738 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6739 class member access expressions [expr.ref].
6741 Returns a representation of the expression. */
6744 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6745 bool member_access_only_p
, bool decltype_p
,
6746 cp_id_kind
* pidk_return
)
6751 cp_id_kind idk
= CP_ID_KIND_NONE
;
6752 cp_expr postfix_expression
= NULL_TREE
;
6753 bool is_member_access
= false;
6755 /* Peek at the next token. */
6756 token
= cp_lexer_peek_token (parser
->lexer
);
6757 loc
= token
->location
;
6758 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6760 /* Some of the productions are determined by keywords. */
6761 keyword
= token
->keyword
;
6771 const char *saved_message
;
6772 bool saved_in_type_id_in_expr_p
;
6774 /* All of these can be handled in the same way from the point
6775 of view of parsing. Begin by consuming the token
6776 identifying the cast. */
6777 cp_lexer_consume_token (parser
->lexer
);
6779 /* New types cannot be defined in the cast. */
6780 saved_message
= parser
->type_definition_forbidden_message
;
6781 parser
->type_definition_forbidden_message
6782 = G_("types may not be defined in casts");
6784 /* Look for the opening `<'. */
6785 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6786 /* Parse the type to which we are casting. */
6787 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6788 parser
->in_type_id_in_expr_p
= true;
6789 type
= cp_parser_type_id (parser
);
6790 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6791 /* Look for the closing `>'. */
6792 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6793 /* Restore the old message. */
6794 parser
->type_definition_forbidden_message
= saved_message
;
6796 bool saved_greater_than_is_operator_p
6797 = parser
->greater_than_is_operator_p
;
6798 parser
->greater_than_is_operator_p
= true;
6800 /* And the expression which is being cast. */
6801 matching_parens parens
;
6802 parens
.require_open (parser
);
6803 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6804 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6806 location_t end_loc
= close_paren
?
6807 close_paren
->location
: UNKNOWN_LOCATION
;
6809 parser
->greater_than_is_operator_p
6810 = saved_greater_than_is_operator_p
;
6812 /* Only type conversions to integral or enumeration types
6813 can be used in constant-expressions. */
6814 if (!cast_valid_in_integral_constant_expression_p (type
)
6815 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6817 postfix_expression
= error_mark_node
;
6825 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6829 = build_static_cast (type
, expression
, tf_warning_or_error
);
6833 = build_reinterpret_cast (type
, expression
,
6834 tf_warning_or_error
);
6838 = build_const_cast (type
, expression
, tf_warning_or_error
);
6844 /* Construct a location e.g. :
6845 reinterpret_cast <int *> (expr)
6846 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6847 ranging from the start of the "*_cast" token to the final closing
6848 paren, with the caret at the start. */
6849 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6850 postfix_expression
.set_location (cp_cast_loc
);
6857 const char *saved_message
;
6858 bool saved_in_type_id_in_expr_p
;
6860 /* Consume the `typeid' token. */
6861 cp_lexer_consume_token (parser
->lexer
);
6862 /* Look for the `(' token. */
6863 matching_parens parens
;
6864 parens
.require_open (parser
);
6865 /* Types cannot be defined in a `typeid' expression. */
6866 saved_message
= parser
->type_definition_forbidden_message
;
6867 parser
->type_definition_forbidden_message
6868 = G_("types may not be defined in a %<typeid%> expression");
6869 /* We can't be sure yet whether we're looking at a type-id or an
6871 cp_parser_parse_tentatively (parser
);
6872 /* Try a type-id first. */
6873 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6874 parser
->in_type_id_in_expr_p
= true;
6875 type
= cp_parser_type_id (parser
);
6876 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6877 /* Look for the `)' token. Otherwise, we can't be sure that
6878 we're not looking at an expression: consider `typeid (int
6879 (3))', for example. */
6880 cp_token
*close_paren
= parens
.require_close (parser
);
6881 /* If all went well, simply lookup the type-id. */
6882 if (cp_parser_parse_definitely (parser
))
6883 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6884 /* Otherwise, fall back to the expression variant. */
6889 /* Look for an expression. */
6890 expression
= cp_parser_expression (parser
, & idk
);
6891 /* Compute its typeid. */
6892 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6893 /* Look for the `)' token. */
6894 close_paren
= parens
.require_close (parser
);
6896 /* Restore the saved message. */
6897 parser
->type_definition_forbidden_message
= saved_message
;
6898 /* `typeid' may not appear in an integral constant expression. */
6899 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6900 postfix_expression
= error_mark_node
;
6902 /* Construct a location e.g. :
6905 ranging from the start of the "typeid" token to the final closing
6906 paren, with the caret at the start. */
6909 location_t typeid_loc
6910 = make_location (start_loc
, start_loc
, close_paren
->location
);
6911 postfix_expression
.set_location (typeid_loc
);
6912 postfix_expression
.maybe_add_location_wrapper ();
6920 /* The syntax permitted here is the same permitted for an
6921 elaborated-type-specifier. */
6922 ++parser
->prevent_constrained_type_specifiers
;
6923 type
= cp_parser_elaborated_type_specifier (parser
,
6924 /*is_friend=*/false,
6925 /*is_declaration=*/false);
6926 --parser
->prevent_constrained_type_specifiers
;
6927 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6932 case RID_BUILTIN_SHUFFLE
:
6933 case RID_BUILTIN_LAUNDER
:
6935 vec
<tree
, va_gc
> *vec
;
6939 cp_lexer_consume_token (parser
->lexer
);
6940 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6941 /*cast_p=*/false, /*allow_expansion_p=*/true,
6942 /*non_constant_p=*/NULL
);
6945 postfix_expression
= error_mark_node
;
6949 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6955 if (vec
->length () == 1)
6957 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6960 error_at (loc
, "wrong number of arguments to "
6961 "%<__builtin_addressof%>");
6962 postfix_expression
= error_mark_node
;
6966 case RID_BUILTIN_LAUNDER
:
6967 if (vec
->length () == 1)
6968 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6969 tf_warning_or_error
);
6972 error_at (loc
, "wrong number of arguments to "
6973 "%<__builtin_launder%>");
6974 postfix_expression
= error_mark_node
;
6978 case RID_BUILTIN_SHUFFLE
:
6979 if (vec
->length () == 2)
6981 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6982 (*vec
)[1], tf_warning_or_error
);
6983 else if (vec
->length () == 3)
6985 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6986 (*vec
)[2], tf_warning_or_error
);
6989 error_at (loc
, "wrong number of arguments to "
6990 "%<__builtin_shuffle%>");
6991 postfix_expression
= error_mark_node
;
7005 /* If the next thing is a simple-type-specifier, we may be
7006 looking at a functional cast. We could also be looking at
7007 an id-expression. So, we try the functional cast, and if
7008 that doesn't work we fall back to the primary-expression. */
7009 cp_parser_parse_tentatively (parser
);
7010 /* Look for the simple-type-specifier. */
7011 ++parser
->prevent_constrained_type_specifiers
;
7012 type
= cp_parser_simple_type_specifier (parser
,
7013 /*decl_specs=*/NULL
,
7014 CP_PARSER_FLAGS_NONE
);
7015 --parser
->prevent_constrained_type_specifiers
;
7016 /* Parse the cast itself. */
7017 if (!cp_parser_error_occurred (parser
))
7019 = cp_parser_functional_cast (parser
, type
);
7020 /* If that worked, we're done. */
7021 if (cp_parser_parse_definitely (parser
))
7024 /* If the functional-cast didn't work out, try a
7025 compound-literal. */
7026 if (cp_parser_allow_gnu_extensions_p (parser
)
7027 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7029 cp_expr initializer
= NULL_TREE
;
7031 cp_parser_parse_tentatively (parser
);
7033 matching_parens parens
;
7034 parens
.consume_open (parser
);
7036 /* Avoid calling cp_parser_type_id pointlessly, see comment
7037 in cp_parser_cast_expression about c++/29234. */
7038 if (!cp_parser_compound_literal_p (parser
))
7039 cp_parser_simulate_error (parser
);
7042 /* Parse the type. */
7043 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7044 parser
->in_type_id_in_expr_p
= true;
7045 type
= cp_parser_type_id (parser
);
7046 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7047 parens
.require_close (parser
);
7050 /* If things aren't going well, there's no need to
7052 if (!cp_parser_error_occurred (parser
))
7054 bool non_constant_p
;
7055 /* Parse the brace-enclosed initializer list. */
7056 initializer
= cp_parser_braced_list (parser
,
7059 /* If that worked, we're definitely looking at a
7060 compound-literal expression. */
7061 if (cp_parser_parse_definitely (parser
))
7063 /* Warn the user that a compound literal is not
7064 allowed in standard C++. */
7065 pedwarn (input_location
, OPT_Wpedantic
,
7066 "ISO C++ forbids compound-literals");
7067 /* For simplicity, we disallow compound literals in
7068 constant-expressions. We could
7069 allow compound literals of integer type, whose
7070 initializer was a constant, in constant
7071 expressions. Permitting that usage, as a further
7072 extension, would not change the meaning of any
7073 currently accepted programs. (Of course, as
7074 compound literals are not part of ISO C++, the
7075 standard has nothing to say.) */
7076 if (cp_parser_non_integral_constant_expression (parser
,
7079 postfix_expression
= error_mark_node
;
7082 /* Form the representation of the compound-literal. */
7084 = finish_compound_literal (type
, initializer
,
7085 tf_warning_or_error
, fcl_c99
);
7086 postfix_expression
.set_location (initializer
.get_location ());
7091 /* It must be a primary-expression. */
7093 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7094 /*template_arg_p=*/false,
7101 /* Note that we don't need to worry about calling build_cplus_new on a
7102 class-valued CALL_EXPR in decltype when it isn't the end of the
7103 postfix-expression; unary_complex_lvalue will take care of that for
7106 /* Keep looping until the postfix-expression is complete. */
7109 if (idk
== CP_ID_KIND_UNQUALIFIED
7110 && identifier_p (postfix_expression
)
7111 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7112 /* It is not a Koenig lookup function call. */
7114 = unqualified_name_lookup_error (postfix_expression
);
7116 /* Peek at the next token. */
7117 token
= cp_lexer_peek_token (parser
->lexer
);
7119 switch (token
->type
)
7121 case CPP_OPEN_SQUARE
:
7122 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7124 cp_parser_error (parser
,
7125 "two consecutive %<[%> shall "
7126 "only introduce an attribute");
7127 return error_mark_node
;
7130 = cp_parser_postfix_open_square_expression (parser
,
7134 postfix_expression
.set_range (start_loc
,
7135 postfix_expression
.get_location ());
7137 idk
= CP_ID_KIND_NONE
;
7138 is_member_access
= false;
7141 case CPP_OPEN_PAREN
:
7142 /* postfix-expression ( expression-list [opt] ) */
7145 bool is_builtin_constant_p
;
7146 bool saved_integral_constant_expression_p
= false;
7147 bool saved_non_integral_constant_expression_p
= false;
7148 tsubst_flags_t complain
= complain_flags (decltype_p
);
7149 vec
<tree
, va_gc
> *args
;
7150 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7152 is_member_access
= false;
7154 is_builtin_constant_p
7155 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7156 if (is_builtin_constant_p
)
7158 /* The whole point of __builtin_constant_p is to allow
7159 non-constant expressions to appear as arguments. */
7160 saved_integral_constant_expression_p
7161 = parser
->integral_constant_expression_p
;
7162 saved_non_integral_constant_expression_p
7163 = parser
->non_integral_constant_expression_p
;
7164 parser
->integral_constant_expression_p
= false;
7166 args
= (cp_parser_parenthesized_expression_list
7168 /*cast_p=*/false, /*allow_expansion_p=*/true,
7169 /*non_constant_p=*/NULL
,
7170 /*close_paren_loc=*/&close_paren_loc
,
7171 /*wrap_locations_p=*/true));
7172 if (is_builtin_constant_p
)
7174 parser
->integral_constant_expression_p
7175 = saved_integral_constant_expression_p
;
7176 parser
->non_integral_constant_expression_p
7177 = saved_non_integral_constant_expression_p
;
7182 postfix_expression
= error_mark_node
;
7186 /* Function calls are not permitted in
7187 constant-expressions. */
7188 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7189 && cp_parser_non_integral_constant_expression (parser
,
7192 postfix_expression
= error_mark_node
;
7193 release_tree_vector (args
);
7198 if (idk
== CP_ID_KIND_UNQUALIFIED
7199 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7201 if (identifier_p (postfix_expression
)
7202 /* In C++2A, we may need to perform ADL for a template
7204 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7205 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7207 if (!args
->is_empty ())
7210 if (!any_type_dependent_arguments_p (args
))
7212 = perform_koenig_lookup (postfix_expression
, args
,
7217 = unqualified_fn_lookup_error (postfix_expression
);
7219 /* We do not perform argument-dependent lookup if
7220 normal lookup finds a non-function, in accordance
7221 with the expected resolution of DR 218. */
7222 else if (!args
->is_empty ()
7223 && is_overloaded_fn (postfix_expression
))
7225 tree fn
= get_first_fn (postfix_expression
);
7226 fn
= STRIP_TEMPLATE (fn
);
7228 /* Do not do argument dependent lookup if regular
7229 lookup finds a member function or a block-scope
7230 function declaration. [basic.lookup.argdep]/3 */
7231 if (!DECL_FUNCTION_MEMBER_P (fn
)
7232 && !DECL_LOCAL_FUNCTION_P (fn
))
7235 if (!any_type_dependent_arguments_p (args
))
7237 = perform_koenig_lookup (postfix_expression
, args
,
7243 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7245 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7246 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7248 if (processing_template_decl
7249 && (type_dependent_object_expression_p (instance
)
7250 || (!BASELINK_P (fn
)
7251 && TREE_CODE (fn
) != FIELD_DECL
)
7252 || type_dependent_expression_p (fn
)
7253 || any_type_dependent_arguments_p (args
)))
7255 maybe_generic_this_capture (instance
, fn
);
7257 = build_min_nt_call_vec (postfix_expression
, args
);
7258 release_tree_vector (args
);
7262 if (BASELINK_P (fn
))
7265 = (build_new_method_call
7266 (instance
, fn
, &args
, NULL_TREE
,
7267 (idk
== CP_ID_KIND_QUALIFIED
7268 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7275 = finish_call_expr (postfix_expression
, &args
,
7276 /*disallow_virtual=*/false,
7280 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7281 || TREE_CODE (postfix_expression
) == MEMBER_REF
7282 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7283 postfix_expression
= (build_offset_ref_call_from_tree
7284 (postfix_expression
, &args
,
7286 else if (idk
== CP_ID_KIND_QUALIFIED
)
7287 /* A call to a static class member, or a namespace-scope
7290 = finish_call_expr (postfix_expression
, &args
,
7291 /*disallow_virtual=*/true,
7295 /* All other function calls. */
7297 = finish_call_expr (postfix_expression
, &args
,
7298 /*disallow_virtual=*/false,
7302 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7304 location_t combined_loc
= make_location (token
->location
,
7307 postfix_expression
.set_location (combined_loc
);
7310 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7311 idk
= CP_ID_KIND_NONE
;
7313 release_tree_vector (args
);
7319 /* postfix-expression . template [opt] id-expression
7320 postfix-expression . pseudo-destructor-name
7321 postfix-expression -> template [opt] id-expression
7322 postfix-expression -> pseudo-destructor-name */
7324 /* Consume the `.' or `->' operator. */
7325 cp_lexer_consume_token (parser
->lexer
);
7328 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7332 is_member_access
= true;
7336 /* postfix-expression ++ */
7337 /* Consume the `++' token. */
7338 cp_lexer_consume_token (parser
->lexer
);
7339 /* Generate a representation for the complete expression. */
7341 = finish_increment_expr (postfix_expression
,
7342 POSTINCREMENT_EXPR
);
7343 /* Increments may not appear in constant-expressions. */
7344 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7345 postfix_expression
= error_mark_node
;
7346 idk
= CP_ID_KIND_NONE
;
7347 is_member_access
= false;
7350 case CPP_MINUS_MINUS
:
7351 /* postfix-expression -- */
7352 /* Consume the `--' token. */
7353 cp_lexer_consume_token (parser
->lexer
);
7354 /* Generate a representation for the complete expression. */
7356 = finish_increment_expr (postfix_expression
,
7357 POSTDECREMENT_EXPR
);
7358 /* Decrements may not appear in constant-expressions. */
7359 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7360 postfix_expression
= error_mark_node
;
7361 idk
= CP_ID_KIND_NONE
;
7362 is_member_access
= false;
7366 if (pidk_return
!= NULL
)
7367 * pidk_return
= idk
;
7368 if (member_access_only_p
)
7369 return is_member_access
7370 ? postfix_expression
7371 : cp_expr (error_mark_node
);
7373 return postfix_expression
;
7377 /* We should never get here. */
7379 return error_mark_node
;
7382 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7383 by cp_parser_builtin_offsetof. We're looking for
7385 postfix-expression [ expression ]
7386 postfix-expression [ braced-init-list ] (C++11)
7388 FOR_OFFSETOF is set if we're being called in that context, which
7389 changes how we deal with integer constant expressions. */
7392 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7393 tree postfix_expression
,
7397 tree index
= NULL_TREE
;
7398 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7399 bool saved_greater_than_is_operator_p
;
7401 /* Consume the `[' token. */
7402 cp_lexer_consume_token (parser
->lexer
);
7404 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7405 parser
->greater_than_is_operator_p
= true;
7407 /* Parse the index expression. */
7408 /* ??? For offsetof, there is a question of what to allow here. If
7409 offsetof is not being used in an integral constant expression context,
7410 then we *could* get the right answer by computing the value at runtime.
7411 If we are in an integral constant expression context, then we might
7412 could accept any constant expression; hard to say without analysis.
7413 Rather than open the barn door too wide right away, allow only integer
7414 constant expressions here. */
7416 index
= cp_parser_constant_expression (parser
);
7419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7421 bool expr_nonconst_p
;
7422 cp_lexer_set_source_position (parser
->lexer
);
7423 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7424 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7427 index
= cp_parser_expression (parser
);
7430 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7432 /* Look for the closing `]'. */
7433 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7435 /* Build the ARRAY_REF. */
7436 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7439 /* When not doing offsetof, array references are not permitted in
7440 constant-expressions. */
7442 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7443 postfix_expression
= error_mark_node
;
7445 return postfix_expression
;
7448 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7449 dereference of incomplete type, returns true if error_mark_node should
7450 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7451 and *DEPENDENT_P. */
7454 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind
= (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7462 switch (TREE_CODE (*postfix_expression
))
7465 case REINTERPRET_CAST_EXPR
:
7466 case CONST_CAST_EXPR
:
7467 case STATIC_CAST_EXPR
:
7468 case DYNAMIC_CAST_EXPR
:
7469 case IMPLICIT_CONV_EXPR
:
7470 case VIEW_CONVERT_EXPR
:
7471 case NON_LVALUE_EXPR
:
7475 /* Don't emit any diagnostic for OVERLOADs. */
7479 /* Avoid clobbering e.g. DECLs. */
7480 if (!EXPR_P (*postfix_expression
))
7485 if (kind
== DK_IGNORED
)
7488 location_t exploc
= location_of (*postfix_expression
);
7489 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7490 if (!MAYBE_CLASS_TYPE_P (*scope
))
7492 if (kind
== DK_ERROR
)
7493 *scope
= *postfix_expression
= error_mark_node
;
7494 else if (processing_template_decl
)
7496 *dependent_p
= true;
7497 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7502 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7503 by cp_parser_builtin_offsetof. We're looking for
7505 postfix-expression . template [opt] id-expression
7506 postfix-expression . pseudo-destructor-name
7507 postfix-expression -> template [opt] id-expression
7508 postfix-expression -> pseudo-destructor-name
7510 FOR_OFFSETOF is set if we're being called in that context. That sorta
7511 limits what of the above we'll actually accept, but nevermind.
7512 TOKEN_TYPE is the "." or "->" token, which will already have been
7513 removed from the stream. */
7516 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7517 enum cpp_ttype token_type
,
7518 cp_expr postfix_expression
,
7519 bool for_offsetof
, cp_id_kind
*idk
,
7520 location_t location
)
7524 bool pseudo_destructor_p
;
7525 tree scope
= NULL_TREE
;
7526 location_t start_loc
= postfix_expression
.get_start ();
7528 /* If this is a `->' operator, dereference the pointer. */
7529 if (token_type
== CPP_DEREF
)
7530 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7531 tf_warning_or_error
);
7532 /* Check to see whether or not the expression is type-dependent and
7533 not the current instantiation. */
7534 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7535 /* The identifier following the `->' or `.' is not qualified. */
7536 parser
->scope
= NULL_TREE
;
7537 parser
->qualifying_scope
= NULL_TREE
;
7538 parser
->object_scope
= NULL_TREE
;
7539 *idk
= CP_ID_KIND_NONE
;
7541 /* Enter the scope corresponding to the type of the object
7542 given by the POSTFIX_EXPRESSION. */
7545 scope
= TREE_TYPE (postfix_expression
);
7546 /* According to the standard, no expression should ever have
7547 reference type. Unfortunately, we do not currently match
7548 the standard in this respect in that our internal representation
7549 of an expression may have reference type even when the standard
7550 says it does not. Therefore, we have to manually obtain the
7551 underlying type here. */
7552 scope
= non_reference (scope
);
7553 /* The type of the POSTFIX_EXPRESSION must be complete. */
7554 /* Unlike the object expression in other contexts, *this is not
7555 required to be of complete type for purposes of class member
7556 access (5.2.5) outside the member function body. */
7557 if (postfix_expression
!= current_class_ref
7558 && scope
!= error_mark_node
7559 && !currently_open_class (scope
))
7561 scope
= complete_type (scope
);
7562 if (!COMPLETE_TYPE_P (scope
)
7563 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7565 return error_mark_node
;
7570 /* Let the name lookup machinery know that we are processing a
7571 class member access expression. */
7572 parser
->context
->object_type
= scope
;
7573 /* If something went wrong, we want to be able to discern that case,
7574 as opposed to the case where there was no SCOPE due to the type
7575 of expression being dependent. */
7577 scope
= error_mark_node
;
7578 /* If the SCOPE was erroneous, make the various semantic analysis
7579 functions exit quickly -- and without issuing additional error
7581 if (scope
== error_mark_node
)
7582 postfix_expression
= error_mark_node
;
7587 /* Tell cp_parser_lookup_name that there was an object, even though it's
7589 parser
->context
->object_type
= unknown_type_node
;
7591 /* Assume this expression is not a pseudo-destructor access. */
7592 pseudo_destructor_p
= false;
7594 /* If the SCOPE is a scalar type, then, if this is a valid program,
7595 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7596 is type dependent, it can be pseudo-destructor-name or something else.
7597 Try to parse it as pseudo-destructor-name first. */
7598 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7603 cp_parser_parse_tentatively (parser
);
7604 /* Parse the pseudo-destructor-name. */
7606 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7609 && (cp_parser_error_occurred (parser
)
7610 || !SCALAR_TYPE_P (type
)))
7611 cp_parser_abort_tentative_parse (parser
);
7612 else if (cp_parser_parse_definitely (parser
))
7614 pseudo_destructor_p
= true;
7616 = finish_pseudo_destructor_expr (postfix_expression
,
7621 if (!pseudo_destructor_p
)
7623 /* If the SCOPE is not a scalar type, we are looking at an
7624 ordinary class member access expression, rather than a
7625 pseudo-destructor-name. */
7627 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7628 /* Parse the id-expression. */
7629 name
= (cp_parser_id_expression
7631 cp_parser_optional_template_keyword (parser
),
7632 /*check_dependency_p=*/true,
7634 /*declarator_p=*/false,
7635 /*optional_p=*/false));
7636 /* In general, build a SCOPE_REF if the member name is qualified.
7637 However, if the name was not dependent and has already been
7638 resolved; there is no need to build the SCOPE_REF. For example;
7640 struct X { void f(); };
7641 template <typename T> void f(T* t) { t->X::f(); }
7643 Even though "t" is dependent, "X::f" is not and has been resolved
7644 to a BASELINK; there is no need to include scope information. */
7646 /* But we do need to remember that there was an explicit scope for
7647 virtual function calls. */
7649 *idk
= CP_ID_KIND_QUALIFIED
;
7651 /* If the name is a template-id that names a type, we will get a
7652 TYPE_DECL here. That is invalid code. */
7653 if (TREE_CODE (name
) == TYPE_DECL
)
7655 error_at (token
->location
, "invalid use of %qD", name
);
7656 postfix_expression
= error_mark_node
;
7660 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7662 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7664 error_at (token
->location
, "%<%D::%D%> is not a class member",
7665 parser
->scope
, name
);
7666 postfix_expression
= error_mark_node
;
7669 name
= build_qualified_name (/*type=*/NULL_TREE
,
7673 parser
->scope
= NULL_TREE
;
7674 parser
->qualifying_scope
= NULL_TREE
;
7675 parser
->object_scope
= NULL_TREE
;
7677 if (parser
->scope
&& name
&& BASELINK_P (name
))
7678 adjust_result_of_qualified_name_lookup
7679 (name
, parser
->scope
, scope
);
7681 = finish_class_member_access_expr (postfix_expression
, name
,
7683 tf_warning_or_error
);
7684 /* Build a location e.g.:
7687 where the caret is at the deref token, ranging from
7688 the start of postfix_expression to the end of the access expr. */
7690 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7691 location_t combined_loc
7692 = make_location (input_location
, start_loc
, end_loc
);
7693 protected_set_expr_location (postfix_expression
, combined_loc
);
7697 /* We no longer need to look up names in the scope of the object on
7698 the left-hand side of the `.' or `->' operator. */
7699 parser
->context
->object_type
= NULL_TREE
;
7701 /* Outside of offsetof, these operators may not appear in
7702 constant-expressions. */
7704 && (cp_parser_non_integral_constant_expression
7705 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7706 postfix_expression
= error_mark_node
;
7708 return postfix_expression
;
7711 /* Parse a parenthesized expression-list.
7714 assignment-expression
7715 expression-list, assignment-expression
7720 identifier, expression-list
7722 CAST_P is true if this expression is the target of a cast.
7724 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7727 WRAP_LOCATIONS_P is true if expressions within this list for which
7728 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7729 their source locations.
7731 Returns a vector of trees. Each element is a representation of an
7732 assignment-expression. NULL is returned if the ( and or ) are
7733 missing. An empty, but allocated, vector is returned on no
7734 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7735 if we are parsing an attribute list for an attribute that wants a
7736 plain identifier argument, normal_attr for an attribute that wants
7737 an expression, or non_attr if we aren't parsing an attribute list. If
7738 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7739 not all of the expressions in the list were constant.
7740 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7741 will be written to with the location of the closing parenthesis. If
7742 an error occurs, it may or may not be written to. */
7744 static vec
<tree
, va_gc
> *
7745 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7746 int is_attribute_list
,
7748 bool allow_expansion_p
,
7749 bool *non_constant_p
,
7750 location_t
*close_paren_loc
,
7751 bool wrap_locations_p
)
7753 vec
<tree
, va_gc
> *expression_list
;
7754 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7755 tree identifier
= NULL_TREE
;
7756 bool saved_greater_than_is_operator_p
;
7758 /* Assume all the expressions will be constant. */
7760 *non_constant_p
= false;
7762 matching_parens parens
;
7763 if (!parens
.require_open (parser
))
7766 expression_list
= make_tree_vector ();
7768 /* Within a parenthesized expression, a `>' token is always
7769 the greater-than operator. */
7770 saved_greater_than_is_operator_p
7771 = parser
->greater_than_is_operator_p
;
7772 parser
->greater_than_is_operator_p
= true;
7774 cp_expr
expr (NULL_TREE
);
7776 /* Consume expressions until there are no more. */
7777 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7780 /* At the beginning of attribute lists, check to see if the
7781 next token is an identifier. */
7782 if (is_attribute_list
== id_attr
7783 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7787 /* Consume the identifier. */
7788 token
= cp_lexer_consume_token (parser
->lexer
);
7789 /* Save the identifier. */
7790 identifier
= token
->u
.value
;
7794 bool expr_non_constant_p
;
7796 /* Parse the next assignment-expression. */
7797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7799 /* A braced-init-list. */
7800 cp_lexer_set_source_position (parser
->lexer
);
7801 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7802 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7803 if (non_constant_p
&& expr_non_constant_p
)
7804 *non_constant_p
= true;
7806 else if (non_constant_p
)
7808 expr
= (cp_parser_constant_expression
7809 (parser
, /*allow_non_constant_p=*/true,
7810 &expr_non_constant_p
));
7811 if (expr_non_constant_p
)
7812 *non_constant_p
= true;
7815 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7819 expr
= instantiate_non_dependent_expr (expr
);
7821 /* If we have an ellipsis, then this is an expression
7823 if (allow_expansion_p
7824 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7826 /* Consume the `...'. */
7827 cp_lexer_consume_token (parser
->lexer
);
7829 /* Build the argument pack. */
7830 expr
= make_pack_expansion (expr
);
7833 if (wrap_locations_p
)
7834 expr
.maybe_add_location_wrapper ();
7836 /* Add it to the list. We add error_mark_node
7837 expressions to the list, so that we can still tell if
7838 the correct form for a parenthesized expression-list
7839 is found. That gives better errors. */
7840 vec_safe_push (expression_list
, expr
.get_value ());
7842 if (expr
== error_mark_node
)
7846 /* After the first item, attribute lists look the same as
7847 expression lists. */
7848 is_attribute_list
= non_attr
;
7851 /* If the next token isn't a `,', then we are done. */
7852 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7855 /* Otherwise, consume the `,' and keep going. */
7856 cp_lexer_consume_token (parser
->lexer
);
7859 if (close_paren_loc
)
7860 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7862 if (!parens
.require_close (parser
))
7867 /* We try and resync to an unnested comma, as that will give the
7868 user better diagnostics. */
7869 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7870 /*recovering=*/true,
7872 /*consume_paren=*/true);
7877 parser
->greater_than_is_operator_p
7878 = saved_greater_than_is_operator_p
;
7883 parser
->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p
;
7887 vec_safe_insert (expression_list
, 0, identifier
);
7889 return expression_list
;
7892 /* Parse a pseudo-destructor-name.
7894 pseudo-destructor-name:
7895 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7896 :: [opt] nested-name-specifier template template-id :: ~ type-name
7897 :: [opt] nested-name-specifier [opt] ~ type-name
7899 If either of the first two productions is used, sets *SCOPE to the
7900 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7901 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7902 or ERROR_MARK_NODE if the parse fails. */
7905 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7910 bool nested_name_specifier_p
;
7913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7914 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7915 && !type_dependent_expression_p (object
))
7917 if (cxx_dialect
< cxx14
)
7918 pedwarn (input_location
, 0,
7919 "%<~auto%> only available with "
7920 "-std=c++14 or -std=gnu++14");
7921 cp_lexer_consume_token (parser
->lexer
);
7922 cp_lexer_consume_token (parser
->lexer
);
7924 *type
= TREE_TYPE (object
);
7928 /* Assume that things will not work out. */
7929 *type
= error_mark_node
;
7931 /* Look for the optional `::' operator. */
7932 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7933 /* Look for the optional nested-name-specifier. */
7934 nested_name_specifier_p
7935 = (cp_parser_nested_name_specifier_opt (parser
,
7936 /*typename_keyword_p=*/false,
7937 /*check_dependency_p=*/true,
7939 /*is_declaration=*/false)
7941 /* Now, if we saw a nested-name-specifier, we might be doing the
7942 second production. */
7943 if (nested_name_specifier_p
7944 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7946 /* Consume the `template' keyword. */
7947 cp_lexer_consume_token (parser
->lexer
);
7948 /* Parse the template-id. */
7949 cp_parser_template_id (parser
,
7950 /*template_keyword_p=*/true,
7951 /*check_dependency_p=*/false,
7953 /*is_declaration=*/true);
7954 /* Look for the `::' token. */
7955 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7957 /* If the next token is not a `~', then there might be some
7958 additional qualification. */
7959 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7961 /* At this point, we're looking for "type-name :: ~". The type-name
7962 must not be a class-name, since this is a pseudo-destructor. So,
7963 it must be either an enum-name, or a typedef-name -- both of which
7964 are just identifiers. So, we peek ahead to check that the "::"
7965 and "~" tokens are present; if they are not, then we can avoid
7966 calling type_name. */
7967 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7968 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7969 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7971 cp_parser_error (parser
, "non-scalar type");
7975 /* Look for the type-name. */
7976 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7977 if (*scope
== error_mark_node
)
7980 /* Look for the `::' token. */
7981 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7986 /* Look for the `~'. */
7987 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7989 /* Once we see the ~, this has to be a pseudo-destructor. */
7990 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7991 cp_parser_commit_to_topmost_tentative_parse (parser
);
7993 /* Look for the type-name again. We are not responsible for
7994 checking that it matches the first type-name. */
7995 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7998 /* Parse a unary-expression.
8004 unary-operator cast-expression
8005 sizeof unary-expression
8007 alignof ( type-id ) [C++0x]
8014 __extension__ cast-expression
8015 __alignof__ unary-expression
8016 __alignof__ ( type-id )
8017 alignof unary-expression [C++0x]
8018 __real__ cast-expression
8019 __imag__ cast-expression
8021 sizeof ( type-id ) { initializer-list , [opt] }
8022 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8023 __alignof__ ( type-id ) { initializer-list , [opt] }
8025 ADDRESS_P is true iff the unary-expression is appearing as the
8026 operand of the `&' operator. CAST_P is true if this expression is
8027 the target of a cast.
8029 Returns a representation of the expression. */
8032 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8033 bool address_p
, bool cast_p
, bool decltype_p
)
8036 enum tree_code unary_operator
;
8038 /* Peek at the next token. */
8039 token
= cp_lexer_peek_token (parser
->lexer
);
8040 /* Some keywords give away the kind of expression. */
8041 if (token
->type
== CPP_KEYWORD
)
8043 enum rid keyword
= token
->keyword
;
8052 location_t start_loc
= token
->location
;
8054 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8055 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8057 /* Consume the token. */
8058 cp_lexer_consume_token (parser
->lexer
);
8059 /* Parse the operand. */
8060 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8062 if (TYPE_P (operand
))
8063 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8067 /* ISO C++ defines alignof only with types, not with
8068 expressions. So pedwarn if alignof is used with a non-
8069 type expression. However, __alignof__ is ok. */
8071 pedwarn (token
->location
, OPT_Wpedantic
,
8072 "ISO C++ does not allow %<alignof%> "
8075 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8077 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8078 SIZEOF_EXPR with the original operand. */
8079 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8081 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8083 if (!processing_template_decl
&& TYPE_P (operand
))
8085 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8086 build1 (NOP_EXPR
, operand
,
8088 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8091 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8092 TREE_SIDE_EFFECTS (ret
) = 0;
8093 TREE_READONLY (ret
) = 1;
8097 /* Construct a location e.g. :
8100 with start == caret at the start of the "alignof"/"sizeof"
8101 token, with the endpoint at the final closing paren. */
8102 location_t finish_loc
8103 = cp_lexer_previous_token (parser
->lexer
)->location
;
8104 location_t compound_loc
8105 = make_location (start_loc
, start_loc
, finish_loc
);
8107 cp_expr
ret_expr (ret
);
8108 ret_expr
.set_location (compound_loc
);
8109 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8114 return cp_parser_new_expression (parser
);
8117 return cp_parser_delete_expression (parser
);
8121 /* The saved value of the PEDANTIC flag. */
8125 /* Save away the PEDANTIC flag. */
8126 cp_parser_extension_opt (parser
, &saved_pedantic
);
8127 /* Parse the cast-expression. */
8128 expr
= cp_parser_simple_cast_expression (parser
);
8129 /* Restore the PEDANTIC flag. */
8130 pedantic
= saved_pedantic
;
8140 /* Consume the `__real__' or `__imag__' token. */
8141 cp_lexer_consume_token (parser
->lexer
);
8142 /* Parse the cast-expression. */
8143 expression
= cp_parser_simple_cast_expression (parser
);
8144 /* Create the complete representation. */
8145 return build_x_unary_op (token
->location
,
8146 (keyword
== RID_REALPART
8147 ? REALPART_EXPR
: IMAGPART_EXPR
),
8149 tf_warning_or_error
);
8153 case RID_TRANSACTION_ATOMIC
:
8154 case RID_TRANSACTION_RELAXED
:
8155 return cp_parser_transaction_expression (parser
, keyword
);
8160 const char *saved_message
;
8161 bool saved_integral_constant_expression_p
;
8162 bool saved_non_integral_constant_expression_p
;
8163 bool saved_greater_than_is_operator_p
;
8165 location_t start_loc
= token
->location
;
8167 cp_lexer_consume_token (parser
->lexer
);
8168 matching_parens parens
;
8169 parens
.require_open (parser
);
8171 saved_message
= parser
->type_definition_forbidden_message
;
8172 parser
->type_definition_forbidden_message
8173 = G_("types may not be defined in %<noexcept%> expressions");
8175 saved_integral_constant_expression_p
8176 = parser
->integral_constant_expression_p
;
8177 saved_non_integral_constant_expression_p
8178 = parser
->non_integral_constant_expression_p
;
8179 parser
->integral_constant_expression_p
= false;
8181 saved_greater_than_is_operator_p
8182 = parser
->greater_than_is_operator_p
;
8183 parser
->greater_than_is_operator_p
= true;
8185 ++cp_unevaluated_operand
;
8186 ++c_inhibit_evaluation_warnings
;
8187 ++cp_noexcept_operand
;
8188 expr
= cp_parser_expression (parser
);
8189 --cp_noexcept_operand
;
8190 --c_inhibit_evaluation_warnings
;
8191 --cp_unevaluated_operand
;
8193 parser
->greater_than_is_operator_p
8194 = saved_greater_than_is_operator_p
;
8196 parser
->integral_constant_expression_p
8197 = saved_integral_constant_expression_p
;
8198 parser
->non_integral_constant_expression_p
8199 = saved_non_integral_constant_expression_p
;
8201 parser
->type_definition_forbidden_message
= saved_message
;
8203 location_t finish_loc
8204 = cp_lexer_peek_token (parser
->lexer
)->location
;
8205 parens
.require_close (parser
);
8207 /* Construct a location of the form:
8210 with start == caret, finishing at the close-paren. */
8211 location_t noexcept_loc
8212 = make_location (start_loc
, start_loc
, finish_loc
);
8214 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8223 /* Look for the `:: new' and `:: delete', which also signal the
8224 beginning of a new-expression, or delete-expression,
8225 respectively. If the next token is `::', then it might be one of
8227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8231 /* See if the token after the `::' is one of the keywords in
8232 which we're interested. */
8233 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8234 /* If it's `new', we have a new-expression. */
8235 if (keyword
== RID_NEW
)
8236 return cp_parser_new_expression (parser
);
8237 /* Similarly, for `delete'. */
8238 else if (keyword
== RID_DELETE
)
8239 return cp_parser_delete_expression (parser
);
8242 /* Look for a unary operator. */
8243 unary_operator
= cp_parser_unary_operator (token
);
8244 /* The `++' and `--' operators can be handled similarly, even though
8245 they are not technically unary-operators in the grammar. */
8246 if (unary_operator
== ERROR_MARK
)
8248 if (token
->type
== CPP_PLUS_PLUS
)
8249 unary_operator
= PREINCREMENT_EXPR
;
8250 else if (token
->type
== CPP_MINUS_MINUS
)
8251 unary_operator
= PREDECREMENT_EXPR
;
8252 /* Handle the GNU address-of-label extension. */
8253 else if (cp_parser_allow_gnu_extensions_p (parser
)
8254 && token
->type
== CPP_AND_AND
)
8258 location_t start_loc
= token
->location
;
8260 /* Consume the '&&' token. */
8261 cp_lexer_consume_token (parser
->lexer
);
8262 /* Look for the identifier. */
8263 location_t finish_loc
8264 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8265 identifier
= cp_parser_identifier (parser
);
8266 /* Construct a location of the form:
8269 with caret==start at the "&&", finish at the end of the label. */
8270 location_t combined_loc
8271 = make_location (start_loc
, start_loc
, finish_loc
);
8272 /* Create an expression representing the address. */
8273 expression
= finish_label_address_expr (identifier
, combined_loc
);
8274 if (cp_parser_non_integral_constant_expression (parser
,
8276 expression
= error_mark_node
;
8280 if (unary_operator
!= ERROR_MARK
)
8282 cp_expr cast_expression
;
8283 cp_expr expression
= error_mark_node
;
8284 non_integral_constant non_constant_p
= NIC_NONE
;
8285 location_t loc
= token
->location
;
8286 tsubst_flags_t complain
= complain_flags (decltype_p
);
8288 /* Consume the operator token. */
8289 token
= cp_lexer_consume_token (parser
->lexer
);
8290 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8292 /* Parse the cast-expression. */
8294 = cp_parser_cast_expression (parser
,
8295 unary_operator
== ADDR_EXPR
,
8301 OP_TOKEN CAST_EXPRESSION
8302 ^~~~~~~~~~~~~~~~~~~~~~~~~
8303 with start==caret at the operator token, and
8304 extending to the end of the cast_expression. */
8305 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8307 /* Now, build an appropriate representation. */
8308 switch (unary_operator
)
8311 non_constant_p
= NIC_STAR
;
8312 expression
= build_x_indirect_ref (loc
, cast_expression
,
8315 /* TODO: build_x_indirect_ref does not always honor the
8316 location, so ensure it is set. */
8317 expression
.set_location (loc
);
8321 non_constant_p
= NIC_ADDR
;
8324 expression
= build_x_unary_op (loc
, unary_operator
,
8327 /* TODO: build_x_unary_op does not always honor the location,
8328 so ensure it is set. */
8329 expression
.set_location (loc
);
8332 case PREINCREMENT_EXPR
:
8333 case PREDECREMENT_EXPR
:
8334 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8335 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8338 /* Immediately fold negation of a constant, unless the constant is 0
8339 (since -0 == 0) or it would overflow. */
8340 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8341 && CONSTANT_CLASS_P (cast_expression
)
8342 && !integer_zerop (cast_expression
)
8343 && !TREE_OVERFLOW (cast_expression
))
8345 tree folded
= fold_build1 (unary_operator
,
8346 TREE_TYPE (cast_expression
),
8348 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8350 expression
= cp_expr (folded
, loc
);
8355 case UNARY_PLUS_EXPR
:
8356 case TRUTH_NOT_EXPR
:
8357 expression
= finish_unary_op_expr (loc
, unary_operator
,
8358 cast_expression
, complain
);
8365 if (non_constant_p
!= NIC_NONE
8366 && cp_parser_non_integral_constant_expression (parser
,
8368 expression
= error_mark_node
;
8373 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8374 /*member_access_only_p=*/false,
8379 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8380 unary-operator, the corresponding tree code is returned. */
8382 static enum tree_code
8383 cp_parser_unary_operator (cp_token
* token
)
8385 switch (token
->type
)
8388 return INDIRECT_REF
;
8394 return UNARY_PLUS_EXPR
;
8400 return TRUTH_NOT_EXPR
;
8403 return BIT_NOT_EXPR
;
8410 /* Parse a new-expression.
8413 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8414 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8416 Returns a representation of the expression. */
8419 cp_parser_new_expression (cp_parser
* parser
)
8421 bool global_scope_p
;
8422 vec
<tree
, va_gc
> *placement
;
8424 vec
<tree
, va_gc
> *initializer
;
8425 tree nelts
= NULL_TREE
;
8428 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8430 /* Look for the optional `::' operator. */
8432 = (cp_parser_global_scope_opt (parser
,
8433 /*current_scope_valid_p=*/false)
8435 /* Look for the `new' operator. */
8436 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8437 /* There's no easy way to tell a new-placement from the
8438 `( type-id )' construct. */
8439 cp_parser_parse_tentatively (parser
);
8440 /* Look for a new-placement. */
8441 placement
= cp_parser_new_placement (parser
);
8442 /* If that didn't work out, there's no new-placement. */
8443 if (!cp_parser_parse_definitely (parser
))
8445 if (placement
!= NULL
)
8446 release_tree_vector (placement
);
8450 /* If the next token is a `(', then we have a parenthesized
8452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8455 const char *saved_message
= parser
->type_definition_forbidden_message
;
8457 /* Consume the `('. */
8458 matching_parens parens
;
8459 parens
.consume_open (parser
);
8461 /* Parse the type-id. */
8462 parser
->type_definition_forbidden_message
8463 = G_("types may not be defined in a new-expression");
8465 type_id_in_expr_sentinel
s (parser
);
8466 type
= cp_parser_type_id (parser
);
8468 parser
->type_definition_forbidden_message
= saved_message
;
8470 /* Look for the closing `)'. */
8471 parens
.require_close (parser
);
8472 token
= cp_lexer_peek_token (parser
->lexer
);
8473 /* There should not be a direct-new-declarator in this production,
8474 but GCC used to allowed this, so we check and emit a sensible error
8475 message for this case. */
8476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8478 error_at (token
->location
,
8479 "array bound forbidden after parenthesized type-id");
8480 inform (token
->location
,
8481 "try removing the parentheses around the type-id");
8482 cp_parser_direct_new_declarator (parser
);
8485 /* Otherwise, there must be a new-type-id. */
8487 type
= cp_parser_new_type_id (parser
, &nelts
);
8489 /* If the next token is a `(' or '{', then we have a new-initializer. */
8490 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8491 if (token
->type
== CPP_OPEN_PAREN
8492 || token
->type
== CPP_OPEN_BRACE
)
8493 initializer
= cp_parser_new_initializer (parser
);
8497 /* A new-expression may not appear in an integral constant
8499 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8500 ret
= error_mark_node
;
8501 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8502 of a new-type-id or type-id of a new-expression, the new-expression shall
8503 contain a new-initializer of the form ( assignment-expression )".
8504 Additionally, consistently with the spirit of DR 1467, we want to accept
8505 'new auto { 2 }' too. */
8506 else if ((ret
= type_uses_auto (type
))
8507 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8508 && (vec_safe_length (initializer
) != 1
8509 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8510 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8512 error_at (token
->location
,
8513 "initialization of new-expression for type %<auto%> "
8514 "requires exactly one element");
8515 ret
= error_mark_node
;
8519 /* Construct a location e.g.:
8522 with caret == start at the start of the "new" token, and the end
8523 at the end of the final token we consumed. */
8524 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8525 location_t end_loc
= get_finish (end_tok
->location
);
8526 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8528 /* Create a representation of the new-expression. */
8529 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8530 tf_warning_or_error
);
8531 protected_set_expr_location (ret
, combined_loc
);
8534 if (placement
!= NULL
)
8535 release_tree_vector (placement
);
8536 if (initializer
!= NULL
)
8537 release_tree_vector (initializer
);
8542 /* Parse a new-placement.
8547 Returns the same representation as for an expression-list. */
8549 static vec
<tree
, va_gc
> *
8550 cp_parser_new_placement (cp_parser
* parser
)
8552 vec
<tree
, va_gc
> *expression_list
;
8554 /* Parse the expression-list. */
8555 expression_list
= (cp_parser_parenthesized_expression_list
8556 (parser
, non_attr
, /*cast_p=*/false,
8557 /*allow_expansion_p=*/true,
8558 /*non_constant_p=*/NULL
));
8560 if (expression_list
&& expression_list
->is_empty ())
8561 error ("expected expression-list or type-id");
8563 return expression_list
;
8566 /* Parse a new-type-id.
8569 type-specifier-seq new-declarator [opt]
8571 Returns the TYPE allocated. If the new-type-id indicates an array
8572 type, *NELTS is set to the number of elements in the last array
8573 bound; the TYPE will not include the last array bound. */
8576 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8578 cp_decl_specifier_seq type_specifier_seq
;
8579 cp_declarator
*new_declarator
;
8580 cp_declarator
*declarator
;
8581 cp_declarator
*outer_declarator
;
8582 const char *saved_message
;
8584 /* The type-specifier sequence must not contain type definitions.
8585 (It cannot contain declarations of new types either, but if they
8586 are not definitions we will catch that because they are not
8588 saved_message
= parser
->type_definition_forbidden_message
;
8589 parser
->type_definition_forbidden_message
8590 = G_("types may not be defined in a new-type-id");
8591 /* Parse the type-specifier-seq. */
8592 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8593 /*is_trailing_return=*/false,
8594 &type_specifier_seq
);
8595 /* Restore the old message. */
8596 parser
->type_definition_forbidden_message
= saved_message
;
8598 if (type_specifier_seq
.type
== error_mark_node
)
8599 return error_mark_node
;
8601 /* Parse the new-declarator. */
8602 new_declarator
= cp_parser_new_declarator_opt (parser
);
8604 /* Determine the number of elements in the last array dimension, if
8607 /* Skip down to the last array dimension. */
8608 declarator
= new_declarator
;
8609 outer_declarator
= NULL
;
8610 while (declarator
&& (declarator
->kind
== cdk_pointer
8611 || declarator
->kind
== cdk_ptrmem
))
8613 outer_declarator
= declarator
;
8614 declarator
= declarator
->declarator
;
8617 && declarator
->kind
== cdk_array
8618 && declarator
->declarator
8619 && declarator
->declarator
->kind
== cdk_array
)
8621 outer_declarator
= declarator
;
8622 declarator
= declarator
->declarator
;
8625 if (declarator
&& declarator
->kind
== cdk_array
)
8627 *nelts
= declarator
->u
.array
.bounds
;
8628 if (*nelts
== error_mark_node
)
8629 *nelts
= integer_one_node
;
8631 if (outer_declarator
)
8632 outer_declarator
->declarator
= declarator
->declarator
;
8634 new_declarator
= NULL
;
8637 return groktypename (&type_specifier_seq
, new_declarator
, false);
8640 /* Parse an (optional) new-declarator.
8643 ptr-operator new-declarator [opt]
8644 direct-new-declarator
8646 Returns the declarator. */
8648 static cp_declarator
*
8649 cp_parser_new_declarator_opt (cp_parser
* parser
)
8651 enum tree_code code
;
8652 tree type
, std_attributes
= NULL_TREE
;
8653 cp_cv_quals cv_quals
;
8655 /* We don't know if there's a ptr-operator next, or not. */
8656 cp_parser_parse_tentatively (parser
);
8657 /* Look for a ptr-operator. */
8658 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8659 /* If that worked, look for more new-declarators. */
8660 if (cp_parser_parse_definitely (parser
))
8662 cp_declarator
*declarator
;
8664 /* Parse another optional declarator. */
8665 declarator
= cp_parser_new_declarator_opt (parser
);
8667 declarator
= cp_parser_make_indirect_declarator
8668 (code
, type
, cv_quals
, declarator
, std_attributes
);
8673 /* If the next token is a `[', there is a direct-new-declarator. */
8674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8675 return cp_parser_direct_new_declarator (parser
);
8680 /* Parse a direct-new-declarator.
8682 direct-new-declarator:
8684 direct-new-declarator [constant-expression]
8688 static cp_declarator
*
8689 cp_parser_direct_new_declarator (cp_parser
* parser
)
8691 cp_declarator
*declarator
= NULL
;
8698 /* Look for the opening `['. */
8699 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8701 token
= cp_lexer_peek_token (parser
->lexer
);
8702 expression
= cp_parser_expression (parser
);
8703 /* The standard requires that the expression have integral
8704 type. DR 74 adds enumeration types. We believe that the
8705 real intent is that these expressions be handled like the
8706 expression in a `switch' condition, which also allows
8707 classes with a single conversion to integral or
8708 enumeration type. */
8709 if (!processing_template_decl
)
8712 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8717 error_at (token
->location
,
8718 "expression in new-declarator must have integral "
8719 "or enumeration type");
8720 expression
= error_mark_node
;
8724 /* Look for the closing `]'. */
8725 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8727 /* Add this bound to the declarator. */
8728 declarator
= make_array_declarator (declarator
, expression
);
8730 /* If the next token is not a `[', then there are no more
8732 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8739 /* Parse a new-initializer.
8742 ( expression-list [opt] )
8745 Returns a representation of the expression-list. */
8747 static vec
<tree
, va_gc
> *
8748 cp_parser_new_initializer (cp_parser
* parser
)
8750 vec
<tree
, va_gc
> *expression_list
;
8752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8755 bool expr_non_constant_p
;
8756 cp_lexer_set_source_position (parser
->lexer
);
8757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8758 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8759 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8760 expression_list
= make_tree_vector_single (t
);
8763 expression_list
= (cp_parser_parenthesized_expression_list
8764 (parser
, non_attr
, /*cast_p=*/false,
8765 /*allow_expansion_p=*/true,
8766 /*non_constant_p=*/NULL
));
8768 return expression_list
;
8771 /* Parse a delete-expression.
8774 :: [opt] delete cast-expression
8775 :: [opt] delete [ ] cast-expression
8777 Returns a representation of the expression. */
8780 cp_parser_delete_expression (cp_parser
* parser
)
8782 bool global_scope_p
;
8786 /* Look for the optional `::' operator. */
8788 = (cp_parser_global_scope_opt (parser
,
8789 /*current_scope_valid_p=*/false)
8791 /* Look for the `delete' keyword. */
8792 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8793 /* See if the array syntax is in use. */
8794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8796 /* Consume the `[' token. */
8797 cp_lexer_consume_token (parser
->lexer
);
8798 /* Look for the `]' token. */
8799 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8800 /* Remember that this is the `[]' construct. */
8806 /* Parse the cast-expression. */
8807 expression
= cp_parser_simple_cast_expression (parser
);
8809 /* A delete-expression may not appear in an integral constant
8811 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8812 return error_mark_node
;
8814 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8815 tf_warning_or_error
);
8818 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8819 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8823 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8825 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8826 switch (token
->type
)
8832 case CPP_CLOSE_SQUARE
:
8833 case CPP_CLOSE_PAREN
:
8834 case CPP_CLOSE_BRACE
:
8835 case CPP_OPEN_BRACE
:
8839 case CPP_DEREF_STAR
:
8847 case CPP_GREATER_EQ
:
8868 case CPP_OPEN_PAREN
:
8869 /* In ((type ()) () the last () isn't a valid cast-expression,
8870 so the whole must be parsed as postfix-expression. */
8871 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8874 case CPP_OPEN_SQUARE
:
8875 /* '[' may start a primary-expression in obj-c++ and in C++11,
8876 as a lambda-expression, eg, '(void)[]{}'. */
8877 if (cxx_dialect
>= cxx11
)
8879 return c_dialect_objc ();
8882 case CPP_MINUS_MINUS
:
8883 /* '++' and '--' may or may not start a cast-expression:
8885 struct T { void operator++(int); };
8886 void f() { (T())++; }
8899 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8900 in the order: const_cast, static_cast, reinterpret_cast.
8902 Don't suggest dynamic_cast.
8904 Return the first legal cast kind found, or NULL otherwise. */
8907 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8911 /* Reuse the parser logic by attempting to build the various kinds of
8912 cast, with "complain" disabled.
8913 Identify the first such cast that is valid. */
8915 /* Don't attempt to run such logic within template processing. */
8916 if (processing_template_decl
)
8919 /* First try const_cast. */
8920 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8921 if (trial
!= error_mark_node
)
8922 return "const_cast";
8924 /* If that fails, try static_cast. */
8925 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8926 if (trial
!= error_mark_node
)
8927 return "static_cast";
8929 /* Finally, try reinterpret_cast. */
8930 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8931 if (trial
!= error_mark_node
)
8932 return "reinterpret_cast";
8934 /* No such cast possible. */
8938 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8939 suggesting how to convert a C-style cast of the form:
8943 to a C++-style cast.
8945 The primary range of RICHLOC is asssumed to be that of the original
8946 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8947 of the parens in the C-style cast. */
8950 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8951 location_t close_paren_loc
, tree orig_expr
,
8954 /* This function is non-trivial, so bail out now if the warning isn't
8955 going to be emitted. */
8956 if (!warn_old_style_cast
)
8959 /* Try to find a legal C++ cast, trying them in order:
8960 const_cast, static_cast, reinterpret_cast. */
8961 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8962 if (!cast_suggestion
)
8965 /* Replace the open paren with "CAST_SUGGESTION<". */
8967 pp_printf (&pp
, "%s<", cast_suggestion
);
8968 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8970 /* Replace the close paren with "> (". */
8971 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8973 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8974 rich_loc
->add_fixit_insert_after (")");
8978 /* Parse a cast-expression.
8982 ( type-id ) cast-expression
8984 ADDRESS_P is true iff the unary-expression is appearing as the
8985 operand of the `&' operator. CAST_P is true if this expression is
8986 the target of a cast.
8988 Returns a representation of the expression. */
8991 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8992 bool decltype_p
, cp_id_kind
* pidk
)
8994 /* If it's a `(', then we might be looking at a cast. */
8995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8997 tree type
= NULL_TREE
;
8998 cp_expr
expr (NULL_TREE
);
8999 int cast_expression
= 0;
9000 const char *saved_message
;
9002 /* There's no way to know yet whether or not this is a cast.
9003 For example, `(int (3))' is a unary-expression, while `(int)
9004 3' is a cast. So, we resort to parsing tentatively. */
9005 cp_parser_parse_tentatively (parser
);
9006 /* Types may not be defined in a cast. */
9007 saved_message
= parser
->type_definition_forbidden_message
;
9008 parser
->type_definition_forbidden_message
9009 = G_("types may not be defined in casts");
9010 /* Consume the `('. */
9011 matching_parens parens
;
9012 cp_token
*open_paren
= parens
.consume_open (parser
);
9013 location_t open_paren_loc
= open_paren
->location
;
9014 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9016 /* A very tricky bit is that `(struct S) { 3 }' is a
9017 compound-literal (which we permit in C++ as an extension).
9018 But, that construct is not a cast-expression -- it is a
9019 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9020 is legal; if the compound-literal were a cast-expression,
9021 you'd need an extra set of parentheses.) But, if we parse
9022 the type-id, and it happens to be a class-specifier, then we
9023 will commit to the parse at that point, because we cannot
9024 undo the action that is done when creating a new class. So,
9025 then we cannot back up and do a postfix-expression.
9027 Another tricky case is the following (c++/29234):
9029 struct S { void operator () (); };
9036 As a type-id we parse the parenthesized S()() as a function
9037 returning a function, groktypename complains and we cannot
9038 back up in this case either.
9040 Therefore, we scan ahead to the closing `)', and check to see
9041 if the tokens after the `)' can start a cast-expression. Otherwise
9042 we are dealing with an unary-expression, a postfix-expression
9045 Yet another tricky case, in C++11, is the following (c++/54891):
9049 The issue is that usually, besides the case of lambda-expressions,
9050 the parenthesized type-id cannot be followed by '[', and, eg, we
9051 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9052 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9053 we don't commit, we try a cast-expression, then an unary-expression.
9055 Save tokens so that we can put them back. */
9056 cp_lexer_save_tokens (parser
->lexer
);
9058 /* We may be looking at a cast-expression. */
9059 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9060 /*consume_paren=*/true))
9062 = cp_parser_tokens_start_cast_expression (parser
);
9064 /* Roll back the tokens we skipped. */
9065 cp_lexer_rollback_tokens (parser
->lexer
);
9066 /* If we aren't looking at a cast-expression, simulate an error so
9067 that the call to cp_parser_error_occurred below returns true. */
9068 if (!cast_expression
)
9069 cp_parser_simulate_error (parser
);
9072 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9073 parser
->in_type_id_in_expr_p
= true;
9074 /* Look for the type-id. */
9075 type
= cp_parser_type_id (parser
);
9076 /* Look for the closing `)'. */
9077 cp_token
*close_paren
= parens
.require_close (parser
);
9079 close_paren_loc
= close_paren
->location
;
9080 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9083 /* Restore the saved message. */
9084 parser
->type_definition_forbidden_message
= saved_message
;
9086 /* At this point this can only be either a cast or a
9087 parenthesized ctor such as `(T ())' that looks like a cast to
9088 function returning T. */
9089 if (!cp_parser_error_occurred (parser
))
9091 /* Only commit if the cast-expression doesn't start with
9092 '++', '--', or '[' in C++11. */
9093 if (cast_expression
> 0)
9094 cp_parser_commit_to_topmost_tentative_parse (parser
);
9096 expr
= cp_parser_cast_expression (parser
,
9097 /*address_p=*/false,
9099 /*decltype_p=*/false,
9102 if (cp_parser_parse_definitely (parser
))
9104 /* Warn about old-style casts, if so requested. */
9105 if (warn_old_style_cast
9106 && !in_system_header_at (input_location
)
9107 && !VOID_TYPE_P (type
)
9108 && current_lang_name
!= lang_name_c
)
9110 gcc_rich_location
rich_loc (input_location
);
9111 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9113 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9114 "use of old-style cast to %q#T", type
);
9117 /* Only type conversions to integral or enumeration types
9118 can be used in constant-expressions. */
9119 if (!cast_valid_in_integral_constant_expression_p (type
)
9120 && cp_parser_non_integral_constant_expression (parser
,
9122 return error_mark_node
;
9124 /* Perform the cast. */
9128 with start==caret at the open paren, extending to the
9130 location_t cast_loc
= make_location (open_paren_loc
,
9132 expr
.get_finish ());
9133 expr
= build_c_cast (cast_loc
, type
, expr
);
9138 cp_parser_abort_tentative_parse (parser
);
9141 /* If we get here, then it's not a cast, so it must be a
9142 unary-expression. */
9143 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9144 cast_p
, decltype_p
);
9147 /* Parse a binary expression of the general form:
9151 pm-expression .* cast-expression
9152 pm-expression ->* cast-expression
9154 multiplicative-expression:
9156 multiplicative-expression * pm-expression
9157 multiplicative-expression / pm-expression
9158 multiplicative-expression % pm-expression
9160 additive-expression:
9161 multiplicative-expression
9162 additive-expression + multiplicative-expression
9163 additive-expression - multiplicative-expression
9167 shift-expression << additive-expression
9168 shift-expression >> additive-expression
9170 relational-expression:
9172 relational-expression < shift-expression
9173 relational-expression > shift-expression
9174 relational-expression <= shift-expression
9175 relational-expression >= shift-expression
9179 relational-expression:
9180 relational-expression <? shift-expression
9181 relational-expression >? shift-expression
9183 equality-expression:
9184 relational-expression
9185 equality-expression == relational-expression
9186 equality-expression != relational-expression
9190 and-expression & equality-expression
9192 exclusive-or-expression:
9194 exclusive-or-expression ^ and-expression
9196 inclusive-or-expression:
9197 exclusive-or-expression
9198 inclusive-or-expression | exclusive-or-expression
9200 logical-and-expression:
9201 inclusive-or-expression
9202 logical-and-expression && inclusive-or-expression
9204 logical-or-expression:
9205 logical-and-expression
9206 logical-or-expression || logical-and-expression
9208 All these are implemented with a single function like:
9211 simple-cast-expression
9212 binary-expression <token> binary-expression
9214 CAST_P is true if this expression is the target of a cast.
9216 The binops_by_token map is used to get the tree codes for each <token> type.
9217 binary-expressions are associated according to a precedence table. */
9219 #define TOKEN_PRECEDENCE(token) \
9220 (((token->type == CPP_GREATER \
9221 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9222 && !parser->greater_than_is_operator_p) \
9223 ? PREC_NOT_OPERATOR \
9224 : binops_by_token[token->type].prec)
9227 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9228 bool no_toplevel_fold_p
,
9230 enum cp_parser_prec prec
,
9233 cp_parser_expression_stack stack
;
9234 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9235 cp_parser_expression_stack_entry current
;
9238 enum tree_code rhs_type
;
9239 enum cp_parser_prec new_prec
, lookahead_prec
;
9242 /* Parse the first expression. */
9243 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9244 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9245 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9246 cast_p
, decltype_p
, pidk
);
9247 current
.prec
= prec
;
9249 if (cp_parser_error_occurred (parser
))
9250 return error_mark_node
;
9254 /* Get an operator token. */
9255 token
= cp_lexer_peek_token (parser
->lexer
);
9257 if (warn_cxx11_compat
9258 && token
->type
== CPP_RSHIFT
9259 && !parser
->greater_than_is_operator_p
)
9261 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9262 "%<>>%> operator is treated"
9263 " as two right angle brackets in C++11"))
9264 inform (token
->location
,
9265 "suggest parentheses around %<>>%> expression");
9268 new_prec
= TOKEN_PRECEDENCE (token
);
9269 if (new_prec
!= PREC_NOT_OPERATOR
9270 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9271 /* This is a fold-expression; handle it later. */
9272 new_prec
= PREC_NOT_OPERATOR
;
9274 /* Popping an entry off the stack means we completed a subexpression:
9275 - either we found a token which is not an operator (`>' where it is not
9276 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9277 will happen repeatedly;
9278 - or, we found an operator which has lower priority. This is the case
9279 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9281 if (new_prec
<= current
.prec
)
9290 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9291 current
.loc
= token
->location
;
9293 /* We used the operator token. */
9294 cp_lexer_consume_token (parser
->lexer
);
9296 /* For "false && x" or "true || x", x will never be executed;
9297 disable warnings while evaluating it. */
9298 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9299 c_inhibit_evaluation_warnings
+=
9300 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9301 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9302 c_inhibit_evaluation_warnings
+=
9303 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9305 /* Extract another operand. It may be the RHS of this expression
9306 or the LHS of a new, higher priority expression. */
9307 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9308 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9309 rhs
= cp_parser_simple_cast_expression (parser
);
9311 /* Get another operator token. Look up its precedence to avoid
9312 building a useless (immediately popped) stack entry for common
9313 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9314 token
= cp_lexer_peek_token (parser
->lexer
);
9315 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9316 if (lookahead_prec
!= PREC_NOT_OPERATOR
9317 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9318 lookahead_prec
= PREC_NOT_OPERATOR
;
9319 if (lookahead_prec
> new_prec
)
9321 /* ... and prepare to parse the RHS of the new, higher priority
9322 expression. Since precedence levels on the stack are
9323 monotonically increasing, we do not have to care about
9328 current
.lhs_type
= rhs_type
;
9329 current
.prec
= new_prec
;
9330 new_prec
= lookahead_prec
;
9334 lookahead_prec
= new_prec
;
9335 /* If the stack is not empty, we have parsed into LHS the right side
9336 (`4' in the example above) of an expression we had suspended.
9337 We can use the information on the stack to recover the LHS (`3')
9338 from the stack together with the tree code (`MULT_EXPR'), and
9339 the precedence of the higher level subexpression
9340 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9341 which will be used to actually build the additive expression. */
9343 rhs_type
= current
.lhs_type
;
9348 /* Undo the disabling of warnings done above. */
9349 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9350 c_inhibit_evaluation_warnings
-=
9351 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9352 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9353 c_inhibit_evaluation_warnings
-=
9354 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9356 if (warn_logical_not_paren
9357 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9358 && current
.lhs_type
== TRUTH_NOT_EXPR
9359 /* Avoid warning for !!x == y. */
9360 && (TREE_CODE (current
.lhs
) != NE_EXPR
9361 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9362 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9363 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9364 /* Avoid warning for !b == y where b is boolean. */
9365 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9366 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9368 /* Avoid warning for !!b == y where b is boolean. */
9369 && (!DECL_P (current
.lhs
)
9370 || TREE_TYPE (current
.lhs
) == NULL_TREE
9371 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9372 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9373 current
.lhs
, maybe_constant_value (rhs
));
9377 location_t combined_loc
= make_location (current
.loc
,
9378 current
.lhs
.get_start (),
9381 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9382 ERROR_MARK for everything that is not a binary expression.
9383 This makes warn_about_parentheses miss some warnings that
9384 involve unary operators. For unary expressions we should
9385 pass the correct tree_code unless the unary expression was
9386 surrounded by parentheses.
9388 if (no_toplevel_fold_p
9389 && lookahead_prec
<= current
.prec
9392 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9393 current
.lhs
= error_mark_node
;
9397 = build_min (current
.tree_type
,
9398 TREE_CODE_CLASS (current
.tree_type
)
9400 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9401 current
.lhs
.get_value (), rhs
.get_value ());
9402 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9407 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9408 current
.lhs
, current
.lhs_type
,
9409 rhs
, rhs_type
, &overload
,
9410 complain_flags (decltype_p
));
9411 /* TODO: build_x_binary_op doesn't always honor the location. */
9412 current
.lhs
.set_location (combined_loc
);
9414 current
.lhs_type
= current
.tree_type
;
9416 /* If the binary operator required the use of an overloaded operator,
9417 then this expression cannot be an integral constant-expression.
9418 An overloaded operator can be used even if both operands are
9419 otherwise permissible in an integral constant-expression if at
9420 least one of the operands is of enumeration type. */
9423 && cp_parser_non_integral_constant_expression (parser
,
9425 return error_mark_node
;
9432 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9433 bool no_toplevel_fold_p
,
9434 enum cp_parser_prec prec
,
9437 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9438 /*decltype*/false, prec
, pidk
);
9441 /* Parse the `? expression : assignment-expression' part of a
9442 conditional-expression. The LOGICAL_OR_EXPR is the
9443 logical-or-expression that started the conditional-expression.
9444 Returns a representation of the entire conditional-expression.
9446 This routine is used by cp_parser_assignment_expression.
9448 ? expression : assignment-expression
9452 ? : assignment-expression */
9455 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9457 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9458 cp_expr assignment_expr
;
9459 struct cp_token
*token
;
9460 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9462 /* Consume the `?' token. */
9463 cp_lexer_consume_token (parser
->lexer
);
9464 token
= cp_lexer_peek_token (parser
->lexer
);
9465 if (cp_parser_allow_gnu_extensions_p (parser
)
9466 && token
->type
== CPP_COLON
)
9468 pedwarn (token
->location
, OPT_Wpedantic
,
9469 "ISO C++ does not allow ?: with omitted middle operand");
9470 /* Implicit true clause. */
9472 c_inhibit_evaluation_warnings
+=
9473 folded_logical_or_expr
== truthvalue_true_node
;
9474 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9478 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9479 parser
->colon_corrects_to_scope_p
= false;
9480 /* Parse the expression. */
9481 c_inhibit_evaluation_warnings
+=
9482 folded_logical_or_expr
== truthvalue_false_node
;
9483 expr
= cp_parser_expression (parser
);
9484 c_inhibit_evaluation_warnings
+=
9485 ((folded_logical_or_expr
== truthvalue_true_node
)
9486 - (folded_logical_or_expr
== truthvalue_false_node
));
9487 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9490 /* The next token should be a `:'. */
9491 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9492 /* Parse the assignment-expression. */
9493 assignment_expr
= cp_parser_assignment_expression (parser
);
9494 c_inhibit_evaluation_warnings
-=
9495 folded_logical_or_expr
== truthvalue_true_node
;
9498 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9499 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9500 with the caret at the "?", ranging from the start of
9501 the logical_or_expr to the end of the assignment_expr. */
9502 loc
= make_location (loc
,
9503 logical_or_expr
.get_start (),
9504 assignment_expr
.get_finish ());
9506 /* Build the conditional-expression. */
9507 return build_x_conditional_expr (loc
, logical_or_expr
,
9510 tf_warning_or_error
);
9513 /* Parse an assignment-expression.
9515 assignment-expression:
9516 conditional-expression
9517 logical-or-expression assignment-operator assignment_expression
9520 CAST_P is true if this expression is the target of a cast.
9521 DECLTYPE_P is true if this expression is the operand of decltype.
9523 Returns a representation for the expression. */
9526 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9527 bool cast_p
, bool decltype_p
)
9531 /* If the next token is the `throw' keyword, then we're looking at
9532 a throw-expression. */
9533 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9534 expr
= cp_parser_throw_expression (parser
);
9535 /* Otherwise, it must be that we are looking at a
9536 logical-or-expression. */
9539 /* Parse the binary expressions (logical-or-expression). */
9540 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9542 PREC_NOT_OPERATOR
, pidk
);
9543 /* If the next token is a `?' then we're actually looking at a
9544 conditional-expression. */
9545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9546 return cp_parser_question_colon_clause (parser
, expr
);
9549 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9551 /* If it's an assignment-operator, we're using the second
9553 enum tree_code assignment_operator
9554 = cp_parser_assignment_operator_opt (parser
);
9555 if (assignment_operator
!= ERROR_MARK
)
9557 bool non_constant_p
;
9559 /* Parse the right-hand side of the assignment. */
9560 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9563 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9564 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9566 /* An assignment may not appear in a
9567 constant-expression. */
9568 if (cp_parser_non_integral_constant_expression (parser
,
9570 return error_mark_node
;
9571 /* Build the assignment expression. Its default
9575 is the location of the '=' token as the
9576 caret, ranging from the start of the lhs to the
9578 loc
= make_location (loc
,
9581 expr
= build_x_modify_expr (loc
, expr
,
9582 assignment_operator
,
9584 complain_flags (decltype_p
));
9585 /* TODO: build_x_modify_expr doesn't honor the location,
9586 so we must set it here. */
9587 expr
.set_location (loc
);
9595 /* Parse an (optional) assignment-operator.
9597 assignment-operator: one of
9598 = *= /= %= += -= >>= <<= &= ^= |=
9602 assignment-operator: one of
9605 If the next token is an assignment operator, the corresponding tree
9606 code is returned, and the token is consumed. For example, for
9607 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9608 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9609 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9610 operator, ERROR_MARK is returned. */
9612 static enum tree_code
9613 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9618 /* Peek at the next token. */
9619 token
= cp_lexer_peek_token (parser
->lexer
);
9621 switch (token
->type
)
9632 op
= TRUNC_DIV_EXPR
;
9636 op
= TRUNC_MOD_EXPR
;
9668 /* Nothing else is an assignment operator. */
9672 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9673 if (op
!= ERROR_MARK
9674 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9677 /* If it was an assignment operator, consume it. */
9678 if (op
!= ERROR_MARK
)
9679 cp_lexer_consume_token (parser
->lexer
);
9684 /* Parse an expression.
9687 assignment-expression
9688 expression , assignment-expression
9690 CAST_P is true if this expression is the target of a cast.
9691 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9692 except possibly parenthesized or on the RHS of a comma (N3276).
9694 Returns a representation of the expression. */
9697 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9698 bool cast_p
, bool decltype_p
)
9700 cp_expr expression
= NULL_TREE
;
9701 location_t loc
= UNKNOWN_LOCATION
;
9705 cp_expr assignment_expression
;
9707 /* Parse the next assignment-expression. */
9708 assignment_expression
9709 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9711 /* We don't create a temporary for a call that is the immediate operand
9712 of decltype or on the RHS of a comma. But when we see a comma, we
9713 need to create a temporary for a call on the LHS. */
9714 if (decltype_p
&& !processing_template_decl
9715 && TREE_CODE (assignment_expression
) == CALL_EXPR
9716 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9717 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9718 assignment_expression
9719 = build_cplus_new (TREE_TYPE (assignment_expression
),
9720 assignment_expression
, tf_warning_or_error
);
9722 /* If this is the first assignment-expression, we can just
9725 expression
= assignment_expression
;
9728 /* Create a location with caret at the comma, ranging
9729 from the start of the LHS to the end of the RHS. */
9730 loc
= make_location (loc
,
9731 expression
.get_start (),
9732 assignment_expression
.get_finish ());
9733 expression
= build_x_compound_expr (loc
, expression
,
9734 assignment_expression
,
9735 complain_flags (decltype_p
));
9736 expression
.set_location (loc
);
9738 /* If the next token is not a comma, or we're in a fold-expression, then
9739 we are done with the expression. */
9740 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9741 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9743 /* Consume the `,'. */
9744 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9745 cp_lexer_consume_token (parser
->lexer
);
9746 /* A comma operator cannot appear in a constant-expression. */
9747 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9748 expression
= error_mark_node
;
9754 /* Parse a constant-expression.
9756 constant-expression:
9757 conditional-expression
9759 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9760 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9761 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9762 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9763 only parse a conditional-expression, otherwise parse an
9764 assignment-expression. See below for rationale. */
9767 cp_parser_constant_expression (cp_parser
* parser
,
9768 bool allow_non_constant_p
,
9769 bool *non_constant_p
,
9772 bool saved_integral_constant_expression_p
;
9773 bool saved_allow_non_integral_constant_expression_p
;
9774 bool saved_non_integral_constant_expression_p
;
9777 /* It might seem that we could simply parse the
9778 conditional-expression, and then check to see if it were
9779 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9780 one that the compiler can figure out is constant, possibly after
9781 doing some simplifications or optimizations. The standard has a
9782 precise definition of constant-expression, and we must honor
9783 that, even though it is somewhat more restrictive.
9789 is not a legal declaration, because `(2, 3)' is not a
9790 constant-expression. The `,' operator is forbidden in a
9791 constant-expression. However, GCC's constant-folding machinery
9792 will fold this operation to an INTEGER_CST for `3'. */
9794 /* Save the old settings. */
9795 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9796 saved_allow_non_integral_constant_expression_p
9797 = parser
->allow_non_integral_constant_expression_p
;
9798 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9799 /* We are now parsing a constant-expression. */
9800 parser
->integral_constant_expression_p
= true;
9801 parser
->allow_non_integral_constant_expression_p
9802 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9803 parser
->non_integral_constant_expression_p
= false;
9804 /* Although the grammar says "conditional-expression", when not STRICT_P,
9805 we parse an "assignment-expression", which also permits
9806 "throw-expression" and the use of assignment operators. In the case
9807 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9808 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9809 actually essential that we look for an assignment-expression.
9810 For example, cp_parser_initializer_clauses uses this function to
9811 determine whether a particular assignment-expression is in fact
9815 /* Parse the binary expressions (logical-or-expression). */
9816 expression
= cp_parser_binary_expression (parser
, false, false, false,
9817 PREC_NOT_OPERATOR
, NULL
);
9818 /* If the next token is a `?' then we're actually looking at
9819 a conditional-expression; otherwise we're done. */
9820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9821 expression
= cp_parser_question_colon_clause (parser
, expression
);
9824 expression
= cp_parser_assignment_expression (parser
);
9825 /* Restore the old settings. */
9826 parser
->integral_constant_expression_p
9827 = saved_integral_constant_expression_p
;
9828 parser
->allow_non_integral_constant_expression_p
9829 = saved_allow_non_integral_constant_expression_p
;
9830 if (cxx_dialect
>= cxx11
)
9832 /* Require an rvalue constant expression here; that's what our
9833 callers expect. Reference constant expressions are handled
9834 separately in e.g. cp_parser_template_argument. */
9835 tree decay
= expression
;
9836 if (TREE_TYPE (expression
)
9837 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9838 decay
= build_address (expression
);
9839 bool is_const
= potential_rvalue_constant_expression (decay
);
9840 parser
->non_integral_constant_expression_p
= !is_const
;
9841 if (!is_const
&& !allow_non_constant_p
)
9842 require_potential_rvalue_constant_expression (decay
);
9844 if (allow_non_constant_p
)
9845 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9846 parser
->non_integral_constant_expression_p
9847 = saved_non_integral_constant_expression_p
;
9852 /* Parse __builtin_offsetof.
9854 offsetof-expression:
9855 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9857 offsetof-member-designator:
9859 | offsetof-member-designator "." id-expression
9860 | offsetof-member-designator "[" expression "]"
9861 | offsetof-member-designator "->" id-expression */
9864 cp_parser_builtin_offsetof (cp_parser
*parser
)
9866 int save_ice_p
, save_non_ice_p
;
9871 location_t finish_loc
;
9873 /* We're about to accept non-integral-constant things, but will
9874 definitely yield an integral constant expression. Save and
9875 restore these values around our local parsing. */
9876 save_ice_p
= parser
->integral_constant_expression_p
;
9877 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9879 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9881 /* Consume the "__builtin_offsetof" token. */
9882 cp_lexer_consume_token (parser
->lexer
);
9883 /* Consume the opening `('. */
9884 matching_parens parens
;
9885 parens
.require_open (parser
);
9886 /* Parse the type-id. */
9887 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9889 const char *saved_message
= parser
->type_definition_forbidden_message
;
9890 parser
->type_definition_forbidden_message
9891 = G_("types may not be defined within __builtin_offsetof");
9892 type
= cp_parser_type_id (parser
);
9893 parser
->type_definition_forbidden_message
= saved_message
;
9895 /* Look for the `,'. */
9896 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9897 token
= cp_lexer_peek_token (parser
->lexer
);
9899 /* Build the (type *)null that begins the traditional offsetof macro. */
9901 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9902 tf_warning_or_error
);
9904 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9905 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9906 true, &dummy
, token
->location
);
9909 token
= cp_lexer_peek_token (parser
->lexer
);
9910 switch (token
->type
)
9912 case CPP_OPEN_SQUARE
:
9913 /* offsetof-member-designator "[" expression "]" */
9914 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9919 /* offsetof-member-designator "->" identifier */
9920 expr
= grok_array_decl (token
->location
, expr
,
9921 integer_zero_node
, false);
9925 /* offsetof-member-designator "." identifier */
9926 cp_lexer_consume_token (parser
->lexer
);
9927 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9932 case CPP_CLOSE_PAREN
:
9933 /* Consume the ")" token. */
9934 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9935 cp_lexer_consume_token (parser
->lexer
);
9939 /* Error. We know the following require will fail, but
9940 that gives the proper error message. */
9941 parens
.require_close (parser
);
9942 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9943 expr
= error_mark_node
;
9949 /* Make a location of the form:
9950 __builtin_offsetof (struct s, f)
9951 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9952 with caret at the type-id, ranging from the start of the
9953 "_builtin_offsetof" token to the close paren. */
9954 loc
= make_location (loc
, start_loc
, finish_loc
);
9955 /* The result will be an INTEGER_CST, so we need to explicitly
9956 preserve the location. */
9957 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9960 parser
->integral_constant_expression_p
= save_ice_p
;
9961 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9963 expr
= expr
.maybe_add_location_wrapper ();
9967 /* Parse a trait expression.
9969 Returns a representation of the expression, the underlying type
9970 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9973 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9976 tree type1
, type2
= NULL_TREE
;
9977 bool binary
= false;
9978 bool variadic
= false;
9982 case RID_HAS_NOTHROW_ASSIGN
:
9983 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9985 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9986 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9988 case RID_HAS_NOTHROW_COPY
:
9989 kind
= CPTK_HAS_NOTHROW_COPY
;
9991 case RID_HAS_TRIVIAL_ASSIGN
:
9992 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9994 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9995 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9997 case RID_HAS_TRIVIAL_COPY
:
9998 kind
= CPTK_HAS_TRIVIAL_COPY
;
10000 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10001 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10003 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10004 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10006 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10007 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10009 case RID_IS_ABSTRACT
:
10010 kind
= CPTK_IS_ABSTRACT
;
10012 case RID_IS_AGGREGATE
:
10013 kind
= CPTK_IS_AGGREGATE
;
10015 case RID_IS_BASE_OF
:
10016 kind
= CPTK_IS_BASE_OF
;
10020 kind
= CPTK_IS_CLASS
;
10023 kind
= CPTK_IS_EMPTY
;
10026 kind
= CPTK_IS_ENUM
;
10029 kind
= CPTK_IS_FINAL
;
10031 case RID_IS_LITERAL_TYPE
:
10032 kind
= CPTK_IS_LITERAL_TYPE
;
10035 kind
= CPTK_IS_POD
;
10037 case RID_IS_POLYMORPHIC
:
10038 kind
= CPTK_IS_POLYMORPHIC
;
10040 case RID_IS_SAME_AS
:
10041 kind
= CPTK_IS_SAME_AS
;
10044 case RID_IS_STD_LAYOUT
:
10045 kind
= CPTK_IS_STD_LAYOUT
;
10047 case RID_IS_TRIVIAL
:
10048 kind
= CPTK_IS_TRIVIAL
;
10050 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10051 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10054 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10055 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10058 case RID_IS_TRIVIALLY_COPYABLE
:
10059 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10062 kind
= CPTK_IS_UNION
;
10064 case RID_UNDERLYING_TYPE
:
10065 kind
= CPTK_UNDERLYING_TYPE
;
10070 case RID_DIRECT_BASES
:
10071 kind
= CPTK_DIRECT_BASES
;
10073 case RID_IS_ASSIGNABLE
:
10074 kind
= CPTK_IS_ASSIGNABLE
;
10077 case RID_IS_CONSTRUCTIBLE
:
10078 kind
= CPTK_IS_CONSTRUCTIBLE
;
10082 gcc_unreachable ();
10085 /* Get location of initial token. */
10086 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10088 /* Consume the token. */
10089 cp_lexer_consume_token (parser
->lexer
);
10091 matching_parens parens
;
10092 parens
.require_open (parser
);
10095 type_id_in_expr_sentinel
s (parser
);
10096 type1
= cp_parser_type_id (parser
);
10099 if (type1
== error_mark_node
)
10100 return error_mark_node
;
10104 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10107 type_id_in_expr_sentinel
s (parser
);
10108 type2
= cp_parser_type_id (parser
);
10111 if (type2
== error_mark_node
)
10112 return error_mark_node
;
10116 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10118 cp_lexer_consume_token (parser
->lexer
);
10119 tree elt
= cp_parser_type_id (parser
);
10120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10122 cp_lexer_consume_token (parser
->lexer
);
10123 elt
= make_pack_expansion (elt
);
10125 if (elt
== error_mark_node
)
10126 return error_mark_node
;
10127 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10131 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10132 parens
.require_close (parser
);
10134 /* Construct a location of the form:
10135 __is_trivially_copyable(_Tp)
10136 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10137 with start == caret, finishing at the close-paren. */
10138 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10140 /* Complete the trait expression, which may mean either processing
10141 the trait expr now or saving it for template instantiation. */
10144 case CPTK_UNDERLYING_TYPE
:
10145 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10147 return cp_expr (finish_bases (type1
, false), trait_loc
);
10148 case CPTK_DIRECT_BASES
:
10149 return cp_expr (finish_bases (type1
, true), trait_loc
);
10151 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10155 /* Parse a lambda expression.
10158 lambda-introducer lambda-declarator [opt] compound-statement
10160 Returns a representation of the expression. */
10163 cp_parser_lambda_expression (cp_parser
* parser
)
10165 tree lambda_expr
= build_lambda_expr ();
10168 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10169 cp_token_position start
= 0;
10171 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10173 if (cxx_dialect
>= cxx2a
)
10174 /* C++20 allows lambdas in unevaluated context. */;
10175 else if (cp_unevaluated_operand
)
10177 if (!token
->error_reported
)
10179 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10180 "lambda-expression in unevaluated context"
10181 " only available with -std=c++2a or -std=gnu++2a");
10182 token
->error_reported
= true;
10186 else if (parser
->in_template_argument_list_p
)
10188 if (!token
->error_reported
)
10190 error_at (token
->location
, "lambda-expression in template-argument"
10191 " only available with -std=c++2a or -std=gnu++2a");
10192 token
->error_reported
= true;
10197 /* We may be in the middle of deferred access check. Disable
10199 push_deferring_access_checks (dk_no_deferred
);
10201 cp_parser_lambda_introducer (parser
, lambda_expr
);
10202 if (cp_parser_error_occurred (parser
))
10203 return error_mark_node
;
10205 type
= begin_lambda_type (lambda_expr
);
10206 if (type
== error_mark_node
)
10207 return error_mark_node
;
10209 record_lambda_scope (lambda_expr
);
10211 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10212 determine_visibility (TYPE_NAME (type
));
10214 /* Now that we've started the type, add the capture fields for any
10215 explicit captures. */
10216 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10219 /* Inside the class, surrounding template-parameter-lists do not apply. */
10220 unsigned int saved_num_template_parameter_lists
10221 = parser
->num_template_parameter_lists
;
10222 unsigned char in_statement
= parser
->in_statement
;
10223 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10224 bool fully_implicit_function_template_p
10225 = parser
->fully_implicit_function_template_p
;
10226 tree implicit_template_parms
= parser
->implicit_template_parms
;
10227 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10228 bool auto_is_implicit_function_template_parm_p
10229 = parser
->auto_is_implicit_function_template_parm_p
;
10231 parser
->num_template_parameter_lists
= 0;
10232 parser
->in_statement
= 0;
10233 parser
->in_switch_statement_p
= false;
10234 parser
->fully_implicit_function_template_p
= false;
10235 parser
->implicit_template_parms
= 0;
10236 parser
->implicit_template_scope
= 0;
10237 parser
->auto_is_implicit_function_template_parm_p
= false;
10239 /* By virtue of defining a local class, a lambda expression has access to
10240 the private variables of enclosing classes. */
10242 if (cp_parser_start_tentative_firewall (parser
))
10245 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10247 if (ok
&& cp_parser_error_occurred (parser
))
10252 cp_parser_lambda_body (parser
, lambda_expr
);
10254 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10256 if (cp_parser_skip_to_closing_brace (parser
))
10257 cp_lexer_consume_token (parser
->lexer
);
10260 /* The capture list was built up in reverse order; fix that now. */
10261 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10262 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10265 maybe_add_lambda_conv_op (type
);
10267 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10269 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10270 parser
->in_statement
= in_statement
;
10271 parser
->in_switch_statement_p
= in_switch_statement_p
;
10272 parser
->fully_implicit_function_template_p
10273 = fully_implicit_function_template_p
;
10274 parser
->implicit_template_parms
= implicit_template_parms
;
10275 parser
->implicit_template_scope
= implicit_template_scope
;
10276 parser
->auto_is_implicit_function_template_parm_p
10277 = auto_is_implicit_function_template_parm_p
;
10280 /* This field is only used during parsing of the lambda. */
10281 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10283 /* This lambda shouldn't have any proxies left at this point. */
10284 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10285 /* And now that we're done, push proxies for an enclosing lambda. */
10286 insert_pending_capture_proxies ();
10288 /* Update the lambda expression to a range. */
10289 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10290 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10292 end_tok
->location
);
10295 lambda_expr
= build_lambda_object (lambda_expr
);
10297 lambda_expr
= error_mark_node
;
10299 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10301 pop_deferring_access_checks ();
10303 return lambda_expr
;
10306 /* Parse the beginning of a lambda expression.
10309 [ lambda-capture [opt] ]
10311 LAMBDA_EXPR is the current representation of the lambda expression. */
10314 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10316 /* Need commas after the first capture. */
10319 /* Eat the leading `['. */
10320 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10322 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10324 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10325 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10326 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10327 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10329 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10331 cp_lexer_consume_token (parser
->lexer
);
10334 if (!(at_function_scope_p () || parsing_nsdmi ()))
10335 error ("non-local lambda expression cannot have a capture-default");
10338 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10340 cp_token
* capture_token
;
10342 tree capture_init_expr
;
10343 cp_id_kind idk
= CP_ID_KIND_NONE
;
10344 bool explicit_init_p
= false;
10346 enum capture_kind_type
10351 enum capture_kind_type capture_kind
= BY_COPY
;
10353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10355 error ("expected end of capture-list");
10362 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10364 /* Possibly capture `this'. */
10365 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10367 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10368 if (cxx_dialect
< cxx2a
10369 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10370 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10371 "with by-copy capture default");
10372 cp_lexer_consume_token (parser
->lexer
);
10373 add_capture (lambda_expr
,
10374 /*id=*/this_identifier
,
10375 /*initializer=*/finish_this_expr (),
10376 /*by_reference_p=*/true,
10381 /* Possibly capture `*this'. */
10382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10383 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10385 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10386 if (cxx_dialect
< cxx17
)
10387 pedwarn (loc
, 0, "%<*this%> capture only available with "
10388 "-std=c++17 or -std=gnu++17");
10389 cp_lexer_consume_token (parser
->lexer
);
10390 cp_lexer_consume_token (parser
->lexer
);
10391 add_capture (lambda_expr
,
10392 /*id=*/this_identifier
,
10393 /*initializer=*/finish_this_expr (),
10394 /*by_reference_p=*/false,
10399 bool init_pack_expansion
= false;
10400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10402 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10403 if (cxx_dialect
< cxx2a
)
10404 pedwarn (loc
, 0, "pack init-capture only available with "
10405 "-std=c++2a or -std=gnu++2a");
10406 cp_lexer_consume_token (parser
->lexer
);
10407 init_pack_expansion
= true;
10410 /* Remember whether we want to capture as a reference or not. */
10411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10413 capture_kind
= BY_REFERENCE
;
10414 cp_lexer_consume_token (parser
->lexer
);
10417 /* Get the identifier. */
10418 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10419 capture_id
= cp_parser_identifier (parser
);
10421 if (capture_id
== error_mark_node
)
10422 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10423 delimiters, but I modified this to stop on unnested ']' as well. It
10424 was already changed to stop on unnested '}', so the
10425 "closing_parenthesis" name is no more misleading with my change. */
10427 cp_parser_skip_to_closing_parenthesis (parser
,
10428 /*recovering=*/true,
10430 /*consume_paren=*/true);
10434 /* Find the initializer for this capture. */
10435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10436 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10437 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10439 bool direct
, non_constant
;
10440 /* An explicit initializer exists. */
10441 if (cxx_dialect
< cxx14
)
10442 pedwarn (input_location
, 0,
10443 "lambda capture initializers "
10444 "only available with -std=c++14 or -std=gnu++14");
10445 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10446 &non_constant
, true);
10447 explicit_init_p
= true;
10448 if (capture_init_expr
== NULL_TREE
)
10450 error ("empty initializer for lambda init-capture");
10451 capture_init_expr
= error_mark_node
;
10453 if (init_pack_expansion
)
10454 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10458 const char* error_msg
;
10460 /* Turn the identifier into an id-expression. */
10462 = cp_parser_lookup_name_simple (parser
, capture_id
,
10463 capture_token
->location
);
10465 if (capture_init_expr
== error_mark_node
)
10467 unqualified_name_lookup_error (capture_id
);
10470 else if (!VAR_P (capture_init_expr
)
10471 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10473 error_at (capture_token
->location
,
10474 "capture of non-variable %qE",
10475 capture_init_expr
);
10476 if (DECL_P (capture_init_expr
))
10477 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10478 "%q#D declared here", capture_init_expr
);
10481 if (VAR_P (capture_init_expr
)
10482 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10484 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10485 "%qD with non-automatic storage duration",
10486 capture_init_expr
))
10487 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10488 "%q#D declared here", capture_init_expr
);
10493 = finish_id_expression
10498 /*integral_constant_expression_p=*/false,
10499 /*allow_non_integral_constant_expression_p=*/false,
10500 /*non_integral_constant_expression_p=*/NULL
,
10501 /*template_p=*/false,
10503 /*address_p=*/false,
10504 /*template_arg_p=*/false,
10506 capture_token
->location
);
10508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10510 cp_lexer_consume_token (parser
->lexer
);
10511 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10515 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10516 && !explicit_init_p
)
10518 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10519 && capture_kind
== BY_COPY
)
10520 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10521 "of %qD redundant with by-copy capture default",
10523 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10524 && capture_kind
== BY_REFERENCE
)
10525 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10526 "capture of %qD redundant with by-reference capture "
10527 "default", capture_id
);
10530 add_capture (lambda_expr
,
10533 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10536 /* If there is any qualification still in effect, clear it
10537 now; we will be starting fresh with the next capture. */
10538 parser
->scope
= NULL_TREE
;
10539 parser
->qualifying_scope
= NULL_TREE
;
10540 parser
->object_scope
= NULL_TREE
;
10543 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10546 /* Parse the (optional) middle of a lambda expression.
10549 < template-parameter-list [opt] >
10550 ( parameter-declaration-clause [opt] )
10551 attribute-specifier [opt]
10552 decl-specifier-seq [opt]
10553 exception-specification [opt]
10554 lambda-return-type-clause [opt]
10556 LAMBDA_EXPR is the current representation of the lambda expression. */
10559 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10561 /* 5.1.1.4 of the standard says:
10562 If a lambda-expression does not include a lambda-declarator, it is as if
10563 the lambda-declarator were ().
10564 This means an empty parameter list, no attributes, and no exception
10566 tree param_list
= void_list_node
;
10567 tree attributes
= NULL_TREE
;
10568 tree exception_spec
= NULL_TREE
;
10569 tree template_param_list
= NULL_TREE
;
10570 tree tx_qual
= NULL_TREE
;
10571 tree return_type
= NULL_TREE
;
10572 cp_decl_specifier_seq lambda_specs
;
10573 clear_decl_specs (&lambda_specs
);
10575 /* The template-parameter-list is optional, but must begin with
10576 an opening angle if present. */
10577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10579 if (cxx_dialect
< cxx14
)
10580 pedwarn (parser
->lexer
->next_token
->location
, 0,
10581 "lambda templates are only available with "
10582 "-std=c++14 or -std=gnu++14");
10583 else if (cxx_dialect
< cxx2a
)
10584 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10585 "lambda templates are only available with "
10586 "-std=c++2a or -std=gnu++2a");
10588 cp_lexer_consume_token (parser
->lexer
);
10590 template_param_list
= cp_parser_template_parameter_list (parser
);
10592 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10594 /* We just processed one more parameter list. */
10595 ++parser
->num_template_parameter_lists
;
10598 /* The parameter-declaration-clause is optional (unless
10599 template-parameter-list was given), but must begin with an
10600 opening parenthesis if present. */
10601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10603 matching_parens parens
;
10604 parens
.consume_open (parser
);
10606 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10608 /* Parse parameters. */
10609 param_list
= cp_parser_parameter_declaration_clause (parser
);
10611 /* Default arguments shall not be specified in the
10612 parameter-declaration-clause of a lambda-declarator. */
10613 if (cxx_dialect
< cxx14
)
10614 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10615 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10616 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10617 "default argument specified for lambda parameter");
10619 parens
.require_close (parser
);
10621 /* In the decl-specifier-seq of the lambda-declarator, each
10622 decl-specifier shall either be mutable or constexpr. */
10623 int declares_class_or_enum
;
10624 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10625 cp_parser_decl_specifier_seq (parser
,
10626 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10627 &lambda_specs
, &declares_class_or_enum
);
10628 if (lambda_specs
.storage_class
== sc_mutable
)
10630 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10631 if (lambda_specs
.conflicting_specifiers_p
)
10632 error_at (lambda_specs
.locations
[ds_storage_class
],
10633 "duplicate %<mutable%>");
10636 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10638 /* Parse optional exception specification. */
10639 exception_spec
= cp_parser_exception_specification_opt (parser
);
10641 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10643 /* Parse optional trailing return type. */
10644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10646 cp_lexer_consume_token (parser
->lexer
);
10647 return_type
= cp_parser_trailing_type_id (parser
);
10650 /* The function parameters must be in scope all the way until after the
10651 trailing-return-type in case of decltype. */
10652 pop_bindings_and_leave_scope ();
10654 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10655 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10657 /* Create the function call operator.
10659 Messing with declarators like this is no uglier than building up the
10660 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10663 cp_decl_specifier_seq return_type_specs
;
10664 cp_declarator
* declarator
;
10669 clear_decl_specs (&return_type_specs
);
10670 return_type_specs
.type
= make_auto ();
10672 if (lambda_specs
.locations
[ds_constexpr
])
10674 if (cxx_dialect
>= cxx17
)
10675 return_type_specs
.locations
[ds_constexpr
]
10676 = lambda_specs
.locations
[ds_constexpr
];
10678 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10679 "lambda only available with -std=c++17 or -std=gnu++17");
10682 p
= obstack_alloc (&declarator_obstack
, 0);
10684 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10685 LAMBDA_EXPR_LOCATION (lambda_expr
));
10687 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10688 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10689 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10690 VIRT_SPEC_UNSPECIFIED
,
10695 /*requires_clause*/NULL_TREE
);
10696 declarator
->std_attributes
= attributes
;
10698 fco
= grokmethod (&return_type_specs
,
10701 if (fco
!= error_mark_node
)
10703 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10704 DECL_ARTIFICIAL (fco
) = 1;
10705 /* Give the object parameter a different name. */
10706 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10707 DECL_LAMBDA_FUNCTION (fco
) = 1;
10709 if (template_param_list
)
10711 fco
= finish_member_template_decl (fco
);
10712 finish_template_decl (template_param_list
);
10713 --parser
->num_template_parameter_lists
;
10715 else if (parser
->fully_implicit_function_template_p
)
10716 fco
= finish_fully_implicit_template (parser
, fco
);
10718 finish_member_declaration (fco
);
10720 obstack_free (&declarator_obstack
, p
);
10722 return (fco
!= error_mark_node
);
10726 /* Parse the body of a lambda expression, which is simply
10730 but which requires special handling.
10731 LAMBDA_EXPR is the current representation of the lambda expression. */
10734 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10736 bool nested
= (current_function_decl
!= NULL_TREE
);
10737 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10738 bool in_function_body
= parser
->in_function_body
;
10740 /* The body of a lambda-expression is not a subexpression of the enclosing
10745 push_function_context ();
10747 /* Still increment function_depth so that we don't GC in the
10748 middle of an expression. */
10751 vec
<tree
> omp_privatization_save
;
10752 save_omp_privatization_clauses (omp_privatization_save
);
10753 /* Clear this in case we're in the middle of a default argument. */
10754 parser
->local_variables_forbidden_p
= false;
10755 parser
->in_function_body
= true;
10758 local_specialization_stack
s (lss_copy
);
10759 tree fco
= lambda_function (lambda_expr
);
10760 tree body
= start_lambda_function (fco
, lambda_expr
);
10761 matching_braces braces
;
10763 if (braces
.require_open (parser
))
10765 tree compound_stmt
= begin_compound_stmt (0);
10767 /* Originally C++11 required us to peek for 'return expr'; and
10768 process it specially here to deduce the return type. N3638
10769 removed the need for that. */
10771 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10772 cp_parser_label_declaration (parser
);
10773 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10774 braces
.require_close (parser
);
10776 finish_compound_stmt (compound_stmt
);
10779 finish_lambda_function (body
);
10782 restore_omp_privatization_clauses (omp_privatization_save
);
10783 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10784 parser
->in_function_body
= in_function_body
;
10786 pop_function_context();
10791 /* Statements [gram.stmt.stmt] */
10793 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10796 add_debug_begin_stmt (location_t loc
)
10798 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10800 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10801 /* A concept is never expanded normally. */
10804 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10805 SET_EXPR_LOCATION (stmt
, loc
);
10809 /* Parse a statement.
10813 expression-statement
10815 selection-statement
10816 iteration-statement
10818 declaration-statement
10825 attribute-specifier-seq (opt) expression-statement
10826 attribute-specifier-seq (opt) compound-statement
10827 attribute-specifier-seq (opt) selection-statement
10828 attribute-specifier-seq (opt) iteration-statement
10829 attribute-specifier-seq (opt) jump-statement
10830 declaration-statement
10831 attribute-specifier-seq (opt) try-block
10834 expression-statement
10842 IN_COMPOUND is true when the statement is nested inside a
10843 cp_parser_compound_statement; this matters for certain pragmas.
10845 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10846 is a (possibly labeled) if statement which is not enclosed in braces
10847 and has an else clause. This is used to implement -Wparentheses.
10849 CHAIN is a vector of if-else-if conditions. */
10852 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10853 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10854 location_t
*loc_after_labels
)
10856 tree statement
, std_attrs
= NULL_TREE
;
10858 location_t statement_location
, attrs_location
;
10863 /* There is no statement yet. */
10864 statement
= NULL_TREE
;
10866 saved_token_sentinel
saved_tokens (parser
->lexer
);
10867 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10868 if (c_dialect_objc ())
10869 /* In obj-c++, seeing '[[' might be the either the beginning of
10870 c++11 attributes, or a nested objc-message-expression. So
10871 let's parse the c++11 attributes tentatively. */
10872 cp_parser_parse_tentatively (parser
);
10873 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10874 if (c_dialect_objc ())
10876 if (!cp_parser_parse_definitely (parser
))
10877 std_attrs
= NULL_TREE
;
10880 /* Peek at the next token. */
10881 token
= cp_lexer_peek_token (parser
->lexer
);
10882 /* Remember the location of the first token in the statement. */
10883 cp_token
*statement_token
= token
;
10884 statement_location
= token
->location
;
10885 add_debug_begin_stmt (statement_location
);
10886 /* If this is a keyword, then that will often determine what kind of
10887 statement we have. */
10888 if (token
->type
== CPP_KEYWORD
)
10890 enum rid keyword
= token
->keyword
;
10896 /* Looks like a labeled-statement with a case label.
10897 Parse the label, and then use tail recursion to parse
10899 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10900 in_compound
= false;
10905 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10906 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10912 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10913 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10920 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10921 statement
= cp_parser_jump_statement (parser
);
10924 /* Objective-C++ exception-handling constructs. */
10927 case RID_AT_FINALLY
:
10928 case RID_AT_SYNCHRONIZED
:
10930 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10931 statement
= cp_parser_objc_statement (parser
);
10935 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10936 statement
= cp_parser_try_block (parser
);
10939 case RID_NAMESPACE
:
10940 /* This must be a namespace alias definition. */
10941 if (std_attrs
!= NULL_TREE
)
10943 /* Attributes should be parsed as part of the the
10944 declaration, so let's un-parse them. */
10945 saved_tokens
.rollback();
10946 std_attrs
= NULL_TREE
;
10948 cp_parser_declaration_statement (parser
);
10951 case RID_TRANSACTION_ATOMIC
:
10952 case RID_TRANSACTION_RELAXED
:
10953 case RID_SYNCHRONIZED
:
10954 case RID_ATOMIC_NOEXCEPT
:
10955 case RID_ATOMIC_CANCEL
:
10956 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10957 statement
= cp_parser_transaction (parser
, token
);
10959 case RID_TRANSACTION_CANCEL
:
10960 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
10961 statement
= cp_parser_transaction_cancel (parser
);
10965 /* It might be a keyword like `int' that can start a
10966 declaration-statement. */
10970 else if (token
->type
== CPP_NAME
)
10972 /* If the next token is a `:', then we are looking at a
10973 labeled-statement. */
10974 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10975 if (token
->type
== CPP_COLON
)
10977 /* Looks like a labeled-statement with an ordinary label.
10978 Parse the label, and then use tail recursion to parse
10981 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10982 in_compound
= false;
10986 /* Anything that starts with a `{' must be a compound-statement. */
10987 else if (token
->type
== CPP_OPEN_BRACE
)
10988 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10989 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10990 a statement all its own. */
10991 else if (token
->type
== CPP_PRAGMA
)
10993 /* Only certain OpenMP pragmas are attached to statements, and thus
10994 are considered statements themselves. All others are not. In
10995 the context of a compound, accept the pragma as a "statement" and
10996 return so that we can check for a close brace. Otherwise we
10997 require a real statement and must go back and read one. */
10999 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11000 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11004 else if (token
->type
== CPP_EOF
)
11006 cp_parser_error (parser
, "expected statement");
11010 /* Everything else must be a declaration-statement or an
11011 expression-statement. Try for the declaration-statement
11012 first, unless we are looking at a `;', in which case we know that
11013 we have an expression-statement. */
11016 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11018 if (std_attrs
!= NULL_TREE
)
11019 /* Attributes should be parsed as part of the declaration,
11020 so let's un-parse them. */
11021 saved_tokens
.rollback();
11023 cp_parser_parse_tentatively (parser
);
11024 /* Try to parse the declaration-statement. */
11025 cp_parser_declaration_statement (parser
);
11026 /* If that worked, we're done. */
11027 if (cp_parser_parse_definitely (parser
))
11029 /* It didn't work, restore the post-attribute position. */
11031 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11033 /* All preceding labels have been parsed at this point. */
11034 if (loc_after_labels
!= NULL
)
11035 *loc_after_labels
= statement_location
;
11037 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11039 /* Look for an expression-statement instead. */
11040 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11042 /* Handle [[fallthrough]];. */
11043 if (attribute_fallthrough_p (std_attrs
))
11045 /* The next token after the fallthrough attribute is ';'. */
11046 if (statement
== NULL_TREE
)
11048 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11049 statement
= build_call_expr_internal_loc (statement_location
,
11051 void_type_node
, 0);
11052 finish_expr_stmt (statement
);
11055 warning_at (statement_location
, OPT_Wattributes
,
11056 "%<fallthrough%> attribute not followed by %<;%>");
11057 std_attrs
= NULL_TREE
;
11061 /* Set the line number for the statement. */
11062 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11063 SET_EXPR_LOCATION (statement
, statement_location
);
11065 /* Allow "[[fallthrough]];", but warn otherwise. */
11066 if (std_attrs
!= NULL_TREE
)
11067 warning_at (attrs_location
,
11069 "attributes at the beginning of statement are ignored");
11072 /* Append ATTR to attribute list ATTRS. */
11075 attr_chainon (tree attrs
, tree attr
)
11077 if (attrs
== error_mark_node
)
11078 return error_mark_node
;
11079 if (attr
== error_mark_node
)
11080 return error_mark_node
;
11081 return chainon (attrs
, attr
);
11084 /* Parse the label for a labeled-statement, i.e.
11087 case constant-expression :
11091 case constant-expression ... constant-expression : statement
11093 When a label is parsed without errors, the label is added to the
11094 parse tree by the finish_* functions, so this function doesn't
11095 have to return the label. */
11098 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11101 tree label
= NULL_TREE
;
11102 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11104 /* The next token should be an identifier. */
11105 token
= cp_lexer_peek_token (parser
->lexer
);
11106 if (token
->type
!= CPP_NAME
11107 && token
->type
!= CPP_KEYWORD
)
11109 cp_parser_error (parser
, "expected labeled-statement");
11113 /* Remember whether this case or a user-defined label is allowed to fall
11115 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11117 parser
->colon_corrects_to_scope_p
= false;
11118 switch (token
->keyword
)
11122 tree expr
, expr_hi
;
11123 cp_token
*ellipsis
;
11125 /* Consume the `case' token. */
11126 cp_lexer_consume_token (parser
->lexer
);
11127 /* Parse the constant-expression. */
11128 expr
= cp_parser_constant_expression (parser
);
11129 if (check_for_bare_parameter_packs (expr
))
11130 expr
= error_mark_node
;
11132 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11133 if (ellipsis
->type
== CPP_ELLIPSIS
)
11135 /* Consume the `...' token. */
11136 cp_lexer_consume_token (parser
->lexer
);
11137 expr_hi
= cp_parser_constant_expression (parser
);
11138 if (check_for_bare_parameter_packs (expr_hi
))
11139 expr_hi
= error_mark_node
;
11141 /* We don't need to emit warnings here, as the common code
11142 will do this for us. */
11145 expr_hi
= NULL_TREE
;
11147 if (parser
->in_switch_statement_p
)
11149 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11150 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11152 label
= CASE_LABEL (l
);
11153 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11157 error_at (token
->location
,
11158 "case label %qE not within a switch statement",
11164 /* Consume the `default' token. */
11165 cp_lexer_consume_token (parser
->lexer
);
11167 if (parser
->in_switch_statement_p
)
11169 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11170 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11172 label
= CASE_LABEL (l
);
11173 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11177 error_at (token
->location
, "case label not within a switch statement");
11181 /* Anything else must be an ordinary label. */
11182 label
= finish_label_stmt (cp_parser_identifier (parser
));
11183 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11184 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11188 /* Require the `:' token. */
11189 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11191 /* An ordinary label may optionally be followed by attributes.
11192 However, this is only permitted if the attributes are then
11193 followed by a semicolon. This is because, for backward
11194 compatibility, when parsing
11195 lab: __attribute__ ((unused)) int i;
11196 we want the attribute to attach to "i", not "lab". */
11197 if (label
!= NULL_TREE
11198 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11201 cp_parser_parse_tentatively (parser
);
11202 attrs
= cp_parser_gnu_attributes_opt (parser
);
11203 if (attrs
== NULL_TREE
11204 /* And fallthrough always binds to the expression-statement. */
11205 || attribute_fallthrough_p (attrs
)
11206 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11207 cp_parser_abort_tentative_parse (parser
);
11208 else if (!cp_parser_parse_definitely (parser
))
11211 attributes
= attr_chainon (attributes
, attrs
);
11214 if (attributes
!= NULL_TREE
)
11215 cplus_decl_attributes (&label
, attributes
, 0);
11217 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11220 /* Parse an expression-statement.
11222 expression-statement:
11225 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11226 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11227 indicates whether this expression-statement is part of an
11228 expression statement. */
11231 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11233 tree statement
= NULL_TREE
;
11234 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11235 location_t loc
= token
->location
;
11237 /* There might be attribute fallthrough. */
11238 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11240 /* If the next token is a ';', then there is no expression
11242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11244 statement
= cp_parser_expression (parser
);
11245 if (statement
== error_mark_node
11246 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11248 cp_parser_skip_to_end_of_block_or_statement (parser
);
11249 return error_mark_node
;
11253 /* Handle [[fallthrough]];. */
11254 if (attribute_fallthrough_p (attr
))
11256 /* The next token after the fallthrough attribute is ';'. */
11257 if (statement
== NULL_TREE
)
11258 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11259 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11260 void_type_node
, 0);
11262 warning_at (loc
, OPT_Wattributes
,
11263 "%<fallthrough%> attribute not followed by %<;%>");
11267 /* Allow "[[fallthrough]];", but warn otherwise. */
11268 if (attr
!= NULL_TREE
)
11269 warning_at (loc
, OPT_Wattributes
,
11270 "attributes at the beginning of statement are ignored");
11272 /* Give a helpful message for "A<T>::type t;" and the like. */
11273 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11274 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11276 if (TREE_CODE (statement
) == SCOPE_REF
)
11277 error_at (token
->location
, "need %<typename%> before %qE because "
11278 "%qT is a dependent scope",
11279 statement
, TREE_OPERAND (statement
, 0));
11280 else if (is_overloaded_fn (statement
)
11281 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11284 tree fn
= get_first_fn (statement
);
11285 error_at (token
->location
,
11286 "%<%T::%D%> names the constructor, not the type",
11287 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11291 /* Consume the final `;'. */
11292 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11294 if (in_statement_expr
11295 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11296 /* This is the final expression statement of a statement
11298 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11299 else if (statement
)
11300 statement
= finish_expr_stmt (statement
);
11305 /* Parse a compound-statement.
11307 compound-statement:
11308 { statement-seq [opt] }
11312 compound-statement:
11313 { label-declaration-seq [opt] statement-seq [opt] }
11315 label-declaration-seq:
11317 label-declaration-seq label-declaration
11319 Returns a tree representing the statement. */
11322 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11323 int bcs_flags
, bool function_body
)
11325 tree compound_stmt
;
11326 matching_braces braces
;
11328 /* Consume the `{'. */
11329 if (!braces
.require_open (parser
))
11330 return error_mark_node
;
11331 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11332 && !function_body
&& cxx_dialect
< cxx14
)
11333 pedwarn (input_location
, OPT_Wpedantic
,
11334 "compound-statement in %<constexpr%> function");
11335 /* Begin the compound-statement. */
11336 compound_stmt
= begin_compound_stmt (bcs_flags
);
11337 /* If the next keyword is `__label__' we have a label declaration. */
11338 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11339 cp_parser_label_declaration (parser
);
11340 /* Parse an (optional) statement-seq. */
11341 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11342 /* Finish the compound-statement. */
11343 finish_compound_stmt (compound_stmt
);
11344 /* Consume the `}'. */
11345 braces
.require_close (parser
);
11347 return compound_stmt
;
11350 /* Parse an (optional) statement-seq.
11354 statement-seq [opt] statement */
11357 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11359 /* Scan statements until there aren't any more. */
11362 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11364 /* If we are looking at a `}', then we have run out of
11365 statements; the same is true if we have reached the end
11366 of file, or have stumbled upon a stray '@end'. */
11367 if (token
->type
== CPP_CLOSE_BRACE
11368 || token
->type
== CPP_EOF
11369 || token
->type
== CPP_PRAGMA_EOL
11370 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11373 /* If we are in a compound statement and find 'else' then
11374 something went wrong. */
11375 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11377 if (parser
->in_statement
& IN_IF_STMT
)
11381 token
= cp_lexer_consume_token (parser
->lexer
);
11382 error_at (token
->location
, "%<else%> without a previous %<if%>");
11386 /* Parse the statement. */
11387 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11391 /* Return true if this is the C++20 version of range-based-for with
11395 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11399 /* Save tokens so that we can put them back. */
11400 cp_lexer_save_tokens (parser
->lexer
);
11402 /* There has to be an unnested ; followed by an unnested :. */
11403 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11404 /*recovering=*/false,
11406 /*consume_paren=*/false) != -1)
11409 /* We found the semicolon, eat it now. */
11410 cp_lexer_consume_token (parser
->lexer
);
11412 /* Now look for ':' that is not nested in () or {}. */
11413 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11414 /*recovering=*/false,
11416 /*consume_paren=*/false) == -1);
11419 /* Roll back the tokens we skipped. */
11420 cp_lexer_rollback_tokens (parser
->lexer
);
11425 /* Return true if we're looking at (init; cond), false otherwise. */
11428 cp_parser_init_statement_p (cp_parser
*parser
)
11430 /* Save tokens so that we can put them back. */
11431 cp_lexer_save_tokens (parser
->lexer
);
11433 /* Look for ';' that is not nested in () or {}. */
11434 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11435 /*recovering=*/false,
11437 /*consume_paren=*/false);
11439 /* Roll back the tokens we skipped. */
11440 cp_lexer_rollback_tokens (parser
->lexer
);
11445 /* Parse a selection-statement.
11447 selection-statement:
11448 if ( init-statement [opt] condition ) statement
11449 if ( init-statement [opt] condition ) statement else statement
11450 switch ( init-statement [opt] condition ) statement
11452 Returns the new IF_STMT or SWITCH_STMT.
11454 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11455 is a (possibly labeled) if statement which is not enclosed in
11456 braces and has an else clause. This is used to implement
11459 CHAIN is a vector of if-else-if conditions. This is used to implement
11460 -Wduplicated-cond. */
11463 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11468 token_indent_info guard_tinfo
;
11473 /* Peek at the next token. */
11474 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11475 guard_tinfo
= get_token_indent_info (token
);
11477 /* See what kind of keyword it is. */
11478 keyword
= token
->keyword
;
11488 if (keyword
== RID_IF
11489 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11493 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11494 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11495 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11496 "with -std=c++17 or -std=gnu++17");
11499 /* Look for the `('. */
11500 matching_parens parens
;
11501 if (!parens
.require_open (parser
))
11503 cp_parser_skip_to_end_of_statement (parser
);
11504 return error_mark_node
;
11507 /* Begin the selection-statement. */
11508 if (keyword
== RID_IF
)
11510 statement
= begin_if_stmt ();
11511 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11514 statement
= begin_switch_stmt ();
11516 /* Parse the optional init-statement. */
11517 if (cp_parser_init_statement_p (parser
))
11520 if (cxx_dialect
< cxx17
)
11521 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11522 "init-statement in selection statements only available "
11523 "with -std=c++17 or -std=gnu++17");
11524 cp_parser_init_statement (parser
, &decl
);
11527 /* Parse the condition. */
11528 condition
= cp_parser_condition (parser
);
11529 /* Look for the `)'. */
11530 if (!parens
.require_close (parser
))
11531 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11532 /*consume_paren=*/true);
11534 if (keyword
== RID_IF
)
11537 unsigned char in_statement
;
11539 /* Add the condition. */
11540 condition
= finish_if_stmt_cond (condition
, statement
);
11542 if (warn_duplicated_cond
)
11543 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11546 /* Parse the then-clause. */
11547 in_statement
= parser
->in_statement
;
11548 parser
->in_statement
|= IN_IF_STMT
;
11550 /* Outside a template, the non-selected branch of a constexpr
11551 if is a 'discarded statement', i.e. unevaluated. */
11552 bool was_discarded
= in_discarded_stmt
;
11553 bool discard_then
= (cx
&& !processing_template_decl
11554 && integer_zerop (condition
));
11557 in_discarded_stmt
= true;
11558 ++c_inhibit_evaluation_warnings
;
11561 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11564 parser
->in_statement
= in_statement
;
11566 finish_then_clause (statement
);
11570 THEN_CLAUSE (statement
) = NULL_TREE
;
11571 in_discarded_stmt
= was_discarded
;
11572 --c_inhibit_evaluation_warnings
;
11575 /* If the next token is `else', parse the else-clause. */
11576 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11579 bool discard_else
= (cx
&& !processing_template_decl
11580 && integer_nonzerop (condition
));
11583 in_discarded_stmt
= true;
11584 ++c_inhibit_evaluation_warnings
;
11588 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11589 /* Consume the `else' keyword. */
11590 cp_lexer_consume_token (parser
->lexer
);
11591 if (warn_duplicated_cond
)
11593 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11597 /* We've got "if (COND) else if (COND2)". Start
11598 the condition chain and add COND as the first
11600 chain
= new vec
<tree
> ();
11601 if (!CONSTANT_CLASS_P (condition
)
11602 && !TREE_SIDE_EFFECTS (condition
))
11604 /* Wrap it in a NOP_EXPR so that we can set the
11605 location of the condition. */
11606 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11608 SET_EXPR_LOCATION (e
, token
->location
);
11609 chain
->safe_push (e
);
11612 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11615 /* This is if-else without subsequent if. Zap the
11616 condition chain; we would have already warned at
11622 begin_else_clause (statement
);
11623 /* Parse the else-clause. */
11624 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11625 guard_tinfo
, chain
);
11627 finish_else_clause (statement
);
11629 /* If we are currently parsing a then-clause, then
11630 IF_P will not be NULL. We set it to true to
11631 indicate that this if statement has an else clause.
11632 This may trigger the Wparentheses warning below
11633 when we get back up to the parent if statement. */
11639 ELSE_CLAUSE (statement
) = NULL_TREE
;
11640 in_discarded_stmt
= was_discarded
;
11641 --c_inhibit_evaluation_warnings
;
11646 /* This if statement does not have an else clause. If
11647 NESTED_IF is true, then the then-clause has an if
11648 statement which does have an else clause. We warn
11649 about the potential ambiguity. */
11651 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11652 "suggest explicit braces to avoid ambiguous"
11654 if (warn_duplicated_cond
)
11656 /* We don't need the condition chain anymore. */
11662 /* Now we're all done with the if-statement. */
11663 finish_if_stmt (statement
);
11667 bool in_switch_statement_p
;
11668 unsigned char in_statement
;
11670 /* Add the condition. */
11671 finish_switch_cond (condition
, statement
);
11673 /* Parse the body of the switch-statement. */
11674 in_switch_statement_p
= parser
->in_switch_statement_p
;
11675 in_statement
= parser
->in_statement
;
11676 parser
->in_switch_statement_p
= true;
11677 parser
->in_statement
|= IN_SWITCH_STMT
;
11678 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11680 parser
->in_switch_statement_p
= in_switch_statement_p
;
11681 parser
->in_statement
= in_statement
;
11683 /* Now we're all done with the switch-statement. */
11684 finish_switch_stmt (statement
);
11692 cp_parser_error (parser
, "expected selection-statement");
11693 return error_mark_node
;
11697 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11698 If we have seen at least one decl-specifier, and the next token
11699 is not a parenthesis, then we must be looking at a declaration.
11700 (After "int (" we might be looking at a functional cast.) */
11703 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11704 bool any_specifiers_p
)
11706 if (any_specifiers_p
11707 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11708 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11709 && !cp_parser_error_occurred (parser
))
11710 cp_parser_commit_to_tentative_parse (parser
);
11713 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11714 The declarator shall not specify a function or an array. Returns
11715 TRUE if the declarator is valid, FALSE otherwise. */
11718 cp_parser_check_condition_declarator (cp_parser
* parser
,
11719 cp_declarator
*declarator
,
11722 if (declarator
== cp_error_declarator
11723 || function_declarator_p (declarator
)
11724 || declarator
->kind
== cdk_array
)
11726 if (declarator
== cp_error_declarator
)
11727 /* Already complained. */;
11728 else if (declarator
->kind
== cdk_array
)
11729 error_at (loc
, "condition declares an array");
11731 error_at (loc
, "condition declares a function");
11732 if (parser
->fully_implicit_function_template_p
)
11733 abort_fully_implicit_template (parser
);
11734 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11735 /*or_comma=*/false,
11736 /*consume_paren=*/false);
11743 /* Parse a condition.
11747 type-specifier-seq declarator = initializer-clause
11748 type-specifier-seq declarator braced-init-list
11753 type-specifier-seq declarator asm-specification [opt]
11754 attributes [opt] = assignment-expression
11756 Returns the expression that should be tested. */
11759 cp_parser_condition (cp_parser
* parser
)
11761 cp_decl_specifier_seq type_specifiers
;
11762 const char *saved_message
;
11763 int declares_class_or_enum
;
11765 /* Try the declaration first. */
11766 cp_parser_parse_tentatively (parser
);
11767 /* New types are not allowed in the type-specifier-seq for a
11769 saved_message
= parser
->type_definition_forbidden_message
;
11770 parser
->type_definition_forbidden_message
11771 = G_("types may not be defined in conditions");
11772 /* Parse the type-specifier-seq. */
11773 cp_parser_decl_specifier_seq (parser
,
11774 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11776 &declares_class_or_enum
);
11777 /* Restore the saved message. */
11778 parser
->type_definition_forbidden_message
= saved_message
;
11780 cp_parser_maybe_commit_to_declaration (parser
,
11781 type_specifiers
.any_specifiers_p
);
11783 /* If all is well, we might be looking at a declaration. */
11784 if (!cp_parser_error_occurred (parser
))
11787 tree asm_specification
;
11789 cp_declarator
*declarator
;
11790 tree initializer
= NULL_TREE
;
11791 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11793 /* Parse the declarator. */
11794 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11795 /*ctor_dtor_or_conv_p=*/NULL
,
11796 /*parenthesized_p=*/NULL
,
11797 /*member_p=*/false,
11798 /*friend_p=*/false);
11799 /* Parse the attributes. */
11800 attributes
= cp_parser_attributes_opt (parser
);
11801 /* Parse the asm-specification. */
11802 asm_specification
= cp_parser_asm_specification_opt (parser
);
11803 /* If the next token is not an `=' or '{', then we might still be
11804 looking at an expression. For example:
11808 looks like a decl-specifier-seq and a declarator -- but then
11809 there is no `=', so this is an expression. */
11810 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11811 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11812 cp_parser_simulate_error (parser
);
11814 /* If we did see an `=' or '{', then we are looking at a declaration
11816 if (cp_parser_parse_definitely (parser
))
11819 bool non_constant_p
= false;
11820 int flags
= LOOKUP_ONLYCONVERTING
;
11822 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11823 return error_mark_node
;
11825 /* Create the declaration. */
11826 decl
= start_decl (declarator
, &type_specifiers
,
11827 /*initialized_p=*/true,
11828 attributes
, /*prefix_attributes=*/NULL_TREE
,
11831 /* Parse the initializer. */
11832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11834 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11835 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11838 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11840 /* Consume the `='. */
11841 cp_lexer_consume_token (parser
->lexer
);
11842 initializer
= cp_parser_initializer_clause (parser
,
11847 cp_parser_error (parser
, "expected initializer");
11848 initializer
= error_mark_node
;
11850 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11851 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11853 /* Process the initializer. */
11854 cp_finish_decl (decl
,
11855 initializer
, !non_constant_p
,
11860 pop_scope (pushed_scope
);
11862 return convert_from_reference (decl
);
11865 /* If we didn't even get past the declarator successfully, we are
11866 definitely not looking at a declaration. */
11868 cp_parser_abort_tentative_parse (parser
);
11870 /* Otherwise, we are looking at an expression. */
11871 return cp_parser_expression (parser
);
11874 /* Parses a for-statement or range-for-statement until the closing ')',
11878 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11880 tree init
, scope
, decl
;
11883 /* Begin the for-statement. */
11884 scope
= begin_for_scope (&init
);
11886 /* Parse the initialization. */
11887 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11890 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
11893 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11897 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11898 unsigned short unroll
)
11900 /* Normal for loop */
11901 tree condition
= NULL_TREE
;
11902 tree expression
= NULL_TREE
;
11905 stmt
= begin_for_stmt (scope
, init
);
11906 /* The init-statement has already been parsed in
11907 cp_parser_init_statement, so no work is needed here. */
11908 finish_init_stmt (stmt
);
11910 /* If there's a condition, process it. */
11911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11912 condition
= cp_parser_condition (parser
);
11915 cp_parser_error (parser
, "missing loop condition in loop with "
11916 "%<GCC ivdep%> pragma");
11917 condition
= error_mark_node
;
11921 cp_parser_error (parser
, "missing loop condition in loop with "
11922 "%<GCC unroll%> pragma");
11923 condition
= error_mark_node
;
11925 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11926 /* Look for the `;'. */
11927 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11929 /* If there's an expression, process it. */
11930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11931 expression
= cp_parser_expression (parser
);
11932 finish_for_expr (expression
, stmt
);
11937 /* Tries to parse a range-based for-statement:
11940 decl-specifier-seq declarator : expression
11942 The decl-specifier-seq declarator and the `:' are already parsed by
11943 cp_parser_init_statement. If processing_template_decl it returns a
11944 newly created RANGE_FOR_STMT; if not, it is converted to a
11945 regular FOR_STMT. */
11948 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11949 bool ivdep
, unsigned short unroll
, bool is_omp
)
11951 tree stmt
, range_expr
;
11952 auto_vec
<cxx_binding
*, 16> bindings
;
11953 auto_vec
<tree
, 16> names
;
11954 tree decomp_first_name
= NULL_TREE
;
11955 unsigned int decomp_cnt
= 0;
11957 /* Get the range declaration momentarily out of the way so that
11958 the range expression doesn't clash with it. */
11959 if (range_decl
!= error_mark_node
)
11961 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11963 tree v
= DECL_VALUE_EXPR (range_decl
);
11964 /* For decomposition declaration get all of the corresponding
11965 declarations out of the way. */
11966 if (TREE_CODE (v
) == ARRAY_REF
11967 && VAR_P (TREE_OPERAND (v
, 0))
11968 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11970 tree d
= range_decl
;
11971 range_decl
= TREE_OPERAND (v
, 0);
11972 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11973 decomp_first_name
= d
;
11974 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11976 tree name
= DECL_NAME (d
);
11977 names
.safe_push (name
);
11978 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11979 IDENTIFIER_BINDING (name
)
11980 = IDENTIFIER_BINDING (name
)->previous
;
11984 if (names
.is_empty ())
11986 tree name
= DECL_NAME (range_decl
);
11987 names
.safe_push (name
);
11988 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11989 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11995 bool expr_non_constant_p
;
11996 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11999 range_expr
= cp_parser_expression (parser
);
12001 /* Put the range declaration(s) back into scope. */
12002 for (unsigned int i
= 0; i
< names
.length (); i
++)
12004 cxx_binding
*binding
= bindings
[i
];
12005 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12006 IDENTIFIER_BINDING (names
[i
]) = binding
;
12009 /* finish_omp_for has its own code for the following, so just
12010 return the range_expr instead. */
12014 /* If in template, STMT is converted to a normal for-statement
12015 at instantiation. If not, it is done just ahead. */
12016 if (processing_template_decl
)
12018 if (check_for_bare_parameter_packs (range_expr
))
12019 range_expr
= error_mark_node
;
12020 stmt
= begin_range_for_stmt (scope
, init
);
12022 RANGE_FOR_IVDEP (stmt
) = 1;
12024 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12025 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12026 if (!type_dependent_expression_p (range_expr
)
12027 /* do_auto_deduction doesn't mess with template init-lists. */
12028 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12029 do_range_for_auto_deduction (range_decl
, range_expr
);
12033 stmt
= begin_for_stmt (scope
, init
);
12034 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12035 decomp_first_name
, decomp_cnt
, ivdep
,
12041 /* Subroutine of cp_convert_range_for: given the initializer expression,
12042 builds up the range temporary. */
12045 build_range_temp (tree range_expr
)
12047 tree range_type
, range_temp
;
12049 /* Find out the type deduced by the declaration
12050 `auto &&__range = range_expr'. */
12051 range_type
= cp_build_reference_type (make_auto (), true);
12052 range_type
= do_auto_deduction (range_type
, range_expr
,
12053 type_uses_auto (range_type
));
12055 /* Create the __range variable. */
12056 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12058 TREE_USED (range_temp
) = 1;
12059 DECL_ARTIFICIAL (range_temp
) = 1;
12064 /* Used by cp_parser_range_for in template context: we aren't going to
12065 do a full conversion yet, but we still need to resolve auto in the
12066 type of the for-range-declaration if present. This is basically
12067 a shortcut version of cp_convert_range_for. */
12070 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12072 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12075 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12076 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12077 iter_type
= (cp_parser_perform_range_for_lookup
12078 (range_temp
, &begin_dummy
, &end_dummy
));
12081 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12083 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12085 tf_warning_or_error
);
12086 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12087 iter_decl
, auto_node
);
12092 /* Converts a range-based for-statement into a normal
12093 for-statement, as per the definition.
12095 for (RANGE_DECL : RANGE_EXPR)
12098 should be equivalent to:
12101 auto &&__range = RANGE_EXPR;
12102 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12106 RANGE_DECL = *__begin;
12111 If RANGE_EXPR is an array:
12112 BEGIN_EXPR = __range
12113 END_EXPR = __range + ARRAY_SIZE(__range)
12114 Else if RANGE_EXPR has a member 'begin' or 'end':
12115 BEGIN_EXPR = __range.begin()
12116 END_EXPR = __range.end()
12118 BEGIN_EXPR = begin(__range)
12119 END_EXPR = end(__range);
12121 If __range has a member 'begin' but not 'end', or vice versa, we must
12122 still use the second alternative (it will surely fail, however).
12123 When calling begin()/end() in the third alternative we must use
12124 argument dependent lookup, but always considering 'std' as an associated
12128 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12129 tree decomp_first_name
, unsigned int decomp_cnt
,
12130 bool ivdep
, unsigned short unroll
)
12133 tree iter_type
, begin_expr
, end_expr
;
12134 tree condition
, expression
;
12136 range_expr
= mark_lvalue_use (range_expr
);
12138 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12139 /* If an error happened previously do nothing or else a lot of
12140 unhelpful errors would be issued. */
12141 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12146 if (VAR_P (range_expr
)
12147 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12148 /* Can't bind a reference to an array of runtime bound. */
12149 range_temp
= range_expr
;
12152 range_temp
= build_range_temp (range_expr
);
12153 pushdecl (range_temp
);
12154 cp_finish_decl (range_temp
, range_expr
,
12155 /*is_constant_init*/false, NULL_TREE
,
12156 LOOKUP_ONLYCONVERTING
);
12157 range_temp
= convert_from_reference (range_temp
);
12159 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12160 &begin_expr
, &end_expr
);
12163 /* The new for initialization statement. */
12164 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12166 TREE_USED (begin
) = 1;
12167 DECL_ARTIFICIAL (begin
) = 1;
12169 cp_finish_decl (begin
, begin_expr
,
12170 /*is_constant_init*/false, NULL_TREE
,
12171 LOOKUP_ONLYCONVERTING
);
12173 if (cxx_dialect
>= cxx17
)
12174 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12175 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12176 TREE_USED (end
) = 1;
12177 DECL_ARTIFICIAL (end
) = 1;
12179 cp_finish_decl (end
, end_expr
,
12180 /*is_constant_init*/false, NULL_TREE
,
12181 LOOKUP_ONLYCONVERTING
);
12183 finish_init_stmt (statement
);
12185 /* The new for condition. */
12186 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12189 NULL
, tf_warning_or_error
);
12190 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12192 /* The new increment expression. */
12193 expression
= finish_unary_op_expr (input_location
,
12194 PREINCREMENT_EXPR
, begin
,
12195 tf_warning_or_error
);
12196 finish_for_expr (expression
, statement
);
12198 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12199 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12201 /* The declaration is initialized with *__begin inside the loop body. */
12202 cp_finish_decl (range_decl
,
12203 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12204 tf_warning_or_error
),
12205 /*is_constant_init*/false, NULL_TREE
,
12206 LOOKUP_ONLYCONVERTING
);
12207 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12208 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12213 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12214 We need to solve both at the same time because the method used
12215 depends on the existence of members begin or end.
12216 Returns the type deduced for the iterator expression. */
12219 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12221 if (error_operand_p (range
))
12223 *begin
= *end
= error_mark_node
;
12224 return error_mark_node
;
12227 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12229 error ("range-based %<for%> expression of type %qT "
12230 "has incomplete type", TREE_TYPE (range
));
12231 *begin
= *end
= error_mark_node
;
12232 return error_mark_node
;
12234 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12236 /* If RANGE is an array, we will use pointer arithmetic. */
12237 *begin
= decay_conversion (range
, tf_warning_or_error
);
12238 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12240 array_type_nelts_top (TREE_TYPE (range
)),
12242 return TREE_TYPE (*begin
);
12246 /* If it is not an array, we must do a bit of magic. */
12247 tree id_begin
, id_end
;
12248 tree member_begin
, member_end
;
12250 *begin
= *end
= error_mark_node
;
12252 id_begin
= get_identifier ("begin");
12253 id_end
= get_identifier ("end");
12254 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12255 /*protect=*/2, /*want_type=*/false,
12256 tf_warning_or_error
);
12257 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12258 /*protect=*/2, /*want_type=*/false,
12259 tf_warning_or_error
);
12261 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12263 /* Use the member functions. */
12264 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12265 *end
= cp_parser_range_for_member_function (range
, id_end
);
12269 /* Use global functions with ADL. */
12270 vec
<tree
, va_gc
> *vec
;
12271 vec
= make_tree_vector ();
12273 vec_safe_push (vec
, range
);
12275 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12276 tf_warning_or_error
);
12277 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12278 tf_warning_or_error
);
12279 member_end
= perform_koenig_lookup (id_end
, vec
,
12280 tf_warning_or_error
);
12281 *end
= finish_call_expr (member_end
, &vec
, false, true,
12282 tf_warning_or_error
);
12284 release_tree_vector (vec
);
12287 /* Last common checks. */
12288 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12290 /* If one of the expressions is an error do no more checks. */
12291 *begin
= *end
= error_mark_node
;
12292 return error_mark_node
;
12294 else if (type_dependent_expression_p (*begin
)
12295 || type_dependent_expression_p (*end
))
12296 /* Can happen, when, eg, in a template context, Koenig lookup
12297 can't resolve begin/end (c++/58503). */
12301 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12302 /* The unqualified type of the __begin and __end temporaries should
12303 be the same, as required by the multiple auto declaration. */
12304 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12306 if (cxx_dialect
>= cxx17
12307 && (build_x_binary_op (input_location
, NE_EXPR
,
12308 *begin
, ERROR_MARK
,
12311 != error_mark_node
))
12312 /* P0184R0 allows __begin and __end to have different types,
12313 but make sure they are comparable so we can give a better
12316 error ("inconsistent begin/end types in range-based %<for%> "
12317 "statement: %qT and %qT",
12318 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12325 /* Helper function for cp_parser_perform_range_for_lookup.
12326 Builds a tree for RANGE.IDENTIFIER(). */
12329 cp_parser_range_for_member_function (tree range
, tree identifier
)
12332 vec
<tree
, va_gc
> *vec
;
12334 member
= finish_class_member_access_expr (range
, identifier
,
12335 false, tf_warning_or_error
);
12336 if (member
== error_mark_node
)
12337 return error_mark_node
;
12339 vec
= make_tree_vector ();
12340 res
= finish_call_expr (member
, &vec
,
12341 /*disallow_virtual=*/false,
12342 /*koenig_p=*/false,
12343 tf_warning_or_error
);
12344 release_tree_vector (vec
);
12348 /* Parse an iteration-statement.
12350 iteration-statement:
12351 while ( condition ) statement
12352 do statement while ( expression ) ;
12353 for ( init-statement condition [opt] ; expression [opt] )
12356 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12359 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12360 unsigned short unroll
)
12365 unsigned char in_statement
;
12366 token_indent_info guard_tinfo
;
12368 /* Peek at the next token. */
12369 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12371 return error_mark_node
;
12373 guard_tinfo
= get_token_indent_info (token
);
12375 /* Remember whether or not we are already within an iteration
12377 in_statement
= parser
->in_statement
;
12379 /* See what kind of keyword it is. */
12380 keyword
= token
->keyword
;
12387 /* Begin the while-statement. */
12388 statement
= begin_while_stmt ();
12389 /* Look for the `('. */
12390 matching_parens parens
;
12391 parens
.require_open (parser
);
12392 /* Parse the condition. */
12393 condition
= cp_parser_condition (parser
);
12394 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12395 /* Look for the `)'. */
12396 parens
.require_close (parser
);
12397 /* Parse the dependent statement. */
12398 parser
->in_statement
= IN_ITERATION_STMT
;
12399 bool prev
= note_iteration_stmt_body_start ();
12400 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12401 note_iteration_stmt_body_end (prev
);
12402 parser
->in_statement
= in_statement
;
12403 /* We're done with the while-statement. */
12404 finish_while_stmt (statement
);
12412 /* Begin the do-statement. */
12413 statement
= begin_do_stmt ();
12414 /* Parse the body of the do-statement. */
12415 parser
->in_statement
= IN_ITERATION_STMT
;
12416 bool prev
= note_iteration_stmt_body_start ();
12417 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12418 note_iteration_stmt_body_end (prev
);
12419 parser
->in_statement
= in_statement
;
12420 finish_do_body (statement
);
12421 /* Look for the `while' keyword. */
12422 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12423 /* Look for the `('. */
12424 matching_parens parens
;
12425 parens
.require_open (parser
);
12426 /* Parse the expression. */
12427 expression
= cp_parser_expression (parser
);
12428 /* We're done with the do-statement. */
12429 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12430 /* Look for the `)'. */
12431 parens
.require_close (parser
);
12432 /* Look for the `;'. */
12433 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12439 /* Look for the `('. */
12440 matching_parens parens
;
12441 parens
.require_open (parser
);
12443 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12445 /* Look for the `)'. */
12446 parens
.require_close (parser
);
12448 /* Parse the body of the for-statement. */
12449 parser
->in_statement
= IN_ITERATION_STMT
;
12450 bool prev
= note_iteration_stmt_body_start ();
12451 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12452 note_iteration_stmt_body_end (prev
);
12453 parser
->in_statement
= in_statement
;
12455 /* We're done with the for-statement. */
12456 finish_for_stmt (statement
);
12461 cp_parser_error (parser
, "expected iteration-statement");
12462 statement
= error_mark_node
;
12469 /* Parse a init-statement or the declarator of a range-based-for.
12470 Returns true if a range-based-for declaration is seen.
12473 expression-statement
12474 simple-declaration */
12477 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12479 /* If the next token is a `;', then we have an empty
12480 expression-statement. Grammatically, this is also a
12481 simple-declaration, but an invalid one, because it does not
12482 declare anything. Therefore, if we did not handle this case
12483 specially, we would issue an error message about an invalid
12485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12487 bool is_range_for
= false;
12488 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12490 /* Try to parse the init-statement. */
12491 if (cp_parser_range_based_for_with_init_p (parser
))
12494 cp_parser_parse_tentatively (parser
);
12495 /* Parse the declaration. */
12496 cp_parser_simple_declaration (parser
,
12497 /*function_definition_allowed_p=*/false,
12499 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12500 if (!cp_parser_parse_definitely (parser
))
12501 /* That didn't work, try to parse it as an expression-statement. */
12502 cp_parser_expression_statement (parser
, NULL_TREE
);
12504 if (cxx_dialect
< cxx2a
)
12506 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12507 "range-based %<for%> loops with initializer only "
12508 "available with -std=c++2a or -std=gnu++2a");
12509 *decl
= error_mark_node
;
12513 /* A colon is used in range-based for. */
12514 parser
->colon_corrects_to_scope_p
= false;
12516 /* We're going to speculatively look for a declaration, falling back
12517 to an expression, if necessary. */
12518 cp_parser_parse_tentatively (parser
);
12519 /* Parse the declaration. */
12520 cp_parser_simple_declaration (parser
,
12521 /*function_definition_allowed_p=*/false,
12523 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12526 /* It is a range-for, consume the ':'. */
12527 cp_lexer_consume_token (parser
->lexer
);
12528 is_range_for
= true;
12529 if (cxx_dialect
< cxx11
)
12530 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12531 "range-based %<for%> loops only available with "
12532 "-std=c++11 or -std=gnu++11");
12535 /* The ';' is not consumed yet because we told
12536 cp_parser_simple_declaration not to. */
12537 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12539 if (cp_parser_parse_definitely (parser
))
12540 return is_range_for
;
12541 /* If the tentative parse failed, then we shall need to look for an
12542 expression-statement. */
12544 /* If we are here, it is an expression-statement. */
12545 cp_parser_expression_statement (parser
, NULL_TREE
);
12549 /* Parse a jump-statement.
12554 return expression [opt] ;
12555 return braced-init-list ;
12561 goto * expression ;
12563 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12566 cp_parser_jump_statement (cp_parser
* parser
)
12568 tree statement
= error_mark_node
;
12571 unsigned char in_statement
;
12573 /* Peek at the next token. */
12574 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12576 return error_mark_node
;
12578 /* See what kind of keyword it is. */
12579 keyword
= token
->keyword
;
12583 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12584 switch (in_statement
)
12587 error_at (token
->location
, "break statement not within loop or switch");
12590 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12591 || in_statement
== IN_ITERATION_STMT
);
12592 statement
= finish_break_stmt ();
12593 if (in_statement
== IN_ITERATION_STMT
)
12594 break_maybe_infinite_loop ();
12597 error_at (token
->location
, "invalid exit from OpenMP structured block");
12600 error_at (token
->location
, "break statement used with OpenMP for loop");
12603 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12607 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12610 error_at (token
->location
, "continue statement not within a loop");
12612 /* Fall through. */
12613 case IN_ITERATION_STMT
:
12615 statement
= finish_continue_stmt ();
12618 error_at (token
->location
, "invalid exit from OpenMP structured block");
12621 gcc_unreachable ();
12623 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12629 bool expr_non_constant_p
;
12631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12633 cp_lexer_set_source_position (parser
->lexer
);
12634 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12635 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12637 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12638 expr
= cp_parser_expression (parser
);
12640 /* If the next token is a `;', then there is no
12643 /* Build the return-statement. */
12644 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12645 /* Don't deduce from a discarded return statement. */;
12647 statement
= finish_return_stmt (expr
);
12648 /* Look for the final `;'. */
12649 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12654 if (parser
->in_function_body
12655 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12657 error ("%<goto%> in %<constexpr%> function");
12658 cp_function_chain
->invalid_constexpr
= true;
12661 /* Create the goto-statement. */
12662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12664 /* Issue a warning about this use of a GNU extension. */
12665 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12666 /* Consume the '*' token. */
12667 cp_lexer_consume_token (parser
->lexer
);
12668 /* Parse the dependent expression. */
12669 finish_goto_stmt (cp_parser_expression (parser
));
12672 finish_goto_stmt (cp_parser_identifier (parser
));
12673 /* Look for the final `;'. */
12674 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12678 cp_parser_error (parser
, "expected jump-statement");
12685 /* Parse a declaration-statement.
12687 declaration-statement:
12688 block-declaration */
12691 cp_parser_declaration_statement (cp_parser
* parser
)
12695 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12696 p
= obstack_alloc (&declarator_obstack
, 0);
12698 /* Parse the block-declaration. */
12699 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12701 /* Free any declarators allocated. */
12702 obstack_free (&declarator_obstack
, p
);
12705 /* Some dependent statements (like `if (cond) statement'), are
12706 implicitly in their own scope. In other words, if the statement is
12707 a single statement (as opposed to a compound-statement), it is
12708 none-the-less treated as if it were enclosed in braces. Any
12709 declarations appearing in the dependent statement are out of scope
12710 after control passes that point. This function parses a statement,
12711 but ensures that is in its own scope, even if it is not a
12712 compound-statement.
12714 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12715 is a (possibly labeled) if statement which is not enclosed in
12716 braces and has an else clause. This is used to implement
12719 CHAIN is a vector of if-else-if conditions. This is used to implement
12722 Returns the new statement. */
12725 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12726 const token_indent_info
&guard_tinfo
,
12730 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12731 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12732 token_indent_info body_tinfo
12733 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12738 /* Mark if () ; with a special NOP_EXPR. */
12739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12741 cp_lexer_consume_token (parser
->lexer
);
12742 statement
= add_stmt (build_empty_stmt (body_loc
));
12744 if (guard_tinfo
.keyword
== RID_IF
12745 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12746 warning_at (body_loc
, OPT_Wempty_body
,
12747 "suggest braces around empty body in an %<if%> statement");
12748 else if (guard_tinfo
.keyword
== RID_ELSE
)
12749 warning_at (body_loc
, OPT_Wempty_body
,
12750 "suggest braces around empty body in an %<else%> statement");
12752 /* if a compound is opened, we simply parse the statement directly. */
12753 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12754 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12755 /* If the token is not a `{', then we must take special action. */
12758 /* Create a compound-statement. */
12759 statement
= begin_compound_stmt (0);
12760 /* Parse the dependent-statement. */
12761 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12762 &body_loc_after_labels
);
12763 /* Finish the dummy compound-statement. */
12764 finish_compound_stmt (statement
);
12767 token_indent_info next_tinfo
12768 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12769 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12771 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12772 && next_tinfo
.type
!= CPP_SEMICOLON
)
12773 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12774 guard_tinfo
.location
, guard_tinfo
.keyword
);
12776 /* Return the statement. */
12780 /* For some dependent statements (like `while (cond) statement'), we
12781 have already created a scope. Therefore, even if the dependent
12782 statement is a compound-statement, we do not want to create another
12786 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12787 const token_indent_info
&guard_tinfo
)
12789 /* If the token is a `{', then we must take special action. */
12790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12792 token_indent_info body_tinfo
12793 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12794 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12796 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12797 &loc_after_labels
);
12798 token_indent_info next_tinfo
12799 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12800 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12802 if (loc_after_labels
!= UNKNOWN_LOCATION
12803 && next_tinfo
.type
!= CPP_SEMICOLON
)
12804 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12805 guard_tinfo
.location
,
12806 guard_tinfo
.keyword
);
12810 /* Avoid calling cp_parser_compound_statement, so that we
12811 don't create a new scope. Do everything else by hand. */
12812 matching_braces braces
;
12813 braces
.require_open (parser
);
12814 /* If the next keyword is `__label__' we have a label declaration. */
12815 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12816 cp_parser_label_declaration (parser
);
12817 /* Parse an (optional) statement-seq. */
12818 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12819 braces
.require_close (parser
);
12823 /* Declarations [gram.dcl.dcl] */
12825 /* Parse an optional declaration-sequence.
12829 declaration-seq declaration */
12832 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12836 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12838 if (token
->type
== CPP_CLOSE_BRACE
12839 || token
->type
== CPP_EOF
)
12842 cp_parser_toplevel_declaration (parser
);
12846 /* Parse a declaration.
12850 function-definition
12851 template-declaration
12852 explicit-instantiation
12853 explicit-specialization
12854 linkage-specification
12855 namespace-definition
12863 __extension__ declaration */
12866 cp_parser_declaration (cp_parser
* parser
)
12870 int saved_pedantic
;
12872 tree attributes
= NULL_TREE
;
12874 /* Check for the `__extension__' keyword. */
12875 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12877 /* Parse the qualified declaration. */
12878 cp_parser_declaration (parser
);
12879 /* Restore the PEDANTIC flag. */
12880 pedantic
= saved_pedantic
;
12885 /* Try to figure out what kind of declaration is present. */
12886 token1
= *cp_lexer_peek_token (parser
->lexer
);
12888 if (token1
.type
!= CPP_EOF
)
12889 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12892 token2
.type
= CPP_EOF
;
12893 token2
.keyword
= RID_MAX
;
12896 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12897 p
= obstack_alloc (&declarator_obstack
, 0);
12899 /* If the next token is `extern' and the following token is a string
12900 literal, then we have a linkage specification. */
12901 if (token1
.keyword
== RID_EXTERN
12902 && cp_parser_is_pure_string_literal (&token2
))
12903 cp_parser_linkage_specification (parser
);
12904 /* If the next token is `template', then we have either a template
12905 declaration, an explicit instantiation, or an explicit
12907 else if (token1
.keyword
== RID_TEMPLATE
)
12909 /* `template <>' indicates a template specialization. */
12910 if (token2
.type
== CPP_LESS
12911 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12912 cp_parser_explicit_specialization (parser
);
12913 /* `template <' indicates a template declaration. */
12914 else if (token2
.type
== CPP_LESS
)
12915 cp_parser_template_declaration (parser
, /*member_p=*/false);
12916 /* Anything else must be an explicit instantiation. */
12918 cp_parser_explicit_instantiation (parser
);
12920 /* If the next token is `export', then we have a template
12922 else if (token1
.keyword
== RID_EXPORT
)
12923 cp_parser_template_declaration (parser
, /*member_p=*/false);
12924 /* If the next token is `extern', 'static' or 'inline' and the one
12925 after that is `template', we have a GNU extended explicit
12926 instantiation directive. */
12927 else if (cp_parser_allow_gnu_extensions_p (parser
)
12928 && (token1
.keyword
== RID_EXTERN
12929 || token1
.keyword
== RID_STATIC
12930 || token1
.keyword
== RID_INLINE
)
12931 && token2
.keyword
== RID_TEMPLATE
)
12932 cp_parser_explicit_instantiation (parser
);
12933 /* If the next token is `namespace', check for a named or unnamed
12934 namespace definition. */
12935 else if (token1
.keyword
== RID_NAMESPACE
12936 && (/* A named namespace definition. */
12937 (token2
.type
== CPP_NAME
12938 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12940 || (token2
.type
== CPP_OPEN_SQUARE
12941 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12942 == CPP_OPEN_SQUARE
)
12943 /* An unnamed namespace definition. */
12944 || token2
.type
== CPP_OPEN_BRACE
12945 || token2
.keyword
== RID_ATTRIBUTE
))
12946 cp_parser_namespace_definition (parser
);
12947 /* An inline (associated) namespace definition. */
12948 else if (token1
.keyword
== RID_INLINE
12949 && token2
.keyword
== RID_NAMESPACE
)
12950 cp_parser_namespace_definition (parser
);
12951 /* Objective-C++ declaration/definition. */
12952 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12953 cp_parser_objc_declaration (parser
, NULL_TREE
);
12954 else if (c_dialect_objc ()
12955 && token1
.keyword
== RID_ATTRIBUTE
12956 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12957 cp_parser_objc_declaration (parser
, attributes
);
12958 /* At this point we may have a template declared by a concept
12960 else if (flag_concepts
12961 && cp_parser_template_declaration_after_export (parser
,
12962 /*member_p=*/false))
12965 /* Try to parse a block-declaration, or a function-definition. */
12966 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12968 /* Free any declarators allocated. */
12969 obstack_free (&declarator_obstack
, p
);
12972 /* Parse a namespace-scope declaration. */
12975 cp_parser_toplevel_declaration (cp_parser
* parser
)
12977 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12979 if (token
->type
== CPP_PRAGMA
)
12980 /* A top-level declaration can consist solely of a #pragma. A
12981 nested declaration cannot, so this is done here and not in
12982 cp_parser_declaration. (A #pragma at block scope is
12983 handled in cp_parser_statement.) */
12984 cp_parser_pragma (parser
, pragma_external
, NULL
);
12985 else if (token
->type
== CPP_SEMICOLON
)
12987 /* A declaration consisting of a single semicolon is
12988 invalid. Allow it unless we're being pedantic. */
12989 cp_lexer_consume_token (parser
->lexer
);
12990 if (!in_system_header_at (input_location
))
12991 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12994 /* Parse the declaration itself. */
12995 cp_parser_declaration (parser
);
12998 /* Parse a block-declaration.
13003 namespace-alias-definition
13010 __extension__ block-declaration
13015 static_assert-declaration
13017 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13018 part of a declaration-statement. */
13021 cp_parser_block_declaration (cp_parser
*parser
,
13025 int saved_pedantic
;
13027 /* Check for the `__extension__' keyword. */
13028 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13030 /* Parse the qualified declaration. */
13031 cp_parser_block_declaration (parser
, statement_p
);
13032 /* Restore the PEDANTIC flag. */
13033 pedantic
= saved_pedantic
;
13038 /* Peek at the next token to figure out which kind of declaration is
13040 token1
= cp_lexer_peek_token (parser
->lexer
);
13042 /* If the next keyword is `asm', we have an asm-definition. */
13043 if (token1
->keyword
== RID_ASM
)
13046 cp_parser_commit_to_tentative_parse (parser
);
13047 cp_parser_asm_definition (parser
);
13049 /* If the next keyword is `namespace', we have a
13050 namespace-alias-definition. */
13051 else if (token1
->keyword
== RID_NAMESPACE
)
13052 cp_parser_namespace_alias_definition (parser
);
13053 /* If the next keyword is `using', we have a
13054 using-declaration, a using-directive, or an alias-declaration. */
13055 else if (token1
->keyword
== RID_USING
)
13060 cp_parser_commit_to_tentative_parse (parser
);
13061 /* If the token after `using' is `namespace', then we have a
13062 using-directive. */
13063 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13064 if (token2
->keyword
== RID_NAMESPACE
)
13065 cp_parser_using_directive (parser
);
13066 /* If the second token after 'using' is '=', then we have an
13067 alias-declaration. */
13068 else if (cxx_dialect
>= cxx11
13069 && token2
->type
== CPP_NAME
13070 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13071 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13072 cp_parser_alias_declaration (parser
);
13073 /* Otherwise, it's a using-declaration. */
13075 cp_parser_using_declaration (parser
,
13076 /*access_declaration_p=*/false);
13078 /* If the next keyword is `__label__' we have a misplaced label
13080 else if (token1
->keyword
== RID_LABEL
)
13082 cp_lexer_consume_token (parser
->lexer
);
13083 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13084 cp_parser_skip_to_end_of_statement (parser
);
13085 /* If the next token is now a `;', consume it. */
13086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13087 cp_lexer_consume_token (parser
->lexer
);
13089 /* If the next token is `static_assert' we have a static assertion. */
13090 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13091 cp_parser_static_assert (parser
, /*member_p=*/false);
13092 /* Anything else must be a simple-declaration. */
13094 cp_parser_simple_declaration (parser
, !statement_p
,
13095 /*maybe_range_for_decl*/NULL
);
13098 /* Parse a simple-declaration.
13100 simple-declaration:
13101 decl-specifier-seq [opt] init-declarator-list [opt] ;
13102 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13103 brace-or-equal-initializer ;
13105 init-declarator-list:
13107 init-declarator-list , init-declarator
13109 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13110 function-definition as a simple-declaration.
13112 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13113 parsed declaration if it is an uninitialized single declarator not followed
13114 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13115 if present, will not be consumed. */
13118 cp_parser_simple_declaration (cp_parser
* parser
,
13119 bool function_definition_allowed_p
,
13120 tree
*maybe_range_for_decl
)
13122 cp_decl_specifier_seq decl_specifiers
;
13123 int declares_class_or_enum
;
13124 bool saw_declarator
;
13125 location_t comma_loc
= UNKNOWN_LOCATION
;
13126 location_t init_loc
= UNKNOWN_LOCATION
;
13128 if (maybe_range_for_decl
)
13129 *maybe_range_for_decl
= NULL_TREE
;
13131 /* Defer access checks until we know what is being declared; the
13132 checks for names appearing in the decl-specifier-seq should be
13133 done as if we were in the scope of the thing being declared. */
13134 push_deferring_access_checks (dk_deferred
);
13136 /* Parse the decl-specifier-seq. We have to keep track of whether
13137 or not the decl-specifier-seq declares a named class or
13138 enumeration type, since that is the only case in which the
13139 init-declarator-list is allowed to be empty.
13143 In a simple-declaration, the optional init-declarator-list can be
13144 omitted only when declaring a class or enumeration, that is when
13145 the decl-specifier-seq contains either a class-specifier, an
13146 elaborated-type-specifier, or an enum-specifier. */
13147 cp_parser_decl_specifier_seq (parser
,
13148 CP_PARSER_FLAGS_OPTIONAL
,
13150 &declares_class_or_enum
);
13151 /* We no longer need to defer access checks. */
13152 stop_deferring_access_checks ();
13154 /* In a block scope, a valid declaration must always have a
13155 decl-specifier-seq. By not trying to parse declarators, we can
13156 resolve the declaration/expression ambiguity more quickly. */
13157 if (!function_definition_allowed_p
13158 && !decl_specifiers
.any_specifiers_p
)
13160 cp_parser_error (parser
, "expected declaration");
13164 /* If the next two tokens are both identifiers, the code is
13165 erroneous. The usual cause of this situation is code like:
13169 where "T" should name a type -- but does not. */
13170 if (!decl_specifiers
.any_type_specifiers_p
13171 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13173 /* If parsing tentatively, we should commit; we really are
13174 looking at a declaration. */
13175 cp_parser_commit_to_tentative_parse (parser
);
13180 cp_parser_maybe_commit_to_declaration (parser
,
13181 decl_specifiers
.any_specifiers_p
);
13183 /* Look for C++17 decomposition declaration. */
13184 for (size_t n
= 1; ; n
++)
13185 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13186 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13188 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13189 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13190 && decl_specifiers
.any_specifiers_p
)
13193 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13194 maybe_range_for_decl
,
13197 /* The next token should be either a `,' or a `;'. */
13198 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13199 /* If it's a `;', we are done. */
13200 if (token
->type
== CPP_SEMICOLON
)
13202 else if (maybe_range_for_decl
)
13204 if (*maybe_range_for_decl
== NULL_TREE
)
13205 *maybe_range_for_decl
= error_mark_node
;
13208 /* Anything else is an error. */
13211 /* If we have already issued an error message we don't need
13212 to issue another one. */
13213 if ((decl
!= error_mark_node
13214 && DECL_INITIAL (decl
) != error_mark_node
)
13215 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13216 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13217 /* Skip tokens until we reach the end of the statement. */
13218 cp_parser_skip_to_end_of_statement (parser
);
13219 /* If the next token is now a `;', consume it. */
13220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13221 cp_lexer_consume_token (parser
->lexer
);
13229 bool auto_specifier_p
;
13230 /* NULL_TREE if both variable and function declaration are allowed,
13231 error_mark_node if function declaration are not allowed and
13232 a FUNCTION_DECL that should be diagnosed if it is followed by
13233 variable declarations. */
13234 tree auto_function_declaration
;
13236 last_type
= NULL_TREE
;
13238 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13239 auto_function_declaration
= NULL_TREE
;
13241 /* Keep going until we hit the `;' at the end of the simple
13243 saw_declarator
= false;
13244 while (cp_lexer_next_token_is_not (parser
->lexer
,
13248 bool function_definition_p
;
13250 tree auto_result
= NULL_TREE
;
13252 if (saw_declarator
)
13254 /* If we are processing next declarator, comma is expected */
13255 token
= cp_lexer_peek_token (parser
->lexer
);
13256 gcc_assert (token
->type
== CPP_COMMA
);
13257 cp_lexer_consume_token (parser
->lexer
);
13258 if (maybe_range_for_decl
)
13260 *maybe_range_for_decl
= error_mark_node
;
13261 if (comma_loc
== UNKNOWN_LOCATION
)
13262 comma_loc
= token
->location
;
13266 saw_declarator
= true;
13268 /* Parse the init-declarator. */
13269 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13271 function_definition_allowed_p
,
13272 /*member_p=*/false,
13273 declares_class_or_enum
,
13274 &function_definition_p
,
13275 maybe_range_for_decl
,
13278 /* If an error occurred while parsing tentatively, exit quickly.
13279 (That usually happens when in the body of a function; each
13280 statement is treated as a declaration-statement until proven
13282 if (cp_parser_error_occurred (parser
))
13285 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13287 /* If the init-declarator-list contains more than one
13288 init-declarator, they shall all form declarations of
13290 if (auto_function_declaration
== NULL_TREE
)
13291 auto_function_declaration
13292 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13293 else if (TREE_CODE (decl
) == FUNCTION_DECL
13294 || auto_function_declaration
!= error_mark_node
)
13296 error_at (decl_specifiers
.locations
[ds_type_spec
],
13297 "non-variable %qD in declaration with more than one "
13298 "declarator with placeholder type",
13299 TREE_CODE (decl
) == FUNCTION_DECL
13300 ? decl
: auto_function_declaration
);
13301 auto_function_declaration
= error_mark_node
;
13306 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13309 && last_type
!= error_mark_node
13310 && !same_type_p (auto_result
, last_type
))
13312 /* If the list of declarators contains more than one declarator,
13313 the type of each declared variable is determined as described
13314 above. If the type deduced for the template parameter U is not
13315 the same in each deduction, the program is ill-formed. */
13316 error_at (decl_specifiers
.locations
[ds_type_spec
],
13317 "inconsistent deduction for %qT: %qT and then %qT",
13318 decl_specifiers
.type
, last_type
, auto_result
);
13319 last_type
= error_mark_node
;
13322 last_type
= auto_result
;
13325 /* Handle function definitions specially. */
13326 if (function_definition_p
)
13328 /* If the next token is a `,', then we are probably
13329 processing something like:
13333 which is erroneous. */
13334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13336 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13337 error_at (token
->location
,
13339 " declarations and function-definitions is forbidden");
13341 /* Otherwise, we're done with the list of declarators. */
13344 pop_deferring_access_checks ();
13348 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13349 *maybe_range_for_decl
= decl
;
13350 /* The next token should be either a `,' or a `;'. */
13351 token
= cp_lexer_peek_token (parser
->lexer
);
13352 /* If it's a `,', there are more declarators to come. */
13353 if (token
->type
== CPP_COMMA
)
13354 /* will be consumed next time around */;
13355 /* If it's a `;', we are done. */
13356 else if (token
->type
== CPP_SEMICOLON
)
13358 else if (maybe_range_for_decl
)
13360 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13361 permerror (decl_specifiers
.locations
[ds_type_spec
],
13362 "types may not be defined in a for-range-declaration");
13365 /* Anything else is an error. */
13368 /* If we have already issued an error message we don't need
13369 to issue another one. */
13370 if ((decl
!= error_mark_node
13371 && DECL_INITIAL (decl
) != error_mark_node
)
13372 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13373 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13374 /* Skip tokens until we reach the end of the statement. */
13375 cp_parser_skip_to_end_of_statement (parser
);
13376 /* If the next token is now a `;', consume it. */
13377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13378 cp_lexer_consume_token (parser
->lexer
);
13381 /* After the first time around, a function-definition is not
13382 allowed -- even if it was OK at first. For example:
13387 function_definition_allowed_p
= false;
13390 /* Issue an error message if no declarators are present, and the
13391 decl-specifier-seq does not itself declare a class or
13392 enumeration: [dcl.dcl]/3. */
13393 if (!saw_declarator
)
13395 if (cp_parser_declares_only_class_p (parser
))
13397 if (!declares_class_or_enum
13398 && decl_specifiers
.type
13399 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13400 /* Ensure an error is issued anyway when finish_decltype_type,
13401 called via cp_parser_decl_specifier_seq, returns a class or
13402 an enumeration (c++/51786). */
13403 decl_specifiers
.type
= NULL_TREE
;
13404 shadow_tag (&decl_specifiers
);
13406 /* Perform any deferred access checks. */
13407 perform_deferred_access_checks (tf_warning_or_error
);
13410 /* Consume the `;'. */
13412 if (!maybe_range_for_decl
)
13413 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13414 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13416 if (init_loc
!= UNKNOWN_LOCATION
)
13417 error_at (init_loc
, "initializer in range-based %<for%> loop");
13418 if (comma_loc
!= UNKNOWN_LOCATION
)
13419 error_at (comma_loc
,
13420 "multiple declarations in range-based %<for%> loop");
13424 pop_deferring_access_checks ();
13427 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13428 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13432 cp_parser_decomposition_declaration (cp_parser
*parser
,
13433 cp_decl_specifier_seq
*decl_specifiers
,
13434 tree
*maybe_range_for_decl
,
13435 location_t
*init_loc
)
13437 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13438 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13439 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13441 /* Parse the identifier-list. */
13442 auto_vec
<cp_expr
, 10> v
;
13443 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13446 cp_expr e
= cp_parser_identifier (parser
);
13447 if (e
.get_value () == error_mark_node
)
13450 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13452 cp_lexer_consume_token (parser
->lexer
);
13455 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13456 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13458 end_loc
= UNKNOWN_LOCATION
;
13459 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13462 cp_lexer_consume_token (parser
->lexer
);
13465 cp_parser_skip_to_end_of_statement (parser
);
13466 return error_mark_node
;
13470 if (cxx_dialect
< cxx17
)
13471 pedwarn (loc
, 0, "structured bindings only available with "
13472 "-std=c++17 or -std=gnu++17");
13475 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13476 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13477 declarator
->id_loc
= loc
;
13478 if (ref_qual
!= REF_QUAL_NONE
)
13479 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13480 ref_qual
== REF_QUAL_RVALUE
,
13482 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13483 NULL_TREE
, decl_specifiers
->attributes
,
13485 tree orig_decl
= decl
;
13489 cp_decl_specifier_seq decl_specs
;
13490 clear_decl_specs (&decl_specs
);
13491 decl_specs
.type
= make_auto ();
13493 FOR_EACH_VEC_ELT (v
, i
, e
)
13496 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13497 sfk_none
, e
.get_location ());
13500 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13501 declarator
->id_loc
= e
.get_location ();
13503 tree elt_pushed_scope
;
13504 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13505 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13506 if (decl2
== error_mark_node
)
13507 decl
= error_mark_node
;
13508 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13510 /* Ensure we've diagnosed redeclaration if we aren't creating
13512 gcc_assert (errorcount
);
13513 decl
= error_mark_node
;
13517 if (elt_pushed_scope
)
13518 pop_scope (elt_pushed_scope
);
13523 error_at (loc
, "empty structured binding declaration");
13524 decl
= error_mark_node
;
13527 if (maybe_range_for_decl
== NULL
13528 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13530 bool non_constant_p
= false, is_direct_init
= false;
13531 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13532 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13534 if (initializer
== NULL_TREE
13535 || (TREE_CODE (initializer
) == TREE_LIST
13536 && TREE_CHAIN (initializer
))
13538 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13539 && CONSTRUCTOR_NELTS (initializer
) != 1))
13541 error_at (loc
, "invalid initializer for structured binding "
13543 initializer
= error_mark_node
;
13546 if (decl
!= error_mark_node
)
13548 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13549 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13550 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13551 cp_finish_decomp (decl
, prev
, v
.length ());
13554 else if (decl
!= error_mark_node
)
13556 *maybe_range_for_decl
= prev
;
13557 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13558 the underlying DECL. */
13559 cp_finish_decomp (decl
, prev
, v
.length ());
13563 pop_scope (pushed_scope
);
13565 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13567 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13568 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13574 /* Parse a decl-specifier-seq.
13576 decl-specifier-seq:
13577 decl-specifier-seq [opt] decl-specifier
13578 decl-specifier attribute-specifier-seq [opt] (C++11)
13581 storage-class-specifier
13592 Concepts Extension:
13597 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13599 The parser flags FLAGS is used to control type-specifier parsing.
13601 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13604 1: one of the decl-specifiers is an elaborated-type-specifier
13605 (i.e., a type declaration)
13606 2: one of the decl-specifiers is an enum-specifier or a
13607 class-specifier (i.e., a type definition)
13612 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13613 cp_parser_flags flags
,
13614 cp_decl_specifier_seq
*decl_specs
,
13615 int* declares_class_or_enum
)
13617 bool constructor_possible_p
= !parser
->in_declarator_p
;
13618 bool found_decl_spec
= false;
13619 cp_token
*start_token
= NULL
;
13622 /* Clear DECL_SPECS. */
13623 clear_decl_specs (decl_specs
);
13625 /* Assume no class or enumeration type is declared. */
13626 *declares_class_or_enum
= 0;
13628 /* Keep reading specifiers until there are no more to read. */
13631 bool constructor_p
;
13635 /* Peek at the next token. */
13636 token
= cp_lexer_peek_token (parser
->lexer
);
13638 /* Save the first token of the decl spec list for error
13641 start_token
= token
;
13642 /* Handle attributes. */
13643 if (cp_next_tokens_can_be_attribute_p (parser
))
13645 /* Parse the attributes. */
13646 tree attrs
= cp_parser_attributes_opt (parser
);
13648 /* In a sequence of declaration specifiers, c++11 attributes
13649 appertain to the type that precede them. In that case
13652 The attribute-specifier-seq affects the type only for
13653 the declaration it appears in, not other declarations
13654 involving the same type.
13656 But for now let's force the user to position the
13657 attribute either at the beginning of the declaration or
13658 after the declarator-id, which would clearly mean that it
13659 applies to the declarator. */
13660 if (cxx11_attribute_p (attrs
))
13662 if (!found_decl_spec
)
13663 /* The c++11 attribute is at the beginning of the
13664 declaration. It appertains to the entity being
13668 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13670 /* This is an attribute following a
13671 class-specifier. */
13672 if (decl_specs
->type_definition_p
)
13673 warn_misplaced_attr_for_class_type (token
->location
,
13679 decl_specs
->std_attributes
13680 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13681 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13682 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13688 decl_specs
->attributes
13689 = attr_chainon (decl_specs
->attributes
, attrs
);
13690 if (decl_specs
->locations
[ds_attribute
] == 0)
13691 decl_specs
->locations
[ds_attribute
] = token
->location
;
13694 /* Assume we will find a decl-specifier keyword. */
13695 found_decl_spec
= true;
13696 /* If the next token is an appropriate keyword, we can simply
13697 add it to the list. */
13698 switch (token
->keyword
)
13704 if (!at_class_scope_p ())
13706 gcc_rich_location
richloc (token
->location
);
13707 richloc
.add_fixit_remove ();
13708 error_at (&richloc
, "%<friend%> used outside of class");
13709 cp_lexer_purge_token (parser
->lexer
);
13714 /* Consume the token. */
13715 cp_lexer_consume_token (parser
->lexer
);
13719 case RID_CONSTEXPR
:
13721 cp_lexer_consume_token (parser
->lexer
);
13726 cp_lexer_consume_token (parser
->lexer
);
13729 /* function-specifier:
13736 cp_parser_function_specifier_opt (parser
, decl_specs
);
13743 /* Consume the token. */
13744 cp_lexer_consume_token (parser
->lexer
);
13745 /* A constructor declarator cannot appear in a typedef. */
13746 constructor_possible_p
= false;
13747 /* The "typedef" keyword can only occur in a declaration; we
13748 may as well commit at this point. */
13749 cp_parser_commit_to_tentative_parse (parser
);
13751 if (decl_specs
->storage_class
!= sc_none
)
13752 decl_specs
->conflicting_specifiers_p
= true;
13755 /* storage-class-specifier:
13765 if (cxx_dialect
== cxx98
)
13767 /* Consume the token. */
13768 cp_lexer_consume_token (parser
->lexer
);
13770 /* Complain about `auto' as a storage specifier, if
13771 we're complaining about C++0x compatibility. */
13772 gcc_rich_location
richloc (token
->location
);
13773 richloc
.add_fixit_remove ();
13774 warning_at (&richloc
, OPT_Wc__11_compat
,
13775 "%<auto%> changes meaning in C++11; "
13776 "please remove it");
13778 /* Set the storage class anyway. */
13779 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13783 /* C++0x auto type-specifier. */
13784 found_decl_spec
= false;
13791 /* Consume the token. */
13792 cp_lexer_consume_token (parser
->lexer
);
13793 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13797 /* Consume the token. */
13799 cp_lexer_consume_token (parser
->lexer
);
13803 /* We did not yet find a decl-specifier yet. */
13804 found_decl_spec
= false;
13808 if (found_decl_spec
13809 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13810 && token
->keyword
!= RID_CONSTEXPR
)
13811 error ("decl-specifier invalid in condition");
13813 if (found_decl_spec
13814 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13815 && token
->keyword
!= RID_MUTABLE
13816 && token
->keyword
!= RID_CONSTEXPR
)
13817 error_at (token
->location
, "%qD invalid in lambda",
13818 ridpointers
[token
->keyword
]);
13821 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13823 /* Constructors are a special case. The `S' in `S()' is not a
13824 decl-specifier; it is the beginning of the declarator. */
13826 = (!found_decl_spec
13827 && constructor_possible_p
13828 && (cp_parser_constructor_declarator_p
13829 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13831 /* If we don't have a DECL_SPEC yet, then we must be looking at
13832 a type-specifier. */
13833 if (!found_decl_spec
&& !constructor_p
)
13835 int decl_spec_declares_class_or_enum
;
13836 bool is_cv_qualifier
;
13840 = cp_parser_type_specifier (parser
, flags
,
13842 /*is_declaration=*/true,
13843 &decl_spec_declares_class_or_enum
,
13845 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13847 /* If this type-specifier referenced a user-defined type
13848 (a typedef, class-name, etc.), then we can't allow any
13849 more such type-specifiers henceforth.
13853 The longest sequence of decl-specifiers that could
13854 possibly be a type name is taken as the
13855 decl-specifier-seq of a declaration. The sequence shall
13856 be self-consistent as described below.
13860 As a general rule, at most one type-specifier is allowed
13861 in the complete decl-specifier-seq of a declaration. The
13862 only exceptions are the following:
13864 -- const or volatile can be combined with any other
13867 -- signed or unsigned can be combined with char, long,
13875 void g (const int Pc);
13877 Here, Pc is *not* part of the decl-specifier seq; it's
13878 the declarator. Therefore, once we see a type-specifier
13879 (other than a cv-qualifier), we forbid any additional
13880 user-defined types. We *do* still allow things like `int
13881 int' to be considered a decl-specifier-seq, and issue the
13882 error message later. */
13883 if (type_spec
&& !is_cv_qualifier
)
13884 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13885 /* A constructor declarator cannot follow a type-specifier. */
13888 constructor_possible_p
= false;
13889 found_decl_spec
= true;
13890 if (!is_cv_qualifier
)
13891 decl_specs
->any_type_specifiers_p
= true;
13893 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
13894 error_at (token
->location
, "type-specifier invalid in lambda");
13898 /* If we still do not have a DECL_SPEC, then there are no more
13899 decl-specifiers. */
13900 if (!found_decl_spec
)
13903 decl_specs
->any_specifiers_p
= true;
13904 /* After we see one decl-specifier, further decl-specifiers are
13905 always optional. */
13906 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13909 /* Don't allow a friend specifier with a class definition. */
13910 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13911 && (*declares_class_or_enum
& 2))
13912 error_at (decl_specs
->locations
[ds_friend
],
13913 "class definition may not be declared a friend");
13916 /* Parse an (optional) storage-class-specifier.
13918 storage-class-specifier:
13927 storage-class-specifier:
13930 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13933 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13935 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13938 if (cxx_dialect
!= cxx98
)
13940 /* Fall through for C++98. */
13941 gcc_fallthrough ();
13948 /* Consume the token. */
13949 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13956 /* Parse an (optional) function-specifier.
13958 function-specifier:
13964 explicit(constant-expression)
13966 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13967 Updates DECL_SPECS, if it is non-NULL. */
13970 cp_parser_function_specifier_opt (cp_parser
* parser
,
13971 cp_decl_specifier_seq
*decl_specs
)
13973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13974 switch (token
->keyword
)
13977 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13981 /* 14.5.2.3 [temp.mem]
13983 A member function template shall not be virtual. */
13984 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13985 && current_class_type
)
13986 error_at (token
->location
, "templates may not be %<virtual%>");
13988 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13993 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13994 /* If we see '(', it's C++20 explicit(bool). */
13996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13998 matching_parens parens
;
13999 parens
.consume_open (parser
);
14001 /* New types are not allowed in an explicit-specifier. */
14002 const char *saved_message
14003 = parser
->type_definition_forbidden_message
;
14004 parser
->type_definition_forbidden_message
14005 = G_("types may not be defined in explicit-specifier");
14007 if (cxx_dialect
< cxx2a
)
14008 pedwarn (token
->location
, 0,
14009 "%<explicit(bool)%> only available with -std=c++2a "
14010 "or -std=gnu++2a");
14012 /* Parse the constant-expression. */
14013 expr
= cp_parser_constant_expression (parser
);
14015 /* Restore the saved message. */
14016 parser
->type_definition_forbidden_message
= saved_message
;
14017 parens
.require_close (parser
);
14020 /* The explicit-specifier explicit without a constant-expression is
14021 equivalent to the explicit-specifier explicit(true). */
14022 expr
= boolean_true_node
;
14025 "the constant-expression, if supplied, shall be a contextually
14026 converted constant expression of type bool." */
14027 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14028 /* We could evaluate it -- mark the decl as appropriate. */
14029 if (expr
== boolean_true_node
)
14030 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14031 else if (expr
== boolean_false_node
)
14032 /* Don't mark the decl as explicit. */;
14033 else if (decl_specs
)
14034 /* The expression was value-dependent. Remember it so that we can
14035 substitute it later. */
14036 decl_specs
->explicit_specifier
= expr
;
14044 /* Consume the token. */
14045 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14048 /* Parse a linkage-specification.
14050 linkage-specification:
14051 extern string-literal { declaration-seq [opt] }
14052 extern string-literal declaration */
14055 cp_parser_linkage_specification (cp_parser
* parser
)
14059 /* Look for the `extern' keyword. */
14060 cp_token
*extern_token
14061 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14063 /* Look for the string-literal. */
14064 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14065 linkage
= cp_parser_string_literal (parser
, false, false);
14067 /* Transform the literal into an identifier. If the literal is a
14068 wide-character string, or contains embedded NULs, then we can't
14069 handle it as the user wants. */
14070 if (strlen (TREE_STRING_POINTER (linkage
))
14071 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14073 cp_parser_error (parser
, "invalid linkage-specification");
14074 /* Assume C++ linkage. */
14075 linkage
= lang_name_cplusplus
;
14078 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14080 /* We're now using the new linkage. */
14081 push_lang_context (linkage
);
14083 /* Preserve the location of the the innermost linkage specification,
14084 tracking the locations of nested specifications via a local. */
14085 location_t saved_location
14086 = parser
->innermost_linkage_specification_location
;
14087 /* Construct a location ranging from the start of the "extern" to
14088 the end of the string-literal, with the caret at the start, e.g.:
14092 parser
->innermost_linkage_specification_location
14093 = make_location (extern_token
->location
,
14094 extern_token
->location
,
14095 get_finish (string_token
->location
));
14097 /* If the next token is a `{', then we're using the first
14099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14101 cp_ensure_no_omp_declare_simd (parser
);
14102 cp_ensure_no_oacc_routine (parser
);
14104 /* Consume the `{' token. */
14105 matching_braces braces
;
14106 braces
.consume_open (parser
)->location
;
14107 /* Parse the declarations. */
14108 cp_parser_declaration_seq_opt (parser
);
14109 /* Look for the closing `}'. */
14110 braces
.require_close (parser
);
14112 /* Otherwise, there's just one declaration. */
14115 bool saved_in_unbraced_linkage_specification_p
;
14117 saved_in_unbraced_linkage_specification_p
14118 = parser
->in_unbraced_linkage_specification_p
;
14119 parser
->in_unbraced_linkage_specification_p
= true;
14120 cp_parser_declaration (parser
);
14121 parser
->in_unbraced_linkage_specification_p
14122 = saved_in_unbraced_linkage_specification_p
;
14125 /* We're done with the linkage-specification. */
14126 pop_lang_context ();
14128 /* Restore location of parent linkage specification, if any. */
14129 parser
->innermost_linkage_specification_location
= saved_location
;
14132 /* Parse a static_assert-declaration.
14134 static_assert-declaration:
14135 static_assert ( constant-expression , string-literal ) ;
14136 static_assert ( constant-expression ) ; (C++17)
14138 If MEMBER_P, this static_assert is a class member. */
14141 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14144 location_t token_loc
;
14148 /* Peek at the `static_assert' token so we can keep track of exactly
14149 where the static assertion started. */
14150 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14152 /* Look for the `static_assert' keyword. */
14153 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14157 /* We know we are in a static assertion; commit to any tentative
14159 if (cp_parser_parsing_tentatively (parser
))
14160 cp_parser_commit_to_tentative_parse (parser
);
14162 /* Parse the `(' starting the static assertion condition. */
14163 matching_parens parens
;
14164 parens
.require_open (parser
);
14166 /* Parse the constant-expression. Allow a non-constant expression
14167 here in order to give better diagnostics in finish_static_assert. */
14169 cp_parser_constant_expression (parser
,
14170 /*allow_non_constant_p=*/true,
14171 /*non_constant_p=*/&dummy
);
14173 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14175 if (cxx_dialect
< cxx17
)
14176 pedwarn (input_location
, OPT_Wpedantic
,
14177 "static_assert without a message "
14178 "only available with -std=c++17 or -std=gnu++17");
14180 cp_lexer_consume_token (parser
->lexer
);
14181 message
= build_string (1, "");
14182 TREE_TYPE (message
) = char_array_type_node
;
14183 fix_string_type (message
);
14187 /* Parse the separating `,'. */
14188 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14190 /* Parse the string-literal message. */
14191 message
= cp_parser_string_literal (parser
,
14192 /*translate=*/false,
14195 /* A `)' completes the static assertion. */
14196 if (!parens
.require_close (parser
))
14197 cp_parser_skip_to_closing_parenthesis (parser
,
14198 /*recovering=*/true,
14199 /*or_comma=*/false,
14200 /*consume_paren=*/true);
14203 /* A semicolon terminates the declaration. */
14204 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14206 /* Get the location for the static assertion. Use that of the
14207 condition if available, otherwise, use that of the "static_assert"
14209 location_t assert_loc
= condition
.get_location ();
14210 if (assert_loc
== UNKNOWN_LOCATION
)
14211 assert_loc
= token_loc
;
14213 /* Complete the static assertion, which may mean either processing
14214 the static assert now or saving it for template instantiation. */
14215 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14218 /* Parse the expression in decltype ( expression ). */
14221 cp_parser_decltype_expr (cp_parser
*parser
,
14222 bool &id_expression_or_member_access_p
)
14224 cp_token
*id_expr_start_token
;
14227 /* Since we're going to preserve any side-effects from this parse, set up a
14228 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14229 in the expression. */
14230 tentative_firewall
firewall (parser
);
14232 /* First, try parsing an id-expression. */
14233 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14234 cp_parser_parse_tentatively (parser
);
14235 expr
= cp_parser_id_expression (parser
,
14236 /*template_keyword_p=*/false,
14237 /*check_dependency_p=*/true,
14238 /*template_p=*/NULL
,
14239 /*declarator_p=*/false,
14240 /*optional_p=*/false);
14242 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14244 bool non_integral_constant_expression_p
= false;
14245 tree id_expression
= expr
;
14247 const char *error_msg
;
14249 if (identifier_p (expr
))
14250 /* Lookup the name we got back from the id-expression. */
14251 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14252 id_expr_start_token
->location
);
14254 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14255 /* A template without args is not a complete id-expression. */
14256 expr
= error_mark_node
;
14259 && expr
!= error_mark_node
14260 && TREE_CODE (expr
) != TYPE_DECL
14261 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14262 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14263 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14265 /* Complete lookup of the id-expression. */
14266 expr
= (finish_id_expression
14267 (id_expression
, expr
, parser
->scope
, &idk
,
14268 /*integral_constant_expression_p=*/false,
14269 /*allow_non_integral_constant_expression_p=*/true,
14270 &non_integral_constant_expression_p
,
14271 /*template_p=*/false,
14273 /*address_p=*/false,
14274 /*template_arg_p=*/false,
14276 id_expr_start_token
->location
));
14278 if (expr
== error_mark_node
)
14279 /* We found an id-expression, but it was something that we
14280 should not have found. This is an error, not something
14281 we can recover from, so note that we found an
14282 id-expression and we'll recover as gracefully as
14284 id_expression_or_member_access_p
= true;
14288 && expr
!= error_mark_node
14289 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14290 /* We have an id-expression. */
14291 id_expression_or_member_access_p
= true;
14294 if (!id_expression_or_member_access_p
)
14296 /* Abort the id-expression parse. */
14297 cp_parser_abort_tentative_parse (parser
);
14299 /* Parsing tentatively, again. */
14300 cp_parser_parse_tentatively (parser
);
14302 /* Parse a class member access. */
14303 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14304 /*cast_p=*/false, /*decltype*/true,
14305 /*member_access_only_p=*/true, NULL
);
14308 && expr
!= error_mark_node
14309 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14310 /* We have an id-expression. */
14311 id_expression_or_member_access_p
= true;
14314 if (id_expression_or_member_access_p
)
14315 /* We have parsed the complete id-expression or member access. */
14316 cp_parser_parse_definitely (parser
);
14319 /* Abort our attempt to parse an id-expression or member access
14321 cp_parser_abort_tentative_parse (parser
);
14323 /* Commit to the tentative_firewall so we get syntax errors. */
14324 cp_parser_commit_to_tentative_parse (parser
);
14326 /* Parse a full expression. */
14327 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14328 /*decltype_p=*/true);
14334 /* Parse a `decltype' type. Returns the type.
14336 simple-type-specifier:
14337 decltype ( expression )
14339 decltype ( auto ) */
14342 cp_parser_decltype (cp_parser
*parser
)
14344 bool id_expression_or_member_access_p
= false;
14345 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14347 if (start_token
->type
== CPP_DECLTYPE
)
14349 /* Already parsed. */
14350 cp_lexer_consume_token (parser
->lexer
);
14351 return saved_checks_value (start_token
->u
.tree_check_value
);
14354 /* Look for the `decltype' token. */
14355 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14356 return error_mark_node
;
14358 /* Parse the opening `('. */
14359 matching_parens parens
;
14360 if (!parens
.require_open (parser
))
14361 return error_mark_node
;
14363 push_deferring_access_checks (dk_deferred
);
14365 tree expr
= NULL_TREE
;
14367 if (cxx_dialect
>= cxx14
14368 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14369 /* decltype (auto) */
14370 cp_lexer_consume_token (parser
->lexer
);
14373 /* decltype (expression) */
14375 /* Types cannot be defined in a `decltype' expression. Save away the
14376 old message and set the new one. */
14377 const char *saved_message
= parser
->type_definition_forbidden_message
;
14378 parser
->type_definition_forbidden_message
14379 = G_("types may not be defined in %<decltype%> expressions");
14381 /* The restrictions on constant-expressions do not apply inside
14382 decltype expressions. */
14383 bool saved_integral_constant_expression_p
14384 = parser
->integral_constant_expression_p
;
14385 bool saved_non_integral_constant_expression_p
14386 = parser
->non_integral_constant_expression_p
;
14387 parser
->integral_constant_expression_p
= false;
14389 /* Within a parenthesized expression, a `>' token is always
14390 the greater-than operator. */
14391 bool saved_greater_than_is_operator_p
14392 = parser
->greater_than_is_operator_p
;
14393 parser
->greater_than_is_operator_p
= true;
14395 /* Do not actually evaluate the expression. */
14396 ++cp_unevaluated_operand
;
14398 /* Do not warn about problems with the expression. */
14399 ++c_inhibit_evaluation_warnings
;
14401 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14403 /* Go back to evaluating expressions. */
14404 --cp_unevaluated_operand
;
14405 --c_inhibit_evaluation_warnings
;
14407 /* The `>' token might be the end of a template-id or
14408 template-parameter-list now. */
14409 parser
->greater_than_is_operator_p
14410 = saved_greater_than_is_operator_p
;
14412 /* Restore the old message and the integral constant expression
14414 parser
->type_definition_forbidden_message
= saved_message
;
14415 parser
->integral_constant_expression_p
14416 = saved_integral_constant_expression_p
;
14417 parser
->non_integral_constant_expression_p
14418 = saved_non_integral_constant_expression_p
;
14421 /* Parse to the closing `)'. */
14422 if (!parens
.require_close (parser
))
14424 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14425 /*consume_paren=*/true);
14426 pop_deferring_access_checks ();
14427 return error_mark_node
;
14433 expr
= make_decltype_auto ();
14434 AUTO_IS_DECLTYPE (expr
) = true;
14437 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14438 tf_warning_or_error
);
14440 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14442 start_token
->type
= CPP_DECLTYPE
;
14443 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14444 start_token
->u
.tree_check_value
->value
= expr
;
14445 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14446 start_token
->keyword
= RID_MAX
;
14447 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14449 pop_to_parent_deferring_access_checks ();
14454 /* Special member functions [gram.special] */
14456 /* Parse a conversion-function-id.
14458 conversion-function-id:
14459 operator conversion-type-id
14461 Returns an IDENTIFIER_NODE representing the operator. */
14464 cp_parser_conversion_function_id (cp_parser
* parser
)
14468 tree saved_qualifying_scope
;
14469 tree saved_object_scope
;
14470 tree pushed_scope
= NULL_TREE
;
14472 /* Look for the `operator' token. */
14473 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14474 return error_mark_node
;
14475 /* When we parse the conversion-type-id, the current scope will be
14476 reset. However, we need that information in able to look up the
14477 conversion function later, so we save it here. */
14478 saved_scope
= parser
->scope
;
14479 saved_qualifying_scope
= parser
->qualifying_scope
;
14480 saved_object_scope
= parser
->object_scope
;
14481 /* We must enter the scope of the class so that the names of
14482 entities declared within the class are available in the
14483 conversion-type-id. For example, consider:
14490 S::operator I() { ... }
14492 In order to see that `I' is a type-name in the definition, we
14493 must be in the scope of `S'. */
14495 pushed_scope
= push_scope (saved_scope
);
14496 /* Parse the conversion-type-id. */
14497 type
= cp_parser_conversion_type_id (parser
);
14498 /* Leave the scope of the class, if any. */
14500 pop_scope (pushed_scope
);
14501 /* Restore the saved scope. */
14502 parser
->scope
= saved_scope
;
14503 parser
->qualifying_scope
= saved_qualifying_scope
;
14504 parser
->object_scope
= saved_object_scope
;
14505 /* If the TYPE is invalid, indicate failure. */
14506 if (type
== error_mark_node
)
14507 return error_mark_node
;
14508 return make_conv_op_name (type
);
14511 /* Parse a conversion-type-id:
14513 conversion-type-id:
14514 type-specifier-seq conversion-declarator [opt]
14516 Returns the TYPE specified. */
14519 cp_parser_conversion_type_id (cp_parser
* parser
)
14522 cp_decl_specifier_seq type_specifiers
;
14523 cp_declarator
*declarator
;
14524 tree type_specified
;
14525 const char *saved_message
;
14527 /* Parse the attributes. */
14528 attributes
= cp_parser_attributes_opt (parser
);
14530 saved_message
= parser
->type_definition_forbidden_message
;
14531 parser
->type_definition_forbidden_message
14532 = G_("types may not be defined in a conversion-type-id");
14534 /* Parse the type-specifiers. */
14535 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14536 /*is_trailing_return=*/false,
14539 parser
->type_definition_forbidden_message
= saved_message
;
14541 /* If that didn't work, stop. */
14542 if (type_specifiers
.type
== error_mark_node
)
14543 return error_mark_node
;
14544 /* Parse the conversion-declarator. */
14545 declarator
= cp_parser_conversion_declarator_opt (parser
);
14547 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14548 /*initialized=*/0, &attributes
);
14550 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14552 /* Don't give this error when parsing tentatively. This happens to
14553 work because we always parse this definitively once. */
14554 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14555 && type_uses_auto (type_specified
))
14557 if (cxx_dialect
< cxx14
)
14559 error ("invalid use of %<auto%> in conversion operator");
14560 return error_mark_node
;
14562 else if (template_parm_scope_p ())
14563 warning (0, "use of %<auto%> in member template "
14564 "conversion operator can never be deduced");
14567 return type_specified
;
14570 /* Parse an (optional) conversion-declarator.
14572 conversion-declarator:
14573 ptr-operator conversion-declarator [opt]
14577 static cp_declarator
*
14578 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14580 enum tree_code code
;
14581 tree class_type
, std_attributes
= NULL_TREE
;
14582 cp_cv_quals cv_quals
;
14584 /* We don't know if there's a ptr-operator next, or not. */
14585 cp_parser_parse_tentatively (parser
);
14586 /* Try the ptr-operator. */
14587 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14589 /* If it worked, look for more conversion-declarators. */
14590 if (cp_parser_parse_definitely (parser
))
14592 cp_declarator
*declarator
;
14594 /* Parse another optional declarator. */
14595 declarator
= cp_parser_conversion_declarator_opt (parser
);
14597 declarator
= cp_parser_make_indirect_declarator
14598 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14606 /* Parse an (optional) ctor-initializer.
14609 : mem-initializer-list */
14612 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14614 /* If the next token is not a `:', then there is no
14615 ctor-initializer. */
14616 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14618 /* Do default initialization of any bases and members. */
14619 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14620 finish_mem_initializers (NULL_TREE
);
14624 /* Consume the `:' token. */
14625 cp_lexer_consume_token (parser
->lexer
);
14626 /* And the mem-initializer-list. */
14627 cp_parser_mem_initializer_list (parser
);
14630 /* Parse a mem-initializer-list.
14632 mem-initializer-list:
14633 mem-initializer ... [opt]
14634 mem-initializer ... [opt] , mem-initializer-list */
14637 cp_parser_mem_initializer_list (cp_parser
* parser
)
14639 tree mem_initializer_list
= NULL_TREE
;
14640 tree target_ctor
= error_mark_node
;
14641 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14643 /* Let the semantic analysis code know that we are starting the
14644 mem-initializer-list. */
14645 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14646 error_at (token
->location
,
14647 "only constructors take member initializers");
14649 /* Loop through the list. */
14652 tree mem_initializer
;
14654 token
= cp_lexer_peek_token (parser
->lexer
);
14655 /* Parse the mem-initializer. */
14656 mem_initializer
= cp_parser_mem_initializer (parser
);
14657 /* If the next token is a `...', we're expanding member initializers. */
14658 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14660 || (mem_initializer
!= error_mark_node
14661 && check_for_bare_parameter_packs (TREE_PURPOSE
14662 (mem_initializer
))))
14664 /* Consume the `...'. */
14666 cp_lexer_consume_token (parser
->lexer
);
14668 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14669 can be expanded but members cannot. */
14670 if (mem_initializer
!= error_mark_node
14671 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14673 error_at (token
->location
,
14674 "cannot expand initializer for member %qD",
14675 TREE_PURPOSE (mem_initializer
));
14676 mem_initializer
= error_mark_node
;
14679 /* Construct the pack expansion type. */
14680 if (mem_initializer
!= error_mark_node
)
14681 mem_initializer
= make_pack_expansion (mem_initializer
);
14683 if (target_ctor
!= error_mark_node
14684 && mem_initializer
!= error_mark_node
)
14686 error ("mem-initializer for %qD follows constructor delegation",
14687 TREE_PURPOSE (mem_initializer
));
14688 mem_initializer
= error_mark_node
;
14690 /* Look for a target constructor. */
14691 if (mem_initializer
!= error_mark_node
14692 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14693 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14695 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14696 if (mem_initializer_list
)
14698 error ("constructor delegation follows mem-initializer for %qD",
14699 TREE_PURPOSE (mem_initializer_list
));
14700 mem_initializer
= error_mark_node
;
14702 target_ctor
= mem_initializer
;
14704 /* Add it to the list, unless it was erroneous. */
14705 if (mem_initializer
!= error_mark_node
)
14707 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14708 mem_initializer_list
= mem_initializer
;
14710 /* If the next token is not a `,', we're done. */
14711 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14713 /* Consume the `,' token. */
14714 cp_lexer_consume_token (parser
->lexer
);
14717 /* Perform semantic analysis. */
14718 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14719 finish_mem_initializers (mem_initializer_list
);
14722 /* Parse a mem-initializer.
14725 mem-initializer-id ( expression-list [opt] )
14726 mem-initializer-id braced-init-list
14731 ( expression-list [opt] )
14733 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14734 class) or FIELD_DECL (for a non-static data member) to initialize;
14735 the TREE_VALUE is the expression-list. An empty initialization
14736 list is represented by void_list_node. */
14739 cp_parser_mem_initializer (cp_parser
* parser
)
14741 tree mem_initializer_id
;
14742 tree expression_list
;
14744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14746 /* Find out what is being initialized. */
14747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14749 permerror (token
->location
,
14750 "anachronistic old-style base class initializer");
14751 mem_initializer_id
= NULL_TREE
;
14755 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14756 if (mem_initializer_id
== error_mark_node
)
14757 return mem_initializer_id
;
14759 member
= expand_member_init (mem_initializer_id
);
14760 if (member
&& !DECL_P (member
))
14761 in_base_initializer
= 1;
14763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14765 bool expr_non_constant_p
;
14766 cp_lexer_set_source_position (parser
->lexer
);
14767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14768 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14769 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14770 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14774 vec
<tree
, va_gc
> *vec
;
14775 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14777 /*allow_expansion_p=*/true,
14778 /*non_constant_p=*/NULL
);
14780 return error_mark_node
;
14781 expression_list
= build_tree_list_vec (vec
);
14782 release_tree_vector (vec
);
14785 if (expression_list
== error_mark_node
)
14786 return error_mark_node
;
14787 if (!expression_list
)
14788 expression_list
= void_type_node
;
14790 in_base_initializer
= 0;
14792 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14795 /* Parse a mem-initializer-id.
14797 mem-initializer-id:
14798 :: [opt] nested-name-specifier [opt] class-name
14799 decltype-specifier (C++11)
14802 Returns a TYPE indicating the class to be initialized for the first
14803 production (and the second in C++11). Returns an IDENTIFIER_NODE
14804 indicating the data member to be initialized for the last production. */
14807 cp_parser_mem_initializer_id (cp_parser
* parser
)
14809 bool global_scope_p
;
14810 bool nested_name_specifier_p
;
14811 bool template_p
= false;
14814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14816 /* `typename' is not allowed in this context ([temp.res]). */
14817 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14819 error_at (token
->location
,
14820 "keyword %<typename%> not allowed in this context (a qualified "
14821 "member initializer is implicitly a type)");
14822 cp_lexer_consume_token (parser
->lexer
);
14824 /* Look for the optional `::' operator. */
14826 = (cp_parser_global_scope_opt (parser
,
14827 /*current_scope_valid_p=*/false)
14829 /* Look for the optional nested-name-specifier. The simplest way to
14834 The keyword `typename' is not permitted in a base-specifier or
14835 mem-initializer; in these contexts a qualified name that
14836 depends on a template-parameter is implicitly assumed to be a
14839 is to assume that we have seen the `typename' keyword at this
14841 nested_name_specifier_p
14842 = (cp_parser_nested_name_specifier_opt (parser
,
14843 /*typename_keyword_p=*/true,
14844 /*check_dependency_p=*/true,
14846 /*is_declaration=*/true)
14848 if (nested_name_specifier_p
)
14849 template_p
= cp_parser_optional_template_keyword (parser
);
14850 /* If there is a `::' operator or a nested-name-specifier, then we
14851 are definitely looking for a class-name. */
14852 if (global_scope_p
|| nested_name_specifier_p
)
14853 return cp_parser_class_name (parser
,
14854 /*typename_keyword_p=*/true,
14855 /*template_keyword_p=*/template_p
,
14857 /*check_dependency_p=*/true,
14858 /*class_head_p=*/false,
14859 /*is_declaration=*/true);
14860 /* Otherwise, we could also be looking for an ordinary identifier. */
14861 cp_parser_parse_tentatively (parser
);
14862 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14863 /* Try a decltype-specifier. */
14864 id
= cp_parser_decltype (parser
);
14866 /* Otherwise, try a class-name. */
14867 id
= cp_parser_class_name (parser
,
14868 /*typename_keyword_p=*/true,
14869 /*template_keyword_p=*/false,
14871 /*check_dependency_p=*/true,
14872 /*class_head_p=*/false,
14873 /*is_declaration=*/true);
14874 /* If we found one, we're done. */
14875 if (cp_parser_parse_definitely (parser
))
14877 /* Otherwise, look for an ordinary identifier. */
14878 return cp_parser_identifier (parser
);
14881 /* Overloading [gram.over] */
14883 /* Parse an operator-function-id.
14885 operator-function-id:
14888 Returns an IDENTIFIER_NODE for the operator which is a
14889 human-readable spelling of the identifier, e.g., `operator +'. */
14892 cp_parser_operator_function_id (cp_parser
* parser
)
14894 /* Look for the `operator' keyword. */
14895 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14896 return error_mark_node
;
14897 /* And then the name of the operator itself. */
14898 return cp_parser_operator (parser
);
14901 /* Return an identifier node for a user-defined literal operator.
14902 The suffix identifier is chained to the operator name identifier. */
14905 cp_literal_operator_id (const char* name
)
14908 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14909 + strlen (name
) + 10);
14910 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14911 identifier
= get_identifier (buffer
);
14916 /* Parse an operator.
14919 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14920 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14921 || ++ -- , ->* -> () []
14928 Returns an IDENTIFIER_NODE for the operator which is a
14929 human-readable spelling of the identifier, e.g., `operator +'. */
14932 cp_parser_operator (cp_parser
* parser
)
14934 tree id
= NULL_TREE
;
14938 /* Peek at the next token. */
14939 token
= cp_lexer_peek_token (parser
->lexer
);
14941 location_t start_loc
= token
->location
;
14943 /* Figure out which operator we have. */
14944 enum tree_code op
= ERROR_MARK
;
14945 bool assop
= false;
14946 bool consumed
= false;
14947 switch (token
->type
)
14951 /* The keyword should be either `new' or `delete'. */
14952 if (token
->keyword
== RID_NEW
)
14954 else if (token
->keyword
== RID_DELETE
)
14959 /* Consume the `new' or `delete' token. */
14960 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14962 /* Peek at the next token. */
14963 token
= cp_lexer_peek_token (parser
->lexer
);
14964 /* If it's a `[' token then this is the array variant of the
14966 if (token
->type
== CPP_OPEN_SQUARE
)
14968 /* Consume the `[' token. */
14969 cp_lexer_consume_token (parser
->lexer
);
14970 /* Look for the `]' token. */
14971 if (cp_token
*close_token
14972 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14973 end_loc
= close_token
->location
;
14974 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14976 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14994 op
= TRUNC_DIV_EXPR
;
14998 op
= TRUNC_MOD_EXPR
;
15018 op
= TRUTH_NOT_EXPR
;
15051 op
= TRUNC_DIV_EXPR
;
15056 op
= TRUNC_MOD_EXPR
;
15082 case CPP_LSHIFT_EQ
:
15087 case CPP_RSHIFT_EQ
:
15104 case CPP_GREATER_EQ
:
15109 op
= TRUTH_ANDIF_EXPR
;
15113 op
= TRUTH_ORIF_EXPR
;
15116 case CPP_PLUS_PLUS
:
15117 op
= POSTINCREMENT_EXPR
;
15120 case CPP_MINUS_MINUS
:
15121 op
= PREDECREMENT_EXPR
;
15125 op
= COMPOUND_EXPR
;
15128 case CPP_DEREF_STAR
:
15133 op
= COMPONENT_REF
;
15136 case CPP_OPEN_PAREN
:
15138 /* Consume the `('. */
15139 matching_parens parens
;
15140 parens
.consume_open (parser
);
15141 /* Look for the matching `)'. */
15142 parens
.require_close (parser
);
15148 case CPP_OPEN_SQUARE
:
15149 /* Consume the `['. */
15150 cp_lexer_consume_token (parser
->lexer
);
15151 /* Look for the matching `]'. */
15152 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15157 case CPP_UTF8STRING
:
15158 case CPP_UTF8STRING_USERDEF
:
15165 case CPP_STRING_USERDEF
:
15166 case CPP_WSTRING_USERDEF
:
15167 case CPP_STRING16_USERDEF
:
15168 case CPP_STRING32_USERDEF
:
15170 tree str
, string_tree
;
15173 if (cxx_dialect
== cxx98
)
15174 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15176 /* Consume the string. */
15177 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15178 /*wide_ok=*/true, /*lookup_udlit=*/false);
15179 if (str
== error_mark_node
)
15180 return error_mark_node
;
15181 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15183 string_tree
= USERDEF_LITERAL_VALUE (str
);
15184 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15189 /* Look for the suffix identifier. */
15190 token
= cp_lexer_peek_token (parser
->lexer
);
15191 if (token
->type
== CPP_NAME
)
15192 id
= cp_parser_identifier (parser
);
15193 else if (token
->type
== CPP_KEYWORD
)
15195 error ("unexpected keyword;"
15196 " remove space between quotes and suffix identifier");
15197 return error_mark_node
;
15201 error ("expected suffix identifier");
15202 return error_mark_node
;
15205 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15206 (TREE_TYPE (TREE_TYPE (string_tree
))));
15207 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15210 error ("expected empty string after %<operator%> keyword");
15211 return error_mark_node
;
15213 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15216 error ("invalid encoding prefix in literal operator");
15217 return error_mark_node
;
15219 if (id
!= error_mark_node
)
15221 const char *name
= IDENTIFIER_POINTER (id
);
15222 id
= cp_literal_operator_id (name
);
15228 /* Anything else is an error. */
15232 /* If we have selected an identifier, we need to consume the
15234 if (op
!= ERROR_MARK
)
15236 id
= ovl_op_identifier (assop
, op
);
15238 cp_lexer_consume_token (parser
->lexer
);
15240 /* Otherwise, no valid operator name was present. */
15243 cp_parser_error (parser
, "expected operator");
15244 id
= error_mark_node
;
15247 return cp_expr (id
, start_loc
);
15250 /* Parse a template-declaration.
15252 template-declaration:
15253 export [opt] template < template-parameter-list > declaration
15255 If MEMBER_P is TRUE, this template-declaration occurs within a
15258 The grammar rule given by the standard isn't correct. What
15259 is really meant is:
15261 template-declaration:
15262 export [opt] template-parameter-list-seq
15263 decl-specifier-seq [opt] init-declarator [opt] ;
15264 export [opt] template-parameter-list-seq
15265 function-definition
15267 template-parameter-list-seq:
15268 template-parameter-list-seq [opt]
15269 template < template-parameter-list >
15271 Concept Extensions:
15273 template-parameter-list-seq:
15274 template < template-parameter-list > requires-clause [opt]
15277 requires logical-or-expression */
15280 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15282 /* Check for `export'. */
15283 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15285 /* Consume the `export' token. */
15286 cp_lexer_consume_token (parser
->lexer
);
15287 /* Warn that we do not support `export'. */
15288 warning (0, "keyword %<export%> not implemented, and will be ignored");
15291 cp_parser_template_declaration_after_export (parser
, member_p
);
15294 /* Parse a template-parameter-list.
15296 template-parameter-list:
15298 template-parameter-list , template-parameter
15300 Returns a TREE_LIST. Each node represents a template parameter.
15301 The nodes are connected via their TREE_CHAINs. */
15304 cp_parser_template_parameter_list (cp_parser
* parser
)
15306 tree parameter_list
= NULL_TREE
;
15308 begin_template_parm_list ();
15310 /* The loop below parses the template parms. We first need to know
15311 the total number of template parms to be able to compute proper
15312 canonical types of each dependent type. So after the loop, when
15313 we know the total number of template parms,
15314 end_template_parm_list computes the proper canonical types and
15315 fixes up the dependent types accordingly. */
15320 bool is_parameter_pack
;
15321 location_t parm_loc
;
15323 /* Parse the template-parameter. */
15324 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15325 parameter
= cp_parser_template_parameter (parser
,
15327 &is_parameter_pack
);
15328 /* Add it to the list. */
15329 if (parameter
!= error_mark_node
)
15330 parameter_list
= process_template_parm (parameter_list
,
15334 is_parameter_pack
);
15337 tree err_parm
= build_tree_list (parameter
, parameter
);
15338 parameter_list
= chainon (parameter_list
, err_parm
);
15341 /* If the next token is not a `,', we're done. */
15342 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15344 /* Otherwise, consume the `,' token. */
15345 cp_lexer_consume_token (parser
->lexer
);
15348 return end_template_parm_list (parameter_list
);
15351 /* Parse a introduction-list.
15354 introduced-parameter
15355 introduction-list , introduced-parameter
15357 introduced-parameter:
15358 ...[opt] identifier
15360 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15361 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15362 WILDCARD_DECL will also have DECL_NAME set and token location in
15363 DECL_SOURCE_LOCATION. */
15366 cp_parser_introduction_list (cp_parser
*parser
)
15368 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15372 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15374 cp_lexer_consume_token (parser
->lexer
);
15376 tree identifier
= cp_parser_identifier (parser
);
15377 if (identifier
== error_mark_node
)
15380 /* Build placeholder. */
15381 tree parm
= build_nt (WILDCARD_DECL
);
15382 DECL_SOURCE_LOCATION (parm
)
15383 = cp_lexer_peek_token (parser
->lexer
)->location
;
15384 DECL_NAME (parm
) = identifier
;
15385 WILDCARD_PACK_P (parm
) = is_pack
;
15386 vec_safe_push (introduction_vec
, parm
);
15388 /* If the next token is not a `,', we're done. */
15389 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15391 /* Otherwise, consume the `,' token. */
15392 cp_lexer_consume_token (parser
->lexer
);
15395 /* Convert the vec into a TREE_VEC. */
15396 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15399 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15400 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15402 release_tree_vector (introduction_vec
);
15403 return introduction_list
;
15406 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15407 is an abstract declarator. */
15409 static inline cp_declarator
*
15410 get_id_declarator (cp_declarator
*declarator
)
15412 cp_declarator
*d
= declarator
;
15413 while (d
&& d
->kind
!= cdk_id
)
15418 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15419 is an abstract declarator. */
15422 get_unqualified_id (cp_declarator
*declarator
)
15424 declarator
= get_id_declarator (declarator
);
15426 return declarator
->u
.id
.unqualified_name
;
15431 /* Returns true if DECL represents a constrained-parameter. */
15434 is_constrained_parameter (tree decl
)
15437 && TREE_CODE (decl
) == TYPE_DECL
15438 && CONSTRAINED_PARM_CONCEPT (decl
)
15439 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15442 /* Returns true if PARM declares a constrained-parameter. */
15445 is_constrained_parameter (cp_parameter_declarator
*parm
)
15447 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15450 /* Check that the type parameter is only a declarator-id, and that its
15451 type is not cv-qualified. */
15454 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15455 cp_parameter_declarator
*parm
)
15457 if (!parm
->declarator
)
15460 if (parm
->declarator
->kind
!= cdk_id
)
15462 cp_parser_error (parser
, "invalid constrained type parameter");
15466 /* Don't allow cv-qualified type parameters. */
15467 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15468 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15470 cp_parser_error (parser
, "cv-qualified type parameter");
15477 /* Finish parsing/processing a template type parameter and checking
15478 various restrictions. */
15481 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15483 cp_parameter_declarator
* parmdecl
)
15485 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15486 return finish_template_type_parm (class_type_node
, id
);
15488 return error_mark_node
;
15492 finish_constrained_template_template_parm (tree proto
, tree id
)
15494 /* FIXME: This should probably be copied, and we may need to adjust
15495 the template parameter depths. */
15496 tree saved_parms
= current_template_parms
;
15497 begin_template_parm_list ();
15498 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15499 end_template_parm_list ();
15501 tree parm
= finish_template_template_parm (class_type_node
, id
);
15502 current_template_parms
= saved_parms
;
15507 /* Finish parsing/processing a template template parameter by borrowing
15508 the template parameter list from the prototype parameter. */
15511 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15514 cp_parameter_declarator
*parmdecl
)
15516 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15517 return error_mark_node
;
15518 return finish_constrained_template_template_parm (proto
, id
);
15521 /* Create a new non-type template parameter from the given PARM
15525 constrained_non_type_template_parm (bool *is_non_type
,
15526 cp_parameter_declarator
*parm
)
15528 *is_non_type
= true;
15529 cp_declarator
*decl
= parm
->declarator
;
15530 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15531 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15532 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15535 /* Build a constrained template parameter based on the PARMDECL
15536 declarator. The type of PARMDECL is the constrained type, which
15537 refers to the prototype template parameter that ultimately
15538 specifies the type of the declared parameter. */
15541 finish_constrained_parameter (cp_parser
*parser
,
15542 cp_parameter_declarator
*parmdecl
,
15544 bool *is_parameter_pack
)
15546 tree decl
= parmdecl
->decl_specifiers
.type
;
15547 tree id
= get_unqualified_id (parmdecl
->declarator
);
15548 tree def
= parmdecl
->default_argument
;
15549 tree proto
= DECL_INITIAL (decl
);
15551 /* A template parameter constrained by a variadic concept shall also
15552 be declared as a template parameter pack. */
15553 bool is_variadic
= template_parameter_pack_p (proto
);
15554 if (is_variadic
&& !*is_parameter_pack
)
15555 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15557 /* Build the parameter. Return an error if the declarator was invalid. */
15559 if (TREE_CODE (proto
) == TYPE_DECL
)
15560 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15561 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15562 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15565 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15566 if (parm
== error_mark_node
)
15567 return error_mark_node
;
15569 /* Finish the parameter decl and create a node attaching the
15570 default argument and constraint. */
15571 parm
= build_tree_list (def
, parm
);
15572 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15577 /* Returns true if the parsed type actually represents the declaration
15578 of a type template-parameter. */
15581 declares_constrained_type_template_parameter (tree type
)
15583 return (is_constrained_parameter (type
)
15584 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15588 /* Returns true if the parsed type actually represents the declaration of
15589 a template template-parameter. */
15592 declares_constrained_template_template_parameter (tree type
)
15594 return (is_constrained_parameter (type
)
15595 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15598 /* Parse a default argument for a type template-parameter.
15599 Note that diagnostics are handled in cp_parser_template_parameter. */
15602 cp_parser_default_type_template_argument (cp_parser
*parser
)
15604 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15606 /* Consume the `=' token. */
15607 cp_lexer_consume_token (parser
->lexer
);
15609 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15611 /* Parse the default-argument. */
15612 push_deferring_access_checks (dk_no_deferred
);
15613 tree default_argument
= cp_parser_type_id (parser
);
15614 pop_deferring_access_checks ();
15616 if (flag_concepts
&& type_uses_auto (default_argument
))
15618 error_at (token
->location
,
15619 "invalid use of %<auto%> in default template argument");
15620 return error_mark_node
;
15623 return default_argument
;
15626 /* Parse a default argument for a template template-parameter. */
15629 cp_parser_default_template_template_argument (cp_parser
*parser
)
15631 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15635 /* Consume the `='. */
15636 cp_lexer_consume_token (parser
->lexer
);
15637 /* Parse the id-expression. */
15638 push_deferring_access_checks (dk_no_deferred
);
15639 /* save token before parsing the id-expression, for error
15641 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15642 tree default_argument
15643 = cp_parser_id_expression (parser
,
15644 /*template_keyword_p=*/false,
15645 /*check_dependency_p=*/true,
15646 /*template_p=*/&is_template
,
15647 /*declarator_p=*/false,
15648 /*optional_p=*/false);
15649 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15650 /* If the id-expression was a template-id that refers to
15651 a template-class, we already have the declaration here,
15652 so no further lookup is needed. */
15655 /* Look up the name. */
15657 = cp_parser_lookup_name (parser
, default_argument
,
15659 /*is_template=*/is_template
,
15660 /*is_namespace=*/false,
15661 /*check_dependency=*/true,
15662 /*ambiguous_decls=*/NULL
,
15664 /* See if the default argument is valid. */
15665 default_argument
= check_template_template_default_arg (default_argument
);
15666 pop_deferring_access_checks ();
15667 return default_argument
;
15670 /* Parse a template-parameter.
15672 template-parameter:
15674 parameter-declaration
15676 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15677 the parameter. The TREE_PURPOSE is the default value, if any.
15678 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15679 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15680 set to true iff this parameter is a parameter pack. */
15683 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15684 bool *is_parameter_pack
)
15687 cp_parameter_declarator
*parameter_declarator
;
15690 /* Assume it is a type parameter or a template parameter. */
15691 *is_non_type
= false;
15692 /* Assume it not a parameter pack. */
15693 *is_parameter_pack
= false;
15694 /* Peek at the next token. */
15695 token
= cp_lexer_peek_token (parser
->lexer
);
15696 /* If it is `template', we have a type-parameter. */
15697 if (token
->keyword
== RID_TEMPLATE
)
15698 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15699 /* If it is `class' or `typename' we do not know yet whether it is a
15700 type parameter or a non-type parameter. Consider:
15702 template <typename T, typename T::X X> ...
15706 template <class C, class D*> ...
15708 Here, the first parameter is a type parameter, and the second is
15709 a non-type parameter. We can tell by looking at the token after
15710 the identifier -- if it is a `,', `=', or `>' then we have a type
15712 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15714 /* Peek at the token after `class' or `typename'. */
15715 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15716 /* If it's an ellipsis, we have a template type parameter
15718 if (token
->type
== CPP_ELLIPSIS
)
15719 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15720 /* If it's an identifier, skip it. */
15721 if (token
->type
== CPP_NAME
)
15722 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15723 /* Now, see if the token looks like the end of a template
15725 if (token
->type
== CPP_COMMA
15726 || token
->type
== CPP_EQ
15727 || token
->type
== CPP_GREATER
)
15728 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15731 /* Otherwise, it is a non-type parameter or a constrained parameter.
15735 When parsing a default template-argument for a non-type
15736 template-parameter, the first non-nested `>' is taken as the end
15737 of the template parameter-list rather than a greater-than
15739 parameter_declarator
15740 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15741 /*parenthesized_p=*/NULL
);
15743 if (!parameter_declarator
)
15744 return error_mark_node
;
15746 /* If the parameter declaration is marked as a parameter pack, set
15747 *IS_PARAMETER_PACK to notify the caller. */
15748 if (parameter_declarator
->template_parameter_pack_p
)
15749 *is_parameter_pack
= true;
15751 if (parameter_declarator
->default_argument
)
15753 /* Can happen in some cases of erroneous input (c++/34892). */
15754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15755 /* Consume the `...' for better error recovery. */
15756 cp_lexer_consume_token (parser
->lexer
);
15759 // The parameter may have been constrained.
15760 if (is_constrained_parameter (parameter_declarator
))
15761 return finish_constrained_parameter (parser
,
15762 parameter_declarator
,
15764 is_parameter_pack
);
15766 // Now we're sure that the parameter is a non-type parameter.
15767 *is_non_type
= true;
15769 parm
= grokdeclarator (parameter_declarator
->declarator
,
15770 ¶meter_declarator
->decl_specifiers
,
15771 TPARM
, /*initialized=*/0,
15772 /*attrlist=*/NULL
);
15773 if (parm
== error_mark_node
)
15774 return error_mark_node
;
15776 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15779 /* Parse a type-parameter.
15782 class identifier [opt]
15783 class identifier [opt] = type-id
15784 typename identifier [opt]
15785 typename identifier [opt] = type-id
15786 template < template-parameter-list > class identifier [opt]
15787 template < template-parameter-list > class identifier [opt]
15790 GNU Extension (variadic templates):
15793 class ... identifier [opt]
15794 typename ... identifier [opt]
15796 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15797 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15798 the declaration of the parameter.
15800 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15803 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15808 /* Look for a keyword to tell us what kind of parameter this is. */
15809 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15811 return error_mark_node
;
15813 switch (token
->keyword
)
15819 tree default_argument
;
15821 /* If the next token is an ellipsis, we have a template
15823 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser
->lexer
);
15827 maybe_warn_variadic_templates ();
15829 *is_parameter_pack
= true;
15832 /* If the next token is an identifier, then it names the
15834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15835 identifier
= cp_parser_identifier (parser
);
15837 identifier
= NULL_TREE
;
15839 /* Create the parameter. */
15840 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15842 /* If the next token is an `=', we have a default argument. */
15843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15846 = cp_parser_default_type_template_argument (parser
);
15848 /* Template parameter packs cannot have default
15850 if (*is_parameter_pack
)
15853 error_at (token
->location
,
15854 "template parameter pack %qD cannot have a "
15855 "default argument", identifier
);
15857 error_at (token
->location
,
15858 "template parameter packs cannot have "
15859 "default arguments");
15860 default_argument
= NULL_TREE
;
15862 else if (check_for_bare_parameter_packs (default_argument
))
15863 default_argument
= error_mark_node
;
15866 default_argument
= NULL_TREE
;
15868 /* Create the combined representation of the parameter and the
15869 default argument. */
15870 parameter
= build_tree_list (default_argument
, parameter
);
15877 tree default_argument
;
15879 /* Look for the `<'. */
15880 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15881 /* Parse the template-parameter-list. */
15882 cp_parser_template_parameter_list (parser
);
15883 /* Look for the `>'. */
15884 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15886 // If template requirements are present, parse them.
15889 tree reqs
= get_shorthand_constraints (current_template_parms
);
15890 if (tree r
= cp_parser_requires_clause_opt (parser
))
15891 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15892 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15895 /* Look for the `class' or 'typename' keywords. */
15896 cp_parser_type_parameter_key (parser
);
15897 /* If the next token is an ellipsis, we have a template
15899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15901 /* Consume the `...' token. */
15902 cp_lexer_consume_token (parser
->lexer
);
15903 maybe_warn_variadic_templates ();
15905 *is_parameter_pack
= true;
15907 /* If the next token is an `=', then there is a
15908 default-argument. If the next token is a `>', we are at
15909 the end of the parameter-list. If the next token is a `,',
15910 then we are at the end of this parameter. */
15911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15912 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15913 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15915 identifier
= cp_parser_identifier (parser
);
15916 /* Treat invalid names as if the parameter were nameless. */
15917 if (identifier
== error_mark_node
)
15918 identifier
= NULL_TREE
;
15921 identifier
= NULL_TREE
;
15923 /* Create the template parameter. */
15924 parameter
= finish_template_template_parm (class_type_node
,
15927 /* If the next token is an `=', then there is a
15928 default-argument. */
15929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15932 = cp_parser_default_template_template_argument (parser
);
15934 /* Template parameter packs cannot have default
15936 if (*is_parameter_pack
)
15939 error_at (token
->location
,
15940 "template parameter pack %qD cannot "
15941 "have a default argument",
15944 error_at (token
->location
, "template parameter packs cannot "
15945 "have default arguments");
15946 default_argument
= NULL_TREE
;
15950 default_argument
= NULL_TREE
;
15952 /* Create the combined representation of the parameter and the
15953 default argument. */
15954 parameter
= build_tree_list (default_argument
, parameter
);
15959 gcc_unreachable ();
15966 /* Parse a template-id.
15969 template-name < template-argument-list [opt] >
15971 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15972 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15973 returned. Otherwise, if the template-name names a function, or set
15974 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15975 names a class, returns a TYPE_DECL for the specialization.
15977 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15978 uninstantiated templates. */
15981 cp_parser_template_id (cp_parser
*parser
,
15982 bool template_keyword_p
,
15983 bool check_dependency_p
,
15984 enum tag_types tag_type
,
15985 bool is_declaration
)
15990 cp_token_position start_of_id
= 0;
15991 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15992 bool is_identifier
;
15994 /* If the next token corresponds to a template-id, there is no need
15996 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15997 if (token
->type
== CPP_TEMPLATE_ID
)
15999 cp_lexer_consume_token (parser
->lexer
);
16000 return saved_checks_value (token
->u
.tree_check_value
);
16003 /* Avoid performing name lookup if there is no possibility of
16004 finding a template-id. */
16005 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16006 || (token
->type
== CPP_NAME
16007 && !cp_parser_nth_token_starts_template_argument_list_p
16010 cp_parser_error (parser
, "expected template-id");
16011 return error_mark_node
;
16014 /* Remember where the template-id starts. */
16015 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16016 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16018 push_deferring_access_checks (dk_deferred
);
16020 /* Parse the template-name. */
16021 is_identifier
= false;
16022 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16023 check_dependency_p
,
16027 if (templ
== error_mark_node
|| is_identifier
)
16029 pop_deferring_access_checks ();
16033 /* Since we're going to preserve any side-effects from this parse, set up a
16034 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16035 in the template arguments. */
16036 tentative_firewall
firewall (parser
);
16038 /* If we find the sequence `[:' after a template-name, it's probably
16039 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16040 parse correctly the argument list. */
16041 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16042 == CPP_OPEN_SQUARE
)
16043 && next_token
->flags
& DIGRAPH
16044 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16046 && !(next_token_2
->flags
& PREV_WHITE
))
16048 cp_parser_parse_tentatively (parser
);
16049 /* Change `:' into `::'. */
16050 next_token_2
->type
= CPP_SCOPE
;
16051 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16053 cp_lexer_consume_token (parser
->lexer
);
16055 /* Parse the arguments. */
16056 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16057 if (!cp_parser_parse_definitely (parser
))
16059 /* If we couldn't parse an argument list, then we revert our changes
16060 and return simply an error. Maybe this is not a template-id
16062 next_token_2
->type
= CPP_COLON
;
16063 cp_parser_error (parser
, "expected %<<%>");
16064 pop_deferring_access_checks ();
16065 return error_mark_node
;
16067 /* Otherwise, emit an error about the invalid digraph, but continue
16068 parsing because we got our argument list. */
16069 if (permerror (next_token
->location
,
16070 "%<<::%> cannot begin a template-argument list"))
16072 static bool hint
= false;
16073 inform (next_token
->location
,
16074 "%<<:%> is an alternate spelling for %<[%>."
16075 " Insert whitespace between %<<%> and %<::%>");
16076 if (!hint
&& !flag_permissive
)
16078 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16079 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16080 "accept your code)");
16087 /* Look for the `<' that starts the template-argument-list. */
16088 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16090 pop_deferring_access_checks ();
16091 return error_mark_node
;
16093 /* Parse the arguments. */
16094 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16096 if ((cxx_dialect
> cxx17
)
16097 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16098 && !template_keyword_p
16099 && (cp_parser_error_occurred (parser
)
16100 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16102 /* This didn't go well. */
16103 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16105 /* C++2A says that "function-name < a;" is now ill-formed. */
16106 if (cp_parser_error_occurred (parser
))
16108 error_at (token
->location
, "invalid template-argument-list");
16109 inform (token
->location
, "function name as the left hand "
16110 "operand of %<<%> is ill-formed in C++2a; wrap the "
16111 "function name in %<()%>");
16114 /* We expect "f<targs>" to be followed by "(args)". */
16115 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16116 "expected %<(%> after template-argument-list");
16118 /* Purge all subsequent tokens. */
16119 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16122 cp_parser_simulate_error (parser
);
16123 pop_deferring_access_checks ();
16124 return error_mark_node
;
16128 /* Set the location to be of the form:
16129 template-name < template-argument-list [opt] >
16130 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16131 with caret == start at the start of the template-name,
16132 ranging until the closing '>'. */
16133 location_t finish_loc
16134 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16135 location_t combined_loc
16136 = make_location (token
->location
, token
->location
, finish_loc
);
16138 /* Check for concepts autos where they don't belong. We could
16139 identify types in some cases of idnetifier TEMPL, looking ahead
16140 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16141 types. We reject them in functions, but if what we have is an
16142 identifier, even with none_type we can't conclude it's NOT a
16143 type, we have to wait for template substitution. */
16144 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16145 template_id
= error_mark_node
;
16146 /* Build a representation of the specialization. */
16147 else if (identifier_p (templ
))
16148 template_id
= build_min_nt_loc (combined_loc
,
16151 else if (DECL_TYPE_TEMPLATE_P (templ
)
16152 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16154 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16155 template (rather than some instantiation thereof) only if
16156 is not nested within some other construct. For example, in
16157 "template <typename T> void f(T) { A<T>::", A<T> is just an
16158 instantiation of A. */
16159 bool entering_scope
16160 = (template_parm_scope_p ()
16161 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16163 = finish_template_type (templ
, arguments
, entering_scope
);
16165 /* A template-like identifier may be a partial concept id. */
16166 else if (flag_concepts
16167 && (template_id
= (cp_parser_maybe_partial_concept_id
16168 (parser
, templ
, arguments
))))
16169 return template_id
;
16170 else if (variable_template_p (templ
))
16172 template_id
= lookup_template_variable (templ
, arguments
);
16173 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16174 SET_EXPR_LOCATION (template_id
, combined_loc
);
16178 /* If it's not a class-template or a template-template, it should be
16179 a function-template. */
16180 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16181 || TREE_CODE (templ
) == OVERLOAD
16182 || TREE_CODE (templ
) == FUNCTION_DECL
16183 || BASELINK_P (templ
)));
16185 template_id
= lookup_template_function (templ
, arguments
);
16186 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16187 SET_EXPR_LOCATION (template_id
, combined_loc
);
16190 /* If parsing tentatively, replace the sequence of tokens that makes
16191 up the template-id with a CPP_TEMPLATE_ID token. That way,
16192 should we re-parse the token stream, we will not have to repeat
16193 the effort required to do the parse, nor will we issue duplicate
16194 error messages about problems during instantiation of the
16197 /* Don't do this if we had a parse error in a declarator; re-parsing
16198 might succeed if a name changes meaning (60361). */
16199 && !(cp_parser_error_occurred (parser
)
16200 && cp_parser_parsing_tentatively (parser
)
16201 && parser
->in_declarator_p
))
16203 /* Reset the contents of the START_OF_ID token. */
16204 token
->type
= CPP_TEMPLATE_ID
;
16205 token
->location
= combined_loc
;
16207 /* Retrieve any deferred checks. Do not pop this access checks yet
16208 so the memory will not be reclaimed during token replacing below. */
16209 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16210 token
->u
.tree_check_value
->value
= template_id
;
16211 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16212 token
->keyword
= RID_MAX
;
16214 /* Purge all subsequent tokens. */
16215 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16217 /* ??? Can we actually assume that, if template_id ==
16218 error_mark_node, we will have issued a diagnostic to the
16219 user, as opposed to simply marking the tentative parse as
16221 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16222 error_at (token
->location
, "parse error in template argument list");
16225 pop_to_parent_deferring_access_checks ();
16226 return template_id
;
16229 /* Parse a template-name.
16234 The standard should actually say:
16238 operator-function-id
16240 A defect report has been filed about this issue.
16242 A conversion-function-id cannot be a template name because they cannot
16243 be part of a template-id. In fact, looking at this code:
16245 a.operator K<int>()
16247 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16248 It is impossible to call a templated conversion-function-id with an
16249 explicit argument list, since the only allowed template parameter is
16250 the type to which it is converting.
16252 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16253 `template' keyword, in a construction like:
16257 In that case `f' is taken to be a template-name, even though there
16258 is no way of knowing for sure.
16260 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16261 name refers to a set of overloaded functions, at least one of which
16262 is a template, or an IDENTIFIER_NODE with the name of the template,
16263 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16264 names are looked up inside uninstantiated templates. */
16267 cp_parser_template_name (cp_parser
* parser
,
16268 bool template_keyword_p
,
16269 bool check_dependency_p
,
16270 bool is_declaration
,
16271 enum tag_types tag_type
,
16272 bool *is_identifier
)
16276 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16278 /* If the next token is `operator', then we have either an
16279 operator-function-id or a conversion-function-id. */
16280 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16282 /* We don't know whether we're looking at an
16283 operator-function-id or a conversion-function-id. */
16284 cp_parser_parse_tentatively (parser
);
16285 /* Try an operator-function-id. */
16286 identifier
= cp_parser_operator_function_id (parser
);
16287 /* If that didn't work, try a conversion-function-id. */
16288 if (!cp_parser_parse_definitely (parser
))
16290 cp_parser_error (parser
, "expected template-name");
16291 return error_mark_node
;
16294 /* Look for the identifier. */
16296 identifier
= cp_parser_identifier (parser
);
16298 /* If we didn't find an identifier, we don't have a template-id. */
16299 if (identifier
== error_mark_node
)
16300 return error_mark_node
;
16302 /* If the name immediately followed the `template' keyword, then it
16303 is a template-name. However, if the next token is not `<', then
16304 we do not treat it as a template-name, since it is not being used
16305 as part of a template-id. This enables us to handle constructs
16308 template <typename T> struct S { S(); };
16309 template <typename T> S<T>::S();
16311 correctly. We would treat `S' as a template -- if it were `S<T>'
16312 -- but we do not if there is no `<'. */
16314 if (processing_template_decl
16315 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16317 /* In a declaration, in a dependent context, we pretend that the
16318 "template" keyword was present in order to improve error
16319 recovery. For example, given:
16321 template <typename T> void f(T::X<int>);
16323 we want to treat "X<int>" as a template-id. */
16325 && !template_keyword_p
16326 && parser
->scope
&& TYPE_P (parser
->scope
)
16327 && check_dependency_p
16328 && dependent_scope_p (parser
->scope
)
16329 /* Do not do this for dtors (or ctors), since they never
16330 need the template keyword before their name. */
16331 && !constructor_name_p (identifier
, parser
->scope
))
16333 cp_token_position start
= 0;
16335 /* Explain what went wrong. */
16336 error_at (token
->location
, "non-template %qD used as template",
16338 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16339 parser
->scope
, identifier
);
16340 /* If parsing tentatively, find the location of the "<" token. */
16341 if (cp_parser_simulate_error (parser
))
16342 start
= cp_lexer_token_position (parser
->lexer
, true);
16343 /* Parse the template arguments so that we can issue error
16344 messages about them. */
16345 cp_lexer_consume_token (parser
->lexer
);
16346 cp_parser_enclosed_template_argument_list (parser
);
16347 /* Skip tokens until we find a good place from which to
16348 continue parsing. */
16349 cp_parser_skip_to_closing_parenthesis (parser
,
16350 /*recovering=*/true,
16352 /*consume_paren=*/false);
16353 /* If parsing tentatively, permanently remove the
16354 template argument list. That will prevent duplicate
16355 error messages from being issued about the missing
16356 "template" keyword. */
16358 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16360 *is_identifier
= true;
16361 parser
->context
->object_type
= NULL_TREE
;
16365 /* If the "template" keyword is present, then there is generally
16366 no point in doing name-lookup, so we just return IDENTIFIER.
16367 But, if the qualifying scope is non-dependent then we can
16368 (and must) do name-lookup normally. */
16369 if (template_keyword_p
)
16371 tree scope
= (parser
->scope
? parser
->scope
16372 : parser
->context
->object_type
);
16373 if (scope
&& TYPE_P (scope
)
16374 && (!CLASS_TYPE_P (scope
)
16375 || (check_dependency_p
&& dependent_type_p (scope
))))
16377 /* We're optimizing away the call to cp_parser_lookup_name, but
16378 we still need to do this. */
16379 parser
->context
->object_type
= NULL_TREE
;
16385 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16386 const bool scoped_p
= ((parser
->scope
? parser
->scope
16387 : parser
->context
->object_type
) != NULL_TREE
);
16389 /* Look up the name. */
16390 decl
= cp_parser_lookup_name (parser
, identifier
,
16392 /*is_template=*/true,
16393 /*is_namespace=*/false,
16394 check_dependency_p
,
16395 /*ambiguous_decls=*/NULL
,
16398 decl
= strip_using_decl (decl
);
16400 /* If DECL is a template, then the name was a template-name. */
16401 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16403 if (TREE_DEPRECATED (decl
)
16404 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16405 warn_deprecated_use (decl
, NULL_TREE
);
16409 /* The standard does not explicitly indicate whether a name that
16410 names a set of overloaded declarations, some of which are
16411 templates, is a template-name. However, such a name should
16412 be a template-name; otherwise, there is no way to form a
16413 template-id for the overloaded templates. */
16414 bool found
= false;
16416 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16417 !found
&& iter
; ++iter
)
16418 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16422 && (cxx_dialect
> cxx17
)
16424 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
16426 /* [temp.names] says "A name is also considered to refer to a template
16427 if it is an unqualified-id followed by a < and name lookup finds
16428 either one or more functions or finds nothing." */
16430 /* The "more functions" case. Just use the OVERLOAD as normally.
16431 We don't use is_overloaded_fn here to avoid considering
16433 if (TREE_CODE (decl
) == OVERLOAD
16434 /* Name lookup found one function. */
16435 || TREE_CODE (decl
) == FUNCTION_DECL
)
16437 /* Name lookup found nothing. */
16438 else if (decl
== error_mark_node
)
16444 /* The name does not name a template. */
16445 cp_parser_error (parser
, "expected template-name");
16446 return error_mark_node
;
16453 /* Parse a template-argument-list.
16455 template-argument-list:
16456 template-argument ... [opt]
16457 template-argument-list , template-argument ... [opt]
16459 Returns a TREE_VEC containing the arguments. */
16462 cp_parser_template_argument_list (cp_parser
* parser
)
16464 tree fixed_args
[10];
16465 unsigned n_args
= 0;
16466 unsigned alloced
= 10;
16467 tree
*arg_ary
= fixed_args
;
16469 bool saved_in_template_argument_list_p
;
16471 bool saved_non_ice_p
;
16473 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16474 parser
->in_template_argument_list_p
= true;
16475 /* Even if the template-id appears in an integral
16476 constant-expression, the contents of the argument list do
16478 saved_ice_p
= parser
->integral_constant_expression_p
;
16479 parser
->integral_constant_expression_p
= false;
16480 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16481 parser
->non_integral_constant_expression_p
= false;
16483 /* Parse the arguments. */
16489 /* Consume the comma. */
16490 cp_lexer_consume_token (parser
->lexer
);
16492 /* Parse the template-argument. */
16493 argument
= cp_parser_template_argument (parser
);
16495 /* If the next token is an ellipsis, we're expanding a template
16497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16499 if (argument
== error_mark_node
)
16501 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16502 error_at (token
->location
,
16503 "expected parameter pack before %<...%>");
16505 /* Consume the `...' token. */
16506 cp_lexer_consume_token (parser
->lexer
);
16508 /* Make the argument into a TYPE_PACK_EXPANSION or
16509 EXPR_PACK_EXPANSION. */
16510 argument
= make_pack_expansion (argument
);
16513 if (n_args
== alloced
)
16517 if (arg_ary
== fixed_args
)
16519 arg_ary
= XNEWVEC (tree
, alloced
);
16520 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16523 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16525 arg_ary
[n_args
++] = argument
;
16527 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16529 vec
= make_tree_vec (n_args
);
16532 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16534 if (arg_ary
!= fixed_args
)
16536 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16537 parser
->integral_constant_expression_p
= saved_ice_p
;
16538 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16544 /* Parse a template-argument.
16547 assignment-expression
16551 The representation is that of an assignment-expression, type-id, or
16552 id-expression -- except that the qualified id-expression is
16553 evaluated, so that the value returned is either a DECL or an
16556 Although the standard says "assignment-expression", it forbids
16557 throw-expressions or assignments in the template argument.
16558 Therefore, we use "conditional-expression" instead. */
16561 cp_parser_template_argument (cp_parser
* parser
)
16566 bool maybe_type_id
= false;
16567 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16568 location_t loc
= 0;
16571 /* There's really no way to know what we're looking at, so we just
16572 try each alternative in order.
16576 In a template-argument, an ambiguity between a type-id and an
16577 expression is resolved to a type-id, regardless of the form of
16578 the corresponding template-parameter.
16580 Therefore, we try a type-id first. */
16581 cp_parser_parse_tentatively (parser
);
16582 argument
= cp_parser_template_type_arg (parser
);
16583 /* If there was no error parsing the type-id but the next token is a
16584 '>>', our behavior depends on which dialect of C++ we're
16585 parsing. In C++98, we probably found a typo for '> >'. But there
16586 are type-id which are also valid expressions. For instance:
16588 struct X { int operator >> (int); };
16589 template <int V> struct Foo {};
16592 Here 'X()' is a valid type-id of a function type, but the user just
16593 wanted to write the expression "X() >> 5". Thus, we remember that we
16594 found a valid type-id, but we still try to parse the argument as an
16595 expression to see what happens.
16597 In C++0x, the '>>' will be considered two separate '>'
16599 if (!cp_parser_error_occurred (parser
)
16600 && cxx_dialect
== cxx98
16601 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16603 maybe_type_id
= true;
16604 cp_parser_abort_tentative_parse (parser
);
16608 /* If the next token isn't a `,' or a `>', then this argument wasn't
16609 really finished. This means that the argument is not a valid
16611 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16612 cp_parser_error (parser
, "expected template-argument");
16613 /* If that worked, we're done. */
16614 if (cp_parser_parse_definitely (parser
))
16617 /* We're still not sure what the argument will be. */
16618 cp_parser_parse_tentatively (parser
);
16619 /* Try a template. */
16620 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16621 argument
= cp_parser_id_expression (parser
,
16622 /*template_keyword_p=*/false,
16623 /*check_dependency_p=*/true,
16625 /*declarator_p=*/false,
16626 /*optional_p=*/false);
16627 /* If the next token isn't a `,' or a `>', then this argument wasn't
16628 really finished. */
16629 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16630 cp_parser_error (parser
, "expected template-argument");
16631 if (!cp_parser_error_occurred (parser
))
16633 /* Figure out what is being referred to. If the id-expression
16634 was for a class template specialization, then we will have a
16635 TYPE_DECL at this point. There is no need to do name lookup
16636 at this point in that case. */
16637 if (TREE_CODE (argument
) != TYPE_DECL
)
16638 argument
= cp_parser_lookup_name (parser
, argument
,
16640 /*is_template=*/template_p
,
16641 /*is_namespace=*/false,
16642 /*check_dependency=*/true,
16643 /*ambiguous_decls=*/NULL
,
16644 argument_start_token
->location
);
16645 /* Handle a constrained-type-specifier for a non-type template
16647 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16649 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16650 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16651 cp_parser_error (parser
, "expected template-name");
16653 if (cp_parser_parse_definitely (parser
))
16655 if (TREE_DEPRECATED (argument
))
16656 warn_deprecated_use (argument
, NULL_TREE
);
16659 /* It must be a non-type argument. In C++17 any constant-expression is
16661 if (cxx_dialect
> cxx14
)
16664 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16666 -- an integral constant-expression of integral or enumeration
16669 -- the name of a non-type template-parameter; or
16671 -- the name of an object or function with external linkage...
16673 -- the address of an object or function with external linkage...
16675 -- a pointer to member... */
16676 /* Look for a non-type template parameter. */
16677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16679 cp_parser_parse_tentatively (parser
);
16680 argument
= cp_parser_primary_expression (parser
,
16681 /*address_p=*/false,
16683 /*template_arg_p=*/true,
16685 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16686 || !cp_parser_next_token_ends_template_argument_p (parser
))
16687 cp_parser_simulate_error (parser
);
16688 if (cp_parser_parse_definitely (parser
))
16692 /* If the next token is "&", the argument must be the address of an
16693 object or function with external linkage. */
16694 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16697 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16698 cp_lexer_consume_token (parser
->lexer
);
16700 /* See if we might have an id-expression. */
16701 token
= cp_lexer_peek_token (parser
->lexer
);
16702 if (token
->type
== CPP_NAME
16703 || token
->keyword
== RID_OPERATOR
16704 || token
->type
== CPP_SCOPE
16705 || token
->type
== CPP_TEMPLATE_ID
16706 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16708 cp_parser_parse_tentatively (parser
);
16709 argument
= cp_parser_primary_expression (parser
,
16712 /*template_arg_p=*/true,
16714 if (cp_parser_error_occurred (parser
)
16715 || !cp_parser_next_token_ends_template_argument_p (parser
))
16716 cp_parser_abort_tentative_parse (parser
);
16721 if (INDIRECT_REF_P (argument
))
16723 /* Strip the dereference temporarily. */
16724 gcc_assert (REFERENCE_REF_P (argument
));
16725 argument
= TREE_OPERAND (argument
, 0);
16728 /* If we're in a template, we represent a qualified-id referring
16729 to a static data member as a SCOPE_REF even if the scope isn't
16730 dependent so that we can check access control later. */
16732 if (TREE_CODE (probe
) == SCOPE_REF
)
16733 probe
= TREE_OPERAND (probe
, 1);
16736 /* A variable without external linkage might still be a
16737 valid constant-expression, so no error is issued here
16738 if the external-linkage check fails. */
16739 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16740 cp_parser_simulate_error (parser
);
16742 else if (is_overloaded_fn (argument
))
16743 /* All overloaded functions are allowed; if the external
16744 linkage test does not pass, an error will be issued
16748 && (TREE_CODE (argument
) == OFFSET_REF
16749 || TREE_CODE (argument
) == SCOPE_REF
))
16750 /* A pointer-to-member. */
16752 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16755 cp_parser_simulate_error (parser
);
16757 if (cp_parser_parse_definitely (parser
))
16760 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16761 tf_warning_or_error
);
16763 argument
= convert_from_reference (argument
);
16768 /* If the argument started with "&", there are no other valid
16769 alternatives at this point. */
16772 cp_parser_error (parser
, "invalid non-type template argument");
16773 return error_mark_node
;
16777 /* If the argument wasn't successfully parsed as a type-id followed
16778 by '>>', the argument can only be a constant expression now.
16779 Otherwise, we try parsing the constant-expression tentatively,
16780 because the argument could really be a type-id. */
16782 cp_parser_parse_tentatively (parser
);
16784 if (cxx_dialect
<= cxx14
)
16785 argument
= cp_parser_constant_expression (parser
);
16788 /* With C++17 generalized non-type template arguments we need to handle
16789 lvalue constant expressions, too. */
16790 argument
= cp_parser_assignment_expression (parser
);
16791 require_potential_constant_expression (argument
);
16794 if (!maybe_type_id
)
16796 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16797 cp_parser_error (parser
, "expected template-argument");
16798 if (cp_parser_parse_definitely (parser
))
16800 /* We did our best to parse the argument as a non type-id, but that
16801 was the only alternative that matched (albeit with a '>' after
16802 it). We can assume it's just a typo from the user, and a
16803 diagnostic will then be issued. */
16804 return cp_parser_template_type_arg (parser
);
16807 /* Parse an explicit-instantiation.
16809 explicit-instantiation:
16810 template declaration
16812 Although the standard says `declaration', what it really means is:
16814 explicit-instantiation:
16815 template decl-specifier-seq [opt] declarator [opt] ;
16817 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16818 supposed to be allowed. A defect report has been filed about this
16823 explicit-instantiation:
16824 storage-class-specifier template
16825 decl-specifier-seq [opt] declarator [opt] ;
16826 function-specifier template
16827 decl-specifier-seq [opt] declarator [opt] ; */
16830 cp_parser_explicit_instantiation (cp_parser
* parser
)
16832 int declares_class_or_enum
;
16833 cp_decl_specifier_seq decl_specifiers
;
16834 tree extension_specifier
= NULL_TREE
;
16836 timevar_push (TV_TEMPLATE_INST
);
16838 /* Look for an (optional) storage-class-specifier or
16839 function-specifier. */
16840 if (cp_parser_allow_gnu_extensions_p (parser
))
16842 extension_specifier
16843 = cp_parser_storage_class_specifier_opt (parser
);
16844 if (!extension_specifier
)
16845 extension_specifier
16846 = cp_parser_function_specifier_opt (parser
,
16847 /*decl_specs=*/NULL
);
16850 /* Look for the `template' keyword. */
16851 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16852 /* Let the front end know that we are processing an explicit
16854 begin_explicit_instantiation ();
16855 /* [temp.explicit] says that we are supposed to ignore access
16856 control while processing explicit instantiation directives. */
16857 push_deferring_access_checks (dk_no_check
);
16858 /* Parse a decl-specifier-seq. */
16859 cp_parser_decl_specifier_seq (parser
,
16860 CP_PARSER_FLAGS_OPTIONAL
,
16862 &declares_class_or_enum
);
16863 /* If there was exactly one decl-specifier, and it declared a class,
16864 and there's no declarator, then we have an explicit type
16866 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16870 type
= check_tag_decl (&decl_specifiers
,
16871 /*explicit_type_instantiation_p=*/true);
16872 /* Turn access control back on for names used during
16873 template instantiation. */
16874 pop_deferring_access_checks ();
16876 do_type_instantiation (type
, extension_specifier
,
16877 /*complain=*/tf_error
);
16881 cp_declarator
*declarator
;
16884 /* Parse the declarator. */
16886 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16887 /*ctor_dtor_or_conv_p=*/NULL
,
16888 /*parenthesized_p=*/NULL
,
16889 /*member_p=*/false,
16890 /*friend_p=*/false);
16891 if (declares_class_or_enum
& 2)
16892 cp_parser_check_for_definition_in_return_type (declarator
,
16893 decl_specifiers
.type
,
16894 decl_specifiers
.locations
[ds_type_spec
]);
16895 if (declarator
!= cp_error_declarator
)
16897 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16898 permerror (decl_specifiers
.locations
[ds_inline
],
16899 "explicit instantiation shall not use"
16900 " %<inline%> specifier");
16901 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16902 permerror (decl_specifiers
.locations
[ds_constexpr
],
16903 "explicit instantiation shall not use"
16904 " %<constexpr%> specifier");
16906 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16907 NORMAL
, 0, &decl_specifiers
.attributes
);
16908 /* Turn access control back on for names used during
16909 template instantiation. */
16910 pop_deferring_access_checks ();
16911 /* Do the explicit instantiation. */
16912 do_decl_instantiation (decl
, extension_specifier
);
16916 pop_deferring_access_checks ();
16917 /* Skip the body of the explicit instantiation. */
16918 cp_parser_skip_to_end_of_statement (parser
);
16921 /* We're done with the instantiation. */
16922 end_explicit_instantiation ();
16924 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16926 timevar_pop (TV_TEMPLATE_INST
);
16929 /* Parse an explicit-specialization.
16931 explicit-specialization:
16932 template < > declaration
16934 Although the standard says `declaration', what it really means is:
16936 explicit-specialization:
16937 template <> decl-specifier [opt] init-declarator [opt] ;
16938 template <> function-definition
16939 template <> explicit-specialization
16940 template <> template-declaration */
16943 cp_parser_explicit_specialization (cp_parser
* parser
)
16945 bool need_lang_pop
;
16946 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16948 /* Look for the `template' keyword. */
16949 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16950 /* Look for the `<'. */
16951 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16952 /* Look for the `>'. */
16953 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16954 /* We have processed another parameter list. */
16955 ++parser
->num_template_parameter_lists
;
16958 A template ... explicit specialization ... shall not have C
16960 if (current_lang_name
== lang_name_c
)
16962 error_at (token
->location
, "template specialization with C linkage");
16963 maybe_show_extern_c_location ();
16964 /* Give it C++ linkage to avoid confusing other parts of the
16966 push_lang_context (lang_name_cplusplus
);
16967 need_lang_pop
= true;
16970 need_lang_pop
= false;
16971 /* Let the front end know that we are beginning a specialization. */
16972 if (!begin_specialization ())
16974 end_specialization ();
16978 /* If the next keyword is `template', we need to figure out whether
16979 or not we're looking a template-declaration. */
16980 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16982 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16983 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16984 cp_parser_template_declaration_after_export (parser
,
16985 /*member_p=*/false);
16987 cp_parser_explicit_specialization (parser
);
16990 /* Parse the dependent declaration. */
16991 cp_parser_single_declaration (parser
,
16993 /*member_p=*/false,
16994 /*explicit_specialization_p=*/true,
16995 /*friend_p=*/NULL
);
16996 /* We're done with the specialization. */
16997 end_specialization ();
16998 /* For the erroneous case of a template with C linkage, we pushed an
16999 implicit C++ linkage scope; exit that scope now. */
17001 pop_lang_context ();
17002 /* We're done with this parameter list. */
17003 --parser
->num_template_parameter_lists
;
17006 /* Parse a type-specifier.
17009 simple-type-specifier
17012 elaborated-type-specifier
17020 Returns a representation of the type-specifier. For a
17021 class-specifier, enum-specifier, or elaborated-type-specifier, a
17022 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17024 The parser flags FLAGS is used to control type-specifier parsing.
17026 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17027 in a decl-specifier-seq.
17029 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17030 class-specifier, enum-specifier, or elaborated-type-specifier, then
17031 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17032 if a type is declared; 2 if it is defined. Otherwise, it is set to
17035 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17036 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17037 is set to FALSE. */
17040 cp_parser_type_specifier (cp_parser
* parser
,
17041 cp_parser_flags flags
,
17042 cp_decl_specifier_seq
*decl_specs
,
17043 bool is_declaration
,
17044 int* declares_class_or_enum
,
17045 bool* is_cv_qualifier
)
17047 tree type_spec
= NULL_TREE
;
17050 cp_decl_spec ds
= ds_last
;
17052 /* Assume this type-specifier does not declare a new type. */
17053 if (declares_class_or_enum
)
17054 *declares_class_or_enum
= 0;
17055 /* And that it does not specify a cv-qualifier. */
17056 if (is_cv_qualifier
)
17057 *is_cv_qualifier
= false;
17058 /* Peek at the next token. */
17059 token
= cp_lexer_peek_token (parser
->lexer
);
17061 /* If we're looking at a keyword, we can use that to guide the
17062 production we choose. */
17063 keyword
= token
->keyword
;
17067 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17068 goto elaborated_type_specifier
;
17070 /* Look for the enum-specifier. */
17071 type_spec
= cp_parser_enum_specifier (parser
);
17072 /* If that worked, we're done. */
17075 if (declares_class_or_enum
)
17076 *declares_class_or_enum
= 2;
17078 cp_parser_set_decl_spec_type (decl_specs
,
17081 /*type_definition_p=*/true);
17085 goto elaborated_type_specifier
;
17087 /* Any of these indicate either a class-specifier, or an
17088 elaborated-type-specifier. */
17092 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17093 goto elaborated_type_specifier
;
17095 /* Parse tentatively so that we can back up if we don't find a
17096 class-specifier. */
17097 cp_parser_parse_tentatively (parser
);
17098 /* Look for the class-specifier. */
17099 type_spec
= cp_parser_class_specifier (parser
);
17100 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17101 /* If that worked, we're done. */
17102 if (cp_parser_parse_definitely (parser
))
17104 if (declares_class_or_enum
)
17105 *declares_class_or_enum
= 2;
17107 cp_parser_set_decl_spec_type (decl_specs
,
17110 /*type_definition_p=*/true);
17114 /* Fall through. */
17115 elaborated_type_specifier
:
17116 /* We're declaring (not defining) a class or enum. */
17117 if (declares_class_or_enum
)
17118 *declares_class_or_enum
= 1;
17120 /* Fall through. */
17122 /* Look for an elaborated-type-specifier. */
17124 = (cp_parser_elaborated_type_specifier
17126 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17129 cp_parser_set_decl_spec_type (decl_specs
,
17132 /*type_definition_p=*/false);
17137 if (is_cv_qualifier
)
17138 *is_cv_qualifier
= true;
17143 if (is_cv_qualifier
)
17144 *is_cv_qualifier
= true;
17149 if (is_cv_qualifier
)
17150 *is_cv_qualifier
= true;
17154 /* The `__complex__' keyword is a GNU extension. */
17162 /* Handle simple keywords. */
17167 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17168 decl_specs
->any_specifiers_p
= true;
17170 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17173 /* If we do not already have a type-specifier, assume we are looking
17174 at a simple-type-specifier. */
17175 type_spec
= cp_parser_simple_type_specifier (parser
,
17179 /* If we didn't find a type-specifier, and a type-specifier was not
17180 optional in this context, issue an error message. */
17181 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17183 cp_parser_error (parser
, "expected type specifier");
17184 return error_mark_node
;
17190 /* Parse a simple-type-specifier.
17192 simple-type-specifier:
17193 :: [opt] nested-name-specifier [opt] type-name
17194 :: [opt] nested-name-specifier template template-id
17209 simple-type-specifier:
17211 decltype ( expression )
17214 __underlying_type ( type-id )
17218 nested-name-specifier(opt) template-name
17222 simple-type-specifier:
17224 __typeof__ unary-expression
17225 __typeof__ ( type-id )
17226 __typeof__ ( type-id ) { initializer-list , [opt] }
17228 Concepts Extension:
17230 simple-type-specifier:
17231 constrained-type-specifier
17233 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17234 appropriately updated. */
17237 cp_parser_simple_type_specifier (cp_parser
* parser
,
17238 cp_decl_specifier_seq
*decl_specs
,
17239 cp_parser_flags flags
)
17241 tree type
= NULL_TREE
;
17245 /* Peek at the next token. */
17246 token
= cp_lexer_peek_token (parser
->lexer
);
17248 /* If we're looking at a keyword, things are easy. */
17249 switch (token
->keyword
)
17253 decl_specs
->explicit_char_p
= true;
17254 type
= char_type_node
;
17257 type
= char16_type_node
;
17260 type
= char32_type_node
;
17263 type
= wchar_type_node
;
17266 type
= boolean_type_node
;
17269 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17270 type
= short_integer_type_node
;
17274 decl_specs
->explicit_int_p
= true;
17275 type
= integer_type_node
;
17281 idx
= token
->keyword
- RID_INT_N_0
;
17282 if (! int_n_enabled_p
[idx
])
17286 decl_specs
->explicit_intN_p
= true;
17287 decl_specs
->int_n_idx
= idx
;
17289 type
= int_n_trees
[idx
].signed_type
;
17293 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17294 type
= long_integer_type_node
;
17297 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17298 type
= integer_type_node
;
17301 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17302 type
= unsigned_type_node
;
17305 type
= float_type_node
;
17308 type
= double_type_node
;
17311 type
= void_type_node
;
17315 maybe_warn_cpp0x (CPP0X_AUTO
);
17316 if (parser
->auto_is_implicit_function_template_parm_p
)
17318 /* The 'auto' might be the placeholder return type for a function decl
17319 with trailing return type. */
17320 bool have_trailing_return_fn_decl
= false;
17322 cp_parser_parse_tentatively (parser
);
17323 cp_lexer_consume_token (parser
->lexer
);
17324 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17325 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17326 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17327 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17331 cp_lexer_consume_token (parser
->lexer
);
17332 cp_parser_skip_to_closing_parenthesis (parser
,
17333 /*recovering*/false,
17335 /*consume_paren*/true);
17339 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17341 have_trailing_return_fn_decl
= true;
17345 cp_lexer_consume_token (parser
->lexer
);
17347 cp_parser_abort_tentative_parse (parser
);
17349 if (have_trailing_return_fn_decl
)
17351 type
= make_auto ();
17355 if (cxx_dialect
>= cxx14
)
17357 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17358 type
= TREE_TYPE (type
);
17361 type
= error_mark_node
;
17363 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17365 if (cxx_dialect
< cxx14
)
17366 error_at (token
->location
,
17367 "use of %<auto%> in lambda parameter declaration "
17368 "only available with "
17369 "-std=c++14 or -std=gnu++14");
17371 else if (cxx_dialect
< cxx14
)
17372 error_at (token
->location
,
17373 "use of %<auto%> in parameter declaration "
17374 "only available with "
17375 "-std=c++14 or -std=gnu++14");
17376 else if (!flag_concepts
)
17377 pedwarn (token
->location
, 0,
17378 "use of %<auto%> in parameter declaration "
17379 "only available with -fconcepts");
17382 type
= make_auto ();
17386 /* Since DR 743, decltype can either be a simple-type-specifier by
17387 itself or begin a nested-name-specifier. Parsing it will replace
17388 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17389 handling below decide what to do. */
17390 cp_parser_decltype (parser
);
17391 cp_lexer_set_token_position (parser
->lexer
, token
);
17395 /* Consume the `typeof' token. */
17396 cp_lexer_consume_token (parser
->lexer
);
17397 /* Parse the operand to `typeof'. */
17398 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17399 /* If it is not already a TYPE, take its type. */
17400 if (!TYPE_P (type
))
17401 type
= finish_typeof (type
);
17404 cp_parser_set_decl_spec_type (decl_specs
, type
,
17406 /*type_definition_p=*/false);
17410 case RID_UNDERLYING_TYPE
:
17411 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17413 cp_parser_set_decl_spec_type (decl_specs
, type
,
17415 /*type_definition_p=*/false);
17420 case RID_DIRECT_BASES
:
17421 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17423 cp_parser_set_decl_spec_type (decl_specs
, type
,
17425 /*type_definition_p=*/false);
17431 /* If token is an already-parsed decltype not followed by ::,
17432 it's a simple-type-specifier. */
17433 if (token
->type
== CPP_DECLTYPE
17434 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17436 type
= saved_checks_value (token
->u
.tree_check_value
);
17439 cp_parser_set_decl_spec_type (decl_specs
, type
,
17441 /*type_definition_p=*/false);
17442 /* Remember that we are handling a decltype in order to
17443 implement the resolution of DR 1510 when the argument
17444 isn't instantiation dependent. */
17445 decl_specs
->decltype_p
= true;
17447 cp_lexer_consume_token (parser
->lexer
);
17451 /* If the type-specifier was for a built-in type, we're done. */
17454 /* Record the type. */
17456 && (token
->keyword
!= RID_SIGNED
17457 && token
->keyword
!= RID_UNSIGNED
17458 && token
->keyword
!= RID_SHORT
17459 && token
->keyword
!= RID_LONG
))
17460 cp_parser_set_decl_spec_type (decl_specs
,
17463 /*type_definition_p=*/false);
17465 decl_specs
->any_specifiers_p
= true;
17467 /* Consume the token. */
17468 cp_lexer_consume_token (parser
->lexer
);
17470 if (type
== error_mark_node
)
17471 return error_mark_node
;
17473 /* There is no valid C++ program where a non-template type is
17474 followed by a "<". That usually indicates that the user thought
17475 that the type was a template. */
17476 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17479 return TYPE_NAME (type
);
17482 /* The type-specifier must be a user-defined type. */
17483 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17488 /* Don't gobble tokens or issue error messages if this is an
17489 optional type-specifier. */
17490 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17491 cp_parser_parse_tentatively (parser
);
17493 token
= cp_lexer_peek_token (parser
->lexer
);
17495 /* Look for the optional `::' operator. */
17497 = (cp_parser_global_scope_opt (parser
,
17498 /*current_scope_valid_p=*/false)
17500 /* Look for the nested-name specifier. */
17502 = (cp_parser_nested_name_specifier_opt (parser
,
17503 /*typename_keyword_p=*/false,
17504 /*check_dependency_p=*/true,
17506 /*is_declaration=*/false)
17508 /* If we have seen a nested-name-specifier, and the next token
17509 is `template', then we are using the template-id production. */
17511 && cp_parser_optional_template_keyword (parser
))
17513 /* Look for the template-id. */
17514 type
= cp_parser_template_id (parser
,
17515 /*template_keyword_p=*/true,
17516 /*check_dependency_p=*/true,
17518 /*is_declaration=*/false);
17519 /* If the template-id did not name a type, we are out of
17521 if (TREE_CODE (type
) != TYPE_DECL
)
17523 cp_parser_error (parser
, "expected template-id for type");
17527 /* Otherwise, look for a type-name. */
17529 type
= cp_parser_type_name (parser
);
17530 /* Keep track of all name-lookups performed in class scopes. */
17534 && TREE_CODE (type
) == TYPE_DECL
17535 && identifier_p (DECL_NAME (type
)))
17536 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17537 /* If it didn't work out, we don't have a TYPE. */
17538 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17539 && !cp_parser_parse_definitely (parser
))
17541 if (!type
&& cxx_dialect
>= cxx17
)
17543 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17544 cp_parser_parse_tentatively (parser
);
17546 cp_parser_global_scope_opt (parser
,
17547 /*current_scope_valid_p=*/false);
17548 cp_parser_nested_name_specifier_opt (parser
,
17549 /*typename_keyword_p=*/false,
17550 /*check_dependency_p=*/true,
17552 /*is_declaration=*/false);
17553 tree name
= cp_parser_identifier (parser
);
17554 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17555 && parser
->scope
!= error_mark_node
)
17557 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17559 /*is_template=*/false,
17560 /*is_namespace=*/false,
17561 /*check_dependency=*/true,
17562 /*ambiguous_decls=*/NULL
,
17564 if (tmpl
&& tmpl
!= error_mark_node
17565 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17566 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17567 type
= make_template_placeholder (tmpl
);
17570 type
= error_mark_node
;
17571 if (!cp_parser_simulate_error (parser
))
17572 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17573 NLE_TYPE
, token
->location
);
17577 type
= error_mark_node
;
17579 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17580 && !cp_parser_parse_definitely (parser
))
17583 if (type
&& decl_specs
)
17584 cp_parser_set_decl_spec_type (decl_specs
, type
,
17586 /*type_definition_p=*/false);
17589 /* If we didn't get a type-name, issue an error message. */
17590 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17592 cp_parser_error (parser
, "expected type-name");
17593 return error_mark_node
;
17596 if (type
&& type
!= error_mark_node
)
17598 /* See if TYPE is an Objective-C type, and if so, parse and
17599 accept any protocol references following it. Do this before
17600 the cp_parser_check_for_invalid_template_id() call, because
17601 Objective-C types can be followed by '<...>' which would
17602 enclose protocol names rather than template arguments, and so
17603 everything is fine. */
17604 if (c_dialect_objc () && !parser
->scope
17605 && (objc_is_id (type
) || objc_is_class_name (type
)))
17607 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17608 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17610 /* Clobber the "unqualified" type previously entered into
17611 DECL_SPECS with the new, improved protocol-qualified version. */
17613 decl_specs
->type
= qual_type
;
17618 /* There is no valid C++ program where a non-template type is
17619 followed by a "<". That usually indicates that the user
17620 thought that the type was a template. */
17621 cp_parser_check_for_invalid_template_id (parser
, type
,
17629 /* Parse a type-name.
17635 simple-template-id [in c++0x]
17652 Returns a TYPE_DECL for the type. */
17655 cp_parser_type_name (cp_parser
* parser
)
17657 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17662 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17666 /* We can't know yet whether it is a class-name or not. */
17667 cp_parser_parse_tentatively (parser
);
17668 /* Try a class-name. */
17669 type_decl
= cp_parser_class_name (parser
,
17670 typename_keyword_p
,
17671 /*template_keyword_p=*/false,
17673 /*check_dependency_p=*/true,
17674 /*class_head_p=*/false,
17675 /*is_declaration=*/false);
17676 /* If it's not a class-name, keep looking. */
17677 if (!cp_parser_parse_definitely (parser
))
17679 if (cxx_dialect
< cxx11
)
17680 /* It must be a typedef-name or an enum-name. */
17681 return cp_parser_nonclass_name (parser
);
17683 cp_parser_parse_tentatively (parser
);
17684 /* It is either a simple-template-id representing an
17685 instantiation of an alias template... */
17686 type_decl
= cp_parser_template_id (parser
,
17687 /*template_keyword_p=*/false,
17688 /*check_dependency_p=*/true,
17690 /*is_declaration=*/false);
17691 /* Note that this must be an instantiation of an alias template
17692 because [temp.names]/6 says:
17694 A template-id that names an alias template specialization
17697 Whereas [temp.names]/7 says:
17699 A simple-template-id that names a class template
17700 specialization is a class-name.
17702 With concepts, this could also be a partial-concept-id that
17703 declares a non-type template parameter. */
17704 if (type_decl
!= NULL_TREE
17705 && TREE_CODE (type_decl
) == TYPE_DECL
17706 && TYPE_DECL_ALIAS_P (type_decl
))
17707 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17708 else if (is_constrained_parameter (type_decl
))
17709 /* Don't do anything. */ ;
17711 cp_parser_simulate_error (parser
);
17713 if (!cp_parser_parse_definitely (parser
))
17714 /* ... Or a typedef-name or an enum-name. */
17715 return cp_parser_nonclass_name (parser
);
17721 /* Check if DECL and ARGS can form a constrained-type-specifier.
17722 If ARGS is non-null, we try to form a concept check of the
17723 form DECL<?, ARGS> where ? is a wildcard that matches any
17724 kind of template argument. If ARGS is NULL, then we try to
17725 form a concept check of the form DECL<?>. */
17728 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17729 tree decl
, tree args
)
17731 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17733 /* If we a constrained-type-specifier cannot be deduced. */
17734 if (parser
->prevent_constrained_type_specifiers
)
17737 /* A constrained type specifier can only be found in an
17738 overload set or as a reference to a template declaration.
17740 FIXME: This might be masking a bug. It's possible that
17741 that the deduction below is causing template specializations
17742 to be formed with the wildcard as an argument. */
17743 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17746 /* Try to build a call expression that evaluates the
17747 concept. This can fail if the overload set refers
17748 only to non-templates. */
17749 tree placeholder
= build_nt (WILDCARD_DECL
);
17750 tree check
= build_concept_check (decl
, placeholder
, args
);
17751 if (check
== error_mark_node
)
17754 /* Deduce the checked constraint and the prototype parameter.
17756 FIXME: In certain cases, failure to deduce should be a
17757 diagnosable error. */
17760 if (!deduce_constrained_parameter (check
, conc
, proto
))
17763 /* In template parameter scope, this results in a constrained
17764 parameter. Return a descriptor of that parm. */
17765 if (processing_template_parmlist
)
17766 return build_constrained_parameter (conc
, proto
, args
);
17768 /* In a parameter-declaration-clause, constrained-type
17769 specifiers result in invented template parameters. */
17770 if (parser
->auto_is_implicit_function_template_parm_p
)
17772 tree x
= build_constrained_parameter (conc
, proto
, args
);
17773 return synthesize_implicit_template_parm (parser
, x
);
17777 /* Otherwise, we're in a context where the constrained
17778 type name is deduced and the constraint applies
17779 after deduction. */
17780 return make_constrained_auto (conc
, args
);
17786 /* If DECL refers to a concept, return a TYPE_DECL representing
17787 the result of using the constrained type specifier in the
17788 current context. DECL refers to a concept if
17790 - it is an overload set containing a function concept taking a single
17793 - it is a variable concept taking a single type argument. */
17796 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17799 && (TREE_CODE (decl
) == OVERLOAD
17800 || BASELINK_P (decl
)
17801 || variable_concept_p (decl
)))
17802 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17807 /* Check if DECL and ARGS form a partial-concept-id. If so,
17808 assign ID to the resulting constrained placeholder.
17810 Returns true if the partial-concept-id designates a placeholder
17811 and false otherwise. Note that *id is set to NULL_TREE in
17815 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17817 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17820 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17832 Returns a TYPE_DECL for the type. */
17835 cp_parser_nonclass_name (cp_parser
* parser
)
17840 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17841 identifier
= cp_parser_identifier (parser
);
17842 if (identifier
== error_mark_node
)
17843 return error_mark_node
;
17845 /* Look up the type-name. */
17846 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17848 type_decl
= strip_using_decl (type_decl
);
17850 /* If we found an overload set, then it may refer to a concept-name. */
17851 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17854 if (TREE_CODE (type_decl
) != TYPE_DECL
17855 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17857 /* See if this is an Objective-C type. */
17858 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17859 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17861 type_decl
= TYPE_NAME (type
);
17864 /* Issue an error if we did not find a type-name. */
17865 if (TREE_CODE (type_decl
) != TYPE_DECL
17866 /* In Objective-C, we have the complication that class names are
17867 normally type names and start declarations (eg, the
17868 "NSObject" in "NSObject *object;"), but can be used in an
17869 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17870 is an expression. So, a classname followed by a dot is not a
17871 valid type-name. */
17872 || (objc_is_class_name (TREE_TYPE (type_decl
))
17873 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17875 if (!cp_parser_simulate_error (parser
))
17876 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17877 NLE_TYPE
, token
->location
);
17878 return error_mark_node
;
17880 /* Remember that the name was used in the definition of the
17881 current class so that we can check later to see if the
17882 meaning would have been different after the class was
17883 entirely defined. */
17884 else if (type_decl
!= error_mark_node
17886 maybe_note_name_used_in_class (identifier
, type_decl
);
17891 /* Parse an elaborated-type-specifier. Note that the grammar given
17892 here incorporates the resolution to DR68.
17894 elaborated-type-specifier:
17895 class-key :: [opt] nested-name-specifier [opt] identifier
17896 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17897 enum-key :: [opt] nested-name-specifier [opt] identifier
17898 typename :: [opt] nested-name-specifier identifier
17899 typename :: [opt] nested-name-specifier template [opt]
17904 elaborated-type-specifier:
17905 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17906 class-key attributes :: [opt] nested-name-specifier [opt]
17907 template [opt] template-id
17908 enum attributes :: [opt] nested-name-specifier [opt] identifier
17910 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17911 declared `friend'. If IS_DECLARATION is TRUE, then this
17912 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17913 something is being declared.
17915 Returns the TYPE specified. */
17918 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17920 bool is_declaration
)
17922 enum tag_types tag_type
;
17924 tree type
= NULL_TREE
;
17925 tree attributes
= NULL_TREE
;
17927 cp_token
*token
= NULL
;
17929 /* See if we're looking at the `enum' keyword. */
17930 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17932 /* Consume the `enum' token. */
17933 cp_lexer_consume_token (parser
->lexer
);
17934 /* Remember that it's an enumeration type. */
17935 tag_type
= enum_type
;
17936 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17937 enums) is used here. */
17938 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17939 if (cp_parser_is_keyword (token
, RID_CLASS
)
17940 || cp_parser_is_keyword (token
, RID_STRUCT
))
17942 gcc_rich_location
richloc (token
->location
);
17943 richloc
.add_range (input_location
);
17944 richloc
.add_fixit_remove ();
17945 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17946 "a scoped enum must not use the %qD keyword",
17948 /* Consume the `struct' or `class' and parse it anyway. */
17949 cp_lexer_consume_token (parser
->lexer
);
17951 /* Parse the attributes. */
17952 attributes
= cp_parser_attributes_opt (parser
);
17954 /* Or, it might be `typename'. */
17955 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17958 /* Consume the `typename' token. */
17959 cp_lexer_consume_token (parser
->lexer
);
17960 /* Remember that it's a `typename' type. */
17961 tag_type
= typename_type
;
17963 /* Otherwise it must be a class-key. */
17966 tag_type
= cp_parser_class_key (parser
);
17967 if (tag_type
== none_type
)
17968 return error_mark_node
;
17969 /* Parse the attributes. */
17970 attributes
= cp_parser_attributes_opt (parser
);
17973 /* Look for the `::' operator. */
17974 globalscope
= cp_parser_global_scope_opt (parser
,
17975 /*current_scope_valid_p=*/false);
17976 /* Look for the nested-name-specifier. */
17977 tree nested_name_specifier
;
17978 if (tag_type
== typename_type
&& !globalscope
)
17980 nested_name_specifier
17981 = cp_parser_nested_name_specifier (parser
,
17982 /*typename_keyword_p=*/true,
17983 /*check_dependency_p=*/true,
17986 if (!nested_name_specifier
)
17987 return error_mark_node
;
17990 /* Even though `typename' is not present, the proposed resolution
17991 to Core Issue 180 says that in `class A<T>::B', `B' should be
17992 considered a type-name, even if `A<T>' is dependent. */
17993 nested_name_specifier
17994 = cp_parser_nested_name_specifier_opt (parser
,
17995 /*typename_keyword_p=*/true,
17996 /*check_dependency_p=*/true,
17999 /* For everything but enumeration types, consider a template-id.
18000 For an enumeration type, consider only a plain identifier. */
18001 if (tag_type
!= enum_type
)
18003 bool template_p
= false;
18006 /* Allow the `template' keyword. */
18007 template_p
= cp_parser_optional_template_keyword (parser
);
18008 /* If we didn't see `template', we don't know if there's a
18009 template-id or not. */
18011 cp_parser_parse_tentatively (parser
);
18012 /* The `template' keyword must follow a nested-name-specifier. */
18013 else if (!nested_name_specifier
)
18015 cp_parser_error (parser
, "%<template%> must follow a nested-"
18017 return error_mark_node
;
18020 /* Parse the template-id. */
18021 token
= cp_lexer_peek_token (parser
->lexer
);
18022 decl
= cp_parser_template_id (parser
, template_p
,
18023 /*check_dependency_p=*/true,
18026 /* If we didn't find a template-id, look for an ordinary
18028 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18030 /* We can get here when cp_parser_template_id, called by
18031 cp_parser_class_name with tag_type == none_type, succeeds
18032 and caches a BASELINK. Then, when called again here,
18033 instead of failing and returning an error_mark_node
18034 returns it (see template/typename17.C in C++11).
18035 ??? Could we diagnose this earlier? */
18036 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18038 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18039 type
= error_mark_node
;
18041 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18042 in effect, then we must assume that, upon instantiation, the
18043 template will correspond to a class. */
18044 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18045 && tag_type
== typename_type
)
18046 type
= make_typename_type (parser
->scope
, decl
,
18048 /*complain=*/tf_error
);
18049 /* If the `typename' keyword is in effect and DECL is not a type
18050 decl, then type is non existent. */
18051 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18053 else if (TREE_CODE (decl
) == TYPE_DECL
)
18055 type
= check_elaborated_type_specifier (tag_type
, decl
,
18056 /*allow_template_p=*/true);
18058 /* If the next token is a semicolon, this must be a specialization,
18059 instantiation, or friend declaration. Check the scope while we
18060 still know whether or not we had a nested-name-specifier. */
18061 if (type
!= error_mark_node
18062 && !nested_name_specifier
&& !is_friend
18063 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18064 check_unqualified_spec_or_inst (type
, token
->location
);
18066 else if (decl
== error_mark_node
)
18067 type
= error_mark_node
;
18072 token
= cp_lexer_peek_token (parser
->lexer
);
18073 identifier
= cp_parser_identifier (parser
);
18075 if (identifier
== error_mark_node
)
18077 parser
->scope
= NULL_TREE
;
18078 return error_mark_node
;
18081 /* For a `typename', we needn't call xref_tag. */
18082 if (tag_type
== typename_type
18083 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18084 return cp_parser_make_typename_type (parser
, identifier
,
18087 /* Template parameter lists apply only if we are not within a
18088 function parameter list. */
18089 bool template_parm_lists_apply
18090 = parser
->num_template_parameter_lists
;
18091 if (template_parm_lists_apply
)
18092 for (cp_binding_level
*s
= current_binding_level
;
18093 s
&& s
->kind
!= sk_template_parms
;
18094 s
= s
->level_chain
)
18095 if (s
->kind
== sk_function_parms
)
18096 template_parm_lists_apply
= false;
18098 /* Look up a qualified name in the usual way. */
18102 tree ambiguous_decls
;
18104 decl
= cp_parser_lookup_name (parser
, identifier
,
18106 /*is_template=*/false,
18107 /*is_namespace=*/false,
18108 /*check_dependency=*/true,
18112 /* If the lookup was ambiguous, an error will already have been
18114 if (ambiguous_decls
)
18115 return error_mark_node
;
18117 /* If we are parsing friend declaration, DECL may be a
18118 TEMPLATE_DECL tree node here. However, we need to check
18119 whether this TEMPLATE_DECL results in valid code. Consider
18120 the following example:
18123 template <class T> class C {};
18126 template <class T> friend class N::C; // #1, valid code
18128 template <class T> class Y {
18129 friend class N::C; // #2, invalid code
18132 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18133 name lookup of `N::C'. We see that friend declaration must
18134 be template for the code to be valid. Note that
18135 processing_template_decl does not work here since it is
18136 always 1 for the above two cases. */
18138 decl
= (cp_parser_maybe_treat_template_as_class
18139 (decl
, /*tag_name_p=*/is_friend
18140 && template_parm_lists_apply
));
18142 if (TREE_CODE (decl
) != TYPE_DECL
)
18144 cp_parser_diagnose_invalid_type_name (parser
,
18147 return error_mark_node
;
18150 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18152 bool allow_template
= (template_parm_lists_apply
18153 || DECL_SELF_REFERENCE_P (decl
));
18154 type
= check_elaborated_type_specifier (tag_type
, decl
,
18157 if (type
== error_mark_node
)
18158 return error_mark_node
;
18161 /* Forward declarations of nested types, such as
18166 are invalid unless all components preceding the final '::'
18167 are complete. If all enclosing types are complete, these
18168 declarations become merely pointless.
18170 Invalid forward declarations of nested types are errors
18171 caught elsewhere in parsing. Those that are pointless arrive
18174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18175 && !is_friend
&& !processing_explicit_instantiation
)
18176 warning (0, "declaration %qD does not declare anything", decl
);
18178 type
= TREE_TYPE (decl
);
18182 /* An elaborated-type-specifier sometimes introduces a new type and
18183 sometimes names an existing type. Normally, the rule is that it
18184 introduces a new type only if there is not an existing type of
18185 the same name already in scope. For example, given:
18188 void f() { struct S s; }
18190 the `struct S' in the body of `f' is the same `struct S' as in
18191 the global scope; the existing definition is used. However, if
18192 there were no global declaration, this would introduce a new
18193 local class named `S'.
18195 An exception to this rule applies to the following code:
18197 namespace N { struct S; }
18199 Here, the elaborated-type-specifier names a new type
18200 unconditionally; even if there is already an `S' in the
18201 containing scope this declaration names a new type.
18202 This exception only applies if the elaborated-type-specifier
18203 forms the complete declaration:
18207 A declaration consisting solely of `class-key identifier ;' is
18208 either a redeclaration of the name in the current scope or a
18209 forward declaration of the identifier as a class name. It
18210 introduces the name into the current scope.
18212 We are in this situation precisely when the next token is a `;'.
18214 An exception to the exception is that a `friend' declaration does
18215 *not* name a new type; i.e., given:
18217 struct S { friend struct T; };
18219 `T' is not a new type in the scope of `S'.
18221 Also, `new struct S' or `sizeof (struct S)' never results in the
18222 definition of a new type; a new type can only be declared in a
18223 declaration context. */
18229 /* Friends have special name lookup rules. */
18230 ts
= ts_within_enclosing_non_class
;
18231 else if (is_declaration
18232 && cp_lexer_next_token_is (parser
->lexer
,
18234 /* This is a `class-key identifier ;' */
18240 (template_parm_lists_apply
18241 && (cp_parser_next_token_starts_class_definition_p (parser
)
18242 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18243 /* An unqualified name was used to reference this type, so
18244 there were no qualifying templates. */
18245 if (template_parm_lists_apply
18246 && !cp_parser_check_template_parameters (parser
,
18247 /*num_templates=*/0,
18248 /*template_id*/false,
18250 /*declarator=*/NULL
))
18251 return error_mark_node
;
18252 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18256 if (type
== error_mark_node
)
18257 return error_mark_node
;
18259 /* Allow attributes on forward declarations of classes. */
18262 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18263 warning (OPT_Wattributes
,
18264 "attributes ignored on uninstantiated type");
18265 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18266 && ! processing_explicit_instantiation
)
18267 warning (OPT_Wattributes
,
18268 "attributes ignored on template instantiation");
18269 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18270 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18272 warning (OPT_Wattributes
,
18273 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18276 if (tag_type
!= enum_type
)
18278 /* Indicate whether this class was declared as a `class' or as a
18280 if (CLASS_TYPE_P (type
))
18281 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18282 cp_parser_check_class_key (tag_type
, type
);
18285 /* A "<" cannot follow an elaborated type specifier. If that
18286 happens, the user was probably trying to form a template-id. */
18287 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18293 /* Parse an enum-specifier.
18296 enum-head { enumerator-list [opt] }
18297 enum-head { enumerator-list , } [C++0x]
18300 enum-key identifier [opt] enum-base [opt]
18301 enum-key nested-name-specifier identifier enum-base [opt]
18306 enum struct [C++0x]
18309 : type-specifier-seq
18311 opaque-enum-specifier:
18312 enum-key identifier enum-base [opt] ;
18315 enum-key attributes[opt] identifier [opt] enum-base [opt]
18316 { enumerator-list [opt] }attributes[opt]
18317 enum-key attributes[opt] identifier [opt] enum-base [opt]
18318 { enumerator-list, }attributes[opt] [C++0x]
18320 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18321 if the token stream isn't an enum-specifier after all. */
18324 cp_parser_enum_specifier (cp_parser
* parser
)
18327 tree type
= NULL_TREE
;
18329 tree nested_name_specifier
= NULL_TREE
;
18331 bool scoped_enum_p
= false;
18332 bool has_underlying_type
= false;
18333 bool nested_being_defined
= false;
18334 bool new_value_list
= false;
18335 bool is_new_type
= false;
18336 bool is_unnamed
= false;
18337 tree underlying_type
= NULL_TREE
;
18338 cp_token
*type_start_token
= NULL
;
18339 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18341 parser
->colon_corrects_to_scope_p
= false;
18343 /* Parse tentatively so that we can back up if we don't find a
18345 cp_parser_parse_tentatively (parser
);
18347 /* Caller guarantees that the current token is 'enum', an identifier
18348 possibly follows, and the token after that is an opening brace.
18349 If we don't have an identifier, fabricate an anonymous name for
18350 the enumeration being defined. */
18351 cp_lexer_consume_token (parser
->lexer
);
18353 /* Parse the "class" or "struct", which indicates a scoped
18354 enumeration type in C++0x. */
18355 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18356 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18358 if (cxx_dialect
< cxx11
)
18359 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18361 /* Consume the `struct' or `class' token. */
18362 cp_lexer_consume_token (parser
->lexer
);
18364 scoped_enum_p
= true;
18367 attributes
= cp_parser_attributes_opt (parser
);
18369 /* Clear the qualification. */
18370 parser
->scope
= NULL_TREE
;
18371 parser
->qualifying_scope
= NULL_TREE
;
18372 parser
->object_scope
= NULL_TREE
;
18374 /* Figure out in what scope the declaration is being placed. */
18375 prev_scope
= current_scope ();
18377 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18379 push_deferring_access_checks (dk_no_check
);
18380 nested_name_specifier
18381 = cp_parser_nested_name_specifier_opt (parser
,
18382 /*typename_keyword_p=*/true,
18383 /*check_dependency_p=*/false,
18385 /*is_declaration=*/false);
18387 if (nested_name_specifier
)
18391 identifier
= cp_parser_identifier (parser
);
18392 name
= cp_parser_lookup_name (parser
, identifier
,
18394 /*is_template=*/false,
18395 /*is_namespace=*/false,
18396 /*check_dependency=*/true,
18397 /*ambiguous_decls=*/NULL
,
18399 if (name
&& name
!= error_mark_node
)
18401 type
= TREE_TYPE (name
);
18402 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18404 /* Are template enums allowed in ISO? */
18405 if (template_parm_scope_p ())
18406 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18407 "%qD is an enumeration template", name
);
18408 /* ignore a typename reference, for it will be solved by name
18413 else if (nested_name_specifier
== error_mark_node
)
18414 /* We already issued an error. */;
18417 error_at (type_start_token
->location
,
18418 "%qD does not name an enumeration in %qT",
18419 identifier
, nested_name_specifier
);
18420 nested_name_specifier
= error_mark_node
;
18425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18426 identifier
= cp_parser_identifier (parser
);
18429 identifier
= make_anon_name ();
18432 error_at (type_start_token
->location
,
18433 "unnamed scoped enum is not allowed");
18436 pop_deferring_access_checks ();
18438 /* Check for the `:' that denotes a specified underlying type in C++0x.
18439 Note that a ':' could also indicate a bitfield width, however. */
18440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18442 cp_decl_specifier_seq type_specifiers
;
18444 /* Consume the `:'. */
18445 cp_lexer_consume_token (parser
->lexer
);
18447 /* Parse the type-specifier-seq. */
18448 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18449 /*is_trailing_return=*/false,
18452 /* At this point this is surely not elaborated type specifier. */
18453 if (!cp_parser_parse_definitely (parser
))
18456 if (cxx_dialect
< cxx11
)
18457 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18459 has_underlying_type
= true;
18461 /* If that didn't work, stop. */
18462 if (type_specifiers
.type
!= error_mark_node
)
18464 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18465 /*initialized=*/0, NULL
);
18466 if (underlying_type
== error_mark_node
18467 || check_for_bare_parameter_packs (underlying_type
))
18468 underlying_type
= NULL_TREE
;
18472 /* Look for the `{' but don't consume it yet. */
18473 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18475 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18477 cp_parser_error (parser
, "expected %<{%>");
18478 if (has_underlying_type
)
18484 /* An opaque-enum-specifier must have a ';' here. */
18485 if ((scoped_enum_p
|| underlying_type
)
18486 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18488 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18489 if (has_underlying_type
)
18497 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18500 if (nested_name_specifier
)
18502 if (CLASS_TYPE_P (nested_name_specifier
))
18504 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18505 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18506 push_scope (nested_name_specifier
);
18508 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18510 push_nested_namespace (nested_name_specifier
);
18514 /* Issue an error message if type-definitions are forbidden here. */
18515 if (!cp_parser_check_type_definition (parser
))
18516 type
= error_mark_node
;
18518 /* Create the new type. We do this before consuming the opening
18519 brace so the enum will be recorded as being on the line of its
18520 tag (or the 'enum' keyword, if there is no tag). */
18521 type
= start_enum (identifier
, type
, underlying_type
,
18522 attributes
, scoped_enum_p
, &is_new_type
);
18524 /* If the next token is not '{' it is an opaque-enum-specifier or an
18525 elaborated-type-specifier. */
18526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18528 timevar_push (TV_PARSE_ENUM
);
18529 if (nested_name_specifier
18530 && nested_name_specifier
!= error_mark_node
)
18532 /* The following catches invalid code such as:
18533 enum class S<int>::E { A, B, C }; */
18534 if (!processing_specialization
18535 && CLASS_TYPE_P (nested_name_specifier
)
18536 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18537 error_at (type_start_token
->location
, "cannot add an enumerator "
18538 "list to a template instantiation");
18540 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18542 error_at (type_start_token
->location
,
18543 "%<%T::%E%> has not been declared",
18544 TYPE_CONTEXT (nested_name_specifier
),
18545 nested_name_specifier
);
18546 type
= error_mark_node
;
18548 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18549 && !CLASS_TYPE_P (nested_name_specifier
))
18551 error_at (type_start_token
->location
, "nested name specifier "
18552 "%qT for enum declaration does not name a class "
18553 "or namespace", nested_name_specifier
);
18554 type
= error_mark_node
;
18556 /* If that scope does not contain the scope in which the
18557 class was originally declared, the program is invalid. */
18558 else if (prev_scope
&& !is_ancestor (prev_scope
,
18559 nested_name_specifier
))
18561 if (at_namespace_scope_p ())
18562 error_at (type_start_token
->location
,
18563 "declaration of %qD in namespace %qD which does not "
18565 type
, prev_scope
, nested_name_specifier
);
18567 error_at (type_start_token
->location
,
18568 "declaration of %qD in %qD which does not "
18570 type
, prev_scope
, nested_name_specifier
);
18571 type
= error_mark_node
;
18573 /* If that scope is the scope where the declaration is being placed
18574 the program is invalid. */
18575 else if (CLASS_TYPE_P (nested_name_specifier
)
18576 && CLASS_TYPE_P (prev_scope
)
18577 && same_type_p (nested_name_specifier
, prev_scope
))
18579 permerror (type_start_token
->location
,
18580 "extra qualification not allowed");
18581 nested_name_specifier
= NULL_TREE
;
18586 begin_scope (sk_scoped_enum
, type
);
18588 /* Consume the opening brace. */
18589 matching_braces braces
;
18590 braces
.consume_open (parser
);
18592 if (type
== error_mark_node
)
18593 ; /* Nothing to add */
18594 else if (OPAQUE_ENUM_P (type
)
18595 || (cxx_dialect
> cxx98
&& processing_specialization
))
18597 new_value_list
= true;
18598 SET_OPAQUE_ENUM_P (type
, false);
18599 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18603 error_at (type_start_token
->location
,
18604 "multiple definition of %q#T", type
);
18605 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18606 "previous definition here");
18607 type
= error_mark_node
;
18610 if (type
== error_mark_node
)
18611 cp_parser_skip_to_end_of_block_or_statement (parser
);
18612 /* If the next token is not '}', then there are some enumerators. */
18613 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18615 if (is_unnamed
&& !scoped_enum_p
)
18616 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18617 "ISO C++ forbids empty unnamed enum");
18620 cp_parser_enumerator_list (parser
, type
);
18622 /* Consume the final '}'. */
18623 braces
.require_close (parser
);
18627 timevar_pop (TV_PARSE_ENUM
);
18631 /* If a ';' follows, then it is an opaque-enum-specifier
18632 and additional restrictions apply. */
18633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18636 error_at (type_start_token
->location
,
18637 "opaque-enum-specifier without name");
18638 else if (nested_name_specifier
)
18639 error_at (type_start_token
->location
,
18640 "opaque-enum-specifier must use a simple identifier");
18644 /* Look for trailing attributes to apply to this enumeration, and
18645 apply them if appropriate. */
18646 if (cp_parser_allow_gnu_extensions_p (parser
))
18648 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18649 cplus_decl_attributes (&type
,
18651 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18654 /* Finish up the enumeration. */
18655 if (type
!= error_mark_node
)
18657 if (new_value_list
)
18658 finish_enum_value_list (type
);
18660 finish_enum (type
);
18663 if (nested_name_specifier
)
18665 if (CLASS_TYPE_P (nested_name_specifier
))
18667 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18668 pop_scope (nested_name_specifier
);
18670 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18672 pop_nested_namespace (nested_name_specifier
);
18676 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18680 /* Parse an enumerator-list. The enumerators all have the indicated
18684 enumerator-definition
18685 enumerator-list , enumerator-definition */
18688 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18692 /* Parse an enumerator-definition. */
18693 cp_parser_enumerator_definition (parser
, type
);
18695 /* If the next token is not a ',', we've reached the end of
18697 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18699 /* Otherwise, consume the `,' and keep going. */
18700 cp_lexer_consume_token (parser
->lexer
);
18701 /* If the next token is a `}', there is a trailing comma. */
18702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18704 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18705 pedwarn (input_location
, OPT_Wpedantic
,
18706 "comma at end of enumerator list");
18712 /* Parse an enumerator-definition. The enumerator has the indicated
18715 enumerator-definition:
18717 enumerator = constant-expression
18724 enumerator-definition:
18725 enumerator attributes [opt]
18726 enumerator attributes [opt] = constant-expression */
18729 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18735 /* Save the input location because we are interested in the location
18736 of the identifier and not the location of the explicit value. */
18737 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18739 /* Look for the identifier. */
18740 identifier
= cp_parser_identifier (parser
);
18741 if (identifier
== error_mark_node
)
18744 /* Parse any specified attributes. */
18745 tree attrs
= cp_parser_attributes_opt (parser
);
18747 /* If the next token is an '=', then there is an explicit value. */
18748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18750 /* Consume the `=' token. */
18751 cp_lexer_consume_token (parser
->lexer
);
18752 /* Parse the value. */
18753 value
= cp_parser_constant_expression (parser
);
18758 /* If we are processing a template, make sure the initializer of the
18759 enumerator doesn't contain any bare template parameter pack. */
18760 if (check_for_bare_parameter_packs (value
))
18761 value
= error_mark_node
;
18763 /* Create the enumerator. */
18764 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18767 /* Parse a namespace-name.
18770 original-namespace-name
18773 Returns the NAMESPACE_DECL for the namespace. */
18776 cp_parser_namespace_name (cp_parser
* parser
)
18779 tree namespace_decl
;
18781 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18783 /* Get the name of the namespace. */
18784 identifier
= cp_parser_identifier (parser
);
18785 if (identifier
== error_mark_node
)
18786 return error_mark_node
;
18788 /* Look up the identifier in the currently active scope. Look only
18789 for namespaces, due to:
18791 [basic.lookup.udir]
18793 When looking up a namespace-name in a using-directive or alias
18794 definition, only namespace names are considered.
18798 [basic.lookup.qual]
18800 During the lookup of a name preceding the :: scope resolution
18801 operator, object, function, and enumerator names are ignored.
18803 (Note that cp_parser_qualifying_entity only calls this
18804 function if the token after the name is the scope resolution
18806 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18808 /*is_template=*/false,
18809 /*is_namespace=*/true,
18810 /*check_dependency=*/true,
18811 /*ambiguous_decls=*/NULL
,
18813 /* If it's not a namespace, issue an error. */
18814 if (namespace_decl
== error_mark_node
18815 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18817 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18819 auto_diagnostic_group d
;
18821 if (namespace_decl
== error_mark_node
18822 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18823 hint
= suggest_alternative_in_explicit_scope (token
->location
,
18826 if (const char *suggestion
= hint
.suggestion ())
18828 gcc_rich_location
richloc (token
->location
);
18829 richloc
.add_fixit_replace (suggestion
);
18830 error_at (&richloc
,
18831 "%qD is not a namespace-name; did you mean %qs?",
18832 identifier
, suggestion
);
18835 error_at (token
->location
, "%qD is not a namespace-name",
18839 cp_parser_error (parser
, "expected namespace-name");
18840 namespace_decl
= error_mark_node
;
18843 return namespace_decl
;
18846 /* Parse a namespace-definition.
18848 namespace-definition:
18849 named-namespace-definition
18850 unnamed-namespace-definition
18852 named-namespace-definition:
18853 original-namespace-definition
18854 extension-namespace-definition
18856 original-namespace-definition:
18857 namespace identifier { namespace-body }
18859 extension-namespace-definition:
18860 namespace original-namespace-name { namespace-body }
18862 unnamed-namespace-definition:
18863 namespace { namespace-body } */
18866 cp_parser_namespace_definition (cp_parser
* parser
)
18869 int nested_definition_count
= 0;
18871 cp_ensure_no_omp_declare_simd (parser
);
18872 cp_ensure_no_oacc_routine (parser
);
18874 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18878 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18879 cp_lexer_consume_token (parser
->lexer
);
18882 /* Look for the `namespace' keyword. */
18884 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18886 /* Parse any specified attributes before the identifier. */
18887 tree attribs
= cp_parser_attributes_opt (parser
);
18891 identifier
= NULL_TREE
;
18893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18895 identifier
= cp_parser_identifier (parser
);
18897 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18898 pedwarn (input_location
, OPT_Wpedantic
,
18899 "standard attributes on namespaces must precede "
18900 "the namespace name");
18902 /* Parse any attributes specified after the identifier. */
18903 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18906 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18909 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18910 pedwarn (input_location
, OPT_Wpedantic
,
18911 "nested namespace definitions only available with "
18912 "-std=c++17 or -std=gnu++17");
18914 /* Nested namespace names can create new namespaces (unlike
18915 other qualified-ids). */
18916 if (int count
= identifier
? push_namespace (identifier
) : 0)
18917 nested_definition_count
+= count
;
18919 cp_parser_error (parser
, "nested namespace name required");
18920 cp_lexer_consume_token (parser
->lexer
);
18923 if (nested_definition_count
&& !identifier
)
18924 cp_parser_error (parser
, "namespace name required");
18926 if (nested_definition_count
&& attribs
)
18927 error_at (token
->location
,
18928 "a nested namespace definition cannot have attributes");
18929 if (nested_definition_count
&& is_inline
)
18930 error_at (token
->location
,
18931 "a nested namespace definition cannot be inline");
18933 /* Start the namespace. */
18934 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18936 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18938 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18940 /* Look for the `{' to validate starting the namespace. */
18941 matching_braces braces
;
18942 if (braces
.require_open (parser
))
18944 /* Parse the body of the namespace. */
18945 cp_parser_namespace_body (parser
);
18947 /* Look for the final `}'. */
18948 braces
.require_close (parser
);
18951 if (has_visibility
)
18952 pop_visibility (1);
18954 /* Pop the nested namespace definitions. */
18955 while (nested_definition_count
--)
18959 /* Parse a namespace-body.
18962 declaration-seq [opt] */
18965 cp_parser_namespace_body (cp_parser
* parser
)
18967 cp_parser_declaration_seq_opt (parser
);
18970 /* Parse a namespace-alias-definition.
18972 namespace-alias-definition:
18973 namespace identifier = qualified-namespace-specifier ; */
18976 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18979 tree namespace_specifier
;
18981 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18983 /* Look for the `namespace' keyword. */
18984 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18985 /* Look for the identifier. */
18986 identifier
= cp_parser_identifier (parser
);
18987 if (identifier
== error_mark_node
)
18989 /* Look for the `=' token. */
18990 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18991 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18993 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18994 /* Skip the definition. */
18995 cp_lexer_consume_token (parser
->lexer
);
18996 if (cp_parser_skip_to_closing_brace (parser
))
18997 cp_lexer_consume_token (parser
->lexer
);
19000 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19001 /* Look for the qualified-namespace-specifier. */
19002 namespace_specifier
19003 = cp_parser_qualified_namespace_specifier (parser
);
19004 /* Look for the `;' token. */
19005 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19007 /* Register the alias in the symbol table. */
19008 do_namespace_alias (identifier
, namespace_specifier
);
19011 /* Parse a qualified-namespace-specifier.
19013 qualified-namespace-specifier:
19014 :: [opt] nested-name-specifier [opt] namespace-name
19016 Returns a NAMESPACE_DECL corresponding to the specified
19020 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19022 /* Look for the optional `::'. */
19023 cp_parser_global_scope_opt (parser
,
19024 /*current_scope_valid_p=*/false);
19026 /* Look for the optional nested-name-specifier. */
19027 cp_parser_nested_name_specifier_opt (parser
,
19028 /*typename_keyword_p=*/false,
19029 /*check_dependency_p=*/true,
19031 /*is_declaration=*/true);
19033 return cp_parser_namespace_name (parser
);
19036 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19037 access declaration.
19040 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19041 using :: unqualified-id ;
19043 access-declaration:
19049 cp_parser_using_declaration (cp_parser
* parser
,
19050 bool access_declaration_p
)
19053 bool typename_p
= false;
19054 bool global_scope_p
;
19058 int oldcount
= errorcount
;
19059 cp_token
*diag_token
= NULL
;
19061 if (access_declaration_p
)
19063 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19064 cp_parser_parse_tentatively (parser
);
19068 /* Look for the `using' keyword. */
19069 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19072 /* Peek at the next token. */
19073 token
= cp_lexer_peek_token (parser
->lexer
);
19074 /* See if it's `typename'. */
19075 if (token
->keyword
== RID_TYPENAME
)
19077 /* Remember that we've seen it. */
19079 /* Consume the `typename' token. */
19080 cp_lexer_consume_token (parser
->lexer
);
19084 /* Look for the optional global scope qualification. */
19086 = (cp_parser_global_scope_opt (parser
,
19087 /*current_scope_valid_p=*/false)
19090 /* If we saw `typename', or didn't see `::', then there must be a
19091 nested-name-specifier present. */
19092 if (typename_p
|| !global_scope_p
)
19094 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19095 /*check_dependency_p=*/true,
19097 /*is_declaration=*/true);
19098 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19100 cp_parser_skip_to_end_of_block_or_statement (parser
);
19104 /* Otherwise, we could be in either of the two productions. In that
19105 case, treat the nested-name-specifier as optional. */
19107 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19108 /*typename_keyword_p=*/false,
19109 /*check_dependency_p=*/true,
19111 /*is_declaration=*/true);
19113 qscope
= global_namespace
;
19114 else if (UNSCOPED_ENUM_P (qscope
))
19115 qscope
= CP_TYPE_CONTEXT (qscope
);
19117 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19118 /* Something has already gone wrong; there's no need to parse
19119 further. Since an error has occurred, the return value of
19120 cp_parser_parse_definitely will be false, as required. */
19121 return cp_parser_parse_definitely (parser
);
19123 token
= cp_lexer_peek_token (parser
->lexer
);
19124 /* Parse the unqualified-id. */
19125 identifier
= cp_parser_unqualified_id (parser
,
19126 /*template_keyword_p=*/false,
19127 /*check_dependency_p=*/true,
19128 /*declarator_p=*/true,
19129 /*optional_p=*/false);
19131 if (access_declaration_p
)
19133 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19134 cp_parser_simulate_error (parser
);
19135 if (!cp_parser_parse_definitely (parser
))
19138 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19140 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19141 if (cxx_dialect
< cxx17
19142 && !in_system_header_at (ell
->location
))
19143 pedwarn (ell
->location
, 0,
19144 "pack expansion in using-declaration only available "
19145 "with -std=c++17 or -std=gnu++17");
19146 qscope
= make_pack_expansion (qscope
);
19149 /* The function we call to handle a using-declaration is different
19150 depending on what scope we are in. */
19151 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19153 else if (!identifier_p (identifier
)
19154 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19155 /* [namespace.udecl]
19157 A using declaration shall not name a template-id. */
19158 error_at (token
->location
,
19159 "a template-id may not appear in a using-declaration");
19162 if (at_class_scope_p ())
19164 /* Create the USING_DECL. */
19165 decl
= do_class_using_decl (qscope
, identifier
);
19167 if (decl
&& typename_p
)
19168 USING_DECL_TYPENAME_P (decl
) = 1;
19170 if (check_for_bare_parameter_packs (decl
))
19172 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19176 /* Add it to the list of members in this class. */
19177 finish_member_declaration (decl
);
19181 decl
= cp_parser_lookup_name_simple (parser
,
19184 if (decl
== error_mark_node
)
19185 cp_parser_name_lookup_error (parser
, identifier
,
19188 else if (check_for_bare_parameter_packs (decl
))
19190 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19193 else if (!at_namespace_scope_p ())
19194 finish_local_using_decl (decl
, qscope
, identifier
);
19196 finish_namespace_using_decl (decl
, qscope
, identifier
);
19200 if (!access_declaration_p
19201 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19203 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19204 if (cxx_dialect
< cxx17
)
19205 pedwarn (comma
->location
, 0,
19206 "comma-separated list in using-declaration only available "
19207 "with -std=c++17 or -std=gnu++17");
19211 /* Look for the final `;'. */
19212 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19214 if (access_declaration_p
&& errorcount
== oldcount
)
19215 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19216 "access declarations are deprecated "
19217 "in favour of using-declarations; "
19218 "suggestion: add the %<using%> keyword");
19223 /* Parse an alias-declaration.
19226 using identifier attribute-specifier-seq [opt] = type-id */
19229 cp_parser_alias_declaration (cp_parser
* parser
)
19231 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19232 location_t id_location
, type_location
;
19233 cp_declarator
*declarator
;
19234 cp_decl_specifier_seq decl_specs
;
19236 const char *saved_message
= NULL
;
19238 /* Look for the `using' keyword. */
19239 cp_token
*using_token
19240 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19241 if (using_token
== NULL
)
19242 return error_mark_node
;
19244 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19245 id
= cp_parser_identifier (parser
);
19246 if (id
== error_mark_node
)
19247 return error_mark_node
;
19249 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19250 attributes
= cp_parser_attributes_opt (parser
);
19251 if (attributes
== error_mark_node
)
19252 return error_mark_node
;
19254 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19256 if (cp_parser_error_occurred (parser
))
19257 return error_mark_node
;
19259 cp_parser_commit_to_tentative_parse (parser
);
19261 /* Now we are going to parse the type-id of the declaration. */
19266 "A type-specifier-seq shall not define a class or enumeration
19267 unless it appears in the type-id of an alias-declaration (7.1.3) that
19268 is not the declaration of a template-declaration."
19270 In other words, if we currently are in an alias template, the
19271 type-id should not define a type.
19273 So let's set parser->type_definition_forbidden_message in that
19274 case; cp_parser_check_type_definition (called by
19275 cp_parser_class_specifier) will then emit an error if a type is
19276 defined in the type-id. */
19277 if (parser
->num_template_parameter_lists
)
19279 saved_message
= parser
->type_definition_forbidden_message
;
19280 parser
->type_definition_forbidden_message
=
19281 G_("types may not be defined in alias template declarations");
19284 type
= cp_parser_type_id (parser
, &type_location
);
19286 /* Restore the error message if need be. */
19287 if (parser
->num_template_parameter_lists
)
19288 parser
->type_definition_forbidden_message
= saved_message
;
19290 if (type
== error_mark_node
19291 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19293 cp_parser_skip_to_end_of_block_or_statement (parser
);
19294 return error_mark_node
;
19297 /* A typedef-name can also be introduced by an alias-declaration. The
19298 identifier following the using keyword becomes a typedef-name. It has
19299 the same semantics as if it were introduced by the typedef
19300 specifier. In particular, it does not define a new type and it shall
19301 not appear in the type-id. */
19303 clear_decl_specs (&decl_specs
);
19304 decl_specs
.type
= type
;
19305 if (attributes
!= NULL_TREE
)
19307 decl_specs
.attributes
= attributes
;
19308 set_and_check_decl_spec_loc (&decl_specs
,
19312 set_and_check_decl_spec_loc (&decl_specs
,
19315 set_and_check_decl_spec_loc (&decl_specs
,
19318 decl_specs
.locations
[ds_type_spec
] = type_location
;
19320 if (parser
->num_template_parameter_lists
19321 && !cp_parser_check_template_parameters (parser
,
19322 /*num_templates=*/0,
19323 /*template_id*/false,
19325 /*declarator=*/NULL
))
19326 return error_mark_node
;
19328 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19330 member_p
= at_class_scope_p ();
19332 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19333 NULL_TREE
, attributes
);
19335 decl
= start_decl (declarator
, &decl_specs
, 0,
19336 attributes
, NULL_TREE
, &pushed_scope
);
19337 if (decl
== error_mark_node
)
19340 // Attach constraints to the alias declaration.
19341 if (flag_concepts
&& current_template_parms
)
19343 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19344 tree constr
= build_constraints (reqs
, NULL_TREE
);
19345 set_constraints (decl
, constr
);
19348 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19351 pop_scope (pushed_scope
);
19353 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19354 added into the symbol table; otherwise, return the TYPE_DECL. */
19355 if (DECL_LANG_SPECIFIC (decl
)
19356 && DECL_TEMPLATE_INFO (decl
)
19357 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19359 decl
= DECL_TI_TEMPLATE (decl
);
19361 check_member_template (decl
);
19367 /* Parse a using-directive.
19370 using namespace :: [opt] nested-name-specifier [opt]
19371 namespace-name ; */
19374 cp_parser_using_directive (cp_parser
* parser
)
19376 tree namespace_decl
;
19379 /* Look for the `using' keyword. */
19380 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19381 /* And the `namespace' keyword. */
19382 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19383 /* Look for the optional `::' operator. */
19384 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19385 /* And the optional nested-name-specifier. */
19386 cp_parser_nested_name_specifier_opt (parser
,
19387 /*typename_keyword_p=*/false,
19388 /*check_dependency_p=*/true,
19390 /*is_declaration=*/true);
19391 /* Get the namespace being used. */
19392 namespace_decl
= cp_parser_namespace_name (parser
);
19393 /* And any specified attributes. */
19394 attribs
= cp_parser_attributes_opt (parser
);
19396 /* Update the symbol table. */
19397 if (namespace_bindings_p ())
19398 finish_namespace_using_directive (namespace_decl
, attribs
);
19400 finish_local_using_directive (namespace_decl
, attribs
);
19402 /* Look for the final `;'. */
19403 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19406 /* Parse an asm-definition.
19409 asm ( string-literal ) ;
19414 asm volatile [opt] ( string-literal ) ;
19415 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19416 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19417 : asm-operand-list [opt] ) ;
19418 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19419 : asm-operand-list [opt]
19420 : asm-clobber-list [opt] ) ;
19421 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19422 : asm-clobber-list [opt]
19423 : asm-goto-list ) ; */
19426 cp_parser_asm_definition (cp_parser
* parser
)
19429 tree outputs
= NULL_TREE
;
19430 tree inputs
= NULL_TREE
;
19431 tree clobbers
= NULL_TREE
;
19432 tree labels
= NULL_TREE
;
19434 bool volatile_p
= false;
19435 bool extended_p
= false;
19436 bool invalid_inputs_p
= false;
19437 bool invalid_outputs_p
= false;
19438 bool goto_p
= false;
19439 required_token missing
= RT_NONE
;
19441 /* Look for the `asm' keyword. */
19442 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19444 if (parser
->in_function_body
19445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19447 error ("%<asm%> in %<constexpr%> function");
19448 cp_function_chain
->invalid_constexpr
= true;
19451 /* See if the next token is `volatile'. */
19452 if (cp_parser_allow_gnu_extensions_p (parser
)
19453 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19455 /* Remember that we saw the `volatile' keyword. */
19457 /* Consume the token. */
19458 cp_lexer_consume_token (parser
->lexer
);
19460 if (cp_parser_allow_gnu_extensions_p (parser
)
19461 && parser
->in_function_body
19462 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19464 /* Remember that we saw the `goto' keyword. */
19466 /* Consume the token. */
19467 cp_lexer_consume_token (parser
->lexer
);
19469 /* Look for the opening `('. */
19470 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19472 /* Look for the string. */
19473 string
= cp_parser_string_literal (parser
, false, false);
19474 if (string
== error_mark_node
)
19476 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19477 /*consume_paren=*/true);
19481 /* If we're allowing GNU extensions, check for the extended assembly
19482 syntax. Unfortunately, the `:' tokens need not be separated by
19483 a space in C, and so, for compatibility, we tolerate that here
19484 too. Doing that means that we have to treat the `::' operator as
19486 if (cp_parser_allow_gnu_extensions_p (parser
)
19487 && parser
->in_function_body
19488 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19489 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19491 bool inputs_p
= false;
19492 bool clobbers_p
= false;
19493 bool labels_p
= false;
19495 /* The extended syntax was used. */
19498 /* Look for outputs. */
19499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19501 /* Consume the `:'. */
19502 cp_lexer_consume_token (parser
->lexer
);
19503 /* Parse the output-operands. */
19504 if (cp_lexer_next_token_is_not (parser
->lexer
,
19506 && cp_lexer_next_token_is_not (parser
->lexer
,
19508 && cp_lexer_next_token_is_not (parser
->lexer
,
19512 outputs
= cp_parser_asm_operand_list (parser
);
19513 if (outputs
== error_mark_node
)
19514 invalid_outputs_p
= true;
19517 /* If the next token is `::', there are no outputs, and the
19518 next token is the beginning of the inputs. */
19519 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19520 /* The inputs are coming next. */
19523 /* Look for inputs. */
19525 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19527 /* Consume the `:' or `::'. */
19528 cp_lexer_consume_token (parser
->lexer
);
19529 /* Parse the output-operands. */
19530 if (cp_lexer_next_token_is_not (parser
->lexer
,
19532 && cp_lexer_next_token_is_not (parser
->lexer
,
19534 && cp_lexer_next_token_is_not (parser
->lexer
,
19537 inputs
= cp_parser_asm_operand_list (parser
);
19538 if (inputs
== error_mark_node
)
19539 invalid_inputs_p
= true;
19542 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19543 /* The clobbers are coming next. */
19546 /* Look for clobbers. */
19548 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19551 /* Consume the `:' or `::'. */
19552 cp_lexer_consume_token (parser
->lexer
);
19553 /* Parse the clobbers. */
19554 if (cp_lexer_next_token_is_not (parser
->lexer
,
19556 && cp_lexer_next_token_is_not (parser
->lexer
,
19558 clobbers
= cp_parser_asm_clobber_list (parser
);
19561 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19562 /* The labels are coming next. */
19565 /* Look for labels. */
19567 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19570 /* Consume the `:' or `::'. */
19571 cp_lexer_consume_token (parser
->lexer
);
19572 /* Parse the labels. */
19573 labels
= cp_parser_asm_label_list (parser
);
19576 if (goto_p
&& !labels_p
)
19577 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19580 missing
= RT_COLON_SCOPE
;
19582 /* Look for the closing `)'. */
19583 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19584 missing
? missing
: RT_CLOSE_PAREN
))
19585 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19586 /*consume_paren=*/true);
19587 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19589 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19591 /* Create the ASM_EXPR. */
19592 if (parser
->in_function_body
)
19594 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19595 inputs
, clobbers
, labels
);
19596 /* If the extended syntax was not used, mark the ASM_EXPR. */
19599 tree temp
= asm_stmt
;
19600 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19601 temp
= TREE_OPERAND (temp
, 0);
19603 ASM_INPUT_P (temp
) = 1;
19607 symtab
->finalize_toplevel_asm (string
);
19611 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19612 type that comes from the decl-specifier-seq. */
19615 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19617 for (cp_declarator
*d
= declarator
; d
;)
19627 if (TYPE_PTRMEMFUNC_P (type
))
19628 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19629 type
= TREE_TYPE (type
);
19637 /* Declarators [gram.dcl.decl] */
19639 /* Parse an init-declarator.
19642 declarator initializer [opt]
19647 declarator asm-specification [opt] attributes [opt] initializer [opt]
19649 function-definition:
19650 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19652 decl-specifier-seq [opt] declarator function-try-block
19656 function-definition:
19657 __extension__ function-definition
19661 function-definition:
19662 decl-specifier-seq [opt] declarator function-transaction-block
19664 The DECL_SPECIFIERS apply to this declarator. Returns a
19665 representation of the entity declared. If MEMBER_P is TRUE, then
19666 this declarator appears in a class scope. The new DECL created by
19667 this declarator is returned.
19669 The CHECKS are access checks that should be performed once we know
19670 what entity is being declared (and, therefore, what classes have
19673 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19674 for a function-definition here as well. If the declarator is a
19675 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19676 be TRUE upon return. By that point, the function-definition will
19677 have been completely parsed.
19679 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19682 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19683 parsed declaration if it is an uninitialized single declarator not followed
19684 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19685 if present, will not be consumed. If returned, this declarator will be
19686 created with SD_INITIALIZED but will not call cp_finish_decl.
19688 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19689 and there is an initializer, the pointed location_t is set to the
19690 location of the '=' or `(', or '{' in C++11 token introducing the
19694 cp_parser_init_declarator (cp_parser
* parser
,
19695 cp_decl_specifier_seq
*decl_specifiers
,
19696 vec
<deferred_access_check
, va_gc
> *checks
,
19697 bool function_definition_allowed_p
,
19699 int declares_class_or_enum
,
19700 bool* function_definition_p
,
19701 tree
* maybe_range_for_decl
,
19702 location_t
* init_loc
,
19705 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19706 *attributes_start_token
= NULL
;
19707 cp_declarator
*declarator
;
19708 tree prefix_attributes
;
19709 tree attributes
= NULL
;
19710 tree asm_specification
;
19712 tree decl
= NULL_TREE
;
19714 int is_initialized
;
19715 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19716 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19718 enum cpp_ttype initialization_kind
;
19719 bool is_direct_init
= false;
19720 bool is_non_constant_init
;
19721 int ctor_dtor_or_conv_p
;
19722 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19723 tree pushed_scope
= NULL_TREE
;
19724 bool range_for_decl_p
= false;
19725 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19726 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19728 /* Gather the attributes that were provided with the
19729 decl-specifiers. */
19730 prefix_attributes
= decl_specifiers
->attributes
;
19732 /* Assume that this is not the declarator for a function
19734 if (function_definition_p
)
19735 *function_definition_p
= false;
19737 /* Default arguments are only permitted for function parameters. */
19738 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19739 parser
->default_arg_ok_p
= false;
19741 /* Defer access checks while parsing the declarator; we cannot know
19742 what names are accessible until we know what is being
19744 resume_deferring_access_checks ();
19746 token
= cp_lexer_peek_token (parser
->lexer
);
19748 /* Parse the declarator. */
19750 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19751 &ctor_dtor_or_conv_p
,
19752 /*parenthesized_p=*/NULL
,
19753 member_p
, friend_p
);
19754 /* Gather up the deferred checks. */
19755 stop_deferring_access_checks ();
19757 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19759 /* If the DECLARATOR was erroneous, there's no need to go
19761 if (declarator
== cp_error_declarator
)
19762 return error_mark_node
;
19764 /* Check that the number of template-parameter-lists is OK. */
19765 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19767 return error_mark_node
;
19769 if (declares_class_or_enum
& 2)
19770 cp_parser_check_for_definition_in_return_type (declarator
,
19771 decl_specifiers
->type
,
19772 decl_specifiers
->locations
[ds_type_spec
]);
19774 /* Figure out what scope the entity declared by the DECLARATOR is
19775 located in. `grokdeclarator' sometimes changes the scope, so
19776 we compute it now. */
19777 scope
= get_scope_of_declarator (declarator
);
19779 /* Perform any lookups in the declared type which were thought to be
19780 dependent, but are not in the scope of the declarator. */
19781 decl_specifiers
->type
19782 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19784 /* If we're allowing GNU extensions, look for an
19785 asm-specification. */
19786 if (cp_parser_allow_gnu_extensions_p (parser
))
19788 /* Look for an asm-specification. */
19789 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19790 asm_specification
= cp_parser_asm_specification_opt (parser
);
19793 asm_specification
= NULL_TREE
;
19795 /* Look for attributes. */
19796 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19797 attributes
= cp_parser_attributes_opt (parser
);
19799 /* Peek at the next token. */
19800 token
= cp_lexer_peek_token (parser
->lexer
);
19802 bool bogus_implicit_tmpl
= false;
19804 if (function_declarator_p (declarator
))
19806 /* Handle C++17 deduction guides. */
19807 if (!decl_specifiers
->type
19808 && ctor_dtor_or_conv_p
<= 0
19809 && cxx_dialect
>= cxx17
)
19811 cp_declarator
*id
= get_id_declarator (declarator
);
19812 tree name
= id
->u
.id
.unqualified_name
;
19813 parser
->scope
= id
->u
.id
.qualifying_scope
;
19814 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19816 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19817 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19819 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19820 id
->u
.id
.sfk
= sfk_deduction_guide
;
19821 ctor_dtor_or_conv_p
= 1;
19825 /* Check to see if the token indicates the start of a
19826 function-definition. */
19827 if (cp_parser_token_starts_function_definition_p (token
))
19829 if (!function_definition_allowed_p
)
19831 /* If a function-definition should not appear here, issue an
19833 cp_parser_error (parser
,
19834 "a function-definition is not allowed here");
19835 return error_mark_node
;
19838 location_t func_brace_location
19839 = cp_lexer_peek_token (parser
->lexer
)->location
;
19841 /* Neither attributes nor an asm-specification are allowed
19842 on a function-definition. */
19843 if (asm_specification
)
19844 error_at (asm_spec_start_token
->location
,
19845 "an asm-specification is not allowed "
19846 "on a function-definition");
19848 error_at (attributes_start_token
->location
,
19849 "attributes are not allowed "
19850 "on a function-definition");
19851 /* This is a function-definition. */
19852 *function_definition_p
= true;
19854 /* Parse the function definition. */
19856 decl
= cp_parser_save_member_function_body (parser
,
19859 prefix_attributes
);
19862 (cp_parser_function_definition_from_specifiers_and_declarator
19863 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19865 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19867 /* This is where the prologue starts... */
19868 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19869 = func_brace_location
;
19875 else if (parser
->fully_implicit_function_template_p
)
19877 /* A non-template declaration involving a function parameter list
19878 containing an implicit template parameter will be made into a
19879 template. If the resulting declaration is not going to be an
19880 actual function then finish the template scope here to prevent it.
19881 An error message will be issued once we have a decl to talk about.
19883 FIXME probably we should do type deduction rather than create an
19884 implicit template, but the standard currently doesn't allow it. */
19885 bogus_implicit_tmpl
= true;
19886 finish_fully_implicit_template (parser
, NULL_TREE
);
19891 Only in function declarations for constructors, destructors, type
19892 conversions, and deduction guides can the decl-specifier-seq be omitted.
19894 We explicitly postpone this check past the point where we handle
19895 function-definitions because we tolerate function-definitions
19896 that are missing their return types in some modes. */
19897 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19899 cp_parser_error (parser
,
19900 "expected constructor, destructor, or type conversion");
19901 return error_mark_node
;
19904 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19905 if (token
->type
== CPP_EQ
19906 || token
->type
== CPP_OPEN_PAREN
19907 || token
->type
== CPP_OPEN_BRACE
)
19909 is_initialized
= SD_INITIALIZED
;
19910 initialization_kind
= token
->type
;
19911 if (maybe_range_for_decl
)
19912 *maybe_range_for_decl
= error_mark_node
;
19913 tmp_init_loc
= token
->location
;
19914 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19915 *init_loc
= tmp_init_loc
;
19917 if (token
->type
== CPP_EQ
19918 && function_declarator_p (declarator
))
19920 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19921 if (t2
->keyword
== RID_DEFAULT
)
19922 is_initialized
= SD_DEFAULTED
;
19923 else if (t2
->keyword
== RID_DELETE
)
19924 is_initialized
= SD_DELETED
;
19929 /* If the init-declarator isn't initialized and isn't followed by a
19930 `,' or `;', it's not a valid init-declarator. */
19931 if (token
->type
!= CPP_COMMA
19932 && token
->type
!= CPP_SEMICOLON
)
19934 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19935 range_for_decl_p
= true;
19938 if (!maybe_range_for_decl
)
19939 cp_parser_error (parser
, "expected initializer");
19940 return error_mark_node
;
19943 is_initialized
= SD_UNINITIALIZED
;
19944 initialization_kind
= CPP_EOF
;
19947 /* Because start_decl has side-effects, we should only call it if we
19948 know we're going ahead. By this point, we know that we cannot
19949 possibly be looking at any other construct. */
19950 cp_parser_commit_to_tentative_parse (parser
);
19952 /* Enter the newly declared entry in the symbol table. If we're
19953 processing a declaration in a class-specifier, we wait until
19954 after processing the initializer. */
19957 if (parser
->in_unbraced_linkage_specification_p
)
19958 decl_specifiers
->storage_class
= sc_extern
;
19959 decl
= start_decl (declarator
, decl_specifiers
,
19960 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19961 attributes
, prefix_attributes
, &pushed_scope
);
19962 cp_finalize_omp_declare_simd (parser
, decl
);
19963 cp_finalize_oacc_routine (parser
, decl
, false);
19964 /* Adjust location of decl if declarator->id_loc is more appropriate:
19965 set, and decl wasn't merged with another decl, in which case its
19966 location would be different from input_location, and more accurate. */
19968 && declarator
->id_loc
!= UNKNOWN_LOCATION
19969 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19970 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19973 /* Enter the SCOPE. That way unqualified names appearing in the
19974 initializer will be looked up in SCOPE. */
19975 pushed_scope
= push_scope (scope
);
19977 /* Perform deferred access control checks, now that we know in which
19978 SCOPE the declared entity resides. */
19979 if (!member_p
&& decl
)
19981 tree saved_current_function_decl
= NULL_TREE
;
19983 /* If the entity being declared is a function, pretend that we
19984 are in its scope. If it is a `friend', it may have access to
19985 things that would not otherwise be accessible. */
19986 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19988 saved_current_function_decl
= current_function_decl
;
19989 current_function_decl
= decl
;
19992 /* Perform access checks for template parameters. */
19993 cp_parser_perform_template_parameter_access_checks (checks
);
19995 /* Perform the access control checks for the declarator and the
19996 decl-specifiers. */
19997 perform_deferred_access_checks (tf_warning_or_error
);
19999 /* Restore the saved value. */
20000 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20001 current_function_decl
= saved_current_function_decl
;
20004 /* Parse the initializer. */
20005 initializer
= NULL_TREE
;
20006 is_direct_init
= false;
20007 is_non_constant_init
= true;
20008 if (is_initialized
)
20010 if (function_declarator_p (declarator
))
20012 if (initialization_kind
== CPP_EQ
)
20013 initializer
= cp_parser_pure_specifier (parser
);
20016 /* If the declaration was erroneous, we don't really
20017 know what the user intended, so just silently
20018 consume the initializer. */
20019 if (decl
!= error_mark_node
)
20020 error_at (tmp_init_loc
, "initializer provided for function");
20021 cp_parser_skip_to_closing_parenthesis (parser
,
20022 /*recovering=*/true,
20023 /*or_comma=*/false,
20024 /*consume_paren=*/true);
20029 /* We want to record the extra mangling scope for in-class
20030 initializers of class members and initializers of static data
20031 member templates. The former involves deferring
20032 parsing of the initializer until end of class as with default
20033 arguments. So right here we only handle the latter. */
20034 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20035 start_lambda_scope (decl
);
20036 initializer
= cp_parser_initializer (parser
,
20038 &is_non_constant_init
);
20039 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20040 finish_lambda_scope ();
20041 if (initializer
== error_mark_node
)
20042 cp_parser_skip_to_end_of_statement (parser
);
20046 /* The old parser allows attributes to appear after a parenthesized
20047 initializer. Mark Mitchell proposed removing this functionality
20048 on the GCC mailing lists on 2002-08-13. This parser accepts the
20049 attributes -- but ignores them. Made a permerror in GCC 8. */
20050 if (cp_parser_allow_gnu_extensions_p (parser
)
20051 && initialization_kind
== CPP_OPEN_PAREN
20052 && cp_parser_attributes_opt (parser
)
20053 && permerror (input_location
,
20054 "attributes after parenthesized initializer ignored"))
20057 if (flag_permissive
&& !hint
)
20060 inform (input_location
,
20061 "this flexibility is deprecated and will be removed");
20065 /* And now complain about a non-function implicit template. */
20066 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20067 error_at (DECL_SOURCE_LOCATION (decl
),
20068 "non-function %qD declared as implicit template", decl
);
20070 /* For an in-class declaration, use `grokfield' to create the
20076 pop_scope (pushed_scope
);
20077 pushed_scope
= NULL_TREE
;
20079 decl
= grokfield (declarator
, decl_specifiers
,
20080 initializer
, !is_non_constant_init
,
20081 /*asmspec=*/NULL_TREE
,
20082 attr_chainon (attributes
, prefix_attributes
));
20083 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20084 cp_parser_save_default_args (parser
, decl
);
20085 cp_finalize_omp_declare_simd (parser
, decl
);
20086 cp_finalize_oacc_routine (parser
, decl
, false);
20089 /* Finish processing the declaration. But, skip member
20091 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20093 cp_finish_decl (decl
,
20094 initializer
, !is_non_constant_init
,
20096 /* If the initializer is in parentheses, then this is
20097 a direct-initialization, which means that an
20098 `explicit' constructor is OK. Otherwise, an
20099 `explicit' constructor cannot be used. */
20100 ((is_direct_init
|| !is_initialized
)
20101 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20103 else if ((cxx_dialect
!= cxx98
) && friend_p
20104 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20105 /* Core issue #226 (C++0x only): A default template-argument
20106 shall not be specified in a friend class template
20108 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20109 /*is_partial=*/false, /*is_friend_decl=*/1);
20111 if (!friend_p
&& pushed_scope
)
20112 pop_scope (pushed_scope
);
20114 if (function_declarator_p (declarator
)
20115 && parser
->fully_implicit_function_template_p
)
20118 decl
= finish_fully_implicit_template (parser
, decl
);
20120 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20123 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20124 && type_uses_auto (decl_specifiers
->type
))
20125 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20130 /* Parse a declarator.
20134 ptr-operator declarator
20136 abstract-declarator:
20137 ptr-operator abstract-declarator [opt]
20138 direct-abstract-declarator
20143 attributes [opt] direct-declarator
20144 attributes [opt] ptr-operator declarator
20146 abstract-declarator:
20147 attributes [opt] ptr-operator abstract-declarator [opt]
20148 attributes [opt] direct-abstract-declarator
20150 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20151 detect constructors, destructors, deduction guides, or conversion operators.
20152 It is set to -1 if the declarator is a name, and +1 if it is a
20153 function. Otherwise it is set to zero. Usually you just want to
20154 test for >0, but internally the negative value is used.
20156 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20157 a decl-specifier-seq unless it declares a constructor, destructor,
20158 or conversion. It might seem that we could check this condition in
20159 semantic analysis, rather than parsing, but that makes it difficult
20160 to handle something like `f()'. We want to notice that there are
20161 no decl-specifiers, and therefore realize that this is an
20162 expression, not a declaration.)
20164 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20165 the declarator is a direct-declarator of the form "(...)".
20167 MEMBER_P is true iff this declarator is a member-declarator.
20169 FRIEND_P is true iff this declarator is a friend. */
20171 static cp_declarator
*
20172 cp_parser_declarator (cp_parser
* parser
,
20173 cp_parser_declarator_kind dcl_kind
,
20174 int* ctor_dtor_or_conv_p
,
20175 bool* parenthesized_p
,
20176 bool member_p
, bool friend_p
)
20178 cp_declarator
*declarator
;
20179 enum tree_code code
;
20180 cp_cv_quals cv_quals
;
20182 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20184 /* Assume this is not a constructor, destructor, or type-conversion
20186 if (ctor_dtor_or_conv_p
)
20187 *ctor_dtor_or_conv_p
= 0;
20189 if (cp_parser_allow_gnu_extensions_p (parser
))
20190 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20192 /* Check for the ptr-operator production. */
20193 cp_parser_parse_tentatively (parser
);
20194 /* Parse the ptr-operator. */
20195 code
= cp_parser_ptr_operator (parser
,
20200 /* If that worked, then we have a ptr-operator. */
20201 if (cp_parser_parse_definitely (parser
))
20203 /* If a ptr-operator was found, then this declarator was not
20205 if (parenthesized_p
)
20206 *parenthesized_p
= true;
20207 /* The dependent declarator is optional if we are parsing an
20208 abstract-declarator. */
20209 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20210 cp_parser_parse_tentatively (parser
);
20212 /* Parse the dependent declarator. */
20213 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20214 /*ctor_dtor_or_conv_p=*/NULL
,
20215 /*parenthesized_p=*/NULL
,
20216 /*member_p=*/false,
20219 /* If we are parsing an abstract-declarator, we must handle the
20220 case where the dependent declarator is absent. */
20221 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20222 && !cp_parser_parse_definitely (parser
))
20225 declarator
= cp_parser_make_indirect_declarator
20226 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20228 /* Everything else is a direct-declarator. */
20231 if (parenthesized_p
)
20232 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20234 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20235 ctor_dtor_or_conv_p
,
20236 member_p
, friend_p
);
20239 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20240 declarator
->attributes
= gnu_attributes
;
20244 /* Parse a direct-declarator or direct-abstract-declarator.
20248 direct-declarator ( parameter-declaration-clause )
20249 cv-qualifier-seq [opt]
20250 ref-qualifier [opt]
20251 exception-specification [opt]
20252 direct-declarator [ constant-expression [opt] ]
20255 direct-abstract-declarator:
20256 direct-abstract-declarator [opt]
20257 ( parameter-declaration-clause )
20258 cv-qualifier-seq [opt]
20259 ref-qualifier [opt]
20260 exception-specification [opt]
20261 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20262 ( abstract-declarator )
20264 Returns a representation of the declarator. DCL_KIND is
20265 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20266 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20267 we are parsing a direct-declarator. It is
20268 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20269 of ambiguity we prefer an abstract declarator, as per
20270 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20271 as for cp_parser_declarator. */
20273 static cp_declarator
*
20274 cp_parser_direct_declarator (cp_parser
* parser
,
20275 cp_parser_declarator_kind dcl_kind
,
20276 int* ctor_dtor_or_conv_p
,
20277 bool member_p
, bool friend_p
)
20280 cp_declarator
*declarator
= NULL
;
20281 tree scope
= NULL_TREE
;
20282 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20283 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20285 tree pushed_scope
= NULL_TREE
;
20286 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20290 /* Peek at the next token. */
20291 token
= cp_lexer_peek_token (parser
->lexer
);
20292 if (token
->type
== CPP_OPEN_PAREN
)
20294 /* This is either a parameter-declaration-clause, or a
20295 parenthesized declarator. When we know we are parsing a
20296 named declarator, it must be a parenthesized declarator
20297 if FIRST is true. For instance, `(int)' is a
20298 parameter-declaration-clause, with an omitted
20299 direct-abstract-declarator. But `((*))', is a
20300 parenthesized abstract declarator. Finally, when T is a
20301 template parameter `(T)' is a
20302 parameter-declaration-clause, and not a parenthesized
20305 We first try and parse a parameter-declaration-clause,
20306 and then try a nested declarator (if FIRST is true).
20308 It is not an error for it not to be a
20309 parameter-declaration-clause, even when FIRST is
20315 The first is the declaration of a function while the
20316 second is the definition of a variable, including its
20319 Having seen only the parenthesis, we cannot know which of
20320 these two alternatives should be selected. Even more
20321 complex are examples like:
20326 The former is a function-declaration; the latter is a
20327 variable initialization.
20329 Thus again, we try a parameter-declaration-clause, and if
20330 that fails, we back out and return. */
20332 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20335 bool is_declarator
= false;
20339 /* In a member-declarator, the only valid interpretation
20340 of a parenthesis is the start of a
20341 parameter-declaration-clause. (It is invalid to
20342 initialize a static data member with a parenthesized
20343 initializer; only the "=" form of initialization is
20346 cp_parser_parse_tentatively (parser
);
20348 /* Consume the `('. */
20349 matching_parens parens
;
20350 parens
.consume_open (parser
);
20353 /* If this is going to be an abstract declarator, we're
20354 in a declarator and we can't have default args. */
20355 parser
->default_arg_ok_p
= false;
20356 parser
->in_declarator_p
= true;
20359 begin_scope (sk_function_parms
, NULL_TREE
);
20361 /* Parse the parameter-declaration-clause. */
20362 params
= cp_parser_parameter_declaration_clause (parser
);
20364 /* Consume the `)'. */
20365 parens
.require_close (parser
);
20367 /* If all went well, parse the cv-qualifier-seq,
20368 ref-qualifier and the exception-specification. */
20369 if (member_p
|| cp_parser_parse_definitely (parser
))
20371 cp_cv_quals cv_quals
;
20372 cp_virt_specifiers virt_specifiers
;
20373 cp_ref_qualifier ref_qual
;
20374 tree exception_specification
;
20377 bool memfn
= (member_p
|| (pushed_scope
20378 && CLASS_TYPE_P (pushed_scope
)));
20380 is_declarator
= true;
20382 if (ctor_dtor_or_conv_p
)
20383 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20386 /* Parse the cv-qualifier-seq. */
20387 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20388 /* Parse the ref-qualifier. */
20389 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20390 /* Parse the tx-qualifier. */
20391 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20392 /* And the exception-specification. */
20393 exception_specification
20394 = cp_parser_exception_specification_opt (parser
);
20396 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20398 /* In here, we handle cases where attribute is used after
20399 the function declaration. For example:
20400 void func (int x) __attribute__((vector(..))); */
20401 tree gnu_attrs
= NULL_TREE
;
20402 tree requires_clause
= NULL_TREE
;
20403 late_return
= (cp_parser_late_return_type_opt
20404 (parser
, declarator
, requires_clause
,
20405 memfn
? cv_quals
: -1));
20407 /* Parse the virt-specifier-seq. */
20408 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20410 /* Create the function-declarator. */
20411 declarator
= make_call_declarator (declarator
,
20417 exception_specification
,
20420 declarator
->std_attributes
= attrs
;
20421 declarator
->attributes
= gnu_attrs
;
20422 /* Any subsequent parameter lists are to do with
20423 return type, so are not those of the declared
20425 parser
->default_arg_ok_p
= false;
20428 /* Remove the function parms from scope. */
20429 pop_bindings_and_leave_scope ();
20432 /* Repeat the main loop. */
20436 /* If this is the first, we can try a parenthesized
20440 bool saved_in_type_id_in_expr_p
;
20442 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20443 parser
->in_declarator_p
= saved_in_declarator_p
;
20445 open_paren
= token
;
20446 /* Consume the `('. */
20447 matching_parens parens
;
20448 parens
.consume_open (parser
);
20449 /* Parse the nested declarator. */
20450 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20451 parser
->in_type_id_in_expr_p
= true;
20453 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20454 /*parenthesized_p=*/NULL
,
20455 member_p
, friend_p
);
20456 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20458 /* Expect a `)'. */
20459 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20460 if (!parens
.require_close (parser
))
20461 declarator
= cp_error_declarator
;
20462 if (declarator
== cp_error_declarator
)
20465 goto handle_declarator
;
20467 /* Otherwise, we must be done. */
20471 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20472 && token
->type
== CPP_OPEN_SQUARE
20473 && !cp_next_tokens_can_be_attribute_p (parser
))
20475 /* Parse an array-declarator. */
20476 tree bounds
, attrs
;
20478 if (ctor_dtor_or_conv_p
)
20479 *ctor_dtor_or_conv_p
= 0;
20483 parser
->default_arg_ok_p
= false;
20484 parser
->in_declarator_p
= true;
20485 /* Consume the `['. */
20486 cp_lexer_consume_token (parser
->lexer
);
20487 /* Peek at the next token. */
20488 token
= cp_lexer_peek_token (parser
->lexer
);
20489 /* If the next token is `]', then there is no
20490 constant-expression. */
20491 if (token
->type
!= CPP_CLOSE_SQUARE
)
20493 bool non_constant_p
;
20495 = cp_parser_constant_expression (parser
,
20496 /*allow_non_constant=*/true,
20498 if (!non_constant_p
)
20500 else if (error_operand_p (bounds
))
20501 /* Already gave an error. */;
20502 else if (!parser
->in_function_body
20503 || current_binding_level
->kind
== sk_function_parms
)
20505 /* Normally, the array bound must be an integral constant
20506 expression. However, as an extension, we allow VLAs
20507 in function scopes as long as they aren't part of a
20508 parameter declaration. */
20509 cp_parser_error (parser
,
20510 "array bound is not an integer constant");
20511 bounds
= error_mark_node
;
20513 else if (processing_template_decl
20514 && !type_dependent_expression_p (bounds
))
20516 /* Remember this wasn't a constant-expression. */
20517 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20518 TREE_SIDE_EFFECTS (bounds
) = 1;
20522 bounds
= NULL_TREE
;
20523 /* Look for the closing `]'. */
20524 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20526 declarator
= cp_error_declarator
;
20530 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20531 declarator
= make_array_declarator (declarator
, bounds
);
20532 declarator
->std_attributes
= attrs
;
20534 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20537 tree qualifying_scope
;
20538 tree unqualified_name
;
20540 special_function_kind sfk
;
20542 bool pack_expansion_p
= false;
20543 cp_token
*declarator_id_start_token
;
20545 /* Parse a declarator-id */
20546 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20549 cp_parser_parse_tentatively (parser
);
20551 /* If we see an ellipsis, we should be looking at a
20553 if (token
->type
== CPP_ELLIPSIS
)
20555 /* Consume the `...' */
20556 cp_lexer_consume_token (parser
->lexer
);
20558 pack_expansion_p
= true;
20562 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20564 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20565 qualifying_scope
= parser
->scope
;
20570 if (!unqualified_name
&& pack_expansion_p
)
20572 /* Check whether an error occurred. */
20573 okay
= !cp_parser_error_occurred (parser
);
20575 /* We already consumed the ellipsis to mark a
20576 parameter pack, but we have no way to report it,
20577 so abort the tentative parse. We will be exiting
20578 immediately anyway. */
20579 cp_parser_abort_tentative_parse (parser
);
20582 okay
= cp_parser_parse_definitely (parser
);
20585 unqualified_name
= error_mark_node
;
20586 else if (unqualified_name
20587 && (qualifying_scope
20588 || (!identifier_p (unqualified_name
))))
20590 cp_parser_error (parser
, "expected unqualified-id");
20591 unqualified_name
= error_mark_node
;
20595 if (!unqualified_name
)
20597 if (unqualified_name
== error_mark_node
)
20599 declarator
= cp_error_declarator
;
20600 pack_expansion_p
= false;
20601 declarator
->parameter_pack_p
= false;
20605 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20607 if (qualifying_scope
&& at_namespace_scope_p ()
20608 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20610 /* In the declaration of a member of a template class
20611 outside of the class itself, the SCOPE will sometimes
20612 be a TYPENAME_TYPE. For example, given:
20614 template <typename T>
20615 int S<T>::R::i = 3;
20617 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20618 this context, we must resolve S<T>::R to an ordinary
20619 type, rather than a typename type.
20621 The reason we normally avoid resolving TYPENAME_TYPEs
20622 is that a specialization of `S' might render
20623 `S<T>::R' not a type. However, if `S' is
20624 specialized, then this `i' will not be used, so there
20625 is no harm in resolving the types here. */
20628 /* Resolve the TYPENAME_TYPE. */
20629 type
= resolve_typename_type (qualifying_scope
,
20630 /*only_current_p=*/false);
20631 /* If that failed, the declarator is invalid. */
20632 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20634 if (typedef_variant_p (type
))
20635 error_at (declarator_id_start_token
->location
,
20636 "cannot define member of dependent typedef "
20639 error_at (declarator_id_start_token
->location
,
20640 "%<%T::%E%> is not a type",
20641 TYPE_CONTEXT (qualifying_scope
),
20642 TYPE_IDENTIFIER (qualifying_scope
));
20644 qualifying_scope
= type
;
20649 if (unqualified_name
)
20653 if (qualifying_scope
20654 && CLASS_TYPE_P (qualifying_scope
))
20655 class_type
= qualifying_scope
;
20657 class_type
= current_class_type
;
20659 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20661 tree name_type
= TREE_TYPE (unqualified_name
);
20663 if (!class_type
|| !same_type_p (name_type
, class_type
))
20665 /* We do not attempt to print the declarator
20666 here because we do not have enough
20667 information about its original syntactic
20669 cp_parser_error (parser
, "invalid declarator");
20670 declarator
= cp_error_declarator
;
20673 else if (qualifying_scope
20674 && CLASSTYPE_USE_TEMPLATE (name_type
))
20676 error_at (declarator_id_start_token
->location
,
20677 "invalid use of constructor as a template");
20678 inform (declarator_id_start_token
->location
,
20679 "use %<%T::%D%> instead of %<%T::%D%> to "
20680 "name the constructor in a qualified name",
20682 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20683 class_type
, name_type
);
20684 declarator
= cp_error_declarator
;
20687 unqualified_name
= constructor_name (class_type
);
20692 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20693 sfk
= sfk_destructor
;
20694 else if (identifier_p (unqualified_name
)
20695 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20696 sfk
= sfk_conversion
;
20697 else if (/* There's no way to declare a constructor
20698 for an unnamed type, even if the type
20699 got a name for linkage purposes. */
20700 !TYPE_WAS_UNNAMED (class_type
)
20701 /* Handle correctly (c++/19200):
20715 friend void N::S();
20717 && (!friend_p
|| class_type
== qualifying_scope
)
20718 && constructor_name_p (unqualified_name
,
20720 sfk
= sfk_constructor
;
20721 else if (is_overloaded_fn (unqualified_name
)
20722 && DECL_CONSTRUCTOR_P (get_first_fn
20723 (unqualified_name
)))
20724 sfk
= sfk_constructor
;
20726 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20727 *ctor_dtor_or_conv_p
= -1;
20730 declarator
= make_id_declarator (qualifying_scope
,
20732 sfk
, token
->location
);
20733 declarator
->std_attributes
= attrs
;
20734 declarator
->parameter_pack_p
= pack_expansion_p
;
20736 if (pack_expansion_p
)
20737 maybe_warn_variadic_templates ();
20740 handle_declarator
:;
20741 scope
= get_scope_of_declarator (declarator
);
20744 /* Any names that appear after the declarator-id for a
20745 member are looked up in the containing scope. */
20746 if (at_function_scope_p ())
20748 /* But declarations with qualified-ids can't appear in a
20750 cp_parser_error (parser
, "qualified-id in declaration");
20751 declarator
= cp_error_declarator
;
20754 pushed_scope
= push_scope (scope
);
20756 parser
->in_declarator_p
= true;
20757 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20758 || (declarator
&& declarator
->kind
== cdk_id
))
20759 /* Default args are only allowed on function
20761 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20763 parser
->default_arg_ok_p
= false;
20772 /* For an abstract declarator, we might wind up with nothing at this
20773 point. That's an error; the declarator is not optional. */
20775 cp_parser_error (parser
, "expected declarator");
20776 else if (open_paren
)
20778 /* Record overly parenthesized declarator so we can give a
20779 diagnostic about confusing decl/expr disambiguation. */
20780 if (declarator
->kind
== cdk_array
)
20782 /* If the open and close parens are on different lines, this
20783 is probably a formatting thing, so ignore. */
20784 expanded_location open
= expand_location (open_paren
->location
);
20785 expanded_location close
= expand_location (close_paren
->location
);
20786 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20790 declarator
->parenthesized
= open_paren
->location
;
20793 /* If we entered a scope, we must exit it now. */
20795 pop_scope (pushed_scope
);
20797 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20798 parser
->in_declarator_p
= saved_in_declarator_p
;
20803 /* Parse a ptr-operator.
20806 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20807 * cv-qualifier-seq [opt]
20809 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20810 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20815 & cv-qualifier-seq [opt]
20817 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20818 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20819 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20820 filled in with the TYPE containing the member. *CV_QUALS is
20821 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20822 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20823 Note that the tree codes returned by this function have nothing
20824 to do with the types of trees that will be eventually be created
20825 to represent the pointer or reference type being parsed. They are
20826 just constants with suggestive names. */
20827 static enum tree_code
20828 cp_parser_ptr_operator (cp_parser
* parser
,
20830 cp_cv_quals
*cv_quals
,
20833 enum tree_code code
= ERROR_MARK
;
20835 tree attrs
= NULL_TREE
;
20837 /* Assume that it's not a pointer-to-member. */
20839 /* And that there are no cv-qualifiers. */
20840 *cv_quals
= TYPE_UNQUALIFIED
;
20842 /* Peek at the next token. */
20843 token
= cp_lexer_peek_token (parser
->lexer
);
20845 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20846 if (token
->type
== CPP_MULT
)
20847 code
= INDIRECT_REF
;
20848 else if (token
->type
== CPP_AND
)
20850 else if ((cxx_dialect
!= cxx98
) &&
20851 token
->type
== CPP_AND_AND
) /* C++0x only */
20852 code
= NON_LVALUE_EXPR
;
20854 if (code
!= ERROR_MARK
)
20856 /* Consume the `*', `&' or `&&'. */
20857 cp_lexer_consume_token (parser
->lexer
);
20859 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20860 `&', if we are allowing GNU extensions. (The only qualifier
20861 that can legally appear after `&' is `restrict', but that is
20862 enforced during semantic analysis. */
20863 if (code
== INDIRECT_REF
20864 || cp_parser_allow_gnu_extensions_p (parser
))
20865 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20867 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20868 if (attributes
!= NULL
)
20869 *attributes
= attrs
;
20873 /* Try the pointer-to-member case. */
20874 cp_parser_parse_tentatively (parser
);
20875 /* Look for the optional `::' operator. */
20876 cp_parser_global_scope_opt (parser
,
20877 /*current_scope_valid_p=*/false);
20878 /* Look for the nested-name specifier. */
20879 token
= cp_lexer_peek_token (parser
->lexer
);
20880 cp_parser_nested_name_specifier (parser
,
20881 /*typename_keyword_p=*/false,
20882 /*check_dependency_p=*/true,
20884 /*is_declaration=*/false);
20885 /* If we found it, and the next token is a `*', then we are
20886 indeed looking at a pointer-to-member operator. */
20887 if (!cp_parser_error_occurred (parser
)
20888 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20890 /* Indicate that the `*' operator was used. */
20891 code
= INDIRECT_REF
;
20893 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20894 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20895 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20896 error_at (token
->location
, "cannot form pointer to member of "
20897 "non-class %q#T", parser
->scope
);
20900 /* The type of which the member is a member is given by the
20902 *type
= parser
->scope
;
20903 /* The next name will not be qualified. */
20904 parser
->scope
= NULL_TREE
;
20905 parser
->qualifying_scope
= NULL_TREE
;
20906 parser
->object_scope
= NULL_TREE
;
20907 /* Look for optional c++11 attributes. */
20908 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20909 if (attributes
!= NULL
)
20910 *attributes
= attrs
;
20911 /* Look for the optional cv-qualifier-seq. */
20912 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20915 /* If that didn't work we don't have a ptr-operator. */
20916 if (!cp_parser_parse_definitely (parser
))
20917 cp_parser_error (parser
, "expected ptr-operator");
20923 /* Parse an (optional) cv-qualifier-seq.
20926 cv-qualifier cv-qualifier-seq [opt]
20937 Returns a bitmask representing the cv-qualifiers. */
20940 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20942 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20947 cp_cv_quals cv_qualifier
;
20949 /* Peek at the next token. */
20950 token
= cp_lexer_peek_token (parser
->lexer
);
20951 /* See if it's a cv-qualifier. */
20952 switch (token
->keyword
)
20955 cv_qualifier
= TYPE_QUAL_CONST
;
20959 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20963 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20967 cv_qualifier
= TYPE_UNQUALIFIED
;
20974 if (cv_quals
& cv_qualifier
)
20976 gcc_rich_location
richloc (token
->location
);
20977 richloc
.add_fixit_remove ();
20978 error_at (&richloc
, "duplicate cv-qualifier");
20979 cp_lexer_purge_token (parser
->lexer
);
20983 cp_lexer_consume_token (parser
->lexer
);
20984 cv_quals
|= cv_qualifier
;
20991 /* Parse an (optional) ref-qualifier
20997 Returns cp_ref_qualifier representing ref-qualifier. */
20999 static cp_ref_qualifier
21000 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21002 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21004 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21005 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21010 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21011 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21013 switch (token
->type
)
21016 curr_ref_qual
= REF_QUAL_LVALUE
;
21020 curr_ref_qual
= REF_QUAL_RVALUE
;
21024 curr_ref_qual
= REF_QUAL_NONE
;
21028 if (!curr_ref_qual
)
21032 error_at (token
->location
, "multiple ref-qualifiers");
21033 cp_lexer_purge_token (parser
->lexer
);
21037 ref_qual
= curr_ref_qual
;
21038 cp_lexer_consume_token (parser
->lexer
);
21045 /* Parse an optional tx-qualifier.
21049 transaction_safe_dynamic */
21052 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21054 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21055 if (token
->type
== CPP_NAME
)
21057 tree name
= token
->u
.value
;
21058 const char *p
= IDENTIFIER_POINTER (name
);
21059 const int len
= strlen ("transaction_safe");
21060 if (!strncmp (p
, "transaction_safe", len
))
21064 || !strcmp (p
, "_dynamic"))
21066 cp_lexer_consume_token (parser
->lexer
);
21069 error ("%qE requires %<-fgnu-tm%>", name
);
21080 /* Parse an (optional) virt-specifier-seq.
21082 virt-specifier-seq:
21083 virt-specifier virt-specifier-seq [opt]
21089 Returns a bitmask representing the virt-specifiers. */
21091 static cp_virt_specifiers
21092 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21094 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21099 cp_virt_specifiers virt_specifier
;
21101 /* Peek at the next token. */
21102 token
= cp_lexer_peek_token (parser
->lexer
);
21103 /* See if it's a virt-specifier-qualifier. */
21104 if (token
->type
!= CPP_NAME
)
21106 if (id_equal (token
->u
.value
, "override"))
21108 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21109 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21111 else if (id_equal (token
->u
.value
, "final"))
21113 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21114 virt_specifier
= VIRT_SPEC_FINAL
;
21116 else if (id_equal (token
->u
.value
, "__final"))
21118 virt_specifier
= VIRT_SPEC_FINAL
;
21123 if (virt_specifiers
& virt_specifier
)
21125 gcc_rich_location
richloc (token
->location
);
21126 richloc
.add_fixit_remove ();
21127 error_at (&richloc
, "duplicate virt-specifier");
21128 cp_lexer_purge_token (parser
->lexer
);
21132 cp_lexer_consume_token (parser
->lexer
);
21133 virt_specifiers
|= virt_specifier
;
21136 return virt_specifiers
;
21139 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21140 is in scope even though it isn't real. */
21143 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21147 if (current_class_ptr
)
21149 /* We don't clear this between NSDMIs. Is it already what we want? */
21150 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21151 if (DECL_P (current_class_ptr
)
21152 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21153 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21154 && cp_type_quals (type
) == quals
)
21158 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21159 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21160 current_class_ptr
= NULL_TREE
;
21162 = cp_build_fold_indirect_ref (this_parm
);
21163 current_class_ptr
= this_parm
;
21166 /* Return true iff our current scope is a non-static data member
21170 parsing_nsdmi (void)
21172 /* We recognize NSDMI context by the context-less 'this' pointer set up
21173 by the function above. */
21174 if (current_class_ptr
21175 && TREE_CODE (current_class_ptr
) == PARM_DECL
21176 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21181 /* Parse a late-specified return type, if any. This is not a separate
21182 non-terminal, but part of a function declarator, which looks like
21184 -> trailing-type-specifier-seq abstract-declarator(opt)
21186 Returns the type indicated by the type-id.
21188 In addition to this, parse any queued up #pragma omp declare simd
21189 clauses, and #pragma acc routine clauses.
21191 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21195 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21196 tree
& requires_clause
, cp_cv_quals quals
)
21199 tree type
= NULL_TREE
;
21200 bool declare_simd_p
= (parser
->omp_declare_simd
21202 && declarator
->kind
== cdk_id
);
21204 bool oacc_routine_p
= (parser
->oacc_routine
21206 && declarator
->kind
== cdk_id
);
21208 /* Peek at the next token. */
21209 token
= cp_lexer_peek_token (parser
->lexer
);
21210 /* A late-specified return type is indicated by an initial '->'. */
21211 if (token
->type
!= CPP_DEREF
21212 && token
->keyword
!= RID_REQUIRES
21213 && !(token
->type
== CPP_NAME
21214 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21215 && !(declare_simd_p
|| oacc_routine_p
))
21218 tree save_ccp
= current_class_ptr
;
21219 tree save_ccr
= current_class_ref
;
21222 /* DR 1207: 'this' is in scope in the trailing return type. */
21223 inject_this_parameter (current_class_type
, quals
);
21226 if (token
->type
== CPP_DEREF
)
21228 /* Consume the ->. */
21229 cp_lexer_consume_token (parser
->lexer
);
21231 type
= cp_parser_trailing_type_id (parser
);
21234 /* Function declarations may be followed by a trailing
21235 requires-clause. */
21236 requires_clause
= cp_parser_requires_clause_opt (parser
);
21238 if (declare_simd_p
)
21239 declarator
->attributes
21240 = cp_parser_late_parsing_omp_declare_simd (parser
,
21241 declarator
->attributes
);
21242 if (oacc_routine_p
)
21243 declarator
->attributes
21244 = cp_parser_late_parsing_oacc_routine (parser
,
21245 declarator
->attributes
);
21249 current_class_ptr
= save_ccp
;
21250 current_class_ref
= save_ccr
;
21256 /* Parse a declarator-id.
21260 :: [opt] nested-name-specifier [opt] type-name
21262 In the `id-expression' case, the value returned is as for
21263 cp_parser_id_expression if the id-expression was an unqualified-id.
21264 If the id-expression was a qualified-id, then a SCOPE_REF is
21265 returned. The first operand is the scope (either a NAMESPACE_DECL
21266 or TREE_TYPE), but the second is still just a representation of an
21270 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21273 /* The expression must be an id-expression. Assume that qualified
21274 names are the names of types so that:
21277 int S<T>::R::i = 3;
21279 will work; we must treat `S<T>::R' as the name of a type.
21280 Similarly, assume that qualified names are templates, where
21284 int S<T>::R<T>::i = 3;
21287 id
= cp_parser_id_expression (parser
,
21288 /*template_keyword_p=*/false,
21289 /*check_dependency_p=*/false,
21290 /*template_p=*/NULL
,
21291 /*declarator_p=*/true,
21293 if (id
&& BASELINK_P (id
))
21294 id
= BASELINK_FUNCTIONS (id
);
21298 /* Parse a type-id.
21301 type-specifier-seq abstract-declarator [opt]
21303 Returns the TYPE specified. */
21306 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21307 bool is_trailing_return
, location_t
* type_location
)
21309 cp_decl_specifier_seq type_specifier_seq
;
21310 cp_declarator
*abstract_declarator
;
21312 /* Parse the type-specifier-seq. */
21313 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21314 is_trailing_return
,
21315 &type_specifier_seq
);
21317 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21319 if (is_template_arg
&& type_specifier_seq
.type
21320 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21321 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21322 /* A bare template name as a template argument is a template template
21323 argument, not a placeholder, so fail parsing it as a type argument. */
21325 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21326 cp_parser_simulate_error (parser
);
21327 return error_mark_node
;
21329 if (type_specifier_seq
.type
== error_mark_node
)
21330 return error_mark_node
;
21332 /* There might or might not be an abstract declarator. */
21333 cp_parser_parse_tentatively (parser
);
21334 /* Look for the declarator. */
21335 abstract_declarator
21336 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21337 /*parenthesized_p=*/NULL
,
21338 /*member_p=*/false,
21339 /*friend_p=*/false);
21340 /* Check to see if there really was a declarator. */
21341 if (!cp_parser_parse_definitely (parser
))
21342 abstract_declarator
= NULL
;
21344 if (type_specifier_seq
.type
21345 /* The concepts TS allows 'auto' as a type-id. */
21346 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21347 /* None of the valid uses of 'auto' in C++14 involve the type-id
21348 nonterminal, but it is valid in a trailing-return-type. */
21349 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21350 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21352 /* A type-id with type 'auto' is only ok if the abstract declarator
21353 is a function declarator with a late-specified return type.
21355 A type-id with 'auto' is also valid in a trailing-return-type
21356 in a compound-requirement. */
21357 if (abstract_declarator
21358 && abstract_declarator
->kind
== cdk_function
21359 && abstract_declarator
->u
.function
.late_return_type
)
21361 else if (parser
->in_result_type_constraint_p
)
21365 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21366 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21368 error_at (loc
, "missing template arguments after %qT",
21370 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21374 error_at (loc
, "invalid use of %qT", auto_node
);
21375 return error_mark_node
;
21379 return groktypename (&type_specifier_seq
, abstract_declarator
,
21384 cp_parser_type_id (cp_parser
*parser
, location_t
* type_location
)
21386 return cp_parser_type_id_1 (parser
, false, false, type_location
);
21390 cp_parser_template_type_arg (cp_parser
*parser
)
21393 const char *saved_message
= parser
->type_definition_forbidden_message
;
21394 parser
->type_definition_forbidden_message
21395 = G_("types may not be defined in template arguments");
21396 r
= cp_parser_type_id_1 (parser
, true, false, NULL
);
21397 parser
->type_definition_forbidden_message
= saved_message
;
21398 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21400 error ("invalid use of %<auto%> in template argument");
21401 r
= error_mark_node
;
21407 cp_parser_trailing_type_id (cp_parser
*parser
)
21409 return cp_parser_type_id_1 (parser
, false, true, NULL
);
21412 /* Parse a type-specifier-seq.
21414 type-specifier-seq:
21415 type-specifier type-specifier-seq [opt]
21419 type-specifier-seq:
21420 attributes type-specifier-seq [opt]
21422 If IS_DECLARATION is true, we are at the start of a "condition" or
21423 exception-declaration, so we might be followed by a declarator-id.
21425 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21426 i.e. we've just seen "->".
21428 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21431 cp_parser_type_specifier_seq (cp_parser
* parser
,
21432 bool is_declaration
,
21433 bool is_trailing_return
,
21434 cp_decl_specifier_seq
*type_specifier_seq
)
21436 bool seen_type_specifier
= false;
21437 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21438 cp_token
*start_token
= NULL
;
21440 /* Clear the TYPE_SPECIFIER_SEQ. */
21441 clear_decl_specs (type_specifier_seq
);
21443 /* In the context of a trailing return type, enum E { } is an
21444 elaborated-type-specifier followed by a function-body, not an
21446 if (is_trailing_return
)
21447 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21449 /* Parse the type-specifiers and attributes. */
21452 tree type_specifier
;
21453 bool is_cv_qualifier
;
21455 /* Check for attributes first. */
21456 if (cp_next_tokens_can_be_attribute_p (parser
))
21458 type_specifier_seq
->attributes
21459 = attr_chainon (type_specifier_seq
->attributes
,
21460 cp_parser_attributes_opt (parser
));
21464 /* record the token of the beginning of the type specifier seq,
21465 for error reporting purposes*/
21467 start_token
= cp_lexer_peek_token (parser
->lexer
);
21469 /* Look for the type-specifier. */
21470 type_specifier
= cp_parser_type_specifier (parser
,
21472 type_specifier_seq
,
21473 /*is_declaration=*/false,
21476 if (!type_specifier
)
21478 /* If the first type-specifier could not be found, this is not a
21479 type-specifier-seq at all. */
21480 if (!seen_type_specifier
)
21482 /* Set in_declarator_p to avoid skipping to the semicolon. */
21483 int in_decl
= parser
->in_declarator_p
;
21484 parser
->in_declarator_p
= true;
21486 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21487 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21488 cp_parser_error (parser
, "expected type-specifier");
21490 parser
->in_declarator_p
= in_decl
;
21492 type_specifier_seq
->type
= error_mark_node
;
21495 /* If subsequent type-specifiers could not be found, the
21496 type-specifier-seq is complete. */
21500 seen_type_specifier
= true;
21501 /* The standard says that a condition can be:
21503 type-specifier-seq declarator = assignment-expression
21510 we should treat the "S" as a declarator, not as a
21511 type-specifier. The standard doesn't say that explicitly for
21512 type-specifier-seq, but it does say that for
21513 decl-specifier-seq in an ordinary declaration. Perhaps it
21514 would be clearer just to allow a decl-specifier-seq here, and
21515 then add a semantic restriction that if any decl-specifiers
21516 that are not type-specifiers appear, the program is invalid. */
21517 if (is_declaration
&& !is_cv_qualifier
)
21518 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21522 /* Return whether the function currently being declared has an associated
21523 template parameter list. */
21526 function_being_declared_is_template_p (cp_parser
* parser
)
21528 if (!current_template_parms
|| processing_template_parmlist
)
21531 if (parser
->implicit_template_scope
)
21534 if (at_class_scope_p ()
21535 && TYPE_BEING_DEFINED (current_class_type
))
21536 return parser
->num_template_parameter_lists
!= 0;
21538 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21539 (current_class_type
));
21542 /* Parse a parameter-declaration-clause.
21544 parameter-declaration-clause:
21545 parameter-declaration-list [opt] ... [opt]
21546 parameter-declaration-list , ...
21548 Returns a representation for the parameter declarations. A return
21549 value of NULL indicates a parameter-declaration-clause consisting
21550 only of an ellipsis. */
21553 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21559 temp_override
<bool> cleanup
21560 (parser
->auto_is_implicit_function_template_parm_p
);
21562 if (!processing_specialization
21563 && !processing_template_parmlist
21564 && !processing_explicit_instantiation
21565 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21566 actual function or a random abstract declarator. */
21567 && parser
->default_arg_ok_p
)
21568 if (!current_function_decl
21569 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21570 parser
->auto_is_implicit_function_template_parm_p
= true;
21572 /* Peek at the next token. */
21573 token
= cp_lexer_peek_token (parser
->lexer
);
21574 /* Check for trivial parameter-declaration-clauses. */
21575 if (token
->type
== CPP_ELLIPSIS
)
21577 /* Consume the `...' token. */
21578 cp_lexer_consume_token (parser
->lexer
);
21581 else if (token
->type
== CPP_CLOSE_PAREN
)
21582 /* There are no parameters. */
21583 return void_list_node
;
21584 /* Check for `(void)', too, which is a special case. */
21585 else if (token
->keyword
== RID_VOID
21586 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21587 == CPP_CLOSE_PAREN
))
21589 /* Consume the `void' token. */
21590 cp_lexer_consume_token (parser
->lexer
);
21591 /* There are no parameters. */
21592 return void_list_node
;
21595 /* Parse the parameter-declaration-list. */
21596 parameters
= cp_parser_parameter_declaration_list (parser
);
21597 /* If a parse error occurred while parsing the
21598 parameter-declaration-list, then the entire
21599 parameter-declaration-clause is erroneous. */
21600 if (parameters
== error_mark_node
)
21603 /* Peek at the next token. */
21604 token
= cp_lexer_peek_token (parser
->lexer
);
21605 /* If it's a `,', the clause should terminate with an ellipsis. */
21606 if (token
->type
== CPP_COMMA
)
21608 /* Consume the `,'. */
21609 cp_lexer_consume_token (parser
->lexer
);
21610 /* Expect an ellipsis. */
21612 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21614 /* It might also be `...' if the optional trailing `,' was
21616 else if (token
->type
== CPP_ELLIPSIS
)
21618 /* Consume the `...' token. */
21619 cp_lexer_consume_token (parser
->lexer
);
21620 /* And remember that we saw it. */
21624 ellipsis_p
= false;
21626 /* Finish the parameter list. */
21628 parameters
= chainon (parameters
, void_list_node
);
21633 /* Parse a parameter-declaration-list.
21635 parameter-declaration-list:
21636 parameter-declaration
21637 parameter-declaration-list , parameter-declaration
21639 Returns a representation of the parameter-declaration-list, as for
21640 cp_parser_parameter_declaration_clause. However, the
21641 `void_list_node' is never appended to the list. */
21644 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21646 tree parameters
= NULL_TREE
;
21647 tree
*tail
= ¶meters
;
21648 bool saved_in_unbraced_linkage_specification_p
;
21651 /* The special considerations that apply to a function within an
21652 unbraced linkage specifications do not apply to the parameters
21653 to the function. */
21654 saved_in_unbraced_linkage_specification_p
21655 = parser
->in_unbraced_linkage_specification_p
;
21656 parser
->in_unbraced_linkage_specification_p
= false;
21658 /* Look for more parameters. */
21661 cp_parameter_declarator
*parameter
;
21662 tree decl
= error_mark_node
;
21663 bool parenthesized_p
= false;
21665 /* Parse the parameter. */
21667 = cp_parser_parameter_declaration (parser
,
21668 /*template_parm_p=*/false,
21671 /* We don't know yet if the enclosing context is deprecated, so wait
21672 and warn in grokparms if appropriate. */
21673 deprecated_state
= DEPRECATED_SUPPRESS
;
21677 decl
= grokdeclarator (parameter
->declarator
,
21678 ¶meter
->decl_specifiers
,
21680 parameter
->default_argument
!= NULL_TREE
,
21681 ¶meter
->decl_specifiers
.attributes
);
21682 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21683 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21686 deprecated_state
= DEPRECATED_NORMAL
;
21688 /* If a parse error occurred parsing the parameter declaration,
21689 then the entire parameter-declaration-list is erroneous. */
21690 if (decl
== error_mark_node
)
21692 parameters
= error_mark_node
;
21696 if (parameter
->decl_specifiers
.attributes
)
21697 cplus_decl_attributes (&decl
,
21698 parameter
->decl_specifiers
.attributes
,
21700 if (DECL_NAME (decl
))
21701 decl
= pushdecl (decl
);
21703 if (decl
!= error_mark_node
)
21705 retrofit_lang_decl (decl
);
21706 DECL_PARM_INDEX (decl
) = ++index
;
21707 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21710 /* Add the new parameter to the list. */
21711 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21712 tail
= &TREE_CHAIN (*tail
);
21714 /* Peek at the next token. */
21715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21716 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21717 /* These are for Objective-C++ */
21718 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21719 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21720 /* The parameter-declaration-list is complete. */
21722 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21726 /* Peek at the next token. */
21727 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21728 /* If it's an ellipsis, then the list is complete. */
21729 if (token
->type
== CPP_ELLIPSIS
)
21731 /* Otherwise, there must be more parameters. Consume the
21733 cp_lexer_consume_token (parser
->lexer
);
21734 /* When parsing something like:
21736 int i(float f, double d)
21738 we can tell after seeing the declaration for "f" that we
21739 are not looking at an initialization of a variable "i",
21740 but rather at the declaration of a function "i".
21742 Due to the fact that the parsing of template arguments
21743 (as specified to a template-id) requires backtracking we
21744 cannot use this technique when inside a template argument
21746 if (!parser
->in_template_argument_list_p
21747 && !parser
->in_type_id_in_expr_p
21748 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21749 /* However, a parameter-declaration of the form
21750 "float(f)" (which is a valid declaration of a
21751 parameter "f") can also be interpreted as an
21752 expression (the conversion of "f" to "float"). */
21753 && !parenthesized_p
)
21754 cp_parser_commit_to_tentative_parse (parser
);
21758 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21759 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21760 cp_parser_skip_to_closing_parenthesis (parser
,
21761 /*recovering=*/true,
21762 /*or_comma=*/false,
21763 /*consume_paren=*/false);
21768 parser
->in_unbraced_linkage_specification_p
21769 = saved_in_unbraced_linkage_specification_p
;
21771 /* Reset implicit_template_scope if we are about to leave the function
21772 parameter list that introduced it. Note that for out-of-line member
21773 definitions, there will be one or more class scopes before we get to
21774 the template parameter scope. */
21776 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21777 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21779 while (maybe_its
->kind
== sk_class
)
21780 maybe_its
= maybe_its
->level_chain
;
21781 if (maybe_its
== its
)
21783 parser
->implicit_template_parms
= 0;
21784 parser
->implicit_template_scope
= 0;
21791 /* Parse a parameter declaration.
21793 parameter-declaration:
21794 decl-specifier-seq ... [opt] declarator
21795 decl-specifier-seq declarator = assignment-expression
21796 decl-specifier-seq ... [opt] abstract-declarator [opt]
21797 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21799 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21800 declares a template parameter. (In that case, a non-nested `>'
21801 token encountered during the parsing of the assignment-expression
21802 is not interpreted as a greater-than operator.)
21804 Returns a representation of the parameter, or NULL if an error
21805 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21806 true iff the declarator is of the form "(p)". */
21808 static cp_parameter_declarator
*
21809 cp_parser_parameter_declaration (cp_parser
*parser
,
21810 bool template_parm_p
,
21811 bool *parenthesized_p
)
21813 int declares_class_or_enum
;
21814 cp_decl_specifier_seq decl_specifiers
;
21815 cp_declarator
*declarator
;
21816 tree default_argument
;
21817 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21818 const char *saved_message
;
21819 bool template_parameter_pack_p
= false;
21821 /* In a template parameter, `>' is not an operator.
21825 When parsing a default template-argument for a non-type
21826 template-parameter, the first non-nested `>' is taken as the end
21827 of the template parameter-list rather than a greater-than
21830 /* Type definitions may not appear in parameter types. */
21831 saved_message
= parser
->type_definition_forbidden_message
;
21832 parser
->type_definition_forbidden_message
21833 = G_("types may not be defined in parameter types");
21835 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21836 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21837 (current_template_parms
)) : 0);
21839 /* Parse the declaration-specifiers. */
21840 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21841 cp_parser_decl_specifier_seq (parser
,
21842 CP_PARSER_FLAGS_NONE
,
21844 &declares_class_or_enum
);
21846 /* Complain about missing 'typename' or other invalid type names. */
21847 if (!decl_specifiers
.any_type_specifiers_p
21848 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21849 decl_specifiers
.type
= error_mark_node
;
21851 /* If an error occurred, there's no reason to attempt to parse the
21852 rest of the declaration. */
21853 if (cp_parser_error_occurred (parser
))
21855 parser
->type_definition_forbidden_message
= saved_message
;
21859 /* Peek at the next token. */
21860 token
= cp_lexer_peek_token (parser
->lexer
);
21862 /* If the next token is a `)', `,', `=', `>', or `...', then there
21863 is no declarator. However, when variadic templates are enabled,
21864 there may be a declarator following `...'. */
21865 if (token
->type
== CPP_CLOSE_PAREN
21866 || token
->type
== CPP_COMMA
21867 || token
->type
== CPP_EQ
21868 || token
->type
== CPP_GREATER
)
21871 if (parenthesized_p
)
21872 *parenthesized_p
= false;
21874 /* Otherwise, there should be a declarator. */
21877 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21878 parser
->default_arg_ok_p
= false;
21880 /* After seeing a decl-specifier-seq, if the next token is not a
21881 "(", there is no possibility that the code is a valid
21882 expression. Therefore, if parsing tentatively, we commit at
21884 if (!parser
->in_template_argument_list_p
21885 /* In an expression context, having seen:
21889 we cannot be sure whether we are looking at a
21890 function-type (taking a "char" as a parameter) or a cast
21891 of some object of type "char" to "int". */
21892 && !parser
->in_type_id_in_expr_p
21893 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21894 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21895 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21896 cp_parser_commit_to_tentative_parse (parser
);
21897 /* Parse the declarator. */
21898 declarator_token_start
= token
;
21899 declarator
= cp_parser_declarator (parser
,
21900 CP_PARSER_DECLARATOR_EITHER
,
21901 /*ctor_dtor_or_conv_p=*/NULL
,
21903 /*member_p=*/false,
21904 /*friend_p=*/false);
21905 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21906 /* After the declarator, allow more attributes. */
21907 decl_specifiers
.attributes
21908 = attr_chainon (decl_specifiers
.attributes
,
21909 cp_parser_attributes_opt (parser
));
21911 /* If the declarator is a template parameter pack, remember that and
21912 clear the flag in the declarator itself so we don't get errors
21913 from grokdeclarator. */
21914 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21916 declarator
->parameter_pack_p
= false;
21917 template_parameter_pack_p
= true;
21921 /* If the next token is an ellipsis, and we have not seen a declarator
21922 name, and if either the type of the declarator contains parameter
21923 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21924 for, eg, abbreviated integral type names), then we actually have a
21925 parameter pack expansion expression. Otherwise, leave the ellipsis
21926 for a C-style variadic function. */
21927 token
= cp_lexer_peek_token (parser
->lexer
);
21929 /* If a function parameter pack was specified and an implicit template
21930 parameter was introduced during cp_parser_parameter_declaration,
21931 change any implicit parameters introduced into packs. */
21932 if (parser
->implicit_template_parms
21933 && ((token
->type
== CPP_ELLIPSIS
21934 && declarator_can_be_parameter_pack (declarator
))
21935 || (declarator
&& declarator
->parameter_pack_p
)))
21937 int latest_template_parm_idx
= TREE_VEC_LENGTH
21938 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21940 if (latest_template_parm_idx
!= template_parm_idx
)
21941 decl_specifiers
.type
= convert_generic_types_to_packs
21942 (decl_specifiers
.type
,
21943 template_parm_idx
, latest_template_parm_idx
);
21946 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21948 tree type
= decl_specifiers
.type
;
21950 if (type
&& DECL_P (type
))
21951 type
= TREE_TYPE (type
);
21954 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21955 && (template_parm_p
|| uses_parameter_packs (type
)))
21956 || (!type
&& template_parm_p
))
21957 && declarator_can_be_parameter_pack (declarator
))
21959 /* Consume the `...'. */
21960 cp_lexer_consume_token (parser
->lexer
);
21961 maybe_warn_variadic_templates ();
21963 /* Build a pack expansion type */
21964 if (template_parm_p
)
21965 template_parameter_pack_p
= true;
21966 else if (declarator
)
21967 declarator
->parameter_pack_p
= true;
21969 decl_specifiers
.type
= make_pack_expansion (type
);
21973 /* The restriction on defining new types applies only to the type
21974 of the parameter, not to the default argument. */
21975 parser
->type_definition_forbidden_message
= saved_message
;
21977 /* If the next token is `=', then process a default argument. */
21978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21980 tree type
= decl_specifiers
.type
;
21981 token
= cp_lexer_peek_token (parser
->lexer
);
21982 /* If we are defining a class, then the tokens that make up the
21983 default argument must be saved and processed later. */
21984 if (!template_parm_p
&& at_class_scope_p ()
21985 && TYPE_BEING_DEFINED (current_class_type
)
21986 && !LAMBDA_TYPE_P (current_class_type
))
21987 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21989 // A constrained-type-specifier may declare a type template-parameter.
21990 else if (declares_constrained_type_template_parameter (type
))
21992 = cp_parser_default_type_template_argument (parser
);
21994 // A constrained-type-specifier may declare a template-template-parameter.
21995 else if (declares_constrained_template_template_parameter (type
))
21997 = cp_parser_default_template_template_argument (parser
);
21999 /* Outside of a class definition, we can just parse the
22000 assignment-expression. */
22003 = cp_parser_default_argument (parser
, template_parm_p
);
22005 if (!parser
->default_arg_ok_p
)
22007 permerror (token
->location
,
22008 "default arguments are only "
22009 "permitted for function parameters");
22011 else if ((declarator
&& declarator
->parameter_pack_p
)
22012 || template_parameter_pack_p
22013 || (decl_specifiers
.type
22014 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22016 /* Find the name of the parameter pack. */
22017 cp_declarator
*id_declarator
= declarator
;
22018 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22019 id_declarator
= id_declarator
->declarator
;
22021 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22022 error_at (declarator_token_start
->location
,
22024 ? G_("template parameter pack %qD "
22025 "cannot have a default argument")
22026 : G_("parameter pack %qD cannot have "
22027 "a default argument"),
22028 id_declarator
->u
.id
.unqualified_name
);
22030 error_at (declarator_token_start
->location
,
22032 ? G_("template parameter pack cannot have "
22033 "a default argument")
22034 : G_("parameter pack cannot have a "
22035 "default argument"));
22037 default_argument
= NULL_TREE
;
22041 default_argument
= NULL_TREE
;
22043 /* Generate a location for the parameter, ranging from the start of the
22044 initial token to the end of the final token (using input_location for
22045 the latter, set up by cp_lexer_set_source_position_from_token when
22048 If we have a identifier, then use it for the caret location, e.g.
22050 extern int callee (int one, int (*two)(int, int), float three);
22051 ~~~~~~^~~~~~~~~~~~~~
22053 otherwise, reuse the start location for the caret location e.g.:
22055 extern int callee (int one, int (*)(int, int), float three);
22059 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22060 ? declarator
->id_loc
22061 : decl_spec_token_start
->location
);
22062 location_t param_loc
= make_location (caret_loc
,
22063 decl_spec_token_start
->location
,
22066 return make_parameter_declarator (&decl_specifiers
,
22070 template_parameter_pack_p
);
22073 /* Parse a default argument and return it.
22075 TEMPLATE_PARM_P is true if this is a default argument for a
22076 non-type template parameter. */
22078 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22080 tree default_argument
= NULL_TREE
;
22081 bool saved_greater_than_is_operator_p
;
22082 bool saved_local_variables_forbidden_p
;
22083 bool non_constant_p
, is_direct_init
;
22085 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22087 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22088 parser
->greater_than_is_operator_p
= !template_parm_p
;
22089 /* Local variable names (and the `this' keyword) may not
22090 appear in a default argument. */
22091 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22092 parser
->local_variables_forbidden_p
= true;
22093 /* Parse the assignment-expression. */
22094 if (template_parm_p
)
22095 push_deferring_access_checks (dk_no_deferred
);
22096 tree saved_class_ptr
= NULL_TREE
;
22097 tree saved_class_ref
= NULL_TREE
;
22098 /* The "this" pointer is not valid in a default argument. */
22101 saved_class_ptr
= current_class_ptr
;
22102 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22103 saved_class_ref
= current_class_ref
;
22104 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22107 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22108 /* Restore the "this" pointer. */
22111 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22112 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22114 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22115 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22116 if (template_parm_p
)
22117 pop_deferring_access_checks ();
22118 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22119 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22121 return default_argument
;
22124 /* Parse a function-body.
22127 compound_statement */
22130 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22132 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22133 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22137 /* Parse a ctor-initializer-opt followed by a function-body. Return
22138 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22139 is true we are parsing a function-try-block. */
22142 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22143 bool in_function_try_block
)
22146 const bool check_body_p
=
22147 DECL_CONSTRUCTOR_P (current_function_decl
)
22148 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22151 /* Begin the function body. */
22152 body
= begin_function_body ();
22153 /* Parse the optional ctor-initializer. */
22154 cp_parser_ctor_initializer_opt (parser
);
22156 /* If we're parsing a constexpr constructor definition, we need
22157 to check that the constructor body is indeed empty. However,
22158 before we get to cp_parser_function_body lot of junk has been
22159 generated, so we can't just check that we have an empty block.
22160 Rather we take a snapshot of the outermost block, and check whether
22161 cp_parser_function_body changed its state. */
22164 list
= cur_stmt_list
;
22165 if (STATEMENT_LIST_TAIL (list
))
22166 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22168 /* Parse the function-body. */
22169 cp_parser_function_body (parser
, in_function_try_block
);
22171 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22172 /* Finish the function body. */
22173 finish_function_body (body
);
22176 /* Parse an initializer.
22179 = initializer-clause
22180 ( expression-list )
22182 Returns an expression representing the initializer. If no
22183 initializer is present, NULL_TREE is returned.
22185 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22186 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22187 set to TRUE if there is no initializer present. If there is an
22188 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22189 is set to true; otherwise it is set to false. */
22192 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22193 bool* non_constant_p
, bool subexpression_p
)
22198 /* Peek at the next token. */
22199 token
= cp_lexer_peek_token (parser
->lexer
);
22201 /* Let our caller know whether or not this initializer was
22203 *is_direct_init
= (token
->type
!= CPP_EQ
);
22204 /* Assume that the initializer is constant. */
22205 *non_constant_p
= false;
22207 if (token
->type
== CPP_EQ
)
22209 /* Consume the `='. */
22210 cp_lexer_consume_token (parser
->lexer
);
22211 /* Parse the initializer-clause. */
22212 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22214 else if (token
->type
== CPP_OPEN_PAREN
)
22216 vec
<tree
, va_gc
> *vec
;
22217 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22219 /*allow_expansion_p=*/true,
22222 return error_mark_node
;
22223 init
= build_tree_list_vec (vec
);
22224 release_tree_vector (vec
);
22226 else if (token
->type
== CPP_OPEN_BRACE
)
22228 cp_lexer_set_source_position (parser
->lexer
);
22229 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22230 init
= cp_parser_braced_list (parser
, non_constant_p
);
22231 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22235 /* Anything else is an error. */
22236 cp_parser_error (parser
, "expected initializer");
22237 init
= error_mark_node
;
22240 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22241 init
= error_mark_node
;
22246 /* Parse an initializer-clause.
22248 initializer-clause:
22249 assignment-expression
22252 Returns an expression representing the initializer.
22254 If the `assignment-expression' production is used the value
22255 returned is simply a representation for the expression.
22257 Otherwise, calls cp_parser_braced_list. */
22260 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22262 cp_expr initializer
;
22264 /* Assume the expression is constant. */
22265 *non_constant_p
= false;
22267 /* If it is not a `{', then we are looking at an
22268 assignment-expression. */
22269 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22272 = cp_parser_constant_expression (parser
,
22273 /*allow_non_constant_p=*/true,
22277 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22279 return initializer
;
22282 /* Parse a brace-enclosed initializer list.
22285 { initializer-list , [opt] }
22286 { designated-initializer-list , [opt] }
22289 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22290 the elements of the initializer-list (or NULL, if the last
22291 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22292 NULL_TREE. There is no way to detect whether or not the optional
22293 trailing `,' was provided. NON_CONSTANT_P is as for
22294 cp_parser_initializer. */
22297 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22300 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22302 /* Consume the `{' token. */
22303 matching_braces braces
;
22304 braces
.require_open (parser
);
22305 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22306 initializer
= make_node (CONSTRUCTOR
);
22307 /* If it's not a `}', then there is a non-trivial initializer. */
22308 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22310 /* Parse the initializer list. */
22311 CONSTRUCTOR_ELTS (initializer
)
22312 = cp_parser_initializer_list (parser
, non_constant_p
);
22313 /* A trailing `,' token is allowed. */
22314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22315 cp_lexer_consume_token (parser
->lexer
);
22318 *non_constant_p
= false;
22319 /* Now, there should be a trailing `}'. */
22320 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22321 braces
.require_close (parser
);
22322 TREE_TYPE (initializer
) = init_list_type_node
;
22324 cp_expr
result (initializer
);
22325 /* Build a location of the form:
22328 with caret==start at the open brace, finish at the close brace. */
22329 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22330 result
.set_location (combined_loc
);
22334 /* Consume tokens up to, and including, the next non-nested closing `]'.
22335 Returns true iff we found a closing `]'. */
22338 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22340 unsigned square_depth
= 0;
22344 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22346 switch (token
->type
)
22349 case CPP_PRAGMA_EOL
:
22350 /* If we've run out of tokens, then there is no closing `]'. */
22353 case CPP_OPEN_SQUARE
:
22357 case CPP_CLOSE_SQUARE
:
22358 if (!square_depth
--)
22360 cp_lexer_consume_token (parser
->lexer
);
22369 /* Consume the token. */
22370 cp_lexer_consume_token (parser
->lexer
);
22374 /* Return true if we are looking at an array-designator, false otherwise. */
22377 cp_parser_array_designator_p (cp_parser
*parser
)
22379 /* Consume the `['. */
22380 cp_lexer_consume_token (parser
->lexer
);
22382 cp_lexer_save_tokens (parser
->lexer
);
22384 /* Skip tokens until the next token is a closing square bracket.
22385 If we find the closing `]', and the next token is a `=', then
22386 we are looking at an array designator. */
22387 bool array_designator_p
22388 = (cp_parser_skip_to_closing_square_bracket (parser
)
22389 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22391 /* Roll back the tokens we skipped. */
22392 cp_lexer_rollback_tokens (parser
->lexer
);
22394 return array_designator_p
;
22397 /* Parse an initializer-list.
22400 initializer-clause ... [opt]
22401 initializer-list , initializer-clause ... [opt]
22405 designated-initializer-list:
22406 designated-initializer-clause
22407 designated-initializer-list , designated-initializer-clause
22409 designated-initializer-clause:
22410 designator brace-or-equal-initializer
22418 designation initializer-clause ...[opt]
22419 initializer-list , designation initializer-clause ...[opt]
22424 [ constant-expression ] =
22426 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22427 for the initializer. If the INDEX of the elt is non-NULL, it is the
22428 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22429 as for cp_parser_initializer. */
22431 static vec
<constructor_elt
, va_gc
> *
22432 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22434 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22435 bool first_p
= true;
22436 tree first_designator
= NULL_TREE
;
22438 /* Assume all of the expressions are constant. */
22439 *non_constant_p
= false;
22441 /* Parse the rest of the list. */
22447 bool clause_non_constant_p
;
22448 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22450 /* Handle the C++2A syntax, '. id ='. */
22451 if ((cxx_dialect
>= cxx2a
22452 || cp_parser_allow_gnu_extensions_p (parser
))
22453 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22454 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22455 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22456 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22457 == CPP_OPEN_BRACE
)))
22459 if (cxx_dialect
< cxx2a
)
22460 pedwarn (loc
, OPT_Wpedantic
,
22461 "C++ designated initializers only available with "
22462 "-std=c++2a or -std=gnu++2a");
22463 /* Consume the `.'. */
22464 cp_lexer_consume_token (parser
->lexer
);
22465 /* Consume the identifier. */
22466 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22468 /* Consume the `='. */
22469 cp_lexer_consume_token (parser
->lexer
);
22471 /* Also, if the next token is an identifier and the following one is a
22472 colon, we are looking at the GNU designated-initializer
22474 else if (cp_parser_allow_gnu_extensions_p (parser
)
22475 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22476 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22479 /* Warn the user that they are using an extension. */
22480 pedwarn (loc
, OPT_Wpedantic
,
22481 "ISO C++ does not allow GNU designated initializers");
22482 /* Consume the identifier. */
22483 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22484 /* Consume the `:'. */
22485 cp_lexer_consume_token (parser
->lexer
);
22487 /* Also handle C99 array designators, '[ const ] ='. */
22488 else if (cp_parser_allow_gnu_extensions_p (parser
)
22489 && !c_dialect_objc ()
22490 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22492 /* In C++11, [ could start a lambda-introducer. */
22493 bool non_const
= false;
22495 cp_parser_parse_tentatively (parser
);
22497 if (!cp_parser_array_designator_p (parser
))
22499 cp_parser_simulate_error (parser
);
22500 designator
= NULL_TREE
;
22504 designator
= cp_parser_constant_expression (parser
, true,
22506 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22507 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22510 if (!cp_parser_parse_definitely (parser
))
22511 designator
= NULL_TREE
;
22513 && (!require_potential_rvalue_constant_expression
22515 designator
= NULL_TREE
;
22517 /* Warn the user that they are using an extension. */
22518 pedwarn (loc
, OPT_Wpedantic
,
22519 "ISO C++ does not allow C99 designated initializers");
22522 designator
= NULL_TREE
;
22526 first_designator
= designator
;
22529 else if (cxx_dialect
>= cxx2a
22530 && first_designator
!= error_mark_node
22531 && (!first_designator
!= !designator
))
22533 error_at (loc
, "either all initializer clauses should be designated "
22534 "or none of them should be");
22535 first_designator
= error_mark_node
;
22537 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22538 first_designator
= designator
;
22540 /* Parse the initializer. */
22541 initializer
= cp_parser_initializer_clause (parser
,
22542 &clause_non_constant_p
);
22543 /* If any clause is non-constant, so is the entire initializer. */
22544 if (clause_non_constant_p
)
22545 *non_constant_p
= true;
22547 /* If we have an ellipsis, this is an initializer pack
22549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22551 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22553 /* Consume the `...'. */
22554 cp_lexer_consume_token (parser
->lexer
);
22556 if (designator
&& cxx_dialect
>= cxx2a
)
22558 "%<...%> not allowed in designated initializer list");
22560 /* Turn the initializer into an initializer expansion. */
22561 initializer
= make_pack_expansion (initializer
);
22564 /* Add it to the vector. */
22565 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22567 /* If the next token is not a comma, we have reached the end of
22569 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22572 /* Peek at the next token. */
22573 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22574 /* If the next token is a `}', then we're still done. An
22575 initializer-clause can have a trailing `,' after the
22576 initializer-list and before the closing `}'. */
22577 if (token
->type
== CPP_CLOSE_BRACE
)
22580 /* Consume the `,' token. */
22581 cp_lexer_consume_token (parser
->lexer
);
22584 /* The same identifier shall not appear in multiple designators
22585 of a designated-initializer-list. */
22586 if (first_designator
)
22589 tree designator
, val
;
22590 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22591 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22593 if (IDENTIFIER_MARKED (designator
))
22595 error_at (cp_expr_loc_or_loc (val
, input_location
),
22596 "%<.%s%> designator used multiple times in "
22597 "the same initializer list",
22598 IDENTIFIER_POINTER (designator
));
22599 (*v
)[i
].index
= error_mark_node
;
22602 IDENTIFIER_MARKED (designator
) = 1;
22604 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22605 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22606 IDENTIFIER_MARKED (designator
) = 0;
22612 /* Classes [gram.class] */
22614 /* Parse a class-name.
22620 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22621 to indicate that names looked up in dependent types should be
22622 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22623 keyword has been used to indicate that the name that appears next
22624 is a template. TAG_TYPE indicates the explicit tag given before
22625 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22626 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22627 is the class being defined in a class-head. If ENUM_OK is TRUE,
22628 enum-names are also accepted.
22630 Returns the TYPE_DECL representing the class. */
22633 cp_parser_class_name (cp_parser
*parser
,
22634 bool typename_keyword_p
,
22635 bool template_keyword_p
,
22636 enum tag_types tag_type
,
22637 bool check_dependency_p
,
22639 bool is_declaration
,
22646 tree identifier
= NULL_TREE
;
22648 /* All class-names start with an identifier. */
22649 token
= cp_lexer_peek_token (parser
->lexer
);
22650 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22652 cp_parser_error (parser
, "expected class-name");
22653 return error_mark_node
;
22656 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22657 to a template-id, so we save it here. */
22658 scope
= parser
->scope
;
22659 if (scope
== error_mark_node
)
22660 return error_mark_node
;
22662 /* Any name names a type if we're following the `typename' keyword
22663 in a qualified name where the enclosing scope is type-dependent. */
22664 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22665 && dependent_type_p (scope
));
22666 /* Handle the common case (an identifier, but not a template-id)
22668 if (token
->type
== CPP_NAME
22669 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22671 cp_token
*identifier_token
;
22674 /* Look for the identifier. */
22675 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22676 ambiguous_p
= identifier_token
->error_reported
;
22677 identifier
= cp_parser_identifier (parser
);
22678 /* If the next token isn't an identifier, we are certainly not
22679 looking at a class-name. */
22680 if (identifier
== error_mark_node
)
22681 decl
= error_mark_node
;
22682 /* If we know this is a type-name, there's no need to look it
22684 else if (typename_p
)
22688 tree ambiguous_decls
;
22689 /* If we already know that this lookup is ambiguous, then
22690 we've already issued an error message; there's no reason
22694 cp_parser_simulate_error (parser
);
22695 return error_mark_node
;
22697 /* If the next token is a `::', then the name must be a type
22700 [basic.lookup.qual]
22702 During the lookup for a name preceding the :: scope
22703 resolution operator, object, function, and enumerator
22704 names are ignored. */
22705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22706 tag_type
= scope_type
;
22707 /* Look up the name. */
22708 decl
= cp_parser_lookup_name (parser
, identifier
,
22710 /*is_template=*/false,
22711 /*is_namespace=*/false,
22712 check_dependency_p
,
22714 identifier_token
->location
);
22715 if (ambiguous_decls
)
22717 if (cp_parser_parsing_tentatively (parser
))
22718 cp_parser_simulate_error (parser
);
22719 return error_mark_node
;
22725 /* Try a template-id. */
22726 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22727 check_dependency_p
,
22730 if (decl
== error_mark_node
)
22731 return error_mark_node
;
22734 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22736 /* If this is a typename, create a TYPENAME_TYPE. */
22737 if (typename_p
&& decl
!= error_mark_node
)
22739 decl
= make_typename_type (scope
, decl
, typename_type
,
22740 /*complain=*/tf_error
);
22741 if (decl
!= error_mark_node
)
22742 decl
= TYPE_NAME (decl
);
22745 decl
= strip_using_decl (decl
);
22747 /* Check to see that it is really the name of a class. */
22748 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22749 && identifier_p (TREE_OPERAND (decl
, 0))
22750 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22751 /* Situations like this:
22753 template <typename T> struct A {
22754 typename T::template X<int>::I i;
22757 are problematic. Is `T::template X<int>' a class-name? The
22758 standard does not seem to be definitive, but there is no other
22759 valid interpretation of the following `::'. Therefore, those
22760 names are considered class-names. */
22762 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22763 if (decl
!= error_mark_node
)
22764 decl
= TYPE_NAME (decl
);
22766 else if (TREE_CODE (decl
) != TYPE_DECL
22767 || TREE_TYPE (decl
) == error_mark_node
22768 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22769 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22770 /* In Objective-C 2.0, a classname followed by '.' starts a
22771 dot-syntax expression, and it's not a type-name. */
22772 || (c_dialect_objc ()
22773 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22774 && objc_is_class_name (decl
)))
22775 decl
= error_mark_node
;
22777 if (decl
== error_mark_node
)
22778 cp_parser_error (parser
, "expected class-name");
22779 else if (identifier
&& !parser
->scope
)
22780 maybe_note_name_used_in_class (identifier
, decl
);
22785 /* Parse a class-specifier.
22788 class-head { member-specification [opt] }
22790 Returns the TREE_TYPE representing the class. */
22793 cp_parser_class_specifier_1 (cp_parser
* parser
)
22796 tree attributes
= NULL_TREE
;
22797 bool nested_name_specifier_p
;
22798 unsigned saved_num_template_parameter_lists
;
22799 bool saved_in_function_body
;
22800 unsigned char in_statement
;
22801 bool in_switch_statement_p
;
22802 bool saved_in_unbraced_linkage_specification_p
;
22803 tree old_scope
= NULL_TREE
;
22804 tree scope
= NULL_TREE
;
22805 cp_token
*closing_brace
;
22807 push_deferring_access_checks (dk_no_deferred
);
22809 /* Parse the class-head. */
22810 type
= cp_parser_class_head (parser
,
22811 &nested_name_specifier_p
);
22812 /* If the class-head was a semantic disaster, skip the entire body
22816 cp_parser_skip_to_end_of_block_or_statement (parser
);
22817 pop_deferring_access_checks ();
22818 return error_mark_node
;
22821 /* Look for the `{'. */
22822 matching_braces braces
;
22823 if (!braces
.require_open (parser
))
22825 pop_deferring_access_checks ();
22826 return error_mark_node
;
22829 cp_ensure_no_omp_declare_simd (parser
);
22830 cp_ensure_no_oacc_routine (parser
);
22832 /* Issue an error message if type-definitions are forbidden here. */
22833 cp_parser_check_type_definition (parser
);
22834 /* Remember that we are defining one more class. */
22835 ++parser
->num_classes_being_defined
;
22836 /* Inside the class, surrounding template-parameter-lists do not
22838 saved_num_template_parameter_lists
22839 = parser
->num_template_parameter_lists
;
22840 parser
->num_template_parameter_lists
= 0;
22841 /* We are not in a function body. */
22842 saved_in_function_body
= parser
->in_function_body
;
22843 parser
->in_function_body
= false;
22844 /* Or in a loop. */
22845 in_statement
= parser
->in_statement
;
22846 parser
->in_statement
= 0;
22847 /* Or in a switch. */
22848 in_switch_statement_p
= parser
->in_switch_statement_p
;
22849 parser
->in_switch_statement_p
= false;
22850 /* We are not immediately inside an extern "lang" block. */
22851 saved_in_unbraced_linkage_specification_p
22852 = parser
->in_unbraced_linkage_specification_p
;
22853 parser
->in_unbraced_linkage_specification_p
= false;
22855 // Associate constraints with the type.
22857 type
= associate_classtype_constraints (type
);
22859 /* Start the class. */
22860 if (nested_name_specifier_p
)
22862 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22863 old_scope
= push_inner_scope (scope
);
22865 type
= begin_class_definition (type
);
22867 if (type
== error_mark_node
)
22868 /* If the type is erroneous, skip the entire body of the class. */
22869 cp_parser_skip_to_closing_brace (parser
);
22871 /* Parse the member-specification. */
22872 cp_parser_member_specification_opt (parser
);
22874 /* Look for the trailing `}'. */
22875 closing_brace
= braces
.require_close (parser
);
22876 /* Look for trailing attributes to apply to this class. */
22877 if (cp_parser_allow_gnu_extensions_p (parser
))
22878 attributes
= cp_parser_gnu_attributes_opt (parser
);
22879 if (type
!= error_mark_node
)
22880 type
= finish_struct (type
, attributes
);
22881 if (nested_name_specifier_p
)
22882 pop_inner_scope (old_scope
, scope
);
22884 /* We've finished a type definition. Check for the common syntax
22885 error of forgetting a semicolon after the definition. We need to
22886 be careful, as we can't just check for not-a-semicolon and be done
22887 with it; the user might have typed:
22889 class X { } c = ...;
22890 class X { } *p = ...;
22892 and so forth. Instead, enumerate all the possible tokens that
22893 might follow this production; if we don't see one of them, then
22894 complain and silently insert the semicolon. */
22896 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22897 bool want_semicolon
= true;
22899 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22900 /* Don't try to parse c++11 attributes here. As per the
22901 grammar, that should be a task for
22902 cp_parser_decl_specifier_seq. */
22903 want_semicolon
= false;
22905 switch (token
->type
)
22908 case CPP_SEMICOLON
:
22911 case CPP_OPEN_PAREN
:
22912 case CPP_CLOSE_PAREN
:
22914 want_semicolon
= false;
22917 /* While it's legal for type qualifiers and storage class
22918 specifiers to follow type definitions in the grammar, only
22919 compiler testsuites contain code like that. Assume that if
22920 we see such code, then what we're really seeing is a case
22924 const <type> var = ...;
22929 static <type> func (...) ...
22931 i.e. the qualifier or specifier applies to the next
22932 declaration. To do so, however, we need to look ahead one
22933 more token to see if *that* token is a type specifier.
22935 This code could be improved to handle:
22938 static const <type> var = ...; */
22940 if (keyword_is_decl_specifier (token
->keyword
))
22942 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22944 /* Handling user-defined types here would be nice, but very
22947 = (lookahead
->type
== CPP_KEYWORD
22948 && keyword_begins_type_specifier (lookahead
->keyword
));
22955 /* If we don't have a type, then something is very wrong and we
22956 shouldn't try to do anything clever. Likewise for not seeing the
22958 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22960 /* Locate the closing brace. */
22961 cp_token_position prev
22962 = cp_lexer_previous_token_position (parser
->lexer
);
22963 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22964 location_t loc
= prev_token
->location
;
22966 /* We want to suggest insertion of a ';' immediately *after* the
22967 closing brace, so, if we can, offset the location by 1 column. */
22968 location_t next_loc
= loc
;
22969 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22970 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22972 rich_location
richloc (line_table
, next_loc
);
22974 /* If we successfully offset the location, suggest the fix-it. */
22975 if (next_loc
!= loc
)
22976 richloc
.add_fixit_insert_before (next_loc
, ";");
22978 if (CLASSTYPE_DECLARED_CLASS (type
))
22979 error_at (&richloc
,
22980 "expected %<;%> after class definition");
22981 else if (TREE_CODE (type
) == RECORD_TYPE
)
22982 error_at (&richloc
,
22983 "expected %<;%> after struct definition");
22984 else if (TREE_CODE (type
) == UNION_TYPE
)
22985 error_at (&richloc
,
22986 "expected %<;%> after union definition");
22988 gcc_unreachable ();
22990 /* Unget one token and smash it to look as though we encountered
22991 a semicolon in the input stream. */
22992 cp_lexer_set_token_position (parser
->lexer
, prev
);
22993 token
= cp_lexer_peek_token (parser
->lexer
);
22994 token
->type
= CPP_SEMICOLON
;
22995 token
->keyword
= RID_MAX
;
22999 /* If this class is not itself within the scope of another class,
23000 then we need to parse the bodies of all of the queued function
23001 definitions. Note that the queued functions defined in a class
23002 are not always processed immediately following the
23003 class-specifier for that class. Consider:
23006 struct B { void f() { sizeof (A); } };
23009 If `f' were processed before the processing of `A' were
23010 completed, there would be no way to compute the size of `A'.
23011 Note that the nesting we are interested in here is lexical --
23012 not the semantic nesting given by TYPE_CONTEXT. In particular,
23015 struct A { struct B; };
23016 struct A::B { void f() { } };
23018 there is no need to delay the parsing of `A::B::f'. */
23019 if (--parser
->num_classes_being_defined
== 0)
23022 tree class_type
= NULL_TREE
;
23023 tree pushed_scope
= NULL_TREE
;
23025 cp_default_arg_entry
*e
;
23026 tree save_ccp
, save_ccr
;
23028 if (any_erroneous_template_args_p (type
))
23030 /* Skip default arguments, NSDMIs, etc, in order to improve
23031 error recovery (c++/71169, c++/71832). */
23032 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23033 vec_safe_truncate (unparsed_nsdmis
, 0);
23034 vec_safe_truncate (unparsed_classes
, 0);
23035 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23038 /* In a first pass, parse default arguments to the functions.
23039 Then, in a second pass, parse the bodies of the functions.
23040 This two-phased approach handles cases like:
23048 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23051 /* If there are default arguments that have not yet been processed,
23052 take care of them now. */
23053 if (class_type
!= e
->class_type
)
23056 pop_scope (pushed_scope
);
23057 class_type
= e
->class_type
;
23058 pushed_scope
= push_scope (class_type
);
23060 /* Make sure that any template parameters are in scope. */
23061 maybe_begin_member_template_processing (decl
);
23062 /* Parse the default argument expressions. */
23063 cp_parser_late_parsing_default_args (parser
, decl
);
23064 /* Remove any template parameters from the symbol table. */
23065 maybe_end_member_template_processing ();
23067 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23068 /* Now parse any NSDMIs. */
23069 save_ccp
= current_class_ptr
;
23070 save_ccr
= current_class_ref
;
23071 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23073 if (class_type
!= DECL_CONTEXT (decl
))
23076 pop_scope (pushed_scope
);
23077 class_type
= DECL_CONTEXT (decl
);
23078 pushed_scope
= push_scope (class_type
);
23080 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23081 cp_parser_late_parsing_nsdmi (parser
, decl
);
23083 vec_safe_truncate (unparsed_nsdmis
, 0);
23084 current_class_ptr
= save_ccp
;
23085 current_class_ref
= save_ccr
;
23087 pop_scope (pushed_scope
);
23089 /* Now do some post-NSDMI bookkeeping. */
23090 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23091 after_nsdmi_defaulted_late_checks (class_type
);
23092 vec_safe_truncate (unparsed_classes
, 0);
23093 after_nsdmi_defaulted_late_checks (type
);
23095 /* Now parse the body of the functions. */
23098 /* OpenMP UDRs need to be parsed before all other functions. */
23099 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23100 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23101 cp_parser_late_parsing_for_member (parser
, decl
);
23102 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23103 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23104 cp_parser_late_parsing_for_member (parser
, decl
);
23107 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23108 cp_parser_late_parsing_for_member (parser
, decl
);
23109 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23112 vec_safe_push (unparsed_classes
, type
);
23114 /* Put back any saved access checks. */
23115 pop_deferring_access_checks ();
23117 /* Restore saved state. */
23118 parser
->in_switch_statement_p
= in_switch_statement_p
;
23119 parser
->in_statement
= in_statement
;
23120 parser
->in_function_body
= saved_in_function_body
;
23121 parser
->num_template_parameter_lists
23122 = saved_num_template_parameter_lists
;
23123 parser
->in_unbraced_linkage_specification_p
23124 = saved_in_unbraced_linkage_specification_p
;
23130 cp_parser_class_specifier (cp_parser
* parser
)
23133 timevar_push (TV_PARSE_STRUCT
);
23134 ret
= cp_parser_class_specifier_1 (parser
);
23135 timevar_pop (TV_PARSE_STRUCT
);
23139 /* Parse a class-head.
23142 class-key identifier [opt] base-clause [opt]
23143 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23144 class-key nested-name-specifier [opt] template-id
23147 class-virt-specifier:
23151 class-key attributes identifier [opt] base-clause [opt]
23152 class-key attributes nested-name-specifier identifier base-clause [opt]
23153 class-key attributes nested-name-specifier [opt] template-id
23156 Upon return BASES is initialized to the list of base classes (or
23157 NULL, if there are none) in the same form returned by
23158 cp_parser_base_clause.
23160 Returns the TYPE of the indicated class. Sets
23161 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23162 involving a nested-name-specifier was used, and FALSE otherwise.
23164 Returns error_mark_node if this is not a class-head.
23166 Returns NULL_TREE if the class-head is syntactically valid, but
23167 semantically invalid in a way that means we should skip the entire
23168 body of the class. */
23171 cp_parser_class_head (cp_parser
* parser
,
23172 bool* nested_name_specifier_p
)
23174 tree nested_name_specifier
;
23175 enum tag_types class_key
;
23176 tree id
= NULL_TREE
;
23177 tree type
= NULL_TREE
;
23180 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23181 bool template_id_p
= false;
23182 bool qualified_p
= false;
23183 bool invalid_nested_name_p
= false;
23184 bool invalid_explicit_specialization_p
= false;
23185 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23186 tree pushed_scope
= NULL_TREE
;
23187 unsigned num_templates
;
23188 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23189 /* Assume no nested-name-specifier will be present. */
23190 *nested_name_specifier_p
= false;
23191 /* Assume no template parameter lists will be used in defining the
23194 parser
->colon_corrects_to_scope_p
= false;
23196 /* Look for the class-key. */
23197 class_key
= cp_parser_class_key (parser
);
23198 if (class_key
== none_type
)
23199 return error_mark_node
;
23201 location_t class_head_start_location
= input_location
;
23203 /* Parse the attributes. */
23204 attributes
= cp_parser_attributes_opt (parser
);
23206 /* If the next token is `::', that is invalid -- but sometimes
23207 people do try to write:
23211 Handle this gracefully by accepting the extra qualifier, and then
23212 issuing an error about it later if this really is a
23213 class-head. If it turns out just to be an elaborated type
23214 specifier, remain silent. */
23215 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23216 qualified_p
= true;
23218 push_deferring_access_checks (dk_no_check
);
23220 /* Determine the name of the class. Begin by looking for an
23221 optional nested-name-specifier. */
23222 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23223 nested_name_specifier
23224 = cp_parser_nested_name_specifier_opt (parser
,
23225 /*typename_keyword_p=*/false,
23226 /*check_dependency_p=*/false,
23228 /*is_declaration=*/false);
23229 /* If there was a nested-name-specifier, then there *must* be an
23232 cp_token
*bad_template_keyword
= NULL
;
23234 if (nested_name_specifier
)
23236 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23237 /* Although the grammar says `identifier', it really means
23238 `class-name' or `template-name'. You are only allowed to
23239 define a class that has already been declared with this
23242 The proposed resolution for Core Issue 180 says that wherever
23243 you see `class T::X' you should treat `X' as a type-name.
23245 It is OK to define an inaccessible class; for example:
23247 class A { class B; };
23250 We do not know if we will see a class-name, or a
23251 template-name. We look for a class-name first, in case the
23252 class-name is a template-id; if we looked for the
23253 template-name first we would stop after the template-name. */
23254 cp_parser_parse_tentatively (parser
);
23255 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23256 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23257 type
= cp_parser_class_name (parser
,
23258 /*typename_keyword_p=*/false,
23259 /*template_keyword_p=*/false,
23261 /*check_dependency_p=*/false,
23262 /*class_head_p=*/true,
23263 /*is_declaration=*/false);
23264 /* If that didn't work, ignore the nested-name-specifier. */
23265 if (!cp_parser_parse_definitely (parser
))
23267 invalid_nested_name_p
= true;
23268 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23269 id
= cp_parser_identifier (parser
);
23270 if (id
== error_mark_node
)
23273 /* If we could not find a corresponding TYPE, treat this
23274 declaration like an unqualified declaration. */
23275 if (type
== error_mark_node
)
23276 nested_name_specifier
= NULL_TREE
;
23277 /* Otherwise, count the number of templates used in TYPE and its
23278 containing scopes. */
23280 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23282 /* Otherwise, the identifier is optional. */
23285 /* We don't know whether what comes next is a template-id,
23286 an identifier, or nothing at all. */
23287 cp_parser_parse_tentatively (parser
);
23288 /* Check for a template-id. */
23289 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23290 id
= cp_parser_template_id (parser
,
23291 /*template_keyword_p=*/false,
23292 /*check_dependency_p=*/true,
23294 /*is_declaration=*/true);
23295 /* If that didn't work, it could still be an identifier. */
23296 if (!cp_parser_parse_definitely (parser
))
23298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23300 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23301 id
= cp_parser_identifier (parser
);
23308 template_id_p
= true;
23313 pop_deferring_access_checks ();
23317 cp_parser_check_for_invalid_template_id (parser
, id
,
23319 type_start_token
->location
);
23321 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23323 /* If it's not a `:' or a `{' then we can't really be looking at a
23324 class-head, since a class-head only appears as part of a
23325 class-specifier. We have to detect this situation before calling
23326 xref_tag, since that has irreversible side-effects. */
23327 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23329 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23330 type
= error_mark_node
;
23334 /* At this point, we're going ahead with the class-specifier, even
23335 if some other problem occurs. */
23336 cp_parser_commit_to_tentative_parse (parser
);
23337 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23339 cp_parser_error (parser
,
23340 "cannot specify %<override%> for a class");
23341 type
= error_mark_node
;
23344 /* Issue the error about the overly-qualified name now. */
23347 cp_parser_error (parser
,
23348 "global qualification of class name is invalid");
23349 type
= error_mark_node
;
23352 else if (invalid_nested_name_p
)
23354 cp_parser_error (parser
,
23355 "qualified name does not name a class");
23356 type
= error_mark_node
;
23359 else if (nested_name_specifier
)
23363 if (bad_template_keyword
)
23364 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23365 keyword template shall not appear at the top level. */
23366 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23367 "keyword %<template%> not allowed in class-head-name");
23369 /* Reject typedef-names in class heads. */
23370 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23372 error_at (type_start_token
->location
,
23373 "invalid class name in declaration of %qD",
23379 /* Figure out in what scope the declaration is being placed. */
23380 scope
= current_scope ();
23381 /* If that scope does not contain the scope in which the
23382 class was originally declared, the program is invalid. */
23383 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23385 if (at_namespace_scope_p ())
23386 error_at (type_start_token
->location
,
23387 "declaration of %qD in namespace %qD which does not "
23389 type
, scope
, nested_name_specifier
);
23391 error_at (type_start_token
->location
,
23392 "declaration of %qD in %qD which does not enclose %qD",
23393 type
, scope
, nested_name_specifier
);
23399 A declarator-id shall not be qualified except for the
23400 definition of a ... nested class outside of its class
23401 ... [or] the definition or explicit instantiation of a
23402 class member of a namespace outside of its namespace. */
23403 if (scope
== nested_name_specifier
)
23405 permerror (nested_name_specifier_token_start
->location
,
23406 "extra qualification not allowed");
23407 nested_name_specifier
= NULL_TREE
;
23411 /* An explicit-specialization must be preceded by "template <>". If
23412 it is not, try to recover gracefully. */
23413 if (at_namespace_scope_p ()
23414 && parser
->num_template_parameter_lists
== 0
23415 && !processing_template_parmlist
23418 /* Build a location of this form:
23419 struct typename <ARGS>
23420 ^~~~~~~~~~~~~~~~~~~~~~
23421 with caret==start at the start token, and
23422 finishing at the end of the type. */
23423 location_t reported_loc
23424 = make_location (class_head_start_location
,
23425 class_head_start_location
,
23426 get_finish (type_start_token
->location
));
23427 rich_location
richloc (line_table
, reported_loc
);
23428 richloc
.add_fixit_insert_before (class_head_start_location
,
23430 error_at (&richloc
,
23431 "an explicit specialization must be preceded by"
23432 " %<template <>%>");
23433 invalid_explicit_specialization_p
= true;
23434 /* Take the same action that would have been taken by
23435 cp_parser_explicit_specialization. */
23436 ++parser
->num_template_parameter_lists
;
23437 begin_specialization ();
23439 /* There must be no "return" statements between this point and the
23440 end of this function; set "type "to the correct return value and
23441 use "goto done;" to return. */
23442 /* Make sure that the right number of template parameters were
23444 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23446 type_start_token
->location
,
23447 /*declarator=*/NULL
))
23449 /* If something went wrong, there is no point in even trying to
23450 process the class-definition. */
23455 /* Look up the type. */
23458 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23459 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23460 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23462 error_at (type_start_token
->location
,
23463 "function template %qD redeclared as a class template", id
);
23464 type
= error_mark_node
;
23468 type
= TREE_TYPE (id
);
23469 type
= maybe_process_partial_specialization (type
);
23471 /* Check the scope while we still know whether or not we had a
23472 nested-name-specifier. */
23473 if (type
!= error_mark_node
)
23474 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23476 if (nested_name_specifier
)
23477 pushed_scope
= push_scope (nested_name_specifier
);
23479 else if (nested_name_specifier
)
23485 template <typename T> struct S { struct T };
23486 template <typename T> struct S<T>::T { };
23488 we will get a TYPENAME_TYPE when processing the definition of
23489 `S::T'. We need to resolve it to the actual type before we
23490 try to define it. */
23491 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23493 class_type
= resolve_typename_type (TREE_TYPE (type
),
23494 /*only_current_p=*/false);
23495 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23496 type
= TYPE_NAME (class_type
);
23499 cp_parser_error (parser
, "could not resolve typename type");
23500 type
= error_mark_node
;
23504 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23505 == error_mark_node
)
23511 class_type
= current_class_type
;
23512 /* Enter the scope indicated by the nested-name-specifier. */
23513 pushed_scope
= push_scope (nested_name_specifier
);
23514 /* Get the canonical version of this type. */
23515 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23516 /* Call push_template_decl if it seems like we should be defining a
23517 template either from the template headers or the type we're
23518 defining, so that we diagnose both extra and missing headers. */
23519 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23520 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23521 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23523 type
= push_template_decl (type
);
23524 if (type
== error_mark_node
)
23531 type
= TREE_TYPE (type
);
23532 *nested_name_specifier_p
= true;
23534 else /* The name is not a nested name. */
23536 /* If the class was unnamed, create a dummy name. */
23538 id
= make_anon_name ();
23539 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23540 ? ts_within_enclosing_non_class
23542 type
= xref_tag (class_key
, id
, tag_scope
,
23543 parser
->num_template_parameter_lists
);
23546 /* Indicate whether this class was declared as a `class' or as a
23548 if (TREE_CODE (type
) == RECORD_TYPE
)
23549 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23550 cp_parser_check_class_key (class_key
, type
);
23552 /* If this type was already complete, and we see another definition,
23553 that's an error. */
23554 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23556 error_at (type_start_token
->location
, "redefinition of %q#T",
23558 inform (location_of (type
), "previous definition of %q#T",
23563 else if (type
== error_mark_node
)
23568 /* Apply attributes now, before any use of the class as a template
23569 argument in its base list. */
23570 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23571 fixup_attribute_variants (type
);
23574 /* We will have entered the scope containing the class; the names of
23575 base classes should be looked up in that context. For example:
23577 struct A { struct B {}; struct C; };
23578 struct A::C : B {};
23582 /* Get the list of base-classes, if there is one. */
23583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23585 /* PR59482: enter the class scope so that base-specifiers are looked
23589 bases
= cp_parser_base_clause (parser
);
23590 /* PR59482: get out of the previously pushed class scope so that the
23591 subsequent pops pop the right thing. */
23598 /* If we're really defining a class, process the base classes.
23599 If they're invalid, fail. */
23600 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23601 xref_basetypes (type
, bases
);
23604 /* Leave the scope given by the nested-name-specifier. We will
23605 enter the class scope itself while processing the members. */
23607 pop_scope (pushed_scope
);
23609 if (invalid_explicit_specialization_p
)
23611 end_specialization ();
23612 --parser
->num_template_parameter_lists
;
23616 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23617 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23618 CLASSTYPE_FINAL (type
) = 1;
23620 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23624 /* Parse a class-key.
23631 Returns the kind of class-key specified, or none_type to indicate
23634 static enum tag_types
23635 cp_parser_class_key (cp_parser
* parser
)
23638 enum tag_types tag_type
;
23640 /* Look for the class-key. */
23641 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23645 /* Check to see if the TOKEN is a class-key. */
23646 tag_type
= cp_parser_token_is_class_key (token
);
23648 cp_parser_error (parser
, "expected class-key");
23652 /* Parse a type-parameter-key.
23654 type-parameter-key:
23660 cp_parser_type_parameter_key (cp_parser
* parser
)
23662 /* Look for the type-parameter-key. */
23663 enum tag_types tag_type
= none_type
;
23664 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23665 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23667 cp_lexer_consume_token (parser
->lexer
);
23668 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23669 /* typename is not allowed in a template template parameter
23670 by the standard until C++17. */
23671 pedwarn (token
->location
, OPT_Wpedantic
,
23672 "ISO C++ forbids typename key in template template parameter;"
23673 " use -std=c++17 or -std=gnu++17");
23676 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23681 /* Parse an (optional) member-specification.
23683 member-specification:
23684 member-declaration member-specification [opt]
23685 access-specifier : member-specification [opt] */
23688 cp_parser_member_specification_opt (cp_parser
* parser
)
23695 /* Peek at the next token. */
23696 token
= cp_lexer_peek_token (parser
->lexer
);
23697 /* If it's a `}', or EOF then we've seen all the members. */
23698 if (token
->type
== CPP_CLOSE_BRACE
23699 || token
->type
== CPP_EOF
23700 || token
->type
== CPP_PRAGMA_EOL
)
23703 /* See if this token is a keyword. */
23704 keyword
= token
->keyword
;
23708 case RID_PROTECTED
:
23710 /* Consume the access-specifier. */
23711 cp_lexer_consume_token (parser
->lexer
);
23712 /* Remember which access-specifier is active. */
23713 current_access_specifier
= token
->u
.value
;
23714 /* Look for the `:'. */
23715 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23719 /* Accept #pragmas at class scope. */
23720 if (token
->type
== CPP_PRAGMA
)
23722 cp_parser_pragma (parser
, pragma_member
, NULL
);
23726 /* Otherwise, the next construction must be a
23727 member-declaration. */
23728 cp_parser_member_declaration (parser
);
23733 /* Parse a member-declaration.
23735 member-declaration:
23736 decl-specifier-seq [opt] member-declarator-list [opt] ;
23737 function-definition ; [opt]
23738 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23740 template-declaration
23743 member-declarator-list:
23745 member-declarator-list , member-declarator
23748 declarator pure-specifier [opt]
23749 declarator constant-initializer [opt]
23750 identifier [opt] : constant-expression
23754 member-declaration:
23755 __extension__ member-declaration
23758 declarator attributes [opt] pure-specifier [opt]
23759 declarator attributes [opt] constant-initializer [opt]
23760 identifier [opt] attributes [opt] : constant-expression
23764 member-declaration:
23765 static_assert-declaration */
23768 cp_parser_member_declaration (cp_parser
* parser
)
23770 cp_decl_specifier_seq decl_specifiers
;
23771 tree prefix_attributes
;
23773 int declares_class_or_enum
;
23775 cp_token
*token
= NULL
;
23776 cp_token
*decl_spec_token_start
= NULL
;
23777 cp_token
*initializer_token_start
= NULL
;
23778 int saved_pedantic
;
23779 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23781 /* Check for the `__extension__' keyword. */
23782 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23785 cp_parser_member_declaration (parser
);
23786 /* Restore the old value of the PEDANTIC flag. */
23787 pedantic
= saved_pedantic
;
23792 /* Check for a template-declaration. */
23793 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23795 /* An explicit specialization here is an error condition, and we
23796 expect the specialization handler to detect and report this. */
23797 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23798 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23799 cp_parser_explicit_specialization (parser
);
23801 cp_parser_template_declaration (parser
, /*member_p=*/true);
23805 /* Check for a template introduction. */
23806 else if (cp_parser_template_declaration_after_export (parser
, true))
23809 /* Check for a using-declaration. */
23810 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23812 if (cxx_dialect
< cxx11
)
23814 /* Parse the using-declaration. */
23815 cp_parser_using_declaration (parser
,
23816 /*access_declaration_p=*/false);
23822 bool alias_decl_expected
;
23823 cp_parser_parse_tentatively (parser
);
23824 decl
= cp_parser_alias_declaration (parser
);
23825 /* Note that if we actually see the '=' token after the
23826 identifier, cp_parser_alias_declaration commits the
23827 tentative parse. In that case, we really expect an
23828 alias-declaration. Otherwise, we expect a using
23830 alias_decl_expected
=
23831 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23832 cp_parser_parse_definitely (parser
);
23834 if (alias_decl_expected
)
23835 finish_member_declaration (decl
);
23837 cp_parser_using_declaration (parser
,
23838 /*access_declaration_p=*/false);
23843 /* Check for @defs. */
23844 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23847 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23848 ivar
= ivar_chains
;
23852 ivar
= TREE_CHAIN (member
);
23853 TREE_CHAIN (member
) = NULL_TREE
;
23854 finish_member_declaration (member
);
23859 /* If the next token is `static_assert' we have a static assertion. */
23860 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23862 cp_parser_static_assert (parser
, /*member_p=*/true);
23866 parser
->colon_corrects_to_scope_p
= false;
23868 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23871 /* Parse the decl-specifier-seq. */
23872 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23873 cp_parser_decl_specifier_seq (parser
,
23874 CP_PARSER_FLAGS_OPTIONAL
,
23876 &declares_class_or_enum
);
23877 /* Check for an invalid type-name. */
23878 if (!decl_specifiers
.any_type_specifiers_p
23879 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23881 /* If there is no declarator, then the decl-specifier-seq should
23883 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23885 /* If there was no decl-specifier-seq, and the next token is a
23886 `;', then we have something like:
23892 Each member-declaration shall declare at least one member
23893 name of the class. */
23894 if (!decl_specifiers
.any_specifiers_p
)
23896 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23897 if (!in_system_header_at (token
->location
))
23899 gcc_rich_location
richloc (token
->location
);
23900 richloc
.add_fixit_remove ();
23901 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23908 /* See if this declaration is a friend. */
23909 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23910 /* If there were decl-specifiers, check to see if there was
23911 a class-declaration. */
23912 type
= check_tag_decl (&decl_specifiers
,
23913 /*explicit_type_instantiation_p=*/false);
23914 /* Nested classes have already been added to the class, but
23915 a `friend' needs to be explicitly registered. */
23918 /* If the `friend' keyword was present, the friend must
23919 be introduced with a class-key. */
23920 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23921 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23922 "in C++03 a class-key must be used "
23923 "when declaring a friend");
23926 template <typename T> struct A {
23927 friend struct A<T>::B;
23930 A<T>::B will be represented by a TYPENAME_TYPE, and
23931 therefore not recognized by check_tag_decl. */
23934 type
= decl_specifiers
.type
;
23935 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23936 type
= TREE_TYPE (type
);
23938 if (!type
|| !TYPE_P (type
))
23939 error_at (decl_spec_token_start
->location
,
23940 "friend declaration does not name a class or "
23943 make_friend_class (current_class_type
, type
,
23944 /*complain=*/true);
23946 /* If there is no TYPE, an error message will already have
23948 else if (!type
|| type
== error_mark_node
)
23950 /* An anonymous aggregate has to be handled specially; such
23951 a declaration really declares a data member (with a
23952 particular type), as opposed to a nested class. */
23953 else if (ANON_AGGR_TYPE_P (type
))
23956 if (decl_specifiers
.storage_class
!= sc_none
)
23957 error_at (decl_spec_token_start
->location
,
23958 "a storage class on an anonymous aggregate "
23959 "in class scope is not allowed");
23961 /* Remove constructors and such from TYPE, now that we
23962 know it is an anonymous aggregate. */
23963 fixup_anonymous_aggr (type
);
23964 /* And make the corresponding data member. */
23965 decl
= build_decl (decl_spec_token_start
->location
,
23966 FIELD_DECL
, NULL_TREE
, type
);
23967 /* Add it to the class. */
23968 finish_member_declaration (decl
);
23971 cp_parser_check_access_in_redeclaration
23973 decl_spec_token_start
->location
);
23978 bool assume_semicolon
= false;
23980 /* Clear attributes from the decl_specifiers but keep them
23981 around as prefix attributes that apply them to the entity
23983 prefix_attributes
= decl_specifiers
.attributes
;
23984 decl_specifiers
.attributes
= NULL_TREE
;
23986 /* See if these declarations will be friends. */
23987 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23989 /* Keep going until we hit the `;' at the end of the
23991 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23993 tree attributes
= NULL_TREE
;
23994 tree first_attribute
;
23996 bool named_bitfld
= false;
23998 /* Peek at the next token. */
23999 token
= cp_lexer_peek_token (parser
->lexer
);
24001 /* The following code wants to know early if it is a bit-field
24002 or some other declaration. Attributes can appear before
24003 the `:' token. Skip over them without consuming any tokens
24004 to peek if they are followed by `:'. */
24005 if (cp_next_tokens_can_be_attribute_p (parser
)
24006 || (token
->type
== CPP_NAME
24007 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24008 && (named_bitfld
= true)))
24011 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24012 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24015 /* Check for a bitfield declaration. */
24016 if (token
->type
== CPP_COLON
24017 || (token
->type
== CPP_NAME
24018 && token
== cp_lexer_peek_token (parser
->lexer
)
24019 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24020 && (named_bitfld
= true)))
24024 tree late_attributes
= NULL_TREE
;
24025 location_t id_location
24026 = cp_lexer_peek_token (parser
->lexer
)->location
;
24029 identifier
= cp_parser_identifier (parser
);
24031 identifier
= NULL_TREE
;
24033 /* Look for attributes that apply to the bitfield. */
24034 attributes
= cp_parser_attributes_opt (parser
);
24036 /* Consume the `:' token. */
24037 cp_lexer_consume_token (parser
->lexer
);
24039 /* Get the width of the bitfield. */
24040 width
= cp_parser_constant_expression (parser
, false, NULL
,
24041 cxx_dialect
>= cxx11
);
24043 /* In C++2A and as extension for C++11 and above we allow
24044 default member initializers for bit-fields. */
24045 initializer
= NULL_TREE
;
24046 if (cxx_dialect
>= cxx11
24047 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24048 || cp_lexer_next_token_is (parser
->lexer
,
24052 = cp_lexer_peek_token (parser
->lexer
)->location
;
24053 if (cxx_dialect
< cxx2a
24054 && !in_system_header_at (loc
)
24055 && identifier
!= NULL_TREE
)
24057 "default member initializers for bit-fields "
24058 "only available with -std=c++2a or "
24061 initializer
= cp_parser_save_nsdmi (parser
);
24062 if (identifier
== NULL_TREE
)
24064 error_at (loc
, "default member initializer for "
24065 "unnamed bit-field");
24066 initializer
= NULL_TREE
;
24071 /* Look for attributes that apply to the bitfield after
24072 the `:' token and width. This is where GCC used to
24073 parse attributes in the past, pedwarn if there is
24074 a std attribute. */
24075 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24076 pedwarn (input_location
, OPT_Wpedantic
,
24077 "ISO C++ allows bit-field attributes only "
24078 "before the %<:%> token");
24080 late_attributes
= cp_parser_attributes_opt (parser
);
24083 attributes
= attr_chainon (attributes
, late_attributes
);
24085 /* Remember which attributes are prefix attributes and
24087 first_attribute
= attributes
;
24088 /* Combine the attributes. */
24089 attributes
= attr_chainon (prefix_attributes
, attributes
);
24091 /* Create the bitfield declaration. */
24092 decl
= grokbitfield (identifier
24093 ? make_id_declarator (NULL_TREE
,
24099 width
, initializer
,
24104 cp_declarator
*declarator
;
24105 tree asm_specification
;
24106 int ctor_dtor_or_conv_p
;
24108 /* Parse the declarator. */
24110 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24111 &ctor_dtor_or_conv_p
,
24112 /*parenthesized_p=*/NULL
,
24116 /* If something went wrong parsing the declarator, make sure
24117 that we at least consume some tokens. */
24118 if (declarator
== cp_error_declarator
)
24120 /* Skip to the end of the statement. */
24121 cp_parser_skip_to_end_of_statement (parser
);
24122 /* If the next token is not a semicolon, that is
24123 probably because we just skipped over the body of
24124 a function. So, we consume a semicolon if
24125 present, but do not issue an error message if it
24127 if (cp_lexer_next_token_is (parser
->lexer
,
24129 cp_lexer_consume_token (parser
->lexer
);
24133 if (declares_class_or_enum
& 2)
24134 cp_parser_check_for_definition_in_return_type
24135 (declarator
, decl_specifiers
.type
,
24136 decl_specifiers
.locations
[ds_type_spec
]);
24138 /* Look for an asm-specification. */
24139 asm_specification
= cp_parser_asm_specification_opt (parser
);
24140 /* Look for attributes that apply to the declaration. */
24141 attributes
= cp_parser_attributes_opt (parser
);
24142 /* Remember which attributes are prefix attributes and
24144 first_attribute
= attributes
;
24145 /* Combine the attributes. */
24146 attributes
= attr_chainon (prefix_attributes
, attributes
);
24148 /* If it's an `=', then we have a constant-initializer or a
24149 pure-specifier. It is not correct to parse the
24150 initializer before registering the member declaration
24151 since the member declaration should be in scope while
24152 its initializer is processed. However, the rest of the
24153 front end does not yet provide an interface that allows
24154 us to handle this correctly. */
24155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24159 A pure-specifier shall be used only in the declaration of
24160 a virtual function.
24162 A member-declarator can contain a constant-initializer
24163 only if it declares a static member of integral or
24166 Therefore, if the DECLARATOR is for a function, we look
24167 for a pure-specifier; otherwise, we look for a
24168 constant-initializer. When we call `grokfield', it will
24169 perform more stringent semantics checks. */
24170 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24171 if (function_declarator_p (declarator
)
24172 || (decl_specifiers
.type
24173 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24174 && declarator
->kind
== cdk_id
24175 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24176 == FUNCTION_TYPE
)))
24177 initializer
= cp_parser_pure_specifier (parser
);
24178 else if (decl_specifiers
.storage_class
!= sc_static
)
24179 initializer
= cp_parser_save_nsdmi (parser
);
24180 else if (cxx_dialect
>= cxx11
)
24183 /* Don't require a constant rvalue in C++11, since we
24184 might want a reference constant. We'll enforce
24185 constancy later. */
24186 cp_lexer_consume_token (parser
->lexer
);
24187 /* Parse the initializer. */
24188 initializer
= cp_parser_initializer_clause (parser
,
24192 /* Parse the initializer. */
24193 initializer
= cp_parser_constant_initializer (parser
);
24195 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24196 && !function_declarator_p (declarator
))
24199 if (decl_specifiers
.storage_class
!= sc_static
)
24200 initializer
= cp_parser_save_nsdmi (parser
);
24202 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24204 /* Otherwise, there is no initializer. */
24206 initializer
= NULL_TREE
;
24208 /* See if we are probably looking at a function
24209 definition. We are certainly not looking at a
24210 member-declarator. Calling `grokfield' has
24211 side-effects, so we must not do it unless we are sure
24212 that we are looking at a member-declarator. */
24213 if (cp_parser_token_starts_function_definition_p
24214 (cp_lexer_peek_token (parser
->lexer
)))
24216 /* The grammar does not allow a pure-specifier to be
24217 used when a member function is defined. (It is
24218 possible that this fact is an oversight in the
24219 standard, since a pure function may be defined
24220 outside of the class-specifier. */
24221 if (initializer
&& initializer_token_start
)
24222 error_at (initializer_token_start
->location
,
24223 "pure-specifier on function-definition");
24224 decl
= cp_parser_save_member_function_body (parser
,
24228 if (parser
->fully_implicit_function_template_p
)
24229 decl
= finish_fully_implicit_template (parser
, decl
);
24230 /* If the member was not a friend, declare it here. */
24232 finish_member_declaration (decl
);
24233 /* Peek at the next token. */
24234 token
= cp_lexer_peek_token (parser
->lexer
);
24235 /* If the next token is a semicolon, consume it. */
24236 if (token
->type
== CPP_SEMICOLON
)
24238 location_t semicolon_loc
24239 = cp_lexer_consume_token (parser
->lexer
)->location
;
24240 gcc_rich_location
richloc (semicolon_loc
);
24241 richloc
.add_fixit_remove ();
24242 warning_at (&richloc
, OPT_Wextra_semi
,
24243 "extra %<;%> after in-class "
24244 "function definition");
24249 if (declarator
->kind
== cdk_function
)
24250 declarator
->id_loc
= token
->location
;
24251 /* Create the declaration. */
24252 decl
= grokfield (declarator
, &decl_specifiers
,
24253 initializer
, /*init_const_expr_p=*/true,
24254 asm_specification
, attributes
);
24255 if (parser
->fully_implicit_function_template_p
)
24258 finish_fully_implicit_template (parser
, 0);
24260 decl
= finish_fully_implicit_template (parser
, decl
);
24264 cp_finalize_omp_declare_simd (parser
, decl
);
24265 cp_finalize_oacc_routine (parser
, decl
, false);
24267 /* Reset PREFIX_ATTRIBUTES. */
24268 if (attributes
!= error_mark_node
)
24270 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24271 attributes
= TREE_CHAIN (attributes
);
24273 TREE_CHAIN (attributes
) = NULL_TREE
;
24276 /* If there is any qualification still in effect, clear it
24277 now; we will be starting fresh with the next declarator. */
24278 parser
->scope
= NULL_TREE
;
24279 parser
->qualifying_scope
= NULL_TREE
;
24280 parser
->object_scope
= NULL_TREE
;
24281 /* If it's a `,', then there are more declarators. */
24282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24284 cp_lexer_consume_token (parser
->lexer
);
24285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24287 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24288 gcc_rich_location
richloc (token
->location
);
24289 richloc
.add_fixit_remove ();
24290 error_at (&richloc
, "stray %<,%> at end of "
24291 "member declaration");
24294 /* If the next token isn't a `;', then we have a parse error. */
24295 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24298 /* The next token might be a ways away from where the
24299 actual semicolon is missing. Find the previous token
24300 and use that for our error position. */
24301 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24302 gcc_rich_location
richloc (token
->location
);
24303 richloc
.add_fixit_insert_after (";");
24304 error_at (&richloc
, "expected %<;%> at end of "
24305 "member declaration");
24307 /* Assume that the user meant to provide a semicolon. If
24308 we were to cp_parser_skip_to_end_of_statement, we might
24309 skip to a semicolon inside a member function definition
24310 and issue nonsensical error messages. */
24311 assume_semicolon
= true;
24316 /* Add DECL to the list of members. */
24318 /* Explicitly include, eg, NSDMIs, for better error
24319 recovery (c++/58650). */
24320 || !DECL_DECLARES_FUNCTION_P (decl
))
24321 finish_member_declaration (decl
);
24323 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24324 cp_parser_save_default_args (parser
, decl
);
24325 else if (TREE_CODE (decl
) == FIELD_DECL
24326 && DECL_INITIAL (decl
))
24327 /* Add DECL to the queue of NSDMI to be parsed later. */
24328 vec_safe_push (unparsed_nsdmis
, decl
);
24331 if (assume_semicolon
)
24336 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24338 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24341 /* Parse a pure-specifier.
24346 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24347 Otherwise, ERROR_MARK_NODE is returned. */
24350 cp_parser_pure_specifier (cp_parser
* parser
)
24354 /* Look for the `=' token. */
24355 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24356 return error_mark_node
;
24357 /* Look for the `0' token. */
24358 token
= cp_lexer_peek_token (parser
->lexer
);
24360 if (token
->type
== CPP_EOF
24361 || token
->type
== CPP_PRAGMA_EOL
)
24362 return error_mark_node
;
24364 cp_lexer_consume_token (parser
->lexer
);
24366 /* Accept = default or = delete in c++0x mode. */
24367 if (token
->keyword
== RID_DEFAULT
24368 || token
->keyword
== RID_DELETE
)
24370 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24371 return token
->u
.value
;
24374 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24375 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24377 cp_parser_error (parser
,
24378 "invalid pure specifier (only %<= 0%> is allowed)");
24379 cp_parser_skip_to_end_of_statement (parser
);
24380 return error_mark_node
;
24382 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24384 error_at (token
->location
, "templates may not be %<virtual%>");
24385 return error_mark_node
;
24388 return integer_zero_node
;
24391 /* Parse a constant-initializer.
24393 constant-initializer:
24394 = constant-expression
24396 Returns a representation of the constant-expression. */
24399 cp_parser_constant_initializer (cp_parser
* parser
)
24401 /* Look for the `=' token. */
24402 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24403 return error_mark_node
;
24405 /* It is invalid to write:
24407 struct S { static const int i = { 7 }; };
24410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24412 cp_parser_error (parser
,
24413 "a brace-enclosed initializer is not allowed here");
24414 /* Consume the opening brace. */
24415 matching_braces braces
;
24416 braces
.consume_open (parser
);
24417 /* Skip the initializer. */
24418 cp_parser_skip_to_closing_brace (parser
);
24419 /* Look for the trailing `}'. */
24420 braces
.require_close (parser
);
24422 return error_mark_node
;
24425 return cp_parser_constant_expression (parser
);
24428 /* Derived classes [gram.class.derived] */
24430 /* Parse a base-clause.
24433 : base-specifier-list
24435 base-specifier-list:
24436 base-specifier ... [opt]
24437 base-specifier-list , base-specifier ... [opt]
24439 Returns a TREE_LIST representing the base-classes, in the order in
24440 which they were declared. The representation of each node is as
24441 described by cp_parser_base_specifier.
24443 In the case that no bases are specified, this function will return
24444 NULL_TREE, not ERROR_MARK_NODE. */
24447 cp_parser_base_clause (cp_parser
* parser
)
24449 tree bases
= NULL_TREE
;
24451 /* Look for the `:' that begins the list. */
24452 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24454 /* Scan the base-specifier-list. */
24459 bool pack_expansion_p
= false;
24461 /* Look for the base-specifier. */
24462 base
= cp_parser_base_specifier (parser
);
24463 /* Look for the (optional) ellipsis. */
24464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24466 /* Consume the `...'. */
24467 cp_lexer_consume_token (parser
->lexer
);
24469 pack_expansion_p
= true;
24472 /* Add BASE to the front of the list. */
24473 if (base
&& base
!= error_mark_node
)
24475 if (pack_expansion_p
)
24476 /* Make this a pack expansion type. */
24477 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24479 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24481 TREE_CHAIN (base
) = bases
;
24485 /* Peek at the next token. */
24486 token
= cp_lexer_peek_token (parser
->lexer
);
24487 /* If it's not a comma, then the list is complete. */
24488 if (token
->type
!= CPP_COMMA
)
24490 /* Consume the `,'. */
24491 cp_lexer_consume_token (parser
->lexer
);
24494 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24495 base class had a qualified name. However, the next name that
24496 appears is certainly not qualified. */
24497 parser
->scope
= NULL_TREE
;
24498 parser
->qualifying_scope
= NULL_TREE
;
24499 parser
->object_scope
= NULL_TREE
;
24501 return nreverse (bases
);
24504 /* Parse a base-specifier.
24507 :: [opt] nested-name-specifier [opt] class-name
24508 virtual access-specifier [opt] :: [opt] nested-name-specifier
24510 access-specifier virtual [opt] :: [opt] nested-name-specifier
24513 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24514 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24515 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24516 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24519 cp_parser_base_specifier (cp_parser
* parser
)
24523 bool virtual_p
= false;
24524 bool duplicate_virtual_error_issued_p
= false;
24525 bool duplicate_access_error_issued_p
= false;
24526 bool class_scope_p
, template_p
;
24527 tree access
= access_default_node
;
24530 /* Process the optional `virtual' and `access-specifier'. */
24533 /* Peek at the next token. */
24534 token
= cp_lexer_peek_token (parser
->lexer
);
24535 /* Process `virtual'. */
24536 switch (token
->keyword
)
24539 /* If `virtual' appears more than once, issue an error. */
24540 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24542 cp_parser_error (parser
,
24543 "%<virtual%> specified more than once in base-specifier");
24544 duplicate_virtual_error_issued_p
= true;
24549 /* Consume the `virtual' token. */
24550 cp_lexer_consume_token (parser
->lexer
);
24555 case RID_PROTECTED
:
24557 /* If more than one access specifier appears, issue an
24559 if (access
!= access_default_node
24560 && !duplicate_access_error_issued_p
)
24562 cp_parser_error (parser
,
24563 "more than one access specifier in base-specifier");
24564 duplicate_access_error_issued_p
= true;
24567 access
= ridpointers
[(int) token
->keyword
];
24569 /* Consume the access-specifier. */
24570 cp_lexer_consume_token (parser
->lexer
);
24579 /* It is not uncommon to see programs mechanically, erroneously, use
24580 the 'typename' keyword to denote (dependent) qualified types
24581 as base classes. */
24582 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24584 token
= cp_lexer_peek_token (parser
->lexer
);
24585 if (!processing_template_decl
)
24586 error_at (token
->location
,
24587 "keyword %<typename%> not allowed outside of templates");
24589 error_at (token
->location
,
24590 "keyword %<typename%> not allowed in this context "
24591 "(the base class is implicitly a type)");
24592 cp_lexer_consume_token (parser
->lexer
);
24595 /* Look for the optional `::' operator. */
24596 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24597 /* Look for the nested-name-specifier. The simplest way to
24602 The keyword `typename' is not permitted in a base-specifier or
24603 mem-initializer; in these contexts a qualified name that
24604 depends on a template-parameter is implicitly assumed to be a
24607 is to pretend that we have seen the `typename' keyword at this
24609 cp_parser_nested_name_specifier_opt (parser
,
24610 /*typename_keyword_p=*/true,
24611 /*check_dependency_p=*/true,
24613 /*is_declaration=*/true);
24614 /* If the base class is given by a qualified name, assume that names
24615 we see are type names or templates, as appropriate. */
24616 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24617 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24620 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24621 /* DR 950 allows decltype as a base-specifier. */
24622 type
= cp_parser_decltype (parser
);
24625 /* Otherwise, look for the class-name. */
24626 type
= cp_parser_class_name (parser
,
24630 /*check_dependency_p=*/true,
24631 /*class_head_p=*/false,
24632 /*is_declaration=*/true);
24633 type
= TREE_TYPE (type
);
24636 if (type
== error_mark_node
)
24637 return error_mark_node
;
24639 return finish_base_specifier (type
, access
, virtual_p
);
24642 /* Exception handling [gram.exception] */
24644 /* Parse an (optional) noexcept-specification.
24646 noexcept-specification:
24647 noexcept ( constant-expression ) [opt]
24649 If no noexcept-specification is present, returns NULL_TREE.
24650 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24651 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24652 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24653 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24654 in which case a boolean condition is returned instead. */
24657 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24658 bool require_constexpr
,
24659 bool* consumed_expr
,
24663 const char *saved_message
;
24665 /* Peek at the next token. */
24666 token
= cp_lexer_peek_token (parser
->lexer
);
24668 /* Is it a noexcept-specification? */
24669 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24672 cp_lexer_consume_token (parser
->lexer
);
24674 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24676 matching_parens parens
;
24677 parens
.consume_open (parser
);
24679 tree save_ccp
= current_class_ptr
;
24680 tree save_ccr
= current_class_ref
;
24682 if (current_class_type
)
24683 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
24685 if (require_constexpr
)
24687 /* Types may not be defined in an exception-specification. */
24688 saved_message
= parser
->type_definition_forbidden_message
;
24689 parser
->type_definition_forbidden_message
24690 = G_("types may not be defined in an exception-specification");
24692 expr
= cp_parser_constant_expression (parser
);
24694 /* Restore the saved message. */
24695 parser
->type_definition_forbidden_message
= saved_message
;
24699 expr
= cp_parser_expression (parser
);
24700 *consumed_expr
= true;
24703 parens
.require_close (parser
);
24705 current_class_ptr
= save_ccp
;
24706 current_class_ref
= save_ccr
;
24710 expr
= boolean_true_node
;
24711 if (!require_constexpr
)
24712 *consumed_expr
= false;
24715 /* We cannot build a noexcept-spec right away because this will check
24716 that expr is a constexpr. */
24718 return build_noexcept_spec (expr
, tf_warning_or_error
);
24726 /* Parse an (optional) exception-specification.
24728 exception-specification:
24729 throw ( type-id-list [opt] )
24731 Returns a TREE_LIST representing the exception-specification. The
24732 TREE_VALUE of each node is a type. */
24735 cp_parser_exception_specification_opt (cp_parser
* parser
)
24739 const char *saved_message
;
24741 /* Peek at the next token. */
24742 token
= cp_lexer_peek_token (parser
->lexer
);
24744 /* Is it a noexcept-specification? */
24745 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24747 if (type_id_list
!= NULL_TREE
)
24748 return type_id_list
;
24750 /* If it's not `throw', then there's no exception-specification. */
24751 if (!cp_parser_is_keyword (token
, RID_THROW
))
24754 location_t loc
= token
->location
;
24756 /* Consume the `throw'. */
24757 cp_lexer_consume_token (parser
->lexer
);
24759 /* Look for the `('. */
24760 matching_parens parens
;
24761 parens
.require_open (parser
);
24763 /* Peek at the next token. */
24764 token
= cp_lexer_peek_token (parser
->lexer
);
24765 /* If it's not a `)', then there is a type-id-list. */
24766 if (token
->type
!= CPP_CLOSE_PAREN
)
24768 /* Types may not be defined in an exception-specification. */
24769 saved_message
= parser
->type_definition_forbidden_message
;
24770 parser
->type_definition_forbidden_message
24771 = G_("types may not be defined in an exception-specification");
24772 /* Parse the type-id-list. */
24773 type_id_list
= cp_parser_type_id_list (parser
);
24774 /* Restore the saved message. */
24775 parser
->type_definition_forbidden_message
= saved_message
;
24777 if (cxx_dialect
>= cxx17
)
24779 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24781 type_id_list
= NULL_TREE
;
24783 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24784 warning_at (loc
, OPT_Wdeprecated
,
24785 "dynamic exception specifications are deprecated in "
24788 /* In C++17, throw() is equivalent to noexcept (true). throw()
24789 is deprecated in C++11 and above as well, but is still widely used,
24790 so don't warn about it yet. */
24791 else if (cxx_dialect
>= cxx17
)
24792 type_id_list
= noexcept_true_spec
;
24794 type_id_list
= empty_except_spec
;
24796 /* Look for the `)'. */
24797 parens
.require_close (parser
);
24799 return type_id_list
;
24802 /* Parse an (optional) type-id-list.
24806 type-id-list , type-id ... [opt]
24808 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24809 in the order that the types were presented. */
24812 cp_parser_type_id_list (cp_parser
* parser
)
24814 tree types
= NULL_TREE
;
24821 token
= cp_lexer_peek_token (parser
->lexer
);
24823 /* Get the next type-id. */
24824 type
= cp_parser_type_id (parser
);
24825 /* Check for invalid 'auto'. */
24826 if (flag_concepts
&& type_uses_auto (type
))
24828 error_at (token
->location
,
24829 "invalid use of %<auto%> in exception-specification");
24830 type
= error_mark_node
;
24832 /* Parse the optional ellipsis. */
24833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24835 /* Consume the `...'. */
24836 cp_lexer_consume_token (parser
->lexer
);
24838 /* Turn the type into a pack expansion expression. */
24839 type
= make_pack_expansion (type
);
24841 /* Add it to the list. */
24842 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24843 /* Peek at the next token. */
24844 token
= cp_lexer_peek_token (parser
->lexer
);
24845 /* If it is not a `,', we are done. */
24846 if (token
->type
!= CPP_COMMA
)
24848 /* Consume the `,'. */
24849 cp_lexer_consume_token (parser
->lexer
);
24852 return nreverse (types
);
24855 /* Parse a try-block.
24858 try compound-statement handler-seq */
24861 cp_parser_try_block (cp_parser
* parser
)
24865 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24866 if (parser
->in_function_body
24867 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24868 error ("%<try%> in %<constexpr%> function");
24870 try_block
= begin_try_block ();
24871 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24872 finish_try_block (try_block
);
24873 cp_parser_handler_seq (parser
);
24874 finish_handler_sequence (try_block
);
24879 /* Parse a function-try-block.
24881 function-try-block:
24882 try ctor-initializer [opt] function-body handler-seq */
24885 cp_parser_function_try_block (cp_parser
* parser
)
24887 tree compound_stmt
;
24890 /* Look for the `try' keyword. */
24891 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24893 /* Let the rest of the front end know where we are. */
24894 try_block
= begin_function_try_block (&compound_stmt
);
24895 /* Parse the function-body. */
24896 cp_parser_ctor_initializer_opt_and_function_body
24897 (parser
, /*in_function_try_block=*/true);
24898 /* We're done with the `try' part. */
24899 finish_function_try_block (try_block
);
24900 /* Parse the handlers. */
24901 cp_parser_handler_seq (parser
);
24902 /* We're done with the handlers. */
24903 finish_function_handler_sequence (try_block
, compound_stmt
);
24906 /* Parse a handler-seq.
24909 handler handler-seq [opt] */
24912 cp_parser_handler_seq (cp_parser
* parser
)
24918 /* Parse the handler. */
24919 cp_parser_handler (parser
);
24920 /* Peek at the next token. */
24921 token
= cp_lexer_peek_token (parser
->lexer
);
24922 /* If it's not `catch' then there are no more handlers. */
24923 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24928 /* Parse a handler.
24931 catch ( exception-declaration ) compound-statement */
24934 cp_parser_handler (cp_parser
* parser
)
24939 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24940 handler
= begin_handler ();
24941 matching_parens parens
;
24942 parens
.require_open (parser
);
24943 declaration
= cp_parser_exception_declaration (parser
);
24944 finish_handler_parms (declaration
, handler
);
24945 parens
.require_close (parser
);
24946 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24947 finish_handler (handler
);
24950 /* Parse an exception-declaration.
24952 exception-declaration:
24953 type-specifier-seq declarator
24954 type-specifier-seq abstract-declarator
24958 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24959 ellipsis variant is used. */
24962 cp_parser_exception_declaration (cp_parser
* parser
)
24964 cp_decl_specifier_seq type_specifiers
;
24965 cp_declarator
*declarator
;
24966 const char *saved_message
;
24968 /* If it's an ellipsis, it's easy to handle. */
24969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24971 /* Consume the `...' token. */
24972 cp_lexer_consume_token (parser
->lexer
);
24976 /* Types may not be defined in exception-declarations. */
24977 saved_message
= parser
->type_definition_forbidden_message
;
24978 parser
->type_definition_forbidden_message
24979 = G_("types may not be defined in exception-declarations");
24981 /* Parse the type-specifier-seq. */
24982 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24983 /*is_trailing_return=*/false,
24985 /* If it's a `)', then there is no declarator. */
24986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24989 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24990 /*ctor_dtor_or_conv_p=*/NULL
,
24991 /*parenthesized_p=*/NULL
,
24992 /*member_p=*/false,
24993 /*friend_p=*/false);
24995 /* Restore the saved message. */
24996 parser
->type_definition_forbidden_message
= saved_message
;
24998 if (!type_specifiers
.any_specifiers_p
)
24999 return error_mark_node
;
25001 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25004 /* Parse a throw-expression.
25007 throw assignment-expression [opt]
25009 Returns a THROW_EXPR representing the throw-expression. */
25012 cp_parser_throw_expression (cp_parser
* parser
)
25017 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25018 token
= cp_lexer_peek_token (parser
->lexer
);
25019 /* Figure out whether or not there is an assignment-expression
25020 following the "throw" keyword. */
25021 if (token
->type
== CPP_COMMA
25022 || token
->type
== CPP_SEMICOLON
25023 || token
->type
== CPP_CLOSE_PAREN
25024 || token
->type
== CPP_CLOSE_SQUARE
25025 || token
->type
== CPP_CLOSE_BRACE
25026 || token
->type
== CPP_COLON
)
25027 expression
= NULL_TREE
;
25029 expression
= cp_parser_assignment_expression (parser
);
25031 return build_throw (expression
);
25034 /* GNU Extensions */
25036 /* Parse an (optional) asm-specification.
25039 asm ( string-literal )
25041 If the asm-specification is present, returns a STRING_CST
25042 corresponding to the string-literal. Otherwise, returns
25046 cp_parser_asm_specification_opt (cp_parser
* parser
)
25049 tree asm_specification
;
25051 /* Peek at the next token. */
25052 token
= cp_lexer_peek_token (parser
->lexer
);
25053 /* If the next token isn't the `asm' keyword, then there's no
25054 asm-specification. */
25055 if (!cp_parser_is_keyword (token
, RID_ASM
))
25058 /* Consume the `asm' token. */
25059 cp_lexer_consume_token (parser
->lexer
);
25060 /* Look for the `('. */
25061 matching_parens parens
;
25062 parens
.require_open (parser
);
25064 /* Look for the string-literal. */
25065 asm_specification
= cp_parser_string_literal (parser
, false, false);
25067 /* Look for the `)'. */
25068 parens
.require_close (parser
);
25070 return asm_specification
;
25073 /* Parse an asm-operand-list.
25077 asm-operand-list , asm-operand
25080 string-literal ( expression )
25081 [ string-literal ] string-literal ( expression )
25083 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25084 each node is the expression. The TREE_PURPOSE is itself a
25085 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25086 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25087 is a STRING_CST for the string literal before the parenthesis. Returns
25088 ERROR_MARK_NODE if any of the operands are invalid. */
25091 cp_parser_asm_operand_list (cp_parser
* parser
)
25093 tree asm_operands
= NULL_TREE
;
25094 bool invalid_operands
= false;
25098 tree string_literal
;
25102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25104 /* Consume the `[' token. */
25105 cp_lexer_consume_token (parser
->lexer
);
25106 /* Read the operand name. */
25107 name
= cp_parser_identifier (parser
);
25108 if (name
!= error_mark_node
)
25109 name
= build_string (IDENTIFIER_LENGTH (name
),
25110 IDENTIFIER_POINTER (name
));
25111 /* Look for the closing `]'. */
25112 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25116 /* Look for the string-literal. */
25117 string_literal
= cp_parser_string_literal (parser
, false, false);
25119 /* Look for the `('. */
25120 matching_parens parens
;
25121 parens
.require_open (parser
);
25122 /* Parse the expression. */
25123 expression
= cp_parser_expression (parser
);
25124 /* Look for the `)'. */
25125 parens
.require_close (parser
);
25127 if (name
== error_mark_node
25128 || string_literal
== error_mark_node
25129 || expression
== error_mark_node
)
25130 invalid_operands
= true;
25132 /* Add this operand to the list. */
25133 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25136 /* If the next token is not a `,', there are no more
25138 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25140 /* Consume the `,'. */
25141 cp_lexer_consume_token (parser
->lexer
);
25144 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25147 /* Parse an asm-clobber-list.
25151 asm-clobber-list , string-literal
25153 Returns a TREE_LIST, indicating the clobbers in the order that they
25154 appeared. The TREE_VALUE of each node is a STRING_CST. */
25157 cp_parser_asm_clobber_list (cp_parser
* parser
)
25159 tree clobbers
= NULL_TREE
;
25163 tree string_literal
;
25165 /* Look for the string literal. */
25166 string_literal
= cp_parser_string_literal (parser
, false, false);
25167 /* Add it to the list. */
25168 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25169 /* If the next token is not a `,', then the list is
25171 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25173 /* Consume the `,' token. */
25174 cp_lexer_consume_token (parser
->lexer
);
25180 /* Parse an asm-label-list.
25184 asm-label-list , identifier
25186 Returns a TREE_LIST, indicating the labels in the order that they
25187 appeared. The TREE_VALUE of each node is a label. */
25190 cp_parser_asm_label_list (cp_parser
* parser
)
25192 tree labels
= NULL_TREE
;
25196 tree identifier
, label
, name
;
25198 /* Look for the identifier. */
25199 identifier
= cp_parser_identifier (parser
);
25200 if (!error_operand_p (identifier
))
25202 label
= lookup_label (identifier
);
25203 if (TREE_CODE (label
) == LABEL_DECL
)
25205 TREE_USED (label
) = 1;
25206 check_goto (label
);
25207 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25208 IDENTIFIER_POINTER (identifier
));
25209 labels
= tree_cons (name
, label
, labels
);
25212 /* If the next token is not a `,', then the list is
25214 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25216 /* Consume the `,' token. */
25217 cp_lexer_consume_token (parser
->lexer
);
25220 return nreverse (labels
);
25223 /* Return TRUE iff the next tokens in the stream are possibly the
25224 beginning of a GNU extension attribute. */
25227 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25229 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25232 /* Return TRUE iff the next tokens in the stream are possibly the
25233 beginning of a standard C++-11 attribute specifier. */
25236 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25238 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25241 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25242 beginning of a standard C++-11 attribute specifier. */
25245 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25247 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25249 return (cxx_dialect
>= cxx11
25250 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25251 || (token
->type
== CPP_OPEN_SQUARE
25252 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25253 && token
->type
== CPP_OPEN_SQUARE
)));
25256 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25257 beginning of a GNU extension attribute. */
25260 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25262 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25264 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25267 /* Return true iff the next tokens can be the beginning of either a
25268 GNU attribute list, or a standard C++11 attribute sequence. */
25271 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25273 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25274 || cp_next_tokens_can_be_std_attribute_p (parser
));
25277 /* Return true iff the next Nth tokens can be the beginning of either
25278 a GNU attribute list, or a standard C++11 attribute sequence. */
25281 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25283 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25284 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25287 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25288 of GNU attributes, or return NULL. */
25291 cp_parser_attributes_opt (cp_parser
*parser
)
25293 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25294 return cp_parser_gnu_attributes_opt (parser
);
25295 return cp_parser_std_attribute_spec_seq (parser
);
25298 /* Parse an (optional) series of attributes.
25301 attributes attribute
25304 __attribute__ (( attribute-list [opt] ))
25306 The return value is as for cp_parser_gnu_attribute_list. */
25309 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25311 tree attributes
= NULL_TREE
;
25313 temp_override
<bool> cleanup
25314 (parser
->auto_is_implicit_function_template_parm_p
, false);
25319 tree attribute_list
;
25322 /* Peek at the next token. */
25323 token
= cp_lexer_peek_token (parser
->lexer
);
25324 /* If it's not `__attribute__', then we're done. */
25325 if (token
->keyword
!= RID_ATTRIBUTE
)
25328 /* Consume the `__attribute__' keyword. */
25329 cp_lexer_consume_token (parser
->lexer
);
25330 /* Look for the two `(' tokens. */
25331 matching_parens outer_parens
;
25332 outer_parens
.require_open (parser
);
25333 matching_parens inner_parens
;
25334 inner_parens
.require_open (parser
);
25336 /* Peek at the next token. */
25337 token
= cp_lexer_peek_token (parser
->lexer
);
25338 if (token
->type
!= CPP_CLOSE_PAREN
)
25339 /* Parse the attribute-list. */
25340 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25342 /* If the next token is a `)', then there is no attribute
25344 attribute_list
= NULL
;
25346 /* Look for the two `)' tokens. */
25347 if (!inner_parens
.require_close (parser
))
25349 if (!outer_parens
.require_close (parser
))
25352 cp_parser_skip_to_end_of_statement (parser
);
25354 /* Add these new attributes to the list. */
25355 attributes
= attr_chainon (attributes
, attribute_list
);
25361 /* Parse a GNU attribute-list.
25365 attribute-list , attribute
25369 identifier ( identifier )
25370 identifier ( identifier , expression-list )
25371 identifier ( expression-list )
25373 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25374 to an attribute. The TREE_PURPOSE of each node is the identifier
25375 indicating which attribute is in use. The TREE_VALUE represents
25376 the arguments, if any. */
25379 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25381 tree attribute_list
= NULL_TREE
;
25382 bool save_translate_strings_p
= parser
->translate_strings_p
;
25384 parser
->translate_strings_p
= false;
25391 /* Look for the identifier. We also allow keywords here; for
25392 example `__attribute__ ((const))' is legal. */
25393 token
= cp_lexer_peek_token (parser
->lexer
);
25394 if (token
->type
== CPP_NAME
25395 || token
->type
== CPP_KEYWORD
)
25397 tree arguments
= NULL_TREE
;
25399 /* Consume the token, but save it since we need it for the
25400 SIMD enabled function parsing. */
25401 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25403 /* Save away the identifier that indicates which attribute
25405 identifier
= (token
->type
== CPP_KEYWORD
)
25406 /* For keywords, use the canonical spelling, not the
25407 parsed identifier. */
25408 ? ridpointers
[(int) token
->keyword
]
25409 : id_token
->u
.value
;
25411 identifier
= canonicalize_attr_name (identifier
);
25412 attribute
= build_tree_list (identifier
, NULL_TREE
);
25414 /* Peek at the next token. */
25415 token
= cp_lexer_peek_token (parser
->lexer
);
25416 /* If it's an `(', then parse the attribute arguments. */
25417 if (token
->type
== CPP_OPEN_PAREN
)
25419 vec
<tree
, va_gc
> *vec
;
25420 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25421 ? id_attr
: normal_attr
);
25422 vec
= cp_parser_parenthesized_expression_list
25423 (parser
, attr_flag
, /*cast_p=*/false,
25424 /*allow_expansion_p=*/false,
25425 /*non_constant_p=*/NULL
);
25427 arguments
= error_mark_node
;
25430 arguments
= build_tree_list_vec (vec
);
25431 release_tree_vector (vec
);
25433 /* Save the arguments away. */
25434 TREE_VALUE (attribute
) = arguments
;
25437 if (arguments
!= error_mark_node
)
25439 /* Add this attribute to the list. */
25440 TREE_CHAIN (attribute
) = attribute_list
;
25441 attribute_list
= attribute
;
25444 token
= cp_lexer_peek_token (parser
->lexer
);
25446 /* Now, look for more attributes. If the next token isn't a
25447 `,', we're done. */
25448 if (token
->type
!= CPP_COMMA
)
25451 /* Consume the comma and keep going. */
25452 cp_lexer_consume_token (parser
->lexer
);
25454 parser
->translate_strings_p
= save_translate_strings_p
;
25456 /* We built up the list in reverse order. */
25457 return nreverse (attribute_list
);
25460 /* Parse a standard C++11 attribute.
25462 The returned representation is a TREE_LIST which TREE_PURPOSE is
25463 the scoped name of the attribute, and the TREE_VALUE is its
25466 Note that the scoped name of the attribute is itself a TREE_LIST
25467 which TREE_PURPOSE is the namespace of the attribute, and
25468 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25469 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25470 and which TREE_PURPOSE is directly the attribute name.
25472 Clients of the attribute code should use get_attribute_namespace
25473 and get_attribute_name to get the actual namespace and name of
25474 attributes, regardless of their being GNU or C++11 attributes.
25477 attribute-token attribute-argument-clause [opt]
25481 attribute-scoped-token
25483 attribute-scoped-token:
25484 attribute-namespace :: identifier
25486 attribute-namespace:
25489 attribute-argument-clause:
25490 ( balanced-token-seq )
25492 balanced-token-seq:
25493 balanced-token [opt]
25494 balanced-token-seq balanced-token
25497 ( balanced-token-seq )
25498 [ balanced-token-seq ]
25499 { balanced-token-seq }. */
25502 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25504 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25507 temp_override
<bool> cleanup
25508 (parser
->auto_is_implicit_function_template_parm_p
, false);
25510 /* First, parse name of the attribute, a.k.a attribute-token. */
25512 token
= cp_lexer_peek_token (parser
->lexer
);
25513 if (token
->type
== CPP_NAME
)
25514 attr_id
= token
->u
.value
;
25515 else if (token
->type
== CPP_KEYWORD
)
25516 attr_id
= ridpointers
[(int) token
->keyword
];
25517 else if (token
->flags
& NAMED_OP
)
25518 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25520 if (attr_id
== NULL_TREE
)
25523 cp_lexer_consume_token (parser
->lexer
);
25525 token
= cp_lexer_peek_token (parser
->lexer
);
25526 if (token
->type
== CPP_SCOPE
)
25528 /* We are seeing a scoped attribute token. */
25530 cp_lexer_consume_token (parser
->lexer
);
25532 error_at (token
->location
, "attribute using prefix used together "
25533 "with scoped attribute token");
25536 token
= cp_lexer_consume_token (parser
->lexer
);
25537 if (token
->type
== CPP_NAME
)
25538 attr_id
= token
->u
.value
;
25539 else if (token
->type
== CPP_KEYWORD
)
25540 attr_id
= ridpointers
[(int) token
->keyword
];
25541 else if (token
->flags
& NAMED_OP
)
25542 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25545 error_at (token
->location
,
25546 "expected an identifier for the attribute name");
25547 return error_mark_node
;
25550 attr_ns
= canonicalize_attr_name (attr_ns
);
25551 attr_id
= canonicalize_attr_name (attr_id
);
25552 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25554 token
= cp_lexer_peek_token (parser
->lexer
);
25558 attr_ns
= canonicalize_attr_name (attr_ns
);
25559 attr_id
= canonicalize_attr_name (attr_id
);
25560 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25565 attr_id
= canonicalize_attr_name (attr_id
);
25566 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25568 /* C++11 noreturn attribute is equivalent to GNU's. */
25569 if (is_attribute_p ("noreturn", attr_id
))
25570 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25571 /* C++14 deprecated attribute is equivalent to GNU's. */
25572 else if (is_attribute_p ("deprecated", attr_id
))
25573 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25574 /* C++17 fallthrough attribute is equivalent to GNU's. */
25575 else if (is_attribute_p ("fallthrough", attr_id
))
25576 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25577 /* Transactional Memory TS optimize_for_synchronized attribute is
25578 equivalent to GNU transaction_callable. */
25579 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25580 TREE_PURPOSE (attribute
)
25581 = get_identifier ("transaction_callable");
25582 /* Transactional Memory attributes are GNU attributes. */
25583 else if (tm_attr_to_mask (attr_id
))
25584 TREE_PURPOSE (attribute
) = attr_id
;
25587 /* Now parse the optional argument clause of the attribute. */
25589 if (token
->type
!= CPP_OPEN_PAREN
)
25593 vec
<tree
, va_gc
> *vec
;
25594 int attr_flag
= normal_attr
;
25596 if (attr_ns
== gnu_identifier
25597 && attribute_takes_identifier_p (attr_id
))
25598 /* A GNU attribute that takes an identifier in parameter. */
25599 attr_flag
= id_attr
;
25601 vec
= cp_parser_parenthesized_expression_list
25602 (parser
, attr_flag
, /*cast_p=*/false,
25603 /*allow_expansion_p=*/true,
25604 /*non_constant_p=*/NULL
);
25606 arguments
= error_mark_node
;
25609 arguments
= build_tree_list_vec (vec
);
25610 release_tree_vector (vec
);
25613 if (arguments
== error_mark_node
)
25614 attribute
= error_mark_node
;
25616 TREE_VALUE (attribute
) = arguments
;
25622 /* Check that the attribute ATTRIBUTE appears at most once in the
25623 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25624 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25625 isn't implemented yet in GCC. */
25628 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25632 tree name
= get_attribute_name (attribute
);
25633 if (is_attribute_p ("noreturn", name
)
25634 && lookup_attribute ("noreturn", attributes
))
25635 error ("attribute %<noreturn%> can appear at most once "
25636 "in an attribute-list");
25637 else if (is_attribute_p ("deprecated", name
)
25638 && lookup_attribute ("deprecated", attributes
))
25639 error ("attribute %<deprecated%> can appear at most once "
25640 "in an attribute-list");
25644 /* Parse a list of standard C++-11 attributes.
25648 attribute-list , attribute[opt]
25650 attribute-list , attribute ...
25654 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25656 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25657 cp_token
*token
= NULL
;
25661 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25662 if (attribute
== error_mark_node
)
25664 if (attribute
!= NULL_TREE
)
25666 cp_parser_check_std_attribute (attributes
, attribute
);
25667 TREE_CHAIN (attribute
) = attributes
;
25668 attributes
= attribute
;
25670 token
= cp_lexer_peek_token (parser
->lexer
);
25671 if (token
->type
== CPP_ELLIPSIS
)
25673 cp_lexer_consume_token (parser
->lexer
);
25674 if (attribute
== NULL_TREE
)
25675 error_at (token
->location
,
25676 "expected attribute before %<...%>");
25679 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25680 if (pack
== error_mark_node
)
25681 return error_mark_node
;
25682 TREE_VALUE (attribute
) = pack
;
25684 token
= cp_lexer_peek_token (parser
->lexer
);
25686 if (token
->type
!= CPP_COMMA
)
25688 cp_lexer_consume_token (parser
->lexer
);
25690 attributes
= nreverse (attributes
);
25694 /* Parse a standard C++-11 attribute specifier.
25696 attribute-specifier:
25697 [ [ attribute-using-prefix [opt] attribute-list ] ]
25698 alignment-specifier
25700 attribute-using-prefix:
25701 using attribute-namespace :
25703 alignment-specifier:
25704 alignas ( type-id ... [opt] )
25705 alignas ( alignment-expression ... [opt] ). */
25708 cp_parser_std_attribute_spec (cp_parser
*parser
)
25710 tree attributes
= NULL_TREE
;
25711 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25713 if (token
->type
== CPP_OPEN_SQUARE
25714 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25716 tree attr_ns
= NULL_TREE
;
25718 cp_lexer_consume_token (parser
->lexer
);
25719 cp_lexer_consume_token (parser
->lexer
);
25721 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25723 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25724 if (token
->type
== CPP_NAME
)
25725 attr_ns
= token
->u
.value
;
25726 else if (token
->type
== CPP_KEYWORD
)
25727 attr_ns
= ridpointers
[(int) token
->keyword
];
25728 else if (token
->flags
& NAMED_OP
)
25729 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25732 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25734 if (cxx_dialect
< cxx17
25735 && !in_system_header_at (input_location
))
25736 pedwarn (input_location
, 0,
25737 "attribute using prefix only available "
25738 "with -std=c++17 or -std=gnu++17");
25740 cp_lexer_consume_token (parser
->lexer
);
25741 cp_lexer_consume_token (parser
->lexer
);
25742 cp_lexer_consume_token (parser
->lexer
);
25745 attr_ns
= NULL_TREE
;
25748 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25750 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25751 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25752 cp_parser_skip_to_end_of_statement (parser
);
25754 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25755 when we are sure that we have actually parsed them. */
25756 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25762 /* Look for an alignment-specifier. */
25764 token
= cp_lexer_peek_token (parser
->lexer
);
25766 if (token
->type
!= CPP_KEYWORD
25767 || token
->keyword
!= RID_ALIGNAS
)
25770 cp_lexer_consume_token (parser
->lexer
);
25771 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25773 matching_parens parens
;
25774 if (!parens
.require_open (parser
))
25775 return error_mark_node
;
25777 cp_parser_parse_tentatively (parser
);
25778 alignas_expr
= cp_parser_type_id (parser
);
25780 if (!cp_parser_parse_definitely (parser
))
25782 alignas_expr
= cp_parser_assignment_expression (parser
);
25783 if (alignas_expr
== error_mark_node
)
25784 cp_parser_skip_to_end_of_statement (parser
);
25785 if (alignas_expr
== NULL_TREE
25786 || alignas_expr
== error_mark_node
)
25787 return alignas_expr
;
25790 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25791 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25793 /* Handle alignas (pack...). */
25794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25796 cp_lexer_consume_token (parser
->lexer
);
25797 alignas_expr
= make_pack_expansion (alignas_expr
);
25800 /* Something went wrong, so don't build the attribute. */
25801 if (alignas_expr
== error_mark_node
)
25802 return error_mark_node
;
25804 if (!parens
.require_close (parser
))
25805 return error_mark_node
;
25807 /* Build the C++-11 representation of an 'aligned'
25810 = build_tree_list (build_tree_list (gnu_identifier
,
25811 aligned_identifier
), alignas_expr
);
25817 /* Parse a standard C++-11 attribute-specifier-seq.
25819 attribute-specifier-seq:
25820 attribute-specifier-seq [opt] attribute-specifier
25824 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25826 tree attr_specs
= NULL_TREE
;
25827 tree attr_last
= NULL_TREE
;
25831 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25832 if (attr_spec
== NULL_TREE
)
25834 if (attr_spec
== error_mark_node
)
25835 return error_mark_node
;
25838 TREE_CHAIN (attr_last
) = attr_spec
;
25840 attr_specs
= attr_last
= attr_spec
;
25841 attr_last
= tree_last (attr_last
);
25847 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25848 return index of the first token after balanced-token, or N on failure. */
25851 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25854 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25856 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25859 case CPP_PRAGMA_EOL
:
25860 /* Ran out of tokens. */
25862 case CPP_OPEN_PAREN
:
25865 case CPP_OPEN_BRACE
:
25868 case CPP_OPEN_SQUARE
:
25871 case CPP_CLOSE_PAREN
:
25874 case CPP_CLOSE_BRACE
:
25877 case CPP_CLOSE_SQUARE
:
25883 while (nparens
|| nbraces
|| nsquares
);
25887 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25888 return index of the first token after the GNU attribute tokens, or N on
25892 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25896 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25897 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25898 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25901 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25904 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25911 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25912 next token), return index of the first token after the standard C++11
25913 attribute tokens, or N on failure. */
25916 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25920 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25921 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25923 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25926 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25930 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25931 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25933 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25944 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25945 as the next token), return index of the first token after the attribute
25946 tokens, or N on failure. */
25949 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25951 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25952 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25953 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25956 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25957 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25958 current value of the PEDANTIC flag, regardless of whether or not
25959 the `__extension__' keyword is present. The caller is responsible
25960 for restoring the value of the PEDANTIC flag. */
25963 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25965 /* Save the old value of the PEDANTIC flag. */
25966 *saved_pedantic
= pedantic
;
25968 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25970 /* Consume the `__extension__' token. */
25971 cp_lexer_consume_token (parser
->lexer
);
25972 /* We're not being pedantic while the `__extension__' keyword is
25982 /* Parse a label declaration.
25985 __label__ label-declarator-seq ;
25987 label-declarator-seq:
25988 identifier , label-declarator-seq
25992 cp_parser_label_declaration (cp_parser
* parser
)
25994 /* Look for the `__label__' keyword. */
25995 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26001 /* Look for an identifier. */
26002 identifier
= cp_parser_identifier (parser
);
26003 /* If we failed, stop. */
26004 if (identifier
== error_mark_node
)
26006 /* Declare it as a label. */
26007 finish_label_decl (identifier
);
26008 /* If the next token is a `;', stop. */
26009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26011 /* Look for the `,' separating the label declarations. */
26012 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26015 /* Look for the final `;'. */
26016 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26019 // -------------------------------------------------------------------------- //
26022 // Parse a requires clause.
26024 // requires-clause:
26025 // 'requires' logical-or-expression
26027 // The required logical-or-expression must be a constant expression. Note
26028 // that we don't check that the expression is constepxr here. We defer until
26029 // we analyze constraints and then, we only check atomic constraints.
26031 cp_parser_requires_clause (cp_parser
*parser
)
26033 // Parse the requires clause so that it is not automatically folded.
26034 ++processing_template_decl
;
26035 tree expr
= cp_parser_binary_expression (parser
, false, false,
26036 PREC_NOT_OPERATOR
, NULL
);
26037 if (check_for_bare_parameter_packs (expr
))
26038 expr
= error_mark_node
;
26039 --processing_template_decl
;
26043 // Optionally parse a requires clause:
26045 cp_parser_requires_clause_opt (cp_parser
*parser
)
26047 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26048 if (tok
->keyword
!= RID_REQUIRES
)
26050 if (!flag_concepts
&& tok
->type
== CPP_NAME
26051 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26053 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26054 "%<requires%> only available with -fconcepts");
26055 /* Parse and discard the requires-clause. */
26056 cp_lexer_consume_token (parser
->lexer
);
26057 cp_parser_requires_clause (parser
);
26061 cp_lexer_consume_token (parser
->lexer
);
26062 return cp_parser_requires_clause (parser
);
26066 /*---------------------------------------------------------------------------
26067 Requires expressions
26068 ---------------------------------------------------------------------------*/
26070 /* Parse a requires expression
26072 requirement-expression:
26073 'requires' requirement-parameter-list [opt] requirement-body */
26075 cp_parser_requires_expression (cp_parser
*parser
)
26077 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26078 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26080 /* A requires-expression shall appear only within a concept
26081 definition or a requires-clause.
26083 TODO: Implement this diagnostic correctly. */
26084 if (!processing_template_decl
)
26086 error_at (loc
, "a requires expression cannot appear outside a template");
26087 cp_parser_skip_to_end_of_statement (parser
);
26088 return error_mark_node
;
26093 /* Local parameters are delared as variables within the scope
26094 of the expression. They are not visible past the end of
26095 the expression. Expressions within the requires-expression
26096 are unevaluated. */
26097 struct scope_sentinel
26101 ++cp_unevaluated_operand
;
26102 begin_scope (sk_block
, NULL_TREE
);
26107 pop_bindings_and_leave_scope ();
26108 --cp_unevaluated_operand
;
26112 /* Parse the optional parameter list. */
26113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26115 parms
= cp_parser_requirement_parameter_list (parser
);
26116 if (parms
== error_mark_node
)
26117 return error_mark_node
;
26122 /* Parse the requirement body. */
26123 reqs
= cp_parser_requirement_body (parser
);
26124 if (reqs
== error_mark_node
)
26125 return error_mark_node
;
26128 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26130 grokparms (parms
, &parms
);
26131 return finish_requires_expr (parms
, reqs
);
26134 /* Parse a parameterized requirement.
26136 requirement-parameter-list:
26137 '(' parameter-declaration-clause ')' */
26139 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26141 matching_parens parens
;
26142 if (!parens
.require_open (parser
))
26143 return error_mark_node
;
26145 tree parms
= cp_parser_parameter_declaration_clause (parser
);
26147 if (!parens
.require_close (parser
))
26148 return error_mark_node
;
26153 /* Parse the body of a requirement.
26156 '{' requirement-list '}' */
26158 cp_parser_requirement_body (cp_parser
*parser
)
26160 matching_braces braces
;
26161 if (!braces
.require_open (parser
))
26162 return error_mark_node
;
26164 tree reqs
= cp_parser_requirement_list (parser
);
26166 if (!braces
.require_close (parser
))
26167 return error_mark_node
;
26172 /* Parse a list of requirements.
26176 requirement-list ';' requirement[opt] */
26178 cp_parser_requirement_list (cp_parser
*parser
)
26180 tree result
= NULL_TREE
;
26183 tree req
= cp_parser_requirement (parser
);
26184 if (req
== error_mark_node
)
26185 return error_mark_node
;
26187 result
= tree_cons (NULL_TREE
, req
, result
);
26189 /* If we see a semi-colon, consume it. */
26190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26191 cp_lexer_consume_token (parser
->lexer
);
26193 /* Stop processing at the end of the list. */
26194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26198 /* Reverse the order of requirements so they are analyzed in
26199 declaration order. */
26200 return nreverse (result
);
26203 /* Parse a syntactic requirement or type requirement.
26207 compound-requirement
26209 nested-requirement */
26211 cp_parser_requirement (cp_parser
*parser
)
26213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26214 return cp_parser_compound_requirement (parser
);
26215 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26216 return cp_parser_type_requirement (parser
);
26217 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26218 return cp_parser_nested_requirement (parser
);
26220 return cp_parser_simple_requirement (parser
);
26223 /* Parse a simple requirement.
26225 simple-requirement:
26228 cp_parser_simple_requirement (cp_parser
*parser
)
26230 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26231 if (!expr
|| expr
== error_mark_node
)
26232 return error_mark_node
;
26234 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26235 return error_mark_node
;
26237 return finish_simple_requirement (expr
);
26240 /* Parse a type requirement
26243 nested-name-specifier [opt] required-type-name ';'
26245 required-type-name:
26247 'template' [opt] simple-template-id */
26249 cp_parser_type_requirement (cp_parser
*parser
)
26251 cp_lexer_consume_token (parser
->lexer
);
26253 // Save the scope before parsing name specifiers.
26254 tree saved_scope
= parser
->scope
;
26255 tree saved_object_scope
= parser
->object_scope
;
26256 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26257 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26258 cp_parser_nested_name_specifier_opt (parser
,
26259 /*typename_keyword_p=*/true,
26260 /*check_dependency_p=*/false,
26262 /*is_declaration=*/false);
26265 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26267 cp_lexer_consume_token (parser
->lexer
);
26268 type
= cp_parser_template_id (parser
,
26269 /*template_keyword_p=*/true,
26270 /*check_dependency=*/false,
26271 /*tag_type=*/none_type
,
26272 /*is_declaration=*/false);
26273 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26274 /*complain=*/tf_error
);
26277 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26279 if (TREE_CODE (type
) == TYPE_DECL
)
26280 type
= TREE_TYPE (type
);
26282 parser
->scope
= saved_scope
;
26283 parser
->object_scope
= saved_object_scope
;
26284 parser
->qualifying_scope
= saved_qualifying_scope
;
26286 if (type
== error_mark_node
)
26287 cp_parser_skip_to_end_of_statement (parser
);
26289 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26290 return error_mark_node
;
26291 if (type
== error_mark_node
)
26292 return error_mark_node
;
26294 return finish_type_requirement (type
);
26297 /* Parse a compound requirement
26299 compound-requirement:
26300 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26302 cp_parser_compound_requirement (cp_parser
*parser
)
26304 /* Parse an expression enclosed in '{ }'s. */
26305 matching_braces braces
;
26306 if (!braces
.require_open (parser
))
26307 return error_mark_node
;
26309 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26310 if (!expr
|| expr
== error_mark_node
)
26311 return error_mark_node
;
26313 if (!braces
.require_close (parser
))
26314 return error_mark_node
;
26316 /* Parse the optional noexcept. */
26317 bool noexcept_p
= false;
26318 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26320 cp_lexer_consume_token (parser
->lexer
);
26324 /* Parse the optional trailing return type. */
26325 tree type
= NULL_TREE
;
26326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26328 cp_lexer_consume_token (parser
->lexer
);
26329 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26330 parser
->in_result_type_constraint_p
= true;
26331 type
= cp_parser_trailing_type_id (parser
);
26332 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26333 if (type
== error_mark_node
)
26334 return error_mark_node
;
26337 return finish_compound_requirement (expr
, type
, noexcept_p
);
26340 /* Parse a nested requirement. This is the same as a requires clause.
26342 nested-requirement:
26345 cp_parser_nested_requirement (cp_parser
*parser
)
26347 cp_lexer_consume_token (parser
->lexer
);
26348 tree req
= cp_parser_requires_clause (parser
);
26349 if (req
== error_mark_node
)
26350 return error_mark_node
;
26351 return finish_nested_requirement (req
);
26354 /* Support Functions */
26356 /* Return the appropriate prefer_type argument for lookup_name_real based on
26357 tag_type and template_mem_access. */
26360 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26362 /* DR 141: When looking in the current enclosing context for a template-name
26363 after -> or ., only consider class templates. */
26364 if (template_mem_access
)
26368 case none_type
: return 0; // No preference.
26369 case scope_type
: return 1; // Type or namespace.
26370 default: return 2; // Type only.
26374 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26375 NAME should have one of the representations used for an
26376 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26377 is returned. If PARSER->SCOPE is a dependent type, then a
26378 SCOPE_REF is returned.
26380 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26381 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26382 was formed. Abstractly, such entities should not be passed to this
26383 function, because they do not need to be looked up, but it is
26384 simpler to check for this special case here, rather than at the
26387 In cases not explicitly covered above, this function returns a
26388 DECL, OVERLOAD, or baselink representing the result of the lookup.
26389 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26392 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26393 (e.g., "struct") that was used. In that case bindings that do not
26394 refer to types are ignored.
26396 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26399 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26402 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26405 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26406 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26407 NULL_TREE otherwise. */
26410 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26411 enum tag_types tag_type
,
26414 bool check_dependency
,
26415 tree
*ambiguous_decls
,
26416 location_t name_location
)
26419 tree object_type
= parser
->context
->object_type
;
26421 /* Assume that the lookup will be unambiguous. */
26422 if (ambiguous_decls
)
26423 *ambiguous_decls
= NULL_TREE
;
26425 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26426 no longer valid. Note that if we are parsing tentatively, and
26427 the parse fails, OBJECT_TYPE will be automatically restored. */
26428 parser
->context
->object_type
= NULL_TREE
;
26430 if (name
== error_mark_node
)
26431 return error_mark_node
;
26433 /* A template-id has already been resolved; there is no lookup to
26435 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26437 if (BASELINK_P (name
))
26439 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26440 == TEMPLATE_ID_EXPR
);
26444 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26445 it should already have been checked to make sure that the name
26446 used matches the type being destroyed. */
26447 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26451 /* Figure out to which type this destructor applies. */
26453 type
= parser
->scope
;
26454 else if (object_type
)
26455 type
= object_type
;
26457 type
= current_class_type
;
26458 /* If that's not a class type, there is no destructor. */
26459 if (!type
|| !CLASS_TYPE_P (type
))
26460 return error_mark_node
;
26462 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26463 lazily_declare_fn (sfk_destructor
, type
);
26465 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26468 return error_mark_node
;
26471 /* By this point, the NAME should be an ordinary identifier. If
26472 the id-expression was a qualified name, the qualifying scope is
26473 stored in PARSER->SCOPE at this point. */
26474 gcc_assert (identifier_p (name
));
26476 /* Perform the lookup. */
26481 if (parser
->scope
== error_mark_node
)
26482 return error_mark_node
;
26484 /* If the SCOPE is dependent, the lookup must be deferred until
26485 the template is instantiated -- unless we are explicitly
26486 looking up names in uninstantiated templates. Even then, we
26487 cannot look up the name if the scope is not a class type; it
26488 might, for example, be a template type parameter. */
26489 dependent_p
= (TYPE_P (parser
->scope
)
26490 && dependent_scope_p (parser
->scope
));
26491 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26493 /* Defer lookup. */
26494 decl
= error_mark_node
;
26497 tree pushed_scope
= NULL_TREE
;
26499 /* If PARSER->SCOPE is a dependent type, then it must be a
26500 class type, and we must not be checking dependencies;
26501 otherwise, we would have processed this lookup above. So
26502 that PARSER->SCOPE is not considered a dependent base by
26503 lookup_member, we must enter the scope here. */
26505 pushed_scope
= push_scope (parser
->scope
);
26507 /* If the PARSER->SCOPE is a template specialization, it
26508 may be instantiated during name lookup. In that case,
26509 errors may be issued. Even if we rollback the current
26510 tentative parse, those errors are valid. */
26511 decl
= lookup_qualified_name (parser
->scope
, name
,
26512 prefer_type_arg (tag_type
),
26513 /*complain=*/true);
26515 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26516 lookup result and the nested-name-specifier nominates a class C:
26517 * if the name specified after the nested-name-specifier, when
26518 looked up in C, is the injected-class-name of C (Clause 9), or
26519 * if the name specified after the nested-name-specifier is the
26520 same as the identifier or the simple-template-id's template-
26521 name in the last component of the nested-name-specifier,
26522 the name is instead considered to name the constructor of
26523 class C. [ Note: for example, the constructor is not an
26524 acceptable lookup result in an elaborated-type-specifier so
26525 the constructor would not be used in place of the
26526 injected-class-name. --end note ] Such a constructor name
26527 shall be used only in the declarator-id of a declaration that
26528 names a constructor or in a using-declaration. */
26529 if (tag_type
== none_type
26530 && DECL_SELF_REFERENCE_P (decl
)
26531 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26532 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26533 prefer_type_arg (tag_type
),
26534 /*complain=*/true);
26536 /* If we have a single function from a using decl, pull it out. */
26537 if (TREE_CODE (decl
) == OVERLOAD
26538 && !really_overloaded_fn (decl
))
26539 decl
= OVL_FUNCTION (decl
);
26542 pop_scope (pushed_scope
);
26545 /* If the scope is a dependent type and either we deferred lookup or
26546 we did lookup but didn't find the name, rememeber the name. */
26547 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26548 && dependent_type_p (parser
->scope
))
26554 /* The resolution to Core Issue 180 says that `struct
26555 A::B' should be considered a type-name, even if `A'
26557 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26558 /*complain=*/tf_error
);
26559 if (type
!= error_mark_node
)
26560 decl
= TYPE_NAME (type
);
26562 else if (is_template
26563 && (cp_parser_next_token_ends_template_argument_p (parser
)
26564 || cp_lexer_next_token_is (parser
->lexer
,
26566 decl
= make_unbound_class_template (parser
->scope
,
26568 /*complain=*/tf_error
);
26570 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26571 parser
->scope
, name
,
26574 parser
->qualifying_scope
= parser
->scope
;
26575 parser
->object_scope
= NULL_TREE
;
26577 else if (object_type
)
26579 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26580 OBJECT_TYPE is not a class. */
26581 if (CLASS_TYPE_P (object_type
))
26582 /* If the OBJECT_TYPE is a template specialization, it may
26583 be instantiated during name lookup. In that case, errors
26584 may be issued. Even if we rollback the current tentative
26585 parse, those errors are valid. */
26586 decl
= lookup_member (object_type
,
26589 prefer_type_arg (tag_type
),
26590 tf_warning_or_error
);
26595 /* Look it up in the enclosing context. DR 141: When looking for a
26596 template-name after -> or ., only consider class templates. */
26597 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26599 /*block_p=*/true, is_namespace
, 0);
26600 if (object_type
== unknown_type_node
)
26601 /* The object is type-dependent, so we can't look anything up; we used
26602 this to get the DR 141 behavior. */
26603 object_type
= NULL_TREE
;
26604 parser
->object_scope
= object_type
;
26605 parser
->qualifying_scope
= NULL_TREE
;
26609 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26611 /*block_p=*/true, is_namespace
, 0);
26612 parser
->qualifying_scope
= NULL_TREE
;
26613 parser
->object_scope
= NULL_TREE
;
26616 /* If the lookup failed, let our caller know. */
26617 if (!decl
|| decl
== error_mark_node
)
26618 return error_mark_node
;
26620 /* Pull out the template from an injected-class-name (or multiple). */
26622 decl
= maybe_get_template_decl_from_type_decl (decl
);
26624 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26625 if (TREE_CODE (decl
) == TREE_LIST
)
26627 if (ambiguous_decls
)
26628 *ambiguous_decls
= decl
;
26629 /* The error message we have to print is too complicated for
26630 cp_parser_error, so we incorporate its actions directly. */
26631 if (!cp_parser_simulate_error (parser
))
26633 error_at (name_location
, "reference to %qD is ambiguous",
26635 print_candidates (decl
);
26637 return error_mark_node
;
26640 gcc_assert (DECL_P (decl
)
26641 || TREE_CODE (decl
) == OVERLOAD
26642 || TREE_CODE (decl
) == SCOPE_REF
26643 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26644 || BASELINK_P (decl
));
26646 /* If we have resolved the name of a member declaration, check to
26647 see if the declaration is accessible. When the name resolves to
26648 set of overloaded functions, accessibility is checked when
26649 overload resolution is done.
26651 During an explicit instantiation, access is not checked at all,
26652 as per [temp.explicit]. */
26654 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26656 maybe_record_typedef_use (decl
);
26658 return cp_expr (decl
, name_location
);
26661 /* Like cp_parser_lookup_name, but for use in the typical case where
26662 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26663 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26666 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26668 return cp_parser_lookup_name (parser
, name
,
26670 /*is_template=*/false,
26671 /*is_namespace=*/false,
26672 /*check_dependency=*/true,
26673 /*ambiguous_decls=*/NULL
,
26677 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26678 the current context, return the TYPE_DECL. If TAG_NAME_P is
26679 true, the DECL indicates the class being defined in a class-head,
26680 or declared in an elaborated-type-specifier.
26682 Otherwise, return DECL. */
26685 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26687 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26688 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26691 template <typename T> struct B;
26694 template <typename T> struct A::B {};
26696 Similarly, in an elaborated-type-specifier:
26698 namespace N { struct X{}; }
26701 template <typename T> friend struct N::X;
26704 However, if the DECL refers to a class type, and we are in
26705 the scope of the class, then the name lookup automatically
26706 finds the TYPE_DECL created by build_self_reference rather
26707 than a TEMPLATE_DECL. For example, in:
26709 template <class T> struct S {
26713 there is no need to handle such case. */
26715 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26716 return DECL_TEMPLATE_RESULT (decl
);
26721 /* If too many, or too few, template-parameter lists apply to the
26722 declarator, issue an error message. Returns TRUE if all went well,
26723 and FALSE otherwise. */
26726 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26727 cp_declarator
*declarator
,
26728 location_t declarator_location
)
26730 switch (declarator
->kind
)
26734 unsigned num_templates
= 0;
26735 tree scope
= declarator
->u
.id
.qualifying_scope
;
26736 bool template_id_p
= false;
26739 num_templates
= num_template_headers_for_class (scope
);
26740 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26741 == TEMPLATE_ID_EXPR
)
26743 /* If the DECLARATOR has the form `X<y>' then it uses one
26744 additional level of template parameters. */
26746 template_id_p
= true;
26749 return cp_parser_check_template_parameters
26750 (parser
, num_templates
, template_id_p
, declarator_location
,
26757 case cdk_reference
:
26759 return (cp_parser_check_declarator_template_parameters
26760 (parser
, declarator
->declarator
, declarator_location
));
26767 gcc_unreachable ();
26772 /* NUM_TEMPLATES were used in the current declaration. If that is
26773 invalid, return FALSE and issue an error messages. Otherwise,
26774 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26775 declarator and we can print more accurate diagnostics. */
26778 cp_parser_check_template_parameters (cp_parser
* parser
,
26779 unsigned num_templates
,
26780 bool template_id_p
,
26781 location_t location
,
26782 cp_declarator
*declarator
)
26784 /* If there are the same number of template classes and parameter
26785 lists, that's OK. */
26786 if (parser
->num_template_parameter_lists
== num_templates
)
26788 /* If there are more, but only one more, and the name ends in an identifier,
26789 then we are declaring a primary template. That's OK too. */
26791 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26793 /* If there are more template classes than parameter lists, we have
26796 template <class T> void S<T>::R<T>::f (); */
26797 if (parser
->num_template_parameter_lists
< num_templates
)
26799 if (declarator
&& !current_function_decl
)
26800 error_at (location
, "specializing member %<%T::%E%> "
26801 "requires %<template<>%> syntax",
26802 declarator
->u
.id
.qualifying_scope
,
26803 declarator
->u
.id
.unqualified_name
);
26804 else if (declarator
)
26805 error_at (location
, "invalid declaration of %<%T::%E%>",
26806 declarator
->u
.id
.qualifying_scope
,
26807 declarator
->u
.id
.unqualified_name
);
26809 error_at (location
, "too few template-parameter-lists");
26812 /* Otherwise, there are too many template parameter lists. We have
26815 template <class T> template <class U> void S::f(); */
26816 error_at (location
, "too many template-parameter-lists");
26820 /* Parse an optional `::' token indicating that the following name is
26821 from the global namespace. If so, PARSER->SCOPE is set to the
26822 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26823 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26824 Returns the new value of PARSER->SCOPE, if the `::' token is
26825 present, and NULL_TREE otherwise. */
26828 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26832 /* Peek at the next token. */
26833 token
= cp_lexer_peek_token (parser
->lexer
);
26834 /* If we're looking at a `::' token then we're starting from the
26835 global namespace, not our current location. */
26836 if (token
->type
== CPP_SCOPE
)
26838 /* Consume the `::' token. */
26839 cp_lexer_consume_token (parser
->lexer
);
26840 /* Set the SCOPE so that we know where to start the lookup. */
26841 parser
->scope
= global_namespace
;
26842 parser
->qualifying_scope
= global_namespace
;
26843 parser
->object_scope
= NULL_TREE
;
26845 return parser
->scope
;
26847 else if (!current_scope_valid_p
)
26849 parser
->scope
= NULL_TREE
;
26850 parser
->qualifying_scope
= NULL_TREE
;
26851 parser
->object_scope
= NULL_TREE
;
26857 /* Returns TRUE if the upcoming token sequence is the start of a
26858 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26859 declarator is preceded by the `friend' specifier. */
26862 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26864 bool constructor_p
;
26865 bool outside_class_specifier_p
;
26866 tree nested_name_specifier
;
26867 cp_token
*next_token
;
26869 /* The common case is that this is not a constructor declarator, so
26870 try to avoid doing lots of work if at all possible. It's not
26871 valid declare a constructor at function scope. */
26872 if (parser
->in_function_body
)
26874 /* And only certain tokens can begin a constructor declarator. */
26875 next_token
= cp_lexer_peek_token (parser
->lexer
);
26876 if (next_token
->type
!= CPP_NAME
26877 && next_token
->type
!= CPP_SCOPE
26878 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26879 && next_token
->type
!= CPP_TEMPLATE_ID
)
26882 /* Parse tentatively; we are going to roll back all of the tokens
26884 cp_parser_parse_tentatively (parser
);
26885 /* Assume that we are looking at a constructor declarator. */
26886 constructor_p
= true;
26888 /* Look for the optional `::' operator. */
26889 cp_parser_global_scope_opt (parser
,
26890 /*current_scope_valid_p=*/false);
26891 /* Look for the nested-name-specifier. */
26892 nested_name_specifier
26893 = (cp_parser_nested_name_specifier_opt (parser
,
26894 /*typename_keyword_p=*/false,
26895 /*check_dependency_p=*/false,
26897 /*is_declaration=*/false));
26899 outside_class_specifier_p
= (!at_class_scope_p ()
26900 || !TYPE_BEING_DEFINED (current_class_type
)
26903 /* Outside of a class-specifier, there must be a
26904 nested-name-specifier. Except in C++17 mode, where we
26905 might be declaring a guiding declaration. */
26906 if (!nested_name_specifier
&& outside_class_specifier_p
26907 && cxx_dialect
< cxx17
)
26908 constructor_p
= false;
26909 else if (nested_name_specifier
== error_mark_node
)
26910 constructor_p
= false;
26912 /* If we have a class scope, this is easy; DR 147 says that S::S always
26913 names the constructor, and no other qualified name could. */
26914 if (constructor_p
&& nested_name_specifier
26915 && CLASS_TYPE_P (nested_name_specifier
))
26917 tree id
= cp_parser_unqualified_id (parser
,
26918 /*template_keyword_p=*/false,
26919 /*check_dependency_p=*/false,
26920 /*declarator_p=*/true,
26921 /*optional_p=*/false);
26922 if (is_overloaded_fn (id
))
26923 id
= DECL_NAME (get_first_fn (id
));
26924 if (!constructor_name_p (id
, nested_name_specifier
))
26925 constructor_p
= false;
26927 /* If we still think that this might be a constructor-declarator,
26928 look for a class-name. */
26929 else if (constructor_p
)
26933 template <typename T> struct S {
26937 we must recognize that the nested `S' names a class. */
26938 if (cxx_dialect
>= cxx17
)
26939 cp_parser_parse_tentatively (parser
);
26942 type_decl
= cp_parser_class_name (parser
,
26943 /*typename_keyword_p=*/false,
26944 /*template_keyword_p=*/false,
26946 /*check_dependency_p=*/false,
26947 /*class_head_p=*/false,
26948 /*is_declaration=*/false);
26950 if (cxx_dialect
>= cxx17
26951 && !cp_parser_parse_definitely (parser
))
26953 type_decl
= NULL_TREE
;
26954 tree tmpl
= cp_parser_template_name (parser
,
26955 /*template_keyword*/false,
26956 /*check_dependency_p*/false,
26957 /*is_declaration*/false,
26959 /*is_identifier*/NULL
);
26960 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26961 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26962 /* It's a deduction guide, return true. */;
26964 cp_parser_simulate_error (parser
);
26967 /* If there was no class-name, then this is not a constructor.
26968 Otherwise, if we are in a class-specifier and we aren't
26969 handling a friend declaration, check that its type matches
26970 current_class_type (c++/38313). Note: error_mark_node
26971 is left alone for error recovery purposes. */
26972 constructor_p
= (!cp_parser_error_occurred (parser
)
26973 && (outside_class_specifier_p
26974 || type_decl
== NULL_TREE
26975 || type_decl
== error_mark_node
26976 || same_type_p (current_class_type
,
26977 TREE_TYPE (type_decl
))));
26979 /* If we're still considering a constructor, we have to see a `(',
26980 to begin the parameter-declaration-clause, followed by either a
26981 `)', an `...', or a decl-specifier. We need to check for a
26982 type-specifier to avoid being fooled into thinking that:
26986 is a constructor. (It is actually a function named `f' that
26987 takes one parameter (of type `int') and returns a value of type
26990 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26991 constructor_p
= false;
26994 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26995 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26996 /* A parameter declaration begins with a decl-specifier,
26997 which is either the "attribute" keyword, a storage class
26998 specifier, or (usually) a type-specifier. */
26999 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27002 tree pushed_scope
= NULL_TREE
;
27003 unsigned saved_num_template_parameter_lists
;
27005 /* Names appearing in the type-specifier should be looked up
27006 in the scope of the class. */
27007 if (current_class_type
)
27009 else if (type_decl
)
27011 type
= TREE_TYPE (type_decl
);
27012 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27014 type
= resolve_typename_type (type
,
27015 /*only_current_p=*/false);
27016 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27018 cp_parser_abort_tentative_parse (parser
);
27022 pushed_scope
= push_scope (type
);
27025 /* Inside the constructor parameter list, surrounding
27026 template-parameter-lists do not apply. */
27027 saved_num_template_parameter_lists
27028 = parser
->num_template_parameter_lists
;
27029 parser
->num_template_parameter_lists
= 0;
27031 /* Look for the type-specifier. */
27032 cp_parser_type_specifier (parser
,
27033 CP_PARSER_FLAGS_NONE
,
27034 /*decl_specs=*/NULL
,
27035 /*is_declarator=*/true,
27036 /*declares_class_or_enum=*/NULL
,
27037 /*is_cv_qualifier=*/NULL
);
27039 parser
->num_template_parameter_lists
27040 = saved_num_template_parameter_lists
;
27042 /* Leave the scope of the class. */
27044 pop_scope (pushed_scope
);
27046 constructor_p
= !cp_parser_error_occurred (parser
);
27050 /* We did not really want to consume any tokens. */
27051 cp_parser_abort_tentative_parse (parser
);
27053 return constructor_p
;
27056 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27057 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27058 they must be performed once we are in the scope of the function.
27060 Returns the function defined. */
27063 cp_parser_function_definition_from_specifiers_and_declarator
27064 (cp_parser
* parser
,
27065 cp_decl_specifier_seq
*decl_specifiers
,
27067 const cp_declarator
*declarator
)
27072 /* Begin the function-definition. */
27073 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27075 /* The things we're about to see are not directly qualified by any
27076 template headers we've seen thus far. */
27077 reset_specialization ();
27079 /* If there were names looked up in the decl-specifier-seq that we
27080 did not check, check them now. We must wait until we are in the
27081 scope of the function to perform the checks, since the function
27082 might be a friend. */
27083 perform_deferred_access_checks (tf_warning_or_error
);
27087 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27088 parser
->omp_declare_simd
= NULL
;
27089 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27090 parser
->oacc_routine
= NULL
;
27095 /* Skip the entire function. */
27096 cp_parser_skip_to_end_of_block_or_statement (parser
);
27097 fn
= error_mark_node
;
27099 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27101 /* Seen already, skip it. An error message has already been output. */
27102 cp_parser_skip_to_end_of_block_or_statement (parser
);
27103 fn
= current_function_decl
;
27104 current_function_decl
= NULL_TREE
;
27105 /* If this is a function from a class, pop the nested class. */
27106 if (current_class_name
)
27107 pop_nested_class ();
27112 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27113 tv
= TV_PARSE_INLINE
;
27115 tv
= TV_PARSE_FUNC
;
27117 fn
= cp_parser_function_definition_after_declarator (parser
,
27118 /*inline_p=*/false);
27125 /* Parse the part of a function-definition that follows the
27126 declarator. INLINE_P is TRUE iff this function is an inline
27127 function defined within a class-specifier.
27129 Returns the function defined. */
27132 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27136 bool saved_in_unbraced_linkage_specification_p
;
27137 bool saved_in_function_body
;
27138 unsigned saved_num_template_parameter_lists
;
27140 bool fully_implicit_function_template_p
27141 = parser
->fully_implicit_function_template_p
;
27142 parser
->fully_implicit_function_template_p
= false;
27143 tree implicit_template_parms
27144 = parser
->implicit_template_parms
;
27145 parser
->implicit_template_parms
= 0;
27146 cp_binding_level
* implicit_template_scope
27147 = parser
->implicit_template_scope
;
27148 parser
->implicit_template_scope
= 0;
27150 saved_in_function_body
= parser
->in_function_body
;
27151 parser
->in_function_body
= true;
27152 /* If the next token is `return', then the code may be trying to
27153 make use of the "named return value" extension that G++ used to
27155 token
= cp_lexer_peek_token (parser
->lexer
);
27156 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27158 /* Consume the `return' keyword. */
27159 cp_lexer_consume_token (parser
->lexer
);
27160 /* Look for the identifier that indicates what value is to be
27162 cp_parser_identifier (parser
);
27163 /* Issue an error message. */
27164 error_at (token
->location
,
27165 "named return values are no longer supported");
27166 /* Skip tokens until we reach the start of the function body. */
27169 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27170 if (token
->type
== CPP_OPEN_BRACE
27171 || token
->type
== CPP_EOF
27172 || token
->type
== CPP_PRAGMA_EOL
)
27174 cp_lexer_consume_token (parser
->lexer
);
27177 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27178 anything declared inside `f'. */
27179 saved_in_unbraced_linkage_specification_p
27180 = parser
->in_unbraced_linkage_specification_p
;
27181 parser
->in_unbraced_linkage_specification_p
= false;
27182 /* Inside the function, surrounding template-parameter-lists do not
27184 saved_num_template_parameter_lists
27185 = parser
->num_template_parameter_lists
;
27186 parser
->num_template_parameter_lists
= 0;
27188 /* If the next token is `try', `__transaction_atomic', or
27189 `__transaction_relaxed`, then we are looking at either function-try-block
27190 or function-transaction-block. Note that all of these include the
27192 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27193 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27194 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27195 RID_TRANSACTION_RELAXED
))
27196 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27197 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27198 cp_parser_function_try_block (parser
);
27200 cp_parser_ctor_initializer_opt_and_function_body
27201 (parser
, /*in_function_try_block=*/false);
27203 /* Finish the function. */
27204 fn
= finish_function (inline_p
);
27205 /* Generate code for it, if necessary. */
27206 expand_or_defer_fn (fn
);
27207 /* Restore the saved values. */
27208 parser
->in_unbraced_linkage_specification_p
27209 = saved_in_unbraced_linkage_specification_p
;
27210 parser
->num_template_parameter_lists
27211 = saved_num_template_parameter_lists
;
27212 parser
->in_function_body
= saved_in_function_body
;
27214 parser
->fully_implicit_function_template_p
27215 = fully_implicit_function_template_p
;
27216 parser
->implicit_template_parms
27217 = implicit_template_parms
;
27218 parser
->implicit_template_scope
27219 = implicit_template_scope
;
27221 if (parser
->fully_implicit_function_template_p
)
27222 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27227 /* Parse a template-declaration body (following argument list). */
27230 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27231 tree parameter_list
,
27234 tree decl
= NULL_TREE
;
27235 bool friend_p
= false;
27237 /* We just processed one more parameter list. */
27238 ++parser
->num_template_parameter_lists
;
27240 /* Get the deferred access checks from the parameter list. These
27241 will be checked once we know what is being declared, as for a
27242 member template the checks must be performed in the scope of the
27243 class containing the member. */
27244 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27246 /* Tentatively parse for a new template parameter list, which can either be
27247 the template keyword or a template introduction. */
27248 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27250 else if (cxx_dialect
>= cxx11
27251 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27252 decl
= cp_parser_alias_declaration (parser
);
27255 /* There are no access checks when parsing a template, as we do not
27256 know if a specialization will be a friend. */
27257 push_deferring_access_checks (dk_no_check
);
27258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27259 decl
= cp_parser_single_declaration (parser
,
27262 /*explicit_specialization_p=*/false,
27264 pop_deferring_access_checks ();
27266 /* If this is a member template declaration, let the front
27268 if (member_p
&& !friend_p
&& decl
)
27270 if (TREE_CODE (decl
) == TYPE_DECL
)
27271 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27273 decl
= finish_member_template_decl (decl
);
27275 else if (friend_p
&& decl
27276 && DECL_DECLARES_TYPE_P (decl
))
27277 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27278 /*complain=*/true);
27280 /* We are done with the current parameter list. */
27281 --parser
->num_template_parameter_lists
;
27283 pop_deferring_access_checks ();
27286 finish_template_decl (parameter_list
);
27288 /* Check the template arguments for a literal operator template. */
27290 && DECL_DECLARES_FUNCTION_P (decl
)
27291 && UDLIT_OPER_P (DECL_NAME (decl
)))
27294 if (parameter_list
== NULL_TREE
)
27298 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27299 if (num_parms
== 1)
27301 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27302 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27303 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27304 /* OK, C++20 string literal operator template. We don't need
27305 to warn in lower dialects here because we will have already
27306 warned about the template parameter. */;
27307 else if (TREE_TYPE (parm
) != char_type_node
27308 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27311 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27313 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27314 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27315 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27316 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27317 if (parm
== error_mark_node
27318 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27319 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27322 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27323 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27324 "ISO C++ did not adopt string literal operator templa"
27325 "tes taking an argument pack of characters");
27332 if (cxx_dialect
> cxx17
)
27333 error ("literal operator template %qD has invalid parameter list;"
27334 " Expected non-type template parameter pack <char...> "
27335 " or single non-type parameter of class type",
27338 error ("literal operator template %qD has invalid parameter list."
27339 " Expected non-type template parameter pack <char...>",
27344 /* Register member declarations. */
27345 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27346 finish_member_declaration (decl
);
27347 /* If DECL is a function template, we must return to parse it later.
27348 (Even though there is no definition, there might be default
27349 arguments that need handling.) */
27350 if (member_p
&& decl
27351 && DECL_DECLARES_FUNCTION_P (decl
))
27352 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27355 /* Parse a template introduction header for a template-declaration. Returns
27356 false if tentative parse fails. */
27359 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27361 cp_parser_parse_tentatively (parser
);
27363 tree saved_scope
= parser
->scope
;
27364 tree saved_object_scope
= parser
->object_scope
;
27365 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27367 /* Look for the optional `::' operator. */
27368 cp_parser_global_scope_opt (parser
,
27369 /*current_scope_valid_p=*/false);
27370 /* Look for the nested-name-specifier. */
27371 cp_parser_nested_name_specifier_opt (parser
,
27372 /*typename_keyword_p=*/false,
27373 /*check_dependency_p=*/true,
27375 /*is_declaration=*/false);
27377 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27378 tree concept_name
= cp_parser_identifier (parser
);
27380 /* Look up the concept for which we will be matching
27381 template parameters. */
27382 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27384 parser
->scope
= saved_scope
;
27385 parser
->object_scope
= saved_object_scope
;
27386 parser
->qualifying_scope
= saved_qualifying_scope
;
27388 if (concept_name
== error_mark_node
)
27389 cp_parser_simulate_error (parser
);
27391 /* Look for opening brace for introduction. */
27392 matching_braces braces
;
27393 braces
.require_open (parser
);
27395 if (!cp_parser_parse_definitely (parser
))
27398 push_deferring_access_checks (dk_deferred
);
27400 /* Build vector of placeholder parameters and grab
27401 matching identifiers. */
27402 tree introduction_list
= cp_parser_introduction_list (parser
);
27404 /* Look for closing brace for introduction. */
27405 if (!braces
.require_close (parser
))
27408 /* The introduction-list shall not be empty. */
27409 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27412 /* In cp_parser_introduction_list we have already issued an error. */
27416 if (tmpl_decl
== error_mark_node
)
27418 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27423 /* Build and associate the constraint. */
27424 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27425 if (parms
&& parms
!= error_mark_node
)
27427 cp_parser_template_declaration_after_parameters (parser
, parms
,
27432 error_at (token
->location
, "no matching concept for template-introduction");
27436 /* Parse a normal template-declaration following the template keyword. */
27439 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27441 tree parameter_list
;
27442 bool need_lang_pop
;
27443 location_t location
= input_location
;
27445 /* Look for the `<' token. */
27446 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27448 if (at_class_scope_p () && current_function_decl
)
27450 /* 14.5.2.2 [temp.mem]
27452 A local class shall not have member templates. */
27453 error_at (location
,
27454 "invalid declaration of member template in local class");
27455 cp_parser_skip_to_end_of_block_or_statement (parser
);
27460 A template ... shall not have C linkage. */
27461 if (current_lang_name
== lang_name_c
)
27463 error_at (location
, "template with C linkage");
27464 maybe_show_extern_c_location ();
27465 /* Give it C++ linkage to avoid confusing other parts of the
27467 push_lang_context (lang_name_cplusplus
);
27468 need_lang_pop
= true;
27471 need_lang_pop
= false;
27473 /* We cannot perform access checks on the template parameter
27474 declarations until we know what is being declared, just as we
27475 cannot check the decl-specifier list. */
27476 push_deferring_access_checks (dk_deferred
);
27478 /* If the next token is `>', then we have an invalid
27479 specialization. Rather than complain about an invalid template
27480 parameter, issue an error message here. */
27481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27483 cp_parser_error (parser
, "invalid explicit specialization");
27484 begin_specialization ();
27485 parameter_list
= NULL_TREE
;
27489 /* Parse the template parameters. */
27490 parameter_list
= cp_parser_template_parameter_list (parser
);
27493 /* Look for the `>'. */
27494 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27496 /* Manage template requirements */
27499 tree reqs
= get_shorthand_constraints (current_template_parms
);
27500 if (tree r
= cp_parser_requires_clause_opt (parser
))
27501 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27502 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27505 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27508 /* For the erroneous case of a template with C linkage, we pushed an
27509 implicit C++ linkage scope; exit that scope now. */
27511 pop_lang_context ();
27514 /* Parse a template-declaration, assuming that the `export' (and
27515 `extern') keywords, if present, has already been scanned. MEMBER_P
27516 is as for cp_parser_template_declaration. */
27519 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27521 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27523 cp_lexer_consume_token (parser
->lexer
);
27524 cp_parser_explicit_template_declaration (parser
, member_p
);
27527 else if (flag_concepts
)
27528 return cp_parser_template_introduction (parser
, member_p
);
27533 /* Perform the deferred access checks from a template-parameter-list.
27534 CHECKS is a TREE_LIST of access checks, as returned by
27535 get_deferred_access_checks. */
27538 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27540 ++processing_template_parmlist
;
27541 perform_access_checks (checks
, tf_warning_or_error
);
27542 --processing_template_parmlist
;
27545 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27546 `function-definition' sequence that follows a template header.
27547 If MEMBER_P is true, this declaration appears in a class scope.
27549 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27550 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27553 cp_parser_single_declaration (cp_parser
* parser
,
27554 vec
<deferred_access_check
, va_gc
> *checks
,
27556 bool explicit_specialization_p
,
27559 int declares_class_or_enum
;
27560 tree decl
= NULL_TREE
;
27561 cp_decl_specifier_seq decl_specifiers
;
27562 bool function_definition_p
= false;
27563 cp_token
*decl_spec_token_start
;
27565 /* This function is only used when processing a template
27567 gcc_assert (innermost_scope_kind () == sk_template_parms
27568 || innermost_scope_kind () == sk_template_spec
);
27570 /* Defer access checks until we know what is being declared. */
27571 push_deferring_access_checks (dk_deferred
);
27573 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27575 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27576 cp_parser_decl_specifier_seq (parser
,
27577 CP_PARSER_FLAGS_OPTIONAL
,
27579 &declares_class_or_enum
);
27581 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27583 /* There are no template typedefs. */
27584 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27586 error_at (decl_spec_token_start
->location
,
27587 "template declaration of %<typedef%>");
27588 decl
= error_mark_node
;
27591 /* Gather up the access checks that occurred the
27592 decl-specifier-seq. */
27593 stop_deferring_access_checks ();
27595 /* Check for the declaration of a template class. */
27596 if (declares_class_or_enum
)
27598 if (cp_parser_declares_only_class_p (parser
)
27599 || (declares_class_or_enum
& 2))
27601 // If this is a declaration, but not a definition, associate
27602 // any constraints with the type declaration. Constraints
27603 // are associated with definitions in cp_parser_class_specifier.
27604 if (declares_class_or_enum
== 1)
27605 associate_classtype_constraints (decl_specifiers
.type
);
27607 decl
= shadow_tag (&decl_specifiers
);
27612 friend template <typename T> struct A<T>::B;
27615 A<T>::B will be represented by a TYPENAME_TYPE, and
27616 therefore not recognized by shadow_tag. */
27617 if (friend_p
&& *friend_p
27619 && decl_specifiers
.type
27620 && TYPE_P (decl_specifiers
.type
))
27621 decl
= decl_specifiers
.type
;
27623 if (decl
&& decl
!= error_mark_node
)
27624 decl
= TYPE_NAME (decl
);
27626 decl
= error_mark_node
;
27628 /* Perform access checks for template parameters. */
27629 cp_parser_perform_template_parameter_access_checks (checks
);
27631 /* Give a helpful diagnostic for
27632 template <class T> struct A { } a;
27633 if we aren't already recovering from an error. */
27634 if (!cp_parser_declares_only_class_p (parser
)
27637 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27638 "a class template declaration must not declare "
27640 cp_parser_skip_to_end_of_block_or_statement (parser
);
27646 /* Complain about missing 'typename' or other invalid type names. */
27647 if (!decl_specifiers
.any_type_specifiers_p
27648 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27650 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27651 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27652 the rest of this declaration. */
27653 decl
= error_mark_node
;
27657 /* If it's not a template class, try for a template function. If
27658 the next token is a `;', then this declaration does not declare
27659 anything. But, if there were errors in the decl-specifiers, then
27660 the error might well have come from an attempted class-specifier.
27661 In that case, there's no need to warn about a missing declarator. */
27663 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27664 || decl_specifiers
.type
!= error_mark_node
))
27666 decl
= cp_parser_init_declarator (parser
,
27669 /*function_definition_allowed_p=*/true,
27671 declares_class_or_enum
,
27672 &function_definition_p
,
27675 /* 7.1.1-1 [dcl.stc]
27677 A storage-class-specifier shall not be specified in an explicit
27678 specialization... */
27680 && explicit_specialization_p
27681 && decl_specifiers
.storage_class
!= sc_none
)
27683 error_at (decl_spec_token_start
->location
,
27684 "explicit template specialization cannot have a storage class");
27685 decl
= error_mark_node
;
27688 if (decl
&& VAR_P (decl
))
27689 check_template_variable (decl
);
27692 /* Look for a trailing `;' after the declaration. */
27693 if (!function_definition_p
27694 && (decl
== error_mark_node
27695 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27696 cp_parser_skip_to_end_of_block_or_statement (parser
);
27699 pop_deferring_access_checks ();
27701 /* Clear any current qualification; whatever comes next is the start
27702 of something new. */
27703 parser
->scope
= NULL_TREE
;
27704 parser
->qualifying_scope
= NULL_TREE
;
27705 parser
->object_scope
= NULL_TREE
;
27710 /* Parse a cast-expression that is not the operand of a unary "&". */
27713 cp_parser_simple_cast_expression (cp_parser
*parser
)
27715 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27716 /*cast_p=*/false, /*decltype*/false, NULL
);
27719 /* Parse a functional cast to TYPE. Returns an expression
27720 representing the cast. */
27723 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27725 vec
<tree
, va_gc
> *vec
;
27726 tree expression_list
;
27730 location_t start_loc
= input_location
;
27733 type
= error_mark_node
;
27735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27737 cp_lexer_set_source_position (parser
->lexer
);
27738 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27739 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27740 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27741 if (TREE_CODE (type
) == TYPE_DECL
)
27742 type
= TREE_TYPE (type
);
27744 cast
= finish_compound_literal (type
, expression_list
,
27745 tf_warning_or_error
, fcl_functional
);
27746 /* Create a location of the form:
27749 with caret == start at the start of the type name,
27750 finishing at the closing brace. */
27751 location_t finish_loc
27752 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27753 location_t combined_loc
= make_location (start_loc
, start_loc
,
27755 cast
.set_location (combined_loc
);
27760 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27762 /*allow_expansion_p=*/true,
27763 /*non_constant_p=*/NULL
);
27765 expression_list
= error_mark_node
;
27768 expression_list
= build_tree_list_vec (vec
);
27769 release_tree_vector (vec
);
27772 cast
= build_functional_cast (type
, expression_list
,
27773 tf_warning_or_error
);
27774 /* [expr.const]/1: In an integral constant expression "only type
27775 conversions to integral or enumeration type can be used". */
27776 if (TREE_CODE (type
) == TYPE_DECL
)
27777 type
= TREE_TYPE (type
);
27778 if (cast
!= error_mark_node
27779 && !cast_valid_in_integral_constant_expression_p (type
)
27780 && cp_parser_non_integral_constant_expression (parser
,
27782 return error_mark_node
;
27784 /* Create a location of the form:
27787 with caret == start at the start of the type name,
27788 finishing at the closing paren. */
27789 location_t finish_loc
27790 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27791 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27792 cast
.set_location (combined_loc
);
27796 /* Save the tokens that make up the body of a member function defined
27797 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27798 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27799 specifiers applied to the declaration. Returns the FUNCTION_DECL
27800 for the member function. */
27803 cp_parser_save_member_function_body (cp_parser
* parser
,
27804 cp_decl_specifier_seq
*decl_specifiers
,
27805 cp_declarator
*declarator
,
27811 bool function_try_block
= false;
27813 /* Create the FUNCTION_DECL. */
27814 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27815 cp_finalize_omp_declare_simd (parser
, fn
);
27816 cp_finalize_oacc_routine (parser
, fn
, true);
27817 /* If something went badly wrong, bail out now. */
27818 if (fn
== error_mark_node
)
27820 /* If there's a function-body, skip it. */
27821 if (cp_parser_token_starts_function_definition_p
27822 (cp_lexer_peek_token (parser
->lexer
)))
27823 cp_parser_skip_to_end_of_block_or_statement (parser
);
27824 return error_mark_node
;
27827 /* Remember it, if there default args to post process. */
27828 cp_parser_save_default_args (parser
, fn
);
27830 /* Save away the tokens that make up the body of the
27832 first
= parser
->lexer
->next_token
;
27834 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27835 cp_lexer_consume_token (parser
->lexer
);
27836 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27837 RID_TRANSACTION_ATOMIC
))
27839 cp_lexer_consume_token (parser
->lexer
);
27840 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27841 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27842 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27843 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27844 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27845 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27846 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27848 cp_lexer_consume_token (parser
->lexer
);
27849 cp_lexer_consume_token (parser
->lexer
);
27850 cp_lexer_consume_token (parser
->lexer
);
27851 cp_lexer_consume_token (parser
->lexer
);
27852 cp_lexer_consume_token (parser
->lexer
);
27855 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27856 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27858 cp_lexer_consume_token (parser
->lexer
);
27859 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27864 /* Handle function try blocks. */
27865 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27867 cp_lexer_consume_token (parser
->lexer
);
27868 function_try_block
= true;
27870 /* We can have braced-init-list mem-initializers before the fn body. */
27871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27873 cp_lexer_consume_token (parser
->lexer
);
27874 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27876 /* cache_group will stop after an un-nested { } pair, too. */
27877 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27880 /* variadic mem-inits have ... after the ')'. */
27881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27882 cp_lexer_consume_token (parser
->lexer
);
27885 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27886 /* Handle function try blocks. */
27887 if (function_try_block
)
27888 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27889 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27890 last
= parser
->lexer
->next_token
;
27892 /* Save away the inline definition; we will process it when the
27893 class is complete. */
27894 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27895 DECL_PENDING_INLINE_P (fn
) = 1;
27897 /* We need to know that this was defined in the class, so that
27898 friend templates are handled correctly. */
27899 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27901 /* Add FN to the queue of functions to be parsed later. */
27902 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27907 /* Save the tokens that make up the in-class initializer for a non-static
27908 data member. Returns a DEFAULT_ARG. */
27911 cp_parser_save_nsdmi (cp_parser
* parser
)
27913 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27916 /* Parse a template-argument-list, as well as the trailing ">" (but
27917 not the opening "<"). See cp_parser_template_argument_list for the
27921 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27925 tree saved_qualifying_scope
;
27926 tree saved_object_scope
;
27927 bool saved_greater_than_is_operator_p
;
27931 When parsing a template-id, the first non-nested `>' is taken as
27932 the end of the template-argument-list rather than a greater-than
27934 saved_greater_than_is_operator_p
27935 = parser
->greater_than_is_operator_p
;
27936 parser
->greater_than_is_operator_p
= false;
27937 /* Parsing the argument list may modify SCOPE, so we save it
27939 saved_scope
= parser
->scope
;
27940 saved_qualifying_scope
= parser
->qualifying_scope
;
27941 saved_object_scope
= parser
->object_scope
;
27942 /* We need to evaluate the template arguments, even though this
27943 template-id may be nested within a "sizeof". */
27945 /* Parse the template-argument-list itself. */
27946 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27947 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27948 arguments
= NULL_TREE
;
27950 arguments
= cp_parser_template_argument_list (parser
);
27951 /* Look for the `>' that ends the template-argument-list. If we find
27952 a '>>' instead, it's probably just a typo. */
27953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27955 if (cxx_dialect
!= cxx98
)
27957 /* In C++0x, a `>>' in a template argument list or cast
27958 expression is considered to be two separate `>'
27959 tokens. So, change the current token to a `>', but don't
27960 consume it: it will be consumed later when the outer
27961 template argument list (or cast expression) is parsed.
27962 Note that this replacement of `>' for `>>' is necessary
27963 even if we are parsing tentatively: in the tentative
27964 case, after calling
27965 cp_parser_enclosed_template_argument_list we will always
27966 throw away all of the template arguments and the first
27967 closing `>', either because the template argument list
27968 was erroneous or because we are replacing those tokens
27969 with a CPP_TEMPLATE_ID token. The second `>' (which will
27970 not have been thrown away) is needed either to close an
27971 outer template argument list or to complete a new-style
27973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27974 token
->type
= CPP_GREATER
;
27976 else if (!saved_greater_than_is_operator_p
)
27978 /* If we're in a nested template argument list, the '>>' has
27979 to be a typo for '> >'. We emit the error message, but we
27980 continue parsing and we push a '>' as next token, so that
27981 the argument list will be parsed correctly. Note that the
27982 global source location is still on the token before the
27983 '>>', so we need to say explicitly where we want it. */
27984 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27985 gcc_rich_location
richloc (token
->location
);
27986 richloc
.add_fixit_replace ("> >");
27987 error_at (&richloc
, "%<>>%> should be %<> >%> "
27988 "within a nested template argument list");
27990 token
->type
= CPP_GREATER
;
27994 /* If this is not a nested template argument list, the '>>'
27995 is a typo for '>'. Emit an error message and continue.
27996 Same deal about the token location, but here we can get it
27997 right by consuming the '>>' before issuing the diagnostic. */
27998 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27999 error_at (token
->location
,
28000 "spurious %<>>%>, use %<>%> to terminate "
28001 "a template argument list");
28005 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28006 /* The `>' token might be a greater-than operator again now. */
28007 parser
->greater_than_is_operator_p
28008 = saved_greater_than_is_operator_p
;
28009 /* Restore the SAVED_SCOPE. */
28010 parser
->scope
= saved_scope
;
28011 parser
->qualifying_scope
= saved_qualifying_scope
;
28012 parser
->object_scope
= saved_object_scope
;
28017 /* MEMBER_FUNCTION is a member function, or a friend. If default
28018 arguments, or the body of the function have not yet been parsed,
28022 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28024 timevar_push (TV_PARSE_INMETH
);
28025 /* If this member is a template, get the underlying
28027 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28028 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28030 /* There should not be any class definitions in progress at this
28031 point; the bodies of members are only parsed outside of all class
28033 gcc_assert (parser
->num_classes_being_defined
== 0);
28034 /* While we're parsing the member functions we might encounter more
28035 classes. We want to handle them right away, but we don't want
28036 them getting mixed up with functions that are currently in the
28038 push_unparsed_function_queues (parser
);
28040 /* Make sure that any template parameters are in scope. */
28041 maybe_begin_member_template_processing (member_function
);
28043 /* If the body of the function has not yet been parsed, parse it
28045 if (DECL_PENDING_INLINE_P (member_function
))
28047 tree function_scope
;
28048 cp_token_cache
*tokens
;
28050 /* The function is no longer pending; we are processing it. */
28051 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28052 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28053 DECL_PENDING_INLINE_P (member_function
) = 0;
28055 /* If this is a local class, enter the scope of the containing
28057 function_scope
= current_function_decl
;
28058 if (function_scope
)
28059 push_function_context ();
28061 /* Push the body of the function onto the lexer stack. */
28062 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28064 /* Let the front end know that we going to be defining this
28066 start_preparsed_function (member_function
, NULL_TREE
,
28067 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28069 /* Don't do access checking if it is a templated function. */
28070 if (processing_template_decl
)
28071 push_deferring_access_checks (dk_no_check
);
28073 /* #pragma omp declare reduction needs special parsing. */
28074 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28076 parser
->lexer
->in_pragma
= true;
28077 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28078 finish_function (/*inline_p=*/true);
28079 cp_check_omp_declare_reduction (member_function
);
28082 /* Now, parse the body of the function. */
28083 cp_parser_function_definition_after_declarator (parser
,
28084 /*inline_p=*/true);
28086 if (processing_template_decl
)
28087 pop_deferring_access_checks ();
28089 /* Leave the scope of the containing function. */
28090 if (function_scope
)
28091 pop_function_context ();
28092 cp_parser_pop_lexer (parser
);
28095 /* Remove any template parameters from the symbol table. */
28096 maybe_end_member_template_processing ();
28098 /* Restore the queue. */
28099 pop_unparsed_function_queues (parser
);
28100 timevar_pop (TV_PARSE_INMETH
);
28103 /* If DECL contains any default args, remember it on the unparsed
28104 functions queue. */
28107 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28111 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28113 probe
= TREE_CHAIN (probe
))
28114 if (TREE_PURPOSE (probe
))
28116 cp_default_arg_entry entry
= {current_class_type
, decl
};
28117 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28122 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28123 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28124 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28125 from the parameter-type-list. */
28128 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28129 tree default_arg
, tree parmtype
)
28131 cp_token_cache
*tokens
;
28135 if (default_arg
== error_mark_node
)
28136 return error_mark_node
;
28138 /* Push the saved tokens for the default argument onto the parser's
28140 tokens
= DEFARG_TOKENS (default_arg
);
28141 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28143 start_lambda_scope (decl
);
28145 /* Parse the default argument. */
28146 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28147 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28150 finish_lambda_scope ();
28152 if (parsed_arg
== error_mark_node
)
28153 cp_parser_skip_to_end_of_statement (parser
);
28155 if (!processing_template_decl
)
28157 /* In a non-template class, check conversions now. In a template,
28158 we'll wait and instantiate these as needed. */
28159 if (TREE_CODE (decl
) == PARM_DECL
)
28160 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28161 tf_warning_or_error
);
28162 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28163 parsed_arg
= error_mark_node
;
28165 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28168 /* If the token stream has not been completely used up, then
28169 there was extra junk after the end of the default
28171 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28173 if (TREE_CODE (decl
) == PARM_DECL
)
28174 cp_parser_error (parser
, "expected %<,%>");
28176 cp_parser_error (parser
, "expected %<;%>");
28179 /* Revert to the main lexer. */
28180 cp_parser_pop_lexer (parser
);
28185 /* FIELD is a non-static data member with an initializer which we saved for
28186 later; parse it now. */
28189 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28193 maybe_begin_member_template_processing (field
);
28195 push_unparsed_function_queues (parser
);
28196 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28197 DECL_INITIAL (field
),
28199 pop_unparsed_function_queues (parser
);
28201 maybe_end_member_template_processing ();
28203 DECL_INITIAL (field
) = def
;
28206 /* FN is a FUNCTION_DECL which may contains a parameter with an
28207 unparsed DEFAULT_ARG. Parse the default args now. This function
28208 assumes that the current scope is the scope in which the default
28209 argument should be processed. */
28212 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28214 bool saved_local_variables_forbidden_p
;
28215 tree parm
, parmdecl
;
28217 /* While we're parsing the default args, we might (due to the
28218 statement expression extension) encounter more classes. We want
28219 to handle them right away, but we don't want them getting mixed
28220 up with default args that are currently in the queue. */
28221 push_unparsed_function_queues (parser
);
28223 /* Local variable names (and the `this' keyword) may not appear
28224 in a default argument. */
28225 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28226 parser
->local_variables_forbidden_p
= true;
28228 push_defarg_context (fn
);
28230 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28231 parmdecl
= DECL_ARGUMENTS (fn
);
28232 parm
&& parm
!= void_list_node
;
28233 parm
= TREE_CHAIN (parm
),
28234 parmdecl
= DECL_CHAIN (parmdecl
))
28236 tree default_arg
= TREE_PURPOSE (parm
);
28238 vec
<tree
, va_gc
> *insts
;
28245 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28246 /* This can happen for a friend declaration for a function
28247 already declared with default arguments. */
28251 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28253 TREE_VALUE (parm
));
28254 TREE_PURPOSE (parm
) = parsed_arg
;
28256 /* Update any instantiations we've already created. */
28257 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28258 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28259 TREE_PURPOSE (copy
) = parsed_arg
;
28262 pop_defarg_context ();
28264 /* Make sure no default arg is missing. */
28265 check_default_args (fn
);
28267 /* Restore the state of local_variables_forbidden_p. */
28268 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28270 /* Restore the queue. */
28271 pop_unparsed_function_queues (parser
);
28274 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28276 sizeof ... ( identifier )
28278 where the 'sizeof' token has already been consumed. */
28281 cp_parser_sizeof_pack (cp_parser
*parser
)
28283 /* Consume the `...'. */
28284 cp_lexer_consume_token (parser
->lexer
);
28285 maybe_warn_variadic_templates ();
28287 matching_parens parens
;
28288 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28290 parens
.consume_open (parser
);
28292 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28293 "%<sizeof...%> argument must be surrounded by parentheses");
28295 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28296 tree name
= cp_parser_identifier (parser
);
28297 if (name
== error_mark_node
)
28298 return error_mark_node
;
28299 /* The name is not qualified. */
28300 parser
->scope
= NULL_TREE
;
28301 parser
->qualifying_scope
= NULL_TREE
;
28302 parser
->object_scope
= NULL_TREE
;
28303 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28304 if (expr
== error_mark_node
)
28305 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28307 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28308 expr
= TREE_TYPE (expr
);
28309 else if (TREE_CODE (expr
) == CONST_DECL
)
28310 expr
= DECL_INITIAL (expr
);
28311 expr
= make_pack_expansion (expr
);
28312 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28315 parens
.require_close (parser
);
28320 /* Parse the operand of `sizeof' (or a similar operator). Returns
28321 either a TYPE or an expression, depending on the form of the
28322 input. The KEYWORD indicates which kind of expression we have
28326 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28328 tree expr
= NULL_TREE
;
28329 const char *saved_message
;
28331 bool saved_integral_constant_expression_p
;
28332 bool saved_non_integral_constant_expression_p
;
28334 /* If it's a `...', then we are computing the length of a parameter
28336 if (keyword
== RID_SIZEOF
28337 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28338 return cp_parser_sizeof_pack (parser
);
28340 /* Types cannot be defined in a `sizeof' expression. Save away the
28342 saved_message
= parser
->type_definition_forbidden_message
;
28343 /* And create the new one. */
28344 tmp
= concat ("types may not be defined in %<",
28345 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28346 "%> expressions", NULL
);
28347 parser
->type_definition_forbidden_message
= tmp
;
28349 /* The restrictions on constant-expressions do not apply inside
28350 sizeof expressions. */
28351 saved_integral_constant_expression_p
28352 = parser
->integral_constant_expression_p
;
28353 saved_non_integral_constant_expression_p
28354 = parser
->non_integral_constant_expression_p
;
28355 parser
->integral_constant_expression_p
= false;
28357 /* Do not actually evaluate the expression. */
28358 ++cp_unevaluated_operand
;
28359 ++c_inhibit_evaluation_warnings
;
28360 /* If it's a `(', then we might be looking at the type-id
28362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28364 tree type
= NULL_TREE
;
28366 /* We can't be sure yet whether we're looking at a type-id or an
28368 cp_parser_parse_tentatively (parser
);
28370 matching_parens parens
;
28371 parens
.consume_open (parser
);
28373 /* Note: as a GNU Extension, compound literals are considered
28374 postfix-expressions as they are in C99, so they are valid
28375 arguments to sizeof. See comment in cp_parser_cast_expression
28377 if (cp_parser_compound_literal_p (parser
))
28378 cp_parser_simulate_error (parser
);
28381 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28382 parser
->in_type_id_in_expr_p
= true;
28383 /* Look for the type-id. */
28384 type
= cp_parser_type_id (parser
);
28385 /* Look for the closing `)'. */
28386 parens
.require_close (parser
);
28387 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28390 /* If all went well, then we're done. */
28391 if (cp_parser_parse_definitely (parser
))
28395 /* If the type-id production did not work out, then we must be
28396 looking at the unary-expression production. */
28398 expr
= cp_parser_unary_expression (parser
);
28400 /* Go back to evaluating expressions. */
28401 --cp_unevaluated_operand
;
28402 --c_inhibit_evaluation_warnings
;
28404 /* Free the message we created. */
28406 /* And restore the old one. */
28407 parser
->type_definition_forbidden_message
= saved_message
;
28408 parser
->integral_constant_expression_p
28409 = saved_integral_constant_expression_p
;
28410 parser
->non_integral_constant_expression_p
28411 = saved_non_integral_constant_expression_p
;
28416 /* If the current declaration has no declarator, return true. */
28419 cp_parser_declares_only_class_p (cp_parser
*parser
)
28421 /* If the next token is a `;' or a `,' then there is no
28423 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28424 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28427 /* Update the DECL_SPECS to reflect the storage class indicated by
28431 cp_parser_set_storage_class (cp_parser
*parser
,
28432 cp_decl_specifier_seq
*decl_specs
,
28436 cp_storage_class storage_class
;
28438 if (parser
->in_unbraced_linkage_specification_p
)
28440 error_at (token
->location
, "invalid use of %qD in linkage specification",
28441 ridpointers
[keyword
]);
28444 else if (decl_specs
->storage_class
!= sc_none
)
28446 decl_specs
->conflicting_specifiers_p
= true;
28450 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28451 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28452 && decl_specs
->gnu_thread_keyword_p
)
28454 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28455 "%<__thread%> before %qD", ridpointers
[keyword
]);
28461 storage_class
= sc_auto
;
28464 storage_class
= sc_register
;
28467 storage_class
= sc_static
;
28470 storage_class
= sc_extern
;
28473 storage_class
= sc_mutable
;
28476 gcc_unreachable ();
28478 decl_specs
->storage_class
= storage_class
;
28479 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28481 /* A storage class specifier cannot be applied alongside a typedef
28482 specifier. If there is a typedef specifier present then set
28483 conflicting_specifiers_p which will trigger an error later
28484 on in grokdeclarator. */
28485 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28486 decl_specs
->conflicting_specifiers_p
= true;
28489 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28490 is true, the type is a class or enum definition. */
28493 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28496 bool type_definition_p
)
28498 decl_specs
->any_specifiers_p
= true;
28500 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28501 (with, for example, in "typedef int wchar_t;") we remember that
28502 this is what happened. In system headers, we ignore these
28503 declarations so that G++ can work with system headers that are not
28505 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28506 && !type_definition_p
28507 && (type_spec
== boolean_type_node
28508 || type_spec
== char16_type_node
28509 || type_spec
== char32_type_node
28510 || type_spec
== wchar_type_node
)
28511 && (decl_specs
->type
28512 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28513 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28514 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28515 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28517 decl_specs
->redefined_builtin_type
= type_spec
;
28518 set_and_check_decl_spec_loc (decl_specs
,
28519 ds_redefined_builtin_type_spec
,
28521 if (!decl_specs
->type
)
28523 decl_specs
->type
= type_spec
;
28524 decl_specs
->type_definition_p
= false;
28525 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28528 else if (decl_specs
->type
)
28529 decl_specs
->multiple_types_p
= true;
28532 decl_specs
->type
= type_spec
;
28533 decl_specs
->type_definition_p
= type_definition_p
;
28534 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28535 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28539 /* True iff TOKEN is the GNU keyword __thread. */
28542 token_is__thread (cp_token
*token
)
28544 gcc_assert (token
->keyword
== RID_THREAD
);
28545 return id_equal (token
->u
.value
, "__thread");
28548 /* Set the location for a declarator specifier and check if it is
28551 DECL_SPECS is the sequence of declarator specifiers onto which to
28554 DS is the single declarator specifier to set which location is to
28555 be set onto the existing sequence of declarators.
28557 LOCATION is the location for the declarator specifier to
28561 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28562 cp_decl_spec ds
, cp_token
*token
)
28564 gcc_assert (ds
< ds_last
);
28566 if (decl_specs
== NULL
)
28569 location_t location
= token
->location
;
28571 if (decl_specs
->locations
[ds
] == 0)
28573 decl_specs
->locations
[ds
] = location
;
28574 if (ds
== ds_thread
)
28575 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28581 if (decl_specs
->locations
[ds_long_long
] != 0)
28582 error_at (location
,
28583 "%<long long long%> is too long for GCC");
28586 decl_specs
->locations
[ds_long_long
] = location
;
28587 pedwarn_cxx98 (location
,
28589 "ISO C++ 1998 does not support %<long long%>");
28592 else if (ds
== ds_thread
)
28594 bool gnu
= token_is__thread (token
);
28595 gcc_rich_location
richloc (location
);
28596 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28598 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28599 error_at (&richloc
,
28600 "both %<__thread%> and %<thread_local%> specified");
28604 richloc
.add_fixit_remove ();
28605 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28610 static const char *const decl_spec_names
[] = {
28627 gcc_rich_location
richloc (location
);
28628 richloc
.add_fixit_remove ();
28629 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28634 /* Return true iff the declarator specifier DS is present in the
28635 sequence of declarator specifiers DECL_SPECS. */
28638 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28641 gcc_assert (ds
< ds_last
);
28643 if (decl_specs
== NULL
)
28646 return decl_specs
->locations
[ds
] != 0;
28649 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28650 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28653 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28655 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28658 /* Issue an error message indicating that TOKEN_DESC was expected.
28659 If KEYWORD is true, it indicated this function is called by
28660 cp_parser_require_keword and the required token can only be
28661 a indicated keyword.
28663 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28664 within any error as the location of an "opening" token matching
28665 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28666 RT_CLOSE_PAREN). */
28669 cp_parser_required_error (cp_parser
*parser
,
28670 required_token token_desc
,
28672 location_t matching_location
)
28674 if (cp_parser_simulate_error (parser
))
28677 const char *gmsgid
= NULL
;
28678 switch (token_desc
)
28681 gmsgid
= G_("expected %<new%>");
28684 gmsgid
= G_("expected %<delete%>");
28687 gmsgid
= G_("expected %<return%>");
28690 gmsgid
= G_("expected %<while%>");
28693 gmsgid
= G_("expected %<extern%>");
28695 case RT_STATIC_ASSERT
:
28696 gmsgid
= G_("expected %<static_assert%>");
28699 gmsgid
= G_("expected %<decltype%>");
28702 gmsgid
= G_("expected %<operator%>");
28705 gmsgid
= G_("expected %<class%>");
28708 gmsgid
= G_("expected %<template%>");
28711 gmsgid
= G_("expected %<namespace%>");
28714 gmsgid
= G_("expected %<using%>");
28717 gmsgid
= G_("expected %<asm%>");
28720 gmsgid
= G_("expected %<try%>");
28723 gmsgid
= G_("expected %<catch%>");
28726 gmsgid
= G_("expected %<throw%>");
28729 gmsgid
= G_("expected %<__label__%>");
28732 gmsgid
= G_("expected %<@try%>");
28734 case RT_AT_SYNCHRONIZED
:
28735 gmsgid
= G_("expected %<@synchronized%>");
28738 gmsgid
= G_("expected %<@throw%>");
28740 case RT_TRANSACTION_ATOMIC
:
28741 gmsgid
= G_("expected %<__transaction_atomic%>");
28743 case RT_TRANSACTION_RELAXED
:
28744 gmsgid
= G_("expected %<__transaction_relaxed%>");
28750 if (!gmsgid
&& !keyword
)
28752 switch (token_desc
)
28755 gmsgid
= G_("expected %<;%>");
28757 case RT_OPEN_PAREN
:
28758 gmsgid
= G_("expected %<(%>");
28760 case RT_CLOSE_BRACE
:
28761 gmsgid
= G_("expected %<}%>");
28763 case RT_OPEN_BRACE
:
28764 gmsgid
= G_("expected %<{%>");
28766 case RT_CLOSE_SQUARE
:
28767 gmsgid
= G_("expected %<]%>");
28769 case RT_OPEN_SQUARE
:
28770 gmsgid
= G_("expected %<[%>");
28773 gmsgid
= G_("expected %<,%>");
28776 gmsgid
= G_("expected %<::%>");
28779 gmsgid
= G_("expected %<<%>");
28782 gmsgid
= G_("expected %<>%>");
28785 gmsgid
= G_("expected %<=%>");
28788 gmsgid
= G_("expected %<...%>");
28791 gmsgid
= G_("expected %<*%>");
28794 gmsgid
= G_("expected %<~%>");
28797 gmsgid
= G_("expected %<:%>");
28799 case RT_COLON_SCOPE
:
28800 gmsgid
= G_("expected %<:%> or %<::%>");
28802 case RT_CLOSE_PAREN
:
28803 gmsgid
= G_("expected %<)%>");
28805 case RT_COMMA_CLOSE_PAREN
:
28806 gmsgid
= G_("expected %<,%> or %<)%>");
28808 case RT_PRAGMA_EOL
:
28809 gmsgid
= G_("expected end of line");
28812 gmsgid
= G_("expected identifier");
28815 gmsgid
= G_("expected selection-statement");
28818 gmsgid
= G_("expected iteration-statement");
28821 gmsgid
= G_("expected jump-statement");
28824 gmsgid
= G_("expected class-key");
28826 case RT_CLASS_TYPENAME_TEMPLATE
:
28827 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28830 gcc_unreachable ();
28835 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28839 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28840 issue an error message indicating that TOKEN_DESC was expected.
28842 Returns the token consumed, if the token had the appropriate type.
28843 Otherwise, returns NULL.
28845 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28846 within any error as the location of an "opening" token matching
28847 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28848 RT_CLOSE_PAREN). */
28851 cp_parser_require (cp_parser
* parser
,
28852 enum cpp_ttype type
,
28853 required_token token_desc
,
28854 location_t matching_location
)
28856 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28857 return cp_lexer_consume_token (parser
->lexer
);
28860 /* Output the MESSAGE -- unless we're parsing tentatively. */
28861 if (!cp_parser_simulate_error (parser
))
28862 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28863 matching_location
);
28868 /* An error message is produced if the next token is not '>'.
28869 All further tokens are skipped until the desired token is
28870 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28873 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28875 /* Current level of '< ... >'. */
28876 unsigned level
= 0;
28877 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28878 unsigned nesting_depth
= 0;
28880 /* Are we ready, yet? If not, issue error message. */
28881 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28884 /* Skip tokens until the desired token is found. */
28887 /* Peek at the next token. */
28888 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28891 if (!nesting_depth
)
28896 if (cxx_dialect
== cxx98
)
28897 /* C++0x views the `>>' operator as two `>' tokens, but
28900 else if (!nesting_depth
&& level
-- == 0)
28902 /* We've hit a `>>' where the first `>' closes the
28903 template argument list, and the second `>' is
28904 spurious. Just consume the `>>' and stop; we've
28905 already produced at least one error. */
28906 cp_lexer_consume_token (parser
->lexer
);
28909 /* Fall through for C++0x, so we handle the second `>' in
28911 gcc_fallthrough ();
28914 if (!nesting_depth
&& level
-- == 0)
28916 /* We've reached the token we want, consume it and stop. */
28917 cp_lexer_consume_token (parser
->lexer
);
28922 case CPP_OPEN_PAREN
:
28923 case CPP_OPEN_SQUARE
:
28927 case CPP_CLOSE_PAREN
:
28928 case CPP_CLOSE_SQUARE
:
28929 if (nesting_depth
-- == 0)
28934 case CPP_PRAGMA_EOL
:
28935 case CPP_SEMICOLON
:
28936 case CPP_OPEN_BRACE
:
28937 case CPP_CLOSE_BRACE
:
28938 /* The '>' was probably forgotten, don't look further. */
28945 /* Consume this token. */
28946 cp_lexer_consume_token (parser
->lexer
);
28950 /* If the next token is the indicated keyword, consume it. Otherwise,
28951 issue an error message indicating that TOKEN_DESC was expected.
28953 Returns the token consumed, if the token had the appropriate type.
28954 Otherwise, returns NULL. */
28957 cp_parser_require_keyword (cp_parser
* parser
,
28959 required_token token_desc
)
28961 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28963 if (token
&& token
->keyword
!= keyword
)
28965 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28973 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28974 function-definition. */
28977 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28979 return (/* An ordinary function-body begins with an `{'. */
28980 token
->type
== CPP_OPEN_BRACE
28981 /* A ctor-initializer begins with a `:'. */
28982 || token
->type
== CPP_COLON
28983 /* A function-try-block begins with `try'. */
28984 || token
->keyword
== RID_TRY
28985 /* A function-transaction-block begins with `__transaction_atomic'
28986 or `__transaction_relaxed'. */
28987 || token
->keyword
== RID_TRANSACTION_ATOMIC
28988 || token
->keyword
== RID_TRANSACTION_RELAXED
28989 /* The named return value extension begins with `return'. */
28990 || token
->keyword
== RID_RETURN
);
28993 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28997 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29001 token
= cp_lexer_peek_token (parser
->lexer
);
29002 return (token
->type
== CPP_OPEN_BRACE
29003 || (token
->type
== CPP_COLON
29004 && !parser
->colon_doesnt_start_class_def_p
));
29007 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29008 C++0x) ending a template-argument. */
29011 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29015 token
= cp_lexer_peek_token (parser
->lexer
);
29016 return (token
->type
== CPP_COMMA
29017 || token
->type
== CPP_GREATER
29018 || token
->type
== CPP_ELLIPSIS
29019 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29022 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29023 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29026 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29031 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29032 if (token
->type
== CPP_LESS
)
29034 /* Check for the sequence `<::' in the original code. It would be lexed as
29035 `[:', where `[' is a digraph, and there is no whitespace before
29037 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29040 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29041 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29047 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29048 or none_type otherwise. */
29050 static enum tag_types
29051 cp_parser_token_is_class_key (cp_token
* token
)
29053 switch (token
->keyword
)
29058 return record_type
;
29067 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29068 or none_type otherwise or if the token is null. */
29070 static enum tag_types
29071 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29076 switch (token
->keyword
)
29081 return typename_type
;
29088 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29091 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29093 if (type
== error_mark_node
)
29095 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29097 if (permerror (input_location
, "%qs tag used in naming %q#T",
29098 class_key
== union_type
? "union"
29099 : class_key
== record_type
? "struct" : "class",
29101 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29102 "%q#T was previously declared here", type
);
29106 /* Issue an error message if DECL is redeclared with different
29107 access than its original declaration [class.access.spec/3].
29108 This applies to nested classes, nested class templates and
29109 enumerations [class.mem/1]. */
29112 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29115 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29116 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29119 if ((TREE_PRIVATE (decl
)
29120 != (current_access_specifier
== access_private_node
))
29121 || (TREE_PROTECTED (decl
)
29122 != (current_access_specifier
== access_protected_node
)))
29123 error_at (location
, "%qD redeclared with different access", decl
);
29126 /* Look for the `template' keyword, as a syntactic disambiguator.
29127 Return TRUE iff it is present, in which case it will be
29131 cp_parser_optional_template_keyword (cp_parser
*parser
)
29133 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29135 /* In C++98 the `template' keyword can only be used within templates;
29136 outside templates the parser can always figure out what is a
29137 template and what is not. In C++11, per the resolution of DR 468,
29138 `template' is allowed in cases where it is not strictly necessary. */
29139 if (!processing_template_decl
29140 && pedantic
&& cxx_dialect
== cxx98
)
29142 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29143 pedwarn (token
->location
, OPT_Wpedantic
,
29144 "in C++98 %<template%> (as a disambiguator) is only "
29145 "allowed within templates");
29146 /* If this part of the token stream is rescanned, the same
29147 error message would be generated. So, we purge the token
29148 from the stream. */
29149 cp_lexer_purge_token (parser
->lexer
);
29154 /* Consume the `template' keyword. */
29155 cp_lexer_consume_token (parser
->lexer
);
29162 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29163 set PARSER->SCOPE, and perform other related actions. */
29166 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29168 struct tree_check
*check_value
;
29170 /* Get the stored value. */
29171 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29172 /* Set the scope from the stored value. */
29173 parser
->scope
= saved_checks_value (check_value
);
29174 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29175 parser
->object_scope
= NULL_TREE
;
29178 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29179 encounter the end of a block before what we were looking for. */
29182 cp_parser_cache_group (cp_parser
*parser
,
29183 enum cpp_ttype end
,
29188 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29190 /* Abort a parenthesized expression if we encounter a semicolon. */
29191 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29192 && token
->type
== CPP_SEMICOLON
)
29194 /* If we've reached the end of the file, stop. */
29195 if (token
->type
== CPP_EOF
29196 || (end
!= CPP_PRAGMA_EOL
29197 && token
->type
== CPP_PRAGMA_EOL
))
29199 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29200 /* We've hit the end of an enclosing block, so there's been some
29201 kind of syntax error. */
29204 /* Consume the token. */
29205 cp_lexer_consume_token (parser
->lexer
);
29206 /* See if it starts a new group. */
29207 if (token
->type
== CPP_OPEN_BRACE
)
29209 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29210 /* In theory this should probably check end == '}', but
29211 cp_parser_save_member_function_body needs it to exit
29212 after either '}' or ')' when called with ')'. */
29216 else if (token
->type
== CPP_OPEN_PAREN
)
29218 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29219 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29222 else if (token
->type
== CPP_PRAGMA
)
29223 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29224 else if (token
->type
== end
)
29229 /* Like above, for caching a default argument or NSDMI. Both of these are
29230 terminated by a non-nested comma, but it can be unclear whether or not a
29231 comma is nested in a template argument list unless we do more parsing.
29232 In order to handle this ambiguity, when we encounter a ',' after a '<'
29233 we try to parse what follows as a parameter-declaration-list (in the
29234 case of a default argument) or a member-declarator (in the case of an
29235 NSDMI). If that succeeds, then we stop caching. */
29238 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29240 unsigned depth
= 0;
29241 int maybe_template_id
= 0;
29242 cp_token
*first_token
;
29244 tree default_argument
;
29246 /* Add tokens until we have processed the entire default
29247 argument. We add the range [first_token, token). */
29248 first_token
= cp_lexer_peek_token (parser
->lexer
);
29249 if (first_token
->type
== CPP_OPEN_BRACE
)
29251 /* For list-initialization, this is straightforward. */
29252 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29253 token
= cp_lexer_peek_token (parser
->lexer
);
29259 /* Peek at the next token. */
29260 token
= cp_lexer_peek_token (parser
->lexer
);
29261 /* What we do depends on what token we have. */
29262 switch (token
->type
)
29264 /* In valid code, a default argument must be
29265 immediately followed by a `,' `)', or `...'. */
29267 if (depth
== 0 && maybe_template_id
)
29269 /* If we've seen a '<', we might be in a
29270 template-argument-list. Until Core issue 325 is
29271 resolved, we don't know how this situation ought
29272 to be handled, so try to DTRT. We check whether
29273 what comes after the comma is a valid parameter
29274 declaration list. If it is, then the comma ends
29275 the default argument; otherwise the default
29276 argument continues. */
29277 bool error
= false;
29280 /* Set ITALP so cp_parser_parameter_declaration_list
29281 doesn't decide to commit to this parse. */
29282 bool saved_italp
= parser
->in_template_argument_list_p
;
29283 parser
->in_template_argument_list_p
= true;
29285 cp_parser_parse_tentatively (parser
);
29289 /* Parse declarators until we reach a non-comma or
29290 somthing that cannot be an initializer.
29291 Just checking whether we're looking at a single
29292 declarator is insufficient. Consider:
29293 int var = tuple<T,U>::x;
29294 The template parameter 'U' looks exactly like a
29298 int ctor_dtor_or_conv_p
;
29299 cp_lexer_consume_token (parser
->lexer
);
29300 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29301 &ctor_dtor_or_conv_p
,
29302 /*parenthesized_p=*/NULL
,
29304 /*friend_p=*/false);
29305 peek
= cp_lexer_peek_token (parser
->lexer
);
29306 if (cp_parser_error_occurred (parser
))
29309 while (peek
->type
== CPP_COMMA
);
29310 /* If we met an '=' or ';' then the original comma
29311 was the end of the NSDMI. Otherwise assume
29312 we're still in the NSDMI. */
29313 error
= (peek
->type
!= CPP_EQ
29314 && peek
->type
!= CPP_SEMICOLON
);
29318 cp_lexer_consume_token (parser
->lexer
);
29319 begin_scope (sk_function_parms
, NULL_TREE
);
29320 if (cp_parser_parameter_declaration_list (parser
)
29321 == error_mark_node
)
29323 pop_bindings_and_leave_scope ();
29325 if (!cp_parser_error_occurred (parser
) && !error
)
29327 cp_parser_abort_tentative_parse (parser
);
29329 parser
->in_template_argument_list_p
= saved_italp
;
29333 case CPP_CLOSE_PAREN
:
29335 /* If we run into a non-nested `;', `}', or `]',
29336 then the code is invalid -- but the default
29337 argument is certainly over. */
29338 case CPP_SEMICOLON
:
29339 case CPP_CLOSE_BRACE
:
29340 case CPP_CLOSE_SQUARE
:
29342 /* Handle correctly int n = sizeof ... ( p ); */
29343 && token
->type
!= CPP_ELLIPSIS
)
29345 /* Update DEPTH, if necessary. */
29346 else if (token
->type
== CPP_CLOSE_PAREN
29347 || token
->type
== CPP_CLOSE_BRACE
29348 || token
->type
== CPP_CLOSE_SQUARE
)
29352 case CPP_OPEN_PAREN
:
29353 case CPP_OPEN_SQUARE
:
29354 case CPP_OPEN_BRACE
:
29360 /* This might be the comparison operator, or it might
29361 start a template argument list. */
29362 ++maybe_template_id
;
29366 if (cxx_dialect
== cxx98
)
29368 /* Fall through for C++0x, which treats the `>>'
29369 operator like two `>' tokens in certain
29371 gcc_fallthrough ();
29376 /* This might be an operator, or it might close a
29377 template argument list. But if a previous '<'
29378 started a template argument list, this will have
29379 closed it, so we can't be in one anymore. */
29380 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29381 if (maybe_template_id
< 0)
29382 maybe_template_id
= 0;
29386 /* If we run out of tokens, issue an error message. */
29388 case CPP_PRAGMA_EOL
:
29389 error_at (token
->location
, "file ends in default argument");
29390 return error_mark_node
;
29394 /* In these cases, we should look for template-ids.
29395 For example, if the default argument is
29396 `X<int, double>()', we need to do name lookup to
29397 figure out whether or not `X' is a template; if
29398 so, the `,' does not end the default argument.
29400 That is not yet done. */
29407 /* If we've reached the end, stop. */
29411 /* Add the token to the token block. */
29412 token
= cp_lexer_consume_token (parser
->lexer
);
29415 /* Create a DEFAULT_ARG to represent the unparsed default
29417 default_argument
= make_node (DEFAULT_ARG
);
29418 DEFARG_TOKENS (default_argument
)
29419 = cp_token_cache_new (first_token
, token
);
29420 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29422 return default_argument
;
29425 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29428 defarg_location (tree default_argument
)
29430 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29431 location_t start
= tokens
->first
->location
;
29432 location_t end
= tokens
->last
->location
;
29433 return make_location (start
, start
, end
);
29436 /* Begin parsing tentatively. We always save tokens while parsing
29437 tentatively so that if the tentative parsing fails we can restore the
29441 cp_parser_parse_tentatively (cp_parser
* parser
)
29443 /* Enter a new parsing context. */
29444 parser
->context
= cp_parser_context_new (parser
->context
);
29445 /* Begin saving tokens. */
29446 cp_lexer_save_tokens (parser
->lexer
);
29447 /* In order to avoid repetitive access control error messages,
29448 access checks are queued up until we are no longer parsing
29450 push_deferring_access_checks (dk_deferred
);
29453 /* Commit to the currently active tentative parse. */
29456 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29458 cp_parser_context
*context
;
29461 /* Mark all of the levels as committed. */
29462 lexer
= parser
->lexer
;
29463 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29465 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29467 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29468 while (!cp_lexer_saving_tokens (lexer
))
29469 lexer
= lexer
->next
;
29470 cp_lexer_commit_tokens (lexer
);
29474 /* Commit to the topmost currently active tentative parse.
29476 Note that this function shouldn't be called when there are
29477 irreversible side-effects while in a tentative state. For
29478 example, we shouldn't create a permanent entry in the symbol
29479 table, or issue an error message that might not apply if the
29480 tentative parse is aborted. */
29483 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29485 cp_parser_context
*context
= parser
->context
;
29486 cp_lexer
*lexer
= parser
->lexer
;
29490 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29492 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29494 while (!cp_lexer_saving_tokens (lexer
))
29495 lexer
= lexer
->next
;
29496 cp_lexer_commit_tokens (lexer
);
29500 /* Abort the currently active tentative parse. All consumed tokens
29501 will be rolled back, and no diagnostics will be issued. */
29504 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29506 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29507 || errorcount
> 0);
29508 cp_parser_simulate_error (parser
);
29509 /* Now, pretend that we want to see if the construct was
29510 successfully parsed. */
29511 cp_parser_parse_definitely (parser
);
29514 /* Stop parsing tentatively. If a parse error has occurred, restore the
29515 token stream. Otherwise, commit to the tokens we have consumed.
29516 Returns true if no error occurred; false otherwise. */
29519 cp_parser_parse_definitely (cp_parser
* parser
)
29521 bool error_occurred
;
29522 cp_parser_context
*context
;
29524 /* Remember whether or not an error occurred, since we are about to
29525 destroy that information. */
29526 error_occurred
= cp_parser_error_occurred (parser
);
29527 /* Remove the topmost context from the stack. */
29528 context
= parser
->context
;
29529 parser
->context
= context
->next
;
29530 /* If no parse errors occurred, commit to the tentative parse. */
29531 if (!error_occurred
)
29533 /* Commit to the tokens read tentatively, unless that was
29535 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29536 cp_lexer_commit_tokens (parser
->lexer
);
29538 pop_to_parent_deferring_access_checks ();
29540 /* Otherwise, if errors occurred, roll back our state so that things
29541 are just as they were before we began the tentative parse. */
29544 cp_lexer_rollback_tokens (parser
->lexer
);
29545 pop_deferring_access_checks ();
29547 /* Add the context to the front of the free list. */
29548 context
->next
= cp_parser_context_free_list
;
29549 cp_parser_context_free_list
= context
;
29551 return !error_occurred
;
29554 /* Returns true if we are parsing tentatively and are not committed to
29555 this tentative parse. */
29558 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29560 return (cp_parser_parsing_tentatively (parser
)
29561 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29564 /* Returns nonzero iff an error has occurred during the most recent
29565 tentative parse. */
29568 cp_parser_error_occurred (cp_parser
* parser
)
29570 return (cp_parser_parsing_tentatively (parser
)
29571 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29574 /* Returns nonzero if GNU extensions are allowed. */
29577 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29579 return parser
->allow_gnu_extensions_p
;
29582 /* Objective-C++ Productions */
29585 /* Parse an Objective-C expression, which feeds into a primary-expression
29589 objc-message-expression
29590 objc-string-literal
29591 objc-encode-expression
29592 objc-protocol-expression
29593 objc-selector-expression
29595 Returns a tree representation of the expression. */
29598 cp_parser_objc_expression (cp_parser
* parser
)
29600 /* Try to figure out what kind of declaration is present. */
29601 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29605 case CPP_OPEN_SQUARE
:
29606 return cp_parser_objc_message_expression (parser
);
29608 case CPP_OBJC_STRING
:
29609 kwd
= cp_lexer_consume_token (parser
->lexer
);
29610 return objc_build_string_object (kwd
->u
.value
);
29613 switch (kwd
->keyword
)
29615 case RID_AT_ENCODE
:
29616 return cp_parser_objc_encode_expression (parser
);
29618 case RID_AT_PROTOCOL
:
29619 return cp_parser_objc_protocol_expression (parser
);
29621 case RID_AT_SELECTOR
:
29622 return cp_parser_objc_selector_expression (parser
);
29629 error_at (kwd
->location
,
29630 "misplaced %<@%D%> Objective-C++ construct",
29632 cp_parser_skip_to_end_of_block_or_statement (parser
);
29635 return error_mark_node
;
29638 /* Parse an Objective-C message expression.
29640 objc-message-expression:
29641 [ objc-message-receiver objc-message-args ]
29643 Returns a representation of an Objective-C message. */
29646 cp_parser_objc_message_expression (cp_parser
* parser
)
29648 tree receiver
, messageargs
;
29650 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29651 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29652 receiver
= cp_parser_objc_message_receiver (parser
);
29653 messageargs
= cp_parser_objc_message_args (parser
);
29654 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29655 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29657 tree result
= objc_build_message_expr (receiver
, messageargs
);
29659 /* Construct a location e.g.
29662 ranging from the '[' to the ']', with the caret at the start. */
29663 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29664 protected_set_expr_location (result
, combined_loc
);
29669 /* Parse an objc-message-receiver.
29671 objc-message-receiver:
29673 simple-type-specifier
29675 Returns a representation of the type or expression. */
29678 cp_parser_objc_message_receiver (cp_parser
* parser
)
29682 /* An Objective-C message receiver may be either (1) a type
29683 or (2) an expression. */
29684 cp_parser_parse_tentatively (parser
);
29685 rcv
= cp_parser_expression (parser
);
29687 /* If that worked out, fine. */
29688 if (cp_parser_parse_definitely (parser
))
29691 cp_parser_parse_tentatively (parser
);
29692 rcv
= cp_parser_simple_type_specifier (parser
,
29693 /*decl_specs=*/NULL
,
29694 CP_PARSER_FLAGS_NONE
);
29696 if (cp_parser_parse_definitely (parser
))
29697 return objc_get_class_reference (rcv
);
29699 cp_parser_error (parser
, "objective-c++ message receiver expected");
29700 return error_mark_node
;
29703 /* Parse the arguments and selectors comprising an Objective-C message.
29708 objc-selector-args , objc-comma-args
29710 objc-selector-args:
29711 objc-selector [opt] : assignment-expression
29712 objc-selector-args objc-selector [opt] : assignment-expression
29715 assignment-expression
29716 objc-comma-args , assignment-expression
29718 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29719 selector arguments and TREE_VALUE containing a list of comma
29723 cp_parser_objc_message_args (cp_parser
* parser
)
29725 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29726 bool maybe_unary_selector_p
= true;
29727 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29729 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29731 tree selector
= NULL_TREE
, arg
;
29733 if (token
->type
!= CPP_COLON
)
29734 selector
= cp_parser_objc_selector (parser
);
29736 /* Detect if we have a unary selector. */
29737 if (maybe_unary_selector_p
29738 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29739 return build_tree_list (selector
, NULL_TREE
);
29741 maybe_unary_selector_p
= false;
29742 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29743 arg
= cp_parser_assignment_expression (parser
);
29746 = chainon (sel_args
,
29747 build_tree_list (selector
, arg
));
29749 token
= cp_lexer_peek_token (parser
->lexer
);
29752 /* Handle non-selector arguments, if any. */
29753 while (token
->type
== CPP_COMMA
)
29757 cp_lexer_consume_token (parser
->lexer
);
29758 arg
= cp_parser_assignment_expression (parser
);
29761 = chainon (addl_args
,
29762 build_tree_list (NULL_TREE
, arg
));
29764 token
= cp_lexer_peek_token (parser
->lexer
);
29767 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29769 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29770 return build_tree_list (error_mark_node
, error_mark_node
);
29773 return build_tree_list (sel_args
, addl_args
);
29776 /* Parse an Objective-C encode expression.
29778 objc-encode-expression:
29779 @encode objc-typename
29781 Returns an encoded representation of the type argument. */
29784 cp_parser_objc_encode_expression (cp_parser
* parser
)
29788 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29790 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29791 matching_parens parens
;
29792 parens
.require_open (parser
);
29793 token
= cp_lexer_peek_token (parser
->lexer
);
29794 type
= complete_type (cp_parser_type_id (parser
));
29795 parens
.require_close (parser
);
29799 error_at (token
->location
,
29800 "%<@encode%> must specify a type as an argument");
29801 return error_mark_node
;
29804 /* This happens if we find @encode(T) (where T is a template
29805 typename or something dependent on a template typename) when
29806 parsing a template. In that case, we can't compile it
29807 immediately, but we rather create an AT_ENCODE_EXPR which will
29808 need to be instantiated when the template is used.
29810 if (dependent_type_p (type
))
29812 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29813 TREE_READONLY (value
) = 1;
29818 /* Build a location of the form:
29821 with caret==start at the @ token, finishing at the close paren. */
29822 location_t combined_loc
29823 = make_location (start_loc
, start_loc
,
29824 cp_lexer_previous_token (parser
->lexer
)->location
);
29826 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29829 /* Parse an Objective-C @defs expression. */
29832 cp_parser_objc_defs_expression (cp_parser
*parser
)
29836 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29837 matching_parens parens
;
29838 parens
.require_open (parser
);
29839 name
= cp_parser_identifier (parser
);
29840 parens
.require_close (parser
);
29842 return objc_get_class_ivars (name
);
29845 /* Parse an Objective-C protocol expression.
29847 objc-protocol-expression:
29848 @protocol ( identifier )
29850 Returns a representation of the protocol expression. */
29853 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29856 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29858 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29859 matching_parens parens
;
29860 parens
.require_open (parser
);
29861 proto
= cp_parser_identifier (parser
);
29862 parens
.require_close (parser
);
29864 /* Build a location of the form:
29867 with caret==start at the @ token, finishing at the close paren. */
29868 location_t combined_loc
29869 = make_location (start_loc
, start_loc
,
29870 cp_lexer_previous_token (parser
->lexer
)->location
);
29871 tree result
= objc_build_protocol_expr (proto
);
29872 protected_set_expr_location (result
, combined_loc
);
29876 /* Parse an Objective-C selector expression.
29878 objc-selector-expression:
29879 @selector ( objc-method-signature )
29881 objc-method-signature:
29887 objc-selector-seq objc-selector :
29889 Returns a representation of the method selector. */
29892 cp_parser_objc_selector_expression (cp_parser
* parser
)
29894 tree sel_seq
= NULL_TREE
;
29895 bool maybe_unary_selector_p
= true;
29897 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29899 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29900 matching_parens parens
;
29901 parens
.require_open (parser
);
29902 token
= cp_lexer_peek_token (parser
->lexer
);
29904 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29905 || token
->type
== CPP_SCOPE
)
29907 tree selector
= NULL_TREE
;
29909 if (token
->type
!= CPP_COLON
29910 || token
->type
== CPP_SCOPE
)
29911 selector
= cp_parser_objc_selector (parser
);
29913 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29914 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29916 /* Detect if we have a unary selector. */
29917 if (maybe_unary_selector_p
)
29919 sel_seq
= selector
;
29920 goto finish_selector
;
29924 cp_parser_error (parser
, "expected %<:%>");
29927 maybe_unary_selector_p
= false;
29928 token
= cp_lexer_consume_token (parser
->lexer
);
29930 if (token
->type
== CPP_SCOPE
)
29933 = chainon (sel_seq
,
29934 build_tree_list (selector
, NULL_TREE
));
29936 = chainon (sel_seq
,
29937 build_tree_list (NULL_TREE
, NULL_TREE
));
29941 = chainon (sel_seq
,
29942 build_tree_list (selector
, NULL_TREE
));
29944 token
= cp_lexer_peek_token (parser
->lexer
);
29948 parens
.require_close (parser
);
29951 /* Build a location of the form:
29954 with caret==start at the @ token, finishing at the close paren. */
29955 location_t combined_loc
29956 = make_location (loc
, loc
,
29957 cp_lexer_previous_token (parser
->lexer
)->location
);
29958 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29959 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29960 protected_set_expr_location (result
, combined_loc
);
29964 /* Parse a list of identifiers.
29966 objc-identifier-list:
29968 objc-identifier-list , identifier
29970 Returns a TREE_LIST of identifier nodes. */
29973 cp_parser_objc_identifier_list (cp_parser
* parser
)
29979 identifier
= cp_parser_identifier (parser
);
29980 if (identifier
== error_mark_node
)
29981 return error_mark_node
;
29983 list
= build_tree_list (NULL_TREE
, identifier
);
29984 sep
= cp_lexer_peek_token (parser
->lexer
);
29986 while (sep
->type
== CPP_COMMA
)
29988 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29989 identifier
= cp_parser_identifier (parser
);
29990 if (identifier
== error_mark_node
)
29993 list
= chainon (list
, build_tree_list (NULL_TREE
,
29995 sep
= cp_lexer_peek_token (parser
->lexer
);
30001 /* Parse an Objective-C alias declaration.
30003 objc-alias-declaration:
30004 @compatibility_alias identifier identifier ;
30006 This function registers the alias mapping with the Objective-C front end.
30007 It returns nothing. */
30010 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30014 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30015 alias
= cp_parser_identifier (parser
);
30016 orig
= cp_parser_identifier (parser
);
30017 objc_declare_alias (alias
, orig
);
30018 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30021 /* Parse an Objective-C class forward-declaration.
30023 objc-class-declaration:
30024 @class objc-identifier-list ;
30026 The function registers the forward declarations with the Objective-C
30027 front end. It returns nothing. */
30030 cp_parser_objc_class_declaration (cp_parser
* parser
)
30032 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30037 id
= cp_parser_identifier (parser
);
30038 if (id
== error_mark_node
)
30041 objc_declare_class (id
);
30043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30044 cp_lexer_consume_token (parser
->lexer
);
30048 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30051 /* Parse a list of Objective-C protocol references.
30053 objc-protocol-refs-opt:
30054 objc-protocol-refs [opt]
30056 objc-protocol-refs:
30057 < objc-identifier-list >
30059 Returns a TREE_LIST of identifiers, if any. */
30062 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30064 tree protorefs
= NULL_TREE
;
30066 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30068 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30069 protorefs
= cp_parser_objc_identifier_list (parser
);
30070 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30076 /* Parse a Objective-C visibility specification. */
30079 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30081 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30083 switch (vis
->keyword
)
30085 case RID_AT_PRIVATE
:
30086 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30088 case RID_AT_PROTECTED
:
30089 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30091 case RID_AT_PUBLIC
:
30092 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30094 case RID_AT_PACKAGE
:
30095 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30101 /* Eat '@private'/'@protected'/'@public'. */
30102 cp_lexer_consume_token (parser
->lexer
);
30105 /* Parse an Objective-C method type. Return 'true' if it is a class
30106 (+) method, and 'false' if it is an instance (-) method. */
30109 cp_parser_objc_method_type (cp_parser
* parser
)
30111 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30117 /* Parse an Objective-C protocol qualifier. */
30120 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30122 tree quals
= NULL_TREE
, node
;
30123 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30125 node
= token
->u
.value
;
30127 while (node
&& identifier_p (node
)
30128 && (node
== ridpointers
[(int) RID_IN
]
30129 || node
== ridpointers
[(int) RID_OUT
]
30130 || node
== ridpointers
[(int) RID_INOUT
]
30131 || node
== ridpointers
[(int) RID_BYCOPY
]
30132 || node
== ridpointers
[(int) RID_BYREF
]
30133 || node
== ridpointers
[(int) RID_ONEWAY
]))
30135 quals
= tree_cons (NULL_TREE
, node
, quals
);
30136 cp_lexer_consume_token (parser
->lexer
);
30137 token
= cp_lexer_peek_token (parser
->lexer
);
30138 node
= token
->u
.value
;
30144 /* Parse an Objective-C typename. */
30147 cp_parser_objc_typename (cp_parser
* parser
)
30149 tree type_name
= NULL_TREE
;
30151 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30153 tree proto_quals
, cp_type
= NULL_TREE
;
30155 matching_parens parens
;
30156 parens
.consume_open (parser
); /* Eat '('. */
30157 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30159 /* An ObjC type name may consist of just protocol qualifiers, in which
30160 case the type shall default to 'id'. */
30161 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30163 cp_type
= cp_parser_type_id (parser
);
30165 /* If the type could not be parsed, an error has already
30166 been produced. For error recovery, behave as if it had
30167 not been specified, which will use the default type
30169 if (cp_type
== error_mark_node
)
30171 cp_type
= NULL_TREE
;
30172 /* We need to skip to the closing parenthesis as
30173 cp_parser_type_id() does not seem to do it for
30175 cp_parser_skip_to_closing_parenthesis (parser
,
30176 /*recovering=*/true,
30177 /*or_comma=*/false,
30178 /*consume_paren=*/false);
30182 parens
.require_close (parser
);
30183 type_name
= build_tree_list (proto_quals
, cp_type
);
30189 /* Check to see if TYPE refers to an Objective-C selector name. */
30192 cp_parser_objc_selector_p (enum cpp_ttype type
)
30194 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30195 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30196 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30197 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30198 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30201 /* Parse an Objective-C selector. */
30204 cp_parser_objc_selector (cp_parser
* parser
)
30206 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30208 if (!cp_parser_objc_selector_p (token
->type
))
30210 error_at (token
->location
, "invalid Objective-C++ selector name");
30211 return error_mark_node
;
30214 /* C++ operator names are allowed to appear in ObjC selectors. */
30215 switch (token
->type
)
30217 case CPP_AND_AND
: return get_identifier ("and");
30218 case CPP_AND_EQ
: return get_identifier ("and_eq");
30219 case CPP_AND
: return get_identifier ("bitand");
30220 case CPP_OR
: return get_identifier ("bitor");
30221 case CPP_COMPL
: return get_identifier ("compl");
30222 case CPP_NOT
: return get_identifier ("not");
30223 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30224 case CPP_OR_OR
: return get_identifier ("or");
30225 case CPP_OR_EQ
: return get_identifier ("or_eq");
30226 case CPP_XOR
: return get_identifier ("xor");
30227 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30228 default: return token
->u
.value
;
30232 /* Parse an Objective-C params list. */
30235 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30237 tree params
= NULL_TREE
;
30238 bool maybe_unary_selector_p
= true;
30239 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30241 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30243 tree selector
= NULL_TREE
, type_name
, identifier
;
30244 tree parm_attr
= NULL_TREE
;
30246 if (token
->keyword
== RID_ATTRIBUTE
)
30249 if (token
->type
!= CPP_COLON
)
30250 selector
= cp_parser_objc_selector (parser
);
30252 /* Detect if we have a unary selector. */
30253 if (maybe_unary_selector_p
30254 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30256 params
= selector
; /* Might be followed by attributes. */
30260 maybe_unary_selector_p
= false;
30261 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30263 /* Something went quite wrong. There should be a colon
30264 here, but there is not. Stop parsing parameters. */
30267 type_name
= cp_parser_objc_typename (parser
);
30268 /* New ObjC allows attributes on parameters too. */
30269 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30270 parm_attr
= cp_parser_attributes_opt (parser
);
30271 identifier
= cp_parser_identifier (parser
);
30275 objc_build_keyword_decl (selector
,
30280 token
= cp_lexer_peek_token (parser
->lexer
);
30283 if (params
== NULL_TREE
)
30285 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30286 return error_mark_node
;
30289 /* We allow tail attributes for the method. */
30290 if (token
->keyword
== RID_ATTRIBUTE
)
30292 *attributes
= cp_parser_attributes_opt (parser
);
30293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30294 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30296 cp_parser_error (parser
,
30297 "method attributes must be specified at the end");
30298 return error_mark_node
;
30301 if (params
== NULL_TREE
)
30303 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30304 return error_mark_node
;
30309 /* Parse the non-keyword Objective-C params. */
30312 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30315 tree params
= make_node (TREE_LIST
);
30316 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30317 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30319 while (token
->type
== CPP_COMMA
)
30321 cp_parameter_declarator
*parmdecl
;
30324 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30325 token
= cp_lexer_peek_token (parser
->lexer
);
30327 if (token
->type
== CPP_ELLIPSIS
)
30329 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30331 token
= cp_lexer_peek_token (parser
->lexer
);
30335 /* TODO: parse attributes for tail parameters. */
30336 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30337 parm
= grokdeclarator (parmdecl
->declarator
,
30338 &parmdecl
->decl_specifiers
,
30339 PARM
, /*initialized=*/0,
30340 /*attrlist=*/NULL
);
30342 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30343 token
= cp_lexer_peek_token (parser
->lexer
);
30346 /* We allow tail attributes for the method. */
30347 if (token
->keyword
== RID_ATTRIBUTE
)
30349 if (*attributes
== NULL_TREE
)
30351 *attributes
= cp_parser_attributes_opt (parser
);
30352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30353 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30357 /* We have an error, but parse the attributes, so that we can
30359 *attributes
= cp_parser_attributes_opt (parser
);
30361 cp_parser_error (parser
,
30362 "method attributes must be specified at the end");
30363 return error_mark_node
;
30369 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30372 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30376 /* If the next token is `extern' and the following token is a string
30377 literal, then we have a linkage specification. */
30378 if (token
->keyword
== RID_EXTERN
30379 && cp_parser_is_pure_string_literal
30380 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30381 cp_parser_linkage_specification (parser
);
30382 /* Handle #pragma, if any. */
30383 else if (token
->type
== CPP_PRAGMA
)
30384 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30385 /* Allow stray semicolons. */
30386 else if (token
->type
== CPP_SEMICOLON
)
30387 cp_lexer_consume_token (parser
->lexer
);
30388 /* Mark methods as optional or required, when building protocols. */
30389 else if (token
->keyword
== RID_AT_OPTIONAL
)
30391 cp_lexer_consume_token (parser
->lexer
);
30392 objc_set_method_opt (true);
30394 else if (token
->keyword
== RID_AT_REQUIRED
)
30396 cp_lexer_consume_token (parser
->lexer
);
30397 objc_set_method_opt (false);
30399 else if (token
->keyword
== RID_NAMESPACE
)
30400 cp_parser_namespace_definition (parser
);
30401 /* Other stray characters must generate errors. */
30402 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30404 cp_lexer_consume_token (parser
->lexer
);
30405 error ("stray %qs between Objective-C++ methods",
30406 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30408 /* Finally, try to parse a block-declaration, or a function-definition. */
30410 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30413 /* Parse a method signature. */
30416 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30418 tree rettype
, kwdparms
, optparms
;
30419 bool ellipsis
= false;
30420 bool is_class_method
;
30422 is_class_method
= cp_parser_objc_method_type (parser
);
30423 rettype
= cp_parser_objc_typename (parser
);
30424 *attributes
= NULL_TREE
;
30425 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30426 if (kwdparms
== error_mark_node
)
30427 return error_mark_node
;
30428 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30429 if (optparms
== error_mark_node
)
30430 return error_mark_node
;
30432 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30436 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30439 cp_lexer_save_tokens (parser
->lexer
);
30440 tattr
= cp_parser_attributes_opt (parser
);
30441 gcc_assert (tattr
) ;
30443 /* If the attributes are followed by a method introducer, this is not allowed.
30444 Dump the attributes and flag the situation. */
30445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30446 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30449 /* Otherwise, the attributes introduce some interstitial code, possibly so
30450 rewind to allow that check. */
30451 cp_lexer_rollback_tokens (parser
->lexer
);
30455 /* Parse an Objective-C method prototype list. */
30458 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30460 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30462 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30464 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30466 tree attributes
, sig
;
30467 bool is_class_method
;
30468 if (token
->type
== CPP_PLUS
)
30469 is_class_method
= true;
30471 is_class_method
= false;
30472 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30473 if (sig
== error_mark_node
)
30475 cp_parser_skip_to_end_of_block_or_statement (parser
);
30476 token
= cp_lexer_peek_token (parser
->lexer
);
30479 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30480 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30482 else if (token
->keyword
== RID_AT_PROPERTY
)
30483 cp_parser_objc_at_property_declaration (parser
);
30484 else if (token
->keyword
== RID_ATTRIBUTE
30485 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30486 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30488 "prefix attributes are ignored for methods");
30490 /* Allow for interspersed non-ObjC++ code. */
30491 cp_parser_objc_interstitial_code (parser
);
30493 token
= cp_lexer_peek_token (parser
->lexer
);
30496 if (token
->type
!= CPP_EOF
)
30497 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30499 cp_parser_error (parser
, "expected %<@end%>");
30501 objc_finish_interface ();
30504 /* Parse an Objective-C method definition list. */
30507 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30509 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30511 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30515 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30518 tree sig
, attribute
;
30519 bool is_class_method
;
30520 if (token
->type
== CPP_PLUS
)
30521 is_class_method
= true;
30523 is_class_method
= false;
30524 push_deferring_access_checks (dk_deferred
);
30525 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30526 if (sig
== error_mark_node
)
30528 cp_parser_skip_to_end_of_block_or_statement (parser
);
30529 token
= cp_lexer_peek_token (parser
->lexer
);
30532 objc_start_method_definition (is_class_method
, sig
, attribute
,
30535 /* For historical reasons, we accept an optional semicolon. */
30536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30537 cp_lexer_consume_token (parser
->lexer
);
30539 ptk
= cp_lexer_peek_token (parser
->lexer
);
30540 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30541 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30543 perform_deferred_access_checks (tf_warning_or_error
);
30544 stop_deferring_access_checks ();
30545 meth
= cp_parser_function_definition_after_declarator (parser
,
30547 pop_deferring_access_checks ();
30548 objc_finish_method_definition (meth
);
30551 /* The following case will be removed once @synthesize is
30552 completely implemented. */
30553 else if (token
->keyword
== RID_AT_PROPERTY
)
30554 cp_parser_objc_at_property_declaration (parser
);
30555 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30556 cp_parser_objc_at_synthesize_declaration (parser
);
30557 else if (token
->keyword
== RID_AT_DYNAMIC
)
30558 cp_parser_objc_at_dynamic_declaration (parser
);
30559 else if (token
->keyword
== RID_ATTRIBUTE
30560 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30561 warning_at (token
->location
, OPT_Wattributes
,
30562 "prefix attributes are ignored for methods");
30564 /* Allow for interspersed non-ObjC++ code. */
30565 cp_parser_objc_interstitial_code (parser
);
30567 token
= cp_lexer_peek_token (parser
->lexer
);
30570 if (token
->type
!= CPP_EOF
)
30571 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30573 cp_parser_error (parser
, "expected %<@end%>");
30575 objc_finish_implementation ();
30578 /* Parse Objective-C ivars. */
30581 cp_parser_objc_class_ivars (cp_parser
* parser
)
30583 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30585 if (token
->type
!= CPP_OPEN_BRACE
)
30586 return; /* No ivars specified. */
30588 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30589 token
= cp_lexer_peek_token (parser
->lexer
);
30591 while (token
->type
!= CPP_CLOSE_BRACE
30592 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30594 cp_decl_specifier_seq declspecs
;
30595 int decl_class_or_enum_p
;
30596 tree prefix_attributes
;
30598 cp_parser_objc_visibility_spec (parser
);
30600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30603 cp_parser_decl_specifier_seq (parser
,
30604 CP_PARSER_FLAGS_OPTIONAL
,
30606 &decl_class_or_enum_p
);
30608 /* auto, register, static, extern, mutable. */
30609 if (declspecs
.storage_class
!= sc_none
)
30611 cp_parser_error (parser
, "invalid type for instance variable");
30612 declspecs
.storage_class
= sc_none
;
30615 /* thread_local. */
30616 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30618 cp_parser_error (parser
, "invalid type for instance variable");
30619 declspecs
.locations
[ds_thread
] = 0;
30623 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30625 cp_parser_error (parser
, "invalid type for instance variable");
30626 declspecs
.locations
[ds_typedef
] = 0;
30629 prefix_attributes
= declspecs
.attributes
;
30630 declspecs
.attributes
= NULL_TREE
;
30632 /* Keep going until we hit the `;' at the end of the
30634 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30636 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30637 cp_declarator
*declarator
= NULL
;
30638 int ctor_dtor_or_conv_p
;
30640 /* Check for a (possibly unnamed) bitfield declaration. */
30641 token
= cp_lexer_peek_token (parser
->lexer
);
30642 if (token
->type
== CPP_COLON
)
30645 if (token
->type
== CPP_NAME
30646 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30649 /* Get the name of the bitfield. */
30650 declarator
= make_id_declarator (NULL_TREE
,
30651 cp_parser_identifier (parser
),
30652 sfk_none
, token
->location
);
30655 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30656 /* Get the width of the bitfield. */
30658 = cp_parser_constant_expression (parser
);
30662 /* Parse the declarator. */
30664 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30665 &ctor_dtor_or_conv_p
,
30666 /*parenthesized_p=*/NULL
,
30667 /*member_p=*/false,
30668 /*friend_p=*/false);
30671 /* Look for attributes that apply to the ivar. */
30672 attributes
= cp_parser_attributes_opt (parser
);
30673 /* Remember which attributes are prefix attributes and
30675 first_attribute
= attributes
;
30676 /* Combine the attributes. */
30677 attributes
= attr_chainon (prefix_attributes
, attributes
);
30680 /* Create the bitfield declaration. */
30681 decl
= grokbitfield (declarator
, &declspecs
,
30682 width
, NULL_TREE
, attributes
);
30684 decl
= grokfield (declarator
, &declspecs
,
30685 NULL_TREE
, /*init_const_expr_p=*/false,
30686 NULL_TREE
, attributes
);
30688 /* Add the instance variable. */
30689 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30690 objc_add_instance_variable (decl
);
30692 /* Reset PREFIX_ATTRIBUTES. */
30693 if (attributes
!= error_mark_node
)
30695 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30696 attributes
= TREE_CHAIN (attributes
);
30698 TREE_CHAIN (attributes
) = NULL_TREE
;
30701 token
= cp_lexer_peek_token (parser
->lexer
);
30703 if (token
->type
== CPP_COMMA
)
30705 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30711 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30712 token
= cp_lexer_peek_token (parser
->lexer
);
30715 if (token
->keyword
== RID_AT_END
)
30716 cp_parser_error (parser
, "expected %<}%>");
30718 /* Do not consume the RID_AT_END, so it will be read again as terminating
30719 the @interface of @implementation. */
30720 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30721 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30723 /* For historical reasons, we accept an optional semicolon. */
30724 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30725 cp_lexer_consume_token (parser
->lexer
);
30728 /* Parse an Objective-C protocol declaration. */
30731 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30733 tree proto
, protorefs
;
30736 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30739 tok
= cp_lexer_peek_token (parser
->lexer
);
30740 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30741 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30745 /* See if we have a forward declaration or a definition. */
30746 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30748 /* Try a forward declaration first. */
30749 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30755 id
= cp_parser_identifier (parser
);
30756 if (id
== error_mark_node
)
30759 objc_declare_protocol (id
, attributes
);
30761 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30762 cp_lexer_consume_token (parser
->lexer
);
30766 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30769 /* Ok, we got a full-fledged definition (or at least should). */
30772 proto
= cp_parser_identifier (parser
);
30773 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30774 objc_start_protocol (proto
, protorefs
, attributes
);
30775 cp_parser_objc_method_prototype_list (parser
);
30779 /* Parse an Objective-C superclass or category. */
30782 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30785 tree
*categ
, bool *is_class_extension
)
30787 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30789 *super
= *categ
= NULL_TREE
;
30790 *is_class_extension
= false;
30791 if (next
->type
== CPP_COLON
)
30793 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30794 *super
= cp_parser_identifier (parser
);
30796 else if (next
->type
== CPP_OPEN_PAREN
)
30798 matching_parens parens
;
30799 parens
.consume_open (parser
); /* Eat '('. */
30801 /* If there is no category name, and this is an @interface, we
30802 have a class extension. */
30803 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30805 *categ
= NULL_TREE
;
30806 *is_class_extension
= true;
30809 *categ
= cp_parser_identifier (parser
);
30811 parens
.require_close (parser
);
30815 /* Parse an Objective-C class interface. */
30818 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30820 tree name
, super
, categ
, protos
;
30821 bool is_class_extension
;
30823 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30824 name
= cp_parser_identifier (parser
);
30825 if (name
== error_mark_node
)
30827 /* It's hard to recover because even if valid @interface stuff
30828 is to follow, we can't compile it (or validate it) if we
30829 don't even know which class it refers to. Let's assume this
30830 was a stray '@interface' token in the stream and skip it.
30834 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30835 &is_class_extension
);
30836 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30838 /* We have either a class or a category on our hands. */
30839 if (categ
|| is_class_extension
)
30840 objc_start_category_interface (name
, categ
, protos
, attributes
);
30843 objc_start_class_interface (name
, super
, protos
, attributes
);
30844 /* Handle instance variable declarations, if any. */
30845 cp_parser_objc_class_ivars (parser
);
30846 objc_continue_interface ();
30849 cp_parser_objc_method_prototype_list (parser
);
30852 /* Parse an Objective-C class implementation. */
30855 cp_parser_objc_class_implementation (cp_parser
* parser
)
30857 tree name
, super
, categ
;
30858 bool is_class_extension
;
30860 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30861 name
= cp_parser_identifier (parser
);
30862 if (name
== error_mark_node
)
30864 /* It's hard to recover because even if valid @implementation
30865 stuff is to follow, we can't compile it (or validate it) if
30866 we don't even know which class it refers to. Let's assume
30867 this was a stray '@implementation' token in the stream and
30872 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30873 &is_class_extension
);
30875 /* We have either a class or a category on our hands. */
30877 objc_start_category_implementation (name
, categ
);
30880 objc_start_class_implementation (name
, super
);
30881 /* Handle instance variable declarations, if any. */
30882 cp_parser_objc_class_ivars (parser
);
30883 objc_continue_implementation ();
30886 cp_parser_objc_method_definition_list (parser
);
30889 /* Consume the @end token and finish off the implementation. */
30892 cp_parser_objc_end_implementation (cp_parser
* parser
)
30894 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30895 objc_finish_implementation ();
30898 /* Parse an Objective-C declaration. */
30901 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30903 /* Try to figure out what kind of declaration is present. */
30904 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30907 switch (kwd
->keyword
)
30912 error_at (kwd
->location
, "attributes may not be specified before"
30913 " the %<@%D%> Objective-C++ keyword",
30917 case RID_AT_IMPLEMENTATION
:
30918 warning_at (kwd
->location
, OPT_Wattributes
,
30919 "prefix attributes are ignored before %<@%D%>",
30926 switch (kwd
->keyword
)
30929 cp_parser_objc_alias_declaration (parser
);
30932 cp_parser_objc_class_declaration (parser
);
30934 case RID_AT_PROTOCOL
:
30935 cp_parser_objc_protocol_declaration (parser
, attributes
);
30937 case RID_AT_INTERFACE
:
30938 cp_parser_objc_class_interface (parser
, attributes
);
30940 case RID_AT_IMPLEMENTATION
:
30941 cp_parser_objc_class_implementation (parser
);
30944 cp_parser_objc_end_implementation (parser
);
30947 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30949 cp_parser_skip_to_end_of_block_or_statement (parser
);
30953 /* Parse an Objective-C try-catch-finally statement.
30955 objc-try-catch-finally-stmt:
30956 @try compound-statement objc-catch-clause-seq [opt]
30957 objc-finally-clause [opt]
30959 objc-catch-clause-seq:
30960 objc-catch-clause objc-catch-clause-seq [opt]
30963 @catch ( objc-exception-declaration ) compound-statement
30965 objc-finally-clause:
30966 @finally compound-statement
30968 objc-exception-declaration:
30969 parameter-declaration
30972 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30976 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30977 for C. Keep them in sync. */
30980 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30982 location_t location
;
30985 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30986 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30987 objc_maybe_warn_exceptions (location
);
30988 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30989 node, lest it get absorbed into the surrounding block. */
30990 stmt
= push_stmt_list ();
30991 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30992 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30994 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30996 cp_parameter_declarator
*parm
;
30997 tree parameter_declaration
= error_mark_node
;
30998 bool seen_open_paren
= false;
30999 matching_parens parens
;
31001 cp_lexer_consume_token (parser
->lexer
);
31002 if (parens
.require_open (parser
))
31003 seen_open_paren
= true;
31004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31006 /* We have "@catch (...)" (where the '...' are literally
31007 what is in the code). Skip the '...'.
31008 parameter_declaration is set to NULL_TREE, and
31009 objc_being_catch_clauses() knows that that means
31011 cp_lexer_consume_token (parser
->lexer
);
31012 parameter_declaration
= NULL_TREE
;
31016 /* We have "@catch (NSException *exception)" or something
31017 like that. Parse the parameter declaration. */
31018 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
31020 parameter_declaration
= error_mark_node
;
31022 parameter_declaration
= grokdeclarator (parm
->declarator
,
31023 &parm
->decl_specifiers
,
31024 PARM
, /*initialized=*/0,
31025 /*attrlist=*/NULL
);
31027 if (seen_open_paren
)
31028 parens
.require_close (parser
);
31031 /* If there was no open parenthesis, we are recovering from
31032 an error, and we are trying to figure out what mistake
31033 the user has made. */
31035 /* If there is an immediate closing parenthesis, the user
31036 probably forgot the opening one (ie, they typed "@catch
31037 NSException *e)". Parse the closing parenthesis and keep
31039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31040 cp_lexer_consume_token (parser
->lexer
);
31042 /* If these is no immediate closing parenthesis, the user
31043 probably doesn't know that parenthesis are required at
31044 all (ie, they typed "@catch NSException *e"). So, just
31045 forget about the closing parenthesis and keep going. */
31047 objc_begin_catch_clause (parameter_declaration
);
31048 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31049 objc_finish_catch_clause ();
31051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31053 cp_lexer_consume_token (parser
->lexer
);
31054 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31055 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31056 node, lest it get absorbed into the surrounding block. */
31057 stmt
= push_stmt_list ();
31058 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31059 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31062 return objc_finish_try_stmt ();
31065 /* Parse an Objective-C synchronized statement.
31067 objc-synchronized-stmt:
31068 @synchronized ( expression ) compound-statement
31070 Returns NULL_TREE. */
31073 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31075 location_t location
;
31078 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31080 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31081 objc_maybe_warn_exceptions (location
);
31082 matching_parens parens
;
31083 parens
.require_open (parser
);
31084 lock
= cp_parser_expression (parser
);
31085 parens
.require_close (parser
);
31087 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31088 node, lest it get absorbed into the surrounding block. */
31089 stmt
= push_stmt_list ();
31090 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31092 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31095 /* Parse an Objective-C throw statement.
31098 @throw assignment-expression [opt] ;
31100 Returns a constructed '@throw' statement. */
31103 cp_parser_objc_throw_statement (cp_parser
*parser
)
31105 tree expr
= NULL_TREE
;
31106 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31108 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31110 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31111 expr
= cp_parser_expression (parser
);
31113 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31115 return objc_build_throw_stmt (loc
, expr
);
31118 /* Parse an Objective-C statement. */
31121 cp_parser_objc_statement (cp_parser
* parser
)
31123 /* Try to figure out what kind of declaration is present. */
31124 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31126 switch (kwd
->keyword
)
31129 return cp_parser_objc_try_catch_finally_statement (parser
);
31130 case RID_AT_SYNCHRONIZED
:
31131 return cp_parser_objc_synchronized_statement (parser
);
31133 return cp_parser_objc_throw_statement (parser
);
31135 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31137 cp_parser_skip_to_end_of_block_or_statement (parser
);
31140 return error_mark_node
;
31143 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31144 look ahead to see if an objc keyword follows the attributes. This
31145 is to detect the use of prefix attributes on ObjC @interface and
31149 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31151 cp_lexer_save_tokens (parser
->lexer
);
31152 *attrib
= cp_parser_attributes_opt (parser
);
31153 gcc_assert (*attrib
);
31154 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31156 cp_lexer_commit_tokens (parser
->lexer
);
31159 cp_lexer_rollback_tokens (parser
->lexer
);
31163 /* This routine is a minimal replacement for
31164 c_parser_struct_declaration () used when parsing the list of
31165 types/names or ObjC++ properties. For example, when parsing the
31168 @property (readonly) int a, b, c;
31170 this function is responsible for parsing "int a, int b, int c" and
31171 returning the declarations as CHAIN of DECLs.
31173 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31174 similar parsing. */
31176 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31178 tree decls
= NULL_TREE
;
31179 cp_decl_specifier_seq declspecs
;
31180 int decl_class_or_enum_p
;
31181 tree prefix_attributes
;
31183 cp_parser_decl_specifier_seq (parser
,
31184 CP_PARSER_FLAGS_NONE
,
31186 &decl_class_or_enum_p
);
31188 if (declspecs
.type
== error_mark_node
)
31189 return error_mark_node
;
31191 /* auto, register, static, extern, mutable. */
31192 if (declspecs
.storage_class
!= sc_none
)
31194 cp_parser_error (parser
, "invalid type for property");
31195 declspecs
.storage_class
= sc_none
;
31198 /* thread_local. */
31199 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31201 cp_parser_error (parser
, "invalid type for property");
31202 declspecs
.locations
[ds_thread
] = 0;
31206 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31208 cp_parser_error (parser
, "invalid type for property");
31209 declspecs
.locations
[ds_typedef
] = 0;
31212 prefix_attributes
= declspecs
.attributes
;
31213 declspecs
.attributes
= NULL_TREE
;
31215 /* Keep going until we hit the `;' at the end of the declaration. */
31216 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31218 tree attributes
, first_attribute
, decl
;
31219 cp_declarator
*declarator
;
31222 /* Parse the declarator. */
31223 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31224 NULL
, NULL
, false, false);
31226 /* Look for attributes that apply to the ivar. */
31227 attributes
= cp_parser_attributes_opt (parser
);
31228 /* Remember which attributes are prefix attributes and
31230 first_attribute
= attributes
;
31231 /* Combine the attributes. */
31232 attributes
= attr_chainon (prefix_attributes
, attributes
);
31234 decl
= grokfield (declarator
, &declspecs
,
31235 NULL_TREE
, /*init_const_expr_p=*/false,
31236 NULL_TREE
, attributes
);
31238 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31239 return error_mark_node
;
31241 /* Reset PREFIX_ATTRIBUTES. */
31242 if (attributes
!= error_mark_node
)
31244 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31245 attributes
= TREE_CHAIN (attributes
);
31247 TREE_CHAIN (attributes
) = NULL_TREE
;
31250 DECL_CHAIN (decl
) = decls
;
31253 token
= cp_lexer_peek_token (parser
->lexer
);
31254 if (token
->type
== CPP_COMMA
)
31256 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31265 /* Parse an Objective-C @property declaration. The syntax is:
31267 objc-property-declaration:
31268 '@property' objc-property-attributes[opt] struct-declaration ;
31270 objc-property-attributes:
31271 '(' objc-property-attribute-list ')'
31273 objc-property-attribute-list:
31274 objc-property-attribute
31275 objc-property-attribute-list, objc-property-attribute
31277 objc-property-attribute
31278 'getter' = identifier
31279 'setter' = identifier
31288 @property NSString *name;
31289 @property (readonly) id object;
31290 @property (retain, nonatomic, getter=getTheName) id name;
31291 @property int a, b, c;
31293 PS: This function is identical to
31294 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31296 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31298 /* The following variables hold the attributes of the properties as
31299 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31300 seen. When we see an attribute, we set them to 'true' (if they
31301 are boolean properties) or to the identifier (if they have an
31302 argument, ie, for getter and setter). Note that here we only
31303 parse the list of attributes, check the syntax and accumulate the
31304 attributes that we find. objc_add_property_declaration() will
31305 then process the information. */
31306 bool property_assign
= false;
31307 bool property_copy
= false;
31308 tree property_getter_ident
= NULL_TREE
;
31309 bool property_nonatomic
= false;
31310 bool property_readonly
= false;
31311 bool property_readwrite
= false;
31312 bool property_retain
= false;
31313 tree property_setter_ident
= NULL_TREE
;
31315 /* 'properties' is the list of properties that we read. Usually a
31316 single one, but maybe more (eg, in "@property int a, b, c;" there
31321 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31323 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31325 /* Parse the optional attribute list... */
31326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31329 matching_parens parens
;
31330 parens
.consume_open (parser
);
31334 bool syntax_error
= false;
31335 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31338 if (token
->type
!= CPP_NAME
)
31340 cp_parser_error (parser
, "expected identifier");
31343 keyword
= C_RID_CODE (token
->u
.value
);
31344 cp_lexer_consume_token (parser
->lexer
);
31347 case RID_ASSIGN
: property_assign
= true; break;
31348 case RID_COPY
: property_copy
= true; break;
31349 case RID_NONATOMIC
: property_nonatomic
= true; break;
31350 case RID_READONLY
: property_readonly
= true; break;
31351 case RID_READWRITE
: property_readwrite
= true; break;
31352 case RID_RETAIN
: property_retain
= true; break;
31356 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31358 if (keyword
== RID_GETTER
)
31359 cp_parser_error (parser
,
31360 "missing %<=%> (after %<getter%> attribute)");
31362 cp_parser_error (parser
,
31363 "missing %<=%> (after %<setter%> attribute)");
31364 syntax_error
= true;
31367 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31368 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31370 cp_parser_error (parser
, "expected identifier");
31371 syntax_error
= true;
31374 if (keyword
== RID_SETTER
)
31376 if (property_setter_ident
!= NULL_TREE
)
31378 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31379 cp_lexer_consume_token (parser
->lexer
);
31382 property_setter_ident
= cp_parser_objc_selector (parser
);
31383 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31384 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31386 cp_lexer_consume_token (parser
->lexer
);
31390 if (property_getter_ident
!= NULL_TREE
)
31392 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31393 cp_lexer_consume_token (parser
->lexer
);
31396 property_getter_ident
= cp_parser_objc_selector (parser
);
31400 cp_parser_error (parser
, "unknown property attribute");
31401 syntax_error
= true;
31408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31409 cp_lexer_consume_token (parser
->lexer
);
31414 /* FIXME: "@property (setter, assign);" will generate a spurious
31415 "error: expected ‘)’ before ‘,’ token". This is because
31416 cp_parser_require, unlike the C counterpart, will produce an
31417 error even if we are in error recovery. */
31418 if (!parens
.require_close (parser
))
31420 cp_parser_skip_to_closing_parenthesis (parser
,
31421 /*recovering=*/true,
31422 /*or_comma=*/false,
31423 /*consume_paren=*/true);
31427 /* ... and the property declaration(s). */
31428 properties
= cp_parser_objc_struct_declaration (parser
);
31430 if (properties
== error_mark_node
)
31432 cp_parser_skip_to_end_of_statement (parser
);
31433 /* If the next token is now a `;', consume it. */
31434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31435 cp_lexer_consume_token (parser
->lexer
);
31439 if (properties
== NULL_TREE
)
31440 cp_parser_error (parser
, "expected identifier");
31443 /* Comma-separated properties are chained together in
31444 reverse order; add them one by one. */
31445 properties
= nreverse (properties
);
31447 for (; properties
; properties
= TREE_CHAIN (properties
))
31448 objc_add_property_declaration (loc
, copy_node (properties
),
31449 property_readonly
, property_readwrite
,
31450 property_assign
, property_retain
,
31451 property_copy
, property_nonatomic
,
31452 property_getter_ident
, property_setter_ident
);
31455 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31458 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31460 objc-synthesize-declaration:
31461 @synthesize objc-synthesize-identifier-list ;
31463 objc-synthesize-identifier-list:
31464 objc-synthesize-identifier
31465 objc-synthesize-identifier-list, objc-synthesize-identifier
31467 objc-synthesize-identifier
31469 identifier = identifier
31472 @synthesize MyProperty;
31473 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31475 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31476 for C. Keep them in sync.
31479 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31481 tree list
= NULL_TREE
;
31483 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31485 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31488 tree property
, ivar
;
31489 property
= cp_parser_identifier (parser
);
31490 if (property
== error_mark_node
)
31492 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31497 cp_lexer_consume_token (parser
->lexer
);
31498 ivar
= cp_parser_identifier (parser
);
31499 if (ivar
== error_mark_node
)
31501 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31507 list
= chainon (list
, build_tree_list (ivar
, property
));
31508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31509 cp_lexer_consume_token (parser
->lexer
);
31513 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31514 objc_add_synthesize_declaration (loc
, list
);
31517 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31519 objc-dynamic-declaration:
31520 @dynamic identifier-list ;
31523 @dynamic MyProperty;
31524 @dynamic MyProperty, AnotherProperty;
31526 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31527 for C. Keep them in sync.
31530 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31532 tree list
= NULL_TREE
;
31534 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31536 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31540 property
= cp_parser_identifier (parser
);
31541 if (property
== error_mark_node
)
31543 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31546 list
= chainon (list
, build_tree_list (NULL
, property
));
31547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31548 cp_lexer_consume_token (parser
->lexer
);
31552 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31553 objc_add_dynamic_declaration (loc
, list
);
31557 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31559 /* Returns name of the next clause.
31560 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31561 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31562 returned and the token is consumed. */
31564 static pragma_omp_clause
31565 cp_parser_omp_clause_name (cp_parser
*parser
)
31567 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31569 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31570 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31571 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31572 result
= PRAGMA_OMP_CLAUSE_IF
;
31573 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31574 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31575 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31576 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31577 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31578 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31579 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31580 result
= PRAGMA_OMP_CLAUSE_FOR
;
31581 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31583 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31584 const char *p
= IDENTIFIER_POINTER (id
);
31589 if (!strcmp ("aligned", p
))
31590 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31591 else if (!strcmp ("async", p
))
31592 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31595 if (!strcmp ("collapse", p
))
31596 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31597 else if (!strcmp ("copy", p
))
31598 result
= PRAGMA_OACC_CLAUSE_COPY
;
31599 else if (!strcmp ("copyin", p
))
31600 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31601 else if (!strcmp ("copyout", p
))
31602 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31603 else if (!strcmp ("copyprivate", p
))
31604 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31605 else if (!strcmp ("create", p
))
31606 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31609 if (!strcmp ("defaultmap", p
))
31610 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31611 else if (!strcmp ("depend", p
))
31612 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31613 else if (!strcmp ("device", p
))
31614 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31615 else if (!strcmp ("deviceptr", p
))
31616 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31617 else if (!strcmp ("device_resident", p
))
31618 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31619 else if (!strcmp ("dist_schedule", p
))
31620 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31623 if (!strcmp ("final", p
))
31624 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31625 else if (!strcmp ("finalize", p
))
31626 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31627 else if (!strcmp ("firstprivate", p
))
31628 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31629 else if (!strcmp ("from", p
))
31630 result
= PRAGMA_OMP_CLAUSE_FROM
;
31633 if (!strcmp ("gang", p
))
31634 result
= PRAGMA_OACC_CLAUSE_GANG
;
31635 else if (!strcmp ("grainsize", p
))
31636 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31639 if (!strcmp ("hint", p
))
31640 result
= PRAGMA_OMP_CLAUSE_HINT
;
31641 else if (!strcmp ("host", p
))
31642 result
= PRAGMA_OACC_CLAUSE_HOST
;
31645 if (!strcmp ("if_present", p
))
31646 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31647 else if (!strcmp ("in_reduction", p
))
31648 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
31649 else if (!strcmp ("inbranch", p
))
31650 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31651 else if (!strcmp ("independent", p
))
31652 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31653 else if (!strcmp ("is_device_ptr", p
))
31654 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31657 if (!strcmp ("lastprivate", p
))
31658 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31659 else if (!strcmp ("linear", p
))
31660 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31661 else if (!strcmp ("link", p
))
31662 result
= PRAGMA_OMP_CLAUSE_LINK
;
31665 if (!strcmp ("map", p
))
31666 result
= PRAGMA_OMP_CLAUSE_MAP
;
31667 else if (!strcmp ("mergeable", p
))
31668 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31671 if (!strcmp ("nogroup", p
))
31672 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31673 else if (!strcmp ("nontemporal", p
))
31674 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
31675 else if (!strcmp ("notinbranch", p
))
31676 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31677 else if (!strcmp ("nowait", p
))
31678 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31679 else if (!strcmp ("num_gangs", p
))
31680 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31681 else if (!strcmp ("num_tasks", p
))
31682 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31683 else if (!strcmp ("num_teams", p
))
31684 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31685 else if (!strcmp ("num_threads", p
))
31686 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31687 else if (!strcmp ("num_workers", p
))
31688 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31691 if (!strcmp ("ordered", p
))
31692 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31695 if (!strcmp ("parallel", p
))
31696 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31697 else if (!strcmp ("present", p
))
31698 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31699 else if (!strcmp ("present_or_copy", p
)
31700 || !strcmp ("pcopy", p
))
31701 result
= PRAGMA_OACC_CLAUSE_COPY
;
31702 else if (!strcmp ("present_or_copyin", p
)
31703 || !strcmp ("pcopyin", p
))
31704 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31705 else if (!strcmp ("present_or_copyout", p
)
31706 || !strcmp ("pcopyout", p
))
31707 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31708 else if (!strcmp ("present_or_create", p
)
31709 || !strcmp ("pcreate", p
))
31710 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31711 else if (!strcmp ("priority", p
))
31712 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31713 else if (!strcmp ("proc_bind", p
))
31714 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31717 if (!strcmp ("reduction", p
))
31718 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31721 if (!strcmp ("safelen", p
))
31722 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31723 else if (!strcmp ("schedule", p
))
31724 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31725 else if (!strcmp ("sections", p
))
31726 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31727 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31728 result
= PRAGMA_OACC_CLAUSE_HOST
;
31729 else if (!strcmp ("seq", p
))
31730 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31731 else if (!strcmp ("shared", p
))
31732 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31733 else if (!strcmp ("simd", p
))
31734 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31735 else if (!strcmp ("simdlen", p
))
31736 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31739 if (!strcmp ("task_reduction", p
))
31740 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
31741 else if (!strcmp ("taskgroup", p
))
31742 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31743 else if (!strcmp ("thread_limit", p
))
31744 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31745 else if (!strcmp ("threads", p
))
31746 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31747 else if (!strcmp ("tile", p
))
31748 result
= PRAGMA_OACC_CLAUSE_TILE
;
31749 else if (!strcmp ("to", p
))
31750 result
= PRAGMA_OMP_CLAUSE_TO
;
31753 if (!strcmp ("uniform", p
))
31754 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31755 else if (!strcmp ("untied", p
))
31756 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31757 else if (!strcmp ("use_device", p
))
31758 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31759 else if (!strcmp ("use_device_ptr", p
))
31760 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31763 if (!strcmp ("vector", p
))
31764 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31765 else if (!strcmp ("vector_length", p
))
31766 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31769 if (!strcmp ("wait", p
))
31770 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31771 else if (!strcmp ("worker", p
))
31772 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31777 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31778 cp_lexer_consume_token (parser
->lexer
);
31783 /* Validate that a clause of the given type does not already exist. */
31786 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31787 const char *name
, location_t location
)
31791 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31792 if (OMP_CLAUSE_CODE (c
) == code
)
31794 error_at (location
, "too many %qs clauses", name
);
31802 variable-list , identifier
31804 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31805 colon). An opening parenthesis will have been consumed by the caller.
31807 If KIND is nonzero, create the appropriate node and install the decl
31808 in OMP_CLAUSE_DECL and add the node to the head of the list.
31810 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31811 return the list created.
31813 COLON can be NULL if only closing parenthesis should end the list,
31814 or pointer to bool which will receive false if the list is terminated
31815 by closing parenthesis or true if the list is terminated by colon. */
31818 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31819 tree list
, bool *colon
)
31822 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31825 parser
->colon_corrects_to_scope_p
= false;
31832 if (kind
== OMP_CLAUSE_DEPEND
)
31833 cp_parser_parse_tentatively (parser
);
31834 token
= cp_lexer_peek_token (parser
->lexer
);
31836 && current_class_ptr
31837 && cp_parser_is_keyword (token
, RID_THIS
))
31839 decl
= finish_this_expr ();
31840 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31841 || CONVERT_EXPR_P (decl
))
31842 decl
= TREE_OPERAND (decl
, 0);
31843 cp_lexer_consume_token (parser
->lexer
);
31847 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31848 /*check_dependency_p=*/true,
31849 /*template_p=*/NULL
,
31850 /*declarator_p=*/false,
31851 /*optional_p=*/false);
31852 if (name
== error_mark_node
)
31854 if (kind
== OMP_CLAUSE_DEPEND
31855 && cp_parser_simulate_error (parser
))
31856 goto depend_lvalue
;
31860 if (identifier_p (name
))
31861 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31864 if (decl
== error_mark_node
)
31866 if (kind
== OMP_CLAUSE_DEPEND
31867 && cp_parser_simulate_error (parser
))
31868 goto depend_lvalue
;
31869 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31873 if (decl
== error_mark_node
)
31875 else if (kind
!= 0)
31879 case OMP_CLAUSE__CACHE_
:
31880 /* The OpenACC cache directive explicitly only allows "array
31881 elements or subarrays". */
31882 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31884 error_at (token
->location
, "expected %<[%>");
31885 decl
= error_mark_node
;
31889 case OMP_CLAUSE_MAP
:
31890 case OMP_CLAUSE_FROM
:
31891 case OMP_CLAUSE_TO
:
31892 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31895 = cp_lexer_peek_token (parser
->lexer
)->location
;
31896 cp_id_kind idk
= CP_ID_KIND_NONE
;
31897 cp_lexer_consume_token (parser
->lexer
);
31898 decl
= convert_from_reference (decl
);
31900 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31905 case OMP_CLAUSE_DEPEND
:
31906 case OMP_CLAUSE_REDUCTION
:
31907 case OMP_CLAUSE_IN_REDUCTION
:
31908 case OMP_CLAUSE_TASK_REDUCTION
:
31909 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31911 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31913 parser
->colon_corrects_to_scope_p
= false;
31914 cp_lexer_consume_token (parser
->lexer
);
31915 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31916 low_bound
= cp_parser_expression (parser
);
31918 parser
->colon_corrects_to_scope_p
31919 = saved_colon_corrects_to_scope_p
;
31920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31921 length
= integer_one_node
;
31924 /* Look for `:'. */
31925 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31927 if (kind
== OMP_CLAUSE_DEPEND
31928 && cp_parser_simulate_error (parser
))
31929 goto depend_lvalue
;
31932 if (kind
== OMP_CLAUSE_DEPEND
)
31933 cp_parser_commit_to_tentative_parse (parser
);
31934 if (!cp_lexer_next_token_is (parser
->lexer
,
31936 length
= cp_parser_expression (parser
);
31938 /* Look for the closing `]'. */
31939 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31942 if (kind
== OMP_CLAUSE_DEPEND
31943 && cp_parser_simulate_error (parser
))
31944 goto depend_lvalue
;
31948 decl
= tree_cons (low_bound
, length
, decl
);
31955 if (kind
== OMP_CLAUSE_DEPEND
)
31957 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
31958 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
31959 && cp_parser_simulate_error (parser
))
31962 cp_parser_abort_tentative_parse (parser
);
31963 decl
= cp_parser_assignment_expression (parser
, NULL
,
31967 cp_parser_parse_definitely (parser
);
31970 tree u
= build_omp_clause (token
->location
, kind
);
31971 OMP_CLAUSE_DECL (u
) = decl
;
31972 OMP_CLAUSE_CHAIN (u
) = list
;
31976 list
= tree_cons (decl
, NULL_TREE
, list
);
31979 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31981 cp_lexer_consume_token (parser
->lexer
);
31985 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31987 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31990 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31994 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31998 /* Try to resync to an unnested comma. Copied from
31999 cp_parser_parenthesized_expression_list. */
32002 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32003 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32004 /*recovering=*/true,
32006 /*consume_paren=*/true);
32014 /* Similarly, but expect leading and trailing parenthesis. This is a very
32015 common case for omp clauses. */
32018 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32020 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32021 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32026 copy ( variable-list )
32027 copyin ( variable-list )
32028 copyout ( variable-list )
32029 create ( variable-list )
32030 delete ( variable-list )
32031 present ( variable-list ) */
32034 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32037 enum gomp_map_kind kind
;
32040 case PRAGMA_OACC_CLAUSE_COPY
:
32041 kind
= GOMP_MAP_TOFROM
;
32043 case PRAGMA_OACC_CLAUSE_COPYIN
:
32044 kind
= GOMP_MAP_TO
;
32046 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32047 kind
= GOMP_MAP_FROM
;
32049 case PRAGMA_OACC_CLAUSE_CREATE
:
32050 kind
= GOMP_MAP_ALLOC
;
32052 case PRAGMA_OACC_CLAUSE_DELETE
:
32053 kind
= GOMP_MAP_RELEASE
;
32055 case PRAGMA_OACC_CLAUSE_DEVICE
:
32056 kind
= GOMP_MAP_FORCE_TO
;
32058 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32059 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32061 case PRAGMA_OACC_CLAUSE_HOST
:
32062 kind
= GOMP_MAP_FORCE_FROM
;
32064 case PRAGMA_OACC_CLAUSE_LINK
:
32065 kind
= GOMP_MAP_LINK
;
32067 case PRAGMA_OACC_CLAUSE_PRESENT
:
32068 kind
= GOMP_MAP_FORCE_PRESENT
;
32071 gcc_unreachable ();
32074 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32076 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32077 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32083 deviceptr ( variable-list ) */
32086 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32088 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32091 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32092 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32093 variable-list must only allow for pointer variables. */
32094 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32095 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32097 tree v
= TREE_PURPOSE (t
);
32098 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32099 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32100 OMP_CLAUSE_DECL (u
) = v
;
32101 OMP_CLAUSE_CHAIN (u
) = list
;
32116 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32117 enum omp_clause_code code
,
32118 tree list
, location_t location
)
32120 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32121 tree c
= build_omp_clause (location
, code
);
32122 OMP_CLAUSE_CHAIN (c
) = list
;
32127 num_gangs ( expression )
32128 num_workers ( expression )
32129 vector_length ( expression ) */
32132 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32133 const char *str
, tree list
)
32135 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32137 matching_parens parens
;
32138 if (!parens
.require_open (parser
))
32141 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32143 if (t
== error_mark_node
32144 || !parens
.require_close (parser
))
32146 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32147 /*or_comma=*/false,
32148 /*consume_paren=*/true);
32152 check_no_duplicate_clause (list
, code
, str
, loc
);
32154 tree c
= build_omp_clause (loc
, code
);
32155 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32156 OMP_CLAUSE_CHAIN (c
) = list
;
32162 gang [( gang-arg-list )]
32163 worker [( [num:] int-expr )]
32164 vector [( [length:] int-expr )]
32166 where gang-arg is one of:
32171 and size-expr may be:
32178 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32179 const char *str
, tree list
)
32181 const char *id
= "num";
32182 cp_lexer
*lexer
= parser
->lexer
;
32183 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32184 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32186 if (kind
== OMP_CLAUSE_VECTOR
)
32189 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32191 matching_parens parens
;
32192 parens
.consume_open (parser
);
32196 cp_token
*next
= cp_lexer_peek_token (lexer
);
32199 /* Gang static argument. */
32200 if (kind
== OMP_CLAUSE_GANG
32201 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32203 cp_lexer_consume_token (lexer
);
32205 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32206 goto cleanup_error
;
32209 if (ops
[idx
] != NULL
)
32211 cp_parser_error (parser
, "too many %<static%> arguments");
32212 goto cleanup_error
;
32215 /* Check for the '*' argument. */
32216 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32217 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32218 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32221 cp_lexer_consume_token (lexer
);
32222 ops
[idx
] = integer_minus_one_node
;
32224 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32226 cp_lexer_consume_token (lexer
);
32232 /* Worker num: argument and vector length: arguments. */
32233 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32234 && id_equal (next
->u
.value
, id
)
32235 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32237 cp_lexer_consume_token (lexer
); /* id */
32238 cp_lexer_consume_token (lexer
); /* ':' */
32241 /* Now collect the actual argument. */
32242 if (ops
[idx
] != NULL_TREE
)
32244 cp_parser_error (parser
, "unexpected argument");
32245 goto cleanup_error
;
32248 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32250 if (expr
== error_mark_node
)
32251 goto cleanup_error
;
32253 mark_exp_read (expr
);
32256 if (kind
== OMP_CLAUSE_GANG
32257 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32259 cp_lexer_consume_token (lexer
);
32266 if (!parens
.require_close (parser
))
32267 goto cleanup_error
;
32270 check_no_duplicate_clause (list
, kind
, str
, loc
);
32272 c
= build_omp_clause (loc
, kind
);
32275 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32277 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32278 OMP_CLAUSE_CHAIN (c
) = list
;
32283 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32288 tile ( size-expr-list ) */
32291 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32293 tree c
, expr
= error_mark_node
;
32294 tree tile
= NULL_TREE
;
32296 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32297 so, but the spec authors never considered such a case and have
32298 differing opinions on what it might mean, including 'not
32300 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32301 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32304 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32309 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32313 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32314 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32316 cp_lexer_consume_token (parser
->lexer
);
32317 expr
= integer_zero_node
;
32320 expr
= cp_parser_constant_expression (parser
);
32322 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32324 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32326 /* Consume the trailing ')'. */
32327 cp_lexer_consume_token (parser
->lexer
);
32329 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32330 tile
= nreverse (tile
);
32331 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32332 OMP_CLAUSE_CHAIN (c
) = list
;
32337 Parse wait clause or directive parameters. */
32340 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32342 vec
<tree
, va_gc
> *args
;
32345 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32347 /*allow_expansion_p=*/true,
32348 /*non_constant_p=*/NULL
);
32350 if (args
== NULL
|| args
->length () == 0)
32352 cp_parser_error (parser
, "expected integer expression before ')'");
32354 release_tree_vector (args
);
32358 args_tree
= build_tree_list_vec (args
);
32360 release_tree_vector (args
);
32362 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32364 tree targ
= TREE_VALUE (t
);
32366 if (targ
!= error_mark_node
)
32368 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32369 error ("%<wait%> expression must be integral");
32372 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32374 targ
= mark_rvalue_use (targ
);
32375 OMP_CLAUSE_DECL (c
) = targ
;
32376 OMP_CLAUSE_CHAIN (c
) = list
;
32386 wait ( int-expr-list ) */
32389 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32391 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32393 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32396 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32402 collapse ( constant-expression ) */
32405 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32411 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32412 matching_parens parens
;
32413 if (!parens
.require_open (parser
))
32416 num
= cp_parser_constant_expression (parser
);
32418 if (!parens
.require_close (parser
))
32419 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32420 /*or_comma=*/false,
32421 /*consume_paren=*/true);
32423 if (num
== error_mark_node
)
32425 num
= fold_non_dependent_expr (num
);
32426 if (!tree_fits_shwi_p (num
)
32427 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32428 || (n
= tree_to_shwi (num
)) <= 0
32431 error_at (loc
, "collapse argument needs positive constant integer expression");
32435 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32436 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32437 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32438 OMP_CLAUSE_CHAIN (c
) = list
;
32439 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32445 default ( none | shared )
32448 default ( none | present ) */
32451 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32452 location_t location
, bool is_oacc
)
32454 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32457 matching_parens parens
;
32458 if (!parens
.require_open (parser
))
32460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32462 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32463 const char *p
= IDENTIFIER_POINTER (id
);
32468 if (strcmp ("none", p
) != 0)
32470 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32474 if (strcmp ("present", p
) != 0 || !is_oacc
)
32476 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32480 if (strcmp ("shared", p
) != 0 || is_oacc
)
32482 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32489 cp_lexer_consume_token (parser
->lexer
);
32495 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32497 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32500 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32501 || !parens
.require_close (parser
))
32502 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32503 /*or_comma=*/false,
32504 /*consume_paren=*/true);
32506 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32509 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32510 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32511 OMP_CLAUSE_CHAIN (c
) = list
;
32512 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32518 final ( expression ) */
32521 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32525 matching_parens parens
;
32526 if (!parens
.require_open (parser
))
32529 t
= cp_parser_assignment_expression (parser
);
32531 if (t
== error_mark_node
32532 || !parens
.require_close (parser
))
32533 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32534 /*or_comma=*/false,
32535 /*consume_paren=*/true);
32537 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32539 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32540 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32541 OMP_CLAUSE_CHAIN (c
) = list
;
32550 if ( directive-name-modifier : expression )
32552 directive-name-modifier:
32553 parallel | task | taskloop | target data | target | target update
32554 | target enter data | target exit data
32557 directive-name-modifier:
32558 ... | simd | cancel */
32561 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32565 enum tree_code if_modifier
= ERROR_MARK
;
32567 matching_parens parens
;
32568 if (!parens
.require_open (parser
))
32571 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32573 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32574 const char *p
= IDENTIFIER_POINTER (id
);
32577 if (strcmp ("cancel", p
) == 0)
32578 if_modifier
= VOID_CST
;
32579 else if (strcmp ("parallel", p
) == 0)
32580 if_modifier
= OMP_PARALLEL
;
32581 else if (strcmp ("simd", p
) == 0)
32582 if_modifier
= OMP_SIMD
;
32583 else if (strcmp ("task", p
) == 0)
32584 if_modifier
= OMP_TASK
;
32585 else if (strcmp ("taskloop", p
) == 0)
32586 if_modifier
= OMP_TASKLOOP
;
32587 else if (strcmp ("target", p
) == 0)
32589 if_modifier
= OMP_TARGET
;
32590 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32592 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32593 p
= IDENTIFIER_POINTER (id
);
32594 if (strcmp ("data", p
) == 0)
32595 if_modifier
= OMP_TARGET_DATA
;
32596 else if (strcmp ("update", p
) == 0)
32597 if_modifier
= OMP_TARGET_UPDATE
;
32598 else if (strcmp ("enter", p
) == 0)
32599 if_modifier
= OMP_TARGET_ENTER_DATA
;
32600 else if (strcmp ("exit", p
) == 0)
32601 if_modifier
= OMP_TARGET_EXIT_DATA
;
32602 if (if_modifier
!= OMP_TARGET
)
32607 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32608 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32610 if_modifier
= ERROR_MARK
;
32612 if (if_modifier
== OMP_TARGET_ENTER_DATA
32613 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32615 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32617 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32618 p
= IDENTIFIER_POINTER (id
);
32619 if (strcmp ("data", p
) == 0)
32625 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32626 error_at (loc
, "expected %<data%>");
32627 if_modifier
= ERROR_MARK
;
32632 if (if_modifier
!= ERROR_MARK
)
32634 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32637 cp_lexer_consume_token (parser
->lexer
);
32644 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32645 error_at (loc
, "expected %<:%>");
32647 if_modifier
= ERROR_MARK
;
32652 t
= cp_parser_assignment_expression (parser
);
32654 if (t
== error_mark_node
32655 || !parens
.require_close (parser
))
32656 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32657 /*or_comma=*/false,
32658 /*consume_paren=*/true);
32660 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32661 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32663 if (if_modifier
!= ERROR_MARK
32664 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32666 const char *p
= NULL
;
32667 switch (if_modifier
)
32669 case VOID_CST
: p
= "cancel"; break;
32670 case OMP_PARALLEL
: p
= "parallel"; break;
32671 case OMP_SIMD
: p
= "simd"; break;
32672 case OMP_TASK
: p
= "task"; break;
32673 case OMP_TASKLOOP
: p
= "taskloop"; break;
32674 case OMP_TARGET_DATA
: p
= "target data"; break;
32675 case OMP_TARGET
: p
= "target"; break;
32676 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32677 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32678 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32679 default: gcc_unreachable ();
32681 error_at (location
, "too many %<if%> clauses with %qs modifier",
32685 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32688 error_at (location
, "too many %<if%> clauses");
32690 error_at (location
, "too many %<if%> clauses without modifier");
32693 else if (if_modifier
== ERROR_MARK
32694 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32696 error_at (location
, "if any %<if%> clause has modifier, then all "
32697 "%<if%> clauses have to use modifier");
32702 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32703 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32704 OMP_CLAUSE_IF_EXPR (c
) = t
;
32705 OMP_CLAUSE_CHAIN (c
) = list
;
32714 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32715 tree list
, location_t location
)
32719 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32722 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32723 OMP_CLAUSE_CHAIN (c
) = list
;
32731 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32732 tree list
, location_t location
)
32736 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32738 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32739 OMP_CLAUSE_CHAIN (c
) = list
;
32744 num_threads ( expression ) */
32747 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32748 location_t location
)
32752 matching_parens parens
;
32753 if (!parens
.require_open (parser
))
32756 t
= cp_parser_assignment_expression (parser
);
32758 if (t
== error_mark_node
32759 || !parens
.require_close (parser
))
32760 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32761 /*or_comma=*/false,
32762 /*consume_paren=*/true);
32764 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32765 "num_threads", location
);
32767 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32768 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32769 OMP_CLAUSE_CHAIN (c
) = list
;
32775 num_tasks ( expression ) */
32778 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32779 location_t location
)
32783 matching_parens parens
;
32784 if (!parens
.require_open (parser
))
32787 t
= cp_parser_assignment_expression (parser
);
32789 if (t
== error_mark_node
32790 || !parens
.require_close (parser
))
32791 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32792 /*or_comma=*/false,
32793 /*consume_paren=*/true);
32795 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32796 "num_tasks", location
);
32798 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32799 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32800 OMP_CLAUSE_CHAIN (c
) = list
;
32806 grainsize ( expression ) */
32809 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32810 location_t location
)
32814 matching_parens parens
;
32815 if (!parens
.require_open (parser
))
32818 t
= cp_parser_assignment_expression (parser
);
32820 if (t
== error_mark_node
32821 || !parens
.require_close (parser
))
32822 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32826 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32827 "grainsize", location
);
32829 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32830 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32831 OMP_CLAUSE_CHAIN (c
) = list
;
32837 priority ( expression ) */
32840 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32841 location_t location
)
32845 matching_parens parens
;
32846 if (!parens
.require_open (parser
))
32849 t
= cp_parser_assignment_expression (parser
);
32851 if (t
== error_mark_node
32852 || !parens
.require_close (parser
))
32853 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32854 /*or_comma=*/false,
32855 /*consume_paren=*/true);
32857 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32858 "priority", location
);
32860 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32861 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32862 OMP_CLAUSE_CHAIN (c
) = list
;
32868 hint ( expression ) */
32871 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
32875 matching_parens parens
;
32876 if (!parens
.require_open (parser
))
32879 t
= cp_parser_assignment_expression (parser
);
32881 if (t
== error_mark_node
32882 || !parens
.require_close (parser
))
32883 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32884 /*or_comma=*/false,
32885 /*consume_paren=*/true);
32887 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32889 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32890 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32891 OMP_CLAUSE_CHAIN (c
) = list
;
32897 defaultmap ( tofrom : scalar )
32900 defaultmap ( implicit-behavior [ : variable-category ] ) */
32903 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32904 location_t location
)
32908 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
32909 enum omp_clause_defaultmap_kind category
32910 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
32912 matching_parens parens
;
32913 if (!parens
.require_open (parser
))
32916 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32918 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32921 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
32922 "%<tofrom%>, %<firstprivate%>, %<none%> "
32928 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32929 p
= IDENTIFIER_POINTER (id
);
32935 if (strcmp ("alloc", p
) == 0)
32936 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
32938 goto invalid_behavior
;
32942 if (strcmp ("default", p
) == 0)
32943 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
32945 goto invalid_behavior
;
32949 if (strcmp ("firstprivate", p
) == 0)
32950 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
32951 else if (strcmp ("from", p
) == 0)
32952 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
32954 goto invalid_behavior
;
32958 if (strcmp ("none", p
) == 0)
32959 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
32961 goto invalid_behavior
;
32965 if (strcmp ("tofrom", p
) == 0)
32966 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
32967 else if (strcmp ("to", p
) == 0)
32968 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
32970 goto invalid_behavior
;
32974 goto invalid_behavior
;
32976 cp_lexer_consume_token (parser
->lexer
);
32978 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
32980 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32983 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32986 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
32990 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32991 p
= IDENTIFIER_POINTER (id
);
32996 if (strcmp ("aggregate", p
) == 0)
32997 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
32999 goto invalid_category
;
33003 if (strcmp ("pointer", p
) == 0)
33004 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33006 goto invalid_category
;
33010 if (strcmp ("scalar", p
) == 0)
33011 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33013 goto invalid_category
;
33017 goto invalid_category
;
33020 cp_lexer_consume_token (parser
->lexer
);
33022 if (!parens
.require_close (parser
))
33025 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33026 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33027 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33028 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33029 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33030 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33032 enum omp_clause_defaultmap_kind cat
= category
;
33033 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33034 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33035 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33039 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33042 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33045 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33048 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33052 gcc_unreachable ();
33055 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33058 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33063 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33064 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33065 OMP_CLAUSE_CHAIN (c
) = list
;
33069 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33070 /*or_comma=*/false,
33071 /*consume_paren=*/true);
33079 ordered ( constant-expression ) */
33082 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33083 tree list
, location_t location
)
33085 tree c
, num
= NULL_TREE
;
33088 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33089 "ordered", location
);
33091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33093 matching_parens parens
;
33094 parens
.consume_open (parser
);
33096 num
= cp_parser_constant_expression (parser
);
33098 if (!parens
.require_close (parser
))
33099 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33100 /*or_comma=*/false,
33101 /*consume_paren=*/true);
33103 if (num
== error_mark_node
)
33105 num
= fold_non_dependent_expr (num
);
33106 if (!tree_fits_shwi_p (num
)
33107 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33108 || (n
= tree_to_shwi (num
)) <= 0
33111 error_at (location
,
33112 "ordered argument needs positive constant integer "
33118 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33119 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33120 OMP_CLAUSE_CHAIN (c
) = list
;
33125 reduction ( reduction-operator : variable-list )
33127 reduction-operator:
33128 One of: + * - & ^ | && ||
33132 reduction-operator:
33133 One of: + * - & ^ | && || min max
33137 reduction-operator:
33138 One of: + * - & ^ | && ||
33142 reduction ( reduction-modifier, reduction-operator : variable-list )
33143 in_reduction ( reduction-operator : variable-list )
33144 task_reduction ( reduction-operator : variable-list ) */
33147 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33148 bool is_omp
, tree list
)
33150 enum tree_code code
= ERROR_MARK
;
33151 tree nlist
, c
, id
= NULL_TREE
;
33153 bool inscan
= false;
33155 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33158 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33160 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33161 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33163 cp_lexer_consume_token (parser
->lexer
);
33164 cp_lexer_consume_token (parser
->lexer
);
33166 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33167 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33169 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33170 const char *p
= IDENTIFIER_POINTER (id
);
33171 if (strcmp (p
, "task") == 0)
33173 else if (strcmp (p
, "inscan") == 0)
33176 sorry ("%<inscan%> modifier on %<reduction%> clause "
33177 "not supported yet");
33179 if (task
|| inscan
)
33181 cp_lexer_consume_token (parser
->lexer
);
33182 cp_lexer_consume_token (parser
->lexer
);
33187 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33189 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33190 case CPP_MULT
: code
= MULT_EXPR
; break;
33191 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33192 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33193 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33194 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33195 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33196 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33200 if (code
!= ERROR_MARK
)
33201 cp_lexer_consume_token (parser
->lexer
);
33204 bool saved_colon_corrects_to_scope_p
;
33205 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33206 parser
->colon_corrects_to_scope_p
= false;
33207 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33208 /*check_dependency_p=*/true,
33209 /*template_p=*/NULL
,
33210 /*declarator_p=*/false,
33211 /*optional_p=*/false);
33212 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33213 if (identifier_p (id
))
33215 const char *p
= IDENTIFIER_POINTER (id
);
33217 if (strcmp (p
, "min") == 0)
33219 else if (strcmp (p
, "max") == 0)
33221 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33223 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33225 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33227 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33228 code
= BIT_AND_EXPR
;
33229 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33230 code
= BIT_IOR_EXPR
;
33231 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33232 code
= BIT_XOR_EXPR
;
33233 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33234 code
= TRUTH_ANDIF_EXPR
;
33235 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33236 code
= TRUTH_ORIF_EXPR
;
33237 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33238 tree scope
= parser
->scope
;
33240 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33241 parser
->scope
= NULL_TREE
;
33242 parser
->qualifying_scope
= NULL_TREE
;
33243 parser
->object_scope
= NULL_TREE
;
33247 error ("invalid reduction-identifier");
33249 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33250 /*or_comma=*/false,
33251 /*consume_paren=*/true);
33256 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33259 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33261 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33263 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33265 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33267 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33268 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33275 schedule ( schedule-kind )
33276 schedule ( schedule-kind , expression )
33279 static | dynamic | guided | runtime | auto
33282 schedule ( schedule-modifier : schedule-kind )
33283 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33291 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33294 int modifiers
= 0, nmodifiers
= 0;
33296 matching_parens parens
;
33297 if (!parens
.require_open (parser
))
33300 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33302 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33304 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33305 const char *p
= IDENTIFIER_POINTER (id
);
33306 if (strcmp ("simd", p
) == 0)
33307 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33308 else if (strcmp ("monotonic", p
) == 0)
33309 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33310 else if (strcmp ("nonmonotonic", p
) == 0)
33311 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33314 cp_lexer_consume_token (parser
->lexer
);
33315 if (nmodifiers
++ == 0
33316 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33317 cp_lexer_consume_token (parser
->lexer
);
33320 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33327 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33328 const char *p
= IDENTIFIER_POINTER (id
);
33333 if (strcmp ("dynamic", p
) != 0)
33335 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33339 if (strcmp ("guided", p
) != 0)
33341 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33345 if (strcmp ("runtime", p
) != 0)
33347 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33354 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33355 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33356 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33357 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33360 cp_lexer_consume_token (parser
->lexer
);
33362 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33363 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33364 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33365 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33367 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33375 cp_lexer_consume_token (parser
->lexer
);
33377 token
= cp_lexer_peek_token (parser
->lexer
);
33378 t
= cp_parser_assignment_expression (parser
);
33380 if (t
== error_mark_node
)
33382 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33383 error_at (token
->location
, "schedule %<runtime%> does not take "
33384 "a %<chunk_size%> parameter");
33385 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33386 error_at (token
->location
, "schedule %<auto%> does not take "
33387 "a %<chunk_size%> parameter");
33389 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33391 if (!parens
.require_close (parser
))
33394 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33397 OMP_CLAUSE_SCHEDULE_KIND (c
)
33398 = (enum omp_clause_schedule_kind
)
33399 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33401 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33402 OMP_CLAUSE_CHAIN (c
) = list
;
33406 cp_parser_error (parser
, "invalid schedule kind");
33408 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33409 /*or_comma=*/false,
33410 /*consume_paren=*/true);
33418 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33419 tree list
, location_t location
)
33423 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33425 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33426 OMP_CLAUSE_CHAIN (c
) = list
;
33435 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33436 tree list
, location_t location
)
33438 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33439 tree c
= build_omp_clause (location
, code
);
33440 OMP_CLAUSE_CHAIN (c
) = list
;
33451 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33452 enum omp_clause_code code
,
33453 tree list
, location_t location
)
33455 tree c
= build_omp_clause (location
, code
);
33456 OMP_CLAUSE_CHAIN (c
) = list
;
33464 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33465 tree list
, location_t location
)
33467 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33468 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33469 OMP_CLAUSE_CHAIN (c
) = list
;
33478 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33479 enum omp_clause_code code
,
33480 tree list
, location_t location
)
33482 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33483 tree c
= build_omp_clause (location
, code
);
33484 OMP_CLAUSE_CHAIN (c
) = list
;
33489 num_teams ( expression ) */
33492 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33493 location_t location
)
33497 matching_parens parens
;
33498 if (!parens
.require_open (parser
))
33501 t
= cp_parser_assignment_expression (parser
);
33503 if (t
== error_mark_node
33504 || !parens
.require_close (parser
))
33505 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33506 /*or_comma=*/false,
33507 /*consume_paren=*/true);
33509 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33510 "num_teams", location
);
33512 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33513 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33514 OMP_CLAUSE_CHAIN (c
) = list
;
33520 thread_limit ( expression ) */
33523 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33524 location_t location
)
33528 matching_parens parens
;
33529 if (!parens
.require_open (parser
))
33532 t
= cp_parser_assignment_expression (parser
);
33534 if (t
== error_mark_node
33535 || !parens
.require_close (parser
))
33536 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33537 /*or_comma=*/false,
33538 /*consume_paren=*/true);
33540 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33541 "thread_limit", location
);
33543 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33544 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33545 OMP_CLAUSE_CHAIN (c
) = list
;
33551 aligned ( variable-list )
33552 aligned ( variable-list : constant-expression ) */
33555 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33557 tree nlist
, c
, alignment
= NULL_TREE
;
33560 matching_parens parens
;
33561 if (!parens
.require_open (parser
))
33564 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33569 alignment
= cp_parser_constant_expression (parser
);
33571 if (!parens
.require_close (parser
))
33572 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33573 /*or_comma=*/false,
33574 /*consume_paren=*/true);
33576 if (alignment
== error_mark_node
)
33577 alignment
= NULL_TREE
;
33580 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33581 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33587 lastprivate ( variable-list )
33590 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33593 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
33595 bool conditional
= false;
33597 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33601 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
33603 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33604 const char *p
= IDENTIFIER_POINTER (id
);
33606 if (strcmp ("conditional", p
) == 0)
33608 conditional
= true;
33609 cp_lexer_consume_token (parser
->lexer
);
33610 cp_lexer_consume_token (parser
->lexer
);
33614 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
33618 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33619 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
33624 linear ( variable-list )
33625 linear ( variable-list : expression )
33628 linear ( modifier ( variable-list ) )
33629 linear ( modifier ( variable-list ) : expression ) */
33632 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33635 tree nlist
, c
, step
= integer_one_node
;
33637 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33639 matching_parens parens
;
33640 if (!parens
.require_open (parser
))
33643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33645 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33646 const char *p
= IDENTIFIER_POINTER (id
);
33648 if (strcmp ("ref", p
) == 0)
33649 kind
= OMP_CLAUSE_LINEAR_REF
;
33650 else if (strcmp ("val", p
) == 0)
33651 kind
= OMP_CLAUSE_LINEAR_VAL
;
33652 else if (strcmp ("uval", p
) == 0)
33653 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33654 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33655 cp_lexer_consume_token (parser
->lexer
);
33657 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33660 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33661 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33665 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33666 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33668 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33669 else if (!parens
.require_close (parser
))
33670 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33671 /*or_comma=*/false,
33672 /*consume_paren=*/true);
33679 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33680 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33682 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33683 cp_parser_parse_tentatively (parser
);
33684 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33685 /*check_dependency_p=*/true,
33686 /*template_p=*/NULL
,
33687 /*declarator_p=*/false,
33688 /*optional_p=*/false);
33689 if (step
!= error_mark_node
)
33690 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33691 if (step
== error_mark_node
)
33694 cp_parser_abort_tentative_parse (parser
);
33696 else if (!cp_parser_parse_definitely (parser
))
33700 step
= cp_parser_assignment_expression (parser
);
33702 if (!parens
.require_close (parser
))
33703 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33704 /*or_comma=*/false,
33705 /*consume_paren=*/true);
33707 if (step
== error_mark_node
)
33711 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33713 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33714 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33721 safelen ( constant-expression ) */
33724 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33725 location_t location
)
33729 matching_parens parens
;
33730 if (!parens
.require_open (parser
))
33733 t
= cp_parser_constant_expression (parser
);
33735 if (t
== error_mark_node
33736 || !parens
.require_close (parser
))
33737 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33738 /*or_comma=*/false,
33739 /*consume_paren=*/true);
33741 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33743 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33744 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33745 OMP_CLAUSE_CHAIN (c
) = list
;
33751 simdlen ( constant-expression ) */
33754 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33755 location_t location
)
33759 matching_parens parens
;
33760 if (!parens
.require_open (parser
))
33763 t
= cp_parser_constant_expression (parser
);
33765 if (t
== error_mark_node
33766 || !parens
.require_close (parser
))
33767 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33768 /*or_comma=*/false,
33769 /*consume_paren=*/true);
33771 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33773 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33774 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33775 OMP_CLAUSE_CHAIN (c
) = list
;
33782 identifier [+/- integer]
33783 vec , identifier [+/- integer]
33787 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33792 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33794 cp_parser_error (parser
, "expected identifier");
33798 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33800 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33801 tree t
, identifier
= cp_parser_identifier (parser
);
33802 tree addend
= NULL
;
33804 if (identifier
== error_mark_node
)
33805 t
= error_mark_node
;
33808 t
= cp_parser_lookup_name_simple
33809 (parser
, identifier
,
33810 cp_lexer_peek_token (parser
->lexer
)->location
);
33811 if (t
== error_mark_node
)
33812 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33819 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33821 addend
= integer_zero_node
;
33822 goto add_to_vector
;
33824 cp_lexer_consume_token (parser
->lexer
);
33826 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33828 cp_parser_error (parser
, "expected integer");
33832 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33833 if (TREE_CODE (addend
) != INTEGER_CST
)
33835 cp_parser_error (parser
, "expected integer");
33838 cp_lexer_consume_token (parser
->lexer
);
33841 if (t
!= error_mark_node
)
33843 vec
= tree_cons (addend
, t
, vec
);
33845 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33848 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33851 cp_lexer_consume_token (parser
->lexer
);
33854 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33856 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33857 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33858 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33859 OMP_CLAUSE_CHAIN (u
) = list
;
33866 iterators ( iterators-definition )
33868 iterators-definition:
33870 iterator-specifier , iterators-definition
33872 iterator-specifier:
33873 identifier = range-specification
33874 iterator-type identifier = range-specification
33876 range-specification:
33878 begin : end : step */
33881 cp_parser_omp_iterators (cp_parser
*parser
)
33883 tree ret
= NULL_TREE
, *last
= &ret
;
33884 cp_lexer_consume_token (parser
->lexer
);
33886 matching_parens parens
;
33887 if (!parens
.require_open (parser
))
33888 return error_mark_node
;
33890 bool saved_colon_corrects_to_scope_p
33891 = parser
->colon_corrects_to_scope_p
;
33892 bool saved_colon_doesnt_start_class_def_p
33893 = parser
->colon_doesnt_start_class_def_p
;
33898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33899 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
33900 iter_type
= integer_type_node
;
33903 const char *saved_message
33904 = parser
->type_definition_forbidden_message
;
33905 parser
->type_definition_forbidden_message
33906 = G_("types may not be defined in iterator type");
33908 iter_type
= cp_parser_type_id (parser
);
33910 parser
->type_definition_forbidden_message
= saved_message
;
33913 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33914 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33916 cp_parser_error (parser
, "expected identifier");
33920 tree id
= cp_parser_identifier (parser
);
33921 if (id
== error_mark_node
)
33924 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33927 parser
->colon_corrects_to_scope_p
= false;
33928 parser
->colon_doesnt_start_class_def_p
= true;
33929 tree begin
= cp_parser_assignment_expression (parser
);
33931 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33934 tree end
= cp_parser_assignment_expression (parser
);
33936 tree step
= integer_one_node
;
33937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
33939 cp_lexer_consume_token (parser
->lexer
);
33940 step
= cp_parser_assignment_expression (parser
);
33943 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
33944 DECL_ARTIFICIAL (iter_var
) = 1;
33945 DECL_CONTEXT (iter_var
) = current_function_decl
;
33946 pushdecl (iter_var
);
33948 *last
= make_tree_vec (6);
33949 TREE_VEC_ELT (*last
, 0) = iter_var
;
33950 TREE_VEC_ELT (*last
, 1) = begin
;
33951 TREE_VEC_ELT (*last
, 2) = end
;
33952 TREE_VEC_ELT (*last
, 3) = step
;
33953 last
= &TREE_CHAIN (*last
);
33955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33957 cp_lexer_consume_token (parser
->lexer
);
33964 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33965 parser
->colon_doesnt_start_class_def_p
33966 = saved_colon_doesnt_start_class_def_p
;
33968 if (!parens
.require_close (parser
))
33969 cp_parser_skip_to_closing_parenthesis (parser
,
33970 /*recovering=*/true,
33971 /*or_comma=*/false,
33972 /*consume_paren=*/true);
33974 return ret
? ret
: error_mark_node
;
33978 depend ( depend-kind : variable-list )
33986 depend ( sink : vec )
33989 depend ( depend-modifier , depend-kind: variable-list )
33992 in | out | inout | mutexinoutset | depobj
33995 iterator ( iterators-definition ) */
33998 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34000 tree nlist
, c
, iterators
= NULL_TREE
;
34001 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34003 matching_parens parens
;
34004 if (!parens
.require_open (parser
))
34009 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34012 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34013 const char *p
= IDENTIFIER_POINTER (id
);
34015 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34017 begin_scope (sk_omp
, NULL
);
34018 iterators
= cp_parser_omp_iterators (parser
);
34019 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34022 if (strcmp ("in", p
) == 0)
34023 kind
= OMP_CLAUSE_DEPEND_IN
;
34024 else if (strcmp ("inout", p
) == 0)
34025 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34026 else if (strcmp ("mutexinoutset", p
) == 0)
34027 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34028 else if (strcmp ("out", p
) == 0)
34029 kind
= OMP_CLAUSE_DEPEND_OUT
;
34030 else if (strcmp ("depobj", p
) == 0)
34031 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34032 else if (strcmp ("sink", p
) == 0)
34033 kind
= OMP_CLAUSE_DEPEND_SINK
;
34034 else if (strcmp ("source", p
) == 0)
34035 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34042 cp_lexer_consume_token (parser
->lexer
);
34045 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34047 poplevel (0, 1, 0);
34048 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34049 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34050 iterators
= NULL_TREE
;
34053 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34055 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34056 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34057 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34058 OMP_CLAUSE_CHAIN (c
) = list
;
34059 if (!parens
.require_close (parser
))
34060 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34061 /*or_comma=*/false,
34062 /*consume_paren=*/true);
34066 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34069 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34070 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34073 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34078 tree block
= poplevel (1, 1, 0);
34079 if (iterators
== error_mark_node
)
34080 iterators
= NULL_TREE
;
34082 TREE_VEC_ELT (iterators
, 5) = block
;
34085 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34087 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34089 OMP_CLAUSE_DECL (c
)
34090 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34096 cp_parser_error (parser
, "invalid depend kind");
34099 poplevel (0, 1, 0);
34100 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34101 /*or_comma=*/false,
34102 /*consume_paren=*/true);
34107 map ( map-kind : variable-list )
34108 map ( variable-list )
34111 alloc | to | from | tofrom
34115 alloc | to | from | tofrom | release | delete
34117 map ( always [,] map-kind: variable-list ) */
34120 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34123 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34124 bool always
= false;
34126 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34131 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34132 const char *p
= IDENTIFIER_POINTER (id
);
34134 if (strcmp ("always", p
) == 0)
34137 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34139 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34140 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34142 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34146 cp_lexer_consume_token (parser
->lexer
);
34148 cp_lexer_consume_token (parser
->lexer
);
34153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34154 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34156 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34157 const char *p
= IDENTIFIER_POINTER (id
);
34159 if (strcmp ("alloc", p
) == 0)
34160 kind
= GOMP_MAP_ALLOC
;
34161 else if (strcmp ("to", p
) == 0)
34162 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34163 else if (strcmp ("from", p
) == 0)
34164 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34165 else if (strcmp ("tofrom", p
) == 0)
34166 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34167 else if (strcmp ("release", p
) == 0)
34168 kind
= GOMP_MAP_RELEASE
;
34171 cp_parser_error (parser
, "invalid map kind");
34172 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34173 /*or_comma=*/false,
34174 /*consume_paren=*/true);
34177 cp_lexer_consume_token (parser
->lexer
);
34178 cp_lexer_consume_token (parser
->lexer
);
34180 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34181 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34183 kind
= GOMP_MAP_DELETE
;
34184 cp_lexer_consume_token (parser
->lexer
);
34185 cp_lexer_consume_token (parser
->lexer
);
34188 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34191 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34192 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34198 device ( expression ) */
34201 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34202 location_t location
)
34206 matching_parens parens
;
34207 if (!parens
.require_open (parser
))
34210 t
= cp_parser_assignment_expression (parser
);
34212 if (t
== error_mark_node
34213 || !parens
.require_close (parser
))
34214 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34215 /*or_comma=*/false,
34216 /*consume_paren=*/true);
34218 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34219 "device", location
);
34221 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34222 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34223 OMP_CLAUSE_CHAIN (c
) = list
;
34229 dist_schedule ( static )
34230 dist_schedule ( static , expression ) */
34233 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34234 location_t location
)
34238 matching_parens parens
;
34239 if (!parens
.require_open (parser
))
34242 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34244 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34246 cp_lexer_consume_token (parser
->lexer
);
34248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34250 cp_lexer_consume_token (parser
->lexer
);
34252 t
= cp_parser_assignment_expression (parser
);
34254 if (t
== error_mark_node
)
34256 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34258 if (!parens
.require_close (parser
))
34261 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34264 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34266 OMP_CLAUSE_CHAIN (c
) = list
;
34270 cp_parser_error (parser
, "invalid dist_schedule kind");
34272 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34273 /*or_comma=*/false,
34274 /*consume_paren=*/true);
34279 proc_bind ( proc-bind-kind )
34282 master | close | spread */
34285 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34286 location_t location
)
34289 enum omp_clause_proc_bind_kind kind
;
34291 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34296 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34297 const char *p
= IDENTIFIER_POINTER (id
);
34299 if (strcmp ("master", p
) == 0)
34300 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34301 else if (strcmp ("close", p
) == 0)
34302 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34303 else if (strcmp ("spread", p
) == 0)
34304 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34311 cp_lexer_consume_token (parser
->lexer
);
34312 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34315 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34316 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34318 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34319 OMP_CLAUSE_CHAIN (c
) = list
;
34323 cp_parser_error (parser
, "invalid depend kind");
34325 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34326 /*or_comma=*/false,
34327 /*consume_paren=*/true);
34332 async [( int-expr )] */
34335 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34338 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34340 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34342 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34344 matching_parens parens
;
34345 parens
.consume_open (parser
);
34347 t
= cp_parser_expression (parser
);
34348 if (t
== error_mark_node
34349 || !parens
.require_close (parser
))
34350 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34351 /*or_comma=*/false,
34352 /*consume_paren=*/true);
34355 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34357 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34358 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34359 OMP_CLAUSE_CHAIN (c
) = list
;
34365 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34366 is a bitmask in MASK. Return the list of clauses found. */
34369 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34370 const char *where
, cp_token
*pragma_tok
,
34371 bool finish_p
= true)
34373 tree clauses
= NULL
;
34376 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34379 pragma_omp_clause c_kind
;
34380 omp_clause_code code
;
34381 const char *c_name
;
34382 tree prev
= clauses
;
34384 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34385 cp_lexer_consume_token (parser
->lexer
);
34387 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34388 c_kind
= cp_parser_omp_clause_name (parser
);
34392 case PRAGMA_OACC_CLAUSE_ASYNC
:
34393 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34396 case PRAGMA_OACC_CLAUSE_AUTO
:
34397 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34401 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34402 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34403 c_name
= "collapse";
34405 case PRAGMA_OACC_CLAUSE_COPY
:
34406 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34409 case PRAGMA_OACC_CLAUSE_COPYIN
:
34410 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34413 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34414 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34415 c_name
= "copyout";
34417 case PRAGMA_OACC_CLAUSE_CREATE
:
34418 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34421 case PRAGMA_OACC_CLAUSE_DELETE
:
34422 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34425 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34426 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34427 c_name
= "default";
34429 case PRAGMA_OACC_CLAUSE_DEVICE
:
34430 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34433 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34434 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34435 c_name
= "deviceptr";
34437 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34438 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34439 c_name
= "device_resident";
34441 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34442 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34444 c_name
= "finalize";
34446 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34447 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34449 c_name
= "firstprivate";
34451 case PRAGMA_OACC_CLAUSE_GANG
:
34453 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34456 case PRAGMA_OACC_CLAUSE_HOST
:
34457 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34460 case PRAGMA_OACC_CLAUSE_IF
:
34461 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34464 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34465 clauses
= cp_parser_oacc_simple_clause (parser
,
34466 OMP_CLAUSE_IF_PRESENT
,
34468 c_name
= "if_present";
34470 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34471 clauses
= cp_parser_oacc_simple_clause (parser
,
34472 OMP_CLAUSE_INDEPENDENT
,
34474 c_name
= "independent";
34476 case PRAGMA_OACC_CLAUSE_LINK
:
34477 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34480 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34481 code
= OMP_CLAUSE_NUM_GANGS
;
34482 c_name
= "num_gangs";
34483 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34486 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34487 c_name
= "num_workers";
34488 code
= OMP_CLAUSE_NUM_WORKERS
;
34489 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34492 case PRAGMA_OACC_CLAUSE_PRESENT
:
34493 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34494 c_name
= "present";
34496 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34497 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34499 c_name
= "private";
34501 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34503 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34505 c_name
= "reduction";
34507 case PRAGMA_OACC_CLAUSE_SEQ
:
34508 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34512 case PRAGMA_OACC_CLAUSE_TILE
:
34513 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34516 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34517 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34519 c_name
= "use_device";
34521 case PRAGMA_OACC_CLAUSE_VECTOR
:
34523 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34526 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34527 c_name
= "vector_length";
34528 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34529 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34532 case PRAGMA_OACC_CLAUSE_WAIT
:
34533 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34536 case PRAGMA_OACC_CLAUSE_WORKER
:
34538 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34542 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34548 if (((mask
>> c_kind
) & 1) == 0)
34550 /* Remove the invalid clause(s) from the list to avoid
34551 confusing the rest of the compiler. */
34553 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34558 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34561 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34566 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34567 is a bitmask in MASK. Return the list of clauses found; the result
34568 of clause default goes in *pdefault. */
34571 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34572 const char *where
, cp_token
*pragma_tok
,
34573 bool finish_p
= true)
34575 tree clauses
= NULL
;
34577 cp_token
*token
= NULL
;
34579 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34581 pragma_omp_clause c_kind
;
34582 const char *c_name
;
34583 tree prev
= clauses
;
34585 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34586 cp_lexer_consume_token (parser
->lexer
);
34588 token
= cp_lexer_peek_token (parser
->lexer
);
34589 c_kind
= cp_parser_omp_clause_name (parser
);
34593 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
34594 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
34596 c_name
= "collapse";
34598 case PRAGMA_OMP_CLAUSE_COPYIN
:
34599 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
34602 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
34603 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
34605 c_name
= "copyprivate";
34607 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34608 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
34609 token
->location
, false);
34610 c_name
= "default";
34612 case PRAGMA_OMP_CLAUSE_FINAL
:
34613 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
34616 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
34617 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34619 c_name
= "firstprivate";
34621 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34622 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34624 c_name
= "grainsize";
34626 case PRAGMA_OMP_CLAUSE_HINT
:
34627 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34631 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34632 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34634 c_name
= "defaultmap";
34636 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34637 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34639 c_name
= "use_device_ptr";
34641 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34642 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34644 c_name
= "is_device_ptr";
34646 case PRAGMA_OMP_CLAUSE_IF
:
34647 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34651 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
34653 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
34655 c_name
= "in_reduction";
34657 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34658 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
34659 c_name
= "lastprivate";
34661 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34662 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34664 c_name
= "mergeable";
34666 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34667 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34670 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34671 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34673 c_name
= "num_tasks";
34675 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34676 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34678 c_name
= "num_threads";
34680 case PRAGMA_OMP_CLAUSE_ORDERED
:
34681 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34683 c_name
= "ordered";
34685 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34686 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34688 c_name
= "priority";
34690 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34691 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34693 c_name
= "private";
34695 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34697 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34699 c_name
= "reduction";
34701 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34702 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34704 c_name
= "schedule";
34706 case PRAGMA_OMP_CLAUSE_SHARED
:
34707 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34711 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
34713 = cp_parser_omp_clause_reduction (parser
,
34714 OMP_CLAUSE_TASK_REDUCTION
,
34716 c_name
= "task_reduction";
34718 case PRAGMA_OMP_CLAUSE_UNTIED
:
34719 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34723 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34724 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34725 clauses
, token
->location
);
34726 c_name
= "inbranch";
34728 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
34729 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
34731 c_name
= "nontemporal";
34733 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34734 clauses
= cp_parser_omp_clause_branch (parser
,
34735 OMP_CLAUSE_NOTINBRANCH
,
34736 clauses
, token
->location
);
34737 c_name
= "notinbranch";
34739 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34740 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34741 clauses
, token
->location
);
34742 c_name
= "parallel";
34746 error_at (token
->location
, "%qs must be the first clause of %qs",
34751 case PRAGMA_OMP_CLAUSE_FOR
:
34752 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34753 clauses
, token
->location
);
34756 goto clause_not_first
;
34758 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34759 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34760 clauses
, token
->location
);
34761 c_name
= "sections";
34763 goto clause_not_first
;
34765 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34766 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34767 clauses
, token
->location
);
34768 c_name
= "taskgroup";
34770 goto clause_not_first
;
34772 case PRAGMA_OMP_CLAUSE_LINK
:
34773 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34776 case PRAGMA_OMP_CLAUSE_TO
:
34777 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34778 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34781 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34784 case PRAGMA_OMP_CLAUSE_FROM
:
34785 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34788 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34789 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34791 c_name
= "uniform";
34793 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34794 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34796 c_name
= "num_teams";
34798 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34799 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34801 c_name
= "thread_limit";
34803 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34804 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34805 c_name
= "aligned";
34807 case PRAGMA_OMP_CLAUSE_LINEAR
:
34809 bool declare_simd
= false;
34810 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34811 declare_simd
= true;
34812 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34816 case PRAGMA_OMP_CLAUSE_DEPEND
:
34817 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34821 case PRAGMA_OMP_CLAUSE_MAP
:
34822 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34825 case PRAGMA_OMP_CLAUSE_DEVICE
:
34826 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34830 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34831 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34833 c_name
= "dist_schedule";
34835 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34836 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34838 c_name
= "proc_bind";
34840 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34841 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34843 c_name
= "safelen";
34845 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34846 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34848 c_name
= "simdlen";
34850 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34851 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34853 c_name
= "nogroup";
34855 case PRAGMA_OMP_CLAUSE_THREADS
:
34857 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34858 clauses
, token
->location
);
34859 c_name
= "threads";
34861 case PRAGMA_OMP_CLAUSE_SIMD
:
34863 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34864 clauses
, token
->location
);
34868 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34874 if (((mask
>> c_kind
) & 1) == 0)
34876 /* Remove the invalid clause(s) from the list to avoid
34877 confusing the rest of the compiler. */
34879 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34883 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34886 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34887 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34889 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34898 In practice, we're also interested in adding the statement to an
34899 outer node. So it is convenient if we work around the fact that
34900 cp_parser_statement calls add_stmt. */
34903 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34905 unsigned save
= parser
->in_statement
;
34907 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34908 This preserves the "not within loop or switch" style error messages
34909 for nonsense cases like
34915 if (parser
->in_statement
)
34916 parser
->in_statement
= IN_OMP_BLOCK
;
34922 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34924 parser
->in_statement
= save
;
34928 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34930 tree stmt
= begin_omp_structured_block ();
34931 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34933 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34935 cp_parser_end_omp_structured_block (parser
, save
);
34936 return finish_omp_structured_block (stmt
);
34940 # pragma omp atomic new-line
34944 x binop= expr | x++ | ++x | x-- | --x
34946 +, *, -, /, &, ^, |, <<, >>
34948 where x is an lvalue expression with scalar type.
34951 # pragma omp atomic new-line
34954 # pragma omp atomic read new-line
34957 # pragma omp atomic write new-line
34960 # pragma omp atomic update new-line
34963 # pragma omp atomic capture new-line
34966 # pragma omp atomic capture new-line
34974 expression-stmt | x = x binop expr
34976 v = expression-stmt
34978 { v = x; update-stmt; } | { update-stmt; v = x; }
34982 expression-stmt | x = x binop expr | x = expr binop x
34986 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34988 where x and v are lvalue expressions with scalar type. */
34991 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34993 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34994 tree rhs1
= NULL_TREE
, orig_lhs
;
34995 location_t loc
= pragma_tok
->location
;
34996 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
34997 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
34998 bool structured_block
= false;
35000 tree clauses
= NULL_TREE
;
35002 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35004 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35005 cp_lexer_consume_token (parser
->lexer
);
35009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35011 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35012 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35013 const char *p
= IDENTIFIER_POINTER (id
);
35014 enum tree_code new_code
= ERROR_MARK
;
35015 enum omp_memory_order new_memory_order
35016 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35018 if (!strcmp (p
, "read"))
35019 new_code
= OMP_ATOMIC_READ
;
35020 else if (!strcmp (p
, "write"))
35021 new_code
= NOP_EXPR
;
35022 else if (!strcmp (p
, "update"))
35023 new_code
= OMP_ATOMIC
;
35024 else if (!strcmp (p
, "capture"))
35025 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35026 else if (!strcmp (p
, "seq_cst"))
35027 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35028 else if (!strcmp (p
, "acq_rel"))
35029 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35030 else if (!strcmp (p
, "release"))
35031 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35032 else if (!strcmp (p
, "acquire"))
35033 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35034 else if (!strcmp (p
, "relaxed"))
35035 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35036 else if (!strcmp (p
, "hint"))
35038 cp_lexer_consume_token (parser
->lexer
);
35039 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35045 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35046 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35047 "%<release%>, %<relaxed%> or %<hint%> clause");
35051 if (new_code
!= ERROR_MARK
)
35053 if (code
!= ERROR_MARK
)
35054 error_at (cloc
, "too many atomic clauses");
35058 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35060 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35061 error_at (cloc
, "too many memory order clauses");
35063 memory_order
= new_memory_order
;
35065 cp_lexer_consume_token (parser
->lexer
);
35071 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35073 if (code
== ERROR_MARK
)
35075 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35078 = (enum omp_requires
) (omp_requires_mask
35079 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35080 switch ((enum omp_memory_order
)
35081 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35083 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35084 case OMP_MEMORY_ORDER_RELAXED
:
35085 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35087 case OMP_MEMORY_ORDER_SEQ_CST
:
35088 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35090 case OMP_MEMORY_ORDER_ACQ_REL
:
35093 case OMP_ATOMIC_READ
:
35094 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35096 case NOP_EXPR
: /* atomic write */
35098 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35101 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35106 gcc_unreachable ();
35112 case OMP_ATOMIC_READ
:
35113 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35114 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35116 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35117 "%<acq_rel%> or %<release%> clauses");
35118 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35121 case NOP_EXPR
: /* atomic write */
35122 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35123 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35125 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35126 "%<acq_rel%> or %<acquire%> clauses");
35127 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35131 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35132 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35134 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35135 "%<acq_rel%> or %<acquire%> clauses");
35136 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35145 case OMP_ATOMIC_READ
:
35146 case NOP_EXPR
: /* atomic write */
35147 v
= cp_parser_unary_expression (parser
);
35148 if (v
== error_mark_node
)
35150 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35152 if (code
== NOP_EXPR
)
35153 lhs
= cp_parser_expression (parser
);
35155 lhs
= cp_parser_unary_expression (parser
);
35156 if (lhs
== error_mark_node
)
35158 if (code
== NOP_EXPR
)
35160 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35168 case OMP_ATOMIC_CAPTURE_NEW
:
35169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35171 cp_lexer_consume_token (parser
->lexer
);
35172 structured_block
= true;
35176 v
= cp_parser_unary_expression (parser
);
35177 if (v
== error_mark_node
)
35179 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35187 lhs
= cp_parser_unary_expression (parser
);
35189 switch (TREE_CODE (lhs
))
35194 case POSTINCREMENT_EXPR
:
35195 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35196 code
= OMP_ATOMIC_CAPTURE_OLD
;
35198 case PREINCREMENT_EXPR
:
35199 lhs
= TREE_OPERAND (lhs
, 0);
35200 opcode
= PLUS_EXPR
;
35201 rhs
= integer_one_node
;
35204 case POSTDECREMENT_EXPR
:
35205 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35206 code
= OMP_ATOMIC_CAPTURE_OLD
;
35208 case PREDECREMENT_EXPR
:
35209 lhs
= TREE_OPERAND (lhs
, 0);
35210 opcode
= MINUS_EXPR
;
35211 rhs
= integer_one_node
;
35214 case COMPOUND_EXPR
:
35215 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35216 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35217 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35218 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35219 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35220 (TREE_OPERAND (lhs
, 1), 0), 0)))
35222 /* Undo effects of boolean_increment for post {in,de}crement. */
35223 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35226 if (TREE_CODE (lhs
) == MODIFY_EXPR
35227 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35229 /* Undo effects of boolean_increment. */
35230 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35232 /* This is pre or post increment. */
35233 rhs
= TREE_OPERAND (lhs
, 1);
35234 lhs
= TREE_OPERAND (lhs
, 0);
35236 if (code
== OMP_ATOMIC_CAPTURE_NEW
35237 && !structured_block
35238 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35239 code
= OMP_ATOMIC_CAPTURE_OLD
;
35245 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35248 opcode
= MULT_EXPR
;
35251 opcode
= TRUNC_DIV_EXPR
;
35254 opcode
= PLUS_EXPR
;
35257 opcode
= MINUS_EXPR
;
35259 case CPP_LSHIFT_EQ
:
35260 opcode
= LSHIFT_EXPR
;
35262 case CPP_RSHIFT_EQ
:
35263 opcode
= RSHIFT_EXPR
;
35266 opcode
= BIT_AND_EXPR
;
35269 opcode
= BIT_IOR_EXPR
;
35272 opcode
= BIT_XOR_EXPR
;
35275 enum cp_parser_prec oprec
;
35277 cp_lexer_consume_token (parser
->lexer
);
35278 cp_parser_parse_tentatively (parser
);
35279 rhs1
= cp_parser_simple_cast_expression (parser
);
35280 if (rhs1
== error_mark_node
)
35282 cp_parser_abort_tentative_parse (parser
);
35283 cp_parser_simple_cast_expression (parser
);
35286 token
= cp_lexer_peek_token (parser
->lexer
);
35287 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35289 cp_parser_abort_tentative_parse (parser
);
35290 cp_parser_parse_tentatively (parser
);
35291 rhs
= cp_parser_binary_expression (parser
, false, true,
35292 PREC_NOT_OPERATOR
, NULL
);
35293 if (rhs
== error_mark_node
)
35295 cp_parser_abort_tentative_parse (parser
);
35296 cp_parser_binary_expression (parser
, false, true,
35297 PREC_NOT_OPERATOR
, NULL
);
35300 switch (TREE_CODE (rhs
))
35303 case TRUNC_DIV_EXPR
:
35312 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35314 if (cp_parser_parse_definitely (parser
))
35316 opcode
= TREE_CODE (rhs
);
35317 rhs1
= TREE_OPERAND (rhs
, 0);
35318 rhs
= TREE_OPERAND (rhs
, 1);
35328 cp_parser_abort_tentative_parse (parser
);
35329 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35331 rhs
= cp_parser_expression (parser
);
35332 if (rhs
== error_mark_node
)
35338 cp_parser_error (parser
,
35339 "invalid form of %<#pragma omp atomic%>");
35342 if (!cp_parser_parse_definitely (parser
))
35344 switch (token
->type
)
35346 case CPP_SEMICOLON
:
35347 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35349 code
= OMP_ATOMIC_CAPTURE_OLD
;
35354 cp_lexer_consume_token (parser
->lexer
);
35357 else if (structured_block
)
35364 cp_parser_error (parser
,
35365 "invalid form of %<#pragma omp atomic%>");
35368 opcode
= MULT_EXPR
;
35371 opcode
= TRUNC_DIV_EXPR
;
35374 opcode
= PLUS_EXPR
;
35377 opcode
= MINUS_EXPR
;
35380 opcode
= LSHIFT_EXPR
;
35383 opcode
= RSHIFT_EXPR
;
35386 opcode
= BIT_AND_EXPR
;
35389 opcode
= BIT_IOR_EXPR
;
35392 opcode
= BIT_XOR_EXPR
;
35395 cp_parser_error (parser
,
35396 "invalid operator for %<#pragma omp atomic%>");
35399 oprec
= TOKEN_PRECEDENCE (token
);
35400 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35401 if (commutative_tree_code (opcode
))
35402 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35403 cp_lexer_consume_token (parser
->lexer
);
35404 rhs
= cp_parser_binary_expression (parser
, false, false,
35406 if (rhs
== error_mark_node
)
35411 cp_parser_error (parser
,
35412 "invalid operator for %<#pragma omp atomic%>");
35415 cp_lexer_consume_token (parser
->lexer
);
35417 rhs
= cp_parser_expression (parser
);
35418 if (rhs
== error_mark_node
)
35423 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35425 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35427 v
= cp_parser_unary_expression (parser
);
35428 if (v
== error_mark_node
)
35430 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35432 lhs1
= cp_parser_unary_expression (parser
);
35433 if (lhs1
== error_mark_node
)
35436 if (structured_block
)
35438 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35439 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35442 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35443 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35444 rhs1
, clauses
, memory_order
);
35445 if (!structured_block
)
35446 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35450 cp_parser_skip_to_end_of_block_or_statement (parser
);
35451 if (structured_block
)
35453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35454 cp_lexer_consume_token (parser
->lexer
);
35455 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35457 cp_parser_skip_to_end_of_block_or_statement (parser
);
35458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35459 cp_lexer_consume_token (parser
->lexer
);
35466 # pragma omp barrier new-line */
35469 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35471 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35472 finish_omp_barrier ();
35476 # pragma omp critical [(name)] new-line
35480 # pragma omp critical [(name) [hint(expression)]] new-line
35481 structured-block */
35483 #define OMP_CRITICAL_CLAUSE_MASK \
35484 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35487 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35489 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35493 matching_parens parens
;
35494 parens
.consume_open (parser
);
35496 name
= cp_parser_identifier (parser
);
35498 if (name
== error_mark_node
35499 || !parens
.require_close (parser
))
35500 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35501 /*or_comma=*/false,
35502 /*consume_paren=*/true);
35503 if (name
== error_mark_node
)
35506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35507 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35508 cp_lexer_consume_token (parser
->lexer
);
35510 clauses
= cp_parser_omp_all_clauses (parser
,
35511 OMP_CRITICAL_CLAUSE_MASK
,
35512 "#pragma omp critical", pragma_tok
);
35515 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35517 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35518 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35522 # pragma omp depobj ( depobj ) depobj-clause new-line
35525 depend (dependence-type : locator)
35527 update (dependence-type)
35536 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
35538 location_t loc
= pragma_tok
->location
;
35539 matching_parens parens
;
35540 if (!parens
.require_open (parser
))
35542 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35546 tree depobj
= cp_parser_assignment_expression (parser
);
35548 if (!parens
.require_close (parser
))
35549 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35550 /*or_comma=*/false,
35551 /*consume_paren=*/true);
35553 tree clause
= NULL_TREE
;
35554 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35555 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35558 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35559 const char *p
= IDENTIFIER_POINTER (id
);
35561 cp_lexer_consume_token (parser
->lexer
);
35562 if (!strcmp ("depend", p
))
35564 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
35566 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
35568 clause
= error_mark_node
;
35570 else if (!strcmp ("destroy", p
))
35571 kind
= OMP_CLAUSE_DEPEND_LAST
;
35572 else if (!strcmp ("update", p
))
35574 matching_parens c_parens
;
35575 if (c_parens
.require_open (parser
))
35578 = cp_lexer_peek_token (parser
->lexer
)->location
;
35579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35581 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35582 const char *p2
= IDENTIFIER_POINTER (id2
);
35584 cp_lexer_consume_token (parser
->lexer
);
35585 if (!strcmp ("in", p2
))
35586 kind
= OMP_CLAUSE_DEPEND_IN
;
35587 else if (!strcmp ("out", p2
))
35588 kind
= OMP_CLAUSE_DEPEND_OUT
;
35589 else if (!strcmp ("inout", p2
))
35590 kind
= OMP_CLAUSE_DEPEND_INOUT
;
35591 else if (!strcmp ("mutexinoutset", p2
))
35592 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
35594 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35596 clause
= error_mark_node
;
35597 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
35598 "%<mutexinoutset%>");
35600 if (!c_parens
.require_close (parser
))
35601 cp_parser_skip_to_closing_parenthesis (parser
,
35602 /*recovering=*/true,
35603 /*or_comma=*/false,
35604 /*consume_paren=*/true);
35607 clause
= error_mark_node
;
35610 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35612 clause
= error_mark_node
;
35613 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
35615 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35617 finish_omp_depobj (loc
, depobj
, kind
, clause
);
35622 # pragma omp flush flush-vars[opt] new-line
35628 # pragma omp flush memory-order-clause new-line */
35631 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
35633 enum memmodel mo
= MEMMODEL_LAST
;
35634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35636 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35637 const char *p
= IDENTIFIER_POINTER (id
);
35638 if (!strcmp (p
, "acq_rel"))
35639 mo
= MEMMODEL_ACQ_REL
;
35640 else if (!strcmp (p
, "release"))
35641 mo
= MEMMODEL_RELEASE
;
35642 else if (!strcmp (p
, "acquire"))
35643 mo
= MEMMODEL_ACQUIRE
;
35645 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35646 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35647 cp_lexer_consume_token (parser
->lexer
);
35649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35651 if (mo
!= MEMMODEL_LAST
)
35652 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35653 "%<flush%> list specified together with memory order "
35655 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35657 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35659 finish_omp_flush (mo
);
35662 /* Helper function, to parse omp for increment expression. */
35665 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
35667 tree cond
= cp_parser_binary_expression (parser
, false, true,
35668 PREC_NOT_OPERATOR
, NULL
);
35669 if (cond
== error_mark_node
35670 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35672 cp_parser_skip_to_end_of_statement (parser
);
35673 return error_mark_node
;
35676 switch (TREE_CODE (cond
))
35684 if (code
!= OACC_LOOP
)
35686 gcc_fallthrough ();
35688 return error_mark_node
;
35691 /* If decl is an iterator, preserve LHS and RHS of the relational
35692 expr until finish_omp_for. */
35694 && (type_dependent_expression_p (decl
)
35695 || CLASS_TYPE_P (TREE_TYPE (decl
))))
35698 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
35700 TREE_OPERAND (cond
, 0), ERROR_MARK
,
35701 TREE_OPERAND (cond
, 1), ERROR_MARK
,
35702 /*overload=*/NULL
, tf_warning_or_error
);
35705 /* Helper function, to parse omp for increment expression. */
35708 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
35710 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35716 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35718 op
= (token
->type
== CPP_PLUS_PLUS
35719 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
35720 cp_lexer_consume_token (parser
->lexer
);
35721 lhs
= cp_parser_simple_cast_expression (parser
);
35723 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35724 return error_mark_node
;
35725 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35728 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
35730 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35731 return error_mark_node
;
35733 token
= cp_lexer_peek_token (parser
->lexer
);
35734 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35736 op
= (token
->type
== CPP_PLUS_PLUS
35737 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
35738 cp_lexer_consume_token (parser
->lexer
);
35739 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35742 op
= cp_parser_assignment_operator_opt (parser
);
35743 if (op
== ERROR_MARK
)
35744 return error_mark_node
;
35746 if (op
!= NOP_EXPR
)
35748 rhs
= cp_parser_assignment_expression (parser
);
35749 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
35750 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35753 lhs
= cp_parser_binary_expression (parser
, false, false,
35754 PREC_ADDITIVE_EXPRESSION
, NULL
);
35755 token
= cp_lexer_peek_token (parser
->lexer
);
35756 decl_first
= (lhs
== decl
35757 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
35760 if (token
->type
!= CPP_PLUS
35761 && token
->type
!= CPP_MINUS
)
35762 return error_mark_node
;
35766 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
35767 cp_lexer_consume_token (parser
->lexer
);
35768 rhs
= cp_parser_binary_expression (parser
, false, false,
35769 PREC_ADDITIVE_EXPRESSION
, NULL
);
35770 token
= cp_lexer_peek_token (parser
->lexer
);
35771 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
35773 if (lhs
== NULL_TREE
)
35775 if (op
== PLUS_EXPR
)
35778 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
35779 tf_warning_or_error
);
35782 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
35783 ERROR_MARK
, NULL
, tf_warning_or_error
);
35786 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
35791 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
35792 || op
== MINUS_EXPR
)
35793 return error_mark_node
;
35794 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
35797 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
35799 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35802 /* Parse the initialization statement of an OpenMP for loop.
35804 Return true if the resulting construct should have an
35805 OMP_CLAUSE_PRIVATE added to it. */
35808 cp_parser_omp_for_loop_init (cp_parser
*parser
,
35809 tree
&this_pre_body
,
35810 vec
<tree
, va_gc
> *&for_block
,
35816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35819 tree add_private_clause
= NULL_TREE
;
35821 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35825 integer-type var = lb
35826 random-access-iterator-type var = lb
35827 pointer-type var = lb
35829 cp_decl_specifier_seq type_specifiers
;
35831 /* First, try to parse as an initialized declaration. See
35832 cp_parser_condition, from whence the bulk of this is copied. */
35834 cp_parser_parse_tentatively (parser
);
35835 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
35836 /*is_trailing_return=*/false,
35838 if (cp_parser_parse_definitely (parser
))
35840 /* If parsing a type specifier seq succeeded, then this
35841 MUST be a initialized declaration. */
35842 tree asm_specification
, attributes
;
35843 cp_declarator
*declarator
;
35845 declarator
= cp_parser_declarator (parser
,
35846 CP_PARSER_DECLARATOR_NAMED
,
35847 /*ctor_dtor_or_conv_p=*/NULL
,
35848 /*parenthesized_p=*/NULL
,
35849 /*member_p=*/false,
35850 /*friend_p=*/false);
35851 attributes
= cp_parser_attributes_opt (parser
);
35852 asm_specification
= cp_parser_asm_specification_opt (parser
);
35854 if (declarator
== cp_error_declarator
)
35855 cp_parser_skip_to_end_of_statement (parser
);
35859 tree pushed_scope
, auto_node
;
35861 decl
= start_decl (declarator
, &type_specifiers
,
35862 SD_INITIALIZED
, attributes
,
35863 /*prefix_attributes=*/NULL_TREE
,
35866 auto_node
= type_uses_auto (TREE_TYPE (decl
));
35867 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
35869 if (cp_lexer_next_token_is (parser
->lexer
,
35871 error ("parenthesized initialization is not allowed in "
35872 "OpenMP %<for%> loop");
35874 /* Trigger an error. */
35875 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35877 init
= error_mark_node
;
35878 cp_parser_skip_to_end_of_statement (parser
);
35880 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35881 || type_dependent_expression_p (decl
)
35884 bool is_direct_init
, is_non_constant_init
;
35886 init
= cp_parser_initializer (parser
,
35888 &is_non_constant_init
);
35893 = do_auto_deduction (TREE_TYPE (decl
), init
,
35896 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35897 && !type_dependent_expression_p (decl
))
35901 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35903 LOOKUP_ONLYCONVERTING
);
35905 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35907 vec_safe_push (for_block
, this_pre_body
);
35912 init
= pop_stmt_list (this_pre_body
);
35913 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35915 tree_stmt_iterator i
= tsi_start (init
);
35916 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35917 while (!tsi_end_p (i
))
35919 tree t
= tsi_stmt (i
);
35920 if (TREE_CODE (t
) == DECL_EXPR
35921 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35924 vec_safe_push (for_block
, t
);
35929 if (tsi_one_before_end_p (i
))
35931 tree t
= tsi_stmt (i
);
35933 free_stmt_list (init
);
35938 this_pre_body
= NULL_TREE
;
35943 cp_lexer_consume_token (parser
->lexer
);
35944 init
= cp_parser_assignment_expression (parser
);
35947 if (TYPE_REF_P (TREE_TYPE (decl
)))
35948 init
= error_mark_node
;
35950 cp_finish_decl (decl
, NULL_TREE
,
35951 /*init_const_expr_p=*/false,
35953 LOOKUP_ONLYCONVERTING
);
35957 pop_scope (pushed_scope
);
35963 /* If parsing a type specifier sequence failed, then
35964 this MUST be a simple expression. */
35965 cp_parser_parse_tentatively (parser
);
35966 decl
= cp_parser_primary_expression (parser
, false, false,
35968 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35969 if (!cp_parser_error_occurred (parser
)
35971 && (TREE_CODE (decl
) == COMPONENT_REF
35972 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35974 cp_parser_abort_tentative_parse (parser
);
35975 cp_parser_parse_tentatively (parser
);
35976 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35977 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35978 /*check_dependency_p=*/true,
35979 /*template_p=*/NULL
,
35980 /*declarator_p=*/false,
35981 /*optional_p=*/false);
35982 if (name
!= error_mark_node
35983 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35985 decl
= cp_parser_lookup_name_simple (parser
, name
,
35987 if (TREE_CODE (decl
) == FIELD_DECL
)
35988 add_private_clause
= omp_privatize_field (decl
, false);
35990 cp_parser_abort_tentative_parse (parser
);
35991 cp_parser_parse_tentatively (parser
);
35992 decl
= cp_parser_primary_expression (parser
, false, false,
35995 if (!cp_parser_error_occurred (parser
)
35998 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36002 cp_parser_parse_definitely (parser
);
36003 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36004 rhs
= cp_parser_assignment_expression (parser
);
36006 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36009 tf_warning_or_error
));
36010 if (!add_private_clause
)
36011 add_private_clause
= decl
;
36016 cp_parser_abort_tentative_parse (parser
);
36017 init
= cp_parser_expression (parser
);
36020 if (TREE_CODE (init
) == MODIFY_EXPR
36021 || TREE_CODE (init
) == MODOP_EXPR
)
36022 real_decl
= TREE_OPERAND (init
, 0);
36026 return add_private_clause
;
36029 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36032 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36033 tree
&decl
, tree
&orig_decl
, tree
&init
,
36034 tree
&orig_init
, tree
&cond
, tree
&incr
)
36036 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36037 tree iter_type
, begin_expr
, end_expr
;
36039 if (processing_template_decl
)
36041 if (check_for_bare_parameter_packs (init
))
36042 init
= error_mark_node
;
36043 if (!type_dependent_expression_p (init
)
36044 /* do_auto_deduction doesn't mess with template init-lists. */
36045 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36048 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36050 tree v
= DECL_VALUE_EXPR (decl
);
36051 if (TREE_CODE (v
) == ARRAY_REF
36052 && VAR_P (TREE_OPERAND (v
, 0))
36053 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36054 d
= TREE_OPERAND (v
, 0);
36056 do_range_for_auto_deduction (d
, init
);
36058 cond
= global_namespace
;
36062 this_pre_body
= pop_stmt_list (this_pre_body
);
36066 init
= mark_lvalue_use (init
);
36068 if (decl
== error_mark_node
|| init
== error_mark_node
)
36069 /* If an error happened previously do nothing or else a lot of
36070 unhelpful errors would be issued. */
36071 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36077 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36078 /* Can't bind a reference to an array of runtime bound. */
36082 range_temp
= build_range_temp (init
);
36083 DECL_NAME (range_temp
) = NULL_TREE
;
36084 pushdecl (range_temp
);
36085 cp_finish_decl (range_temp
, init
,
36086 /*is_constant_init*/false, NULL_TREE
,
36087 LOOKUP_ONLYCONVERTING
);
36088 range_temp_decl
= range_temp
;
36089 range_temp
= convert_from_reference (range_temp
);
36091 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36092 &begin_expr
, &end_expr
);
36095 tree end_iter_type
= iter_type
;
36096 if (cxx_dialect
>= cxx17
)
36097 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36098 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36099 TREE_USED (end
) = 1;
36100 DECL_ARTIFICIAL (end
) = 1;
36102 cp_finish_decl (end
, end_expr
,
36103 /*is_constant_init*/false, NULL_TREE
,
36104 LOOKUP_ONLYCONVERTING
);
36106 /* The new for initialization statement. */
36107 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36108 TREE_USED (begin
) = 1;
36109 DECL_ARTIFICIAL (begin
) = 1;
36112 if (CLASS_TYPE_P (iter_type
))
36117 begin_expr
= NULL_TREE
;
36119 cp_finish_decl (begin
, begin_expr
,
36120 /*is_constant_init*/false, NULL_TREE
,
36121 LOOKUP_ONLYCONVERTING
);
36123 /* The new for condition. */
36124 if (CLASS_TYPE_P (iter_type
))
36125 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36127 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36130 NULL
, tf_warning_or_error
);
36132 /* The new increment expression. */
36133 if (CLASS_TYPE_P (iter_type
))
36134 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36136 incr
= finish_unary_op_expr (input_location
,
36137 PREINCREMENT_EXPR
, begin
,
36138 tf_warning_or_error
);
36144 vec_safe_push (for_block
, this_pre_body
);
36145 this_pre_body
= NULL_TREE
;
36148 tree decomp_first_name
= NULL_TREE
;
36149 unsigned decomp_cnt
= 0;
36150 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36152 tree v
= DECL_VALUE_EXPR (orig_decl
);
36153 if (TREE_CODE (v
) == ARRAY_REF
36154 && VAR_P (TREE_OPERAND (v
, 0))
36155 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36157 tree d
= orig_decl
;
36158 orig_decl
= TREE_OPERAND (v
, 0);
36159 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36160 decomp_first_name
= d
;
36164 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36167 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36169 if (!error_operand_p (t
))
36170 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36174 tree v
= make_tree_vec (decomp_cnt
+ 3);
36175 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36176 TREE_VEC_ELT (v
, 1) = end
;
36177 TREE_VEC_ELT (v
, 2) = orig_decl
;
36178 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36180 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36181 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36183 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36186 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36187 inside of the collapsed body. */
36190 cp_finish_omp_range_for (tree orig
, tree begin
)
36192 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36193 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36194 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36195 tree decomp_first_name
= NULL_TREE
;
36196 unsigned int decomp_cnt
= 0;
36198 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36200 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36201 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36202 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36205 /* The declaration is initialized with *__begin inside the loop body. */
36206 cp_finish_decl (decl
,
36207 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36208 tf_warning_or_error
),
36209 /*is_constant_init*/false, NULL_TREE
,
36210 LOOKUP_ONLYCONVERTING
);
36211 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36212 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36215 /* Parse the restricted form of the for statement allowed by OpenMP. */
36218 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36219 tree
*cclauses
, bool *if_p
)
36221 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36223 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36224 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36225 location_t loc_first
;
36226 bool collapse_err
= false;
36227 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36228 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36229 auto_vec
<tree
, 4> orig_inits
;
36230 bool tiling
= false;
36232 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36233 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36234 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36235 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36238 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36240 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36241 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36244 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36247 if (ordered
&& ordered
< collapse
)
36249 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36250 "%<ordered%> clause parameter is less than %<collapse%>");
36251 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36252 = build_int_cst (NULL_TREE
, collapse
);
36253 ordered
= collapse
;
36257 for (tree
*pc
= &clauses
; *pc
; )
36258 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36260 error_at (OMP_CLAUSE_LOCATION (*pc
),
36261 "%<linear%> clause may not be specified together "
36262 "with %<ordered%> clause with a parameter");
36263 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36266 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36269 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36270 count
= ordered
? ordered
: collapse
;
36272 declv
= make_tree_vec (count
);
36273 initv
= make_tree_vec (count
);
36274 condv
= make_tree_vec (count
);
36275 incrv
= make_tree_vec (count
);
36276 orig_declv
= NULL_TREE
;
36278 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36280 for (i
= 0; i
< count
; i
++)
36282 int bracecount
= 0;
36283 tree add_private_clause
= NULL_TREE
;
36286 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36289 cp_parser_error (parser
, "for statement expected");
36292 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36294 matching_parens parens
;
36295 if (!parens
.require_open (parser
))
36298 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36299 this_pre_body
= push_stmt_list ();
36301 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36303 /* Save tokens so that we can put them back. */
36304 cp_lexer_save_tokens (parser
->lexer
);
36306 /* Look for ':' that is not nested in () or {}. */
36308 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36309 /*recovering=*/false,
36314 /* Roll back the tokens we skipped. */
36315 cp_lexer_rollback_tokens (parser
->lexer
);
36319 bool saved_colon_corrects_to_scope_p
36320 = parser
->colon_corrects_to_scope_p
;
36322 /* A colon is used in range-based for. */
36323 parser
->colon_corrects_to_scope_p
= false;
36325 /* Parse the declaration. */
36326 cp_parser_simple_declaration (parser
,
36327 /*function_definition_allowed_p=*/
36329 parser
->colon_corrects_to_scope_p
36330 = saved_colon_corrects_to_scope_p
;
36332 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36334 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36337 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36338 orig_decl
, init
, orig_init
,
36345 pre_body
= push_stmt_list ();
36347 add_stmt (this_pre_body
);
36348 pre_body
= pop_stmt_list (pre_body
);
36351 pre_body
= this_pre_body
;
36355 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36356 "%<ordered%> clause with parameter on "
36357 "range-based %<for%> loop");
36359 goto parse_close_paren
;
36364 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36365 init
, orig_init
, decl
, real_decl
);
36367 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36370 this_pre_body
= pop_stmt_list (this_pre_body
);
36374 pre_body
= push_stmt_list ();
36376 add_stmt (this_pre_body
);
36377 pre_body
= pop_stmt_list (pre_body
);
36380 pre_body
= this_pre_body
;
36385 if (cclauses
!= NULL
36386 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36387 && real_decl
!= NULL_TREE
)
36390 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36391 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36392 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36394 error_at (loc
, "iteration variable %qD"
36395 " should not be firstprivate", real_decl
);
36396 *c
= OMP_CLAUSE_CHAIN (*c
);
36398 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36399 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36401 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36403 *c
= OMP_CLAUSE_CHAIN (*c
);
36404 if (code
== OMP_SIMD
)
36406 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36407 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36411 OMP_CLAUSE_CHAIN (l
) = clauses
;
36414 add_private_clause
= NULL_TREE
;
36418 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36419 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36420 add_private_clause
= NULL_TREE
;
36421 c
= &OMP_CLAUSE_CHAIN (*c
);
36425 if (add_private_clause
)
36428 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36430 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36431 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36432 && OMP_CLAUSE_DECL (c
) == decl
)
36434 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36435 && OMP_CLAUSE_DECL (c
) == decl
)
36436 error_at (loc
, "iteration variable %qD "
36437 "should not be firstprivate",
36439 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36440 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36441 && OMP_CLAUSE_DECL (c
) == decl
)
36442 error_at (loc
, "iteration variable %qD should not be reduction",
36447 if (code
!= OMP_SIMD
)
36448 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36449 else if (collapse
== 1)
36450 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36452 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36453 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36454 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36457 OMP_CLAUSE_CHAIN (c
) = clauses
;
36459 /* For linear, signal that we need to fill up
36460 the so far unknown linear step. */
36461 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36462 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36468 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36469 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36470 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36475 /* If decl is an iterator, preserve the operator on decl
36476 until finish_omp_for. */
36478 && ((processing_template_decl
36479 && (TREE_TYPE (real_decl
) == NULL_TREE
36480 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36481 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36482 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36484 incr
= cp_parser_expression (parser
);
36485 if (!EXPR_HAS_LOCATION (incr
))
36486 protected_set_expr_location (incr
, input_location
);
36490 if (!parens
.require_close (parser
))
36491 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36492 /*or_comma=*/false,
36493 /*consume_paren=*/true);
36495 TREE_VEC_ELT (declv
, i
) = decl
;
36496 TREE_VEC_ELT (initv
, i
) = init
;
36497 TREE_VEC_ELT (condv
, i
) = cond
;
36498 TREE_VEC_ELT (incrv
, i
) = incr
;
36501 orig_inits
.safe_grow_cleared (i
+ 1);
36502 orig_inits
[i
] = orig_init
;
36507 orig_declv
= copy_node (declv
);
36508 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36510 else if (orig_declv
)
36511 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36513 if (i
== count
- 1)
36516 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36517 in between the collapsed for loops to be still considered perfectly
36518 nested. Hopefully the final version clarifies this.
36519 For now handle (multiple) {'s and empty statements. */
36520 cp_parser_parse_tentatively (parser
);
36523 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36525 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36527 cp_lexer_consume_token (parser
->lexer
);
36530 else if (bracecount
36531 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36532 cp_lexer_consume_token (parser
->lexer
);
36535 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36536 error_at (loc
, "not enough for loops to collapse");
36537 collapse_err
= true;
36538 cp_parser_abort_tentative_parse (parser
);
36546 cp_parser_parse_definitely (parser
);
36547 nbraces
+= bracecount
;
36554 /* Note that we saved the original contents of this flag when we entered
36555 the structured block, and so we don't need to re-save it here. */
36556 parser
->in_statement
= IN_OMP_FOR
;
36558 /* Note that the grammar doesn't call for a structured block here,
36559 though the loop as a whole is a structured block. */
36562 body
= begin_omp_structured_block ();
36563 for (i
= 0; i
< count
; i
++)
36564 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
36565 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
36566 TREE_VEC_ELT (declv
, i
));
36569 body
= push_stmt_list ();
36570 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36572 body
= finish_omp_structured_block (body
);
36574 body
= pop_stmt_list (body
);
36576 if (declv
== NULL_TREE
)
36579 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
36580 incrv
, body
, pre_body
, &orig_inits
, clauses
);
36584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36586 cp_lexer_consume_token (parser
->lexer
);
36589 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36590 cp_lexer_consume_token (parser
->lexer
);
36595 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36596 "collapsed loops not perfectly nested");
36598 collapse_err
= true;
36599 cp_parser_statement_seq_opt (parser
, NULL
);
36600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
36605 while (!for_block
->is_empty ())
36607 tree t
= for_block
->pop ();
36608 if (TREE_CODE (t
) == STATEMENT_LIST
)
36609 add_stmt (pop_stmt_list (t
));
36613 release_tree_vector (for_block
);
36618 /* Helper function for OpenMP parsing, split clauses and call
36619 finish_omp_clauses on each of the set of clauses afterwards. */
36622 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
36623 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
36626 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
36627 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
36629 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
36633 #pragma omp simd simd-clause[optseq] new-line
36636 #define OMP_SIMD_CLAUSE_MASK \
36637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36649 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36650 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36653 tree clauses
, sb
, ret
;
36655 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36657 strcat (p_name
, " simd");
36658 mask
|= OMP_SIMD_CLAUSE_MASK
;
36660 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36664 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
36665 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
36666 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
36667 OMP_CLAUSE_ORDERED
);
36668 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
36670 error_at (OMP_CLAUSE_LOCATION (c
),
36671 "%<ordered%> clause with parameter may not be specified "
36672 "on %qs construct", p_name
);
36673 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
36677 keep_next_level (true);
36678 sb
= begin_omp_structured_block ();
36679 save
= cp_parser_begin_omp_structured_block (parser
);
36681 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
36683 cp_parser_end_omp_structured_block (parser
, save
);
36684 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36690 #pragma omp for for-clause[optseq] new-line
36694 #pragma omp for simd for-simd-clause[optseq] new-line
36697 #define OMP_FOR_CLAUSE_MASK \
36698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36709 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
36710 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36713 tree clauses
, sb
, ret
;
36715 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36717 strcat (p_name
, " for");
36718 mask
|= OMP_FOR_CLAUSE_MASK
;
36719 /* parallel for{, simd} disallows nowait clause, but for
36720 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36721 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
36722 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
36723 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36724 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36725 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
36727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36729 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36730 const char *p
= IDENTIFIER_POINTER (id
);
36732 if (strcmp (p
, "simd") == 0)
36734 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36735 if (cclauses
== NULL
)
36736 cclauses
= cclauses_buf
;
36738 cp_lexer_consume_token (parser
->lexer
);
36739 if (!flag_openmp
) /* flag_openmp_simd */
36740 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36742 sb
= begin_omp_structured_block ();
36743 save
= cp_parser_begin_omp_structured_block (parser
);
36744 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36746 cp_parser_end_omp_structured_block (parser
, save
);
36747 tree body
= finish_omp_structured_block (sb
);
36750 ret
= make_node (OMP_FOR
);
36751 TREE_TYPE (ret
) = void_type_node
;
36752 OMP_FOR_BODY (ret
) = body
;
36753 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36754 SET_EXPR_LOCATION (ret
, loc
);
36759 if (!flag_openmp
) /* flag_openmp_simd */
36761 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36765 /* Composite distribute parallel for disallows linear clause. */
36766 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36767 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
36769 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36773 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
36774 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36777 keep_next_level (true);
36778 sb
= begin_omp_structured_block ();
36779 save
= cp_parser_begin_omp_structured_block (parser
);
36781 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
36783 cp_parser_end_omp_structured_block (parser
, save
);
36784 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36789 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
36790 omp_clause_mask
, tree
*, bool *);
36793 # pragma omp master new-line
36794 structured-block */
36797 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
36798 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36801 tree clauses
, sb
, ret
;
36803 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36805 strcat (p_name
, " master");
36807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36809 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36810 const char *p
= IDENTIFIER_POINTER (id
);
36812 if (strcmp (p
, "taskloop") == 0)
36814 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36815 if (cclauses
== NULL
)
36816 cclauses
= cclauses_buf
;
36818 cp_lexer_consume_token (parser
->lexer
);
36819 if (!flag_openmp
) /* flag_openmp_simd */
36820 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36822 sb
= begin_omp_structured_block ();
36823 save
= cp_parser_begin_omp_structured_block (parser
);
36824 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36826 cp_parser_end_omp_structured_block (parser
, save
);
36827 tree body
= finish_omp_structured_block (sb
);
36830 return c_finish_omp_master (loc
, body
);
36833 if (!flag_openmp
) /* flag_openmp_simd */
36835 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36841 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36843 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
36846 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36848 return c_finish_omp_master (loc
,
36849 cp_parser_omp_structured_block (parser
, if_p
));
36853 # pragma omp ordered new-line
36857 # pragma omp ordered ordered-clauses new-line
36858 structured-block */
36860 #define OMP_ORDERED_CLAUSE_MASK \
36861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36864 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36865 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36868 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
36869 enum pragma_context context
, bool *if_p
)
36871 location_t loc
= pragma_tok
->location
;
36873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36875 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36876 const char *p
= IDENTIFIER_POINTER (id
);
36878 if (strcmp (p
, "depend") == 0)
36880 if (!flag_openmp
) /* flag_openmp_simd */
36882 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36885 if (context
== pragma_stmt
)
36887 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
36888 "%<depend%> clause may only be used in compound "
36890 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36894 = cp_parser_omp_all_clauses (parser
,
36895 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
36896 "#pragma omp ordered", pragma_tok
);
36897 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
36903 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
36904 "#pragma omp ordered", pragma_tok
);
36906 if (!flag_openmp
/* flag_openmp_simd */
36907 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
36910 c_finish_omp_ordered (loc
, clauses
,
36911 cp_parser_omp_structured_block (parser
, if_p
));
36918 { section-sequence }
36921 section-directive[opt] structured-block
36922 section-sequence section-directive structured-block */
36925 cp_parser_omp_sections_scope (cp_parser
*parser
)
36927 tree stmt
, substmt
;
36928 bool error_suppress
= false;
36931 matching_braces braces
;
36932 if (!braces
.require_open (parser
))
36935 stmt
= push_stmt_list ();
36937 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
36938 != PRAGMA_OMP_SECTION
)
36940 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
36941 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
36942 add_stmt (substmt
);
36947 tok
= cp_lexer_peek_token (parser
->lexer
);
36948 if (tok
->type
== CPP_CLOSE_BRACE
)
36950 if (tok
->type
== CPP_EOF
)
36953 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
36955 cp_lexer_consume_token (parser
->lexer
);
36956 cp_parser_require_pragma_eol (parser
, tok
);
36957 error_suppress
= false;
36959 else if (!error_suppress
)
36961 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
36962 error_suppress
= true;
36965 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
36966 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
36967 add_stmt (substmt
);
36969 braces
.require_close (parser
);
36971 substmt
= pop_stmt_list (stmt
);
36973 stmt
= make_node (OMP_SECTIONS
);
36974 TREE_TYPE (stmt
) = void_type_node
;
36975 OMP_SECTIONS_BODY (stmt
) = substmt
;
36982 # pragma omp sections sections-clause[optseq] newline
36985 #define OMP_SECTIONS_CLAUSE_MASK \
36986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36993 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
36994 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
36997 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36999 strcat (p_name
, " sections");
37000 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37002 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37004 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37008 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37009 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37012 ret
= cp_parser_omp_sections_scope (parser
);
37014 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37020 # pragma omp parallel parallel-clause[optseq] new-line
37022 # pragma omp parallel for parallel-for-clause[optseq] new-line
37024 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37028 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37029 structured-block */
37031 #define OMP_PARALLEL_CLAUSE_MASK \
37032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37043 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37044 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37047 tree stmt
, clauses
, block
;
37049 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37051 strcat (p_name
, " parallel");
37052 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37053 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37054 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37055 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37056 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37058 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37060 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37061 if (cclauses
== NULL
)
37062 cclauses
= cclauses_buf
;
37064 cp_lexer_consume_token (parser
->lexer
);
37065 if (!flag_openmp
) /* flag_openmp_simd */
37066 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37068 block
= begin_omp_parallel ();
37069 save
= cp_parser_begin_omp_structured_block (parser
);
37070 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37072 cp_parser_end_omp_structured_block (parser
, save
);
37073 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37075 if (ret
== NULL_TREE
)
37077 OMP_PARALLEL_COMBINED (stmt
) = 1;
37080 /* When combined with distribute, parallel has to be followed by for.
37081 #pragma omp target parallel is allowed though. */
37083 && (mask
& (OMP_CLAUSE_MASK_1
37084 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37086 error_at (loc
, "expected %<for%> after %qs", p_name
);
37087 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37090 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37092 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37093 const char *p
= IDENTIFIER_POINTER (id
);
37094 if (strcmp (p
, "master") == 0)
37096 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37097 cclauses
= cclauses_buf
;
37099 cp_lexer_consume_token (parser
->lexer
);
37100 block
= begin_omp_parallel ();
37101 save
= cp_parser_begin_omp_structured_block (parser
);
37102 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37104 cp_parser_end_omp_structured_block (parser
, save
);
37105 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37107 OMP_PARALLEL_COMBINED (stmt
) = 1;
37108 if (ret
== NULL_TREE
)
37112 else if (!flag_openmp
) /* flag_openmp_simd */
37114 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37117 else if (strcmp (p
, "sections") == 0)
37119 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37120 cclauses
= cclauses_buf
;
37122 cp_lexer_consume_token (parser
->lexer
);
37123 block
= begin_omp_parallel ();
37124 save
= cp_parser_begin_omp_structured_block (parser
);
37125 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37126 cp_parser_end_omp_structured_block (parser
, save
);
37127 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37129 OMP_PARALLEL_COMBINED (stmt
) = 1;
37133 else if (!flag_openmp
) /* flag_openmp_simd */
37135 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37139 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37143 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37144 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37147 block
= begin_omp_parallel ();
37148 save
= cp_parser_begin_omp_structured_block (parser
);
37149 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37150 cp_parser_end_omp_structured_block (parser
, save
);
37151 stmt
= finish_omp_parallel (clauses
, block
);
37156 # pragma omp single single-clause[optseq] new-line
37157 structured-block */
37159 #define OMP_SINGLE_CLAUSE_MASK \
37160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37166 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37168 tree stmt
= make_node (OMP_SINGLE
);
37169 TREE_TYPE (stmt
) = void_type_node
;
37170 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37172 OMP_SINGLE_CLAUSES (stmt
)
37173 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37174 "#pragma omp single", pragma_tok
);
37175 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37177 return add_stmt (stmt
);
37181 # pragma omp task task-clause[optseq] new-line
37182 structured-block */
37184 #define OMP_TASK_CLAUSE_MASK \
37185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37198 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37200 tree clauses
, block
;
37203 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37204 "#pragma omp task", pragma_tok
);
37205 block
= begin_omp_task ();
37206 save
= cp_parser_begin_omp_structured_block (parser
);
37207 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37208 cp_parser_end_omp_structured_block (parser
, save
);
37209 return finish_omp_task (clauses
, block
);
37213 # pragma omp taskwait new-line
37216 # pragma omp taskwait taskwait-clause[opt] new-line */
37218 #define OMP_TASKWAIT_CLAUSE_MASK \
37219 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37222 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37225 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37226 "#pragma omp taskwait", pragma_tok
);
37230 tree stmt
= make_node (OMP_TASK
);
37231 TREE_TYPE (stmt
) = void_node
;
37232 OMP_TASK_CLAUSES (stmt
) = clauses
;
37233 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37234 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37238 finish_omp_taskwait ();
37242 # pragma omp taskyield new-line */
37245 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37247 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37248 finish_omp_taskyield ();
37252 # pragma omp taskgroup new-line
37256 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37258 #define OMP_TASKGROUP_CLAUSE_MASK \
37259 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37262 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37265 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37266 "#pragma omp taskgroup", pragma_tok
);
37267 return c_finish_omp_taskgroup (input_location
,
37268 cp_parser_omp_structured_block (parser
,
37275 # pragma omp threadprivate (variable-list) */
37278 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37282 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37283 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37285 finish_omp_threadprivate (vars
);
37289 # pragma omp cancel cancel-clause[optseq] new-line */
37291 #define OMP_CANCEL_CLAUSE_MASK \
37292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37299 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37301 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37302 "#pragma omp cancel", pragma_tok
);
37303 finish_omp_cancel (clauses
);
37307 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37309 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37310 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37316 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37317 enum pragma_context context
)
37320 bool point_seen
= false;
37322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37324 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37325 const char *p
= IDENTIFIER_POINTER (id
);
37327 if (strcmp (p
, "point") == 0)
37329 cp_lexer_consume_token (parser
->lexer
);
37335 cp_parser_error (parser
, "expected %<point%>");
37336 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37340 if (context
!= pragma_compound
)
37342 if (context
== pragma_stmt
)
37343 error_at (pragma_tok
->location
,
37344 "%<#pragma %s%> may only be used in compound statements",
37345 "omp cancellation point");
37347 cp_parser_error (parser
, "expected declaration specifiers");
37348 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37352 clauses
= cp_parser_omp_all_clauses (parser
,
37353 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37354 "#pragma omp cancellation point",
37356 finish_omp_cancellation_point (clauses
);
37360 #pragma omp distribute distribute-clause[optseq] new-line
37363 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37371 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37372 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37375 tree clauses
, sb
, ret
;
37377 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37379 strcat (p_name
, " distribute");
37380 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37384 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37385 const char *p
= IDENTIFIER_POINTER (id
);
37387 bool parallel
= false;
37389 if (strcmp (p
, "simd") == 0)
37392 parallel
= strcmp (p
, "parallel") == 0;
37393 if (parallel
|| simd
)
37395 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37396 if (cclauses
== NULL
)
37397 cclauses
= cclauses_buf
;
37398 cp_lexer_consume_token (parser
->lexer
);
37399 if (!flag_openmp
) /* flag_openmp_simd */
37402 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37405 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37408 sb
= begin_omp_structured_block ();
37409 save
= cp_parser_begin_omp_structured_block (parser
);
37411 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37414 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37416 cp_parser_end_omp_structured_block (parser
, save
);
37417 tree body
= finish_omp_structured_block (sb
);
37420 ret
= make_node (OMP_DISTRIBUTE
);
37421 TREE_TYPE (ret
) = void_type_node
;
37422 OMP_FOR_BODY (ret
) = body
;
37423 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37424 SET_EXPR_LOCATION (ret
, loc
);
37429 if (!flag_openmp
) /* flag_openmp_simd */
37431 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37435 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37439 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37440 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37443 keep_next_level (true);
37444 sb
= begin_omp_structured_block ();
37445 save
= cp_parser_begin_omp_structured_block (parser
);
37447 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37449 cp_parser_end_omp_structured_block (parser
, save
);
37450 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37456 # pragma omp teams teams-clause[optseq] new-line
37457 structured-block */
37459 #define OMP_TEAMS_CLAUSE_MASK \
37460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37469 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37470 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37473 tree clauses
, sb
, ret
;
37475 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37477 strcat (p_name
, " teams");
37478 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37482 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37483 const char *p
= IDENTIFIER_POINTER (id
);
37484 if (strcmp (p
, "distribute") == 0)
37486 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37487 if (cclauses
== NULL
)
37488 cclauses
= cclauses_buf
;
37490 cp_lexer_consume_token (parser
->lexer
);
37491 if (!flag_openmp
) /* flag_openmp_simd */
37492 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37494 keep_next_level (true);
37495 sb
= begin_omp_structured_block ();
37496 save
= cp_parser_begin_omp_structured_block (parser
);
37497 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37499 cp_parser_end_omp_structured_block (parser
, save
);
37500 tree body
= finish_omp_structured_block (sb
);
37503 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37504 ret
= make_node (OMP_TEAMS
);
37505 TREE_TYPE (ret
) = void_type_node
;
37506 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37507 OMP_TEAMS_BODY (ret
) = body
;
37508 OMP_TEAMS_COMBINED (ret
) = 1;
37509 SET_EXPR_LOCATION (ret
, loc
);
37510 return add_stmt (ret
);
37513 if (!flag_openmp
) /* flag_openmp_simd */
37515 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37519 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37523 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
37524 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37527 tree stmt
= make_node (OMP_TEAMS
);
37528 TREE_TYPE (stmt
) = void_type_node
;
37529 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
37530 keep_next_level (true);
37531 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37532 SET_EXPR_LOCATION (stmt
, loc
);
37534 return add_stmt (stmt
);
37538 # pragma omp target data target-data-clause[optseq] new-line
37539 structured-block */
37541 #define OMP_TARGET_DATA_CLAUSE_MASK \
37542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37548 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37551 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
37552 "#pragma omp target data", pragma_tok
);
37554 for (tree
*pc
= &clauses
; *pc
;)
37556 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37557 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37560 case GOMP_MAP_ALWAYS_TO
:
37561 case GOMP_MAP_FROM
:
37562 case GOMP_MAP_ALWAYS_FROM
:
37563 case GOMP_MAP_TOFROM
:
37564 case GOMP_MAP_ALWAYS_TOFROM
:
37565 case GOMP_MAP_ALLOC
:
37568 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37569 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37570 case GOMP_MAP_ALWAYS_POINTER
:
37574 error_at (OMP_CLAUSE_LOCATION (*pc
),
37575 "%<#pragma omp target data%> with map-type other "
37576 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37577 "on %<map%> clause");
37578 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37581 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
37583 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37589 error_at (pragma_tok
->location
,
37590 "%<#pragma omp target data%> must contain at least "
37591 "one %<map%> or %<use_device_ptr%> clause");
37595 tree stmt
= make_node (OMP_TARGET_DATA
);
37596 TREE_TYPE (stmt
) = void_type_node
;
37597 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
37599 keep_next_level (true);
37600 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37602 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37603 return add_stmt (stmt
);
37607 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37608 structured-block */
37610 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37618 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37619 enum pragma_context context
)
37621 bool data_seen
= false;
37622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37624 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37625 const char *p
= IDENTIFIER_POINTER (id
);
37627 if (strcmp (p
, "data") == 0)
37629 cp_lexer_consume_token (parser
->lexer
);
37635 cp_parser_error (parser
, "expected %<data%>");
37636 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37640 if (context
== pragma_stmt
)
37642 error_at (pragma_tok
->location
,
37643 "%<#pragma %s%> may only be used in compound statements",
37644 "omp target enter data");
37645 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37650 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
37651 "#pragma omp target enter data", pragma_tok
);
37653 for (tree
*pc
= &clauses
; *pc
;)
37655 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37656 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37659 case GOMP_MAP_ALWAYS_TO
:
37660 case GOMP_MAP_ALLOC
:
37663 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37664 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37665 case GOMP_MAP_ALWAYS_POINTER
:
37669 error_at (OMP_CLAUSE_LOCATION (*pc
),
37670 "%<#pragma omp target enter data%> with map-type other "
37671 "than %<to%> or %<alloc%> on %<map%> clause");
37672 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37675 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37681 error_at (pragma_tok
->location
,
37682 "%<#pragma omp target enter data%> must contain at least "
37683 "one %<map%> clause");
37687 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
37688 TREE_TYPE (stmt
) = void_type_node
;
37689 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
37690 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37691 return add_stmt (stmt
);
37695 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37696 structured-block */
37698 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37706 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37707 enum pragma_context context
)
37709 bool data_seen
= false;
37710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37712 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37713 const char *p
= IDENTIFIER_POINTER (id
);
37715 if (strcmp (p
, "data") == 0)
37717 cp_lexer_consume_token (parser
->lexer
);
37723 cp_parser_error (parser
, "expected %<data%>");
37724 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37728 if (context
== pragma_stmt
)
37730 error_at (pragma_tok
->location
,
37731 "%<#pragma %s%> may only be used in compound statements",
37732 "omp target exit data");
37733 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37738 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
37739 "#pragma omp target exit data", pragma_tok
);
37741 for (tree
*pc
= &clauses
; *pc
;)
37743 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37744 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37746 case GOMP_MAP_FROM
:
37747 case GOMP_MAP_ALWAYS_FROM
:
37748 case GOMP_MAP_RELEASE
:
37749 case GOMP_MAP_DELETE
:
37752 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37753 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37754 case GOMP_MAP_ALWAYS_POINTER
:
37758 error_at (OMP_CLAUSE_LOCATION (*pc
),
37759 "%<#pragma omp target exit data%> with map-type other "
37760 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37762 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37765 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37771 error_at (pragma_tok
->location
,
37772 "%<#pragma omp target exit data%> must contain at least "
37773 "one %<map%> clause");
37777 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
37778 TREE_TYPE (stmt
) = void_type_node
;
37779 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
37780 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37781 return add_stmt (stmt
);
37785 # pragma omp target update target-update-clause[optseq] new-line */
37787 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37788 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37796 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
37797 enum pragma_context context
)
37799 if (context
== pragma_stmt
)
37801 error_at (pragma_tok
->location
,
37802 "%<#pragma %s%> may only be used in compound statements",
37803 "omp target update");
37804 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37809 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
37810 "#pragma omp target update", pragma_tok
);
37811 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
37812 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
37814 error_at (pragma_tok
->location
,
37815 "%<#pragma omp target update%> must contain at least one "
37816 "%<from%> or %<to%> clauses");
37820 tree stmt
= make_node (OMP_TARGET_UPDATE
);
37821 TREE_TYPE (stmt
) = void_type_node
;
37822 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
37823 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37829 # pragma omp target target-clause[optseq] new-line
37830 structured-block */
37832 #define OMP_TARGET_CLAUSE_MASK \
37833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37844 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
37845 enum pragma_context context
, bool *if_p
)
37847 tree
*pc
= NULL
, stmt
;
37851 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
37853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37855 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37856 const char *p
= IDENTIFIER_POINTER (id
);
37857 enum tree_code ccode
= ERROR_MARK
;
37859 if (strcmp (p
, "teams") == 0)
37861 else if (strcmp (p
, "parallel") == 0)
37862 ccode
= OMP_PARALLEL
;
37863 else if (strcmp (p
, "simd") == 0)
37865 if (ccode
!= ERROR_MARK
)
37867 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
37868 char p_name
[sizeof ("#pragma omp target teams distribute "
37869 "parallel for simd")];
37871 cp_lexer_consume_token (parser
->lexer
);
37872 strcpy (p_name
, "#pragma omp target");
37873 if (!flag_openmp
) /* flag_openmp_simd */
37879 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
37880 OMP_TARGET_CLAUSE_MASK
,
37884 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
37885 OMP_TARGET_CLAUSE_MASK
,
37889 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
37890 OMP_TARGET_CLAUSE_MASK
,
37894 gcc_unreachable ();
37896 return stmt
!= NULL_TREE
;
37898 keep_next_level (true);
37899 tree sb
= begin_omp_structured_block (), ret
;
37900 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
37904 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
37905 OMP_TARGET_CLAUSE_MASK
, cclauses
,
37909 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
37910 OMP_TARGET_CLAUSE_MASK
, cclauses
,
37914 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
37915 OMP_TARGET_CLAUSE_MASK
, cclauses
,
37919 gcc_unreachable ();
37921 cp_parser_end_omp_structured_block (parser
, save
);
37922 tree body
= finish_omp_structured_block (sb
);
37923 if (ret
== NULL_TREE
)
37925 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
37927 /* For combined target teams, ensure the num_teams and
37928 thread_limit clause expressions are evaluated on the host,
37929 before entering the target construct. */
37931 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37932 c
; c
= OMP_CLAUSE_CHAIN (c
))
37933 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
37934 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
37935 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
37937 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
37938 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
37939 if (expr
== error_mark_node
)
37941 tree tmp
= TARGET_EXPR_SLOT (expr
);
37943 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
37944 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
37945 OMP_CLAUSE_FIRSTPRIVATE
);
37946 OMP_CLAUSE_DECL (tc
) = tmp
;
37947 OMP_CLAUSE_CHAIN (tc
)
37948 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
37949 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
37952 tree stmt
= make_node (OMP_TARGET
);
37953 TREE_TYPE (stmt
) = void_type_node
;
37954 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
37955 OMP_TARGET_BODY (stmt
) = body
;
37956 OMP_TARGET_COMBINED (stmt
) = 1;
37957 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37959 pc
= &OMP_TARGET_CLAUSES (stmt
);
37960 goto check_clauses
;
37962 else if (!flag_openmp
) /* flag_openmp_simd */
37964 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37967 else if (strcmp (p
, "data") == 0)
37969 cp_lexer_consume_token (parser
->lexer
);
37970 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
37973 else if (strcmp (p
, "enter") == 0)
37975 cp_lexer_consume_token (parser
->lexer
);
37976 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
37979 else if (strcmp (p
, "exit") == 0)
37981 cp_lexer_consume_token (parser
->lexer
);
37982 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
37985 else if (strcmp (p
, "update") == 0)
37987 cp_lexer_consume_token (parser
->lexer
);
37988 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
37991 if (!flag_openmp
) /* flag_openmp_simd */
37993 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37997 stmt
= make_node (OMP_TARGET
);
37998 TREE_TYPE (stmt
) = void_type_node
;
38000 OMP_TARGET_CLAUSES (stmt
)
38001 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38002 "#pragma omp target", pragma_tok
);
38003 pc
= &OMP_TARGET_CLAUSES (stmt
);
38004 keep_next_level (true);
38005 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38007 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38013 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38014 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38017 case GOMP_MAP_ALWAYS_TO
:
38018 case GOMP_MAP_FROM
:
38019 case GOMP_MAP_ALWAYS_FROM
:
38020 case GOMP_MAP_TOFROM
:
38021 case GOMP_MAP_ALWAYS_TOFROM
:
38022 case GOMP_MAP_ALLOC
:
38023 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38024 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38025 case GOMP_MAP_ALWAYS_POINTER
:
38028 error_at (OMP_CLAUSE_LOCATION (*pc
),
38029 "%<#pragma omp target%> with map-type other "
38030 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38031 "on %<map%> clause");
38032 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38035 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38041 # pragma acc cache (variable-list) new-line
38045 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38047 tree stmt
, clauses
;
38049 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38050 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38052 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38054 stmt
= make_node (OACC_CACHE
);
38055 TREE_TYPE (stmt
) = void_type_node
;
38056 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38057 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38064 # pragma acc data oacc-data-clause[optseq] new-line
38065 structured-block */
38067 #define OACC_DATA_CLAUSE_MASK \
38068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38077 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38079 tree stmt
, clauses
, block
;
38082 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38083 "#pragma acc data", pragma_tok
);
38085 block
= begin_omp_parallel ();
38086 save
= cp_parser_begin_omp_structured_block (parser
);
38087 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38088 cp_parser_end_omp_structured_block (parser
, save
);
38089 stmt
= finish_oacc_data (clauses
, block
);
38094 # pragma acc host_data <clauses> new-line
38095 structured-block */
38097 #define OACC_HOST_DATA_CLAUSE_MASK \
38098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38101 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38103 tree stmt
, clauses
, block
;
38106 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38107 "#pragma acc host_data", pragma_tok
);
38109 block
= begin_omp_parallel ();
38110 save
= cp_parser_begin_omp_structured_block (parser
);
38111 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38112 cp_parser_end_omp_structured_block (parser
, save
);
38113 stmt
= finish_oacc_host_data (clauses
, block
);
38118 # pragma acc declare oacc-data-clause[optseq] new-line
38121 #define OACC_DECLARE_CLAUSE_MASK \
38122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38132 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38134 tree clauses
, stmt
;
38135 bool error
= false;
38137 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38138 "#pragma acc declare", pragma_tok
, true);
38141 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38143 error_at (pragma_tok
->location
,
38144 "no valid clauses specified in %<#pragma acc declare%>");
38148 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38150 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38151 tree decl
= OMP_CLAUSE_DECL (t
);
38152 if (!DECL_P (decl
))
38154 error_at (loc
, "array section in %<#pragma acc declare%>");
38158 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38159 switch (OMP_CLAUSE_MAP_KIND (t
))
38161 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38162 case GOMP_MAP_ALLOC
:
38164 case GOMP_MAP_FORCE_DEVICEPTR
:
38165 case GOMP_MAP_DEVICE_RESIDENT
:
38168 case GOMP_MAP_LINK
:
38169 if (!global_bindings_p ()
38170 && (TREE_STATIC (decl
)
38171 || !DECL_EXTERNAL (decl
)))
38174 "%qD must be a global variable in "
38175 "%<#pragma acc declare link%>",
38183 if (global_bindings_p ())
38185 error_at (loc
, "invalid OpenACC clause at file scope");
38189 if (DECL_EXTERNAL (decl
))
38192 "invalid use of %<extern%> variable %qD "
38193 "in %<#pragma acc declare%>", decl
);
38197 else if (TREE_PUBLIC (decl
))
38200 "invalid use of %<global%> variable %qD "
38201 "in %<#pragma acc declare%>", decl
);
38208 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38209 || lookup_attribute ("omp declare target link",
38210 DECL_ATTRIBUTES (decl
)))
38212 error_at (loc
, "variable %qD used more than once with "
38213 "%<#pragma acc declare%>", decl
);
38222 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38223 id
= get_identifier ("omp declare target link");
38225 id
= get_identifier ("omp declare target");
38227 DECL_ATTRIBUTES (decl
)
38228 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38229 if (global_bindings_p ())
38231 symtab_node
*node
= symtab_node::get (decl
);
38234 node
->offloadable
= 1;
38235 if (ENABLE_OFFLOADING
)
38237 g
->have_offload
= true;
38238 if (is_a
<varpool_node
*> (node
))
38239 vec_safe_push (offload_vars
, decl
);
38246 if (error
|| global_bindings_p ())
38249 stmt
= make_node (OACC_DECLARE
);
38250 TREE_TYPE (stmt
) = void_type_node
;
38251 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38252 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38260 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38264 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38266 LOC is the location of the #pragma token.
38269 #define OACC_ENTER_DATA_CLAUSE_MASK \
38270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38276 #define OACC_EXIT_DATA_CLAUSE_MASK \
38277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38285 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38288 location_t loc
= pragma_tok
->location
;
38289 tree stmt
, clauses
;
38290 const char *p
= "";
38292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38293 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38295 if (strcmp (p
, "data") != 0)
38297 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38298 enter
? "enter" : "exit");
38299 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38303 cp_lexer_consume_token (parser
->lexer
);
38306 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38307 "#pragma acc enter data", pragma_tok
);
38309 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38310 "#pragma acc exit data", pragma_tok
);
38312 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38314 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38315 enter
? "enter" : "exit");
38319 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38320 TREE_TYPE (stmt
) = void_type_node
;
38321 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38322 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38328 # pragma acc loop oacc-loop-clause[optseq] new-line
38329 structured-block */
38331 #define OACC_LOOP_CLAUSE_MASK \
38332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38344 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38345 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38347 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38349 strcat (p_name
, " loop");
38350 mask
|= OACC_LOOP_CLAUSE_MASK
;
38352 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38356 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38358 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38360 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38363 tree block
= begin_omp_structured_block ();
38364 int save
= cp_parser_begin_omp_structured_block (parser
);
38365 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38366 cp_parser_end_omp_structured_block (parser
, save
);
38367 add_stmt (finish_omp_structured_block (block
));
38373 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38378 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38382 #define OACC_KERNELS_CLAUSE_MASK \
38383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38397 #define OACC_PARALLEL_CLAUSE_MASK \
38398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38416 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38417 char *p_name
, bool *if_p
)
38419 omp_clause_mask mask
;
38420 enum tree_code code
;
38421 switch (cp_parser_pragma_kind (pragma_tok
))
38423 case PRAGMA_OACC_KERNELS
:
38424 strcat (p_name
, " kernels");
38425 mask
= OACC_KERNELS_CLAUSE_MASK
;
38426 code
= OACC_KERNELS
;
38428 case PRAGMA_OACC_PARALLEL
:
38429 strcat (p_name
, " parallel");
38430 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38431 code
= OACC_PARALLEL
;
38434 gcc_unreachable ();
38437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38440 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38441 if (strcmp (p
, "loop") == 0)
38443 cp_lexer_consume_token (parser
->lexer
);
38444 tree block
= begin_omp_parallel ();
38446 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
38448 return finish_omp_construct (code
, block
, clauses
);
38452 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38454 tree block
= begin_omp_parallel ();
38455 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38456 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38457 cp_parser_end_omp_structured_block (parser
, save
);
38458 return finish_omp_construct (code
, block
, clauses
);
38462 # pragma acc update oacc-update-clause[optseq] new-line
38465 #define OACC_UPDATE_CLAUSE_MASK \
38466 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38474 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38476 tree stmt
, clauses
;
38478 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38479 "#pragma acc update", pragma_tok
);
38481 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38483 error_at (pragma_tok
->location
,
38484 "%<#pragma acc update%> must contain at least one "
38485 "%<device%> or %<host%> or %<self%> clause");
38489 stmt
= make_node (OACC_UPDATE
);
38490 TREE_TYPE (stmt
) = void_type_node
;
38491 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38492 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38498 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38500 LOC is the location of the #pragma token.
38503 #define OACC_WAIT_CLAUSE_MASK \
38504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38507 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38509 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38510 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38512 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38513 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
38515 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
38516 "#pragma acc wait", pragma_tok
);
38518 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
38519 stmt
= finish_expr_stmt (stmt
);
38525 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38527 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38528 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38536 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38537 enum pragma_context context
)
38539 bool first_p
= parser
->omp_declare_simd
== NULL
;
38540 cp_omp_declare_simd_data data
;
38543 data
.error_seen
= false;
38544 data
.fndecl_seen
= false;
38545 data
.tokens
= vNULL
;
38546 data
.clauses
= NULL_TREE
;
38547 /* It is safe to take the address of a local variable; it will only be
38548 used while this scope is live. */
38549 parser
->omp_declare_simd
= &data
;
38552 /* Store away all pragma tokens. */
38553 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38554 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38555 cp_lexer_consume_token (parser
->lexer
);
38556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38557 parser
->omp_declare_simd
->error_seen
= true;
38558 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38559 struct cp_token_cache
*cp
38560 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38561 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
38565 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38566 cp_parser_pragma (parser
, context
, NULL
);
38569 case pragma_external
:
38570 cp_parser_declaration (parser
);
38572 case pragma_member
:
38573 cp_parser_member_declaration (parser
);
38575 case pragma_objc_icode
:
38576 cp_parser_block_declaration (parser
, /*statement_p=*/false);
38579 cp_parser_declaration_statement (parser
);
38582 if (parser
->omp_declare_simd
38583 && !parser
->omp_declare_simd
->error_seen
38584 && !parser
->omp_declare_simd
->fndecl_seen
)
38585 error_at (pragma_tok
->location
,
38586 "%<#pragma omp declare simd%> not immediately followed by "
38587 "function declaration or definition");
38588 data
.tokens
.release ();
38589 parser
->omp_declare_simd
= NULL
;
38593 /* Finalize #pragma omp declare simd clauses after direct declarator has
38594 been parsed, and put that into "omp declare simd" attribute. */
38597 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
38599 struct cp_token_cache
*ce
;
38600 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
38603 if (!data
->error_seen
&& data
->fndecl_seen
)
38605 error ("%<#pragma omp declare simd%> not immediately followed by "
38606 "a single function declaration or definition");
38607 data
->error_seen
= true;
38609 if (data
->error_seen
)
38612 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
38616 cp_parser_push_lexer_for_tokens (parser
, ce
);
38617 parser
->lexer
->in_pragma
= true;
38618 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38619 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38620 cp_lexer_consume_token (parser
->lexer
);
38621 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
38622 "#pragma omp declare simd", pragma_tok
);
38623 cp_parser_pop_lexer (parser
);
38625 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
38626 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
38627 TREE_CHAIN (c
) = attrs
;
38628 if (processing_template_decl
)
38629 ATTR_IS_DEPENDENT (c
) = 1;
38633 data
->fndecl_seen
= true;
38639 # pragma omp declare target new-line
38640 declarations and definitions
38641 # pragma omp end declare target new-line
38644 # pragma omp declare target ( extended-list ) new-line
38646 # pragma omp declare target declare-target-clauses[seq] new-line */
38648 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38653 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38655 tree clauses
= NULL_TREE
;
38656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38658 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
38659 "#pragma omp declare target", pragma_tok
);
38660 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38662 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
38664 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38665 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38669 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38670 scope_chain
->omp_declare_target_attribute
++;
38673 if (scope_chain
->omp_declare_target_attribute
)
38674 error_at (pragma_tok
->location
,
38675 "%<#pragma omp declare target%> with clauses in between "
38676 "%<#pragma omp declare target%> without clauses and "
38677 "%<#pragma omp end declare target%>");
38678 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
38680 tree t
= OMP_CLAUSE_DECL (c
), id
;
38681 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
38682 tree at2
= lookup_attribute ("omp declare target link",
38683 DECL_ATTRIBUTES (t
));
38684 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
38686 id
= get_identifier ("omp declare target link");
38687 std::swap (at1
, at2
);
38690 id
= get_identifier ("omp declare target");
38693 error_at (OMP_CLAUSE_LOCATION (c
),
38694 "%qD specified both in declare target %<link%> and %<to%>"
38700 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
38701 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
38704 symtab_node
*node
= symtab_node::get (t
);
38707 node
->offloadable
= 1;
38708 if (ENABLE_OFFLOADING
)
38710 g
->have_offload
= true;
38711 if (is_a
<varpool_node
*> (node
))
38712 vec_safe_push (offload_vars
, t
);
38720 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38722 const char *p
= "";
38723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38725 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38726 p
= IDENTIFIER_POINTER (id
);
38728 if (strcmp (p
, "declare") == 0)
38730 cp_lexer_consume_token (parser
->lexer
);
38732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38734 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38735 p
= IDENTIFIER_POINTER (id
);
38737 if (strcmp (p
, "target") == 0)
38738 cp_lexer_consume_token (parser
->lexer
);
38741 cp_parser_error (parser
, "expected %<target%>");
38742 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38748 cp_parser_error (parser
, "expected %<declare%>");
38749 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38752 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38753 if (!scope_chain
->omp_declare_target_attribute
)
38754 error_at (pragma_tok
->location
,
38755 "%<#pragma omp end declare target%> without corresponding "
38756 "%<#pragma omp declare target%>");
38758 scope_chain
->omp_declare_target_attribute
--;
38761 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38762 expression and optional initializer clause of
38763 #pragma omp declare reduction. We store the expression(s) as
38764 either 3, 6 or 7 special statements inside of the artificial function's
38765 body. The first two statements are DECL_EXPRs for the artificial
38766 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38767 expression that uses those variables.
38768 If there was any INITIALIZER clause, this is followed by further statements,
38769 the fourth and fifth statements are DECL_EXPRs for the artificial
38770 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38771 constructor variant (first token after open paren is not omp_priv),
38772 then the sixth statement is a statement with the function call expression
38773 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38774 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38775 to initialize the OMP_PRIV artificial variable and there is seventh
38776 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38779 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
38781 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
38782 gcc_assert (TYPE_REF_P (type
));
38783 type
= TREE_TYPE (type
);
38784 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
38785 DECL_ARTIFICIAL (omp_out
) = 1;
38786 pushdecl (omp_out
);
38787 add_decl_expr (omp_out
);
38788 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
38789 DECL_ARTIFICIAL (omp_in
) = 1;
38791 add_decl_expr (omp_in
);
38793 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
38795 keep_next_level (true);
38796 tree block
= begin_omp_structured_block ();
38797 combiner
= cp_parser_expression (parser
);
38798 finish_expr_stmt (combiner
);
38799 block
= finish_omp_structured_block (block
);
38802 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38805 const char *p
= "";
38806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38808 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38809 p
= IDENTIFIER_POINTER (id
);
38812 if (strcmp (p
, "initializer") == 0)
38814 cp_lexer_consume_token (parser
->lexer
);
38815 matching_parens parens
;
38816 if (!parens
.require_open (parser
))
38820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38822 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38823 p
= IDENTIFIER_POINTER (id
);
38826 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
38827 DECL_ARTIFICIAL (omp_priv
) = 1;
38828 pushdecl (omp_priv
);
38829 add_decl_expr (omp_priv
);
38830 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
38831 DECL_ARTIFICIAL (omp_orig
) = 1;
38832 pushdecl (omp_orig
);
38833 add_decl_expr (omp_orig
);
38835 keep_next_level (true);
38836 block
= begin_omp_structured_block ();
38839 if (strcmp (p
, "omp_priv") == 0)
38841 bool is_direct_init
, is_non_constant_init
;
38843 cp_lexer_consume_token (parser
->lexer
);
38844 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
38846 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
38847 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
38849 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
38850 == CPP_CLOSE_PAREN
))
38852 finish_omp_structured_block (block
);
38853 error ("invalid initializer clause");
38856 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
38857 &is_non_constant_init
);
38858 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
38859 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
38863 cp_parser_parse_tentatively (parser
);
38864 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
38865 /*check_dependency_p=*/true,
38866 /*template_p=*/NULL
,
38867 /*declarator_p=*/false,
38868 /*optional_p=*/false);
38869 vec
<tree
, va_gc
> *args
;
38870 if (fn_name
== error_mark_node
38871 || cp_parser_error_occurred (parser
)
38872 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
38873 || ((args
= cp_parser_parenthesized_expression_list
38874 (parser
, non_attr
, /*cast_p=*/false,
38875 /*allow_expansion_p=*/true,
38876 /*non_constant_p=*/NULL
)),
38877 cp_parser_error_occurred (parser
)))
38879 finish_omp_structured_block (block
);
38880 cp_parser_abort_tentative_parse (parser
);
38881 cp_parser_error (parser
, "expected id-expression (arguments)");
38886 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
38887 if (arg
== omp_priv
38888 || (TREE_CODE (arg
) == ADDR_EXPR
38889 && TREE_OPERAND (arg
, 0) == omp_priv
))
38891 cp_parser_abort_tentative_parse (parser
);
38892 if (arg
== NULL_TREE
)
38893 error ("one of the initializer call arguments should be %<omp_priv%>"
38894 " or %<&omp_priv%>");
38895 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
38897 finish_expr_stmt (initializer
);
38900 block
= finish_omp_structured_block (block
);
38901 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
38905 add_decl_expr (omp_orig
);
38907 if (!parens
.require_close (parser
))
38911 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
38912 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
38919 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38920 initializer-clause[opt] new-line
38922 initializer-clause:
38923 initializer (omp_priv initializer)
38924 initializer (function-name (argument-list)) */
38927 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
38928 enum pragma_context
)
38930 auto_vec
<tree
> types
;
38931 enum tree_code reduc_code
= ERROR_MARK
;
38932 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
38934 cp_token
*first_token
;
38935 cp_token_cache
*cp
;
38939 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38940 p
= obstack_alloc (&declarator_obstack
, 0);
38942 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38945 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
38948 reduc_code
= PLUS_EXPR
;
38951 reduc_code
= MULT_EXPR
;
38954 reduc_code
= MINUS_EXPR
;
38957 reduc_code
= BIT_AND_EXPR
;
38960 reduc_code
= BIT_XOR_EXPR
;
38963 reduc_code
= BIT_IOR_EXPR
;
38966 reduc_code
= TRUTH_ANDIF_EXPR
;
38969 reduc_code
= TRUTH_ORIF_EXPR
;
38972 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
38975 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38976 "%<|%>, %<&&%>, %<||%> or identifier");
38980 if (reduc_code
!= ERROR_MARK
)
38981 cp_lexer_consume_token (parser
->lexer
);
38983 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
38984 if (reduc_id
== error_mark_node
)
38987 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
38990 /* Types may not be defined in declare reduction type list. */
38991 const char *saved_message
;
38992 saved_message
= parser
->type_definition_forbidden_message
;
38993 parser
->type_definition_forbidden_message
38994 = G_("types may not be defined in declare reduction type list");
38995 bool saved_colon_corrects_to_scope_p
;
38996 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38997 parser
->colon_corrects_to_scope_p
= false;
38998 bool saved_colon_doesnt_start_class_def_p
;
38999 saved_colon_doesnt_start_class_def_p
39000 = parser
->colon_doesnt_start_class_def_p
;
39001 parser
->colon_doesnt_start_class_def_p
= true;
39005 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39006 type
= cp_parser_type_id (parser
);
39007 if (type
== error_mark_node
)
39009 else if (ARITHMETIC_TYPE_P (type
)
39010 && (orig_reduc_id
== NULL_TREE
39011 || (TREE_CODE (type
) != COMPLEX_TYPE
39012 && (id_equal (orig_reduc_id
, "min")
39013 || id_equal (orig_reduc_id
, "max")))))
39014 error_at (loc
, "predeclared arithmetic type %qT in "
39015 "%<#pragma omp declare reduction%>", type
);
39016 else if (TREE_CODE (type
) == FUNCTION_TYPE
39017 || TREE_CODE (type
) == METHOD_TYPE
39018 || TREE_CODE (type
) == ARRAY_TYPE
)
39019 error_at (loc
, "function or array type %qT in "
39020 "%<#pragma omp declare reduction%>", type
);
39021 else if (TYPE_REF_P (type
))
39022 error_at (loc
, "reference type %qT in "
39023 "%<#pragma omp declare reduction%>", type
);
39024 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39025 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39026 "%<#pragma omp declare reduction%>", type
);
39028 types
.safe_push (type
);
39030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39031 cp_lexer_consume_token (parser
->lexer
);
39036 /* Restore the saved message. */
39037 parser
->type_definition_forbidden_message
= saved_message
;
39038 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39039 parser
->colon_doesnt_start_class_def_p
39040 = saved_colon_doesnt_start_class_def_p
;
39042 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39043 || types
.is_empty ())
39046 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39050 first_token
= cp_lexer_peek_token (parser
->lexer
);
39053 FOR_EACH_VEC_ELT (types
, i
, type
)
39056 = build_function_type_list (void_type_node
,
39057 cp_build_reference_type (type
, false),
39059 tree this_reduc_id
= reduc_id
;
39060 if (!dependent_type_p (type
))
39061 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39062 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39063 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39064 DECL_ARTIFICIAL (fndecl
) = 1;
39065 DECL_EXTERNAL (fndecl
) = 1;
39066 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39067 DECL_IGNORED_P (fndecl
) = 1;
39068 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39069 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39070 DECL_ATTRIBUTES (fndecl
)
39071 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39072 DECL_ATTRIBUTES (fndecl
));
39073 if (processing_template_decl
)
39074 fndecl
= push_template_decl (fndecl
);
39075 bool block_scope
= false;
39076 tree block
= NULL_TREE
;
39077 if (current_function_decl
)
39079 block_scope
= true;
39080 DECL_CONTEXT (fndecl
) = global_namespace
;
39081 if (!processing_template_decl
)
39084 else if (current_class_type
)
39088 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39089 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39090 cp_lexer_consume_token (parser
->lexer
);
39091 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39093 cp
= cp_token_cache_new (first_token
,
39094 cp_lexer_peek_nth_token (parser
->lexer
,
39097 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39098 finish_member_declaration (fndecl
);
39099 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39100 DECL_PENDING_INLINE_P (fndecl
) = 1;
39101 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39106 DECL_CONTEXT (fndecl
) = current_namespace
;
39110 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39112 block
= begin_omp_structured_block ();
39115 cp_parser_push_lexer_for_tokens (parser
, cp
);
39116 parser
->lexer
->in_pragma
= true;
39118 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39121 finish_function (/*inline_p=*/false);
39123 DECL_CONTEXT (fndecl
) = current_function_decl
;
39125 cp_parser_pop_lexer (parser
);
39129 cp_parser_pop_lexer (parser
);
39131 finish_function (/*inline_p=*/false);
39134 DECL_CONTEXT (fndecl
) = current_function_decl
;
39135 block
= finish_omp_structured_block (block
);
39136 if (TREE_CODE (block
) == BIND_EXPR
)
39137 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39138 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39139 DECL_SAVED_TREE (fndecl
) = block
;
39140 if (processing_template_decl
)
39141 add_decl_expr (fndecl
);
39143 cp_check_omp_declare_reduction (fndecl
);
39144 if (cp
== NULL
&& types
.length () > 1)
39145 cp
= cp_token_cache_new (first_token
,
39146 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39147 if (errs
!= errorcount
)
39151 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39154 /* Free any declarators allocated. */
39155 obstack_free (&declarator_obstack
, p
);
39159 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39160 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39161 initializer-clause[opt] new-line
39162 #pragma omp declare target new-line */
39165 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39166 enum pragma_context context
)
39168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39170 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39171 const char *p
= IDENTIFIER_POINTER (id
);
39173 if (strcmp (p
, "simd") == 0)
39175 cp_lexer_consume_token (parser
->lexer
);
39176 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39180 cp_ensure_no_omp_declare_simd (parser
);
39181 if (strcmp (p
, "reduction") == 0)
39183 cp_lexer_consume_token (parser
->lexer
);
39184 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39188 if (!flag_openmp
) /* flag_openmp_simd */
39190 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39193 if (strcmp (p
, "target") == 0)
39195 cp_lexer_consume_token (parser
->lexer
);
39196 cp_parser_omp_declare_target (parser
, pragma_tok
);
39200 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39202 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39207 #pragma omp requires clauses[optseq] new-line */
39210 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39213 enum omp_requires new_req
= (enum omp_requires
) 0;
39215 location_t loc
= pragma_tok
->location
;
39216 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39218 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39219 cp_lexer_consume_token (parser
->lexer
);
39223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39225 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39226 const char *p
= IDENTIFIER_POINTER (id
);
39227 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39228 enum omp_requires this_req
= (enum omp_requires
) 0;
39230 if (!strcmp (p
, "unified_address"))
39231 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39232 else if (!strcmp (p
, "unified_shared_memory"))
39233 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39234 else if (!strcmp (p
, "dynamic_allocators"))
39235 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39236 else if (!strcmp (p
, "reverse_offload"))
39237 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39238 else if (!strcmp (p
, "atomic_default_mem_order"))
39240 cp_lexer_consume_token (parser
->lexer
);
39242 matching_parens parens
;
39243 if (parens
.require_open (parser
))
39245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39247 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39248 p
= IDENTIFIER_POINTER (id
);
39250 if (!strcmp (p
, "seq_cst"))
39252 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39253 else if (!strcmp (p
, "relaxed"))
39255 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39256 else if (!strcmp (p
, "acq_rel"))
39258 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39262 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39263 "expected %<seq_cst%>, %<relaxed%> or "
39265 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39267 cp_lexer_consume_token (parser
->lexer
);
39270 cp_lexer_consume_token (parser
->lexer
);
39272 if (!parens
.require_close (parser
))
39273 cp_parser_skip_to_closing_parenthesis (parser
,
39274 /*recovering=*/true,
39275 /*or_comma=*/false,
39281 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39289 error_at (cloc
, "expected %<unified_address%>, "
39290 "%<unified_shared_memory%>, "
39291 "%<dynamic_allocators%>, "
39292 "%<reverse_offload%> "
39293 "or %<atomic_default_mem_order%> clause");
39294 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39298 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39299 "supported yet", p
);
39301 cp_lexer_consume_token (parser
->lexer
);
39304 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39306 if ((this_req
& new_req
) != 0)
39307 error_at (cloc
, "too many %qs clauses", p
);
39308 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39309 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39310 error_at (cloc
, "%qs clause used lexically after first "
39311 "target construct or offloading API", p
);
39313 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39315 error_at (cloc
, "too many %qs clauses",
39316 "atomic_default_mem_order");
39317 this_req
= (enum omp_requires
) 0;
39319 else if ((omp_requires_mask
39320 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39322 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39323 " clause in a single compilation unit");
39325 = (enum omp_requires
)
39327 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39329 else if ((omp_requires_mask
39330 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39331 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39332 "lexically after first %<atomic%> construct "
39333 "without memory order clause");
39334 new_req
= (enum omp_requires
) (new_req
| this_req
);
39336 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39342 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39345 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39351 #pragma omp taskloop taskloop-clause[optseq] new-line
39354 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39357 #define OMP_TASKLOOP_CLAUSE_MASK \
39358 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39376 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39377 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39380 tree clauses
, sb
, ret
;
39382 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39384 strcat (p_name
, " taskloop");
39385 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39386 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39388 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39389 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39393 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39394 const char *p
= IDENTIFIER_POINTER (id
);
39396 if (strcmp (p
, "simd") == 0)
39398 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39399 if (cclauses
== NULL
)
39400 cclauses
= cclauses_buf
;
39402 cp_lexer_consume_token (parser
->lexer
);
39403 if (!flag_openmp
) /* flag_openmp_simd */
39404 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39406 sb
= begin_omp_structured_block ();
39407 save
= cp_parser_begin_omp_structured_block (parser
);
39408 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39410 cp_parser_end_omp_structured_block (parser
, save
);
39411 tree body
= finish_omp_structured_block (sb
);
39414 ret
= make_node (OMP_TASKLOOP
);
39415 TREE_TYPE (ret
) = void_type_node
;
39416 OMP_FOR_BODY (ret
) = body
;
39417 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39418 SET_EXPR_LOCATION (ret
, loc
);
39423 if (!flag_openmp
) /* flag_openmp_simd */
39425 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39429 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39433 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39434 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39437 keep_next_level (true);
39438 sb
= begin_omp_structured_block ();
39439 save
= cp_parser_begin_omp_structured_block (parser
);
39441 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39444 cp_parser_end_omp_structured_block (parser
, save
);
39445 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39452 # pragma acc routine oacc-routine-clause[optseq] new-line
39453 function-definition
39455 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39458 #define OACC_ROUTINE_CLAUSE_MASK \
39459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39465 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39466 component, which must resolve to a declared namespace-scope
39467 function. The clauses are either processed directly (for a named
39468 function), or defered until the immediatley following declaration
39472 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39473 enum pragma_context context
)
39475 gcc_checking_assert (context
== pragma_external
);
39476 /* The checking for "another pragma following this one" in the "no optional
39477 '( name )'" case makes sure that we dont re-enter. */
39478 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39480 cp_oacc_routine_data data
;
39481 data
.error_seen
= false;
39482 data
.fndecl_seen
= false;
39483 data
.tokens
= vNULL
;
39484 data
.clauses
= NULL_TREE
;
39485 data
.loc
= pragma_tok
->location
;
39486 /* It is safe to take the address of a local variable; it will only be
39487 used while this scope is live. */
39488 parser
->oacc_routine
= &data
;
39490 /* Look for optional '( name )'. */
39491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39493 matching_parens parens
;
39494 parens
.consume_open (parser
); /* '(' */
39496 /* We parse the name as an id-expression. If it resolves to
39497 anything other than a non-overloaded function at namespace
39498 scope, it's an error. */
39499 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39500 tree name
= cp_parser_id_expression (parser
,
39501 /*template_keyword_p=*/false,
39502 /*check_dependency_p=*/false,
39503 /*template_p=*/NULL
,
39504 /*declarator_p=*/false,
39505 /*optional_p=*/false);
39506 tree decl
= (identifier_p (name
)
39507 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39509 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39510 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39512 if (decl
== error_mark_node
39513 || !parens
.require_close (parser
))
39515 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39516 parser
->oacc_routine
= NULL
;
39521 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39522 "#pragma acc routine",
39523 cp_lexer_peek_token (parser
->lexer
));
39525 if (decl
&& is_overloaded_fn (decl
)
39526 && (TREE_CODE (decl
) != FUNCTION_DECL
39527 || DECL_FUNCTION_TEMPLATE_P (decl
)))
39529 error_at (name_loc
,
39530 "%<#pragma acc routine%> names a set of overloads");
39531 parser
->oacc_routine
= NULL
;
39535 /* Perhaps we should use the same rule as declarations in different
39537 if (!DECL_NAMESPACE_SCOPE_P (decl
))
39539 error_at (name_loc
,
39540 "%qD does not refer to a namespace scope function", decl
);
39541 parser
->oacc_routine
= NULL
;
39545 if (TREE_CODE (decl
) != FUNCTION_DECL
)
39547 error_at (name_loc
, "%qD does not refer to a function", decl
);
39548 parser
->oacc_routine
= NULL
;
39552 cp_finalize_oacc_routine (parser
, decl
, false);
39553 parser
->oacc_routine
= NULL
;
39555 else /* No optional '( name )'. */
39557 /* Store away all pragma tokens. */
39558 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39559 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39560 cp_lexer_consume_token (parser
->lexer
);
39561 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39562 parser
->oacc_routine
->error_seen
= true;
39563 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39564 struct cp_token_cache
*cp
39565 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39566 parser
->oacc_routine
->tokens
.safe_push (cp
);
39568 /* Emit a helpful diagnostic if there's another pragma following this
39570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39572 cp_ensure_no_oacc_routine (parser
);
39573 data
.tokens
.release ();
39574 /* ..., and then just keep going. */
39578 /* We only have to consider the pragma_external case here. */
39579 cp_parser_declaration (parser
);
39580 if (parser
->oacc_routine
39581 && !parser
->oacc_routine
->fndecl_seen
)
39582 cp_ensure_no_oacc_routine (parser
);
39584 parser
->oacc_routine
= NULL
;
39585 data
.tokens
.release ();
39589 /* Finalize #pragma acc routine clauses after direct declarator has
39593 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
39595 struct cp_token_cache
*ce
;
39596 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
39598 if (!data
->error_seen
&& data
->fndecl_seen
)
39600 error_at (data
->loc
,
39601 "%<#pragma acc routine%> not immediately followed by "
39602 "a single function declaration or definition");
39603 data
->error_seen
= true;
39605 if (data
->error_seen
)
39608 gcc_checking_assert (data
->tokens
.length () == 1);
39609 ce
= data
->tokens
[0];
39611 cp_parser_push_lexer_for_tokens (parser
, ce
);
39612 parser
->lexer
->in_pragma
= true;
39613 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39615 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39616 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
39617 parser
->oacc_routine
->clauses
39618 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39619 "#pragma acc routine", pragma_tok
);
39620 cp_parser_pop_lexer (parser
);
39621 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39627 /* Apply any saved OpenACC routine clauses to a just-parsed
39631 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
39633 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
39635 /* Keep going if we're in error reporting mode. */
39636 if (parser
->oacc_routine
->error_seen
39637 || fndecl
== error_mark_node
)
39640 if (parser
->oacc_routine
->fndecl_seen
)
39642 error_at (parser
->oacc_routine
->loc
,
39643 "%<#pragma acc routine%> not immediately followed by"
39644 " a single function declaration or definition");
39645 parser
->oacc_routine
= NULL
;
39648 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
39650 cp_ensure_no_oacc_routine (parser
);
39654 if (oacc_get_fn_attrib (fndecl
))
39656 error_at (parser
->oacc_routine
->loc
,
39657 "%<#pragma acc routine%> already applied to %qD", fndecl
);
39658 parser
->oacc_routine
= NULL
;
39662 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
39664 error_at (parser
->oacc_routine
->loc
,
39666 ? G_("%<#pragma acc routine%> must be applied before use")
39667 : G_("%<#pragma acc routine%> must be applied before "
39669 parser
->oacc_routine
= NULL
;
39673 /* Process the routine's dimension clauses. */
39674 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
39675 oacc_replace_fn_attrib (fndecl
, dims
);
39677 /* Add an "omp declare target" attribute. */
39678 DECL_ATTRIBUTES (fndecl
)
39679 = tree_cons (get_identifier ("omp declare target"),
39680 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
39682 /* Don't unset parser->oacc_routine here: we may still need it to
39683 diagnose wrong usage. But, remember that we've used this "#pragma acc
39685 parser
->oacc_routine
->fndecl_seen
= true;
39689 /* Main entry point to OpenMP statement pragmas. */
39692 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39695 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
39696 omp_clause_mask
mask (0);
39698 switch (cp_parser_pragma_kind (pragma_tok
))
39700 case PRAGMA_OACC_ATOMIC
:
39701 cp_parser_omp_atomic (parser
, pragma_tok
);
39703 case PRAGMA_OACC_CACHE
:
39704 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
39706 case PRAGMA_OACC_DATA
:
39707 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
39709 case PRAGMA_OACC_ENTER_DATA
:
39710 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
39712 case PRAGMA_OACC_EXIT_DATA
:
39713 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
39715 case PRAGMA_OACC_HOST_DATA
:
39716 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
39718 case PRAGMA_OACC_KERNELS
:
39719 case PRAGMA_OACC_PARALLEL
:
39720 strcpy (p_name
, "#pragma acc");
39721 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
39724 case PRAGMA_OACC_LOOP
:
39725 strcpy (p_name
, "#pragma acc");
39726 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39729 case PRAGMA_OACC_UPDATE
:
39730 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
39732 case PRAGMA_OACC_WAIT
:
39733 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
39735 case PRAGMA_OMP_ATOMIC
:
39736 cp_parser_omp_atomic (parser
, pragma_tok
);
39738 case PRAGMA_OMP_CRITICAL
:
39739 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
39741 case PRAGMA_OMP_DISTRIBUTE
:
39742 strcpy (p_name
, "#pragma omp");
39743 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
39746 case PRAGMA_OMP_FOR
:
39747 strcpy (p_name
, "#pragma omp");
39748 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
39751 case PRAGMA_OMP_MASTER
:
39752 strcpy (p_name
, "#pragma omp");
39753 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
39756 case PRAGMA_OMP_PARALLEL
:
39757 strcpy (p_name
, "#pragma omp");
39758 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
39761 case PRAGMA_OMP_SECTIONS
:
39762 strcpy (p_name
, "#pragma omp");
39763 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
39765 case PRAGMA_OMP_SIMD
:
39766 strcpy (p_name
, "#pragma omp");
39767 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
39770 case PRAGMA_OMP_SINGLE
:
39771 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
39773 case PRAGMA_OMP_TASK
:
39774 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
39776 case PRAGMA_OMP_TASKGROUP
:
39777 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
39779 case PRAGMA_OMP_TASKLOOP
:
39780 strcpy (p_name
, "#pragma omp");
39781 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39784 case PRAGMA_OMP_TEAMS
:
39785 strcpy (p_name
, "#pragma omp");
39786 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
39790 gcc_unreachable ();
39793 protected_set_expr_location (stmt
, pragma_tok
->location
);
39796 /* Transactional Memory parsing routines. */
39798 /* Parse a transaction attribute.
39804 We use this instead of cp_parser_attributes_opt for transactions to avoid
39805 the pedwarn in C++98 mode. */
39808 cp_parser_txn_attribute_opt (cp_parser
*parser
)
39811 tree attr_name
, attr
= NULL
;
39813 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
39814 return cp_parser_attributes_opt (parser
);
39816 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
39818 cp_lexer_consume_token (parser
->lexer
);
39819 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
39822 token
= cp_lexer_peek_token (parser
->lexer
);
39823 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
39825 token
= cp_lexer_consume_token (parser
->lexer
);
39827 attr_name
= (token
->type
== CPP_KEYWORD
39828 /* For keywords, use the canonical spelling,
39829 not the parsed identifier. */
39830 ? ridpointers
[(int) token
->keyword
]
39832 attr
= build_tree_list (attr_name
, NULL_TREE
);
39835 cp_parser_error (parser
, "expected identifier");
39837 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39839 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39843 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39845 transaction-statement:
39846 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39848 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39852 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
39854 unsigned char old_in
= parser
->in_transaction
;
39855 unsigned char this_in
= 1, new_in
;
39856 enum rid keyword
= token
->keyword
;
39857 tree stmt
, attrs
, noex
;
39859 cp_lexer_consume_token (parser
->lexer
);
39861 if (keyword
== RID_TRANSACTION_RELAXED
39862 || keyword
== RID_SYNCHRONIZED
)
39863 this_in
|= TM_STMT_ATTR_RELAXED
;
39866 attrs
= cp_parser_txn_attribute_opt (parser
);
39868 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
39871 /* Parse a noexcept specification. */
39872 if (keyword
== RID_ATOMIC_NOEXCEPT
)
39873 noex
= boolean_true_node
;
39874 else if (keyword
== RID_ATOMIC_CANCEL
)
39876 /* cancel-and-throw is unimplemented. */
39877 sorry ("atomic_cancel");
39881 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
39883 /* Keep track if we're in the lexical scope of an outer transaction. */
39884 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
39886 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
39888 parser
->in_transaction
= new_in
;
39889 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
39890 parser
->in_transaction
= old_in
;
39892 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
39897 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39899 transaction-expression:
39900 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39901 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39905 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
39907 unsigned char old_in
= parser
->in_transaction
;
39908 unsigned char this_in
= 1;
39912 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39914 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
39915 || keyword
== RID_TRANSACTION_RELAXED
);
39919 keyword
== RID_TRANSACTION_RELAXED
39920 ? G_("%<__transaction_relaxed%> without transactional memory "
39922 : G_("%<__transaction_atomic%> without transactional memory "
39923 "support enabled"));
39925 token
= cp_parser_require_keyword (parser
, keyword
,
39926 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
39927 : RT_TRANSACTION_RELAXED
));
39928 gcc_assert (token
!= NULL
);
39930 if (keyword
== RID_TRANSACTION_RELAXED
)
39931 this_in
|= TM_STMT_ATTR_RELAXED
;
39933 /* Set this early. This might mean that we allow transaction_cancel in
39934 an expression that we find out later actually has to be a constexpr.
39935 However, we expect that cxx_constant_value will be able to deal with
39936 this; also, if the noexcept has no constexpr, then what we parse next
39937 really is a transaction's body. */
39938 parser
->in_transaction
= this_in
;
39940 /* Parse a noexcept specification. */
39941 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
39944 if (!noex
|| !noex_expr
39945 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39947 matching_parens parens
;
39948 parens
.require_open (parser
);
39950 expr
= cp_parser_expression (parser
);
39951 expr
= finish_parenthesized_expr (expr
);
39953 parens
.require_close (parser
);
39957 /* The only expression that is available got parsed for the noexcept
39958 already. noexcept is true then. */
39960 noex
= boolean_true_node
;
39963 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
39964 parser
->in_transaction
= old_in
;
39966 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
39967 return error_mark_node
;
39969 return (flag_tm
? expr
: error_mark_node
);
39972 /* Parse a function-transaction-block.
39974 function-transaction-block:
39975 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39977 __transaction_atomic txn-attribute[opt] function-try-block
39978 __transaction_relaxed ctor-initializer[opt] function-body
39979 __transaction_relaxed function-try-block
39983 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
39985 unsigned char old_in
= parser
->in_transaction
;
39986 unsigned char new_in
= 1;
39987 tree compound_stmt
, stmt
, attrs
;
39990 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
39991 || keyword
== RID_TRANSACTION_RELAXED
);
39992 token
= cp_parser_require_keyword (parser
, keyword
,
39993 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
39994 : RT_TRANSACTION_RELAXED
));
39995 gcc_assert (token
!= NULL
);
39997 if (keyword
== RID_TRANSACTION_RELAXED
)
39998 new_in
|= TM_STMT_ATTR_RELAXED
;
40001 attrs
= cp_parser_txn_attribute_opt (parser
);
40003 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40006 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40008 parser
->in_transaction
= new_in
;
40010 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40011 cp_parser_function_try_block (parser
);
40013 cp_parser_ctor_initializer_opt_and_function_body
40014 (parser
, /*in_function_try_block=*/false);
40016 parser
->in_transaction
= old_in
;
40018 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40021 /* Parse a __transaction_cancel statement.
40024 __transaction_cancel txn-attribute[opt] ;
40025 __transaction_cancel txn-attribute[opt] throw-expression ;
40027 ??? Cancel and throw is not yet implemented. */
40030 cp_parser_transaction_cancel (cp_parser
*parser
)
40033 bool is_outer
= false;
40036 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40037 RT_TRANSACTION_CANCEL
);
40038 gcc_assert (token
!= NULL
);
40040 attrs
= cp_parser_txn_attribute_opt (parser
);
40042 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40044 /* ??? Parse cancel-and-throw here. */
40046 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40050 error_at (token
->location
, "%<__transaction_cancel%> without "
40051 "transactional memory support enabled");
40052 return error_mark_node
;
40054 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40056 error_at (token
->location
, "%<__transaction_cancel%> within a "
40057 "%<__transaction_relaxed%>");
40058 return error_mark_node
;
40062 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40063 && !is_tm_may_cancel_outer (current_function_decl
))
40065 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40066 "within outer %<__transaction_atomic%>");
40067 error_at (token
->location
,
40068 " or a %<transaction_may_cancel_outer%> function");
40069 return error_mark_node
;
40072 else if (parser
->in_transaction
== 0)
40074 error_at (token
->location
, "%<__transaction_cancel%> not within "
40075 "%<__transaction_atomic%>");
40076 return error_mark_node
;
40079 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40087 static GTY (()) cp_parser
*the_parser
;
40090 /* Special handling for the first token or line in the file. The first
40091 thing in the file might be #pragma GCC pch_preprocess, which loads a
40092 PCH file, which is a GC collection point. So we need to handle this
40093 first pragma without benefit of an existing lexer structure.
40095 Always returns one token to the caller in *FIRST_TOKEN. This is
40096 either the true first token of the file, or the first token after
40097 the initial pragma. */
40100 cp_parser_initial_pragma (cp_token
*first_token
)
40104 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40105 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40108 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40109 if (first_token
->type
== CPP_STRING
)
40111 name
= first_token
->u
.value
;
40113 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40114 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40115 error_at (first_token
->location
,
40116 "junk at end of %<#pragma GCC pch_preprocess%>");
40119 error_at (first_token
->location
, "expected string literal");
40121 /* Skip to the end of the pragma. */
40122 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40123 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40125 /* Now actually load the PCH file. */
40127 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40129 /* Read one more token to return to our caller. We have to do this
40130 after reading the PCH file in, since its pointers have to be
40132 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40135 /* Parse a pragma GCC ivdep. */
40138 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40140 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40144 /* Parse a pragma GCC unroll. */
40146 static unsigned short
40147 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40149 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40150 tree expr
= cp_parser_constant_expression (parser
);
40151 unsigned short unroll
;
40152 expr
= maybe_constant_value (expr
);
40153 HOST_WIDE_INT lunroll
= 0;
40154 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40155 || TREE_CODE (expr
) != INTEGER_CST
40156 || (lunroll
= tree_to_shwi (expr
)) < 0
40157 || lunroll
>= USHRT_MAX
)
40159 error_at (location
, "%<#pragma GCC unroll%> requires an"
40160 " assignment-expression that evaluates to a non-negative"
40161 " integral constant less than %u", USHRT_MAX
);
40166 unroll
= (unsigned short)lunroll
;
40170 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40174 /* Normal parsing of a pragma token. Here we can (and must) use the
40178 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40180 cp_token
*pragma_tok
;
40185 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40186 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40187 parser
->lexer
->in_pragma
= true;
40189 id
= cp_parser_pragma_kind (pragma_tok
);
40190 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40191 cp_ensure_no_omp_declare_simd (parser
);
40194 case PRAGMA_GCC_PCH_PREPROCESS
:
40195 error_at (pragma_tok
->location
,
40196 "%<#pragma GCC pch_preprocess%> must be first");
40199 case PRAGMA_OMP_BARRIER
:
40202 case pragma_compound
:
40203 cp_parser_omp_barrier (parser
, pragma_tok
);
40206 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40207 "used in compound statements", "omp barrier");
40214 case PRAGMA_OMP_DEPOBJ
:
40217 case pragma_compound
:
40218 cp_parser_omp_depobj (parser
, pragma_tok
);
40221 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40222 "used in compound statements", "omp depobj");
40229 case PRAGMA_OMP_FLUSH
:
40232 case pragma_compound
:
40233 cp_parser_omp_flush (parser
, pragma_tok
);
40236 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40237 "used in compound statements", "omp flush");
40244 case PRAGMA_OMP_TASKWAIT
:
40247 case pragma_compound
:
40248 cp_parser_omp_taskwait (parser
, pragma_tok
);
40251 error_at (pragma_tok
->location
,
40252 "%<#pragma %s%> may only be used in compound statements",
40260 case PRAGMA_OMP_TASKYIELD
:
40263 case pragma_compound
:
40264 cp_parser_omp_taskyield (parser
, pragma_tok
);
40267 error_at (pragma_tok
->location
,
40268 "%<#pragma %s%> may only be used in compound statements",
40276 case PRAGMA_OMP_CANCEL
:
40279 case pragma_compound
:
40280 cp_parser_omp_cancel (parser
, pragma_tok
);
40283 error_at (pragma_tok
->location
,
40284 "%<#pragma %s%> may only be used in compound statements",
40292 case PRAGMA_OMP_CANCELLATION_POINT
:
40293 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40296 case PRAGMA_OMP_THREADPRIVATE
:
40297 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40300 case PRAGMA_OMP_DECLARE
:
40301 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40303 case PRAGMA_OACC_DECLARE
:
40304 cp_parser_oacc_declare (parser
, pragma_tok
);
40307 case PRAGMA_OACC_ENTER_DATA
:
40308 if (context
== pragma_stmt
)
40310 error_at (pragma_tok
->location
,
40311 "%<#pragma %s%> may only be used in compound statements",
40315 else if (context
!= pragma_compound
)
40317 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40320 case PRAGMA_OACC_EXIT_DATA
:
40321 if (context
== pragma_stmt
)
40323 error_at (pragma_tok
->location
,
40324 "%<#pragma %s%> may only be used in compound statements",
40328 else if (context
!= pragma_compound
)
40330 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40333 case PRAGMA_OACC_ROUTINE
:
40334 if (context
!= pragma_external
)
40336 error_at (pragma_tok
->location
,
40337 "%<#pragma acc routine%> must be at file scope");
40340 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40343 case PRAGMA_OACC_UPDATE
:
40344 if (context
== pragma_stmt
)
40346 error_at (pragma_tok
->location
,
40347 "%<#pragma %s%> may only be used in compound statements",
40351 else if (context
!= pragma_compound
)
40353 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40356 case PRAGMA_OACC_WAIT
:
40357 if (context
== pragma_stmt
)
40359 error_at (pragma_tok
->location
,
40360 "%<#pragma %s%> may only be used in compound statements",
40364 else if (context
!= pragma_compound
)
40366 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40369 case PRAGMA_OACC_ATOMIC
:
40370 case PRAGMA_OACC_CACHE
:
40371 case PRAGMA_OACC_DATA
:
40372 case PRAGMA_OACC_HOST_DATA
:
40373 case PRAGMA_OACC_KERNELS
:
40374 case PRAGMA_OACC_PARALLEL
:
40375 case PRAGMA_OACC_LOOP
:
40376 case PRAGMA_OMP_ATOMIC
:
40377 case PRAGMA_OMP_CRITICAL
:
40378 case PRAGMA_OMP_DISTRIBUTE
:
40379 case PRAGMA_OMP_FOR
:
40380 case PRAGMA_OMP_MASTER
:
40381 case PRAGMA_OMP_PARALLEL
:
40382 case PRAGMA_OMP_SECTIONS
:
40383 case PRAGMA_OMP_SIMD
:
40384 case PRAGMA_OMP_SINGLE
:
40385 case PRAGMA_OMP_TASK
:
40386 case PRAGMA_OMP_TASKGROUP
:
40387 case PRAGMA_OMP_TASKLOOP
:
40388 case PRAGMA_OMP_TEAMS
:
40389 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40391 stmt
= push_omp_privatization_clauses (false);
40392 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40393 pop_omp_privatization_clauses (stmt
);
40396 case PRAGMA_OMP_REQUIRES
:
40397 return cp_parser_omp_requires (parser
, pragma_tok
);
40399 case PRAGMA_OMP_ORDERED
:
40400 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40402 stmt
= push_omp_privatization_clauses (false);
40403 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40404 pop_omp_privatization_clauses (stmt
);
40407 case PRAGMA_OMP_TARGET
:
40408 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40410 stmt
= push_omp_privatization_clauses (false);
40411 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40412 pop_omp_privatization_clauses (stmt
);
40415 case PRAGMA_OMP_END_DECLARE_TARGET
:
40416 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40419 case PRAGMA_OMP_SECTION
:
40420 error_at (pragma_tok
->location
,
40421 "%<#pragma omp section%> may only be used in "
40422 "%<#pragma omp sections%> construct");
40427 if (context
== pragma_external
)
40429 error_at (pragma_tok
->location
,
40430 "%<#pragma GCC ivdep%> must be inside a function");
40433 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40434 unsigned short unroll
;
40435 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40436 if (tok
->type
== CPP_PRAGMA
40437 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40439 tok
= cp_lexer_consume_token (parser
->lexer
);
40440 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40441 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40445 if (tok
->type
!= CPP_KEYWORD
40446 || (tok
->keyword
!= RID_FOR
40447 && tok
->keyword
!= RID_WHILE
40448 && tok
->keyword
!= RID_DO
))
40450 cp_parser_error (parser
, "for, while or do statement expected");
40453 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40457 case PRAGMA_UNROLL
:
40459 if (context
== pragma_external
)
40461 error_at (pragma_tok
->location
,
40462 "%<#pragma GCC unroll%> must be inside a function");
40465 const unsigned short unroll
40466 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40468 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40469 if (tok
->type
== CPP_PRAGMA
40470 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40472 tok
= cp_lexer_consume_token (parser
->lexer
);
40473 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40474 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40478 if (tok
->type
!= CPP_KEYWORD
40479 || (tok
->keyword
!= RID_FOR
40480 && tok
->keyword
!= RID_WHILE
40481 && tok
->keyword
!= RID_DO
))
40483 cp_parser_error (parser
, "for, while or do statement expected");
40486 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40491 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40492 c_invoke_pragma_handler (id
);
40496 cp_parser_error (parser
, "expected declaration specifiers");
40500 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40504 /* The interface the pragma parsers have to the lexer. */
40507 pragma_lex (tree
*value
, location_t
*loc
)
40509 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40510 enum cpp_ttype ret
= tok
->type
;
40512 *value
= tok
->u
.value
;
40514 *loc
= tok
->location
;
40516 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
40518 else if (ret
== CPP_STRING
)
40519 *value
= cp_parser_string_literal (the_parser
, false, false);
40522 if (ret
== CPP_KEYWORD
)
40524 cp_lexer_consume_token (the_parser
->lexer
);
40531 /* External interface. */
40533 /* Parse one entire translation unit. */
40536 c_parse_file (void)
40538 static bool already_called
= false;
40540 if (already_called
)
40541 fatal_error (input_location
,
40542 "inter-module optimizations not implemented for C++");
40543 already_called
= true;
40545 the_parser
= cp_parser_new ();
40546 push_deferring_access_checks (flag_access_control
40547 ? dk_no_deferred
: dk_no_check
);
40548 cp_parser_translation_unit (the_parser
);
40551 finish_translation_unit ();
40554 /* Create an identifier for a generic parameter type (a synthesized
40555 template parameter implied by `auto' or a concept identifier). */
40557 static GTY(()) int generic_parm_count
;
40559 make_generic_type_name ()
40562 sprintf (buf
, "auto:%d", ++generic_parm_count
);
40563 return get_identifier (buf
);
40566 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40567 (creating a new template parameter list if necessary). Returns the newly
40568 created template type parm. */
40571 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
40573 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
40575 /* Before committing to modifying any scope, if we're in an
40576 implicit template scope, and we're trying to synthesize a
40577 constrained parameter, try to find a previous parameter with
40578 the same name. This is the same-type rule for abbreviated
40579 function templates.
40581 NOTE: We can generate implicit parameters when tentatively
40582 parsing a nested name specifier, only to reject that parse
40583 later. However, matching the same template-id as part of a
40584 direct-declarator should generate an identical template
40585 parameter, so this rule will merge them. */
40586 if (parser
->implicit_template_scope
&& constr
)
40588 tree t
= parser
->implicit_template_parms
;
40591 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
40593 tree d
= TREE_VALUE (t
);
40594 if (TREE_CODE (d
) == PARM_DECL
)
40595 /* Return the TEMPLATE_PARM_INDEX. */
40596 d
= DECL_INITIAL (d
);
40599 t
= TREE_CHAIN (t
);
40603 /* We are either continuing a function template that already contains implicit
40604 template parameters, creating a new fully-implicit function template, or
40605 extending an existing explicit function template with implicit template
40608 cp_binding_level
*const entry_scope
= current_binding_level
;
40610 bool become_template
= false;
40611 cp_binding_level
*parent_scope
= 0;
40613 if (parser
->implicit_template_scope
)
40615 gcc_assert (parser
->implicit_template_parms
);
40617 current_binding_level
= parser
->implicit_template_scope
;
40621 /* Roll back to the existing template parameter scope (in the case of
40622 extending an explicit function template) or introduce a new template
40623 parameter scope ahead of the function parameter scope (or class scope
40624 in the case of out-of-line member definitions). The function scope is
40625 added back after template parameter synthesis below. */
40627 cp_binding_level
*scope
= entry_scope
;
40629 while (scope
->kind
== sk_function_parms
)
40631 parent_scope
= scope
;
40632 scope
= scope
->level_chain
;
40634 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
40636 /* If not defining a class, then any class scope is a scope level in
40637 an out-of-line member definition. In this case simply wind back
40638 beyond the first such scope to inject the template parameter list.
40639 Otherwise wind back to the class being defined. The latter can
40640 occur in class member friend declarations such as:
40646 friend void A::foo (auto);
40649 The template parameter list synthesized for the friend declaration
40650 must be injected in the scope of 'B'. This can also occur in
40651 erroneous cases such as:
40657 void B::foo (auto) {}
40660 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40661 but, nevertheless, the template parameter list synthesized for the
40662 declarator should be injected into the scope of 'A' as if the
40663 ill-formed template was specified explicitly. */
40665 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
40667 parent_scope
= scope
;
40668 scope
= scope
->level_chain
;
40672 current_binding_level
= scope
;
40674 if (scope
->kind
!= sk_template_parms
40675 || !function_being_declared_is_template_p (parser
))
40677 /* Introduce a new template parameter list for implicit template
40680 become_template
= true;
40682 parser
->implicit_template_scope
40683 = begin_scope (sk_template_parms
, NULL
);
40685 ++processing_template_decl
;
40687 parser
->fully_implicit_function_template_p
= true;
40688 ++parser
->num_template_parameter_lists
;
40692 /* Synthesize implicit template parameters at the end of the explicit
40693 template parameter list. */
40695 gcc_assert (current_template_parms
);
40697 parser
->implicit_template_scope
= scope
;
40699 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40700 parser
->implicit_template_parms
40701 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
40705 /* Synthesize a new template parameter and track the current template
40706 parameter chain with implicit_template_parms. */
40708 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
40709 tree synth_id
= make_generic_type_name ();
40710 tree synth_tmpl_parm
;
40711 bool non_type
= false;
40713 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
40715 = finish_template_type_parm (class_type_node
, synth_id
);
40716 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
40718 = finish_constrained_template_template_parm (proto
, synth_id
);
40721 synth_tmpl_parm
= copy_decl (proto
);
40722 DECL_NAME (synth_tmpl_parm
) = synth_id
;
40726 // Attach the constraint to the parm before processing.
40727 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
40728 TREE_TYPE (node
) = constr
;
40730 = process_template_parm (parser
->implicit_template_parms
,
40733 /*non_type=*/non_type
,
40734 /*param_pack=*/false);
40736 // Chain the new parameter to the list of implicit parameters.
40737 if (parser
->implicit_template_parms
)
40738 parser
->implicit_template_parms
40739 = TREE_CHAIN (parser
->implicit_template_parms
);
40741 parser
->implicit_template_parms
= new_parm
;
40743 tree new_decl
= get_local_decls ();
40745 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40746 new_decl
= DECL_INITIAL (new_decl
);
40748 /* If creating a fully implicit function template, start the new implicit
40749 template parameter list with this synthesized type, otherwise grow the
40750 current template parameter list. */
40752 if (become_template
)
40754 parent_scope
->level_chain
= current_binding_level
;
40756 tree new_parms
= make_tree_vec (1);
40757 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
40758 current_template_parms
= tree_cons (size_int (processing_template_decl
),
40759 new_parms
, current_template_parms
);
40763 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40764 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
40765 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
40766 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
40769 // If the new parameter was constrained, we need to add that to the
40770 // constraints in the template parameter list.
40771 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
40773 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
40774 reqs
= conjoin_constraints (reqs
, req
);
40775 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
40778 current_binding_level
= entry_scope
;
40783 /* Finish the declaration of a fully implicit function template. Such a
40784 template has no explicit template parameter list so has not been through the
40785 normal template head and tail processing. synthesize_implicit_template_parm
40786 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40787 provided if the declaration is a class member such that its template
40788 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40789 form is returned. Otherwise NULL_TREE is returned. */
40792 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
40794 gcc_assert (parser
->fully_implicit_function_template_p
);
40796 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
40797 && DECL_VIRTUAL_P (member_decl_opt
))
40799 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
40800 "implicit templates may not be %<virtual%>");
40801 DECL_VIRTUAL_P (member_decl_opt
) = false;
40804 if (member_decl_opt
)
40805 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
40806 end_template_decl ();
40808 parser
->fully_implicit_function_template_p
= false;
40809 parser
->implicit_template_parms
= 0;
40810 parser
->implicit_template_scope
= 0;
40811 --parser
->num_template_parameter_lists
;
40813 return member_decl_opt
;
40816 /* Like finish_fully_implicit_template, but to be used in error
40817 recovery, rearranging scopes so that we restore the state we had
40818 before synthesize_implicit_template_parm inserted the implement
40819 template parms scope. */
40822 abort_fully_implicit_template (cp_parser
*parser
)
40824 cp_binding_level
*return_to_scope
= current_binding_level
;
40826 if (parser
->implicit_template_scope
40827 && return_to_scope
!= parser
->implicit_template_scope
)
40829 cp_binding_level
*child
= return_to_scope
;
40830 for (cp_binding_level
*scope
= child
->level_chain
;
40831 scope
!= parser
->implicit_template_scope
;
40832 scope
= child
->level_chain
)
40834 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
40835 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
40836 current_binding_level
= parser
->implicit_template_scope
;
40839 return_to_scope
= return_to_scope
->level_chain
;
40841 finish_fully_implicit_template (parser
, NULL
);
40843 gcc_assert (current_binding_level
== return_to_scope
);
40846 /* Helper function for diagnostics that have complained about things
40847 being used with 'extern "C"' linkage.
40849 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40852 maybe_show_extern_c_location (void)
40854 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
40855 inform (the_parser
->innermost_linkage_specification_location
,
40856 "%<extern \"C\"%> linkage started here");
40859 #include "gt-cp-parser.h"