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
)
4263 decl
= lookup_name (name
);
4264 if (!decl
|| !is_overloaded_fn (decl
))
4265 return error_mark_node
;
4267 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4272 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4273 if (parmtypes
!= NULL_TREE
)
4275 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4276 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4278 tree tparm
= TREE_VALUE (parmtypes
);
4279 tree targ
= TREE_TYPE ((*args
)[ix
]);
4280 bool ptr
= TYPE_PTR_P (tparm
);
4281 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4282 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4284 || !same_type_p (TREE_TYPE (tparm
),
4289 && ix
== vec_safe_length (args
)
4290 /* May be this should be sufficient_parms_p instead,
4291 depending on how exactly should user-defined literals
4292 work in presence of default arguments on the literal
4293 operator parameters. */
4294 && parmtypes
== void_list_node
)
4299 return error_mark_node
;
4302 /* Parse a user-defined char constant. Returns a call to a user-defined
4303 literal operator taking the character as an argument. */
4306 cp_parser_userdef_char_literal (cp_parser
*parser
)
4308 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4309 tree literal
= token
->u
.value
;
4310 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4311 tree value
= USERDEF_LITERAL_VALUE (literal
);
4312 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4315 /* Build up a call to the user-defined operator */
4316 /* Lookup the name we got back from the id-expression. */
4317 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4318 vec_safe_push (args
, value
);
4319 decl
= lookup_literal_operator (name
, args
);
4320 if (!decl
|| decl
== error_mark_node
)
4322 error ("unable to find character literal operator %qD with %qT argument",
4323 name
, TREE_TYPE (value
));
4324 release_tree_vector (args
);
4325 return error_mark_node
;
4327 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4328 release_tree_vector (args
);
4332 /* A subroutine of cp_parser_userdef_numeric_literal to
4333 create a char... template parameter pack from a string node. */
4336 make_char_string_pack (tree value
)
4339 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4340 const char *str
= TREE_STRING_POINTER (value
);
4341 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4342 tree argvec
= make_tree_vec (1);
4344 /* Fill in CHARVEC with all of the parameters. */
4345 charvec
= make_tree_vec (len
);
4346 for (i
= 0; i
< len
; ++i
)
4348 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4349 cpp_string in
= { 3, s
};
4350 cpp_string out
= { 0, 0 };
4351 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4353 gcc_assert (out
.len
== 2);
4354 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4358 /* Build the argument packs. */
4359 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4361 TREE_VEC_ELT (argvec
, 0) = argpack
;
4366 /* A subroutine of cp_parser_userdef_numeric_literal to
4367 create a char... template parameter pack from a string node. */
4370 make_string_pack (tree value
)
4373 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4374 const unsigned char *str
4375 = (const unsigned char *) TREE_STRING_POINTER (value
);
4376 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4377 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4378 tree argvec
= make_tree_vec (2);
4380 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4381 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4383 /* First template parm is character type. */
4384 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4386 /* Fill in CHARVEC with all of the parameters. */
4387 charvec
= make_tree_vec (len
);
4388 for (int i
= 0; i
< len
; ++i
)
4389 TREE_VEC_ELT (charvec
, i
)
4390 = double_int_to_tree (str_char_type_node
,
4391 double_int::from_buffer (str
+ i
* sz
, sz
));
4393 /* Build the argument packs. */
4394 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4396 TREE_VEC_ELT (argvec
, 1) = argpack
;
4401 /* Parse a user-defined numeric constant. returns a call to a user-defined
4402 literal operator. */
4405 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4407 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4408 tree literal
= token
->u
.value
;
4409 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4410 tree value
= USERDEF_LITERAL_VALUE (literal
);
4411 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4412 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4413 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4415 vec
<tree
, va_gc
> *args
;
4417 /* Look for a literal operator taking the exact type of numeric argument
4418 as the literal value. */
4419 args
= make_tree_vector ();
4420 vec_safe_push (args
, value
);
4421 decl
= lookup_literal_operator (name
, args
);
4422 if (decl
&& decl
!= error_mark_node
)
4424 result
= finish_call_expr (decl
, &args
, false, true,
4425 tf_warning_or_error
);
4427 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4429 warning_at (token
->location
, OPT_Woverflow
,
4430 "integer literal exceeds range of %qT type",
4431 long_long_unsigned_type_node
);
4436 warning_at (token
->location
, OPT_Woverflow
,
4437 "floating literal exceeds range of %qT type",
4438 long_double_type_node
);
4439 else if (overflow
< 0)
4440 warning_at (token
->location
, OPT_Woverflow
,
4441 "floating literal truncated to zero");
4444 release_tree_vector (args
);
4447 release_tree_vector (args
);
4449 /* If the numeric argument didn't work, look for a raw literal
4450 operator taking a const char* argument consisting of the number
4451 in string format. */
4452 args
= make_tree_vector ();
4453 vec_safe_push (args
, num_string
);
4454 decl
= lookup_literal_operator (name
, args
);
4455 if (decl
&& decl
!= error_mark_node
)
4457 result
= finish_call_expr (decl
, &args
, false, true,
4458 tf_warning_or_error
);
4459 release_tree_vector (args
);
4462 release_tree_vector (args
);
4464 /* If the raw literal didn't work, look for a non-type template
4465 function with parameter pack char.... Call the function with
4466 template parameter characters representing the number. */
4467 args
= make_tree_vector ();
4468 decl
= lookup_literal_operator (name
, args
);
4469 if (decl
&& decl
!= error_mark_node
)
4471 tree tmpl_args
= make_char_string_pack (num_string
);
4472 if (tmpl_args
== NULL_TREE
)
4474 error ("failed to translate literal to execution character set %qT",
4476 return error_mark_node
;
4478 decl
= lookup_template_function (decl
, tmpl_args
);
4479 result
= finish_call_expr (decl
, &args
, false, true,
4480 tf_warning_or_error
);
4481 release_tree_vector (args
);
4485 release_tree_vector (args
);
4487 /* In C++14 the standard library defines complex number suffixes that
4488 conflict with GNU extensions. Prefer them if <complex> is #included. */
4489 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4490 bool i14
= (cxx_dialect
> cxx11
4491 && (id_equal (suffix_id
, "i")
4492 || id_equal (suffix_id
, "if")
4493 || id_equal (suffix_id
, "il")));
4494 diagnostic_t kind
= DK_ERROR
;
4499 tree cxlit
= lookup_qualified_name (std_node
,
4500 get_identifier ("complex_literals"),
4502 if (cxlit
== error_mark_node
)
4504 /* No <complex>, so pedwarn and use GNU semantics. */
4506 opt
= OPT_Wpedantic
;
4511 = emit_diagnostic (kind
, input_location
, opt
,
4512 "unable to find numeric literal operator %qD", name
);
4515 /* Don't inform either. */;
4518 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4519 "(from <complex>) to enable the C++14 user-defined literal "
4522 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4523 "GNU built-in suffix");
4526 inform (token
->location
, "use -fext-numeric-literals "
4527 "to enable more built-in suffixes");
4529 if (kind
== DK_ERROR
)
4530 value
= error_mark_node
;
4533 /* Use the built-in semantics. */
4535 if (id_equal (suffix_id
, "i"))
4537 if (TREE_CODE (value
) == INTEGER_CST
)
4538 type
= integer_type_node
;
4540 type
= double_type_node
;
4542 else if (id_equal (suffix_id
, "if"))
4543 type
= float_type_node
;
4544 else /* if (id_equal (suffix_id, "il")) */
4545 type
= long_double_type_node
;
4547 value
= build_complex (build_complex_type (type
),
4548 fold_convert (type
, integer_zero_node
),
4549 fold_convert (type
, value
));
4552 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4553 /* Avoid repeated diagnostics. */
4554 token
->u
.value
= value
;
4558 /* Parse a user-defined string constant. Returns a call to a user-defined
4559 literal operator taking a character pointer and the length of the string
4563 cp_parser_userdef_string_literal (tree literal
)
4565 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4566 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4567 tree value
= USERDEF_LITERAL_VALUE (literal
);
4568 int len
= TREE_STRING_LENGTH (value
)
4569 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4572 /* Build up a call to the user-defined operator. */
4573 /* Lookup the name we got back from the id-expression. */
4574 releasing_vec rargs
;
4575 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4576 vec_safe_push (args
, value
);
4577 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4578 decl
= lookup_literal_operator (name
, args
);
4580 if (decl
&& decl
!= error_mark_node
)
4581 return finish_call_expr (decl
, &args
, false, true,
4582 tf_warning_or_error
);
4584 /* Look for a suitable template function, either (C++20) with a single
4585 parameter of class type, or (N3599) with typename parameter CharT and
4586 parameter pack CharT... */
4588 decl
= lookup_literal_operator (name
, args
);
4589 if (decl
&& decl
!= error_mark_node
)
4591 /* Use resolve_nondeduced_context to try to choose one form of template
4593 tree tmpl_args
= make_tree_vec (1);
4594 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4595 decl
= lookup_template_function (decl
, tmpl_args
);
4596 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4601 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4602 res
= resolve_nondeduced_context (decl
, tf_none
);
4606 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4607 TREE_OPERAND (decl
, 1) = tmpl_args
;
4608 return finish_call_expr (decl
, &args
, false, true,
4609 tf_warning_or_error
);
4612 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4613 name
, TREE_TYPE (value
), size_type_node
);
4614 return error_mark_node
;
4618 /* Basic concepts [gram.basic] */
4620 /* Parse a translation-unit.
4623 declaration-seq [opt] */
4626 cp_parser_translation_unit (cp_parser
* parser
)
4628 gcc_checking_assert (!cp_error_declarator
);
4630 /* Create the declarator obstack. */
4631 gcc_obstack_init (&declarator_obstack
);
4632 /* Create the error declarator. */
4633 cp_error_declarator
= make_declarator (cdk_error
);
4634 /* Create the empty parameter list. */
4635 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4637 /* Remember where the base of the declarator obstack lies. */
4638 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4640 bool implicit_extern_c
= false;
4644 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4646 /* If we're entering or exiting a region that's implicitly
4647 extern "C", modify the lang context appropriately. */
4648 if (implicit_extern_c
4649 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4651 implicit_extern_c
= !implicit_extern_c
;
4652 if (implicit_extern_c
)
4653 push_lang_context (lang_name_c
);
4655 pop_lang_context ();
4658 if (token
->type
== CPP_EOF
)
4661 if (token
->type
== CPP_CLOSE_BRACE
)
4663 cp_parser_error (parser
, "expected declaration");
4664 cp_lexer_consume_token (parser
->lexer
);
4665 /* If the next token is now a `;', consume it. */
4666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4667 cp_lexer_consume_token (parser
->lexer
);
4670 cp_parser_toplevel_declaration (parser
);
4673 /* Get rid of the token array; we don't need it any more. */
4674 cp_lexer_destroy (parser
->lexer
);
4675 parser
->lexer
= NULL
;
4677 /* The EOF should have reset this. */
4678 gcc_checking_assert (!implicit_extern_c
);
4680 /* Make sure the declarator obstack was fully cleaned up. */
4681 gcc_assert (obstack_next_free (&declarator_obstack
)
4682 == declarator_obstack_base
);
4685 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4686 decltype context. */
4688 static inline tsubst_flags_t
4689 complain_flags (bool decltype_p
)
4691 tsubst_flags_t complain
= tf_warning_or_error
;
4693 complain
|= tf_decltype
;
4697 /* We're about to parse a collection of statements. If we're currently
4698 parsing tentatively, set up a firewall so that any nested
4699 cp_parser_commit_to_tentative_parse won't affect the current context. */
4701 static cp_token_position
4702 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4704 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4707 cp_parser_parse_tentatively (parser
);
4708 cp_parser_commit_to_topmost_tentative_parse (parser
);
4709 return cp_lexer_token_position (parser
->lexer
, false);
4712 /* We've finished parsing the collection of statements. Wrap up the
4713 firewall and replace the relevant tokens with the parsed form. */
4716 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4722 /* Finish the firewall level. */
4723 cp_parser_parse_definitely (parser
);
4724 /* And remember the result of the parse for when we try again. */
4725 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4726 token
->type
= CPP_PREPARSED_EXPR
;
4727 token
->u
.value
= expr
;
4728 token
->keyword
= RID_MAX
;
4729 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4732 /* Like the above functions, but let the user modify the tokens. Used by
4733 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4734 later parses, so it makes sense to localize the effects of
4735 cp_parser_commit_to_tentative_parse. */
4737 struct tentative_firewall
4742 tentative_firewall (cp_parser
*p
): parser(p
)
4744 /* If we're currently parsing tentatively, start a committed level as a
4745 firewall and then an inner tentative parse. */
4746 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4748 cp_parser_parse_tentatively (parser
);
4749 cp_parser_commit_to_topmost_tentative_parse (parser
);
4750 cp_parser_parse_tentatively (parser
);
4754 ~tentative_firewall()
4758 /* Finish the inner tentative parse and the firewall, propagating any
4759 uncommitted error state to the outer tentative parse. */
4760 bool err
= cp_parser_error_occurred (parser
);
4761 cp_parser_parse_definitely (parser
);
4762 cp_parser_parse_definitely (parser
);
4764 cp_parser_simulate_error (parser
);
4769 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4770 This class is for tracking such a matching pair of symbols.
4771 In particular, it tracks the location of the first token,
4772 so that if the second token is missing, we can highlight the
4773 location of the first token when notifying the user about the
4776 template <typename traits_t
>
4780 /* token_pair's ctor. */
4781 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4783 /* If the next token is the opening symbol for this pair, consume it and
4785 Otherwise, issue an error and return false.
4786 In either case, record the location of the opening token. */
4788 bool require_open (cp_parser
*parser
)
4790 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4791 return cp_parser_require (parser
, traits_t::open_token_type
,
4792 traits_t::required_token_open
);
4795 /* Consume the next token from PARSER, recording its location as
4796 that of the opening token within the pair. */
4798 cp_token
* consume_open (cp_parser
*parser
)
4800 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4801 gcc_assert (tok
->type
== traits_t::open_token_type
);
4802 m_open_loc
= tok
->location
;
4806 /* If the next token is the closing symbol for this pair, consume it
4808 Otherwise, issue an error, highlighting the location of the
4809 corresponding opening token, and return NULL. */
4811 cp_token
*require_close (cp_parser
*parser
) const
4813 return cp_parser_require (parser
, traits_t::close_token_type
,
4814 traits_t::required_token_close
,
4819 location_t m_open_loc
;
4822 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4824 struct matching_paren_traits
4826 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4827 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4828 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4829 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4832 /* "matching_parens" is a token_pair<T> class for tracking matching
4833 pairs of parentheses. */
4835 typedef token_pair
<matching_paren_traits
> matching_parens
;
4837 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4839 struct matching_brace_traits
4841 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4842 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4843 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4844 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4847 /* "matching_braces" is a token_pair<T> class for tracking matching
4850 typedef token_pair
<matching_brace_traits
> matching_braces
;
4853 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4854 enclosing parentheses. */
4857 cp_parser_statement_expr (cp_parser
*parser
)
4859 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4861 /* Consume the '('. */
4862 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4863 matching_parens parens
;
4864 parens
.consume_open (parser
);
4865 /* Start the statement-expression. */
4866 tree expr
= begin_stmt_expr ();
4867 /* Parse the compound-statement. */
4868 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4870 expr
= finish_stmt_expr (expr
, false);
4871 /* Consume the ')'. */
4872 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4873 if (!parens
.require_close (parser
))
4874 cp_parser_skip_to_end_of_statement (parser
);
4876 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4877 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4878 return cp_expr (expr
, combined_loc
);
4881 /* Expressions [gram.expr] */
4883 /* Parse a fold-operator.
4886 - * / % ^ & | = < > << >>
4887 = -= *= /= %= ^= &= |= <<= >>=
4888 == != <= >= && || , .* ->*
4890 This returns the tree code corresponding to the matched operator
4891 as an int. When the current token matches a compound assignment
4892 opertor, the resulting tree code is the negative value of the
4893 non-assignment operator. */
4896 cp_parser_fold_operator (cp_token
*token
)
4898 switch (token
->type
)
4900 case CPP_PLUS
: return PLUS_EXPR
;
4901 case CPP_MINUS
: return MINUS_EXPR
;
4902 case CPP_MULT
: return MULT_EXPR
;
4903 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4904 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4905 case CPP_XOR
: return BIT_XOR_EXPR
;
4906 case CPP_AND
: return BIT_AND_EXPR
;
4907 case CPP_OR
: return BIT_IOR_EXPR
;
4908 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4909 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4911 case CPP_EQ
: return -NOP_EXPR
;
4912 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4913 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4914 case CPP_MULT_EQ
: return -MULT_EXPR
;
4915 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4916 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4917 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4918 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4919 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4920 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4921 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4923 case CPP_EQ_EQ
: return EQ_EXPR
;
4924 case CPP_NOT_EQ
: return NE_EXPR
;
4925 case CPP_LESS
: return LT_EXPR
;
4926 case CPP_GREATER
: return GT_EXPR
;
4927 case CPP_LESS_EQ
: return LE_EXPR
;
4928 case CPP_GREATER_EQ
: return GE_EXPR
;
4930 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4931 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4933 case CPP_COMMA
: return COMPOUND_EXPR
;
4935 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4936 case CPP_DEREF_STAR
: return MEMBER_REF
;
4938 default: return ERROR_MARK
;
4942 /* Returns true if CODE indicates a binary expression, which is not allowed in
4943 the LHS of a fold-expression. More codes will need to be added to use this
4944 function in other contexts. */
4947 is_binary_op (tree_code code
)
4952 case POINTER_PLUS_EXPR
:
4955 case TRUNC_DIV_EXPR
:
4956 case TRUNC_MOD_EXPR
:
4972 case TRUTH_ANDIF_EXPR
:
4973 case TRUTH_ORIF_EXPR
:
4986 /* If the next token is a suitable fold operator, consume it and return as
4987 the function above. */
4990 cp_parser_fold_operator (cp_parser
*parser
)
4992 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4993 int code
= cp_parser_fold_operator (token
);
4994 if (code
!= ERROR_MARK
)
4995 cp_lexer_consume_token (parser
->lexer
);
4999 /* Parse a fold-expression.
5002 ( ... folding-operator cast-expression)
5003 ( cast-expression folding-operator ... )
5004 ( cast-expression folding operator ... folding-operator cast-expression)
5006 Note that the '(' and ')' are matched in primary expression. */
5009 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5016 cp_lexer_consume_token (parser
->lexer
);
5017 int op
= cp_parser_fold_operator (parser
);
5018 if (op
== ERROR_MARK
)
5020 cp_parser_error (parser
, "expected binary operator");
5021 return error_mark_node
;
5024 tree expr
= cp_parser_cast_expression (parser
, false, false,
5026 if (expr
== error_mark_node
)
5027 return error_mark_node
;
5028 return finish_left_unary_fold_expr (expr
, op
);
5031 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5032 int op
= cp_parser_fold_operator (parser
);
5033 if (op
== ERROR_MARK
)
5035 cp_parser_error (parser
, "expected binary operator");
5036 return error_mark_node
;
5039 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5041 cp_parser_error (parser
, "expected ...");
5042 return error_mark_node
;
5044 cp_lexer_consume_token (parser
->lexer
);
5046 /* The operands of a fold-expression are cast-expressions, so binary or
5047 conditional expressions are not allowed. We check this here to avoid
5048 tentative parsing. */
5049 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5050 /* OK, the expression was parenthesized. */;
5051 else if (is_binary_op (TREE_CODE (expr1
)))
5052 error_at (location_of (expr1
),
5053 "binary expression in operand of fold-expression");
5054 else if (TREE_CODE (expr1
) == COND_EXPR
5055 || (REFERENCE_REF_P (expr1
)
5056 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5057 error_at (location_of (expr1
),
5058 "conditional expression in operand of fold-expression");
5061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5062 return finish_right_unary_fold_expr (expr1
, op
);
5064 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5066 cp_parser_error (parser
, "mismatched operator in fold-expression");
5067 return error_mark_node
;
5069 cp_lexer_consume_token (parser
->lexer
);
5071 // Binary left or right fold.
5072 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5073 if (expr2
== error_mark_node
)
5074 return error_mark_node
;
5075 return finish_binary_fold_expr (expr1
, expr2
, op
);
5078 /* Parse a primary-expression.
5085 lambda-expression (C++11)
5090 ( compound-statement )
5091 __builtin_va_arg ( assignment-expression , type-id )
5092 __builtin_offsetof ( type-id , offsetof-expression )
5095 __has_nothrow_assign ( type-id )
5096 __has_nothrow_constructor ( type-id )
5097 __has_nothrow_copy ( type-id )
5098 __has_trivial_assign ( type-id )
5099 __has_trivial_constructor ( type-id )
5100 __has_trivial_copy ( type-id )
5101 __has_trivial_destructor ( type-id )
5102 __has_virtual_destructor ( type-id )
5103 __is_abstract ( type-id )
5104 __is_base_of ( type-id , type-id )
5105 __is_class ( type-id )
5106 __is_empty ( type-id )
5107 __is_enum ( type-id )
5108 __is_final ( type-id )
5109 __is_literal_type ( type-id )
5110 __is_pod ( type-id )
5111 __is_polymorphic ( type-id )
5112 __is_std_layout ( type-id )
5113 __is_trivial ( type-id )
5114 __is_union ( type-id )
5116 Objective-C++ Extension:
5124 ADDRESS_P is true iff this expression was immediately preceded by
5125 "&" and therefore might denote a pointer-to-member. CAST_P is true
5126 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5127 true iff this expression is a template argument.
5129 Returns a representation of the expression. Upon return, *IDK
5130 indicates what kind of id-expression (if any) was present. */
5133 cp_parser_primary_expression (cp_parser
*parser
,
5136 bool template_arg_p
,
5140 cp_token
*token
= NULL
;
5142 /* Assume the primary expression is not an id-expression. */
5143 *idk
= CP_ID_KIND_NONE
;
5145 /* Peek at the next token. */
5146 token
= cp_lexer_peek_token (parser
->lexer
);
5147 switch ((int) token
->type
)
5156 user-defined-literal */
5163 case CPP_PREPARSED_EXPR
:
5164 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5165 return cp_parser_userdef_numeric_literal (parser
);
5166 token
= cp_lexer_consume_token (parser
->lexer
);
5167 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5169 error_at (token
->location
,
5170 "fixed-point types not supported in C++");
5171 return error_mark_node
;
5173 /* Floating-point literals are only allowed in an integral
5174 constant expression if they are cast to an integral or
5175 enumeration type. */
5176 if (TREE_CODE (token
->u
.value
) == REAL_CST
5177 && parser
->integral_constant_expression_p
5180 /* CAST_P will be set even in invalid code like "int(2.7 +
5181 ...)". Therefore, we have to check that the next token
5182 is sure to end the cast. */
5185 cp_token
*next_token
;
5187 next_token
= cp_lexer_peek_token (parser
->lexer
);
5188 if (/* The comma at the end of an
5189 enumerator-definition. */
5190 next_token
->type
!= CPP_COMMA
5191 /* The curly brace at the end of an enum-specifier. */
5192 && next_token
->type
!= CPP_CLOSE_BRACE
5193 /* The end of a statement. */
5194 && next_token
->type
!= CPP_SEMICOLON
5195 /* The end of the cast-expression. */
5196 && next_token
->type
!= CPP_CLOSE_PAREN
5197 /* The end of an array bound. */
5198 && next_token
->type
!= CPP_CLOSE_SQUARE
5199 /* The closing ">" in a template-argument-list. */
5200 && (next_token
->type
!= CPP_GREATER
5201 || parser
->greater_than_is_operator_p
)
5202 /* C++0x only: A ">>" treated like two ">" tokens,
5203 in a template-argument-list. */
5204 && (next_token
->type
!= CPP_RSHIFT
5205 || (cxx_dialect
== cxx98
)
5206 || parser
->greater_than_is_operator_p
))
5210 /* If we are within a cast, then the constraint that the
5211 cast is to an integral or enumeration type will be
5212 checked at that point. If we are not within a cast, then
5213 this code is invalid. */
5215 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5217 return cp_expr (token
->u
.value
, token
->location
);
5219 case CPP_CHAR_USERDEF
:
5220 case CPP_CHAR16_USERDEF
:
5221 case CPP_CHAR32_USERDEF
:
5222 case CPP_WCHAR_USERDEF
:
5223 case CPP_UTF8CHAR_USERDEF
:
5224 return cp_parser_userdef_char_literal (parser
);
5230 case CPP_UTF8STRING
:
5231 case CPP_STRING_USERDEF
:
5232 case CPP_STRING16_USERDEF
:
5233 case CPP_STRING32_USERDEF
:
5234 case CPP_WSTRING_USERDEF
:
5235 case CPP_UTF8STRING_USERDEF
:
5236 /* ??? Should wide strings be allowed when parser->translate_strings_p
5237 is false (i.e. in attributes)? If not, we can kill the third
5238 argument to cp_parser_string_literal. */
5239 return cp_parser_string_literal (parser
,
5240 parser
->translate_strings_p
,
5243 case CPP_OPEN_PAREN
:
5244 /* If we see `( { ' then we are looking at the beginning of
5245 a GNU statement-expression. */
5246 if (cp_parser_allow_gnu_extensions_p (parser
)
5247 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5249 /* Statement-expressions are not allowed by the standard. */
5250 pedwarn (token
->location
, OPT_Wpedantic
,
5251 "ISO C++ forbids braced-groups within expressions");
5253 /* And they're not allowed outside of a function-body; you
5254 cannot, for example, write:
5256 int i = ({ int j = 3; j + 1; });
5258 at class or namespace scope. */
5259 if (!parser
->in_function_body
5260 || parser
->in_template_argument_list_p
)
5262 error_at (token
->location
,
5263 "statement-expressions are not allowed outside "
5264 "functions nor in template-argument lists");
5265 cp_parser_skip_to_end_of_block_or_statement (parser
);
5266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5267 cp_lexer_consume_token (parser
->lexer
);
5268 return error_mark_node
;
5271 return cp_parser_statement_expr (parser
);
5273 /* Otherwise it's a normal parenthesized expression. */
5276 bool saved_greater_than_is_operator_p
;
5278 location_t open_paren_loc
= token
->location
;
5280 /* Consume the `('. */
5281 matching_parens parens
;
5282 parens
.consume_open (parser
);
5283 /* Within a parenthesized expression, a `>' token is always
5284 the greater-than operator. */
5285 saved_greater_than_is_operator_p
5286 = parser
->greater_than_is_operator_p
;
5287 parser
->greater_than_is_operator_p
= true;
5289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5290 /* Left fold expression. */
5293 /* Parse the parenthesized expression. */
5294 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5296 token
= cp_lexer_peek_token (parser
->lexer
);
5297 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5299 expr
= cp_parser_fold_expression (parser
, expr
);
5300 if (expr
!= error_mark_node
5301 && cxx_dialect
< cxx17
5302 && !in_system_header_at (input_location
))
5303 pedwarn (input_location
, 0, "fold-expressions only available "
5304 "with -std=c++17 or -std=gnu++17");
5307 /* Let the front end know that this expression was
5308 enclosed in parentheses. This matters in case, for
5309 example, the expression is of the form `A::B', since
5310 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5312 expr
= finish_parenthesized_expr (expr
);
5314 /* DR 705: Wrapping an unqualified name in parentheses
5315 suppresses arg-dependent lookup. We want to pass back
5316 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5317 (c++/37862), but none of the others. */
5318 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5319 *idk
= CP_ID_KIND_NONE
;
5321 /* The `>' token might be the end of a template-id or
5322 template-parameter-list now. */
5323 parser
->greater_than_is_operator_p
5324 = saved_greater_than_is_operator_p
;
5326 /* Consume the `)'. */
5327 token
= cp_lexer_peek_token (parser
->lexer
);
5328 location_t close_paren_loc
= token
->location
;
5329 expr
.set_range (open_paren_loc
, close_paren_loc
);
5330 if (!parens
.require_close (parser
)
5331 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5332 cp_parser_skip_to_end_of_statement (parser
);
5337 case CPP_OPEN_SQUARE
:
5339 if (c_dialect_objc ())
5341 /* We might have an Objective-C++ message. */
5342 cp_parser_parse_tentatively (parser
);
5343 tree msg
= cp_parser_objc_message_expression (parser
);
5344 /* If that works out, we're done ... */
5345 if (cp_parser_parse_definitely (parser
))
5347 /* ... else, fall though to see if it's a lambda. */
5349 cp_expr lam
= cp_parser_lambda_expression (parser
);
5350 /* Don't warn about a failed tentative parse. */
5351 if (cp_parser_error_occurred (parser
))
5352 return error_mark_node
;
5353 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5357 case CPP_OBJC_STRING
:
5358 if (c_dialect_objc ())
5359 /* We have an Objective-C++ string literal. */
5360 return cp_parser_objc_expression (parser
);
5361 cp_parser_error (parser
, "expected primary-expression");
5362 return error_mark_node
;
5365 switch (token
->keyword
)
5367 /* These two are the boolean literals. */
5369 cp_lexer_consume_token (parser
->lexer
);
5370 return cp_expr (boolean_true_node
, token
->location
);
5372 cp_lexer_consume_token (parser
->lexer
);
5373 return cp_expr (boolean_false_node
, token
->location
);
5375 /* The `__null' literal. */
5377 cp_lexer_consume_token (parser
->lexer
);
5378 return cp_expr (null_node
, token
->location
);
5380 /* The `nullptr' literal. */
5382 cp_lexer_consume_token (parser
->lexer
);
5383 return cp_expr (nullptr_node
, token
->location
);
5385 /* Recognize the `this' keyword. */
5387 cp_lexer_consume_token (parser
->lexer
);
5388 if (parser
->local_variables_forbidden_p
)
5390 error_at (token
->location
,
5391 "%<this%> may not be used in this context");
5392 return error_mark_node
;
5394 /* Pointers cannot appear in constant-expressions. */
5395 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5396 return error_mark_node
;
5397 return cp_expr (finish_this_expr (), token
->location
);
5399 /* The `operator' keyword can be the beginning of an
5404 case RID_FUNCTION_NAME
:
5405 case RID_PRETTY_FUNCTION_NAME
:
5406 case RID_C99_FUNCTION_NAME
:
5408 non_integral_constant name
;
5410 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5411 __func__ are the names of variables -- but they are
5412 treated specially. Therefore, they are handled here,
5413 rather than relying on the generic id-expression logic
5414 below. Grammatically, these names are id-expressions.
5416 Consume the token. */
5417 token
= cp_lexer_consume_token (parser
->lexer
);
5419 switch (token
->keyword
)
5421 case RID_FUNCTION_NAME
:
5422 name
= NIC_FUNC_NAME
;
5424 case RID_PRETTY_FUNCTION_NAME
:
5425 name
= NIC_PRETTY_FUNC
;
5427 case RID_C99_FUNCTION_NAME
:
5428 name
= NIC_C99_FUNC
;
5434 if (cp_parser_non_integral_constant_expression (parser
, name
))
5435 return error_mark_node
;
5437 /* Look up the name. */
5438 return finish_fname (token
->u
.value
);
5445 location_t type_location
;
5446 location_t start_loc
5447 = cp_lexer_peek_token (parser
->lexer
)->location
;
5448 /* The `__builtin_va_arg' construct is used to handle
5449 `va_arg'. Consume the `__builtin_va_arg' token. */
5450 cp_lexer_consume_token (parser
->lexer
);
5451 /* Look for the opening `('. */
5452 matching_parens parens
;
5453 parens
.require_open (parser
);
5454 /* Now, parse the assignment-expression. */
5455 expression
= cp_parser_assignment_expression (parser
);
5456 /* Look for the `,'. */
5457 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5458 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5459 /* Parse the type-id. */
5461 type_id_in_expr_sentinel
s (parser
);
5462 type
= cp_parser_type_id (parser
);
5464 /* Look for the closing `)'. */
5465 location_t finish_loc
5466 = cp_lexer_peek_token (parser
->lexer
)->location
;
5467 parens
.require_close (parser
);
5468 /* Using `va_arg' in a constant-expression is not
5470 if (cp_parser_non_integral_constant_expression (parser
,
5472 return error_mark_node
;
5473 /* Construct a location of the form:
5474 __builtin_va_arg (v, int)
5475 ~~~~~~~~~~~~~~~~~~~~~^~~~
5476 with the caret at the type, ranging from the start of the
5477 "__builtin_va_arg" token to the close paren. */
5478 location_t combined_loc
5479 = make_location (type_location
, start_loc
, finish_loc
);
5480 return build_x_va_arg (combined_loc
, expression
, type
);
5484 return cp_parser_builtin_offsetof (parser
);
5486 case RID_HAS_NOTHROW_ASSIGN
:
5487 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5488 case RID_HAS_NOTHROW_COPY
:
5489 case RID_HAS_TRIVIAL_ASSIGN
:
5490 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5491 case RID_HAS_TRIVIAL_COPY
:
5492 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5493 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5494 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5495 case RID_IS_ABSTRACT
:
5496 case RID_IS_AGGREGATE
:
5497 case RID_IS_BASE_OF
:
5502 case RID_IS_LITERAL_TYPE
:
5504 case RID_IS_POLYMORPHIC
:
5505 case RID_IS_SAME_AS
:
5506 case RID_IS_STD_LAYOUT
:
5507 case RID_IS_TRIVIAL
:
5508 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5509 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5510 case RID_IS_TRIVIALLY_COPYABLE
:
5512 case RID_IS_ASSIGNABLE
:
5513 case RID_IS_CONSTRUCTIBLE
:
5514 return cp_parser_trait_expr (parser
, token
->keyword
);
5518 return cp_parser_requires_expression (parser
);
5520 /* Objective-C++ expressions. */
5522 case RID_AT_PROTOCOL
:
5523 case RID_AT_SELECTOR
:
5524 return cp_parser_objc_expression (parser
);
5527 if (parser
->in_function_body
5528 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5531 error_at (token
->location
,
5532 "a template declaration cannot appear at block scope");
5533 cp_parser_skip_to_end_of_block_or_statement (parser
);
5534 return error_mark_node
;
5538 cp_parser_error (parser
, "expected primary-expression");
5539 return error_mark_node
;
5542 /* An id-expression can start with either an identifier, a
5543 `::' as the beginning of a qualified-id, or the "operator"
5547 case CPP_TEMPLATE_ID
:
5548 case CPP_NESTED_NAME_SPECIFIER
:
5551 cp_expr id_expression
;
5553 const char *error_msg
;
5556 cp_token
*id_expr_token
;
5558 /* Parse the id-expression. */
5560 = cp_parser_id_expression (parser
,
5561 /*template_keyword_p=*/false,
5562 /*check_dependency_p=*/true,
5564 /*declarator_p=*/false,
5565 /*optional_p=*/false);
5566 if (id_expression
== error_mark_node
)
5567 return error_mark_node
;
5568 id_expr_token
= token
;
5569 token
= cp_lexer_peek_token (parser
->lexer
);
5570 done
= (token
->type
!= CPP_OPEN_SQUARE
5571 && token
->type
!= CPP_OPEN_PAREN
5572 && token
->type
!= CPP_DOT
5573 && token
->type
!= CPP_DEREF
5574 && token
->type
!= CPP_PLUS_PLUS
5575 && token
->type
!= CPP_MINUS_MINUS
);
5576 /* If we have a template-id, then no further lookup is
5577 required. If the template-id was for a template-class, we
5578 will sometimes have a TYPE_DECL at this point. */
5579 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5580 || TREE_CODE (id_expression
) == TYPE_DECL
)
5581 decl
= id_expression
;
5582 /* Look up the name. */
5585 tree ambiguous_decls
;
5587 /* If we already know that this lookup is ambiguous, then
5588 we've already issued an error message; there's no reason
5590 if (id_expr_token
->type
== CPP_NAME
5591 && id_expr_token
->error_reported
)
5593 cp_parser_simulate_error (parser
);
5594 return error_mark_node
;
5597 decl
= cp_parser_lookup_name (parser
, id_expression
,
5600 /*is_namespace=*/false,
5601 /*check_dependency=*/true,
5603 id_expr_token
->location
);
5604 /* If the lookup was ambiguous, an error will already have
5606 if (ambiguous_decls
)
5607 return error_mark_node
;
5609 /* In Objective-C++, we may have an Objective-C 2.0
5610 dot-syntax for classes here. */
5611 if (c_dialect_objc ()
5612 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5613 && TREE_CODE (decl
) == TYPE_DECL
5614 && objc_is_class_name (decl
))
5617 cp_lexer_consume_token (parser
->lexer
);
5618 component
= cp_parser_identifier (parser
);
5619 if (component
== error_mark_node
)
5620 return error_mark_node
;
5622 tree result
= objc_build_class_component_ref (id_expression
,
5624 /* Build a location of the form:
5627 with caret at the start of the component name (at
5628 input_location), ranging from the start of the id_expression
5629 to the end of the component name. */
5630 location_t combined_loc
5631 = make_location (input_location
, id_expression
.get_start (),
5632 get_finish (input_location
));
5633 protected_set_expr_location (result
, combined_loc
);
5637 /* In Objective-C++, an instance variable (ivar) may be preferred
5638 to whatever cp_parser_lookup_name() found.
5639 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5640 rest of c-family, we have to do a little extra work to preserve
5641 any location information in cp_expr "decl". Given that
5642 objc_lookup_ivar is implemented in "c-family" and "objc", we
5643 have a trip through the pure "tree" type, rather than cp_expr.
5644 Naively copying it back to "decl" would implicitly give the
5645 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5646 store an EXPR_LOCATION. Hence we only update "decl" (and
5647 hence its location_t) if we get back a different tree node. */
5648 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5650 if (decl_tree
!= decl
.get_value ())
5651 decl
= cp_expr (decl_tree
);
5653 /* If name lookup gives us a SCOPE_REF, then the
5654 qualifying scope was dependent. */
5655 if (TREE_CODE (decl
) == SCOPE_REF
)
5657 /* At this point, we do not know if DECL is a valid
5658 integral constant expression. We assume that it is
5659 in fact such an expression, so that code like:
5661 template <int N> struct A {
5665 is accepted. At template-instantiation time, we
5666 will check that B<N>::i is actually a constant. */
5669 /* Check to see if DECL is a local variable in a context
5670 where that is forbidden. */
5671 if (parser
->local_variables_forbidden_p
5672 && local_variable_p (decl
))
5674 error_at (id_expr_token
->location
,
5675 "local variable %qD may not appear in this context",
5677 return error_mark_node
;
5681 if (processing_template_decl
)
5682 if (tree fns
= maybe_get_fns (decl
))
5683 /* It's too difficult to mark ths in all the places where
5684 we know for sure we need to keep the lookup, so do it
5685 now. The cost is extra GC to recycle the lookups
5686 resolved at parse time. */
5689 decl
= (finish_id_expression
5690 (id_expression
, decl
, parser
->scope
,
5692 parser
->integral_constant_expression_p
,
5693 parser
->allow_non_integral_constant_expression_p
,
5694 &parser
->non_integral_constant_expression_p
,
5695 template_p
, done
, address_p
,
5698 id_expression
.get_location ()));
5700 cp_parser_error (parser
, error_msg
);
5701 decl
.set_location (id_expr_token
->location
);
5705 /* Anything else is an error. */
5707 cp_parser_error (parser
, "expected primary-expression");
5708 return error_mark_node
;
5712 static inline cp_expr
5713 cp_parser_primary_expression (cp_parser
*parser
,
5716 bool template_arg_p
,
5719 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5720 /*decltype*/false, idk
);
5723 /* Parse an id-expression.
5730 :: [opt] nested-name-specifier template [opt] unqualified-id
5732 :: operator-function-id
5735 Return a representation of the unqualified portion of the
5736 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5737 a `::' or nested-name-specifier.
5739 Often, if the id-expression was a qualified-id, the caller will
5740 want to make a SCOPE_REF to represent the qualified-id. This
5741 function does not do this in order to avoid wastefully creating
5742 SCOPE_REFs when they are not required.
5744 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5747 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5748 uninstantiated templates.
5750 If *TEMPLATE_P is non-NULL, it is set to true iff the
5751 `template' keyword is used to explicitly indicate that the entity
5752 named is a template.
5754 If DECLARATOR_P is true, the id-expression is appearing as part of
5755 a declarator, rather than as part of an expression. */
5758 cp_parser_id_expression (cp_parser
*parser
,
5759 bool template_keyword_p
,
5760 bool check_dependency_p
,
5765 bool global_scope_p
;
5766 bool nested_name_specifier_p
;
5768 /* Assume the `template' keyword was not used. */
5770 *template_p
= template_keyword_p
;
5772 /* Look for the optional `::' operator. */
5774 = (!template_keyword_p
5775 && (cp_parser_global_scope_opt (parser
,
5776 /*current_scope_valid_p=*/false)
5779 /* Look for the optional nested-name-specifier. */
5780 nested_name_specifier_p
5781 = (cp_parser_nested_name_specifier_opt (parser
,
5782 /*typename_keyword_p=*/false,
5789 /* If there is a nested-name-specifier, then we are looking at
5790 the first qualified-id production. */
5791 if (nested_name_specifier_p
)
5794 tree saved_object_scope
;
5795 tree saved_qualifying_scope
;
5796 cp_expr unqualified_id
;
5799 /* See if the next token is the `template' keyword. */
5801 template_p
= &is_template
;
5802 *template_p
= cp_parser_optional_template_keyword (parser
);
5803 /* Name lookup we do during the processing of the
5804 unqualified-id might obliterate SCOPE. */
5805 saved_scope
= parser
->scope
;
5806 saved_object_scope
= parser
->object_scope
;
5807 saved_qualifying_scope
= parser
->qualifying_scope
;
5808 /* Process the final unqualified-id. */
5809 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5812 /*optional_p=*/false);
5813 /* Restore the SAVED_SCOPE for our caller. */
5814 parser
->scope
= saved_scope
;
5815 parser
->object_scope
= saved_object_scope
;
5816 parser
->qualifying_scope
= saved_qualifying_scope
;
5818 return unqualified_id
;
5820 /* Otherwise, if we are in global scope, then we are looking at one
5821 of the other qualified-id productions. */
5822 else if (global_scope_p
)
5827 /* Peek at the next token. */
5828 token
= cp_lexer_peek_token (parser
->lexer
);
5830 /* If it's an identifier, and the next token is not a "<", then
5831 we can avoid the template-id case. This is an optimization
5832 for this common case. */
5833 if (token
->type
== CPP_NAME
5834 && !cp_parser_nth_token_starts_template_argument_list_p
5836 return cp_parser_identifier (parser
);
5838 cp_parser_parse_tentatively (parser
);
5839 /* Try a template-id. */
5840 id
= cp_parser_template_id (parser
,
5841 /*template_keyword_p=*/false,
5842 /*check_dependency_p=*/true,
5845 /* If that worked, we're done. */
5846 if (cp_parser_parse_definitely (parser
))
5849 /* Peek at the next token. (Changes in the token buffer may
5850 have invalidated the pointer obtained above.) */
5851 token
= cp_lexer_peek_token (parser
->lexer
);
5853 switch (token
->type
)
5856 return cp_parser_identifier (parser
);
5859 if (token
->keyword
== RID_OPERATOR
)
5860 return cp_parser_operator_function_id (parser
);
5864 cp_parser_error (parser
, "expected id-expression");
5865 return error_mark_node
;
5869 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5870 /*check_dependency_p=*/true,
5875 /* Parse an unqualified-id.
5879 operator-function-id
5880 conversion-function-id
5884 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5885 keyword, in a construct like `A::template ...'.
5887 Returns a representation of unqualified-id. For the `identifier'
5888 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5889 production a BIT_NOT_EXPR is returned; the operand of the
5890 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5891 other productions, see the documentation accompanying the
5892 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5893 names are looked up in uninstantiated templates. If DECLARATOR_P
5894 is true, the unqualified-id is appearing as part of a declarator,
5895 rather than as part of an expression. */
5898 cp_parser_unqualified_id (cp_parser
* parser
,
5899 bool template_keyword_p
,
5900 bool check_dependency_p
,
5906 /* Peek at the next token. */
5907 token
= cp_lexer_peek_token (parser
->lexer
);
5909 switch ((int) token
->type
)
5915 /* We don't know yet whether or not this will be a
5917 cp_parser_parse_tentatively (parser
);
5918 /* Try a template-id. */
5919 id
= cp_parser_template_id (parser
, template_keyword_p
,
5923 /* If it worked, we're done. */
5924 if (cp_parser_parse_definitely (parser
))
5926 /* Otherwise, it's an ordinary identifier. */
5927 return cp_parser_identifier (parser
);
5930 case CPP_TEMPLATE_ID
:
5931 return cp_parser_template_id (parser
, template_keyword_p
,
5939 tree qualifying_scope
;
5944 /* Consume the `~' token. */
5945 cp_lexer_consume_token (parser
->lexer
);
5946 /* Parse the class-name. The standard, as written, seems to
5949 template <typename T> struct S { ~S (); };
5950 template <typename T> S<T>::~S() {}
5952 is invalid, since `~' must be followed by a class-name, but
5953 `S<T>' is dependent, and so not known to be a class.
5954 That's not right; we need to look in uninstantiated
5955 templates. A further complication arises from:
5957 template <typename T> void f(T t) {
5961 Here, it is not possible to look up `T' in the scope of `T'
5962 itself. We must look in both the current scope, and the
5963 scope of the containing complete expression.
5965 Yet another issue is:
5974 The standard does not seem to say that the `S' in `~S'
5975 should refer to the type `S' and not the data member
5978 /* DR 244 says that we look up the name after the "~" in the
5979 same scope as we looked up the qualifying name. That idea
5980 isn't fully worked out; it's more complicated than that. */
5981 scope
= parser
->scope
;
5982 object_scope
= parser
->object_scope
;
5983 qualifying_scope
= parser
->qualifying_scope
;
5985 /* Check for invalid scopes. */
5986 if (scope
== error_mark_node
)
5988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5989 cp_lexer_consume_token (parser
->lexer
);
5990 return error_mark_node
;
5992 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5994 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5995 error_at (token
->location
,
5996 "scope %qT before %<~%> is not a class-name",
5998 cp_parser_simulate_error (parser
);
5999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6000 cp_lexer_consume_token (parser
->lexer
);
6001 return error_mark_node
;
6003 gcc_assert (!scope
|| TYPE_P (scope
));
6005 /* If the name is of the form "X::~X" it's OK even if X is a
6007 token
= cp_lexer_peek_token (parser
->lexer
);
6009 && token
->type
== CPP_NAME
6010 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6012 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6013 || (CLASS_TYPE_P (scope
)
6014 && constructor_name_p (token
->u
.value
, scope
))))
6016 cp_lexer_consume_token (parser
->lexer
);
6017 return build_nt (BIT_NOT_EXPR
, scope
);
6020 /* ~auto means the destructor of whatever the object is. */
6021 if (cp_parser_is_keyword (token
, RID_AUTO
))
6023 if (cxx_dialect
< cxx14
)
6024 pedwarn (input_location
, 0,
6025 "%<~auto%> only available with "
6026 "-std=c++14 or -std=gnu++14");
6027 cp_lexer_consume_token (parser
->lexer
);
6028 return build_nt (BIT_NOT_EXPR
, make_auto ());
6031 /* If there was an explicit qualification (S::~T), first look
6032 in the scope given by the qualification (i.e., S).
6034 Note: in the calls to cp_parser_class_name below we pass
6035 typename_type so that lookup finds the injected-class-name
6036 rather than the constructor. */
6038 type_decl
= NULL_TREE
;
6041 cp_parser_parse_tentatively (parser
);
6042 type_decl
= cp_parser_class_name (parser
,
6043 /*typename_keyword_p=*/false,
6044 /*template_keyword_p=*/false,
6046 /*check_dependency=*/false,
6047 /*class_head_p=*/false,
6049 if (cp_parser_parse_definitely (parser
))
6052 /* In "N::S::~S", look in "N" as well. */
6053 if (!done
&& scope
&& qualifying_scope
)
6055 cp_parser_parse_tentatively (parser
);
6056 parser
->scope
= qualifying_scope
;
6057 parser
->object_scope
= NULL_TREE
;
6058 parser
->qualifying_scope
= NULL_TREE
;
6060 = cp_parser_class_name (parser
,
6061 /*typename_keyword_p=*/false,
6062 /*template_keyword_p=*/false,
6064 /*check_dependency=*/false,
6065 /*class_head_p=*/false,
6067 if (cp_parser_parse_definitely (parser
))
6070 /* In "p->S::~T", look in the scope given by "*p" as well. */
6071 else if (!done
&& object_scope
)
6073 cp_parser_parse_tentatively (parser
);
6074 parser
->scope
= object_scope
;
6075 parser
->object_scope
= NULL_TREE
;
6076 parser
->qualifying_scope
= NULL_TREE
;
6078 = cp_parser_class_name (parser
,
6079 /*typename_keyword_p=*/false,
6080 /*template_keyword_p=*/false,
6082 /*check_dependency=*/false,
6083 /*class_head_p=*/false,
6085 if (cp_parser_parse_definitely (parser
))
6088 /* Look in the surrounding context. */
6091 parser
->scope
= NULL_TREE
;
6092 parser
->object_scope
= NULL_TREE
;
6093 parser
->qualifying_scope
= NULL_TREE
;
6094 if (processing_template_decl
)
6095 cp_parser_parse_tentatively (parser
);
6097 = cp_parser_class_name (parser
,
6098 /*typename_keyword_p=*/false,
6099 /*template_keyword_p=*/false,
6101 /*check_dependency=*/false,
6102 /*class_head_p=*/false,
6104 if (processing_template_decl
6105 && ! cp_parser_parse_definitely (parser
))
6107 /* We couldn't find a type with this name. If we're parsing
6108 tentatively, fail and try something else. */
6109 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6111 cp_parser_simulate_error (parser
);
6112 return error_mark_node
;
6114 /* Otherwise, accept it and check for a match at instantiation
6116 type_decl
= cp_parser_identifier (parser
);
6117 if (type_decl
!= error_mark_node
)
6118 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6122 /* If an error occurred, assume that the name of the
6123 destructor is the same as the name of the qualifying
6124 class. That allows us to keep parsing after running
6125 into ill-formed destructor names. */
6126 if (type_decl
== error_mark_node
&& scope
)
6127 return build_nt (BIT_NOT_EXPR
, scope
);
6128 else if (type_decl
== error_mark_node
)
6129 return error_mark_node
;
6131 /* Check that destructor name and scope match. */
6132 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6134 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6135 error_at (token
->location
,
6136 "declaration of %<~%T%> as member of %qT",
6138 cp_parser_simulate_error (parser
);
6139 return error_mark_node
;
6144 A typedef-name that names a class shall not be used as the
6145 identifier in the declarator for a destructor declaration. */
6147 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6148 && !DECL_SELF_REFERENCE_P (type_decl
)
6149 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6150 error_at (token
->location
,
6151 "typedef-name %qD used as destructor declarator",
6154 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6158 if (token
->keyword
== RID_OPERATOR
)
6162 /* This could be a template-id, so we try that first. */
6163 cp_parser_parse_tentatively (parser
);
6164 /* Try a template-id. */
6165 id
= cp_parser_template_id (parser
, template_keyword_p
,
6166 /*check_dependency_p=*/true,
6169 /* If that worked, we're done. */
6170 if (cp_parser_parse_definitely (parser
))
6172 /* We still don't know whether we're looking at an
6173 operator-function-id or a conversion-function-id. */
6174 cp_parser_parse_tentatively (parser
);
6175 /* Try an operator-function-id. */
6176 id
= cp_parser_operator_function_id (parser
);
6177 /* If that didn't work, try a conversion-function-id. */
6178 if (!cp_parser_parse_definitely (parser
))
6179 id
= cp_parser_conversion_function_id (parser
);
6188 cp_parser_error (parser
, "expected unqualified-id");
6189 return error_mark_node
;
6193 /* Parse an (optional) nested-name-specifier.
6195 nested-name-specifier: [C++98]
6196 class-or-namespace-name :: nested-name-specifier [opt]
6197 class-or-namespace-name :: template nested-name-specifier [opt]
6199 nested-name-specifier: [C++0x]
6202 nested-name-specifier identifier ::
6203 nested-name-specifier template [opt] simple-template-id ::
6205 PARSER->SCOPE should be set appropriately before this function is
6206 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6207 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6210 Sets PARSER->SCOPE to the class (TYPE) or namespace
6211 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6212 it unchanged if there is no nested-name-specifier. Returns the new
6213 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6215 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6216 part of a declaration and/or decl-specifier. */
6219 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6220 bool typename_keyword_p
,
6221 bool check_dependency_p
,
6223 bool is_declaration
,
6224 bool template_keyword_p
/* = false */)
6226 bool success
= false;
6227 cp_token_position start
= 0;
6230 /* Remember where the nested-name-specifier starts. */
6231 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6233 start
= cp_lexer_token_position (parser
->lexer
, false);
6234 push_deferring_access_checks (dk_deferred
);
6241 tree saved_qualifying_scope
;
6243 /* Spot cases that cannot be the beginning of a
6244 nested-name-specifier. */
6245 token
= cp_lexer_peek_token (parser
->lexer
);
6247 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6248 the already parsed nested-name-specifier. */
6249 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6251 /* Grab the nested-name-specifier and continue the loop. */
6252 cp_parser_pre_parsed_nested_name_specifier (parser
);
6253 /* If we originally encountered this nested-name-specifier
6254 with IS_DECLARATION set to false, we will not have
6255 resolved TYPENAME_TYPEs, so we must do so here. */
6257 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6259 new_scope
= resolve_typename_type (parser
->scope
,
6260 /*only_current_p=*/false);
6261 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6262 parser
->scope
= new_scope
;
6268 /* Spot cases that cannot be the beginning of a
6269 nested-name-specifier. On the second and subsequent times
6270 through the loop, we look for the `template' keyword. */
6271 if (success
&& token
->keyword
== RID_TEMPLATE
)
6273 /* A template-id can start a nested-name-specifier. */
6274 else if (token
->type
== CPP_TEMPLATE_ID
)
6276 /* DR 743: decltype can be used in a nested-name-specifier. */
6277 else if (token_is_decltype (token
))
6281 /* If the next token is not an identifier, then it is
6282 definitely not a type-name or namespace-name. */
6283 if (token
->type
!= CPP_NAME
)
6285 /* If the following token is neither a `<' (to begin a
6286 template-id), nor a `::', then we are not looking at a
6287 nested-name-specifier. */
6288 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6290 if (token
->type
== CPP_COLON
6291 && parser
->colon_corrects_to_scope_p
6292 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6294 gcc_rich_location
richloc (token
->location
);
6295 richloc
.add_fixit_replace ("::");
6297 "found %<:%> in nested-name-specifier, "
6299 token
->type
= CPP_SCOPE
;
6302 if (token
->type
!= CPP_SCOPE
6303 && !cp_parser_nth_token_starts_template_argument_list_p
6308 /* The nested-name-specifier is optional, so we parse
6310 cp_parser_parse_tentatively (parser
);
6312 /* Look for the optional `template' keyword, if this isn't the
6313 first time through the loop. */
6315 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6317 /* Save the old scope since the name lookup we are about to do
6318 might destroy it. */
6319 old_scope
= parser
->scope
;
6320 saved_qualifying_scope
= parser
->qualifying_scope
;
6321 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6322 look up names in "X<T>::I" in order to determine that "Y" is
6323 a template. So, if we have a typename at this point, we make
6324 an effort to look through it. */
6326 && !typename_keyword_p
6328 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6329 parser
->scope
= resolve_typename_type (parser
->scope
,
6330 /*only_current_p=*/false);
6331 /* Parse the qualifying entity. */
6333 = cp_parser_qualifying_entity (parser
,
6339 /* Look for the `::' token. */
6340 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6342 /* If we found what we wanted, we keep going; otherwise, we're
6344 if (!cp_parser_parse_definitely (parser
))
6346 bool error_p
= false;
6348 /* Restore the OLD_SCOPE since it was valid before the
6349 failed attempt at finding the last
6350 class-or-namespace-name. */
6351 parser
->scope
= old_scope
;
6352 parser
->qualifying_scope
= saved_qualifying_scope
;
6354 /* If the next token is a decltype, and the one after that is a
6355 `::', then the decltype has failed to resolve to a class or
6356 enumeration type. Give this error even when parsing
6357 tentatively since it can't possibly be valid--and we're going
6358 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6359 won't get another chance.*/
6360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6361 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6364 token
= cp_lexer_consume_token (parser
->lexer
);
6365 error_at (token
->location
, "decltype evaluates to %qT, "
6366 "which is not a class or enumeration type",
6367 token
->u
.tree_check_value
->value
);
6368 parser
->scope
= error_mark_node
;
6372 cp_lexer_consume_token (parser
->lexer
);
6375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6376 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6378 /* If we have a non-type template-id followed by ::, it can't
6379 possibly be valid. */
6380 token
= cp_lexer_peek_token (parser
->lexer
);
6381 tree tid
= token
->u
.tree_check_value
->value
;
6382 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6383 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6385 tree tmpl
= NULL_TREE
;
6386 if (is_overloaded_fn (tid
))
6388 tree fns
= get_fns (tid
);
6389 if (OVL_SINGLE_P (fns
))
6390 tmpl
= OVL_FIRST (fns
);
6391 error_at (token
->location
, "function template-id %qD "
6392 "in nested-name-specifier", tid
);
6396 /* Variable template. */
6397 tmpl
= TREE_OPERAND (tid
, 0);
6398 gcc_assert (variable_template_p (tmpl
));
6399 error_at (token
->location
, "variable template-id %qD "
6400 "in nested-name-specifier", tid
);
6403 inform (DECL_SOURCE_LOCATION (tmpl
),
6404 "%qD declared here", tmpl
);
6406 parser
->scope
= error_mark_node
;
6410 cp_lexer_consume_token (parser
->lexer
);
6411 cp_lexer_consume_token (parser
->lexer
);
6415 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6417 /* If the next token is an identifier, and the one after
6418 that is a `::', then any valid interpretation would have
6419 found a class-or-namespace-name. */
6420 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6421 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6423 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6426 token
= cp_lexer_consume_token (parser
->lexer
);
6429 if (!token
->error_reported
)
6432 tree ambiguous_decls
;
6434 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6436 /*is_template=*/false,
6437 /*is_namespace=*/false,
6438 /*check_dependency=*/true,
6441 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6442 error_at (token
->location
,
6443 "%qD used without template arguments",
6445 else if (ambiguous_decls
)
6447 // cp_parser_lookup_name has the same diagnostic,
6448 // thus make sure to emit it at most once.
6449 if (cp_parser_uncommitted_to_tentative_parse_p
6452 error_at (token
->location
,
6453 "reference to %qD is ambiguous",
6455 print_candidates (ambiguous_decls
);
6457 decl
= error_mark_node
;
6461 if (cxx_dialect
!= cxx98
)
6462 cp_parser_name_lookup_error
6463 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6466 cp_parser_name_lookup_error
6467 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6471 parser
->scope
= error_mark_node
;
6473 /* Treat this as a successful nested-name-specifier
6478 If the name found is not a class-name (clause
6479 _class_) or namespace-name (_namespace.def_), the
6480 program is ill-formed. */
6483 cp_lexer_consume_token (parser
->lexer
);
6487 /* We've found one valid nested-name-specifier. */
6489 /* Name lookup always gives us a DECL. */
6490 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6491 new_scope
= TREE_TYPE (new_scope
);
6492 /* Uses of "template" must be followed by actual templates. */
6493 if (template_keyword_p
6494 && !(CLASS_TYPE_P (new_scope
)
6495 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6496 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6497 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6498 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6499 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6500 == TEMPLATE_ID_EXPR
)))
6501 permerror (input_location
, TYPE_P (new_scope
)
6502 ? G_("%qT is not a template")
6503 : G_("%qD is not a template"),
6505 /* If it is a class scope, try to complete it; we are about to
6506 be looking up names inside the class. */
6507 if (TYPE_P (new_scope
)
6508 /* Since checking types for dependency can be expensive,
6509 avoid doing it if the type is already complete. */
6510 && !COMPLETE_TYPE_P (new_scope
)
6511 /* Do not try to complete dependent types. */
6512 && !dependent_type_p (new_scope
))
6514 new_scope
= complete_type (new_scope
);
6515 /* If it is a typedef to current class, use the current
6516 class instead, as the typedef won't have any names inside
6518 if (!COMPLETE_TYPE_P (new_scope
)
6519 && currently_open_class (new_scope
))
6520 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6522 /* Make sure we look in the right scope the next time through
6524 parser
->scope
= new_scope
;
6527 /* If parsing tentatively, replace the sequence of tokens that makes
6528 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6529 token. That way, should we re-parse the token stream, we will
6530 not have to repeat the effort required to do the parse, nor will
6531 we issue duplicate error messages. */
6532 if (success
&& start
)
6536 token
= cp_lexer_token_at (parser
->lexer
, start
);
6537 /* Reset the contents of the START token. */
6538 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6539 /* Retrieve any deferred checks. Do not pop this access checks yet
6540 so the memory will not be reclaimed during token replacing below. */
6541 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6542 token
->u
.tree_check_value
->value
= parser
->scope
;
6543 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6544 token
->u
.tree_check_value
->qualifying_scope
=
6545 parser
->qualifying_scope
;
6546 token
->keyword
= RID_MAX
;
6548 /* Purge all subsequent tokens. */
6549 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6553 pop_to_parent_deferring_access_checks ();
6555 return success
? parser
->scope
: NULL_TREE
;
6558 /* Parse a nested-name-specifier. See
6559 cp_parser_nested_name_specifier_opt for details. This function
6560 behaves identically, except that it will an issue an error if no
6561 nested-name-specifier is present. */
6564 cp_parser_nested_name_specifier (cp_parser
*parser
,
6565 bool typename_keyword_p
,
6566 bool check_dependency_p
,
6568 bool is_declaration
)
6572 /* Look for the nested-name-specifier. */
6573 scope
= cp_parser_nested_name_specifier_opt (parser
,
6578 /* If it was not present, issue an error message. */
6581 cp_parser_error (parser
, "expected nested-name-specifier");
6582 parser
->scope
= NULL_TREE
;
6588 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6589 this is either a class-name or a namespace-name (which corresponds
6590 to the class-or-namespace-name production in the grammar). For
6591 C++0x, it can also be a type-name that refers to an enumeration
6592 type or a simple-template-id.
6594 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6595 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6596 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6597 TYPE_P is TRUE iff the next name should be taken as a class-name,
6598 even the same name is declared to be another entity in the same
6601 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6602 specified by the class-or-namespace-name. If neither is found the
6603 ERROR_MARK_NODE is returned. */
6606 cp_parser_qualifying_entity (cp_parser
*parser
,
6607 bool typename_keyword_p
,
6608 bool template_keyword_p
,
6609 bool check_dependency_p
,
6611 bool is_declaration
)
6614 tree saved_qualifying_scope
;
6615 tree saved_object_scope
;
6618 bool successful_parse_p
;
6620 /* DR 743: decltype can appear in a nested-name-specifier. */
6621 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6623 scope
= cp_parser_decltype (parser
);
6624 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6625 && !MAYBE_CLASS_TYPE_P (scope
))
6627 cp_parser_simulate_error (parser
);
6628 return error_mark_node
;
6630 if (TYPE_NAME (scope
))
6631 scope
= TYPE_NAME (scope
);
6635 /* Before we try to parse the class-name, we must save away the
6636 current PARSER->SCOPE since cp_parser_class_name will destroy
6638 saved_scope
= parser
->scope
;
6639 saved_qualifying_scope
= parser
->qualifying_scope
;
6640 saved_object_scope
= parser
->object_scope
;
6641 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6642 there is no need to look for a namespace-name. */
6643 only_class_p
= template_keyword_p
6644 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6646 cp_parser_parse_tentatively (parser
);
6647 scope
= cp_parser_class_name (parser
,
6650 type_p
? class_type
: none_type
,
6652 /*class_head_p=*/false,
6654 /*enum_ok=*/cxx_dialect
> cxx98
);
6655 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6656 /* If that didn't work, try for a namespace-name. */
6657 if (!only_class_p
&& !successful_parse_p
)
6659 /* Restore the saved scope. */
6660 parser
->scope
= saved_scope
;
6661 parser
->qualifying_scope
= saved_qualifying_scope
;
6662 parser
->object_scope
= saved_object_scope
;
6663 /* If we are not looking at an identifier followed by the scope
6664 resolution operator, then this is not part of a
6665 nested-name-specifier. (Note that this function is only used
6666 to parse the components of a nested-name-specifier.) */
6667 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6668 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6669 return error_mark_node
;
6670 scope
= cp_parser_namespace_name (parser
);
6676 /* Return true if we are looking at a compound-literal, false otherwise. */
6679 cp_parser_compound_literal_p (cp_parser
*parser
)
6681 cp_lexer_save_tokens (parser
->lexer
);
6683 /* Skip tokens until the next token is a closing parenthesis.
6684 If we find the closing `)', and the next token is a `{', then
6685 we are looking at a compound-literal. */
6686 bool compound_literal_p
6687 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6688 /*consume_paren=*/true)
6689 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6691 /* Roll back the tokens we skipped. */
6692 cp_lexer_rollback_tokens (parser
->lexer
);
6694 return compound_literal_p
;
6697 /* Return true if EXPR is the integer constant zero or a complex constant
6698 of zero, without any folding, but ignoring location wrappers. */
6701 literal_integer_zerop (const_tree expr
)
6703 return (location_wrapper_p (expr
)
6704 && integer_zerop (TREE_OPERAND (expr
, 0)));
6707 /* Parse a postfix-expression.
6711 postfix-expression [ expression ]
6712 postfix-expression ( expression-list [opt] )
6713 simple-type-specifier ( expression-list [opt] )
6714 typename :: [opt] nested-name-specifier identifier
6715 ( expression-list [opt] )
6716 typename :: [opt] nested-name-specifier template [opt] template-id
6717 ( expression-list [opt] )
6718 postfix-expression . template [opt] id-expression
6719 postfix-expression -> template [opt] id-expression
6720 postfix-expression . pseudo-destructor-name
6721 postfix-expression -> pseudo-destructor-name
6722 postfix-expression ++
6723 postfix-expression --
6724 dynamic_cast < type-id > ( expression )
6725 static_cast < type-id > ( expression )
6726 reinterpret_cast < type-id > ( expression )
6727 const_cast < type-id > ( expression )
6728 typeid ( expression )
6734 ( type-id ) { initializer-list , [opt] }
6736 This extension is a GNU version of the C99 compound-literal
6737 construct. (The C99 grammar uses `type-name' instead of `type-id',
6738 but they are essentially the same concept.)
6740 If ADDRESS_P is true, the postfix expression is the operand of the
6741 `&' operator. CAST_P is true if this expression is the target of a
6744 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6745 class member access expressions [expr.ref].
6747 Returns a representation of the expression. */
6750 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6751 bool member_access_only_p
, bool decltype_p
,
6752 cp_id_kind
* pidk_return
)
6757 cp_id_kind idk
= CP_ID_KIND_NONE
;
6758 cp_expr postfix_expression
= NULL_TREE
;
6759 bool is_member_access
= false;
6761 /* Peek at the next token. */
6762 token
= cp_lexer_peek_token (parser
->lexer
);
6763 loc
= token
->location
;
6764 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6766 /* Some of the productions are determined by keywords. */
6767 keyword
= token
->keyword
;
6777 const char *saved_message
;
6778 bool saved_in_type_id_in_expr_p
;
6780 /* All of these can be handled in the same way from the point
6781 of view of parsing. Begin by consuming the token
6782 identifying the cast. */
6783 cp_lexer_consume_token (parser
->lexer
);
6785 /* New types cannot be defined in the cast. */
6786 saved_message
= parser
->type_definition_forbidden_message
;
6787 parser
->type_definition_forbidden_message
6788 = G_("types may not be defined in casts");
6790 /* Look for the opening `<'. */
6791 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6792 /* Parse the type to which we are casting. */
6793 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6794 parser
->in_type_id_in_expr_p
= true;
6795 type
= cp_parser_type_id (parser
);
6796 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6797 /* Look for the closing `>'. */
6798 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6799 /* Restore the old message. */
6800 parser
->type_definition_forbidden_message
= saved_message
;
6802 bool saved_greater_than_is_operator_p
6803 = parser
->greater_than_is_operator_p
;
6804 parser
->greater_than_is_operator_p
= true;
6806 /* And the expression which is being cast. */
6807 matching_parens parens
;
6808 parens
.require_open (parser
);
6809 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6810 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6812 location_t end_loc
= close_paren
?
6813 close_paren
->location
: UNKNOWN_LOCATION
;
6815 parser
->greater_than_is_operator_p
6816 = saved_greater_than_is_operator_p
;
6818 /* Only type conversions to integral or enumeration types
6819 can be used in constant-expressions. */
6820 if (!cast_valid_in_integral_constant_expression_p (type
)
6821 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6823 postfix_expression
= error_mark_node
;
6831 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6835 = build_static_cast (type
, expression
, tf_warning_or_error
);
6839 = build_reinterpret_cast (type
, expression
,
6840 tf_warning_or_error
);
6844 = build_const_cast (type
, expression
, tf_warning_or_error
);
6850 /* Construct a location e.g. :
6851 reinterpret_cast <int *> (expr)
6852 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6853 ranging from the start of the "*_cast" token to the final closing
6854 paren, with the caret at the start. */
6855 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6856 postfix_expression
.set_location (cp_cast_loc
);
6863 const char *saved_message
;
6864 bool saved_in_type_id_in_expr_p
;
6866 /* Consume the `typeid' token. */
6867 cp_lexer_consume_token (parser
->lexer
);
6868 /* Look for the `(' token. */
6869 matching_parens parens
;
6870 parens
.require_open (parser
);
6871 /* Types cannot be defined in a `typeid' expression. */
6872 saved_message
= parser
->type_definition_forbidden_message
;
6873 parser
->type_definition_forbidden_message
6874 = G_("types may not be defined in a %<typeid%> expression");
6875 /* We can't be sure yet whether we're looking at a type-id or an
6877 cp_parser_parse_tentatively (parser
);
6878 /* Try a type-id first. */
6879 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6880 parser
->in_type_id_in_expr_p
= true;
6881 type
= cp_parser_type_id (parser
);
6882 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6883 /* Look for the `)' token. Otherwise, we can't be sure that
6884 we're not looking at an expression: consider `typeid (int
6885 (3))', for example. */
6886 cp_token
*close_paren
= parens
.require_close (parser
);
6887 /* If all went well, simply lookup the type-id. */
6888 if (cp_parser_parse_definitely (parser
))
6889 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6890 /* Otherwise, fall back to the expression variant. */
6895 /* Look for an expression. */
6896 expression
= cp_parser_expression (parser
, & idk
);
6897 /* Compute its typeid. */
6898 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6899 /* Look for the `)' token. */
6900 close_paren
= parens
.require_close (parser
);
6902 /* Restore the saved message. */
6903 parser
->type_definition_forbidden_message
= saved_message
;
6904 /* `typeid' may not appear in an integral constant expression. */
6905 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6906 postfix_expression
= error_mark_node
;
6908 /* Construct a location e.g. :
6911 ranging from the start of the "typeid" token to the final closing
6912 paren, with the caret at the start. */
6915 location_t typeid_loc
6916 = make_location (start_loc
, start_loc
, close_paren
->location
);
6917 postfix_expression
.set_location (typeid_loc
);
6918 postfix_expression
.maybe_add_location_wrapper ();
6926 /* The syntax permitted here is the same permitted for an
6927 elaborated-type-specifier. */
6928 ++parser
->prevent_constrained_type_specifiers
;
6929 type
= cp_parser_elaborated_type_specifier (parser
,
6930 /*is_friend=*/false,
6931 /*is_declaration=*/false);
6932 --parser
->prevent_constrained_type_specifiers
;
6933 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6938 case RID_BUILTIN_SHUFFLE
:
6939 case RID_BUILTIN_LAUNDER
:
6941 vec
<tree
, va_gc
> *vec
;
6945 cp_lexer_consume_token (parser
->lexer
);
6946 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6947 /*cast_p=*/false, /*allow_expansion_p=*/true,
6948 /*non_constant_p=*/NULL
);
6951 postfix_expression
= error_mark_node
;
6955 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6961 if (vec
->length () == 1)
6963 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6966 error_at (loc
, "wrong number of arguments to "
6967 "%<__builtin_addressof%>");
6968 postfix_expression
= error_mark_node
;
6972 case RID_BUILTIN_LAUNDER
:
6973 if (vec
->length () == 1)
6974 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6975 tf_warning_or_error
);
6978 error_at (loc
, "wrong number of arguments to "
6979 "%<__builtin_launder%>");
6980 postfix_expression
= error_mark_node
;
6984 case RID_BUILTIN_SHUFFLE
:
6985 if (vec
->length () == 2)
6987 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6988 (*vec
)[1], tf_warning_or_error
);
6989 else if (vec
->length () == 3)
6991 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6992 (*vec
)[2], tf_warning_or_error
);
6995 error_at (loc
, "wrong number of arguments to "
6996 "%<__builtin_shuffle%>");
6997 postfix_expression
= error_mark_node
;
7011 /* If the next thing is a simple-type-specifier, we may be
7012 looking at a functional cast. We could also be looking at
7013 an id-expression. So, we try the functional cast, and if
7014 that doesn't work we fall back to the primary-expression. */
7015 cp_parser_parse_tentatively (parser
);
7016 /* Look for the simple-type-specifier. */
7017 ++parser
->prevent_constrained_type_specifiers
;
7018 type
= cp_parser_simple_type_specifier (parser
,
7019 /*decl_specs=*/NULL
,
7020 CP_PARSER_FLAGS_NONE
);
7021 --parser
->prevent_constrained_type_specifiers
;
7022 /* Parse the cast itself. */
7023 if (!cp_parser_error_occurred (parser
))
7025 = cp_parser_functional_cast (parser
, type
);
7026 /* If that worked, we're done. */
7027 if (cp_parser_parse_definitely (parser
))
7030 /* If the functional-cast didn't work out, try a
7031 compound-literal. */
7032 if (cp_parser_allow_gnu_extensions_p (parser
)
7033 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7035 cp_expr initializer
= NULL_TREE
;
7037 cp_parser_parse_tentatively (parser
);
7039 matching_parens parens
;
7040 parens
.consume_open (parser
);
7042 /* Avoid calling cp_parser_type_id pointlessly, see comment
7043 in cp_parser_cast_expression about c++/29234. */
7044 if (!cp_parser_compound_literal_p (parser
))
7045 cp_parser_simulate_error (parser
);
7048 /* Parse the type. */
7049 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7050 parser
->in_type_id_in_expr_p
= true;
7051 type
= cp_parser_type_id (parser
);
7052 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7053 parens
.require_close (parser
);
7056 /* If things aren't going well, there's no need to
7058 if (!cp_parser_error_occurred (parser
))
7060 bool non_constant_p
;
7061 /* Parse the brace-enclosed initializer list. */
7062 initializer
= cp_parser_braced_list (parser
,
7065 /* If that worked, we're definitely looking at a
7066 compound-literal expression. */
7067 if (cp_parser_parse_definitely (parser
))
7069 /* Warn the user that a compound literal is not
7070 allowed in standard C++. */
7071 pedwarn (input_location
, OPT_Wpedantic
,
7072 "ISO C++ forbids compound-literals");
7073 /* For simplicity, we disallow compound literals in
7074 constant-expressions. We could
7075 allow compound literals of integer type, whose
7076 initializer was a constant, in constant
7077 expressions. Permitting that usage, as a further
7078 extension, would not change the meaning of any
7079 currently accepted programs. (Of course, as
7080 compound literals are not part of ISO C++, the
7081 standard has nothing to say.) */
7082 if (cp_parser_non_integral_constant_expression (parser
,
7085 postfix_expression
= error_mark_node
;
7088 /* Form the representation of the compound-literal. */
7090 = finish_compound_literal (type
, initializer
,
7091 tf_warning_or_error
, fcl_c99
);
7092 postfix_expression
.set_location (initializer
.get_location ());
7097 /* It must be a primary-expression. */
7099 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7100 /*template_arg_p=*/false,
7107 /* Note that we don't need to worry about calling build_cplus_new on a
7108 class-valued CALL_EXPR in decltype when it isn't the end of the
7109 postfix-expression; unary_complex_lvalue will take care of that for
7112 /* Keep looping until the postfix-expression is complete. */
7115 if (idk
== CP_ID_KIND_UNQUALIFIED
7116 && identifier_p (postfix_expression
)
7117 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7118 /* It is not a Koenig lookup function call. */
7120 = unqualified_name_lookup_error (postfix_expression
);
7122 /* Peek at the next token. */
7123 token
= cp_lexer_peek_token (parser
->lexer
);
7125 switch (token
->type
)
7127 case CPP_OPEN_SQUARE
:
7128 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7130 cp_parser_error (parser
,
7131 "two consecutive %<[%> shall "
7132 "only introduce an attribute");
7133 return error_mark_node
;
7136 = cp_parser_postfix_open_square_expression (parser
,
7140 postfix_expression
.set_range (start_loc
,
7141 postfix_expression
.get_location ());
7143 idk
= CP_ID_KIND_NONE
;
7144 is_member_access
= false;
7147 case CPP_OPEN_PAREN
:
7148 /* postfix-expression ( expression-list [opt] ) */
7151 bool is_builtin_constant_p
;
7152 bool saved_integral_constant_expression_p
= false;
7153 bool saved_non_integral_constant_expression_p
= false;
7154 tsubst_flags_t complain
= complain_flags (decltype_p
);
7155 vec
<tree
, va_gc
> *args
;
7156 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7158 is_member_access
= false;
7160 is_builtin_constant_p
7161 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7162 if (is_builtin_constant_p
)
7164 /* The whole point of __builtin_constant_p is to allow
7165 non-constant expressions to appear as arguments. */
7166 saved_integral_constant_expression_p
7167 = parser
->integral_constant_expression_p
;
7168 saved_non_integral_constant_expression_p
7169 = parser
->non_integral_constant_expression_p
;
7170 parser
->integral_constant_expression_p
= false;
7172 args
= (cp_parser_parenthesized_expression_list
7174 /*cast_p=*/false, /*allow_expansion_p=*/true,
7175 /*non_constant_p=*/NULL
,
7176 /*close_paren_loc=*/&close_paren_loc
,
7177 /*wrap_locations_p=*/true));
7178 if (is_builtin_constant_p
)
7180 parser
->integral_constant_expression_p
7181 = saved_integral_constant_expression_p
;
7182 parser
->non_integral_constant_expression_p
7183 = saved_non_integral_constant_expression_p
;
7188 postfix_expression
= error_mark_node
;
7192 /* Function calls are not permitted in
7193 constant-expressions. */
7194 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7195 && cp_parser_non_integral_constant_expression (parser
,
7198 postfix_expression
= error_mark_node
;
7199 release_tree_vector (args
);
7204 if (idk
== CP_ID_KIND_UNQUALIFIED
7205 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7207 if (identifier_p (postfix_expression
)
7208 /* In C++2A, we may need to perform ADL for a template
7210 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7211 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7213 if (!args
->is_empty ())
7216 if (!any_type_dependent_arguments_p (args
))
7218 = perform_koenig_lookup (postfix_expression
, args
,
7223 = unqualified_fn_lookup_error (postfix_expression
);
7225 /* We do not perform argument-dependent lookup if
7226 normal lookup finds a non-function, in accordance
7227 with the expected resolution of DR 218. */
7228 else if (!args
->is_empty ()
7229 && is_overloaded_fn (postfix_expression
))
7231 tree fn
= get_first_fn (postfix_expression
);
7232 fn
= STRIP_TEMPLATE (fn
);
7234 /* Do not do argument dependent lookup if regular
7235 lookup finds a member function or a block-scope
7236 function declaration. [basic.lookup.argdep]/3 */
7237 if (!DECL_FUNCTION_MEMBER_P (fn
)
7238 && !DECL_LOCAL_FUNCTION_P (fn
))
7241 if (!any_type_dependent_arguments_p (args
))
7243 = perform_koenig_lookup (postfix_expression
, args
,
7249 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7251 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7252 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7254 if (processing_template_decl
7255 && (type_dependent_object_expression_p (instance
)
7256 || (!BASELINK_P (fn
)
7257 && TREE_CODE (fn
) != FIELD_DECL
)
7258 || type_dependent_expression_p (fn
)
7259 || any_type_dependent_arguments_p (args
)))
7261 maybe_generic_this_capture (instance
, fn
);
7263 = build_min_nt_call_vec (postfix_expression
, args
);
7264 release_tree_vector (args
);
7268 if (BASELINK_P (fn
))
7271 = (build_new_method_call
7272 (instance
, fn
, &args
, NULL_TREE
,
7273 (idk
== CP_ID_KIND_QUALIFIED
7274 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7281 = finish_call_expr (postfix_expression
, &args
,
7282 /*disallow_virtual=*/false,
7286 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7287 || TREE_CODE (postfix_expression
) == MEMBER_REF
7288 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7289 postfix_expression
= (build_offset_ref_call_from_tree
7290 (postfix_expression
, &args
,
7292 else if (idk
== CP_ID_KIND_QUALIFIED
)
7293 /* A call to a static class member, or a namespace-scope
7296 = finish_call_expr (postfix_expression
, &args
,
7297 /*disallow_virtual=*/true,
7301 /* All other function calls. */
7303 = finish_call_expr (postfix_expression
, &args
,
7304 /*disallow_virtual=*/false,
7308 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7310 location_t combined_loc
= make_location (token
->location
,
7313 postfix_expression
.set_location (combined_loc
);
7316 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7317 idk
= CP_ID_KIND_NONE
;
7319 release_tree_vector (args
);
7325 /* postfix-expression . template [opt] id-expression
7326 postfix-expression . pseudo-destructor-name
7327 postfix-expression -> template [opt] id-expression
7328 postfix-expression -> pseudo-destructor-name */
7330 /* Consume the `.' or `->' operator. */
7331 cp_lexer_consume_token (parser
->lexer
);
7334 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7338 is_member_access
= true;
7342 /* postfix-expression ++ */
7343 /* Consume the `++' token. */
7344 cp_lexer_consume_token (parser
->lexer
);
7345 /* Generate a representation for the complete expression. */
7347 = finish_increment_expr (postfix_expression
,
7348 POSTINCREMENT_EXPR
);
7349 /* Increments may not appear in constant-expressions. */
7350 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7351 postfix_expression
= error_mark_node
;
7352 idk
= CP_ID_KIND_NONE
;
7353 is_member_access
= false;
7356 case CPP_MINUS_MINUS
:
7357 /* postfix-expression -- */
7358 /* Consume the `--' token. */
7359 cp_lexer_consume_token (parser
->lexer
);
7360 /* Generate a representation for the complete expression. */
7362 = finish_increment_expr (postfix_expression
,
7363 POSTDECREMENT_EXPR
);
7364 /* Decrements may not appear in constant-expressions. */
7365 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7366 postfix_expression
= error_mark_node
;
7367 idk
= CP_ID_KIND_NONE
;
7368 is_member_access
= false;
7372 if (pidk_return
!= NULL
)
7373 * pidk_return
= idk
;
7374 if (member_access_only_p
)
7375 return is_member_access
7376 ? postfix_expression
7377 : cp_expr (error_mark_node
);
7379 return postfix_expression
;
7383 /* We should never get here. */
7385 return error_mark_node
;
7388 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7389 by cp_parser_builtin_offsetof. We're looking for
7391 postfix-expression [ expression ]
7392 postfix-expression [ braced-init-list ] (C++11)
7394 FOR_OFFSETOF is set if we're being called in that context, which
7395 changes how we deal with integer constant expressions. */
7398 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7399 tree postfix_expression
,
7403 tree index
= NULL_TREE
;
7404 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7405 bool saved_greater_than_is_operator_p
;
7407 /* Consume the `[' token. */
7408 cp_lexer_consume_token (parser
->lexer
);
7410 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7411 parser
->greater_than_is_operator_p
= true;
7413 /* Parse the index expression. */
7414 /* ??? For offsetof, there is a question of what to allow here. If
7415 offsetof is not being used in an integral constant expression context,
7416 then we *could* get the right answer by computing the value at runtime.
7417 If we are in an integral constant expression context, then we might
7418 could accept any constant expression; hard to say without analysis.
7419 Rather than open the barn door too wide right away, allow only integer
7420 constant expressions here. */
7422 index
= cp_parser_constant_expression (parser
);
7425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7427 bool expr_nonconst_p
;
7428 cp_lexer_set_source_position (parser
->lexer
);
7429 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7430 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7433 index
= cp_parser_expression (parser
);
7436 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7438 /* Look for the closing `]'. */
7439 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7441 /* Build the ARRAY_REF. */
7442 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7445 /* When not doing offsetof, array references are not permitted in
7446 constant-expressions. */
7448 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7449 postfix_expression
= error_mark_node
;
7451 return postfix_expression
;
7454 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7455 dereference of incomplete type, returns true if error_mark_node should
7456 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7457 and *DEPENDENT_P. */
7460 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7463 /* In a template, be permissive by treating an object expression
7464 of incomplete type as dependent (after a pedwarn). */
7465 diagnostic_t kind
= (processing_template_decl
7466 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7468 switch (TREE_CODE (*postfix_expression
))
7471 case REINTERPRET_CAST_EXPR
:
7472 case CONST_CAST_EXPR
:
7473 case STATIC_CAST_EXPR
:
7474 case DYNAMIC_CAST_EXPR
:
7475 case IMPLICIT_CONV_EXPR
:
7476 case VIEW_CONVERT_EXPR
:
7477 case NON_LVALUE_EXPR
:
7481 /* Don't emit any diagnostic for OVERLOADs. */
7485 /* Avoid clobbering e.g. DECLs. */
7486 if (!EXPR_P (*postfix_expression
))
7491 if (kind
== DK_IGNORED
)
7494 location_t exploc
= location_of (*postfix_expression
);
7495 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7496 if (!MAYBE_CLASS_TYPE_P (*scope
))
7498 if (kind
== DK_ERROR
)
7499 *scope
= *postfix_expression
= error_mark_node
;
7500 else if (processing_template_decl
)
7502 *dependent_p
= true;
7503 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7508 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7509 by cp_parser_builtin_offsetof. We're looking for
7511 postfix-expression . template [opt] id-expression
7512 postfix-expression . pseudo-destructor-name
7513 postfix-expression -> template [opt] id-expression
7514 postfix-expression -> pseudo-destructor-name
7516 FOR_OFFSETOF is set if we're being called in that context. That sorta
7517 limits what of the above we'll actually accept, but nevermind.
7518 TOKEN_TYPE is the "." or "->" token, which will already have been
7519 removed from the stream. */
7522 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7523 enum cpp_ttype token_type
,
7524 cp_expr postfix_expression
,
7525 bool for_offsetof
, cp_id_kind
*idk
,
7526 location_t location
)
7530 bool pseudo_destructor_p
;
7531 tree scope
= NULL_TREE
;
7532 location_t start_loc
= postfix_expression
.get_start ();
7534 /* If this is a `->' operator, dereference the pointer. */
7535 if (token_type
== CPP_DEREF
)
7536 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7537 tf_warning_or_error
);
7538 /* Check to see whether or not the expression is type-dependent and
7539 not the current instantiation. */
7540 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7541 /* The identifier following the `->' or `.' is not qualified. */
7542 parser
->scope
= NULL_TREE
;
7543 parser
->qualifying_scope
= NULL_TREE
;
7544 parser
->object_scope
= NULL_TREE
;
7545 *idk
= CP_ID_KIND_NONE
;
7547 /* Enter the scope corresponding to the type of the object
7548 given by the POSTFIX_EXPRESSION. */
7551 scope
= TREE_TYPE (postfix_expression
);
7552 /* According to the standard, no expression should ever have
7553 reference type. Unfortunately, we do not currently match
7554 the standard in this respect in that our internal representation
7555 of an expression may have reference type even when the standard
7556 says it does not. Therefore, we have to manually obtain the
7557 underlying type here. */
7558 scope
= non_reference (scope
);
7559 /* The type of the POSTFIX_EXPRESSION must be complete. */
7560 /* Unlike the object expression in other contexts, *this is not
7561 required to be of complete type for purposes of class member
7562 access (5.2.5) outside the member function body. */
7563 if (postfix_expression
!= current_class_ref
7564 && scope
!= error_mark_node
7565 && !currently_open_class (scope
))
7567 scope
= complete_type (scope
);
7568 if (!COMPLETE_TYPE_P (scope
)
7569 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7571 return error_mark_node
;
7576 /* Let the name lookup machinery know that we are processing a
7577 class member access expression. */
7578 parser
->context
->object_type
= scope
;
7579 /* If something went wrong, we want to be able to discern that case,
7580 as opposed to the case where there was no SCOPE due to the type
7581 of expression being dependent. */
7583 scope
= error_mark_node
;
7584 /* If the SCOPE was erroneous, make the various semantic analysis
7585 functions exit quickly -- and without issuing additional error
7587 if (scope
== error_mark_node
)
7588 postfix_expression
= error_mark_node
;
7593 /* Tell cp_parser_lookup_name that there was an object, even though it's
7595 parser
->context
->object_type
= unknown_type_node
;
7597 /* Assume this expression is not a pseudo-destructor access. */
7598 pseudo_destructor_p
= false;
7600 /* If the SCOPE is a scalar type, then, if this is a valid program,
7601 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7602 is type dependent, it can be pseudo-destructor-name or something else.
7603 Try to parse it as pseudo-destructor-name first. */
7604 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7609 cp_parser_parse_tentatively (parser
);
7610 /* Parse the pseudo-destructor-name. */
7612 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7615 && (cp_parser_error_occurred (parser
)
7616 || !SCALAR_TYPE_P (type
)))
7617 cp_parser_abort_tentative_parse (parser
);
7618 else if (cp_parser_parse_definitely (parser
))
7620 pseudo_destructor_p
= true;
7622 = finish_pseudo_destructor_expr (postfix_expression
,
7627 if (!pseudo_destructor_p
)
7629 /* If the SCOPE is not a scalar type, we are looking at an
7630 ordinary class member access expression, rather than a
7631 pseudo-destructor-name. */
7633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7634 /* Parse the id-expression. */
7635 name
= (cp_parser_id_expression
7637 cp_parser_optional_template_keyword (parser
),
7638 /*check_dependency_p=*/true,
7640 /*declarator_p=*/false,
7641 /*optional_p=*/false));
7642 /* In general, build a SCOPE_REF if the member name is qualified.
7643 However, if the name was not dependent and has already been
7644 resolved; there is no need to build the SCOPE_REF. For example;
7646 struct X { void f(); };
7647 template <typename T> void f(T* t) { t->X::f(); }
7649 Even though "t" is dependent, "X::f" is not and has been resolved
7650 to a BASELINK; there is no need to include scope information. */
7652 /* But we do need to remember that there was an explicit scope for
7653 virtual function calls. */
7655 *idk
= CP_ID_KIND_QUALIFIED
;
7657 /* If the name is a template-id that names a type, we will get a
7658 TYPE_DECL here. That is invalid code. */
7659 if (TREE_CODE (name
) == TYPE_DECL
)
7661 error_at (token
->location
, "invalid use of %qD", name
);
7662 postfix_expression
= error_mark_node
;
7666 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7668 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7670 error_at (token
->location
, "%<%D::%D%> is not a class member",
7671 parser
->scope
, name
);
7672 postfix_expression
= error_mark_node
;
7675 name
= build_qualified_name (/*type=*/NULL_TREE
,
7679 parser
->scope
= NULL_TREE
;
7680 parser
->qualifying_scope
= NULL_TREE
;
7681 parser
->object_scope
= NULL_TREE
;
7683 if (parser
->scope
&& name
&& BASELINK_P (name
))
7684 adjust_result_of_qualified_name_lookup
7685 (name
, parser
->scope
, scope
);
7687 = finish_class_member_access_expr (postfix_expression
, name
,
7689 tf_warning_or_error
);
7690 /* Build a location e.g.:
7693 where the caret is at the deref token, ranging from
7694 the start of postfix_expression to the end of the access expr. */
7696 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7697 location_t combined_loc
7698 = make_location (input_location
, start_loc
, end_loc
);
7699 protected_set_expr_location (postfix_expression
, combined_loc
);
7703 /* We no longer need to look up names in the scope of the object on
7704 the left-hand side of the `.' or `->' operator. */
7705 parser
->context
->object_type
= NULL_TREE
;
7707 /* Outside of offsetof, these operators may not appear in
7708 constant-expressions. */
7710 && (cp_parser_non_integral_constant_expression
7711 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7712 postfix_expression
= error_mark_node
;
7714 return postfix_expression
;
7717 /* Parse a parenthesized expression-list.
7720 assignment-expression
7721 expression-list, assignment-expression
7726 identifier, expression-list
7728 CAST_P is true if this expression is the target of a cast.
7730 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7733 WRAP_LOCATIONS_P is true if expressions within this list for which
7734 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7735 their source locations.
7737 Returns a vector of trees. Each element is a representation of an
7738 assignment-expression. NULL is returned if the ( and or ) are
7739 missing. An empty, but allocated, vector is returned on no
7740 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7741 if we are parsing an attribute list for an attribute that wants a
7742 plain identifier argument, normal_attr for an attribute that wants
7743 an expression, or non_attr if we aren't parsing an attribute list. If
7744 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7745 not all of the expressions in the list were constant.
7746 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7747 will be written to with the location of the closing parenthesis. If
7748 an error occurs, it may or may not be written to. */
7750 static vec
<tree
, va_gc
> *
7751 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7752 int is_attribute_list
,
7754 bool allow_expansion_p
,
7755 bool *non_constant_p
,
7756 location_t
*close_paren_loc
,
7757 bool wrap_locations_p
)
7759 vec
<tree
, va_gc
> *expression_list
;
7760 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7761 tree identifier
= NULL_TREE
;
7762 bool saved_greater_than_is_operator_p
;
7764 /* Assume all the expressions will be constant. */
7766 *non_constant_p
= false;
7768 matching_parens parens
;
7769 if (!parens
.require_open (parser
))
7772 expression_list
= make_tree_vector ();
7774 /* Within a parenthesized expression, a `>' token is always
7775 the greater-than operator. */
7776 saved_greater_than_is_operator_p
7777 = parser
->greater_than_is_operator_p
;
7778 parser
->greater_than_is_operator_p
= true;
7780 cp_expr
expr (NULL_TREE
);
7782 /* Consume expressions until there are no more. */
7783 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7786 /* At the beginning of attribute lists, check to see if the
7787 next token is an identifier. */
7788 if (is_attribute_list
== id_attr
7789 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7793 /* Consume the identifier. */
7794 token
= cp_lexer_consume_token (parser
->lexer
);
7795 /* Save the identifier. */
7796 identifier
= token
->u
.value
;
7800 bool expr_non_constant_p
;
7802 /* Parse the next assignment-expression. */
7803 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7805 /* A braced-init-list. */
7806 cp_lexer_set_source_position (parser
->lexer
);
7807 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7808 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7809 if (non_constant_p
&& expr_non_constant_p
)
7810 *non_constant_p
= true;
7812 else if (non_constant_p
)
7814 expr
= (cp_parser_constant_expression
7815 (parser
, /*allow_non_constant_p=*/true,
7816 &expr_non_constant_p
));
7817 if (expr_non_constant_p
)
7818 *non_constant_p
= true;
7821 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7825 expr
= instantiate_non_dependent_expr (expr
);
7827 /* If we have an ellipsis, then this is an expression
7829 if (allow_expansion_p
7830 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7832 /* Consume the `...'. */
7833 cp_lexer_consume_token (parser
->lexer
);
7835 /* Build the argument pack. */
7836 expr
= make_pack_expansion (expr
);
7839 if (wrap_locations_p
)
7840 expr
.maybe_add_location_wrapper ();
7842 /* Add it to the list. We add error_mark_node
7843 expressions to the list, so that we can still tell if
7844 the correct form for a parenthesized expression-list
7845 is found. That gives better errors. */
7846 vec_safe_push (expression_list
, expr
.get_value ());
7848 if (expr
== error_mark_node
)
7852 /* After the first item, attribute lists look the same as
7853 expression lists. */
7854 is_attribute_list
= non_attr
;
7857 /* If the next token isn't a `,', then we are done. */
7858 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7861 /* Otherwise, consume the `,' and keep going. */
7862 cp_lexer_consume_token (parser
->lexer
);
7865 if (close_paren_loc
)
7866 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7868 if (!parens
.require_close (parser
))
7873 /* We try and resync to an unnested comma, as that will give the
7874 user better diagnostics. */
7875 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7876 /*recovering=*/true,
7878 /*consume_paren=*/true);
7883 parser
->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p
;
7889 parser
->greater_than_is_operator_p
7890 = saved_greater_than_is_operator_p
;
7893 vec_safe_insert (expression_list
, 0, identifier
);
7895 return expression_list
;
7898 /* Parse a pseudo-destructor-name.
7900 pseudo-destructor-name:
7901 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7902 :: [opt] nested-name-specifier template template-id :: ~ type-name
7903 :: [opt] nested-name-specifier [opt] ~ type-name
7905 If either of the first two productions is used, sets *SCOPE to the
7906 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7907 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7908 or ERROR_MARK_NODE if the parse fails. */
7911 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7916 bool nested_name_specifier_p
;
7919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7920 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7921 && !type_dependent_expression_p (object
))
7923 if (cxx_dialect
< cxx14
)
7924 pedwarn (input_location
, 0,
7925 "%<~auto%> only available with "
7926 "-std=c++14 or -std=gnu++14");
7927 cp_lexer_consume_token (parser
->lexer
);
7928 cp_lexer_consume_token (parser
->lexer
);
7930 *type
= TREE_TYPE (object
);
7934 /* Assume that things will not work out. */
7935 *type
= error_mark_node
;
7937 /* Look for the optional `::' operator. */
7938 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7939 /* Look for the optional nested-name-specifier. */
7940 nested_name_specifier_p
7941 = (cp_parser_nested_name_specifier_opt (parser
,
7942 /*typename_keyword_p=*/false,
7943 /*check_dependency_p=*/true,
7945 /*is_declaration=*/false)
7947 /* Now, if we saw a nested-name-specifier, we might be doing the
7948 second production. */
7949 if (nested_name_specifier_p
7950 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7952 /* Consume the `template' keyword. */
7953 cp_lexer_consume_token (parser
->lexer
);
7954 /* Parse the template-id. */
7955 cp_parser_template_id (parser
,
7956 /*template_keyword_p=*/true,
7957 /*check_dependency_p=*/false,
7959 /*is_declaration=*/true);
7960 /* Look for the `::' token. */
7961 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7963 /* If the next token is not a `~', then there might be some
7964 additional qualification. */
7965 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7967 /* At this point, we're looking for "type-name :: ~". The type-name
7968 must not be a class-name, since this is a pseudo-destructor. So,
7969 it must be either an enum-name, or a typedef-name -- both of which
7970 are just identifiers. So, we peek ahead to check that the "::"
7971 and "~" tokens are present; if they are not, then we can avoid
7972 calling type_name. */
7973 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7974 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7975 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7977 cp_parser_error (parser
, "non-scalar type");
7981 /* Look for the type-name. */
7982 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7983 if (*scope
== error_mark_node
)
7986 /* Look for the `::' token. */
7987 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7992 /* Look for the `~'. */
7993 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7995 /* Once we see the ~, this has to be a pseudo-destructor. */
7996 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7997 cp_parser_commit_to_topmost_tentative_parse (parser
);
7999 /* Look for the type-name again. We are not responsible for
8000 checking that it matches the first type-name. */
8001 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8004 /* Parse a unary-expression.
8010 unary-operator cast-expression
8011 sizeof unary-expression
8013 alignof ( type-id ) [C++0x]
8020 __extension__ cast-expression
8021 __alignof__ unary-expression
8022 __alignof__ ( type-id )
8023 alignof unary-expression [C++0x]
8024 __real__ cast-expression
8025 __imag__ cast-expression
8027 sizeof ( type-id ) { initializer-list , [opt] }
8028 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8029 __alignof__ ( type-id ) { initializer-list , [opt] }
8031 ADDRESS_P is true iff the unary-expression is appearing as the
8032 operand of the `&' operator. CAST_P is true if this expression is
8033 the target of a cast.
8035 Returns a representation of the expression. */
8038 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8039 bool address_p
, bool cast_p
, bool decltype_p
)
8042 enum tree_code unary_operator
;
8044 /* Peek at the next token. */
8045 token
= cp_lexer_peek_token (parser
->lexer
);
8046 /* Some keywords give away the kind of expression. */
8047 if (token
->type
== CPP_KEYWORD
)
8049 enum rid keyword
= token
->keyword
;
8058 location_t start_loc
= token
->location
;
8060 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8061 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8063 /* Consume the token. */
8064 cp_lexer_consume_token (parser
->lexer
);
8065 /* Parse the operand. */
8066 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8068 if (TYPE_P (operand
))
8069 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8073 /* ISO C++ defines alignof only with types, not with
8074 expressions. So pedwarn if alignof is used with a non-
8075 type expression. However, __alignof__ is ok. */
8077 pedwarn (token
->location
, OPT_Wpedantic
,
8078 "ISO C++ does not allow %<alignof%> "
8081 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8083 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8084 SIZEOF_EXPR with the original operand. */
8085 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8087 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8089 if (!processing_template_decl
&& TYPE_P (operand
))
8091 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8092 build1 (NOP_EXPR
, operand
,
8094 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8097 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8098 TREE_SIDE_EFFECTS (ret
) = 0;
8099 TREE_READONLY (ret
) = 1;
8103 /* Construct a location e.g. :
8106 with start == caret at the start of the "alignof"/"sizeof"
8107 token, with the endpoint at the final closing paren. */
8108 location_t finish_loc
8109 = cp_lexer_previous_token (parser
->lexer
)->location
;
8110 location_t compound_loc
8111 = make_location (start_loc
, start_loc
, finish_loc
);
8113 cp_expr
ret_expr (ret
);
8114 ret_expr
.set_location (compound_loc
);
8115 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8120 return cp_parser_new_expression (parser
);
8123 return cp_parser_delete_expression (parser
);
8127 /* The saved value of the PEDANTIC flag. */
8131 /* Save away the PEDANTIC flag. */
8132 cp_parser_extension_opt (parser
, &saved_pedantic
);
8133 /* Parse the cast-expression. */
8134 expr
= cp_parser_simple_cast_expression (parser
);
8135 /* Restore the PEDANTIC flag. */
8136 pedantic
= saved_pedantic
;
8146 /* Consume the `__real__' or `__imag__' token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 /* Parse the cast-expression. */
8149 expression
= cp_parser_simple_cast_expression (parser
);
8150 /* Create the complete representation. */
8151 return build_x_unary_op (token
->location
,
8152 (keyword
== RID_REALPART
8153 ? REALPART_EXPR
: IMAGPART_EXPR
),
8155 tf_warning_or_error
);
8159 case RID_TRANSACTION_ATOMIC
:
8160 case RID_TRANSACTION_RELAXED
:
8161 return cp_parser_transaction_expression (parser
, keyword
);
8166 const char *saved_message
;
8167 bool saved_integral_constant_expression_p
;
8168 bool saved_non_integral_constant_expression_p
;
8169 bool saved_greater_than_is_operator_p
;
8171 location_t start_loc
= token
->location
;
8173 cp_lexer_consume_token (parser
->lexer
);
8174 matching_parens parens
;
8175 parens
.require_open (parser
);
8177 saved_message
= parser
->type_definition_forbidden_message
;
8178 parser
->type_definition_forbidden_message
8179 = G_("types may not be defined in %<noexcept%> expressions");
8181 saved_integral_constant_expression_p
8182 = parser
->integral_constant_expression_p
;
8183 saved_non_integral_constant_expression_p
8184 = parser
->non_integral_constant_expression_p
;
8185 parser
->integral_constant_expression_p
= false;
8187 saved_greater_than_is_operator_p
8188 = parser
->greater_than_is_operator_p
;
8189 parser
->greater_than_is_operator_p
= true;
8191 ++cp_unevaluated_operand
;
8192 ++c_inhibit_evaluation_warnings
;
8193 ++cp_noexcept_operand
;
8194 expr
= cp_parser_expression (parser
);
8195 --cp_noexcept_operand
;
8196 --c_inhibit_evaluation_warnings
;
8197 --cp_unevaluated_operand
;
8199 parser
->greater_than_is_operator_p
8200 = saved_greater_than_is_operator_p
;
8202 parser
->integral_constant_expression_p
8203 = saved_integral_constant_expression_p
;
8204 parser
->non_integral_constant_expression_p
8205 = saved_non_integral_constant_expression_p
;
8207 parser
->type_definition_forbidden_message
= saved_message
;
8209 location_t finish_loc
8210 = cp_lexer_peek_token (parser
->lexer
)->location
;
8211 parens
.require_close (parser
);
8213 /* Construct a location of the form:
8216 with start == caret, finishing at the close-paren. */
8217 location_t noexcept_loc
8218 = make_location (start_loc
, start_loc
, finish_loc
);
8220 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8229 /* Look for the `:: new' and `:: delete', which also signal the
8230 beginning of a new-expression, or delete-expression,
8231 respectively. If the next token is `::', then it might be one of
8233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8237 /* See if the token after the `::' is one of the keywords in
8238 which we're interested. */
8239 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8240 /* If it's `new', we have a new-expression. */
8241 if (keyword
== RID_NEW
)
8242 return cp_parser_new_expression (parser
);
8243 /* Similarly, for `delete'. */
8244 else if (keyword
== RID_DELETE
)
8245 return cp_parser_delete_expression (parser
);
8248 /* Look for a unary operator. */
8249 unary_operator
= cp_parser_unary_operator (token
);
8250 /* The `++' and `--' operators can be handled similarly, even though
8251 they are not technically unary-operators in the grammar. */
8252 if (unary_operator
== ERROR_MARK
)
8254 if (token
->type
== CPP_PLUS_PLUS
)
8255 unary_operator
= PREINCREMENT_EXPR
;
8256 else if (token
->type
== CPP_MINUS_MINUS
)
8257 unary_operator
= PREDECREMENT_EXPR
;
8258 /* Handle the GNU address-of-label extension. */
8259 else if (cp_parser_allow_gnu_extensions_p (parser
)
8260 && token
->type
== CPP_AND_AND
)
8264 location_t start_loc
= token
->location
;
8266 /* Consume the '&&' token. */
8267 cp_lexer_consume_token (parser
->lexer
);
8268 /* Look for the identifier. */
8269 location_t finish_loc
8270 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8271 identifier
= cp_parser_identifier (parser
);
8272 /* Construct a location of the form:
8275 with caret==start at the "&&", finish at the end of the label. */
8276 location_t combined_loc
8277 = make_location (start_loc
, start_loc
, finish_loc
);
8278 /* Create an expression representing the address. */
8279 expression
= finish_label_address_expr (identifier
, combined_loc
);
8280 if (cp_parser_non_integral_constant_expression (parser
,
8282 expression
= error_mark_node
;
8286 if (unary_operator
!= ERROR_MARK
)
8288 cp_expr cast_expression
;
8289 cp_expr expression
= error_mark_node
;
8290 non_integral_constant non_constant_p
= NIC_NONE
;
8291 location_t loc
= token
->location
;
8292 tsubst_flags_t complain
= complain_flags (decltype_p
);
8294 /* Consume the operator token. */
8295 token
= cp_lexer_consume_token (parser
->lexer
);
8296 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8298 /* Parse the cast-expression. */
8300 = cp_parser_cast_expression (parser
,
8301 unary_operator
== ADDR_EXPR
,
8307 OP_TOKEN CAST_EXPRESSION
8308 ^~~~~~~~~~~~~~~~~~~~~~~~~
8309 with start==caret at the operator token, and
8310 extending to the end of the cast_expression. */
8311 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8313 /* Now, build an appropriate representation. */
8314 switch (unary_operator
)
8317 non_constant_p
= NIC_STAR
;
8318 expression
= build_x_indirect_ref (loc
, cast_expression
,
8321 /* TODO: build_x_indirect_ref does not always honor the
8322 location, so ensure it is set. */
8323 expression
.set_location (loc
);
8327 non_constant_p
= NIC_ADDR
;
8330 expression
= build_x_unary_op (loc
, unary_operator
,
8333 /* TODO: build_x_unary_op does not always honor the location,
8334 so ensure it is set. */
8335 expression
.set_location (loc
);
8338 case PREINCREMENT_EXPR
:
8339 case PREDECREMENT_EXPR
:
8340 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8341 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8344 /* Immediately fold negation of a constant, unless the constant is 0
8345 (since -0 == 0) or it would overflow. */
8346 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8347 && CONSTANT_CLASS_P (cast_expression
)
8348 && !integer_zerop (cast_expression
)
8349 && !TREE_OVERFLOW (cast_expression
))
8351 tree folded
= fold_build1 (unary_operator
,
8352 TREE_TYPE (cast_expression
),
8354 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8356 expression
= cp_expr (folded
, loc
);
8361 case UNARY_PLUS_EXPR
:
8362 case TRUTH_NOT_EXPR
:
8363 expression
= finish_unary_op_expr (loc
, unary_operator
,
8364 cast_expression
, complain
);
8371 if (non_constant_p
!= NIC_NONE
8372 && cp_parser_non_integral_constant_expression (parser
,
8374 expression
= error_mark_node
;
8379 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8380 /*member_access_only_p=*/false,
8385 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8386 unary-operator, the corresponding tree code is returned. */
8388 static enum tree_code
8389 cp_parser_unary_operator (cp_token
* token
)
8391 switch (token
->type
)
8394 return INDIRECT_REF
;
8400 return UNARY_PLUS_EXPR
;
8406 return TRUTH_NOT_EXPR
;
8409 return BIT_NOT_EXPR
;
8416 /* Parse a new-expression.
8419 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8420 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8422 Returns a representation of the expression. */
8425 cp_parser_new_expression (cp_parser
* parser
)
8427 bool global_scope_p
;
8428 vec
<tree
, va_gc
> *placement
;
8430 vec
<tree
, va_gc
> *initializer
;
8431 tree nelts
= NULL_TREE
;
8434 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8436 /* Look for the optional `::' operator. */
8438 = (cp_parser_global_scope_opt (parser
,
8439 /*current_scope_valid_p=*/false)
8441 /* Look for the `new' operator. */
8442 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8443 /* There's no easy way to tell a new-placement from the
8444 `( type-id )' construct. */
8445 cp_parser_parse_tentatively (parser
);
8446 /* Look for a new-placement. */
8447 placement
= cp_parser_new_placement (parser
);
8448 /* If that didn't work out, there's no new-placement. */
8449 if (!cp_parser_parse_definitely (parser
))
8451 if (placement
!= NULL
)
8452 release_tree_vector (placement
);
8456 /* If the next token is a `(', then we have a parenthesized
8458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8461 const char *saved_message
= parser
->type_definition_forbidden_message
;
8463 /* Consume the `('. */
8464 matching_parens parens
;
8465 parens
.consume_open (parser
);
8467 /* Parse the type-id. */
8468 parser
->type_definition_forbidden_message
8469 = G_("types may not be defined in a new-expression");
8471 type_id_in_expr_sentinel
s (parser
);
8472 type
= cp_parser_type_id (parser
);
8474 parser
->type_definition_forbidden_message
= saved_message
;
8476 /* Look for the closing `)'. */
8477 parens
.require_close (parser
);
8478 token
= cp_lexer_peek_token (parser
->lexer
);
8479 /* There should not be a direct-new-declarator in this production,
8480 but GCC used to allowed this, so we check and emit a sensible error
8481 message for this case. */
8482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8484 error_at (token
->location
,
8485 "array bound forbidden after parenthesized type-id");
8486 inform (token
->location
,
8487 "try removing the parentheses around the type-id");
8488 cp_parser_direct_new_declarator (parser
);
8491 /* Otherwise, there must be a new-type-id. */
8493 type
= cp_parser_new_type_id (parser
, &nelts
);
8495 /* If the next token is a `(' or '{', then we have a new-initializer. */
8496 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8497 if (token
->type
== CPP_OPEN_PAREN
8498 || token
->type
== CPP_OPEN_BRACE
)
8499 initializer
= cp_parser_new_initializer (parser
);
8503 /* A new-expression may not appear in an integral constant
8505 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8506 ret
= error_mark_node
;
8507 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8508 of a new-type-id or type-id of a new-expression, the new-expression shall
8509 contain a new-initializer of the form ( assignment-expression )".
8510 Additionally, consistently with the spirit of DR 1467, we want to accept
8511 'new auto { 2 }' too. */
8512 else if ((ret
= type_uses_auto (type
))
8513 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8514 && (vec_safe_length (initializer
) != 1
8515 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8516 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8518 error_at (token
->location
,
8519 "initialization of new-expression for type %<auto%> "
8520 "requires exactly one element");
8521 ret
= error_mark_node
;
8525 /* Construct a location e.g.:
8528 with caret == start at the start of the "new" token, and the end
8529 at the end of the final token we consumed. */
8530 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8531 location_t end_loc
= get_finish (end_tok
->location
);
8532 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8534 /* Create a representation of the new-expression. */
8535 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8536 tf_warning_or_error
);
8537 protected_set_expr_location (ret
, combined_loc
);
8540 if (placement
!= NULL
)
8541 release_tree_vector (placement
);
8542 if (initializer
!= NULL
)
8543 release_tree_vector (initializer
);
8548 /* Parse a new-placement.
8553 Returns the same representation as for an expression-list. */
8555 static vec
<tree
, va_gc
> *
8556 cp_parser_new_placement (cp_parser
* parser
)
8558 vec
<tree
, va_gc
> *expression_list
;
8560 /* Parse the expression-list. */
8561 expression_list
= (cp_parser_parenthesized_expression_list
8562 (parser
, non_attr
, /*cast_p=*/false,
8563 /*allow_expansion_p=*/true,
8564 /*non_constant_p=*/NULL
));
8566 if (expression_list
&& expression_list
->is_empty ())
8567 error ("expected expression-list or type-id");
8569 return expression_list
;
8572 /* Parse a new-type-id.
8575 type-specifier-seq new-declarator [opt]
8577 Returns the TYPE allocated. If the new-type-id indicates an array
8578 type, *NELTS is set to the number of elements in the last array
8579 bound; the TYPE will not include the last array bound. */
8582 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8584 cp_decl_specifier_seq type_specifier_seq
;
8585 cp_declarator
*new_declarator
;
8586 cp_declarator
*declarator
;
8587 cp_declarator
*outer_declarator
;
8588 const char *saved_message
;
8590 /* The type-specifier sequence must not contain type definitions.
8591 (It cannot contain declarations of new types either, but if they
8592 are not definitions we will catch that because they are not
8594 saved_message
= parser
->type_definition_forbidden_message
;
8595 parser
->type_definition_forbidden_message
8596 = G_("types may not be defined in a new-type-id");
8597 /* Parse the type-specifier-seq. */
8598 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8599 /*is_trailing_return=*/false,
8600 &type_specifier_seq
);
8601 /* Restore the old message. */
8602 parser
->type_definition_forbidden_message
= saved_message
;
8604 if (type_specifier_seq
.type
== error_mark_node
)
8605 return error_mark_node
;
8607 /* Parse the new-declarator. */
8608 new_declarator
= cp_parser_new_declarator_opt (parser
);
8610 /* Determine the number of elements in the last array dimension, if
8613 /* Skip down to the last array dimension. */
8614 declarator
= new_declarator
;
8615 outer_declarator
= NULL
;
8616 while (declarator
&& (declarator
->kind
== cdk_pointer
8617 || declarator
->kind
== cdk_ptrmem
))
8619 outer_declarator
= declarator
;
8620 declarator
= declarator
->declarator
;
8623 && declarator
->kind
== cdk_array
8624 && declarator
->declarator
8625 && declarator
->declarator
->kind
== cdk_array
)
8627 outer_declarator
= declarator
;
8628 declarator
= declarator
->declarator
;
8631 if (declarator
&& declarator
->kind
== cdk_array
)
8633 *nelts
= declarator
->u
.array
.bounds
;
8634 if (*nelts
== error_mark_node
)
8635 *nelts
= integer_one_node
;
8637 if (outer_declarator
)
8638 outer_declarator
->declarator
= declarator
->declarator
;
8640 new_declarator
= NULL
;
8643 return groktypename (&type_specifier_seq
, new_declarator
, false);
8646 /* Parse an (optional) new-declarator.
8649 ptr-operator new-declarator [opt]
8650 direct-new-declarator
8652 Returns the declarator. */
8654 static cp_declarator
*
8655 cp_parser_new_declarator_opt (cp_parser
* parser
)
8657 enum tree_code code
;
8658 tree type
, std_attributes
= NULL_TREE
;
8659 cp_cv_quals cv_quals
;
8661 /* We don't know if there's a ptr-operator next, or not. */
8662 cp_parser_parse_tentatively (parser
);
8663 /* Look for a ptr-operator. */
8664 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8665 /* If that worked, look for more new-declarators. */
8666 if (cp_parser_parse_definitely (parser
))
8668 cp_declarator
*declarator
;
8670 /* Parse another optional declarator. */
8671 declarator
= cp_parser_new_declarator_opt (parser
);
8673 declarator
= cp_parser_make_indirect_declarator
8674 (code
, type
, cv_quals
, declarator
, std_attributes
);
8679 /* If the next token is a `[', there is a direct-new-declarator. */
8680 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8681 return cp_parser_direct_new_declarator (parser
);
8686 /* Parse a direct-new-declarator.
8688 direct-new-declarator:
8690 direct-new-declarator [constant-expression]
8694 static cp_declarator
*
8695 cp_parser_direct_new_declarator (cp_parser
* parser
)
8697 cp_declarator
*declarator
= NULL
;
8704 /* Look for the opening `['. */
8705 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8707 token
= cp_lexer_peek_token (parser
->lexer
);
8708 expression
= cp_parser_expression (parser
);
8709 /* The standard requires that the expression have integral
8710 type. DR 74 adds enumeration types. We believe that the
8711 real intent is that these expressions be handled like the
8712 expression in a `switch' condition, which also allows
8713 classes with a single conversion to integral or
8714 enumeration type. */
8715 if (!processing_template_decl
)
8718 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8723 error_at (token
->location
,
8724 "expression in new-declarator must have integral "
8725 "or enumeration type");
8726 expression
= error_mark_node
;
8730 /* Look for the closing `]'. */
8731 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8733 /* Add this bound to the declarator. */
8734 declarator
= make_array_declarator (declarator
, expression
);
8736 /* If the next token is not a `[', then there are no more
8738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8745 /* Parse a new-initializer.
8748 ( expression-list [opt] )
8751 Returns a representation of the expression-list. */
8753 static vec
<tree
, va_gc
> *
8754 cp_parser_new_initializer (cp_parser
* parser
)
8756 vec
<tree
, va_gc
> *expression_list
;
8758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8761 bool expr_non_constant_p
;
8762 cp_lexer_set_source_position (parser
->lexer
);
8763 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8764 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8765 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8766 expression_list
= make_tree_vector_single (t
);
8769 expression_list
= (cp_parser_parenthesized_expression_list
8770 (parser
, non_attr
, /*cast_p=*/false,
8771 /*allow_expansion_p=*/true,
8772 /*non_constant_p=*/NULL
));
8774 return expression_list
;
8777 /* Parse a delete-expression.
8780 :: [opt] delete cast-expression
8781 :: [opt] delete [ ] cast-expression
8783 Returns a representation of the expression. */
8786 cp_parser_delete_expression (cp_parser
* parser
)
8788 bool global_scope_p
;
8792 /* Look for the optional `::' operator. */
8794 = (cp_parser_global_scope_opt (parser
,
8795 /*current_scope_valid_p=*/false)
8797 /* Look for the `delete' keyword. */
8798 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8799 /* See if the array syntax is in use. */
8800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8802 /* Consume the `[' token. */
8803 cp_lexer_consume_token (parser
->lexer
);
8804 /* Look for the `]' token. */
8805 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8806 /* Remember that this is the `[]' construct. */
8812 /* Parse the cast-expression. */
8813 expression
= cp_parser_simple_cast_expression (parser
);
8815 /* A delete-expression may not appear in an integral constant
8817 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8818 return error_mark_node
;
8820 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8821 tf_warning_or_error
);
8824 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8825 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8829 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8831 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8832 switch (token
->type
)
8838 case CPP_CLOSE_SQUARE
:
8839 case CPP_CLOSE_PAREN
:
8840 case CPP_CLOSE_BRACE
:
8841 case CPP_OPEN_BRACE
:
8845 case CPP_DEREF_STAR
:
8853 case CPP_GREATER_EQ
:
8874 case CPP_OPEN_PAREN
:
8875 /* In ((type ()) () the last () isn't a valid cast-expression,
8876 so the whole must be parsed as postfix-expression. */
8877 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8880 case CPP_OPEN_SQUARE
:
8881 /* '[' may start a primary-expression in obj-c++ and in C++11,
8882 as a lambda-expression, eg, '(void)[]{}'. */
8883 if (cxx_dialect
>= cxx11
)
8885 return c_dialect_objc ();
8888 case CPP_MINUS_MINUS
:
8889 /* '++' and '--' may or may not start a cast-expression:
8891 struct T { void operator++(int); };
8892 void f() { (T())++; }
8905 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8906 in the order: const_cast, static_cast, reinterpret_cast.
8908 Don't suggest dynamic_cast.
8910 Return the first legal cast kind found, or NULL otherwise. */
8913 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8917 /* Reuse the parser logic by attempting to build the various kinds of
8918 cast, with "complain" disabled.
8919 Identify the first such cast that is valid. */
8921 /* Don't attempt to run such logic within template processing. */
8922 if (processing_template_decl
)
8925 /* First try const_cast. */
8926 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8927 if (trial
!= error_mark_node
)
8928 return "const_cast";
8930 /* If that fails, try static_cast. */
8931 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8932 if (trial
!= error_mark_node
)
8933 return "static_cast";
8935 /* Finally, try reinterpret_cast. */
8936 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8937 if (trial
!= error_mark_node
)
8938 return "reinterpret_cast";
8940 /* No such cast possible. */
8944 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8945 suggesting how to convert a C-style cast of the form:
8949 to a C++-style cast.
8951 The primary range of RICHLOC is asssumed to be that of the original
8952 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8953 of the parens in the C-style cast. */
8956 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8957 location_t close_paren_loc
, tree orig_expr
,
8960 /* This function is non-trivial, so bail out now if the warning isn't
8961 going to be emitted. */
8962 if (!warn_old_style_cast
)
8965 /* Try to find a legal C++ cast, trying them in order:
8966 const_cast, static_cast, reinterpret_cast. */
8967 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8968 if (!cast_suggestion
)
8971 /* Replace the open paren with "CAST_SUGGESTION<". */
8973 pp_printf (&pp
, "%s<", cast_suggestion
);
8974 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8976 /* Replace the close paren with "> (". */
8977 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8979 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8980 rich_loc
->add_fixit_insert_after (")");
8984 /* Parse a cast-expression.
8988 ( type-id ) cast-expression
8990 ADDRESS_P is true iff the unary-expression is appearing as the
8991 operand of the `&' operator. CAST_P is true if this expression is
8992 the target of a cast.
8994 Returns a representation of the expression. */
8997 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8998 bool decltype_p
, cp_id_kind
* pidk
)
9000 /* If it's a `(', then we might be looking at a cast. */
9001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9003 tree type
= NULL_TREE
;
9004 cp_expr
expr (NULL_TREE
);
9005 int cast_expression
= 0;
9006 const char *saved_message
;
9008 /* There's no way to know yet whether or not this is a cast.
9009 For example, `(int (3))' is a unary-expression, while `(int)
9010 3' is a cast. So, we resort to parsing tentatively. */
9011 cp_parser_parse_tentatively (parser
);
9012 /* Types may not be defined in a cast. */
9013 saved_message
= parser
->type_definition_forbidden_message
;
9014 parser
->type_definition_forbidden_message
9015 = G_("types may not be defined in casts");
9016 /* Consume the `('. */
9017 matching_parens parens
;
9018 cp_token
*open_paren
= parens
.consume_open (parser
);
9019 location_t open_paren_loc
= open_paren
->location
;
9020 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9022 /* A very tricky bit is that `(struct S) { 3 }' is a
9023 compound-literal (which we permit in C++ as an extension).
9024 But, that construct is not a cast-expression -- it is a
9025 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9026 is legal; if the compound-literal were a cast-expression,
9027 you'd need an extra set of parentheses.) But, if we parse
9028 the type-id, and it happens to be a class-specifier, then we
9029 will commit to the parse at that point, because we cannot
9030 undo the action that is done when creating a new class. So,
9031 then we cannot back up and do a postfix-expression.
9033 Another tricky case is the following (c++/29234):
9035 struct S { void operator () (); };
9042 As a type-id we parse the parenthesized S()() as a function
9043 returning a function, groktypename complains and we cannot
9044 back up in this case either.
9046 Therefore, we scan ahead to the closing `)', and check to see
9047 if the tokens after the `)' can start a cast-expression. Otherwise
9048 we are dealing with an unary-expression, a postfix-expression
9051 Yet another tricky case, in C++11, is the following (c++/54891):
9055 The issue is that usually, besides the case of lambda-expressions,
9056 the parenthesized type-id cannot be followed by '[', and, eg, we
9057 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9058 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9059 we don't commit, we try a cast-expression, then an unary-expression.
9061 Save tokens so that we can put them back. */
9062 cp_lexer_save_tokens (parser
->lexer
);
9064 /* We may be looking at a cast-expression. */
9065 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9066 /*consume_paren=*/true))
9068 = cp_parser_tokens_start_cast_expression (parser
);
9070 /* Roll back the tokens we skipped. */
9071 cp_lexer_rollback_tokens (parser
->lexer
);
9072 /* If we aren't looking at a cast-expression, simulate an error so
9073 that the call to cp_parser_error_occurred below returns true. */
9074 if (!cast_expression
)
9075 cp_parser_simulate_error (parser
);
9078 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9079 parser
->in_type_id_in_expr_p
= true;
9080 /* Look for the type-id. */
9081 type
= cp_parser_type_id (parser
);
9082 /* Look for the closing `)'. */
9083 cp_token
*close_paren
= parens
.require_close (parser
);
9085 close_paren_loc
= close_paren
->location
;
9086 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9089 /* Restore the saved message. */
9090 parser
->type_definition_forbidden_message
= saved_message
;
9092 /* At this point this can only be either a cast or a
9093 parenthesized ctor such as `(T ())' that looks like a cast to
9094 function returning T. */
9095 if (!cp_parser_error_occurred (parser
))
9097 /* Only commit if the cast-expression doesn't start with
9098 '++', '--', or '[' in C++11. */
9099 if (cast_expression
> 0)
9100 cp_parser_commit_to_topmost_tentative_parse (parser
);
9102 expr
= cp_parser_cast_expression (parser
,
9103 /*address_p=*/false,
9105 /*decltype_p=*/false,
9108 if (cp_parser_parse_definitely (parser
))
9110 /* Warn about old-style casts, if so requested. */
9111 if (warn_old_style_cast
9112 && !in_system_header_at (input_location
)
9113 && !VOID_TYPE_P (type
)
9114 && current_lang_name
!= lang_name_c
)
9116 gcc_rich_location
rich_loc (input_location
);
9117 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9119 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9120 "use of old-style cast to %q#T", type
);
9123 /* Only type conversions to integral or enumeration types
9124 can be used in constant-expressions. */
9125 if (!cast_valid_in_integral_constant_expression_p (type
)
9126 && cp_parser_non_integral_constant_expression (parser
,
9128 return error_mark_node
;
9130 /* Perform the cast. */
9134 with start==caret at the open paren, extending to the
9136 location_t cast_loc
= make_location (open_paren_loc
,
9138 expr
.get_finish ());
9139 expr
= build_c_cast (cast_loc
, type
, expr
);
9144 cp_parser_abort_tentative_parse (parser
);
9147 /* If we get here, then it's not a cast, so it must be a
9148 unary-expression. */
9149 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9150 cast_p
, decltype_p
);
9153 /* Parse a binary expression of the general form:
9157 pm-expression .* cast-expression
9158 pm-expression ->* cast-expression
9160 multiplicative-expression:
9162 multiplicative-expression * pm-expression
9163 multiplicative-expression / pm-expression
9164 multiplicative-expression % pm-expression
9166 additive-expression:
9167 multiplicative-expression
9168 additive-expression + multiplicative-expression
9169 additive-expression - multiplicative-expression
9173 shift-expression << additive-expression
9174 shift-expression >> additive-expression
9176 relational-expression:
9178 relational-expression < shift-expression
9179 relational-expression > shift-expression
9180 relational-expression <= shift-expression
9181 relational-expression >= shift-expression
9185 relational-expression:
9186 relational-expression <? shift-expression
9187 relational-expression >? shift-expression
9189 equality-expression:
9190 relational-expression
9191 equality-expression == relational-expression
9192 equality-expression != relational-expression
9196 and-expression & equality-expression
9198 exclusive-or-expression:
9200 exclusive-or-expression ^ and-expression
9202 inclusive-or-expression:
9203 exclusive-or-expression
9204 inclusive-or-expression | exclusive-or-expression
9206 logical-and-expression:
9207 inclusive-or-expression
9208 logical-and-expression && inclusive-or-expression
9210 logical-or-expression:
9211 logical-and-expression
9212 logical-or-expression || logical-and-expression
9214 All these are implemented with a single function like:
9217 simple-cast-expression
9218 binary-expression <token> binary-expression
9220 CAST_P is true if this expression is the target of a cast.
9222 The binops_by_token map is used to get the tree codes for each <token> type.
9223 binary-expressions are associated according to a precedence table. */
9225 #define TOKEN_PRECEDENCE(token) \
9226 (((token->type == CPP_GREATER \
9227 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9228 && !parser->greater_than_is_operator_p) \
9229 ? PREC_NOT_OPERATOR \
9230 : binops_by_token[token->type].prec)
9233 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9234 bool no_toplevel_fold_p
,
9236 enum cp_parser_prec prec
,
9239 cp_parser_expression_stack stack
;
9240 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9241 cp_parser_expression_stack_entry current
;
9244 enum tree_code rhs_type
;
9245 enum cp_parser_prec new_prec
, lookahead_prec
;
9248 /* Parse the first expression. */
9249 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9250 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9251 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9252 cast_p
, decltype_p
, pidk
);
9253 current
.prec
= prec
;
9255 if (cp_parser_error_occurred (parser
))
9256 return error_mark_node
;
9260 /* Get an operator token. */
9261 token
= cp_lexer_peek_token (parser
->lexer
);
9263 if (warn_cxx11_compat
9264 && token
->type
== CPP_RSHIFT
9265 && !parser
->greater_than_is_operator_p
)
9267 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9268 "%<>>%> operator is treated"
9269 " as two right angle brackets in C++11"))
9270 inform (token
->location
,
9271 "suggest parentheses around %<>>%> expression");
9274 new_prec
= TOKEN_PRECEDENCE (token
);
9275 if (new_prec
!= PREC_NOT_OPERATOR
9276 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9277 /* This is a fold-expression; handle it later. */
9278 new_prec
= PREC_NOT_OPERATOR
;
9280 /* Popping an entry off the stack means we completed a subexpression:
9281 - either we found a token which is not an operator (`>' where it is not
9282 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9283 will happen repeatedly;
9284 - or, we found an operator which has lower priority. This is the case
9285 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9287 if (new_prec
<= current
.prec
)
9296 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9297 current
.loc
= token
->location
;
9299 /* We used the operator token. */
9300 cp_lexer_consume_token (parser
->lexer
);
9302 /* For "false && x" or "true || x", x will never be executed;
9303 disable warnings while evaluating it. */
9304 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9305 c_inhibit_evaluation_warnings
+=
9306 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9307 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9308 c_inhibit_evaluation_warnings
+=
9309 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9311 /* Extract another operand. It may be the RHS of this expression
9312 or the LHS of a new, higher priority expression. */
9313 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9314 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9315 rhs
= cp_parser_simple_cast_expression (parser
);
9317 /* Get another operator token. Look up its precedence to avoid
9318 building a useless (immediately popped) stack entry for common
9319 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9320 token
= cp_lexer_peek_token (parser
->lexer
);
9321 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9322 if (lookahead_prec
!= PREC_NOT_OPERATOR
9323 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9324 lookahead_prec
= PREC_NOT_OPERATOR
;
9325 if (lookahead_prec
> new_prec
)
9327 /* ... and prepare to parse the RHS of the new, higher priority
9328 expression. Since precedence levels on the stack are
9329 monotonically increasing, we do not have to care about
9334 current
.lhs_type
= rhs_type
;
9335 current
.prec
= new_prec
;
9336 new_prec
= lookahead_prec
;
9340 lookahead_prec
= new_prec
;
9341 /* If the stack is not empty, we have parsed into LHS the right side
9342 (`4' in the example above) of an expression we had suspended.
9343 We can use the information on the stack to recover the LHS (`3')
9344 from the stack together with the tree code (`MULT_EXPR'), and
9345 the precedence of the higher level subexpression
9346 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9347 which will be used to actually build the additive expression. */
9349 rhs_type
= current
.lhs_type
;
9354 /* Undo the disabling of warnings done above. */
9355 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9356 c_inhibit_evaluation_warnings
-=
9357 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9358 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9359 c_inhibit_evaluation_warnings
-=
9360 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9362 if (warn_logical_not_paren
9363 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9364 && current
.lhs_type
== TRUTH_NOT_EXPR
9365 /* Avoid warning for !!x == y. */
9366 && (TREE_CODE (current
.lhs
) != NE_EXPR
9367 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9368 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9369 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9370 /* Avoid warning for !b == y where b is boolean. */
9371 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9372 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9374 /* Avoid warning for !!b == y where b is boolean. */
9375 && (!DECL_P (current
.lhs
)
9376 || TREE_TYPE (current
.lhs
) == NULL_TREE
9377 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9378 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9379 current
.lhs
, maybe_constant_value (rhs
));
9383 location_t combined_loc
= make_location (current
.loc
,
9384 current
.lhs
.get_start (),
9387 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9388 ERROR_MARK for everything that is not a binary expression.
9389 This makes warn_about_parentheses miss some warnings that
9390 involve unary operators. For unary expressions we should
9391 pass the correct tree_code unless the unary expression was
9392 surrounded by parentheses.
9394 if (no_toplevel_fold_p
9395 && lookahead_prec
<= current
.prec
9398 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9399 current
.lhs
= error_mark_node
;
9403 = build_min (current
.tree_type
,
9404 TREE_CODE_CLASS (current
.tree_type
)
9406 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9407 current
.lhs
.get_value (), rhs
.get_value ());
9408 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9413 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9414 current
.lhs
, current
.lhs_type
,
9415 rhs
, rhs_type
, &overload
,
9416 complain_flags (decltype_p
));
9417 /* TODO: build_x_binary_op doesn't always honor the location. */
9418 current
.lhs
.set_location (combined_loc
);
9420 current
.lhs_type
= current
.tree_type
;
9422 /* If the binary operator required the use of an overloaded operator,
9423 then this expression cannot be an integral constant-expression.
9424 An overloaded operator can be used even if both operands are
9425 otherwise permissible in an integral constant-expression if at
9426 least one of the operands is of enumeration type. */
9429 && cp_parser_non_integral_constant_expression (parser
,
9431 return error_mark_node
;
9438 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9439 bool no_toplevel_fold_p
,
9440 enum cp_parser_prec prec
,
9443 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9444 /*decltype*/false, prec
, pidk
);
9447 /* Parse the `? expression : assignment-expression' part of a
9448 conditional-expression. The LOGICAL_OR_EXPR is the
9449 logical-or-expression that started the conditional-expression.
9450 Returns a representation of the entire conditional-expression.
9452 This routine is used by cp_parser_assignment_expression.
9454 ? expression : assignment-expression
9458 ? : assignment-expression */
9461 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9463 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9464 cp_expr assignment_expr
;
9465 struct cp_token
*token
;
9466 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9468 /* Consume the `?' token. */
9469 cp_lexer_consume_token (parser
->lexer
);
9470 token
= cp_lexer_peek_token (parser
->lexer
);
9471 if (cp_parser_allow_gnu_extensions_p (parser
)
9472 && token
->type
== CPP_COLON
)
9474 pedwarn (token
->location
, OPT_Wpedantic
,
9475 "ISO C++ does not allow ?: with omitted middle operand");
9476 /* Implicit true clause. */
9478 c_inhibit_evaluation_warnings
+=
9479 folded_logical_or_expr
== truthvalue_true_node
;
9480 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9484 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9485 parser
->colon_corrects_to_scope_p
= false;
9486 /* Parse the expression. */
9487 c_inhibit_evaluation_warnings
+=
9488 folded_logical_or_expr
== truthvalue_false_node
;
9489 expr
= cp_parser_expression (parser
);
9490 c_inhibit_evaluation_warnings
+=
9491 ((folded_logical_or_expr
== truthvalue_true_node
)
9492 - (folded_logical_or_expr
== truthvalue_false_node
));
9493 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9496 /* The next token should be a `:'. */
9497 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9498 /* Parse the assignment-expression. */
9499 assignment_expr
= cp_parser_assignment_expression (parser
);
9500 c_inhibit_evaluation_warnings
-=
9501 folded_logical_or_expr
== truthvalue_true_node
;
9504 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9505 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9506 with the caret at the "?", ranging from the start of
9507 the logical_or_expr to the end of the assignment_expr. */
9508 loc
= make_location (loc
,
9509 logical_or_expr
.get_start (),
9510 assignment_expr
.get_finish ());
9512 /* Build the conditional-expression. */
9513 return build_x_conditional_expr (loc
, logical_or_expr
,
9516 tf_warning_or_error
);
9519 /* Parse an assignment-expression.
9521 assignment-expression:
9522 conditional-expression
9523 logical-or-expression assignment-operator assignment_expression
9526 CAST_P is true if this expression is the target of a cast.
9527 DECLTYPE_P is true if this expression is the operand of decltype.
9529 Returns a representation for the expression. */
9532 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9533 bool cast_p
, bool decltype_p
)
9537 /* If the next token is the `throw' keyword, then we're looking at
9538 a throw-expression. */
9539 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9540 expr
= cp_parser_throw_expression (parser
);
9541 /* Otherwise, it must be that we are looking at a
9542 logical-or-expression. */
9545 /* Parse the binary expressions (logical-or-expression). */
9546 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9548 PREC_NOT_OPERATOR
, pidk
);
9549 /* If the next token is a `?' then we're actually looking at a
9550 conditional-expression. */
9551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9552 return cp_parser_question_colon_clause (parser
, expr
);
9555 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9557 /* If it's an assignment-operator, we're using the second
9559 enum tree_code assignment_operator
9560 = cp_parser_assignment_operator_opt (parser
);
9561 if (assignment_operator
!= ERROR_MARK
)
9563 bool non_constant_p
;
9565 /* Parse the right-hand side of the assignment. */
9566 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9569 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9572 /* An assignment may not appear in a
9573 constant-expression. */
9574 if (cp_parser_non_integral_constant_expression (parser
,
9576 return error_mark_node
;
9577 /* Build the assignment expression. Its default
9581 is the location of the '=' token as the
9582 caret, ranging from the start of the lhs to the
9584 loc
= make_location (loc
,
9587 expr
= build_x_modify_expr (loc
, expr
,
9588 assignment_operator
,
9590 complain_flags (decltype_p
));
9591 /* TODO: build_x_modify_expr doesn't honor the location,
9592 so we must set it here. */
9593 expr
.set_location (loc
);
9601 /* Parse an (optional) assignment-operator.
9603 assignment-operator: one of
9604 = *= /= %= += -= >>= <<= &= ^= |=
9608 assignment-operator: one of
9611 If the next token is an assignment operator, the corresponding tree
9612 code is returned, and the token is consumed. For example, for
9613 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9614 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9615 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9616 operator, ERROR_MARK is returned. */
9618 static enum tree_code
9619 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9624 /* Peek at the next token. */
9625 token
= cp_lexer_peek_token (parser
->lexer
);
9627 switch (token
->type
)
9638 op
= TRUNC_DIV_EXPR
;
9642 op
= TRUNC_MOD_EXPR
;
9674 /* Nothing else is an assignment operator. */
9678 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9679 if (op
!= ERROR_MARK
9680 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9683 /* If it was an assignment operator, consume it. */
9684 if (op
!= ERROR_MARK
)
9685 cp_lexer_consume_token (parser
->lexer
);
9690 /* Parse an expression.
9693 assignment-expression
9694 expression , assignment-expression
9696 CAST_P is true if this expression is the target of a cast.
9697 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9698 except possibly parenthesized or on the RHS of a comma (N3276).
9700 Returns a representation of the expression. */
9703 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9704 bool cast_p
, bool decltype_p
)
9706 cp_expr expression
= NULL_TREE
;
9707 location_t loc
= UNKNOWN_LOCATION
;
9711 cp_expr assignment_expression
;
9713 /* Parse the next assignment-expression. */
9714 assignment_expression
9715 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9717 /* We don't create a temporary for a call that is the immediate operand
9718 of decltype or on the RHS of a comma. But when we see a comma, we
9719 need to create a temporary for a call on the LHS. */
9720 if (decltype_p
&& !processing_template_decl
9721 && TREE_CODE (assignment_expression
) == CALL_EXPR
9722 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9723 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9724 assignment_expression
9725 = build_cplus_new (TREE_TYPE (assignment_expression
),
9726 assignment_expression
, tf_warning_or_error
);
9728 /* If this is the first assignment-expression, we can just
9731 expression
= assignment_expression
;
9734 /* Create a location with caret at the comma, ranging
9735 from the start of the LHS to the end of the RHS. */
9736 loc
= make_location (loc
,
9737 expression
.get_start (),
9738 assignment_expression
.get_finish ());
9739 expression
= build_x_compound_expr (loc
, expression
,
9740 assignment_expression
,
9741 complain_flags (decltype_p
));
9742 expression
.set_location (loc
);
9744 /* If the next token is not a comma, or we're in a fold-expression, then
9745 we are done with the expression. */
9746 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9747 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9749 /* Consume the `,'. */
9750 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9751 cp_lexer_consume_token (parser
->lexer
);
9752 /* A comma operator cannot appear in a constant-expression. */
9753 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9754 expression
= error_mark_node
;
9760 /* Parse a constant-expression.
9762 constant-expression:
9763 conditional-expression
9765 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9766 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9767 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9768 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9769 only parse a conditional-expression, otherwise parse an
9770 assignment-expression. See below for rationale. */
9773 cp_parser_constant_expression (cp_parser
* parser
,
9774 bool allow_non_constant_p
,
9775 bool *non_constant_p
,
9778 bool saved_integral_constant_expression_p
;
9779 bool saved_allow_non_integral_constant_expression_p
;
9780 bool saved_non_integral_constant_expression_p
;
9783 /* It might seem that we could simply parse the
9784 conditional-expression, and then check to see if it were
9785 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9786 one that the compiler can figure out is constant, possibly after
9787 doing some simplifications or optimizations. The standard has a
9788 precise definition of constant-expression, and we must honor
9789 that, even though it is somewhat more restrictive.
9795 is not a legal declaration, because `(2, 3)' is not a
9796 constant-expression. The `,' operator is forbidden in a
9797 constant-expression. However, GCC's constant-folding machinery
9798 will fold this operation to an INTEGER_CST for `3'. */
9800 /* Save the old settings. */
9801 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9802 saved_allow_non_integral_constant_expression_p
9803 = parser
->allow_non_integral_constant_expression_p
;
9804 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9805 /* We are now parsing a constant-expression. */
9806 parser
->integral_constant_expression_p
= true;
9807 parser
->allow_non_integral_constant_expression_p
9808 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9809 parser
->non_integral_constant_expression_p
= false;
9810 /* Although the grammar says "conditional-expression", when not STRICT_P,
9811 we parse an "assignment-expression", which also permits
9812 "throw-expression" and the use of assignment operators. In the case
9813 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9814 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9815 actually essential that we look for an assignment-expression.
9816 For example, cp_parser_initializer_clauses uses this function to
9817 determine whether a particular assignment-expression is in fact
9821 /* Parse the binary expressions (logical-or-expression). */
9822 expression
= cp_parser_binary_expression (parser
, false, false, false,
9823 PREC_NOT_OPERATOR
, NULL
);
9824 /* If the next token is a `?' then we're actually looking at
9825 a conditional-expression; otherwise we're done. */
9826 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9827 expression
= cp_parser_question_colon_clause (parser
, expression
);
9830 expression
= cp_parser_assignment_expression (parser
);
9831 /* Restore the old settings. */
9832 parser
->integral_constant_expression_p
9833 = saved_integral_constant_expression_p
;
9834 parser
->allow_non_integral_constant_expression_p
9835 = saved_allow_non_integral_constant_expression_p
;
9836 if (cxx_dialect
>= cxx11
)
9838 /* Require an rvalue constant expression here; that's what our
9839 callers expect. Reference constant expressions are handled
9840 separately in e.g. cp_parser_template_argument. */
9841 tree decay
= expression
;
9842 if (TREE_TYPE (expression
)
9843 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9844 decay
= build_address (expression
);
9845 bool is_const
= potential_rvalue_constant_expression (decay
);
9846 parser
->non_integral_constant_expression_p
= !is_const
;
9847 if (!is_const
&& !allow_non_constant_p
)
9848 require_potential_rvalue_constant_expression (decay
);
9850 if (allow_non_constant_p
)
9851 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9852 parser
->non_integral_constant_expression_p
9853 = saved_non_integral_constant_expression_p
;
9858 /* Parse __builtin_offsetof.
9860 offsetof-expression:
9861 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9863 offsetof-member-designator:
9865 | offsetof-member-designator "." id-expression
9866 | offsetof-member-designator "[" expression "]"
9867 | offsetof-member-designator "->" id-expression */
9870 cp_parser_builtin_offsetof (cp_parser
*parser
)
9872 int save_ice_p
, save_non_ice_p
;
9877 location_t finish_loc
;
9879 /* We're about to accept non-integral-constant things, but will
9880 definitely yield an integral constant expression. Save and
9881 restore these values around our local parsing. */
9882 save_ice_p
= parser
->integral_constant_expression_p
;
9883 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9885 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9887 /* Consume the "__builtin_offsetof" token. */
9888 cp_lexer_consume_token (parser
->lexer
);
9889 /* Consume the opening `('. */
9890 matching_parens parens
;
9891 parens
.require_open (parser
);
9892 /* Parse the type-id. */
9893 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9895 const char *saved_message
= parser
->type_definition_forbidden_message
;
9896 parser
->type_definition_forbidden_message
9897 = G_("types may not be defined within __builtin_offsetof");
9898 type
= cp_parser_type_id (parser
);
9899 parser
->type_definition_forbidden_message
= saved_message
;
9901 /* Look for the `,'. */
9902 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9903 token
= cp_lexer_peek_token (parser
->lexer
);
9905 /* Build the (type *)null that begins the traditional offsetof macro. */
9907 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9908 tf_warning_or_error
);
9910 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9911 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9912 true, &dummy
, token
->location
);
9915 token
= cp_lexer_peek_token (parser
->lexer
);
9916 switch (token
->type
)
9918 case CPP_OPEN_SQUARE
:
9919 /* offsetof-member-designator "[" expression "]" */
9920 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9925 /* offsetof-member-designator "->" identifier */
9926 expr
= grok_array_decl (token
->location
, expr
,
9927 integer_zero_node
, false);
9931 /* offsetof-member-designator "." identifier */
9932 cp_lexer_consume_token (parser
->lexer
);
9933 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9938 case CPP_CLOSE_PAREN
:
9939 /* Consume the ")" token. */
9940 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9941 cp_lexer_consume_token (parser
->lexer
);
9945 /* Error. We know the following require will fail, but
9946 that gives the proper error message. */
9947 parens
.require_close (parser
);
9948 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9949 expr
= error_mark_node
;
9955 /* Make a location of the form:
9956 __builtin_offsetof (struct s, f)
9957 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9958 with caret at the type-id, ranging from the start of the
9959 "_builtin_offsetof" token to the close paren. */
9960 loc
= make_location (loc
, start_loc
, finish_loc
);
9961 /* The result will be an INTEGER_CST, so we need to explicitly
9962 preserve the location. */
9963 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9966 parser
->integral_constant_expression_p
= save_ice_p
;
9967 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9969 expr
= expr
.maybe_add_location_wrapper ();
9973 /* Parse a trait expression.
9975 Returns a representation of the expression, the underlying type
9976 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9979 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9982 tree type1
, type2
= NULL_TREE
;
9983 bool binary
= false;
9984 bool variadic
= false;
9988 case RID_HAS_NOTHROW_ASSIGN
:
9989 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9991 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9992 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9994 case RID_HAS_NOTHROW_COPY
:
9995 kind
= CPTK_HAS_NOTHROW_COPY
;
9997 case RID_HAS_TRIVIAL_ASSIGN
:
9998 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10000 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10001 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10003 case RID_HAS_TRIVIAL_COPY
:
10004 kind
= CPTK_HAS_TRIVIAL_COPY
;
10006 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10007 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10009 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10010 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10012 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10013 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10015 case RID_IS_ABSTRACT
:
10016 kind
= CPTK_IS_ABSTRACT
;
10018 case RID_IS_AGGREGATE
:
10019 kind
= CPTK_IS_AGGREGATE
;
10021 case RID_IS_BASE_OF
:
10022 kind
= CPTK_IS_BASE_OF
;
10026 kind
= CPTK_IS_CLASS
;
10029 kind
= CPTK_IS_EMPTY
;
10032 kind
= CPTK_IS_ENUM
;
10035 kind
= CPTK_IS_FINAL
;
10037 case RID_IS_LITERAL_TYPE
:
10038 kind
= CPTK_IS_LITERAL_TYPE
;
10041 kind
= CPTK_IS_POD
;
10043 case RID_IS_POLYMORPHIC
:
10044 kind
= CPTK_IS_POLYMORPHIC
;
10046 case RID_IS_SAME_AS
:
10047 kind
= CPTK_IS_SAME_AS
;
10050 case RID_IS_STD_LAYOUT
:
10051 kind
= CPTK_IS_STD_LAYOUT
;
10053 case RID_IS_TRIVIAL
:
10054 kind
= CPTK_IS_TRIVIAL
;
10056 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10057 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10060 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10061 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10064 case RID_IS_TRIVIALLY_COPYABLE
:
10065 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10068 kind
= CPTK_IS_UNION
;
10070 case RID_UNDERLYING_TYPE
:
10071 kind
= CPTK_UNDERLYING_TYPE
;
10076 case RID_DIRECT_BASES
:
10077 kind
= CPTK_DIRECT_BASES
;
10079 case RID_IS_ASSIGNABLE
:
10080 kind
= CPTK_IS_ASSIGNABLE
;
10083 case RID_IS_CONSTRUCTIBLE
:
10084 kind
= CPTK_IS_CONSTRUCTIBLE
;
10088 gcc_unreachable ();
10091 /* Get location of initial token. */
10092 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10094 /* Consume the token. */
10095 cp_lexer_consume_token (parser
->lexer
);
10097 matching_parens parens
;
10098 parens
.require_open (parser
);
10101 type_id_in_expr_sentinel
s (parser
);
10102 type1
= cp_parser_type_id (parser
);
10105 if (type1
== error_mark_node
)
10106 return error_mark_node
;
10110 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10113 type_id_in_expr_sentinel
s (parser
);
10114 type2
= cp_parser_type_id (parser
);
10117 if (type2
== error_mark_node
)
10118 return error_mark_node
;
10122 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10124 cp_lexer_consume_token (parser
->lexer
);
10125 tree elt
= cp_parser_type_id (parser
);
10126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10128 cp_lexer_consume_token (parser
->lexer
);
10129 elt
= make_pack_expansion (elt
);
10131 if (elt
== error_mark_node
)
10132 return error_mark_node
;
10133 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10137 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10138 parens
.require_close (parser
);
10140 /* Construct a location of the form:
10141 __is_trivially_copyable(_Tp)
10142 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10143 with start == caret, finishing at the close-paren. */
10144 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10146 /* Complete the trait expression, which may mean either processing
10147 the trait expr now or saving it for template instantiation. */
10150 case CPTK_UNDERLYING_TYPE
:
10151 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10153 return cp_expr (finish_bases (type1
, false), trait_loc
);
10154 case CPTK_DIRECT_BASES
:
10155 return cp_expr (finish_bases (type1
, true), trait_loc
);
10157 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10161 /* Parse a lambda expression.
10164 lambda-introducer lambda-declarator [opt] compound-statement
10166 Returns a representation of the expression. */
10169 cp_parser_lambda_expression (cp_parser
* parser
)
10171 tree lambda_expr
= build_lambda_expr ();
10174 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10175 cp_token_position start
= 0;
10177 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10179 if (cxx_dialect
>= cxx2a
)
10180 /* C++20 allows lambdas in unevaluated context. */;
10181 else if (cp_unevaluated_operand
)
10183 if (!token
->error_reported
)
10185 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10186 "lambda-expression in unevaluated context"
10187 " only available with -std=c++2a or -std=gnu++2a");
10188 token
->error_reported
= true;
10192 else if (parser
->in_template_argument_list_p
)
10194 if (!token
->error_reported
)
10196 error_at (token
->location
, "lambda-expression in template-argument"
10197 " only available with -std=c++2a or -std=gnu++2a");
10198 token
->error_reported
= true;
10203 /* We may be in the middle of deferred access check. Disable
10205 push_deferring_access_checks (dk_no_deferred
);
10207 cp_parser_lambda_introducer (parser
, lambda_expr
);
10208 if (cp_parser_error_occurred (parser
))
10209 return error_mark_node
;
10211 type
= begin_lambda_type (lambda_expr
);
10212 if (type
== error_mark_node
)
10213 return error_mark_node
;
10215 record_lambda_scope (lambda_expr
);
10217 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10218 determine_visibility (TYPE_NAME (type
));
10220 /* Now that we've started the type, add the capture fields for any
10221 explicit captures. */
10222 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10225 /* Inside the class, surrounding template-parameter-lists do not apply. */
10226 unsigned int saved_num_template_parameter_lists
10227 = parser
->num_template_parameter_lists
;
10228 unsigned char in_statement
= parser
->in_statement
;
10229 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10230 bool fully_implicit_function_template_p
10231 = parser
->fully_implicit_function_template_p
;
10232 tree implicit_template_parms
= parser
->implicit_template_parms
;
10233 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10234 bool auto_is_implicit_function_template_parm_p
10235 = parser
->auto_is_implicit_function_template_parm_p
;
10237 parser
->num_template_parameter_lists
= 0;
10238 parser
->in_statement
= 0;
10239 parser
->in_switch_statement_p
= false;
10240 parser
->fully_implicit_function_template_p
= false;
10241 parser
->implicit_template_parms
= 0;
10242 parser
->implicit_template_scope
= 0;
10243 parser
->auto_is_implicit_function_template_parm_p
= false;
10245 /* By virtue of defining a local class, a lambda expression has access to
10246 the private variables of enclosing classes. */
10248 if (cp_parser_start_tentative_firewall (parser
))
10251 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10253 if (ok
&& cp_parser_error_occurred (parser
))
10258 cp_parser_lambda_body (parser
, lambda_expr
);
10260 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10262 if (cp_parser_skip_to_closing_brace (parser
))
10263 cp_lexer_consume_token (parser
->lexer
);
10266 /* The capture list was built up in reverse order; fix that now. */
10267 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10268 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10271 maybe_add_lambda_conv_op (type
);
10273 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10275 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10276 parser
->in_statement
= in_statement
;
10277 parser
->in_switch_statement_p
= in_switch_statement_p
;
10278 parser
->fully_implicit_function_template_p
10279 = fully_implicit_function_template_p
;
10280 parser
->implicit_template_parms
= implicit_template_parms
;
10281 parser
->implicit_template_scope
= implicit_template_scope
;
10282 parser
->auto_is_implicit_function_template_parm_p
10283 = auto_is_implicit_function_template_parm_p
;
10286 /* This field is only used during parsing of the lambda. */
10287 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10289 /* This lambda shouldn't have any proxies left at this point. */
10290 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10291 /* And now that we're done, push proxies for an enclosing lambda. */
10292 insert_pending_capture_proxies ();
10294 /* Update the lambda expression to a range. */
10295 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10296 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10298 end_tok
->location
);
10301 lambda_expr
= build_lambda_object (lambda_expr
);
10303 lambda_expr
= error_mark_node
;
10305 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10307 pop_deferring_access_checks ();
10309 return lambda_expr
;
10312 /* Parse the beginning of a lambda expression.
10315 [ lambda-capture [opt] ]
10317 LAMBDA_EXPR is the current representation of the lambda expression. */
10320 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10322 /* Need commas after the first capture. */
10325 /* Eat the leading `['. */
10326 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10328 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10330 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10331 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10332 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10333 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10335 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10337 cp_lexer_consume_token (parser
->lexer
);
10340 if (!(at_function_scope_p () || parsing_nsdmi ()))
10341 error ("non-local lambda expression cannot have a capture-default");
10344 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10346 cp_token
* capture_token
;
10348 tree capture_init_expr
;
10349 cp_id_kind idk
= CP_ID_KIND_NONE
;
10350 bool explicit_init_p
= false;
10352 enum capture_kind_type
10357 enum capture_kind_type capture_kind
= BY_COPY
;
10359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10361 error ("expected end of capture-list");
10368 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10370 /* Possibly capture `this'. */
10371 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10373 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10374 if (cxx_dialect
< cxx2a
10375 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10376 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10377 "with by-copy capture default");
10378 cp_lexer_consume_token (parser
->lexer
);
10379 add_capture (lambda_expr
,
10380 /*id=*/this_identifier
,
10381 /*initializer=*/finish_this_expr (),
10382 /*by_reference_p=*/true,
10387 /* Possibly capture `*this'. */
10388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10389 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10391 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10392 if (cxx_dialect
< cxx17
)
10393 pedwarn (loc
, 0, "%<*this%> capture only available with "
10394 "-std=c++17 or -std=gnu++17");
10395 cp_lexer_consume_token (parser
->lexer
);
10396 cp_lexer_consume_token (parser
->lexer
);
10397 add_capture (lambda_expr
,
10398 /*id=*/this_identifier
,
10399 /*initializer=*/finish_this_expr (),
10400 /*by_reference_p=*/false,
10405 bool init_pack_expansion
= false;
10406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10408 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10409 if (cxx_dialect
< cxx2a
)
10410 pedwarn (loc
, 0, "pack init-capture only available with "
10411 "-std=c++2a or -std=gnu++2a");
10412 cp_lexer_consume_token (parser
->lexer
);
10413 init_pack_expansion
= true;
10416 /* Remember whether we want to capture as a reference or not. */
10417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10419 capture_kind
= BY_REFERENCE
;
10420 cp_lexer_consume_token (parser
->lexer
);
10423 /* Get the identifier. */
10424 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10425 capture_id
= cp_parser_identifier (parser
);
10427 if (capture_id
== error_mark_node
)
10428 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10429 delimiters, but I modified this to stop on unnested ']' as well. It
10430 was already changed to stop on unnested '}', so the
10431 "closing_parenthesis" name is no more misleading with my change. */
10433 cp_parser_skip_to_closing_parenthesis (parser
,
10434 /*recovering=*/true,
10436 /*consume_paren=*/true);
10440 /* Find the initializer for this capture. */
10441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10442 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10443 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10445 bool direct
, non_constant
;
10446 /* An explicit initializer exists. */
10447 if (cxx_dialect
< cxx14
)
10448 pedwarn (input_location
, 0,
10449 "lambda capture initializers "
10450 "only available with -std=c++14 or -std=gnu++14");
10451 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10452 &non_constant
, true);
10453 explicit_init_p
= true;
10454 if (capture_init_expr
== NULL_TREE
)
10456 error ("empty initializer for lambda init-capture");
10457 capture_init_expr
= error_mark_node
;
10459 if (init_pack_expansion
)
10460 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10464 const char* error_msg
;
10466 /* Turn the identifier into an id-expression. */
10468 = cp_parser_lookup_name_simple (parser
, capture_id
,
10469 capture_token
->location
);
10471 if (capture_init_expr
== error_mark_node
)
10473 unqualified_name_lookup_error (capture_id
);
10476 else if (!VAR_P (capture_init_expr
)
10477 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10479 error_at (capture_token
->location
,
10480 "capture of non-variable %qE",
10481 capture_init_expr
);
10482 if (DECL_P (capture_init_expr
))
10483 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10484 "%q#D declared here", capture_init_expr
);
10487 if (VAR_P (capture_init_expr
)
10488 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10490 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10491 "%qD with non-automatic storage duration",
10492 capture_init_expr
))
10493 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10494 "%q#D declared here", capture_init_expr
);
10499 = finish_id_expression
10504 /*integral_constant_expression_p=*/false,
10505 /*allow_non_integral_constant_expression_p=*/false,
10506 /*non_integral_constant_expression_p=*/NULL
,
10507 /*template_p=*/false,
10509 /*address_p=*/false,
10510 /*template_arg_p=*/false,
10512 capture_token
->location
);
10514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10516 cp_lexer_consume_token (parser
->lexer
);
10517 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10521 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10522 && !explicit_init_p
)
10524 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10525 && capture_kind
== BY_COPY
)
10526 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10527 "of %qD redundant with by-copy capture default",
10529 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10530 && capture_kind
== BY_REFERENCE
)
10531 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10532 "capture of %qD redundant with by-reference capture "
10533 "default", capture_id
);
10536 add_capture (lambda_expr
,
10539 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10542 /* If there is any qualification still in effect, clear it
10543 now; we will be starting fresh with the next capture. */
10544 parser
->scope
= NULL_TREE
;
10545 parser
->qualifying_scope
= NULL_TREE
;
10546 parser
->object_scope
= NULL_TREE
;
10549 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10552 /* Parse the (optional) middle of a lambda expression.
10555 < template-parameter-list [opt] >
10556 ( parameter-declaration-clause [opt] )
10557 attribute-specifier [opt]
10558 decl-specifier-seq [opt]
10559 exception-specification [opt]
10560 lambda-return-type-clause [opt]
10562 LAMBDA_EXPR is the current representation of the lambda expression. */
10565 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10567 /* 5.1.1.4 of the standard says:
10568 If a lambda-expression does not include a lambda-declarator, it is as if
10569 the lambda-declarator were ().
10570 This means an empty parameter list, no attributes, and no exception
10572 tree param_list
= void_list_node
;
10573 tree attributes
= NULL_TREE
;
10574 tree exception_spec
= NULL_TREE
;
10575 tree template_param_list
= NULL_TREE
;
10576 tree tx_qual
= NULL_TREE
;
10577 tree return_type
= NULL_TREE
;
10578 cp_decl_specifier_seq lambda_specs
;
10579 clear_decl_specs (&lambda_specs
);
10581 /* The template-parameter-list is optional, but must begin with
10582 an opening angle if present. */
10583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10585 if (cxx_dialect
< cxx14
)
10586 pedwarn (parser
->lexer
->next_token
->location
, 0,
10587 "lambda templates are only available with "
10588 "-std=c++14 or -std=gnu++14");
10589 else if (cxx_dialect
< cxx2a
)
10590 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10591 "lambda templates are only available with "
10592 "-std=c++2a or -std=gnu++2a");
10594 cp_lexer_consume_token (parser
->lexer
);
10596 template_param_list
= cp_parser_template_parameter_list (parser
);
10598 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10600 /* We just processed one more parameter list. */
10601 ++parser
->num_template_parameter_lists
;
10604 /* The parameter-declaration-clause is optional (unless
10605 template-parameter-list was given), but must begin with an
10606 opening parenthesis if present. */
10607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10609 matching_parens parens
;
10610 parens
.consume_open (parser
);
10612 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10614 /* Parse parameters. */
10615 param_list
= cp_parser_parameter_declaration_clause (parser
);
10617 /* Default arguments shall not be specified in the
10618 parameter-declaration-clause of a lambda-declarator. */
10619 if (cxx_dialect
< cxx14
)
10620 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10621 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10622 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10623 "default argument specified for lambda parameter");
10625 parens
.require_close (parser
);
10627 /* In the decl-specifier-seq of the lambda-declarator, each
10628 decl-specifier shall either be mutable or constexpr. */
10629 int declares_class_or_enum
;
10630 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10631 cp_parser_decl_specifier_seq (parser
,
10632 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10633 &lambda_specs
, &declares_class_or_enum
);
10634 if (lambda_specs
.storage_class
== sc_mutable
)
10636 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10637 if (lambda_specs
.conflicting_specifiers_p
)
10638 error_at (lambda_specs
.locations
[ds_storage_class
],
10639 "duplicate %<mutable%>");
10642 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10644 /* Parse optional exception specification. */
10645 exception_spec
= cp_parser_exception_specification_opt (parser
);
10647 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10649 /* Parse optional trailing return type. */
10650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10652 cp_lexer_consume_token (parser
->lexer
);
10653 return_type
= cp_parser_trailing_type_id (parser
);
10656 /* The function parameters must be in scope all the way until after the
10657 trailing-return-type in case of decltype. */
10658 pop_bindings_and_leave_scope ();
10660 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10661 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10663 /* Create the function call operator.
10665 Messing with declarators like this is no uglier than building up the
10666 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10669 cp_decl_specifier_seq return_type_specs
;
10670 cp_declarator
* declarator
;
10675 clear_decl_specs (&return_type_specs
);
10676 return_type_specs
.type
= make_auto ();
10678 if (lambda_specs
.locations
[ds_constexpr
])
10680 if (cxx_dialect
>= cxx17
)
10681 return_type_specs
.locations
[ds_constexpr
]
10682 = lambda_specs
.locations
[ds_constexpr
];
10684 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10685 "lambda only available with -std=c++17 or -std=gnu++17");
10688 p
= obstack_alloc (&declarator_obstack
, 0);
10690 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10691 LAMBDA_EXPR_LOCATION (lambda_expr
));
10693 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10694 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10695 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10696 VIRT_SPEC_UNSPECIFIED
,
10701 /*requires_clause*/NULL_TREE
);
10702 declarator
->std_attributes
= attributes
;
10704 fco
= grokmethod (&return_type_specs
,
10707 if (fco
!= error_mark_node
)
10709 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10710 DECL_ARTIFICIAL (fco
) = 1;
10711 /* Give the object parameter a different name. */
10712 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10713 DECL_LAMBDA_FUNCTION (fco
) = 1;
10715 if (template_param_list
)
10717 fco
= finish_member_template_decl (fco
);
10718 finish_template_decl (template_param_list
);
10719 --parser
->num_template_parameter_lists
;
10721 else if (parser
->fully_implicit_function_template_p
)
10722 fco
= finish_fully_implicit_template (parser
, fco
);
10724 finish_member_declaration (fco
);
10726 obstack_free (&declarator_obstack
, p
);
10728 return (fco
!= error_mark_node
);
10732 /* Parse the body of a lambda expression, which is simply
10736 but which requires special handling.
10737 LAMBDA_EXPR is the current representation of the lambda expression. */
10740 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10742 bool nested
= (current_function_decl
!= NULL_TREE
);
10743 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10744 bool in_function_body
= parser
->in_function_body
;
10746 /* The body of a lambda-expression is not a subexpression of the enclosing
10751 push_function_context ();
10753 /* Still increment function_depth so that we don't GC in the
10754 middle of an expression. */
10757 vec
<tree
> omp_privatization_save
;
10758 save_omp_privatization_clauses (omp_privatization_save
);
10759 /* Clear this in case we're in the middle of a default argument. */
10760 parser
->local_variables_forbidden_p
= false;
10761 parser
->in_function_body
= true;
10764 local_specialization_stack
s (lss_copy
);
10765 tree fco
= lambda_function (lambda_expr
);
10766 tree body
= start_lambda_function (fco
, lambda_expr
);
10767 matching_braces braces
;
10769 if (braces
.require_open (parser
))
10771 tree compound_stmt
= begin_compound_stmt (0);
10773 /* Originally C++11 required us to peek for 'return expr'; and
10774 process it specially here to deduce the return type. N3638
10775 removed the need for that. */
10777 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10778 cp_parser_label_declaration (parser
);
10779 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10780 braces
.require_close (parser
);
10782 finish_compound_stmt (compound_stmt
);
10785 finish_lambda_function (body
);
10788 restore_omp_privatization_clauses (omp_privatization_save
);
10789 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10790 parser
->in_function_body
= in_function_body
;
10792 pop_function_context();
10797 /* Statements [gram.stmt.stmt] */
10799 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10802 add_debug_begin_stmt (location_t loc
)
10804 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10806 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10807 /* A concept is never expanded normally. */
10810 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10811 SET_EXPR_LOCATION (stmt
, loc
);
10815 /* Parse a statement.
10819 expression-statement
10821 selection-statement
10822 iteration-statement
10824 declaration-statement
10831 attribute-specifier-seq (opt) expression-statement
10832 attribute-specifier-seq (opt) compound-statement
10833 attribute-specifier-seq (opt) selection-statement
10834 attribute-specifier-seq (opt) iteration-statement
10835 attribute-specifier-seq (opt) jump-statement
10836 declaration-statement
10837 attribute-specifier-seq (opt) try-block
10840 expression-statement
10848 IN_COMPOUND is true when the statement is nested inside a
10849 cp_parser_compound_statement; this matters for certain pragmas.
10851 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10852 is a (possibly labeled) if statement which is not enclosed in braces
10853 and has an else clause. This is used to implement -Wparentheses.
10855 CHAIN is a vector of if-else-if conditions. */
10858 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10859 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10860 location_t
*loc_after_labels
)
10862 tree statement
, std_attrs
= NULL_TREE
;
10864 location_t statement_location
, attrs_location
;
10869 /* There is no statement yet. */
10870 statement
= NULL_TREE
;
10872 saved_token_sentinel
saved_tokens (parser
->lexer
);
10873 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10874 if (c_dialect_objc ())
10875 /* In obj-c++, seeing '[[' might be the either the beginning of
10876 c++11 attributes, or a nested objc-message-expression. So
10877 let's parse the c++11 attributes tentatively. */
10878 cp_parser_parse_tentatively (parser
);
10879 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10880 if (c_dialect_objc ())
10882 if (!cp_parser_parse_definitely (parser
))
10883 std_attrs
= NULL_TREE
;
10886 /* Peek at the next token. */
10887 token
= cp_lexer_peek_token (parser
->lexer
);
10888 /* Remember the location of the first token in the statement. */
10889 statement_location
= token
->location
;
10890 add_debug_begin_stmt (statement_location
);
10891 /* If this is a keyword, then that will often determine what kind of
10892 statement we have. */
10893 if (token
->type
== CPP_KEYWORD
)
10895 enum rid keyword
= token
->keyword
;
10901 /* Looks like a labeled-statement with a case label.
10902 Parse the label, and then use tail recursion to parse
10904 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10905 in_compound
= false;
10910 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10916 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10923 statement
= cp_parser_jump_statement (parser
);
10926 /* Objective-C++ exception-handling constructs. */
10929 case RID_AT_FINALLY
:
10930 case RID_AT_SYNCHRONIZED
:
10932 statement
= cp_parser_objc_statement (parser
);
10936 statement
= cp_parser_try_block (parser
);
10939 case RID_NAMESPACE
:
10940 /* This must be a namespace alias definition. */
10941 cp_parser_declaration_statement (parser
);
10944 case RID_TRANSACTION_ATOMIC
:
10945 case RID_TRANSACTION_RELAXED
:
10946 case RID_SYNCHRONIZED
:
10947 case RID_ATOMIC_NOEXCEPT
:
10948 case RID_ATOMIC_CANCEL
:
10949 statement
= cp_parser_transaction (parser
, token
);
10951 case RID_TRANSACTION_CANCEL
:
10952 statement
= cp_parser_transaction_cancel (parser
);
10956 /* It might be a keyword like `int' that can start a
10957 declaration-statement. */
10961 else if (token
->type
== CPP_NAME
)
10963 /* If the next token is a `:', then we are looking at a
10964 labeled-statement. */
10965 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10966 if (token
->type
== CPP_COLON
)
10968 /* Looks like a labeled-statement with an ordinary label.
10969 Parse the label, and then use tail recursion to parse
10972 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10973 in_compound
= false;
10977 /* Anything that starts with a `{' must be a compound-statement. */
10978 else if (token
->type
== CPP_OPEN_BRACE
)
10979 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10980 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10981 a statement all its own. */
10982 else if (token
->type
== CPP_PRAGMA
)
10984 /* Only certain OpenMP pragmas are attached to statements, and thus
10985 are considered statements themselves. All others are not. In
10986 the context of a compound, accept the pragma as a "statement" and
10987 return so that we can check for a close brace. Otherwise we
10988 require a real statement and must go back and read one. */
10990 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10991 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10995 else if (token
->type
== CPP_EOF
)
10997 cp_parser_error (parser
, "expected statement");
11001 /* Everything else must be a declaration-statement or an
11002 expression-statement. Try for the declaration-statement
11003 first, unless we are looking at a `;', in which case we know that
11004 we have an expression-statement. */
11007 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11009 if (std_attrs
!= NULL_TREE
)
11011 /* Attributes should be parsed as part of the the
11012 declaration, so let's un-parse them. */
11013 saved_tokens
.rollback();
11014 std_attrs
= NULL_TREE
;
11017 cp_parser_parse_tentatively (parser
);
11018 /* Try to parse the declaration-statement. */
11019 cp_parser_declaration_statement (parser
);
11020 /* If that worked, we're done. */
11021 if (cp_parser_parse_definitely (parser
))
11024 /* All preceding labels have been parsed at this point. */
11025 if (loc_after_labels
!= NULL
)
11026 *loc_after_labels
= statement_location
;
11028 /* Look for an expression-statement instead. */
11029 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11031 /* Handle [[fallthrough]];. */
11032 if (attribute_fallthrough_p (std_attrs
))
11034 /* The next token after the fallthrough attribute is ';'. */
11035 if (statement
== NULL_TREE
)
11037 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11038 statement
= build_call_expr_internal_loc (statement_location
,
11040 void_type_node
, 0);
11041 finish_expr_stmt (statement
);
11044 warning_at (statement_location
, OPT_Wattributes
,
11045 "%<fallthrough%> attribute not followed by %<;%>");
11046 std_attrs
= NULL_TREE
;
11050 /* Set the line number for the statement. */
11051 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11052 SET_EXPR_LOCATION (statement
, statement_location
);
11054 /* Allow "[[fallthrough]];", but warn otherwise. */
11055 if (std_attrs
!= NULL_TREE
)
11056 warning_at (attrs_location
,
11058 "attributes at the beginning of statement are ignored");
11061 /* Append ATTR to attribute list ATTRS. */
11064 attr_chainon (tree attrs
, tree attr
)
11066 if (attrs
== error_mark_node
)
11067 return error_mark_node
;
11068 if (attr
== error_mark_node
)
11069 return error_mark_node
;
11070 return chainon (attrs
, attr
);
11073 /* Parse the label for a labeled-statement, i.e.
11076 case constant-expression :
11080 case constant-expression ... constant-expression : statement
11082 When a label is parsed without errors, the label is added to the
11083 parse tree by the finish_* functions, so this function doesn't
11084 have to return the label. */
11087 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11090 tree label
= NULL_TREE
;
11091 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11093 /* The next token should be an identifier. */
11094 token
= cp_lexer_peek_token (parser
->lexer
);
11095 if (token
->type
!= CPP_NAME
11096 && token
->type
!= CPP_KEYWORD
)
11098 cp_parser_error (parser
, "expected labeled-statement");
11102 /* Remember whether this case or a user-defined label is allowed to fall
11104 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11106 parser
->colon_corrects_to_scope_p
= false;
11107 switch (token
->keyword
)
11111 tree expr
, expr_hi
;
11112 cp_token
*ellipsis
;
11114 /* Consume the `case' token. */
11115 cp_lexer_consume_token (parser
->lexer
);
11116 /* Parse the constant-expression. */
11117 expr
= cp_parser_constant_expression (parser
);
11118 if (check_for_bare_parameter_packs (expr
))
11119 expr
= error_mark_node
;
11121 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11122 if (ellipsis
->type
== CPP_ELLIPSIS
)
11124 /* Consume the `...' token. */
11125 cp_lexer_consume_token (parser
->lexer
);
11126 expr_hi
= cp_parser_constant_expression (parser
);
11127 if (check_for_bare_parameter_packs (expr_hi
))
11128 expr_hi
= error_mark_node
;
11130 /* We don't need to emit warnings here, as the common code
11131 will do this for us. */
11134 expr_hi
= NULL_TREE
;
11136 if (parser
->in_switch_statement_p
)
11138 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11139 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11140 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11143 error_at (token
->location
,
11144 "case label %qE not within a switch statement",
11150 /* Consume the `default' token. */
11151 cp_lexer_consume_token (parser
->lexer
);
11153 if (parser
->in_switch_statement_p
)
11155 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11156 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11157 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11160 error_at (token
->location
, "case label not within a switch statement");
11164 /* Anything else must be an ordinary label. */
11165 label
= finish_label_stmt (cp_parser_identifier (parser
));
11166 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11167 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11171 /* Require the `:' token. */
11172 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11174 /* An ordinary label may optionally be followed by attributes.
11175 However, this is only permitted if the attributes are then
11176 followed by a semicolon. This is because, for backward
11177 compatibility, when parsing
11178 lab: __attribute__ ((unused)) int i;
11179 we want the attribute to attach to "i", not "lab". */
11180 if (label
!= NULL_TREE
11181 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11184 cp_parser_parse_tentatively (parser
);
11185 attrs
= cp_parser_gnu_attributes_opt (parser
);
11186 if (attrs
== NULL_TREE
11187 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11188 cp_parser_abort_tentative_parse (parser
);
11189 else if (!cp_parser_parse_definitely (parser
))
11192 attributes
= attr_chainon (attributes
, attrs
);
11195 if (attributes
!= NULL_TREE
)
11196 cplus_decl_attributes (&label
, attributes
, 0);
11198 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11201 /* Parse an expression-statement.
11203 expression-statement:
11206 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11207 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11208 indicates whether this expression-statement is part of an
11209 expression statement. */
11212 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11214 tree statement
= NULL_TREE
;
11215 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11216 location_t loc
= token
->location
;
11218 /* There might be attribute fallthrough. */
11219 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11221 /* If the next token is a ';', then there is no expression
11223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11225 statement
= cp_parser_expression (parser
);
11226 if (statement
== error_mark_node
11227 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11229 cp_parser_skip_to_end_of_block_or_statement (parser
);
11230 return error_mark_node
;
11234 /* Handle [[fallthrough]];. */
11235 if (attribute_fallthrough_p (attr
))
11237 /* The next token after the fallthrough attribute is ';'. */
11238 if (statement
== NULL_TREE
)
11239 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11240 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11241 void_type_node
, 0);
11243 warning_at (loc
, OPT_Wattributes
,
11244 "%<fallthrough%> attribute not followed by %<;%>");
11248 /* Allow "[[fallthrough]];", but warn otherwise. */
11249 if (attr
!= NULL_TREE
)
11250 warning_at (loc
, OPT_Wattributes
,
11251 "attributes at the beginning of statement are ignored");
11253 /* Give a helpful message for "A<T>::type t;" and the like. */
11254 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11255 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11257 if (TREE_CODE (statement
) == SCOPE_REF
)
11258 error_at (token
->location
, "need %<typename%> before %qE because "
11259 "%qT is a dependent scope",
11260 statement
, TREE_OPERAND (statement
, 0));
11261 else if (is_overloaded_fn (statement
)
11262 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11265 tree fn
= get_first_fn (statement
);
11266 error_at (token
->location
,
11267 "%<%T::%D%> names the constructor, not the type",
11268 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11272 /* Consume the final `;'. */
11273 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11275 if (in_statement_expr
11276 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11277 /* This is the final expression statement of a statement
11279 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11280 else if (statement
)
11281 statement
= finish_expr_stmt (statement
);
11286 /* Parse a compound-statement.
11288 compound-statement:
11289 { statement-seq [opt] }
11293 compound-statement:
11294 { label-declaration-seq [opt] statement-seq [opt] }
11296 label-declaration-seq:
11298 label-declaration-seq label-declaration
11300 Returns a tree representing the statement. */
11303 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11304 int bcs_flags
, bool function_body
)
11306 tree compound_stmt
;
11307 matching_braces braces
;
11309 /* Consume the `{'. */
11310 if (!braces
.require_open (parser
))
11311 return error_mark_node
;
11312 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11313 && !function_body
&& cxx_dialect
< cxx14
)
11314 pedwarn (input_location
, OPT_Wpedantic
,
11315 "compound-statement in %<constexpr%> function");
11316 /* Begin the compound-statement. */
11317 compound_stmt
= begin_compound_stmt (bcs_flags
);
11318 /* If the next keyword is `__label__' we have a label declaration. */
11319 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11320 cp_parser_label_declaration (parser
);
11321 /* Parse an (optional) statement-seq. */
11322 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11323 /* Finish the compound-statement. */
11324 finish_compound_stmt (compound_stmt
);
11325 /* Consume the `}'. */
11326 braces
.require_close (parser
);
11328 return compound_stmt
;
11331 /* Parse an (optional) statement-seq.
11335 statement-seq [opt] statement */
11338 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11340 /* Scan statements until there aren't any more. */
11343 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11345 /* If we are looking at a `}', then we have run out of
11346 statements; the same is true if we have reached the end
11347 of file, or have stumbled upon a stray '@end'. */
11348 if (token
->type
== CPP_CLOSE_BRACE
11349 || token
->type
== CPP_EOF
11350 || token
->type
== CPP_PRAGMA_EOL
11351 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11354 /* If we are in a compound statement and find 'else' then
11355 something went wrong. */
11356 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11358 if (parser
->in_statement
& IN_IF_STMT
)
11362 token
= cp_lexer_consume_token (parser
->lexer
);
11363 error_at (token
->location
, "%<else%> without a previous %<if%>");
11367 /* Parse the statement. */
11368 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11372 /* Return true if this is the C++20 version of range-based-for with
11376 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11380 /* Save tokens so that we can put them back. */
11381 cp_lexer_save_tokens (parser
->lexer
);
11383 /* There has to be an unnested ; followed by an unnested :. */
11384 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11385 /*recovering=*/false,
11387 /*consume_paren=*/false) != -1)
11390 /* We found the semicolon, eat it now. */
11391 cp_lexer_consume_token (parser
->lexer
);
11393 /* Now look for ':' that is not nested in () or {}. */
11394 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11395 /*recovering=*/false,
11397 /*consume_paren=*/false) == -1);
11400 /* Roll back the tokens we skipped. */
11401 cp_lexer_rollback_tokens (parser
->lexer
);
11406 /* Return true if we're looking at (init; cond), false otherwise. */
11409 cp_parser_init_statement_p (cp_parser
*parser
)
11411 /* Save tokens so that we can put them back. */
11412 cp_lexer_save_tokens (parser
->lexer
);
11414 /* Look for ';' that is not nested in () or {}. */
11415 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11416 /*recovering=*/false,
11418 /*consume_paren=*/false);
11420 /* Roll back the tokens we skipped. */
11421 cp_lexer_rollback_tokens (parser
->lexer
);
11426 /* Parse a selection-statement.
11428 selection-statement:
11429 if ( init-statement [opt] condition ) statement
11430 if ( init-statement [opt] condition ) statement else statement
11431 switch ( init-statement [opt] condition ) statement
11433 Returns the new IF_STMT or SWITCH_STMT.
11435 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11436 is a (possibly labeled) if statement which is not enclosed in
11437 braces and has an else clause. This is used to implement
11440 CHAIN is a vector of if-else-if conditions. This is used to implement
11441 -Wduplicated-cond. */
11444 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11449 token_indent_info guard_tinfo
;
11454 /* Peek at the next token. */
11455 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11456 guard_tinfo
= get_token_indent_info (token
);
11458 /* See what kind of keyword it is. */
11459 keyword
= token
->keyword
;
11469 if (keyword
== RID_IF
11470 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11474 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11475 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11476 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11477 "with -std=c++17 or -std=gnu++17");
11480 /* Look for the `('. */
11481 matching_parens parens
;
11482 if (!parens
.require_open (parser
))
11484 cp_parser_skip_to_end_of_statement (parser
);
11485 return error_mark_node
;
11488 /* Begin the selection-statement. */
11489 if (keyword
== RID_IF
)
11491 statement
= begin_if_stmt ();
11492 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11495 statement
= begin_switch_stmt ();
11497 /* Parse the optional init-statement. */
11498 if (cp_parser_init_statement_p (parser
))
11501 if (cxx_dialect
< cxx17
)
11502 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11503 "init-statement in selection statements only available "
11504 "with -std=c++17 or -std=gnu++17");
11505 cp_parser_init_statement (parser
, &decl
);
11508 /* Parse the condition. */
11509 condition
= cp_parser_condition (parser
);
11510 /* Look for the `)'. */
11511 if (!parens
.require_close (parser
))
11512 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11513 /*consume_paren=*/true);
11515 if (keyword
== RID_IF
)
11518 unsigned char in_statement
;
11520 /* Add the condition. */
11521 condition
= finish_if_stmt_cond (condition
, statement
);
11523 if (warn_duplicated_cond
)
11524 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11527 /* Parse the then-clause. */
11528 in_statement
= parser
->in_statement
;
11529 parser
->in_statement
|= IN_IF_STMT
;
11531 /* Outside a template, the non-selected branch of a constexpr
11532 if is a 'discarded statement', i.e. unevaluated. */
11533 bool was_discarded
= in_discarded_stmt
;
11534 bool discard_then
= (cx
&& !processing_template_decl
11535 && integer_zerop (condition
));
11538 in_discarded_stmt
= true;
11539 ++c_inhibit_evaluation_warnings
;
11542 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11545 parser
->in_statement
= in_statement
;
11547 finish_then_clause (statement
);
11551 THEN_CLAUSE (statement
) = NULL_TREE
;
11552 in_discarded_stmt
= was_discarded
;
11553 --c_inhibit_evaluation_warnings
;
11556 /* If the next token is `else', parse the else-clause. */
11557 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11560 bool discard_else
= (cx
&& !processing_template_decl
11561 && integer_nonzerop (condition
));
11564 in_discarded_stmt
= true;
11565 ++c_inhibit_evaluation_warnings
;
11569 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11570 /* Consume the `else' keyword. */
11571 cp_lexer_consume_token (parser
->lexer
);
11572 if (warn_duplicated_cond
)
11574 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11578 /* We've got "if (COND) else if (COND2)". Start
11579 the condition chain and add COND as the first
11581 chain
= new vec
<tree
> ();
11582 if (!CONSTANT_CLASS_P (condition
)
11583 && !TREE_SIDE_EFFECTS (condition
))
11585 /* Wrap it in a NOP_EXPR so that we can set the
11586 location of the condition. */
11587 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11589 SET_EXPR_LOCATION (e
, token
->location
);
11590 chain
->safe_push (e
);
11593 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11596 /* This is if-else without subsequent if. Zap the
11597 condition chain; we would have already warned at
11603 begin_else_clause (statement
);
11604 /* Parse the else-clause. */
11605 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11606 guard_tinfo
, chain
);
11608 finish_else_clause (statement
);
11610 /* If we are currently parsing a then-clause, then
11611 IF_P will not be NULL. We set it to true to
11612 indicate that this if statement has an else clause.
11613 This may trigger the Wparentheses warning below
11614 when we get back up to the parent if statement. */
11620 ELSE_CLAUSE (statement
) = NULL_TREE
;
11621 in_discarded_stmt
= was_discarded
;
11622 --c_inhibit_evaluation_warnings
;
11627 /* This if statement does not have an else clause. If
11628 NESTED_IF is true, then the then-clause has an if
11629 statement which does have an else clause. We warn
11630 about the potential ambiguity. */
11632 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11633 "suggest explicit braces to avoid ambiguous"
11635 if (warn_duplicated_cond
)
11637 /* We don't need the condition chain anymore. */
11643 /* Now we're all done with the if-statement. */
11644 finish_if_stmt (statement
);
11648 bool in_switch_statement_p
;
11649 unsigned char in_statement
;
11651 /* Add the condition. */
11652 finish_switch_cond (condition
, statement
);
11654 /* Parse the body of the switch-statement. */
11655 in_switch_statement_p
= parser
->in_switch_statement_p
;
11656 in_statement
= parser
->in_statement
;
11657 parser
->in_switch_statement_p
= true;
11658 parser
->in_statement
|= IN_SWITCH_STMT
;
11659 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11661 parser
->in_switch_statement_p
= in_switch_statement_p
;
11662 parser
->in_statement
= in_statement
;
11664 /* Now we're all done with the switch-statement. */
11665 finish_switch_stmt (statement
);
11673 cp_parser_error (parser
, "expected selection-statement");
11674 return error_mark_node
;
11678 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11679 If we have seen at least one decl-specifier, and the next token
11680 is not a parenthesis, then we must be looking at a declaration.
11681 (After "int (" we might be looking at a functional cast.) */
11684 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11685 bool any_specifiers_p
)
11687 if (any_specifiers_p
11688 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11689 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11690 && !cp_parser_error_occurred (parser
))
11691 cp_parser_commit_to_tentative_parse (parser
);
11694 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11695 The declarator shall not specify a function or an array. Returns
11696 TRUE if the declarator is valid, FALSE otherwise. */
11699 cp_parser_check_condition_declarator (cp_parser
* parser
,
11700 cp_declarator
*declarator
,
11703 if (declarator
== cp_error_declarator
11704 || function_declarator_p (declarator
)
11705 || declarator
->kind
== cdk_array
)
11707 if (declarator
== cp_error_declarator
)
11708 /* Already complained. */;
11709 else if (declarator
->kind
== cdk_array
)
11710 error_at (loc
, "condition declares an array");
11712 error_at (loc
, "condition declares a function");
11713 if (parser
->fully_implicit_function_template_p
)
11714 abort_fully_implicit_template (parser
);
11715 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11716 /*or_comma=*/false,
11717 /*consume_paren=*/false);
11724 /* Parse a condition.
11728 type-specifier-seq declarator = initializer-clause
11729 type-specifier-seq declarator braced-init-list
11734 type-specifier-seq declarator asm-specification [opt]
11735 attributes [opt] = assignment-expression
11737 Returns the expression that should be tested. */
11740 cp_parser_condition (cp_parser
* parser
)
11742 cp_decl_specifier_seq type_specifiers
;
11743 const char *saved_message
;
11744 int declares_class_or_enum
;
11746 /* Try the declaration first. */
11747 cp_parser_parse_tentatively (parser
);
11748 /* New types are not allowed in the type-specifier-seq for a
11750 saved_message
= parser
->type_definition_forbidden_message
;
11751 parser
->type_definition_forbidden_message
11752 = G_("types may not be defined in conditions");
11753 /* Parse the type-specifier-seq. */
11754 cp_parser_decl_specifier_seq (parser
,
11755 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11757 &declares_class_or_enum
);
11758 /* Restore the saved message. */
11759 parser
->type_definition_forbidden_message
= saved_message
;
11761 cp_parser_maybe_commit_to_declaration (parser
,
11762 type_specifiers
.any_specifiers_p
);
11764 /* If all is well, we might be looking at a declaration. */
11765 if (!cp_parser_error_occurred (parser
))
11768 tree asm_specification
;
11770 cp_declarator
*declarator
;
11771 tree initializer
= NULL_TREE
;
11772 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11774 /* Parse the declarator. */
11775 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11776 /*ctor_dtor_or_conv_p=*/NULL
,
11777 /*parenthesized_p=*/NULL
,
11778 /*member_p=*/false,
11779 /*friend_p=*/false);
11780 /* Parse the attributes. */
11781 attributes
= cp_parser_attributes_opt (parser
);
11782 /* Parse the asm-specification. */
11783 asm_specification
= cp_parser_asm_specification_opt (parser
);
11784 /* If the next token is not an `=' or '{', then we might still be
11785 looking at an expression. For example:
11789 looks like a decl-specifier-seq and a declarator -- but then
11790 there is no `=', so this is an expression. */
11791 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11792 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11793 cp_parser_simulate_error (parser
);
11795 /* If we did see an `=' or '{', then we are looking at a declaration
11797 if (cp_parser_parse_definitely (parser
))
11800 bool non_constant_p
= false;
11801 int flags
= LOOKUP_ONLYCONVERTING
;
11803 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11804 return error_mark_node
;
11806 /* Create the declaration. */
11807 decl
= start_decl (declarator
, &type_specifiers
,
11808 /*initialized_p=*/true,
11809 attributes
, /*prefix_attributes=*/NULL_TREE
,
11812 /* Parse the initializer. */
11813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11815 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11816 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11819 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11821 /* Consume the `='. */
11822 cp_lexer_consume_token (parser
->lexer
);
11823 initializer
= cp_parser_initializer_clause (parser
,
11828 cp_parser_error (parser
, "expected initializer");
11829 initializer
= error_mark_node
;
11831 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11832 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11834 /* Process the initializer. */
11835 cp_finish_decl (decl
,
11836 initializer
, !non_constant_p
,
11841 pop_scope (pushed_scope
);
11843 return convert_from_reference (decl
);
11846 /* If we didn't even get past the declarator successfully, we are
11847 definitely not looking at a declaration. */
11849 cp_parser_abort_tentative_parse (parser
);
11851 /* Otherwise, we are looking at an expression. */
11852 return cp_parser_expression (parser
);
11855 /* Parses a for-statement or range-for-statement until the closing ')',
11859 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11861 tree init
, scope
, decl
;
11864 /* Begin the for-statement. */
11865 scope
= begin_for_scope (&init
);
11867 /* Parse the initialization. */
11868 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11871 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
11874 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11878 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11879 unsigned short unroll
)
11881 /* Normal for loop */
11882 tree condition
= NULL_TREE
;
11883 tree expression
= NULL_TREE
;
11886 stmt
= begin_for_stmt (scope
, init
);
11887 /* The init-statement has already been parsed in
11888 cp_parser_init_statement, so no work is needed here. */
11889 finish_init_stmt (stmt
);
11891 /* If there's a condition, process it. */
11892 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11893 condition
= cp_parser_condition (parser
);
11896 cp_parser_error (parser
, "missing loop condition in loop with "
11897 "%<GCC ivdep%> pragma");
11898 condition
= error_mark_node
;
11902 cp_parser_error (parser
, "missing loop condition in loop with "
11903 "%<GCC unroll%> pragma");
11904 condition
= error_mark_node
;
11906 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11907 /* Look for the `;'. */
11908 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11910 /* If there's an expression, process it. */
11911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11912 expression
= cp_parser_expression (parser
);
11913 finish_for_expr (expression
, stmt
);
11918 /* Tries to parse a range-based for-statement:
11921 decl-specifier-seq declarator : expression
11923 The decl-specifier-seq declarator and the `:' are already parsed by
11924 cp_parser_init_statement. If processing_template_decl it returns a
11925 newly created RANGE_FOR_STMT; if not, it is converted to a
11926 regular FOR_STMT. */
11929 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11930 bool ivdep
, unsigned short unroll
, bool is_omp
)
11932 tree stmt
, range_expr
;
11933 auto_vec
<cxx_binding
*, 16> bindings
;
11934 auto_vec
<tree
, 16> names
;
11935 tree decomp_first_name
= NULL_TREE
;
11936 unsigned int decomp_cnt
= 0;
11938 /* Get the range declaration momentarily out of the way so that
11939 the range expression doesn't clash with it. */
11940 if (range_decl
!= error_mark_node
)
11942 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11944 tree v
= DECL_VALUE_EXPR (range_decl
);
11945 /* For decomposition declaration get all of the corresponding
11946 declarations out of the way. */
11947 if (TREE_CODE (v
) == ARRAY_REF
11948 && VAR_P (TREE_OPERAND (v
, 0))
11949 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11951 tree d
= range_decl
;
11952 range_decl
= TREE_OPERAND (v
, 0);
11953 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11954 decomp_first_name
= d
;
11955 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11957 tree name
= DECL_NAME (d
);
11958 names
.safe_push (name
);
11959 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11960 IDENTIFIER_BINDING (name
)
11961 = IDENTIFIER_BINDING (name
)->previous
;
11965 if (names
.is_empty ())
11967 tree name
= DECL_NAME (range_decl
);
11968 names
.safe_push (name
);
11969 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11970 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11976 bool expr_non_constant_p
;
11977 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11980 range_expr
= cp_parser_expression (parser
);
11982 /* Put the range declaration(s) back into scope. */
11983 for (unsigned int i
= 0; i
< names
.length (); i
++)
11985 cxx_binding
*binding
= bindings
[i
];
11986 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11987 IDENTIFIER_BINDING (names
[i
]) = binding
;
11990 /* finish_omp_for has its own code for the following, so just
11991 return the range_expr instead. */
11995 /* If in template, STMT is converted to a normal for-statement
11996 at instantiation. If not, it is done just ahead. */
11997 if (processing_template_decl
)
11999 if (check_for_bare_parameter_packs (range_expr
))
12000 range_expr
= error_mark_node
;
12001 stmt
= begin_range_for_stmt (scope
, init
);
12003 RANGE_FOR_IVDEP (stmt
) = 1;
12005 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12006 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12007 if (!type_dependent_expression_p (range_expr
)
12008 /* do_auto_deduction doesn't mess with template init-lists. */
12009 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12010 do_range_for_auto_deduction (range_decl
, range_expr
);
12014 stmt
= begin_for_stmt (scope
, init
);
12015 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12016 decomp_first_name
, decomp_cnt
, ivdep
,
12022 /* Subroutine of cp_convert_range_for: given the initializer expression,
12023 builds up the range temporary. */
12026 build_range_temp (tree range_expr
)
12028 tree range_type
, range_temp
;
12030 /* Find out the type deduced by the declaration
12031 `auto &&__range = range_expr'. */
12032 range_type
= cp_build_reference_type (make_auto (), true);
12033 range_type
= do_auto_deduction (range_type
, range_expr
,
12034 type_uses_auto (range_type
));
12036 /* Create the __range variable. */
12037 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12039 TREE_USED (range_temp
) = 1;
12040 DECL_ARTIFICIAL (range_temp
) = 1;
12045 /* Used by cp_parser_range_for in template context: we aren't going to
12046 do a full conversion yet, but we still need to resolve auto in the
12047 type of the for-range-declaration if present. This is basically
12048 a shortcut version of cp_convert_range_for. */
12051 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12053 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12056 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12057 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12058 iter_type
= (cp_parser_perform_range_for_lookup
12059 (range_temp
, &begin_dummy
, &end_dummy
));
12062 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12064 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12066 tf_warning_or_error
);
12067 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12068 iter_decl
, auto_node
);
12073 /* Converts a range-based for-statement into a normal
12074 for-statement, as per the definition.
12076 for (RANGE_DECL : RANGE_EXPR)
12079 should be equivalent to:
12082 auto &&__range = RANGE_EXPR;
12083 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12087 RANGE_DECL = *__begin;
12092 If RANGE_EXPR is an array:
12093 BEGIN_EXPR = __range
12094 END_EXPR = __range + ARRAY_SIZE(__range)
12095 Else if RANGE_EXPR has a member 'begin' or 'end':
12096 BEGIN_EXPR = __range.begin()
12097 END_EXPR = __range.end()
12099 BEGIN_EXPR = begin(__range)
12100 END_EXPR = end(__range);
12102 If __range has a member 'begin' but not 'end', or vice versa, we must
12103 still use the second alternative (it will surely fail, however).
12104 When calling begin()/end() in the third alternative we must use
12105 argument dependent lookup, but always considering 'std' as an associated
12109 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12110 tree decomp_first_name
, unsigned int decomp_cnt
,
12111 bool ivdep
, unsigned short unroll
)
12114 tree iter_type
, begin_expr
, end_expr
;
12115 tree condition
, expression
;
12117 range_expr
= mark_lvalue_use (range_expr
);
12119 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12120 /* If an error happened previously do nothing or else a lot of
12121 unhelpful errors would be issued. */
12122 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12127 if (VAR_P (range_expr
)
12128 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12129 /* Can't bind a reference to an array of runtime bound. */
12130 range_temp
= range_expr
;
12133 range_temp
= build_range_temp (range_expr
);
12134 pushdecl (range_temp
);
12135 cp_finish_decl (range_temp
, range_expr
,
12136 /*is_constant_init*/false, NULL_TREE
,
12137 LOOKUP_ONLYCONVERTING
);
12138 range_temp
= convert_from_reference (range_temp
);
12140 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12141 &begin_expr
, &end_expr
);
12144 /* The new for initialization statement. */
12145 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12147 TREE_USED (begin
) = 1;
12148 DECL_ARTIFICIAL (begin
) = 1;
12150 cp_finish_decl (begin
, begin_expr
,
12151 /*is_constant_init*/false, NULL_TREE
,
12152 LOOKUP_ONLYCONVERTING
);
12154 if (cxx_dialect
>= cxx17
)
12155 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12156 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12157 TREE_USED (end
) = 1;
12158 DECL_ARTIFICIAL (end
) = 1;
12160 cp_finish_decl (end
, end_expr
,
12161 /*is_constant_init*/false, NULL_TREE
,
12162 LOOKUP_ONLYCONVERTING
);
12164 finish_init_stmt (statement
);
12166 /* The new for condition. */
12167 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12170 NULL
, tf_warning_or_error
);
12171 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12173 /* The new increment expression. */
12174 expression
= finish_unary_op_expr (input_location
,
12175 PREINCREMENT_EXPR
, begin
,
12176 tf_warning_or_error
);
12177 finish_for_expr (expression
, statement
);
12179 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12180 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12182 /* The declaration is initialized with *__begin inside the loop body. */
12183 cp_finish_decl (range_decl
,
12184 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12185 tf_warning_or_error
),
12186 /*is_constant_init*/false, NULL_TREE
,
12187 LOOKUP_ONLYCONVERTING
);
12188 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12189 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12194 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12195 We need to solve both at the same time because the method used
12196 depends on the existence of members begin or end.
12197 Returns the type deduced for the iterator expression. */
12200 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12202 if (error_operand_p (range
))
12204 *begin
= *end
= error_mark_node
;
12205 return error_mark_node
;
12208 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12210 error ("range-based %<for%> expression of type %qT "
12211 "has incomplete type", TREE_TYPE (range
));
12212 *begin
= *end
= error_mark_node
;
12213 return error_mark_node
;
12215 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12217 /* If RANGE is an array, we will use pointer arithmetic. */
12218 *begin
= decay_conversion (range
, tf_warning_or_error
);
12219 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12221 array_type_nelts_top (TREE_TYPE (range
)),
12223 return TREE_TYPE (*begin
);
12227 /* If it is not an array, we must do a bit of magic. */
12228 tree id_begin
, id_end
;
12229 tree member_begin
, member_end
;
12231 *begin
= *end
= error_mark_node
;
12233 id_begin
= get_identifier ("begin");
12234 id_end
= get_identifier ("end");
12235 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12236 /*protect=*/2, /*want_type=*/false,
12237 tf_warning_or_error
);
12238 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12239 /*protect=*/2, /*want_type=*/false,
12240 tf_warning_or_error
);
12242 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12244 /* Use the member functions. */
12245 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12246 *end
= cp_parser_range_for_member_function (range
, id_end
);
12250 /* Use global functions with ADL. */
12251 vec
<tree
, va_gc
> *vec
;
12252 vec
= make_tree_vector ();
12254 vec_safe_push (vec
, range
);
12256 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12257 tf_warning_or_error
);
12258 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12259 tf_warning_or_error
);
12260 member_end
= perform_koenig_lookup (id_end
, vec
,
12261 tf_warning_or_error
);
12262 *end
= finish_call_expr (member_end
, &vec
, false, true,
12263 tf_warning_or_error
);
12265 release_tree_vector (vec
);
12268 /* Last common checks. */
12269 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12271 /* If one of the expressions is an error do no more checks. */
12272 *begin
= *end
= error_mark_node
;
12273 return error_mark_node
;
12275 else if (type_dependent_expression_p (*begin
)
12276 || type_dependent_expression_p (*end
))
12277 /* Can happen, when, eg, in a template context, Koenig lookup
12278 can't resolve begin/end (c++/58503). */
12282 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12283 /* The unqualified type of the __begin and __end temporaries should
12284 be the same, as required by the multiple auto declaration. */
12285 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12287 if (cxx_dialect
>= cxx17
12288 && (build_x_binary_op (input_location
, NE_EXPR
,
12289 *begin
, ERROR_MARK
,
12292 != error_mark_node
))
12293 /* P0184R0 allows __begin and __end to have different types,
12294 but make sure they are comparable so we can give a better
12297 error ("inconsistent begin/end types in range-based %<for%> "
12298 "statement: %qT and %qT",
12299 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12306 /* Helper function for cp_parser_perform_range_for_lookup.
12307 Builds a tree for RANGE.IDENTIFIER(). */
12310 cp_parser_range_for_member_function (tree range
, tree identifier
)
12313 vec
<tree
, va_gc
> *vec
;
12315 member
= finish_class_member_access_expr (range
, identifier
,
12316 false, tf_warning_or_error
);
12317 if (member
== error_mark_node
)
12318 return error_mark_node
;
12320 vec
= make_tree_vector ();
12321 res
= finish_call_expr (member
, &vec
,
12322 /*disallow_virtual=*/false,
12323 /*koenig_p=*/false,
12324 tf_warning_or_error
);
12325 release_tree_vector (vec
);
12329 /* Parse an iteration-statement.
12331 iteration-statement:
12332 while ( condition ) statement
12333 do statement while ( expression ) ;
12334 for ( init-statement condition [opt] ; expression [opt] )
12337 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12340 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12341 unsigned short unroll
)
12346 unsigned char in_statement
;
12347 token_indent_info guard_tinfo
;
12349 /* Peek at the next token. */
12350 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12352 return error_mark_node
;
12354 guard_tinfo
= get_token_indent_info (token
);
12356 /* Remember whether or not we are already within an iteration
12358 in_statement
= parser
->in_statement
;
12360 /* See what kind of keyword it is. */
12361 keyword
= token
->keyword
;
12368 /* Begin the while-statement. */
12369 statement
= begin_while_stmt ();
12370 /* Look for the `('. */
12371 matching_parens parens
;
12372 parens
.require_open (parser
);
12373 /* Parse the condition. */
12374 condition
= cp_parser_condition (parser
);
12375 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12376 /* Look for the `)'. */
12377 parens
.require_close (parser
);
12378 /* Parse the dependent statement. */
12379 parser
->in_statement
= IN_ITERATION_STMT
;
12380 bool prev
= note_iteration_stmt_body_start ();
12381 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12382 note_iteration_stmt_body_end (prev
);
12383 parser
->in_statement
= in_statement
;
12384 /* We're done with the while-statement. */
12385 finish_while_stmt (statement
);
12393 /* Begin the do-statement. */
12394 statement
= begin_do_stmt ();
12395 /* Parse the body of the do-statement. */
12396 parser
->in_statement
= IN_ITERATION_STMT
;
12397 bool prev
= note_iteration_stmt_body_start ();
12398 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12399 note_iteration_stmt_body_end (prev
);
12400 parser
->in_statement
= in_statement
;
12401 finish_do_body (statement
);
12402 /* Look for the `while' keyword. */
12403 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12404 /* Look for the `('. */
12405 matching_parens parens
;
12406 parens
.require_open (parser
);
12407 /* Parse the expression. */
12408 expression
= cp_parser_expression (parser
);
12409 /* We're done with the do-statement. */
12410 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12411 /* Look for the `)'. */
12412 parens
.require_close (parser
);
12413 /* Look for the `;'. */
12414 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12420 /* Look for the `('. */
12421 matching_parens parens
;
12422 parens
.require_open (parser
);
12424 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12426 /* Look for the `)'. */
12427 parens
.require_close (parser
);
12429 /* Parse the body of the for-statement. */
12430 parser
->in_statement
= IN_ITERATION_STMT
;
12431 bool prev
= note_iteration_stmt_body_start ();
12432 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12433 note_iteration_stmt_body_end (prev
);
12434 parser
->in_statement
= in_statement
;
12436 /* We're done with the for-statement. */
12437 finish_for_stmt (statement
);
12442 cp_parser_error (parser
, "expected iteration-statement");
12443 statement
= error_mark_node
;
12450 /* Parse a init-statement or the declarator of a range-based-for.
12451 Returns true if a range-based-for declaration is seen.
12454 expression-statement
12455 simple-declaration */
12458 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12460 /* If the next token is a `;', then we have an empty
12461 expression-statement. Grammatically, this is also a
12462 simple-declaration, but an invalid one, because it does not
12463 declare anything. Therefore, if we did not handle this case
12464 specially, we would issue an error message about an invalid
12466 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12468 bool is_range_for
= false;
12469 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12471 /* Try to parse the init-statement. */
12472 if (cp_parser_range_based_for_with_init_p (parser
))
12475 cp_parser_parse_tentatively (parser
);
12476 /* Parse the declaration. */
12477 cp_parser_simple_declaration (parser
,
12478 /*function_definition_allowed_p=*/false,
12480 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12481 if (!cp_parser_parse_definitely (parser
))
12482 /* That didn't work, try to parse it as an expression-statement. */
12483 cp_parser_expression_statement (parser
, NULL_TREE
);
12485 if (cxx_dialect
< cxx2a
)
12487 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12488 "range-based %<for%> loops with initializer only "
12489 "available with -std=c++2a or -std=gnu++2a");
12490 *decl
= error_mark_node
;
12494 /* A colon is used in range-based for. */
12495 parser
->colon_corrects_to_scope_p
= false;
12497 /* We're going to speculatively look for a declaration, falling back
12498 to an expression, if necessary. */
12499 cp_parser_parse_tentatively (parser
);
12500 /* Parse the declaration. */
12501 cp_parser_simple_declaration (parser
,
12502 /*function_definition_allowed_p=*/false,
12504 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12507 /* It is a range-for, consume the ':'. */
12508 cp_lexer_consume_token (parser
->lexer
);
12509 is_range_for
= true;
12510 if (cxx_dialect
< cxx11
)
12511 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12512 "range-based %<for%> loops only available with "
12513 "-std=c++11 or -std=gnu++11");
12516 /* The ';' is not consumed yet because we told
12517 cp_parser_simple_declaration not to. */
12518 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12520 if (cp_parser_parse_definitely (parser
))
12521 return is_range_for
;
12522 /* If the tentative parse failed, then we shall need to look for an
12523 expression-statement. */
12525 /* If we are here, it is an expression-statement. */
12526 cp_parser_expression_statement (parser
, NULL_TREE
);
12530 /* Parse a jump-statement.
12535 return expression [opt] ;
12536 return braced-init-list ;
12542 goto * expression ;
12544 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12547 cp_parser_jump_statement (cp_parser
* parser
)
12549 tree statement
= error_mark_node
;
12552 unsigned char in_statement
;
12554 /* Peek at the next token. */
12555 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12557 return error_mark_node
;
12559 /* See what kind of keyword it is. */
12560 keyword
= token
->keyword
;
12564 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12565 switch (in_statement
)
12568 error_at (token
->location
, "break statement not within loop or switch");
12571 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12572 || in_statement
== IN_ITERATION_STMT
);
12573 statement
= finish_break_stmt ();
12574 if (in_statement
== IN_ITERATION_STMT
)
12575 break_maybe_infinite_loop ();
12578 error_at (token
->location
, "invalid exit from OpenMP structured block");
12581 error_at (token
->location
, "break statement used with OpenMP for loop");
12584 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12588 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12591 error_at (token
->location
, "continue statement not within a loop");
12593 /* Fall through. */
12594 case IN_ITERATION_STMT
:
12596 statement
= finish_continue_stmt ();
12599 error_at (token
->location
, "invalid exit from OpenMP structured block");
12602 gcc_unreachable ();
12604 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12610 bool expr_non_constant_p
;
12612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12614 cp_lexer_set_source_position (parser
->lexer
);
12615 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12616 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12618 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12619 expr
= cp_parser_expression (parser
);
12621 /* If the next token is a `;', then there is no
12624 /* Build the return-statement. */
12625 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12626 /* Don't deduce from a discarded return statement. */;
12628 statement
= finish_return_stmt (expr
);
12629 /* Look for the final `;'. */
12630 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12635 if (parser
->in_function_body
12636 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12638 error ("%<goto%> in %<constexpr%> function");
12639 cp_function_chain
->invalid_constexpr
= true;
12642 /* Create the goto-statement. */
12643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12645 /* Issue a warning about this use of a GNU extension. */
12646 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12647 /* Consume the '*' token. */
12648 cp_lexer_consume_token (parser
->lexer
);
12649 /* Parse the dependent expression. */
12650 finish_goto_stmt (cp_parser_expression (parser
));
12653 finish_goto_stmt (cp_parser_identifier (parser
));
12654 /* Look for the final `;'. */
12655 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12659 cp_parser_error (parser
, "expected jump-statement");
12666 /* Parse a declaration-statement.
12668 declaration-statement:
12669 block-declaration */
12672 cp_parser_declaration_statement (cp_parser
* parser
)
12676 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12677 p
= obstack_alloc (&declarator_obstack
, 0);
12679 /* Parse the block-declaration. */
12680 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12682 /* Free any declarators allocated. */
12683 obstack_free (&declarator_obstack
, p
);
12686 /* Some dependent statements (like `if (cond) statement'), are
12687 implicitly in their own scope. In other words, if the statement is
12688 a single statement (as opposed to a compound-statement), it is
12689 none-the-less treated as if it were enclosed in braces. Any
12690 declarations appearing in the dependent statement are out of scope
12691 after control passes that point. This function parses a statement,
12692 but ensures that is in its own scope, even if it is not a
12693 compound-statement.
12695 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12696 is a (possibly labeled) if statement which is not enclosed in
12697 braces and has an else clause. This is used to implement
12700 CHAIN is a vector of if-else-if conditions. This is used to implement
12703 Returns the new statement. */
12706 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12707 const token_indent_info
&guard_tinfo
,
12711 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12712 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12713 token_indent_info body_tinfo
12714 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12719 /* Mark if () ; with a special NOP_EXPR. */
12720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12722 cp_lexer_consume_token (parser
->lexer
);
12723 statement
= add_stmt (build_empty_stmt (body_loc
));
12725 if (guard_tinfo
.keyword
== RID_IF
12726 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12727 warning_at (body_loc
, OPT_Wempty_body
,
12728 "suggest braces around empty body in an %<if%> statement");
12729 else if (guard_tinfo
.keyword
== RID_ELSE
)
12730 warning_at (body_loc
, OPT_Wempty_body
,
12731 "suggest braces around empty body in an %<else%> statement");
12733 /* if a compound is opened, we simply parse the statement directly. */
12734 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12735 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12736 /* If the token is not a `{', then we must take special action. */
12739 /* Create a compound-statement. */
12740 statement
= begin_compound_stmt (0);
12741 /* Parse the dependent-statement. */
12742 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12743 &body_loc_after_labels
);
12744 /* Finish the dummy compound-statement. */
12745 finish_compound_stmt (statement
);
12748 token_indent_info next_tinfo
12749 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12750 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12752 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12753 && next_tinfo
.type
!= CPP_SEMICOLON
)
12754 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12755 guard_tinfo
.location
, guard_tinfo
.keyword
);
12757 /* Return the statement. */
12761 /* For some dependent statements (like `while (cond) statement'), we
12762 have already created a scope. Therefore, even if the dependent
12763 statement is a compound-statement, we do not want to create another
12767 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12768 const token_indent_info
&guard_tinfo
)
12770 /* If the token is a `{', then we must take special action. */
12771 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12773 token_indent_info body_tinfo
12774 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12775 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12777 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12778 &loc_after_labels
);
12779 token_indent_info next_tinfo
12780 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12781 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12783 if (loc_after_labels
!= UNKNOWN_LOCATION
12784 && next_tinfo
.type
!= CPP_SEMICOLON
)
12785 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12786 guard_tinfo
.location
,
12787 guard_tinfo
.keyword
);
12791 /* Avoid calling cp_parser_compound_statement, so that we
12792 don't create a new scope. Do everything else by hand. */
12793 matching_braces braces
;
12794 braces
.require_open (parser
);
12795 /* If the next keyword is `__label__' we have a label declaration. */
12796 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12797 cp_parser_label_declaration (parser
);
12798 /* Parse an (optional) statement-seq. */
12799 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12800 braces
.require_close (parser
);
12804 /* Declarations [gram.dcl.dcl] */
12806 /* Parse an optional declaration-sequence.
12810 declaration-seq declaration */
12813 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12819 if (token
->type
== CPP_CLOSE_BRACE
12820 || token
->type
== CPP_EOF
)
12823 cp_parser_toplevel_declaration (parser
);
12827 /* Parse a declaration.
12831 function-definition
12832 template-declaration
12833 explicit-instantiation
12834 explicit-specialization
12835 linkage-specification
12836 namespace-definition
12844 __extension__ declaration */
12847 cp_parser_declaration (cp_parser
* parser
)
12851 int saved_pedantic
;
12853 tree attributes
= NULL_TREE
;
12855 /* Check for the `__extension__' keyword. */
12856 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12858 /* Parse the qualified declaration. */
12859 cp_parser_declaration (parser
);
12860 /* Restore the PEDANTIC flag. */
12861 pedantic
= saved_pedantic
;
12866 /* Try to figure out what kind of declaration is present. */
12867 token1
= *cp_lexer_peek_token (parser
->lexer
);
12869 if (token1
.type
!= CPP_EOF
)
12870 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12873 token2
.type
= CPP_EOF
;
12874 token2
.keyword
= RID_MAX
;
12877 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12878 p
= obstack_alloc (&declarator_obstack
, 0);
12880 /* If the next token is `extern' and the following token is a string
12881 literal, then we have a linkage specification. */
12882 if (token1
.keyword
== RID_EXTERN
12883 && cp_parser_is_pure_string_literal (&token2
))
12884 cp_parser_linkage_specification (parser
);
12885 /* If the next token is `template', then we have either a template
12886 declaration, an explicit instantiation, or an explicit
12888 else if (token1
.keyword
== RID_TEMPLATE
)
12890 /* `template <>' indicates a template specialization. */
12891 if (token2
.type
== CPP_LESS
12892 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12893 cp_parser_explicit_specialization (parser
);
12894 /* `template <' indicates a template declaration. */
12895 else if (token2
.type
== CPP_LESS
)
12896 cp_parser_template_declaration (parser
, /*member_p=*/false);
12897 /* Anything else must be an explicit instantiation. */
12899 cp_parser_explicit_instantiation (parser
);
12901 /* If the next token is `export', then we have a template
12903 else if (token1
.keyword
== RID_EXPORT
)
12904 cp_parser_template_declaration (parser
, /*member_p=*/false);
12905 /* If the next token is `extern', 'static' or 'inline' and the one
12906 after that is `template', we have a GNU extended explicit
12907 instantiation directive. */
12908 else if (cp_parser_allow_gnu_extensions_p (parser
)
12909 && (token1
.keyword
== RID_EXTERN
12910 || token1
.keyword
== RID_STATIC
12911 || token1
.keyword
== RID_INLINE
)
12912 && token2
.keyword
== RID_TEMPLATE
)
12913 cp_parser_explicit_instantiation (parser
);
12914 /* If the next token is `namespace', check for a named or unnamed
12915 namespace definition. */
12916 else if (token1
.keyword
== RID_NAMESPACE
12917 && (/* A named namespace definition. */
12918 (token2
.type
== CPP_NAME
12919 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12921 || (token2
.type
== CPP_OPEN_SQUARE
12922 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12923 == CPP_OPEN_SQUARE
)
12924 /* An unnamed namespace definition. */
12925 || token2
.type
== CPP_OPEN_BRACE
12926 || token2
.keyword
== RID_ATTRIBUTE
))
12927 cp_parser_namespace_definition (parser
);
12928 /* An inline (associated) namespace definition. */
12929 else if (token1
.keyword
== RID_INLINE
12930 && token2
.keyword
== RID_NAMESPACE
)
12931 cp_parser_namespace_definition (parser
);
12932 /* Objective-C++ declaration/definition. */
12933 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12934 cp_parser_objc_declaration (parser
, NULL_TREE
);
12935 else if (c_dialect_objc ()
12936 && token1
.keyword
== RID_ATTRIBUTE
12937 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12938 cp_parser_objc_declaration (parser
, attributes
);
12939 /* At this point we may have a template declared by a concept
12941 else if (flag_concepts
12942 && cp_parser_template_declaration_after_export (parser
,
12943 /*member_p=*/false))
12946 /* Try to parse a block-declaration, or a function-definition. */
12947 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12949 /* Free any declarators allocated. */
12950 obstack_free (&declarator_obstack
, p
);
12953 /* Parse a namespace-scope declaration. */
12956 cp_parser_toplevel_declaration (cp_parser
* parser
)
12958 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12960 if (token
->type
== CPP_PRAGMA
)
12961 /* A top-level declaration can consist solely of a #pragma. A
12962 nested declaration cannot, so this is done here and not in
12963 cp_parser_declaration. (A #pragma at block scope is
12964 handled in cp_parser_statement.) */
12965 cp_parser_pragma (parser
, pragma_external
, NULL
);
12966 else if (token
->type
== CPP_SEMICOLON
)
12968 /* A declaration consisting of a single semicolon is
12969 invalid. Allow it unless we're being pedantic. */
12970 cp_lexer_consume_token (parser
->lexer
);
12971 if (!in_system_header_at (input_location
))
12972 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12975 /* Parse the declaration itself. */
12976 cp_parser_declaration (parser
);
12979 /* Parse a block-declaration.
12984 namespace-alias-definition
12991 __extension__ block-declaration
12996 static_assert-declaration
12998 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12999 part of a declaration-statement. */
13002 cp_parser_block_declaration (cp_parser
*parser
,
13006 int saved_pedantic
;
13008 /* Check for the `__extension__' keyword. */
13009 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13011 /* Parse the qualified declaration. */
13012 cp_parser_block_declaration (parser
, statement_p
);
13013 /* Restore the PEDANTIC flag. */
13014 pedantic
= saved_pedantic
;
13019 /* Peek at the next token to figure out which kind of declaration is
13021 token1
= cp_lexer_peek_token (parser
->lexer
);
13023 /* If the next keyword is `asm', we have an asm-definition. */
13024 if (token1
->keyword
== RID_ASM
)
13027 cp_parser_commit_to_tentative_parse (parser
);
13028 cp_parser_asm_definition (parser
);
13030 /* If the next keyword is `namespace', we have a
13031 namespace-alias-definition. */
13032 else if (token1
->keyword
== RID_NAMESPACE
)
13033 cp_parser_namespace_alias_definition (parser
);
13034 /* If the next keyword is `using', we have a
13035 using-declaration, a using-directive, or an alias-declaration. */
13036 else if (token1
->keyword
== RID_USING
)
13041 cp_parser_commit_to_tentative_parse (parser
);
13042 /* If the token after `using' is `namespace', then we have a
13043 using-directive. */
13044 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13045 if (token2
->keyword
== RID_NAMESPACE
)
13046 cp_parser_using_directive (parser
);
13047 /* If the second token after 'using' is '=', then we have an
13048 alias-declaration. */
13049 else if (cxx_dialect
>= cxx11
13050 && token2
->type
== CPP_NAME
13051 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13052 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13053 cp_parser_alias_declaration (parser
);
13054 /* Otherwise, it's a using-declaration. */
13056 cp_parser_using_declaration (parser
,
13057 /*access_declaration_p=*/false);
13059 /* If the next keyword is `__label__' we have a misplaced label
13061 else if (token1
->keyword
== RID_LABEL
)
13063 cp_lexer_consume_token (parser
->lexer
);
13064 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13065 cp_parser_skip_to_end_of_statement (parser
);
13066 /* If the next token is now a `;', consume it. */
13067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13068 cp_lexer_consume_token (parser
->lexer
);
13070 /* If the next token is `static_assert' we have a static assertion. */
13071 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13072 cp_parser_static_assert (parser
, /*member_p=*/false);
13073 /* Anything else must be a simple-declaration. */
13075 cp_parser_simple_declaration (parser
, !statement_p
,
13076 /*maybe_range_for_decl*/NULL
);
13079 /* Parse a simple-declaration.
13081 simple-declaration:
13082 decl-specifier-seq [opt] init-declarator-list [opt] ;
13083 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13084 brace-or-equal-initializer ;
13086 init-declarator-list:
13088 init-declarator-list , init-declarator
13090 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13091 function-definition as a simple-declaration.
13093 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13094 parsed declaration if it is an uninitialized single declarator not followed
13095 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13096 if present, will not be consumed. */
13099 cp_parser_simple_declaration (cp_parser
* parser
,
13100 bool function_definition_allowed_p
,
13101 tree
*maybe_range_for_decl
)
13103 cp_decl_specifier_seq decl_specifiers
;
13104 int declares_class_or_enum
;
13105 bool saw_declarator
;
13106 location_t comma_loc
= UNKNOWN_LOCATION
;
13107 location_t init_loc
= UNKNOWN_LOCATION
;
13109 if (maybe_range_for_decl
)
13110 *maybe_range_for_decl
= NULL_TREE
;
13112 /* Defer access checks until we know what is being declared; the
13113 checks for names appearing in the decl-specifier-seq should be
13114 done as if we were in the scope of the thing being declared. */
13115 push_deferring_access_checks (dk_deferred
);
13117 /* Parse the decl-specifier-seq. We have to keep track of whether
13118 or not the decl-specifier-seq declares a named class or
13119 enumeration type, since that is the only case in which the
13120 init-declarator-list is allowed to be empty.
13124 In a simple-declaration, the optional init-declarator-list can be
13125 omitted only when declaring a class or enumeration, that is when
13126 the decl-specifier-seq contains either a class-specifier, an
13127 elaborated-type-specifier, or an enum-specifier. */
13128 cp_parser_decl_specifier_seq (parser
,
13129 CP_PARSER_FLAGS_OPTIONAL
,
13131 &declares_class_or_enum
);
13132 /* We no longer need to defer access checks. */
13133 stop_deferring_access_checks ();
13135 /* In a block scope, a valid declaration must always have a
13136 decl-specifier-seq. By not trying to parse declarators, we can
13137 resolve the declaration/expression ambiguity more quickly. */
13138 if (!function_definition_allowed_p
13139 && !decl_specifiers
.any_specifiers_p
)
13141 cp_parser_error (parser
, "expected declaration");
13145 /* If the next two tokens are both identifiers, the code is
13146 erroneous. The usual cause of this situation is code like:
13150 where "T" should name a type -- but does not. */
13151 if (!decl_specifiers
.any_type_specifiers_p
13152 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13154 /* If parsing tentatively, we should commit; we really are
13155 looking at a declaration. */
13156 cp_parser_commit_to_tentative_parse (parser
);
13161 cp_parser_maybe_commit_to_declaration (parser
,
13162 decl_specifiers
.any_specifiers_p
);
13164 /* Look for C++17 decomposition declaration. */
13165 for (size_t n
= 1; ; n
++)
13166 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13167 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13169 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13170 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13171 && decl_specifiers
.any_specifiers_p
)
13174 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13175 maybe_range_for_decl
,
13178 /* The next token should be either a `,' or a `;'. */
13179 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13180 /* If it's a `;', we are done. */
13181 if (token
->type
== CPP_SEMICOLON
)
13183 else if (maybe_range_for_decl
)
13185 if (*maybe_range_for_decl
== NULL_TREE
)
13186 *maybe_range_for_decl
= error_mark_node
;
13189 /* Anything else is an error. */
13192 /* If we have already issued an error message we don't need
13193 to issue another one. */
13194 if ((decl
!= error_mark_node
13195 && DECL_INITIAL (decl
) != error_mark_node
)
13196 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13197 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13198 /* Skip tokens until we reach the end of the statement. */
13199 cp_parser_skip_to_end_of_statement (parser
);
13200 /* If the next token is now a `;', consume it. */
13201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13202 cp_lexer_consume_token (parser
->lexer
);
13210 bool auto_specifier_p
;
13211 /* NULL_TREE if both variable and function declaration are allowed,
13212 error_mark_node if function declaration are not allowed and
13213 a FUNCTION_DECL that should be diagnosed if it is followed by
13214 variable declarations. */
13215 tree auto_function_declaration
;
13217 last_type
= NULL_TREE
;
13219 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13220 auto_function_declaration
= NULL_TREE
;
13222 /* Keep going until we hit the `;' at the end of the simple
13224 saw_declarator
= false;
13225 while (cp_lexer_next_token_is_not (parser
->lexer
,
13229 bool function_definition_p
;
13231 tree auto_result
= NULL_TREE
;
13233 if (saw_declarator
)
13235 /* If we are processing next declarator, comma is expected */
13236 token
= cp_lexer_peek_token (parser
->lexer
);
13237 gcc_assert (token
->type
== CPP_COMMA
);
13238 cp_lexer_consume_token (parser
->lexer
);
13239 if (maybe_range_for_decl
)
13241 *maybe_range_for_decl
= error_mark_node
;
13242 if (comma_loc
== UNKNOWN_LOCATION
)
13243 comma_loc
= token
->location
;
13247 saw_declarator
= true;
13249 /* Parse the init-declarator. */
13250 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13252 function_definition_allowed_p
,
13253 /*member_p=*/false,
13254 declares_class_or_enum
,
13255 &function_definition_p
,
13256 maybe_range_for_decl
,
13259 /* If an error occurred while parsing tentatively, exit quickly.
13260 (That usually happens when in the body of a function; each
13261 statement is treated as a declaration-statement until proven
13263 if (cp_parser_error_occurred (parser
))
13266 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13268 /* If the init-declarator-list contains more than one
13269 init-declarator, they shall all form declarations of
13271 if (auto_function_declaration
== NULL_TREE
)
13272 auto_function_declaration
13273 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13274 else if (TREE_CODE (decl
) == FUNCTION_DECL
13275 || auto_function_declaration
!= error_mark_node
)
13277 error_at (decl_specifiers
.locations
[ds_type_spec
],
13278 "non-variable %qD in declaration with more than one "
13279 "declarator with placeholder type",
13280 TREE_CODE (decl
) == FUNCTION_DECL
13281 ? decl
: auto_function_declaration
);
13282 auto_function_declaration
= error_mark_node
;
13287 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13290 && last_type
!= error_mark_node
13291 && !same_type_p (auto_result
, last_type
))
13293 /* If the list of declarators contains more than one declarator,
13294 the type of each declared variable is determined as described
13295 above. If the type deduced for the template parameter U is not
13296 the same in each deduction, the program is ill-formed. */
13297 error_at (decl_specifiers
.locations
[ds_type_spec
],
13298 "inconsistent deduction for %qT: %qT and then %qT",
13299 decl_specifiers
.type
, last_type
, auto_result
);
13300 last_type
= error_mark_node
;
13303 last_type
= auto_result
;
13306 /* Handle function definitions specially. */
13307 if (function_definition_p
)
13309 /* If the next token is a `,', then we are probably
13310 processing something like:
13314 which is erroneous. */
13315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13317 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13318 error_at (token
->location
,
13320 " declarations and function-definitions is forbidden");
13322 /* Otherwise, we're done with the list of declarators. */
13325 pop_deferring_access_checks ();
13329 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13330 *maybe_range_for_decl
= decl
;
13331 /* The next token should be either a `,' or a `;'. */
13332 token
= cp_lexer_peek_token (parser
->lexer
);
13333 /* If it's a `,', there are more declarators to come. */
13334 if (token
->type
== CPP_COMMA
)
13335 /* will be consumed next time around */;
13336 /* If it's a `;', we are done. */
13337 else if (token
->type
== CPP_SEMICOLON
)
13339 else if (maybe_range_for_decl
)
13341 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13342 permerror (decl_specifiers
.locations
[ds_type_spec
],
13343 "types may not be defined in a for-range-declaration");
13346 /* Anything else is an error. */
13349 /* If we have already issued an error message we don't need
13350 to issue another one. */
13351 if ((decl
!= error_mark_node
13352 && DECL_INITIAL (decl
) != error_mark_node
)
13353 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13354 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13355 /* Skip tokens until we reach the end of the statement. */
13356 cp_parser_skip_to_end_of_statement (parser
);
13357 /* If the next token is now a `;', consume it. */
13358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13359 cp_lexer_consume_token (parser
->lexer
);
13362 /* After the first time around, a function-definition is not
13363 allowed -- even if it was OK at first. For example:
13368 function_definition_allowed_p
= false;
13371 /* Issue an error message if no declarators are present, and the
13372 decl-specifier-seq does not itself declare a class or
13373 enumeration: [dcl.dcl]/3. */
13374 if (!saw_declarator
)
13376 if (cp_parser_declares_only_class_p (parser
))
13378 if (!declares_class_or_enum
13379 && decl_specifiers
.type
13380 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13381 /* Ensure an error is issued anyway when finish_decltype_type,
13382 called via cp_parser_decl_specifier_seq, returns a class or
13383 an enumeration (c++/51786). */
13384 decl_specifiers
.type
= NULL_TREE
;
13385 shadow_tag (&decl_specifiers
);
13387 /* Perform any deferred access checks. */
13388 perform_deferred_access_checks (tf_warning_or_error
);
13391 /* Consume the `;'. */
13393 if (!maybe_range_for_decl
)
13394 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13395 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13397 if (init_loc
!= UNKNOWN_LOCATION
)
13398 error_at (init_loc
, "initializer in range-based %<for%> loop");
13399 if (comma_loc
!= UNKNOWN_LOCATION
)
13400 error_at (comma_loc
,
13401 "multiple declarations in range-based %<for%> loop");
13405 pop_deferring_access_checks ();
13408 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13409 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13413 cp_parser_decomposition_declaration (cp_parser
*parser
,
13414 cp_decl_specifier_seq
*decl_specifiers
,
13415 tree
*maybe_range_for_decl
,
13416 location_t
*init_loc
)
13418 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13419 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13420 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13422 /* Parse the identifier-list. */
13423 auto_vec
<cp_expr
, 10> v
;
13424 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13427 cp_expr e
= cp_parser_identifier (parser
);
13428 if (e
.get_value () == error_mark_node
)
13431 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13433 cp_lexer_consume_token (parser
->lexer
);
13436 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13437 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13439 end_loc
= UNKNOWN_LOCATION
;
13440 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13443 cp_lexer_consume_token (parser
->lexer
);
13446 cp_parser_skip_to_end_of_statement (parser
);
13447 return error_mark_node
;
13451 if (cxx_dialect
< cxx17
)
13452 pedwarn (loc
, 0, "structured bindings only available with "
13453 "-std=c++17 or -std=gnu++17");
13456 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13457 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13458 declarator
->id_loc
= loc
;
13459 if (ref_qual
!= REF_QUAL_NONE
)
13460 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13461 ref_qual
== REF_QUAL_RVALUE
,
13463 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13464 NULL_TREE
, decl_specifiers
->attributes
,
13466 tree orig_decl
= decl
;
13470 cp_decl_specifier_seq decl_specs
;
13471 clear_decl_specs (&decl_specs
);
13472 decl_specs
.type
= make_auto ();
13474 FOR_EACH_VEC_ELT (v
, i
, e
)
13477 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13478 sfk_none
, e
.get_location ());
13481 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13482 declarator
->id_loc
= e
.get_location ();
13484 tree elt_pushed_scope
;
13485 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13486 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13487 if (decl2
== error_mark_node
)
13488 decl
= error_mark_node
;
13489 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13491 /* Ensure we've diagnosed redeclaration if we aren't creating
13493 gcc_assert (errorcount
);
13494 decl
= error_mark_node
;
13498 if (elt_pushed_scope
)
13499 pop_scope (elt_pushed_scope
);
13504 error_at (loc
, "empty structured binding declaration");
13505 decl
= error_mark_node
;
13508 if (maybe_range_for_decl
== NULL
13509 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13511 bool non_constant_p
= false, is_direct_init
= false;
13512 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13513 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13515 if (initializer
== NULL_TREE
13516 || (TREE_CODE (initializer
) == TREE_LIST
13517 && TREE_CHAIN (initializer
))
13519 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13520 && CONSTRUCTOR_NELTS (initializer
) != 1))
13522 error_at (loc
, "invalid initializer for structured binding "
13524 initializer
= error_mark_node
;
13527 if (decl
!= error_mark_node
)
13529 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13530 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13531 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13532 cp_finish_decomp (decl
, prev
, v
.length ());
13535 else if (decl
!= error_mark_node
)
13537 *maybe_range_for_decl
= prev
;
13538 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13539 the underlying DECL. */
13540 cp_finish_decomp (decl
, prev
, v
.length ());
13544 pop_scope (pushed_scope
);
13546 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13548 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13549 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13555 /* Parse a decl-specifier-seq.
13557 decl-specifier-seq:
13558 decl-specifier-seq [opt] decl-specifier
13559 decl-specifier attribute-specifier-seq [opt] (C++11)
13562 storage-class-specifier
13573 Concepts Extension:
13578 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13580 The parser flags FLAGS is used to control type-specifier parsing.
13582 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13585 1: one of the decl-specifiers is an elaborated-type-specifier
13586 (i.e., a type declaration)
13587 2: one of the decl-specifiers is an enum-specifier or a
13588 class-specifier (i.e., a type definition)
13593 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13594 cp_parser_flags flags
,
13595 cp_decl_specifier_seq
*decl_specs
,
13596 int* declares_class_or_enum
)
13598 bool constructor_possible_p
= !parser
->in_declarator_p
;
13599 bool found_decl_spec
= false;
13600 cp_token
*start_token
= NULL
;
13603 /* Clear DECL_SPECS. */
13604 clear_decl_specs (decl_specs
);
13606 /* Assume no class or enumeration type is declared. */
13607 *declares_class_or_enum
= 0;
13609 /* Keep reading specifiers until there are no more to read. */
13612 bool constructor_p
;
13616 /* Peek at the next token. */
13617 token
= cp_lexer_peek_token (parser
->lexer
);
13619 /* Save the first token of the decl spec list for error
13622 start_token
= token
;
13623 /* Handle attributes. */
13624 if (cp_next_tokens_can_be_attribute_p (parser
))
13626 /* Parse the attributes. */
13627 tree attrs
= cp_parser_attributes_opt (parser
);
13629 /* In a sequence of declaration specifiers, c++11 attributes
13630 appertain to the type that precede them. In that case
13633 The attribute-specifier-seq affects the type only for
13634 the declaration it appears in, not other declarations
13635 involving the same type.
13637 But for now let's force the user to position the
13638 attribute either at the beginning of the declaration or
13639 after the declarator-id, which would clearly mean that it
13640 applies to the declarator. */
13641 if (cxx11_attribute_p (attrs
))
13643 if (!found_decl_spec
)
13644 /* The c++11 attribute is at the beginning of the
13645 declaration. It appertains to the entity being
13649 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13651 /* This is an attribute following a
13652 class-specifier. */
13653 if (decl_specs
->type_definition_p
)
13654 warn_misplaced_attr_for_class_type (token
->location
,
13660 decl_specs
->std_attributes
13661 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13662 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13663 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13669 decl_specs
->attributes
13670 = attr_chainon (decl_specs
->attributes
, attrs
);
13671 if (decl_specs
->locations
[ds_attribute
] == 0)
13672 decl_specs
->locations
[ds_attribute
] = token
->location
;
13675 /* Assume we will find a decl-specifier keyword. */
13676 found_decl_spec
= true;
13677 /* If the next token is an appropriate keyword, we can simply
13678 add it to the list. */
13679 switch (token
->keyword
)
13685 if (!at_class_scope_p ())
13687 gcc_rich_location
richloc (token
->location
);
13688 richloc
.add_fixit_remove ();
13689 error_at (&richloc
, "%<friend%> used outside of class");
13690 cp_lexer_purge_token (parser
->lexer
);
13695 /* Consume the token. */
13696 cp_lexer_consume_token (parser
->lexer
);
13700 case RID_CONSTEXPR
:
13702 cp_lexer_consume_token (parser
->lexer
);
13707 cp_lexer_consume_token (parser
->lexer
);
13710 /* function-specifier:
13717 cp_parser_function_specifier_opt (parser
, decl_specs
);
13724 /* Consume the token. */
13725 cp_lexer_consume_token (parser
->lexer
);
13726 /* A constructor declarator cannot appear in a typedef. */
13727 constructor_possible_p
= false;
13728 /* The "typedef" keyword can only occur in a declaration; we
13729 may as well commit at this point. */
13730 cp_parser_commit_to_tentative_parse (parser
);
13732 if (decl_specs
->storage_class
!= sc_none
)
13733 decl_specs
->conflicting_specifiers_p
= true;
13736 /* storage-class-specifier:
13746 if (cxx_dialect
== cxx98
)
13748 /* Consume the token. */
13749 cp_lexer_consume_token (parser
->lexer
);
13751 /* Complain about `auto' as a storage specifier, if
13752 we're complaining about C++0x compatibility. */
13753 gcc_rich_location
richloc (token
->location
);
13754 richloc
.add_fixit_remove ();
13755 warning_at (&richloc
, OPT_Wc__11_compat
,
13756 "%<auto%> changes meaning in C++11; "
13757 "please remove it");
13759 /* Set the storage class anyway. */
13760 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13764 /* C++0x auto type-specifier. */
13765 found_decl_spec
= false;
13772 /* Consume the token. */
13773 cp_lexer_consume_token (parser
->lexer
);
13774 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13778 /* Consume the token. */
13780 cp_lexer_consume_token (parser
->lexer
);
13784 /* We did not yet find a decl-specifier yet. */
13785 found_decl_spec
= false;
13789 if (found_decl_spec
13790 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13791 && token
->keyword
!= RID_CONSTEXPR
)
13792 error ("decl-specifier invalid in condition");
13794 if (found_decl_spec
13795 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13796 && token
->keyword
!= RID_MUTABLE
13797 && token
->keyword
!= RID_CONSTEXPR
)
13798 error_at (token
->location
, "%qD invalid in lambda",
13799 ridpointers
[token
->keyword
]);
13802 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13804 /* Constructors are a special case. The `S' in `S()' is not a
13805 decl-specifier; it is the beginning of the declarator. */
13807 = (!found_decl_spec
13808 && constructor_possible_p
13809 && (cp_parser_constructor_declarator_p
13810 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13812 /* If we don't have a DECL_SPEC yet, then we must be looking at
13813 a type-specifier. */
13814 if (!found_decl_spec
&& !constructor_p
)
13816 int decl_spec_declares_class_or_enum
;
13817 bool is_cv_qualifier
;
13821 = cp_parser_type_specifier (parser
, flags
,
13823 /*is_declaration=*/true,
13824 &decl_spec_declares_class_or_enum
,
13826 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13828 /* If this type-specifier referenced a user-defined type
13829 (a typedef, class-name, etc.), then we can't allow any
13830 more such type-specifiers henceforth.
13834 The longest sequence of decl-specifiers that could
13835 possibly be a type name is taken as the
13836 decl-specifier-seq of a declaration. The sequence shall
13837 be self-consistent as described below.
13841 As a general rule, at most one type-specifier is allowed
13842 in the complete decl-specifier-seq of a declaration. The
13843 only exceptions are the following:
13845 -- const or volatile can be combined with any other
13848 -- signed or unsigned can be combined with char, long,
13856 void g (const int Pc);
13858 Here, Pc is *not* part of the decl-specifier seq; it's
13859 the declarator. Therefore, once we see a type-specifier
13860 (other than a cv-qualifier), we forbid any additional
13861 user-defined types. We *do* still allow things like `int
13862 int' to be considered a decl-specifier-seq, and issue the
13863 error message later. */
13864 if (type_spec
&& !is_cv_qualifier
)
13865 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13866 /* A constructor declarator cannot follow a type-specifier. */
13869 constructor_possible_p
= false;
13870 found_decl_spec
= true;
13871 if (!is_cv_qualifier
)
13872 decl_specs
->any_type_specifiers_p
= true;
13874 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
13875 error_at (token
->location
, "type-specifier invalid in lambda");
13879 /* If we still do not have a DECL_SPEC, then there are no more
13880 decl-specifiers. */
13881 if (!found_decl_spec
)
13884 decl_specs
->any_specifiers_p
= true;
13885 /* After we see one decl-specifier, further decl-specifiers are
13886 always optional. */
13887 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13890 /* Don't allow a friend specifier with a class definition. */
13891 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13892 && (*declares_class_or_enum
& 2))
13893 error_at (decl_specs
->locations
[ds_friend
],
13894 "class definition may not be declared a friend");
13897 /* Parse an (optional) storage-class-specifier.
13899 storage-class-specifier:
13908 storage-class-specifier:
13911 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13914 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13916 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13919 if (cxx_dialect
!= cxx98
)
13921 /* Fall through for C++98. */
13922 gcc_fallthrough ();
13929 /* Consume the token. */
13930 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13937 /* Parse an (optional) function-specifier.
13939 function-specifier:
13945 explicit(constant-expression)
13947 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13948 Updates DECL_SPECS, if it is non-NULL. */
13951 cp_parser_function_specifier_opt (cp_parser
* parser
,
13952 cp_decl_specifier_seq
*decl_specs
)
13954 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13955 switch (token
->keyword
)
13958 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13962 /* 14.5.2.3 [temp.mem]
13964 A member function template shall not be virtual. */
13965 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13966 && current_class_type
)
13967 error_at (token
->location
, "templates may not be %<virtual%>");
13969 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13974 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13975 /* If we see '(', it's C++20 explicit(bool). */
13977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13979 matching_parens parens
;
13980 parens
.consume_open (parser
);
13982 /* New types are not allowed in an explicit-specifier. */
13983 const char *saved_message
13984 = parser
->type_definition_forbidden_message
;
13985 parser
->type_definition_forbidden_message
13986 = G_("types may not be defined in explicit-specifier");
13988 if (cxx_dialect
< cxx2a
)
13989 pedwarn (token
->location
, 0,
13990 "%<explicit(bool)%> only available with -std=c++2a "
13991 "or -std=gnu++2a");
13993 /* Parse the constant-expression. */
13994 expr
= cp_parser_constant_expression (parser
);
13996 /* Restore the saved message. */
13997 parser
->type_definition_forbidden_message
= saved_message
;
13998 parens
.require_close (parser
);
14001 /* The explicit-specifier explicit without a constant-expression is
14002 equivalent to the explicit-specifier explicit(true). */
14003 expr
= boolean_true_node
;
14006 "the constant-expression, if supplied, shall be a contextually
14007 converted constant expression of type bool." */
14008 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14009 /* We could evaluate it -- mark the decl as appropriate. */
14010 if (expr
== boolean_true_node
)
14011 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14012 else if (expr
== boolean_false_node
)
14013 /* Don't mark the decl as explicit. */;
14014 else if (decl_specs
)
14015 /* The expression was value-dependent. Remember it so that we can
14016 substitute it later. */
14017 decl_specs
->explicit_specifier
= expr
;
14025 /* Consume the token. */
14026 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14029 /* Parse a linkage-specification.
14031 linkage-specification:
14032 extern string-literal { declaration-seq [opt] }
14033 extern string-literal declaration */
14036 cp_parser_linkage_specification (cp_parser
* parser
)
14040 /* Look for the `extern' keyword. */
14041 cp_token
*extern_token
14042 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14044 /* Look for the string-literal. */
14045 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14046 linkage
= cp_parser_string_literal (parser
, false, false);
14048 /* Transform the literal into an identifier. If the literal is a
14049 wide-character string, or contains embedded NULs, then we can't
14050 handle it as the user wants. */
14051 if (strlen (TREE_STRING_POINTER (linkage
))
14052 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14054 cp_parser_error (parser
, "invalid linkage-specification");
14055 /* Assume C++ linkage. */
14056 linkage
= lang_name_cplusplus
;
14059 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14061 /* We're now using the new linkage. */
14062 push_lang_context (linkage
);
14064 /* Preserve the location of the the innermost linkage specification,
14065 tracking the locations of nested specifications via a local. */
14066 location_t saved_location
14067 = parser
->innermost_linkage_specification_location
;
14068 /* Construct a location ranging from the start of the "extern" to
14069 the end of the string-literal, with the caret at the start, e.g.:
14073 parser
->innermost_linkage_specification_location
14074 = make_location (extern_token
->location
,
14075 extern_token
->location
,
14076 get_finish (string_token
->location
));
14078 /* If the next token is a `{', then we're using the first
14080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14082 cp_ensure_no_omp_declare_simd (parser
);
14083 cp_ensure_no_oacc_routine (parser
);
14085 /* Consume the `{' token. */
14086 matching_braces braces
;
14087 braces
.consume_open (parser
)->location
;
14088 /* Parse the declarations. */
14089 cp_parser_declaration_seq_opt (parser
);
14090 /* Look for the closing `}'. */
14091 braces
.require_close (parser
);
14093 /* Otherwise, there's just one declaration. */
14096 bool saved_in_unbraced_linkage_specification_p
;
14098 saved_in_unbraced_linkage_specification_p
14099 = parser
->in_unbraced_linkage_specification_p
;
14100 parser
->in_unbraced_linkage_specification_p
= true;
14101 cp_parser_declaration (parser
);
14102 parser
->in_unbraced_linkage_specification_p
14103 = saved_in_unbraced_linkage_specification_p
;
14106 /* We're done with the linkage-specification. */
14107 pop_lang_context ();
14109 /* Restore location of parent linkage specification, if any. */
14110 parser
->innermost_linkage_specification_location
= saved_location
;
14113 /* Parse a static_assert-declaration.
14115 static_assert-declaration:
14116 static_assert ( constant-expression , string-literal ) ;
14117 static_assert ( constant-expression ) ; (C++17)
14119 If MEMBER_P, this static_assert is a class member. */
14122 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14125 location_t token_loc
;
14129 /* Peek at the `static_assert' token so we can keep track of exactly
14130 where the static assertion started. */
14131 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14133 /* Look for the `static_assert' keyword. */
14134 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14138 /* We know we are in a static assertion; commit to any tentative
14140 if (cp_parser_parsing_tentatively (parser
))
14141 cp_parser_commit_to_tentative_parse (parser
);
14143 /* Parse the `(' starting the static assertion condition. */
14144 matching_parens parens
;
14145 parens
.require_open (parser
);
14147 /* Parse the constant-expression. Allow a non-constant expression
14148 here in order to give better diagnostics in finish_static_assert. */
14150 cp_parser_constant_expression (parser
,
14151 /*allow_non_constant_p=*/true,
14152 /*non_constant_p=*/&dummy
);
14154 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14156 if (cxx_dialect
< cxx17
)
14157 pedwarn (input_location
, OPT_Wpedantic
,
14158 "static_assert without a message "
14159 "only available with -std=c++17 or -std=gnu++17");
14161 cp_lexer_consume_token (parser
->lexer
);
14162 message
= build_string (1, "");
14163 TREE_TYPE (message
) = char_array_type_node
;
14164 fix_string_type (message
);
14168 /* Parse the separating `,'. */
14169 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14171 /* Parse the string-literal message. */
14172 message
= cp_parser_string_literal (parser
,
14173 /*translate=*/false,
14176 /* A `)' completes the static assertion. */
14177 if (!parens
.require_close (parser
))
14178 cp_parser_skip_to_closing_parenthesis (parser
,
14179 /*recovering=*/true,
14180 /*or_comma=*/false,
14181 /*consume_paren=*/true);
14184 /* A semicolon terminates the declaration. */
14185 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14187 /* Get the location for the static assertion. Use that of the
14188 condition if available, otherwise, use that of the "static_assert"
14190 location_t assert_loc
= condition
.get_location ();
14191 if (assert_loc
== UNKNOWN_LOCATION
)
14192 assert_loc
= token_loc
;
14194 /* Complete the static assertion, which may mean either processing
14195 the static assert now or saving it for template instantiation. */
14196 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14199 /* Parse the expression in decltype ( expression ). */
14202 cp_parser_decltype_expr (cp_parser
*parser
,
14203 bool &id_expression_or_member_access_p
)
14205 cp_token
*id_expr_start_token
;
14208 /* Since we're going to preserve any side-effects from this parse, set up a
14209 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14210 in the expression. */
14211 tentative_firewall
firewall (parser
);
14213 /* First, try parsing an id-expression. */
14214 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14215 cp_parser_parse_tentatively (parser
);
14216 expr
= cp_parser_id_expression (parser
,
14217 /*template_keyword_p=*/false,
14218 /*check_dependency_p=*/true,
14219 /*template_p=*/NULL
,
14220 /*declarator_p=*/false,
14221 /*optional_p=*/false);
14223 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14225 bool non_integral_constant_expression_p
= false;
14226 tree id_expression
= expr
;
14228 const char *error_msg
;
14230 if (identifier_p (expr
))
14231 /* Lookup the name we got back from the id-expression. */
14232 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14233 id_expr_start_token
->location
);
14235 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14236 /* A template without args is not a complete id-expression. */
14237 expr
= error_mark_node
;
14240 && expr
!= error_mark_node
14241 && TREE_CODE (expr
) != TYPE_DECL
14242 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14243 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14244 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14246 /* Complete lookup of the id-expression. */
14247 expr
= (finish_id_expression
14248 (id_expression
, expr
, parser
->scope
, &idk
,
14249 /*integral_constant_expression_p=*/false,
14250 /*allow_non_integral_constant_expression_p=*/true,
14251 &non_integral_constant_expression_p
,
14252 /*template_p=*/false,
14254 /*address_p=*/false,
14255 /*template_arg_p=*/false,
14257 id_expr_start_token
->location
));
14259 if (expr
== error_mark_node
)
14260 /* We found an id-expression, but it was something that we
14261 should not have found. This is an error, not something
14262 we can recover from, so note that we found an
14263 id-expression and we'll recover as gracefully as
14265 id_expression_or_member_access_p
= true;
14269 && expr
!= error_mark_node
14270 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14271 /* We have an id-expression. */
14272 id_expression_or_member_access_p
= true;
14275 if (!id_expression_or_member_access_p
)
14277 /* Abort the id-expression parse. */
14278 cp_parser_abort_tentative_parse (parser
);
14280 /* Parsing tentatively, again. */
14281 cp_parser_parse_tentatively (parser
);
14283 /* Parse a class member access. */
14284 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14285 /*cast_p=*/false, /*decltype*/true,
14286 /*member_access_only_p=*/true, NULL
);
14289 && expr
!= error_mark_node
14290 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14291 /* We have an id-expression. */
14292 id_expression_or_member_access_p
= true;
14295 if (id_expression_or_member_access_p
)
14296 /* We have parsed the complete id-expression or member access. */
14297 cp_parser_parse_definitely (parser
);
14300 /* Abort our attempt to parse an id-expression or member access
14302 cp_parser_abort_tentative_parse (parser
);
14304 /* Commit to the tentative_firewall so we get syntax errors. */
14305 cp_parser_commit_to_tentative_parse (parser
);
14307 /* Parse a full expression. */
14308 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14309 /*decltype_p=*/true);
14315 /* Parse a `decltype' type. Returns the type.
14317 simple-type-specifier:
14318 decltype ( expression )
14320 decltype ( auto ) */
14323 cp_parser_decltype (cp_parser
*parser
)
14325 bool id_expression_or_member_access_p
= false;
14326 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14328 if (start_token
->type
== CPP_DECLTYPE
)
14330 /* Already parsed. */
14331 cp_lexer_consume_token (parser
->lexer
);
14332 return saved_checks_value (start_token
->u
.tree_check_value
);
14335 /* Look for the `decltype' token. */
14336 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14337 return error_mark_node
;
14339 /* Parse the opening `('. */
14340 matching_parens parens
;
14341 if (!parens
.require_open (parser
))
14342 return error_mark_node
;
14344 push_deferring_access_checks (dk_deferred
);
14346 tree expr
= NULL_TREE
;
14348 if (cxx_dialect
>= cxx14
14349 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14350 /* decltype (auto) */
14351 cp_lexer_consume_token (parser
->lexer
);
14354 /* decltype (expression) */
14356 /* Types cannot be defined in a `decltype' expression. Save away the
14357 old message and set the new one. */
14358 const char *saved_message
= parser
->type_definition_forbidden_message
;
14359 parser
->type_definition_forbidden_message
14360 = G_("types may not be defined in %<decltype%> expressions");
14362 /* The restrictions on constant-expressions do not apply inside
14363 decltype expressions. */
14364 bool saved_integral_constant_expression_p
14365 = parser
->integral_constant_expression_p
;
14366 bool saved_non_integral_constant_expression_p
14367 = parser
->non_integral_constant_expression_p
;
14368 parser
->integral_constant_expression_p
= false;
14370 /* Within a parenthesized expression, a `>' token is always
14371 the greater-than operator. */
14372 bool saved_greater_than_is_operator_p
14373 = parser
->greater_than_is_operator_p
;
14374 parser
->greater_than_is_operator_p
= true;
14376 /* Do not actually evaluate the expression. */
14377 ++cp_unevaluated_operand
;
14379 /* Do not warn about problems with the expression. */
14380 ++c_inhibit_evaluation_warnings
;
14382 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14384 /* Go back to evaluating expressions. */
14385 --cp_unevaluated_operand
;
14386 --c_inhibit_evaluation_warnings
;
14388 /* The `>' token might be the end of a template-id or
14389 template-parameter-list now. */
14390 parser
->greater_than_is_operator_p
14391 = saved_greater_than_is_operator_p
;
14393 /* Restore the old message and the integral constant expression
14395 parser
->type_definition_forbidden_message
= saved_message
;
14396 parser
->integral_constant_expression_p
14397 = saved_integral_constant_expression_p
;
14398 parser
->non_integral_constant_expression_p
14399 = saved_non_integral_constant_expression_p
;
14402 /* Parse to the closing `)'. */
14403 if (!parens
.require_close (parser
))
14405 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14406 /*consume_paren=*/true);
14407 pop_deferring_access_checks ();
14408 return error_mark_node
;
14414 expr
= make_decltype_auto ();
14415 AUTO_IS_DECLTYPE (expr
) = true;
14418 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14419 tf_warning_or_error
);
14421 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14423 start_token
->type
= CPP_DECLTYPE
;
14424 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14425 start_token
->u
.tree_check_value
->value
= expr
;
14426 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14427 start_token
->keyword
= RID_MAX
;
14428 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14430 pop_to_parent_deferring_access_checks ();
14435 /* Special member functions [gram.special] */
14437 /* Parse a conversion-function-id.
14439 conversion-function-id:
14440 operator conversion-type-id
14442 Returns an IDENTIFIER_NODE representing the operator. */
14445 cp_parser_conversion_function_id (cp_parser
* parser
)
14449 tree saved_qualifying_scope
;
14450 tree saved_object_scope
;
14451 tree pushed_scope
= NULL_TREE
;
14453 /* Look for the `operator' token. */
14454 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14455 return error_mark_node
;
14456 /* When we parse the conversion-type-id, the current scope will be
14457 reset. However, we need that information in able to look up the
14458 conversion function later, so we save it here. */
14459 saved_scope
= parser
->scope
;
14460 saved_qualifying_scope
= parser
->qualifying_scope
;
14461 saved_object_scope
= parser
->object_scope
;
14462 /* We must enter the scope of the class so that the names of
14463 entities declared within the class are available in the
14464 conversion-type-id. For example, consider:
14471 S::operator I() { ... }
14473 In order to see that `I' is a type-name in the definition, we
14474 must be in the scope of `S'. */
14476 pushed_scope
= push_scope (saved_scope
);
14477 /* Parse the conversion-type-id. */
14478 type
= cp_parser_conversion_type_id (parser
);
14479 /* Leave the scope of the class, if any. */
14481 pop_scope (pushed_scope
);
14482 /* Restore the saved scope. */
14483 parser
->scope
= saved_scope
;
14484 parser
->qualifying_scope
= saved_qualifying_scope
;
14485 parser
->object_scope
= saved_object_scope
;
14486 /* If the TYPE is invalid, indicate failure. */
14487 if (type
== error_mark_node
)
14488 return error_mark_node
;
14489 return make_conv_op_name (type
);
14492 /* Parse a conversion-type-id:
14494 conversion-type-id:
14495 type-specifier-seq conversion-declarator [opt]
14497 Returns the TYPE specified. */
14500 cp_parser_conversion_type_id (cp_parser
* parser
)
14503 cp_decl_specifier_seq type_specifiers
;
14504 cp_declarator
*declarator
;
14505 tree type_specified
;
14506 const char *saved_message
;
14508 /* Parse the attributes. */
14509 attributes
= cp_parser_attributes_opt (parser
);
14511 saved_message
= parser
->type_definition_forbidden_message
;
14512 parser
->type_definition_forbidden_message
14513 = G_("types may not be defined in a conversion-type-id");
14515 /* Parse the type-specifiers. */
14516 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14517 /*is_trailing_return=*/false,
14520 parser
->type_definition_forbidden_message
= saved_message
;
14522 /* If that didn't work, stop. */
14523 if (type_specifiers
.type
== error_mark_node
)
14524 return error_mark_node
;
14525 /* Parse the conversion-declarator. */
14526 declarator
= cp_parser_conversion_declarator_opt (parser
);
14528 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14529 /*initialized=*/0, &attributes
);
14531 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14533 /* Don't give this error when parsing tentatively. This happens to
14534 work because we always parse this definitively once. */
14535 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14536 && type_uses_auto (type_specified
))
14538 if (cxx_dialect
< cxx14
)
14540 error ("invalid use of %<auto%> in conversion operator");
14541 return error_mark_node
;
14543 else if (template_parm_scope_p ())
14544 warning (0, "use of %<auto%> in member template "
14545 "conversion operator can never be deduced");
14548 return type_specified
;
14551 /* Parse an (optional) conversion-declarator.
14553 conversion-declarator:
14554 ptr-operator conversion-declarator [opt]
14558 static cp_declarator
*
14559 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14561 enum tree_code code
;
14562 tree class_type
, std_attributes
= NULL_TREE
;
14563 cp_cv_quals cv_quals
;
14565 /* We don't know if there's a ptr-operator next, or not. */
14566 cp_parser_parse_tentatively (parser
);
14567 /* Try the ptr-operator. */
14568 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14570 /* If it worked, look for more conversion-declarators. */
14571 if (cp_parser_parse_definitely (parser
))
14573 cp_declarator
*declarator
;
14575 /* Parse another optional declarator. */
14576 declarator
= cp_parser_conversion_declarator_opt (parser
);
14578 declarator
= cp_parser_make_indirect_declarator
14579 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14587 /* Parse an (optional) ctor-initializer.
14590 : mem-initializer-list */
14593 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14595 /* If the next token is not a `:', then there is no
14596 ctor-initializer. */
14597 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14599 /* Do default initialization of any bases and members. */
14600 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14601 finish_mem_initializers (NULL_TREE
);
14605 /* Consume the `:' token. */
14606 cp_lexer_consume_token (parser
->lexer
);
14607 /* And the mem-initializer-list. */
14608 cp_parser_mem_initializer_list (parser
);
14611 /* Parse a mem-initializer-list.
14613 mem-initializer-list:
14614 mem-initializer ... [opt]
14615 mem-initializer ... [opt] , mem-initializer-list */
14618 cp_parser_mem_initializer_list (cp_parser
* parser
)
14620 tree mem_initializer_list
= NULL_TREE
;
14621 tree target_ctor
= error_mark_node
;
14622 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14624 /* Let the semantic analysis code know that we are starting the
14625 mem-initializer-list. */
14626 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14627 error_at (token
->location
,
14628 "only constructors take member initializers");
14630 /* Loop through the list. */
14633 tree mem_initializer
;
14635 token
= cp_lexer_peek_token (parser
->lexer
);
14636 /* Parse the mem-initializer. */
14637 mem_initializer
= cp_parser_mem_initializer (parser
);
14638 /* If the next token is a `...', we're expanding member initializers. */
14639 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14641 || (mem_initializer
!= error_mark_node
14642 && check_for_bare_parameter_packs (TREE_PURPOSE
14643 (mem_initializer
))))
14645 /* Consume the `...'. */
14647 cp_lexer_consume_token (parser
->lexer
);
14649 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14650 can be expanded but members cannot. */
14651 if (mem_initializer
!= error_mark_node
14652 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14654 error_at (token
->location
,
14655 "cannot expand initializer for member %qD",
14656 TREE_PURPOSE (mem_initializer
));
14657 mem_initializer
= error_mark_node
;
14660 /* Construct the pack expansion type. */
14661 if (mem_initializer
!= error_mark_node
)
14662 mem_initializer
= make_pack_expansion (mem_initializer
);
14664 if (target_ctor
!= error_mark_node
14665 && mem_initializer
!= error_mark_node
)
14667 error ("mem-initializer for %qD follows constructor delegation",
14668 TREE_PURPOSE (mem_initializer
));
14669 mem_initializer
= error_mark_node
;
14671 /* Look for a target constructor. */
14672 if (mem_initializer
!= error_mark_node
14673 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14674 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14676 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14677 if (mem_initializer_list
)
14679 error ("constructor delegation follows mem-initializer for %qD",
14680 TREE_PURPOSE (mem_initializer_list
));
14681 mem_initializer
= error_mark_node
;
14683 target_ctor
= mem_initializer
;
14685 /* Add it to the list, unless it was erroneous. */
14686 if (mem_initializer
!= error_mark_node
)
14688 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14689 mem_initializer_list
= mem_initializer
;
14691 /* If the next token is not a `,', we're done. */
14692 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14694 /* Consume the `,' token. */
14695 cp_lexer_consume_token (parser
->lexer
);
14698 /* Perform semantic analysis. */
14699 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14700 finish_mem_initializers (mem_initializer_list
);
14703 /* Parse a mem-initializer.
14706 mem-initializer-id ( expression-list [opt] )
14707 mem-initializer-id braced-init-list
14712 ( expression-list [opt] )
14714 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14715 class) or FIELD_DECL (for a non-static data member) to initialize;
14716 the TREE_VALUE is the expression-list. An empty initialization
14717 list is represented by void_list_node. */
14720 cp_parser_mem_initializer (cp_parser
* parser
)
14722 tree mem_initializer_id
;
14723 tree expression_list
;
14725 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14727 /* Find out what is being initialized. */
14728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14730 permerror (token
->location
,
14731 "anachronistic old-style base class initializer");
14732 mem_initializer_id
= NULL_TREE
;
14736 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14737 if (mem_initializer_id
== error_mark_node
)
14738 return mem_initializer_id
;
14740 member
= expand_member_init (mem_initializer_id
);
14741 if (member
&& !DECL_P (member
))
14742 in_base_initializer
= 1;
14744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14746 bool expr_non_constant_p
;
14747 cp_lexer_set_source_position (parser
->lexer
);
14748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14749 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14750 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14751 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14755 vec
<tree
, va_gc
> *vec
;
14756 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14758 /*allow_expansion_p=*/true,
14759 /*non_constant_p=*/NULL
);
14761 return error_mark_node
;
14762 expression_list
= build_tree_list_vec (vec
);
14763 release_tree_vector (vec
);
14766 if (expression_list
== error_mark_node
)
14767 return error_mark_node
;
14768 if (!expression_list
)
14769 expression_list
= void_type_node
;
14771 in_base_initializer
= 0;
14773 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14776 /* Parse a mem-initializer-id.
14778 mem-initializer-id:
14779 :: [opt] nested-name-specifier [opt] class-name
14780 decltype-specifier (C++11)
14783 Returns a TYPE indicating the class to be initialized for the first
14784 production (and the second in C++11). Returns an IDENTIFIER_NODE
14785 indicating the data member to be initialized for the last production. */
14788 cp_parser_mem_initializer_id (cp_parser
* parser
)
14790 bool global_scope_p
;
14791 bool nested_name_specifier_p
;
14792 bool template_p
= false;
14795 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14797 /* `typename' is not allowed in this context ([temp.res]). */
14798 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14800 error_at (token
->location
,
14801 "keyword %<typename%> not allowed in this context (a qualified "
14802 "member initializer is implicitly a type)");
14803 cp_lexer_consume_token (parser
->lexer
);
14805 /* Look for the optional `::' operator. */
14807 = (cp_parser_global_scope_opt (parser
,
14808 /*current_scope_valid_p=*/false)
14810 /* Look for the optional nested-name-specifier. The simplest way to
14815 The keyword `typename' is not permitted in a base-specifier or
14816 mem-initializer; in these contexts a qualified name that
14817 depends on a template-parameter is implicitly assumed to be a
14820 is to assume that we have seen the `typename' keyword at this
14822 nested_name_specifier_p
14823 = (cp_parser_nested_name_specifier_opt (parser
,
14824 /*typename_keyword_p=*/true,
14825 /*check_dependency_p=*/true,
14827 /*is_declaration=*/true)
14829 if (nested_name_specifier_p
)
14830 template_p
= cp_parser_optional_template_keyword (parser
);
14831 /* If there is a `::' operator or a nested-name-specifier, then we
14832 are definitely looking for a class-name. */
14833 if (global_scope_p
|| nested_name_specifier_p
)
14834 return cp_parser_class_name (parser
,
14835 /*typename_keyword_p=*/true,
14836 /*template_keyword_p=*/template_p
,
14838 /*check_dependency_p=*/true,
14839 /*class_head_p=*/false,
14840 /*is_declaration=*/true);
14841 /* Otherwise, we could also be looking for an ordinary identifier. */
14842 cp_parser_parse_tentatively (parser
);
14843 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14844 /* Try a decltype-specifier. */
14845 id
= cp_parser_decltype (parser
);
14847 /* Otherwise, try a class-name. */
14848 id
= cp_parser_class_name (parser
,
14849 /*typename_keyword_p=*/true,
14850 /*template_keyword_p=*/false,
14852 /*check_dependency_p=*/true,
14853 /*class_head_p=*/false,
14854 /*is_declaration=*/true);
14855 /* If we found one, we're done. */
14856 if (cp_parser_parse_definitely (parser
))
14858 /* Otherwise, look for an ordinary identifier. */
14859 return cp_parser_identifier (parser
);
14862 /* Overloading [gram.over] */
14864 /* Parse an operator-function-id.
14866 operator-function-id:
14869 Returns an IDENTIFIER_NODE for the operator which is a
14870 human-readable spelling of the identifier, e.g., `operator +'. */
14873 cp_parser_operator_function_id (cp_parser
* parser
)
14875 /* Look for the `operator' keyword. */
14876 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14877 return error_mark_node
;
14878 /* And then the name of the operator itself. */
14879 return cp_parser_operator (parser
);
14882 /* Return an identifier node for a user-defined literal operator.
14883 The suffix identifier is chained to the operator name identifier. */
14886 cp_literal_operator_id (const char* name
)
14889 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14890 + strlen (name
) + 10);
14891 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14892 identifier
= get_identifier (buffer
);
14897 /* Parse an operator.
14900 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14901 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14902 || ++ -- , ->* -> () []
14909 Returns an IDENTIFIER_NODE for the operator which is a
14910 human-readable spelling of the identifier, e.g., `operator +'. */
14913 cp_parser_operator (cp_parser
* parser
)
14915 tree id
= NULL_TREE
;
14919 /* Peek at the next token. */
14920 token
= cp_lexer_peek_token (parser
->lexer
);
14922 location_t start_loc
= token
->location
;
14924 /* Figure out which operator we have. */
14925 enum tree_code op
= ERROR_MARK
;
14926 bool assop
= false;
14927 bool consumed
= false;
14928 switch (token
->type
)
14932 /* The keyword should be either `new' or `delete'. */
14933 if (token
->keyword
== RID_NEW
)
14935 else if (token
->keyword
== RID_DELETE
)
14940 /* Consume the `new' or `delete' token. */
14941 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14943 /* Peek at the next token. */
14944 token
= cp_lexer_peek_token (parser
->lexer
);
14945 /* If it's a `[' token then this is the array variant of the
14947 if (token
->type
== CPP_OPEN_SQUARE
)
14949 /* Consume the `[' token. */
14950 cp_lexer_consume_token (parser
->lexer
);
14951 /* Look for the `]' token. */
14952 if (cp_token
*close_token
14953 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14954 end_loc
= close_token
->location
;
14955 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14957 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14975 op
= TRUNC_DIV_EXPR
;
14979 op
= TRUNC_MOD_EXPR
;
14999 op
= TRUTH_NOT_EXPR
;
15032 op
= TRUNC_DIV_EXPR
;
15037 op
= TRUNC_MOD_EXPR
;
15063 case CPP_LSHIFT_EQ
:
15068 case CPP_RSHIFT_EQ
:
15085 case CPP_GREATER_EQ
:
15090 op
= TRUTH_ANDIF_EXPR
;
15094 op
= TRUTH_ORIF_EXPR
;
15097 case CPP_PLUS_PLUS
:
15098 op
= POSTINCREMENT_EXPR
;
15101 case CPP_MINUS_MINUS
:
15102 op
= PREDECREMENT_EXPR
;
15106 op
= COMPOUND_EXPR
;
15109 case CPP_DEREF_STAR
:
15114 op
= COMPONENT_REF
;
15117 case CPP_OPEN_PAREN
:
15119 /* Consume the `('. */
15120 matching_parens parens
;
15121 parens
.consume_open (parser
);
15122 /* Look for the matching `)'. */
15123 parens
.require_close (parser
);
15129 case CPP_OPEN_SQUARE
:
15130 /* Consume the `['. */
15131 cp_lexer_consume_token (parser
->lexer
);
15132 /* Look for the matching `]'. */
15133 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15138 case CPP_UTF8STRING
:
15139 case CPP_UTF8STRING_USERDEF
:
15146 case CPP_STRING_USERDEF
:
15147 case CPP_WSTRING_USERDEF
:
15148 case CPP_STRING16_USERDEF
:
15149 case CPP_STRING32_USERDEF
:
15151 tree str
, string_tree
;
15154 if (cxx_dialect
== cxx98
)
15155 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15157 /* Consume the string. */
15158 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15159 /*wide_ok=*/true, /*lookup_udlit=*/false);
15160 if (str
== error_mark_node
)
15161 return error_mark_node
;
15162 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15164 string_tree
= USERDEF_LITERAL_VALUE (str
);
15165 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15170 /* Look for the suffix identifier. */
15171 token
= cp_lexer_peek_token (parser
->lexer
);
15172 if (token
->type
== CPP_NAME
)
15173 id
= cp_parser_identifier (parser
);
15174 else if (token
->type
== CPP_KEYWORD
)
15176 error ("unexpected keyword;"
15177 " remove space between quotes and suffix identifier");
15178 return error_mark_node
;
15182 error ("expected suffix identifier");
15183 return error_mark_node
;
15186 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15187 (TREE_TYPE (TREE_TYPE (string_tree
))));
15188 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15191 error ("expected empty string after %<operator%> keyword");
15192 return error_mark_node
;
15194 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15197 error ("invalid encoding prefix in literal operator");
15198 return error_mark_node
;
15200 if (id
!= error_mark_node
)
15202 const char *name
= IDENTIFIER_POINTER (id
);
15203 id
= cp_literal_operator_id (name
);
15209 /* Anything else is an error. */
15213 /* If we have selected an identifier, we need to consume the
15215 if (op
!= ERROR_MARK
)
15217 id
= ovl_op_identifier (assop
, op
);
15219 cp_lexer_consume_token (parser
->lexer
);
15221 /* Otherwise, no valid operator name was present. */
15224 cp_parser_error (parser
, "expected operator");
15225 id
= error_mark_node
;
15228 return cp_expr (id
, start_loc
);
15231 /* Parse a template-declaration.
15233 template-declaration:
15234 export [opt] template < template-parameter-list > declaration
15236 If MEMBER_P is TRUE, this template-declaration occurs within a
15239 The grammar rule given by the standard isn't correct. What
15240 is really meant is:
15242 template-declaration:
15243 export [opt] template-parameter-list-seq
15244 decl-specifier-seq [opt] init-declarator [opt] ;
15245 export [opt] template-parameter-list-seq
15246 function-definition
15248 template-parameter-list-seq:
15249 template-parameter-list-seq [opt]
15250 template < template-parameter-list >
15252 Concept Extensions:
15254 template-parameter-list-seq:
15255 template < template-parameter-list > requires-clause [opt]
15258 requires logical-or-expression */
15261 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15263 /* Check for `export'. */
15264 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15266 /* Consume the `export' token. */
15267 cp_lexer_consume_token (parser
->lexer
);
15268 /* Warn that we do not support `export'. */
15269 warning (0, "keyword %<export%> not implemented, and will be ignored");
15272 cp_parser_template_declaration_after_export (parser
, member_p
);
15275 /* Parse a template-parameter-list.
15277 template-parameter-list:
15279 template-parameter-list , template-parameter
15281 Returns a TREE_LIST. Each node represents a template parameter.
15282 The nodes are connected via their TREE_CHAINs. */
15285 cp_parser_template_parameter_list (cp_parser
* parser
)
15287 tree parameter_list
= NULL_TREE
;
15289 begin_template_parm_list ();
15291 /* The loop below parses the template parms. We first need to know
15292 the total number of template parms to be able to compute proper
15293 canonical types of each dependent type. So after the loop, when
15294 we know the total number of template parms,
15295 end_template_parm_list computes the proper canonical types and
15296 fixes up the dependent types accordingly. */
15301 bool is_parameter_pack
;
15302 location_t parm_loc
;
15304 /* Parse the template-parameter. */
15305 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15306 parameter
= cp_parser_template_parameter (parser
,
15308 &is_parameter_pack
);
15309 /* Add it to the list. */
15310 if (parameter
!= error_mark_node
)
15311 parameter_list
= process_template_parm (parameter_list
,
15315 is_parameter_pack
);
15318 tree err_parm
= build_tree_list (parameter
, parameter
);
15319 parameter_list
= chainon (parameter_list
, err_parm
);
15322 /* If the next token is not a `,', we're done. */
15323 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15325 /* Otherwise, consume the `,' token. */
15326 cp_lexer_consume_token (parser
->lexer
);
15329 return end_template_parm_list (parameter_list
);
15332 /* Parse a introduction-list.
15335 introduced-parameter
15336 introduction-list , introduced-parameter
15338 introduced-parameter:
15339 ...[opt] identifier
15341 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15342 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15343 WILDCARD_DECL will also have DECL_NAME set and token location in
15344 DECL_SOURCE_LOCATION. */
15347 cp_parser_introduction_list (cp_parser
*parser
)
15349 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15353 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15355 cp_lexer_consume_token (parser
->lexer
);
15357 tree identifier
= cp_parser_identifier (parser
);
15358 if (identifier
== error_mark_node
)
15361 /* Build placeholder. */
15362 tree parm
= build_nt (WILDCARD_DECL
);
15363 DECL_SOURCE_LOCATION (parm
)
15364 = cp_lexer_peek_token (parser
->lexer
)->location
;
15365 DECL_NAME (parm
) = identifier
;
15366 WILDCARD_PACK_P (parm
) = is_pack
;
15367 vec_safe_push (introduction_vec
, parm
);
15369 /* If the next token is not a `,', we're done. */
15370 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15372 /* Otherwise, consume the `,' token. */
15373 cp_lexer_consume_token (parser
->lexer
);
15376 /* Convert the vec into a TREE_VEC. */
15377 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15380 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15381 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15383 release_tree_vector (introduction_vec
);
15384 return introduction_list
;
15387 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15388 is an abstract declarator. */
15390 static inline cp_declarator
*
15391 get_id_declarator (cp_declarator
*declarator
)
15393 cp_declarator
*d
= declarator
;
15394 while (d
&& d
->kind
!= cdk_id
)
15399 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15400 is an abstract declarator. */
15403 get_unqualified_id (cp_declarator
*declarator
)
15405 declarator
= get_id_declarator (declarator
);
15407 return declarator
->u
.id
.unqualified_name
;
15412 /* Returns true if DECL represents a constrained-parameter. */
15415 is_constrained_parameter (tree decl
)
15418 && TREE_CODE (decl
) == TYPE_DECL
15419 && CONSTRAINED_PARM_CONCEPT (decl
)
15420 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15423 /* Returns true if PARM declares a constrained-parameter. */
15426 is_constrained_parameter (cp_parameter_declarator
*parm
)
15428 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15431 /* Check that the type parameter is only a declarator-id, and that its
15432 type is not cv-qualified. */
15435 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15436 cp_parameter_declarator
*parm
)
15438 if (!parm
->declarator
)
15441 if (parm
->declarator
->kind
!= cdk_id
)
15443 cp_parser_error (parser
, "invalid constrained type parameter");
15447 /* Don't allow cv-qualified type parameters. */
15448 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15449 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15451 cp_parser_error (parser
, "cv-qualified type parameter");
15458 /* Finish parsing/processing a template type parameter and checking
15459 various restrictions. */
15462 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15464 cp_parameter_declarator
* parmdecl
)
15466 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15467 return finish_template_type_parm (class_type_node
, id
);
15469 return error_mark_node
;
15473 finish_constrained_template_template_parm (tree proto
, tree id
)
15475 /* FIXME: This should probably be copied, and we may need to adjust
15476 the template parameter depths. */
15477 tree saved_parms
= current_template_parms
;
15478 begin_template_parm_list ();
15479 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15480 end_template_parm_list ();
15482 tree parm
= finish_template_template_parm (class_type_node
, id
);
15483 current_template_parms
= saved_parms
;
15488 /* Finish parsing/processing a template template parameter by borrowing
15489 the template parameter list from the prototype parameter. */
15492 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15495 cp_parameter_declarator
*parmdecl
)
15497 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15498 return error_mark_node
;
15499 return finish_constrained_template_template_parm (proto
, id
);
15502 /* Create a new non-type template parameter from the given PARM
15506 constrained_non_type_template_parm (bool *is_non_type
,
15507 cp_parameter_declarator
*parm
)
15509 *is_non_type
= true;
15510 cp_declarator
*decl
= parm
->declarator
;
15511 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15512 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15513 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15516 /* Build a constrained template parameter based on the PARMDECL
15517 declarator. The type of PARMDECL is the constrained type, which
15518 refers to the prototype template parameter that ultimately
15519 specifies the type of the declared parameter. */
15522 finish_constrained_parameter (cp_parser
*parser
,
15523 cp_parameter_declarator
*parmdecl
,
15525 bool *is_parameter_pack
)
15527 tree decl
= parmdecl
->decl_specifiers
.type
;
15528 tree id
= get_unqualified_id (parmdecl
->declarator
);
15529 tree def
= parmdecl
->default_argument
;
15530 tree proto
= DECL_INITIAL (decl
);
15532 /* A template parameter constrained by a variadic concept shall also
15533 be declared as a template parameter pack. */
15534 bool is_variadic
= template_parameter_pack_p (proto
);
15535 if (is_variadic
&& !*is_parameter_pack
)
15536 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15538 /* Build the parameter. Return an error if the declarator was invalid. */
15540 if (TREE_CODE (proto
) == TYPE_DECL
)
15541 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15542 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15543 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15546 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15547 if (parm
== error_mark_node
)
15548 return error_mark_node
;
15550 /* Finish the parameter decl and create a node attaching the
15551 default argument and constraint. */
15552 parm
= build_tree_list (def
, parm
);
15553 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15558 /* Returns true if the parsed type actually represents the declaration
15559 of a type template-parameter. */
15562 declares_constrained_type_template_parameter (tree type
)
15564 return (is_constrained_parameter (type
)
15565 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15569 /* Returns true if the parsed type actually represents the declaration of
15570 a template template-parameter. */
15573 declares_constrained_template_template_parameter (tree type
)
15575 return (is_constrained_parameter (type
)
15576 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15579 /* Parse a default argument for a type template-parameter.
15580 Note that diagnostics are handled in cp_parser_template_parameter. */
15583 cp_parser_default_type_template_argument (cp_parser
*parser
)
15585 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15587 /* Consume the `=' token. */
15588 cp_lexer_consume_token (parser
->lexer
);
15590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15592 /* Parse the default-argument. */
15593 push_deferring_access_checks (dk_no_deferred
);
15594 tree default_argument
= cp_parser_type_id (parser
);
15595 pop_deferring_access_checks ();
15597 if (flag_concepts
&& type_uses_auto (default_argument
))
15599 error_at (token
->location
,
15600 "invalid use of %<auto%> in default template argument");
15601 return error_mark_node
;
15604 return default_argument
;
15607 /* Parse a default argument for a template template-parameter. */
15610 cp_parser_default_template_template_argument (cp_parser
*parser
)
15612 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15616 /* Consume the `='. */
15617 cp_lexer_consume_token (parser
->lexer
);
15618 /* Parse the id-expression. */
15619 push_deferring_access_checks (dk_no_deferred
);
15620 /* save token before parsing the id-expression, for error
15622 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15623 tree default_argument
15624 = cp_parser_id_expression (parser
,
15625 /*template_keyword_p=*/false,
15626 /*check_dependency_p=*/true,
15627 /*template_p=*/&is_template
,
15628 /*declarator_p=*/false,
15629 /*optional_p=*/false);
15630 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15631 /* If the id-expression was a template-id that refers to
15632 a template-class, we already have the declaration here,
15633 so no further lookup is needed. */
15636 /* Look up the name. */
15638 = cp_parser_lookup_name (parser
, default_argument
,
15640 /*is_template=*/is_template
,
15641 /*is_namespace=*/false,
15642 /*check_dependency=*/true,
15643 /*ambiguous_decls=*/NULL
,
15645 /* See if the default argument is valid. */
15646 default_argument
= check_template_template_default_arg (default_argument
);
15647 pop_deferring_access_checks ();
15648 return default_argument
;
15651 /* Parse a template-parameter.
15653 template-parameter:
15655 parameter-declaration
15657 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15658 the parameter. The TREE_PURPOSE is the default value, if any.
15659 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15660 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15661 set to true iff this parameter is a parameter pack. */
15664 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15665 bool *is_parameter_pack
)
15668 cp_parameter_declarator
*parameter_declarator
;
15671 /* Assume it is a type parameter or a template parameter. */
15672 *is_non_type
= false;
15673 /* Assume it not a parameter pack. */
15674 *is_parameter_pack
= false;
15675 /* Peek at the next token. */
15676 token
= cp_lexer_peek_token (parser
->lexer
);
15677 /* If it is `template', we have a type-parameter. */
15678 if (token
->keyword
== RID_TEMPLATE
)
15679 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15680 /* If it is `class' or `typename' we do not know yet whether it is a
15681 type parameter or a non-type parameter. Consider:
15683 template <typename T, typename T::X X> ...
15687 template <class C, class D*> ...
15689 Here, the first parameter is a type parameter, and the second is
15690 a non-type parameter. We can tell by looking at the token after
15691 the identifier -- if it is a `,', `=', or `>' then we have a type
15693 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15695 /* Peek at the token after `class' or `typename'. */
15696 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15697 /* If it's an ellipsis, we have a template type parameter
15699 if (token
->type
== CPP_ELLIPSIS
)
15700 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15701 /* If it's an identifier, skip it. */
15702 if (token
->type
== CPP_NAME
)
15703 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15704 /* Now, see if the token looks like the end of a template
15706 if (token
->type
== CPP_COMMA
15707 || token
->type
== CPP_EQ
15708 || token
->type
== CPP_GREATER
)
15709 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15712 /* Otherwise, it is a non-type parameter or a constrained parameter.
15716 When parsing a default template-argument for a non-type
15717 template-parameter, the first non-nested `>' is taken as the end
15718 of the template parameter-list rather than a greater-than
15720 parameter_declarator
15721 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15722 /*parenthesized_p=*/NULL
);
15724 if (!parameter_declarator
)
15725 return error_mark_node
;
15727 /* If the parameter declaration is marked as a parameter pack, set
15728 *IS_PARAMETER_PACK to notify the caller. */
15729 if (parameter_declarator
->template_parameter_pack_p
)
15730 *is_parameter_pack
= true;
15732 if (parameter_declarator
->default_argument
)
15734 /* Can happen in some cases of erroneous input (c++/34892). */
15735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15736 /* Consume the `...' for better error recovery. */
15737 cp_lexer_consume_token (parser
->lexer
);
15740 // The parameter may have been constrained.
15741 if (is_constrained_parameter (parameter_declarator
))
15742 return finish_constrained_parameter (parser
,
15743 parameter_declarator
,
15745 is_parameter_pack
);
15747 // Now we're sure that the parameter is a non-type parameter.
15748 *is_non_type
= true;
15750 parm
= grokdeclarator (parameter_declarator
->declarator
,
15751 ¶meter_declarator
->decl_specifiers
,
15752 TPARM
, /*initialized=*/0,
15753 /*attrlist=*/NULL
);
15754 if (parm
== error_mark_node
)
15755 return error_mark_node
;
15757 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15760 /* Parse a type-parameter.
15763 class identifier [opt]
15764 class identifier [opt] = type-id
15765 typename identifier [opt]
15766 typename identifier [opt] = type-id
15767 template < template-parameter-list > class identifier [opt]
15768 template < template-parameter-list > class identifier [opt]
15771 GNU Extension (variadic templates):
15774 class ... identifier [opt]
15775 typename ... identifier [opt]
15777 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15778 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15779 the declaration of the parameter.
15781 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15784 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15789 /* Look for a keyword to tell us what kind of parameter this is. */
15790 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15792 return error_mark_node
;
15794 switch (token
->keyword
)
15800 tree default_argument
;
15802 /* If the next token is an ellipsis, we have a template
15804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15806 /* Consume the `...' token. */
15807 cp_lexer_consume_token (parser
->lexer
);
15808 maybe_warn_variadic_templates ();
15810 *is_parameter_pack
= true;
15813 /* If the next token is an identifier, then it names the
15815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15816 identifier
= cp_parser_identifier (parser
);
15818 identifier
= NULL_TREE
;
15820 /* Create the parameter. */
15821 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15823 /* If the next token is an `=', we have a default argument. */
15824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15827 = cp_parser_default_type_template_argument (parser
);
15829 /* Template parameter packs cannot have default
15831 if (*is_parameter_pack
)
15834 error_at (token
->location
,
15835 "template parameter pack %qD cannot have a "
15836 "default argument", identifier
);
15838 error_at (token
->location
,
15839 "template parameter packs cannot have "
15840 "default arguments");
15841 default_argument
= NULL_TREE
;
15843 else if (check_for_bare_parameter_packs (default_argument
))
15844 default_argument
= error_mark_node
;
15847 default_argument
= NULL_TREE
;
15849 /* Create the combined representation of the parameter and the
15850 default argument. */
15851 parameter
= build_tree_list (default_argument
, parameter
);
15858 tree default_argument
;
15860 /* Look for the `<'. */
15861 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15862 /* Parse the template-parameter-list. */
15863 cp_parser_template_parameter_list (parser
);
15864 /* Look for the `>'. */
15865 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15867 // If template requirements are present, parse them.
15870 tree reqs
= get_shorthand_constraints (current_template_parms
);
15871 if (tree r
= cp_parser_requires_clause_opt (parser
))
15872 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15873 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15876 /* Look for the `class' or 'typename' keywords. */
15877 cp_parser_type_parameter_key (parser
);
15878 /* If the next token is an ellipsis, we have a template
15880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15882 /* Consume the `...' token. */
15883 cp_lexer_consume_token (parser
->lexer
);
15884 maybe_warn_variadic_templates ();
15886 *is_parameter_pack
= true;
15888 /* If the next token is an `=', then there is a
15889 default-argument. If the next token is a `>', we are at
15890 the end of the parameter-list. If the next token is a `,',
15891 then we are at the end of this parameter. */
15892 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15893 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15894 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15896 identifier
= cp_parser_identifier (parser
);
15897 /* Treat invalid names as if the parameter were nameless. */
15898 if (identifier
== error_mark_node
)
15899 identifier
= NULL_TREE
;
15902 identifier
= NULL_TREE
;
15904 /* Create the template parameter. */
15905 parameter
= finish_template_template_parm (class_type_node
,
15908 /* If the next token is an `=', then there is a
15909 default-argument. */
15910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15913 = cp_parser_default_template_template_argument (parser
);
15915 /* Template parameter packs cannot have default
15917 if (*is_parameter_pack
)
15920 error_at (token
->location
,
15921 "template parameter pack %qD cannot "
15922 "have a default argument",
15925 error_at (token
->location
, "template parameter packs cannot "
15926 "have default arguments");
15927 default_argument
= NULL_TREE
;
15931 default_argument
= NULL_TREE
;
15933 /* Create the combined representation of the parameter and the
15934 default argument. */
15935 parameter
= build_tree_list (default_argument
, parameter
);
15940 gcc_unreachable ();
15947 /* Parse a template-id.
15950 template-name < template-argument-list [opt] >
15952 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15953 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15954 returned. Otherwise, if the template-name names a function, or set
15955 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15956 names a class, returns a TYPE_DECL for the specialization.
15958 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15959 uninstantiated templates. */
15962 cp_parser_template_id (cp_parser
*parser
,
15963 bool template_keyword_p
,
15964 bool check_dependency_p
,
15965 enum tag_types tag_type
,
15966 bool is_declaration
)
15971 cp_token_position start_of_id
= 0;
15972 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15973 bool is_identifier
;
15975 /* If the next token corresponds to a template-id, there is no need
15977 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15978 if (token
->type
== CPP_TEMPLATE_ID
)
15980 cp_lexer_consume_token (parser
->lexer
);
15981 return saved_checks_value (token
->u
.tree_check_value
);
15984 /* Avoid performing name lookup if there is no possibility of
15985 finding a template-id. */
15986 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15987 || (token
->type
== CPP_NAME
15988 && !cp_parser_nth_token_starts_template_argument_list_p
15991 cp_parser_error (parser
, "expected template-id");
15992 return error_mark_node
;
15995 /* Remember where the template-id starts. */
15996 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15997 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15999 push_deferring_access_checks (dk_deferred
);
16001 /* Parse the template-name. */
16002 is_identifier
= false;
16003 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16004 check_dependency_p
,
16008 if (templ
== error_mark_node
|| is_identifier
)
16010 pop_deferring_access_checks ();
16014 /* Since we're going to preserve any side-effects from this parse, set up a
16015 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16016 in the template arguments. */
16017 tentative_firewall
firewall (parser
);
16019 /* If we find the sequence `[:' after a template-name, it's probably
16020 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16021 parse correctly the argument list. */
16022 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16023 == CPP_OPEN_SQUARE
)
16024 && next_token
->flags
& DIGRAPH
16025 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16027 && !(next_token_2
->flags
& PREV_WHITE
))
16029 cp_parser_parse_tentatively (parser
);
16030 /* Change `:' into `::'. */
16031 next_token_2
->type
= CPP_SCOPE
;
16032 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16034 cp_lexer_consume_token (parser
->lexer
);
16036 /* Parse the arguments. */
16037 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16038 if (!cp_parser_parse_definitely (parser
))
16040 /* If we couldn't parse an argument list, then we revert our changes
16041 and return simply an error. Maybe this is not a template-id
16043 next_token_2
->type
= CPP_COLON
;
16044 cp_parser_error (parser
, "expected %<<%>");
16045 pop_deferring_access_checks ();
16046 return error_mark_node
;
16048 /* Otherwise, emit an error about the invalid digraph, but continue
16049 parsing because we got our argument list. */
16050 if (permerror (next_token
->location
,
16051 "%<<::%> cannot begin a template-argument list"))
16053 static bool hint
= false;
16054 inform (next_token
->location
,
16055 "%<<:%> is an alternate spelling for %<[%>."
16056 " Insert whitespace between %<<%> and %<::%>");
16057 if (!hint
&& !flag_permissive
)
16059 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16060 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16061 "accept your code)");
16068 /* Look for the `<' that starts the template-argument-list. */
16069 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16071 pop_deferring_access_checks ();
16072 return error_mark_node
;
16074 /* Parse the arguments. */
16075 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16077 if ((cxx_dialect
> cxx17
)
16078 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16079 && !template_keyword_p
16080 && (cp_parser_error_occurred (parser
)
16081 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16083 /* This didn't go well. */
16084 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16086 /* C++2A says that "function-name < a;" is now ill-formed. */
16087 if (cp_parser_error_occurred (parser
))
16089 error_at (token
->location
, "invalid template-argument-list");
16090 inform (token
->location
, "function name as the left hand "
16091 "operand of %<<%> is ill-formed in C++2a; wrap the "
16092 "function name in %<()%>");
16095 /* We expect "f<targs>" to be followed by "(args)". */
16096 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16097 "expected %<(%> after template-argument-list");
16099 /* Purge all subsequent tokens. */
16100 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16103 cp_parser_simulate_error (parser
);
16104 pop_deferring_access_checks ();
16105 return error_mark_node
;
16109 /* Set the location to be of the form:
16110 template-name < template-argument-list [opt] >
16111 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16112 with caret == start at the start of the template-name,
16113 ranging until the closing '>'. */
16114 location_t finish_loc
16115 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16116 location_t combined_loc
16117 = make_location (token
->location
, token
->location
, finish_loc
);
16119 /* Check for concepts autos where they don't belong. We could
16120 identify types in some cases of idnetifier TEMPL, looking ahead
16121 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16122 types. We reject them in functions, but if what we have is an
16123 identifier, even with none_type we can't conclude it's NOT a
16124 type, we have to wait for template substitution. */
16125 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16126 template_id
= error_mark_node
;
16127 /* Build a representation of the specialization. */
16128 else if (identifier_p (templ
))
16129 template_id
= build_min_nt_loc (combined_loc
,
16132 else if (DECL_TYPE_TEMPLATE_P (templ
)
16133 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16135 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16136 template (rather than some instantiation thereof) only if
16137 is not nested within some other construct. For example, in
16138 "template <typename T> void f(T) { A<T>::", A<T> is just an
16139 instantiation of A. */
16140 bool entering_scope
16141 = (template_parm_scope_p ()
16142 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16144 = finish_template_type (templ
, arguments
, entering_scope
);
16146 /* A template-like identifier may be a partial concept id. */
16147 else if (flag_concepts
16148 && (template_id
= (cp_parser_maybe_partial_concept_id
16149 (parser
, templ
, arguments
))))
16150 return template_id
;
16151 else if (variable_template_p (templ
))
16153 template_id
= lookup_template_variable (templ
, arguments
);
16154 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16155 SET_EXPR_LOCATION (template_id
, combined_loc
);
16159 /* If it's not a class-template or a template-template, it should be
16160 a function-template. */
16161 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16162 || TREE_CODE (templ
) == OVERLOAD
16163 || TREE_CODE (templ
) == FUNCTION_DECL
16164 || BASELINK_P (templ
)));
16166 template_id
= lookup_template_function (templ
, arguments
);
16167 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16168 SET_EXPR_LOCATION (template_id
, combined_loc
);
16171 /* If parsing tentatively, replace the sequence of tokens that makes
16172 up the template-id with a CPP_TEMPLATE_ID token. That way,
16173 should we re-parse the token stream, we will not have to repeat
16174 the effort required to do the parse, nor will we issue duplicate
16175 error messages about problems during instantiation of the
16178 /* Don't do this if we had a parse error in a declarator; re-parsing
16179 might succeed if a name changes meaning (60361). */
16180 && !(cp_parser_error_occurred (parser
)
16181 && cp_parser_parsing_tentatively (parser
)
16182 && parser
->in_declarator_p
))
16184 /* Reset the contents of the START_OF_ID token. */
16185 token
->type
= CPP_TEMPLATE_ID
;
16186 token
->location
= combined_loc
;
16188 /* Retrieve any deferred checks. Do not pop this access checks yet
16189 so the memory will not be reclaimed during token replacing below. */
16190 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16191 token
->u
.tree_check_value
->value
= template_id
;
16192 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16193 token
->keyword
= RID_MAX
;
16195 /* Purge all subsequent tokens. */
16196 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16198 /* ??? Can we actually assume that, if template_id ==
16199 error_mark_node, we will have issued a diagnostic to the
16200 user, as opposed to simply marking the tentative parse as
16202 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16203 error_at (token
->location
, "parse error in template argument list");
16206 pop_to_parent_deferring_access_checks ();
16207 return template_id
;
16210 /* Parse a template-name.
16215 The standard should actually say:
16219 operator-function-id
16221 A defect report has been filed about this issue.
16223 A conversion-function-id cannot be a template name because they cannot
16224 be part of a template-id. In fact, looking at this code:
16226 a.operator K<int>()
16228 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16229 It is impossible to call a templated conversion-function-id with an
16230 explicit argument list, since the only allowed template parameter is
16231 the type to which it is converting.
16233 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16234 `template' keyword, in a construction like:
16238 In that case `f' is taken to be a template-name, even though there
16239 is no way of knowing for sure.
16241 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16242 name refers to a set of overloaded functions, at least one of which
16243 is a template, or an IDENTIFIER_NODE with the name of the template,
16244 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16245 names are looked up inside uninstantiated templates. */
16248 cp_parser_template_name (cp_parser
* parser
,
16249 bool template_keyword_p
,
16250 bool check_dependency_p
,
16251 bool is_declaration
,
16252 enum tag_types tag_type
,
16253 bool *is_identifier
)
16257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16259 /* If the next token is `operator', then we have either an
16260 operator-function-id or a conversion-function-id. */
16261 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16263 /* We don't know whether we're looking at an
16264 operator-function-id or a conversion-function-id. */
16265 cp_parser_parse_tentatively (parser
);
16266 /* Try an operator-function-id. */
16267 identifier
= cp_parser_operator_function_id (parser
);
16268 /* If that didn't work, try a conversion-function-id. */
16269 if (!cp_parser_parse_definitely (parser
))
16271 cp_parser_error (parser
, "expected template-name");
16272 return error_mark_node
;
16275 /* Look for the identifier. */
16277 identifier
= cp_parser_identifier (parser
);
16279 /* If we didn't find an identifier, we don't have a template-id. */
16280 if (identifier
== error_mark_node
)
16281 return error_mark_node
;
16283 /* If the name immediately followed the `template' keyword, then it
16284 is a template-name. However, if the next token is not `<', then
16285 we do not treat it as a template-name, since it is not being used
16286 as part of a template-id. This enables us to handle constructs
16289 template <typename T> struct S { S(); };
16290 template <typename T> S<T>::S();
16292 correctly. We would treat `S' as a template -- if it were `S<T>'
16293 -- but we do not if there is no `<'. */
16295 if (processing_template_decl
16296 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16298 /* In a declaration, in a dependent context, we pretend that the
16299 "template" keyword was present in order to improve error
16300 recovery. For example, given:
16302 template <typename T> void f(T::X<int>);
16304 we want to treat "X<int>" as a template-id. */
16306 && !template_keyword_p
16307 && parser
->scope
&& TYPE_P (parser
->scope
)
16308 && check_dependency_p
16309 && dependent_scope_p (parser
->scope
)
16310 /* Do not do this for dtors (or ctors), since they never
16311 need the template keyword before their name. */
16312 && !constructor_name_p (identifier
, parser
->scope
))
16314 cp_token_position start
= 0;
16316 /* Explain what went wrong. */
16317 error_at (token
->location
, "non-template %qD used as template",
16319 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16320 parser
->scope
, identifier
);
16321 /* If parsing tentatively, find the location of the "<" token. */
16322 if (cp_parser_simulate_error (parser
))
16323 start
= cp_lexer_token_position (parser
->lexer
, true);
16324 /* Parse the template arguments so that we can issue error
16325 messages about them. */
16326 cp_lexer_consume_token (parser
->lexer
);
16327 cp_parser_enclosed_template_argument_list (parser
);
16328 /* Skip tokens until we find a good place from which to
16329 continue parsing. */
16330 cp_parser_skip_to_closing_parenthesis (parser
,
16331 /*recovering=*/true,
16333 /*consume_paren=*/false);
16334 /* If parsing tentatively, permanently remove the
16335 template argument list. That will prevent duplicate
16336 error messages from being issued about the missing
16337 "template" keyword. */
16339 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16341 *is_identifier
= true;
16342 parser
->context
->object_type
= NULL_TREE
;
16346 /* If the "template" keyword is present, then there is generally
16347 no point in doing name-lookup, so we just return IDENTIFIER.
16348 But, if the qualifying scope is non-dependent then we can
16349 (and must) do name-lookup normally. */
16350 if (template_keyword_p
)
16352 tree scope
= (parser
->scope
? parser
->scope
16353 : parser
->context
->object_type
);
16354 if (scope
&& TYPE_P (scope
)
16355 && (!CLASS_TYPE_P (scope
)
16356 || (check_dependency_p
&& dependent_type_p (scope
))))
16358 /* We're optimizing away the call to cp_parser_lookup_name, but
16359 we still need to do this. */
16360 parser
->context
->object_type
= NULL_TREE
;
16366 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16367 const bool scoped_p
= ((parser
->scope
? parser
->scope
16368 : parser
->context
->object_type
) != NULL_TREE
);
16370 /* Look up the name. */
16371 decl
= cp_parser_lookup_name (parser
, identifier
,
16373 /*is_template=*/true,
16374 /*is_namespace=*/false,
16375 check_dependency_p
,
16376 /*ambiguous_decls=*/NULL
,
16379 decl
= strip_using_decl (decl
);
16381 /* If DECL is a template, then the name was a template-name. */
16382 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16384 if (TREE_DEPRECATED (decl
)
16385 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16386 warn_deprecated_use (decl
, NULL_TREE
);
16390 /* The standard does not explicitly indicate whether a name that
16391 names a set of overloaded declarations, some of which are
16392 templates, is a template-name. However, such a name should
16393 be a template-name; otherwise, there is no way to form a
16394 template-id for the overloaded templates. */
16395 bool found
= false;
16397 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16398 !found
&& iter
; ++iter
)
16399 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16403 && (cxx_dialect
> cxx17
)
16405 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
16407 /* [temp.names] says "A name is also considered to refer to a template
16408 if it is an unqualified-id followed by a < and name lookup finds
16409 either one or more functions or finds nothing." */
16411 /* The "more functions" case. Just use the OVERLOAD as normally.
16412 We don't use is_overloaded_fn here to avoid considering
16414 if (TREE_CODE (decl
) == OVERLOAD
16415 /* Name lookup found one function. */
16416 || TREE_CODE (decl
) == FUNCTION_DECL
)
16418 /* Name lookup found nothing. */
16419 else if (decl
== error_mark_node
)
16425 /* The name does not name a template. */
16426 cp_parser_error (parser
, "expected template-name");
16427 return error_mark_node
;
16434 /* Parse a template-argument-list.
16436 template-argument-list:
16437 template-argument ... [opt]
16438 template-argument-list , template-argument ... [opt]
16440 Returns a TREE_VEC containing the arguments. */
16443 cp_parser_template_argument_list (cp_parser
* parser
)
16445 tree fixed_args
[10];
16446 unsigned n_args
= 0;
16447 unsigned alloced
= 10;
16448 tree
*arg_ary
= fixed_args
;
16450 bool saved_in_template_argument_list_p
;
16452 bool saved_non_ice_p
;
16454 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16455 parser
->in_template_argument_list_p
= true;
16456 /* Even if the template-id appears in an integral
16457 constant-expression, the contents of the argument list do
16459 saved_ice_p
= parser
->integral_constant_expression_p
;
16460 parser
->integral_constant_expression_p
= false;
16461 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16462 parser
->non_integral_constant_expression_p
= false;
16464 /* Parse the arguments. */
16470 /* Consume the comma. */
16471 cp_lexer_consume_token (parser
->lexer
);
16473 /* Parse the template-argument. */
16474 argument
= cp_parser_template_argument (parser
);
16476 /* If the next token is an ellipsis, we're expanding a template
16478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16480 if (argument
== error_mark_node
)
16482 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16483 error_at (token
->location
,
16484 "expected parameter pack before %<...%>");
16486 /* Consume the `...' token. */
16487 cp_lexer_consume_token (parser
->lexer
);
16489 /* Make the argument into a TYPE_PACK_EXPANSION or
16490 EXPR_PACK_EXPANSION. */
16491 argument
= make_pack_expansion (argument
);
16494 if (n_args
== alloced
)
16498 if (arg_ary
== fixed_args
)
16500 arg_ary
= XNEWVEC (tree
, alloced
);
16501 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16504 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16506 arg_ary
[n_args
++] = argument
;
16508 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16510 vec
= make_tree_vec (n_args
);
16513 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16515 if (arg_ary
!= fixed_args
)
16517 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16518 parser
->integral_constant_expression_p
= saved_ice_p
;
16519 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16521 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16525 /* Parse a template-argument.
16528 assignment-expression
16532 The representation is that of an assignment-expression, type-id, or
16533 id-expression -- except that the qualified id-expression is
16534 evaluated, so that the value returned is either a DECL or an
16537 Although the standard says "assignment-expression", it forbids
16538 throw-expressions or assignments in the template argument.
16539 Therefore, we use "conditional-expression" instead. */
16542 cp_parser_template_argument (cp_parser
* parser
)
16547 bool maybe_type_id
= false;
16548 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16549 location_t loc
= 0;
16552 /* There's really no way to know what we're looking at, so we just
16553 try each alternative in order.
16557 In a template-argument, an ambiguity between a type-id and an
16558 expression is resolved to a type-id, regardless of the form of
16559 the corresponding template-parameter.
16561 Therefore, we try a type-id first. */
16562 cp_parser_parse_tentatively (parser
);
16563 argument
= cp_parser_template_type_arg (parser
);
16564 /* If there was no error parsing the type-id but the next token is a
16565 '>>', our behavior depends on which dialect of C++ we're
16566 parsing. In C++98, we probably found a typo for '> >'. But there
16567 are type-id which are also valid expressions. For instance:
16569 struct X { int operator >> (int); };
16570 template <int V> struct Foo {};
16573 Here 'X()' is a valid type-id of a function type, but the user just
16574 wanted to write the expression "X() >> 5". Thus, we remember that we
16575 found a valid type-id, but we still try to parse the argument as an
16576 expression to see what happens.
16578 In C++0x, the '>>' will be considered two separate '>'
16580 if (!cp_parser_error_occurred (parser
)
16581 && cxx_dialect
== cxx98
16582 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16584 maybe_type_id
= true;
16585 cp_parser_abort_tentative_parse (parser
);
16589 /* If the next token isn't a `,' or a `>', then this argument wasn't
16590 really finished. This means that the argument is not a valid
16592 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16593 cp_parser_error (parser
, "expected template-argument");
16594 /* If that worked, we're done. */
16595 if (cp_parser_parse_definitely (parser
))
16598 /* We're still not sure what the argument will be. */
16599 cp_parser_parse_tentatively (parser
);
16600 /* Try a template. */
16601 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16602 argument
= cp_parser_id_expression (parser
,
16603 /*template_keyword_p=*/false,
16604 /*check_dependency_p=*/true,
16606 /*declarator_p=*/false,
16607 /*optional_p=*/false);
16608 /* If the next token isn't a `,' or a `>', then this argument wasn't
16609 really finished. */
16610 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16611 cp_parser_error (parser
, "expected template-argument");
16612 if (!cp_parser_error_occurred (parser
))
16614 /* Figure out what is being referred to. If the id-expression
16615 was for a class template specialization, then we will have a
16616 TYPE_DECL at this point. There is no need to do name lookup
16617 at this point in that case. */
16618 if (TREE_CODE (argument
) != TYPE_DECL
)
16619 argument
= cp_parser_lookup_name (parser
, argument
,
16621 /*is_template=*/template_p
,
16622 /*is_namespace=*/false,
16623 /*check_dependency=*/true,
16624 /*ambiguous_decls=*/NULL
,
16625 argument_start_token
->location
);
16626 /* Handle a constrained-type-specifier for a non-type template
16628 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16630 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16631 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16632 cp_parser_error (parser
, "expected template-name");
16634 if (cp_parser_parse_definitely (parser
))
16636 if (TREE_DEPRECATED (argument
))
16637 warn_deprecated_use (argument
, NULL_TREE
);
16640 /* It must be a non-type argument. In C++17 any constant-expression is
16642 if (cxx_dialect
> cxx14
)
16645 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16647 -- an integral constant-expression of integral or enumeration
16650 -- the name of a non-type template-parameter; or
16652 -- the name of an object or function with external linkage...
16654 -- the address of an object or function with external linkage...
16656 -- a pointer to member... */
16657 /* Look for a non-type template parameter. */
16658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16660 cp_parser_parse_tentatively (parser
);
16661 argument
= cp_parser_primary_expression (parser
,
16662 /*address_p=*/false,
16664 /*template_arg_p=*/true,
16666 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16667 || !cp_parser_next_token_ends_template_argument_p (parser
))
16668 cp_parser_simulate_error (parser
);
16669 if (cp_parser_parse_definitely (parser
))
16673 /* If the next token is "&", the argument must be the address of an
16674 object or function with external linkage. */
16675 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16678 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16679 cp_lexer_consume_token (parser
->lexer
);
16681 /* See if we might have an id-expression. */
16682 token
= cp_lexer_peek_token (parser
->lexer
);
16683 if (token
->type
== CPP_NAME
16684 || token
->keyword
== RID_OPERATOR
16685 || token
->type
== CPP_SCOPE
16686 || token
->type
== CPP_TEMPLATE_ID
16687 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16689 cp_parser_parse_tentatively (parser
);
16690 argument
= cp_parser_primary_expression (parser
,
16693 /*template_arg_p=*/true,
16695 if (cp_parser_error_occurred (parser
)
16696 || !cp_parser_next_token_ends_template_argument_p (parser
))
16697 cp_parser_abort_tentative_parse (parser
);
16702 if (INDIRECT_REF_P (argument
))
16704 /* Strip the dereference temporarily. */
16705 gcc_assert (REFERENCE_REF_P (argument
));
16706 argument
= TREE_OPERAND (argument
, 0);
16709 /* If we're in a template, we represent a qualified-id referring
16710 to a static data member as a SCOPE_REF even if the scope isn't
16711 dependent so that we can check access control later. */
16713 if (TREE_CODE (probe
) == SCOPE_REF
)
16714 probe
= TREE_OPERAND (probe
, 1);
16717 /* A variable without external linkage might still be a
16718 valid constant-expression, so no error is issued here
16719 if the external-linkage check fails. */
16720 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16721 cp_parser_simulate_error (parser
);
16723 else if (is_overloaded_fn (argument
))
16724 /* All overloaded functions are allowed; if the external
16725 linkage test does not pass, an error will be issued
16729 && (TREE_CODE (argument
) == OFFSET_REF
16730 || TREE_CODE (argument
) == SCOPE_REF
))
16731 /* A pointer-to-member. */
16733 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16736 cp_parser_simulate_error (parser
);
16738 if (cp_parser_parse_definitely (parser
))
16741 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16742 tf_warning_or_error
);
16744 argument
= convert_from_reference (argument
);
16749 /* If the argument started with "&", there are no other valid
16750 alternatives at this point. */
16753 cp_parser_error (parser
, "invalid non-type template argument");
16754 return error_mark_node
;
16758 /* If the argument wasn't successfully parsed as a type-id followed
16759 by '>>', the argument can only be a constant expression now.
16760 Otherwise, we try parsing the constant-expression tentatively,
16761 because the argument could really be a type-id. */
16763 cp_parser_parse_tentatively (parser
);
16765 if (cxx_dialect
<= cxx14
)
16766 argument
= cp_parser_constant_expression (parser
);
16769 /* With C++17 generalized non-type template arguments we need to handle
16770 lvalue constant expressions, too. */
16771 argument
= cp_parser_assignment_expression (parser
);
16772 require_potential_constant_expression (argument
);
16775 if (!maybe_type_id
)
16777 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16778 cp_parser_error (parser
, "expected template-argument");
16779 if (cp_parser_parse_definitely (parser
))
16781 /* We did our best to parse the argument as a non type-id, but that
16782 was the only alternative that matched (albeit with a '>' after
16783 it). We can assume it's just a typo from the user, and a
16784 diagnostic will then be issued. */
16785 return cp_parser_template_type_arg (parser
);
16788 /* Parse an explicit-instantiation.
16790 explicit-instantiation:
16791 template declaration
16793 Although the standard says `declaration', what it really means is:
16795 explicit-instantiation:
16796 template decl-specifier-seq [opt] declarator [opt] ;
16798 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16799 supposed to be allowed. A defect report has been filed about this
16804 explicit-instantiation:
16805 storage-class-specifier template
16806 decl-specifier-seq [opt] declarator [opt] ;
16807 function-specifier template
16808 decl-specifier-seq [opt] declarator [opt] ; */
16811 cp_parser_explicit_instantiation (cp_parser
* parser
)
16813 int declares_class_or_enum
;
16814 cp_decl_specifier_seq decl_specifiers
;
16815 tree extension_specifier
= NULL_TREE
;
16817 timevar_push (TV_TEMPLATE_INST
);
16819 /* Look for an (optional) storage-class-specifier or
16820 function-specifier. */
16821 if (cp_parser_allow_gnu_extensions_p (parser
))
16823 extension_specifier
16824 = cp_parser_storage_class_specifier_opt (parser
);
16825 if (!extension_specifier
)
16826 extension_specifier
16827 = cp_parser_function_specifier_opt (parser
,
16828 /*decl_specs=*/NULL
);
16831 /* Look for the `template' keyword. */
16832 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16833 /* Let the front end know that we are processing an explicit
16835 begin_explicit_instantiation ();
16836 /* [temp.explicit] says that we are supposed to ignore access
16837 control while processing explicit instantiation directives. */
16838 push_deferring_access_checks (dk_no_check
);
16839 /* Parse a decl-specifier-seq. */
16840 cp_parser_decl_specifier_seq (parser
,
16841 CP_PARSER_FLAGS_OPTIONAL
,
16843 &declares_class_or_enum
);
16844 /* If there was exactly one decl-specifier, and it declared a class,
16845 and there's no declarator, then we have an explicit type
16847 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16851 type
= check_tag_decl (&decl_specifiers
,
16852 /*explicit_type_instantiation_p=*/true);
16853 /* Turn access control back on for names used during
16854 template instantiation. */
16855 pop_deferring_access_checks ();
16857 do_type_instantiation (type
, extension_specifier
,
16858 /*complain=*/tf_error
);
16862 cp_declarator
*declarator
;
16865 /* Parse the declarator. */
16867 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16868 /*ctor_dtor_or_conv_p=*/NULL
,
16869 /*parenthesized_p=*/NULL
,
16870 /*member_p=*/false,
16871 /*friend_p=*/false);
16872 if (declares_class_or_enum
& 2)
16873 cp_parser_check_for_definition_in_return_type (declarator
,
16874 decl_specifiers
.type
,
16875 decl_specifiers
.locations
[ds_type_spec
]);
16876 if (declarator
!= cp_error_declarator
)
16878 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16879 permerror (decl_specifiers
.locations
[ds_inline
],
16880 "explicit instantiation shall not use"
16881 " %<inline%> specifier");
16882 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16883 permerror (decl_specifiers
.locations
[ds_constexpr
],
16884 "explicit instantiation shall not use"
16885 " %<constexpr%> specifier");
16887 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16888 NORMAL
, 0, &decl_specifiers
.attributes
);
16889 /* Turn access control back on for names used during
16890 template instantiation. */
16891 pop_deferring_access_checks ();
16892 /* Do the explicit instantiation. */
16893 do_decl_instantiation (decl
, extension_specifier
);
16897 pop_deferring_access_checks ();
16898 /* Skip the body of the explicit instantiation. */
16899 cp_parser_skip_to_end_of_statement (parser
);
16902 /* We're done with the instantiation. */
16903 end_explicit_instantiation ();
16905 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16907 timevar_pop (TV_TEMPLATE_INST
);
16910 /* Parse an explicit-specialization.
16912 explicit-specialization:
16913 template < > declaration
16915 Although the standard says `declaration', what it really means is:
16917 explicit-specialization:
16918 template <> decl-specifier [opt] init-declarator [opt] ;
16919 template <> function-definition
16920 template <> explicit-specialization
16921 template <> template-declaration */
16924 cp_parser_explicit_specialization (cp_parser
* parser
)
16926 bool need_lang_pop
;
16927 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16929 /* Look for the `template' keyword. */
16930 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16931 /* Look for the `<'. */
16932 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16933 /* Look for the `>'. */
16934 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16935 /* We have processed another parameter list. */
16936 ++parser
->num_template_parameter_lists
;
16939 A template ... explicit specialization ... shall not have C
16941 if (current_lang_name
== lang_name_c
)
16943 error_at (token
->location
, "template specialization with C linkage");
16944 maybe_show_extern_c_location ();
16945 /* Give it C++ linkage to avoid confusing other parts of the
16947 push_lang_context (lang_name_cplusplus
);
16948 need_lang_pop
= true;
16951 need_lang_pop
= false;
16952 /* Let the front end know that we are beginning a specialization. */
16953 if (!begin_specialization ())
16955 end_specialization ();
16959 /* If the next keyword is `template', we need to figure out whether
16960 or not we're looking a template-declaration. */
16961 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16963 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16964 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16965 cp_parser_template_declaration_after_export (parser
,
16966 /*member_p=*/false);
16968 cp_parser_explicit_specialization (parser
);
16971 /* Parse the dependent declaration. */
16972 cp_parser_single_declaration (parser
,
16974 /*member_p=*/false,
16975 /*explicit_specialization_p=*/true,
16976 /*friend_p=*/NULL
);
16977 /* We're done with the specialization. */
16978 end_specialization ();
16979 /* For the erroneous case of a template with C linkage, we pushed an
16980 implicit C++ linkage scope; exit that scope now. */
16982 pop_lang_context ();
16983 /* We're done with this parameter list. */
16984 --parser
->num_template_parameter_lists
;
16987 /* Parse a type-specifier.
16990 simple-type-specifier
16993 elaborated-type-specifier
17001 Returns a representation of the type-specifier. For a
17002 class-specifier, enum-specifier, or elaborated-type-specifier, a
17003 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17005 The parser flags FLAGS is used to control type-specifier parsing.
17007 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17008 in a decl-specifier-seq.
17010 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17011 class-specifier, enum-specifier, or elaborated-type-specifier, then
17012 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17013 if a type is declared; 2 if it is defined. Otherwise, it is set to
17016 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17017 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17018 is set to FALSE. */
17021 cp_parser_type_specifier (cp_parser
* parser
,
17022 cp_parser_flags flags
,
17023 cp_decl_specifier_seq
*decl_specs
,
17024 bool is_declaration
,
17025 int* declares_class_or_enum
,
17026 bool* is_cv_qualifier
)
17028 tree type_spec
= NULL_TREE
;
17031 cp_decl_spec ds
= ds_last
;
17033 /* Assume this type-specifier does not declare a new type. */
17034 if (declares_class_or_enum
)
17035 *declares_class_or_enum
= 0;
17036 /* And that it does not specify a cv-qualifier. */
17037 if (is_cv_qualifier
)
17038 *is_cv_qualifier
= false;
17039 /* Peek at the next token. */
17040 token
= cp_lexer_peek_token (parser
->lexer
);
17042 /* If we're looking at a keyword, we can use that to guide the
17043 production we choose. */
17044 keyword
= token
->keyword
;
17048 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17049 goto elaborated_type_specifier
;
17051 /* Look for the enum-specifier. */
17052 type_spec
= cp_parser_enum_specifier (parser
);
17053 /* If that worked, we're done. */
17056 if (declares_class_or_enum
)
17057 *declares_class_or_enum
= 2;
17059 cp_parser_set_decl_spec_type (decl_specs
,
17062 /*type_definition_p=*/true);
17066 goto elaborated_type_specifier
;
17068 /* Any of these indicate either a class-specifier, or an
17069 elaborated-type-specifier. */
17073 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17074 goto elaborated_type_specifier
;
17076 /* Parse tentatively so that we can back up if we don't find a
17077 class-specifier. */
17078 cp_parser_parse_tentatively (parser
);
17079 /* Look for the class-specifier. */
17080 type_spec
= cp_parser_class_specifier (parser
);
17081 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17082 /* If that worked, we're done. */
17083 if (cp_parser_parse_definitely (parser
))
17085 if (declares_class_or_enum
)
17086 *declares_class_or_enum
= 2;
17088 cp_parser_set_decl_spec_type (decl_specs
,
17091 /*type_definition_p=*/true);
17095 /* Fall through. */
17096 elaborated_type_specifier
:
17097 /* We're declaring (not defining) a class or enum. */
17098 if (declares_class_or_enum
)
17099 *declares_class_or_enum
= 1;
17101 /* Fall through. */
17103 /* Look for an elaborated-type-specifier. */
17105 = (cp_parser_elaborated_type_specifier
17107 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17110 cp_parser_set_decl_spec_type (decl_specs
,
17113 /*type_definition_p=*/false);
17118 if (is_cv_qualifier
)
17119 *is_cv_qualifier
= true;
17124 if (is_cv_qualifier
)
17125 *is_cv_qualifier
= true;
17130 if (is_cv_qualifier
)
17131 *is_cv_qualifier
= true;
17135 /* The `__complex__' keyword is a GNU extension. */
17143 /* Handle simple keywords. */
17148 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17149 decl_specs
->any_specifiers_p
= true;
17151 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17154 /* If we do not already have a type-specifier, assume we are looking
17155 at a simple-type-specifier. */
17156 type_spec
= cp_parser_simple_type_specifier (parser
,
17160 /* If we didn't find a type-specifier, and a type-specifier was not
17161 optional in this context, issue an error message. */
17162 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17164 cp_parser_error (parser
, "expected type specifier");
17165 return error_mark_node
;
17171 /* Parse a simple-type-specifier.
17173 simple-type-specifier:
17174 :: [opt] nested-name-specifier [opt] type-name
17175 :: [opt] nested-name-specifier template template-id
17190 simple-type-specifier:
17192 decltype ( expression )
17195 __underlying_type ( type-id )
17199 nested-name-specifier(opt) template-name
17203 simple-type-specifier:
17205 __typeof__ unary-expression
17206 __typeof__ ( type-id )
17207 __typeof__ ( type-id ) { initializer-list , [opt] }
17209 Concepts Extension:
17211 simple-type-specifier:
17212 constrained-type-specifier
17214 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17215 appropriately updated. */
17218 cp_parser_simple_type_specifier (cp_parser
* parser
,
17219 cp_decl_specifier_seq
*decl_specs
,
17220 cp_parser_flags flags
)
17222 tree type
= NULL_TREE
;
17226 /* Peek at the next token. */
17227 token
= cp_lexer_peek_token (parser
->lexer
);
17229 /* If we're looking at a keyword, things are easy. */
17230 switch (token
->keyword
)
17234 decl_specs
->explicit_char_p
= true;
17235 type
= char_type_node
;
17238 type
= char16_type_node
;
17241 type
= char32_type_node
;
17244 type
= wchar_type_node
;
17247 type
= boolean_type_node
;
17250 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17251 type
= short_integer_type_node
;
17255 decl_specs
->explicit_int_p
= true;
17256 type
= integer_type_node
;
17262 idx
= token
->keyword
- RID_INT_N_0
;
17263 if (! int_n_enabled_p
[idx
])
17267 decl_specs
->explicit_intN_p
= true;
17268 decl_specs
->int_n_idx
= idx
;
17270 type
= int_n_trees
[idx
].signed_type
;
17274 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17275 type
= long_integer_type_node
;
17278 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17279 type
= integer_type_node
;
17282 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17283 type
= unsigned_type_node
;
17286 type
= float_type_node
;
17289 type
= double_type_node
;
17292 type
= void_type_node
;
17296 maybe_warn_cpp0x (CPP0X_AUTO
);
17297 if (parser
->auto_is_implicit_function_template_parm_p
)
17299 /* The 'auto' might be the placeholder return type for a function decl
17300 with trailing return type. */
17301 bool have_trailing_return_fn_decl
= false;
17303 cp_parser_parse_tentatively (parser
);
17304 cp_lexer_consume_token (parser
->lexer
);
17305 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17306 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17307 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17308 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17312 cp_lexer_consume_token (parser
->lexer
);
17313 cp_parser_skip_to_closing_parenthesis (parser
,
17314 /*recovering*/false,
17316 /*consume_paren*/true);
17320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17322 have_trailing_return_fn_decl
= true;
17326 cp_lexer_consume_token (parser
->lexer
);
17328 cp_parser_abort_tentative_parse (parser
);
17330 if (have_trailing_return_fn_decl
)
17332 type
= make_auto ();
17336 if (cxx_dialect
>= cxx14
)
17338 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17339 type
= TREE_TYPE (type
);
17342 type
= error_mark_node
;
17344 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17346 if (cxx_dialect
< cxx14
)
17347 error_at (token
->location
,
17348 "use of %<auto%> in lambda parameter declaration "
17349 "only available with "
17350 "-std=c++14 or -std=gnu++14");
17352 else if (cxx_dialect
< cxx14
)
17353 error_at (token
->location
,
17354 "use of %<auto%> in parameter declaration "
17355 "only available with "
17356 "-std=c++14 or -std=gnu++14");
17357 else if (!flag_concepts
)
17358 pedwarn (token
->location
, 0,
17359 "use of %<auto%> in parameter declaration "
17360 "only available with -fconcepts");
17363 type
= make_auto ();
17367 /* Since DR 743, decltype can either be a simple-type-specifier by
17368 itself or begin a nested-name-specifier. Parsing it will replace
17369 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17370 handling below decide what to do. */
17371 cp_parser_decltype (parser
);
17372 cp_lexer_set_token_position (parser
->lexer
, token
);
17376 /* Consume the `typeof' token. */
17377 cp_lexer_consume_token (parser
->lexer
);
17378 /* Parse the operand to `typeof'. */
17379 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17380 /* If it is not already a TYPE, take its type. */
17381 if (!TYPE_P (type
))
17382 type
= finish_typeof (type
);
17385 cp_parser_set_decl_spec_type (decl_specs
, type
,
17387 /*type_definition_p=*/false);
17391 case RID_UNDERLYING_TYPE
:
17392 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17394 cp_parser_set_decl_spec_type (decl_specs
, type
,
17396 /*type_definition_p=*/false);
17401 case RID_DIRECT_BASES
:
17402 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17404 cp_parser_set_decl_spec_type (decl_specs
, type
,
17406 /*type_definition_p=*/false);
17412 /* If token is an already-parsed decltype not followed by ::,
17413 it's a simple-type-specifier. */
17414 if (token
->type
== CPP_DECLTYPE
17415 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17417 type
= saved_checks_value (token
->u
.tree_check_value
);
17420 cp_parser_set_decl_spec_type (decl_specs
, type
,
17422 /*type_definition_p=*/false);
17423 /* Remember that we are handling a decltype in order to
17424 implement the resolution of DR 1510 when the argument
17425 isn't instantiation dependent. */
17426 decl_specs
->decltype_p
= true;
17428 cp_lexer_consume_token (parser
->lexer
);
17432 /* If the type-specifier was for a built-in type, we're done. */
17435 /* Record the type. */
17437 && (token
->keyword
!= RID_SIGNED
17438 && token
->keyword
!= RID_UNSIGNED
17439 && token
->keyword
!= RID_SHORT
17440 && token
->keyword
!= RID_LONG
))
17441 cp_parser_set_decl_spec_type (decl_specs
,
17444 /*type_definition_p=*/false);
17446 decl_specs
->any_specifiers_p
= true;
17448 /* Consume the token. */
17449 cp_lexer_consume_token (parser
->lexer
);
17451 if (type
== error_mark_node
)
17452 return error_mark_node
;
17454 /* There is no valid C++ program where a non-template type is
17455 followed by a "<". That usually indicates that the user thought
17456 that the type was a template. */
17457 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17460 return TYPE_NAME (type
);
17463 /* The type-specifier must be a user-defined type. */
17464 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17469 /* Don't gobble tokens or issue error messages if this is an
17470 optional type-specifier. */
17471 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17472 cp_parser_parse_tentatively (parser
);
17474 token
= cp_lexer_peek_token (parser
->lexer
);
17476 /* Look for the optional `::' operator. */
17478 = (cp_parser_global_scope_opt (parser
,
17479 /*current_scope_valid_p=*/false)
17481 /* Look for the nested-name specifier. */
17483 = (cp_parser_nested_name_specifier_opt (parser
,
17484 /*typename_keyword_p=*/false,
17485 /*check_dependency_p=*/true,
17487 /*is_declaration=*/false)
17489 /* If we have seen a nested-name-specifier, and the next token
17490 is `template', then we are using the template-id production. */
17492 && cp_parser_optional_template_keyword (parser
))
17494 /* Look for the template-id. */
17495 type
= cp_parser_template_id (parser
,
17496 /*template_keyword_p=*/true,
17497 /*check_dependency_p=*/true,
17499 /*is_declaration=*/false);
17500 /* If the template-id did not name a type, we are out of
17502 if (TREE_CODE (type
) != TYPE_DECL
)
17504 cp_parser_error (parser
, "expected template-id for type");
17508 /* Otherwise, look for a type-name. */
17510 type
= cp_parser_type_name (parser
);
17511 /* Keep track of all name-lookups performed in class scopes. */
17515 && TREE_CODE (type
) == TYPE_DECL
17516 && identifier_p (DECL_NAME (type
)))
17517 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17518 /* If it didn't work out, we don't have a TYPE. */
17519 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17520 && !cp_parser_parse_definitely (parser
))
17522 if (!type
&& cxx_dialect
>= cxx17
)
17524 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17525 cp_parser_parse_tentatively (parser
);
17527 cp_parser_global_scope_opt (parser
,
17528 /*current_scope_valid_p=*/false);
17529 cp_parser_nested_name_specifier_opt (parser
,
17530 /*typename_keyword_p=*/false,
17531 /*check_dependency_p=*/true,
17533 /*is_declaration=*/false);
17534 tree name
= cp_parser_identifier (parser
);
17535 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17536 && parser
->scope
!= error_mark_node
)
17538 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17540 /*is_template=*/false,
17541 /*is_namespace=*/false,
17542 /*check_dependency=*/true,
17543 /*ambiguous_decls=*/NULL
,
17545 if (tmpl
&& tmpl
!= error_mark_node
17546 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17547 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17548 type
= make_template_placeholder (tmpl
);
17551 type
= error_mark_node
;
17552 if (!cp_parser_simulate_error (parser
))
17553 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17554 NLE_TYPE
, token
->location
);
17558 type
= error_mark_node
;
17560 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17561 && !cp_parser_parse_definitely (parser
))
17564 if (type
&& decl_specs
)
17565 cp_parser_set_decl_spec_type (decl_specs
, type
,
17567 /*type_definition_p=*/false);
17570 /* If we didn't get a type-name, issue an error message. */
17571 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17573 cp_parser_error (parser
, "expected type-name");
17574 return error_mark_node
;
17577 if (type
&& type
!= error_mark_node
)
17579 /* See if TYPE is an Objective-C type, and if so, parse and
17580 accept any protocol references following it. Do this before
17581 the cp_parser_check_for_invalid_template_id() call, because
17582 Objective-C types can be followed by '<...>' which would
17583 enclose protocol names rather than template arguments, and so
17584 everything is fine. */
17585 if (c_dialect_objc () && !parser
->scope
17586 && (objc_is_id (type
) || objc_is_class_name (type
)))
17588 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17589 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17591 /* Clobber the "unqualified" type previously entered into
17592 DECL_SPECS with the new, improved protocol-qualified version. */
17594 decl_specs
->type
= qual_type
;
17599 /* There is no valid C++ program where a non-template type is
17600 followed by a "<". That usually indicates that the user
17601 thought that the type was a template. */
17602 cp_parser_check_for_invalid_template_id (parser
, type
,
17610 /* Parse a type-name.
17616 simple-template-id [in c++0x]
17633 Returns a TYPE_DECL for the type. */
17636 cp_parser_type_name (cp_parser
* parser
)
17638 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17643 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17647 /* We can't know yet whether it is a class-name or not. */
17648 cp_parser_parse_tentatively (parser
);
17649 /* Try a class-name. */
17650 type_decl
= cp_parser_class_name (parser
,
17651 typename_keyword_p
,
17652 /*template_keyword_p=*/false,
17654 /*check_dependency_p=*/true,
17655 /*class_head_p=*/false,
17656 /*is_declaration=*/false);
17657 /* If it's not a class-name, keep looking. */
17658 if (!cp_parser_parse_definitely (parser
))
17660 if (cxx_dialect
< cxx11
)
17661 /* It must be a typedef-name or an enum-name. */
17662 return cp_parser_nonclass_name (parser
);
17664 cp_parser_parse_tentatively (parser
);
17665 /* It is either a simple-template-id representing an
17666 instantiation of an alias template... */
17667 type_decl
= cp_parser_template_id (parser
,
17668 /*template_keyword_p=*/false,
17669 /*check_dependency_p=*/true,
17671 /*is_declaration=*/false);
17672 /* Note that this must be an instantiation of an alias template
17673 because [temp.names]/6 says:
17675 A template-id that names an alias template specialization
17678 Whereas [temp.names]/7 says:
17680 A simple-template-id that names a class template
17681 specialization is a class-name.
17683 With concepts, this could also be a partial-concept-id that
17684 declares a non-type template parameter. */
17685 if (type_decl
!= NULL_TREE
17686 && TREE_CODE (type_decl
) == TYPE_DECL
17687 && TYPE_DECL_ALIAS_P (type_decl
))
17688 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17689 else if (is_constrained_parameter (type_decl
))
17690 /* Don't do anything. */ ;
17692 cp_parser_simulate_error (parser
);
17694 if (!cp_parser_parse_definitely (parser
))
17695 /* ... Or a typedef-name or an enum-name. */
17696 return cp_parser_nonclass_name (parser
);
17702 /* Check if DECL and ARGS can form a constrained-type-specifier.
17703 If ARGS is non-null, we try to form a concept check of the
17704 form DECL<?, ARGS> where ? is a wildcard that matches any
17705 kind of template argument. If ARGS is NULL, then we try to
17706 form a concept check of the form DECL<?>. */
17709 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17710 tree decl
, tree args
)
17712 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17714 /* If we a constrained-type-specifier cannot be deduced. */
17715 if (parser
->prevent_constrained_type_specifiers
)
17718 /* A constrained type specifier can only be found in an
17719 overload set or as a reference to a template declaration.
17721 FIXME: This might be masking a bug. It's possible that
17722 that the deduction below is causing template specializations
17723 to be formed with the wildcard as an argument. */
17724 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17727 /* Try to build a call expression that evaluates the
17728 concept. This can fail if the overload set refers
17729 only to non-templates. */
17730 tree placeholder
= build_nt (WILDCARD_DECL
);
17731 tree check
= build_concept_check (decl
, placeholder
, args
);
17732 if (check
== error_mark_node
)
17735 /* Deduce the checked constraint and the prototype parameter.
17737 FIXME: In certain cases, failure to deduce should be a
17738 diagnosable error. */
17741 if (!deduce_constrained_parameter (check
, conc
, proto
))
17744 /* In template parameter scope, this results in a constrained
17745 parameter. Return a descriptor of that parm. */
17746 if (processing_template_parmlist
)
17747 return build_constrained_parameter (conc
, proto
, args
);
17749 /* In a parameter-declaration-clause, constrained-type
17750 specifiers result in invented template parameters. */
17751 if (parser
->auto_is_implicit_function_template_parm_p
)
17753 tree x
= build_constrained_parameter (conc
, proto
, args
);
17754 return synthesize_implicit_template_parm (parser
, x
);
17758 /* Otherwise, we're in a context where the constrained
17759 type name is deduced and the constraint applies
17760 after deduction. */
17761 return make_constrained_auto (conc
, args
);
17767 /* If DECL refers to a concept, return a TYPE_DECL representing
17768 the result of using the constrained type specifier in the
17769 current context. DECL refers to a concept if
17771 - it is an overload set containing a function concept taking a single
17774 - it is a variable concept taking a single type argument. */
17777 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17780 && (TREE_CODE (decl
) == OVERLOAD
17781 || BASELINK_P (decl
)
17782 || variable_concept_p (decl
)))
17783 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17788 /* Check if DECL and ARGS form a partial-concept-id. If so,
17789 assign ID to the resulting constrained placeholder.
17791 Returns true if the partial-concept-id designates a placeholder
17792 and false otherwise. Note that *id is set to NULL_TREE in
17796 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17798 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17801 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17813 Returns a TYPE_DECL for the type. */
17816 cp_parser_nonclass_name (cp_parser
* parser
)
17821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17822 identifier
= cp_parser_identifier (parser
);
17823 if (identifier
== error_mark_node
)
17824 return error_mark_node
;
17826 /* Look up the type-name. */
17827 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17829 type_decl
= strip_using_decl (type_decl
);
17831 /* If we found an overload set, then it may refer to a concept-name. */
17832 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17835 if (TREE_CODE (type_decl
) != TYPE_DECL
17836 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17838 /* See if this is an Objective-C type. */
17839 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17840 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17842 type_decl
= TYPE_NAME (type
);
17845 /* Issue an error if we did not find a type-name. */
17846 if (TREE_CODE (type_decl
) != TYPE_DECL
17847 /* In Objective-C, we have the complication that class names are
17848 normally type names and start declarations (eg, the
17849 "NSObject" in "NSObject *object;"), but can be used in an
17850 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17851 is an expression. So, a classname followed by a dot is not a
17852 valid type-name. */
17853 || (objc_is_class_name (TREE_TYPE (type_decl
))
17854 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17856 if (!cp_parser_simulate_error (parser
))
17857 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17858 NLE_TYPE
, token
->location
);
17859 return error_mark_node
;
17861 /* Remember that the name was used in the definition of the
17862 current class so that we can check later to see if the
17863 meaning would have been different after the class was
17864 entirely defined. */
17865 else if (type_decl
!= error_mark_node
17867 maybe_note_name_used_in_class (identifier
, type_decl
);
17872 /* Parse an elaborated-type-specifier. Note that the grammar given
17873 here incorporates the resolution to DR68.
17875 elaborated-type-specifier:
17876 class-key :: [opt] nested-name-specifier [opt] identifier
17877 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17878 enum-key :: [opt] nested-name-specifier [opt] identifier
17879 typename :: [opt] nested-name-specifier identifier
17880 typename :: [opt] nested-name-specifier template [opt]
17885 elaborated-type-specifier:
17886 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17887 class-key attributes :: [opt] nested-name-specifier [opt]
17888 template [opt] template-id
17889 enum attributes :: [opt] nested-name-specifier [opt] identifier
17891 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17892 declared `friend'. If IS_DECLARATION is TRUE, then this
17893 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17894 something is being declared.
17896 Returns the TYPE specified. */
17899 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17901 bool is_declaration
)
17903 enum tag_types tag_type
;
17905 tree type
= NULL_TREE
;
17906 tree attributes
= NULL_TREE
;
17908 cp_token
*token
= NULL
;
17910 /* See if we're looking at the `enum' keyword. */
17911 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17913 /* Consume the `enum' token. */
17914 cp_lexer_consume_token (parser
->lexer
);
17915 /* Remember that it's an enumeration type. */
17916 tag_type
= enum_type
;
17917 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17918 enums) is used here. */
17919 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17920 if (cp_parser_is_keyword (token
, RID_CLASS
)
17921 || cp_parser_is_keyword (token
, RID_STRUCT
))
17923 gcc_rich_location
richloc (token
->location
);
17924 richloc
.add_range (input_location
);
17925 richloc
.add_fixit_remove ();
17926 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17927 "a scoped enum must not use the %qD keyword",
17929 /* Consume the `struct' or `class' and parse it anyway. */
17930 cp_lexer_consume_token (parser
->lexer
);
17932 /* Parse the attributes. */
17933 attributes
= cp_parser_attributes_opt (parser
);
17935 /* Or, it might be `typename'. */
17936 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17939 /* Consume the `typename' token. */
17940 cp_lexer_consume_token (parser
->lexer
);
17941 /* Remember that it's a `typename' type. */
17942 tag_type
= typename_type
;
17944 /* Otherwise it must be a class-key. */
17947 tag_type
= cp_parser_class_key (parser
);
17948 if (tag_type
== none_type
)
17949 return error_mark_node
;
17950 /* Parse the attributes. */
17951 attributes
= cp_parser_attributes_opt (parser
);
17954 /* Look for the `::' operator. */
17955 globalscope
= cp_parser_global_scope_opt (parser
,
17956 /*current_scope_valid_p=*/false);
17957 /* Look for the nested-name-specifier. */
17958 tree nested_name_specifier
;
17959 if (tag_type
== typename_type
&& !globalscope
)
17961 nested_name_specifier
17962 = cp_parser_nested_name_specifier (parser
,
17963 /*typename_keyword_p=*/true,
17964 /*check_dependency_p=*/true,
17967 if (!nested_name_specifier
)
17968 return error_mark_node
;
17971 /* Even though `typename' is not present, the proposed resolution
17972 to Core Issue 180 says that in `class A<T>::B', `B' should be
17973 considered a type-name, even if `A<T>' is dependent. */
17974 nested_name_specifier
17975 = cp_parser_nested_name_specifier_opt (parser
,
17976 /*typename_keyword_p=*/true,
17977 /*check_dependency_p=*/true,
17980 /* For everything but enumeration types, consider a template-id.
17981 For an enumeration type, consider only a plain identifier. */
17982 if (tag_type
!= enum_type
)
17984 bool template_p
= false;
17987 /* Allow the `template' keyword. */
17988 template_p
= cp_parser_optional_template_keyword (parser
);
17989 /* If we didn't see `template', we don't know if there's a
17990 template-id or not. */
17992 cp_parser_parse_tentatively (parser
);
17993 /* Parse the template-id. */
17994 token
= cp_lexer_peek_token (parser
->lexer
);
17995 decl
= cp_parser_template_id (parser
, template_p
,
17996 /*check_dependency_p=*/true,
17999 /* If we didn't find a template-id, look for an ordinary
18001 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18003 /* We can get here when cp_parser_template_id, called by
18004 cp_parser_class_name with tag_type == none_type, succeeds
18005 and caches a BASELINK. Then, when called again here,
18006 instead of failing and returning an error_mark_node
18007 returns it (see template/typename17.C in C++11).
18008 ??? Could we diagnose this earlier? */
18009 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18011 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18012 type
= error_mark_node
;
18014 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18015 in effect, then we must assume that, upon instantiation, the
18016 template will correspond to a class. */
18017 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18018 && tag_type
== typename_type
)
18019 type
= make_typename_type (parser
->scope
, decl
,
18021 /*complain=*/tf_error
);
18022 /* If the `typename' keyword is in effect and DECL is not a type
18023 decl, then type is non existent. */
18024 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18026 else if (TREE_CODE (decl
) == TYPE_DECL
)
18028 type
= check_elaborated_type_specifier (tag_type
, decl
,
18029 /*allow_template_p=*/true);
18031 /* If the next token is a semicolon, this must be a specialization,
18032 instantiation, or friend declaration. Check the scope while we
18033 still know whether or not we had a nested-name-specifier. */
18034 if (type
!= error_mark_node
18035 && !nested_name_specifier
&& !is_friend
18036 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18037 check_unqualified_spec_or_inst (type
, token
->location
);
18039 else if (decl
== error_mark_node
)
18040 type
= error_mark_node
;
18045 token
= cp_lexer_peek_token (parser
->lexer
);
18046 identifier
= cp_parser_identifier (parser
);
18048 if (identifier
== error_mark_node
)
18050 parser
->scope
= NULL_TREE
;
18051 return error_mark_node
;
18054 /* For a `typename', we needn't call xref_tag. */
18055 if (tag_type
== typename_type
18056 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18057 return cp_parser_make_typename_type (parser
, identifier
,
18060 /* Template parameter lists apply only if we are not within a
18061 function parameter list. */
18062 bool template_parm_lists_apply
18063 = parser
->num_template_parameter_lists
;
18064 if (template_parm_lists_apply
)
18065 for (cp_binding_level
*s
= current_binding_level
;
18066 s
&& s
->kind
!= sk_template_parms
;
18067 s
= s
->level_chain
)
18068 if (s
->kind
== sk_function_parms
)
18069 template_parm_lists_apply
= false;
18071 /* Look up a qualified name in the usual way. */
18075 tree ambiguous_decls
;
18077 decl
= cp_parser_lookup_name (parser
, identifier
,
18079 /*is_template=*/false,
18080 /*is_namespace=*/false,
18081 /*check_dependency=*/true,
18085 /* If the lookup was ambiguous, an error will already have been
18087 if (ambiguous_decls
)
18088 return error_mark_node
;
18090 /* If we are parsing friend declaration, DECL may be a
18091 TEMPLATE_DECL tree node here. However, we need to check
18092 whether this TEMPLATE_DECL results in valid code. Consider
18093 the following example:
18096 template <class T> class C {};
18099 template <class T> friend class N::C; // #1, valid code
18101 template <class T> class Y {
18102 friend class N::C; // #2, invalid code
18105 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18106 name lookup of `N::C'. We see that friend declaration must
18107 be template for the code to be valid. Note that
18108 processing_template_decl does not work here since it is
18109 always 1 for the above two cases. */
18111 decl
= (cp_parser_maybe_treat_template_as_class
18112 (decl
, /*tag_name_p=*/is_friend
18113 && template_parm_lists_apply
));
18115 if (TREE_CODE (decl
) != TYPE_DECL
)
18117 cp_parser_diagnose_invalid_type_name (parser
,
18120 return error_mark_node
;
18123 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18125 bool allow_template
= (template_parm_lists_apply
18126 || DECL_SELF_REFERENCE_P (decl
));
18127 type
= check_elaborated_type_specifier (tag_type
, decl
,
18130 if (type
== error_mark_node
)
18131 return error_mark_node
;
18134 /* Forward declarations of nested types, such as
18139 are invalid unless all components preceding the final '::'
18140 are complete. If all enclosing types are complete, these
18141 declarations become merely pointless.
18143 Invalid forward declarations of nested types are errors
18144 caught elsewhere in parsing. Those that are pointless arrive
18147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18148 && !is_friend
&& !processing_explicit_instantiation
)
18149 warning (0, "declaration %qD does not declare anything", decl
);
18151 type
= TREE_TYPE (decl
);
18155 /* An elaborated-type-specifier sometimes introduces a new type and
18156 sometimes names an existing type. Normally, the rule is that it
18157 introduces a new type only if there is not an existing type of
18158 the same name already in scope. For example, given:
18161 void f() { struct S s; }
18163 the `struct S' in the body of `f' is the same `struct S' as in
18164 the global scope; the existing definition is used. However, if
18165 there were no global declaration, this would introduce a new
18166 local class named `S'.
18168 An exception to this rule applies to the following code:
18170 namespace N { struct S; }
18172 Here, the elaborated-type-specifier names a new type
18173 unconditionally; even if there is already an `S' in the
18174 containing scope this declaration names a new type.
18175 This exception only applies if the elaborated-type-specifier
18176 forms the complete declaration:
18180 A declaration consisting solely of `class-key identifier ;' is
18181 either a redeclaration of the name in the current scope or a
18182 forward declaration of the identifier as a class name. It
18183 introduces the name into the current scope.
18185 We are in this situation precisely when the next token is a `;'.
18187 An exception to the exception is that a `friend' declaration does
18188 *not* name a new type; i.e., given:
18190 struct S { friend struct T; };
18192 `T' is not a new type in the scope of `S'.
18194 Also, `new struct S' or `sizeof (struct S)' never results in the
18195 definition of a new type; a new type can only be declared in a
18196 declaration context. */
18202 /* Friends have special name lookup rules. */
18203 ts
= ts_within_enclosing_non_class
;
18204 else if (is_declaration
18205 && cp_lexer_next_token_is (parser
->lexer
,
18207 /* This is a `class-key identifier ;' */
18213 (template_parm_lists_apply
18214 && (cp_parser_next_token_starts_class_definition_p (parser
)
18215 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18216 /* An unqualified name was used to reference this type, so
18217 there were no qualifying templates. */
18218 if (template_parm_lists_apply
18219 && !cp_parser_check_template_parameters (parser
,
18220 /*num_templates=*/0,
18221 /*template_id*/false,
18223 /*declarator=*/NULL
))
18224 return error_mark_node
;
18225 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18229 if (type
== error_mark_node
)
18230 return error_mark_node
;
18232 /* Allow attributes on forward declarations of classes. */
18235 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18236 warning (OPT_Wattributes
,
18237 "attributes ignored on uninstantiated type");
18238 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18239 && ! processing_explicit_instantiation
)
18240 warning (OPT_Wattributes
,
18241 "attributes ignored on template instantiation");
18242 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18243 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18245 warning (OPT_Wattributes
,
18246 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18249 if (tag_type
!= enum_type
)
18251 /* Indicate whether this class was declared as a `class' or as a
18253 if (CLASS_TYPE_P (type
))
18254 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18255 cp_parser_check_class_key (tag_type
, type
);
18258 /* A "<" cannot follow an elaborated type specifier. If that
18259 happens, the user was probably trying to form a template-id. */
18260 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18266 /* Parse an enum-specifier.
18269 enum-head { enumerator-list [opt] }
18270 enum-head { enumerator-list , } [C++0x]
18273 enum-key identifier [opt] enum-base [opt]
18274 enum-key nested-name-specifier identifier enum-base [opt]
18279 enum struct [C++0x]
18282 : type-specifier-seq
18284 opaque-enum-specifier:
18285 enum-key identifier enum-base [opt] ;
18288 enum-key attributes[opt] identifier [opt] enum-base [opt]
18289 { enumerator-list [opt] }attributes[opt]
18290 enum-key attributes[opt] identifier [opt] enum-base [opt]
18291 { enumerator-list, }attributes[opt] [C++0x]
18293 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18294 if the token stream isn't an enum-specifier after all. */
18297 cp_parser_enum_specifier (cp_parser
* parser
)
18300 tree type
= NULL_TREE
;
18302 tree nested_name_specifier
= NULL_TREE
;
18304 bool scoped_enum_p
= false;
18305 bool has_underlying_type
= false;
18306 bool nested_being_defined
= false;
18307 bool new_value_list
= false;
18308 bool is_new_type
= false;
18309 bool is_unnamed
= false;
18310 tree underlying_type
= NULL_TREE
;
18311 cp_token
*type_start_token
= NULL
;
18312 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18314 parser
->colon_corrects_to_scope_p
= false;
18316 /* Parse tentatively so that we can back up if we don't find a
18318 cp_parser_parse_tentatively (parser
);
18320 /* Caller guarantees that the current token is 'enum', an identifier
18321 possibly follows, and the token after that is an opening brace.
18322 If we don't have an identifier, fabricate an anonymous name for
18323 the enumeration being defined. */
18324 cp_lexer_consume_token (parser
->lexer
);
18326 /* Parse the "class" or "struct", which indicates a scoped
18327 enumeration type in C++0x. */
18328 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18329 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18331 if (cxx_dialect
< cxx11
)
18332 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18334 /* Consume the `struct' or `class' token. */
18335 cp_lexer_consume_token (parser
->lexer
);
18337 scoped_enum_p
= true;
18340 attributes
= cp_parser_attributes_opt (parser
);
18342 /* Clear the qualification. */
18343 parser
->scope
= NULL_TREE
;
18344 parser
->qualifying_scope
= NULL_TREE
;
18345 parser
->object_scope
= NULL_TREE
;
18347 /* Figure out in what scope the declaration is being placed. */
18348 prev_scope
= current_scope ();
18350 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18352 push_deferring_access_checks (dk_no_check
);
18353 nested_name_specifier
18354 = cp_parser_nested_name_specifier_opt (parser
,
18355 /*typename_keyword_p=*/true,
18356 /*check_dependency_p=*/false,
18358 /*is_declaration=*/false);
18360 if (nested_name_specifier
)
18364 identifier
= cp_parser_identifier (parser
);
18365 name
= cp_parser_lookup_name (parser
, identifier
,
18367 /*is_template=*/false,
18368 /*is_namespace=*/false,
18369 /*check_dependency=*/true,
18370 /*ambiguous_decls=*/NULL
,
18372 if (name
&& name
!= error_mark_node
)
18374 type
= TREE_TYPE (name
);
18375 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18377 /* Are template enums allowed in ISO? */
18378 if (template_parm_scope_p ())
18379 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18380 "%qD is an enumeration template", name
);
18381 /* ignore a typename reference, for it will be solved by name
18386 else if (nested_name_specifier
== error_mark_node
)
18387 /* We already issued an error. */;
18390 error_at (type_start_token
->location
,
18391 "%qD does not name an enumeration in %qT",
18392 identifier
, nested_name_specifier
);
18393 nested_name_specifier
= error_mark_node
;
18398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18399 identifier
= cp_parser_identifier (parser
);
18402 identifier
= make_anon_name ();
18405 error_at (type_start_token
->location
,
18406 "unnamed scoped enum is not allowed");
18409 pop_deferring_access_checks ();
18411 /* Check for the `:' that denotes a specified underlying type in C++0x.
18412 Note that a ':' could also indicate a bitfield width, however. */
18413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18415 cp_decl_specifier_seq type_specifiers
;
18417 /* Consume the `:'. */
18418 cp_lexer_consume_token (parser
->lexer
);
18420 /* Parse the type-specifier-seq. */
18421 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18422 /*is_trailing_return=*/false,
18425 /* At this point this is surely not elaborated type specifier. */
18426 if (!cp_parser_parse_definitely (parser
))
18429 if (cxx_dialect
< cxx11
)
18430 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18432 has_underlying_type
= true;
18434 /* If that didn't work, stop. */
18435 if (type_specifiers
.type
!= error_mark_node
)
18437 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18438 /*initialized=*/0, NULL
);
18439 if (underlying_type
== error_mark_node
18440 || check_for_bare_parameter_packs (underlying_type
))
18441 underlying_type
= NULL_TREE
;
18445 /* Look for the `{' but don't consume it yet. */
18446 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18448 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18450 cp_parser_error (parser
, "expected %<{%>");
18451 if (has_underlying_type
)
18457 /* An opaque-enum-specifier must have a ';' here. */
18458 if ((scoped_enum_p
|| underlying_type
)
18459 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18461 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18462 if (has_underlying_type
)
18470 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18473 if (nested_name_specifier
)
18475 if (CLASS_TYPE_P (nested_name_specifier
))
18477 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18478 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18479 push_scope (nested_name_specifier
);
18481 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18483 push_nested_namespace (nested_name_specifier
);
18487 /* Issue an error message if type-definitions are forbidden here. */
18488 if (!cp_parser_check_type_definition (parser
))
18489 type
= error_mark_node
;
18491 /* Create the new type. We do this before consuming the opening
18492 brace so the enum will be recorded as being on the line of its
18493 tag (or the 'enum' keyword, if there is no tag). */
18494 type
= start_enum (identifier
, type
, underlying_type
,
18495 attributes
, scoped_enum_p
, &is_new_type
);
18497 /* If the next token is not '{' it is an opaque-enum-specifier or an
18498 elaborated-type-specifier. */
18499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18501 timevar_push (TV_PARSE_ENUM
);
18502 if (nested_name_specifier
18503 && nested_name_specifier
!= error_mark_node
)
18505 /* The following catches invalid code such as:
18506 enum class S<int>::E { A, B, C }; */
18507 if (!processing_specialization
18508 && CLASS_TYPE_P (nested_name_specifier
)
18509 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18510 error_at (type_start_token
->location
, "cannot add an enumerator "
18511 "list to a template instantiation");
18513 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18515 error_at (type_start_token
->location
,
18516 "%<%T::%E%> has not been declared",
18517 TYPE_CONTEXT (nested_name_specifier
),
18518 nested_name_specifier
);
18519 type
= error_mark_node
;
18521 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18522 && !CLASS_TYPE_P (nested_name_specifier
))
18524 error_at (type_start_token
->location
, "nested name specifier "
18525 "%qT for enum declaration does not name a class "
18526 "or namespace", nested_name_specifier
);
18527 type
= error_mark_node
;
18529 /* If that scope does not contain the scope in which the
18530 class was originally declared, the program is invalid. */
18531 else if (prev_scope
&& !is_ancestor (prev_scope
,
18532 nested_name_specifier
))
18534 if (at_namespace_scope_p ())
18535 error_at (type_start_token
->location
,
18536 "declaration of %qD in namespace %qD which does not "
18538 type
, prev_scope
, nested_name_specifier
);
18540 error_at (type_start_token
->location
,
18541 "declaration of %qD in %qD which does not "
18543 type
, prev_scope
, nested_name_specifier
);
18544 type
= error_mark_node
;
18546 /* If that scope is the scope where the declaration is being placed
18547 the program is invalid. */
18548 else if (CLASS_TYPE_P (nested_name_specifier
)
18549 && CLASS_TYPE_P (prev_scope
)
18550 && same_type_p (nested_name_specifier
, prev_scope
))
18552 permerror (type_start_token
->location
,
18553 "extra qualification not allowed");
18554 nested_name_specifier
= NULL_TREE
;
18559 begin_scope (sk_scoped_enum
, type
);
18561 /* Consume the opening brace. */
18562 matching_braces braces
;
18563 braces
.consume_open (parser
);
18565 if (type
== error_mark_node
)
18566 ; /* Nothing to add */
18567 else if (OPAQUE_ENUM_P (type
)
18568 || (cxx_dialect
> cxx98
&& processing_specialization
))
18570 new_value_list
= true;
18571 SET_OPAQUE_ENUM_P (type
, false);
18572 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18576 error_at (type_start_token
->location
,
18577 "multiple definition of %q#T", type
);
18578 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18579 "previous definition here");
18580 type
= error_mark_node
;
18583 if (type
== error_mark_node
)
18584 cp_parser_skip_to_end_of_block_or_statement (parser
);
18585 /* If the next token is not '}', then there are some enumerators. */
18586 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18588 if (is_unnamed
&& !scoped_enum_p
)
18589 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18590 "ISO C++ forbids empty unnamed enum");
18593 cp_parser_enumerator_list (parser
, type
);
18595 /* Consume the final '}'. */
18596 braces
.require_close (parser
);
18600 timevar_pop (TV_PARSE_ENUM
);
18604 /* If a ';' follows, then it is an opaque-enum-specifier
18605 and additional restrictions apply. */
18606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18609 error_at (type_start_token
->location
,
18610 "opaque-enum-specifier without name");
18611 else if (nested_name_specifier
)
18612 error_at (type_start_token
->location
,
18613 "opaque-enum-specifier must use a simple identifier");
18617 /* Look for trailing attributes to apply to this enumeration, and
18618 apply them if appropriate. */
18619 if (cp_parser_allow_gnu_extensions_p (parser
))
18621 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18622 cplus_decl_attributes (&type
,
18624 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18627 /* Finish up the enumeration. */
18628 if (type
!= error_mark_node
)
18630 if (new_value_list
)
18631 finish_enum_value_list (type
);
18633 finish_enum (type
);
18636 if (nested_name_specifier
)
18638 if (CLASS_TYPE_P (nested_name_specifier
))
18640 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18641 pop_scope (nested_name_specifier
);
18643 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18645 pop_nested_namespace (nested_name_specifier
);
18649 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18653 /* Parse an enumerator-list. The enumerators all have the indicated
18657 enumerator-definition
18658 enumerator-list , enumerator-definition */
18661 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18665 /* Parse an enumerator-definition. */
18666 cp_parser_enumerator_definition (parser
, type
);
18668 /* If the next token is not a ',', we've reached the end of
18670 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18672 /* Otherwise, consume the `,' and keep going. */
18673 cp_lexer_consume_token (parser
->lexer
);
18674 /* If the next token is a `}', there is a trailing comma. */
18675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18677 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18678 pedwarn (input_location
, OPT_Wpedantic
,
18679 "comma at end of enumerator list");
18685 /* Parse an enumerator-definition. The enumerator has the indicated
18688 enumerator-definition:
18690 enumerator = constant-expression
18697 enumerator-definition:
18698 enumerator attributes [opt]
18699 enumerator attributes [opt] = constant-expression */
18702 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18708 /* Save the input location because we are interested in the location
18709 of the identifier and not the location of the explicit value. */
18710 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18712 /* Look for the identifier. */
18713 identifier
= cp_parser_identifier (parser
);
18714 if (identifier
== error_mark_node
)
18717 /* Parse any specified attributes. */
18718 tree attrs
= cp_parser_attributes_opt (parser
);
18720 /* If the next token is an '=', then there is an explicit value. */
18721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18723 /* Consume the `=' token. */
18724 cp_lexer_consume_token (parser
->lexer
);
18725 /* Parse the value. */
18726 value
= cp_parser_constant_expression (parser
);
18731 /* If we are processing a template, make sure the initializer of the
18732 enumerator doesn't contain any bare template parameter pack. */
18733 if (check_for_bare_parameter_packs (value
))
18734 value
= error_mark_node
;
18736 /* Create the enumerator. */
18737 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18740 /* Parse a namespace-name.
18743 original-namespace-name
18746 Returns the NAMESPACE_DECL for the namespace. */
18749 cp_parser_namespace_name (cp_parser
* parser
)
18752 tree namespace_decl
;
18754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18756 /* Get the name of the namespace. */
18757 identifier
= cp_parser_identifier (parser
);
18758 if (identifier
== error_mark_node
)
18759 return error_mark_node
;
18761 /* Look up the identifier in the currently active scope. Look only
18762 for namespaces, due to:
18764 [basic.lookup.udir]
18766 When looking up a namespace-name in a using-directive or alias
18767 definition, only namespace names are considered.
18771 [basic.lookup.qual]
18773 During the lookup of a name preceding the :: scope resolution
18774 operator, object, function, and enumerator names are ignored.
18776 (Note that cp_parser_qualifying_entity only calls this
18777 function if the token after the name is the scope resolution
18779 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18781 /*is_template=*/false,
18782 /*is_namespace=*/true,
18783 /*check_dependency=*/true,
18784 /*ambiguous_decls=*/NULL
,
18786 /* If it's not a namespace, issue an error. */
18787 if (namespace_decl
== error_mark_node
18788 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18790 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18792 auto_diagnostic_group d
;
18794 if (namespace_decl
== error_mark_node
18795 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18796 hint
= suggest_alternative_in_explicit_scope (token
->location
,
18799 if (const char *suggestion
= hint
.suggestion ())
18801 gcc_rich_location
richloc (token
->location
);
18802 richloc
.add_fixit_replace (suggestion
);
18803 error_at (&richloc
,
18804 "%qD is not a namespace-name; did you mean %qs?",
18805 identifier
, suggestion
);
18808 error_at (token
->location
, "%qD is not a namespace-name",
18812 cp_parser_error (parser
, "expected namespace-name");
18813 namespace_decl
= error_mark_node
;
18816 return namespace_decl
;
18819 /* Parse a namespace-definition.
18821 namespace-definition:
18822 named-namespace-definition
18823 unnamed-namespace-definition
18825 named-namespace-definition:
18826 original-namespace-definition
18827 extension-namespace-definition
18829 original-namespace-definition:
18830 namespace identifier { namespace-body }
18832 extension-namespace-definition:
18833 namespace original-namespace-name { namespace-body }
18835 unnamed-namespace-definition:
18836 namespace { namespace-body } */
18839 cp_parser_namespace_definition (cp_parser
* parser
)
18842 int nested_definition_count
= 0;
18844 cp_ensure_no_omp_declare_simd (parser
);
18845 cp_ensure_no_oacc_routine (parser
);
18847 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18851 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18852 cp_lexer_consume_token (parser
->lexer
);
18855 /* Look for the `namespace' keyword. */
18857 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18859 /* Parse any specified attributes before the identifier. */
18860 tree attribs
= cp_parser_attributes_opt (parser
);
18864 identifier
= NULL_TREE
;
18866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18868 identifier
= cp_parser_identifier (parser
);
18870 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18871 pedwarn (input_location
, OPT_Wpedantic
,
18872 "standard attributes on namespaces must precede "
18873 "the namespace name");
18875 /* Parse any attributes specified after the identifier. */
18876 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18879 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18882 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18883 pedwarn (input_location
, OPT_Wpedantic
,
18884 "nested namespace definitions only available with "
18885 "-std=c++17 or -std=gnu++17");
18887 /* Nested namespace names can create new namespaces (unlike
18888 other qualified-ids). */
18889 if (int count
= identifier
? push_namespace (identifier
) : 0)
18890 nested_definition_count
+= count
;
18892 cp_parser_error (parser
, "nested namespace name required");
18893 cp_lexer_consume_token (parser
->lexer
);
18896 if (nested_definition_count
&& !identifier
)
18897 cp_parser_error (parser
, "namespace name required");
18899 if (nested_definition_count
&& attribs
)
18900 error_at (token
->location
,
18901 "a nested namespace definition cannot have attributes");
18902 if (nested_definition_count
&& is_inline
)
18903 error_at (token
->location
,
18904 "a nested namespace definition cannot be inline");
18906 /* Start the namespace. */
18907 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18909 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18911 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18913 /* Look for the `{' to validate starting the namespace. */
18914 matching_braces braces
;
18915 if (braces
.require_open (parser
))
18917 /* Parse the body of the namespace. */
18918 cp_parser_namespace_body (parser
);
18920 /* Look for the final `}'. */
18921 braces
.require_close (parser
);
18924 if (has_visibility
)
18925 pop_visibility (1);
18927 /* Pop the nested namespace definitions. */
18928 while (nested_definition_count
--)
18932 /* Parse a namespace-body.
18935 declaration-seq [opt] */
18938 cp_parser_namespace_body (cp_parser
* parser
)
18940 cp_parser_declaration_seq_opt (parser
);
18943 /* Parse a namespace-alias-definition.
18945 namespace-alias-definition:
18946 namespace identifier = qualified-namespace-specifier ; */
18949 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18952 tree namespace_specifier
;
18954 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18956 /* Look for the `namespace' keyword. */
18957 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18958 /* Look for the identifier. */
18959 identifier
= cp_parser_identifier (parser
);
18960 if (identifier
== error_mark_node
)
18962 /* Look for the `=' token. */
18963 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18964 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18966 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18967 /* Skip the definition. */
18968 cp_lexer_consume_token (parser
->lexer
);
18969 if (cp_parser_skip_to_closing_brace (parser
))
18970 cp_lexer_consume_token (parser
->lexer
);
18973 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18974 /* Look for the qualified-namespace-specifier. */
18975 namespace_specifier
18976 = cp_parser_qualified_namespace_specifier (parser
);
18977 /* Look for the `;' token. */
18978 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18980 /* Register the alias in the symbol table. */
18981 do_namespace_alias (identifier
, namespace_specifier
);
18984 /* Parse a qualified-namespace-specifier.
18986 qualified-namespace-specifier:
18987 :: [opt] nested-name-specifier [opt] namespace-name
18989 Returns a NAMESPACE_DECL corresponding to the specified
18993 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18995 /* Look for the optional `::'. */
18996 cp_parser_global_scope_opt (parser
,
18997 /*current_scope_valid_p=*/false);
18999 /* Look for the optional nested-name-specifier. */
19000 cp_parser_nested_name_specifier_opt (parser
,
19001 /*typename_keyword_p=*/false,
19002 /*check_dependency_p=*/true,
19004 /*is_declaration=*/true);
19006 return cp_parser_namespace_name (parser
);
19009 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19010 access declaration.
19013 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19014 using :: unqualified-id ;
19016 access-declaration:
19022 cp_parser_using_declaration (cp_parser
* parser
,
19023 bool access_declaration_p
)
19026 bool typename_p
= false;
19027 bool global_scope_p
;
19031 int oldcount
= errorcount
;
19032 cp_token
*diag_token
= NULL
;
19034 if (access_declaration_p
)
19036 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19037 cp_parser_parse_tentatively (parser
);
19041 /* Look for the `using' keyword. */
19042 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19045 /* Peek at the next token. */
19046 token
= cp_lexer_peek_token (parser
->lexer
);
19047 /* See if it's `typename'. */
19048 if (token
->keyword
== RID_TYPENAME
)
19050 /* Remember that we've seen it. */
19052 /* Consume the `typename' token. */
19053 cp_lexer_consume_token (parser
->lexer
);
19057 /* Look for the optional global scope qualification. */
19059 = (cp_parser_global_scope_opt (parser
,
19060 /*current_scope_valid_p=*/false)
19063 /* If we saw `typename', or didn't see `::', then there must be a
19064 nested-name-specifier present. */
19065 if (typename_p
|| !global_scope_p
)
19067 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19068 /*check_dependency_p=*/true,
19070 /*is_declaration=*/true);
19071 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19073 cp_parser_skip_to_end_of_block_or_statement (parser
);
19077 /* Otherwise, we could be in either of the two productions. In that
19078 case, treat the nested-name-specifier as optional. */
19080 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19081 /*typename_keyword_p=*/false,
19082 /*check_dependency_p=*/true,
19084 /*is_declaration=*/true);
19086 qscope
= global_namespace
;
19087 else if (UNSCOPED_ENUM_P (qscope
))
19088 qscope
= CP_TYPE_CONTEXT (qscope
);
19090 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19091 /* Something has already gone wrong; there's no need to parse
19092 further. Since an error has occurred, the return value of
19093 cp_parser_parse_definitely will be false, as required. */
19094 return cp_parser_parse_definitely (parser
);
19096 token
= cp_lexer_peek_token (parser
->lexer
);
19097 /* Parse the unqualified-id. */
19098 identifier
= cp_parser_unqualified_id (parser
,
19099 /*template_keyword_p=*/false,
19100 /*check_dependency_p=*/true,
19101 /*declarator_p=*/true,
19102 /*optional_p=*/false);
19104 if (access_declaration_p
)
19106 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19107 cp_parser_simulate_error (parser
);
19108 if (!cp_parser_parse_definitely (parser
))
19111 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19113 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19114 if (cxx_dialect
< cxx17
19115 && !in_system_header_at (ell
->location
))
19116 pedwarn (ell
->location
, 0,
19117 "pack expansion in using-declaration only available "
19118 "with -std=c++17 or -std=gnu++17");
19119 qscope
= make_pack_expansion (qscope
);
19122 /* The function we call to handle a using-declaration is different
19123 depending on what scope we are in. */
19124 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19126 else if (!identifier_p (identifier
)
19127 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19128 /* [namespace.udecl]
19130 A using declaration shall not name a template-id. */
19131 error_at (token
->location
,
19132 "a template-id may not appear in a using-declaration");
19135 if (at_class_scope_p ())
19137 /* Create the USING_DECL. */
19138 decl
= do_class_using_decl (qscope
, identifier
);
19140 if (decl
&& typename_p
)
19141 USING_DECL_TYPENAME_P (decl
) = 1;
19143 if (check_for_bare_parameter_packs (decl
))
19145 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19149 /* Add it to the list of members in this class. */
19150 finish_member_declaration (decl
);
19154 decl
= cp_parser_lookup_name_simple (parser
,
19157 if (decl
== error_mark_node
)
19158 cp_parser_name_lookup_error (parser
, identifier
,
19161 else if (check_for_bare_parameter_packs (decl
))
19163 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19166 else if (!at_namespace_scope_p ())
19167 finish_local_using_decl (decl
, qscope
, identifier
);
19169 finish_namespace_using_decl (decl
, qscope
, identifier
);
19173 if (!access_declaration_p
19174 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19176 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19177 if (cxx_dialect
< cxx17
)
19178 pedwarn (comma
->location
, 0,
19179 "comma-separated list in using-declaration only available "
19180 "with -std=c++17 or -std=gnu++17");
19184 /* Look for the final `;'. */
19185 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19187 if (access_declaration_p
&& errorcount
== oldcount
)
19188 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19189 "access declarations are deprecated "
19190 "in favour of using-declarations; "
19191 "suggestion: add the %<using%> keyword");
19196 /* Parse an alias-declaration.
19199 using identifier attribute-specifier-seq [opt] = type-id */
19202 cp_parser_alias_declaration (cp_parser
* parser
)
19204 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19205 location_t id_location
, type_location
;
19206 cp_declarator
*declarator
;
19207 cp_decl_specifier_seq decl_specs
;
19209 const char *saved_message
= NULL
;
19211 /* Look for the `using' keyword. */
19212 cp_token
*using_token
19213 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19214 if (using_token
== NULL
)
19215 return error_mark_node
;
19217 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19218 id
= cp_parser_identifier (parser
);
19219 if (id
== error_mark_node
)
19220 return error_mark_node
;
19222 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19223 attributes
= cp_parser_attributes_opt (parser
);
19224 if (attributes
== error_mark_node
)
19225 return error_mark_node
;
19227 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19229 if (cp_parser_error_occurred (parser
))
19230 return error_mark_node
;
19232 cp_parser_commit_to_tentative_parse (parser
);
19234 /* Now we are going to parse the type-id of the declaration. */
19239 "A type-specifier-seq shall not define a class or enumeration
19240 unless it appears in the type-id of an alias-declaration (7.1.3) that
19241 is not the declaration of a template-declaration."
19243 In other words, if we currently are in an alias template, the
19244 type-id should not define a type.
19246 So let's set parser->type_definition_forbidden_message in that
19247 case; cp_parser_check_type_definition (called by
19248 cp_parser_class_specifier) will then emit an error if a type is
19249 defined in the type-id. */
19250 if (parser
->num_template_parameter_lists
)
19252 saved_message
= parser
->type_definition_forbidden_message
;
19253 parser
->type_definition_forbidden_message
=
19254 G_("types may not be defined in alias template declarations");
19257 type
= cp_parser_type_id (parser
, &type_location
);
19259 /* Restore the error message if need be. */
19260 if (parser
->num_template_parameter_lists
)
19261 parser
->type_definition_forbidden_message
= saved_message
;
19263 if (type
== error_mark_node
19264 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19266 cp_parser_skip_to_end_of_block_or_statement (parser
);
19267 return error_mark_node
;
19270 /* A typedef-name can also be introduced by an alias-declaration. The
19271 identifier following the using keyword becomes a typedef-name. It has
19272 the same semantics as if it were introduced by the typedef
19273 specifier. In particular, it does not define a new type and it shall
19274 not appear in the type-id. */
19276 clear_decl_specs (&decl_specs
);
19277 decl_specs
.type
= type
;
19278 if (attributes
!= NULL_TREE
)
19280 decl_specs
.attributes
= attributes
;
19281 set_and_check_decl_spec_loc (&decl_specs
,
19285 set_and_check_decl_spec_loc (&decl_specs
,
19288 set_and_check_decl_spec_loc (&decl_specs
,
19291 decl_specs
.locations
[ds_type_spec
] = type_location
;
19293 if (parser
->num_template_parameter_lists
19294 && !cp_parser_check_template_parameters (parser
,
19295 /*num_templates=*/0,
19296 /*template_id*/false,
19298 /*declarator=*/NULL
))
19299 return error_mark_node
;
19301 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19303 member_p
= at_class_scope_p ();
19305 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19306 NULL_TREE
, attributes
);
19308 decl
= start_decl (declarator
, &decl_specs
, 0,
19309 attributes
, NULL_TREE
, &pushed_scope
);
19310 if (decl
== error_mark_node
)
19313 // Attach constraints to the alias declaration.
19314 if (flag_concepts
&& current_template_parms
)
19316 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19317 tree constr
= build_constraints (reqs
, NULL_TREE
);
19318 set_constraints (decl
, constr
);
19321 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19324 pop_scope (pushed_scope
);
19326 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19327 added into the symbol table; otherwise, return the TYPE_DECL. */
19328 if (DECL_LANG_SPECIFIC (decl
)
19329 && DECL_TEMPLATE_INFO (decl
)
19330 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19332 decl
= DECL_TI_TEMPLATE (decl
);
19334 check_member_template (decl
);
19340 /* Parse a using-directive.
19343 using namespace :: [opt] nested-name-specifier [opt]
19344 namespace-name ; */
19347 cp_parser_using_directive (cp_parser
* parser
)
19349 tree namespace_decl
;
19352 /* Look for the `using' keyword. */
19353 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19354 /* And the `namespace' keyword. */
19355 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19356 /* Look for the optional `::' operator. */
19357 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19358 /* And the optional nested-name-specifier. */
19359 cp_parser_nested_name_specifier_opt (parser
,
19360 /*typename_keyword_p=*/false,
19361 /*check_dependency_p=*/true,
19363 /*is_declaration=*/true);
19364 /* Get the namespace being used. */
19365 namespace_decl
= cp_parser_namespace_name (parser
);
19366 /* And any specified attributes. */
19367 attribs
= cp_parser_attributes_opt (parser
);
19369 /* Update the symbol table. */
19370 if (namespace_bindings_p ())
19371 finish_namespace_using_directive (namespace_decl
, attribs
);
19373 finish_local_using_directive (namespace_decl
, attribs
);
19375 /* Look for the final `;'. */
19376 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19379 /* Parse an asm-definition.
19382 asm ( string-literal ) ;
19387 asm volatile [opt] ( string-literal ) ;
19388 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19389 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19390 : asm-operand-list [opt] ) ;
19391 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19392 : asm-operand-list [opt]
19393 : asm-clobber-list [opt] ) ;
19394 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19395 : asm-clobber-list [opt]
19396 : asm-goto-list ) ; */
19399 cp_parser_asm_definition (cp_parser
* parser
)
19402 tree outputs
= NULL_TREE
;
19403 tree inputs
= NULL_TREE
;
19404 tree clobbers
= NULL_TREE
;
19405 tree labels
= NULL_TREE
;
19407 bool volatile_p
= false;
19408 bool extended_p
= false;
19409 bool invalid_inputs_p
= false;
19410 bool invalid_outputs_p
= false;
19411 bool goto_p
= false;
19412 required_token missing
= RT_NONE
;
19414 /* Look for the `asm' keyword. */
19415 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19417 if (parser
->in_function_body
19418 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19420 error ("%<asm%> in %<constexpr%> function");
19421 cp_function_chain
->invalid_constexpr
= true;
19424 /* See if the next token is `volatile'. */
19425 if (cp_parser_allow_gnu_extensions_p (parser
)
19426 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19428 /* Remember that we saw the `volatile' keyword. */
19430 /* Consume the token. */
19431 cp_lexer_consume_token (parser
->lexer
);
19433 if (cp_parser_allow_gnu_extensions_p (parser
)
19434 && parser
->in_function_body
19435 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19437 /* Remember that we saw the `goto' keyword. */
19439 /* Consume the token. */
19440 cp_lexer_consume_token (parser
->lexer
);
19442 /* Look for the opening `('. */
19443 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19445 /* Look for the string. */
19446 string
= cp_parser_string_literal (parser
, false, false);
19447 if (string
== error_mark_node
)
19449 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19450 /*consume_paren=*/true);
19454 /* If we're allowing GNU extensions, check for the extended assembly
19455 syntax. Unfortunately, the `:' tokens need not be separated by
19456 a space in C, and so, for compatibility, we tolerate that here
19457 too. Doing that means that we have to treat the `::' operator as
19459 if (cp_parser_allow_gnu_extensions_p (parser
)
19460 && parser
->in_function_body
19461 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19462 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19464 bool inputs_p
= false;
19465 bool clobbers_p
= false;
19466 bool labels_p
= false;
19468 /* The extended syntax was used. */
19471 /* Look for outputs. */
19472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19474 /* Consume the `:'. */
19475 cp_lexer_consume_token (parser
->lexer
);
19476 /* Parse the output-operands. */
19477 if (cp_lexer_next_token_is_not (parser
->lexer
,
19479 && cp_lexer_next_token_is_not (parser
->lexer
,
19481 && cp_lexer_next_token_is_not (parser
->lexer
,
19485 outputs
= cp_parser_asm_operand_list (parser
);
19486 if (outputs
== error_mark_node
)
19487 invalid_outputs_p
= true;
19490 /* If the next token is `::', there are no outputs, and the
19491 next token is the beginning of the inputs. */
19492 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19493 /* The inputs are coming next. */
19496 /* Look for inputs. */
19498 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19500 /* Consume the `:' or `::'. */
19501 cp_lexer_consume_token (parser
->lexer
);
19502 /* Parse the output-operands. */
19503 if (cp_lexer_next_token_is_not (parser
->lexer
,
19505 && cp_lexer_next_token_is_not (parser
->lexer
,
19507 && cp_lexer_next_token_is_not (parser
->lexer
,
19510 inputs
= cp_parser_asm_operand_list (parser
);
19511 if (inputs
== error_mark_node
)
19512 invalid_inputs_p
= true;
19515 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19516 /* The clobbers are coming next. */
19519 /* Look for clobbers. */
19521 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19524 /* Consume the `:' or `::'. */
19525 cp_lexer_consume_token (parser
->lexer
);
19526 /* Parse the clobbers. */
19527 if (cp_lexer_next_token_is_not (parser
->lexer
,
19529 && cp_lexer_next_token_is_not (parser
->lexer
,
19531 clobbers
= cp_parser_asm_clobber_list (parser
);
19534 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19535 /* The labels are coming next. */
19538 /* Look for labels. */
19540 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19543 /* Consume the `:' or `::'. */
19544 cp_lexer_consume_token (parser
->lexer
);
19545 /* Parse the labels. */
19546 labels
= cp_parser_asm_label_list (parser
);
19549 if (goto_p
&& !labels_p
)
19550 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19553 missing
= RT_COLON_SCOPE
;
19555 /* Look for the closing `)'. */
19556 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19557 missing
? missing
: RT_CLOSE_PAREN
))
19558 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19559 /*consume_paren=*/true);
19560 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19562 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19564 /* Create the ASM_EXPR. */
19565 if (parser
->in_function_body
)
19567 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19568 inputs
, clobbers
, labels
);
19569 /* If the extended syntax was not used, mark the ASM_EXPR. */
19572 tree temp
= asm_stmt
;
19573 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19574 temp
= TREE_OPERAND (temp
, 0);
19576 ASM_INPUT_P (temp
) = 1;
19580 symtab
->finalize_toplevel_asm (string
);
19584 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19585 type that comes from the decl-specifier-seq. */
19588 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19590 for (cp_declarator
*d
= declarator
; d
;)
19600 if (TYPE_PTRMEMFUNC_P (type
))
19601 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19602 type
= TREE_TYPE (type
);
19610 /* Declarators [gram.dcl.decl] */
19612 /* Parse an init-declarator.
19615 declarator initializer [opt]
19620 declarator asm-specification [opt] attributes [opt] initializer [opt]
19622 function-definition:
19623 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19625 decl-specifier-seq [opt] declarator function-try-block
19629 function-definition:
19630 __extension__ function-definition
19634 function-definition:
19635 decl-specifier-seq [opt] declarator function-transaction-block
19637 The DECL_SPECIFIERS apply to this declarator. Returns a
19638 representation of the entity declared. If MEMBER_P is TRUE, then
19639 this declarator appears in a class scope. The new DECL created by
19640 this declarator is returned.
19642 The CHECKS are access checks that should be performed once we know
19643 what entity is being declared (and, therefore, what classes have
19646 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19647 for a function-definition here as well. If the declarator is a
19648 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19649 be TRUE upon return. By that point, the function-definition will
19650 have been completely parsed.
19652 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19655 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19656 parsed declaration if it is an uninitialized single declarator not followed
19657 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19658 if present, will not be consumed. If returned, this declarator will be
19659 created with SD_INITIALIZED but will not call cp_finish_decl.
19661 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19662 and there is an initializer, the pointed location_t is set to the
19663 location of the '=' or `(', or '{' in C++11 token introducing the
19667 cp_parser_init_declarator (cp_parser
* parser
,
19668 cp_decl_specifier_seq
*decl_specifiers
,
19669 vec
<deferred_access_check
, va_gc
> *checks
,
19670 bool function_definition_allowed_p
,
19672 int declares_class_or_enum
,
19673 bool* function_definition_p
,
19674 tree
* maybe_range_for_decl
,
19675 location_t
* init_loc
,
19678 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19679 *attributes_start_token
= NULL
;
19680 cp_declarator
*declarator
;
19681 tree prefix_attributes
;
19682 tree attributes
= NULL
;
19683 tree asm_specification
;
19685 tree decl
= NULL_TREE
;
19687 int is_initialized
;
19688 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19689 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19691 enum cpp_ttype initialization_kind
;
19692 bool is_direct_init
= false;
19693 bool is_non_constant_init
;
19694 int ctor_dtor_or_conv_p
;
19695 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19696 tree pushed_scope
= NULL_TREE
;
19697 bool range_for_decl_p
= false;
19698 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19699 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19701 /* Gather the attributes that were provided with the
19702 decl-specifiers. */
19703 prefix_attributes
= decl_specifiers
->attributes
;
19705 /* Assume that this is not the declarator for a function
19707 if (function_definition_p
)
19708 *function_definition_p
= false;
19710 /* Default arguments are only permitted for function parameters. */
19711 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19712 parser
->default_arg_ok_p
= false;
19714 /* Defer access checks while parsing the declarator; we cannot know
19715 what names are accessible until we know what is being
19717 resume_deferring_access_checks ();
19719 token
= cp_lexer_peek_token (parser
->lexer
);
19721 /* Parse the declarator. */
19723 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19724 &ctor_dtor_or_conv_p
,
19725 /*parenthesized_p=*/NULL
,
19726 member_p
, friend_p
);
19727 /* Gather up the deferred checks. */
19728 stop_deferring_access_checks ();
19730 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19732 /* If the DECLARATOR was erroneous, there's no need to go
19734 if (declarator
== cp_error_declarator
)
19735 return error_mark_node
;
19737 /* Check that the number of template-parameter-lists is OK. */
19738 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19740 return error_mark_node
;
19742 if (declares_class_or_enum
& 2)
19743 cp_parser_check_for_definition_in_return_type (declarator
,
19744 decl_specifiers
->type
,
19745 decl_specifiers
->locations
[ds_type_spec
]);
19747 /* Figure out what scope the entity declared by the DECLARATOR is
19748 located in. `grokdeclarator' sometimes changes the scope, so
19749 we compute it now. */
19750 scope
= get_scope_of_declarator (declarator
);
19752 /* Perform any lookups in the declared type which were thought to be
19753 dependent, but are not in the scope of the declarator. */
19754 decl_specifiers
->type
19755 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19757 /* If we're allowing GNU extensions, look for an
19758 asm-specification. */
19759 if (cp_parser_allow_gnu_extensions_p (parser
))
19761 /* Look for an asm-specification. */
19762 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19763 asm_specification
= cp_parser_asm_specification_opt (parser
);
19766 asm_specification
= NULL_TREE
;
19768 /* Look for attributes. */
19769 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19770 attributes
= cp_parser_attributes_opt (parser
);
19772 /* Peek at the next token. */
19773 token
= cp_lexer_peek_token (parser
->lexer
);
19775 bool bogus_implicit_tmpl
= false;
19777 if (function_declarator_p (declarator
))
19779 /* Handle C++17 deduction guides. */
19780 if (!decl_specifiers
->type
19781 && ctor_dtor_or_conv_p
<= 0
19782 && cxx_dialect
>= cxx17
)
19784 cp_declarator
*id
= get_id_declarator (declarator
);
19785 tree name
= id
->u
.id
.unqualified_name
;
19786 parser
->scope
= id
->u
.id
.qualifying_scope
;
19787 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19789 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19790 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19792 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19793 id
->u
.id
.sfk
= sfk_deduction_guide
;
19794 ctor_dtor_or_conv_p
= 1;
19798 /* Check to see if the token indicates the start of a
19799 function-definition. */
19800 if (cp_parser_token_starts_function_definition_p (token
))
19802 if (!function_definition_allowed_p
)
19804 /* If a function-definition should not appear here, issue an
19806 cp_parser_error (parser
,
19807 "a function-definition is not allowed here");
19808 return error_mark_node
;
19811 location_t func_brace_location
19812 = cp_lexer_peek_token (parser
->lexer
)->location
;
19814 /* Neither attributes nor an asm-specification are allowed
19815 on a function-definition. */
19816 if (asm_specification
)
19817 error_at (asm_spec_start_token
->location
,
19818 "an asm-specification is not allowed "
19819 "on a function-definition");
19821 error_at (attributes_start_token
->location
,
19822 "attributes are not allowed "
19823 "on a function-definition");
19824 /* This is a function-definition. */
19825 *function_definition_p
= true;
19827 /* Parse the function definition. */
19829 decl
= cp_parser_save_member_function_body (parser
,
19832 prefix_attributes
);
19835 (cp_parser_function_definition_from_specifiers_and_declarator
19836 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19838 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19840 /* This is where the prologue starts... */
19841 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19842 = func_brace_location
;
19848 else if (parser
->fully_implicit_function_template_p
)
19850 /* A non-template declaration involving a function parameter list
19851 containing an implicit template parameter will be made into a
19852 template. If the resulting declaration is not going to be an
19853 actual function then finish the template scope here to prevent it.
19854 An error message will be issued once we have a decl to talk about.
19856 FIXME probably we should do type deduction rather than create an
19857 implicit template, but the standard currently doesn't allow it. */
19858 bogus_implicit_tmpl
= true;
19859 finish_fully_implicit_template (parser
, NULL_TREE
);
19864 Only in function declarations for constructors, destructors, type
19865 conversions, and deduction guides can the decl-specifier-seq be omitted.
19867 We explicitly postpone this check past the point where we handle
19868 function-definitions because we tolerate function-definitions
19869 that are missing their return types in some modes. */
19870 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19872 cp_parser_error (parser
,
19873 "expected constructor, destructor, or type conversion");
19874 return error_mark_node
;
19877 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19878 if (token
->type
== CPP_EQ
19879 || token
->type
== CPP_OPEN_PAREN
19880 || token
->type
== CPP_OPEN_BRACE
)
19882 is_initialized
= SD_INITIALIZED
;
19883 initialization_kind
= token
->type
;
19884 if (maybe_range_for_decl
)
19885 *maybe_range_for_decl
= error_mark_node
;
19886 tmp_init_loc
= token
->location
;
19887 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19888 *init_loc
= tmp_init_loc
;
19890 if (token
->type
== CPP_EQ
19891 && function_declarator_p (declarator
))
19893 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19894 if (t2
->keyword
== RID_DEFAULT
)
19895 is_initialized
= SD_DEFAULTED
;
19896 else if (t2
->keyword
== RID_DELETE
)
19897 is_initialized
= SD_DELETED
;
19902 /* If the init-declarator isn't initialized and isn't followed by a
19903 `,' or `;', it's not a valid init-declarator. */
19904 if (token
->type
!= CPP_COMMA
19905 && token
->type
!= CPP_SEMICOLON
)
19907 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19908 range_for_decl_p
= true;
19911 if (!maybe_range_for_decl
)
19912 cp_parser_error (parser
, "expected initializer");
19913 return error_mark_node
;
19916 is_initialized
= SD_UNINITIALIZED
;
19917 initialization_kind
= CPP_EOF
;
19920 /* Because start_decl has side-effects, we should only call it if we
19921 know we're going ahead. By this point, we know that we cannot
19922 possibly be looking at any other construct. */
19923 cp_parser_commit_to_tentative_parse (parser
);
19925 /* Enter the newly declared entry in the symbol table. If we're
19926 processing a declaration in a class-specifier, we wait until
19927 after processing the initializer. */
19930 if (parser
->in_unbraced_linkage_specification_p
)
19931 decl_specifiers
->storage_class
= sc_extern
;
19932 decl
= start_decl (declarator
, decl_specifiers
,
19933 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19934 attributes
, prefix_attributes
, &pushed_scope
);
19935 cp_finalize_omp_declare_simd (parser
, decl
);
19936 cp_finalize_oacc_routine (parser
, decl
, false);
19937 /* Adjust location of decl if declarator->id_loc is more appropriate:
19938 set, and decl wasn't merged with another decl, in which case its
19939 location would be different from input_location, and more accurate. */
19941 && declarator
->id_loc
!= UNKNOWN_LOCATION
19942 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19943 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19946 /* Enter the SCOPE. That way unqualified names appearing in the
19947 initializer will be looked up in SCOPE. */
19948 pushed_scope
= push_scope (scope
);
19950 /* Perform deferred access control checks, now that we know in which
19951 SCOPE the declared entity resides. */
19952 if (!member_p
&& decl
)
19954 tree saved_current_function_decl
= NULL_TREE
;
19956 /* If the entity being declared is a function, pretend that we
19957 are in its scope. If it is a `friend', it may have access to
19958 things that would not otherwise be accessible. */
19959 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19961 saved_current_function_decl
= current_function_decl
;
19962 current_function_decl
= decl
;
19965 /* Perform access checks for template parameters. */
19966 cp_parser_perform_template_parameter_access_checks (checks
);
19968 /* Perform the access control checks for the declarator and the
19969 decl-specifiers. */
19970 perform_deferred_access_checks (tf_warning_or_error
);
19972 /* Restore the saved value. */
19973 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19974 current_function_decl
= saved_current_function_decl
;
19977 /* Parse the initializer. */
19978 initializer
= NULL_TREE
;
19979 is_direct_init
= false;
19980 is_non_constant_init
= true;
19981 if (is_initialized
)
19983 if (function_declarator_p (declarator
))
19985 if (initialization_kind
== CPP_EQ
)
19986 initializer
= cp_parser_pure_specifier (parser
);
19989 /* If the declaration was erroneous, we don't really
19990 know what the user intended, so just silently
19991 consume the initializer. */
19992 if (decl
!= error_mark_node
)
19993 error_at (tmp_init_loc
, "initializer provided for function");
19994 cp_parser_skip_to_closing_parenthesis (parser
,
19995 /*recovering=*/true,
19996 /*or_comma=*/false,
19997 /*consume_paren=*/true);
20002 /* We want to record the extra mangling scope for in-class
20003 initializers of class members and initializers of static data
20004 member templates. The former involves deferring
20005 parsing of the initializer until end of class as with default
20006 arguments. So right here we only handle the latter. */
20007 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20008 start_lambda_scope (decl
);
20009 initializer
= cp_parser_initializer (parser
,
20011 &is_non_constant_init
);
20012 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20013 finish_lambda_scope ();
20014 if (initializer
== error_mark_node
)
20015 cp_parser_skip_to_end_of_statement (parser
);
20019 /* The old parser allows attributes to appear after a parenthesized
20020 initializer. Mark Mitchell proposed removing this functionality
20021 on the GCC mailing lists on 2002-08-13. This parser accepts the
20022 attributes -- but ignores them. Made a permerror in GCC 8. */
20023 if (cp_parser_allow_gnu_extensions_p (parser
)
20024 && initialization_kind
== CPP_OPEN_PAREN
20025 && cp_parser_attributes_opt (parser
)
20026 && permerror (input_location
,
20027 "attributes after parenthesized initializer ignored"))
20030 if (flag_permissive
&& !hint
)
20033 inform (input_location
,
20034 "this flexibility is deprecated and will be removed");
20038 /* And now complain about a non-function implicit template. */
20039 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20040 error_at (DECL_SOURCE_LOCATION (decl
),
20041 "non-function %qD declared as implicit template", decl
);
20043 /* For an in-class declaration, use `grokfield' to create the
20049 pop_scope (pushed_scope
);
20050 pushed_scope
= NULL_TREE
;
20052 decl
= grokfield (declarator
, decl_specifiers
,
20053 initializer
, !is_non_constant_init
,
20054 /*asmspec=*/NULL_TREE
,
20055 attr_chainon (attributes
, prefix_attributes
));
20056 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20057 cp_parser_save_default_args (parser
, decl
);
20058 cp_finalize_omp_declare_simd (parser
, decl
);
20059 cp_finalize_oacc_routine (parser
, decl
, false);
20062 /* Finish processing the declaration. But, skip member
20064 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20066 cp_finish_decl (decl
,
20067 initializer
, !is_non_constant_init
,
20069 /* If the initializer is in parentheses, then this is
20070 a direct-initialization, which means that an
20071 `explicit' constructor is OK. Otherwise, an
20072 `explicit' constructor cannot be used. */
20073 ((is_direct_init
|| !is_initialized
)
20074 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20076 else if ((cxx_dialect
!= cxx98
) && friend_p
20077 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20078 /* Core issue #226 (C++0x only): A default template-argument
20079 shall not be specified in a friend class template
20081 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20082 /*is_partial=*/false, /*is_friend_decl=*/1);
20084 if (!friend_p
&& pushed_scope
)
20085 pop_scope (pushed_scope
);
20087 if (function_declarator_p (declarator
)
20088 && parser
->fully_implicit_function_template_p
)
20091 decl
= finish_fully_implicit_template (parser
, decl
);
20093 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20096 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20097 && type_uses_auto (decl_specifiers
->type
))
20098 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20103 /* Parse a declarator.
20107 ptr-operator declarator
20109 abstract-declarator:
20110 ptr-operator abstract-declarator [opt]
20111 direct-abstract-declarator
20116 attributes [opt] direct-declarator
20117 attributes [opt] ptr-operator declarator
20119 abstract-declarator:
20120 attributes [opt] ptr-operator abstract-declarator [opt]
20121 attributes [opt] direct-abstract-declarator
20123 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20124 detect constructors, destructors, deduction guides, or conversion operators.
20125 It is set to -1 if the declarator is a name, and +1 if it is a
20126 function. Otherwise it is set to zero. Usually you just want to
20127 test for >0, but internally the negative value is used.
20129 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20130 a decl-specifier-seq unless it declares a constructor, destructor,
20131 or conversion. It might seem that we could check this condition in
20132 semantic analysis, rather than parsing, but that makes it difficult
20133 to handle something like `f()'. We want to notice that there are
20134 no decl-specifiers, and therefore realize that this is an
20135 expression, not a declaration.)
20137 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20138 the declarator is a direct-declarator of the form "(...)".
20140 MEMBER_P is true iff this declarator is a member-declarator.
20142 FRIEND_P is true iff this declarator is a friend. */
20144 static cp_declarator
*
20145 cp_parser_declarator (cp_parser
* parser
,
20146 cp_parser_declarator_kind dcl_kind
,
20147 int* ctor_dtor_or_conv_p
,
20148 bool* parenthesized_p
,
20149 bool member_p
, bool friend_p
)
20151 cp_declarator
*declarator
;
20152 enum tree_code code
;
20153 cp_cv_quals cv_quals
;
20155 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20157 /* Assume this is not a constructor, destructor, or type-conversion
20159 if (ctor_dtor_or_conv_p
)
20160 *ctor_dtor_or_conv_p
= 0;
20162 if (cp_parser_allow_gnu_extensions_p (parser
))
20163 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20165 /* Check for the ptr-operator production. */
20166 cp_parser_parse_tentatively (parser
);
20167 /* Parse the ptr-operator. */
20168 code
= cp_parser_ptr_operator (parser
,
20173 /* If that worked, then we have a ptr-operator. */
20174 if (cp_parser_parse_definitely (parser
))
20176 /* If a ptr-operator was found, then this declarator was not
20178 if (parenthesized_p
)
20179 *parenthesized_p
= true;
20180 /* The dependent declarator is optional if we are parsing an
20181 abstract-declarator. */
20182 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20183 cp_parser_parse_tentatively (parser
);
20185 /* Parse the dependent declarator. */
20186 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20187 /*ctor_dtor_or_conv_p=*/NULL
,
20188 /*parenthesized_p=*/NULL
,
20189 /*member_p=*/false,
20192 /* If we are parsing an abstract-declarator, we must handle the
20193 case where the dependent declarator is absent. */
20194 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20195 && !cp_parser_parse_definitely (parser
))
20198 declarator
= cp_parser_make_indirect_declarator
20199 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20201 /* Everything else is a direct-declarator. */
20204 if (parenthesized_p
)
20205 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20207 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20208 ctor_dtor_or_conv_p
,
20209 member_p
, friend_p
);
20212 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20213 declarator
->attributes
= gnu_attributes
;
20217 /* Parse a direct-declarator or direct-abstract-declarator.
20221 direct-declarator ( parameter-declaration-clause )
20222 cv-qualifier-seq [opt]
20223 ref-qualifier [opt]
20224 exception-specification [opt]
20225 direct-declarator [ constant-expression [opt] ]
20228 direct-abstract-declarator:
20229 direct-abstract-declarator [opt]
20230 ( parameter-declaration-clause )
20231 cv-qualifier-seq [opt]
20232 ref-qualifier [opt]
20233 exception-specification [opt]
20234 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20235 ( abstract-declarator )
20237 Returns a representation of the declarator. DCL_KIND is
20238 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20239 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20240 we are parsing a direct-declarator. It is
20241 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20242 of ambiguity we prefer an abstract declarator, as per
20243 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20244 as for cp_parser_declarator. */
20246 static cp_declarator
*
20247 cp_parser_direct_declarator (cp_parser
* parser
,
20248 cp_parser_declarator_kind dcl_kind
,
20249 int* ctor_dtor_or_conv_p
,
20250 bool member_p
, bool friend_p
)
20253 cp_declarator
*declarator
= NULL
;
20254 tree scope
= NULL_TREE
;
20255 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20256 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20258 tree pushed_scope
= NULL_TREE
;
20259 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20263 /* Peek at the next token. */
20264 token
= cp_lexer_peek_token (parser
->lexer
);
20265 if (token
->type
== CPP_OPEN_PAREN
)
20267 /* This is either a parameter-declaration-clause, or a
20268 parenthesized declarator. When we know we are parsing a
20269 named declarator, it must be a parenthesized declarator
20270 if FIRST is true. For instance, `(int)' is a
20271 parameter-declaration-clause, with an omitted
20272 direct-abstract-declarator. But `((*))', is a
20273 parenthesized abstract declarator. Finally, when T is a
20274 template parameter `(T)' is a
20275 parameter-declaration-clause, and not a parenthesized
20278 We first try and parse a parameter-declaration-clause,
20279 and then try a nested declarator (if FIRST is true).
20281 It is not an error for it not to be a
20282 parameter-declaration-clause, even when FIRST is
20288 The first is the declaration of a function while the
20289 second is the definition of a variable, including its
20292 Having seen only the parenthesis, we cannot know which of
20293 these two alternatives should be selected. Even more
20294 complex are examples like:
20299 The former is a function-declaration; the latter is a
20300 variable initialization.
20302 Thus again, we try a parameter-declaration-clause, and if
20303 that fails, we back out and return. */
20305 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20308 bool is_declarator
= false;
20312 /* In a member-declarator, the only valid interpretation
20313 of a parenthesis is the start of a
20314 parameter-declaration-clause. (It is invalid to
20315 initialize a static data member with a parenthesized
20316 initializer; only the "=" form of initialization is
20319 cp_parser_parse_tentatively (parser
);
20321 /* Consume the `('. */
20322 matching_parens parens
;
20323 parens
.consume_open (parser
);
20326 /* If this is going to be an abstract declarator, we're
20327 in a declarator and we can't have default args. */
20328 parser
->default_arg_ok_p
= false;
20329 parser
->in_declarator_p
= true;
20332 begin_scope (sk_function_parms
, NULL_TREE
);
20334 /* Parse the parameter-declaration-clause. */
20335 params
= cp_parser_parameter_declaration_clause (parser
);
20337 /* Consume the `)'. */
20338 parens
.require_close (parser
);
20340 /* If all went well, parse the cv-qualifier-seq,
20341 ref-qualifier and the exception-specification. */
20342 if (member_p
|| cp_parser_parse_definitely (parser
))
20344 cp_cv_quals cv_quals
;
20345 cp_virt_specifiers virt_specifiers
;
20346 cp_ref_qualifier ref_qual
;
20347 tree exception_specification
;
20350 bool memfn
= (member_p
|| (pushed_scope
20351 && CLASS_TYPE_P (pushed_scope
)));
20353 is_declarator
= true;
20355 if (ctor_dtor_or_conv_p
)
20356 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20359 /* Parse the cv-qualifier-seq. */
20360 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20361 /* Parse the ref-qualifier. */
20362 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20363 /* Parse the tx-qualifier. */
20364 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20365 /* And the exception-specification. */
20366 exception_specification
20367 = cp_parser_exception_specification_opt (parser
);
20369 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20371 /* In here, we handle cases where attribute is used after
20372 the function declaration. For example:
20373 void func (int x) __attribute__((vector(..))); */
20374 tree gnu_attrs
= NULL_TREE
;
20375 tree requires_clause
= NULL_TREE
;
20376 late_return
= (cp_parser_late_return_type_opt
20377 (parser
, declarator
, requires_clause
,
20378 memfn
? cv_quals
: -1));
20380 /* Parse the virt-specifier-seq. */
20381 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20383 /* Create the function-declarator. */
20384 declarator
= make_call_declarator (declarator
,
20390 exception_specification
,
20393 declarator
->std_attributes
= attrs
;
20394 declarator
->attributes
= gnu_attrs
;
20395 /* Any subsequent parameter lists are to do with
20396 return type, so are not those of the declared
20398 parser
->default_arg_ok_p
= false;
20401 /* Remove the function parms from scope. */
20402 pop_bindings_and_leave_scope ();
20405 /* Repeat the main loop. */
20409 /* If this is the first, we can try a parenthesized
20413 bool saved_in_type_id_in_expr_p
;
20415 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20416 parser
->in_declarator_p
= saved_in_declarator_p
;
20418 open_paren
= token
;
20419 /* Consume the `('. */
20420 matching_parens parens
;
20421 parens
.consume_open (parser
);
20422 /* Parse the nested declarator. */
20423 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20424 parser
->in_type_id_in_expr_p
= true;
20426 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20427 /*parenthesized_p=*/NULL
,
20428 member_p
, friend_p
);
20429 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20431 /* Expect a `)'. */
20432 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20433 if (!parens
.require_close (parser
))
20434 declarator
= cp_error_declarator
;
20435 if (declarator
== cp_error_declarator
)
20438 goto handle_declarator
;
20440 /* Otherwise, we must be done. */
20444 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20445 && token
->type
== CPP_OPEN_SQUARE
20446 && !cp_next_tokens_can_be_attribute_p (parser
))
20448 /* Parse an array-declarator. */
20449 tree bounds
, attrs
;
20451 if (ctor_dtor_or_conv_p
)
20452 *ctor_dtor_or_conv_p
= 0;
20456 parser
->default_arg_ok_p
= false;
20457 parser
->in_declarator_p
= true;
20458 /* Consume the `['. */
20459 cp_lexer_consume_token (parser
->lexer
);
20460 /* Peek at the next token. */
20461 token
= cp_lexer_peek_token (parser
->lexer
);
20462 /* If the next token is `]', then there is no
20463 constant-expression. */
20464 if (token
->type
!= CPP_CLOSE_SQUARE
)
20466 bool non_constant_p
;
20468 = cp_parser_constant_expression (parser
,
20469 /*allow_non_constant=*/true,
20471 if (!non_constant_p
)
20473 else if (error_operand_p (bounds
))
20474 /* Already gave an error. */;
20475 else if (!parser
->in_function_body
20476 || current_binding_level
->kind
== sk_function_parms
)
20478 /* Normally, the array bound must be an integral constant
20479 expression. However, as an extension, we allow VLAs
20480 in function scopes as long as they aren't part of a
20481 parameter declaration. */
20482 cp_parser_error (parser
,
20483 "array bound is not an integer constant");
20484 bounds
= error_mark_node
;
20486 else if (processing_template_decl
20487 && !type_dependent_expression_p (bounds
))
20489 /* Remember this wasn't a constant-expression. */
20490 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20491 TREE_SIDE_EFFECTS (bounds
) = 1;
20495 bounds
= NULL_TREE
;
20496 /* Look for the closing `]'. */
20497 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20499 declarator
= cp_error_declarator
;
20503 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20504 declarator
= make_array_declarator (declarator
, bounds
);
20505 declarator
->std_attributes
= attrs
;
20507 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20510 tree qualifying_scope
;
20511 tree unqualified_name
;
20513 special_function_kind sfk
;
20515 bool pack_expansion_p
= false;
20516 cp_token
*declarator_id_start_token
;
20518 /* Parse a declarator-id */
20519 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20522 cp_parser_parse_tentatively (parser
);
20524 /* If we see an ellipsis, we should be looking at a
20526 if (token
->type
== CPP_ELLIPSIS
)
20528 /* Consume the `...' */
20529 cp_lexer_consume_token (parser
->lexer
);
20531 pack_expansion_p
= true;
20535 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20537 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20538 qualifying_scope
= parser
->scope
;
20543 if (!unqualified_name
&& pack_expansion_p
)
20545 /* Check whether an error occurred. */
20546 okay
= !cp_parser_error_occurred (parser
);
20548 /* We already consumed the ellipsis to mark a
20549 parameter pack, but we have no way to report it,
20550 so abort the tentative parse. We will be exiting
20551 immediately anyway. */
20552 cp_parser_abort_tentative_parse (parser
);
20555 okay
= cp_parser_parse_definitely (parser
);
20558 unqualified_name
= error_mark_node
;
20559 else if (unqualified_name
20560 && (qualifying_scope
20561 || (!identifier_p (unqualified_name
))))
20563 cp_parser_error (parser
, "expected unqualified-id");
20564 unqualified_name
= error_mark_node
;
20568 if (!unqualified_name
)
20570 if (unqualified_name
== error_mark_node
)
20572 declarator
= cp_error_declarator
;
20573 pack_expansion_p
= false;
20574 declarator
->parameter_pack_p
= false;
20578 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20580 if (qualifying_scope
&& at_namespace_scope_p ()
20581 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20583 /* In the declaration of a member of a template class
20584 outside of the class itself, the SCOPE will sometimes
20585 be a TYPENAME_TYPE. For example, given:
20587 template <typename T>
20588 int S<T>::R::i = 3;
20590 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20591 this context, we must resolve S<T>::R to an ordinary
20592 type, rather than a typename type.
20594 The reason we normally avoid resolving TYPENAME_TYPEs
20595 is that a specialization of `S' might render
20596 `S<T>::R' not a type. However, if `S' is
20597 specialized, then this `i' will not be used, so there
20598 is no harm in resolving the types here. */
20601 /* Resolve the TYPENAME_TYPE. */
20602 type
= resolve_typename_type (qualifying_scope
,
20603 /*only_current_p=*/false);
20604 /* If that failed, the declarator is invalid. */
20605 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20607 if (typedef_variant_p (type
))
20608 error_at (declarator_id_start_token
->location
,
20609 "cannot define member of dependent typedef "
20612 error_at (declarator_id_start_token
->location
,
20613 "%<%T::%E%> is not a type",
20614 TYPE_CONTEXT (qualifying_scope
),
20615 TYPE_IDENTIFIER (qualifying_scope
));
20617 qualifying_scope
= type
;
20622 if (unqualified_name
)
20626 if (qualifying_scope
20627 && CLASS_TYPE_P (qualifying_scope
))
20628 class_type
= qualifying_scope
;
20630 class_type
= current_class_type
;
20632 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20634 tree name_type
= TREE_TYPE (unqualified_name
);
20636 if (!class_type
|| !same_type_p (name_type
, class_type
))
20638 /* We do not attempt to print the declarator
20639 here because we do not have enough
20640 information about its original syntactic
20642 cp_parser_error (parser
, "invalid declarator");
20643 declarator
= cp_error_declarator
;
20646 else if (qualifying_scope
20647 && CLASSTYPE_USE_TEMPLATE (name_type
))
20649 error_at (declarator_id_start_token
->location
,
20650 "invalid use of constructor as a template");
20651 inform (declarator_id_start_token
->location
,
20652 "use %<%T::%D%> instead of %<%T::%D%> to "
20653 "name the constructor in a qualified name",
20655 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20656 class_type
, name_type
);
20657 declarator
= cp_error_declarator
;
20660 unqualified_name
= constructor_name (class_type
);
20665 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20666 sfk
= sfk_destructor
;
20667 else if (identifier_p (unqualified_name
)
20668 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20669 sfk
= sfk_conversion
;
20670 else if (/* There's no way to declare a constructor
20671 for an unnamed type, even if the type
20672 got a name for linkage purposes. */
20673 !TYPE_WAS_UNNAMED (class_type
)
20674 /* Handle correctly (c++/19200):
20688 friend void N::S();
20690 && (!friend_p
|| class_type
== qualifying_scope
)
20691 && constructor_name_p (unqualified_name
,
20693 sfk
= sfk_constructor
;
20694 else if (is_overloaded_fn (unqualified_name
)
20695 && DECL_CONSTRUCTOR_P (get_first_fn
20696 (unqualified_name
)))
20697 sfk
= sfk_constructor
;
20699 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20700 *ctor_dtor_or_conv_p
= -1;
20703 declarator
= make_id_declarator (qualifying_scope
,
20705 sfk
, token
->location
);
20706 declarator
->std_attributes
= attrs
;
20707 declarator
->parameter_pack_p
= pack_expansion_p
;
20709 if (pack_expansion_p
)
20710 maybe_warn_variadic_templates ();
20713 handle_declarator
:;
20714 scope
= get_scope_of_declarator (declarator
);
20717 /* Any names that appear after the declarator-id for a
20718 member are looked up in the containing scope. */
20719 if (at_function_scope_p ())
20721 /* But declarations with qualified-ids can't appear in a
20723 cp_parser_error (parser
, "qualified-id in declaration");
20724 declarator
= cp_error_declarator
;
20727 pushed_scope
= push_scope (scope
);
20729 parser
->in_declarator_p
= true;
20730 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20731 || (declarator
&& declarator
->kind
== cdk_id
))
20732 /* Default args are only allowed on function
20734 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20736 parser
->default_arg_ok_p
= false;
20745 /* For an abstract declarator, we might wind up with nothing at this
20746 point. That's an error; the declarator is not optional. */
20748 cp_parser_error (parser
, "expected declarator");
20749 else if (open_paren
)
20751 /* Record overly parenthesized declarator so we can give a
20752 diagnostic about confusing decl/expr disambiguation. */
20753 if (declarator
->kind
== cdk_array
)
20755 /* If the open and close parens are on different lines, this
20756 is probably a formatting thing, so ignore. */
20757 expanded_location open
= expand_location (open_paren
->location
);
20758 expanded_location close
= expand_location (close_paren
->location
);
20759 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20763 declarator
->parenthesized
= open_paren
->location
;
20766 /* If we entered a scope, we must exit it now. */
20768 pop_scope (pushed_scope
);
20770 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20771 parser
->in_declarator_p
= saved_in_declarator_p
;
20776 /* Parse a ptr-operator.
20779 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20780 * cv-qualifier-seq [opt]
20782 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20783 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20788 & cv-qualifier-seq [opt]
20790 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20791 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20792 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20793 filled in with the TYPE containing the member. *CV_QUALS is
20794 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20795 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20796 Note that the tree codes returned by this function have nothing
20797 to do with the types of trees that will be eventually be created
20798 to represent the pointer or reference type being parsed. They are
20799 just constants with suggestive names. */
20800 static enum tree_code
20801 cp_parser_ptr_operator (cp_parser
* parser
,
20803 cp_cv_quals
*cv_quals
,
20806 enum tree_code code
= ERROR_MARK
;
20808 tree attrs
= NULL_TREE
;
20810 /* Assume that it's not a pointer-to-member. */
20812 /* And that there are no cv-qualifiers. */
20813 *cv_quals
= TYPE_UNQUALIFIED
;
20815 /* Peek at the next token. */
20816 token
= cp_lexer_peek_token (parser
->lexer
);
20818 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20819 if (token
->type
== CPP_MULT
)
20820 code
= INDIRECT_REF
;
20821 else if (token
->type
== CPP_AND
)
20823 else if ((cxx_dialect
!= cxx98
) &&
20824 token
->type
== CPP_AND_AND
) /* C++0x only */
20825 code
= NON_LVALUE_EXPR
;
20827 if (code
!= ERROR_MARK
)
20829 /* Consume the `*', `&' or `&&'. */
20830 cp_lexer_consume_token (parser
->lexer
);
20832 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20833 `&', if we are allowing GNU extensions. (The only qualifier
20834 that can legally appear after `&' is `restrict', but that is
20835 enforced during semantic analysis. */
20836 if (code
== INDIRECT_REF
20837 || cp_parser_allow_gnu_extensions_p (parser
))
20838 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20840 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20841 if (attributes
!= NULL
)
20842 *attributes
= attrs
;
20846 /* Try the pointer-to-member case. */
20847 cp_parser_parse_tentatively (parser
);
20848 /* Look for the optional `::' operator. */
20849 cp_parser_global_scope_opt (parser
,
20850 /*current_scope_valid_p=*/false);
20851 /* Look for the nested-name specifier. */
20852 token
= cp_lexer_peek_token (parser
->lexer
);
20853 cp_parser_nested_name_specifier (parser
,
20854 /*typename_keyword_p=*/false,
20855 /*check_dependency_p=*/true,
20857 /*is_declaration=*/false);
20858 /* If we found it, and the next token is a `*', then we are
20859 indeed looking at a pointer-to-member operator. */
20860 if (!cp_parser_error_occurred (parser
)
20861 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20863 /* Indicate that the `*' operator was used. */
20864 code
= INDIRECT_REF
;
20866 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20867 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20868 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20869 error_at (token
->location
, "cannot form pointer to member of "
20870 "non-class %q#T", parser
->scope
);
20873 /* The type of which the member is a member is given by the
20875 *type
= parser
->scope
;
20876 /* The next name will not be qualified. */
20877 parser
->scope
= NULL_TREE
;
20878 parser
->qualifying_scope
= NULL_TREE
;
20879 parser
->object_scope
= NULL_TREE
;
20880 /* Look for optional c++11 attributes. */
20881 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20882 if (attributes
!= NULL
)
20883 *attributes
= attrs
;
20884 /* Look for the optional cv-qualifier-seq. */
20885 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20888 /* If that didn't work we don't have a ptr-operator. */
20889 if (!cp_parser_parse_definitely (parser
))
20890 cp_parser_error (parser
, "expected ptr-operator");
20896 /* Parse an (optional) cv-qualifier-seq.
20899 cv-qualifier cv-qualifier-seq [opt]
20910 Returns a bitmask representing the cv-qualifiers. */
20913 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20915 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20920 cp_cv_quals cv_qualifier
;
20922 /* Peek at the next token. */
20923 token
= cp_lexer_peek_token (parser
->lexer
);
20924 /* See if it's a cv-qualifier. */
20925 switch (token
->keyword
)
20928 cv_qualifier
= TYPE_QUAL_CONST
;
20932 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20936 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20940 cv_qualifier
= TYPE_UNQUALIFIED
;
20947 if (cv_quals
& cv_qualifier
)
20949 gcc_rich_location
richloc (token
->location
);
20950 richloc
.add_fixit_remove ();
20951 error_at (&richloc
, "duplicate cv-qualifier");
20952 cp_lexer_purge_token (parser
->lexer
);
20956 cp_lexer_consume_token (parser
->lexer
);
20957 cv_quals
|= cv_qualifier
;
20964 /* Parse an (optional) ref-qualifier
20970 Returns cp_ref_qualifier representing ref-qualifier. */
20972 static cp_ref_qualifier
20973 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20975 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20977 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20978 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20983 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20984 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20986 switch (token
->type
)
20989 curr_ref_qual
= REF_QUAL_LVALUE
;
20993 curr_ref_qual
= REF_QUAL_RVALUE
;
20997 curr_ref_qual
= REF_QUAL_NONE
;
21001 if (!curr_ref_qual
)
21005 error_at (token
->location
, "multiple ref-qualifiers");
21006 cp_lexer_purge_token (parser
->lexer
);
21010 ref_qual
= curr_ref_qual
;
21011 cp_lexer_consume_token (parser
->lexer
);
21018 /* Parse an optional tx-qualifier.
21022 transaction_safe_dynamic */
21025 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21027 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21028 if (token
->type
== CPP_NAME
)
21030 tree name
= token
->u
.value
;
21031 const char *p
= IDENTIFIER_POINTER (name
);
21032 const int len
= strlen ("transaction_safe");
21033 if (!strncmp (p
, "transaction_safe", len
))
21037 || !strcmp (p
, "_dynamic"))
21039 cp_lexer_consume_token (parser
->lexer
);
21042 error ("%qE requires %<-fgnu-tm%>", name
);
21053 /* Parse an (optional) virt-specifier-seq.
21055 virt-specifier-seq:
21056 virt-specifier virt-specifier-seq [opt]
21062 Returns a bitmask representing the virt-specifiers. */
21064 static cp_virt_specifiers
21065 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21067 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21072 cp_virt_specifiers virt_specifier
;
21074 /* Peek at the next token. */
21075 token
= cp_lexer_peek_token (parser
->lexer
);
21076 /* See if it's a virt-specifier-qualifier. */
21077 if (token
->type
!= CPP_NAME
)
21079 if (id_equal (token
->u
.value
, "override"))
21081 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21082 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21084 else if (id_equal (token
->u
.value
, "final"))
21086 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21087 virt_specifier
= VIRT_SPEC_FINAL
;
21089 else if (id_equal (token
->u
.value
, "__final"))
21091 virt_specifier
= VIRT_SPEC_FINAL
;
21096 if (virt_specifiers
& virt_specifier
)
21098 gcc_rich_location
richloc (token
->location
);
21099 richloc
.add_fixit_remove ();
21100 error_at (&richloc
, "duplicate virt-specifier");
21101 cp_lexer_purge_token (parser
->lexer
);
21105 cp_lexer_consume_token (parser
->lexer
);
21106 virt_specifiers
|= virt_specifier
;
21109 return virt_specifiers
;
21112 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21113 is in scope even though it isn't real. */
21116 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21120 if (current_class_ptr
)
21122 /* We don't clear this between NSDMIs. Is it already what we want? */
21123 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21124 if (DECL_P (current_class_ptr
)
21125 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21126 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21127 && cp_type_quals (type
) == quals
)
21131 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21132 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21133 current_class_ptr
= NULL_TREE
;
21135 = cp_build_fold_indirect_ref (this_parm
);
21136 current_class_ptr
= this_parm
;
21139 /* Return true iff our current scope is a non-static data member
21143 parsing_nsdmi (void)
21145 /* We recognize NSDMI context by the context-less 'this' pointer set up
21146 by the function above. */
21147 if (current_class_ptr
21148 && TREE_CODE (current_class_ptr
) == PARM_DECL
21149 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21154 /* Parse a late-specified return type, if any. This is not a separate
21155 non-terminal, but part of a function declarator, which looks like
21157 -> trailing-type-specifier-seq abstract-declarator(opt)
21159 Returns the type indicated by the type-id.
21161 In addition to this, parse any queued up #pragma omp declare simd
21162 clauses, and #pragma acc routine clauses.
21164 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21168 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21169 tree
& requires_clause
, cp_cv_quals quals
)
21172 tree type
= NULL_TREE
;
21173 bool declare_simd_p
= (parser
->omp_declare_simd
21175 && declarator
->kind
== cdk_id
);
21177 bool oacc_routine_p
= (parser
->oacc_routine
21179 && declarator
->kind
== cdk_id
);
21181 /* Peek at the next token. */
21182 token
= cp_lexer_peek_token (parser
->lexer
);
21183 /* A late-specified return type is indicated by an initial '->'. */
21184 if (token
->type
!= CPP_DEREF
21185 && token
->keyword
!= RID_REQUIRES
21186 && !(token
->type
== CPP_NAME
21187 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21188 && !(declare_simd_p
|| oacc_routine_p
))
21191 tree save_ccp
= current_class_ptr
;
21192 tree save_ccr
= current_class_ref
;
21195 /* DR 1207: 'this' is in scope in the trailing return type. */
21196 inject_this_parameter (current_class_type
, quals
);
21199 if (token
->type
== CPP_DEREF
)
21201 /* Consume the ->. */
21202 cp_lexer_consume_token (parser
->lexer
);
21204 type
= cp_parser_trailing_type_id (parser
);
21207 /* Function declarations may be followed by a trailing
21208 requires-clause. */
21209 requires_clause
= cp_parser_requires_clause_opt (parser
);
21211 if (declare_simd_p
)
21212 declarator
->attributes
21213 = cp_parser_late_parsing_omp_declare_simd (parser
,
21214 declarator
->attributes
);
21215 if (oacc_routine_p
)
21216 declarator
->attributes
21217 = cp_parser_late_parsing_oacc_routine (parser
,
21218 declarator
->attributes
);
21222 current_class_ptr
= save_ccp
;
21223 current_class_ref
= save_ccr
;
21229 /* Parse a declarator-id.
21233 :: [opt] nested-name-specifier [opt] type-name
21235 In the `id-expression' case, the value returned is as for
21236 cp_parser_id_expression if the id-expression was an unqualified-id.
21237 If the id-expression was a qualified-id, then a SCOPE_REF is
21238 returned. The first operand is the scope (either a NAMESPACE_DECL
21239 or TREE_TYPE), but the second is still just a representation of an
21243 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21246 /* The expression must be an id-expression. Assume that qualified
21247 names are the names of types so that:
21250 int S<T>::R::i = 3;
21252 will work; we must treat `S<T>::R' as the name of a type.
21253 Similarly, assume that qualified names are templates, where
21257 int S<T>::R<T>::i = 3;
21260 id
= cp_parser_id_expression (parser
,
21261 /*template_keyword_p=*/false,
21262 /*check_dependency_p=*/false,
21263 /*template_p=*/NULL
,
21264 /*declarator_p=*/true,
21266 if (id
&& BASELINK_P (id
))
21267 id
= BASELINK_FUNCTIONS (id
);
21271 /* Parse a type-id.
21274 type-specifier-seq abstract-declarator [opt]
21276 Returns the TYPE specified. */
21279 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21280 bool is_trailing_return
, location_t
* type_location
)
21282 cp_decl_specifier_seq type_specifier_seq
;
21283 cp_declarator
*abstract_declarator
;
21285 /* Parse the type-specifier-seq. */
21286 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21287 is_trailing_return
,
21288 &type_specifier_seq
);
21290 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21292 if (is_template_arg
&& type_specifier_seq
.type
21293 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21294 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21295 /* A bare template name as a template argument is a template template
21296 argument, not a placeholder, so fail parsing it as a type argument. */
21298 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21299 cp_parser_simulate_error (parser
);
21300 return error_mark_node
;
21302 if (type_specifier_seq
.type
== error_mark_node
)
21303 return error_mark_node
;
21305 /* There might or might not be an abstract declarator. */
21306 cp_parser_parse_tentatively (parser
);
21307 /* Look for the declarator. */
21308 abstract_declarator
21309 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21310 /*parenthesized_p=*/NULL
,
21311 /*member_p=*/false,
21312 /*friend_p=*/false);
21313 /* Check to see if there really was a declarator. */
21314 if (!cp_parser_parse_definitely (parser
))
21315 abstract_declarator
= NULL
;
21317 if (type_specifier_seq
.type
21318 /* The concepts TS allows 'auto' as a type-id. */
21319 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21320 /* None of the valid uses of 'auto' in C++14 involve the type-id
21321 nonterminal, but it is valid in a trailing-return-type. */
21322 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21323 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21325 /* A type-id with type 'auto' is only ok if the abstract declarator
21326 is a function declarator with a late-specified return type.
21328 A type-id with 'auto' is also valid in a trailing-return-type
21329 in a compound-requirement. */
21330 if (abstract_declarator
21331 && abstract_declarator
->kind
== cdk_function
21332 && abstract_declarator
->u
.function
.late_return_type
)
21334 else if (parser
->in_result_type_constraint_p
)
21338 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21339 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21341 error_at (loc
, "missing template arguments after %qT",
21343 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21347 error_at (loc
, "invalid use of %qT", auto_node
);
21348 return error_mark_node
;
21352 return groktypename (&type_specifier_seq
, abstract_declarator
,
21357 cp_parser_type_id (cp_parser
*parser
, location_t
* type_location
)
21359 return cp_parser_type_id_1 (parser
, false, false, type_location
);
21363 cp_parser_template_type_arg (cp_parser
*parser
)
21366 const char *saved_message
= parser
->type_definition_forbidden_message
;
21367 parser
->type_definition_forbidden_message
21368 = G_("types may not be defined in template arguments");
21369 r
= cp_parser_type_id_1 (parser
, true, false, NULL
);
21370 parser
->type_definition_forbidden_message
= saved_message
;
21371 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21373 error ("invalid use of %<auto%> in template argument");
21374 r
= error_mark_node
;
21380 cp_parser_trailing_type_id (cp_parser
*parser
)
21382 return cp_parser_type_id_1 (parser
, false, true, NULL
);
21385 /* Parse a type-specifier-seq.
21387 type-specifier-seq:
21388 type-specifier type-specifier-seq [opt]
21392 type-specifier-seq:
21393 attributes type-specifier-seq [opt]
21395 If IS_DECLARATION is true, we are at the start of a "condition" or
21396 exception-declaration, so we might be followed by a declarator-id.
21398 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21399 i.e. we've just seen "->".
21401 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21404 cp_parser_type_specifier_seq (cp_parser
* parser
,
21405 bool is_declaration
,
21406 bool is_trailing_return
,
21407 cp_decl_specifier_seq
*type_specifier_seq
)
21409 bool seen_type_specifier
= false;
21410 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21411 cp_token
*start_token
= NULL
;
21413 /* Clear the TYPE_SPECIFIER_SEQ. */
21414 clear_decl_specs (type_specifier_seq
);
21416 /* In the context of a trailing return type, enum E { } is an
21417 elaborated-type-specifier followed by a function-body, not an
21419 if (is_trailing_return
)
21420 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21422 /* Parse the type-specifiers and attributes. */
21425 tree type_specifier
;
21426 bool is_cv_qualifier
;
21428 /* Check for attributes first. */
21429 if (cp_next_tokens_can_be_attribute_p (parser
))
21431 type_specifier_seq
->attributes
21432 = attr_chainon (type_specifier_seq
->attributes
,
21433 cp_parser_attributes_opt (parser
));
21437 /* record the token of the beginning of the type specifier seq,
21438 for error reporting purposes*/
21440 start_token
= cp_lexer_peek_token (parser
->lexer
);
21442 /* Look for the type-specifier. */
21443 type_specifier
= cp_parser_type_specifier (parser
,
21445 type_specifier_seq
,
21446 /*is_declaration=*/false,
21449 if (!type_specifier
)
21451 /* If the first type-specifier could not be found, this is not a
21452 type-specifier-seq at all. */
21453 if (!seen_type_specifier
)
21455 /* Set in_declarator_p to avoid skipping to the semicolon. */
21456 int in_decl
= parser
->in_declarator_p
;
21457 parser
->in_declarator_p
= true;
21459 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21460 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21461 cp_parser_error (parser
, "expected type-specifier");
21463 parser
->in_declarator_p
= in_decl
;
21465 type_specifier_seq
->type
= error_mark_node
;
21468 /* If subsequent type-specifiers could not be found, the
21469 type-specifier-seq is complete. */
21473 seen_type_specifier
= true;
21474 /* The standard says that a condition can be:
21476 type-specifier-seq declarator = assignment-expression
21483 we should treat the "S" as a declarator, not as a
21484 type-specifier. The standard doesn't say that explicitly for
21485 type-specifier-seq, but it does say that for
21486 decl-specifier-seq in an ordinary declaration. Perhaps it
21487 would be clearer just to allow a decl-specifier-seq here, and
21488 then add a semantic restriction that if any decl-specifiers
21489 that are not type-specifiers appear, the program is invalid. */
21490 if (is_declaration
&& !is_cv_qualifier
)
21491 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21495 /* Return whether the function currently being declared has an associated
21496 template parameter list. */
21499 function_being_declared_is_template_p (cp_parser
* parser
)
21501 if (!current_template_parms
|| processing_template_parmlist
)
21504 if (parser
->implicit_template_scope
)
21507 if (at_class_scope_p ()
21508 && TYPE_BEING_DEFINED (current_class_type
))
21509 return parser
->num_template_parameter_lists
!= 0;
21511 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21512 (current_class_type
));
21515 /* Parse a parameter-declaration-clause.
21517 parameter-declaration-clause:
21518 parameter-declaration-list [opt] ... [opt]
21519 parameter-declaration-list , ...
21521 Returns a representation for the parameter declarations. A return
21522 value of NULL indicates a parameter-declaration-clause consisting
21523 only of an ellipsis. */
21526 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21532 temp_override
<bool> cleanup
21533 (parser
->auto_is_implicit_function_template_parm_p
);
21535 if (!processing_specialization
21536 && !processing_template_parmlist
21537 && !processing_explicit_instantiation
21538 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21539 actual function or a random abstract declarator. */
21540 && parser
->default_arg_ok_p
)
21541 if (!current_function_decl
21542 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21543 parser
->auto_is_implicit_function_template_parm_p
= true;
21545 /* Peek at the next token. */
21546 token
= cp_lexer_peek_token (parser
->lexer
);
21547 /* Check for trivial parameter-declaration-clauses. */
21548 if (token
->type
== CPP_ELLIPSIS
)
21550 /* Consume the `...' token. */
21551 cp_lexer_consume_token (parser
->lexer
);
21554 else if (token
->type
== CPP_CLOSE_PAREN
)
21555 /* There are no parameters. */
21556 return void_list_node
;
21557 /* Check for `(void)', too, which is a special case. */
21558 else if (token
->keyword
== RID_VOID
21559 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21560 == CPP_CLOSE_PAREN
))
21562 /* Consume the `void' token. */
21563 cp_lexer_consume_token (parser
->lexer
);
21564 /* There are no parameters. */
21565 return void_list_node
;
21568 /* Parse the parameter-declaration-list. */
21569 parameters
= cp_parser_parameter_declaration_list (parser
);
21570 /* If a parse error occurred while parsing the
21571 parameter-declaration-list, then the entire
21572 parameter-declaration-clause is erroneous. */
21573 if (parameters
== error_mark_node
)
21576 /* Peek at the next token. */
21577 token
= cp_lexer_peek_token (parser
->lexer
);
21578 /* If it's a `,', the clause should terminate with an ellipsis. */
21579 if (token
->type
== CPP_COMMA
)
21581 /* Consume the `,'. */
21582 cp_lexer_consume_token (parser
->lexer
);
21583 /* Expect an ellipsis. */
21585 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21587 /* It might also be `...' if the optional trailing `,' was
21589 else if (token
->type
== CPP_ELLIPSIS
)
21591 /* Consume the `...' token. */
21592 cp_lexer_consume_token (parser
->lexer
);
21593 /* And remember that we saw it. */
21597 ellipsis_p
= false;
21599 /* Finish the parameter list. */
21601 parameters
= chainon (parameters
, void_list_node
);
21606 /* Parse a parameter-declaration-list.
21608 parameter-declaration-list:
21609 parameter-declaration
21610 parameter-declaration-list , parameter-declaration
21612 Returns a representation of the parameter-declaration-list, as for
21613 cp_parser_parameter_declaration_clause. However, the
21614 `void_list_node' is never appended to the list. */
21617 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21619 tree parameters
= NULL_TREE
;
21620 tree
*tail
= ¶meters
;
21621 bool saved_in_unbraced_linkage_specification_p
;
21624 /* The special considerations that apply to a function within an
21625 unbraced linkage specifications do not apply to the parameters
21626 to the function. */
21627 saved_in_unbraced_linkage_specification_p
21628 = parser
->in_unbraced_linkage_specification_p
;
21629 parser
->in_unbraced_linkage_specification_p
= false;
21631 /* Look for more parameters. */
21634 cp_parameter_declarator
*parameter
;
21635 tree decl
= error_mark_node
;
21636 bool parenthesized_p
= false;
21638 /* Parse the parameter. */
21640 = cp_parser_parameter_declaration (parser
,
21641 /*template_parm_p=*/false,
21644 /* We don't know yet if the enclosing context is deprecated, so wait
21645 and warn in grokparms if appropriate. */
21646 deprecated_state
= DEPRECATED_SUPPRESS
;
21650 decl
= grokdeclarator (parameter
->declarator
,
21651 ¶meter
->decl_specifiers
,
21653 parameter
->default_argument
!= NULL_TREE
,
21654 ¶meter
->decl_specifiers
.attributes
);
21655 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21656 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21659 deprecated_state
= DEPRECATED_NORMAL
;
21661 /* If a parse error occurred parsing the parameter declaration,
21662 then the entire parameter-declaration-list is erroneous. */
21663 if (decl
== error_mark_node
)
21665 parameters
= error_mark_node
;
21669 if (parameter
->decl_specifiers
.attributes
)
21670 cplus_decl_attributes (&decl
,
21671 parameter
->decl_specifiers
.attributes
,
21673 if (DECL_NAME (decl
))
21674 decl
= pushdecl (decl
);
21676 if (decl
!= error_mark_node
)
21678 retrofit_lang_decl (decl
);
21679 DECL_PARM_INDEX (decl
) = ++index
;
21680 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21683 /* Add the new parameter to the list. */
21684 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21685 tail
= &TREE_CHAIN (*tail
);
21687 /* Peek at the next token. */
21688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21689 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21690 /* These are for Objective-C++ */
21691 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21692 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21693 /* The parameter-declaration-list is complete. */
21695 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21699 /* Peek at the next token. */
21700 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21701 /* If it's an ellipsis, then the list is complete. */
21702 if (token
->type
== CPP_ELLIPSIS
)
21704 /* Otherwise, there must be more parameters. Consume the
21706 cp_lexer_consume_token (parser
->lexer
);
21707 /* When parsing something like:
21709 int i(float f, double d)
21711 we can tell after seeing the declaration for "f" that we
21712 are not looking at an initialization of a variable "i",
21713 but rather at the declaration of a function "i".
21715 Due to the fact that the parsing of template arguments
21716 (as specified to a template-id) requires backtracking we
21717 cannot use this technique when inside a template argument
21719 if (!parser
->in_template_argument_list_p
21720 && !parser
->in_type_id_in_expr_p
21721 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21722 /* However, a parameter-declaration of the form
21723 "float(f)" (which is a valid declaration of a
21724 parameter "f") can also be interpreted as an
21725 expression (the conversion of "f" to "float"). */
21726 && !parenthesized_p
)
21727 cp_parser_commit_to_tentative_parse (parser
);
21731 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21733 cp_parser_skip_to_closing_parenthesis (parser
,
21734 /*recovering=*/true,
21735 /*or_comma=*/false,
21736 /*consume_paren=*/false);
21741 parser
->in_unbraced_linkage_specification_p
21742 = saved_in_unbraced_linkage_specification_p
;
21744 /* Reset implicit_template_scope if we are about to leave the function
21745 parameter list that introduced it. Note that for out-of-line member
21746 definitions, there will be one or more class scopes before we get to
21747 the template parameter scope. */
21749 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21750 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21752 while (maybe_its
->kind
== sk_class
)
21753 maybe_its
= maybe_its
->level_chain
;
21754 if (maybe_its
== its
)
21756 parser
->implicit_template_parms
= 0;
21757 parser
->implicit_template_scope
= 0;
21764 /* Parse a parameter declaration.
21766 parameter-declaration:
21767 decl-specifier-seq ... [opt] declarator
21768 decl-specifier-seq declarator = assignment-expression
21769 decl-specifier-seq ... [opt] abstract-declarator [opt]
21770 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21772 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21773 declares a template parameter. (In that case, a non-nested `>'
21774 token encountered during the parsing of the assignment-expression
21775 is not interpreted as a greater-than operator.)
21777 Returns a representation of the parameter, or NULL if an error
21778 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21779 true iff the declarator is of the form "(p)". */
21781 static cp_parameter_declarator
*
21782 cp_parser_parameter_declaration (cp_parser
*parser
,
21783 bool template_parm_p
,
21784 bool *parenthesized_p
)
21786 int declares_class_or_enum
;
21787 cp_decl_specifier_seq decl_specifiers
;
21788 cp_declarator
*declarator
;
21789 tree default_argument
;
21790 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21791 const char *saved_message
;
21792 bool template_parameter_pack_p
= false;
21794 /* In a template parameter, `>' is not an operator.
21798 When parsing a default template-argument for a non-type
21799 template-parameter, the first non-nested `>' is taken as the end
21800 of the template parameter-list rather than a greater-than
21803 /* Type definitions may not appear in parameter types. */
21804 saved_message
= parser
->type_definition_forbidden_message
;
21805 parser
->type_definition_forbidden_message
21806 = G_("types may not be defined in parameter types");
21808 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21809 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21810 (current_template_parms
)) : 0);
21812 /* Parse the declaration-specifiers. */
21813 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21814 cp_parser_decl_specifier_seq (parser
,
21815 CP_PARSER_FLAGS_NONE
,
21817 &declares_class_or_enum
);
21819 /* Complain about missing 'typename' or other invalid type names. */
21820 if (!decl_specifiers
.any_type_specifiers_p
21821 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21822 decl_specifiers
.type
= error_mark_node
;
21824 /* If an error occurred, there's no reason to attempt to parse the
21825 rest of the declaration. */
21826 if (cp_parser_error_occurred (parser
))
21828 parser
->type_definition_forbidden_message
= saved_message
;
21832 /* Peek at the next token. */
21833 token
= cp_lexer_peek_token (parser
->lexer
);
21835 /* If the next token is a `)', `,', `=', `>', or `...', then there
21836 is no declarator. However, when variadic templates are enabled,
21837 there may be a declarator following `...'. */
21838 if (token
->type
== CPP_CLOSE_PAREN
21839 || token
->type
== CPP_COMMA
21840 || token
->type
== CPP_EQ
21841 || token
->type
== CPP_GREATER
)
21844 if (parenthesized_p
)
21845 *parenthesized_p
= false;
21847 /* Otherwise, there should be a declarator. */
21850 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21851 parser
->default_arg_ok_p
= false;
21853 /* After seeing a decl-specifier-seq, if the next token is not a
21854 "(", there is no possibility that the code is a valid
21855 expression. Therefore, if parsing tentatively, we commit at
21857 if (!parser
->in_template_argument_list_p
21858 /* In an expression context, having seen:
21862 we cannot be sure whether we are looking at a
21863 function-type (taking a "char" as a parameter) or a cast
21864 of some object of type "char" to "int". */
21865 && !parser
->in_type_id_in_expr_p
21866 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21867 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21868 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21869 cp_parser_commit_to_tentative_parse (parser
);
21870 /* Parse the declarator. */
21871 declarator_token_start
= token
;
21872 declarator
= cp_parser_declarator (parser
,
21873 CP_PARSER_DECLARATOR_EITHER
,
21874 /*ctor_dtor_or_conv_p=*/NULL
,
21876 /*member_p=*/false,
21877 /*friend_p=*/false);
21878 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21879 /* After the declarator, allow more attributes. */
21880 decl_specifiers
.attributes
21881 = attr_chainon (decl_specifiers
.attributes
,
21882 cp_parser_attributes_opt (parser
));
21884 /* If the declarator is a template parameter pack, remember that and
21885 clear the flag in the declarator itself so we don't get errors
21886 from grokdeclarator. */
21887 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21889 declarator
->parameter_pack_p
= false;
21890 template_parameter_pack_p
= true;
21894 /* If the next token is an ellipsis, and we have not seen a declarator
21895 name, and if either the type of the declarator contains parameter
21896 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21897 for, eg, abbreviated integral type names), then we actually have a
21898 parameter pack expansion expression. Otherwise, leave the ellipsis
21899 for a C-style variadic function. */
21900 token
= cp_lexer_peek_token (parser
->lexer
);
21902 /* If a function parameter pack was specified and an implicit template
21903 parameter was introduced during cp_parser_parameter_declaration,
21904 change any implicit parameters introduced into packs. */
21905 if (parser
->implicit_template_parms
21906 && ((token
->type
== CPP_ELLIPSIS
21907 && declarator_can_be_parameter_pack (declarator
))
21908 || (declarator
&& declarator
->parameter_pack_p
)))
21910 int latest_template_parm_idx
= TREE_VEC_LENGTH
21911 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21913 if (latest_template_parm_idx
!= template_parm_idx
)
21914 decl_specifiers
.type
= convert_generic_types_to_packs
21915 (decl_specifiers
.type
,
21916 template_parm_idx
, latest_template_parm_idx
);
21919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21921 tree type
= decl_specifiers
.type
;
21923 if (type
&& DECL_P (type
))
21924 type
= TREE_TYPE (type
);
21927 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21928 && (template_parm_p
|| uses_parameter_packs (type
)))
21929 || (!type
&& template_parm_p
))
21930 && declarator_can_be_parameter_pack (declarator
))
21932 /* Consume the `...'. */
21933 cp_lexer_consume_token (parser
->lexer
);
21934 maybe_warn_variadic_templates ();
21936 /* Build a pack expansion type */
21937 if (template_parm_p
)
21938 template_parameter_pack_p
= true;
21939 else if (declarator
)
21940 declarator
->parameter_pack_p
= true;
21942 decl_specifiers
.type
= make_pack_expansion (type
);
21946 /* The restriction on defining new types applies only to the type
21947 of the parameter, not to the default argument. */
21948 parser
->type_definition_forbidden_message
= saved_message
;
21950 /* If the next token is `=', then process a default argument. */
21951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21953 tree type
= decl_specifiers
.type
;
21954 token
= cp_lexer_peek_token (parser
->lexer
);
21955 /* If we are defining a class, then the tokens that make up the
21956 default argument must be saved and processed later. */
21957 if (!template_parm_p
&& at_class_scope_p ()
21958 && TYPE_BEING_DEFINED (current_class_type
)
21959 && !LAMBDA_TYPE_P (current_class_type
))
21960 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21962 // A constrained-type-specifier may declare a type template-parameter.
21963 else if (declares_constrained_type_template_parameter (type
))
21965 = cp_parser_default_type_template_argument (parser
);
21967 // A constrained-type-specifier may declare a template-template-parameter.
21968 else if (declares_constrained_template_template_parameter (type
))
21970 = cp_parser_default_template_template_argument (parser
);
21972 /* Outside of a class definition, we can just parse the
21973 assignment-expression. */
21976 = cp_parser_default_argument (parser
, template_parm_p
);
21978 if (!parser
->default_arg_ok_p
)
21980 permerror (token
->location
,
21981 "default arguments are only "
21982 "permitted for function parameters");
21984 else if ((declarator
&& declarator
->parameter_pack_p
)
21985 || template_parameter_pack_p
21986 || (decl_specifiers
.type
21987 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21989 /* Find the name of the parameter pack. */
21990 cp_declarator
*id_declarator
= declarator
;
21991 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21992 id_declarator
= id_declarator
->declarator
;
21994 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21995 error_at (declarator_token_start
->location
,
21997 ? G_("template parameter pack %qD "
21998 "cannot have a default argument")
21999 : G_("parameter pack %qD cannot have "
22000 "a default argument"),
22001 id_declarator
->u
.id
.unqualified_name
);
22003 error_at (declarator_token_start
->location
,
22005 ? G_("template parameter pack cannot have "
22006 "a default argument")
22007 : G_("parameter pack cannot have a "
22008 "default argument"));
22010 default_argument
= NULL_TREE
;
22014 default_argument
= NULL_TREE
;
22016 /* Generate a location for the parameter, ranging from the start of the
22017 initial token to the end of the final token (using input_location for
22018 the latter, set up by cp_lexer_set_source_position_from_token when
22021 If we have a identifier, then use it for the caret location, e.g.
22023 extern int callee (int one, int (*two)(int, int), float three);
22024 ~~~~~~^~~~~~~~~~~~~~
22026 otherwise, reuse the start location for the caret location e.g.:
22028 extern int callee (int one, int (*)(int, int), float three);
22032 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22033 ? declarator
->id_loc
22034 : decl_spec_token_start
->location
);
22035 location_t param_loc
= make_location (caret_loc
,
22036 decl_spec_token_start
->location
,
22039 return make_parameter_declarator (&decl_specifiers
,
22043 template_parameter_pack_p
);
22046 /* Parse a default argument and return it.
22048 TEMPLATE_PARM_P is true if this is a default argument for a
22049 non-type template parameter. */
22051 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22053 tree default_argument
= NULL_TREE
;
22054 bool saved_greater_than_is_operator_p
;
22055 bool saved_local_variables_forbidden_p
;
22056 bool non_constant_p
, is_direct_init
;
22058 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22060 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22061 parser
->greater_than_is_operator_p
= !template_parm_p
;
22062 /* Local variable names (and the `this' keyword) may not
22063 appear in a default argument. */
22064 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22065 parser
->local_variables_forbidden_p
= true;
22066 /* Parse the assignment-expression. */
22067 if (template_parm_p
)
22068 push_deferring_access_checks (dk_no_deferred
);
22069 tree saved_class_ptr
= NULL_TREE
;
22070 tree saved_class_ref
= NULL_TREE
;
22071 /* The "this" pointer is not valid in a default argument. */
22074 saved_class_ptr
= current_class_ptr
;
22075 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22076 saved_class_ref
= current_class_ref
;
22077 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22080 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22081 /* Restore the "this" pointer. */
22084 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22085 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22087 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22088 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22089 if (template_parm_p
)
22090 pop_deferring_access_checks ();
22091 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22092 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22094 return default_argument
;
22097 /* Parse a function-body.
22100 compound_statement */
22103 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22105 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22106 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22110 /* Parse a ctor-initializer-opt followed by a function-body. Return
22111 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22112 is true we are parsing a function-try-block. */
22115 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22116 bool in_function_try_block
)
22119 const bool check_body_p
=
22120 DECL_CONSTRUCTOR_P (current_function_decl
)
22121 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22124 /* Begin the function body. */
22125 body
= begin_function_body ();
22126 /* Parse the optional ctor-initializer. */
22127 cp_parser_ctor_initializer_opt (parser
);
22129 /* If we're parsing a constexpr constructor definition, we need
22130 to check that the constructor body is indeed empty. However,
22131 before we get to cp_parser_function_body lot of junk has been
22132 generated, so we can't just check that we have an empty block.
22133 Rather we take a snapshot of the outermost block, and check whether
22134 cp_parser_function_body changed its state. */
22137 list
= cur_stmt_list
;
22138 if (STATEMENT_LIST_TAIL (list
))
22139 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22141 /* Parse the function-body. */
22142 cp_parser_function_body (parser
, in_function_try_block
);
22144 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22145 /* Finish the function body. */
22146 finish_function_body (body
);
22149 /* Parse an initializer.
22152 = initializer-clause
22153 ( expression-list )
22155 Returns an expression representing the initializer. If no
22156 initializer is present, NULL_TREE is returned.
22158 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22159 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22160 set to TRUE if there is no initializer present. If there is an
22161 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22162 is set to true; otherwise it is set to false. */
22165 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22166 bool* non_constant_p
, bool subexpression_p
)
22171 /* Peek at the next token. */
22172 token
= cp_lexer_peek_token (parser
->lexer
);
22174 /* Let our caller know whether or not this initializer was
22176 *is_direct_init
= (token
->type
!= CPP_EQ
);
22177 /* Assume that the initializer is constant. */
22178 *non_constant_p
= false;
22180 if (token
->type
== CPP_EQ
)
22182 /* Consume the `='. */
22183 cp_lexer_consume_token (parser
->lexer
);
22184 /* Parse the initializer-clause. */
22185 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22187 else if (token
->type
== CPP_OPEN_PAREN
)
22189 vec
<tree
, va_gc
> *vec
;
22190 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22192 /*allow_expansion_p=*/true,
22195 return error_mark_node
;
22196 init
= build_tree_list_vec (vec
);
22197 release_tree_vector (vec
);
22199 else if (token
->type
== CPP_OPEN_BRACE
)
22201 cp_lexer_set_source_position (parser
->lexer
);
22202 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22203 init
= cp_parser_braced_list (parser
, non_constant_p
);
22204 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22208 /* Anything else is an error. */
22209 cp_parser_error (parser
, "expected initializer");
22210 init
= error_mark_node
;
22213 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22214 init
= error_mark_node
;
22219 /* Parse an initializer-clause.
22221 initializer-clause:
22222 assignment-expression
22225 Returns an expression representing the initializer.
22227 If the `assignment-expression' production is used the value
22228 returned is simply a representation for the expression.
22230 Otherwise, calls cp_parser_braced_list. */
22233 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22235 cp_expr initializer
;
22237 /* Assume the expression is constant. */
22238 *non_constant_p
= false;
22240 /* If it is not a `{', then we are looking at an
22241 assignment-expression. */
22242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22245 = cp_parser_constant_expression (parser
,
22246 /*allow_non_constant_p=*/true,
22250 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22252 return initializer
;
22255 /* Parse a brace-enclosed initializer list.
22258 { initializer-list , [opt] }
22259 { designated-initializer-list , [opt] }
22262 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22263 the elements of the initializer-list (or NULL, if the last
22264 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22265 NULL_TREE. There is no way to detect whether or not the optional
22266 trailing `,' was provided. NON_CONSTANT_P is as for
22267 cp_parser_initializer. */
22270 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22273 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22275 /* Consume the `{' token. */
22276 matching_braces braces
;
22277 braces
.require_open (parser
);
22278 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22279 initializer
= make_node (CONSTRUCTOR
);
22280 /* If it's not a `}', then there is a non-trivial initializer. */
22281 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22283 /* Parse the initializer list. */
22284 CONSTRUCTOR_ELTS (initializer
)
22285 = cp_parser_initializer_list (parser
, non_constant_p
);
22286 /* A trailing `,' token is allowed. */
22287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22288 cp_lexer_consume_token (parser
->lexer
);
22291 *non_constant_p
= false;
22292 /* Now, there should be a trailing `}'. */
22293 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22294 braces
.require_close (parser
);
22295 TREE_TYPE (initializer
) = init_list_type_node
;
22297 cp_expr
result (initializer
);
22298 /* Build a location of the form:
22301 with caret==start at the open brace, finish at the close brace. */
22302 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22303 result
.set_location (combined_loc
);
22307 /* Consume tokens up to, and including, the next non-nested closing `]'.
22308 Returns true iff we found a closing `]'. */
22311 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22313 unsigned square_depth
= 0;
22317 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22319 switch (token
->type
)
22322 case CPP_PRAGMA_EOL
:
22323 /* If we've run out of tokens, then there is no closing `]'. */
22326 case CPP_OPEN_SQUARE
:
22330 case CPP_CLOSE_SQUARE
:
22331 if (!square_depth
--)
22333 cp_lexer_consume_token (parser
->lexer
);
22342 /* Consume the token. */
22343 cp_lexer_consume_token (parser
->lexer
);
22347 /* Return true if we are looking at an array-designator, false otherwise. */
22350 cp_parser_array_designator_p (cp_parser
*parser
)
22352 /* Consume the `['. */
22353 cp_lexer_consume_token (parser
->lexer
);
22355 cp_lexer_save_tokens (parser
->lexer
);
22357 /* Skip tokens until the next token is a closing square bracket.
22358 If we find the closing `]', and the next token is a `=', then
22359 we are looking at an array designator. */
22360 bool array_designator_p
22361 = (cp_parser_skip_to_closing_square_bracket (parser
)
22362 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22364 /* Roll back the tokens we skipped. */
22365 cp_lexer_rollback_tokens (parser
->lexer
);
22367 return array_designator_p
;
22370 /* Parse an initializer-list.
22373 initializer-clause ... [opt]
22374 initializer-list , initializer-clause ... [opt]
22378 designated-initializer-list:
22379 designated-initializer-clause
22380 designated-initializer-list , designated-initializer-clause
22382 designated-initializer-clause:
22383 designator brace-or-equal-initializer
22391 designation initializer-clause ...[opt]
22392 initializer-list , designation initializer-clause ...[opt]
22397 [ constant-expression ] =
22399 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22400 for the initializer. If the INDEX of the elt is non-NULL, it is the
22401 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22402 as for cp_parser_initializer. */
22404 static vec
<constructor_elt
, va_gc
> *
22405 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22407 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22408 bool first_p
= true;
22409 tree first_designator
= NULL_TREE
;
22411 /* Assume all of the expressions are constant. */
22412 *non_constant_p
= false;
22414 /* Parse the rest of the list. */
22420 bool clause_non_constant_p
;
22421 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22423 /* Handle the C++2A syntax, '. id ='. */
22424 if ((cxx_dialect
>= cxx2a
22425 || cp_parser_allow_gnu_extensions_p (parser
))
22426 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22427 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22428 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22429 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22430 == CPP_OPEN_BRACE
)))
22432 if (cxx_dialect
< cxx2a
)
22433 pedwarn (loc
, OPT_Wpedantic
,
22434 "C++ designated initializers only available with "
22435 "-std=c++2a or -std=gnu++2a");
22436 /* Consume the `.'. */
22437 cp_lexer_consume_token (parser
->lexer
);
22438 /* Consume the identifier. */
22439 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22441 /* Consume the `='. */
22442 cp_lexer_consume_token (parser
->lexer
);
22444 /* Also, if the next token is an identifier and the following one is a
22445 colon, we are looking at the GNU designated-initializer
22447 else if (cp_parser_allow_gnu_extensions_p (parser
)
22448 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22449 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22452 /* Warn the user that they are using an extension. */
22453 pedwarn (loc
, OPT_Wpedantic
,
22454 "ISO C++ does not allow GNU designated initializers");
22455 /* Consume the identifier. */
22456 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22457 /* Consume the `:'. */
22458 cp_lexer_consume_token (parser
->lexer
);
22460 /* Also handle C99 array designators, '[ const ] ='. */
22461 else if (cp_parser_allow_gnu_extensions_p (parser
)
22462 && !c_dialect_objc ()
22463 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22465 /* In C++11, [ could start a lambda-introducer. */
22466 bool non_const
= false;
22468 cp_parser_parse_tentatively (parser
);
22470 if (!cp_parser_array_designator_p (parser
))
22472 cp_parser_simulate_error (parser
);
22473 designator
= NULL_TREE
;
22477 designator
= cp_parser_constant_expression (parser
, true,
22479 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22480 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22483 if (!cp_parser_parse_definitely (parser
))
22484 designator
= NULL_TREE
;
22486 && (!require_potential_rvalue_constant_expression
22488 designator
= NULL_TREE
;
22490 /* Warn the user that they are using an extension. */
22491 pedwarn (loc
, OPT_Wpedantic
,
22492 "ISO C++ does not allow C99 designated initializers");
22495 designator
= NULL_TREE
;
22499 first_designator
= designator
;
22502 else if (cxx_dialect
>= cxx2a
22503 && first_designator
!= error_mark_node
22504 && (!first_designator
!= !designator
))
22506 error_at (loc
, "either all initializer clauses should be designated "
22507 "or none of them should be");
22508 first_designator
= error_mark_node
;
22510 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22511 first_designator
= designator
;
22513 /* Parse the initializer. */
22514 initializer
= cp_parser_initializer_clause (parser
,
22515 &clause_non_constant_p
);
22516 /* If any clause is non-constant, so is the entire initializer. */
22517 if (clause_non_constant_p
)
22518 *non_constant_p
= true;
22520 /* If we have an ellipsis, this is an initializer pack
22522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22524 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22526 /* Consume the `...'. */
22527 cp_lexer_consume_token (parser
->lexer
);
22529 if (designator
&& cxx_dialect
>= cxx2a
)
22531 "%<...%> not allowed in designated initializer list");
22533 /* Turn the initializer into an initializer expansion. */
22534 initializer
= make_pack_expansion (initializer
);
22537 /* Add it to the vector. */
22538 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22540 /* If the next token is not a comma, we have reached the end of
22542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22545 /* Peek at the next token. */
22546 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22547 /* If the next token is a `}', then we're still done. An
22548 initializer-clause can have a trailing `,' after the
22549 initializer-list and before the closing `}'. */
22550 if (token
->type
== CPP_CLOSE_BRACE
)
22553 /* Consume the `,' token. */
22554 cp_lexer_consume_token (parser
->lexer
);
22557 /* The same identifier shall not appear in multiple designators
22558 of a designated-initializer-list. */
22559 if (first_designator
)
22562 tree designator
, val
;
22563 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22564 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22566 if (IDENTIFIER_MARKED (designator
))
22568 error_at (cp_expr_loc_or_loc (val
, input_location
),
22569 "%<.%s%> designator used multiple times in "
22570 "the same initializer list",
22571 IDENTIFIER_POINTER (designator
));
22572 (*v
)[i
].index
= error_mark_node
;
22575 IDENTIFIER_MARKED (designator
) = 1;
22577 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22578 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22579 IDENTIFIER_MARKED (designator
) = 0;
22585 /* Classes [gram.class] */
22587 /* Parse a class-name.
22593 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22594 to indicate that names looked up in dependent types should be
22595 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22596 keyword has been used to indicate that the name that appears next
22597 is a template. TAG_TYPE indicates the explicit tag given before
22598 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22599 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22600 is the class being defined in a class-head. If ENUM_OK is TRUE,
22601 enum-names are also accepted.
22603 Returns the TYPE_DECL representing the class. */
22606 cp_parser_class_name (cp_parser
*parser
,
22607 bool typename_keyword_p
,
22608 bool template_keyword_p
,
22609 enum tag_types tag_type
,
22610 bool check_dependency_p
,
22612 bool is_declaration
,
22619 tree identifier
= NULL_TREE
;
22621 /* All class-names start with an identifier. */
22622 token
= cp_lexer_peek_token (parser
->lexer
);
22623 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22625 cp_parser_error (parser
, "expected class-name");
22626 return error_mark_node
;
22629 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22630 to a template-id, so we save it here. */
22631 scope
= parser
->scope
;
22632 if (scope
== error_mark_node
)
22633 return error_mark_node
;
22635 /* Any name names a type if we're following the `typename' keyword
22636 in a qualified name where the enclosing scope is type-dependent. */
22637 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22638 && dependent_type_p (scope
));
22639 /* Handle the common case (an identifier, but not a template-id)
22641 if (token
->type
== CPP_NAME
22642 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22644 cp_token
*identifier_token
;
22647 /* Look for the identifier. */
22648 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22649 ambiguous_p
= identifier_token
->error_reported
;
22650 identifier
= cp_parser_identifier (parser
);
22651 /* If the next token isn't an identifier, we are certainly not
22652 looking at a class-name. */
22653 if (identifier
== error_mark_node
)
22654 decl
= error_mark_node
;
22655 /* If we know this is a type-name, there's no need to look it
22657 else if (typename_p
)
22661 tree ambiguous_decls
;
22662 /* If we already know that this lookup is ambiguous, then
22663 we've already issued an error message; there's no reason
22667 cp_parser_simulate_error (parser
);
22668 return error_mark_node
;
22670 /* If the next token is a `::', then the name must be a type
22673 [basic.lookup.qual]
22675 During the lookup for a name preceding the :: scope
22676 resolution operator, object, function, and enumerator
22677 names are ignored. */
22678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22679 tag_type
= scope_type
;
22680 /* Look up the name. */
22681 decl
= cp_parser_lookup_name (parser
, identifier
,
22683 /*is_template=*/false,
22684 /*is_namespace=*/false,
22685 check_dependency_p
,
22687 identifier_token
->location
);
22688 if (ambiguous_decls
)
22690 if (cp_parser_parsing_tentatively (parser
))
22691 cp_parser_simulate_error (parser
);
22692 return error_mark_node
;
22698 /* Try a template-id. */
22699 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22700 check_dependency_p
,
22703 if (decl
== error_mark_node
)
22704 return error_mark_node
;
22707 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22709 /* If this is a typename, create a TYPENAME_TYPE. */
22710 if (typename_p
&& decl
!= error_mark_node
)
22712 decl
= make_typename_type (scope
, decl
, typename_type
,
22713 /*complain=*/tf_error
);
22714 if (decl
!= error_mark_node
)
22715 decl
= TYPE_NAME (decl
);
22718 decl
= strip_using_decl (decl
);
22720 /* Check to see that it is really the name of a class. */
22721 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22722 && identifier_p (TREE_OPERAND (decl
, 0))
22723 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22724 /* Situations like this:
22726 template <typename T> struct A {
22727 typename T::template X<int>::I i;
22730 are problematic. Is `T::template X<int>' a class-name? The
22731 standard does not seem to be definitive, but there is no other
22732 valid interpretation of the following `::'. Therefore, those
22733 names are considered class-names. */
22735 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22736 if (decl
!= error_mark_node
)
22737 decl
= TYPE_NAME (decl
);
22739 else if (TREE_CODE (decl
) != TYPE_DECL
22740 || TREE_TYPE (decl
) == error_mark_node
22741 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22742 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22743 /* In Objective-C 2.0, a classname followed by '.' starts a
22744 dot-syntax expression, and it's not a type-name. */
22745 || (c_dialect_objc ()
22746 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22747 && objc_is_class_name (decl
)))
22748 decl
= error_mark_node
;
22750 if (decl
== error_mark_node
)
22751 cp_parser_error (parser
, "expected class-name");
22752 else if (identifier
&& !parser
->scope
)
22753 maybe_note_name_used_in_class (identifier
, decl
);
22758 /* Parse a class-specifier.
22761 class-head { member-specification [opt] }
22763 Returns the TREE_TYPE representing the class. */
22766 cp_parser_class_specifier_1 (cp_parser
* parser
)
22769 tree attributes
= NULL_TREE
;
22770 bool nested_name_specifier_p
;
22771 unsigned saved_num_template_parameter_lists
;
22772 bool saved_in_function_body
;
22773 unsigned char in_statement
;
22774 bool in_switch_statement_p
;
22775 bool saved_in_unbraced_linkage_specification_p
;
22776 tree old_scope
= NULL_TREE
;
22777 tree scope
= NULL_TREE
;
22778 cp_token
*closing_brace
;
22780 push_deferring_access_checks (dk_no_deferred
);
22782 /* Parse the class-head. */
22783 type
= cp_parser_class_head (parser
,
22784 &nested_name_specifier_p
);
22785 /* If the class-head was a semantic disaster, skip the entire body
22789 cp_parser_skip_to_end_of_block_or_statement (parser
);
22790 pop_deferring_access_checks ();
22791 return error_mark_node
;
22794 /* Look for the `{'. */
22795 matching_braces braces
;
22796 if (!braces
.require_open (parser
))
22798 pop_deferring_access_checks ();
22799 return error_mark_node
;
22802 cp_ensure_no_omp_declare_simd (parser
);
22803 cp_ensure_no_oacc_routine (parser
);
22805 /* Issue an error message if type-definitions are forbidden here. */
22806 cp_parser_check_type_definition (parser
);
22807 /* Remember that we are defining one more class. */
22808 ++parser
->num_classes_being_defined
;
22809 /* Inside the class, surrounding template-parameter-lists do not
22811 saved_num_template_parameter_lists
22812 = parser
->num_template_parameter_lists
;
22813 parser
->num_template_parameter_lists
= 0;
22814 /* We are not in a function body. */
22815 saved_in_function_body
= parser
->in_function_body
;
22816 parser
->in_function_body
= false;
22817 /* Or in a loop. */
22818 in_statement
= parser
->in_statement
;
22819 parser
->in_statement
= 0;
22820 /* Or in a switch. */
22821 in_switch_statement_p
= parser
->in_switch_statement_p
;
22822 parser
->in_switch_statement_p
= false;
22823 /* We are not immediately inside an extern "lang" block. */
22824 saved_in_unbraced_linkage_specification_p
22825 = parser
->in_unbraced_linkage_specification_p
;
22826 parser
->in_unbraced_linkage_specification_p
= false;
22828 // Associate constraints with the type.
22830 type
= associate_classtype_constraints (type
);
22832 /* Start the class. */
22833 if (nested_name_specifier_p
)
22835 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22836 old_scope
= push_inner_scope (scope
);
22838 type
= begin_class_definition (type
);
22840 if (type
== error_mark_node
)
22841 /* If the type is erroneous, skip the entire body of the class. */
22842 cp_parser_skip_to_closing_brace (parser
);
22844 /* Parse the member-specification. */
22845 cp_parser_member_specification_opt (parser
);
22847 /* Look for the trailing `}'. */
22848 closing_brace
= braces
.require_close (parser
);
22849 /* Look for trailing attributes to apply to this class. */
22850 if (cp_parser_allow_gnu_extensions_p (parser
))
22851 attributes
= cp_parser_gnu_attributes_opt (parser
);
22852 if (type
!= error_mark_node
)
22853 type
= finish_struct (type
, attributes
);
22854 if (nested_name_specifier_p
)
22855 pop_inner_scope (old_scope
, scope
);
22857 /* We've finished a type definition. Check for the common syntax
22858 error of forgetting a semicolon after the definition. We need to
22859 be careful, as we can't just check for not-a-semicolon and be done
22860 with it; the user might have typed:
22862 class X { } c = ...;
22863 class X { } *p = ...;
22865 and so forth. Instead, enumerate all the possible tokens that
22866 might follow this production; if we don't see one of them, then
22867 complain and silently insert the semicolon. */
22869 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22870 bool want_semicolon
= true;
22872 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22873 /* Don't try to parse c++11 attributes here. As per the
22874 grammar, that should be a task for
22875 cp_parser_decl_specifier_seq. */
22876 want_semicolon
= false;
22878 switch (token
->type
)
22881 case CPP_SEMICOLON
:
22884 case CPP_OPEN_PAREN
:
22885 case CPP_CLOSE_PAREN
:
22887 want_semicolon
= false;
22890 /* While it's legal for type qualifiers and storage class
22891 specifiers to follow type definitions in the grammar, only
22892 compiler testsuites contain code like that. Assume that if
22893 we see such code, then what we're really seeing is a case
22897 const <type> var = ...;
22902 static <type> func (...) ...
22904 i.e. the qualifier or specifier applies to the next
22905 declaration. To do so, however, we need to look ahead one
22906 more token to see if *that* token is a type specifier.
22908 This code could be improved to handle:
22911 static const <type> var = ...; */
22913 if (keyword_is_decl_specifier (token
->keyword
))
22915 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22917 /* Handling user-defined types here would be nice, but very
22920 = (lookahead
->type
== CPP_KEYWORD
22921 && keyword_begins_type_specifier (lookahead
->keyword
));
22928 /* If we don't have a type, then something is very wrong and we
22929 shouldn't try to do anything clever. Likewise for not seeing the
22931 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22933 /* Locate the closing brace. */
22934 cp_token_position prev
22935 = cp_lexer_previous_token_position (parser
->lexer
);
22936 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22937 location_t loc
= prev_token
->location
;
22939 /* We want to suggest insertion of a ';' immediately *after* the
22940 closing brace, so, if we can, offset the location by 1 column. */
22941 location_t next_loc
= loc
;
22942 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22943 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22945 rich_location
richloc (line_table
, next_loc
);
22947 /* If we successfully offset the location, suggest the fix-it. */
22948 if (next_loc
!= loc
)
22949 richloc
.add_fixit_insert_before (next_loc
, ";");
22951 if (CLASSTYPE_DECLARED_CLASS (type
))
22952 error_at (&richloc
,
22953 "expected %<;%> after class definition");
22954 else if (TREE_CODE (type
) == RECORD_TYPE
)
22955 error_at (&richloc
,
22956 "expected %<;%> after struct definition");
22957 else if (TREE_CODE (type
) == UNION_TYPE
)
22958 error_at (&richloc
,
22959 "expected %<;%> after union definition");
22961 gcc_unreachable ();
22963 /* Unget one token and smash it to look as though we encountered
22964 a semicolon in the input stream. */
22965 cp_lexer_set_token_position (parser
->lexer
, prev
);
22966 token
= cp_lexer_peek_token (parser
->lexer
);
22967 token
->type
= CPP_SEMICOLON
;
22968 token
->keyword
= RID_MAX
;
22972 /* If this class is not itself within the scope of another class,
22973 then we need to parse the bodies of all of the queued function
22974 definitions. Note that the queued functions defined in a class
22975 are not always processed immediately following the
22976 class-specifier for that class. Consider:
22979 struct B { void f() { sizeof (A); } };
22982 If `f' were processed before the processing of `A' were
22983 completed, there would be no way to compute the size of `A'.
22984 Note that the nesting we are interested in here is lexical --
22985 not the semantic nesting given by TYPE_CONTEXT. In particular,
22988 struct A { struct B; };
22989 struct A::B { void f() { } };
22991 there is no need to delay the parsing of `A::B::f'. */
22992 if (--parser
->num_classes_being_defined
== 0)
22995 tree class_type
= NULL_TREE
;
22996 tree pushed_scope
= NULL_TREE
;
22998 cp_default_arg_entry
*e
;
22999 tree save_ccp
, save_ccr
;
23001 if (any_erroneous_template_args_p (type
))
23003 /* Skip default arguments, NSDMIs, etc, in order to improve
23004 error recovery (c++/71169, c++/71832). */
23005 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23006 vec_safe_truncate (unparsed_nsdmis
, 0);
23007 vec_safe_truncate (unparsed_classes
, 0);
23008 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23011 /* In a first pass, parse default arguments to the functions.
23012 Then, in a second pass, parse the bodies of the functions.
23013 This two-phased approach handles cases like:
23021 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23024 /* If there are default arguments that have not yet been processed,
23025 take care of them now. */
23026 if (class_type
!= e
->class_type
)
23029 pop_scope (pushed_scope
);
23030 class_type
= e
->class_type
;
23031 pushed_scope
= push_scope (class_type
);
23033 /* Make sure that any template parameters are in scope. */
23034 maybe_begin_member_template_processing (decl
);
23035 /* Parse the default argument expressions. */
23036 cp_parser_late_parsing_default_args (parser
, decl
);
23037 /* Remove any template parameters from the symbol table. */
23038 maybe_end_member_template_processing ();
23040 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23041 /* Now parse any NSDMIs. */
23042 save_ccp
= current_class_ptr
;
23043 save_ccr
= current_class_ref
;
23044 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23046 if (class_type
!= DECL_CONTEXT (decl
))
23049 pop_scope (pushed_scope
);
23050 class_type
= DECL_CONTEXT (decl
);
23051 pushed_scope
= push_scope (class_type
);
23053 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23054 cp_parser_late_parsing_nsdmi (parser
, decl
);
23056 vec_safe_truncate (unparsed_nsdmis
, 0);
23057 current_class_ptr
= save_ccp
;
23058 current_class_ref
= save_ccr
;
23060 pop_scope (pushed_scope
);
23062 /* Now do some post-NSDMI bookkeeping. */
23063 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23064 after_nsdmi_defaulted_late_checks (class_type
);
23065 vec_safe_truncate (unparsed_classes
, 0);
23066 after_nsdmi_defaulted_late_checks (type
);
23068 /* Now parse the body of the functions. */
23071 /* OpenMP UDRs need to be parsed before all other functions. */
23072 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23073 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23074 cp_parser_late_parsing_for_member (parser
, decl
);
23075 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23076 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23077 cp_parser_late_parsing_for_member (parser
, decl
);
23080 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23081 cp_parser_late_parsing_for_member (parser
, decl
);
23082 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23085 vec_safe_push (unparsed_classes
, type
);
23087 /* Put back any saved access checks. */
23088 pop_deferring_access_checks ();
23090 /* Restore saved state. */
23091 parser
->in_switch_statement_p
= in_switch_statement_p
;
23092 parser
->in_statement
= in_statement
;
23093 parser
->in_function_body
= saved_in_function_body
;
23094 parser
->num_template_parameter_lists
23095 = saved_num_template_parameter_lists
;
23096 parser
->in_unbraced_linkage_specification_p
23097 = saved_in_unbraced_linkage_specification_p
;
23103 cp_parser_class_specifier (cp_parser
* parser
)
23106 timevar_push (TV_PARSE_STRUCT
);
23107 ret
= cp_parser_class_specifier_1 (parser
);
23108 timevar_pop (TV_PARSE_STRUCT
);
23112 /* Parse a class-head.
23115 class-key identifier [opt] base-clause [opt]
23116 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23117 class-key nested-name-specifier [opt] template-id
23120 class-virt-specifier:
23124 class-key attributes identifier [opt] base-clause [opt]
23125 class-key attributes nested-name-specifier identifier base-clause [opt]
23126 class-key attributes nested-name-specifier [opt] template-id
23129 Upon return BASES is initialized to the list of base classes (or
23130 NULL, if there are none) in the same form returned by
23131 cp_parser_base_clause.
23133 Returns the TYPE of the indicated class. Sets
23134 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23135 involving a nested-name-specifier was used, and FALSE otherwise.
23137 Returns error_mark_node if this is not a class-head.
23139 Returns NULL_TREE if the class-head is syntactically valid, but
23140 semantically invalid in a way that means we should skip the entire
23141 body of the class. */
23144 cp_parser_class_head (cp_parser
* parser
,
23145 bool* nested_name_specifier_p
)
23147 tree nested_name_specifier
;
23148 enum tag_types class_key
;
23149 tree id
= NULL_TREE
;
23150 tree type
= NULL_TREE
;
23153 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23154 bool template_id_p
= false;
23155 bool qualified_p
= false;
23156 bool invalid_nested_name_p
= false;
23157 bool invalid_explicit_specialization_p
= false;
23158 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23159 tree pushed_scope
= NULL_TREE
;
23160 unsigned num_templates
;
23161 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23162 /* Assume no nested-name-specifier will be present. */
23163 *nested_name_specifier_p
= false;
23164 /* Assume no template parameter lists will be used in defining the
23167 parser
->colon_corrects_to_scope_p
= false;
23169 /* Look for the class-key. */
23170 class_key
= cp_parser_class_key (parser
);
23171 if (class_key
== none_type
)
23172 return error_mark_node
;
23174 location_t class_head_start_location
= input_location
;
23176 /* Parse the attributes. */
23177 attributes
= cp_parser_attributes_opt (parser
);
23179 /* If the next token is `::', that is invalid -- but sometimes
23180 people do try to write:
23184 Handle this gracefully by accepting the extra qualifier, and then
23185 issuing an error about it later if this really is a
23186 class-head. If it turns out just to be an elaborated type
23187 specifier, remain silent. */
23188 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23189 qualified_p
= true;
23191 push_deferring_access_checks (dk_no_check
);
23193 /* Determine the name of the class. Begin by looking for an
23194 optional nested-name-specifier. */
23195 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23196 nested_name_specifier
23197 = cp_parser_nested_name_specifier_opt (parser
,
23198 /*typename_keyword_p=*/false,
23199 /*check_dependency_p=*/false,
23201 /*is_declaration=*/false);
23202 /* If there was a nested-name-specifier, then there *must* be an
23205 cp_token
*bad_template_keyword
= NULL
;
23207 if (nested_name_specifier
)
23209 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23210 /* Although the grammar says `identifier', it really means
23211 `class-name' or `template-name'. You are only allowed to
23212 define a class that has already been declared with this
23215 The proposed resolution for Core Issue 180 says that wherever
23216 you see `class T::X' you should treat `X' as a type-name.
23218 It is OK to define an inaccessible class; for example:
23220 class A { class B; };
23223 We do not know if we will see a class-name, or a
23224 template-name. We look for a class-name first, in case the
23225 class-name is a template-id; if we looked for the
23226 template-name first we would stop after the template-name. */
23227 cp_parser_parse_tentatively (parser
);
23228 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23229 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23230 type
= cp_parser_class_name (parser
,
23231 /*typename_keyword_p=*/false,
23232 /*template_keyword_p=*/false,
23234 /*check_dependency_p=*/false,
23235 /*class_head_p=*/true,
23236 /*is_declaration=*/false);
23237 /* If that didn't work, ignore the nested-name-specifier. */
23238 if (!cp_parser_parse_definitely (parser
))
23240 invalid_nested_name_p
= true;
23241 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23242 id
= cp_parser_identifier (parser
);
23243 if (id
== error_mark_node
)
23246 /* If we could not find a corresponding TYPE, treat this
23247 declaration like an unqualified declaration. */
23248 if (type
== error_mark_node
)
23249 nested_name_specifier
= NULL_TREE
;
23250 /* Otherwise, count the number of templates used in TYPE and its
23251 containing scopes. */
23253 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23255 /* Otherwise, the identifier is optional. */
23258 /* We don't know whether what comes next is a template-id,
23259 an identifier, or nothing at all. */
23260 cp_parser_parse_tentatively (parser
);
23261 /* Check for a template-id. */
23262 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23263 id
= cp_parser_template_id (parser
,
23264 /*template_keyword_p=*/false,
23265 /*check_dependency_p=*/true,
23267 /*is_declaration=*/true);
23268 /* If that didn't work, it could still be an identifier. */
23269 if (!cp_parser_parse_definitely (parser
))
23271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23273 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23274 id
= cp_parser_identifier (parser
);
23281 template_id_p
= true;
23286 pop_deferring_access_checks ();
23290 cp_parser_check_for_invalid_template_id (parser
, id
,
23292 type_start_token
->location
);
23294 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23296 /* If it's not a `:' or a `{' then we can't really be looking at a
23297 class-head, since a class-head only appears as part of a
23298 class-specifier. We have to detect this situation before calling
23299 xref_tag, since that has irreversible side-effects. */
23300 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23302 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23303 type
= error_mark_node
;
23307 /* At this point, we're going ahead with the class-specifier, even
23308 if some other problem occurs. */
23309 cp_parser_commit_to_tentative_parse (parser
);
23310 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23312 cp_parser_error (parser
,
23313 "cannot specify %<override%> for a class");
23314 type
= error_mark_node
;
23317 /* Issue the error about the overly-qualified name now. */
23320 cp_parser_error (parser
,
23321 "global qualification of class name is invalid");
23322 type
= error_mark_node
;
23325 else if (invalid_nested_name_p
)
23327 cp_parser_error (parser
,
23328 "qualified name does not name a class");
23329 type
= error_mark_node
;
23332 else if (nested_name_specifier
)
23336 if (bad_template_keyword
)
23337 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23338 keyword template shall not appear at the top level. */
23339 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23340 "keyword %<template%> not allowed in class-head-name");
23342 /* Reject typedef-names in class heads. */
23343 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23345 error_at (type_start_token
->location
,
23346 "invalid class name in declaration of %qD",
23352 /* Figure out in what scope the declaration is being placed. */
23353 scope
= current_scope ();
23354 /* If that scope does not contain the scope in which the
23355 class was originally declared, the program is invalid. */
23356 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23358 if (at_namespace_scope_p ())
23359 error_at (type_start_token
->location
,
23360 "declaration of %qD in namespace %qD which does not "
23362 type
, scope
, nested_name_specifier
);
23364 error_at (type_start_token
->location
,
23365 "declaration of %qD in %qD which does not enclose %qD",
23366 type
, scope
, nested_name_specifier
);
23372 A declarator-id shall not be qualified except for the
23373 definition of a ... nested class outside of its class
23374 ... [or] the definition or explicit instantiation of a
23375 class member of a namespace outside of its namespace. */
23376 if (scope
== nested_name_specifier
)
23378 permerror (nested_name_specifier_token_start
->location
,
23379 "extra qualification not allowed");
23380 nested_name_specifier
= NULL_TREE
;
23384 /* An explicit-specialization must be preceded by "template <>". If
23385 it is not, try to recover gracefully. */
23386 if (at_namespace_scope_p ()
23387 && parser
->num_template_parameter_lists
== 0
23388 && !processing_template_parmlist
23391 /* Build a location of this form:
23392 struct typename <ARGS>
23393 ^~~~~~~~~~~~~~~~~~~~~~
23394 with caret==start at the start token, and
23395 finishing at the end of the type. */
23396 location_t reported_loc
23397 = make_location (class_head_start_location
,
23398 class_head_start_location
,
23399 get_finish (type_start_token
->location
));
23400 rich_location
richloc (line_table
, reported_loc
);
23401 richloc
.add_fixit_insert_before (class_head_start_location
,
23403 error_at (&richloc
,
23404 "an explicit specialization must be preceded by"
23405 " %<template <>%>");
23406 invalid_explicit_specialization_p
= true;
23407 /* Take the same action that would have been taken by
23408 cp_parser_explicit_specialization. */
23409 ++parser
->num_template_parameter_lists
;
23410 begin_specialization ();
23412 /* There must be no "return" statements between this point and the
23413 end of this function; set "type "to the correct return value and
23414 use "goto done;" to return. */
23415 /* Make sure that the right number of template parameters were
23417 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23419 type_start_token
->location
,
23420 /*declarator=*/NULL
))
23422 /* If something went wrong, there is no point in even trying to
23423 process the class-definition. */
23428 /* Look up the type. */
23431 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23432 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23433 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23435 error_at (type_start_token
->location
,
23436 "function template %qD redeclared as a class template", id
);
23437 type
= error_mark_node
;
23441 type
= TREE_TYPE (id
);
23442 type
= maybe_process_partial_specialization (type
);
23444 /* Check the scope while we still know whether or not we had a
23445 nested-name-specifier. */
23446 if (type
!= error_mark_node
)
23447 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23449 if (nested_name_specifier
)
23450 pushed_scope
= push_scope (nested_name_specifier
);
23452 else if (nested_name_specifier
)
23458 template <typename T> struct S { struct T };
23459 template <typename T> struct S<T>::T { };
23461 we will get a TYPENAME_TYPE when processing the definition of
23462 `S::T'. We need to resolve it to the actual type before we
23463 try to define it. */
23464 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23466 class_type
= resolve_typename_type (TREE_TYPE (type
),
23467 /*only_current_p=*/false);
23468 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23469 type
= TYPE_NAME (class_type
);
23472 cp_parser_error (parser
, "could not resolve typename type");
23473 type
= error_mark_node
;
23477 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23478 == error_mark_node
)
23484 class_type
= current_class_type
;
23485 /* Enter the scope indicated by the nested-name-specifier. */
23486 pushed_scope
= push_scope (nested_name_specifier
);
23487 /* Get the canonical version of this type. */
23488 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23489 /* Call push_template_decl if it seems like we should be defining a
23490 template either from the template headers or the type we're
23491 defining, so that we diagnose both extra and missing headers. */
23492 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23493 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23494 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23496 type
= push_template_decl (type
);
23497 if (type
== error_mark_node
)
23504 type
= TREE_TYPE (type
);
23505 *nested_name_specifier_p
= true;
23507 else /* The name is not a nested name. */
23509 /* If the class was unnamed, create a dummy name. */
23511 id
= make_anon_name ();
23512 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23513 ? ts_within_enclosing_non_class
23515 type
= xref_tag (class_key
, id
, tag_scope
,
23516 parser
->num_template_parameter_lists
);
23519 /* Indicate whether this class was declared as a `class' or as a
23521 if (TREE_CODE (type
) == RECORD_TYPE
)
23522 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23523 cp_parser_check_class_key (class_key
, type
);
23525 /* If this type was already complete, and we see another definition,
23526 that's an error. */
23527 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23529 error_at (type_start_token
->location
, "redefinition of %q#T",
23531 inform (location_of (type
), "previous definition of %q#T",
23536 else if (type
== error_mark_node
)
23541 /* Apply attributes now, before any use of the class as a template
23542 argument in its base list. */
23543 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23544 fixup_attribute_variants (type
);
23547 /* We will have entered the scope containing the class; the names of
23548 base classes should be looked up in that context. For example:
23550 struct A { struct B {}; struct C; };
23551 struct A::C : B {};
23555 /* Get the list of base-classes, if there is one. */
23556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23558 /* PR59482: enter the class scope so that base-specifiers are looked
23562 bases
= cp_parser_base_clause (parser
);
23563 /* PR59482: get out of the previously pushed class scope so that the
23564 subsequent pops pop the right thing. */
23571 /* If we're really defining a class, process the base classes.
23572 If they're invalid, fail. */
23573 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23574 xref_basetypes (type
, bases
);
23577 /* Leave the scope given by the nested-name-specifier. We will
23578 enter the class scope itself while processing the members. */
23580 pop_scope (pushed_scope
);
23582 if (invalid_explicit_specialization_p
)
23584 end_specialization ();
23585 --parser
->num_template_parameter_lists
;
23589 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23590 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23591 CLASSTYPE_FINAL (type
) = 1;
23593 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23597 /* Parse a class-key.
23604 Returns the kind of class-key specified, or none_type to indicate
23607 static enum tag_types
23608 cp_parser_class_key (cp_parser
* parser
)
23611 enum tag_types tag_type
;
23613 /* Look for the class-key. */
23614 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23618 /* Check to see if the TOKEN is a class-key. */
23619 tag_type
= cp_parser_token_is_class_key (token
);
23621 cp_parser_error (parser
, "expected class-key");
23625 /* Parse a type-parameter-key.
23627 type-parameter-key:
23633 cp_parser_type_parameter_key (cp_parser
* parser
)
23635 /* Look for the type-parameter-key. */
23636 enum tag_types tag_type
= none_type
;
23637 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23638 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23640 cp_lexer_consume_token (parser
->lexer
);
23641 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23642 /* typename is not allowed in a template template parameter
23643 by the standard until C++17. */
23644 pedwarn (token
->location
, OPT_Wpedantic
,
23645 "ISO C++ forbids typename key in template template parameter;"
23646 " use -std=c++17 or -std=gnu++17");
23649 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23654 /* Parse an (optional) member-specification.
23656 member-specification:
23657 member-declaration member-specification [opt]
23658 access-specifier : member-specification [opt] */
23661 cp_parser_member_specification_opt (cp_parser
* parser
)
23668 /* Peek at the next token. */
23669 token
= cp_lexer_peek_token (parser
->lexer
);
23670 /* If it's a `}', or EOF then we've seen all the members. */
23671 if (token
->type
== CPP_CLOSE_BRACE
23672 || token
->type
== CPP_EOF
23673 || token
->type
== CPP_PRAGMA_EOL
)
23676 /* See if this token is a keyword. */
23677 keyword
= token
->keyword
;
23681 case RID_PROTECTED
:
23683 /* Consume the access-specifier. */
23684 cp_lexer_consume_token (parser
->lexer
);
23685 /* Remember which access-specifier is active. */
23686 current_access_specifier
= token
->u
.value
;
23687 /* Look for the `:'. */
23688 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23692 /* Accept #pragmas at class scope. */
23693 if (token
->type
== CPP_PRAGMA
)
23695 cp_parser_pragma (parser
, pragma_member
, NULL
);
23699 /* Otherwise, the next construction must be a
23700 member-declaration. */
23701 cp_parser_member_declaration (parser
);
23706 /* Parse a member-declaration.
23708 member-declaration:
23709 decl-specifier-seq [opt] member-declarator-list [opt] ;
23710 function-definition ; [opt]
23711 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23713 template-declaration
23716 member-declarator-list:
23718 member-declarator-list , member-declarator
23721 declarator pure-specifier [opt]
23722 declarator constant-initializer [opt]
23723 identifier [opt] : constant-expression
23727 member-declaration:
23728 __extension__ member-declaration
23731 declarator attributes [opt] pure-specifier [opt]
23732 declarator attributes [opt] constant-initializer [opt]
23733 identifier [opt] attributes [opt] : constant-expression
23737 member-declaration:
23738 static_assert-declaration */
23741 cp_parser_member_declaration (cp_parser
* parser
)
23743 cp_decl_specifier_seq decl_specifiers
;
23744 tree prefix_attributes
;
23746 int declares_class_or_enum
;
23748 cp_token
*token
= NULL
;
23749 cp_token
*decl_spec_token_start
= NULL
;
23750 cp_token
*initializer_token_start
= NULL
;
23751 int saved_pedantic
;
23752 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23754 /* Check for the `__extension__' keyword. */
23755 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23758 cp_parser_member_declaration (parser
);
23759 /* Restore the old value of the PEDANTIC flag. */
23760 pedantic
= saved_pedantic
;
23765 /* Check for a template-declaration. */
23766 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23768 /* An explicit specialization here is an error condition, and we
23769 expect the specialization handler to detect and report this. */
23770 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23771 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23772 cp_parser_explicit_specialization (parser
);
23774 cp_parser_template_declaration (parser
, /*member_p=*/true);
23778 /* Check for a template introduction. */
23779 else if (cp_parser_template_declaration_after_export (parser
, true))
23782 /* Check for a using-declaration. */
23783 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23785 if (cxx_dialect
< cxx11
)
23787 /* Parse the using-declaration. */
23788 cp_parser_using_declaration (parser
,
23789 /*access_declaration_p=*/false);
23795 bool alias_decl_expected
;
23796 cp_parser_parse_tentatively (parser
);
23797 decl
= cp_parser_alias_declaration (parser
);
23798 /* Note that if we actually see the '=' token after the
23799 identifier, cp_parser_alias_declaration commits the
23800 tentative parse. In that case, we really expect an
23801 alias-declaration. Otherwise, we expect a using
23803 alias_decl_expected
=
23804 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23805 cp_parser_parse_definitely (parser
);
23807 if (alias_decl_expected
)
23808 finish_member_declaration (decl
);
23810 cp_parser_using_declaration (parser
,
23811 /*access_declaration_p=*/false);
23816 /* Check for @defs. */
23817 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23820 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23821 ivar
= ivar_chains
;
23825 ivar
= TREE_CHAIN (member
);
23826 TREE_CHAIN (member
) = NULL_TREE
;
23827 finish_member_declaration (member
);
23832 /* If the next token is `static_assert' we have a static assertion. */
23833 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23835 cp_parser_static_assert (parser
, /*member_p=*/true);
23839 parser
->colon_corrects_to_scope_p
= false;
23841 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23844 /* Parse the decl-specifier-seq. */
23845 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23846 cp_parser_decl_specifier_seq (parser
,
23847 CP_PARSER_FLAGS_OPTIONAL
,
23849 &declares_class_or_enum
);
23850 /* Check for an invalid type-name. */
23851 if (!decl_specifiers
.any_type_specifiers_p
23852 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23854 /* If there is no declarator, then the decl-specifier-seq should
23856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23858 /* If there was no decl-specifier-seq, and the next token is a
23859 `;', then we have something like:
23865 Each member-declaration shall declare at least one member
23866 name of the class. */
23867 if (!decl_specifiers
.any_specifiers_p
)
23869 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23870 if (!in_system_header_at (token
->location
))
23872 gcc_rich_location
richloc (token
->location
);
23873 richloc
.add_fixit_remove ();
23874 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23881 /* See if this declaration is a friend. */
23882 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23883 /* If there were decl-specifiers, check to see if there was
23884 a class-declaration. */
23885 type
= check_tag_decl (&decl_specifiers
,
23886 /*explicit_type_instantiation_p=*/false);
23887 /* Nested classes have already been added to the class, but
23888 a `friend' needs to be explicitly registered. */
23891 /* If the `friend' keyword was present, the friend must
23892 be introduced with a class-key. */
23893 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23894 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23895 "in C++03 a class-key must be used "
23896 "when declaring a friend");
23899 template <typename T> struct A {
23900 friend struct A<T>::B;
23903 A<T>::B will be represented by a TYPENAME_TYPE, and
23904 therefore not recognized by check_tag_decl. */
23907 type
= decl_specifiers
.type
;
23908 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23909 type
= TREE_TYPE (type
);
23911 if (!type
|| !TYPE_P (type
))
23912 error_at (decl_spec_token_start
->location
,
23913 "friend declaration does not name a class or "
23916 make_friend_class (current_class_type
, type
,
23917 /*complain=*/true);
23919 /* If there is no TYPE, an error message will already have
23921 else if (!type
|| type
== error_mark_node
)
23923 /* An anonymous aggregate has to be handled specially; such
23924 a declaration really declares a data member (with a
23925 particular type), as opposed to a nested class. */
23926 else if (ANON_AGGR_TYPE_P (type
))
23929 if (decl_specifiers
.storage_class
!= sc_none
)
23930 error_at (decl_spec_token_start
->location
,
23931 "a storage class on an anonymous aggregate "
23932 "in class scope is not allowed");
23934 /* Remove constructors and such from TYPE, now that we
23935 know it is an anonymous aggregate. */
23936 fixup_anonymous_aggr (type
);
23937 /* And make the corresponding data member. */
23938 decl
= build_decl (decl_spec_token_start
->location
,
23939 FIELD_DECL
, NULL_TREE
, type
);
23940 /* Add it to the class. */
23941 finish_member_declaration (decl
);
23944 cp_parser_check_access_in_redeclaration
23946 decl_spec_token_start
->location
);
23951 bool assume_semicolon
= false;
23953 /* Clear attributes from the decl_specifiers but keep them
23954 around as prefix attributes that apply them to the entity
23956 prefix_attributes
= decl_specifiers
.attributes
;
23957 decl_specifiers
.attributes
= NULL_TREE
;
23959 /* See if these declarations will be friends. */
23960 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23962 /* Keep going until we hit the `;' at the end of the
23964 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23966 tree attributes
= NULL_TREE
;
23967 tree first_attribute
;
23969 bool named_bitfld
= false;
23971 /* Peek at the next token. */
23972 token
= cp_lexer_peek_token (parser
->lexer
);
23974 /* The following code wants to know early if it is a bit-field
23975 or some other declaration. Attributes can appear before
23976 the `:' token. Skip over them without consuming any tokens
23977 to peek if they are followed by `:'. */
23978 if (cp_next_tokens_can_be_attribute_p (parser
)
23979 || (token
->type
== CPP_NAME
23980 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23981 && (named_bitfld
= true)))
23984 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23985 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23988 /* Check for a bitfield declaration. */
23989 if (token
->type
== CPP_COLON
23990 || (token
->type
== CPP_NAME
23991 && token
== cp_lexer_peek_token (parser
->lexer
)
23992 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23993 && (named_bitfld
= true)))
23997 tree late_attributes
= NULL_TREE
;
23998 location_t id_location
23999 = cp_lexer_peek_token (parser
->lexer
)->location
;
24002 identifier
= cp_parser_identifier (parser
);
24004 identifier
= NULL_TREE
;
24006 /* Look for attributes that apply to the bitfield. */
24007 attributes
= cp_parser_attributes_opt (parser
);
24009 /* Consume the `:' token. */
24010 cp_lexer_consume_token (parser
->lexer
);
24012 /* Get the width of the bitfield. */
24013 width
= cp_parser_constant_expression (parser
, false, NULL
,
24014 cxx_dialect
>= cxx11
);
24016 /* In C++2A and as extension for C++11 and above we allow
24017 default member initializers for bit-fields. */
24018 initializer
= NULL_TREE
;
24019 if (cxx_dialect
>= cxx11
24020 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24021 || cp_lexer_next_token_is (parser
->lexer
,
24025 = cp_lexer_peek_token (parser
->lexer
)->location
;
24026 if (cxx_dialect
< cxx2a
24027 && !in_system_header_at (loc
)
24028 && identifier
!= NULL_TREE
)
24030 "default member initializers for bit-fields "
24031 "only available with -std=c++2a or "
24034 initializer
= cp_parser_save_nsdmi (parser
);
24035 if (identifier
== NULL_TREE
)
24037 error_at (loc
, "default member initializer for "
24038 "unnamed bit-field");
24039 initializer
= NULL_TREE
;
24044 /* Look for attributes that apply to the bitfield after
24045 the `:' token and width. This is where GCC used to
24046 parse attributes in the past, pedwarn if there is
24047 a std attribute. */
24048 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24049 pedwarn (input_location
, OPT_Wpedantic
,
24050 "ISO C++ allows bit-field attributes only "
24051 "before the %<:%> token");
24053 late_attributes
= cp_parser_attributes_opt (parser
);
24056 attributes
= attr_chainon (attributes
, late_attributes
);
24058 /* Remember which attributes are prefix attributes and
24060 first_attribute
= attributes
;
24061 /* Combine the attributes. */
24062 attributes
= attr_chainon (prefix_attributes
, attributes
);
24064 /* Create the bitfield declaration. */
24065 decl
= grokbitfield (identifier
24066 ? make_id_declarator (NULL_TREE
,
24072 width
, initializer
,
24077 cp_declarator
*declarator
;
24078 tree asm_specification
;
24079 int ctor_dtor_or_conv_p
;
24081 /* Parse the declarator. */
24083 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24084 &ctor_dtor_or_conv_p
,
24085 /*parenthesized_p=*/NULL
,
24089 /* If something went wrong parsing the declarator, make sure
24090 that we at least consume some tokens. */
24091 if (declarator
== cp_error_declarator
)
24093 /* Skip to the end of the statement. */
24094 cp_parser_skip_to_end_of_statement (parser
);
24095 /* If the next token is not a semicolon, that is
24096 probably because we just skipped over the body of
24097 a function. So, we consume a semicolon if
24098 present, but do not issue an error message if it
24100 if (cp_lexer_next_token_is (parser
->lexer
,
24102 cp_lexer_consume_token (parser
->lexer
);
24106 if (declares_class_or_enum
& 2)
24107 cp_parser_check_for_definition_in_return_type
24108 (declarator
, decl_specifiers
.type
,
24109 decl_specifiers
.locations
[ds_type_spec
]);
24111 /* Look for an asm-specification. */
24112 asm_specification
= cp_parser_asm_specification_opt (parser
);
24113 /* Look for attributes that apply to the declaration. */
24114 attributes
= cp_parser_attributes_opt (parser
);
24115 /* Remember which attributes are prefix attributes and
24117 first_attribute
= attributes
;
24118 /* Combine the attributes. */
24119 attributes
= attr_chainon (prefix_attributes
, attributes
);
24121 /* If it's an `=', then we have a constant-initializer or a
24122 pure-specifier. It is not correct to parse the
24123 initializer before registering the member declaration
24124 since the member declaration should be in scope while
24125 its initializer is processed. However, the rest of the
24126 front end does not yet provide an interface that allows
24127 us to handle this correctly. */
24128 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24132 A pure-specifier shall be used only in the declaration of
24133 a virtual function.
24135 A member-declarator can contain a constant-initializer
24136 only if it declares a static member of integral or
24139 Therefore, if the DECLARATOR is for a function, we look
24140 for a pure-specifier; otherwise, we look for a
24141 constant-initializer. When we call `grokfield', it will
24142 perform more stringent semantics checks. */
24143 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24144 if (function_declarator_p (declarator
)
24145 || (decl_specifiers
.type
24146 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24147 && declarator
->kind
== cdk_id
24148 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24149 == FUNCTION_TYPE
)))
24150 initializer
= cp_parser_pure_specifier (parser
);
24151 else if (decl_specifiers
.storage_class
!= sc_static
)
24152 initializer
= cp_parser_save_nsdmi (parser
);
24153 else if (cxx_dialect
>= cxx11
)
24156 /* Don't require a constant rvalue in C++11, since we
24157 might want a reference constant. We'll enforce
24158 constancy later. */
24159 cp_lexer_consume_token (parser
->lexer
);
24160 /* Parse the initializer. */
24161 initializer
= cp_parser_initializer_clause (parser
,
24165 /* Parse the initializer. */
24166 initializer
= cp_parser_constant_initializer (parser
);
24168 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24169 && !function_declarator_p (declarator
))
24172 if (decl_specifiers
.storage_class
!= sc_static
)
24173 initializer
= cp_parser_save_nsdmi (parser
);
24175 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24177 /* Otherwise, there is no initializer. */
24179 initializer
= NULL_TREE
;
24181 /* See if we are probably looking at a function
24182 definition. We are certainly not looking at a
24183 member-declarator. Calling `grokfield' has
24184 side-effects, so we must not do it unless we are sure
24185 that we are looking at a member-declarator. */
24186 if (cp_parser_token_starts_function_definition_p
24187 (cp_lexer_peek_token (parser
->lexer
)))
24189 /* The grammar does not allow a pure-specifier to be
24190 used when a member function is defined. (It is
24191 possible that this fact is an oversight in the
24192 standard, since a pure function may be defined
24193 outside of the class-specifier. */
24194 if (initializer
&& initializer_token_start
)
24195 error_at (initializer_token_start
->location
,
24196 "pure-specifier on function-definition");
24197 decl
= cp_parser_save_member_function_body (parser
,
24201 if (parser
->fully_implicit_function_template_p
)
24202 decl
= finish_fully_implicit_template (parser
, decl
);
24203 /* If the member was not a friend, declare it here. */
24205 finish_member_declaration (decl
);
24206 /* Peek at the next token. */
24207 token
= cp_lexer_peek_token (parser
->lexer
);
24208 /* If the next token is a semicolon, consume it. */
24209 if (token
->type
== CPP_SEMICOLON
)
24211 location_t semicolon_loc
24212 = cp_lexer_consume_token (parser
->lexer
)->location
;
24213 gcc_rich_location
richloc (semicolon_loc
);
24214 richloc
.add_fixit_remove ();
24215 warning_at (&richloc
, OPT_Wextra_semi
,
24216 "extra %<;%> after in-class "
24217 "function definition");
24222 if (declarator
->kind
== cdk_function
)
24223 declarator
->id_loc
= token
->location
;
24224 /* Create the declaration. */
24225 decl
= grokfield (declarator
, &decl_specifiers
,
24226 initializer
, /*init_const_expr_p=*/true,
24227 asm_specification
, attributes
);
24228 if (parser
->fully_implicit_function_template_p
)
24231 finish_fully_implicit_template (parser
, 0);
24233 decl
= finish_fully_implicit_template (parser
, decl
);
24237 cp_finalize_omp_declare_simd (parser
, decl
);
24238 cp_finalize_oacc_routine (parser
, decl
, false);
24240 /* Reset PREFIX_ATTRIBUTES. */
24241 if (attributes
!= error_mark_node
)
24243 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24244 attributes
= TREE_CHAIN (attributes
);
24246 TREE_CHAIN (attributes
) = NULL_TREE
;
24249 /* If there is any qualification still in effect, clear it
24250 now; we will be starting fresh with the next declarator. */
24251 parser
->scope
= NULL_TREE
;
24252 parser
->qualifying_scope
= NULL_TREE
;
24253 parser
->object_scope
= NULL_TREE
;
24254 /* If it's a `,', then there are more declarators. */
24255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24257 cp_lexer_consume_token (parser
->lexer
);
24258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24260 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24261 gcc_rich_location
richloc (token
->location
);
24262 richloc
.add_fixit_remove ();
24263 error_at (&richloc
, "stray %<,%> at end of "
24264 "member declaration");
24267 /* If the next token isn't a `;', then we have a parse error. */
24268 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24271 /* The next token might be a ways away from where the
24272 actual semicolon is missing. Find the previous token
24273 and use that for our error position. */
24274 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24275 gcc_rich_location
richloc (token
->location
);
24276 richloc
.add_fixit_insert_after (";");
24277 error_at (&richloc
, "expected %<;%> at end of "
24278 "member declaration");
24280 /* Assume that the user meant to provide a semicolon. If
24281 we were to cp_parser_skip_to_end_of_statement, we might
24282 skip to a semicolon inside a member function definition
24283 and issue nonsensical error messages. */
24284 assume_semicolon
= true;
24289 /* Add DECL to the list of members. */
24291 /* Explicitly include, eg, NSDMIs, for better error
24292 recovery (c++/58650). */
24293 || !DECL_DECLARES_FUNCTION_P (decl
))
24294 finish_member_declaration (decl
);
24296 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24297 cp_parser_save_default_args (parser
, decl
);
24298 else if (TREE_CODE (decl
) == FIELD_DECL
24299 && DECL_INITIAL (decl
))
24300 /* Add DECL to the queue of NSDMI to be parsed later. */
24301 vec_safe_push (unparsed_nsdmis
, decl
);
24304 if (assume_semicolon
)
24309 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24311 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24314 /* Parse a pure-specifier.
24319 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24320 Otherwise, ERROR_MARK_NODE is returned. */
24323 cp_parser_pure_specifier (cp_parser
* parser
)
24327 /* Look for the `=' token. */
24328 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24329 return error_mark_node
;
24330 /* Look for the `0' token. */
24331 token
= cp_lexer_peek_token (parser
->lexer
);
24333 if (token
->type
== CPP_EOF
24334 || token
->type
== CPP_PRAGMA_EOL
)
24335 return error_mark_node
;
24337 cp_lexer_consume_token (parser
->lexer
);
24339 /* Accept = default or = delete in c++0x mode. */
24340 if (token
->keyword
== RID_DEFAULT
24341 || token
->keyword
== RID_DELETE
)
24343 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24344 return token
->u
.value
;
24347 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24348 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24350 cp_parser_error (parser
,
24351 "invalid pure specifier (only %<= 0%> is allowed)");
24352 cp_parser_skip_to_end_of_statement (parser
);
24353 return error_mark_node
;
24355 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24357 error_at (token
->location
, "templates may not be %<virtual%>");
24358 return error_mark_node
;
24361 return integer_zero_node
;
24364 /* Parse a constant-initializer.
24366 constant-initializer:
24367 = constant-expression
24369 Returns a representation of the constant-expression. */
24372 cp_parser_constant_initializer (cp_parser
* parser
)
24374 /* Look for the `=' token. */
24375 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24376 return error_mark_node
;
24378 /* It is invalid to write:
24380 struct S { static const int i = { 7 }; };
24383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24385 cp_parser_error (parser
,
24386 "a brace-enclosed initializer is not allowed here");
24387 /* Consume the opening brace. */
24388 matching_braces braces
;
24389 braces
.consume_open (parser
);
24390 /* Skip the initializer. */
24391 cp_parser_skip_to_closing_brace (parser
);
24392 /* Look for the trailing `}'. */
24393 braces
.require_close (parser
);
24395 return error_mark_node
;
24398 return cp_parser_constant_expression (parser
);
24401 /* Derived classes [gram.class.derived] */
24403 /* Parse a base-clause.
24406 : base-specifier-list
24408 base-specifier-list:
24409 base-specifier ... [opt]
24410 base-specifier-list , base-specifier ... [opt]
24412 Returns a TREE_LIST representing the base-classes, in the order in
24413 which they were declared. The representation of each node is as
24414 described by cp_parser_base_specifier.
24416 In the case that no bases are specified, this function will return
24417 NULL_TREE, not ERROR_MARK_NODE. */
24420 cp_parser_base_clause (cp_parser
* parser
)
24422 tree bases
= NULL_TREE
;
24424 /* Look for the `:' that begins the list. */
24425 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24427 /* Scan the base-specifier-list. */
24432 bool pack_expansion_p
= false;
24434 /* Look for the base-specifier. */
24435 base
= cp_parser_base_specifier (parser
);
24436 /* Look for the (optional) ellipsis. */
24437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24439 /* Consume the `...'. */
24440 cp_lexer_consume_token (parser
->lexer
);
24442 pack_expansion_p
= true;
24445 /* Add BASE to the front of the list. */
24446 if (base
&& base
!= error_mark_node
)
24448 if (pack_expansion_p
)
24449 /* Make this a pack expansion type. */
24450 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24452 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24454 TREE_CHAIN (base
) = bases
;
24458 /* Peek at the next token. */
24459 token
= cp_lexer_peek_token (parser
->lexer
);
24460 /* If it's not a comma, then the list is complete. */
24461 if (token
->type
!= CPP_COMMA
)
24463 /* Consume the `,'. */
24464 cp_lexer_consume_token (parser
->lexer
);
24467 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24468 base class had a qualified name. However, the next name that
24469 appears is certainly not qualified. */
24470 parser
->scope
= NULL_TREE
;
24471 parser
->qualifying_scope
= NULL_TREE
;
24472 parser
->object_scope
= NULL_TREE
;
24474 return nreverse (bases
);
24477 /* Parse a base-specifier.
24480 :: [opt] nested-name-specifier [opt] class-name
24481 virtual access-specifier [opt] :: [opt] nested-name-specifier
24483 access-specifier virtual [opt] :: [opt] nested-name-specifier
24486 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24487 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24488 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24489 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24492 cp_parser_base_specifier (cp_parser
* parser
)
24496 bool virtual_p
= false;
24497 bool duplicate_virtual_error_issued_p
= false;
24498 bool duplicate_access_error_issued_p
= false;
24499 bool class_scope_p
, template_p
;
24500 tree access
= access_default_node
;
24503 /* Process the optional `virtual' and `access-specifier'. */
24506 /* Peek at the next token. */
24507 token
= cp_lexer_peek_token (parser
->lexer
);
24508 /* Process `virtual'. */
24509 switch (token
->keyword
)
24512 /* If `virtual' appears more than once, issue an error. */
24513 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24515 cp_parser_error (parser
,
24516 "%<virtual%> specified more than once in base-specifier");
24517 duplicate_virtual_error_issued_p
= true;
24522 /* Consume the `virtual' token. */
24523 cp_lexer_consume_token (parser
->lexer
);
24528 case RID_PROTECTED
:
24530 /* If more than one access specifier appears, issue an
24532 if (access
!= access_default_node
24533 && !duplicate_access_error_issued_p
)
24535 cp_parser_error (parser
,
24536 "more than one access specifier in base-specifier");
24537 duplicate_access_error_issued_p
= true;
24540 access
= ridpointers
[(int) token
->keyword
];
24542 /* Consume the access-specifier. */
24543 cp_lexer_consume_token (parser
->lexer
);
24552 /* It is not uncommon to see programs mechanically, erroneously, use
24553 the 'typename' keyword to denote (dependent) qualified types
24554 as base classes. */
24555 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24557 token
= cp_lexer_peek_token (parser
->lexer
);
24558 if (!processing_template_decl
)
24559 error_at (token
->location
,
24560 "keyword %<typename%> not allowed outside of templates");
24562 error_at (token
->location
,
24563 "keyword %<typename%> not allowed in this context "
24564 "(the base class is implicitly a type)");
24565 cp_lexer_consume_token (parser
->lexer
);
24568 /* Look for the optional `::' operator. */
24569 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24570 /* Look for the nested-name-specifier. The simplest way to
24575 The keyword `typename' is not permitted in a base-specifier or
24576 mem-initializer; in these contexts a qualified name that
24577 depends on a template-parameter is implicitly assumed to be a
24580 is to pretend that we have seen the `typename' keyword at this
24582 cp_parser_nested_name_specifier_opt (parser
,
24583 /*typename_keyword_p=*/true,
24584 /*check_dependency_p=*/true,
24586 /*is_declaration=*/true);
24587 /* If the base class is given by a qualified name, assume that names
24588 we see are type names or templates, as appropriate. */
24589 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24590 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24593 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24594 /* DR 950 allows decltype as a base-specifier. */
24595 type
= cp_parser_decltype (parser
);
24598 /* Otherwise, look for the class-name. */
24599 type
= cp_parser_class_name (parser
,
24603 /*check_dependency_p=*/true,
24604 /*class_head_p=*/false,
24605 /*is_declaration=*/true);
24606 type
= TREE_TYPE (type
);
24609 if (type
== error_mark_node
)
24610 return error_mark_node
;
24612 return finish_base_specifier (type
, access
, virtual_p
);
24615 /* Exception handling [gram.exception] */
24617 /* Parse an (optional) noexcept-specification.
24619 noexcept-specification:
24620 noexcept ( constant-expression ) [opt]
24622 If no noexcept-specification is present, returns NULL_TREE.
24623 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24624 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24625 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24626 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24627 in which case a boolean condition is returned instead. */
24630 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24631 bool require_constexpr
,
24632 bool* consumed_expr
,
24636 const char *saved_message
;
24638 /* Peek at the next token. */
24639 token
= cp_lexer_peek_token (parser
->lexer
);
24641 /* Is it a noexcept-specification? */
24642 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24645 cp_lexer_consume_token (parser
->lexer
);
24647 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24649 matching_parens parens
;
24650 parens
.consume_open (parser
);
24652 if (require_constexpr
)
24654 /* Types may not be defined in an exception-specification. */
24655 saved_message
= parser
->type_definition_forbidden_message
;
24656 parser
->type_definition_forbidden_message
24657 = G_("types may not be defined in an exception-specification");
24659 expr
= cp_parser_constant_expression (parser
);
24661 /* Restore the saved message. */
24662 parser
->type_definition_forbidden_message
= saved_message
;
24666 expr
= cp_parser_expression (parser
);
24667 *consumed_expr
= true;
24670 parens
.require_close (parser
);
24674 expr
= boolean_true_node
;
24675 if (!require_constexpr
)
24676 *consumed_expr
= false;
24679 /* We cannot build a noexcept-spec right away because this will check
24680 that expr is a constexpr. */
24682 return build_noexcept_spec (expr
, tf_warning_or_error
);
24690 /* Parse an (optional) exception-specification.
24692 exception-specification:
24693 throw ( type-id-list [opt] )
24695 Returns a TREE_LIST representing the exception-specification. The
24696 TREE_VALUE of each node is a type. */
24699 cp_parser_exception_specification_opt (cp_parser
* parser
)
24703 const char *saved_message
;
24705 /* Peek at the next token. */
24706 token
= cp_lexer_peek_token (parser
->lexer
);
24708 /* Is it a noexcept-specification? */
24709 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24711 if (type_id_list
!= NULL_TREE
)
24712 return type_id_list
;
24714 /* If it's not `throw', then there's no exception-specification. */
24715 if (!cp_parser_is_keyword (token
, RID_THROW
))
24718 location_t loc
= token
->location
;
24720 /* Consume the `throw'. */
24721 cp_lexer_consume_token (parser
->lexer
);
24723 /* Look for the `('. */
24724 matching_parens parens
;
24725 parens
.require_open (parser
);
24727 /* Peek at the next token. */
24728 token
= cp_lexer_peek_token (parser
->lexer
);
24729 /* If it's not a `)', then there is a type-id-list. */
24730 if (token
->type
!= CPP_CLOSE_PAREN
)
24732 /* Types may not be defined in an exception-specification. */
24733 saved_message
= parser
->type_definition_forbidden_message
;
24734 parser
->type_definition_forbidden_message
24735 = G_("types may not be defined in an exception-specification");
24736 /* Parse the type-id-list. */
24737 type_id_list
= cp_parser_type_id_list (parser
);
24738 /* Restore the saved message. */
24739 parser
->type_definition_forbidden_message
= saved_message
;
24741 if (cxx_dialect
>= cxx17
)
24743 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24745 type_id_list
= NULL_TREE
;
24747 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24748 warning_at (loc
, OPT_Wdeprecated
,
24749 "dynamic exception specifications are deprecated in "
24752 /* In C++17, throw() is equivalent to noexcept (true). throw()
24753 is deprecated in C++11 and above as well, but is still widely used,
24754 so don't warn about it yet. */
24755 else if (cxx_dialect
>= cxx17
)
24756 type_id_list
= noexcept_true_spec
;
24758 type_id_list
= empty_except_spec
;
24760 /* Look for the `)'. */
24761 parens
.require_close (parser
);
24763 return type_id_list
;
24766 /* Parse an (optional) type-id-list.
24770 type-id-list , type-id ... [opt]
24772 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24773 in the order that the types were presented. */
24776 cp_parser_type_id_list (cp_parser
* parser
)
24778 tree types
= NULL_TREE
;
24785 token
= cp_lexer_peek_token (parser
->lexer
);
24787 /* Get the next type-id. */
24788 type
= cp_parser_type_id (parser
);
24789 /* Check for invalid 'auto'. */
24790 if (flag_concepts
&& type_uses_auto (type
))
24792 error_at (token
->location
,
24793 "invalid use of %<auto%> in exception-specification");
24794 type
= error_mark_node
;
24796 /* Parse the optional ellipsis. */
24797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24799 /* Consume the `...'. */
24800 cp_lexer_consume_token (parser
->lexer
);
24802 /* Turn the type into a pack expansion expression. */
24803 type
= make_pack_expansion (type
);
24805 /* Add it to the list. */
24806 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24807 /* Peek at the next token. */
24808 token
= cp_lexer_peek_token (parser
->lexer
);
24809 /* If it is not a `,', we are done. */
24810 if (token
->type
!= CPP_COMMA
)
24812 /* Consume the `,'. */
24813 cp_lexer_consume_token (parser
->lexer
);
24816 return nreverse (types
);
24819 /* Parse a try-block.
24822 try compound-statement handler-seq */
24825 cp_parser_try_block (cp_parser
* parser
)
24829 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24830 if (parser
->in_function_body
24831 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24832 error ("%<try%> in %<constexpr%> function");
24834 try_block
= begin_try_block ();
24835 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24836 finish_try_block (try_block
);
24837 cp_parser_handler_seq (parser
);
24838 finish_handler_sequence (try_block
);
24843 /* Parse a function-try-block.
24845 function-try-block:
24846 try ctor-initializer [opt] function-body handler-seq */
24849 cp_parser_function_try_block (cp_parser
* parser
)
24851 tree compound_stmt
;
24854 /* Look for the `try' keyword. */
24855 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24857 /* Let the rest of the front end know where we are. */
24858 try_block
= begin_function_try_block (&compound_stmt
);
24859 /* Parse the function-body. */
24860 cp_parser_ctor_initializer_opt_and_function_body
24861 (parser
, /*in_function_try_block=*/true);
24862 /* We're done with the `try' part. */
24863 finish_function_try_block (try_block
);
24864 /* Parse the handlers. */
24865 cp_parser_handler_seq (parser
);
24866 /* We're done with the handlers. */
24867 finish_function_handler_sequence (try_block
, compound_stmt
);
24870 /* Parse a handler-seq.
24873 handler handler-seq [opt] */
24876 cp_parser_handler_seq (cp_parser
* parser
)
24882 /* Parse the handler. */
24883 cp_parser_handler (parser
);
24884 /* Peek at the next token. */
24885 token
= cp_lexer_peek_token (parser
->lexer
);
24886 /* If it's not `catch' then there are no more handlers. */
24887 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24892 /* Parse a handler.
24895 catch ( exception-declaration ) compound-statement */
24898 cp_parser_handler (cp_parser
* parser
)
24903 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24904 handler
= begin_handler ();
24905 matching_parens parens
;
24906 parens
.require_open (parser
);
24907 declaration
= cp_parser_exception_declaration (parser
);
24908 finish_handler_parms (declaration
, handler
);
24909 parens
.require_close (parser
);
24910 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24911 finish_handler (handler
);
24914 /* Parse an exception-declaration.
24916 exception-declaration:
24917 type-specifier-seq declarator
24918 type-specifier-seq abstract-declarator
24922 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24923 ellipsis variant is used. */
24926 cp_parser_exception_declaration (cp_parser
* parser
)
24928 cp_decl_specifier_seq type_specifiers
;
24929 cp_declarator
*declarator
;
24930 const char *saved_message
;
24932 /* If it's an ellipsis, it's easy to handle. */
24933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24935 /* Consume the `...' token. */
24936 cp_lexer_consume_token (parser
->lexer
);
24940 /* Types may not be defined in exception-declarations. */
24941 saved_message
= parser
->type_definition_forbidden_message
;
24942 parser
->type_definition_forbidden_message
24943 = G_("types may not be defined in exception-declarations");
24945 /* Parse the type-specifier-seq. */
24946 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24947 /*is_trailing_return=*/false,
24949 /* If it's a `)', then there is no declarator. */
24950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24953 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24954 /*ctor_dtor_or_conv_p=*/NULL
,
24955 /*parenthesized_p=*/NULL
,
24956 /*member_p=*/false,
24957 /*friend_p=*/false);
24959 /* Restore the saved message. */
24960 parser
->type_definition_forbidden_message
= saved_message
;
24962 if (!type_specifiers
.any_specifiers_p
)
24963 return error_mark_node
;
24965 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24968 /* Parse a throw-expression.
24971 throw assignment-expression [opt]
24973 Returns a THROW_EXPR representing the throw-expression. */
24976 cp_parser_throw_expression (cp_parser
* parser
)
24981 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24982 token
= cp_lexer_peek_token (parser
->lexer
);
24983 /* Figure out whether or not there is an assignment-expression
24984 following the "throw" keyword. */
24985 if (token
->type
== CPP_COMMA
24986 || token
->type
== CPP_SEMICOLON
24987 || token
->type
== CPP_CLOSE_PAREN
24988 || token
->type
== CPP_CLOSE_SQUARE
24989 || token
->type
== CPP_CLOSE_BRACE
24990 || token
->type
== CPP_COLON
)
24991 expression
= NULL_TREE
;
24993 expression
= cp_parser_assignment_expression (parser
);
24995 return build_throw (expression
);
24998 /* GNU Extensions */
25000 /* Parse an (optional) asm-specification.
25003 asm ( string-literal )
25005 If the asm-specification is present, returns a STRING_CST
25006 corresponding to the string-literal. Otherwise, returns
25010 cp_parser_asm_specification_opt (cp_parser
* parser
)
25013 tree asm_specification
;
25015 /* Peek at the next token. */
25016 token
= cp_lexer_peek_token (parser
->lexer
);
25017 /* If the next token isn't the `asm' keyword, then there's no
25018 asm-specification. */
25019 if (!cp_parser_is_keyword (token
, RID_ASM
))
25022 /* Consume the `asm' token. */
25023 cp_lexer_consume_token (parser
->lexer
);
25024 /* Look for the `('. */
25025 matching_parens parens
;
25026 parens
.require_open (parser
);
25028 /* Look for the string-literal. */
25029 asm_specification
= cp_parser_string_literal (parser
, false, false);
25031 /* Look for the `)'. */
25032 parens
.require_close (parser
);
25034 return asm_specification
;
25037 /* Parse an asm-operand-list.
25041 asm-operand-list , asm-operand
25044 string-literal ( expression )
25045 [ string-literal ] string-literal ( expression )
25047 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25048 each node is the expression. The TREE_PURPOSE is itself a
25049 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25050 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25051 is a STRING_CST for the string literal before the parenthesis. Returns
25052 ERROR_MARK_NODE if any of the operands are invalid. */
25055 cp_parser_asm_operand_list (cp_parser
* parser
)
25057 tree asm_operands
= NULL_TREE
;
25058 bool invalid_operands
= false;
25062 tree string_literal
;
25066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25068 /* Consume the `[' token. */
25069 cp_lexer_consume_token (parser
->lexer
);
25070 /* Read the operand name. */
25071 name
= cp_parser_identifier (parser
);
25072 if (name
!= error_mark_node
)
25073 name
= build_string (IDENTIFIER_LENGTH (name
),
25074 IDENTIFIER_POINTER (name
));
25075 /* Look for the closing `]'. */
25076 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25080 /* Look for the string-literal. */
25081 string_literal
= cp_parser_string_literal (parser
, false, false);
25083 /* Look for the `('. */
25084 matching_parens parens
;
25085 parens
.require_open (parser
);
25086 /* Parse the expression. */
25087 expression
= cp_parser_expression (parser
);
25088 /* Look for the `)'. */
25089 parens
.require_close (parser
);
25091 if (name
== error_mark_node
25092 || string_literal
== error_mark_node
25093 || expression
== error_mark_node
)
25094 invalid_operands
= true;
25096 /* Add this operand to the list. */
25097 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25100 /* If the next token is not a `,', there are no more
25102 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25104 /* Consume the `,'. */
25105 cp_lexer_consume_token (parser
->lexer
);
25108 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25111 /* Parse an asm-clobber-list.
25115 asm-clobber-list , string-literal
25117 Returns a TREE_LIST, indicating the clobbers in the order that they
25118 appeared. The TREE_VALUE of each node is a STRING_CST. */
25121 cp_parser_asm_clobber_list (cp_parser
* parser
)
25123 tree clobbers
= NULL_TREE
;
25127 tree string_literal
;
25129 /* Look for the string literal. */
25130 string_literal
= cp_parser_string_literal (parser
, false, false);
25131 /* Add it to the list. */
25132 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25133 /* If the next token is not a `,', then the list is
25135 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25137 /* Consume the `,' token. */
25138 cp_lexer_consume_token (parser
->lexer
);
25144 /* Parse an asm-label-list.
25148 asm-label-list , identifier
25150 Returns a TREE_LIST, indicating the labels in the order that they
25151 appeared. The TREE_VALUE of each node is a label. */
25154 cp_parser_asm_label_list (cp_parser
* parser
)
25156 tree labels
= NULL_TREE
;
25160 tree identifier
, label
, name
;
25162 /* Look for the identifier. */
25163 identifier
= cp_parser_identifier (parser
);
25164 if (!error_operand_p (identifier
))
25166 label
= lookup_label (identifier
);
25167 if (TREE_CODE (label
) == LABEL_DECL
)
25169 TREE_USED (label
) = 1;
25170 check_goto (label
);
25171 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25172 IDENTIFIER_POINTER (identifier
));
25173 labels
= tree_cons (name
, label
, labels
);
25176 /* If the next token is not a `,', then the list is
25178 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25180 /* Consume the `,' token. */
25181 cp_lexer_consume_token (parser
->lexer
);
25184 return nreverse (labels
);
25187 /* Return TRUE iff the next tokens in the stream are possibly the
25188 beginning of a GNU extension attribute. */
25191 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25193 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25196 /* Return TRUE iff the next tokens in the stream are possibly the
25197 beginning of a standard C++-11 attribute specifier. */
25200 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25202 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25205 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25206 beginning of a standard C++-11 attribute specifier. */
25209 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25211 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25213 return (cxx_dialect
>= cxx11
25214 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25215 || (token
->type
== CPP_OPEN_SQUARE
25216 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25217 && token
->type
== CPP_OPEN_SQUARE
)));
25220 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25221 beginning of a GNU extension attribute. */
25224 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25226 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25228 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25231 /* Return true iff the next tokens can be the beginning of either a
25232 GNU attribute list, or a standard C++11 attribute sequence. */
25235 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25237 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25238 || cp_next_tokens_can_be_std_attribute_p (parser
));
25241 /* Return true iff the next Nth tokens can be the beginning of either
25242 a GNU attribute list, or a standard C++11 attribute sequence. */
25245 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25247 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25248 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25251 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25252 of GNU attributes, or return NULL. */
25255 cp_parser_attributes_opt (cp_parser
*parser
)
25257 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25258 return cp_parser_gnu_attributes_opt (parser
);
25259 return cp_parser_std_attribute_spec_seq (parser
);
25262 /* Parse an (optional) series of attributes.
25265 attributes attribute
25268 __attribute__ (( attribute-list [opt] ))
25270 The return value is as for cp_parser_gnu_attribute_list. */
25273 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25275 tree attributes
= NULL_TREE
;
25277 temp_override
<bool> cleanup
25278 (parser
->auto_is_implicit_function_template_parm_p
, false);
25283 tree attribute_list
;
25286 /* Peek at the next token. */
25287 token
= cp_lexer_peek_token (parser
->lexer
);
25288 /* If it's not `__attribute__', then we're done. */
25289 if (token
->keyword
!= RID_ATTRIBUTE
)
25292 /* Consume the `__attribute__' keyword. */
25293 cp_lexer_consume_token (parser
->lexer
);
25294 /* Look for the two `(' tokens. */
25295 matching_parens outer_parens
;
25296 outer_parens
.require_open (parser
);
25297 matching_parens inner_parens
;
25298 inner_parens
.require_open (parser
);
25300 /* Peek at the next token. */
25301 token
= cp_lexer_peek_token (parser
->lexer
);
25302 if (token
->type
!= CPP_CLOSE_PAREN
)
25303 /* Parse the attribute-list. */
25304 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25306 /* If the next token is a `)', then there is no attribute
25308 attribute_list
= NULL
;
25310 /* Look for the two `)' tokens. */
25311 if (!inner_parens
.require_close (parser
))
25313 if (!outer_parens
.require_close (parser
))
25316 cp_parser_skip_to_end_of_statement (parser
);
25318 /* Add these new attributes to the list. */
25319 attributes
= attr_chainon (attributes
, attribute_list
);
25325 /* Parse a GNU attribute-list.
25329 attribute-list , attribute
25333 identifier ( identifier )
25334 identifier ( identifier , expression-list )
25335 identifier ( expression-list )
25337 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25338 to an attribute. The TREE_PURPOSE of each node is the identifier
25339 indicating which attribute is in use. The TREE_VALUE represents
25340 the arguments, if any. */
25343 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25345 tree attribute_list
= NULL_TREE
;
25346 bool save_translate_strings_p
= parser
->translate_strings_p
;
25348 parser
->translate_strings_p
= false;
25355 /* Look for the identifier. We also allow keywords here; for
25356 example `__attribute__ ((const))' is legal. */
25357 token
= cp_lexer_peek_token (parser
->lexer
);
25358 if (token
->type
== CPP_NAME
25359 || token
->type
== CPP_KEYWORD
)
25361 tree arguments
= NULL_TREE
;
25363 /* Consume the token, but save it since we need it for the
25364 SIMD enabled function parsing. */
25365 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25367 /* Save away the identifier that indicates which attribute
25369 identifier
= (token
->type
== CPP_KEYWORD
)
25370 /* For keywords, use the canonical spelling, not the
25371 parsed identifier. */
25372 ? ridpointers
[(int) token
->keyword
]
25373 : id_token
->u
.value
;
25375 identifier
= canonicalize_attr_name (identifier
);
25376 attribute
= build_tree_list (identifier
, NULL_TREE
);
25378 /* Peek at the next token. */
25379 token
= cp_lexer_peek_token (parser
->lexer
);
25380 /* If it's an `(', then parse the attribute arguments. */
25381 if (token
->type
== CPP_OPEN_PAREN
)
25383 vec
<tree
, va_gc
> *vec
;
25384 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25385 ? id_attr
: normal_attr
);
25386 vec
= cp_parser_parenthesized_expression_list
25387 (parser
, attr_flag
, /*cast_p=*/false,
25388 /*allow_expansion_p=*/false,
25389 /*non_constant_p=*/NULL
);
25391 arguments
= error_mark_node
;
25394 arguments
= build_tree_list_vec (vec
);
25395 release_tree_vector (vec
);
25397 /* Save the arguments away. */
25398 TREE_VALUE (attribute
) = arguments
;
25401 if (arguments
!= error_mark_node
)
25403 /* Add this attribute to the list. */
25404 TREE_CHAIN (attribute
) = attribute_list
;
25405 attribute_list
= attribute
;
25408 token
= cp_lexer_peek_token (parser
->lexer
);
25410 /* Now, look for more attributes. If the next token isn't a
25411 `,', we're done. */
25412 if (token
->type
!= CPP_COMMA
)
25415 /* Consume the comma and keep going. */
25416 cp_lexer_consume_token (parser
->lexer
);
25418 parser
->translate_strings_p
= save_translate_strings_p
;
25420 /* We built up the list in reverse order. */
25421 return nreverse (attribute_list
);
25424 /* Parse a standard C++11 attribute.
25426 The returned representation is a TREE_LIST which TREE_PURPOSE is
25427 the scoped name of the attribute, and the TREE_VALUE is its
25430 Note that the scoped name of the attribute is itself a TREE_LIST
25431 which TREE_PURPOSE is the namespace of the attribute, and
25432 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25433 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25434 and which TREE_PURPOSE is directly the attribute name.
25436 Clients of the attribute code should use get_attribute_namespace
25437 and get_attribute_name to get the actual namespace and name of
25438 attributes, regardless of their being GNU or C++11 attributes.
25441 attribute-token attribute-argument-clause [opt]
25445 attribute-scoped-token
25447 attribute-scoped-token:
25448 attribute-namespace :: identifier
25450 attribute-namespace:
25453 attribute-argument-clause:
25454 ( balanced-token-seq )
25456 balanced-token-seq:
25457 balanced-token [opt]
25458 balanced-token-seq balanced-token
25461 ( balanced-token-seq )
25462 [ balanced-token-seq ]
25463 { balanced-token-seq }. */
25466 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25468 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25471 temp_override
<bool> cleanup
25472 (parser
->auto_is_implicit_function_template_parm_p
, false);
25474 /* First, parse name of the attribute, a.k.a attribute-token. */
25476 token
= cp_lexer_peek_token (parser
->lexer
);
25477 if (token
->type
== CPP_NAME
)
25478 attr_id
= token
->u
.value
;
25479 else if (token
->type
== CPP_KEYWORD
)
25480 attr_id
= ridpointers
[(int) token
->keyword
];
25481 else if (token
->flags
& NAMED_OP
)
25482 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25484 if (attr_id
== NULL_TREE
)
25487 cp_lexer_consume_token (parser
->lexer
);
25489 token
= cp_lexer_peek_token (parser
->lexer
);
25490 if (token
->type
== CPP_SCOPE
)
25492 /* We are seeing a scoped attribute token. */
25494 cp_lexer_consume_token (parser
->lexer
);
25496 error_at (token
->location
, "attribute using prefix used together "
25497 "with scoped attribute token");
25500 token
= cp_lexer_consume_token (parser
->lexer
);
25501 if (token
->type
== CPP_NAME
)
25502 attr_id
= token
->u
.value
;
25503 else if (token
->type
== CPP_KEYWORD
)
25504 attr_id
= ridpointers
[(int) token
->keyword
];
25505 else if (token
->flags
& NAMED_OP
)
25506 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25509 error_at (token
->location
,
25510 "expected an identifier for the attribute name");
25511 return error_mark_node
;
25514 attr_ns
= canonicalize_attr_name (attr_ns
);
25515 attr_id
= canonicalize_attr_name (attr_id
);
25516 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25518 token
= cp_lexer_peek_token (parser
->lexer
);
25522 attr_ns
= canonicalize_attr_name (attr_ns
);
25523 attr_id
= canonicalize_attr_name (attr_id
);
25524 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25529 attr_id
= canonicalize_attr_name (attr_id
);
25530 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25532 /* C++11 noreturn attribute is equivalent to GNU's. */
25533 if (is_attribute_p ("noreturn", attr_id
))
25534 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25535 /* C++14 deprecated attribute is equivalent to GNU's. */
25536 else if (is_attribute_p ("deprecated", attr_id
))
25537 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25538 /* C++17 fallthrough attribute is equivalent to GNU's. */
25539 else if (is_attribute_p ("fallthrough", attr_id
))
25540 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25541 /* Transactional Memory TS optimize_for_synchronized attribute is
25542 equivalent to GNU transaction_callable. */
25543 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25544 TREE_PURPOSE (attribute
)
25545 = get_identifier ("transaction_callable");
25546 /* Transactional Memory attributes are GNU attributes. */
25547 else if (tm_attr_to_mask (attr_id
))
25548 TREE_PURPOSE (attribute
) = attr_id
;
25551 /* Now parse the optional argument clause of the attribute. */
25553 if (token
->type
!= CPP_OPEN_PAREN
)
25557 vec
<tree
, va_gc
> *vec
;
25558 int attr_flag
= normal_attr
;
25560 if (attr_ns
== gnu_identifier
25561 && attribute_takes_identifier_p (attr_id
))
25562 /* A GNU attribute that takes an identifier in parameter. */
25563 attr_flag
= id_attr
;
25565 vec
= cp_parser_parenthesized_expression_list
25566 (parser
, attr_flag
, /*cast_p=*/false,
25567 /*allow_expansion_p=*/true,
25568 /*non_constant_p=*/NULL
);
25570 arguments
= error_mark_node
;
25573 arguments
= build_tree_list_vec (vec
);
25574 release_tree_vector (vec
);
25577 if (arguments
== error_mark_node
)
25578 attribute
= error_mark_node
;
25580 TREE_VALUE (attribute
) = arguments
;
25586 /* Check that the attribute ATTRIBUTE appears at most once in the
25587 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25588 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25589 isn't implemented yet in GCC. */
25592 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25596 tree name
= get_attribute_name (attribute
);
25597 if (is_attribute_p ("noreturn", name
)
25598 && lookup_attribute ("noreturn", attributes
))
25599 error ("attribute %<noreturn%> can appear at most once "
25600 "in an attribute-list");
25601 else if (is_attribute_p ("deprecated", name
)
25602 && lookup_attribute ("deprecated", attributes
))
25603 error ("attribute %<deprecated%> can appear at most once "
25604 "in an attribute-list");
25608 /* Parse a list of standard C++-11 attributes.
25612 attribute-list , attribute[opt]
25614 attribute-list , attribute ...
25618 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25620 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25621 cp_token
*token
= NULL
;
25625 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25626 if (attribute
== error_mark_node
)
25628 if (attribute
!= NULL_TREE
)
25630 cp_parser_check_std_attribute (attributes
, attribute
);
25631 TREE_CHAIN (attribute
) = attributes
;
25632 attributes
= attribute
;
25634 token
= cp_lexer_peek_token (parser
->lexer
);
25635 if (token
->type
== CPP_ELLIPSIS
)
25637 cp_lexer_consume_token (parser
->lexer
);
25638 if (attribute
== NULL_TREE
)
25639 error_at (token
->location
,
25640 "expected attribute before %<...%>");
25643 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25644 if (pack
== error_mark_node
)
25645 return error_mark_node
;
25646 TREE_VALUE (attribute
) = pack
;
25648 token
= cp_lexer_peek_token (parser
->lexer
);
25650 if (token
->type
!= CPP_COMMA
)
25652 cp_lexer_consume_token (parser
->lexer
);
25654 attributes
= nreverse (attributes
);
25658 /* Parse a standard C++-11 attribute specifier.
25660 attribute-specifier:
25661 [ [ attribute-using-prefix [opt] attribute-list ] ]
25662 alignment-specifier
25664 attribute-using-prefix:
25665 using attribute-namespace :
25667 alignment-specifier:
25668 alignas ( type-id ... [opt] )
25669 alignas ( alignment-expression ... [opt] ). */
25672 cp_parser_std_attribute_spec (cp_parser
*parser
)
25674 tree attributes
= NULL_TREE
;
25675 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25677 if (token
->type
== CPP_OPEN_SQUARE
25678 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25680 tree attr_ns
= NULL_TREE
;
25682 cp_lexer_consume_token (parser
->lexer
);
25683 cp_lexer_consume_token (parser
->lexer
);
25685 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25687 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25688 if (token
->type
== CPP_NAME
)
25689 attr_ns
= token
->u
.value
;
25690 else if (token
->type
== CPP_KEYWORD
)
25691 attr_ns
= ridpointers
[(int) token
->keyword
];
25692 else if (token
->flags
& NAMED_OP
)
25693 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25696 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25698 if (cxx_dialect
< cxx17
25699 && !in_system_header_at (input_location
))
25700 pedwarn (input_location
, 0,
25701 "attribute using prefix only available "
25702 "with -std=c++17 or -std=gnu++17");
25704 cp_lexer_consume_token (parser
->lexer
);
25705 cp_lexer_consume_token (parser
->lexer
);
25706 cp_lexer_consume_token (parser
->lexer
);
25709 attr_ns
= NULL_TREE
;
25712 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25714 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25715 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25716 cp_parser_skip_to_end_of_statement (parser
);
25718 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25719 when we are sure that we have actually parsed them. */
25720 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25726 /* Look for an alignment-specifier. */
25728 token
= cp_lexer_peek_token (parser
->lexer
);
25730 if (token
->type
!= CPP_KEYWORD
25731 || token
->keyword
!= RID_ALIGNAS
)
25734 cp_lexer_consume_token (parser
->lexer
);
25735 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25737 matching_parens parens
;
25738 if (!parens
.require_open (parser
))
25739 return error_mark_node
;
25741 cp_parser_parse_tentatively (parser
);
25742 alignas_expr
= cp_parser_type_id (parser
);
25744 if (!cp_parser_parse_definitely (parser
))
25746 alignas_expr
= cp_parser_assignment_expression (parser
);
25747 if (alignas_expr
== error_mark_node
)
25748 cp_parser_skip_to_end_of_statement (parser
);
25749 if (alignas_expr
== NULL_TREE
25750 || alignas_expr
== error_mark_node
)
25751 return alignas_expr
;
25754 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25755 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25757 /* Handle alignas (pack...). */
25758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25760 cp_lexer_consume_token (parser
->lexer
);
25761 alignas_expr
= make_pack_expansion (alignas_expr
);
25764 /* Something went wrong, so don't build the attribute. */
25765 if (alignas_expr
== error_mark_node
)
25766 return error_mark_node
;
25768 if (!parens
.require_close (parser
))
25769 return error_mark_node
;
25771 /* Build the C++-11 representation of an 'aligned'
25774 = build_tree_list (build_tree_list (gnu_identifier
,
25775 aligned_identifier
), alignas_expr
);
25781 /* Parse a standard C++-11 attribute-specifier-seq.
25783 attribute-specifier-seq:
25784 attribute-specifier-seq [opt] attribute-specifier
25788 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25790 tree attr_specs
= NULL_TREE
;
25791 tree attr_last
= NULL_TREE
;
25795 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25796 if (attr_spec
== NULL_TREE
)
25798 if (attr_spec
== error_mark_node
)
25799 return error_mark_node
;
25802 TREE_CHAIN (attr_last
) = attr_spec
;
25804 attr_specs
= attr_last
= attr_spec
;
25805 attr_last
= tree_last (attr_last
);
25811 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25812 return index of the first token after balanced-token, or N on failure. */
25815 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25818 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25820 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25823 case CPP_PRAGMA_EOL
:
25824 /* Ran out of tokens. */
25826 case CPP_OPEN_PAREN
:
25829 case CPP_OPEN_BRACE
:
25832 case CPP_OPEN_SQUARE
:
25835 case CPP_CLOSE_PAREN
:
25838 case CPP_CLOSE_BRACE
:
25841 case CPP_CLOSE_SQUARE
:
25847 while (nparens
|| nbraces
|| nsquares
);
25851 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25852 return index of the first token after the GNU attribute tokens, or N on
25856 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25860 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25861 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25862 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25865 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25868 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25875 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25876 next token), return index of the first token after the standard C++11
25877 attribute tokens, or N on failure. */
25880 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25884 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25885 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25887 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25890 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25894 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25895 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25897 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25908 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25909 as the next token), return index of the first token after the attribute
25910 tokens, or N on failure. */
25913 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25915 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25916 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25917 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25920 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25921 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25922 current value of the PEDANTIC flag, regardless of whether or not
25923 the `__extension__' keyword is present. The caller is responsible
25924 for restoring the value of the PEDANTIC flag. */
25927 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25929 /* Save the old value of the PEDANTIC flag. */
25930 *saved_pedantic
= pedantic
;
25932 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25934 /* Consume the `__extension__' token. */
25935 cp_lexer_consume_token (parser
->lexer
);
25936 /* We're not being pedantic while the `__extension__' keyword is
25946 /* Parse a label declaration.
25949 __label__ label-declarator-seq ;
25951 label-declarator-seq:
25952 identifier , label-declarator-seq
25956 cp_parser_label_declaration (cp_parser
* parser
)
25958 /* Look for the `__label__' keyword. */
25959 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25965 /* Look for an identifier. */
25966 identifier
= cp_parser_identifier (parser
);
25967 /* If we failed, stop. */
25968 if (identifier
== error_mark_node
)
25970 /* Declare it as a label. */
25971 finish_label_decl (identifier
);
25972 /* If the next token is a `;', stop. */
25973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25975 /* Look for the `,' separating the label declarations. */
25976 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25979 /* Look for the final `;'. */
25980 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25983 // -------------------------------------------------------------------------- //
25986 // Parse a requires clause.
25988 // requires-clause:
25989 // 'requires' logical-or-expression
25991 // The required logical-or-expression must be a constant expression. Note
25992 // that we don't check that the expression is constepxr here. We defer until
25993 // we analyze constraints and then, we only check atomic constraints.
25995 cp_parser_requires_clause (cp_parser
*parser
)
25997 // Parse the requires clause so that it is not automatically folded.
25998 ++processing_template_decl
;
25999 tree expr
= cp_parser_binary_expression (parser
, false, false,
26000 PREC_NOT_OPERATOR
, NULL
);
26001 if (check_for_bare_parameter_packs (expr
))
26002 expr
= error_mark_node
;
26003 --processing_template_decl
;
26007 // Optionally parse a requires clause:
26009 cp_parser_requires_clause_opt (cp_parser
*parser
)
26011 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26012 if (tok
->keyword
!= RID_REQUIRES
)
26014 if (!flag_concepts
&& tok
->type
== CPP_NAME
26015 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26017 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26018 "%<requires%> only available with -fconcepts");
26019 /* Parse and discard the requires-clause. */
26020 cp_lexer_consume_token (parser
->lexer
);
26021 cp_parser_requires_clause (parser
);
26025 cp_lexer_consume_token (parser
->lexer
);
26026 return cp_parser_requires_clause (parser
);
26030 /*---------------------------------------------------------------------------
26031 Requires expressions
26032 ---------------------------------------------------------------------------*/
26034 /* Parse a requires expression
26036 requirement-expression:
26037 'requires' requirement-parameter-list [opt] requirement-body */
26039 cp_parser_requires_expression (cp_parser
*parser
)
26041 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26042 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26044 /* A requires-expression shall appear only within a concept
26045 definition or a requires-clause.
26047 TODO: Implement this diagnostic correctly. */
26048 if (!processing_template_decl
)
26050 error_at (loc
, "a requires expression cannot appear outside a template");
26051 cp_parser_skip_to_end_of_statement (parser
);
26052 return error_mark_node
;
26057 /* Local parameters are delared as variables within the scope
26058 of the expression. They are not visible past the end of
26059 the expression. Expressions within the requires-expression
26060 are unevaluated. */
26061 struct scope_sentinel
26065 ++cp_unevaluated_operand
;
26066 begin_scope (sk_block
, NULL_TREE
);
26071 pop_bindings_and_leave_scope ();
26072 --cp_unevaluated_operand
;
26076 /* Parse the optional parameter list. */
26077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26079 parms
= cp_parser_requirement_parameter_list (parser
);
26080 if (parms
== error_mark_node
)
26081 return error_mark_node
;
26086 /* Parse the requirement body. */
26087 reqs
= cp_parser_requirement_body (parser
);
26088 if (reqs
== error_mark_node
)
26089 return error_mark_node
;
26092 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26094 grokparms (parms
, &parms
);
26095 return finish_requires_expr (parms
, reqs
);
26098 /* Parse a parameterized requirement.
26100 requirement-parameter-list:
26101 '(' parameter-declaration-clause ')' */
26103 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26105 matching_parens parens
;
26106 if (!parens
.require_open (parser
))
26107 return error_mark_node
;
26109 tree parms
= cp_parser_parameter_declaration_clause (parser
);
26111 if (!parens
.require_close (parser
))
26112 return error_mark_node
;
26117 /* Parse the body of a requirement.
26120 '{' requirement-list '}' */
26122 cp_parser_requirement_body (cp_parser
*parser
)
26124 matching_braces braces
;
26125 if (!braces
.require_open (parser
))
26126 return error_mark_node
;
26128 tree reqs
= cp_parser_requirement_list (parser
);
26130 if (!braces
.require_close (parser
))
26131 return error_mark_node
;
26136 /* Parse a list of requirements.
26140 requirement-list ';' requirement[opt] */
26142 cp_parser_requirement_list (cp_parser
*parser
)
26144 tree result
= NULL_TREE
;
26147 tree req
= cp_parser_requirement (parser
);
26148 if (req
== error_mark_node
)
26149 return error_mark_node
;
26151 result
= tree_cons (NULL_TREE
, req
, result
);
26153 /* If we see a semi-colon, consume it. */
26154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26155 cp_lexer_consume_token (parser
->lexer
);
26157 /* Stop processing at the end of the list. */
26158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26162 /* Reverse the order of requirements so they are analyzed in
26163 declaration order. */
26164 return nreverse (result
);
26167 /* Parse a syntactic requirement or type requirement.
26171 compound-requirement
26173 nested-requirement */
26175 cp_parser_requirement (cp_parser
*parser
)
26177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26178 return cp_parser_compound_requirement (parser
);
26179 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26180 return cp_parser_type_requirement (parser
);
26181 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26182 return cp_parser_nested_requirement (parser
);
26184 return cp_parser_simple_requirement (parser
);
26187 /* Parse a simple requirement.
26189 simple-requirement:
26192 cp_parser_simple_requirement (cp_parser
*parser
)
26194 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26195 if (!expr
|| expr
== error_mark_node
)
26196 return error_mark_node
;
26198 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26199 return error_mark_node
;
26201 return finish_simple_requirement (expr
);
26204 /* Parse a type requirement
26207 nested-name-specifier [opt] required-type-name ';'
26209 required-type-name:
26211 'template' [opt] simple-template-id */
26213 cp_parser_type_requirement (cp_parser
*parser
)
26215 cp_lexer_consume_token (parser
->lexer
);
26217 // Save the scope before parsing name specifiers.
26218 tree saved_scope
= parser
->scope
;
26219 tree saved_object_scope
= parser
->object_scope
;
26220 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26221 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26222 cp_parser_nested_name_specifier_opt (parser
,
26223 /*typename_keyword_p=*/true,
26224 /*check_dependency_p=*/false,
26226 /*is_declaration=*/false);
26229 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26231 cp_lexer_consume_token (parser
->lexer
);
26232 type
= cp_parser_template_id (parser
,
26233 /*template_keyword_p=*/true,
26234 /*check_dependency=*/false,
26235 /*tag_type=*/none_type
,
26236 /*is_declaration=*/false);
26237 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26238 /*complain=*/tf_error
);
26241 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26243 if (TREE_CODE (type
) == TYPE_DECL
)
26244 type
= TREE_TYPE (type
);
26246 parser
->scope
= saved_scope
;
26247 parser
->object_scope
= saved_object_scope
;
26248 parser
->qualifying_scope
= saved_qualifying_scope
;
26250 if (type
== error_mark_node
)
26251 cp_parser_skip_to_end_of_statement (parser
);
26253 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26254 return error_mark_node
;
26255 if (type
== error_mark_node
)
26256 return error_mark_node
;
26258 return finish_type_requirement (type
);
26261 /* Parse a compound requirement
26263 compound-requirement:
26264 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26266 cp_parser_compound_requirement (cp_parser
*parser
)
26268 /* Parse an expression enclosed in '{ }'s. */
26269 matching_braces braces
;
26270 if (!braces
.require_open (parser
))
26271 return error_mark_node
;
26273 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26274 if (!expr
|| expr
== error_mark_node
)
26275 return error_mark_node
;
26277 if (!braces
.require_close (parser
))
26278 return error_mark_node
;
26280 /* Parse the optional noexcept. */
26281 bool noexcept_p
= false;
26282 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26284 cp_lexer_consume_token (parser
->lexer
);
26288 /* Parse the optional trailing return type. */
26289 tree type
= NULL_TREE
;
26290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26292 cp_lexer_consume_token (parser
->lexer
);
26293 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26294 parser
->in_result_type_constraint_p
= true;
26295 type
= cp_parser_trailing_type_id (parser
);
26296 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26297 if (type
== error_mark_node
)
26298 return error_mark_node
;
26301 return finish_compound_requirement (expr
, type
, noexcept_p
);
26304 /* Parse a nested requirement. This is the same as a requires clause.
26306 nested-requirement:
26309 cp_parser_nested_requirement (cp_parser
*parser
)
26311 cp_lexer_consume_token (parser
->lexer
);
26312 tree req
= cp_parser_requires_clause (parser
);
26313 if (req
== error_mark_node
)
26314 return error_mark_node
;
26315 return finish_nested_requirement (req
);
26318 /* Support Functions */
26320 /* Return the appropriate prefer_type argument for lookup_name_real based on
26321 tag_type and template_mem_access. */
26324 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26326 /* DR 141: When looking in the current enclosing context for a template-name
26327 after -> or ., only consider class templates. */
26328 if (template_mem_access
)
26332 case none_type
: return 0; // No preference.
26333 case scope_type
: return 1; // Type or namespace.
26334 default: return 2; // Type only.
26338 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26339 NAME should have one of the representations used for an
26340 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26341 is returned. If PARSER->SCOPE is a dependent type, then a
26342 SCOPE_REF is returned.
26344 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26345 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26346 was formed. Abstractly, such entities should not be passed to this
26347 function, because they do not need to be looked up, but it is
26348 simpler to check for this special case here, rather than at the
26351 In cases not explicitly covered above, this function returns a
26352 DECL, OVERLOAD, or baselink representing the result of the lookup.
26353 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26356 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26357 (e.g., "struct") that was used. In that case bindings that do not
26358 refer to types are ignored.
26360 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26363 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26366 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26369 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26370 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26371 NULL_TREE otherwise. */
26374 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26375 enum tag_types tag_type
,
26378 bool check_dependency
,
26379 tree
*ambiguous_decls
,
26380 location_t name_location
)
26383 tree object_type
= parser
->context
->object_type
;
26385 /* Assume that the lookup will be unambiguous. */
26386 if (ambiguous_decls
)
26387 *ambiguous_decls
= NULL_TREE
;
26389 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26390 no longer valid. Note that if we are parsing tentatively, and
26391 the parse fails, OBJECT_TYPE will be automatically restored. */
26392 parser
->context
->object_type
= NULL_TREE
;
26394 if (name
== error_mark_node
)
26395 return error_mark_node
;
26397 /* A template-id has already been resolved; there is no lookup to
26399 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26401 if (BASELINK_P (name
))
26403 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26404 == TEMPLATE_ID_EXPR
);
26408 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26409 it should already have been checked to make sure that the name
26410 used matches the type being destroyed. */
26411 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26415 /* Figure out to which type this destructor applies. */
26417 type
= parser
->scope
;
26418 else if (object_type
)
26419 type
= object_type
;
26421 type
= current_class_type
;
26422 /* If that's not a class type, there is no destructor. */
26423 if (!type
|| !CLASS_TYPE_P (type
))
26424 return error_mark_node
;
26426 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26427 lazily_declare_fn (sfk_destructor
, type
);
26429 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26432 return error_mark_node
;
26435 /* By this point, the NAME should be an ordinary identifier. If
26436 the id-expression was a qualified name, the qualifying scope is
26437 stored in PARSER->SCOPE at this point. */
26438 gcc_assert (identifier_p (name
));
26440 /* Perform the lookup. */
26445 if (parser
->scope
== error_mark_node
)
26446 return error_mark_node
;
26448 /* If the SCOPE is dependent, the lookup must be deferred until
26449 the template is instantiated -- unless we are explicitly
26450 looking up names in uninstantiated templates. Even then, we
26451 cannot look up the name if the scope is not a class type; it
26452 might, for example, be a template type parameter. */
26453 dependent_p
= (TYPE_P (parser
->scope
)
26454 && dependent_scope_p (parser
->scope
));
26455 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26457 /* Defer lookup. */
26458 decl
= error_mark_node
;
26461 tree pushed_scope
= NULL_TREE
;
26463 /* If PARSER->SCOPE is a dependent type, then it must be a
26464 class type, and we must not be checking dependencies;
26465 otherwise, we would have processed this lookup above. So
26466 that PARSER->SCOPE is not considered a dependent base by
26467 lookup_member, we must enter the scope here. */
26469 pushed_scope
= push_scope (parser
->scope
);
26471 /* If the PARSER->SCOPE is a template specialization, it
26472 may be instantiated during name lookup. In that case,
26473 errors may be issued. Even if we rollback the current
26474 tentative parse, those errors are valid. */
26475 decl
= lookup_qualified_name (parser
->scope
, name
,
26476 prefer_type_arg (tag_type
),
26477 /*complain=*/true);
26479 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26480 lookup result and the nested-name-specifier nominates a class C:
26481 * if the name specified after the nested-name-specifier, when
26482 looked up in C, is the injected-class-name of C (Clause 9), or
26483 * if the name specified after the nested-name-specifier is the
26484 same as the identifier or the simple-template-id's template-
26485 name in the last component of the nested-name-specifier,
26486 the name is instead considered to name the constructor of
26487 class C. [ Note: for example, the constructor is not an
26488 acceptable lookup result in an elaborated-type-specifier so
26489 the constructor would not be used in place of the
26490 injected-class-name. --end note ] Such a constructor name
26491 shall be used only in the declarator-id of a declaration that
26492 names a constructor or in a using-declaration. */
26493 if (tag_type
== none_type
26494 && DECL_SELF_REFERENCE_P (decl
)
26495 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26496 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26497 prefer_type_arg (tag_type
),
26498 /*complain=*/true);
26500 /* If we have a single function from a using decl, pull it out. */
26501 if (TREE_CODE (decl
) == OVERLOAD
26502 && !really_overloaded_fn (decl
))
26503 decl
= OVL_FUNCTION (decl
);
26506 pop_scope (pushed_scope
);
26509 /* If the scope is a dependent type and either we deferred lookup or
26510 we did lookup but didn't find the name, rememeber the name. */
26511 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26512 && dependent_type_p (parser
->scope
))
26518 /* The resolution to Core Issue 180 says that `struct
26519 A::B' should be considered a type-name, even if `A'
26521 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26522 /*complain=*/tf_error
);
26523 if (type
!= error_mark_node
)
26524 decl
= TYPE_NAME (type
);
26526 else if (is_template
26527 && (cp_parser_next_token_ends_template_argument_p (parser
)
26528 || cp_lexer_next_token_is (parser
->lexer
,
26530 decl
= make_unbound_class_template (parser
->scope
,
26532 /*complain=*/tf_error
);
26534 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26535 parser
->scope
, name
,
26538 parser
->qualifying_scope
= parser
->scope
;
26539 parser
->object_scope
= NULL_TREE
;
26541 else if (object_type
)
26543 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26544 OBJECT_TYPE is not a class. */
26545 if (CLASS_TYPE_P (object_type
))
26546 /* If the OBJECT_TYPE is a template specialization, it may
26547 be instantiated during name lookup. In that case, errors
26548 may be issued. Even if we rollback the current tentative
26549 parse, those errors are valid. */
26550 decl
= lookup_member (object_type
,
26553 prefer_type_arg (tag_type
),
26554 tf_warning_or_error
);
26559 /* Look it up in the enclosing context. DR 141: When looking for a
26560 template-name after -> or ., only consider class templates. */
26561 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26563 /*block_p=*/true, is_namespace
, 0);
26564 if (object_type
== unknown_type_node
)
26565 /* The object is type-dependent, so we can't look anything up; we used
26566 this to get the DR 141 behavior. */
26567 object_type
= NULL_TREE
;
26568 parser
->object_scope
= object_type
;
26569 parser
->qualifying_scope
= NULL_TREE
;
26573 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26575 /*block_p=*/true, is_namespace
, 0);
26576 parser
->qualifying_scope
= NULL_TREE
;
26577 parser
->object_scope
= NULL_TREE
;
26580 /* If the lookup failed, let our caller know. */
26581 if (!decl
|| decl
== error_mark_node
)
26582 return error_mark_node
;
26584 /* Pull out the template from an injected-class-name (or multiple). */
26586 decl
= maybe_get_template_decl_from_type_decl (decl
);
26588 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26589 if (TREE_CODE (decl
) == TREE_LIST
)
26591 if (ambiguous_decls
)
26592 *ambiguous_decls
= decl
;
26593 /* The error message we have to print is too complicated for
26594 cp_parser_error, so we incorporate its actions directly. */
26595 if (!cp_parser_simulate_error (parser
))
26597 error_at (name_location
, "reference to %qD is ambiguous",
26599 print_candidates (decl
);
26601 return error_mark_node
;
26604 gcc_assert (DECL_P (decl
)
26605 || TREE_CODE (decl
) == OVERLOAD
26606 || TREE_CODE (decl
) == SCOPE_REF
26607 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26608 || BASELINK_P (decl
));
26610 /* If we have resolved the name of a member declaration, check to
26611 see if the declaration is accessible. When the name resolves to
26612 set of overloaded functions, accessibility is checked when
26613 overload resolution is done.
26615 During an explicit instantiation, access is not checked at all,
26616 as per [temp.explicit]. */
26618 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26620 maybe_record_typedef_use (decl
);
26622 return cp_expr (decl
, name_location
);
26625 /* Like cp_parser_lookup_name, but for use in the typical case where
26626 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26627 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26630 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26632 return cp_parser_lookup_name (parser
, name
,
26634 /*is_template=*/false,
26635 /*is_namespace=*/false,
26636 /*check_dependency=*/true,
26637 /*ambiguous_decls=*/NULL
,
26641 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26642 the current context, return the TYPE_DECL. If TAG_NAME_P is
26643 true, the DECL indicates the class being defined in a class-head,
26644 or declared in an elaborated-type-specifier.
26646 Otherwise, return DECL. */
26649 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26651 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26652 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26655 template <typename T> struct B;
26658 template <typename T> struct A::B {};
26660 Similarly, in an elaborated-type-specifier:
26662 namespace N { struct X{}; }
26665 template <typename T> friend struct N::X;
26668 However, if the DECL refers to a class type, and we are in
26669 the scope of the class, then the name lookup automatically
26670 finds the TYPE_DECL created by build_self_reference rather
26671 than a TEMPLATE_DECL. For example, in:
26673 template <class T> struct S {
26677 there is no need to handle such case. */
26679 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26680 return DECL_TEMPLATE_RESULT (decl
);
26685 /* If too many, or too few, template-parameter lists apply to the
26686 declarator, issue an error message. Returns TRUE if all went well,
26687 and FALSE otherwise. */
26690 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26691 cp_declarator
*declarator
,
26692 location_t declarator_location
)
26694 switch (declarator
->kind
)
26698 unsigned num_templates
= 0;
26699 tree scope
= declarator
->u
.id
.qualifying_scope
;
26700 bool template_id_p
= false;
26703 num_templates
= num_template_headers_for_class (scope
);
26704 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26705 == TEMPLATE_ID_EXPR
)
26707 /* If the DECLARATOR has the form `X<y>' then it uses one
26708 additional level of template parameters. */
26710 template_id_p
= true;
26713 return cp_parser_check_template_parameters
26714 (parser
, num_templates
, template_id_p
, declarator_location
,
26721 case cdk_reference
:
26723 return (cp_parser_check_declarator_template_parameters
26724 (parser
, declarator
->declarator
, declarator_location
));
26731 gcc_unreachable ();
26736 /* NUM_TEMPLATES were used in the current declaration. If that is
26737 invalid, return FALSE and issue an error messages. Otherwise,
26738 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26739 declarator and we can print more accurate diagnostics. */
26742 cp_parser_check_template_parameters (cp_parser
* parser
,
26743 unsigned num_templates
,
26744 bool template_id_p
,
26745 location_t location
,
26746 cp_declarator
*declarator
)
26748 /* If there are the same number of template classes and parameter
26749 lists, that's OK. */
26750 if (parser
->num_template_parameter_lists
== num_templates
)
26752 /* If there are more, but only one more, and the name ends in an identifier,
26753 then we are declaring a primary template. That's OK too. */
26755 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26757 /* If there are more template classes than parameter lists, we have
26760 template <class T> void S<T>::R<T>::f (); */
26761 if (parser
->num_template_parameter_lists
< num_templates
)
26763 if (declarator
&& !current_function_decl
)
26764 error_at (location
, "specializing member %<%T::%E%> "
26765 "requires %<template<>%> syntax",
26766 declarator
->u
.id
.qualifying_scope
,
26767 declarator
->u
.id
.unqualified_name
);
26768 else if (declarator
)
26769 error_at (location
, "invalid declaration of %<%T::%E%>",
26770 declarator
->u
.id
.qualifying_scope
,
26771 declarator
->u
.id
.unqualified_name
);
26773 error_at (location
, "too few template-parameter-lists");
26776 /* Otherwise, there are too many template parameter lists. We have
26779 template <class T> template <class U> void S::f(); */
26780 error_at (location
, "too many template-parameter-lists");
26784 /* Parse an optional `::' token indicating that the following name is
26785 from the global namespace. If so, PARSER->SCOPE is set to the
26786 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26787 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26788 Returns the new value of PARSER->SCOPE, if the `::' token is
26789 present, and NULL_TREE otherwise. */
26792 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26796 /* Peek at the next token. */
26797 token
= cp_lexer_peek_token (parser
->lexer
);
26798 /* If we're looking at a `::' token then we're starting from the
26799 global namespace, not our current location. */
26800 if (token
->type
== CPP_SCOPE
)
26802 /* Consume the `::' token. */
26803 cp_lexer_consume_token (parser
->lexer
);
26804 /* Set the SCOPE so that we know where to start the lookup. */
26805 parser
->scope
= global_namespace
;
26806 parser
->qualifying_scope
= global_namespace
;
26807 parser
->object_scope
= NULL_TREE
;
26809 return parser
->scope
;
26811 else if (!current_scope_valid_p
)
26813 parser
->scope
= NULL_TREE
;
26814 parser
->qualifying_scope
= NULL_TREE
;
26815 parser
->object_scope
= NULL_TREE
;
26821 /* Returns TRUE if the upcoming token sequence is the start of a
26822 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26823 declarator is preceded by the `friend' specifier. */
26826 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26828 bool constructor_p
;
26829 bool outside_class_specifier_p
;
26830 tree nested_name_specifier
;
26831 cp_token
*next_token
;
26833 /* The common case is that this is not a constructor declarator, so
26834 try to avoid doing lots of work if at all possible. It's not
26835 valid declare a constructor at function scope. */
26836 if (parser
->in_function_body
)
26838 /* And only certain tokens can begin a constructor declarator. */
26839 next_token
= cp_lexer_peek_token (parser
->lexer
);
26840 if (next_token
->type
!= CPP_NAME
26841 && next_token
->type
!= CPP_SCOPE
26842 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26843 && next_token
->type
!= CPP_TEMPLATE_ID
)
26846 /* Parse tentatively; we are going to roll back all of the tokens
26848 cp_parser_parse_tentatively (parser
);
26849 /* Assume that we are looking at a constructor declarator. */
26850 constructor_p
= true;
26852 /* Look for the optional `::' operator. */
26853 cp_parser_global_scope_opt (parser
,
26854 /*current_scope_valid_p=*/false);
26855 /* Look for the nested-name-specifier. */
26856 nested_name_specifier
26857 = (cp_parser_nested_name_specifier_opt (parser
,
26858 /*typename_keyword_p=*/false,
26859 /*check_dependency_p=*/false,
26861 /*is_declaration=*/false));
26863 outside_class_specifier_p
= (!at_class_scope_p ()
26864 || !TYPE_BEING_DEFINED (current_class_type
)
26867 /* Outside of a class-specifier, there must be a
26868 nested-name-specifier. Except in C++17 mode, where we
26869 might be declaring a guiding declaration. */
26870 if (!nested_name_specifier
&& outside_class_specifier_p
26871 && cxx_dialect
< cxx17
)
26872 constructor_p
= false;
26873 else if (nested_name_specifier
== error_mark_node
)
26874 constructor_p
= false;
26876 /* If we have a class scope, this is easy; DR 147 says that S::S always
26877 names the constructor, and no other qualified name could. */
26878 if (constructor_p
&& nested_name_specifier
26879 && CLASS_TYPE_P (nested_name_specifier
))
26881 tree id
= cp_parser_unqualified_id (parser
,
26882 /*template_keyword_p=*/false,
26883 /*check_dependency_p=*/false,
26884 /*declarator_p=*/true,
26885 /*optional_p=*/false);
26886 if (is_overloaded_fn (id
))
26887 id
= DECL_NAME (get_first_fn (id
));
26888 if (!constructor_name_p (id
, nested_name_specifier
))
26889 constructor_p
= false;
26891 /* If we still think that this might be a constructor-declarator,
26892 look for a class-name. */
26893 else if (constructor_p
)
26897 template <typename T> struct S {
26901 we must recognize that the nested `S' names a class. */
26902 if (cxx_dialect
>= cxx17
)
26903 cp_parser_parse_tentatively (parser
);
26906 type_decl
= cp_parser_class_name (parser
,
26907 /*typename_keyword_p=*/false,
26908 /*template_keyword_p=*/false,
26910 /*check_dependency_p=*/false,
26911 /*class_head_p=*/false,
26912 /*is_declaration=*/false);
26914 if (cxx_dialect
>= cxx17
26915 && !cp_parser_parse_definitely (parser
))
26917 type_decl
= NULL_TREE
;
26918 tree tmpl
= cp_parser_template_name (parser
,
26919 /*template_keyword*/false,
26920 /*check_dependency_p*/false,
26921 /*is_declaration*/false,
26923 /*is_identifier*/NULL
);
26924 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26925 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26926 /* It's a deduction guide, return true. */;
26928 cp_parser_simulate_error (parser
);
26931 /* If there was no class-name, then this is not a constructor.
26932 Otherwise, if we are in a class-specifier and we aren't
26933 handling a friend declaration, check that its type matches
26934 current_class_type (c++/38313). Note: error_mark_node
26935 is left alone for error recovery purposes. */
26936 constructor_p
= (!cp_parser_error_occurred (parser
)
26937 && (outside_class_specifier_p
26938 || type_decl
== NULL_TREE
26939 || type_decl
== error_mark_node
26940 || same_type_p (current_class_type
,
26941 TREE_TYPE (type_decl
))));
26943 /* If we're still considering a constructor, we have to see a `(',
26944 to begin the parameter-declaration-clause, followed by either a
26945 `)', an `...', or a decl-specifier. We need to check for a
26946 type-specifier to avoid being fooled into thinking that:
26950 is a constructor. (It is actually a function named `f' that
26951 takes one parameter (of type `int') and returns a value of type
26954 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26955 constructor_p
= false;
26958 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26959 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26960 /* A parameter declaration begins with a decl-specifier,
26961 which is either the "attribute" keyword, a storage class
26962 specifier, or (usually) a type-specifier. */
26963 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26966 tree pushed_scope
= NULL_TREE
;
26967 unsigned saved_num_template_parameter_lists
;
26969 /* Names appearing in the type-specifier should be looked up
26970 in the scope of the class. */
26971 if (current_class_type
)
26973 else if (type_decl
)
26975 type
= TREE_TYPE (type_decl
);
26976 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26978 type
= resolve_typename_type (type
,
26979 /*only_current_p=*/false);
26980 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26982 cp_parser_abort_tentative_parse (parser
);
26986 pushed_scope
= push_scope (type
);
26989 /* Inside the constructor parameter list, surrounding
26990 template-parameter-lists do not apply. */
26991 saved_num_template_parameter_lists
26992 = parser
->num_template_parameter_lists
;
26993 parser
->num_template_parameter_lists
= 0;
26995 /* Look for the type-specifier. */
26996 cp_parser_type_specifier (parser
,
26997 CP_PARSER_FLAGS_NONE
,
26998 /*decl_specs=*/NULL
,
26999 /*is_declarator=*/true,
27000 /*declares_class_or_enum=*/NULL
,
27001 /*is_cv_qualifier=*/NULL
);
27003 parser
->num_template_parameter_lists
27004 = saved_num_template_parameter_lists
;
27006 /* Leave the scope of the class. */
27008 pop_scope (pushed_scope
);
27010 constructor_p
= !cp_parser_error_occurred (parser
);
27014 /* We did not really want to consume any tokens. */
27015 cp_parser_abort_tentative_parse (parser
);
27017 return constructor_p
;
27020 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27021 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27022 they must be performed once we are in the scope of the function.
27024 Returns the function defined. */
27027 cp_parser_function_definition_from_specifiers_and_declarator
27028 (cp_parser
* parser
,
27029 cp_decl_specifier_seq
*decl_specifiers
,
27031 const cp_declarator
*declarator
)
27036 /* Begin the function-definition. */
27037 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27039 /* The things we're about to see are not directly qualified by any
27040 template headers we've seen thus far. */
27041 reset_specialization ();
27043 /* If there were names looked up in the decl-specifier-seq that we
27044 did not check, check them now. We must wait until we are in the
27045 scope of the function to perform the checks, since the function
27046 might be a friend. */
27047 perform_deferred_access_checks (tf_warning_or_error
);
27051 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27052 parser
->omp_declare_simd
= NULL
;
27053 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27054 parser
->oacc_routine
= NULL
;
27059 /* Skip the entire function. */
27060 cp_parser_skip_to_end_of_block_or_statement (parser
);
27061 fn
= error_mark_node
;
27063 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27065 /* Seen already, skip it. An error message has already been output. */
27066 cp_parser_skip_to_end_of_block_or_statement (parser
);
27067 fn
= current_function_decl
;
27068 current_function_decl
= NULL_TREE
;
27069 /* If this is a function from a class, pop the nested class. */
27070 if (current_class_name
)
27071 pop_nested_class ();
27076 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27077 tv
= TV_PARSE_INLINE
;
27079 tv
= TV_PARSE_FUNC
;
27081 fn
= cp_parser_function_definition_after_declarator (parser
,
27082 /*inline_p=*/false);
27089 /* Parse the part of a function-definition that follows the
27090 declarator. INLINE_P is TRUE iff this function is an inline
27091 function defined within a class-specifier.
27093 Returns the function defined. */
27096 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27100 bool saved_in_unbraced_linkage_specification_p
;
27101 bool saved_in_function_body
;
27102 unsigned saved_num_template_parameter_lists
;
27104 bool fully_implicit_function_template_p
27105 = parser
->fully_implicit_function_template_p
;
27106 parser
->fully_implicit_function_template_p
= false;
27107 tree implicit_template_parms
27108 = parser
->implicit_template_parms
;
27109 parser
->implicit_template_parms
= 0;
27110 cp_binding_level
* implicit_template_scope
27111 = parser
->implicit_template_scope
;
27112 parser
->implicit_template_scope
= 0;
27114 saved_in_function_body
= parser
->in_function_body
;
27115 parser
->in_function_body
= true;
27116 /* If the next token is `return', then the code may be trying to
27117 make use of the "named return value" extension that G++ used to
27119 token
= cp_lexer_peek_token (parser
->lexer
);
27120 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27122 /* Consume the `return' keyword. */
27123 cp_lexer_consume_token (parser
->lexer
);
27124 /* Look for the identifier that indicates what value is to be
27126 cp_parser_identifier (parser
);
27127 /* Issue an error message. */
27128 error_at (token
->location
,
27129 "named return values are no longer supported");
27130 /* Skip tokens until we reach the start of the function body. */
27133 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27134 if (token
->type
== CPP_OPEN_BRACE
27135 || token
->type
== CPP_EOF
27136 || token
->type
== CPP_PRAGMA_EOL
)
27138 cp_lexer_consume_token (parser
->lexer
);
27141 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27142 anything declared inside `f'. */
27143 saved_in_unbraced_linkage_specification_p
27144 = parser
->in_unbraced_linkage_specification_p
;
27145 parser
->in_unbraced_linkage_specification_p
= false;
27146 /* Inside the function, surrounding template-parameter-lists do not
27148 saved_num_template_parameter_lists
27149 = parser
->num_template_parameter_lists
;
27150 parser
->num_template_parameter_lists
= 0;
27152 /* If the next token is `try', `__transaction_atomic', or
27153 `__transaction_relaxed`, then we are looking at either function-try-block
27154 or function-transaction-block. Note that all of these include the
27156 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27157 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27158 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27159 RID_TRANSACTION_RELAXED
))
27160 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27161 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27162 cp_parser_function_try_block (parser
);
27164 cp_parser_ctor_initializer_opt_and_function_body
27165 (parser
, /*in_function_try_block=*/false);
27167 /* Finish the function. */
27168 fn
= finish_function (inline_p
);
27169 /* Generate code for it, if necessary. */
27170 expand_or_defer_fn (fn
);
27171 /* Restore the saved values. */
27172 parser
->in_unbraced_linkage_specification_p
27173 = saved_in_unbraced_linkage_specification_p
;
27174 parser
->num_template_parameter_lists
27175 = saved_num_template_parameter_lists
;
27176 parser
->in_function_body
= saved_in_function_body
;
27178 parser
->fully_implicit_function_template_p
27179 = fully_implicit_function_template_p
;
27180 parser
->implicit_template_parms
27181 = implicit_template_parms
;
27182 parser
->implicit_template_scope
27183 = implicit_template_scope
;
27185 if (parser
->fully_implicit_function_template_p
)
27186 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27191 /* Parse a template-declaration body (following argument list). */
27194 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27195 tree parameter_list
,
27198 tree decl
= NULL_TREE
;
27199 bool friend_p
= false;
27201 /* We just processed one more parameter list. */
27202 ++parser
->num_template_parameter_lists
;
27204 /* Get the deferred access checks from the parameter list. These
27205 will be checked once we know what is being declared, as for a
27206 member template the checks must be performed in the scope of the
27207 class containing the member. */
27208 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27210 /* Tentatively parse for a new template parameter list, which can either be
27211 the template keyword or a template introduction. */
27212 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27214 else if (cxx_dialect
>= cxx11
27215 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27216 decl
= cp_parser_alias_declaration (parser
);
27219 /* There are no access checks when parsing a template, as we do not
27220 know if a specialization will be a friend. */
27221 push_deferring_access_checks (dk_no_check
);
27222 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27223 decl
= cp_parser_single_declaration (parser
,
27226 /*explicit_specialization_p=*/false,
27228 pop_deferring_access_checks ();
27230 /* If this is a member template declaration, let the front
27232 if (member_p
&& !friend_p
&& decl
)
27234 if (TREE_CODE (decl
) == TYPE_DECL
)
27235 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27237 decl
= finish_member_template_decl (decl
);
27239 else if (friend_p
&& decl
27240 && DECL_DECLARES_TYPE_P (decl
))
27241 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27242 /*complain=*/true);
27244 /* We are done with the current parameter list. */
27245 --parser
->num_template_parameter_lists
;
27247 pop_deferring_access_checks ();
27250 finish_template_decl (parameter_list
);
27252 /* Check the template arguments for a literal operator template. */
27254 && DECL_DECLARES_FUNCTION_P (decl
)
27255 && UDLIT_OPER_P (DECL_NAME (decl
)))
27258 if (parameter_list
== NULL_TREE
)
27262 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27263 if (num_parms
== 1)
27265 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27266 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27267 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27268 /* OK, C++20 string literal operator template. We don't need
27269 to warn in lower dialects here because we will have already
27270 warned about the template parameter. */;
27271 else if (TREE_TYPE (parm
) != char_type_node
27272 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27275 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27277 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27278 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27279 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27280 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27281 if (parm
== error_mark_node
27282 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27283 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27286 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27287 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27288 "ISO C++ did not adopt string literal operator templa"
27289 "tes taking an argument pack of characters");
27296 if (cxx_dialect
> cxx17
)
27297 error ("literal operator template %qD has invalid parameter list;"
27298 " Expected non-type template parameter pack <char...> "
27299 " or single non-type parameter of class type",
27302 error ("literal operator template %qD has invalid parameter list."
27303 " Expected non-type template parameter pack <char...>",
27308 /* Register member declarations. */
27309 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27310 finish_member_declaration (decl
);
27311 /* If DECL is a function template, we must return to parse it later.
27312 (Even though there is no definition, there might be default
27313 arguments that need handling.) */
27314 if (member_p
&& decl
27315 && DECL_DECLARES_FUNCTION_P (decl
))
27316 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27319 /* Parse a template introduction header for a template-declaration. Returns
27320 false if tentative parse fails. */
27323 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27325 cp_parser_parse_tentatively (parser
);
27327 tree saved_scope
= parser
->scope
;
27328 tree saved_object_scope
= parser
->object_scope
;
27329 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27331 /* Look for the optional `::' operator. */
27332 cp_parser_global_scope_opt (parser
,
27333 /*current_scope_valid_p=*/false);
27334 /* Look for the nested-name-specifier. */
27335 cp_parser_nested_name_specifier_opt (parser
,
27336 /*typename_keyword_p=*/false,
27337 /*check_dependency_p=*/true,
27339 /*is_declaration=*/false);
27341 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27342 tree concept_name
= cp_parser_identifier (parser
);
27344 /* Look up the concept for which we will be matching
27345 template parameters. */
27346 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27348 parser
->scope
= saved_scope
;
27349 parser
->object_scope
= saved_object_scope
;
27350 parser
->qualifying_scope
= saved_qualifying_scope
;
27352 if (concept_name
== error_mark_node
)
27353 cp_parser_simulate_error (parser
);
27355 /* Look for opening brace for introduction. */
27356 matching_braces braces
;
27357 braces
.require_open (parser
);
27359 if (!cp_parser_parse_definitely (parser
))
27362 push_deferring_access_checks (dk_deferred
);
27364 /* Build vector of placeholder parameters and grab
27365 matching identifiers. */
27366 tree introduction_list
= cp_parser_introduction_list (parser
);
27368 /* Look for closing brace for introduction. */
27369 if (!braces
.require_close (parser
))
27372 /* The introduction-list shall not be empty. */
27373 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27376 /* In cp_parser_introduction_list we have already issued an error. */
27380 if (tmpl_decl
== error_mark_node
)
27382 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27387 /* Build and associate the constraint. */
27388 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27389 if (parms
&& parms
!= error_mark_node
)
27391 cp_parser_template_declaration_after_parameters (parser
, parms
,
27396 error_at (token
->location
, "no matching concept for template-introduction");
27400 /* Parse a normal template-declaration following the template keyword. */
27403 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27405 tree parameter_list
;
27406 bool need_lang_pop
;
27407 location_t location
= input_location
;
27409 /* Look for the `<' token. */
27410 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27412 if (at_class_scope_p () && current_function_decl
)
27414 /* 14.5.2.2 [temp.mem]
27416 A local class shall not have member templates. */
27417 error_at (location
,
27418 "invalid declaration of member template in local class");
27419 cp_parser_skip_to_end_of_block_or_statement (parser
);
27424 A template ... shall not have C linkage. */
27425 if (current_lang_name
== lang_name_c
)
27427 error_at (location
, "template with C linkage");
27428 maybe_show_extern_c_location ();
27429 /* Give it C++ linkage to avoid confusing other parts of the
27431 push_lang_context (lang_name_cplusplus
);
27432 need_lang_pop
= true;
27435 need_lang_pop
= false;
27437 /* We cannot perform access checks on the template parameter
27438 declarations until we know what is being declared, just as we
27439 cannot check the decl-specifier list. */
27440 push_deferring_access_checks (dk_deferred
);
27442 /* If the next token is `>', then we have an invalid
27443 specialization. Rather than complain about an invalid template
27444 parameter, issue an error message here. */
27445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27447 cp_parser_error (parser
, "invalid explicit specialization");
27448 begin_specialization ();
27449 parameter_list
= NULL_TREE
;
27453 /* Parse the template parameters. */
27454 parameter_list
= cp_parser_template_parameter_list (parser
);
27457 /* Look for the `>'. */
27458 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27460 /* Manage template requirements */
27463 tree reqs
= get_shorthand_constraints (current_template_parms
);
27464 if (tree r
= cp_parser_requires_clause_opt (parser
))
27465 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27466 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27469 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27472 /* For the erroneous case of a template with C linkage, we pushed an
27473 implicit C++ linkage scope; exit that scope now. */
27475 pop_lang_context ();
27478 /* Parse a template-declaration, assuming that the `export' (and
27479 `extern') keywords, if present, has already been scanned. MEMBER_P
27480 is as for cp_parser_template_declaration. */
27483 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27485 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27487 cp_lexer_consume_token (parser
->lexer
);
27488 cp_parser_explicit_template_declaration (parser
, member_p
);
27491 else if (flag_concepts
)
27492 return cp_parser_template_introduction (parser
, member_p
);
27497 /* Perform the deferred access checks from a template-parameter-list.
27498 CHECKS is a TREE_LIST of access checks, as returned by
27499 get_deferred_access_checks. */
27502 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27504 ++processing_template_parmlist
;
27505 perform_access_checks (checks
, tf_warning_or_error
);
27506 --processing_template_parmlist
;
27509 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27510 `function-definition' sequence that follows a template header.
27511 If MEMBER_P is true, this declaration appears in a class scope.
27513 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27514 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27517 cp_parser_single_declaration (cp_parser
* parser
,
27518 vec
<deferred_access_check
, va_gc
> *checks
,
27520 bool explicit_specialization_p
,
27523 int declares_class_or_enum
;
27524 tree decl
= NULL_TREE
;
27525 cp_decl_specifier_seq decl_specifiers
;
27526 bool function_definition_p
= false;
27527 cp_token
*decl_spec_token_start
;
27529 /* This function is only used when processing a template
27531 gcc_assert (innermost_scope_kind () == sk_template_parms
27532 || innermost_scope_kind () == sk_template_spec
);
27534 /* Defer access checks until we know what is being declared. */
27535 push_deferring_access_checks (dk_deferred
);
27537 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27539 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27540 cp_parser_decl_specifier_seq (parser
,
27541 CP_PARSER_FLAGS_OPTIONAL
,
27543 &declares_class_or_enum
);
27545 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27547 /* There are no template typedefs. */
27548 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27550 error_at (decl_spec_token_start
->location
,
27551 "template declaration of %<typedef%>");
27552 decl
= error_mark_node
;
27555 /* Gather up the access checks that occurred the
27556 decl-specifier-seq. */
27557 stop_deferring_access_checks ();
27559 /* Check for the declaration of a template class. */
27560 if (declares_class_or_enum
)
27562 if (cp_parser_declares_only_class_p (parser
)
27563 || (declares_class_or_enum
& 2))
27565 // If this is a declaration, but not a definition, associate
27566 // any constraints with the type declaration. Constraints
27567 // are associated with definitions in cp_parser_class_specifier.
27568 if (declares_class_or_enum
== 1)
27569 associate_classtype_constraints (decl_specifiers
.type
);
27571 decl
= shadow_tag (&decl_specifiers
);
27576 friend template <typename T> struct A<T>::B;
27579 A<T>::B will be represented by a TYPENAME_TYPE, and
27580 therefore not recognized by shadow_tag. */
27581 if (friend_p
&& *friend_p
27583 && decl_specifiers
.type
27584 && TYPE_P (decl_specifiers
.type
))
27585 decl
= decl_specifiers
.type
;
27587 if (decl
&& decl
!= error_mark_node
)
27588 decl
= TYPE_NAME (decl
);
27590 decl
= error_mark_node
;
27592 /* Perform access checks for template parameters. */
27593 cp_parser_perform_template_parameter_access_checks (checks
);
27595 /* Give a helpful diagnostic for
27596 template <class T> struct A { } a;
27597 if we aren't already recovering from an error. */
27598 if (!cp_parser_declares_only_class_p (parser
)
27601 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27602 "a class template declaration must not declare "
27604 cp_parser_skip_to_end_of_block_or_statement (parser
);
27610 /* Complain about missing 'typename' or other invalid type names. */
27611 if (!decl_specifiers
.any_type_specifiers_p
27612 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27614 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27615 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27616 the rest of this declaration. */
27617 decl
= error_mark_node
;
27621 /* If it's not a template class, try for a template function. If
27622 the next token is a `;', then this declaration does not declare
27623 anything. But, if there were errors in the decl-specifiers, then
27624 the error might well have come from an attempted class-specifier.
27625 In that case, there's no need to warn about a missing declarator. */
27627 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27628 || decl_specifiers
.type
!= error_mark_node
))
27630 decl
= cp_parser_init_declarator (parser
,
27633 /*function_definition_allowed_p=*/true,
27635 declares_class_or_enum
,
27636 &function_definition_p
,
27639 /* 7.1.1-1 [dcl.stc]
27641 A storage-class-specifier shall not be specified in an explicit
27642 specialization... */
27644 && explicit_specialization_p
27645 && decl_specifiers
.storage_class
!= sc_none
)
27647 error_at (decl_spec_token_start
->location
,
27648 "explicit template specialization cannot have a storage class");
27649 decl
= error_mark_node
;
27652 if (decl
&& VAR_P (decl
))
27653 check_template_variable (decl
);
27656 /* Look for a trailing `;' after the declaration. */
27657 if (!function_definition_p
27658 && (decl
== error_mark_node
27659 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27660 cp_parser_skip_to_end_of_block_or_statement (parser
);
27663 pop_deferring_access_checks ();
27665 /* Clear any current qualification; whatever comes next is the start
27666 of something new. */
27667 parser
->scope
= NULL_TREE
;
27668 parser
->qualifying_scope
= NULL_TREE
;
27669 parser
->object_scope
= NULL_TREE
;
27674 /* Parse a cast-expression that is not the operand of a unary "&". */
27677 cp_parser_simple_cast_expression (cp_parser
*parser
)
27679 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27680 /*cast_p=*/false, /*decltype*/false, NULL
);
27683 /* Parse a functional cast to TYPE. Returns an expression
27684 representing the cast. */
27687 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27689 vec
<tree
, va_gc
> *vec
;
27690 tree expression_list
;
27694 location_t start_loc
= input_location
;
27697 type
= error_mark_node
;
27699 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27701 cp_lexer_set_source_position (parser
->lexer
);
27702 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27703 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27704 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27705 if (TREE_CODE (type
) == TYPE_DECL
)
27706 type
= TREE_TYPE (type
);
27708 cast
= finish_compound_literal (type
, expression_list
,
27709 tf_warning_or_error
, fcl_functional
);
27710 /* Create a location of the form:
27713 with caret == start at the start of the type name,
27714 finishing at the closing brace. */
27715 location_t finish_loc
27716 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27717 location_t combined_loc
= make_location (start_loc
, start_loc
,
27719 cast
.set_location (combined_loc
);
27724 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27726 /*allow_expansion_p=*/true,
27727 /*non_constant_p=*/NULL
);
27729 expression_list
= error_mark_node
;
27732 expression_list
= build_tree_list_vec (vec
);
27733 release_tree_vector (vec
);
27736 cast
= build_functional_cast (type
, expression_list
,
27737 tf_warning_or_error
);
27738 /* [expr.const]/1: In an integral constant expression "only type
27739 conversions to integral or enumeration type can be used". */
27740 if (TREE_CODE (type
) == TYPE_DECL
)
27741 type
= TREE_TYPE (type
);
27742 if (cast
!= error_mark_node
27743 && !cast_valid_in_integral_constant_expression_p (type
)
27744 && cp_parser_non_integral_constant_expression (parser
,
27746 return error_mark_node
;
27748 /* Create a location of the form:
27751 with caret == start at the start of the type name,
27752 finishing at the closing paren. */
27753 location_t finish_loc
27754 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27755 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27756 cast
.set_location (combined_loc
);
27760 /* Save the tokens that make up the body of a member function defined
27761 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27762 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27763 specifiers applied to the declaration. Returns the FUNCTION_DECL
27764 for the member function. */
27767 cp_parser_save_member_function_body (cp_parser
* parser
,
27768 cp_decl_specifier_seq
*decl_specifiers
,
27769 cp_declarator
*declarator
,
27775 bool function_try_block
= false;
27777 /* Create the FUNCTION_DECL. */
27778 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27779 cp_finalize_omp_declare_simd (parser
, fn
);
27780 cp_finalize_oacc_routine (parser
, fn
, true);
27781 /* If something went badly wrong, bail out now. */
27782 if (fn
== error_mark_node
)
27784 /* If there's a function-body, skip it. */
27785 if (cp_parser_token_starts_function_definition_p
27786 (cp_lexer_peek_token (parser
->lexer
)))
27787 cp_parser_skip_to_end_of_block_or_statement (parser
);
27788 return error_mark_node
;
27791 /* Remember it, if there default args to post process. */
27792 cp_parser_save_default_args (parser
, fn
);
27794 /* Save away the tokens that make up the body of the
27796 first
= parser
->lexer
->next_token
;
27798 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27799 cp_lexer_consume_token (parser
->lexer
);
27800 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27801 RID_TRANSACTION_ATOMIC
))
27803 cp_lexer_consume_token (parser
->lexer
);
27804 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27806 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27807 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27808 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27809 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27810 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27812 cp_lexer_consume_token (parser
->lexer
);
27813 cp_lexer_consume_token (parser
->lexer
);
27814 cp_lexer_consume_token (parser
->lexer
);
27815 cp_lexer_consume_token (parser
->lexer
);
27816 cp_lexer_consume_token (parser
->lexer
);
27819 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27820 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27822 cp_lexer_consume_token (parser
->lexer
);
27823 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27828 /* Handle function try blocks. */
27829 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27831 cp_lexer_consume_token (parser
->lexer
);
27832 function_try_block
= true;
27834 /* We can have braced-init-list mem-initializers before the fn body. */
27835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27837 cp_lexer_consume_token (parser
->lexer
);
27838 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27840 /* cache_group will stop after an un-nested { } pair, too. */
27841 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27844 /* variadic mem-inits have ... after the ')'. */
27845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27846 cp_lexer_consume_token (parser
->lexer
);
27849 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27850 /* Handle function try blocks. */
27851 if (function_try_block
)
27852 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27853 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27854 last
= parser
->lexer
->next_token
;
27856 /* Save away the inline definition; we will process it when the
27857 class is complete. */
27858 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27859 DECL_PENDING_INLINE_P (fn
) = 1;
27861 /* We need to know that this was defined in the class, so that
27862 friend templates are handled correctly. */
27863 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27865 /* Add FN to the queue of functions to be parsed later. */
27866 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27871 /* Save the tokens that make up the in-class initializer for a non-static
27872 data member. Returns a DEFAULT_ARG. */
27875 cp_parser_save_nsdmi (cp_parser
* parser
)
27877 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27880 /* Parse a template-argument-list, as well as the trailing ">" (but
27881 not the opening "<"). See cp_parser_template_argument_list for the
27885 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27889 tree saved_qualifying_scope
;
27890 tree saved_object_scope
;
27891 bool saved_greater_than_is_operator_p
;
27895 When parsing a template-id, the first non-nested `>' is taken as
27896 the end of the template-argument-list rather than a greater-than
27898 saved_greater_than_is_operator_p
27899 = parser
->greater_than_is_operator_p
;
27900 parser
->greater_than_is_operator_p
= false;
27901 /* Parsing the argument list may modify SCOPE, so we save it
27903 saved_scope
= parser
->scope
;
27904 saved_qualifying_scope
= parser
->qualifying_scope
;
27905 saved_object_scope
= parser
->object_scope
;
27906 /* We need to evaluate the template arguments, even though this
27907 template-id may be nested within a "sizeof". */
27909 /* Parse the template-argument-list itself. */
27910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27911 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27912 arguments
= NULL_TREE
;
27914 arguments
= cp_parser_template_argument_list (parser
);
27915 /* Look for the `>' that ends the template-argument-list. If we find
27916 a '>>' instead, it's probably just a typo. */
27917 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27919 if (cxx_dialect
!= cxx98
)
27921 /* In C++0x, a `>>' in a template argument list or cast
27922 expression is considered to be two separate `>'
27923 tokens. So, change the current token to a `>', but don't
27924 consume it: it will be consumed later when the outer
27925 template argument list (or cast expression) is parsed.
27926 Note that this replacement of `>' for `>>' is necessary
27927 even if we are parsing tentatively: in the tentative
27928 case, after calling
27929 cp_parser_enclosed_template_argument_list we will always
27930 throw away all of the template arguments and the first
27931 closing `>', either because the template argument list
27932 was erroneous or because we are replacing those tokens
27933 with a CPP_TEMPLATE_ID token. The second `>' (which will
27934 not have been thrown away) is needed either to close an
27935 outer template argument list or to complete a new-style
27937 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27938 token
->type
= CPP_GREATER
;
27940 else if (!saved_greater_than_is_operator_p
)
27942 /* If we're in a nested template argument list, the '>>' has
27943 to be a typo for '> >'. We emit the error message, but we
27944 continue parsing and we push a '>' as next token, so that
27945 the argument list will be parsed correctly. Note that the
27946 global source location is still on the token before the
27947 '>>', so we need to say explicitly where we want it. */
27948 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27949 gcc_rich_location
richloc (token
->location
);
27950 richloc
.add_fixit_replace ("> >");
27951 error_at (&richloc
, "%<>>%> should be %<> >%> "
27952 "within a nested template argument list");
27954 token
->type
= CPP_GREATER
;
27958 /* If this is not a nested template argument list, the '>>'
27959 is a typo for '>'. Emit an error message and continue.
27960 Same deal about the token location, but here we can get it
27961 right by consuming the '>>' before issuing the diagnostic. */
27962 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27963 error_at (token
->location
,
27964 "spurious %<>>%>, use %<>%> to terminate "
27965 "a template argument list");
27969 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27970 /* The `>' token might be a greater-than operator again now. */
27971 parser
->greater_than_is_operator_p
27972 = saved_greater_than_is_operator_p
;
27973 /* Restore the SAVED_SCOPE. */
27974 parser
->scope
= saved_scope
;
27975 parser
->qualifying_scope
= saved_qualifying_scope
;
27976 parser
->object_scope
= saved_object_scope
;
27981 /* MEMBER_FUNCTION is a member function, or a friend. If default
27982 arguments, or the body of the function have not yet been parsed,
27986 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27988 timevar_push (TV_PARSE_INMETH
);
27989 /* If this member is a template, get the underlying
27991 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27992 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27994 /* There should not be any class definitions in progress at this
27995 point; the bodies of members are only parsed outside of all class
27997 gcc_assert (parser
->num_classes_being_defined
== 0);
27998 /* While we're parsing the member functions we might encounter more
27999 classes. We want to handle them right away, but we don't want
28000 them getting mixed up with functions that are currently in the
28002 push_unparsed_function_queues (parser
);
28004 /* Make sure that any template parameters are in scope. */
28005 maybe_begin_member_template_processing (member_function
);
28007 /* If the body of the function has not yet been parsed, parse it
28009 if (DECL_PENDING_INLINE_P (member_function
))
28011 tree function_scope
;
28012 cp_token_cache
*tokens
;
28014 /* The function is no longer pending; we are processing it. */
28015 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28016 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28017 DECL_PENDING_INLINE_P (member_function
) = 0;
28019 /* If this is a local class, enter the scope of the containing
28021 function_scope
= current_function_decl
;
28022 if (function_scope
)
28023 push_function_context ();
28025 /* Push the body of the function onto the lexer stack. */
28026 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28028 /* Let the front end know that we going to be defining this
28030 start_preparsed_function (member_function
, NULL_TREE
,
28031 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28033 /* Don't do access checking if it is a templated function. */
28034 if (processing_template_decl
)
28035 push_deferring_access_checks (dk_no_check
);
28037 /* #pragma omp declare reduction needs special parsing. */
28038 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28040 parser
->lexer
->in_pragma
= true;
28041 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28042 finish_function (/*inline_p=*/true);
28043 cp_check_omp_declare_reduction (member_function
);
28046 /* Now, parse the body of the function. */
28047 cp_parser_function_definition_after_declarator (parser
,
28048 /*inline_p=*/true);
28050 if (processing_template_decl
)
28051 pop_deferring_access_checks ();
28053 /* Leave the scope of the containing function. */
28054 if (function_scope
)
28055 pop_function_context ();
28056 cp_parser_pop_lexer (parser
);
28059 /* Remove any template parameters from the symbol table. */
28060 maybe_end_member_template_processing ();
28062 /* Restore the queue. */
28063 pop_unparsed_function_queues (parser
);
28064 timevar_pop (TV_PARSE_INMETH
);
28067 /* If DECL contains any default args, remember it on the unparsed
28068 functions queue. */
28071 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28075 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28077 probe
= TREE_CHAIN (probe
))
28078 if (TREE_PURPOSE (probe
))
28080 cp_default_arg_entry entry
= {current_class_type
, decl
};
28081 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28086 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28087 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28088 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28089 from the parameter-type-list. */
28092 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28093 tree default_arg
, tree parmtype
)
28095 cp_token_cache
*tokens
;
28099 if (default_arg
== error_mark_node
)
28100 return error_mark_node
;
28102 /* Push the saved tokens for the default argument onto the parser's
28104 tokens
= DEFARG_TOKENS (default_arg
);
28105 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28107 start_lambda_scope (decl
);
28109 /* Parse the default argument. */
28110 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28111 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28112 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28114 finish_lambda_scope ();
28116 if (parsed_arg
== error_mark_node
)
28117 cp_parser_skip_to_end_of_statement (parser
);
28119 if (!processing_template_decl
)
28121 /* In a non-template class, check conversions now. In a template,
28122 we'll wait and instantiate these as needed. */
28123 if (TREE_CODE (decl
) == PARM_DECL
)
28124 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28125 tf_warning_or_error
);
28126 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28127 parsed_arg
= error_mark_node
;
28129 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28132 /* If the token stream has not been completely used up, then
28133 there was extra junk after the end of the default
28135 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28137 if (TREE_CODE (decl
) == PARM_DECL
)
28138 cp_parser_error (parser
, "expected %<,%>");
28140 cp_parser_error (parser
, "expected %<;%>");
28143 /* Revert to the main lexer. */
28144 cp_parser_pop_lexer (parser
);
28149 /* FIELD is a non-static data member with an initializer which we saved for
28150 later; parse it now. */
28153 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28157 maybe_begin_member_template_processing (field
);
28159 push_unparsed_function_queues (parser
);
28160 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28161 DECL_INITIAL (field
),
28163 pop_unparsed_function_queues (parser
);
28165 maybe_end_member_template_processing ();
28167 DECL_INITIAL (field
) = def
;
28170 /* FN is a FUNCTION_DECL which may contains a parameter with an
28171 unparsed DEFAULT_ARG. Parse the default args now. This function
28172 assumes that the current scope is the scope in which the default
28173 argument should be processed. */
28176 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28178 bool saved_local_variables_forbidden_p
;
28179 tree parm
, parmdecl
;
28181 /* While we're parsing the default args, we might (due to the
28182 statement expression extension) encounter more classes. We want
28183 to handle them right away, but we don't want them getting mixed
28184 up with default args that are currently in the queue. */
28185 push_unparsed_function_queues (parser
);
28187 /* Local variable names (and the `this' keyword) may not appear
28188 in a default argument. */
28189 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28190 parser
->local_variables_forbidden_p
= true;
28192 push_defarg_context (fn
);
28194 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28195 parmdecl
= DECL_ARGUMENTS (fn
);
28196 parm
&& parm
!= void_list_node
;
28197 parm
= TREE_CHAIN (parm
),
28198 parmdecl
= DECL_CHAIN (parmdecl
))
28200 tree default_arg
= TREE_PURPOSE (parm
);
28202 vec
<tree
, va_gc
> *insts
;
28209 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28210 /* This can happen for a friend declaration for a function
28211 already declared with default arguments. */
28215 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28217 TREE_VALUE (parm
));
28218 TREE_PURPOSE (parm
) = parsed_arg
;
28220 /* Update any instantiations we've already created. */
28221 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28222 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28223 TREE_PURPOSE (copy
) = parsed_arg
;
28226 pop_defarg_context ();
28228 /* Make sure no default arg is missing. */
28229 check_default_args (fn
);
28231 /* Restore the state of local_variables_forbidden_p. */
28232 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28234 /* Restore the queue. */
28235 pop_unparsed_function_queues (parser
);
28238 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28240 sizeof ... ( identifier )
28242 where the 'sizeof' token has already been consumed. */
28245 cp_parser_sizeof_pack (cp_parser
*parser
)
28247 /* Consume the `...'. */
28248 cp_lexer_consume_token (parser
->lexer
);
28249 maybe_warn_variadic_templates ();
28251 matching_parens parens
;
28252 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28254 parens
.consume_open (parser
);
28256 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28257 "%<sizeof...%> argument must be surrounded by parentheses");
28259 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28260 tree name
= cp_parser_identifier (parser
);
28261 if (name
== error_mark_node
)
28262 return error_mark_node
;
28263 /* The name is not qualified. */
28264 parser
->scope
= NULL_TREE
;
28265 parser
->qualifying_scope
= NULL_TREE
;
28266 parser
->object_scope
= NULL_TREE
;
28267 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28268 if (expr
== error_mark_node
)
28269 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28271 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28272 expr
= TREE_TYPE (expr
);
28273 else if (TREE_CODE (expr
) == CONST_DECL
)
28274 expr
= DECL_INITIAL (expr
);
28275 expr
= make_pack_expansion (expr
);
28276 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28279 parens
.require_close (parser
);
28284 /* Parse the operand of `sizeof' (or a similar operator). Returns
28285 either a TYPE or an expression, depending on the form of the
28286 input. The KEYWORD indicates which kind of expression we have
28290 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28292 tree expr
= NULL_TREE
;
28293 const char *saved_message
;
28295 bool saved_integral_constant_expression_p
;
28296 bool saved_non_integral_constant_expression_p
;
28298 /* If it's a `...', then we are computing the length of a parameter
28300 if (keyword
== RID_SIZEOF
28301 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28302 return cp_parser_sizeof_pack (parser
);
28304 /* Types cannot be defined in a `sizeof' expression. Save away the
28306 saved_message
= parser
->type_definition_forbidden_message
;
28307 /* And create the new one. */
28308 tmp
= concat ("types may not be defined in %<",
28309 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28310 "%> expressions", NULL
);
28311 parser
->type_definition_forbidden_message
= tmp
;
28313 /* The restrictions on constant-expressions do not apply inside
28314 sizeof expressions. */
28315 saved_integral_constant_expression_p
28316 = parser
->integral_constant_expression_p
;
28317 saved_non_integral_constant_expression_p
28318 = parser
->non_integral_constant_expression_p
;
28319 parser
->integral_constant_expression_p
= false;
28321 /* Do not actually evaluate the expression. */
28322 ++cp_unevaluated_operand
;
28323 ++c_inhibit_evaluation_warnings
;
28324 /* If it's a `(', then we might be looking at the type-id
28326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28328 tree type
= NULL_TREE
;
28330 /* We can't be sure yet whether we're looking at a type-id or an
28332 cp_parser_parse_tentatively (parser
);
28334 matching_parens parens
;
28335 parens
.consume_open (parser
);
28337 /* Note: as a GNU Extension, compound literals are considered
28338 postfix-expressions as they are in C99, so they are valid
28339 arguments to sizeof. See comment in cp_parser_cast_expression
28341 if (cp_parser_compound_literal_p (parser
))
28342 cp_parser_simulate_error (parser
);
28345 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28346 parser
->in_type_id_in_expr_p
= true;
28347 /* Look for the type-id. */
28348 type
= cp_parser_type_id (parser
);
28349 /* Look for the closing `)'. */
28350 parens
.require_close (parser
);
28351 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28354 /* If all went well, then we're done. */
28355 if (cp_parser_parse_definitely (parser
))
28359 /* If the type-id production did not work out, then we must be
28360 looking at the unary-expression production. */
28362 expr
= cp_parser_unary_expression (parser
);
28364 /* Go back to evaluating expressions. */
28365 --cp_unevaluated_operand
;
28366 --c_inhibit_evaluation_warnings
;
28368 /* Free the message we created. */
28370 /* And restore the old one. */
28371 parser
->type_definition_forbidden_message
= saved_message
;
28372 parser
->integral_constant_expression_p
28373 = saved_integral_constant_expression_p
;
28374 parser
->non_integral_constant_expression_p
28375 = saved_non_integral_constant_expression_p
;
28380 /* If the current declaration has no declarator, return true. */
28383 cp_parser_declares_only_class_p (cp_parser
*parser
)
28385 /* If the next token is a `;' or a `,' then there is no
28387 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28388 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28391 /* Update the DECL_SPECS to reflect the storage class indicated by
28395 cp_parser_set_storage_class (cp_parser
*parser
,
28396 cp_decl_specifier_seq
*decl_specs
,
28400 cp_storage_class storage_class
;
28402 if (parser
->in_unbraced_linkage_specification_p
)
28404 error_at (token
->location
, "invalid use of %qD in linkage specification",
28405 ridpointers
[keyword
]);
28408 else if (decl_specs
->storage_class
!= sc_none
)
28410 decl_specs
->conflicting_specifiers_p
= true;
28414 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28415 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28416 && decl_specs
->gnu_thread_keyword_p
)
28418 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28419 "%<__thread%> before %qD", ridpointers
[keyword
]);
28425 storage_class
= sc_auto
;
28428 storage_class
= sc_register
;
28431 storage_class
= sc_static
;
28434 storage_class
= sc_extern
;
28437 storage_class
= sc_mutable
;
28440 gcc_unreachable ();
28442 decl_specs
->storage_class
= storage_class
;
28443 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28445 /* A storage class specifier cannot be applied alongside a typedef
28446 specifier. If there is a typedef specifier present then set
28447 conflicting_specifiers_p which will trigger an error later
28448 on in grokdeclarator. */
28449 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28450 decl_specs
->conflicting_specifiers_p
= true;
28453 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28454 is true, the type is a class or enum definition. */
28457 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28460 bool type_definition_p
)
28462 decl_specs
->any_specifiers_p
= true;
28464 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28465 (with, for example, in "typedef int wchar_t;") we remember that
28466 this is what happened. In system headers, we ignore these
28467 declarations so that G++ can work with system headers that are not
28469 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28470 && !type_definition_p
28471 && (type_spec
== boolean_type_node
28472 || type_spec
== char16_type_node
28473 || type_spec
== char32_type_node
28474 || type_spec
== wchar_type_node
)
28475 && (decl_specs
->type
28476 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28477 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28478 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28479 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28481 decl_specs
->redefined_builtin_type
= type_spec
;
28482 set_and_check_decl_spec_loc (decl_specs
,
28483 ds_redefined_builtin_type_spec
,
28485 if (!decl_specs
->type
)
28487 decl_specs
->type
= type_spec
;
28488 decl_specs
->type_definition_p
= false;
28489 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28492 else if (decl_specs
->type
)
28493 decl_specs
->multiple_types_p
= true;
28496 decl_specs
->type
= type_spec
;
28497 decl_specs
->type_definition_p
= type_definition_p
;
28498 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28499 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28503 /* True iff TOKEN is the GNU keyword __thread. */
28506 token_is__thread (cp_token
*token
)
28508 gcc_assert (token
->keyword
== RID_THREAD
);
28509 return id_equal (token
->u
.value
, "__thread");
28512 /* Set the location for a declarator specifier and check if it is
28515 DECL_SPECS is the sequence of declarator specifiers onto which to
28518 DS is the single declarator specifier to set which location is to
28519 be set onto the existing sequence of declarators.
28521 LOCATION is the location for the declarator specifier to
28525 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28526 cp_decl_spec ds
, cp_token
*token
)
28528 gcc_assert (ds
< ds_last
);
28530 if (decl_specs
== NULL
)
28533 location_t location
= token
->location
;
28535 if (decl_specs
->locations
[ds
] == 0)
28537 decl_specs
->locations
[ds
] = location
;
28538 if (ds
== ds_thread
)
28539 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28545 if (decl_specs
->locations
[ds_long_long
] != 0)
28546 error_at (location
,
28547 "%<long long long%> is too long for GCC");
28550 decl_specs
->locations
[ds_long_long
] = location
;
28551 pedwarn_cxx98 (location
,
28553 "ISO C++ 1998 does not support %<long long%>");
28556 else if (ds
== ds_thread
)
28558 bool gnu
= token_is__thread (token
);
28559 gcc_rich_location
richloc (location
);
28560 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28562 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28563 error_at (&richloc
,
28564 "both %<__thread%> and %<thread_local%> specified");
28568 richloc
.add_fixit_remove ();
28569 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28574 static const char *const decl_spec_names
[] = {
28591 gcc_rich_location
richloc (location
);
28592 richloc
.add_fixit_remove ();
28593 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28598 /* Return true iff the declarator specifier DS is present in the
28599 sequence of declarator specifiers DECL_SPECS. */
28602 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28605 gcc_assert (ds
< ds_last
);
28607 if (decl_specs
== NULL
)
28610 return decl_specs
->locations
[ds
] != 0;
28613 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28614 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28617 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28619 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28622 /* Issue an error message indicating that TOKEN_DESC was expected.
28623 If KEYWORD is true, it indicated this function is called by
28624 cp_parser_require_keword and the required token can only be
28625 a indicated keyword.
28627 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28628 within any error as the location of an "opening" token matching
28629 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28630 RT_CLOSE_PAREN). */
28633 cp_parser_required_error (cp_parser
*parser
,
28634 required_token token_desc
,
28636 location_t matching_location
)
28638 if (cp_parser_simulate_error (parser
))
28641 const char *gmsgid
= NULL
;
28642 switch (token_desc
)
28645 gmsgid
= G_("expected %<new%>");
28648 gmsgid
= G_("expected %<delete%>");
28651 gmsgid
= G_("expected %<return%>");
28654 gmsgid
= G_("expected %<while%>");
28657 gmsgid
= G_("expected %<extern%>");
28659 case RT_STATIC_ASSERT
:
28660 gmsgid
= G_("expected %<static_assert%>");
28663 gmsgid
= G_("expected %<decltype%>");
28666 gmsgid
= G_("expected %<operator%>");
28669 gmsgid
= G_("expected %<class%>");
28672 gmsgid
= G_("expected %<template%>");
28675 gmsgid
= G_("expected %<namespace%>");
28678 gmsgid
= G_("expected %<using%>");
28681 gmsgid
= G_("expected %<asm%>");
28684 gmsgid
= G_("expected %<try%>");
28687 gmsgid
= G_("expected %<catch%>");
28690 gmsgid
= G_("expected %<throw%>");
28693 gmsgid
= G_("expected %<__label__%>");
28696 gmsgid
= G_("expected %<@try%>");
28698 case RT_AT_SYNCHRONIZED
:
28699 gmsgid
= G_("expected %<@synchronized%>");
28702 gmsgid
= G_("expected %<@throw%>");
28704 case RT_TRANSACTION_ATOMIC
:
28705 gmsgid
= G_("expected %<__transaction_atomic%>");
28707 case RT_TRANSACTION_RELAXED
:
28708 gmsgid
= G_("expected %<__transaction_relaxed%>");
28714 if (!gmsgid
&& !keyword
)
28716 switch (token_desc
)
28719 gmsgid
= G_("expected %<;%>");
28721 case RT_OPEN_PAREN
:
28722 gmsgid
= G_("expected %<(%>");
28724 case RT_CLOSE_BRACE
:
28725 gmsgid
= G_("expected %<}%>");
28727 case RT_OPEN_BRACE
:
28728 gmsgid
= G_("expected %<{%>");
28730 case RT_CLOSE_SQUARE
:
28731 gmsgid
= G_("expected %<]%>");
28733 case RT_OPEN_SQUARE
:
28734 gmsgid
= G_("expected %<[%>");
28737 gmsgid
= G_("expected %<,%>");
28740 gmsgid
= G_("expected %<::%>");
28743 gmsgid
= G_("expected %<<%>");
28746 gmsgid
= G_("expected %<>%>");
28749 gmsgid
= G_("expected %<=%>");
28752 gmsgid
= G_("expected %<...%>");
28755 gmsgid
= G_("expected %<*%>");
28758 gmsgid
= G_("expected %<~%>");
28761 gmsgid
= G_("expected %<:%>");
28763 case RT_COLON_SCOPE
:
28764 gmsgid
= G_("expected %<:%> or %<::%>");
28766 case RT_CLOSE_PAREN
:
28767 gmsgid
= G_("expected %<)%>");
28769 case RT_COMMA_CLOSE_PAREN
:
28770 gmsgid
= G_("expected %<,%> or %<)%>");
28772 case RT_PRAGMA_EOL
:
28773 gmsgid
= G_("expected end of line");
28776 gmsgid
= G_("expected identifier");
28779 gmsgid
= G_("expected selection-statement");
28782 gmsgid
= G_("expected iteration-statement");
28785 gmsgid
= G_("expected jump-statement");
28788 gmsgid
= G_("expected class-key");
28790 case RT_CLASS_TYPENAME_TEMPLATE
:
28791 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28794 gcc_unreachable ();
28799 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28803 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28804 issue an error message indicating that TOKEN_DESC was expected.
28806 Returns the token consumed, if the token had the appropriate type.
28807 Otherwise, returns NULL.
28809 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28810 within any error as the location of an "opening" token matching
28811 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28812 RT_CLOSE_PAREN). */
28815 cp_parser_require (cp_parser
* parser
,
28816 enum cpp_ttype type
,
28817 required_token token_desc
,
28818 location_t matching_location
)
28820 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28821 return cp_lexer_consume_token (parser
->lexer
);
28824 /* Output the MESSAGE -- unless we're parsing tentatively. */
28825 if (!cp_parser_simulate_error (parser
))
28826 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28827 matching_location
);
28832 /* An error message is produced if the next token is not '>'.
28833 All further tokens are skipped until the desired token is
28834 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28837 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28839 /* Current level of '< ... >'. */
28840 unsigned level
= 0;
28841 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28842 unsigned nesting_depth
= 0;
28844 /* Are we ready, yet? If not, issue error message. */
28845 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28848 /* Skip tokens until the desired token is found. */
28851 /* Peek at the next token. */
28852 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28855 if (!nesting_depth
)
28860 if (cxx_dialect
== cxx98
)
28861 /* C++0x views the `>>' operator as two `>' tokens, but
28864 else if (!nesting_depth
&& level
-- == 0)
28866 /* We've hit a `>>' where the first `>' closes the
28867 template argument list, and the second `>' is
28868 spurious. Just consume the `>>' and stop; we've
28869 already produced at least one error. */
28870 cp_lexer_consume_token (parser
->lexer
);
28873 /* Fall through for C++0x, so we handle the second `>' in
28875 gcc_fallthrough ();
28878 if (!nesting_depth
&& level
-- == 0)
28880 /* We've reached the token we want, consume it and stop. */
28881 cp_lexer_consume_token (parser
->lexer
);
28886 case CPP_OPEN_PAREN
:
28887 case CPP_OPEN_SQUARE
:
28891 case CPP_CLOSE_PAREN
:
28892 case CPP_CLOSE_SQUARE
:
28893 if (nesting_depth
-- == 0)
28898 case CPP_PRAGMA_EOL
:
28899 case CPP_SEMICOLON
:
28900 case CPP_OPEN_BRACE
:
28901 case CPP_CLOSE_BRACE
:
28902 /* The '>' was probably forgotten, don't look further. */
28909 /* Consume this token. */
28910 cp_lexer_consume_token (parser
->lexer
);
28914 /* If the next token is the indicated keyword, consume it. Otherwise,
28915 issue an error message indicating that TOKEN_DESC was expected.
28917 Returns the token consumed, if the token had the appropriate type.
28918 Otherwise, returns NULL. */
28921 cp_parser_require_keyword (cp_parser
* parser
,
28923 required_token token_desc
)
28925 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28927 if (token
&& token
->keyword
!= keyword
)
28929 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28937 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28938 function-definition. */
28941 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28943 return (/* An ordinary function-body begins with an `{'. */
28944 token
->type
== CPP_OPEN_BRACE
28945 /* A ctor-initializer begins with a `:'. */
28946 || token
->type
== CPP_COLON
28947 /* A function-try-block begins with `try'. */
28948 || token
->keyword
== RID_TRY
28949 /* A function-transaction-block begins with `__transaction_atomic'
28950 or `__transaction_relaxed'. */
28951 || token
->keyword
== RID_TRANSACTION_ATOMIC
28952 || token
->keyword
== RID_TRANSACTION_RELAXED
28953 /* The named return value extension begins with `return'. */
28954 || token
->keyword
== RID_RETURN
);
28957 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28961 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28965 token
= cp_lexer_peek_token (parser
->lexer
);
28966 return (token
->type
== CPP_OPEN_BRACE
28967 || (token
->type
== CPP_COLON
28968 && !parser
->colon_doesnt_start_class_def_p
));
28971 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28972 C++0x) ending a template-argument. */
28975 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28979 token
= cp_lexer_peek_token (parser
->lexer
);
28980 return (token
->type
== CPP_COMMA
28981 || token
->type
== CPP_GREATER
28982 || token
->type
== CPP_ELLIPSIS
28983 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28986 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28987 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28990 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28995 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28996 if (token
->type
== CPP_LESS
)
28998 /* Check for the sequence `<::' in the original code. It would be lexed as
28999 `[:', where `[' is a digraph, and there is no whitespace before
29001 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29004 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29005 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29011 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29012 or none_type otherwise. */
29014 static enum tag_types
29015 cp_parser_token_is_class_key (cp_token
* token
)
29017 switch (token
->keyword
)
29022 return record_type
;
29031 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29032 or none_type otherwise or if the token is null. */
29034 static enum tag_types
29035 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29040 switch (token
->keyword
)
29045 return typename_type
;
29052 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29055 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29057 if (type
== error_mark_node
)
29059 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29061 if (permerror (input_location
, "%qs tag used in naming %q#T",
29062 class_key
== union_type
? "union"
29063 : class_key
== record_type
? "struct" : "class",
29065 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29066 "%q#T was previously declared here", type
);
29070 /* Issue an error message if DECL is redeclared with different
29071 access than its original declaration [class.access.spec/3].
29072 This applies to nested classes, nested class templates and
29073 enumerations [class.mem/1]. */
29076 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29079 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29080 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29083 if ((TREE_PRIVATE (decl
)
29084 != (current_access_specifier
== access_private_node
))
29085 || (TREE_PROTECTED (decl
)
29086 != (current_access_specifier
== access_protected_node
)))
29087 error_at (location
, "%qD redeclared with different access", decl
);
29090 /* Look for the `template' keyword, as a syntactic disambiguator.
29091 Return TRUE iff it is present, in which case it will be
29095 cp_parser_optional_template_keyword (cp_parser
*parser
)
29097 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29099 /* In C++98 the `template' keyword can only be used within templates;
29100 outside templates the parser can always figure out what is a
29101 template and what is not. In C++11, per the resolution of DR 468,
29102 `template' is allowed in cases where it is not strictly necessary. */
29103 if (!processing_template_decl
29104 && pedantic
&& cxx_dialect
== cxx98
)
29106 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29107 pedwarn (token
->location
, OPT_Wpedantic
,
29108 "in C++98 %<template%> (as a disambiguator) is only "
29109 "allowed within templates");
29110 /* If this part of the token stream is rescanned, the same
29111 error message would be generated. So, we purge the token
29112 from the stream. */
29113 cp_lexer_purge_token (parser
->lexer
);
29118 /* Consume the `template' keyword. */
29119 cp_lexer_consume_token (parser
->lexer
);
29126 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29127 set PARSER->SCOPE, and perform other related actions. */
29130 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29132 struct tree_check
*check_value
;
29134 /* Get the stored value. */
29135 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29136 /* Set the scope from the stored value. */
29137 parser
->scope
= saved_checks_value (check_value
);
29138 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29139 parser
->object_scope
= NULL_TREE
;
29142 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29143 encounter the end of a block before what we were looking for. */
29146 cp_parser_cache_group (cp_parser
*parser
,
29147 enum cpp_ttype end
,
29152 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29154 /* Abort a parenthesized expression if we encounter a semicolon. */
29155 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29156 && token
->type
== CPP_SEMICOLON
)
29158 /* If we've reached the end of the file, stop. */
29159 if (token
->type
== CPP_EOF
29160 || (end
!= CPP_PRAGMA_EOL
29161 && token
->type
== CPP_PRAGMA_EOL
))
29163 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29164 /* We've hit the end of an enclosing block, so there's been some
29165 kind of syntax error. */
29168 /* Consume the token. */
29169 cp_lexer_consume_token (parser
->lexer
);
29170 /* See if it starts a new group. */
29171 if (token
->type
== CPP_OPEN_BRACE
)
29173 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29174 /* In theory this should probably check end == '}', but
29175 cp_parser_save_member_function_body needs it to exit
29176 after either '}' or ')' when called with ')'. */
29180 else if (token
->type
== CPP_OPEN_PAREN
)
29182 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29183 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29186 else if (token
->type
== CPP_PRAGMA
)
29187 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29188 else if (token
->type
== end
)
29193 /* Like above, for caching a default argument or NSDMI. Both of these are
29194 terminated by a non-nested comma, but it can be unclear whether or not a
29195 comma is nested in a template argument list unless we do more parsing.
29196 In order to handle this ambiguity, when we encounter a ',' after a '<'
29197 we try to parse what follows as a parameter-declaration-list (in the
29198 case of a default argument) or a member-declarator (in the case of an
29199 NSDMI). If that succeeds, then we stop caching. */
29202 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29204 unsigned depth
= 0;
29205 int maybe_template_id
= 0;
29206 cp_token
*first_token
;
29208 tree default_argument
;
29210 /* Add tokens until we have processed the entire default
29211 argument. We add the range [first_token, token). */
29212 first_token
= cp_lexer_peek_token (parser
->lexer
);
29213 if (first_token
->type
== CPP_OPEN_BRACE
)
29215 /* For list-initialization, this is straightforward. */
29216 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29217 token
= cp_lexer_peek_token (parser
->lexer
);
29223 /* Peek at the next token. */
29224 token
= cp_lexer_peek_token (parser
->lexer
);
29225 /* What we do depends on what token we have. */
29226 switch (token
->type
)
29228 /* In valid code, a default argument must be
29229 immediately followed by a `,' `)', or `...'. */
29231 if (depth
== 0 && maybe_template_id
)
29233 /* If we've seen a '<', we might be in a
29234 template-argument-list. Until Core issue 325 is
29235 resolved, we don't know how this situation ought
29236 to be handled, so try to DTRT. We check whether
29237 what comes after the comma is a valid parameter
29238 declaration list. If it is, then the comma ends
29239 the default argument; otherwise the default
29240 argument continues. */
29241 bool error
= false;
29244 /* Set ITALP so cp_parser_parameter_declaration_list
29245 doesn't decide to commit to this parse. */
29246 bool saved_italp
= parser
->in_template_argument_list_p
;
29247 parser
->in_template_argument_list_p
= true;
29249 cp_parser_parse_tentatively (parser
);
29253 /* Parse declarators until we reach a non-comma or
29254 somthing that cannot be an initializer.
29255 Just checking whether we're looking at a single
29256 declarator is insufficient. Consider:
29257 int var = tuple<T,U>::x;
29258 The template parameter 'U' looks exactly like a
29262 int ctor_dtor_or_conv_p
;
29263 cp_lexer_consume_token (parser
->lexer
);
29264 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29265 &ctor_dtor_or_conv_p
,
29266 /*parenthesized_p=*/NULL
,
29268 /*friend_p=*/false);
29269 peek
= cp_lexer_peek_token (parser
->lexer
);
29270 if (cp_parser_error_occurred (parser
))
29273 while (peek
->type
== CPP_COMMA
);
29274 /* If we met an '=' or ';' then the original comma
29275 was the end of the NSDMI. Otherwise assume
29276 we're still in the NSDMI. */
29277 error
= (peek
->type
!= CPP_EQ
29278 && peek
->type
!= CPP_SEMICOLON
);
29282 cp_lexer_consume_token (parser
->lexer
);
29283 begin_scope (sk_function_parms
, NULL_TREE
);
29284 if (cp_parser_parameter_declaration_list (parser
)
29285 == error_mark_node
)
29287 pop_bindings_and_leave_scope ();
29289 if (!cp_parser_error_occurred (parser
) && !error
)
29291 cp_parser_abort_tentative_parse (parser
);
29293 parser
->in_template_argument_list_p
= saved_italp
;
29297 case CPP_CLOSE_PAREN
:
29299 /* If we run into a non-nested `;', `}', or `]',
29300 then the code is invalid -- but the default
29301 argument is certainly over. */
29302 case CPP_SEMICOLON
:
29303 case CPP_CLOSE_BRACE
:
29304 case CPP_CLOSE_SQUARE
:
29306 /* Handle correctly int n = sizeof ... ( p ); */
29307 && token
->type
!= CPP_ELLIPSIS
)
29309 /* Update DEPTH, if necessary. */
29310 else if (token
->type
== CPP_CLOSE_PAREN
29311 || token
->type
== CPP_CLOSE_BRACE
29312 || token
->type
== CPP_CLOSE_SQUARE
)
29316 case CPP_OPEN_PAREN
:
29317 case CPP_OPEN_SQUARE
:
29318 case CPP_OPEN_BRACE
:
29324 /* This might be the comparison operator, or it might
29325 start a template argument list. */
29326 ++maybe_template_id
;
29330 if (cxx_dialect
== cxx98
)
29332 /* Fall through for C++0x, which treats the `>>'
29333 operator like two `>' tokens in certain
29335 gcc_fallthrough ();
29340 /* This might be an operator, or it might close a
29341 template argument list. But if a previous '<'
29342 started a template argument list, this will have
29343 closed it, so we can't be in one anymore. */
29344 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29345 if (maybe_template_id
< 0)
29346 maybe_template_id
= 0;
29350 /* If we run out of tokens, issue an error message. */
29352 case CPP_PRAGMA_EOL
:
29353 error_at (token
->location
, "file ends in default argument");
29354 return error_mark_node
;
29358 /* In these cases, we should look for template-ids.
29359 For example, if the default argument is
29360 `X<int, double>()', we need to do name lookup to
29361 figure out whether or not `X' is a template; if
29362 so, the `,' does not end the default argument.
29364 That is not yet done. */
29371 /* If we've reached the end, stop. */
29375 /* Add the token to the token block. */
29376 token
= cp_lexer_consume_token (parser
->lexer
);
29379 /* Create a DEFAULT_ARG to represent the unparsed default
29381 default_argument
= make_node (DEFAULT_ARG
);
29382 DEFARG_TOKENS (default_argument
)
29383 = cp_token_cache_new (first_token
, token
);
29384 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29386 return default_argument
;
29389 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29392 defarg_location (tree default_argument
)
29394 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29395 location_t start
= tokens
->first
->location
;
29396 location_t end
= tokens
->last
->location
;
29397 return make_location (start
, start
, end
);
29400 /* Begin parsing tentatively. We always save tokens while parsing
29401 tentatively so that if the tentative parsing fails we can restore the
29405 cp_parser_parse_tentatively (cp_parser
* parser
)
29407 /* Enter a new parsing context. */
29408 parser
->context
= cp_parser_context_new (parser
->context
);
29409 /* Begin saving tokens. */
29410 cp_lexer_save_tokens (parser
->lexer
);
29411 /* In order to avoid repetitive access control error messages,
29412 access checks are queued up until we are no longer parsing
29414 push_deferring_access_checks (dk_deferred
);
29417 /* Commit to the currently active tentative parse. */
29420 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29422 cp_parser_context
*context
;
29425 /* Mark all of the levels as committed. */
29426 lexer
= parser
->lexer
;
29427 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29429 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29431 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29432 while (!cp_lexer_saving_tokens (lexer
))
29433 lexer
= lexer
->next
;
29434 cp_lexer_commit_tokens (lexer
);
29438 /* Commit to the topmost currently active tentative parse.
29440 Note that this function shouldn't be called when there are
29441 irreversible side-effects while in a tentative state. For
29442 example, we shouldn't create a permanent entry in the symbol
29443 table, or issue an error message that might not apply if the
29444 tentative parse is aborted. */
29447 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29449 cp_parser_context
*context
= parser
->context
;
29450 cp_lexer
*lexer
= parser
->lexer
;
29454 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29456 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29458 while (!cp_lexer_saving_tokens (lexer
))
29459 lexer
= lexer
->next
;
29460 cp_lexer_commit_tokens (lexer
);
29464 /* Abort the currently active tentative parse. All consumed tokens
29465 will be rolled back, and no diagnostics will be issued. */
29468 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29470 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29471 || errorcount
> 0);
29472 cp_parser_simulate_error (parser
);
29473 /* Now, pretend that we want to see if the construct was
29474 successfully parsed. */
29475 cp_parser_parse_definitely (parser
);
29478 /* Stop parsing tentatively. If a parse error has occurred, restore the
29479 token stream. Otherwise, commit to the tokens we have consumed.
29480 Returns true if no error occurred; false otherwise. */
29483 cp_parser_parse_definitely (cp_parser
* parser
)
29485 bool error_occurred
;
29486 cp_parser_context
*context
;
29488 /* Remember whether or not an error occurred, since we are about to
29489 destroy that information. */
29490 error_occurred
= cp_parser_error_occurred (parser
);
29491 /* Remove the topmost context from the stack. */
29492 context
= parser
->context
;
29493 parser
->context
= context
->next
;
29494 /* If no parse errors occurred, commit to the tentative parse. */
29495 if (!error_occurred
)
29497 /* Commit to the tokens read tentatively, unless that was
29499 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29500 cp_lexer_commit_tokens (parser
->lexer
);
29502 pop_to_parent_deferring_access_checks ();
29504 /* Otherwise, if errors occurred, roll back our state so that things
29505 are just as they were before we began the tentative parse. */
29508 cp_lexer_rollback_tokens (parser
->lexer
);
29509 pop_deferring_access_checks ();
29511 /* Add the context to the front of the free list. */
29512 context
->next
= cp_parser_context_free_list
;
29513 cp_parser_context_free_list
= context
;
29515 return !error_occurred
;
29518 /* Returns true if we are parsing tentatively and are not committed to
29519 this tentative parse. */
29522 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29524 return (cp_parser_parsing_tentatively (parser
)
29525 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29528 /* Returns nonzero iff an error has occurred during the most recent
29529 tentative parse. */
29532 cp_parser_error_occurred (cp_parser
* parser
)
29534 return (cp_parser_parsing_tentatively (parser
)
29535 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29538 /* Returns nonzero if GNU extensions are allowed. */
29541 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29543 return parser
->allow_gnu_extensions_p
;
29546 /* Objective-C++ Productions */
29549 /* Parse an Objective-C expression, which feeds into a primary-expression
29553 objc-message-expression
29554 objc-string-literal
29555 objc-encode-expression
29556 objc-protocol-expression
29557 objc-selector-expression
29559 Returns a tree representation of the expression. */
29562 cp_parser_objc_expression (cp_parser
* parser
)
29564 /* Try to figure out what kind of declaration is present. */
29565 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29569 case CPP_OPEN_SQUARE
:
29570 return cp_parser_objc_message_expression (parser
);
29572 case CPP_OBJC_STRING
:
29573 kwd
= cp_lexer_consume_token (parser
->lexer
);
29574 return objc_build_string_object (kwd
->u
.value
);
29577 switch (kwd
->keyword
)
29579 case RID_AT_ENCODE
:
29580 return cp_parser_objc_encode_expression (parser
);
29582 case RID_AT_PROTOCOL
:
29583 return cp_parser_objc_protocol_expression (parser
);
29585 case RID_AT_SELECTOR
:
29586 return cp_parser_objc_selector_expression (parser
);
29593 error_at (kwd
->location
,
29594 "misplaced %<@%D%> Objective-C++ construct",
29596 cp_parser_skip_to_end_of_block_or_statement (parser
);
29599 return error_mark_node
;
29602 /* Parse an Objective-C message expression.
29604 objc-message-expression:
29605 [ objc-message-receiver objc-message-args ]
29607 Returns a representation of an Objective-C message. */
29610 cp_parser_objc_message_expression (cp_parser
* parser
)
29612 tree receiver
, messageargs
;
29614 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29615 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29616 receiver
= cp_parser_objc_message_receiver (parser
);
29617 messageargs
= cp_parser_objc_message_args (parser
);
29618 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29619 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29621 tree result
= objc_build_message_expr (receiver
, messageargs
);
29623 /* Construct a location e.g.
29626 ranging from the '[' to the ']', with the caret at the start. */
29627 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29628 protected_set_expr_location (result
, combined_loc
);
29633 /* Parse an objc-message-receiver.
29635 objc-message-receiver:
29637 simple-type-specifier
29639 Returns a representation of the type or expression. */
29642 cp_parser_objc_message_receiver (cp_parser
* parser
)
29646 /* An Objective-C message receiver may be either (1) a type
29647 or (2) an expression. */
29648 cp_parser_parse_tentatively (parser
);
29649 rcv
= cp_parser_expression (parser
);
29651 /* If that worked out, fine. */
29652 if (cp_parser_parse_definitely (parser
))
29655 cp_parser_parse_tentatively (parser
);
29656 rcv
= cp_parser_simple_type_specifier (parser
,
29657 /*decl_specs=*/NULL
,
29658 CP_PARSER_FLAGS_NONE
);
29660 if (cp_parser_parse_definitely (parser
))
29661 return objc_get_class_reference (rcv
);
29663 cp_parser_error (parser
, "objective-c++ message receiver expected");
29664 return error_mark_node
;
29667 /* Parse the arguments and selectors comprising an Objective-C message.
29672 objc-selector-args , objc-comma-args
29674 objc-selector-args:
29675 objc-selector [opt] : assignment-expression
29676 objc-selector-args objc-selector [opt] : assignment-expression
29679 assignment-expression
29680 objc-comma-args , assignment-expression
29682 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29683 selector arguments and TREE_VALUE containing a list of comma
29687 cp_parser_objc_message_args (cp_parser
* parser
)
29689 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29690 bool maybe_unary_selector_p
= true;
29691 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29693 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29695 tree selector
= NULL_TREE
, arg
;
29697 if (token
->type
!= CPP_COLON
)
29698 selector
= cp_parser_objc_selector (parser
);
29700 /* Detect if we have a unary selector. */
29701 if (maybe_unary_selector_p
29702 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29703 return build_tree_list (selector
, NULL_TREE
);
29705 maybe_unary_selector_p
= false;
29706 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29707 arg
= cp_parser_assignment_expression (parser
);
29710 = chainon (sel_args
,
29711 build_tree_list (selector
, arg
));
29713 token
= cp_lexer_peek_token (parser
->lexer
);
29716 /* Handle non-selector arguments, if any. */
29717 while (token
->type
== CPP_COMMA
)
29721 cp_lexer_consume_token (parser
->lexer
);
29722 arg
= cp_parser_assignment_expression (parser
);
29725 = chainon (addl_args
,
29726 build_tree_list (NULL_TREE
, arg
));
29728 token
= cp_lexer_peek_token (parser
->lexer
);
29731 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29733 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29734 return build_tree_list (error_mark_node
, error_mark_node
);
29737 return build_tree_list (sel_args
, addl_args
);
29740 /* Parse an Objective-C encode expression.
29742 objc-encode-expression:
29743 @encode objc-typename
29745 Returns an encoded representation of the type argument. */
29748 cp_parser_objc_encode_expression (cp_parser
* parser
)
29752 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29754 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29755 matching_parens parens
;
29756 parens
.require_open (parser
);
29757 token
= cp_lexer_peek_token (parser
->lexer
);
29758 type
= complete_type (cp_parser_type_id (parser
));
29759 parens
.require_close (parser
);
29763 error_at (token
->location
,
29764 "%<@encode%> must specify a type as an argument");
29765 return error_mark_node
;
29768 /* This happens if we find @encode(T) (where T is a template
29769 typename or something dependent on a template typename) when
29770 parsing a template. In that case, we can't compile it
29771 immediately, but we rather create an AT_ENCODE_EXPR which will
29772 need to be instantiated when the template is used.
29774 if (dependent_type_p (type
))
29776 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29777 TREE_READONLY (value
) = 1;
29782 /* Build a location of the form:
29785 with caret==start at the @ token, finishing at the close paren. */
29786 location_t combined_loc
29787 = make_location (start_loc
, start_loc
,
29788 cp_lexer_previous_token (parser
->lexer
)->location
);
29790 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29793 /* Parse an Objective-C @defs expression. */
29796 cp_parser_objc_defs_expression (cp_parser
*parser
)
29800 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29801 matching_parens parens
;
29802 parens
.require_open (parser
);
29803 name
= cp_parser_identifier (parser
);
29804 parens
.require_close (parser
);
29806 return objc_get_class_ivars (name
);
29809 /* Parse an Objective-C protocol expression.
29811 objc-protocol-expression:
29812 @protocol ( identifier )
29814 Returns a representation of the protocol expression. */
29817 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29820 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29822 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29823 matching_parens parens
;
29824 parens
.require_open (parser
);
29825 proto
= cp_parser_identifier (parser
);
29826 parens
.require_close (parser
);
29828 /* Build a location of the form:
29831 with caret==start at the @ token, finishing at the close paren. */
29832 location_t combined_loc
29833 = make_location (start_loc
, start_loc
,
29834 cp_lexer_previous_token (parser
->lexer
)->location
);
29835 tree result
= objc_build_protocol_expr (proto
);
29836 protected_set_expr_location (result
, combined_loc
);
29840 /* Parse an Objective-C selector expression.
29842 objc-selector-expression:
29843 @selector ( objc-method-signature )
29845 objc-method-signature:
29851 objc-selector-seq objc-selector :
29853 Returns a representation of the method selector. */
29856 cp_parser_objc_selector_expression (cp_parser
* parser
)
29858 tree sel_seq
= NULL_TREE
;
29859 bool maybe_unary_selector_p
= true;
29861 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29863 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29864 matching_parens parens
;
29865 parens
.require_open (parser
);
29866 token
= cp_lexer_peek_token (parser
->lexer
);
29868 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29869 || token
->type
== CPP_SCOPE
)
29871 tree selector
= NULL_TREE
;
29873 if (token
->type
!= CPP_COLON
29874 || token
->type
== CPP_SCOPE
)
29875 selector
= cp_parser_objc_selector (parser
);
29877 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29878 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29880 /* Detect if we have a unary selector. */
29881 if (maybe_unary_selector_p
)
29883 sel_seq
= selector
;
29884 goto finish_selector
;
29888 cp_parser_error (parser
, "expected %<:%>");
29891 maybe_unary_selector_p
= false;
29892 token
= cp_lexer_consume_token (parser
->lexer
);
29894 if (token
->type
== CPP_SCOPE
)
29897 = chainon (sel_seq
,
29898 build_tree_list (selector
, NULL_TREE
));
29900 = chainon (sel_seq
,
29901 build_tree_list (NULL_TREE
, NULL_TREE
));
29905 = chainon (sel_seq
,
29906 build_tree_list (selector
, NULL_TREE
));
29908 token
= cp_lexer_peek_token (parser
->lexer
);
29912 parens
.require_close (parser
);
29915 /* Build a location of the form:
29918 with caret==start at the @ token, finishing at the close paren. */
29919 location_t combined_loc
29920 = make_location (loc
, loc
,
29921 cp_lexer_previous_token (parser
->lexer
)->location
);
29922 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29923 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29924 protected_set_expr_location (result
, combined_loc
);
29928 /* Parse a list of identifiers.
29930 objc-identifier-list:
29932 objc-identifier-list , identifier
29934 Returns a TREE_LIST of identifier nodes. */
29937 cp_parser_objc_identifier_list (cp_parser
* parser
)
29943 identifier
= cp_parser_identifier (parser
);
29944 if (identifier
== error_mark_node
)
29945 return error_mark_node
;
29947 list
= build_tree_list (NULL_TREE
, identifier
);
29948 sep
= cp_lexer_peek_token (parser
->lexer
);
29950 while (sep
->type
== CPP_COMMA
)
29952 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29953 identifier
= cp_parser_identifier (parser
);
29954 if (identifier
== error_mark_node
)
29957 list
= chainon (list
, build_tree_list (NULL_TREE
,
29959 sep
= cp_lexer_peek_token (parser
->lexer
);
29965 /* Parse an Objective-C alias declaration.
29967 objc-alias-declaration:
29968 @compatibility_alias identifier identifier ;
29970 This function registers the alias mapping with the Objective-C front end.
29971 It returns nothing. */
29974 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29978 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29979 alias
= cp_parser_identifier (parser
);
29980 orig
= cp_parser_identifier (parser
);
29981 objc_declare_alias (alias
, orig
);
29982 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29985 /* Parse an Objective-C class forward-declaration.
29987 objc-class-declaration:
29988 @class objc-identifier-list ;
29990 The function registers the forward declarations with the Objective-C
29991 front end. It returns nothing. */
29994 cp_parser_objc_class_declaration (cp_parser
* parser
)
29996 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30001 id
= cp_parser_identifier (parser
);
30002 if (id
== error_mark_node
)
30005 objc_declare_class (id
);
30007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30008 cp_lexer_consume_token (parser
->lexer
);
30012 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30015 /* Parse a list of Objective-C protocol references.
30017 objc-protocol-refs-opt:
30018 objc-protocol-refs [opt]
30020 objc-protocol-refs:
30021 < objc-identifier-list >
30023 Returns a TREE_LIST of identifiers, if any. */
30026 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30028 tree protorefs
= NULL_TREE
;
30030 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30032 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30033 protorefs
= cp_parser_objc_identifier_list (parser
);
30034 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30040 /* Parse a Objective-C visibility specification. */
30043 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30045 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30047 switch (vis
->keyword
)
30049 case RID_AT_PRIVATE
:
30050 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30052 case RID_AT_PROTECTED
:
30053 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30055 case RID_AT_PUBLIC
:
30056 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30058 case RID_AT_PACKAGE
:
30059 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30065 /* Eat '@private'/'@protected'/'@public'. */
30066 cp_lexer_consume_token (parser
->lexer
);
30069 /* Parse an Objective-C method type. Return 'true' if it is a class
30070 (+) method, and 'false' if it is an instance (-) method. */
30073 cp_parser_objc_method_type (cp_parser
* parser
)
30075 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30081 /* Parse an Objective-C protocol qualifier. */
30084 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30086 tree quals
= NULL_TREE
, node
;
30087 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30089 node
= token
->u
.value
;
30091 while (node
&& identifier_p (node
)
30092 && (node
== ridpointers
[(int) RID_IN
]
30093 || node
== ridpointers
[(int) RID_OUT
]
30094 || node
== ridpointers
[(int) RID_INOUT
]
30095 || node
== ridpointers
[(int) RID_BYCOPY
]
30096 || node
== ridpointers
[(int) RID_BYREF
]
30097 || node
== ridpointers
[(int) RID_ONEWAY
]))
30099 quals
= tree_cons (NULL_TREE
, node
, quals
);
30100 cp_lexer_consume_token (parser
->lexer
);
30101 token
= cp_lexer_peek_token (parser
->lexer
);
30102 node
= token
->u
.value
;
30108 /* Parse an Objective-C typename. */
30111 cp_parser_objc_typename (cp_parser
* parser
)
30113 tree type_name
= NULL_TREE
;
30115 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30117 tree proto_quals
, cp_type
= NULL_TREE
;
30119 matching_parens parens
;
30120 parens
.consume_open (parser
); /* Eat '('. */
30121 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30123 /* An ObjC type name may consist of just protocol qualifiers, in which
30124 case the type shall default to 'id'. */
30125 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30127 cp_type
= cp_parser_type_id (parser
);
30129 /* If the type could not be parsed, an error has already
30130 been produced. For error recovery, behave as if it had
30131 not been specified, which will use the default type
30133 if (cp_type
== error_mark_node
)
30135 cp_type
= NULL_TREE
;
30136 /* We need to skip to the closing parenthesis as
30137 cp_parser_type_id() does not seem to do it for
30139 cp_parser_skip_to_closing_parenthesis (parser
,
30140 /*recovering=*/true,
30141 /*or_comma=*/false,
30142 /*consume_paren=*/false);
30146 parens
.require_close (parser
);
30147 type_name
= build_tree_list (proto_quals
, cp_type
);
30153 /* Check to see if TYPE refers to an Objective-C selector name. */
30156 cp_parser_objc_selector_p (enum cpp_ttype type
)
30158 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30159 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30160 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30161 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30162 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30165 /* Parse an Objective-C selector. */
30168 cp_parser_objc_selector (cp_parser
* parser
)
30170 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30172 if (!cp_parser_objc_selector_p (token
->type
))
30174 error_at (token
->location
, "invalid Objective-C++ selector name");
30175 return error_mark_node
;
30178 /* C++ operator names are allowed to appear in ObjC selectors. */
30179 switch (token
->type
)
30181 case CPP_AND_AND
: return get_identifier ("and");
30182 case CPP_AND_EQ
: return get_identifier ("and_eq");
30183 case CPP_AND
: return get_identifier ("bitand");
30184 case CPP_OR
: return get_identifier ("bitor");
30185 case CPP_COMPL
: return get_identifier ("compl");
30186 case CPP_NOT
: return get_identifier ("not");
30187 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30188 case CPP_OR_OR
: return get_identifier ("or");
30189 case CPP_OR_EQ
: return get_identifier ("or_eq");
30190 case CPP_XOR
: return get_identifier ("xor");
30191 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30192 default: return token
->u
.value
;
30196 /* Parse an Objective-C params list. */
30199 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30201 tree params
= NULL_TREE
;
30202 bool maybe_unary_selector_p
= true;
30203 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30205 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30207 tree selector
= NULL_TREE
, type_name
, identifier
;
30208 tree parm_attr
= NULL_TREE
;
30210 if (token
->keyword
== RID_ATTRIBUTE
)
30213 if (token
->type
!= CPP_COLON
)
30214 selector
= cp_parser_objc_selector (parser
);
30216 /* Detect if we have a unary selector. */
30217 if (maybe_unary_selector_p
30218 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30220 params
= selector
; /* Might be followed by attributes. */
30224 maybe_unary_selector_p
= false;
30225 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30227 /* Something went quite wrong. There should be a colon
30228 here, but there is not. Stop parsing parameters. */
30231 type_name
= cp_parser_objc_typename (parser
);
30232 /* New ObjC allows attributes on parameters too. */
30233 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30234 parm_attr
= cp_parser_attributes_opt (parser
);
30235 identifier
= cp_parser_identifier (parser
);
30239 objc_build_keyword_decl (selector
,
30244 token
= cp_lexer_peek_token (parser
->lexer
);
30247 if (params
== NULL_TREE
)
30249 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30250 return error_mark_node
;
30253 /* We allow tail attributes for the method. */
30254 if (token
->keyword
== RID_ATTRIBUTE
)
30256 *attributes
= cp_parser_attributes_opt (parser
);
30257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30258 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30260 cp_parser_error (parser
,
30261 "method attributes must be specified at the end");
30262 return error_mark_node
;
30265 if (params
== NULL_TREE
)
30267 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30268 return error_mark_node
;
30273 /* Parse the non-keyword Objective-C params. */
30276 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30279 tree params
= make_node (TREE_LIST
);
30280 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30281 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30283 while (token
->type
== CPP_COMMA
)
30285 cp_parameter_declarator
*parmdecl
;
30288 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30289 token
= cp_lexer_peek_token (parser
->lexer
);
30291 if (token
->type
== CPP_ELLIPSIS
)
30293 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30295 token
= cp_lexer_peek_token (parser
->lexer
);
30299 /* TODO: parse attributes for tail parameters. */
30300 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30301 parm
= grokdeclarator (parmdecl
->declarator
,
30302 &parmdecl
->decl_specifiers
,
30303 PARM
, /*initialized=*/0,
30304 /*attrlist=*/NULL
);
30306 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30307 token
= cp_lexer_peek_token (parser
->lexer
);
30310 /* We allow tail attributes for the method. */
30311 if (token
->keyword
== RID_ATTRIBUTE
)
30313 if (*attributes
== NULL_TREE
)
30315 *attributes
= cp_parser_attributes_opt (parser
);
30316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30317 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30321 /* We have an error, but parse the attributes, so that we can
30323 *attributes
= cp_parser_attributes_opt (parser
);
30325 cp_parser_error (parser
,
30326 "method attributes must be specified at the end");
30327 return error_mark_node
;
30333 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30336 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30338 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30340 /* If the next token is `extern' and the following token is a string
30341 literal, then we have a linkage specification. */
30342 if (token
->keyword
== RID_EXTERN
30343 && cp_parser_is_pure_string_literal
30344 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30345 cp_parser_linkage_specification (parser
);
30346 /* Handle #pragma, if any. */
30347 else if (token
->type
== CPP_PRAGMA
)
30348 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30349 /* Allow stray semicolons. */
30350 else if (token
->type
== CPP_SEMICOLON
)
30351 cp_lexer_consume_token (parser
->lexer
);
30352 /* Mark methods as optional or required, when building protocols. */
30353 else if (token
->keyword
== RID_AT_OPTIONAL
)
30355 cp_lexer_consume_token (parser
->lexer
);
30356 objc_set_method_opt (true);
30358 else if (token
->keyword
== RID_AT_REQUIRED
)
30360 cp_lexer_consume_token (parser
->lexer
);
30361 objc_set_method_opt (false);
30363 else if (token
->keyword
== RID_NAMESPACE
)
30364 cp_parser_namespace_definition (parser
);
30365 /* Other stray characters must generate errors. */
30366 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30368 cp_lexer_consume_token (parser
->lexer
);
30369 error ("stray %qs between Objective-C++ methods",
30370 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30372 /* Finally, try to parse a block-declaration, or a function-definition. */
30374 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30377 /* Parse a method signature. */
30380 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30382 tree rettype
, kwdparms
, optparms
;
30383 bool ellipsis
= false;
30384 bool is_class_method
;
30386 is_class_method
= cp_parser_objc_method_type (parser
);
30387 rettype
= cp_parser_objc_typename (parser
);
30388 *attributes
= NULL_TREE
;
30389 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30390 if (kwdparms
== error_mark_node
)
30391 return error_mark_node
;
30392 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30393 if (optparms
== error_mark_node
)
30394 return error_mark_node
;
30396 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30400 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30403 cp_lexer_save_tokens (parser
->lexer
);
30404 tattr
= cp_parser_attributes_opt (parser
);
30405 gcc_assert (tattr
) ;
30407 /* If the attributes are followed by a method introducer, this is not allowed.
30408 Dump the attributes and flag the situation. */
30409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30410 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30413 /* Otherwise, the attributes introduce some interstitial code, possibly so
30414 rewind to allow that check. */
30415 cp_lexer_rollback_tokens (parser
->lexer
);
30419 /* Parse an Objective-C method prototype list. */
30422 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30424 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30426 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30428 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30430 tree attributes
, sig
;
30431 bool is_class_method
;
30432 if (token
->type
== CPP_PLUS
)
30433 is_class_method
= true;
30435 is_class_method
= false;
30436 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30437 if (sig
== error_mark_node
)
30439 cp_parser_skip_to_end_of_block_or_statement (parser
);
30440 token
= cp_lexer_peek_token (parser
->lexer
);
30443 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30444 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30446 else if (token
->keyword
== RID_AT_PROPERTY
)
30447 cp_parser_objc_at_property_declaration (parser
);
30448 else if (token
->keyword
== RID_ATTRIBUTE
30449 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30450 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30452 "prefix attributes are ignored for methods");
30454 /* Allow for interspersed non-ObjC++ code. */
30455 cp_parser_objc_interstitial_code (parser
);
30457 token
= cp_lexer_peek_token (parser
->lexer
);
30460 if (token
->type
!= CPP_EOF
)
30461 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30463 cp_parser_error (parser
, "expected %<@end%>");
30465 objc_finish_interface ();
30468 /* Parse an Objective-C method definition list. */
30471 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30473 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30475 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30479 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30482 tree sig
, attribute
;
30483 bool is_class_method
;
30484 if (token
->type
== CPP_PLUS
)
30485 is_class_method
= true;
30487 is_class_method
= false;
30488 push_deferring_access_checks (dk_deferred
);
30489 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30490 if (sig
== error_mark_node
)
30492 cp_parser_skip_to_end_of_block_or_statement (parser
);
30493 token
= cp_lexer_peek_token (parser
->lexer
);
30496 objc_start_method_definition (is_class_method
, sig
, attribute
,
30499 /* For historical reasons, we accept an optional semicolon. */
30500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30501 cp_lexer_consume_token (parser
->lexer
);
30503 ptk
= cp_lexer_peek_token (parser
->lexer
);
30504 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30505 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30507 perform_deferred_access_checks (tf_warning_or_error
);
30508 stop_deferring_access_checks ();
30509 meth
= cp_parser_function_definition_after_declarator (parser
,
30511 pop_deferring_access_checks ();
30512 objc_finish_method_definition (meth
);
30515 /* The following case will be removed once @synthesize is
30516 completely implemented. */
30517 else if (token
->keyword
== RID_AT_PROPERTY
)
30518 cp_parser_objc_at_property_declaration (parser
);
30519 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30520 cp_parser_objc_at_synthesize_declaration (parser
);
30521 else if (token
->keyword
== RID_AT_DYNAMIC
)
30522 cp_parser_objc_at_dynamic_declaration (parser
);
30523 else if (token
->keyword
== RID_ATTRIBUTE
30524 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30525 warning_at (token
->location
, OPT_Wattributes
,
30526 "prefix attributes are ignored for methods");
30528 /* Allow for interspersed non-ObjC++ code. */
30529 cp_parser_objc_interstitial_code (parser
);
30531 token
= cp_lexer_peek_token (parser
->lexer
);
30534 if (token
->type
!= CPP_EOF
)
30535 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30537 cp_parser_error (parser
, "expected %<@end%>");
30539 objc_finish_implementation ();
30542 /* Parse Objective-C ivars. */
30545 cp_parser_objc_class_ivars (cp_parser
* parser
)
30547 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30549 if (token
->type
!= CPP_OPEN_BRACE
)
30550 return; /* No ivars specified. */
30552 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30553 token
= cp_lexer_peek_token (parser
->lexer
);
30555 while (token
->type
!= CPP_CLOSE_BRACE
30556 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30558 cp_decl_specifier_seq declspecs
;
30559 int decl_class_or_enum_p
;
30560 tree prefix_attributes
;
30562 cp_parser_objc_visibility_spec (parser
);
30564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30567 cp_parser_decl_specifier_seq (parser
,
30568 CP_PARSER_FLAGS_OPTIONAL
,
30570 &decl_class_or_enum_p
);
30572 /* auto, register, static, extern, mutable. */
30573 if (declspecs
.storage_class
!= sc_none
)
30575 cp_parser_error (parser
, "invalid type for instance variable");
30576 declspecs
.storage_class
= sc_none
;
30579 /* thread_local. */
30580 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30582 cp_parser_error (parser
, "invalid type for instance variable");
30583 declspecs
.locations
[ds_thread
] = 0;
30587 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30589 cp_parser_error (parser
, "invalid type for instance variable");
30590 declspecs
.locations
[ds_typedef
] = 0;
30593 prefix_attributes
= declspecs
.attributes
;
30594 declspecs
.attributes
= NULL_TREE
;
30596 /* Keep going until we hit the `;' at the end of the
30598 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30600 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30601 cp_declarator
*declarator
= NULL
;
30602 int ctor_dtor_or_conv_p
;
30604 /* Check for a (possibly unnamed) bitfield declaration. */
30605 token
= cp_lexer_peek_token (parser
->lexer
);
30606 if (token
->type
== CPP_COLON
)
30609 if (token
->type
== CPP_NAME
30610 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30613 /* Get the name of the bitfield. */
30614 declarator
= make_id_declarator (NULL_TREE
,
30615 cp_parser_identifier (parser
),
30616 sfk_none
, token
->location
);
30619 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30620 /* Get the width of the bitfield. */
30622 = cp_parser_constant_expression (parser
);
30626 /* Parse the declarator. */
30628 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30629 &ctor_dtor_or_conv_p
,
30630 /*parenthesized_p=*/NULL
,
30631 /*member_p=*/false,
30632 /*friend_p=*/false);
30635 /* Look for attributes that apply to the ivar. */
30636 attributes
= cp_parser_attributes_opt (parser
);
30637 /* Remember which attributes are prefix attributes and
30639 first_attribute
= attributes
;
30640 /* Combine the attributes. */
30641 attributes
= attr_chainon (prefix_attributes
, attributes
);
30644 /* Create the bitfield declaration. */
30645 decl
= grokbitfield (declarator
, &declspecs
,
30646 width
, NULL_TREE
, attributes
);
30648 decl
= grokfield (declarator
, &declspecs
,
30649 NULL_TREE
, /*init_const_expr_p=*/false,
30650 NULL_TREE
, attributes
);
30652 /* Add the instance variable. */
30653 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30654 objc_add_instance_variable (decl
);
30656 /* Reset PREFIX_ATTRIBUTES. */
30657 if (attributes
!= error_mark_node
)
30659 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30660 attributes
= TREE_CHAIN (attributes
);
30662 TREE_CHAIN (attributes
) = NULL_TREE
;
30665 token
= cp_lexer_peek_token (parser
->lexer
);
30667 if (token
->type
== CPP_COMMA
)
30669 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30675 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30676 token
= cp_lexer_peek_token (parser
->lexer
);
30679 if (token
->keyword
== RID_AT_END
)
30680 cp_parser_error (parser
, "expected %<}%>");
30682 /* Do not consume the RID_AT_END, so it will be read again as terminating
30683 the @interface of @implementation. */
30684 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30685 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30687 /* For historical reasons, we accept an optional semicolon. */
30688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30689 cp_lexer_consume_token (parser
->lexer
);
30692 /* Parse an Objective-C protocol declaration. */
30695 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30697 tree proto
, protorefs
;
30700 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30703 tok
= cp_lexer_peek_token (parser
->lexer
);
30704 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30705 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30709 /* See if we have a forward declaration or a definition. */
30710 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30712 /* Try a forward declaration first. */
30713 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30719 id
= cp_parser_identifier (parser
);
30720 if (id
== error_mark_node
)
30723 objc_declare_protocol (id
, attributes
);
30725 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30726 cp_lexer_consume_token (parser
->lexer
);
30730 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30733 /* Ok, we got a full-fledged definition (or at least should). */
30736 proto
= cp_parser_identifier (parser
);
30737 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30738 objc_start_protocol (proto
, protorefs
, attributes
);
30739 cp_parser_objc_method_prototype_list (parser
);
30743 /* Parse an Objective-C superclass or category. */
30746 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30749 tree
*categ
, bool *is_class_extension
)
30751 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30753 *super
= *categ
= NULL_TREE
;
30754 *is_class_extension
= false;
30755 if (next
->type
== CPP_COLON
)
30757 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30758 *super
= cp_parser_identifier (parser
);
30760 else if (next
->type
== CPP_OPEN_PAREN
)
30762 matching_parens parens
;
30763 parens
.consume_open (parser
); /* Eat '('. */
30765 /* If there is no category name, and this is an @interface, we
30766 have a class extension. */
30767 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30769 *categ
= NULL_TREE
;
30770 *is_class_extension
= true;
30773 *categ
= cp_parser_identifier (parser
);
30775 parens
.require_close (parser
);
30779 /* Parse an Objective-C class interface. */
30782 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30784 tree name
, super
, categ
, protos
;
30785 bool is_class_extension
;
30787 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30788 name
= cp_parser_identifier (parser
);
30789 if (name
== error_mark_node
)
30791 /* It's hard to recover because even if valid @interface stuff
30792 is to follow, we can't compile it (or validate it) if we
30793 don't even know which class it refers to. Let's assume this
30794 was a stray '@interface' token in the stream and skip it.
30798 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30799 &is_class_extension
);
30800 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30802 /* We have either a class or a category on our hands. */
30803 if (categ
|| is_class_extension
)
30804 objc_start_category_interface (name
, categ
, protos
, attributes
);
30807 objc_start_class_interface (name
, super
, protos
, attributes
);
30808 /* Handle instance variable declarations, if any. */
30809 cp_parser_objc_class_ivars (parser
);
30810 objc_continue_interface ();
30813 cp_parser_objc_method_prototype_list (parser
);
30816 /* Parse an Objective-C class implementation. */
30819 cp_parser_objc_class_implementation (cp_parser
* parser
)
30821 tree name
, super
, categ
;
30822 bool is_class_extension
;
30824 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30825 name
= cp_parser_identifier (parser
);
30826 if (name
== error_mark_node
)
30828 /* It's hard to recover because even if valid @implementation
30829 stuff is to follow, we can't compile it (or validate it) if
30830 we don't even know which class it refers to. Let's assume
30831 this was a stray '@implementation' token in the stream and
30836 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30837 &is_class_extension
);
30839 /* We have either a class or a category on our hands. */
30841 objc_start_category_implementation (name
, categ
);
30844 objc_start_class_implementation (name
, super
);
30845 /* Handle instance variable declarations, if any. */
30846 cp_parser_objc_class_ivars (parser
);
30847 objc_continue_implementation ();
30850 cp_parser_objc_method_definition_list (parser
);
30853 /* Consume the @end token and finish off the implementation. */
30856 cp_parser_objc_end_implementation (cp_parser
* parser
)
30858 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30859 objc_finish_implementation ();
30862 /* Parse an Objective-C declaration. */
30865 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30867 /* Try to figure out what kind of declaration is present. */
30868 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30871 switch (kwd
->keyword
)
30876 error_at (kwd
->location
, "attributes may not be specified before"
30877 " the %<@%D%> Objective-C++ keyword",
30881 case RID_AT_IMPLEMENTATION
:
30882 warning_at (kwd
->location
, OPT_Wattributes
,
30883 "prefix attributes are ignored before %<@%D%>",
30890 switch (kwd
->keyword
)
30893 cp_parser_objc_alias_declaration (parser
);
30896 cp_parser_objc_class_declaration (parser
);
30898 case RID_AT_PROTOCOL
:
30899 cp_parser_objc_protocol_declaration (parser
, attributes
);
30901 case RID_AT_INTERFACE
:
30902 cp_parser_objc_class_interface (parser
, attributes
);
30904 case RID_AT_IMPLEMENTATION
:
30905 cp_parser_objc_class_implementation (parser
);
30908 cp_parser_objc_end_implementation (parser
);
30911 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30913 cp_parser_skip_to_end_of_block_or_statement (parser
);
30917 /* Parse an Objective-C try-catch-finally statement.
30919 objc-try-catch-finally-stmt:
30920 @try compound-statement objc-catch-clause-seq [opt]
30921 objc-finally-clause [opt]
30923 objc-catch-clause-seq:
30924 objc-catch-clause objc-catch-clause-seq [opt]
30927 @catch ( objc-exception-declaration ) compound-statement
30929 objc-finally-clause:
30930 @finally compound-statement
30932 objc-exception-declaration:
30933 parameter-declaration
30936 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30940 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30941 for C. Keep them in sync. */
30944 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30946 location_t location
;
30949 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30950 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30951 objc_maybe_warn_exceptions (location
);
30952 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30953 node, lest it get absorbed into the surrounding block. */
30954 stmt
= push_stmt_list ();
30955 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30956 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30958 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30960 cp_parameter_declarator
*parm
;
30961 tree parameter_declaration
= error_mark_node
;
30962 bool seen_open_paren
= false;
30963 matching_parens parens
;
30965 cp_lexer_consume_token (parser
->lexer
);
30966 if (parens
.require_open (parser
))
30967 seen_open_paren
= true;
30968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30970 /* We have "@catch (...)" (where the '...' are literally
30971 what is in the code). Skip the '...'.
30972 parameter_declaration is set to NULL_TREE, and
30973 objc_being_catch_clauses() knows that that means
30975 cp_lexer_consume_token (parser
->lexer
);
30976 parameter_declaration
= NULL_TREE
;
30980 /* We have "@catch (NSException *exception)" or something
30981 like that. Parse the parameter declaration. */
30982 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30984 parameter_declaration
= error_mark_node
;
30986 parameter_declaration
= grokdeclarator (parm
->declarator
,
30987 &parm
->decl_specifiers
,
30988 PARM
, /*initialized=*/0,
30989 /*attrlist=*/NULL
);
30991 if (seen_open_paren
)
30992 parens
.require_close (parser
);
30995 /* If there was no open parenthesis, we are recovering from
30996 an error, and we are trying to figure out what mistake
30997 the user has made. */
30999 /* If there is an immediate closing parenthesis, the user
31000 probably forgot the opening one (ie, they typed "@catch
31001 NSException *e)". Parse the closing parenthesis and keep
31003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31004 cp_lexer_consume_token (parser
->lexer
);
31006 /* If these is no immediate closing parenthesis, the user
31007 probably doesn't know that parenthesis are required at
31008 all (ie, they typed "@catch NSException *e"). So, just
31009 forget about the closing parenthesis and keep going. */
31011 objc_begin_catch_clause (parameter_declaration
);
31012 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31013 objc_finish_catch_clause ();
31015 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31017 cp_lexer_consume_token (parser
->lexer
);
31018 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31019 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31020 node, lest it get absorbed into the surrounding block. */
31021 stmt
= push_stmt_list ();
31022 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31023 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31026 return objc_finish_try_stmt ();
31029 /* Parse an Objective-C synchronized statement.
31031 objc-synchronized-stmt:
31032 @synchronized ( expression ) compound-statement
31034 Returns NULL_TREE. */
31037 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31039 location_t location
;
31042 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31044 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31045 objc_maybe_warn_exceptions (location
);
31046 matching_parens parens
;
31047 parens
.require_open (parser
);
31048 lock
= cp_parser_expression (parser
);
31049 parens
.require_close (parser
);
31051 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31052 node, lest it get absorbed into the surrounding block. */
31053 stmt
= push_stmt_list ();
31054 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31056 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31059 /* Parse an Objective-C throw statement.
31062 @throw assignment-expression [opt] ;
31064 Returns a constructed '@throw' statement. */
31067 cp_parser_objc_throw_statement (cp_parser
*parser
)
31069 tree expr
= NULL_TREE
;
31070 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31072 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31074 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31075 expr
= cp_parser_expression (parser
);
31077 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31079 return objc_build_throw_stmt (loc
, expr
);
31082 /* Parse an Objective-C statement. */
31085 cp_parser_objc_statement (cp_parser
* parser
)
31087 /* Try to figure out what kind of declaration is present. */
31088 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31090 switch (kwd
->keyword
)
31093 return cp_parser_objc_try_catch_finally_statement (parser
);
31094 case RID_AT_SYNCHRONIZED
:
31095 return cp_parser_objc_synchronized_statement (parser
);
31097 return cp_parser_objc_throw_statement (parser
);
31099 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31101 cp_parser_skip_to_end_of_block_or_statement (parser
);
31104 return error_mark_node
;
31107 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31108 look ahead to see if an objc keyword follows the attributes. This
31109 is to detect the use of prefix attributes on ObjC @interface and
31113 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31115 cp_lexer_save_tokens (parser
->lexer
);
31116 *attrib
= cp_parser_attributes_opt (parser
);
31117 gcc_assert (*attrib
);
31118 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31120 cp_lexer_commit_tokens (parser
->lexer
);
31123 cp_lexer_rollback_tokens (parser
->lexer
);
31127 /* This routine is a minimal replacement for
31128 c_parser_struct_declaration () used when parsing the list of
31129 types/names or ObjC++ properties. For example, when parsing the
31132 @property (readonly) int a, b, c;
31134 this function is responsible for parsing "int a, int b, int c" and
31135 returning the declarations as CHAIN of DECLs.
31137 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31138 similar parsing. */
31140 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31142 tree decls
= NULL_TREE
;
31143 cp_decl_specifier_seq declspecs
;
31144 int decl_class_or_enum_p
;
31145 tree prefix_attributes
;
31147 cp_parser_decl_specifier_seq (parser
,
31148 CP_PARSER_FLAGS_NONE
,
31150 &decl_class_or_enum_p
);
31152 if (declspecs
.type
== error_mark_node
)
31153 return error_mark_node
;
31155 /* auto, register, static, extern, mutable. */
31156 if (declspecs
.storage_class
!= sc_none
)
31158 cp_parser_error (parser
, "invalid type for property");
31159 declspecs
.storage_class
= sc_none
;
31162 /* thread_local. */
31163 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31165 cp_parser_error (parser
, "invalid type for property");
31166 declspecs
.locations
[ds_thread
] = 0;
31170 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31172 cp_parser_error (parser
, "invalid type for property");
31173 declspecs
.locations
[ds_typedef
] = 0;
31176 prefix_attributes
= declspecs
.attributes
;
31177 declspecs
.attributes
= NULL_TREE
;
31179 /* Keep going until we hit the `;' at the end of the declaration. */
31180 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31182 tree attributes
, first_attribute
, decl
;
31183 cp_declarator
*declarator
;
31186 /* Parse the declarator. */
31187 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31188 NULL
, NULL
, false, false);
31190 /* Look for attributes that apply to the ivar. */
31191 attributes
= cp_parser_attributes_opt (parser
);
31192 /* Remember which attributes are prefix attributes and
31194 first_attribute
= attributes
;
31195 /* Combine the attributes. */
31196 attributes
= attr_chainon (prefix_attributes
, attributes
);
31198 decl
= grokfield (declarator
, &declspecs
,
31199 NULL_TREE
, /*init_const_expr_p=*/false,
31200 NULL_TREE
, attributes
);
31202 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31203 return error_mark_node
;
31205 /* Reset PREFIX_ATTRIBUTES. */
31206 if (attributes
!= error_mark_node
)
31208 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31209 attributes
= TREE_CHAIN (attributes
);
31211 TREE_CHAIN (attributes
) = NULL_TREE
;
31214 DECL_CHAIN (decl
) = decls
;
31217 token
= cp_lexer_peek_token (parser
->lexer
);
31218 if (token
->type
== CPP_COMMA
)
31220 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31229 /* Parse an Objective-C @property declaration. The syntax is:
31231 objc-property-declaration:
31232 '@property' objc-property-attributes[opt] struct-declaration ;
31234 objc-property-attributes:
31235 '(' objc-property-attribute-list ')'
31237 objc-property-attribute-list:
31238 objc-property-attribute
31239 objc-property-attribute-list, objc-property-attribute
31241 objc-property-attribute
31242 'getter' = identifier
31243 'setter' = identifier
31252 @property NSString *name;
31253 @property (readonly) id object;
31254 @property (retain, nonatomic, getter=getTheName) id name;
31255 @property int a, b, c;
31257 PS: This function is identical to
31258 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31260 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31262 /* The following variables hold the attributes of the properties as
31263 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31264 seen. When we see an attribute, we set them to 'true' (if they
31265 are boolean properties) or to the identifier (if they have an
31266 argument, ie, for getter and setter). Note that here we only
31267 parse the list of attributes, check the syntax and accumulate the
31268 attributes that we find. objc_add_property_declaration() will
31269 then process the information. */
31270 bool property_assign
= false;
31271 bool property_copy
= false;
31272 tree property_getter_ident
= NULL_TREE
;
31273 bool property_nonatomic
= false;
31274 bool property_readonly
= false;
31275 bool property_readwrite
= false;
31276 bool property_retain
= false;
31277 tree property_setter_ident
= NULL_TREE
;
31279 /* 'properties' is the list of properties that we read. Usually a
31280 single one, but maybe more (eg, in "@property int a, b, c;" there
31285 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31287 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31289 /* Parse the optional attribute list... */
31290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31293 matching_parens parens
;
31294 parens
.consume_open (parser
);
31298 bool syntax_error
= false;
31299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31302 if (token
->type
!= CPP_NAME
)
31304 cp_parser_error (parser
, "expected identifier");
31307 keyword
= C_RID_CODE (token
->u
.value
);
31308 cp_lexer_consume_token (parser
->lexer
);
31311 case RID_ASSIGN
: property_assign
= true; break;
31312 case RID_COPY
: property_copy
= true; break;
31313 case RID_NONATOMIC
: property_nonatomic
= true; break;
31314 case RID_READONLY
: property_readonly
= true; break;
31315 case RID_READWRITE
: property_readwrite
= true; break;
31316 case RID_RETAIN
: property_retain
= true; break;
31320 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31322 if (keyword
== RID_GETTER
)
31323 cp_parser_error (parser
,
31324 "missing %<=%> (after %<getter%> attribute)");
31326 cp_parser_error (parser
,
31327 "missing %<=%> (after %<setter%> attribute)");
31328 syntax_error
= true;
31331 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31332 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31334 cp_parser_error (parser
, "expected identifier");
31335 syntax_error
= true;
31338 if (keyword
== RID_SETTER
)
31340 if (property_setter_ident
!= NULL_TREE
)
31342 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31343 cp_lexer_consume_token (parser
->lexer
);
31346 property_setter_ident
= cp_parser_objc_selector (parser
);
31347 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31348 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31350 cp_lexer_consume_token (parser
->lexer
);
31354 if (property_getter_ident
!= NULL_TREE
)
31356 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31357 cp_lexer_consume_token (parser
->lexer
);
31360 property_getter_ident
= cp_parser_objc_selector (parser
);
31364 cp_parser_error (parser
, "unknown property attribute");
31365 syntax_error
= true;
31372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31373 cp_lexer_consume_token (parser
->lexer
);
31378 /* FIXME: "@property (setter, assign);" will generate a spurious
31379 "error: expected ‘)’ before ‘,’ token". This is because
31380 cp_parser_require, unlike the C counterpart, will produce an
31381 error even if we are in error recovery. */
31382 if (!parens
.require_close (parser
))
31384 cp_parser_skip_to_closing_parenthesis (parser
,
31385 /*recovering=*/true,
31386 /*or_comma=*/false,
31387 /*consume_paren=*/true);
31391 /* ... and the property declaration(s). */
31392 properties
= cp_parser_objc_struct_declaration (parser
);
31394 if (properties
== error_mark_node
)
31396 cp_parser_skip_to_end_of_statement (parser
);
31397 /* If the next token is now a `;', consume it. */
31398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31399 cp_lexer_consume_token (parser
->lexer
);
31403 if (properties
== NULL_TREE
)
31404 cp_parser_error (parser
, "expected identifier");
31407 /* Comma-separated properties are chained together in
31408 reverse order; add them one by one. */
31409 properties
= nreverse (properties
);
31411 for (; properties
; properties
= TREE_CHAIN (properties
))
31412 objc_add_property_declaration (loc
, copy_node (properties
),
31413 property_readonly
, property_readwrite
,
31414 property_assign
, property_retain
,
31415 property_copy
, property_nonatomic
,
31416 property_getter_ident
, property_setter_ident
);
31419 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31422 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31424 objc-synthesize-declaration:
31425 @synthesize objc-synthesize-identifier-list ;
31427 objc-synthesize-identifier-list:
31428 objc-synthesize-identifier
31429 objc-synthesize-identifier-list, objc-synthesize-identifier
31431 objc-synthesize-identifier
31433 identifier = identifier
31436 @synthesize MyProperty;
31437 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31439 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31440 for C. Keep them in sync.
31443 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31445 tree list
= NULL_TREE
;
31447 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31449 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31452 tree property
, ivar
;
31453 property
= cp_parser_identifier (parser
);
31454 if (property
== error_mark_node
)
31456 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31459 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31461 cp_lexer_consume_token (parser
->lexer
);
31462 ivar
= cp_parser_identifier (parser
);
31463 if (ivar
== error_mark_node
)
31465 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31471 list
= chainon (list
, build_tree_list (ivar
, property
));
31472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31473 cp_lexer_consume_token (parser
->lexer
);
31477 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31478 objc_add_synthesize_declaration (loc
, list
);
31481 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31483 objc-dynamic-declaration:
31484 @dynamic identifier-list ;
31487 @dynamic MyProperty;
31488 @dynamic MyProperty, AnotherProperty;
31490 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31491 for C. Keep them in sync.
31494 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31496 tree list
= NULL_TREE
;
31498 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31500 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31504 property
= cp_parser_identifier (parser
);
31505 if (property
== error_mark_node
)
31507 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31510 list
= chainon (list
, build_tree_list (NULL
, property
));
31511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31512 cp_lexer_consume_token (parser
->lexer
);
31516 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31517 objc_add_dynamic_declaration (loc
, list
);
31521 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31523 /* Returns name of the next clause.
31524 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31525 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31526 returned and the token is consumed. */
31528 static pragma_omp_clause
31529 cp_parser_omp_clause_name (cp_parser
*parser
)
31531 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31533 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31534 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31535 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31536 result
= PRAGMA_OMP_CLAUSE_IF
;
31537 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31538 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31539 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31540 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31541 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31542 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31543 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31544 result
= PRAGMA_OMP_CLAUSE_FOR
;
31545 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31547 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31548 const char *p
= IDENTIFIER_POINTER (id
);
31553 if (!strcmp ("aligned", p
))
31554 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31555 else if (!strcmp ("async", p
))
31556 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31559 if (!strcmp ("collapse", p
))
31560 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31561 else if (!strcmp ("copy", p
))
31562 result
= PRAGMA_OACC_CLAUSE_COPY
;
31563 else if (!strcmp ("copyin", p
))
31564 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31565 else if (!strcmp ("copyout", p
))
31566 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31567 else if (!strcmp ("copyprivate", p
))
31568 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31569 else if (!strcmp ("create", p
))
31570 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31573 if (!strcmp ("defaultmap", p
))
31574 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31575 else if (!strcmp ("depend", p
))
31576 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31577 else if (!strcmp ("device", p
))
31578 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31579 else if (!strcmp ("deviceptr", p
))
31580 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31581 else if (!strcmp ("device_resident", p
))
31582 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31583 else if (!strcmp ("dist_schedule", p
))
31584 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31587 if (!strcmp ("final", p
))
31588 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31589 else if (!strcmp ("finalize", p
))
31590 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31591 else if (!strcmp ("firstprivate", p
))
31592 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31593 else if (!strcmp ("from", p
))
31594 result
= PRAGMA_OMP_CLAUSE_FROM
;
31597 if (!strcmp ("gang", p
))
31598 result
= PRAGMA_OACC_CLAUSE_GANG
;
31599 else if (!strcmp ("grainsize", p
))
31600 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31603 if (!strcmp ("hint", p
))
31604 result
= PRAGMA_OMP_CLAUSE_HINT
;
31605 else if (!strcmp ("host", p
))
31606 result
= PRAGMA_OACC_CLAUSE_HOST
;
31609 if (!strcmp ("if_present", p
))
31610 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31611 else if (!strcmp ("in_reduction", p
))
31612 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
31613 else if (!strcmp ("inbranch", p
))
31614 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31615 else if (!strcmp ("independent", p
))
31616 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31617 else if (!strcmp ("is_device_ptr", p
))
31618 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31621 if (!strcmp ("lastprivate", p
))
31622 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31623 else if (!strcmp ("linear", p
))
31624 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31625 else if (!strcmp ("link", p
))
31626 result
= PRAGMA_OMP_CLAUSE_LINK
;
31629 if (!strcmp ("map", p
))
31630 result
= PRAGMA_OMP_CLAUSE_MAP
;
31631 else if (!strcmp ("mergeable", p
))
31632 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31635 if (!strcmp ("nogroup", p
))
31636 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31637 else if (!strcmp ("nontemporal", p
))
31638 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
31639 else if (!strcmp ("notinbranch", p
))
31640 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31641 else if (!strcmp ("nowait", p
))
31642 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31643 else if (!strcmp ("num_gangs", p
))
31644 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31645 else if (!strcmp ("num_tasks", p
))
31646 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31647 else if (!strcmp ("num_teams", p
))
31648 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31649 else if (!strcmp ("num_threads", p
))
31650 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31651 else if (!strcmp ("num_workers", p
))
31652 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31655 if (!strcmp ("ordered", p
))
31656 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31659 if (!strcmp ("parallel", p
))
31660 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31661 else if (!strcmp ("present", p
))
31662 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31663 else if (!strcmp ("present_or_copy", p
)
31664 || !strcmp ("pcopy", p
))
31665 result
= PRAGMA_OACC_CLAUSE_COPY
;
31666 else if (!strcmp ("present_or_copyin", p
)
31667 || !strcmp ("pcopyin", p
))
31668 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31669 else if (!strcmp ("present_or_copyout", p
)
31670 || !strcmp ("pcopyout", p
))
31671 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31672 else if (!strcmp ("present_or_create", p
)
31673 || !strcmp ("pcreate", p
))
31674 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31675 else if (!strcmp ("priority", p
))
31676 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31677 else if (!strcmp ("proc_bind", p
))
31678 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31681 if (!strcmp ("reduction", p
))
31682 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31685 if (!strcmp ("safelen", p
))
31686 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31687 else if (!strcmp ("schedule", p
))
31688 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31689 else if (!strcmp ("sections", p
))
31690 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31691 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31692 result
= PRAGMA_OACC_CLAUSE_HOST
;
31693 else if (!strcmp ("seq", p
))
31694 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31695 else if (!strcmp ("shared", p
))
31696 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31697 else if (!strcmp ("simd", p
))
31698 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31699 else if (!strcmp ("simdlen", p
))
31700 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31703 if (!strcmp ("task_reduction", p
))
31704 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
31705 else if (!strcmp ("taskgroup", p
))
31706 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31707 else if (!strcmp ("thread_limit", p
))
31708 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31709 else if (!strcmp ("threads", p
))
31710 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31711 else if (!strcmp ("tile", p
))
31712 result
= PRAGMA_OACC_CLAUSE_TILE
;
31713 else if (!strcmp ("to", p
))
31714 result
= PRAGMA_OMP_CLAUSE_TO
;
31717 if (!strcmp ("uniform", p
))
31718 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31719 else if (!strcmp ("untied", p
))
31720 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31721 else if (!strcmp ("use_device", p
))
31722 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31723 else if (!strcmp ("use_device_ptr", p
))
31724 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31727 if (!strcmp ("vector", p
))
31728 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31729 else if (!strcmp ("vector_length", p
))
31730 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31733 if (!strcmp ("wait", p
))
31734 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31735 else if (!strcmp ("worker", p
))
31736 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31741 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31742 cp_lexer_consume_token (parser
->lexer
);
31747 /* Validate that a clause of the given type does not already exist. */
31750 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31751 const char *name
, location_t location
)
31755 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31756 if (OMP_CLAUSE_CODE (c
) == code
)
31758 error_at (location
, "too many %qs clauses", name
);
31766 variable-list , identifier
31768 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31769 colon). An opening parenthesis will have been consumed by the caller.
31771 If KIND is nonzero, create the appropriate node and install the decl
31772 in OMP_CLAUSE_DECL and add the node to the head of the list.
31774 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31775 return the list created.
31777 COLON can be NULL if only closing parenthesis should end the list,
31778 or pointer to bool which will receive false if the list is terminated
31779 by closing parenthesis or true if the list is terminated by colon. */
31782 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31783 tree list
, bool *colon
)
31786 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31789 parser
->colon_corrects_to_scope_p
= false;
31796 if (kind
== OMP_CLAUSE_DEPEND
)
31797 cp_parser_parse_tentatively (parser
);
31798 token
= cp_lexer_peek_token (parser
->lexer
);
31800 && current_class_ptr
31801 && cp_parser_is_keyword (token
, RID_THIS
))
31803 decl
= finish_this_expr ();
31804 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31805 || CONVERT_EXPR_P (decl
))
31806 decl
= TREE_OPERAND (decl
, 0);
31807 cp_lexer_consume_token (parser
->lexer
);
31811 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31812 /*check_dependency_p=*/true,
31813 /*template_p=*/NULL
,
31814 /*declarator_p=*/false,
31815 /*optional_p=*/false);
31816 if (name
== error_mark_node
)
31818 if (kind
== OMP_CLAUSE_DEPEND
31819 && cp_parser_simulate_error (parser
))
31820 goto depend_lvalue
;
31824 if (identifier_p (name
))
31825 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31828 if (decl
== error_mark_node
)
31830 if (kind
== OMP_CLAUSE_DEPEND
31831 && cp_parser_simulate_error (parser
))
31832 goto depend_lvalue
;
31833 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31837 if (decl
== error_mark_node
)
31839 else if (kind
!= 0)
31843 case OMP_CLAUSE__CACHE_
:
31844 /* The OpenACC cache directive explicitly only allows "array
31845 elements or subarrays". */
31846 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31848 error_at (token
->location
, "expected %<[%>");
31849 decl
= error_mark_node
;
31853 case OMP_CLAUSE_MAP
:
31854 case OMP_CLAUSE_FROM
:
31855 case OMP_CLAUSE_TO
:
31856 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31859 = cp_lexer_peek_token (parser
->lexer
)->location
;
31860 cp_id_kind idk
= CP_ID_KIND_NONE
;
31861 cp_lexer_consume_token (parser
->lexer
);
31862 decl
= convert_from_reference (decl
);
31864 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31869 case OMP_CLAUSE_DEPEND
:
31870 case OMP_CLAUSE_REDUCTION
:
31871 case OMP_CLAUSE_IN_REDUCTION
:
31872 case OMP_CLAUSE_TASK_REDUCTION
:
31873 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31875 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31877 parser
->colon_corrects_to_scope_p
= false;
31878 cp_lexer_consume_token (parser
->lexer
);
31879 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31880 low_bound
= cp_parser_expression (parser
);
31882 parser
->colon_corrects_to_scope_p
31883 = saved_colon_corrects_to_scope_p
;
31884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31885 length
= integer_one_node
;
31888 /* Look for `:'. */
31889 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31891 if (kind
== OMP_CLAUSE_DEPEND
31892 && cp_parser_simulate_error (parser
))
31893 goto depend_lvalue
;
31896 if (kind
== OMP_CLAUSE_DEPEND
)
31897 cp_parser_commit_to_tentative_parse (parser
);
31898 if (!cp_lexer_next_token_is (parser
->lexer
,
31900 length
= cp_parser_expression (parser
);
31902 /* Look for the closing `]'. */
31903 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31906 if (kind
== OMP_CLAUSE_DEPEND
31907 && cp_parser_simulate_error (parser
))
31908 goto depend_lvalue
;
31912 decl
= tree_cons (low_bound
, length
, decl
);
31919 if (kind
== OMP_CLAUSE_DEPEND
)
31921 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
31922 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
31923 && cp_parser_simulate_error (parser
))
31926 cp_parser_abort_tentative_parse (parser
);
31927 decl
= cp_parser_assignment_expression (parser
, NULL
,
31931 cp_parser_parse_definitely (parser
);
31934 tree u
= build_omp_clause (token
->location
, kind
);
31935 OMP_CLAUSE_DECL (u
) = decl
;
31936 OMP_CLAUSE_CHAIN (u
) = list
;
31940 list
= tree_cons (decl
, NULL_TREE
, list
);
31943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31945 cp_lexer_consume_token (parser
->lexer
);
31949 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31951 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31954 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31958 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31962 /* Try to resync to an unnested comma. Copied from
31963 cp_parser_parenthesized_expression_list. */
31966 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31967 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31968 /*recovering=*/true,
31970 /*consume_paren=*/true);
31978 /* Similarly, but expect leading and trailing parenthesis. This is a very
31979 common case for omp clauses. */
31982 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31984 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31985 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31990 copy ( variable-list )
31991 copyin ( variable-list )
31992 copyout ( variable-list )
31993 create ( variable-list )
31994 delete ( variable-list )
31995 present ( variable-list ) */
31998 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32001 enum gomp_map_kind kind
;
32004 case PRAGMA_OACC_CLAUSE_COPY
:
32005 kind
= GOMP_MAP_TOFROM
;
32007 case PRAGMA_OACC_CLAUSE_COPYIN
:
32008 kind
= GOMP_MAP_TO
;
32010 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32011 kind
= GOMP_MAP_FROM
;
32013 case PRAGMA_OACC_CLAUSE_CREATE
:
32014 kind
= GOMP_MAP_ALLOC
;
32016 case PRAGMA_OACC_CLAUSE_DELETE
:
32017 kind
= GOMP_MAP_RELEASE
;
32019 case PRAGMA_OACC_CLAUSE_DEVICE
:
32020 kind
= GOMP_MAP_FORCE_TO
;
32022 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32023 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32025 case PRAGMA_OACC_CLAUSE_HOST
:
32026 kind
= GOMP_MAP_FORCE_FROM
;
32028 case PRAGMA_OACC_CLAUSE_LINK
:
32029 kind
= GOMP_MAP_LINK
;
32031 case PRAGMA_OACC_CLAUSE_PRESENT
:
32032 kind
= GOMP_MAP_FORCE_PRESENT
;
32035 gcc_unreachable ();
32038 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32040 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32041 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32047 deviceptr ( variable-list ) */
32050 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32052 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32055 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32056 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32057 variable-list must only allow for pointer variables. */
32058 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32059 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32061 tree v
= TREE_PURPOSE (t
);
32062 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32063 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32064 OMP_CLAUSE_DECL (u
) = v
;
32065 OMP_CLAUSE_CHAIN (u
) = list
;
32080 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32081 enum omp_clause_code code
,
32082 tree list
, location_t location
)
32084 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32085 tree c
= build_omp_clause (location
, code
);
32086 OMP_CLAUSE_CHAIN (c
) = list
;
32091 num_gangs ( expression )
32092 num_workers ( expression )
32093 vector_length ( expression ) */
32096 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32097 const char *str
, tree list
)
32099 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32101 matching_parens parens
;
32102 if (!parens
.require_open (parser
))
32105 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32107 if (t
== error_mark_node
32108 || !parens
.require_close (parser
))
32110 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32111 /*or_comma=*/false,
32112 /*consume_paren=*/true);
32116 check_no_duplicate_clause (list
, code
, str
, loc
);
32118 tree c
= build_omp_clause (loc
, code
);
32119 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32120 OMP_CLAUSE_CHAIN (c
) = list
;
32126 gang [( gang-arg-list )]
32127 worker [( [num:] int-expr )]
32128 vector [( [length:] int-expr )]
32130 where gang-arg is one of:
32135 and size-expr may be:
32142 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32143 const char *str
, tree list
)
32145 const char *id
= "num";
32146 cp_lexer
*lexer
= parser
->lexer
;
32147 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32148 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32150 if (kind
== OMP_CLAUSE_VECTOR
)
32153 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32155 matching_parens parens
;
32156 parens
.consume_open (parser
);
32160 cp_token
*next
= cp_lexer_peek_token (lexer
);
32163 /* Gang static argument. */
32164 if (kind
== OMP_CLAUSE_GANG
32165 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32167 cp_lexer_consume_token (lexer
);
32169 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32170 goto cleanup_error
;
32173 if (ops
[idx
] != NULL
)
32175 cp_parser_error (parser
, "too many %<static%> arguments");
32176 goto cleanup_error
;
32179 /* Check for the '*' argument. */
32180 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32181 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32182 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32185 cp_lexer_consume_token (lexer
);
32186 ops
[idx
] = integer_minus_one_node
;
32188 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32190 cp_lexer_consume_token (lexer
);
32196 /* Worker num: argument and vector length: arguments. */
32197 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32198 && id_equal (next
->u
.value
, id
)
32199 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32201 cp_lexer_consume_token (lexer
); /* id */
32202 cp_lexer_consume_token (lexer
); /* ':' */
32205 /* Now collect the actual argument. */
32206 if (ops
[idx
] != NULL_TREE
)
32208 cp_parser_error (parser
, "unexpected argument");
32209 goto cleanup_error
;
32212 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32214 if (expr
== error_mark_node
)
32215 goto cleanup_error
;
32217 mark_exp_read (expr
);
32220 if (kind
== OMP_CLAUSE_GANG
32221 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32223 cp_lexer_consume_token (lexer
);
32230 if (!parens
.require_close (parser
))
32231 goto cleanup_error
;
32234 check_no_duplicate_clause (list
, kind
, str
, loc
);
32236 c
= build_omp_clause (loc
, kind
);
32239 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32241 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32242 OMP_CLAUSE_CHAIN (c
) = list
;
32247 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32252 tile ( size-expr-list ) */
32255 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32257 tree c
, expr
= error_mark_node
;
32258 tree tile
= NULL_TREE
;
32260 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32261 so, but the spec authors never considered such a case and have
32262 differing opinions on what it might mean, including 'not
32264 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32265 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32268 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32273 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32277 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32278 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32280 cp_lexer_consume_token (parser
->lexer
);
32281 expr
= integer_zero_node
;
32284 expr
= cp_parser_constant_expression (parser
);
32286 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32288 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32290 /* Consume the trailing ')'. */
32291 cp_lexer_consume_token (parser
->lexer
);
32293 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32294 tile
= nreverse (tile
);
32295 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32296 OMP_CLAUSE_CHAIN (c
) = list
;
32301 Parse wait clause or directive parameters. */
32304 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32306 vec
<tree
, va_gc
> *args
;
32309 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32311 /*allow_expansion_p=*/true,
32312 /*non_constant_p=*/NULL
);
32314 if (args
== NULL
|| args
->length () == 0)
32316 cp_parser_error (parser
, "expected integer expression before ')'");
32318 release_tree_vector (args
);
32322 args_tree
= build_tree_list_vec (args
);
32324 release_tree_vector (args
);
32326 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32328 tree targ
= TREE_VALUE (t
);
32330 if (targ
!= error_mark_node
)
32332 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32333 error ("%<wait%> expression must be integral");
32336 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32338 targ
= mark_rvalue_use (targ
);
32339 OMP_CLAUSE_DECL (c
) = targ
;
32340 OMP_CLAUSE_CHAIN (c
) = list
;
32350 wait ( int-expr-list ) */
32353 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32355 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32357 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32360 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32366 collapse ( constant-expression ) */
32369 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32375 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32376 matching_parens parens
;
32377 if (!parens
.require_open (parser
))
32380 num
= cp_parser_constant_expression (parser
);
32382 if (!parens
.require_close (parser
))
32383 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32384 /*or_comma=*/false,
32385 /*consume_paren=*/true);
32387 if (num
== error_mark_node
)
32389 num
= fold_non_dependent_expr (num
);
32390 if (!tree_fits_shwi_p (num
)
32391 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32392 || (n
= tree_to_shwi (num
)) <= 0
32395 error_at (loc
, "collapse argument needs positive constant integer expression");
32399 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32400 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32401 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32402 OMP_CLAUSE_CHAIN (c
) = list
;
32403 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32409 default ( none | shared )
32412 default ( none | present ) */
32415 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32416 location_t location
, bool is_oacc
)
32418 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32421 matching_parens parens
;
32422 if (!parens
.require_open (parser
))
32424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32426 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32427 const char *p
= IDENTIFIER_POINTER (id
);
32432 if (strcmp ("none", p
) != 0)
32434 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32438 if (strcmp ("present", p
) != 0 || !is_oacc
)
32440 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32444 if (strcmp ("shared", p
) != 0 || is_oacc
)
32446 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32453 cp_lexer_consume_token (parser
->lexer
);
32459 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32461 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32464 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32465 || !parens
.require_close (parser
))
32466 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32467 /*or_comma=*/false,
32468 /*consume_paren=*/true);
32470 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32473 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32474 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32475 OMP_CLAUSE_CHAIN (c
) = list
;
32476 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32482 final ( expression ) */
32485 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32489 matching_parens parens
;
32490 if (!parens
.require_open (parser
))
32493 t
= cp_parser_assignment_expression (parser
);
32495 if (t
== error_mark_node
32496 || !parens
.require_close (parser
))
32497 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32498 /*or_comma=*/false,
32499 /*consume_paren=*/true);
32501 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32503 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32504 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32505 OMP_CLAUSE_CHAIN (c
) = list
;
32514 if ( directive-name-modifier : expression )
32516 directive-name-modifier:
32517 parallel | task | taskloop | target data | target | target update
32518 | target enter data | target exit data
32521 directive-name-modifier:
32522 ... | simd | cancel */
32525 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32529 enum tree_code if_modifier
= ERROR_MARK
;
32531 matching_parens parens
;
32532 if (!parens
.require_open (parser
))
32535 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32537 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32538 const char *p
= IDENTIFIER_POINTER (id
);
32541 if (strcmp ("cancel", p
) == 0)
32542 if_modifier
= VOID_CST
;
32543 else if (strcmp ("parallel", p
) == 0)
32544 if_modifier
= OMP_PARALLEL
;
32545 else if (strcmp ("simd", p
) == 0)
32546 if_modifier
= OMP_SIMD
;
32547 else if (strcmp ("task", p
) == 0)
32548 if_modifier
= OMP_TASK
;
32549 else if (strcmp ("taskloop", p
) == 0)
32550 if_modifier
= OMP_TASKLOOP
;
32551 else if (strcmp ("target", p
) == 0)
32553 if_modifier
= OMP_TARGET
;
32554 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32556 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32557 p
= IDENTIFIER_POINTER (id
);
32558 if (strcmp ("data", p
) == 0)
32559 if_modifier
= OMP_TARGET_DATA
;
32560 else if (strcmp ("update", p
) == 0)
32561 if_modifier
= OMP_TARGET_UPDATE
;
32562 else if (strcmp ("enter", p
) == 0)
32563 if_modifier
= OMP_TARGET_ENTER_DATA
;
32564 else if (strcmp ("exit", p
) == 0)
32565 if_modifier
= OMP_TARGET_EXIT_DATA
;
32566 if (if_modifier
!= OMP_TARGET
)
32571 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32572 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32574 if_modifier
= ERROR_MARK
;
32576 if (if_modifier
== OMP_TARGET_ENTER_DATA
32577 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32579 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32581 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32582 p
= IDENTIFIER_POINTER (id
);
32583 if (strcmp ("data", p
) == 0)
32589 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32590 error_at (loc
, "expected %<data%>");
32591 if_modifier
= ERROR_MARK
;
32596 if (if_modifier
!= ERROR_MARK
)
32598 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32601 cp_lexer_consume_token (parser
->lexer
);
32608 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32609 error_at (loc
, "expected %<:%>");
32611 if_modifier
= ERROR_MARK
;
32616 t
= cp_parser_assignment_expression (parser
);
32618 if (t
== error_mark_node
32619 || !parens
.require_close (parser
))
32620 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32621 /*or_comma=*/false,
32622 /*consume_paren=*/true);
32624 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32625 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32627 if (if_modifier
!= ERROR_MARK
32628 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32630 const char *p
= NULL
;
32631 switch (if_modifier
)
32633 case VOID_CST
: p
= "cancel"; break;
32634 case OMP_PARALLEL
: p
= "parallel"; break;
32635 case OMP_SIMD
: p
= "simd"; break;
32636 case OMP_TASK
: p
= "task"; break;
32637 case OMP_TASKLOOP
: p
= "taskloop"; break;
32638 case OMP_TARGET_DATA
: p
= "target data"; break;
32639 case OMP_TARGET
: p
= "target"; break;
32640 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32641 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32642 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32643 default: gcc_unreachable ();
32645 error_at (location
, "too many %<if%> clauses with %qs modifier",
32649 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32652 error_at (location
, "too many %<if%> clauses");
32654 error_at (location
, "too many %<if%> clauses without modifier");
32657 else if (if_modifier
== ERROR_MARK
32658 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32660 error_at (location
, "if any %<if%> clause has modifier, then all "
32661 "%<if%> clauses have to use modifier");
32666 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32667 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32668 OMP_CLAUSE_IF_EXPR (c
) = t
;
32669 OMP_CLAUSE_CHAIN (c
) = list
;
32678 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32679 tree list
, location_t location
)
32683 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32686 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32687 OMP_CLAUSE_CHAIN (c
) = list
;
32695 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32696 tree list
, location_t location
)
32700 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32702 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32703 OMP_CLAUSE_CHAIN (c
) = list
;
32708 num_threads ( expression ) */
32711 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32712 location_t location
)
32716 matching_parens parens
;
32717 if (!parens
.require_open (parser
))
32720 t
= cp_parser_assignment_expression (parser
);
32722 if (t
== error_mark_node
32723 || !parens
.require_close (parser
))
32724 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32725 /*or_comma=*/false,
32726 /*consume_paren=*/true);
32728 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32729 "num_threads", location
);
32731 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32732 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32733 OMP_CLAUSE_CHAIN (c
) = list
;
32739 num_tasks ( expression ) */
32742 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32743 location_t location
)
32747 matching_parens parens
;
32748 if (!parens
.require_open (parser
))
32751 t
= cp_parser_assignment_expression (parser
);
32753 if (t
== error_mark_node
32754 || !parens
.require_close (parser
))
32755 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32756 /*or_comma=*/false,
32757 /*consume_paren=*/true);
32759 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32760 "num_tasks", location
);
32762 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32763 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32764 OMP_CLAUSE_CHAIN (c
) = list
;
32770 grainsize ( expression ) */
32773 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32774 location_t location
)
32778 matching_parens parens
;
32779 if (!parens
.require_open (parser
))
32782 t
= cp_parser_assignment_expression (parser
);
32784 if (t
== error_mark_node
32785 || !parens
.require_close (parser
))
32786 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32787 /*or_comma=*/false,
32788 /*consume_paren=*/true);
32790 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32791 "grainsize", location
);
32793 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32794 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32795 OMP_CLAUSE_CHAIN (c
) = list
;
32801 priority ( expression ) */
32804 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32805 location_t location
)
32809 matching_parens parens
;
32810 if (!parens
.require_open (parser
))
32813 t
= cp_parser_assignment_expression (parser
);
32815 if (t
== error_mark_node
32816 || !parens
.require_close (parser
))
32817 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32818 /*or_comma=*/false,
32819 /*consume_paren=*/true);
32821 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32822 "priority", location
);
32824 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32825 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32826 OMP_CLAUSE_CHAIN (c
) = list
;
32832 hint ( expression ) */
32835 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
32839 matching_parens parens
;
32840 if (!parens
.require_open (parser
))
32843 t
= cp_parser_assignment_expression (parser
);
32845 if (t
== error_mark_node
32846 || !parens
.require_close (parser
))
32847 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32848 /*or_comma=*/false,
32849 /*consume_paren=*/true);
32851 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32853 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32854 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32855 OMP_CLAUSE_CHAIN (c
) = list
;
32861 defaultmap ( tofrom : scalar )
32864 defaultmap ( implicit-behavior [ : variable-category ] ) */
32867 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32868 location_t location
)
32872 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
32873 enum omp_clause_defaultmap_kind category
32874 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
32876 matching_parens parens
;
32877 if (!parens
.require_open (parser
))
32880 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32882 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32885 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
32886 "%<tofrom%>, %<firstprivate%>, %<none%> "
32892 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32893 p
= IDENTIFIER_POINTER (id
);
32899 if (strcmp ("alloc", p
) == 0)
32900 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
32902 goto invalid_behavior
;
32906 if (strcmp ("default", p
) == 0)
32907 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
32909 goto invalid_behavior
;
32913 if (strcmp ("firstprivate", p
) == 0)
32914 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
32915 else if (strcmp ("from", p
) == 0)
32916 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
32918 goto invalid_behavior
;
32922 if (strcmp ("none", p
) == 0)
32923 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
32925 goto invalid_behavior
;
32929 if (strcmp ("tofrom", p
) == 0)
32930 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
32931 else if (strcmp ("to", p
) == 0)
32932 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
32934 goto invalid_behavior
;
32938 goto invalid_behavior
;
32940 cp_lexer_consume_token (parser
->lexer
);
32942 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
32944 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32947 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32950 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
32954 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32955 p
= IDENTIFIER_POINTER (id
);
32960 if (strcmp ("aggregate", p
) == 0)
32961 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
32963 goto invalid_category
;
32967 if (strcmp ("pointer", p
) == 0)
32968 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
32970 goto invalid_category
;
32974 if (strcmp ("scalar", p
) == 0)
32975 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
32977 goto invalid_category
;
32981 goto invalid_category
;
32984 cp_lexer_consume_token (parser
->lexer
);
32986 if (!parens
.require_close (parser
))
32989 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32990 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
32991 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
32992 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
32993 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
32994 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
32996 enum omp_clause_defaultmap_kind cat
= category
;
32997 location_t loc
= OMP_CLAUSE_LOCATION (c
);
32998 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
32999 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33003 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33006 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33009 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33012 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33016 gcc_unreachable ();
33019 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33022 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33027 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33028 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33029 OMP_CLAUSE_CHAIN (c
) = list
;
33033 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33034 /*or_comma=*/false,
33035 /*consume_paren=*/true);
33043 ordered ( constant-expression ) */
33046 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33047 tree list
, location_t location
)
33049 tree c
, num
= NULL_TREE
;
33052 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33053 "ordered", location
);
33055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33057 matching_parens parens
;
33058 parens
.consume_open (parser
);
33060 num
= cp_parser_constant_expression (parser
);
33062 if (!parens
.require_close (parser
))
33063 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33064 /*or_comma=*/false,
33065 /*consume_paren=*/true);
33067 if (num
== error_mark_node
)
33069 num
= fold_non_dependent_expr (num
);
33070 if (!tree_fits_shwi_p (num
)
33071 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33072 || (n
= tree_to_shwi (num
)) <= 0
33075 error_at (location
,
33076 "ordered argument needs positive constant integer "
33082 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33083 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33084 OMP_CLAUSE_CHAIN (c
) = list
;
33089 reduction ( reduction-operator : variable-list )
33091 reduction-operator:
33092 One of: + * - & ^ | && ||
33096 reduction-operator:
33097 One of: + * - & ^ | && || min max
33101 reduction-operator:
33102 One of: + * - & ^ | && ||
33106 reduction ( reduction-modifier, reduction-operator : variable-list )
33107 in_reduction ( reduction-operator : variable-list )
33108 task_reduction ( reduction-operator : variable-list ) */
33111 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33112 bool is_omp
, tree list
)
33114 enum tree_code code
= ERROR_MARK
;
33115 tree nlist
, c
, id
= NULL_TREE
;
33117 bool inscan
= false;
33119 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33122 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33124 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33125 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33127 cp_lexer_consume_token (parser
->lexer
);
33128 cp_lexer_consume_token (parser
->lexer
);
33130 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33131 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33133 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33134 const char *p
= IDENTIFIER_POINTER (id
);
33135 if (strcmp (p
, "task") == 0)
33137 else if (strcmp (p
, "inscan") == 0)
33140 sorry ("%<inscan%> modifier on %<reduction%> clause "
33141 "not supported yet");
33143 if (task
|| inscan
)
33145 cp_lexer_consume_token (parser
->lexer
);
33146 cp_lexer_consume_token (parser
->lexer
);
33151 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33153 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33154 case CPP_MULT
: code
= MULT_EXPR
; break;
33155 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33156 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33157 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33158 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33159 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33160 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33164 if (code
!= ERROR_MARK
)
33165 cp_lexer_consume_token (parser
->lexer
);
33168 bool saved_colon_corrects_to_scope_p
;
33169 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33170 parser
->colon_corrects_to_scope_p
= false;
33171 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33172 /*check_dependency_p=*/true,
33173 /*template_p=*/NULL
,
33174 /*declarator_p=*/false,
33175 /*optional_p=*/false);
33176 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33177 if (identifier_p (id
))
33179 const char *p
= IDENTIFIER_POINTER (id
);
33181 if (strcmp (p
, "min") == 0)
33183 else if (strcmp (p
, "max") == 0)
33185 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33187 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33189 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33191 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33192 code
= BIT_AND_EXPR
;
33193 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33194 code
= BIT_IOR_EXPR
;
33195 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33196 code
= BIT_XOR_EXPR
;
33197 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33198 code
= TRUTH_ANDIF_EXPR
;
33199 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33200 code
= TRUTH_ORIF_EXPR
;
33201 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33202 tree scope
= parser
->scope
;
33204 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33205 parser
->scope
= NULL_TREE
;
33206 parser
->qualifying_scope
= NULL_TREE
;
33207 parser
->object_scope
= NULL_TREE
;
33211 error ("invalid reduction-identifier");
33213 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33214 /*or_comma=*/false,
33215 /*consume_paren=*/true);
33220 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33223 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33225 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33227 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33229 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33231 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33232 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33239 schedule ( schedule-kind )
33240 schedule ( schedule-kind , expression )
33243 static | dynamic | guided | runtime | auto
33246 schedule ( schedule-modifier : schedule-kind )
33247 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33255 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33258 int modifiers
= 0, nmodifiers
= 0;
33260 matching_parens parens
;
33261 if (!parens
.require_open (parser
))
33264 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33266 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33268 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33269 const char *p
= IDENTIFIER_POINTER (id
);
33270 if (strcmp ("simd", p
) == 0)
33271 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33272 else if (strcmp ("monotonic", p
) == 0)
33273 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33274 else if (strcmp ("nonmonotonic", p
) == 0)
33275 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33278 cp_lexer_consume_token (parser
->lexer
);
33279 if (nmodifiers
++ == 0
33280 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33281 cp_lexer_consume_token (parser
->lexer
);
33284 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33291 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33292 const char *p
= IDENTIFIER_POINTER (id
);
33297 if (strcmp ("dynamic", p
) != 0)
33299 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33303 if (strcmp ("guided", p
) != 0)
33305 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33309 if (strcmp ("runtime", p
) != 0)
33311 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33318 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33319 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33320 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33321 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33324 cp_lexer_consume_token (parser
->lexer
);
33326 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33327 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33328 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33329 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33331 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33339 cp_lexer_consume_token (parser
->lexer
);
33341 token
= cp_lexer_peek_token (parser
->lexer
);
33342 t
= cp_parser_assignment_expression (parser
);
33344 if (t
== error_mark_node
)
33346 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33347 error_at (token
->location
, "schedule %<runtime%> does not take "
33348 "a %<chunk_size%> parameter");
33349 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33350 error_at (token
->location
, "schedule %<auto%> does not take "
33351 "a %<chunk_size%> parameter");
33353 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33355 if (!parens
.require_close (parser
))
33358 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33361 OMP_CLAUSE_SCHEDULE_KIND (c
)
33362 = (enum omp_clause_schedule_kind
)
33363 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33365 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33366 OMP_CLAUSE_CHAIN (c
) = list
;
33370 cp_parser_error (parser
, "invalid schedule kind");
33372 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33373 /*or_comma=*/false,
33374 /*consume_paren=*/true);
33382 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33383 tree list
, location_t location
)
33387 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33389 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33390 OMP_CLAUSE_CHAIN (c
) = list
;
33399 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33400 tree list
, location_t location
)
33402 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33403 tree c
= build_omp_clause (location
, code
);
33404 OMP_CLAUSE_CHAIN (c
) = list
;
33415 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33416 enum omp_clause_code code
,
33417 tree list
, location_t location
)
33419 tree c
= build_omp_clause (location
, code
);
33420 OMP_CLAUSE_CHAIN (c
) = list
;
33428 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33429 tree list
, location_t location
)
33431 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33432 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33433 OMP_CLAUSE_CHAIN (c
) = list
;
33442 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33443 enum omp_clause_code code
,
33444 tree list
, location_t location
)
33446 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33447 tree c
= build_omp_clause (location
, code
);
33448 OMP_CLAUSE_CHAIN (c
) = list
;
33453 num_teams ( expression ) */
33456 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33457 location_t location
)
33461 matching_parens parens
;
33462 if (!parens
.require_open (parser
))
33465 t
= cp_parser_assignment_expression (parser
);
33467 if (t
== error_mark_node
33468 || !parens
.require_close (parser
))
33469 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33470 /*or_comma=*/false,
33471 /*consume_paren=*/true);
33473 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33474 "num_teams", location
);
33476 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33477 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33478 OMP_CLAUSE_CHAIN (c
) = list
;
33484 thread_limit ( expression ) */
33487 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33488 location_t location
)
33492 matching_parens parens
;
33493 if (!parens
.require_open (parser
))
33496 t
= cp_parser_assignment_expression (parser
);
33498 if (t
== error_mark_node
33499 || !parens
.require_close (parser
))
33500 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33501 /*or_comma=*/false,
33502 /*consume_paren=*/true);
33504 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33505 "thread_limit", location
);
33507 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33508 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33509 OMP_CLAUSE_CHAIN (c
) = list
;
33515 aligned ( variable-list )
33516 aligned ( variable-list : constant-expression ) */
33519 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33521 tree nlist
, c
, alignment
= NULL_TREE
;
33524 matching_parens parens
;
33525 if (!parens
.require_open (parser
))
33528 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33533 alignment
= cp_parser_constant_expression (parser
);
33535 if (!parens
.require_close (parser
))
33536 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33537 /*or_comma=*/false,
33538 /*consume_paren=*/true);
33540 if (alignment
== error_mark_node
)
33541 alignment
= NULL_TREE
;
33544 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33545 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33551 lastprivate ( variable-list )
33554 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33557 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
33559 bool conditional
= false;
33561 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33565 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
33567 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33568 const char *p
= IDENTIFIER_POINTER (id
);
33570 if (strcmp ("conditional", p
) == 0)
33572 conditional
= true;
33573 cp_lexer_consume_token (parser
->lexer
);
33574 cp_lexer_consume_token (parser
->lexer
);
33578 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
33582 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33583 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
33588 linear ( variable-list )
33589 linear ( variable-list : expression )
33592 linear ( modifier ( variable-list ) )
33593 linear ( modifier ( variable-list ) : expression ) */
33596 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33599 tree nlist
, c
, step
= integer_one_node
;
33601 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33603 matching_parens parens
;
33604 if (!parens
.require_open (parser
))
33607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33609 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33610 const char *p
= IDENTIFIER_POINTER (id
);
33612 if (strcmp ("ref", p
) == 0)
33613 kind
= OMP_CLAUSE_LINEAR_REF
;
33614 else if (strcmp ("val", p
) == 0)
33615 kind
= OMP_CLAUSE_LINEAR_VAL
;
33616 else if (strcmp ("uval", p
) == 0)
33617 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33618 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33619 cp_lexer_consume_token (parser
->lexer
);
33621 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33624 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33625 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33629 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33630 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33632 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33633 else if (!parens
.require_close (parser
))
33634 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33635 /*or_comma=*/false,
33636 /*consume_paren=*/true);
33643 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33644 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33646 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33647 cp_parser_parse_tentatively (parser
);
33648 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33649 /*check_dependency_p=*/true,
33650 /*template_p=*/NULL
,
33651 /*declarator_p=*/false,
33652 /*optional_p=*/false);
33653 if (step
!= error_mark_node
)
33654 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33655 if (step
== error_mark_node
)
33658 cp_parser_abort_tentative_parse (parser
);
33660 else if (!cp_parser_parse_definitely (parser
))
33664 step
= cp_parser_assignment_expression (parser
);
33666 if (!parens
.require_close (parser
))
33667 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33668 /*or_comma=*/false,
33669 /*consume_paren=*/true);
33671 if (step
== error_mark_node
)
33675 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33677 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33678 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33685 safelen ( constant-expression ) */
33688 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33689 location_t location
)
33693 matching_parens parens
;
33694 if (!parens
.require_open (parser
))
33697 t
= cp_parser_constant_expression (parser
);
33699 if (t
== error_mark_node
33700 || !parens
.require_close (parser
))
33701 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33702 /*or_comma=*/false,
33703 /*consume_paren=*/true);
33705 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33707 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33708 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33709 OMP_CLAUSE_CHAIN (c
) = list
;
33715 simdlen ( constant-expression ) */
33718 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33719 location_t location
)
33723 matching_parens parens
;
33724 if (!parens
.require_open (parser
))
33727 t
= cp_parser_constant_expression (parser
);
33729 if (t
== error_mark_node
33730 || !parens
.require_close (parser
))
33731 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33732 /*or_comma=*/false,
33733 /*consume_paren=*/true);
33735 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33737 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33738 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33739 OMP_CLAUSE_CHAIN (c
) = list
;
33746 identifier [+/- integer]
33747 vec , identifier [+/- integer]
33751 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33756 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33758 cp_parser_error (parser
, "expected identifier");
33762 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33764 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33765 tree t
, identifier
= cp_parser_identifier (parser
);
33766 tree addend
= NULL
;
33768 if (identifier
== error_mark_node
)
33769 t
= error_mark_node
;
33772 t
= cp_parser_lookup_name_simple
33773 (parser
, identifier
,
33774 cp_lexer_peek_token (parser
->lexer
)->location
);
33775 if (t
== error_mark_node
)
33776 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33783 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33785 addend
= integer_zero_node
;
33786 goto add_to_vector
;
33788 cp_lexer_consume_token (parser
->lexer
);
33790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33792 cp_parser_error (parser
, "expected integer");
33796 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33797 if (TREE_CODE (addend
) != INTEGER_CST
)
33799 cp_parser_error (parser
, "expected integer");
33802 cp_lexer_consume_token (parser
->lexer
);
33805 if (t
!= error_mark_node
)
33807 vec
= tree_cons (addend
, t
, vec
);
33809 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33812 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33815 cp_lexer_consume_token (parser
->lexer
);
33818 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33820 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33821 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33822 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33823 OMP_CLAUSE_CHAIN (u
) = list
;
33830 iterators ( iterators-definition )
33832 iterators-definition:
33834 iterator-specifier , iterators-definition
33836 iterator-specifier:
33837 identifier = range-specification
33838 iterator-type identifier = range-specification
33840 range-specification:
33842 begin : end : step */
33845 cp_parser_omp_iterators (cp_parser
*parser
)
33847 tree ret
= NULL_TREE
, *last
= &ret
;
33848 cp_lexer_consume_token (parser
->lexer
);
33850 matching_parens parens
;
33851 if (!parens
.require_open (parser
))
33852 return error_mark_node
;
33854 bool saved_colon_corrects_to_scope_p
33855 = parser
->colon_corrects_to_scope_p
;
33856 bool saved_colon_doesnt_start_class_def_p
33857 = parser
->colon_doesnt_start_class_def_p
;
33862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33863 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
33864 iter_type
= integer_type_node
;
33867 const char *saved_message
33868 = parser
->type_definition_forbidden_message
;
33869 parser
->type_definition_forbidden_message
33870 = G_("types may not be defined in iterator type");
33872 iter_type
= cp_parser_type_id (parser
);
33874 parser
->type_definition_forbidden_message
= saved_message
;
33877 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33878 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33880 cp_parser_error (parser
, "expected identifier");
33884 tree id
= cp_parser_identifier (parser
);
33885 if (id
== error_mark_node
)
33888 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33891 parser
->colon_corrects_to_scope_p
= false;
33892 parser
->colon_doesnt_start_class_def_p
= true;
33893 tree begin
= cp_parser_assignment_expression (parser
);
33895 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33898 tree end
= cp_parser_assignment_expression (parser
);
33900 tree step
= integer_one_node
;
33901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
33903 cp_lexer_consume_token (parser
->lexer
);
33904 step
= cp_parser_assignment_expression (parser
);
33907 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
33908 DECL_ARTIFICIAL (iter_var
) = 1;
33909 DECL_CONTEXT (iter_var
) = current_function_decl
;
33910 pushdecl (iter_var
);
33912 *last
= make_tree_vec (6);
33913 TREE_VEC_ELT (*last
, 0) = iter_var
;
33914 TREE_VEC_ELT (*last
, 1) = begin
;
33915 TREE_VEC_ELT (*last
, 2) = end
;
33916 TREE_VEC_ELT (*last
, 3) = step
;
33917 last
= &TREE_CHAIN (*last
);
33919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33921 cp_lexer_consume_token (parser
->lexer
);
33928 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33929 parser
->colon_doesnt_start_class_def_p
33930 = saved_colon_doesnt_start_class_def_p
;
33932 if (!parens
.require_close (parser
))
33933 cp_parser_skip_to_closing_parenthesis (parser
,
33934 /*recovering=*/true,
33935 /*or_comma=*/false,
33936 /*consume_paren=*/true);
33938 return ret
? ret
: error_mark_node
;
33942 depend ( depend-kind : variable-list )
33950 depend ( sink : vec )
33953 depend ( depend-modifier , depend-kind: variable-list )
33956 in | out | inout | mutexinoutset | depobj
33959 iterator ( iterators-definition ) */
33962 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33964 tree nlist
, c
, iterators
= NULL_TREE
;
33965 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
33967 matching_parens parens
;
33968 if (!parens
.require_open (parser
))
33973 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33976 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33977 const char *p
= IDENTIFIER_POINTER (id
);
33979 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
33981 begin_scope (sk_omp
, NULL
);
33982 iterators
= cp_parser_omp_iterators (parser
);
33983 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
33986 if (strcmp ("in", p
) == 0)
33987 kind
= OMP_CLAUSE_DEPEND_IN
;
33988 else if (strcmp ("inout", p
) == 0)
33989 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33990 else if (strcmp ("mutexinoutset", p
) == 0)
33991 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
33992 else if (strcmp ("out", p
) == 0)
33993 kind
= OMP_CLAUSE_DEPEND_OUT
;
33994 else if (strcmp ("depobj", p
) == 0)
33995 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
33996 else if (strcmp ("sink", p
) == 0)
33997 kind
= OMP_CLAUSE_DEPEND_SINK
;
33998 else if (strcmp ("source", p
) == 0)
33999 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34006 cp_lexer_consume_token (parser
->lexer
);
34009 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34011 poplevel (0, 1, 0);
34012 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34013 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34014 iterators
= NULL_TREE
;
34017 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34019 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34020 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34021 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34022 OMP_CLAUSE_CHAIN (c
) = list
;
34023 if (!parens
.require_close (parser
))
34024 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34025 /*or_comma=*/false,
34026 /*consume_paren=*/true);
34030 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34033 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34034 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34037 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34042 tree block
= poplevel (1, 1, 0);
34043 if (iterators
== error_mark_node
)
34044 iterators
= NULL_TREE
;
34046 TREE_VEC_ELT (iterators
, 5) = block
;
34049 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34051 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34053 OMP_CLAUSE_DECL (c
)
34054 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34060 cp_parser_error (parser
, "invalid depend kind");
34063 poplevel (0, 1, 0);
34064 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34065 /*or_comma=*/false,
34066 /*consume_paren=*/true);
34071 map ( map-kind : variable-list )
34072 map ( variable-list )
34075 alloc | to | from | tofrom
34079 alloc | to | from | tofrom | release | delete
34081 map ( always [,] map-kind: variable-list ) */
34084 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34087 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34088 bool always
= false;
34090 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34095 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34096 const char *p
= IDENTIFIER_POINTER (id
);
34098 if (strcmp ("always", p
) == 0)
34101 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34103 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34104 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34106 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34110 cp_lexer_consume_token (parser
->lexer
);
34112 cp_lexer_consume_token (parser
->lexer
);
34117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34118 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34120 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34121 const char *p
= IDENTIFIER_POINTER (id
);
34123 if (strcmp ("alloc", p
) == 0)
34124 kind
= GOMP_MAP_ALLOC
;
34125 else if (strcmp ("to", p
) == 0)
34126 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34127 else if (strcmp ("from", p
) == 0)
34128 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34129 else if (strcmp ("tofrom", p
) == 0)
34130 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34131 else if (strcmp ("release", p
) == 0)
34132 kind
= GOMP_MAP_RELEASE
;
34135 cp_parser_error (parser
, "invalid map kind");
34136 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34137 /*or_comma=*/false,
34138 /*consume_paren=*/true);
34141 cp_lexer_consume_token (parser
->lexer
);
34142 cp_lexer_consume_token (parser
->lexer
);
34144 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34145 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34147 kind
= GOMP_MAP_DELETE
;
34148 cp_lexer_consume_token (parser
->lexer
);
34149 cp_lexer_consume_token (parser
->lexer
);
34152 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34155 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34156 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34162 device ( expression ) */
34165 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34166 location_t location
)
34170 matching_parens parens
;
34171 if (!parens
.require_open (parser
))
34174 t
= cp_parser_assignment_expression (parser
);
34176 if (t
== error_mark_node
34177 || !parens
.require_close (parser
))
34178 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34179 /*or_comma=*/false,
34180 /*consume_paren=*/true);
34182 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34183 "device", location
);
34185 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34186 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34187 OMP_CLAUSE_CHAIN (c
) = list
;
34193 dist_schedule ( static )
34194 dist_schedule ( static , expression ) */
34197 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34198 location_t location
)
34202 matching_parens parens
;
34203 if (!parens
.require_open (parser
))
34206 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34208 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34210 cp_lexer_consume_token (parser
->lexer
);
34212 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34214 cp_lexer_consume_token (parser
->lexer
);
34216 t
= cp_parser_assignment_expression (parser
);
34218 if (t
== error_mark_node
)
34220 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34222 if (!parens
.require_close (parser
))
34225 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34228 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34230 OMP_CLAUSE_CHAIN (c
) = list
;
34234 cp_parser_error (parser
, "invalid dist_schedule kind");
34236 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34237 /*or_comma=*/false,
34238 /*consume_paren=*/true);
34243 proc_bind ( proc-bind-kind )
34246 master | close | spread */
34249 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34250 location_t location
)
34253 enum omp_clause_proc_bind_kind kind
;
34255 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34260 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34261 const char *p
= IDENTIFIER_POINTER (id
);
34263 if (strcmp ("master", p
) == 0)
34264 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34265 else if (strcmp ("close", p
) == 0)
34266 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34267 else if (strcmp ("spread", p
) == 0)
34268 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34275 cp_lexer_consume_token (parser
->lexer
);
34276 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34279 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34280 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34282 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34283 OMP_CLAUSE_CHAIN (c
) = list
;
34287 cp_parser_error (parser
, "invalid depend kind");
34289 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34290 /*or_comma=*/false,
34291 /*consume_paren=*/true);
34296 async [( int-expr )] */
34299 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34302 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34304 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34306 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34308 matching_parens parens
;
34309 parens
.consume_open (parser
);
34311 t
= cp_parser_expression (parser
);
34312 if (t
== error_mark_node
34313 || !parens
.require_close (parser
))
34314 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34315 /*or_comma=*/false,
34316 /*consume_paren=*/true);
34319 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34321 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34322 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34323 OMP_CLAUSE_CHAIN (c
) = list
;
34329 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34330 is a bitmask in MASK. Return the list of clauses found. */
34333 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34334 const char *where
, cp_token
*pragma_tok
,
34335 bool finish_p
= true)
34337 tree clauses
= NULL
;
34340 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34343 pragma_omp_clause c_kind
;
34344 omp_clause_code code
;
34345 const char *c_name
;
34346 tree prev
= clauses
;
34348 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34349 cp_lexer_consume_token (parser
->lexer
);
34351 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34352 c_kind
= cp_parser_omp_clause_name (parser
);
34356 case PRAGMA_OACC_CLAUSE_ASYNC
:
34357 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34360 case PRAGMA_OACC_CLAUSE_AUTO
:
34361 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34365 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34366 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34367 c_name
= "collapse";
34369 case PRAGMA_OACC_CLAUSE_COPY
:
34370 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34373 case PRAGMA_OACC_CLAUSE_COPYIN
:
34374 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34377 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34378 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34379 c_name
= "copyout";
34381 case PRAGMA_OACC_CLAUSE_CREATE
:
34382 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34385 case PRAGMA_OACC_CLAUSE_DELETE
:
34386 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34389 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34390 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34391 c_name
= "default";
34393 case PRAGMA_OACC_CLAUSE_DEVICE
:
34394 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34397 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34398 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34399 c_name
= "deviceptr";
34401 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34402 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34403 c_name
= "device_resident";
34405 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34406 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34408 c_name
= "finalize";
34410 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34411 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34413 c_name
= "firstprivate";
34415 case PRAGMA_OACC_CLAUSE_GANG
:
34417 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34420 case PRAGMA_OACC_CLAUSE_HOST
:
34421 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34424 case PRAGMA_OACC_CLAUSE_IF
:
34425 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34428 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34429 clauses
= cp_parser_oacc_simple_clause (parser
,
34430 OMP_CLAUSE_IF_PRESENT
,
34432 c_name
= "if_present";
34434 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34435 clauses
= cp_parser_oacc_simple_clause (parser
,
34436 OMP_CLAUSE_INDEPENDENT
,
34438 c_name
= "independent";
34440 case PRAGMA_OACC_CLAUSE_LINK
:
34441 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34444 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34445 code
= OMP_CLAUSE_NUM_GANGS
;
34446 c_name
= "num_gangs";
34447 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34450 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34451 c_name
= "num_workers";
34452 code
= OMP_CLAUSE_NUM_WORKERS
;
34453 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34456 case PRAGMA_OACC_CLAUSE_PRESENT
:
34457 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34458 c_name
= "present";
34460 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34461 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34463 c_name
= "private";
34465 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34467 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34469 c_name
= "reduction";
34471 case PRAGMA_OACC_CLAUSE_SEQ
:
34472 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34476 case PRAGMA_OACC_CLAUSE_TILE
:
34477 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34480 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34481 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34483 c_name
= "use_device";
34485 case PRAGMA_OACC_CLAUSE_VECTOR
:
34487 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34490 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34491 c_name
= "vector_length";
34492 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34493 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34496 case PRAGMA_OACC_CLAUSE_WAIT
:
34497 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34500 case PRAGMA_OACC_CLAUSE_WORKER
:
34502 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34506 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34512 if (((mask
>> c_kind
) & 1) == 0)
34514 /* Remove the invalid clause(s) from the list to avoid
34515 confusing the rest of the compiler. */
34517 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34522 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34525 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34530 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34531 is a bitmask in MASK. Return the list of clauses found; the result
34532 of clause default goes in *pdefault. */
34535 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34536 const char *where
, cp_token
*pragma_tok
,
34537 bool finish_p
= true)
34539 tree clauses
= NULL
;
34541 cp_token
*token
= NULL
;
34543 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34545 pragma_omp_clause c_kind
;
34546 const char *c_name
;
34547 tree prev
= clauses
;
34549 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34550 cp_lexer_consume_token (parser
->lexer
);
34552 token
= cp_lexer_peek_token (parser
->lexer
);
34553 c_kind
= cp_parser_omp_clause_name (parser
);
34557 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
34558 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
34560 c_name
= "collapse";
34562 case PRAGMA_OMP_CLAUSE_COPYIN
:
34563 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
34566 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
34567 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
34569 c_name
= "copyprivate";
34571 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34572 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
34573 token
->location
, false);
34574 c_name
= "default";
34576 case PRAGMA_OMP_CLAUSE_FINAL
:
34577 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
34580 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
34581 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34583 c_name
= "firstprivate";
34585 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34586 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34588 c_name
= "grainsize";
34590 case PRAGMA_OMP_CLAUSE_HINT
:
34591 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34595 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34596 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34598 c_name
= "defaultmap";
34600 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34601 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34603 c_name
= "use_device_ptr";
34605 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34606 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34608 c_name
= "is_device_ptr";
34610 case PRAGMA_OMP_CLAUSE_IF
:
34611 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34615 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
34617 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
34619 c_name
= "in_reduction";
34621 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34622 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
34623 c_name
= "lastprivate";
34625 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34626 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34628 c_name
= "mergeable";
34630 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34631 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34634 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34635 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34637 c_name
= "num_tasks";
34639 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34640 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34642 c_name
= "num_threads";
34644 case PRAGMA_OMP_CLAUSE_ORDERED
:
34645 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34647 c_name
= "ordered";
34649 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34650 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34652 c_name
= "priority";
34654 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34655 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34657 c_name
= "private";
34659 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34661 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34663 c_name
= "reduction";
34665 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34666 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34668 c_name
= "schedule";
34670 case PRAGMA_OMP_CLAUSE_SHARED
:
34671 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34675 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
34677 = cp_parser_omp_clause_reduction (parser
,
34678 OMP_CLAUSE_TASK_REDUCTION
,
34680 c_name
= "task_reduction";
34682 case PRAGMA_OMP_CLAUSE_UNTIED
:
34683 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34687 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34688 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34689 clauses
, token
->location
);
34690 c_name
= "inbranch";
34692 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
34693 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
34695 c_name
= "nontemporal";
34697 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34698 clauses
= cp_parser_omp_clause_branch (parser
,
34699 OMP_CLAUSE_NOTINBRANCH
,
34700 clauses
, token
->location
);
34701 c_name
= "notinbranch";
34703 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34704 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34705 clauses
, token
->location
);
34706 c_name
= "parallel";
34710 error_at (token
->location
, "%qs must be the first clause of %qs",
34715 case PRAGMA_OMP_CLAUSE_FOR
:
34716 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34717 clauses
, token
->location
);
34720 goto clause_not_first
;
34722 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34723 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34724 clauses
, token
->location
);
34725 c_name
= "sections";
34727 goto clause_not_first
;
34729 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34730 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34731 clauses
, token
->location
);
34732 c_name
= "taskgroup";
34734 goto clause_not_first
;
34736 case PRAGMA_OMP_CLAUSE_LINK
:
34737 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34740 case PRAGMA_OMP_CLAUSE_TO
:
34741 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34742 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34745 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34748 case PRAGMA_OMP_CLAUSE_FROM
:
34749 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34752 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34753 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34755 c_name
= "uniform";
34757 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34758 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34760 c_name
= "num_teams";
34762 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34763 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34765 c_name
= "thread_limit";
34767 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34768 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34769 c_name
= "aligned";
34771 case PRAGMA_OMP_CLAUSE_LINEAR
:
34773 bool declare_simd
= false;
34774 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34775 declare_simd
= true;
34776 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34780 case PRAGMA_OMP_CLAUSE_DEPEND
:
34781 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34785 case PRAGMA_OMP_CLAUSE_MAP
:
34786 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34789 case PRAGMA_OMP_CLAUSE_DEVICE
:
34790 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34794 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34795 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34797 c_name
= "dist_schedule";
34799 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34800 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34802 c_name
= "proc_bind";
34804 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34805 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34807 c_name
= "safelen";
34809 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34810 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34812 c_name
= "simdlen";
34814 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34815 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34817 c_name
= "nogroup";
34819 case PRAGMA_OMP_CLAUSE_THREADS
:
34821 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34822 clauses
, token
->location
);
34823 c_name
= "threads";
34825 case PRAGMA_OMP_CLAUSE_SIMD
:
34827 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34828 clauses
, token
->location
);
34832 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34838 if (((mask
>> c_kind
) & 1) == 0)
34840 /* Remove the invalid clause(s) from the list to avoid
34841 confusing the rest of the compiler. */
34843 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34847 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34850 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34851 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34853 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34862 In practice, we're also interested in adding the statement to an
34863 outer node. So it is convenient if we work around the fact that
34864 cp_parser_statement calls add_stmt. */
34867 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34869 unsigned save
= parser
->in_statement
;
34871 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34872 This preserves the "not within loop or switch" style error messages
34873 for nonsense cases like
34879 if (parser
->in_statement
)
34880 parser
->in_statement
= IN_OMP_BLOCK
;
34886 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34888 parser
->in_statement
= save
;
34892 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34894 tree stmt
= begin_omp_structured_block ();
34895 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34897 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34899 cp_parser_end_omp_structured_block (parser
, save
);
34900 return finish_omp_structured_block (stmt
);
34904 # pragma omp atomic new-line
34908 x binop= expr | x++ | ++x | x-- | --x
34910 +, *, -, /, &, ^, |, <<, >>
34912 where x is an lvalue expression with scalar type.
34915 # pragma omp atomic new-line
34918 # pragma omp atomic read new-line
34921 # pragma omp atomic write new-line
34924 # pragma omp atomic update new-line
34927 # pragma omp atomic capture new-line
34930 # pragma omp atomic capture new-line
34938 expression-stmt | x = x binop expr
34940 v = expression-stmt
34942 { v = x; update-stmt; } | { update-stmt; v = x; }
34946 expression-stmt | x = x binop expr | x = expr binop x
34950 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34952 where x and v are lvalue expressions with scalar type. */
34955 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34957 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34958 tree rhs1
= NULL_TREE
, orig_lhs
;
34959 location_t loc
= pragma_tok
->location
;
34960 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
34961 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
34962 bool structured_block
= false;
34964 tree clauses
= NULL_TREE
;
34966 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34968 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34969 cp_lexer_consume_token (parser
->lexer
);
34973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34976 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34977 const char *p
= IDENTIFIER_POINTER (id
);
34978 enum tree_code new_code
= ERROR_MARK
;
34979 enum omp_memory_order new_memory_order
34980 = OMP_MEMORY_ORDER_UNSPECIFIED
;
34982 if (!strcmp (p
, "read"))
34983 new_code
= OMP_ATOMIC_READ
;
34984 else if (!strcmp (p
, "write"))
34985 new_code
= NOP_EXPR
;
34986 else if (!strcmp (p
, "update"))
34987 new_code
= OMP_ATOMIC
;
34988 else if (!strcmp (p
, "capture"))
34989 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
34990 else if (!strcmp (p
, "seq_cst"))
34991 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
34992 else if (!strcmp (p
, "acq_rel"))
34993 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
34994 else if (!strcmp (p
, "release"))
34995 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
34996 else if (!strcmp (p
, "acquire"))
34997 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
34998 else if (!strcmp (p
, "relaxed"))
34999 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35000 else if (!strcmp (p
, "hint"))
35002 cp_lexer_consume_token (parser
->lexer
);
35003 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35009 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35010 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35011 "%<release%>, %<relaxed%> or %<hint%> clause");
35015 if (new_code
!= ERROR_MARK
)
35017 if (code
!= ERROR_MARK
)
35018 error_at (cloc
, "too many atomic clauses");
35022 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35024 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35025 error_at (cloc
, "too many memory order clauses");
35027 memory_order
= new_memory_order
;
35029 cp_lexer_consume_token (parser
->lexer
);
35035 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35037 if (code
== ERROR_MARK
)
35039 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35042 = (enum omp_requires
) (omp_requires_mask
35043 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35044 switch ((enum omp_memory_order
)
35045 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35047 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35048 case OMP_MEMORY_ORDER_RELAXED
:
35049 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35051 case OMP_MEMORY_ORDER_SEQ_CST
:
35052 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35054 case OMP_MEMORY_ORDER_ACQ_REL
:
35057 case OMP_ATOMIC_READ
:
35058 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35060 case NOP_EXPR
: /* atomic write */
35062 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35065 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35070 gcc_unreachable ();
35076 case OMP_ATOMIC_READ
:
35077 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35078 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35080 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35081 "%<acq_rel%> or %<release%> clauses");
35082 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35085 case NOP_EXPR
: /* atomic write */
35086 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35087 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35089 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35090 "%<acq_rel%> or %<acquire%> clauses");
35091 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35095 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35096 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35098 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35099 "%<acq_rel%> or %<acquire%> clauses");
35100 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35109 case OMP_ATOMIC_READ
:
35110 case NOP_EXPR
: /* atomic write */
35111 v
= cp_parser_unary_expression (parser
);
35112 if (v
== error_mark_node
)
35114 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35116 if (code
== NOP_EXPR
)
35117 lhs
= cp_parser_expression (parser
);
35119 lhs
= cp_parser_unary_expression (parser
);
35120 if (lhs
== error_mark_node
)
35122 if (code
== NOP_EXPR
)
35124 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35132 case OMP_ATOMIC_CAPTURE_NEW
:
35133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35135 cp_lexer_consume_token (parser
->lexer
);
35136 structured_block
= true;
35140 v
= cp_parser_unary_expression (parser
);
35141 if (v
== error_mark_node
)
35143 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35151 lhs
= cp_parser_unary_expression (parser
);
35153 switch (TREE_CODE (lhs
))
35158 case POSTINCREMENT_EXPR
:
35159 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35160 code
= OMP_ATOMIC_CAPTURE_OLD
;
35162 case PREINCREMENT_EXPR
:
35163 lhs
= TREE_OPERAND (lhs
, 0);
35164 opcode
= PLUS_EXPR
;
35165 rhs
= integer_one_node
;
35168 case POSTDECREMENT_EXPR
:
35169 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35170 code
= OMP_ATOMIC_CAPTURE_OLD
;
35172 case PREDECREMENT_EXPR
:
35173 lhs
= TREE_OPERAND (lhs
, 0);
35174 opcode
= MINUS_EXPR
;
35175 rhs
= integer_one_node
;
35178 case COMPOUND_EXPR
:
35179 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35180 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35181 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35182 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35183 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35184 (TREE_OPERAND (lhs
, 1), 0), 0)))
35186 /* Undo effects of boolean_increment for post {in,de}crement. */
35187 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35190 if (TREE_CODE (lhs
) == MODIFY_EXPR
35191 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35193 /* Undo effects of boolean_increment. */
35194 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35196 /* This is pre or post increment. */
35197 rhs
= TREE_OPERAND (lhs
, 1);
35198 lhs
= TREE_OPERAND (lhs
, 0);
35200 if (code
== OMP_ATOMIC_CAPTURE_NEW
35201 && !structured_block
35202 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35203 code
= OMP_ATOMIC_CAPTURE_OLD
;
35209 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35212 opcode
= MULT_EXPR
;
35215 opcode
= TRUNC_DIV_EXPR
;
35218 opcode
= PLUS_EXPR
;
35221 opcode
= MINUS_EXPR
;
35223 case CPP_LSHIFT_EQ
:
35224 opcode
= LSHIFT_EXPR
;
35226 case CPP_RSHIFT_EQ
:
35227 opcode
= RSHIFT_EXPR
;
35230 opcode
= BIT_AND_EXPR
;
35233 opcode
= BIT_IOR_EXPR
;
35236 opcode
= BIT_XOR_EXPR
;
35239 enum cp_parser_prec oprec
;
35241 cp_lexer_consume_token (parser
->lexer
);
35242 cp_parser_parse_tentatively (parser
);
35243 rhs1
= cp_parser_simple_cast_expression (parser
);
35244 if (rhs1
== error_mark_node
)
35246 cp_parser_abort_tentative_parse (parser
);
35247 cp_parser_simple_cast_expression (parser
);
35250 token
= cp_lexer_peek_token (parser
->lexer
);
35251 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35253 cp_parser_abort_tentative_parse (parser
);
35254 cp_parser_parse_tentatively (parser
);
35255 rhs
= cp_parser_binary_expression (parser
, false, true,
35256 PREC_NOT_OPERATOR
, NULL
);
35257 if (rhs
== error_mark_node
)
35259 cp_parser_abort_tentative_parse (parser
);
35260 cp_parser_binary_expression (parser
, false, true,
35261 PREC_NOT_OPERATOR
, NULL
);
35264 switch (TREE_CODE (rhs
))
35267 case TRUNC_DIV_EXPR
:
35276 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35278 if (cp_parser_parse_definitely (parser
))
35280 opcode
= TREE_CODE (rhs
);
35281 rhs1
= TREE_OPERAND (rhs
, 0);
35282 rhs
= TREE_OPERAND (rhs
, 1);
35292 cp_parser_abort_tentative_parse (parser
);
35293 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35295 rhs
= cp_parser_expression (parser
);
35296 if (rhs
== error_mark_node
)
35302 cp_parser_error (parser
,
35303 "invalid form of %<#pragma omp atomic%>");
35306 if (!cp_parser_parse_definitely (parser
))
35308 switch (token
->type
)
35310 case CPP_SEMICOLON
:
35311 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35313 code
= OMP_ATOMIC_CAPTURE_OLD
;
35318 cp_lexer_consume_token (parser
->lexer
);
35321 else if (structured_block
)
35328 cp_parser_error (parser
,
35329 "invalid form of %<#pragma omp atomic%>");
35332 opcode
= MULT_EXPR
;
35335 opcode
= TRUNC_DIV_EXPR
;
35338 opcode
= PLUS_EXPR
;
35341 opcode
= MINUS_EXPR
;
35344 opcode
= LSHIFT_EXPR
;
35347 opcode
= RSHIFT_EXPR
;
35350 opcode
= BIT_AND_EXPR
;
35353 opcode
= BIT_IOR_EXPR
;
35356 opcode
= BIT_XOR_EXPR
;
35359 cp_parser_error (parser
,
35360 "invalid operator for %<#pragma omp atomic%>");
35363 oprec
= TOKEN_PRECEDENCE (token
);
35364 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35365 if (commutative_tree_code (opcode
))
35366 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35367 cp_lexer_consume_token (parser
->lexer
);
35368 rhs
= cp_parser_binary_expression (parser
, false, false,
35370 if (rhs
== error_mark_node
)
35375 cp_parser_error (parser
,
35376 "invalid operator for %<#pragma omp atomic%>");
35379 cp_lexer_consume_token (parser
->lexer
);
35381 rhs
= cp_parser_expression (parser
);
35382 if (rhs
== error_mark_node
)
35387 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35389 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35391 v
= cp_parser_unary_expression (parser
);
35392 if (v
== error_mark_node
)
35394 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35396 lhs1
= cp_parser_unary_expression (parser
);
35397 if (lhs1
== error_mark_node
)
35400 if (structured_block
)
35402 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35403 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35406 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35407 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35408 rhs1
, clauses
, memory_order
);
35409 if (!structured_block
)
35410 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35414 cp_parser_skip_to_end_of_block_or_statement (parser
);
35415 if (structured_block
)
35417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35418 cp_lexer_consume_token (parser
->lexer
);
35419 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35421 cp_parser_skip_to_end_of_block_or_statement (parser
);
35422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35423 cp_lexer_consume_token (parser
->lexer
);
35430 # pragma omp barrier new-line */
35433 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35435 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35436 finish_omp_barrier ();
35440 # pragma omp critical [(name)] new-line
35444 # pragma omp critical [(name) [hint(expression)]] new-line
35445 structured-block */
35447 #define OMP_CRITICAL_CLAUSE_MASK \
35448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35451 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35453 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35457 matching_parens parens
;
35458 parens
.consume_open (parser
);
35460 name
= cp_parser_identifier (parser
);
35462 if (name
== error_mark_node
35463 || !parens
.require_close (parser
))
35464 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35465 /*or_comma=*/false,
35466 /*consume_paren=*/true);
35467 if (name
== error_mark_node
)
35470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35471 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35472 cp_lexer_consume_token (parser
->lexer
);
35474 clauses
= cp_parser_omp_all_clauses (parser
,
35475 OMP_CRITICAL_CLAUSE_MASK
,
35476 "#pragma omp critical", pragma_tok
);
35479 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35481 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35482 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35486 # pragma omp depobj ( depobj ) depobj-clause new-line
35489 depend (dependence-type : locator)
35491 update (dependence-type)
35500 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
35502 location_t loc
= pragma_tok
->location
;
35503 matching_parens parens
;
35504 if (!parens
.require_open (parser
))
35506 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35510 tree depobj
= cp_parser_assignment_expression (parser
);
35512 if (!parens
.require_close (parser
))
35513 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35514 /*or_comma=*/false,
35515 /*consume_paren=*/true);
35517 tree clause
= NULL_TREE
;
35518 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35519 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35522 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35523 const char *p
= IDENTIFIER_POINTER (id
);
35525 cp_lexer_consume_token (parser
->lexer
);
35526 if (!strcmp ("depend", p
))
35528 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
35530 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
35532 clause
= error_mark_node
;
35534 else if (!strcmp ("destroy", p
))
35535 kind
= OMP_CLAUSE_DEPEND_LAST
;
35536 else if (!strcmp ("update", p
))
35538 matching_parens c_parens
;
35539 if (c_parens
.require_open (parser
))
35542 = cp_lexer_peek_token (parser
->lexer
)->location
;
35543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35545 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35546 const char *p2
= IDENTIFIER_POINTER (id2
);
35548 cp_lexer_consume_token (parser
->lexer
);
35549 if (!strcmp ("in", p2
))
35550 kind
= OMP_CLAUSE_DEPEND_IN
;
35551 else if (!strcmp ("out", p2
))
35552 kind
= OMP_CLAUSE_DEPEND_OUT
;
35553 else if (!strcmp ("inout", p2
))
35554 kind
= OMP_CLAUSE_DEPEND_INOUT
;
35555 else if (!strcmp ("mutexinoutset", p2
))
35556 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
35558 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35560 clause
= error_mark_node
;
35561 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
35562 "%<mutexinoutset%>");
35564 if (!c_parens
.require_close (parser
))
35565 cp_parser_skip_to_closing_parenthesis (parser
,
35566 /*recovering=*/true,
35567 /*or_comma=*/false,
35568 /*consume_paren=*/true);
35571 clause
= error_mark_node
;
35574 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35576 clause
= error_mark_node
;
35577 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
35579 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35581 finish_omp_depobj (loc
, depobj
, kind
, clause
);
35586 # pragma omp flush flush-vars[opt] new-line
35592 # pragma omp flush memory-order-clause new-line */
35595 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
35597 enum memmodel mo
= MEMMODEL_LAST
;
35598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35600 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35601 const char *p
= IDENTIFIER_POINTER (id
);
35602 if (!strcmp (p
, "acq_rel"))
35603 mo
= MEMMODEL_ACQ_REL
;
35604 else if (!strcmp (p
, "release"))
35605 mo
= MEMMODEL_RELEASE
;
35606 else if (!strcmp (p
, "acquire"))
35607 mo
= MEMMODEL_ACQUIRE
;
35609 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35610 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35611 cp_lexer_consume_token (parser
->lexer
);
35613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35615 if (mo
!= MEMMODEL_LAST
)
35616 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35617 "%<flush%> list specified together with memory order "
35619 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35621 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35623 finish_omp_flush (mo
);
35626 /* Helper function, to parse omp for increment expression. */
35629 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
35631 tree cond
= cp_parser_binary_expression (parser
, false, true,
35632 PREC_NOT_OPERATOR
, NULL
);
35633 if (cond
== error_mark_node
35634 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35636 cp_parser_skip_to_end_of_statement (parser
);
35637 return error_mark_node
;
35640 switch (TREE_CODE (cond
))
35648 if (code
!= OACC_LOOP
)
35650 gcc_fallthrough ();
35652 return error_mark_node
;
35655 /* If decl is an iterator, preserve LHS and RHS of the relational
35656 expr until finish_omp_for. */
35658 && (type_dependent_expression_p (decl
)
35659 || CLASS_TYPE_P (TREE_TYPE (decl
))))
35662 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
35664 TREE_OPERAND (cond
, 0), ERROR_MARK
,
35665 TREE_OPERAND (cond
, 1), ERROR_MARK
,
35666 /*overload=*/NULL
, tf_warning_or_error
);
35669 /* Helper function, to parse omp for increment expression. */
35672 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
35674 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35680 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35682 op
= (token
->type
== CPP_PLUS_PLUS
35683 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
35684 cp_lexer_consume_token (parser
->lexer
);
35685 lhs
= cp_parser_simple_cast_expression (parser
);
35687 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35688 return error_mark_node
;
35689 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35692 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
35694 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35695 return error_mark_node
;
35697 token
= cp_lexer_peek_token (parser
->lexer
);
35698 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35700 op
= (token
->type
== CPP_PLUS_PLUS
35701 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
35702 cp_lexer_consume_token (parser
->lexer
);
35703 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35706 op
= cp_parser_assignment_operator_opt (parser
);
35707 if (op
== ERROR_MARK
)
35708 return error_mark_node
;
35710 if (op
!= NOP_EXPR
)
35712 rhs
= cp_parser_assignment_expression (parser
);
35713 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
35714 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35717 lhs
= cp_parser_binary_expression (parser
, false, false,
35718 PREC_ADDITIVE_EXPRESSION
, NULL
);
35719 token
= cp_lexer_peek_token (parser
->lexer
);
35720 decl_first
= (lhs
== decl
35721 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
35724 if (token
->type
!= CPP_PLUS
35725 && token
->type
!= CPP_MINUS
)
35726 return error_mark_node
;
35730 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
35731 cp_lexer_consume_token (parser
->lexer
);
35732 rhs
= cp_parser_binary_expression (parser
, false, false,
35733 PREC_ADDITIVE_EXPRESSION
, NULL
);
35734 token
= cp_lexer_peek_token (parser
->lexer
);
35735 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
35737 if (lhs
== NULL_TREE
)
35739 if (op
== PLUS_EXPR
)
35742 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
35743 tf_warning_or_error
);
35746 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
35747 ERROR_MARK
, NULL
, tf_warning_or_error
);
35750 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
35755 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
35756 || op
== MINUS_EXPR
)
35757 return error_mark_node
;
35758 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
35761 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
35763 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35766 /* Parse the initialization statement of an OpenMP for loop.
35768 Return true if the resulting construct should have an
35769 OMP_CLAUSE_PRIVATE added to it. */
35772 cp_parser_omp_for_loop_init (cp_parser
*parser
,
35773 tree
&this_pre_body
,
35774 vec
<tree
, va_gc
> *&for_block
,
35780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35783 tree add_private_clause
= NULL_TREE
;
35785 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35789 integer-type var = lb
35790 random-access-iterator-type var = lb
35791 pointer-type var = lb
35793 cp_decl_specifier_seq type_specifiers
;
35795 /* First, try to parse as an initialized declaration. See
35796 cp_parser_condition, from whence the bulk of this is copied. */
35798 cp_parser_parse_tentatively (parser
);
35799 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
35800 /*is_trailing_return=*/false,
35802 if (cp_parser_parse_definitely (parser
))
35804 /* If parsing a type specifier seq succeeded, then this
35805 MUST be a initialized declaration. */
35806 tree asm_specification
, attributes
;
35807 cp_declarator
*declarator
;
35809 declarator
= cp_parser_declarator (parser
,
35810 CP_PARSER_DECLARATOR_NAMED
,
35811 /*ctor_dtor_or_conv_p=*/NULL
,
35812 /*parenthesized_p=*/NULL
,
35813 /*member_p=*/false,
35814 /*friend_p=*/false);
35815 attributes
= cp_parser_attributes_opt (parser
);
35816 asm_specification
= cp_parser_asm_specification_opt (parser
);
35818 if (declarator
== cp_error_declarator
)
35819 cp_parser_skip_to_end_of_statement (parser
);
35823 tree pushed_scope
, auto_node
;
35825 decl
= start_decl (declarator
, &type_specifiers
,
35826 SD_INITIALIZED
, attributes
,
35827 /*prefix_attributes=*/NULL_TREE
,
35830 auto_node
= type_uses_auto (TREE_TYPE (decl
));
35831 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
35833 if (cp_lexer_next_token_is (parser
->lexer
,
35835 error ("parenthesized initialization is not allowed in "
35836 "OpenMP %<for%> loop");
35838 /* Trigger an error. */
35839 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35841 init
= error_mark_node
;
35842 cp_parser_skip_to_end_of_statement (parser
);
35844 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35845 || type_dependent_expression_p (decl
)
35848 bool is_direct_init
, is_non_constant_init
;
35850 init
= cp_parser_initializer (parser
,
35852 &is_non_constant_init
);
35857 = do_auto_deduction (TREE_TYPE (decl
), init
,
35860 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35861 && !type_dependent_expression_p (decl
))
35865 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35867 LOOKUP_ONLYCONVERTING
);
35869 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35871 vec_safe_push (for_block
, this_pre_body
);
35876 init
= pop_stmt_list (this_pre_body
);
35877 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35879 tree_stmt_iterator i
= tsi_start (init
);
35880 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35881 while (!tsi_end_p (i
))
35883 tree t
= tsi_stmt (i
);
35884 if (TREE_CODE (t
) == DECL_EXPR
35885 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35888 vec_safe_push (for_block
, t
);
35893 if (tsi_one_before_end_p (i
))
35895 tree t
= tsi_stmt (i
);
35897 free_stmt_list (init
);
35902 this_pre_body
= NULL_TREE
;
35907 cp_lexer_consume_token (parser
->lexer
);
35908 init
= cp_parser_assignment_expression (parser
);
35911 if (TYPE_REF_P (TREE_TYPE (decl
)))
35912 init
= error_mark_node
;
35914 cp_finish_decl (decl
, NULL_TREE
,
35915 /*init_const_expr_p=*/false,
35917 LOOKUP_ONLYCONVERTING
);
35921 pop_scope (pushed_scope
);
35927 /* If parsing a type specifier sequence failed, then
35928 this MUST be a simple expression. */
35929 cp_parser_parse_tentatively (parser
);
35930 decl
= cp_parser_primary_expression (parser
, false, false,
35932 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35933 if (!cp_parser_error_occurred (parser
)
35935 && (TREE_CODE (decl
) == COMPONENT_REF
35936 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35938 cp_parser_abort_tentative_parse (parser
);
35939 cp_parser_parse_tentatively (parser
);
35940 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35941 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35942 /*check_dependency_p=*/true,
35943 /*template_p=*/NULL
,
35944 /*declarator_p=*/false,
35945 /*optional_p=*/false);
35946 if (name
!= error_mark_node
35947 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35949 decl
= cp_parser_lookup_name_simple (parser
, name
,
35951 if (TREE_CODE (decl
) == FIELD_DECL
)
35952 add_private_clause
= omp_privatize_field (decl
, false);
35954 cp_parser_abort_tentative_parse (parser
);
35955 cp_parser_parse_tentatively (parser
);
35956 decl
= cp_parser_primary_expression (parser
, false, false,
35959 if (!cp_parser_error_occurred (parser
)
35962 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35966 cp_parser_parse_definitely (parser
);
35967 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35968 rhs
= cp_parser_assignment_expression (parser
);
35970 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35973 tf_warning_or_error
));
35974 if (!add_private_clause
)
35975 add_private_clause
= decl
;
35980 cp_parser_abort_tentative_parse (parser
);
35981 init
= cp_parser_expression (parser
);
35984 if (TREE_CODE (init
) == MODIFY_EXPR
35985 || TREE_CODE (init
) == MODOP_EXPR
)
35986 real_decl
= TREE_OPERAND (init
, 0);
35990 return add_private_clause
;
35993 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
35996 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
35997 tree
&decl
, tree
&orig_decl
, tree
&init
,
35998 tree
&orig_init
, tree
&cond
, tree
&incr
)
36000 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36001 tree iter_type
, begin_expr
, end_expr
;
36003 if (processing_template_decl
)
36005 if (check_for_bare_parameter_packs (init
))
36006 init
= error_mark_node
;
36007 if (!type_dependent_expression_p (init
)
36008 /* do_auto_deduction doesn't mess with template init-lists. */
36009 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36012 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36014 tree v
= DECL_VALUE_EXPR (decl
);
36015 if (TREE_CODE (v
) == ARRAY_REF
36016 && VAR_P (TREE_OPERAND (v
, 0))
36017 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36018 d
= TREE_OPERAND (v
, 0);
36020 do_range_for_auto_deduction (d
, init
);
36022 cond
= global_namespace
;
36026 this_pre_body
= pop_stmt_list (this_pre_body
);
36030 init
= mark_lvalue_use (init
);
36032 if (decl
== error_mark_node
|| init
== error_mark_node
)
36033 /* If an error happened previously do nothing or else a lot of
36034 unhelpful errors would be issued. */
36035 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36041 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36042 /* Can't bind a reference to an array of runtime bound. */
36046 range_temp
= build_range_temp (init
);
36047 DECL_NAME (range_temp
) = NULL_TREE
;
36048 pushdecl (range_temp
);
36049 cp_finish_decl (range_temp
, init
,
36050 /*is_constant_init*/false, NULL_TREE
,
36051 LOOKUP_ONLYCONVERTING
);
36052 range_temp_decl
= range_temp
;
36053 range_temp
= convert_from_reference (range_temp
);
36055 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36056 &begin_expr
, &end_expr
);
36059 tree end_iter_type
= iter_type
;
36060 if (cxx_dialect
>= cxx17
)
36061 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36062 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36063 TREE_USED (end
) = 1;
36064 DECL_ARTIFICIAL (end
) = 1;
36066 cp_finish_decl (end
, end_expr
,
36067 /*is_constant_init*/false, NULL_TREE
,
36068 LOOKUP_ONLYCONVERTING
);
36070 /* The new for initialization statement. */
36071 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36072 TREE_USED (begin
) = 1;
36073 DECL_ARTIFICIAL (begin
) = 1;
36076 if (CLASS_TYPE_P (iter_type
))
36081 begin_expr
= NULL_TREE
;
36083 cp_finish_decl (begin
, begin_expr
,
36084 /*is_constant_init*/false, NULL_TREE
,
36085 LOOKUP_ONLYCONVERTING
);
36087 /* The new for condition. */
36088 if (CLASS_TYPE_P (iter_type
))
36089 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36091 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36094 NULL
, tf_warning_or_error
);
36096 /* The new increment expression. */
36097 if (CLASS_TYPE_P (iter_type
))
36098 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36100 incr
= finish_unary_op_expr (input_location
,
36101 PREINCREMENT_EXPR
, begin
,
36102 tf_warning_or_error
);
36108 vec_safe_push (for_block
, this_pre_body
);
36109 this_pre_body
= NULL_TREE
;
36112 tree decomp_first_name
= NULL_TREE
;
36113 unsigned decomp_cnt
= 0;
36114 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36116 tree v
= DECL_VALUE_EXPR (orig_decl
);
36117 if (TREE_CODE (v
) == ARRAY_REF
36118 && VAR_P (TREE_OPERAND (v
, 0))
36119 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36121 tree d
= orig_decl
;
36122 orig_decl
= TREE_OPERAND (v
, 0);
36123 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36124 decomp_first_name
= d
;
36128 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36131 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36133 if (!error_operand_p (t
))
36134 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36138 tree v
= make_tree_vec (decomp_cnt
+ 3);
36139 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36140 TREE_VEC_ELT (v
, 1) = end
;
36141 TREE_VEC_ELT (v
, 2) = orig_decl
;
36142 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36144 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36145 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36147 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36150 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36151 inside of the collapsed body. */
36154 cp_finish_omp_range_for (tree orig
, tree begin
)
36156 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36157 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36158 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36159 tree decomp_first_name
= NULL_TREE
;
36160 unsigned int decomp_cnt
= 0;
36162 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36164 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36165 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36166 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36169 /* The declaration is initialized with *__begin inside the loop body. */
36170 cp_finish_decl (decl
,
36171 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36172 tf_warning_or_error
),
36173 /*is_constant_init*/false, NULL_TREE
,
36174 LOOKUP_ONLYCONVERTING
);
36175 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36176 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36179 /* Parse the restricted form of the for statement allowed by OpenMP. */
36182 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36183 tree
*cclauses
, bool *if_p
)
36185 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36187 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36188 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36189 location_t loc_first
;
36190 bool collapse_err
= false;
36191 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36192 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36193 auto_vec
<tree
, 4> orig_inits
;
36194 bool tiling
= false;
36196 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36197 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36198 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36199 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36202 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36204 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36205 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36208 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36211 if (ordered
&& ordered
< collapse
)
36213 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36214 "%<ordered%> clause parameter is less than %<collapse%>");
36215 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36216 = build_int_cst (NULL_TREE
, collapse
);
36217 ordered
= collapse
;
36221 for (tree
*pc
= &clauses
; *pc
; )
36222 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36224 error_at (OMP_CLAUSE_LOCATION (*pc
),
36225 "%<linear%> clause may not be specified together "
36226 "with %<ordered%> clause with a parameter");
36227 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36230 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36233 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36234 count
= ordered
? ordered
: collapse
;
36236 declv
= make_tree_vec (count
);
36237 initv
= make_tree_vec (count
);
36238 condv
= make_tree_vec (count
);
36239 incrv
= make_tree_vec (count
);
36240 orig_declv
= NULL_TREE
;
36242 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36244 for (i
= 0; i
< count
; i
++)
36246 int bracecount
= 0;
36247 tree add_private_clause
= NULL_TREE
;
36250 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36253 cp_parser_error (parser
, "for statement expected");
36256 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36258 matching_parens parens
;
36259 if (!parens
.require_open (parser
))
36262 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36263 this_pre_body
= push_stmt_list ();
36265 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36267 /* Save tokens so that we can put them back. */
36268 cp_lexer_save_tokens (parser
->lexer
);
36270 /* Look for ':' that is not nested in () or {}. */
36272 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36273 /*recovering=*/false,
36278 /* Roll back the tokens we skipped. */
36279 cp_lexer_rollback_tokens (parser
->lexer
);
36283 bool saved_colon_corrects_to_scope_p
36284 = parser
->colon_corrects_to_scope_p
;
36286 /* A colon is used in range-based for. */
36287 parser
->colon_corrects_to_scope_p
= false;
36289 /* Parse the declaration. */
36290 cp_parser_simple_declaration (parser
,
36291 /*function_definition_allowed_p=*/
36293 parser
->colon_corrects_to_scope_p
36294 = saved_colon_corrects_to_scope_p
;
36296 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36298 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36301 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36302 orig_decl
, init
, orig_init
,
36309 pre_body
= push_stmt_list ();
36311 add_stmt (this_pre_body
);
36312 pre_body
= pop_stmt_list (pre_body
);
36315 pre_body
= this_pre_body
;
36319 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36320 "%<ordered%> clause with parameter on "
36321 "range-based %<for%> loop");
36323 goto parse_close_paren
;
36328 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36329 init
, orig_init
, decl
, real_decl
);
36331 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36334 this_pre_body
= pop_stmt_list (this_pre_body
);
36338 pre_body
= push_stmt_list ();
36340 add_stmt (this_pre_body
);
36341 pre_body
= pop_stmt_list (pre_body
);
36344 pre_body
= this_pre_body
;
36349 if (cclauses
!= NULL
36350 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36351 && real_decl
!= NULL_TREE
)
36354 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36355 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36356 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36358 error_at (loc
, "iteration variable %qD"
36359 " should not be firstprivate", real_decl
);
36360 *c
= OMP_CLAUSE_CHAIN (*c
);
36362 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36363 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36365 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36367 *c
= OMP_CLAUSE_CHAIN (*c
);
36368 if (code
== OMP_SIMD
)
36370 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36371 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36375 OMP_CLAUSE_CHAIN (l
) = clauses
;
36378 add_private_clause
= NULL_TREE
;
36382 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36383 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36384 add_private_clause
= NULL_TREE
;
36385 c
= &OMP_CLAUSE_CHAIN (*c
);
36389 if (add_private_clause
)
36392 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36394 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36395 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36396 && OMP_CLAUSE_DECL (c
) == decl
)
36398 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36399 && OMP_CLAUSE_DECL (c
) == decl
)
36400 error_at (loc
, "iteration variable %qD "
36401 "should not be firstprivate",
36403 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36404 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36405 && OMP_CLAUSE_DECL (c
) == decl
)
36406 error_at (loc
, "iteration variable %qD should not be reduction",
36411 if (code
!= OMP_SIMD
)
36412 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36413 else if (collapse
== 1)
36414 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36416 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36417 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36418 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36421 OMP_CLAUSE_CHAIN (c
) = clauses
;
36423 /* For linear, signal that we need to fill up
36424 the so far unknown linear step. */
36425 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36426 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36432 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36433 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36434 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36437 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36439 /* If decl is an iterator, preserve the operator on decl
36440 until finish_omp_for. */
36442 && ((processing_template_decl
36443 && (TREE_TYPE (real_decl
) == NULL_TREE
36444 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36445 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36446 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36448 incr
= cp_parser_expression (parser
);
36449 if (!EXPR_HAS_LOCATION (incr
))
36450 protected_set_expr_location (incr
, input_location
);
36454 if (!parens
.require_close (parser
))
36455 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36456 /*or_comma=*/false,
36457 /*consume_paren=*/true);
36459 TREE_VEC_ELT (declv
, i
) = decl
;
36460 TREE_VEC_ELT (initv
, i
) = init
;
36461 TREE_VEC_ELT (condv
, i
) = cond
;
36462 TREE_VEC_ELT (incrv
, i
) = incr
;
36465 orig_inits
.safe_grow_cleared (i
+ 1);
36466 orig_inits
[i
] = orig_init
;
36471 orig_declv
= copy_node (declv
);
36472 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36474 else if (orig_declv
)
36475 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36477 if (i
== count
- 1)
36480 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36481 in between the collapsed for loops to be still considered perfectly
36482 nested. Hopefully the final version clarifies this.
36483 For now handle (multiple) {'s and empty statements. */
36484 cp_parser_parse_tentatively (parser
);
36487 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36489 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36491 cp_lexer_consume_token (parser
->lexer
);
36494 else if (bracecount
36495 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36496 cp_lexer_consume_token (parser
->lexer
);
36499 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36500 error_at (loc
, "not enough for loops to collapse");
36501 collapse_err
= true;
36502 cp_parser_abort_tentative_parse (parser
);
36510 cp_parser_parse_definitely (parser
);
36511 nbraces
+= bracecount
;
36518 /* Note that we saved the original contents of this flag when we entered
36519 the structured block, and so we don't need to re-save it here. */
36520 parser
->in_statement
= IN_OMP_FOR
;
36522 /* Note that the grammar doesn't call for a structured block here,
36523 though the loop as a whole is a structured block. */
36526 body
= begin_omp_structured_block ();
36527 for (i
= 0; i
< count
; i
++)
36528 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
36529 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
36530 TREE_VEC_ELT (declv
, i
));
36533 body
= push_stmt_list ();
36534 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36536 body
= finish_omp_structured_block (body
);
36538 body
= pop_stmt_list (body
);
36540 if (declv
== NULL_TREE
)
36543 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
36544 incrv
, body
, pre_body
, &orig_inits
, clauses
);
36548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36550 cp_lexer_consume_token (parser
->lexer
);
36553 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36554 cp_lexer_consume_token (parser
->lexer
);
36559 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36560 "collapsed loops not perfectly nested");
36562 collapse_err
= true;
36563 cp_parser_statement_seq_opt (parser
, NULL
);
36564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
36569 while (!for_block
->is_empty ())
36571 tree t
= for_block
->pop ();
36572 if (TREE_CODE (t
) == STATEMENT_LIST
)
36573 add_stmt (pop_stmt_list (t
));
36577 release_tree_vector (for_block
);
36582 /* Helper function for OpenMP parsing, split clauses and call
36583 finish_omp_clauses on each of the set of clauses afterwards. */
36586 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
36587 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
36590 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
36591 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
36593 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
36597 #pragma omp simd simd-clause[optseq] new-line
36600 #define OMP_SIMD_CLAUSE_MASK \
36601 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36613 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36614 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36617 tree clauses
, sb
, ret
;
36619 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36621 strcat (p_name
, " simd");
36622 mask
|= OMP_SIMD_CLAUSE_MASK
;
36624 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36628 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
36629 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
36630 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
36631 OMP_CLAUSE_ORDERED
);
36632 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
36634 error_at (OMP_CLAUSE_LOCATION (c
),
36635 "%<ordered%> clause with parameter may not be specified "
36636 "on %qs construct", p_name
);
36637 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
36641 keep_next_level (true);
36642 sb
= begin_omp_structured_block ();
36643 save
= cp_parser_begin_omp_structured_block (parser
);
36645 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
36647 cp_parser_end_omp_structured_block (parser
, save
);
36648 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36654 #pragma omp for for-clause[optseq] new-line
36658 #pragma omp for simd for-simd-clause[optseq] new-line
36661 #define OMP_FOR_CLAUSE_MASK \
36662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36673 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
36674 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36677 tree clauses
, sb
, ret
;
36679 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36681 strcat (p_name
, " for");
36682 mask
|= OMP_FOR_CLAUSE_MASK
;
36683 /* parallel for{, simd} disallows nowait clause, but for
36684 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36685 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
36686 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
36687 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36688 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36689 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
36691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36693 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36694 const char *p
= IDENTIFIER_POINTER (id
);
36696 if (strcmp (p
, "simd") == 0)
36698 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36699 if (cclauses
== NULL
)
36700 cclauses
= cclauses_buf
;
36702 cp_lexer_consume_token (parser
->lexer
);
36703 if (!flag_openmp
) /* flag_openmp_simd */
36704 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36706 sb
= begin_omp_structured_block ();
36707 save
= cp_parser_begin_omp_structured_block (parser
);
36708 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36710 cp_parser_end_omp_structured_block (parser
, save
);
36711 tree body
= finish_omp_structured_block (sb
);
36714 ret
= make_node (OMP_FOR
);
36715 TREE_TYPE (ret
) = void_type_node
;
36716 OMP_FOR_BODY (ret
) = body
;
36717 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36718 SET_EXPR_LOCATION (ret
, loc
);
36723 if (!flag_openmp
) /* flag_openmp_simd */
36725 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36729 /* Composite distribute parallel for disallows linear clause. */
36730 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36731 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
36733 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36737 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
36738 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36741 keep_next_level (true);
36742 sb
= begin_omp_structured_block ();
36743 save
= cp_parser_begin_omp_structured_block (parser
);
36745 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
36747 cp_parser_end_omp_structured_block (parser
, save
);
36748 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36753 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
36754 omp_clause_mask
, tree
*, bool *);
36757 # pragma omp master new-line
36758 structured-block */
36761 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
36762 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36765 tree clauses
, sb
, ret
;
36767 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36769 strcat (p_name
, " master");
36771 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36773 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36774 const char *p
= IDENTIFIER_POINTER (id
);
36776 if (strcmp (p
, "taskloop") == 0)
36778 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36779 if (cclauses
== NULL
)
36780 cclauses
= cclauses_buf
;
36782 cp_lexer_consume_token (parser
->lexer
);
36783 if (!flag_openmp
) /* flag_openmp_simd */
36784 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36786 sb
= begin_omp_structured_block ();
36787 save
= cp_parser_begin_omp_structured_block (parser
);
36788 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36790 cp_parser_end_omp_structured_block (parser
, save
);
36791 tree body
= finish_omp_structured_block (sb
);
36794 return c_finish_omp_master (loc
, body
);
36797 if (!flag_openmp
) /* flag_openmp_simd */
36799 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36805 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36807 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
36810 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36812 return c_finish_omp_master (loc
,
36813 cp_parser_omp_structured_block (parser
, if_p
));
36817 # pragma omp ordered new-line
36821 # pragma omp ordered ordered-clauses new-line
36822 structured-block */
36824 #define OMP_ORDERED_CLAUSE_MASK \
36825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36828 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36829 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36832 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
36833 enum pragma_context context
, bool *if_p
)
36835 location_t loc
= pragma_tok
->location
;
36837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36839 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36840 const char *p
= IDENTIFIER_POINTER (id
);
36842 if (strcmp (p
, "depend") == 0)
36844 if (!flag_openmp
) /* flag_openmp_simd */
36846 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36849 if (context
== pragma_stmt
)
36851 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
36852 "%<depend%> clause may only be used in compound "
36854 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36858 = cp_parser_omp_all_clauses (parser
,
36859 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
36860 "#pragma omp ordered", pragma_tok
);
36861 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
36867 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
36868 "#pragma omp ordered", pragma_tok
);
36870 if (!flag_openmp
/* flag_openmp_simd */
36871 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
36874 c_finish_omp_ordered (loc
, clauses
,
36875 cp_parser_omp_structured_block (parser
, if_p
));
36882 { section-sequence }
36885 section-directive[opt] structured-block
36886 section-sequence section-directive structured-block */
36889 cp_parser_omp_sections_scope (cp_parser
*parser
)
36891 tree stmt
, substmt
;
36892 bool error_suppress
= false;
36895 matching_braces braces
;
36896 if (!braces
.require_open (parser
))
36899 stmt
= push_stmt_list ();
36901 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
36902 != PRAGMA_OMP_SECTION
)
36904 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
36905 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
36906 add_stmt (substmt
);
36911 tok
= cp_lexer_peek_token (parser
->lexer
);
36912 if (tok
->type
== CPP_CLOSE_BRACE
)
36914 if (tok
->type
== CPP_EOF
)
36917 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
36919 cp_lexer_consume_token (parser
->lexer
);
36920 cp_parser_require_pragma_eol (parser
, tok
);
36921 error_suppress
= false;
36923 else if (!error_suppress
)
36925 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
36926 error_suppress
= true;
36929 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
36930 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
36931 add_stmt (substmt
);
36933 braces
.require_close (parser
);
36935 substmt
= pop_stmt_list (stmt
);
36937 stmt
= make_node (OMP_SECTIONS
);
36938 TREE_TYPE (stmt
) = void_type_node
;
36939 OMP_SECTIONS_BODY (stmt
) = substmt
;
36946 # pragma omp sections sections-clause[optseq] newline
36949 #define OMP_SECTIONS_CLAUSE_MASK \
36950 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36957 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
36958 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
36961 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36963 strcat (p_name
, " sections");
36964 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
36966 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
36968 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36972 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
36973 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
36976 ret
= cp_parser_omp_sections_scope (parser
);
36978 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
36984 # pragma omp parallel parallel-clause[optseq] new-line
36986 # pragma omp parallel for parallel-for-clause[optseq] new-line
36988 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
36992 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
36993 structured-block */
36995 #define OMP_PARALLEL_CLAUSE_MASK \
36996 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37007 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37008 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37011 tree stmt
, clauses
, block
;
37013 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37015 strcat (p_name
, " parallel");
37016 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37017 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37018 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37019 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37020 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37022 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37024 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37025 if (cclauses
== NULL
)
37026 cclauses
= cclauses_buf
;
37028 cp_lexer_consume_token (parser
->lexer
);
37029 if (!flag_openmp
) /* flag_openmp_simd */
37030 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37032 block
= begin_omp_parallel ();
37033 save
= cp_parser_begin_omp_structured_block (parser
);
37034 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37036 cp_parser_end_omp_structured_block (parser
, save
);
37037 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37039 if (ret
== NULL_TREE
)
37041 OMP_PARALLEL_COMBINED (stmt
) = 1;
37044 /* When combined with distribute, parallel has to be followed by for.
37045 #pragma omp target parallel is allowed though. */
37047 && (mask
& (OMP_CLAUSE_MASK_1
37048 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37050 error_at (loc
, "expected %<for%> after %qs", p_name
);
37051 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37054 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37056 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37057 const char *p
= IDENTIFIER_POINTER (id
);
37058 if (strcmp (p
, "master") == 0)
37060 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37061 cclauses
= cclauses_buf
;
37063 cp_lexer_consume_token (parser
->lexer
);
37064 block
= begin_omp_parallel ();
37065 save
= cp_parser_begin_omp_structured_block (parser
);
37066 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37068 cp_parser_end_omp_structured_block (parser
, save
);
37069 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37071 OMP_PARALLEL_COMBINED (stmt
) = 1;
37072 if (ret
== NULL_TREE
)
37076 else if (!flag_openmp
) /* flag_openmp_simd */
37078 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37081 else if (strcmp (p
, "sections") == 0)
37083 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37084 cclauses
= cclauses_buf
;
37086 cp_lexer_consume_token (parser
->lexer
);
37087 block
= begin_omp_parallel ();
37088 save
= cp_parser_begin_omp_structured_block (parser
);
37089 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37090 cp_parser_end_omp_structured_block (parser
, save
);
37091 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37093 OMP_PARALLEL_COMBINED (stmt
) = 1;
37097 else if (!flag_openmp
) /* flag_openmp_simd */
37099 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37103 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37107 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37108 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37111 block
= begin_omp_parallel ();
37112 save
= cp_parser_begin_omp_structured_block (parser
);
37113 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37114 cp_parser_end_omp_structured_block (parser
, save
);
37115 stmt
= finish_omp_parallel (clauses
, block
);
37120 # pragma omp single single-clause[optseq] new-line
37121 structured-block */
37123 #define OMP_SINGLE_CLAUSE_MASK \
37124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37130 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37132 tree stmt
= make_node (OMP_SINGLE
);
37133 TREE_TYPE (stmt
) = void_type_node
;
37134 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37136 OMP_SINGLE_CLAUSES (stmt
)
37137 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37138 "#pragma omp single", pragma_tok
);
37139 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37141 return add_stmt (stmt
);
37145 # pragma omp task task-clause[optseq] new-line
37146 structured-block */
37148 #define OMP_TASK_CLAUSE_MASK \
37149 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37162 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37164 tree clauses
, block
;
37167 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37168 "#pragma omp task", pragma_tok
);
37169 block
= begin_omp_task ();
37170 save
= cp_parser_begin_omp_structured_block (parser
);
37171 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37172 cp_parser_end_omp_structured_block (parser
, save
);
37173 return finish_omp_task (clauses
, block
);
37177 # pragma omp taskwait new-line
37180 # pragma omp taskwait taskwait-clause[opt] new-line */
37182 #define OMP_TASKWAIT_CLAUSE_MASK \
37183 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37186 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37189 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37190 "#pragma omp taskwait", pragma_tok
);
37194 tree stmt
= make_node (OMP_TASK
);
37195 TREE_TYPE (stmt
) = void_node
;
37196 OMP_TASK_CLAUSES (stmt
) = clauses
;
37197 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37198 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37202 finish_omp_taskwait ();
37206 # pragma omp taskyield new-line */
37209 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37211 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37212 finish_omp_taskyield ();
37216 # pragma omp taskgroup new-line
37220 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37222 #define OMP_TASKGROUP_CLAUSE_MASK \
37223 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37226 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37229 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37230 "#pragma omp taskgroup", pragma_tok
);
37231 return c_finish_omp_taskgroup (input_location
,
37232 cp_parser_omp_structured_block (parser
,
37239 # pragma omp threadprivate (variable-list) */
37242 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37246 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37247 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37249 finish_omp_threadprivate (vars
);
37253 # pragma omp cancel cancel-clause[optseq] new-line */
37255 #define OMP_CANCEL_CLAUSE_MASK \
37256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37263 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37265 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37266 "#pragma omp cancel", pragma_tok
);
37267 finish_omp_cancel (clauses
);
37271 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37273 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37280 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37281 enum pragma_context context
)
37284 bool point_seen
= false;
37286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37288 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37289 const char *p
= IDENTIFIER_POINTER (id
);
37291 if (strcmp (p
, "point") == 0)
37293 cp_lexer_consume_token (parser
->lexer
);
37299 cp_parser_error (parser
, "expected %<point%>");
37300 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37304 if (context
!= pragma_compound
)
37306 if (context
== pragma_stmt
)
37307 error_at (pragma_tok
->location
,
37308 "%<#pragma %s%> may only be used in compound statements",
37309 "omp cancellation point");
37311 cp_parser_error (parser
, "expected declaration specifiers");
37312 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37316 clauses
= cp_parser_omp_all_clauses (parser
,
37317 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37318 "#pragma omp cancellation point",
37320 finish_omp_cancellation_point (clauses
);
37324 #pragma omp distribute distribute-clause[optseq] new-line
37327 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37335 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37336 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37339 tree clauses
, sb
, ret
;
37341 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37343 strcat (p_name
, " distribute");
37344 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37348 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37349 const char *p
= IDENTIFIER_POINTER (id
);
37351 bool parallel
= false;
37353 if (strcmp (p
, "simd") == 0)
37356 parallel
= strcmp (p
, "parallel") == 0;
37357 if (parallel
|| simd
)
37359 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37360 if (cclauses
== NULL
)
37361 cclauses
= cclauses_buf
;
37362 cp_lexer_consume_token (parser
->lexer
);
37363 if (!flag_openmp
) /* flag_openmp_simd */
37366 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37369 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37372 sb
= begin_omp_structured_block ();
37373 save
= cp_parser_begin_omp_structured_block (parser
);
37375 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37378 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37380 cp_parser_end_omp_structured_block (parser
, save
);
37381 tree body
= finish_omp_structured_block (sb
);
37384 ret
= make_node (OMP_DISTRIBUTE
);
37385 TREE_TYPE (ret
) = void_type_node
;
37386 OMP_FOR_BODY (ret
) = body
;
37387 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37388 SET_EXPR_LOCATION (ret
, loc
);
37393 if (!flag_openmp
) /* flag_openmp_simd */
37395 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37399 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37403 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37404 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37407 keep_next_level (true);
37408 sb
= begin_omp_structured_block ();
37409 save
= cp_parser_begin_omp_structured_block (parser
);
37411 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37413 cp_parser_end_omp_structured_block (parser
, save
);
37414 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37420 # pragma omp teams teams-clause[optseq] new-line
37421 structured-block */
37423 #define OMP_TEAMS_CLAUSE_MASK \
37424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37433 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37434 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37437 tree clauses
, sb
, ret
;
37439 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37441 strcat (p_name
, " teams");
37442 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37446 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37447 const char *p
= IDENTIFIER_POINTER (id
);
37448 if (strcmp (p
, "distribute") == 0)
37450 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37451 if (cclauses
== NULL
)
37452 cclauses
= cclauses_buf
;
37454 cp_lexer_consume_token (parser
->lexer
);
37455 if (!flag_openmp
) /* flag_openmp_simd */
37456 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37458 keep_next_level (true);
37459 sb
= begin_omp_structured_block ();
37460 save
= cp_parser_begin_omp_structured_block (parser
);
37461 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37463 cp_parser_end_omp_structured_block (parser
, save
);
37464 tree body
= finish_omp_structured_block (sb
);
37467 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37468 ret
= make_node (OMP_TEAMS
);
37469 TREE_TYPE (ret
) = void_type_node
;
37470 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37471 OMP_TEAMS_BODY (ret
) = body
;
37472 OMP_TEAMS_COMBINED (ret
) = 1;
37473 SET_EXPR_LOCATION (ret
, loc
);
37474 return add_stmt (ret
);
37477 if (!flag_openmp
) /* flag_openmp_simd */
37479 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37483 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37487 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
37488 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37491 tree stmt
= make_node (OMP_TEAMS
);
37492 TREE_TYPE (stmt
) = void_type_node
;
37493 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
37494 keep_next_level (true);
37495 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37496 SET_EXPR_LOCATION (stmt
, loc
);
37498 return add_stmt (stmt
);
37502 # pragma omp target data target-data-clause[optseq] new-line
37503 structured-block */
37505 #define OMP_TARGET_DATA_CLAUSE_MASK \
37506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37512 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37515 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
37516 "#pragma omp target data", pragma_tok
);
37518 for (tree
*pc
= &clauses
; *pc
;)
37520 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37521 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37524 case GOMP_MAP_ALWAYS_TO
:
37525 case GOMP_MAP_FROM
:
37526 case GOMP_MAP_ALWAYS_FROM
:
37527 case GOMP_MAP_TOFROM
:
37528 case GOMP_MAP_ALWAYS_TOFROM
:
37529 case GOMP_MAP_ALLOC
:
37532 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37533 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37534 case GOMP_MAP_ALWAYS_POINTER
:
37538 error_at (OMP_CLAUSE_LOCATION (*pc
),
37539 "%<#pragma omp target data%> with map-type other "
37540 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37541 "on %<map%> clause");
37542 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37545 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
37547 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37553 error_at (pragma_tok
->location
,
37554 "%<#pragma omp target data%> must contain at least "
37555 "one %<map%> or %<use_device_ptr%> clause");
37559 tree stmt
= make_node (OMP_TARGET_DATA
);
37560 TREE_TYPE (stmt
) = void_type_node
;
37561 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
37563 keep_next_level (true);
37564 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37566 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37567 return add_stmt (stmt
);
37571 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37572 structured-block */
37574 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37582 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37583 enum pragma_context context
)
37585 bool data_seen
= false;
37586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37588 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37589 const char *p
= IDENTIFIER_POINTER (id
);
37591 if (strcmp (p
, "data") == 0)
37593 cp_lexer_consume_token (parser
->lexer
);
37599 cp_parser_error (parser
, "expected %<data%>");
37600 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37604 if (context
== pragma_stmt
)
37606 error_at (pragma_tok
->location
,
37607 "%<#pragma %s%> may only be used in compound statements",
37608 "omp target enter data");
37609 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37614 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
37615 "#pragma omp target enter data", pragma_tok
);
37617 for (tree
*pc
= &clauses
; *pc
;)
37619 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37620 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37623 case GOMP_MAP_ALWAYS_TO
:
37624 case GOMP_MAP_ALLOC
:
37627 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37628 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37629 case GOMP_MAP_ALWAYS_POINTER
:
37633 error_at (OMP_CLAUSE_LOCATION (*pc
),
37634 "%<#pragma omp target enter data%> with map-type other "
37635 "than %<to%> or %<alloc%> on %<map%> clause");
37636 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37639 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37645 error_at (pragma_tok
->location
,
37646 "%<#pragma omp target enter data%> must contain at least "
37647 "one %<map%> clause");
37651 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
37652 TREE_TYPE (stmt
) = void_type_node
;
37653 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
37654 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37655 return add_stmt (stmt
);
37659 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37660 structured-block */
37662 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37670 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37671 enum pragma_context context
)
37673 bool data_seen
= false;
37674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37676 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37677 const char *p
= IDENTIFIER_POINTER (id
);
37679 if (strcmp (p
, "data") == 0)
37681 cp_lexer_consume_token (parser
->lexer
);
37687 cp_parser_error (parser
, "expected %<data%>");
37688 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37692 if (context
== pragma_stmt
)
37694 error_at (pragma_tok
->location
,
37695 "%<#pragma %s%> may only be used in compound statements",
37696 "omp target exit data");
37697 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37702 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
37703 "#pragma omp target exit data", pragma_tok
);
37705 for (tree
*pc
= &clauses
; *pc
;)
37707 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37708 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37710 case GOMP_MAP_FROM
:
37711 case GOMP_MAP_ALWAYS_FROM
:
37712 case GOMP_MAP_RELEASE
:
37713 case GOMP_MAP_DELETE
:
37716 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37717 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37718 case GOMP_MAP_ALWAYS_POINTER
:
37722 error_at (OMP_CLAUSE_LOCATION (*pc
),
37723 "%<#pragma omp target exit data%> with map-type other "
37724 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37726 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37729 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37735 error_at (pragma_tok
->location
,
37736 "%<#pragma omp target exit data%> must contain at least "
37737 "one %<map%> clause");
37741 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
37742 TREE_TYPE (stmt
) = void_type_node
;
37743 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
37744 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37745 return add_stmt (stmt
);
37749 # pragma omp target update target-update-clause[optseq] new-line */
37751 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37760 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
37761 enum pragma_context context
)
37763 if (context
== pragma_stmt
)
37765 error_at (pragma_tok
->location
,
37766 "%<#pragma %s%> may only be used in compound statements",
37767 "omp target update");
37768 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37773 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
37774 "#pragma omp target update", pragma_tok
);
37775 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
37776 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
37778 error_at (pragma_tok
->location
,
37779 "%<#pragma omp target update%> must contain at least one "
37780 "%<from%> or %<to%> clauses");
37784 tree stmt
= make_node (OMP_TARGET_UPDATE
);
37785 TREE_TYPE (stmt
) = void_type_node
;
37786 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
37787 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37793 # pragma omp target target-clause[optseq] new-line
37794 structured-block */
37796 #define OMP_TARGET_CLAUSE_MASK \
37797 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37808 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
37809 enum pragma_context context
, bool *if_p
)
37811 tree
*pc
= NULL
, stmt
;
37815 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
37817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37819 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37820 const char *p
= IDENTIFIER_POINTER (id
);
37821 enum tree_code ccode
= ERROR_MARK
;
37823 if (strcmp (p
, "teams") == 0)
37825 else if (strcmp (p
, "parallel") == 0)
37826 ccode
= OMP_PARALLEL
;
37827 else if (strcmp (p
, "simd") == 0)
37829 if (ccode
!= ERROR_MARK
)
37831 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
37832 char p_name
[sizeof ("#pragma omp target teams distribute "
37833 "parallel for simd")];
37835 cp_lexer_consume_token (parser
->lexer
);
37836 strcpy (p_name
, "#pragma omp target");
37837 if (!flag_openmp
) /* flag_openmp_simd */
37843 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
37844 OMP_TARGET_CLAUSE_MASK
,
37848 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
37849 OMP_TARGET_CLAUSE_MASK
,
37853 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
37854 OMP_TARGET_CLAUSE_MASK
,
37858 gcc_unreachable ();
37860 return stmt
!= NULL_TREE
;
37862 keep_next_level (true);
37863 tree sb
= begin_omp_structured_block (), ret
;
37864 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
37868 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
37869 OMP_TARGET_CLAUSE_MASK
, cclauses
,
37873 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
37874 OMP_TARGET_CLAUSE_MASK
, cclauses
,
37878 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
37879 OMP_TARGET_CLAUSE_MASK
, cclauses
,
37883 gcc_unreachable ();
37885 cp_parser_end_omp_structured_block (parser
, save
);
37886 tree body
= finish_omp_structured_block (sb
);
37887 if (ret
== NULL_TREE
)
37889 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
37891 /* For combined target teams, ensure the num_teams and
37892 thread_limit clause expressions are evaluated on the host,
37893 before entering the target construct. */
37895 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37896 c
; c
= OMP_CLAUSE_CHAIN (c
))
37897 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
37898 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
37899 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
37901 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
37902 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
37903 if (expr
== error_mark_node
)
37905 tree tmp
= TARGET_EXPR_SLOT (expr
);
37907 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
37908 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
37909 OMP_CLAUSE_FIRSTPRIVATE
);
37910 OMP_CLAUSE_DECL (tc
) = tmp
;
37911 OMP_CLAUSE_CHAIN (tc
)
37912 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
37913 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
37916 tree stmt
= make_node (OMP_TARGET
);
37917 TREE_TYPE (stmt
) = void_type_node
;
37918 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
37919 OMP_TARGET_BODY (stmt
) = body
;
37920 OMP_TARGET_COMBINED (stmt
) = 1;
37921 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37923 pc
= &OMP_TARGET_CLAUSES (stmt
);
37924 goto check_clauses
;
37926 else if (!flag_openmp
) /* flag_openmp_simd */
37928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37931 else if (strcmp (p
, "data") == 0)
37933 cp_lexer_consume_token (parser
->lexer
);
37934 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
37937 else if (strcmp (p
, "enter") == 0)
37939 cp_lexer_consume_token (parser
->lexer
);
37940 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
37943 else if (strcmp (p
, "exit") == 0)
37945 cp_lexer_consume_token (parser
->lexer
);
37946 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
37949 else if (strcmp (p
, "update") == 0)
37951 cp_lexer_consume_token (parser
->lexer
);
37952 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
37955 if (!flag_openmp
) /* flag_openmp_simd */
37957 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37961 stmt
= make_node (OMP_TARGET
);
37962 TREE_TYPE (stmt
) = void_type_node
;
37964 OMP_TARGET_CLAUSES (stmt
)
37965 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
37966 "#pragma omp target", pragma_tok
);
37967 pc
= &OMP_TARGET_CLAUSES (stmt
);
37968 keep_next_level (true);
37969 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37971 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37977 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37978 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37981 case GOMP_MAP_ALWAYS_TO
:
37982 case GOMP_MAP_FROM
:
37983 case GOMP_MAP_ALWAYS_FROM
:
37984 case GOMP_MAP_TOFROM
:
37985 case GOMP_MAP_ALWAYS_TOFROM
:
37986 case GOMP_MAP_ALLOC
:
37987 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37988 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37989 case GOMP_MAP_ALWAYS_POINTER
:
37992 error_at (OMP_CLAUSE_LOCATION (*pc
),
37993 "%<#pragma omp target%> with map-type other "
37994 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37995 "on %<map%> clause");
37996 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37999 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38005 # pragma acc cache (variable-list) new-line
38009 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38011 tree stmt
, clauses
;
38013 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38014 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38016 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38018 stmt
= make_node (OACC_CACHE
);
38019 TREE_TYPE (stmt
) = void_type_node
;
38020 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38021 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38028 # pragma acc data oacc-data-clause[optseq] new-line
38029 structured-block */
38031 #define OACC_DATA_CLAUSE_MASK \
38032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38041 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38043 tree stmt
, clauses
, block
;
38046 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38047 "#pragma acc data", pragma_tok
);
38049 block
= begin_omp_parallel ();
38050 save
= cp_parser_begin_omp_structured_block (parser
);
38051 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38052 cp_parser_end_omp_structured_block (parser
, save
);
38053 stmt
= finish_oacc_data (clauses
, block
);
38058 # pragma acc host_data <clauses> new-line
38059 structured-block */
38061 #define OACC_HOST_DATA_CLAUSE_MASK \
38062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38065 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38067 tree stmt
, clauses
, block
;
38070 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38071 "#pragma acc host_data", pragma_tok
);
38073 block
= begin_omp_parallel ();
38074 save
= cp_parser_begin_omp_structured_block (parser
);
38075 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38076 cp_parser_end_omp_structured_block (parser
, save
);
38077 stmt
= finish_oacc_host_data (clauses
, block
);
38082 # pragma acc declare oacc-data-clause[optseq] new-line
38085 #define OACC_DECLARE_CLAUSE_MASK \
38086 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38096 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38098 tree clauses
, stmt
;
38099 bool error
= false;
38101 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38102 "#pragma acc declare", pragma_tok
, true);
38105 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38107 error_at (pragma_tok
->location
,
38108 "no valid clauses specified in %<#pragma acc declare%>");
38112 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38114 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38115 tree decl
= OMP_CLAUSE_DECL (t
);
38116 if (!DECL_P (decl
))
38118 error_at (loc
, "array section in %<#pragma acc declare%>");
38122 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38123 switch (OMP_CLAUSE_MAP_KIND (t
))
38125 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38126 case GOMP_MAP_ALLOC
:
38128 case GOMP_MAP_FORCE_DEVICEPTR
:
38129 case GOMP_MAP_DEVICE_RESIDENT
:
38132 case GOMP_MAP_LINK
:
38133 if (!global_bindings_p ()
38134 && (TREE_STATIC (decl
)
38135 || !DECL_EXTERNAL (decl
)))
38138 "%qD must be a global variable in "
38139 "%<#pragma acc declare link%>",
38147 if (global_bindings_p ())
38149 error_at (loc
, "invalid OpenACC clause at file scope");
38153 if (DECL_EXTERNAL (decl
))
38156 "invalid use of %<extern%> variable %qD "
38157 "in %<#pragma acc declare%>", decl
);
38161 else if (TREE_PUBLIC (decl
))
38164 "invalid use of %<global%> variable %qD "
38165 "in %<#pragma acc declare%>", decl
);
38172 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38173 || lookup_attribute ("omp declare target link",
38174 DECL_ATTRIBUTES (decl
)))
38176 error_at (loc
, "variable %qD used more than once with "
38177 "%<#pragma acc declare%>", decl
);
38186 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38187 id
= get_identifier ("omp declare target link");
38189 id
= get_identifier ("omp declare target");
38191 DECL_ATTRIBUTES (decl
)
38192 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38193 if (global_bindings_p ())
38195 symtab_node
*node
= symtab_node::get (decl
);
38198 node
->offloadable
= 1;
38199 if (ENABLE_OFFLOADING
)
38201 g
->have_offload
= true;
38202 if (is_a
<varpool_node
*> (node
))
38203 vec_safe_push (offload_vars
, decl
);
38210 if (error
|| global_bindings_p ())
38213 stmt
= make_node (OACC_DECLARE
);
38214 TREE_TYPE (stmt
) = void_type_node
;
38215 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38216 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38224 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38228 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38230 LOC is the location of the #pragma token.
38233 #define OACC_ENTER_DATA_CLAUSE_MASK \
38234 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38240 #define OACC_EXIT_DATA_CLAUSE_MASK \
38241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38249 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38252 location_t loc
= pragma_tok
->location
;
38253 tree stmt
, clauses
;
38254 const char *p
= "";
38256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38257 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38259 if (strcmp (p
, "data") != 0)
38261 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38262 enter
? "enter" : "exit");
38263 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38267 cp_lexer_consume_token (parser
->lexer
);
38270 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38271 "#pragma acc enter data", pragma_tok
);
38273 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38274 "#pragma acc exit data", pragma_tok
);
38276 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38278 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38279 enter
? "enter" : "exit");
38283 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38284 TREE_TYPE (stmt
) = void_type_node
;
38285 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38286 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38292 # pragma acc loop oacc-loop-clause[optseq] new-line
38293 structured-block */
38295 #define OACC_LOOP_CLAUSE_MASK \
38296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38308 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38309 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38311 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38313 strcat (p_name
, " loop");
38314 mask
|= OACC_LOOP_CLAUSE_MASK
;
38316 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38320 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38322 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38324 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38327 tree block
= begin_omp_structured_block ();
38328 int save
= cp_parser_begin_omp_structured_block (parser
);
38329 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38330 cp_parser_end_omp_structured_block (parser
, save
);
38331 add_stmt (finish_omp_structured_block (block
));
38337 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38342 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38346 #define OACC_KERNELS_CLAUSE_MASK \
38347 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38361 #define OACC_PARALLEL_CLAUSE_MASK \
38362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38380 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38381 char *p_name
, bool *if_p
)
38383 omp_clause_mask mask
;
38384 enum tree_code code
;
38385 switch (cp_parser_pragma_kind (pragma_tok
))
38387 case PRAGMA_OACC_KERNELS
:
38388 strcat (p_name
, " kernels");
38389 mask
= OACC_KERNELS_CLAUSE_MASK
;
38390 code
= OACC_KERNELS
;
38392 case PRAGMA_OACC_PARALLEL
:
38393 strcat (p_name
, " parallel");
38394 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38395 code
= OACC_PARALLEL
;
38398 gcc_unreachable ();
38401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38404 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38405 if (strcmp (p
, "loop") == 0)
38407 cp_lexer_consume_token (parser
->lexer
);
38408 tree block
= begin_omp_parallel ();
38410 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
38412 return finish_omp_construct (code
, block
, clauses
);
38416 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38418 tree block
= begin_omp_parallel ();
38419 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38420 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38421 cp_parser_end_omp_structured_block (parser
, save
);
38422 return finish_omp_construct (code
, block
, clauses
);
38426 # pragma acc update oacc-update-clause[optseq] new-line
38429 #define OACC_UPDATE_CLAUSE_MASK \
38430 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38438 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38440 tree stmt
, clauses
;
38442 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38443 "#pragma acc update", pragma_tok
);
38445 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38447 error_at (pragma_tok
->location
,
38448 "%<#pragma acc update%> must contain at least one "
38449 "%<device%> or %<host%> or %<self%> clause");
38453 stmt
= make_node (OACC_UPDATE
);
38454 TREE_TYPE (stmt
) = void_type_node
;
38455 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38456 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38462 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38464 LOC is the location of the #pragma token.
38467 #define OACC_WAIT_CLAUSE_MASK \
38468 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38471 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38473 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38474 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38476 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38477 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
38479 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
38480 "#pragma acc wait", pragma_tok
);
38482 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
38483 stmt
= finish_expr_stmt (stmt
);
38489 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38491 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38500 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38501 enum pragma_context context
)
38503 bool first_p
= parser
->omp_declare_simd
== NULL
;
38504 cp_omp_declare_simd_data data
;
38507 data
.error_seen
= false;
38508 data
.fndecl_seen
= false;
38509 data
.tokens
= vNULL
;
38510 data
.clauses
= NULL_TREE
;
38511 /* It is safe to take the address of a local variable; it will only be
38512 used while this scope is live. */
38513 parser
->omp_declare_simd
= &data
;
38516 /* Store away all pragma tokens. */
38517 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38518 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38519 cp_lexer_consume_token (parser
->lexer
);
38520 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38521 parser
->omp_declare_simd
->error_seen
= true;
38522 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38523 struct cp_token_cache
*cp
38524 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38525 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
38529 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38530 cp_parser_pragma (parser
, context
, NULL
);
38533 case pragma_external
:
38534 cp_parser_declaration (parser
);
38536 case pragma_member
:
38537 cp_parser_member_declaration (parser
);
38539 case pragma_objc_icode
:
38540 cp_parser_block_declaration (parser
, /*statement_p=*/false);
38543 cp_parser_declaration_statement (parser
);
38546 if (parser
->omp_declare_simd
38547 && !parser
->omp_declare_simd
->error_seen
38548 && !parser
->omp_declare_simd
->fndecl_seen
)
38549 error_at (pragma_tok
->location
,
38550 "%<#pragma omp declare simd%> not immediately followed by "
38551 "function declaration or definition");
38552 data
.tokens
.release ();
38553 parser
->omp_declare_simd
= NULL
;
38557 /* Finalize #pragma omp declare simd clauses after direct declarator has
38558 been parsed, and put that into "omp declare simd" attribute. */
38561 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
38563 struct cp_token_cache
*ce
;
38564 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
38567 if (!data
->error_seen
&& data
->fndecl_seen
)
38569 error ("%<#pragma omp declare simd%> not immediately followed by "
38570 "a single function declaration or definition");
38571 data
->error_seen
= true;
38573 if (data
->error_seen
)
38576 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
38580 cp_parser_push_lexer_for_tokens (parser
, ce
);
38581 parser
->lexer
->in_pragma
= true;
38582 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38583 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38584 cp_lexer_consume_token (parser
->lexer
);
38585 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
38586 "#pragma omp declare simd", pragma_tok
);
38587 cp_parser_pop_lexer (parser
);
38589 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
38590 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
38591 TREE_CHAIN (c
) = attrs
;
38592 if (processing_template_decl
)
38593 ATTR_IS_DEPENDENT (c
) = 1;
38597 data
->fndecl_seen
= true;
38603 # pragma omp declare target new-line
38604 declarations and definitions
38605 # pragma omp end declare target new-line
38608 # pragma omp declare target ( extended-list ) new-line
38610 # pragma omp declare target declare-target-clauses[seq] new-line */
38612 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38617 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38619 tree clauses
= NULL_TREE
;
38620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38622 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
38623 "#pragma omp declare target", pragma_tok
);
38624 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38626 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
38628 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38629 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38633 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38634 scope_chain
->omp_declare_target_attribute
++;
38637 if (scope_chain
->omp_declare_target_attribute
)
38638 error_at (pragma_tok
->location
,
38639 "%<#pragma omp declare target%> with clauses in between "
38640 "%<#pragma omp declare target%> without clauses and "
38641 "%<#pragma omp end declare target%>");
38642 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
38644 tree t
= OMP_CLAUSE_DECL (c
), id
;
38645 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
38646 tree at2
= lookup_attribute ("omp declare target link",
38647 DECL_ATTRIBUTES (t
));
38648 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
38650 id
= get_identifier ("omp declare target link");
38651 std::swap (at1
, at2
);
38654 id
= get_identifier ("omp declare target");
38657 error_at (OMP_CLAUSE_LOCATION (c
),
38658 "%qD specified both in declare target %<link%> and %<to%>"
38664 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
38665 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
38668 symtab_node
*node
= symtab_node::get (t
);
38671 node
->offloadable
= 1;
38672 if (ENABLE_OFFLOADING
)
38674 g
->have_offload
= true;
38675 if (is_a
<varpool_node
*> (node
))
38676 vec_safe_push (offload_vars
, t
);
38684 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38686 const char *p
= "";
38687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38689 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38690 p
= IDENTIFIER_POINTER (id
);
38692 if (strcmp (p
, "declare") == 0)
38694 cp_lexer_consume_token (parser
->lexer
);
38696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38698 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38699 p
= IDENTIFIER_POINTER (id
);
38701 if (strcmp (p
, "target") == 0)
38702 cp_lexer_consume_token (parser
->lexer
);
38705 cp_parser_error (parser
, "expected %<target%>");
38706 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38712 cp_parser_error (parser
, "expected %<declare%>");
38713 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38716 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38717 if (!scope_chain
->omp_declare_target_attribute
)
38718 error_at (pragma_tok
->location
,
38719 "%<#pragma omp end declare target%> without corresponding "
38720 "%<#pragma omp declare target%>");
38722 scope_chain
->omp_declare_target_attribute
--;
38725 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38726 expression and optional initializer clause of
38727 #pragma omp declare reduction. We store the expression(s) as
38728 either 3, 6 or 7 special statements inside of the artificial function's
38729 body. The first two statements are DECL_EXPRs for the artificial
38730 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38731 expression that uses those variables.
38732 If there was any INITIALIZER clause, this is followed by further statements,
38733 the fourth and fifth statements are DECL_EXPRs for the artificial
38734 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38735 constructor variant (first token after open paren is not omp_priv),
38736 then the sixth statement is a statement with the function call expression
38737 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38738 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38739 to initialize the OMP_PRIV artificial variable and there is seventh
38740 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38743 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
38745 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
38746 gcc_assert (TYPE_REF_P (type
));
38747 type
= TREE_TYPE (type
);
38748 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
38749 DECL_ARTIFICIAL (omp_out
) = 1;
38750 pushdecl (omp_out
);
38751 add_decl_expr (omp_out
);
38752 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
38753 DECL_ARTIFICIAL (omp_in
) = 1;
38755 add_decl_expr (omp_in
);
38757 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
38759 keep_next_level (true);
38760 tree block
= begin_omp_structured_block ();
38761 combiner
= cp_parser_expression (parser
);
38762 finish_expr_stmt (combiner
);
38763 block
= finish_omp_structured_block (block
);
38766 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38769 const char *p
= "";
38770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38772 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38773 p
= IDENTIFIER_POINTER (id
);
38776 if (strcmp (p
, "initializer") == 0)
38778 cp_lexer_consume_token (parser
->lexer
);
38779 matching_parens parens
;
38780 if (!parens
.require_open (parser
))
38784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38786 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38787 p
= IDENTIFIER_POINTER (id
);
38790 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
38791 DECL_ARTIFICIAL (omp_priv
) = 1;
38792 pushdecl (omp_priv
);
38793 add_decl_expr (omp_priv
);
38794 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
38795 DECL_ARTIFICIAL (omp_orig
) = 1;
38796 pushdecl (omp_orig
);
38797 add_decl_expr (omp_orig
);
38799 keep_next_level (true);
38800 block
= begin_omp_structured_block ();
38803 if (strcmp (p
, "omp_priv") == 0)
38805 bool is_direct_init
, is_non_constant_init
;
38807 cp_lexer_consume_token (parser
->lexer
);
38808 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
38810 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
38811 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
38813 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
38814 == CPP_CLOSE_PAREN
))
38816 finish_omp_structured_block (block
);
38817 error ("invalid initializer clause");
38820 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
38821 &is_non_constant_init
);
38822 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
38823 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
38827 cp_parser_parse_tentatively (parser
);
38828 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
38829 /*check_dependency_p=*/true,
38830 /*template_p=*/NULL
,
38831 /*declarator_p=*/false,
38832 /*optional_p=*/false);
38833 vec
<tree
, va_gc
> *args
;
38834 if (fn_name
== error_mark_node
38835 || cp_parser_error_occurred (parser
)
38836 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
38837 || ((args
= cp_parser_parenthesized_expression_list
38838 (parser
, non_attr
, /*cast_p=*/false,
38839 /*allow_expansion_p=*/true,
38840 /*non_constant_p=*/NULL
)),
38841 cp_parser_error_occurred (parser
)))
38843 finish_omp_structured_block (block
);
38844 cp_parser_abort_tentative_parse (parser
);
38845 cp_parser_error (parser
, "expected id-expression (arguments)");
38850 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
38851 if (arg
== omp_priv
38852 || (TREE_CODE (arg
) == ADDR_EXPR
38853 && TREE_OPERAND (arg
, 0) == omp_priv
))
38855 cp_parser_abort_tentative_parse (parser
);
38856 if (arg
== NULL_TREE
)
38857 error ("one of the initializer call arguments should be %<omp_priv%>"
38858 " or %<&omp_priv%>");
38859 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
38861 finish_expr_stmt (initializer
);
38864 block
= finish_omp_structured_block (block
);
38865 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
38869 add_decl_expr (omp_orig
);
38871 if (!parens
.require_close (parser
))
38875 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
38876 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
38883 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38884 initializer-clause[opt] new-line
38886 initializer-clause:
38887 initializer (omp_priv initializer)
38888 initializer (function-name (argument-list)) */
38891 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
38892 enum pragma_context
)
38894 auto_vec
<tree
> types
;
38895 enum tree_code reduc_code
= ERROR_MARK
;
38896 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
38898 cp_token
*first_token
;
38899 cp_token_cache
*cp
;
38903 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38904 p
= obstack_alloc (&declarator_obstack
, 0);
38906 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38909 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
38912 reduc_code
= PLUS_EXPR
;
38915 reduc_code
= MULT_EXPR
;
38918 reduc_code
= MINUS_EXPR
;
38921 reduc_code
= BIT_AND_EXPR
;
38924 reduc_code
= BIT_XOR_EXPR
;
38927 reduc_code
= BIT_IOR_EXPR
;
38930 reduc_code
= TRUTH_ANDIF_EXPR
;
38933 reduc_code
= TRUTH_ORIF_EXPR
;
38936 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
38939 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38940 "%<|%>, %<&&%>, %<||%> or identifier");
38944 if (reduc_code
!= ERROR_MARK
)
38945 cp_lexer_consume_token (parser
->lexer
);
38947 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
38948 if (reduc_id
== error_mark_node
)
38951 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
38954 /* Types may not be defined in declare reduction type list. */
38955 const char *saved_message
;
38956 saved_message
= parser
->type_definition_forbidden_message
;
38957 parser
->type_definition_forbidden_message
38958 = G_("types may not be defined in declare reduction type list");
38959 bool saved_colon_corrects_to_scope_p
;
38960 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38961 parser
->colon_corrects_to_scope_p
= false;
38962 bool saved_colon_doesnt_start_class_def_p
;
38963 saved_colon_doesnt_start_class_def_p
38964 = parser
->colon_doesnt_start_class_def_p
;
38965 parser
->colon_doesnt_start_class_def_p
= true;
38969 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38970 type
= cp_parser_type_id (parser
);
38971 if (type
== error_mark_node
)
38973 else if (ARITHMETIC_TYPE_P (type
)
38974 && (orig_reduc_id
== NULL_TREE
38975 || (TREE_CODE (type
) != COMPLEX_TYPE
38976 && (id_equal (orig_reduc_id
, "min")
38977 || id_equal (orig_reduc_id
, "max")))))
38978 error_at (loc
, "predeclared arithmetic type %qT in "
38979 "%<#pragma omp declare reduction%>", type
);
38980 else if (TREE_CODE (type
) == FUNCTION_TYPE
38981 || TREE_CODE (type
) == METHOD_TYPE
38982 || TREE_CODE (type
) == ARRAY_TYPE
)
38983 error_at (loc
, "function or array type %qT in "
38984 "%<#pragma omp declare reduction%>", type
);
38985 else if (TYPE_REF_P (type
))
38986 error_at (loc
, "reference type %qT in "
38987 "%<#pragma omp declare reduction%>", type
);
38988 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
38989 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
38990 "%<#pragma omp declare reduction%>", type
);
38992 types
.safe_push (type
);
38994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38995 cp_lexer_consume_token (parser
->lexer
);
39000 /* Restore the saved message. */
39001 parser
->type_definition_forbidden_message
= saved_message
;
39002 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39003 parser
->colon_doesnt_start_class_def_p
39004 = saved_colon_doesnt_start_class_def_p
;
39006 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39007 || types
.is_empty ())
39010 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39014 first_token
= cp_lexer_peek_token (parser
->lexer
);
39017 FOR_EACH_VEC_ELT (types
, i
, type
)
39020 = build_function_type_list (void_type_node
,
39021 cp_build_reference_type (type
, false),
39023 tree this_reduc_id
= reduc_id
;
39024 if (!dependent_type_p (type
))
39025 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39026 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39027 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39028 DECL_ARTIFICIAL (fndecl
) = 1;
39029 DECL_EXTERNAL (fndecl
) = 1;
39030 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39031 DECL_IGNORED_P (fndecl
) = 1;
39032 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39033 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39034 DECL_ATTRIBUTES (fndecl
)
39035 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39036 DECL_ATTRIBUTES (fndecl
));
39037 if (processing_template_decl
)
39038 fndecl
= push_template_decl (fndecl
);
39039 bool block_scope
= false;
39040 tree block
= NULL_TREE
;
39041 if (current_function_decl
)
39043 block_scope
= true;
39044 DECL_CONTEXT (fndecl
) = global_namespace
;
39045 if (!processing_template_decl
)
39048 else if (current_class_type
)
39052 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39053 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39054 cp_lexer_consume_token (parser
->lexer
);
39055 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39057 cp
= cp_token_cache_new (first_token
,
39058 cp_lexer_peek_nth_token (parser
->lexer
,
39061 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39062 finish_member_declaration (fndecl
);
39063 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39064 DECL_PENDING_INLINE_P (fndecl
) = 1;
39065 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39070 DECL_CONTEXT (fndecl
) = current_namespace
;
39074 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39076 block
= begin_omp_structured_block ();
39079 cp_parser_push_lexer_for_tokens (parser
, cp
);
39080 parser
->lexer
->in_pragma
= true;
39082 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39085 finish_function (/*inline_p=*/false);
39087 DECL_CONTEXT (fndecl
) = current_function_decl
;
39089 cp_parser_pop_lexer (parser
);
39093 cp_parser_pop_lexer (parser
);
39095 finish_function (/*inline_p=*/false);
39098 DECL_CONTEXT (fndecl
) = current_function_decl
;
39099 block
= finish_omp_structured_block (block
);
39100 if (TREE_CODE (block
) == BIND_EXPR
)
39101 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39102 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39103 DECL_SAVED_TREE (fndecl
) = block
;
39104 if (processing_template_decl
)
39105 add_decl_expr (fndecl
);
39107 cp_check_omp_declare_reduction (fndecl
);
39108 if (cp
== NULL
&& types
.length () > 1)
39109 cp
= cp_token_cache_new (first_token
,
39110 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39111 if (errs
!= errorcount
)
39115 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39118 /* Free any declarators allocated. */
39119 obstack_free (&declarator_obstack
, p
);
39123 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39124 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39125 initializer-clause[opt] new-line
39126 #pragma omp declare target new-line */
39129 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39130 enum pragma_context context
)
39132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39134 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39135 const char *p
= IDENTIFIER_POINTER (id
);
39137 if (strcmp (p
, "simd") == 0)
39139 cp_lexer_consume_token (parser
->lexer
);
39140 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39144 cp_ensure_no_omp_declare_simd (parser
);
39145 if (strcmp (p
, "reduction") == 0)
39147 cp_lexer_consume_token (parser
->lexer
);
39148 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39152 if (!flag_openmp
) /* flag_openmp_simd */
39154 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39157 if (strcmp (p
, "target") == 0)
39159 cp_lexer_consume_token (parser
->lexer
);
39160 cp_parser_omp_declare_target (parser
, pragma_tok
);
39164 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39166 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39171 #pragma omp requires clauses[optseq] new-line */
39174 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39177 enum omp_requires new_req
= (enum omp_requires
) 0;
39179 location_t loc
= pragma_tok
->location
;
39180 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39182 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39183 cp_lexer_consume_token (parser
->lexer
);
39187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39189 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39190 const char *p
= IDENTIFIER_POINTER (id
);
39191 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39192 enum omp_requires this_req
= (enum omp_requires
) 0;
39194 if (!strcmp (p
, "unified_address"))
39195 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39196 else if (!strcmp (p
, "unified_shared_memory"))
39197 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39198 else if (!strcmp (p
, "dynamic_allocators"))
39199 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39200 else if (!strcmp (p
, "reverse_offload"))
39201 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39202 else if (!strcmp (p
, "atomic_default_mem_order"))
39204 cp_lexer_consume_token (parser
->lexer
);
39206 matching_parens parens
;
39207 if (parens
.require_open (parser
))
39209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39211 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39212 p
= IDENTIFIER_POINTER (id
);
39214 if (!strcmp (p
, "seq_cst"))
39216 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39217 else if (!strcmp (p
, "relaxed"))
39219 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39220 else if (!strcmp (p
, "acq_rel"))
39222 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39226 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39227 "expected %<seq_cst%>, %<relaxed%> or "
39229 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39231 cp_lexer_consume_token (parser
->lexer
);
39234 cp_lexer_consume_token (parser
->lexer
);
39236 if (!parens
.require_close (parser
))
39237 cp_parser_skip_to_closing_parenthesis (parser
,
39238 /*recovering=*/true,
39239 /*or_comma=*/false,
39245 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39253 error_at (cloc
, "expected %<unified_address%>, "
39254 "%<unified_shared_memory%>, "
39255 "%<dynamic_allocators%>, "
39256 "%<reverse_offload%> "
39257 "or %<atomic_default_mem_order%> clause");
39258 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39262 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39263 "supported yet", p
);
39265 cp_lexer_consume_token (parser
->lexer
);
39268 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39270 if ((this_req
& new_req
) != 0)
39271 error_at (cloc
, "too many %qs clauses", p
);
39272 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39273 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39274 error_at (cloc
, "%qs clause used lexically after first "
39275 "target construct or offloading API", p
);
39277 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39279 error_at (cloc
, "too many %qs clauses",
39280 "atomic_default_mem_order");
39281 this_req
= (enum omp_requires
) 0;
39283 else if ((omp_requires_mask
39284 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39286 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39287 " clause in a single compilation unit");
39289 = (enum omp_requires
)
39291 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39293 else if ((omp_requires_mask
39294 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39295 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39296 "lexically after first %<atomic%> construct "
39297 "without memory order clause");
39298 new_req
= (enum omp_requires
) (new_req
| this_req
);
39300 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39306 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39309 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39315 #pragma omp taskloop taskloop-clause[optseq] new-line
39318 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39321 #define OMP_TASKLOOP_CLAUSE_MASK \
39322 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39340 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39341 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39344 tree clauses
, sb
, ret
;
39346 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39348 strcat (p_name
, " taskloop");
39349 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39350 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39352 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39353 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39357 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39358 const char *p
= IDENTIFIER_POINTER (id
);
39360 if (strcmp (p
, "simd") == 0)
39362 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39363 if (cclauses
== NULL
)
39364 cclauses
= cclauses_buf
;
39366 cp_lexer_consume_token (parser
->lexer
);
39367 if (!flag_openmp
) /* flag_openmp_simd */
39368 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39370 sb
= begin_omp_structured_block ();
39371 save
= cp_parser_begin_omp_structured_block (parser
);
39372 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39374 cp_parser_end_omp_structured_block (parser
, save
);
39375 tree body
= finish_omp_structured_block (sb
);
39378 ret
= make_node (OMP_TASKLOOP
);
39379 TREE_TYPE (ret
) = void_type_node
;
39380 OMP_FOR_BODY (ret
) = body
;
39381 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39382 SET_EXPR_LOCATION (ret
, loc
);
39387 if (!flag_openmp
) /* flag_openmp_simd */
39389 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39393 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39397 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39398 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39401 keep_next_level (true);
39402 sb
= begin_omp_structured_block ();
39403 save
= cp_parser_begin_omp_structured_block (parser
);
39405 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39408 cp_parser_end_omp_structured_block (parser
, save
);
39409 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39416 # pragma acc routine oacc-routine-clause[optseq] new-line
39417 function-definition
39419 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39422 #define OACC_ROUTINE_CLAUSE_MASK \
39423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39429 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39430 component, which must resolve to a declared namespace-scope
39431 function. The clauses are either processed directly (for a named
39432 function), or defered until the immediatley following declaration
39436 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39437 enum pragma_context context
)
39439 gcc_checking_assert (context
== pragma_external
);
39440 /* The checking for "another pragma following this one" in the "no optional
39441 '( name )'" case makes sure that we dont re-enter. */
39442 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39444 cp_oacc_routine_data data
;
39445 data
.error_seen
= false;
39446 data
.fndecl_seen
= false;
39447 data
.tokens
= vNULL
;
39448 data
.clauses
= NULL_TREE
;
39449 data
.loc
= pragma_tok
->location
;
39450 /* It is safe to take the address of a local variable; it will only be
39451 used while this scope is live. */
39452 parser
->oacc_routine
= &data
;
39454 /* Look for optional '( name )'. */
39455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39457 matching_parens parens
;
39458 parens
.consume_open (parser
); /* '(' */
39460 /* We parse the name as an id-expression. If it resolves to
39461 anything other than a non-overloaded function at namespace
39462 scope, it's an error. */
39463 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39464 tree name
= cp_parser_id_expression (parser
,
39465 /*template_keyword_p=*/false,
39466 /*check_dependency_p=*/false,
39467 /*template_p=*/NULL
,
39468 /*declarator_p=*/false,
39469 /*optional_p=*/false);
39470 tree decl
= (identifier_p (name
)
39471 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39473 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39474 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39476 if (decl
== error_mark_node
39477 || !parens
.require_close (parser
))
39479 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39480 parser
->oacc_routine
= NULL
;
39485 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39486 "#pragma acc routine",
39487 cp_lexer_peek_token (parser
->lexer
));
39489 if (decl
&& is_overloaded_fn (decl
)
39490 && (TREE_CODE (decl
) != FUNCTION_DECL
39491 || DECL_FUNCTION_TEMPLATE_P (decl
)))
39493 error_at (name_loc
,
39494 "%<#pragma acc routine%> names a set of overloads");
39495 parser
->oacc_routine
= NULL
;
39499 /* Perhaps we should use the same rule as declarations in different
39501 if (!DECL_NAMESPACE_SCOPE_P (decl
))
39503 error_at (name_loc
,
39504 "%qD does not refer to a namespace scope function", decl
);
39505 parser
->oacc_routine
= NULL
;
39509 if (TREE_CODE (decl
) != FUNCTION_DECL
)
39511 error_at (name_loc
, "%qD does not refer to a function", decl
);
39512 parser
->oacc_routine
= NULL
;
39516 cp_finalize_oacc_routine (parser
, decl
, false);
39517 parser
->oacc_routine
= NULL
;
39519 else /* No optional '( name )'. */
39521 /* Store away all pragma tokens. */
39522 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39523 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39524 cp_lexer_consume_token (parser
->lexer
);
39525 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39526 parser
->oacc_routine
->error_seen
= true;
39527 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39528 struct cp_token_cache
*cp
39529 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39530 parser
->oacc_routine
->tokens
.safe_push (cp
);
39532 /* Emit a helpful diagnostic if there's another pragma following this
39534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39536 cp_ensure_no_oacc_routine (parser
);
39537 data
.tokens
.release ();
39538 /* ..., and then just keep going. */
39542 /* We only have to consider the pragma_external case here. */
39543 cp_parser_declaration (parser
);
39544 if (parser
->oacc_routine
39545 && !parser
->oacc_routine
->fndecl_seen
)
39546 cp_ensure_no_oacc_routine (parser
);
39548 parser
->oacc_routine
= NULL
;
39549 data
.tokens
.release ();
39553 /* Finalize #pragma acc routine clauses after direct declarator has
39557 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
39559 struct cp_token_cache
*ce
;
39560 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
39562 if (!data
->error_seen
&& data
->fndecl_seen
)
39564 error_at (data
->loc
,
39565 "%<#pragma acc routine%> not immediately followed by "
39566 "a single function declaration or definition");
39567 data
->error_seen
= true;
39569 if (data
->error_seen
)
39572 gcc_checking_assert (data
->tokens
.length () == 1);
39573 ce
= data
->tokens
[0];
39575 cp_parser_push_lexer_for_tokens (parser
, ce
);
39576 parser
->lexer
->in_pragma
= true;
39577 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39579 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39580 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
39581 parser
->oacc_routine
->clauses
39582 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39583 "#pragma acc routine", pragma_tok
);
39584 cp_parser_pop_lexer (parser
);
39585 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39591 /* Apply any saved OpenACC routine clauses to a just-parsed
39595 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
39597 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
39599 /* Keep going if we're in error reporting mode. */
39600 if (parser
->oacc_routine
->error_seen
39601 || fndecl
== error_mark_node
)
39604 if (parser
->oacc_routine
->fndecl_seen
)
39606 error_at (parser
->oacc_routine
->loc
,
39607 "%<#pragma acc routine%> not immediately followed by"
39608 " a single function declaration or definition");
39609 parser
->oacc_routine
= NULL
;
39612 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
39614 cp_ensure_no_oacc_routine (parser
);
39618 if (oacc_get_fn_attrib (fndecl
))
39620 error_at (parser
->oacc_routine
->loc
,
39621 "%<#pragma acc routine%> already applied to %qD", fndecl
);
39622 parser
->oacc_routine
= NULL
;
39626 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
39628 error_at (parser
->oacc_routine
->loc
,
39630 ? G_("%<#pragma acc routine%> must be applied before use")
39631 : G_("%<#pragma acc routine%> must be applied before "
39633 parser
->oacc_routine
= NULL
;
39637 /* Process the routine's dimension clauses. */
39638 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
39639 oacc_replace_fn_attrib (fndecl
, dims
);
39641 /* Add an "omp declare target" attribute. */
39642 DECL_ATTRIBUTES (fndecl
)
39643 = tree_cons (get_identifier ("omp declare target"),
39644 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
39646 /* Don't unset parser->oacc_routine here: we may still need it to
39647 diagnose wrong usage. But, remember that we've used this "#pragma acc
39649 parser
->oacc_routine
->fndecl_seen
= true;
39653 /* Main entry point to OpenMP statement pragmas. */
39656 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39659 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
39660 omp_clause_mask
mask (0);
39662 switch (cp_parser_pragma_kind (pragma_tok
))
39664 case PRAGMA_OACC_ATOMIC
:
39665 cp_parser_omp_atomic (parser
, pragma_tok
);
39667 case PRAGMA_OACC_CACHE
:
39668 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
39670 case PRAGMA_OACC_DATA
:
39671 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
39673 case PRAGMA_OACC_ENTER_DATA
:
39674 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
39676 case PRAGMA_OACC_EXIT_DATA
:
39677 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
39679 case PRAGMA_OACC_HOST_DATA
:
39680 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
39682 case PRAGMA_OACC_KERNELS
:
39683 case PRAGMA_OACC_PARALLEL
:
39684 strcpy (p_name
, "#pragma acc");
39685 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
39688 case PRAGMA_OACC_LOOP
:
39689 strcpy (p_name
, "#pragma acc");
39690 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39693 case PRAGMA_OACC_UPDATE
:
39694 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
39696 case PRAGMA_OACC_WAIT
:
39697 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
39699 case PRAGMA_OMP_ATOMIC
:
39700 cp_parser_omp_atomic (parser
, pragma_tok
);
39702 case PRAGMA_OMP_CRITICAL
:
39703 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
39705 case PRAGMA_OMP_DISTRIBUTE
:
39706 strcpy (p_name
, "#pragma omp");
39707 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
39710 case PRAGMA_OMP_FOR
:
39711 strcpy (p_name
, "#pragma omp");
39712 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
39715 case PRAGMA_OMP_MASTER
:
39716 strcpy (p_name
, "#pragma omp");
39717 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
39720 case PRAGMA_OMP_PARALLEL
:
39721 strcpy (p_name
, "#pragma omp");
39722 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
39725 case PRAGMA_OMP_SECTIONS
:
39726 strcpy (p_name
, "#pragma omp");
39727 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
39729 case PRAGMA_OMP_SIMD
:
39730 strcpy (p_name
, "#pragma omp");
39731 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
39734 case PRAGMA_OMP_SINGLE
:
39735 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
39737 case PRAGMA_OMP_TASK
:
39738 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
39740 case PRAGMA_OMP_TASKGROUP
:
39741 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
39743 case PRAGMA_OMP_TASKLOOP
:
39744 strcpy (p_name
, "#pragma omp");
39745 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39748 case PRAGMA_OMP_TEAMS
:
39749 strcpy (p_name
, "#pragma omp");
39750 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
39754 gcc_unreachable ();
39757 protected_set_expr_location (stmt
, pragma_tok
->location
);
39760 /* Transactional Memory parsing routines. */
39762 /* Parse a transaction attribute.
39768 We use this instead of cp_parser_attributes_opt for transactions to avoid
39769 the pedwarn in C++98 mode. */
39772 cp_parser_txn_attribute_opt (cp_parser
*parser
)
39775 tree attr_name
, attr
= NULL
;
39777 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
39778 return cp_parser_attributes_opt (parser
);
39780 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
39782 cp_lexer_consume_token (parser
->lexer
);
39783 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
39786 token
= cp_lexer_peek_token (parser
->lexer
);
39787 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
39789 token
= cp_lexer_consume_token (parser
->lexer
);
39791 attr_name
= (token
->type
== CPP_KEYWORD
39792 /* For keywords, use the canonical spelling,
39793 not the parsed identifier. */
39794 ? ridpointers
[(int) token
->keyword
]
39796 attr
= build_tree_list (attr_name
, NULL_TREE
);
39799 cp_parser_error (parser
, "expected identifier");
39801 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39803 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39807 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39809 transaction-statement:
39810 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39812 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39816 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
39818 unsigned char old_in
= parser
->in_transaction
;
39819 unsigned char this_in
= 1, new_in
;
39820 enum rid keyword
= token
->keyword
;
39821 tree stmt
, attrs
, noex
;
39823 cp_lexer_consume_token (parser
->lexer
);
39825 if (keyword
== RID_TRANSACTION_RELAXED
39826 || keyword
== RID_SYNCHRONIZED
)
39827 this_in
|= TM_STMT_ATTR_RELAXED
;
39830 attrs
= cp_parser_txn_attribute_opt (parser
);
39832 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
39835 /* Parse a noexcept specification. */
39836 if (keyword
== RID_ATOMIC_NOEXCEPT
)
39837 noex
= boolean_true_node
;
39838 else if (keyword
== RID_ATOMIC_CANCEL
)
39840 /* cancel-and-throw is unimplemented. */
39841 sorry ("atomic_cancel");
39845 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
39847 /* Keep track if we're in the lexical scope of an outer transaction. */
39848 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
39850 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
39852 parser
->in_transaction
= new_in
;
39853 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
39854 parser
->in_transaction
= old_in
;
39856 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
39861 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39863 transaction-expression:
39864 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39865 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39869 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
39871 unsigned char old_in
= parser
->in_transaction
;
39872 unsigned char this_in
= 1;
39876 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39878 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
39879 || keyword
== RID_TRANSACTION_RELAXED
);
39883 keyword
== RID_TRANSACTION_RELAXED
39884 ? G_("%<__transaction_relaxed%> without transactional memory "
39886 : G_("%<__transaction_atomic%> without transactional memory "
39887 "support enabled"));
39889 token
= cp_parser_require_keyword (parser
, keyword
,
39890 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
39891 : RT_TRANSACTION_RELAXED
));
39892 gcc_assert (token
!= NULL
);
39894 if (keyword
== RID_TRANSACTION_RELAXED
)
39895 this_in
|= TM_STMT_ATTR_RELAXED
;
39897 /* Set this early. This might mean that we allow transaction_cancel in
39898 an expression that we find out later actually has to be a constexpr.
39899 However, we expect that cxx_constant_value will be able to deal with
39900 this; also, if the noexcept has no constexpr, then what we parse next
39901 really is a transaction's body. */
39902 parser
->in_transaction
= this_in
;
39904 /* Parse a noexcept specification. */
39905 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
39908 if (!noex
|| !noex_expr
39909 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39911 matching_parens parens
;
39912 parens
.require_open (parser
);
39914 expr
= cp_parser_expression (parser
);
39915 expr
= finish_parenthesized_expr (expr
);
39917 parens
.require_close (parser
);
39921 /* The only expression that is available got parsed for the noexcept
39922 already. noexcept is true then. */
39924 noex
= boolean_true_node
;
39927 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
39928 parser
->in_transaction
= old_in
;
39930 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
39931 return error_mark_node
;
39933 return (flag_tm
? expr
: error_mark_node
);
39936 /* Parse a function-transaction-block.
39938 function-transaction-block:
39939 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39941 __transaction_atomic txn-attribute[opt] function-try-block
39942 __transaction_relaxed ctor-initializer[opt] function-body
39943 __transaction_relaxed function-try-block
39947 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
39949 unsigned char old_in
= parser
->in_transaction
;
39950 unsigned char new_in
= 1;
39951 tree compound_stmt
, stmt
, attrs
;
39954 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
39955 || keyword
== RID_TRANSACTION_RELAXED
);
39956 token
= cp_parser_require_keyword (parser
, keyword
,
39957 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
39958 : RT_TRANSACTION_RELAXED
));
39959 gcc_assert (token
!= NULL
);
39961 if (keyword
== RID_TRANSACTION_RELAXED
)
39962 new_in
|= TM_STMT_ATTR_RELAXED
;
39965 attrs
= cp_parser_txn_attribute_opt (parser
);
39967 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
39970 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
39972 parser
->in_transaction
= new_in
;
39974 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
39975 cp_parser_function_try_block (parser
);
39977 cp_parser_ctor_initializer_opt_and_function_body
39978 (parser
, /*in_function_try_block=*/false);
39980 parser
->in_transaction
= old_in
;
39982 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
39985 /* Parse a __transaction_cancel statement.
39988 __transaction_cancel txn-attribute[opt] ;
39989 __transaction_cancel txn-attribute[opt] throw-expression ;
39991 ??? Cancel and throw is not yet implemented. */
39994 cp_parser_transaction_cancel (cp_parser
*parser
)
39997 bool is_outer
= false;
40000 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40001 RT_TRANSACTION_CANCEL
);
40002 gcc_assert (token
!= NULL
);
40004 attrs
= cp_parser_txn_attribute_opt (parser
);
40006 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40008 /* ??? Parse cancel-and-throw here. */
40010 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40014 error_at (token
->location
, "%<__transaction_cancel%> without "
40015 "transactional memory support enabled");
40016 return error_mark_node
;
40018 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40020 error_at (token
->location
, "%<__transaction_cancel%> within a "
40021 "%<__transaction_relaxed%>");
40022 return error_mark_node
;
40026 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40027 && !is_tm_may_cancel_outer (current_function_decl
))
40029 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40030 "within outer %<__transaction_atomic%>");
40031 error_at (token
->location
,
40032 " or a %<transaction_may_cancel_outer%> function");
40033 return error_mark_node
;
40036 else if (parser
->in_transaction
== 0)
40038 error_at (token
->location
, "%<__transaction_cancel%> not within "
40039 "%<__transaction_atomic%>");
40040 return error_mark_node
;
40043 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40051 static GTY (()) cp_parser
*the_parser
;
40054 /* Special handling for the first token or line in the file. The first
40055 thing in the file might be #pragma GCC pch_preprocess, which loads a
40056 PCH file, which is a GC collection point. So we need to handle this
40057 first pragma without benefit of an existing lexer structure.
40059 Always returns one token to the caller in *FIRST_TOKEN. This is
40060 either the true first token of the file, or the first token after
40061 the initial pragma. */
40064 cp_parser_initial_pragma (cp_token
*first_token
)
40068 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40069 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40072 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40073 if (first_token
->type
== CPP_STRING
)
40075 name
= first_token
->u
.value
;
40077 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40078 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40079 error_at (first_token
->location
,
40080 "junk at end of %<#pragma GCC pch_preprocess%>");
40083 error_at (first_token
->location
, "expected string literal");
40085 /* Skip to the end of the pragma. */
40086 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40087 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40089 /* Now actually load the PCH file. */
40091 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40093 /* Read one more token to return to our caller. We have to do this
40094 after reading the PCH file in, since its pointers have to be
40096 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40099 /* Parse a pragma GCC ivdep. */
40102 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40104 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40108 /* Parse a pragma GCC unroll. */
40110 static unsigned short
40111 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40113 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40114 tree expr
= cp_parser_constant_expression (parser
);
40115 unsigned short unroll
;
40116 expr
= maybe_constant_value (expr
);
40117 HOST_WIDE_INT lunroll
= 0;
40118 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40119 || TREE_CODE (expr
) != INTEGER_CST
40120 || (lunroll
= tree_to_shwi (expr
)) < 0
40121 || lunroll
>= USHRT_MAX
)
40123 error_at (location
, "%<#pragma GCC unroll%> requires an"
40124 " assignment-expression that evaluates to a non-negative"
40125 " integral constant less than %u", USHRT_MAX
);
40130 unroll
= (unsigned short)lunroll
;
40134 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40138 /* Normal parsing of a pragma token. Here we can (and must) use the
40142 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40144 cp_token
*pragma_tok
;
40149 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40150 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40151 parser
->lexer
->in_pragma
= true;
40153 id
= cp_parser_pragma_kind (pragma_tok
);
40154 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40155 cp_ensure_no_omp_declare_simd (parser
);
40158 case PRAGMA_GCC_PCH_PREPROCESS
:
40159 error_at (pragma_tok
->location
,
40160 "%<#pragma GCC pch_preprocess%> must be first");
40163 case PRAGMA_OMP_BARRIER
:
40166 case pragma_compound
:
40167 cp_parser_omp_barrier (parser
, pragma_tok
);
40170 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40171 "used in compound statements", "omp barrier");
40178 case PRAGMA_OMP_DEPOBJ
:
40181 case pragma_compound
:
40182 cp_parser_omp_depobj (parser
, pragma_tok
);
40185 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40186 "used in compound statements", "omp depobj");
40193 case PRAGMA_OMP_FLUSH
:
40196 case pragma_compound
:
40197 cp_parser_omp_flush (parser
, pragma_tok
);
40200 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40201 "used in compound statements", "omp flush");
40208 case PRAGMA_OMP_TASKWAIT
:
40211 case pragma_compound
:
40212 cp_parser_omp_taskwait (parser
, pragma_tok
);
40215 error_at (pragma_tok
->location
,
40216 "%<#pragma %s%> may only be used in compound statements",
40224 case PRAGMA_OMP_TASKYIELD
:
40227 case pragma_compound
:
40228 cp_parser_omp_taskyield (parser
, pragma_tok
);
40231 error_at (pragma_tok
->location
,
40232 "%<#pragma %s%> may only be used in compound statements",
40240 case PRAGMA_OMP_CANCEL
:
40243 case pragma_compound
:
40244 cp_parser_omp_cancel (parser
, pragma_tok
);
40247 error_at (pragma_tok
->location
,
40248 "%<#pragma %s%> may only be used in compound statements",
40256 case PRAGMA_OMP_CANCELLATION_POINT
:
40257 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40260 case PRAGMA_OMP_THREADPRIVATE
:
40261 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40264 case PRAGMA_OMP_DECLARE
:
40265 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40267 case PRAGMA_OACC_DECLARE
:
40268 cp_parser_oacc_declare (parser
, pragma_tok
);
40271 case PRAGMA_OACC_ENTER_DATA
:
40272 if (context
== pragma_stmt
)
40274 error_at (pragma_tok
->location
,
40275 "%<#pragma %s%> may only be used in compound statements",
40279 else if (context
!= pragma_compound
)
40281 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40284 case PRAGMA_OACC_EXIT_DATA
:
40285 if (context
== pragma_stmt
)
40287 error_at (pragma_tok
->location
,
40288 "%<#pragma %s%> may only be used in compound statements",
40292 else if (context
!= pragma_compound
)
40294 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40297 case PRAGMA_OACC_ROUTINE
:
40298 if (context
!= pragma_external
)
40300 error_at (pragma_tok
->location
,
40301 "%<#pragma acc routine%> must be at file scope");
40304 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40307 case PRAGMA_OACC_UPDATE
:
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_WAIT
:
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_ATOMIC
:
40334 case PRAGMA_OACC_CACHE
:
40335 case PRAGMA_OACC_DATA
:
40336 case PRAGMA_OACC_HOST_DATA
:
40337 case PRAGMA_OACC_KERNELS
:
40338 case PRAGMA_OACC_PARALLEL
:
40339 case PRAGMA_OACC_LOOP
:
40340 case PRAGMA_OMP_ATOMIC
:
40341 case PRAGMA_OMP_CRITICAL
:
40342 case PRAGMA_OMP_DISTRIBUTE
:
40343 case PRAGMA_OMP_FOR
:
40344 case PRAGMA_OMP_MASTER
:
40345 case PRAGMA_OMP_PARALLEL
:
40346 case PRAGMA_OMP_SECTIONS
:
40347 case PRAGMA_OMP_SIMD
:
40348 case PRAGMA_OMP_SINGLE
:
40349 case PRAGMA_OMP_TASK
:
40350 case PRAGMA_OMP_TASKGROUP
:
40351 case PRAGMA_OMP_TASKLOOP
:
40352 case PRAGMA_OMP_TEAMS
:
40353 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40355 stmt
= push_omp_privatization_clauses (false);
40356 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40357 pop_omp_privatization_clauses (stmt
);
40360 case PRAGMA_OMP_REQUIRES
:
40361 return cp_parser_omp_requires (parser
, pragma_tok
);
40363 case PRAGMA_OMP_ORDERED
:
40364 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40366 stmt
= push_omp_privatization_clauses (false);
40367 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40368 pop_omp_privatization_clauses (stmt
);
40371 case PRAGMA_OMP_TARGET
:
40372 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40374 stmt
= push_omp_privatization_clauses (false);
40375 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40376 pop_omp_privatization_clauses (stmt
);
40379 case PRAGMA_OMP_END_DECLARE_TARGET
:
40380 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40383 case PRAGMA_OMP_SECTION
:
40384 error_at (pragma_tok
->location
,
40385 "%<#pragma omp section%> may only be used in "
40386 "%<#pragma omp sections%> construct");
40391 if (context
== pragma_external
)
40393 error_at (pragma_tok
->location
,
40394 "%<#pragma GCC ivdep%> must be inside a function");
40397 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40398 unsigned short unroll
;
40399 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40400 if (tok
->type
== CPP_PRAGMA
40401 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40403 tok
= cp_lexer_consume_token (parser
->lexer
);
40404 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40405 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40409 if (tok
->type
!= CPP_KEYWORD
40410 || (tok
->keyword
!= RID_FOR
40411 && tok
->keyword
!= RID_WHILE
40412 && tok
->keyword
!= RID_DO
))
40414 cp_parser_error (parser
, "for, while or do statement expected");
40417 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40421 case PRAGMA_UNROLL
:
40423 if (context
== pragma_external
)
40425 error_at (pragma_tok
->location
,
40426 "%<#pragma GCC unroll%> must be inside a function");
40429 const unsigned short unroll
40430 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40432 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40433 if (tok
->type
== CPP_PRAGMA
40434 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40436 tok
= cp_lexer_consume_token (parser
->lexer
);
40437 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40438 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40442 if (tok
->type
!= CPP_KEYWORD
40443 || (tok
->keyword
!= RID_FOR
40444 && tok
->keyword
!= RID_WHILE
40445 && tok
->keyword
!= RID_DO
))
40447 cp_parser_error (parser
, "for, while or do statement expected");
40450 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40455 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40456 c_invoke_pragma_handler (id
);
40460 cp_parser_error (parser
, "expected declaration specifiers");
40464 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40468 /* The interface the pragma parsers have to the lexer. */
40471 pragma_lex (tree
*value
, location_t
*loc
)
40473 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40474 enum cpp_ttype ret
= tok
->type
;
40476 *value
= tok
->u
.value
;
40478 *loc
= tok
->location
;
40480 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
40482 else if (ret
== CPP_STRING
)
40483 *value
= cp_parser_string_literal (the_parser
, false, false);
40486 if (ret
== CPP_KEYWORD
)
40488 cp_lexer_consume_token (the_parser
->lexer
);
40495 /* External interface. */
40497 /* Parse one entire translation unit. */
40500 c_parse_file (void)
40502 static bool already_called
= false;
40504 if (already_called
)
40505 fatal_error (input_location
,
40506 "inter-module optimizations not implemented for C++");
40507 already_called
= true;
40509 the_parser
= cp_parser_new ();
40510 push_deferring_access_checks (flag_access_control
40511 ? dk_no_deferred
: dk_no_check
);
40512 cp_parser_translation_unit (the_parser
);
40515 finish_translation_unit ();
40518 /* Create an identifier for a generic parameter type (a synthesized
40519 template parameter implied by `auto' or a concept identifier). */
40521 static GTY(()) int generic_parm_count
;
40523 make_generic_type_name ()
40526 sprintf (buf
, "auto:%d", ++generic_parm_count
);
40527 return get_identifier (buf
);
40530 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40531 (creating a new template parameter list if necessary). Returns the newly
40532 created template type parm. */
40535 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
40537 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
40539 /* Before committing to modifying any scope, if we're in an
40540 implicit template scope, and we're trying to synthesize a
40541 constrained parameter, try to find a previous parameter with
40542 the same name. This is the same-type rule for abbreviated
40543 function templates.
40545 NOTE: We can generate implicit parameters when tentatively
40546 parsing a nested name specifier, only to reject that parse
40547 later. However, matching the same template-id as part of a
40548 direct-declarator should generate an identical template
40549 parameter, so this rule will merge them. */
40550 if (parser
->implicit_template_scope
&& constr
)
40552 tree t
= parser
->implicit_template_parms
;
40555 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
40557 tree d
= TREE_VALUE (t
);
40558 if (TREE_CODE (d
) == PARM_DECL
)
40559 /* Return the TEMPLATE_PARM_INDEX. */
40560 d
= DECL_INITIAL (d
);
40563 t
= TREE_CHAIN (t
);
40567 /* We are either continuing a function template that already contains implicit
40568 template parameters, creating a new fully-implicit function template, or
40569 extending an existing explicit function template with implicit template
40572 cp_binding_level
*const entry_scope
= current_binding_level
;
40574 bool become_template
= false;
40575 cp_binding_level
*parent_scope
= 0;
40577 if (parser
->implicit_template_scope
)
40579 gcc_assert (parser
->implicit_template_parms
);
40581 current_binding_level
= parser
->implicit_template_scope
;
40585 /* Roll back to the existing template parameter scope (in the case of
40586 extending an explicit function template) or introduce a new template
40587 parameter scope ahead of the function parameter scope (or class scope
40588 in the case of out-of-line member definitions). The function scope is
40589 added back after template parameter synthesis below. */
40591 cp_binding_level
*scope
= entry_scope
;
40593 while (scope
->kind
== sk_function_parms
)
40595 parent_scope
= scope
;
40596 scope
= scope
->level_chain
;
40598 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
40600 /* If not defining a class, then any class scope is a scope level in
40601 an out-of-line member definition. In this case simply wind back
40602 beyond the first such scope to inject the template parameter list.
40603 Otherwise wind back to the class being defined. The latter can
40604 occur in class member friend declarations such as:
40610 friend void A::foo (auto);
40613 The template parameter list synthesized for the friend declaration
40614 must be injected in the scope of 'B'. This can also occur in
40615 erroneous cases such as:
40621 void B::foo (auto) {}
40624 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40625 but, nevertheless, the template parameter list synthesized for the
40626 declarator should be injected into the scope of 'A' as if the
40627 ill-formed template was specified explicitly. */
40629 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
40631 parent_scope
= scope
;
40632 scope
= scope
->level_chain
;
40636 current_binding_level
= scope
;
40638 if (scope
->kind
!= sk_template_parms
40639 || !function_being_declared_is_template_p (parser
))
40641 /* Introduce a new template parameter list for implicit template
40644 become_template
= true;
40646 parser
->implicit_template_scope
40647 = begin_scope (sk_template_parms
, NULL
);
40649 ++processing_template_decl
;
40651 parser
->fully_implicit_function_template_p
= true;
40652 ++parser
->num_template_parameter_lists
;
40656 /* Synthesize implicit template parameters at the end of the explicit
40657 template parameter list. */
40659 gcc_assert (current_template_parms
);
40661 parser
->implicit_template_scope
= scope
;
40663 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40664 parser
->implicit_template_parms
40665 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
40669 /* Synthesize a new template parameter and track the current template
40670 parameter chain with implicit_template_parms. */
40672 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
40673 tree synth_id
= make_generic_type_name ();
40674 tree synth_tmpl_parm
;
40675 bool non_type
= false;
40677 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
40679 = finish_template_type_parm (class_type_node
, synth_id
);
40680 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
40682 = finish_constrained_template_template_parm (proto
, synth_id
);
40685 synth_tmpl_parm
= copy_decl (proto
);
40686 DECL_NAME (synth_tmpl_parm
) = synth_id
;
40690 // Attach the constraint to the parm before processing.
40691 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
40692 TREE_TYPE (node
) = constr
;
40694 = process_template_parm (parser
->implicit_template_parms
,
40697 /*non_type=*/non_type
,
40698 /*param_pack=*/false);
40700 // Chain the new parameter to the list of implicit parameters.
40701 if (parser
->implicit_template_parms
)
40702 parser
->implicit_template_parms
40703 = TREE_CHAIN (parser
->implicit_template_parms
);
40705 parser
->implicit_template_parms
= new_parm
;
40707 tree new_decl
= get_local_decls ();
40709 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40710 new_decl
= DECL_INITIAL (new_decl
);
40712 /* If creating a fully implicit function template, start the new implicit
40713 template parameter list with this synthesized type, otherwise grow the
40714 current template parameter list. */
40716 if (become_template
)
40718 parent_scope
->level_chain
= current_binding_level
;
40720 tree new_parms
= make_tree_vec (1);
40721 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
40722 current_template_parms
= tree_cons (size_int (processing_template_decl
),
40723 new_parms
, current_template_parms
);
40727 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40728 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
40729 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
40730 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
40733 // If the new parameter was constrained, we need to add that to the
40734 // constraints in the template parameter list.
40735 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
40737 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
40738 reqs
= conjoin_constraints (reqs
, req
);
40739 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
40742 current_binding_level
= entry_scope
;
40747 /* Finish the declaration of a fully implicit function template. Such a
40748 template has no explicit template parameter list so has not been through the
40749 normal template head and tail processing. synthesize_implicit_template_parm
40750 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40751 provided if the declaration is a class member such that its template
40752 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40753 form is returned. Otherwise NULL_TREE is returned. */
40756 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
40758 gcc_assert (parser
->fully_implicit_function_template_p
);
40760 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
40761 && DECL_VIRTUAL_P (member_decl_opt
))
40763 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
40764 "implicit templates may not be %<virtual%>");
40765 DECL_VIRTUAL_P (member_decl_opt
) = false;
40768 if (member_decl_opt
)
40769 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
40770 end_template_decl ();
40772 parser
->fully_implicit_function_template_p
= false;
40773 parser
->implicit_template_parms
= 0;
40774 parser
->implicit_template_scope
= 0;
40775 --parser
->num_template_parameter_lists
;
40777 return member_decl_opt
;
40780 /* Like finish_fully_implicit_template, but to be used in error
40781 recovery, rearranging scopes so that we restore the state we had
40782 before synthesize_implicit_template_parm inserted the implement
40783 template parms scope. */
40786 abort_fully_implicit_template (cp_parser
*parser
)
40788 cp_binding_level
*return_to_scope
= current_binding_level
;
40790 if (parser
->implicit_template_scope
40791 && return_to_scope
!= parser
->implicit_template_scope
)
40793 cp_binding_level
*child
= return_to_scope
;
40794 for (cp_binding_level
*scope
= child
->level_chain
;
40795 scope
!= parser
->implicit_template_scope
;
40796 scope
= child
->level_chain
)
40798 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
40799 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
40800 current_binding_level
= parser
->implicit_template_scope
;
40803 return_to_scope
= return_to_scope
->level_chain
;
40805 finish_fully_implicit_template (parser
, NULL
);
40807 gcc_assert (current_binding_level
== return_to_scope
);
40810 /* Helper function for diagnostics that have complained about things
40811 being used with 'extern "C"' linkage.
40813 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40816 maybe_show_extern_c_location (void)
40818 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
40819 inform (the_parser
->innermost_linkage_specification_location
,
40820 "%<extern \"C\"%> linkage started here");
40823 #include "gt-cp-parser.h"