2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token
=
57 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant
{
63 /* floating-point literal */
67 /* %<__FUNCTION__%> */
69 /* %<__PRETTY_FUNCTION__%> */
77 /* %<typeid%> operator */
79 /* non-constant compound literals */
87 /* an array reference */
93 /* the address of a label */
107 /* calls to overloaded operators */
111 /* a comma operator */
113 /* a call to a constructor */
115 /* a transaction expression */
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error
{
125 /* is not a class or namespace */
127 /* is not a class, namespace, or enumeration */
131 /* The various kinds of required token */
132 enum required_token
{
134 RT_SEMICOLON
, /* ';' */
135 RT_OPEN_PAREN
, /* '(' */
136 RT_CLOSE_BRACE
, /* '}' */
137 RT_OPEN_BRACE
, /* '{' */
138 RT_CLOSE_SQUARE
, /* ']' */
139 RT_OPEN_SQUARE
, /* '[' */
143 RT_GREATER
, /* '>' */
145 RT_ELLIPSIS
, /* '...' */
149 RT_COLON_SCOPE
, /* ':' or '::' */
150 RT_CLOSE_PAREN
, /* ')' */
151 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
152 RT_PRAGMA_EOL
, /* end of line */
153 RT_NAME
, /* identifier */
155 /* The type is CPP_KEYWORD */
157 RT_DELETE
, /* delete */
158 RT_RETURN
, /* return */
159 RT_WHILE
, /* while */
160 RT_EXTERN
, /* extern */
161 RT_STATIC_ASSERT
, /* static_assert */
162 RT_DECLTYPE
, /* decltype */
163 RT_OPERATOR
, /* operator */
164 RT_CLASS
, /* class */
165 RT_TEMPLATE
, /* template */
166 RT_NAMESPACE
, /* namespace */
167 RT_USING
, /* using */
170 RT_CATCH
, /* catch */
171 RT_THROW
, /* throw */
172 RT_LABEL
, /* __label__ */
173 RT_AT_TRY
, /* @try */
174 RT_AT_SYNCHRONIZED
, /* @synchronized */
175 RT_AT_THROW
, /* @throw */
177 RT_SELECT
, /* selection-statement */
178 RT_ITERATION
, /* iteration-statement */
179 RT_JUMP
, /* jump-statement */
180 RT_CLASS_KEY
, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
195 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
197 saved (parser
->in_type_id_in_expr_p
)
198 { parser
->in_type_id_in_expr_p
= set
; }
199 ~type_id_in_expr_sentinel ()
200 { parser
->in_type_id_in_expr_p
= saved
; }
205 static cp_lexer
*cp_lexer_new_main
207 static cp_lexer
*cp_lexer_new_from_tokens
208 (cp_token_cache
*tokens
);
209 static void cp_lexer_destroy
211 static int cp_lexer_saving_tokens
213 static cp_token
*cp_lexer_token_at
214 (cp_lexer
*, cp_token_position
);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer
*, cp_token
*);
217 static inline cp_token
*cp_lexer_peek_token
219 static cp_token
*cp_lexer_peek_nth_token
220 (cp_lexer
*, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer
*, enum cpp_ttype
);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer
*, enum rid
);
227 static cp_token
*cp_lexer_consume_token
229 static void cp_lexer_purge_token
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer
*, cp_token_position
);
233 static void cp_lexer_save_tokens
235 static void cp_lexer_commit_tokens
237 static void cp_lexer_rollback_tokens
239 static void cp_lexer_print_token
240 (FILE *, cp_token
*);
241 static inline bool cp_lexer_debugging_p
243 static void cp_lexer_start_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
245 static void cp_lexer_stop_debugging
246 (cp_lexer
*) ATTRIBUTE_UNUSED
;
248 static cp_token_cache
*cp_token_cache_new
249 (cp_token
*, cp_token
*);
251 static void cp_parser_initial_pragma
254 static bool cp_parser_omp_declare_reduction_exprs
256 static void cp_finalize_oacc_routine
257 (cp_parser
*, tree
, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream
;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand
;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
279 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
280 cp_token
*start_token
, unsigned num
,
281 cp_token
*curr_token
)
283 unsigned i
, nprinted
;
287 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
293 num
= buffer
->length ();
295 if (start_token
== NULL
)
296 start_token
= buffer
->address ();
298 if (start_token
> buffer
->address ())
300 cp_lexer_print_token (file
, &(*buffer
)[0]);
301 fprintf (file
, " ... ");
306 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
308 if (token
== start_token
)
315 if (token
== curr_token
)
316 fprintf (file
, "[[");
318 cp_lexer_print_token (file
, token
);
320 if (token
== curr_token
)
321 fprintf (file
, "]]");
327 case CPP_CLOSE_BRACE
:
337 if (i
== num
&& i
< buffer
->length ())
339 fprintf (file
, " ... ");
340 cp_lexer_print_token (file
, &buffer
->last ());
343 fprintf (file
, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
350 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
352 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
356 debug (vec
<cp_token
, va_gc
> &ref
)
358 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> *ptr
)
367 fprintf (stderr
, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
375 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
379 fprintf (file
, "%s: ", desc
);
380 print_node_brief (file
, "", t
, 0);
385 /* Dump parser context C to FILE. */
388 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
390 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
392 print_node_brief (file
, "", c
->object_type
, 0);
393 fprintf (file
, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
403 cp_parser_context
*c
;
405 fprintf (file
, "Parsing context stack:\n");
406 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
408 fprintf (file
, "\t#%u: ", i
);
409 cp_debug_print_context (file
, c
);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
420 fprintf (file
, "%s: true\n", desc
);
424 /* Print an unparsed function entry UF to FILE. */
427 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
430 cp_default_arg_entry
*default_arg_fn
;
433 fprintf (file
, "\tFunctions with default args:\n");
435 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
438 fprintf (file
, "\t\tClass type: ");
439 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
440 fprintf (file
, "\t\tDeclaration: ");
441 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
442 fprintf (file
, "\n");
445 fprintf (file
, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
449 print_node_brief (file
, "", fn
, 0);
452 fprintf (file
, "\n");
454 fprintf (file
, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
458 print_node_brief (file
, "", fn
, 0);
461 fprintf (file
, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
468 cp_debug_print_unparsed_queues (FILE *file
,
469 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
472 cp_unparsed_functions_entry
*uf
;
474 fprintf (file
, "Unparsed functions\n");
475 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
477 fprintf (file
, "#%u:\n", i
);
478 cp_debug_print_unparsed_function (file
, uf
);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
489 cp_token
*next_token
, *first_token
, *start_token
;
494 next_token
= parser
->lexer
->next_token
;
495 first_token
= parser
->lexer
->buffer
->address ();
496 start_token
= (next_token
> first_token
+ window_size
/ 2)
497 ? next_token
- window_size
/ 2
499 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
508 cp_debug_parser (FILE *file
, cp_parser
*parser
)
510 const size_t window_size
= 20;
512 expanded_location eloc
;
517 fprintf (file
, "Parser state\n\n");
518 fprintf (file
, "Number of tokens: %u\n",
519 vec_safe_length (parser
->lexer
->buffer
));
520 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
521 cp_debug_print_tree_if_set (file
, "Object scope",
522 parser
->object_scope
);
523 cp_debug_print_tree_if_set (file
, "Qualifying scope",
524 parser
->qualifying_scope
);
525 cp_debug_print_context_stack (file
, parser
->context
);
526 cp_debug_print_flag (file
, "Allow GNU extensions",
527 parser
->allow_gnu_extensions_p
);
528 cp_debug_print_flag (file
, "'>' token is greater-than",
529 parser
->greater_than_is_operator_p
);
530 cp_debug_print_flag (file
, "Default args allowed in current "
531 "parameter list", parser
->default_arg_ok_p
);
532 cp_debug_print_flag (file
, "Parsing integral constant-expression",
533 parser
->integral_constant_expression_p
);
534 cp_debug_print_flag (file
, "Allow non-constant expression in current "
535 "constant-expression",
536 parser
->allow_non_integral_constant_expression_p
);
537 cp_debug_print_flag (file
, "Seen non-constant expression",
538 parser
->non_integral_constant_expression_p
);
539 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
541 parser
->local_variables_forbidden_p
);
542 cp_debug_print_flag (file
, "In unbraced linkage specification",
543 parser
->in_unbraced_linkage_specification_p
);
544 cp_debug_print_flag (file
, "Parsing a declarator",
545 parser
->in_declarator_p
);
546 cp_debug_print_flag (file
, "In template argument list",
547 parser
->in_template_argument_list_p
);
548 cp_debug_print_flag (file
, "Parsing an iteration statement",
549 parser
->in_statement
& IN_ITERATION_STMT
);
550 cp_debug_print_flag (file
, "Parsing a switch statement",
551 parser
->in_statement
& IN_SWITCH_STMT
);
552 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
553 parser
->in_statement
& IN_OMP_BLOCK
);
554 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
555 parser
->in_statement
& IN_OMP_FOR
);
556 cp_debug_print_flag (file
, "Parsing an if statement",
557 parser
->in_statement
& IN_IF_STMT
);
558 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
559 "context", parser
->in_type_id_in_expr_p
);
560 cp_debug_print_flag (file
, "String expressions should be translated "
561 "to execution character set",
562 parser
->translate_strings_p
);
563 cp_debug_print_flag (file
, "Parsing function body outside of a "
564 "local class", parser
->in_function_body
);
565 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
566 parser
->colon_corrects_to_scope_p
);
567 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
568 parser
->colon_doesnt_start_class_def_p
);
569 if (parser
->type_definition_forbidden_message
)
570 fprintf (file
, "Error message for forbidden type definitions: %s\n",
571 parser
->type_definition_forbidden_message
);
572 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
573 fprintf (file
, "Number of class definitions in progress: %u\n",
574 parser
->num_classes_being_defined
);
575 fprintf (file
, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser
->num_template_parameter_lists
);
577 cp_debug_parser_tokens (file
, parser
, window_size
);
578 token
= parser
->lexer
->next_token
;
579 fprintf (file
, "Next token to parse:\n");
580 fprintf (file
, "\tToken: ");
581 cp_lexer_print_token (file
, token
);
582 eloc
= expand_location (token
->location
);
583 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
584 fprintf (file
, "\tLine: %d\n", eloc
.line
);
585 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
589 debug (cp_parser
&ref
)
591 cp_debug_parser (stderr
, &ref
);
595 debug (cp_parser
*ptr
)
600 fprintf (stderr
, "<nil>\n");
603 /* Allocate memory for a new lexer object and return it. */
606 cp_lexer_alloc (void)
610 c_common_no_more_pch ();
612 /* Allocate the memory. */
613 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
615 /* Initially we are not debugging. */
616 lexer
->debugging_p
= false;
618 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
620 /* Create the buffer. */
621 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 cp_lexer_new_main (void)
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token
);
641 lexer
= cp_lexer_alloc ();
643 /* Put the first token in the buffer. */
644 lexer
->buffer
->quick_push (token
);
646 /* Get the remaining tokens from the preprocessor. */
647 while (token
.type
!= CPP_EOF
)
649 cp_lexer_get_preprocessor_token (lexer
, &token
);
650 vec_safe_push (lexer
->buffer
, token
);
653 lexer
->last_token
= lexer
->buffer
->address ()
654 + lexer
->buffer
->length ()
656 lexer
->next_token
= lexer
->buffer
->length ()
657 ? lexer
->buffer
->address ()
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
664 gcc_assert (!lexer
->next_token
->purged_p
);
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
674 cp_token
*first
= cache
->first
;
675 cp_token
*last
= cache
->last
;
676 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
678 /* We do not own the buffer. */
679 lexer
->buffer
= NULL
;
680 lexer
->next_token
= first
== last
? &eof_token
: first
;
681 lexer
->last_token
= last
;
683 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
685 /* Initially we are not debugging. */
686 lexer
->debugging_p
= false;
688 gcc_assert (!lexer
->next_token
->purged_p
);
692 /* Frees all resources associated with LEXER. */
695 cp_lexer_destroy (cp_lexer
*lexer
)
697 vec_free (lexer
->buffer
);
698 lexer
->saved_tokens
.release ();
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
707 #define LEXER_DEBUGGING_ENABLED_P false
709 /* Returns nonzero if debugging information should be output. */
712 cp_lexer_debugging_p (cp_lexer
*lexer
)
714 if (!LEXER_DEBUGGING_ENABLED_P
)
717 return lexer
->debugging_p
;
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
724 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
726 return lexer
->next_token
- previous_p
;
729 static inline cp_token
*
730 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
736 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
738 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer
*lexer
)
744 if (lexer
->next_token
== &eof_token
)
745 return lexer
->last_token
- 1;
747 return cp_lexer_token_position (lexer
, true);
750 static inline cp_token
*
751 cp_lexer_previous_token (cp_lexer
*lexer
)
753 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
755 /* Skip past purged tokens. */
758 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
762 return cp_lexer_token_at (lexer
, tp
);
765 /* nonzero if we are presently saving tokens. */
768 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
770 return lexer
->saved_tokens
.length () != 0;
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
779 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
781 static int is_extern_c
= 0;
783 /* Get a new token from the preprocessor. */
785 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
786 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
787 token
->keyword
= RID_MAX
;
788 token
->purged_p
= false;
789 token
->error_reported
= false;
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c
+= pending_lang_change
;
795 pending_lang_change
= 0;
796 token
->implicit_extern_c
= is_extern_c
> 0;
798 /* Check to see if this token is a keyword. */
799 if (token
->type
== CPP_NAME
)
801 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
803 /* Mark this token as a keyword. */
804 token
->type
= CPP_KEYWORD
;
805 /* Record which keyword. */
806 token
->keyword
= C_RID_CODE (token
->u
.value
);
810 if (warn_cxx11_compat
811 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
812 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
814 /* Warn about the C++0x keyword (but still treat it as
816 warning (OPT_Wc__11_compat
,
817 "identifier %qE is a keyword in C++11",
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
825 token
->keyword
= RID_MAX
;
828 else if (token
->type
== CPP_AT_NAME
)
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token
->type
= CPP_KEYWORD
;
832 switch (C_RID_CODE (token
->u
.value
))
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
842 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
843 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
844 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
845 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
846 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
847 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
848 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
849 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
850 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
855 /* Update the globals input_location and the input file stack from TOKEN. */
857 cp_lexer_set_source_position_from_token (cp_token
*token
)
859 if (token
->type
!= CPP_EOF
)
861 input_location
= token
->location
;
865 /* Update the globals input_location and the input file stack from LEXER. */
867 cp_lexer_set_source_position (cp_lexer
*lexer
)
869 cp_token
*token
= cp_lexer_peek_token (lexer
);
870 cp_lexer_set_source_position_from_token (token
);
873 /* Return a pointer to the next token in the token stream, but do not
876 static inline cp_token
*
877 cp_lexer_peek_token (cp_lexer
*lexer
)
879 if (cp_lexer_debugging_p (lexer
))
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
882 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
883 putc ('\n', cp_lexer_debug_stream
);
885 return lexer
->next_token
;
888 /* Return true if the next token has the indicated TYPE. */
891 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
893 return cp_lexer_peek_token (lexer
)->type
== type
;
896 /* Return true if the next token does not have the indicated TYPE. */
899 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
901 return !cp_lexer_next_token_is (lexer
, type
);
904 /* Return true if the next token is the indicated KEYWORD. */
907 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
909 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
913 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
915 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
919 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
921 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
924 /* Return true if KEYWORD can start a decl-specifier. */
927 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
934 /* Storage classes. */
940 /* Elaborated type specifiers. */
946 /* Simple type specifiers. */
960 /* GNU extensions. */
963 /* C++0x extensions. */
965 case RID_UNDERLYING_TYPE
:
970 if (keyword
>= RID_FIRST_INT_N
971 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
972 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
978 /* Return true if the next token is a keyword for a decl-specifier. */
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
985 token
= cp_lexer_peek_token (lexer
);
986 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
989 /* Returns TRUE iff the token T begins a decltype type. */
992 token_is_decltype (cp_token
*t
)
994 return (t
->keyword
== RID_DECLTYPE
995 || t
->type
== CPP_DECLTYPE
);
998 /* Returns TRUE iff the next token begins a decltype type. */
1001 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1003 cp_token
*t
= cp_lexer_peek_token (lexer
);
1004 return token_is_decltype (t
);
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1011 saved_checks_value (struct tree_check
*check_value
)
1013 /* Perform any access checks that were deferred. */
1014 vec
<deferred_access_check
, va_gc
> *checks
;
1015 deferred_access_check
*chk
;
1016 checks
= check_value
->checks
;
1020 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1021 perform_or_defer_access_check (chk
->binfo
,
1023 chk
->diag_decl
, tf_warning_or_error
);
1025 /* Return the stored value. */
1026 return check_value
->value
;
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1036 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1040 /* N is 1-based, not zero-based. */
1043 if (cp_lexer_debugging_p (lexer
))
1044 fprintf (cp_lexer_debug_stream
,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1048 token
= lexer
->next_token
;
1049 gcc_assert (!n
|| token
!= &eof_token
);
1053 if (token
== lexer
->last_token
)
1059 if (!token
->purged_p
)
1063 if (cp_lexer_debugging_p (lexer
))
1065 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1066 putc ('\n', cp_lexer_debug_stream
);
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1076 cp_lexer_consume_token (cp_lexer
* lexer
)
1078 cp_token
*token
= lexer
->next_token
;
1080 gcc_assert (token
!= &eof_token
);
1081 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1085 lexer
->next_token
++;
1086 if (lexer
->next_token
== lexer
->last_token
)
1088 lexer
->next_token
= &eof_token
;
1093 while (lexer
->next_token
->purged_p
);
1095 cp_lexer_set_source_position_from_token (token
);
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer
))
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1101 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1102 putc ('\n', cp_lexer_debug_stream
);
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1113 cp_lexer_purge_token (cp_lexer
*lexer
)
1115 cp_token
*tok
= lexer
->next_token
;
1117 gcc_assert (tok
!= &eof_token
);
1118 tok
->purged_p
= true;
1119 tok
->location
= UNKNOWN_LOCATION
;
1120 tok
->u
.value
= NULL_TREE
;
1121 tok
->keyword
= RID_MAX
;
1126 if (tok
== lexer
->last_token
)
1132 while (tok
->purged_p
);
1133 lexer
->next_token
= tok
;
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1141 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1143 cp_token
*peek
= lexer
->next_token
;
1145 if (peek
== &eof_token
)
1146 peek
= lexer
->last_token
;
1148 gcc_assert (tok
< peek
);
1150 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1152 tok
->purged_p
= true;
1153 tok
->location
= UNKNOWN_LOCATION
;
1154 tok
->u
.value
= NULL_TREE
;
1155 tok
->keyword
= RID_MAX
;
1159 /* Begin saving tokens. All tokens consumed after this point will be
1163 cp_lexer_save_tokens (cp_lexer
* lexer
)
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer
))
1167 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1169 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1172 /* Commit to the portion of the token stream most recently saved. */
1175 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer
))
1179 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1181 lexer
->saved_tokens
.pop ();
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1188 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer
))
1192 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1194 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1202 struct saved_token_sentinel
1207 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1209 len
= lexer
->saved_tokens
.length ();
1210 cp_lexer_save_tokens (lexer
);
1214 cp_lexer_rollback_tokens (lexer
);
1217 ~saved_token_sentinel()
1220 cp_lexer_commit_tokens (lexer
);
1221 gcc_assert (lexer
->saved_tokens
.length () == len
);
1225 /* Print a representation of the TOKEN on the STREAM. */
1228 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names
[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1239 /* C++ parser token types - see "Manifest constants", above. */
1242 "NESTED_NAME_SPECIFIER",
1245 /* For some tokens, print the associated data. */
1246 switch (token
->type
)
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token
->u
.value
))
1255 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1262 case CPP_UTF8STRING
:
1263 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1267 print_generic_expr (stream
, token
->u
.value
);
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token
->type
< ARRAY_SIZE(token_names
))
1274 fputs (token_names
[token
->type
], stream
);
1276 fprintf (stream
, "[%d]", token
->type
);
1282 debug (cp_token
&ref
)
1284 cp_lexer_print_token (stderr
, &ref
);
1285 fprintf (stderr
, "\n");
1289 debug (cp_token
*ptr
)
1294 fprintf (stderr
, "<nil>\n");
1298 /* Start emitting debugging information. */
1301 cp_lexer_start_debugging (cp_lexer
* lexer
)
1303 if (!LEXER_DEBUGGING_ENABLED_P
)
1304 fatal_error (input_location
,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1307 lexer
->debugging_p
= true;
1308 cp_lexer_debug_stream
= stderr
;
1311 /* Stop emitting debugging information. */
1314 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1316 if (!LEXER_DEBUGGING_ENABLED_P
)
1317 fatal_error (input_location
,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1320 lexer
->debugging_p
= false;
1321 cp_lexer_debug_stream
= NULL
;
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1326 static cp_token_cache
*
1327 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1329 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1330 cache
->first
= first
;
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1339 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1341 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser
->omp_declare_simd
= NULL
;
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1353 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1355 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1357 if (fndecl
== error_mark_node
)
1359 parser
->omp_declare_simd
= NULL
;
1362 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1364 cp_ensure_no_omp_declare_simd (parser
);
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1374 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1376 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1378 error_at (parser
->oacc_routine
->loc
,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser
->oacc_routine
= NULL
;
1385 /* Decl-specifiers. */
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1390 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1392 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1402 static cp_declarator
*make_call_declarator
1403 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1404 static cp_declarator
*make_array_declarator
1405 (cp_declarator
*, tree
);
1406 static cp_declarator
*make_pointer_declarator
1407 (cp_cv_quals
, cp_declarator
*, tree
);
1408 static cp_declarator
*make_reference_declarator
1409 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1410 static cp_declarator
*make_ptrmem_declarator
1411 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1413 /* An erroneous declarator. */
1414 static cp_declarator
*cp_error_declarator
;
1416 /* The obstack on which declarators and related data structures are
1418 static struct obstack declarator_obstack
;
1420 /* Alloc BYTES from the declarator memory pool. */
1422 static inline void *
1423 alloc_declarator (size_t bytes
)
1425 return obstack_alloc (&declarator_obstack
, bytes
);
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1431 static cp_declarator
*
1432 make_declarator (cp_declarator_kind kind
)
1434 cp_declarator
*declarator
;
1436 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1437 declarator
->kind
= kind
;
1438 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1439 declarator
->attributes
= NULL_TREE
;
1440 declarator
->std_attributes
= NULL_TREE
;
1441 declarator
->declarator
= NULL
;
1442 declarator
->parameter_pack_p
= false;
1443 declarator
->id_loc
= UNKNOWN_LOCATION
;
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1454 static cp_declarator
*
1455 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1456 special_function_kind sfk
, location_t id_location
)
1458 cp_declarator
*declarator
;
1460 /* It is valid to write:
1462 class C { void f(); };
1466 The standard is not clear about whether `typedef const C D' is
1467 legal; as of 2002-09-15 the committee is considering that
1468 question. EDG 3.0 allows that syntax. Therefore, we do as
1470 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1471 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1473 gcc_assert (identifier_p (unqualified_name
)
1474 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1475 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1477 declarator
= make_declarator (cdk_id
);
1478 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1479 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1480 declarator
->u
.id
.sfk
= sfk
;
1481 declarator
->id_loc
= id_location
;
1486 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1487 of modifiers such as const or volatile to apply to the pointer
1488 type, represented as identifiers. ATTRIBUTES represent the attributes that
1489 appertain to the pointer or reference. */
1492 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1495 cp_declarator
*declarator
;
1497 declarator
= make_declarator (cdk_pointer
);
1498 declarator
->declarator
= target
;
1499 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1500 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1503 declarator
->id_loc
= target
->id_loc
;
1504 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1505 target
->parameter_pack_p
= false;
1508 declarator
->parameter_pack_p
= false;
1510 declarator
->std_attributes
= attributes
;
1515 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1516 represent the attributes that appertain to the pointer or
1520 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1521 bool rvalue_ref
, tree attributes
)
1523 cp_declarator
*declarator
;
1525 declarator
= make_declarator (cdk_reference
);
1526 declarator
->declarator
= target
;
1527 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1528 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1531 declarator
->id_loc
= target
->id_loc
;
1532 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1533 target
->parameter_pack_p
= false;
1536 declarator
->parameter_pack_p
= false;
1538 declarator
->std_attributes
= attributes
;
1543 /* Like make_pointer_declarator -- but for a pointer to a non-static
1544 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1545 appertain to the pointer or reference. */
1548 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1549 cp_declarator
*pointee
,
1552 cp_declarator
*declarator
;
1554 declarator
= make_declarator (cdk_ptrmem
);
1555 declarator
->declarator
= pointee
;
1556 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1557 declarator
->u
.pointer
.class_type
= class_type
;
1561 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1562 pointee
->parameter_pack_p
= false;
1565 declarator
->parameter_pack_p
= false;
1567 declarator
->std_attributes
= attributes
;
1572 /* Make a declarator for the function given by TARGET, with the
1573 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1574 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1575 indicates what exceptions can be thrown. */
1578 make_call_declarator (cp_declarator
*target
,
1580 cp_cv_quals cv_qualifiers
,
1581 cp_virt_specifiers virt_specifiers
,
1582 cp_ref_qualifier ref_qualifier
,
1584 tree exception_specification
,
1585 tree late_return_type
,
1586 tree requires_clause
)
1588 cp_declarator
*declarator
;
1590 declarator
= make_declarator (cdk_function
);
1591 declarator
->declarator
= target
;
1592 declarator
->u
.function
.parameters
= parms
;
1593 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1594 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1595 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1596 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1597 declarator
->u
.function
.exception_specification
= exception_specification
;
1598 declarator
->u
.function
.late_return_type
= late_return_type
;
1599 declarator
->u
.function
.requires_clause
= requires_clause
;
1602 declarator
->id_loc
= target
->id_loc
;
1603 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1604 target
->parameter_pack_p
= false;
1607 declarator
->parameter_pack_p
= false;
1612 /* Make a declarator for an array of BOUNDS elements, each of which is
1613 defined by ELEMENT. */
1616 make_array_declarator (cp_declarator
*element
, tree bounds
)
1618 cp_declarator
*declarator
;
1620 declarator
= make_declarator (cdk_array
);
1621 declarator
->declarator
= element
;
1622 declarator
->u
.array
.bounds
= bounds
;
1625 declarator
->id_loc
= element
->id_loc
;
1626 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1627 element
->parameter_pack_p
= false;
1630 declarator
->parameter_pack_p
= false;
1635 /* Determine whether the declarator we've seen so far can be a
1636 parameter pack, when followed by an ellipsis. */
1638 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1640 if (declarator
&& declarator
->parameter_pack_p
)
1641 /* We already saw an ellipsis. */
1644 /* Search for a declarator name, or any other declarator that goes
1645 after the point where the ellipsis could appear in a parameter
1646 pack. If we find any of these, then this declarator can not be
1647 made into a parameter pack. */
1649 while (declarator
&& !found
)
1651 switch ((int)declarator
->kind
)
1663 declarator
= declarator
->declarator
;
1671 cp_parameter_declarator
*no_parameters
;
1673 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1674 DECLARATOR and DEFAULT_ARGUMENT. */
1676 cp_parameter_declarator
*
1677 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1678 cp_declarator
*declarator
,
1679 tree default_argument
,
1681 bool template_parameter_pack_p
= false)
1683 cp_parameter_declarator
*parameter
;
1685 parameter
= ((cp_parameter_declarator
*)
1686 alloc_declarator (sizeof (cp_parameter_declarator
)));
1687 parameter
->next
= NULL
;
1688 if (decl_specifiers
)
1689 parameter
->decl_specifiers
= *decl_specifiers
;
1691 clear_decl_specs (¶meter
->decl_specifiers
);
1692 parameter
->declarator
= declarator
;
1693 parameter
->default_argument
= default_argument
;
1694 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1695 parameter
->loc
= loc
;
1700 /* Returns true iff DECLARATOR is a declaration for a function. */
1703 function_declarator_p (const cp_declarator
*declarator
)
1707 if (declarator
->kind
== cdk_function
1708 && declarator
->declarator
->kind
== cdk_id
)
1710 if (declarator
->kind
== cdk_id
1711 || declarator
->kind
== cdk_decomp
1712 || declarator
->kind
== cdk_error
)
1714 declarator
= declarator
->declarator
;
1724 A cp_parser parses the token stream as specified by the C++
1725 grammar. Its job is purely parsing, not semantic analysis. For
1726 example, the parser breaks the token stream into declarators,
1727 expressions, statements, and other similar syntactic constructs.
1728 It does not check that the types of the expressions on either side
1729 of an assignment-statement are compatible, or that a function is
1730 not declared with a parameter of type `void'.
1732 The parser invokes routines elsewhere in the compiler to perform
1733 semantic analysis and to build up the abstract syntax tree for the
1736 The parser (and the template instantiation code, which is, in a
1737 way, a close relative of parsing) are the only parts of the
1738 compiler that should be calling push_scope and pop_scope, or
1739 related functions. The parser (and template instantiation code)
1740 keeps track of what scope is presently active; everything else
1741 should simply honor that. (The code that generates static
1742 initializers may also need to set the scope, in order to check
1743 access control correctly when emitting the initializers.)
1748 The parser is of the standard recursive-descent variety. Upcoming
1749 tokens in the token stream are examined in order to determine which
1750 production to use when parsing a non-terminal. Some C++ constructs
1751 require arbitrary look ahead to disambiguate. For example, it is
1752 impossible, in the general case, to tell whether a statement is an
1753 expression or declaration without scanning the entire statement.
1754 Therefore, the parser is capable of "parsing tentatively." When the
1755 parser is not sure what construct comes next, it enters this mode.
1756 Then, while we attempt to parse the construct, the parser queues up
1757 error messages, rather than issuing them immediately, and saves the
1758 tokens it consumes. If the construct is parsed successfully, the
1759 parser "commits", i.e., it issues any queued error messages and
1760 the tokens that were being preserved are permanently discarded.
1761 If, however, the construct is not parsed successfully, the parser
1762 rolls back its state completely so that it can resume parsing using
1763 a different alternative.
1768 The performance of the parser could probably be improved substantially.
1769 We could often eliminate the need to parse tentatively by looking ahead
1770 a little bit. In some places, this approach might not entirely eliminate
1771 the need to parse tentatively, but it might still speed up the average
1774 /* Flags that are passed to some parsing functions. These values can
1775 be bitwise-ored together. */
1780 CP_PARSER_FLAGS_NONE
= 0x0,
1781 /* The construct is optional. If it is not present, then no error
1782 should be issued. */
1783 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1784 /* When parsing a type-specifier, treat user-defined type-names
1785 as non-type identifiers. */
1786 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1787 /* When parsing a type-specifier, do not try to parse a class-specifier
1788 or enum-specifier. */
1789 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1790 /* When parsing a decl-specifier-seq, only allow type-specifier or
1792 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1793 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1794 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags
;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT
,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED
,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1820 PREC_LOGICAL_OR_EXPRESSION
,
1821 PREC_LOGICAL_AND_EXPRESSION
,
1822 PREC_INCLUSIVE_OR_EXPRESSION
,
1823 PREC_EXCLUSIVE_OR_EXPRESSION
,
1824 PREC_AND_EXPRESSION
,
1825 PREC_EQUALITY_EXPRESSION
,
1826 PREC_RELATIONAL_EXPRESSION
,
1827 PREC_SHIFT_EXPRESSION
,
1828 PREC_ADDITIVE_EXPRESSION
,
1829 PREC_MULTIPLICATIVE_EXPRESSION
,
1831 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type
;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type
;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec
;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type
;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type
;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec
;
1859 /* Location of the binary operation we are parsing. */
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1871 /* Constructors and destructors. */
1873 static cp_parser_context
*cp_parser_context_new
1874 (cp_parser_context
*);
1876 /* Class variables. */
1878 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1884 static const cp_parser_binary_operations_map_node binops
[] = {
1885 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1886 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1888 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1889 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1890 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1892 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1893 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1895 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1896 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1898 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1899 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1900 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1901 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1903 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1904 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1906 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1908 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1910 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1912 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1914 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context
*
1928 cp_parser_context_new (cp_parser_context
* next
)
1930 cp_parser_context
*context
;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list
!= NULL
)
1935 /* Pull the first entry from the free list. */
1936 context
= cp_parser_context_free_list
;
1937 cp_parser_context_free_list
= context
->next
;
1938 memset (context
, 0, sizeof (*context
));
1941 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1943 /* No errors have occurred yet in this context. */
1944 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context
->object_type
= next
->object_type
;
1952 /* Thread the stack. */
1953 context
->next
= next
;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1971 push_unparsed_function_queues (cp_parser
*parser
)
1973 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1974 vec_safe_push (parser
->unparsed_queues
, e
);
1978 pop_unparsed_function_queues (cp_parser
*parser
)
1980 release_tree_vector (unparsed_funs_with_definitions
);
1981 parser
->unparsed_queues
->pop ();
1986 /* Constructors and destructors. */
1988 static cp_parser
*cp_parser_new
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser
*, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2011 static tree cp_parser_userdef_string_literal
2013 static cp_expr cp_parser_userdef_numeric_literal
2016 /* Basic concepts [gram.basic] */
2018 static void cp_parser_translation_unit (cp_parser
*);
2020 /* Expressions [gram.expr] */
2022 static cp_expr cp_parser_primary_expression
2023 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2024 static cp_expr cp_parser_id_expression
2025 (cp_parser
*, bool, bool, bool *, bool, bool);
2026 static cp_expr cp_parser_unqualified_id
2027 (cp_parser
*, bool, bool, bool, bool);
2028 static tree cp_parser_nested_name_specifier_opt
2029 (cp_parser
*, bool, bool, bool, bool, bool = false);
2030 static tree cp_parser_nested_name_specifier
2031 (cp_parser
*, bool, bool, bool, bool);
2032 static tree cp_parser_qualifying_entity
2033 (cp_parser
*, bool, bool, bool, bool, bool);
2034 static cp_expr cp_parser_postfix_expression
2035 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2036 static tree cp_parser_postfix_open_square_expression
2037 (cp_parser
*, tree
, bool, bool);
2038 static tree cp_parser_postfix_dot_deref_expression
2039 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2040 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2041 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser
*, tree
, tree
*, tree
*);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2051 static tree cp_parser_has_attribute_expression
2053 static tree cp_parser_new_expression
2055 static vec
<tree
, va_gc
> *cp_parser_new_placement
2057 static tree cp_parser_new_type_id
2058 (cp_parser
*, tree
*);
2059 static cp_declarator
*cp_parser_new_declarator_opt
2061 static cp_declarator
*cp_parser_direct_new_declarator
2063 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2065 static tree cp_parser_delete_expression
2067 static cp_expr cp_parser_cast_expression
2068 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2069 static cp_expr cp_parser_binary_expression
2070 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2071 static tree cp_parser_question_colon_clause
2072 (cp_parser
*, cp_expr
);
2073 static cp_expr cp_parser_assignment_expression
2074 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2075 static enum tree_code cp_parser_assignment_operator_opt
2077 static cp_expr cp_parser_expression
2078 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2079 static cp_expr cp_parser_constant_expression
2080 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2081 static cp_expr cp_parser_builtin_offsetof
2083 static cp_expr cp_parser_lambda_expression
2085 static void cp_parser_lambda_introducer
2086 (cp_parser
*, tree
);
2087 static bool cp_parser_lambda_declarator_opt
2088 (cp_parser
*, tree
);
2089 static void cp_parser_lambda_body
2090 (cp_parser
*, tree
);
2092 /* Statements [gram.stmt.stmt] */
2094 static void cp_parser_statement
2095 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2096 static void cp_parser_label_for_labeled_statement
2097 (cp_parser
*, tree
);
2098 static tree cp_parser_expression_statement
2099 (cp_parser
*, tree
);
2100 static tree cp_parser_compound_statement
2101 (cp_parser
*, tree
, int, bool);
2102 static void cp_parser_statement_seq_opt
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_selection_statement
2105 (cp_parser
*, bool *, vec
<tree
> *);
2106 static tree cp_parser_condition
2108 static tree cp_parser_iteration_statement
2109 (cp_parser
*, bool *, bool, unsigned short);
2110 static bool cp_parser_init_statement
2111 (cp_parser
*, tree
*decl
);
2112 static tree cp_parser_for
2113 (cp_parser
*, bool, unsigned short);
2114 static tree cp_parser_c_for
2115 (cp_parser
*, tree
, tree
, bool, unsigned short);
2116 static tree cp_parser_range_for
2117 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2118 static void do_range_for_auto_deduction
2120 static tree cp_parser_perform_range_for_lookup
2121 (tree
, tree
*, tree
*);
2122 static tree cp_parser_range_for_member_function
2124 static tree cp_parser_jump_statement
2126 static void cp_parser_declaration_statement
2129 static tree cp_parser_implicitly_scoped_statement
2130 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2131 static void cp_parser_already_scoped_statement
2132 (cp_parser
*, bool *, const token_indent_info
&);
2134 /* Declarations [gram.dcl.dcl] */
2136 static void cp_parser_declaration_seq_opt
2138 static void cp_parser_declaration
2140 static void cp_parser_toplevel_declaration
2142 static void cp_parser_block_declaration
2143 (cp_parser
*, bool);
2144 static void cp_parser_simple_declaration
2145 (cp_parser
*, bool, tree
*);
2146 static void cp_parser_decl_specifier_seq
2147 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2148 static tree cp_parser_storage_class_specifier_opt
2150 static tree cp_parser_function_specifier_opt
2151 (cp_parser
*, cp_decl_specifier_seq
*);
2152 static tree cp_parser_type_specifier
2153 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2155 static tree cp_parser_simple_type_specifier
2156 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2157 static tree cp_parser_type_name
2158 (cp_parser
*, bool);
2159 static tree cp_parser_type_name
2161 static tree cp_parser_nonclass_name
2162 (cp_parser
* parser
);
2163 static tree cp_parser_elaborated_type_specifier
2164 (cp_parser
*, bool, bool);
2165 static tree cp_parser_enum_specifier
2167 static void cp_parser_enumerator_list
2168 (cp_parser
*, tree
);
2169 static void cp_parser_enumerator_definition
2170 (cp_parser
*, tree
);
2171 static tree cp_parser_namespace_name
2173 static void cp_parser_namespace_definition
2175 static void cp_parser_namespace_body
2177 static tree cp_parser_qualified_namespace_specifier
2179 static void cp_parser_namespace_alias_definition
2181 static bool cp_parser_using_declaration
2182 (cp_parser
*, bool);
2183 static void cp_parser_using_directive
2185 static tree cp_parser_alias_declaration
2187 static void cp_parser_asm_definition
2189 static void cp_parser_linkage_specification
2191 static void cp_parser_static_assert
2192 (cp_parser
*, bool);
2193 static tree cp_parser_decltype
2195 static tree cp_parser_decomposition_declaration
2196 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2198 /* Declarators [gram.dcl.decl] */
2200 static tree cp_parser_init_declarator
2201 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2202 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2203 static cp_declarator
*cp_parser_declarator
2204 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2205 static cp_declarator
*cp_parser_direct_declarator
2206 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2207 static enum tree_code cp_parser_ptr_operator
2208 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2209 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2211 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2213 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2215 static tree cp_parser_tx_qualifier_opt
2217 static tree cp_parser_late_return_type_opt
2218 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2219 static tree cp_parser_declarator_id
2220 (cp_parser
*, bool);
2221 static tree cp_parser_type_id
2222 (cp_parser
*, location_t
* = NULL
);
2223 static tree cp_parser_template_type_arg
2225 static tree
cp_parser_trailing_type_id (cp_parser
*);
2226 static tree cp_parser_type_id_1
2227 (cp_parser
*, bool, bool, location_t
*);
2228 static void cp_parser_type_specifier_seq
2229 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2230 static tree cp_parser_parameter_declaration_clause
2232 static tree cp_parser_parameter_declaration_list
2234 static cp_parameter_declarator
*cp_parser_parameter_declaration
2235 (cp_parser
*, bool, bool *);
2236 static tree cp_parser_default_argument
2237 (cp_parser
*, bool);
2238 static void cp_parser_function_body
2239 (cp_parser
*, bool);
2240 static tree cp_parser_initializer
2241 (cp_parser
*, bool *, bool *, bool = false);
2242 static cp_expr cp_parser_initializer_clause
2243 (cp_parser
*, bool *);
2244 static cp_expr cp_parser_braced_list
2245 (cp_parser
*, bool*);
2246 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2247 (cp_parser
*, bool *);
2249 static void cp_parser_ctor_initializer_opt_and_function_body
2250 (cp_parser
*, bool);
2252 static tree cp_parser_late_parsing_omp_declare_simd
2253 (cp_parser
*, tree
);
2255 static tree cp_parser_late_parsing_oacc_routine
2256 (cp_parser
*, tree
);
2258 static tree synthesize_implicit_template_parm
2259 (cp_parser
*, tree
);
2260 static tree finish_fully_implicit_template
2261 (cp_parser
*, tree
);
2262 static void abort_fully_implicit_template
2265 /* Classes [gram.class] */
2267 static tree cp_parser_class_name
2268 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2269 static tree cp_parser_class_specifier
2271 static tree cp_parser_class_head
2272 (cp_parser
*, bool *);
2273 static enum tag_types cp_parser_class_key
2275 static void cp_parser_type_parameter_key
2276 (cp_parser
* parser
);
2277 static void cp_parser_member_specification_opt
2279 static void cp_parser_member_declaration
2281 static tree cp_parser_pure_specifier
2283 static tree cp_parser_constant_initializer
2286 /* Derived classes [gram.class.derived] */
2288 static tree cp_parser_base_clause
2290 static tree cp_parser_base_specifier
2293 /* Special member functions [gram.special] */
2295 static tree cp_parser_conversion_function_id
2297 static tree cp_parser_conversion_type_id
2299 static cp_declarator
*cp_parser_conversion_declarator_opt
2301 static void cp_parser_ctor_initializer_opt
2303 static void cp_parser_mem_initializer_list
2305 static tree cp_parser_mem_initializer
2307 static tree cp_parser_mem_initializer_id
2310 /* Overloading [gram.over] */
2312 static cp_expr cp_parser_operator_function_id
2314 static cp_expr cp_parser_operator
2315 (cp_parser
*, location_t
);
2317 /* Templates [gram.temp] */
2319 static void cp_parser_template_declaration
2320 (cp_parser
*, bool);
2321 static tree cp_parser_template_parameter_list
2323 static tree cp_parser_template_parameter
2324 (cp_parser
*, bool *, bool *);
2325 static tree cp_parser_type_parameter
2326 (cp_parser
*, bool *);
2327 static tree cp_parser_template_id
2328 (cp_parser
*, bool, bool, enum tag_types
, bool);
2329 static tree cp_parser_template_name
2330 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2331 static tree cp_parser_template_argument_list
2333 static tree cp_parser_template_argument
2335 static void cp_parser_explicit_instantiation
2337 static void cp_parser_explicit_specialization
2340 /* Exception handling [gram.exception] */
2342 static tree cp_parser_try_block
2344 static void cp_parser_function_try_block
2346 static void cp_parser_handler_seq
2348 static void cp_parser_handler
2350 static tree cp_parser_exception_declaration
2352 static tree cp_parser_throw_expression
2354 static tree cp_parser_exception_specification_opt
2356 static tree cp_parser_type_id_list
2359 /* GNU Extensions */
2361 static tree cp_parser_asm_specification_opt
2363 static tree cp_parser_asm_operand_list
2365 static tree cp_parser_asm_clobber_list
2367 static tree cp_parser_asm_label_list
2369 static bool cp_next_tokens_can_be_attribute_p
2371 static bool cp_next_tokens_can_be_gnu_attribute_p
2373 static bool cp_next_tokens_can_be_std_attribute_p
2375 static bool cp_nth_tokens_can_be_std_attribute_p
2376 (cp_parser
*, size_t);
2377 static bool cp_nth_tokens_can_be_gnu_attribute_p
2378 (cp_parser
*, size_t);
2379 static bool cp_nth_tokens_can_be_attribute_p
2380 (cp_parser
*, size_t);
2381 static tree cp_parser_attributes_opt
2383 static tree cp_parser_gnu_attributes_opt
2385 static tree cp_parser_gnu_attribute_list
2386 (cp_parser
*, bool = false);
2387 static tree cp_parser_std_attribute
2388 (cp_parser
*, tree
);
2389 static tree cp_parser_std_attribute_spec
2391 static tree cp_parser_std_attribute_spec_seq
2393 static size_t cp_parser_skip_attributes_opt
2394 (cp_parser
*, size_t);
2395 static bool cp_parser_extension_opt
2396 (cp_parser
*, int *);
2397 static void cp_parser_label_declaration
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2404 static tree cp_parser_requires_clause_opt
2406 static tree cp_parser_requires_expression
2408 static tree cp_parser_requirement_parameter_list
2410 static tree cp_parser_requirement_body
2412 static tree cp_parser_requirement_list
2414 static tree cp_parser_requirement
2416 static tree cp_parser_simple_requirement
2418 static tree cp_parser_compound_requirement
2420 static tree cp_parser_type_requirement
2422 static tree cp_parser_nested_requirement
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser
*, cp_token
*);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser
*, enum rid
);
2431 static void cp_parser_function_transaction
2432 (cp_parser
*, enum rid
);
2433 static tree cp_parser_transaction_cancel
2436 enum pragma_context
{
2443 static bool cp_parser_pragma
2444 (cp_parser
*, enum pragma_context
, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2450 static tree cp_parser_objc_message_args
2452 static tree cp_parser_objc_message_expression
2454 static cp_expr cp_parser_objc_encode_expression
2456 static tree cp_parser_objc_defs_expression
2458 static tree cp_parser_objc_protocol_expression
2460 static tree cp_parser_objc_selector_expression
2462 static cp_expr cp_parser_objc_expression
2464 static bool cp_parser_objc_selector_p
2466 static tree cp_parser_objc_selector
2468 static tree cp_parser_objc_protocol_refs_opt
2470 static void cp_parser_objc_declaration
2471 (cp_parser
*, tree
);
2472 static tree cp_parser_objc_statement
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser
*, tree
*);
2476 static void cp_parser_objc_at_property_declaration
2478 static void cp_parser_objc_at_synthesize_declaration
2480 static void cp_parser_objc_at_dynamic_declaration
2482 static tree cp_parser_objc_struct_declaration
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser
*, tree
, location_t
);
2491 static tree cp_parser_maybe_treat_template_as_class
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser
*, cp_declarator
*, location_t
);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2497 static cp_expr cp_parser_simple_cast_expression
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser
*, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser
*, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser
*, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser
*, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec
<deferred_access_check
, va_gc
> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser
*, tree
);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2517 static tree cp_parser_save_nsdmi
2519 static tree cp_parser_enclosed_template_argument_list
2521 static void cp_parser_save_default_args
2522 (cp_parser
*, tree
);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser
*, tree
);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser
*, tree
, tree
, tree
);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser
*, tree
);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser
*, tree
);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser
*, enum rid
);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser
*, enum rid
);
2535 static bool cp_parser_declares_only_class_p
2537 static void cp_parser_set_storage_class
2538 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq
*decl_specs
,
2543 cp_decl_spec ds
, cp_token
*);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq
*);
2546 static void cp_parser_required_error
2547 (cp_parser
*, required_token
, bool, location_t
);
2548 static cp_token
*cp_parser_require
2549 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2550 static cp_token
*cp_parser_require_keyword
2551 (cp_parser
*, enum rid
, required_token
);
2552 static bool cp_parser_token_starts_function_definition_p
2554 static bool cp_parser_next_token_starts_class_definition_p
2556 static bool cp_parser_next_token_ends_template_argument_p
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser
*, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2564 static void cp_parser_check_class_key
2565 (enum tag_types
, tree type
);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type
, location_t location
);
2568 static bool cp_parser_optional_template_keyword
2570 static void cp_parser_pre_parsed_nested_name_specifier
2572 static bool cp_parser_cache_group
2573 (cp_parser
*, enum cpp_ttype
, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser
*parser
, bool nsdmi
);
2576 static void cp_parser_parse_tentatively
2578 static void cp_parser_commit_to_tentative_parse
2580 static void cp_parser_commit_to_topmost_tentative_parse
2582 static void cp_parser_abort_tentative_parse
2584 static bool cp_parser_parse_definitely
2586 static inline bool cp_parser_parsing_tentatively
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2590 static void cp_parser_error
2591 (cp_parser
*, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2594 static bool cp_parser_simulate_error
2596 static bool cp_parser_check_type_definition
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator
*, tree
, location_t type_location
);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser
*, non_integral_constant
);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser
*, tree
, location_t
);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser
*, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2614 static void cp_parser_skip_to_end_of_block_or_statement
2616 static bool cp_parser_skip_to_closing_brace
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser
*, cp_token
*);
2622 static bool cp_parser_error_occurred
2624 static bool cp_parser_allow_gnu_extensions_p
2626 static bool cp_parser_is_pure_string_literal
2628 static bool cp_parser_is_string_literal
2630 static bool cp_parser_is_keyword
2631 (cp_token
*, enum rid
);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser
*, tree
, location_t location
);
2634 static cp_declarator
* cp_parser_make_indirect_declarator
2635 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2636 static bool cp_parser_compound_literal_p
2638 static bool cp_parser_array_designator_p
2640 static bool cp_parser_init_statement_p
2642 static bool cp_parser_skip_to_closing_square_bracket
2645 /* Concept-related syntactic transformations */
2647 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2648 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand
;
2657 ++c_inhibit_evaluation_warnings
;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings
;
2663 --cp_unevaluated_operand
;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2672 cp_parser_parsing_tentatively (cp_parser
* parser
)
2674 return parser
->context
->next
!= NULL
;
2677 /* Returns nonzero if TOKEN is a string literal. */
2680 cp_parser_is_pure_string_literal (cp_token
* token
)
2682 return (token
->type
== CPP_STRING
||
2683 token
->type
== CPP_STRING16
||
2684 token
->type
== CPP_STRING32
||
2685 token
->type
== CPP_WSTRING
||
2686 token
->type
== CPP_UTF8STRING
);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2693 cp_parser_is_string_literal (cp_token
* token
)
2695 return (cp_parser_is_pure_string_literal (token
) ||
2696 token
->type
== CPP_STRING_USERDEF
||
2697 token
->type
== CPP_STRING16_USERDEF
||
2698 token
->type
== CPP_STRING32_USERDEF
||
2699 token
->type
== CPP_WSTRING_USERDEF
||
2700 token
->type
== CPP_UTF8STRING_USERDEF
);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2708 return token
->keyword
== keyword
;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token
*token
)
2717 if (token
->type
!= CPP_PRAGMA
)
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2735 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2736 location_t
*out_loc
)
2738 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2739 if (token2
->type
!= tok1_kind
)
2741 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2742 if (token3
->type
!= tok1_kind
)
2744 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2745 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2748 /* It must be at the start of the line. */
2749 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2750 if (LOCATION_COLUMN (start_loc
) != 1)
2753 /* We have a conflict marker. Construct a location of the form:
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc
= get_finish (token4
->location
);
2758 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2767 get_matching_symbol (required_token token_desc
)
2774 case RT_CLOSE_BRACE
:
2776 case RT_CLOSE_PAREN
:
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc
)
2790 return CPP_SEMICOLON
;
2792 return CPP_OPEN_PAREN
;
2793 case RT_CLOSE_BRACE
:
2794 return CPP_CLOSE_BRACE
;
2796 return CPP_OPEN_BRACE
;
2797 case RT_CLOSE_SQUARE
:
2798 return CPP_CLOSE_SQUARE
;
2799 case RT_OPEN_SQUARE
:
2800 return CPP_OPEN_SQUARE
;
2805 case RT_CLOSE_PAREN
:
2806 return CPP_CLOSE_PAREN
;
2809 /* Use CPP_EOF as a "no completions possible" code. */
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2834 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2835 required_token missing_token_desc
,
2836 location_t matching_location
)
2838 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token
);
2843 if (token
->type
== CPP_PRAGMA
)
2845 error_at (token
->location
,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser
, token
);
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token
->type
== CPP_LSHIFT
2853 || token
->type
== CPP_RSHIFT
2854 || token
->type
== CPP_EQ_EQ
)
2857 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2859 error_at (loc
, "version control conflict marker in file");
2860 expanded_location token_exploc
= expand_location (token
->location
);
2861 /* Consume tokens until the end of the source line. */
2864 cp_lexer_consume_token (parser
->lexer
);
2865 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2868 expanded_location next_exploc
= expand_location (next
->location
);
2869 if (next_exploc
.file
!= token_exploc
.file
)
2871 if (next_exploc
.line
!= token_exploc
.line
)
2878 gcc_rich_location
richloc (input_location
);
2880 bool added_matching_location
= false;
2882 if (missing_token_desc
!= RT_NONE
)
2884 /* Potentially supply a fix-it hint, suggesting to add the
2885 missing token immediately after the *previous* token.
2886 This may move the primary location within richloc. */
2887 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2888 location_t prev_token_loc
2889 = cp_lexer_previous_token (parser
->lexer
)->location
;
2890 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2892 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2893 Attempt to consolidate diagnostics by printing it as a
2894 secondary range within the main diagnostic. */
2895 if (matching_location
!= UNKNOWN_LOCATION
)
2896 added_matching_location
2897 = richloc
.add_location_if_nearby (matching_location
);
2900 /* Actually emit the error. */
2901 c_parse_error (gmsgid
,
2902 /* Because c_parser_error does not understand
2903 CPP_KEYWORD, keywords are treated like
2905 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2906 token
->u
.value
, token
->flags
, &richloc
);
2908 if (missing_token_desc
!= RT_NONE
)
2910 /* If we weren't able to consolidate matching_location, then
2911 print it as a secondary diagnostic. */
2912 if (matching_location
!= UNKNOWN_LOCATION
2913 && !added_matching_location
)
2914 inform (matching_location
, "to match this %qs",
2915 get_matching_symbol (missing_token_desc
));
2919 /* If not parsing tentatively, issue a diagnostic of the form
2920 FILE:LINE: MESSAGE before TOKEN
2921 where TOKEN is the next token in the input stream. MESSAGE
2922 (specified by the caller) is usually of the form "expected
2926 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2928 if (!cp_parser_simulate_error (parser
))
2929 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2932 /* Issue an error about name-lookup failing. NAME is the
2933 IDENTIFIER_NODE DECL is the result of
2934 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2935 the thing that we hoped to find. */
2938 cp_parser_name_lookup_error (cp_parser
* parser
,
2941 name_lookup_error desired
,
2942 location_t location
)
2944 /* If name lookup completely failed, tell the user that NAME was not
2946 if (decl
== error_mark_node
)
2948 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2949 error_at (location
, "%<%E::%E%> has not been declared",
2950 parser
->scope
, name
);
2951 else if (parser
->scope
== global_namespace
)
2952 error_at (location
, "%<::%E%> has not been declared", name
);
2953 else if (parser
->object_scope
2954 && !CLASS_TYPE_P (parser
->object_scope
))
2955 error_at (location
, "request for member %qE in non-class type %qT",
2956 name
, parser
->object_scope
);
2957 else if (parser
->object_scope
)
2958 error_at (location
, "%<%T::%E%> has not been declared",
2959 parser
->object_scope
, name
);
2961 error_at (location
, "%qE has not been declared", name
);
2963 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2968 error_at (location
, "%<%E::%E%> is not a type",
2969 parser
->scope
, name
);
2972 error_at (location
, "%<%E::%E%> is not a class or namespace",
2973 parser
->scope
, name
);
2977 "%<%E::%E%> is not a class, namespace, or enumeration",
2978 parser
->scope
, name
);
2985 else if (parser
->scope
== global_namespace
)
2990 error_at (location
, "%<::%E%> is not a type", name
);
2993 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2997 "%<::%E%> is not a class, namespace, or enumeration",
3009 error_at (location
, "%qE is not a type", name
);
3012 error_at (location
, "%qE is not a class or namespace", name
);
3016 "%qE is not a class, namespace, or enumeration", name
);
3024 /* If we are parsing tentatively, remember that an error has occurred
3025 during this tentative parse. Returns true if the error was
3026 simulated; false if a message should be issued by the caller. */
3029 cp_parser_simulate_error (cp_parser
* parser
)
3031 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3033 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3039 /* This function is called when a type is defined. If type
3040 definitions are forbidden at this point, an error message is
3044 cp_parser_check_type_definition (cp_parser
* parser
)
3046 /* If types are forbidden here, issue a message. */
3047 if (parser
->type_definition_forbidden_message
)
3049 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3050 in the message need to be interpreted. */
3051 error (parser
->type_definition_forbidden_message
);
3057 /* This function is called when the DECLARATOR is processed. The TYPE
3058 was a type defined in the decl-specifiers. If it is invalid to
3059 define a type in the decl-specifiers for DECLARATOR, an error is
3060 issued. TYPE_LOCATION is the location of TYPE and is used
3061 for error reporting. */
3064 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3065 tree type
, location_t type_location
)
3067 /* [dcl.fct] forbids type definitions in return types.
3068 Unfortunately, it's not easy to know whether or not we are
3069 processing a return type until after the fact. */
3071 && (declarator
->kind
== cdk_pointer
3072 || declarator
->kind
== cdk_reference
3073 || declarator
->kind
== cdk_ptrmem
))
3074 declarator
= declarator
->declarator
;
3076 && declarator
->kind
== cdk_function
)
3078 error_at (type_location
,
3079 "new types may not be defined in a return type");
3080 inform (type_location
,
3081 "(perhaps a semicolon is missing after the definition of %qT)",
3086 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3087 "<" in any valid C++ program. If the next token is indeed "<",
3088 issue a message warning the user about what appears to be an
3089 invalid attempt to form a template-id. LOCATION is the location
3090 of the type-specifier (TYPE) */
3093 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3095 enum tag_types tag_type
,
3096 location_t location
)
3098 cp_token_position start
= 0;
3100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3102 if (TREE_CODE (type
) == TYPE_DECL
)
3103 type
= TREE_TYPE (type
);
3104 if (TYPE_P (type
) && !template_placeholder_p (type
))
3105 error_at (location
, "%qT is not a template", type
);
3106 else if (identifier_p (type
))
3108 if (tag_type
!= none_type
)
3109 error_at (location
, "%qE is not a class template", type
);
3111 error_at (location
, "%qE is not a template", type
);
3114 error_at (location
, "invalid template-id");
3115 /* Remember the location of the invalid "<". */
3116 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3117 start
= cp_lexer_token_position (parser
->lexer
, true);
3118 /* Consume the "<". */
3119 cp_lexer_consume_token (parser
->lexer
);
3120 /* Parse the template arguments. */
3121 cp_parser_enclosed_template_argument_list (parser
);
3122 /* Permanently remove the invalid template arguments so that
3123 this error message is not issued again. */
3125 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3129 /* If parsing an integral constant-expression, issue an error message
3130 about the fact that THING appeared and return true. Otherwise,
3131 return false. In either case, set
3132 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3135 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3136 non_integral_constant thing
)
3138 parser
->non_integral_constant_expression_p
= true;
3139 if (parser
->integral_constant_expression_p
)
3141 if (!parser
->allow_non_integral_constant_expression_p
)
3143 const char *msg
= NULL
;
3147 pedwarn (input_location
, OPT_Wpedantic
,
3148 "ISO C++ forbids using a floating-point literal "
3149 "in a constant-expression");
3152 error ("a cast to a type other than an integral or "
3153 "enumeration type cannot appear in a "
3154 "constant-expression");
3157 error ("%<typeid%> operator "
3158 "cannot appear in a constant-expression");
3161 error ("non-constant compound literals "
3162 "cannot appear in a constant-expression");
3165 error ("a function call "
3166 "cannot appear in a constant-expression");
3169 error ("an increment "
3170 "cannot appear in a constant-expression");
3173 error ("an decrement "
3174 "cannot appear in a constant-expression");
3177 error ("an array reference "
3178 "cannot appear in a constant-expression");
3180 case NIC_ADDR_LABEL
:
3181 error ("the address of a label "
3182 "cannot appear in a constant-expression");
3184 case NIC_OVERLOADED
:
3185 error ("calls to overloaded operators "
3186 "cannot appear in a constant-expression");
3188 case NIC_ASSIGNMENT
:
3189 error ("an assignment cannot appear in a constant-expression");
3192 error ("a comma operator "
3193 "cannot appear in a constant-expression");
3195 case NIC_CONSTRUCTOR
:
3196 error ("a call to a constructor "
3197 "cannot appear in a constant-expression");
3199 case NIC_TRANSACTION
:
3200 error ("a transaction expression "
3201 "cannot appear in a constant-expression");
3207 msg
= "__FUNCTION__";
3209 case NIC_PRETTY_FUNC
:
3210 msg
= "__PRETTY_FUNCTION__";
3230 case NIC_PREINCREMENT
:
3233 case NIC_PREDECREMENT
:
3246 error ("%qs cannot appear in a constant-expression", msg
);
3253 /* Emit a diagnostic for an invalid type name. This function commits
3254 to the current active tentative parse, if any. (Otherwise, the
3255 problematic construct might be encountered again later, resulting
3256 in duplicate error messages.) LOCATION is the location of ID. */
3259 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3260 location_t location
)
3262 tree decl
, ambiguous_decls
;
3263 cp_parser_commit_to_tentative_parse (parser
);
3264 /* Try to lookup the identifier. */
3265 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3266 /*is_template=*/false,
3267 /*is_namespace=*/false,
3268 /*check_dependency=*/true,
3269 &ambiguous_decls
, location
);
3270 if (ambiguous_decls
)
3271 /* If the lookup was ambiguous, an error will already have
3274 /* If the lookup found a template-name, it means that the user forgot
3275 to specify an argument list. Emit a useful error message. */
3276 if (DECL_TYPE_TEMPLATE_P (decl
))
3278 auto_diagnostic_group d
;
3280 "invalid use of template-name %qE without an argument list",
3282 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3283 inform (location
, "class template argument deduction is only available "
3284 "with -std=c++17 or -std=gnu++17");
3285 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3287 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3288 error_at (location
, "invalid use of destructor %qD as a type", id
);
3289 else if (TREE_CODE (decl
) == TYPE_DECL
)
3290 /* Something like 'unsigned A a;' */
3291 error_at (location
, "invalid combination of multiple type-specifiers");
3292 else if (!parser
->scope
)
3294 /* Issue an error message. */
3295 auto_diagnostic_group d
;
3297 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3298 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3299 if (const char *suggestion
= hint
.suggestion ())
3301 gcc_rich_location
richloc (location
);
3302 richloc
.add_fixit_replace (suggestion
);
3304 "%qE does not name a type; did you mean %qs?",
3308 error_at (location
, "%qE does not name a type", id
);
3309 /* If we're in a template class, it's possible that the user was
3310 referring to a type from a base class. For example:
3312 template <typename T> struct A { typedef T X; };
3313 template <typename T> struct B : public A<T> { X x; };
3315 The user should have said "typename A<T>::X". */
3316 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3317 inform (location
, "C++11 %<constexpr%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3320 inform (location
, "C++11 %<noexcept%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect
< cxx11
3323 && TREE_CODE (id
) == IDENTIFIER_NODE
3324 && id_equal (id
, "thread_local"))
3325 inform (location
, "C++11 %<thread_local%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3328 inform (location
, "%<concept%> only available with -fconcepts");
3329 else if (processing_template_decl
&& current_class_type
3330 && TYPE_BINFO (current_class_type
))
3334 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3338 tree base_type
= BINFO_TYPE (b
);
3339 if (CLASS_TYPE_P (base_type
)
3340 && dependent_type_p (base_type
))
3343 /* Go from a particular instantiation of the
3344 template (which will have an empty TYPE_FIELDs),
3345 to the main version. */
3346 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3347 for (field
= TYPE_FIELDS (base_type
);
3349 field
= DECL_CHAIN (field
))
3350 if (TREE_CODE (field
) == TYPE_DECL
3351 && DECL_NAME (field
) == id
)
3354 "(perhaps %<typename %T::%E%> was intended)",
3355 BINFO_TYPE (b
), id
);
3364 /* Here we diagnose qualified-ids where the scope is actually correct,
3365 but the identifier does not resolve to a valid type name. */
3366 else if (parser
->scope
!= error_mark_node
)
3368 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3370 auto_diagnostic_group d
;
3372 if (decl
== error_mark_node
)
3373 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3375 const char *suggestion
= hint
.suggestion ();
3376 gcc_rich_location
richloc (location_of (id
));
3378 richloc
.add_fixit_replace (suggestion
);
3379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3383 "%qE in namespace %qE does not name a template"
3384 " type; did you mean %qs?",
3385 id
, parser
->scope
, suggestion
);
3388 "%qE in namespace %qE does not name a template type",
3391 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3395 "%qE in namespace %qE does not name a template"
3396 " type; did you mean %qs?",
3397 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3400 "%qE in namespace %qE does not name a template"
3402 TREE_OPERAND (id
, 0), parser
->scope
);
3408 "%qE in namespace %qE does not name a type"
3409 "; did you mean %qs?",
3410 id
, parser
->scope
, suggestion
);
3413 "%qE in namespace %qE does not name a type",
3417 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3419 else if (CLASS_TYPE_P (parser
->scope
)
3420 && constructor_name_p (id
, parser
->scope
))
3423 auto_diagnostic_group d
;
3424 error_at (location
, "%<%T::%E%> names the constructor, not"
3425 " the type", parser
->scope
, id
);
3426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3427 error_at (location
, "and %qT has no template constructors",
3430 else if (TYPE_P (parser
->scope
)
3431 && dependent_scope_p (parser
->scope
))
3433 gcc_rich_location
richloc (location
);
3434 richloc
.add_fixit_insert_before ("typename ");
3435 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3437 "need %<typename%> before %<%T::%D::%E%> because "
3438 "%<%T::%D%> is a dependent scope",
3439 TYPE_CONTEXT (parser
->scope
),
3440 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3442 TYPE_CONTEXT (parser
->scope
),
3443 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3445 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3446 "%qT is a dependent scope",
3447 parser
->scope
, id
, parser
->scope
);
3449 else if (TYPE_P (parser
->scope
))
3451 auto_diagnostic_group d
;
3452 if (!COMPLETE_TYPE_P (parser
->scope
))
3453 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3455 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3456 error_at (location_of (id
),
3457 "%qE in %q#T does not name a template type",
3459 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3460 error_at (location_of (id
),
3461 "%qE in %q#T does not name a template type",
3462 TREE_OPERAND (id
, 0), parser
->scope
);
3464 error_at (location_of (id
),
3465 "%qE in %q#T does not name a type",
3468 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3475 /* Check for a common situation where a type-name should be present,
3476 but is not, and issue a sensible error message. Returns true if an
3477 invalid type-name was detected.
3479 The situation handled by this function are variable declarations of the
3480 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3481 Usually, `ID' should name a type, but if we got here it means that it
3482 does not. We try to emit the best possible error message depending on
3483 how exactly the id-expression looks like. */
3486 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3489 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3491 /* Avoid duplicate error about ambiguous lookup. */
3492 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3494 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3495 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3499 cp_parser_parse_tentatively (parser
);
3500 id
= cp_parser_id_expression (parser
,
3501 /*template_keyword_p=*/false,
3502 /*check_dependency_p=*/true,
3503 /*template_p=*/NULL
,
3504 /*declarator_p=*/false,
3505 /*optional_p=*/false);
3506 /* If the next token is a (, this is a function with no explicit return
3507 type, i.e. constructor, destructor or conversion op. */
3508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3509 || TREE_CODE (id
) == TYPE_DECL
)
3511 cp_parser_abort_tentative_parse (parser
);
3514 if (!cp_parser_parse_definitely (parser
))
3517 /* Emit a diagnostic for the invalid type. */
3518 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3520 /* If we aren't in the middle of a declarator (i.e. in a
3521 parameter-declaration-clause), skip to the end of the declaration;
3522 there's no point in trying to process it. */
3523 if (!parser
->in_declarator_p
)
3524 cp_parser_skip_to_end_of_block_or_statement (parser
);
3528 /* Consume tokens up to, and including, the next non-nested closing `)'.
3529 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3530 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3531 found an unnested token of that type. */
3534 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3539 unsigned paren_depth
= 0;
3540 unsigned brace_depth
= 0;
3541 unsigned square_depth
= 0;
3542 unsigned condop_depth
= 0;
3544 if (recovering
&& or_ttype
== CPP_EOF
3545 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3550 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3552 /* Have we found what we're looking for before the closing paren? */
3553 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3554 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3557 switch (token
->type
)
3560 case CPP_PRAGMA_EOL
:
3561 /* If we've run out of tokens, then there is no closing `)'. */
3564 /* This is good for lambda expression capture-lists. */
3565 case CPP_OPEN_SQUARE
:
3568 case CPP_CLOSE_SQUARE
:
3569 if (!square_depth
--)
3574 /* This matches the processing in skip_to_end_of_statement. */
3579 case CPP_OPEN_BRACE
:
3582 case CPP_CLOSE_BRACE
:
3587 case CPP_OPEN_PAREN
:
3592 case CPP_CLOSE_PAREN
:
3593 if (!brace_depth
&& !paren_depth
--)
3596 cp_lexer_consume_token (parser
->lexer
);
3602 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3607 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3615 /* Consume the token. */
3616 cp_lexer_consume_token (parser
->lexer
);
3620 /* Consume tokens up to, and including, the next non-nested closing `)'.
3621 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3622 are doing error recovery. Returns -1 if OR_COMMA is true and we
3623 found an unnested token of that type. */
3626 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3631 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3632 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3633 ttype
, consume_paren
);
3636 /* Consume tokens until we reach the end of the current statement.
3637 Normally, that will be just before consuming a `;'. However, if a
3638 non-nested `}' comes first, then we stop before consuming that. */
3641 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3643 unsigned nesting_depth
= 0;
3645 /* Unwind generic function template scope if necessary. */
3646 if (parser
->fully_implicit_function_template_p
)
3647 abort_fully_implicit_template (parser
);
3651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3653 switch (token
->type
)
3656 case CPP_PRAGMA_EOL
:
3657 /* If we've run out of tokens, stop. */
3661 /* If the next token is a `;', we have reached the end of the
3667 case CPP_CLOSE_BRACE
:
3668 /* If this is a non-nested '}', stop before consuming it.
3669 That way, when confronted with something like:
3673 we stop before consuming the closing '}', even though we
3674 have not yet reached a `;'. */
3675 if (nesting_depth
== 0)
3678 /* If it is the closing '}' for a block that we have
3679 scanned, stop -- but only after consuming the token.
3685 we will stop after the body of the erroneously declared
3686 function, but before consuming the following `typedef'
3688 if (--nesting_depth
== 0)
3690 cp_lexer_consume_token (parser
->lexer
);
3695 case CPP_OPEN_BRACE
:
3703 /* Consume the token. */
3704 cp_lexer_consume_token (parser
->lexer
);
3708 /* This function is called at the end of a statement or declaration.
3709 If the next token is a semicolon, it is consumed; otherwise, error
3710 recovery is attempted. */
3713 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3715 /* Look for the trailing `;'. */
3716 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3718 /* If there is additional (erroneous) input, skip to the end of
3720 cp_parser_skip_to_end_of_statement (parser
);
3721 /* If the next token is now a `;', consume it. */
3722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3723 cp_lexer_consume_token (parser
->lexer
);
3727 /* Skip tokens until we have consumed an entire block, or until we
3728 have consumed a non-nested `;'. */
3731 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3733 int nesting_depth
= 0;
3735 /* Unwind generic function template scope if necessary. */
3736 if (parser
->fully_implicit_function_template_p
)
3737 abort_fully_implicit_template (parser
);
3739 while (nesting_depth
>= 0)
3741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3743 switch (token
->type
)
3746 case CPP_PRAGMA_EOL
:
3747 /* If we've run out of tokens, stop. */
3751 /* Stop if this is an unnested ';'. */
3756 case CPP_CLOSE_BRACE
:
3757 /* Stop if this is an unnested '}', or closes the outermost
3760 if (nesting_depth
< 0)
3766 case CPP_OPEN_BRACE
:
3775 /* Consume the token. */
3776 cp_lexer_consume_token (parser
->lexer
);
3780 /* Skip tokens until a non-nested closing curly brace is the next
3781 token, or there are no more tokens. Return true in the first case,
3785 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3787 unsigned nesting_depth
= 0;
3791 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3793 switch (token
->type
)
3796 case CPP_PRAGMA_EOL
:
3797 /* If we've run out of tokens, stop. */
3800 case CPP_CLOSE_BRACE
:
3801 /* If the next token is a non-nested `}', then we have reached
3802 the end of the current block. */
3803 if (nesting_depth
-- == 0)
3807 case CPP_OPEN_BRACE
:
3808 /* If it the next token is a `{', then we are entering a new
3809 block. Consume the entire block. */
3817 /* Consume the token. */
3818 cp_lexer_consume_token (parser
->lexer
);
3822 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3823 parameter is the PRAGMA token, allowing us to purge the entire pragma
3827 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3831 parser
->lexer
->in_pragma
= false;
3834 token
= cp_lexer_consume_token (parser
->lexer
);
3835 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3837 /* Ensure that the pragma is not parsed again. */
3838 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3841 /* Require pragma end of line, resyncing with it as necessary. The
3842 arguments are as for cp_parser_skip_to_pragma_eol. */
3845 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3847 parser
->lexer
->in_pragma
= false;
3848 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3849 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3852 /* This is a simple wrapper around make_typename_type. When the id is
3853 an unresolved identifier node, we can provide a superior diagnostic
3854 using cp_parser_diagnose_invalid_type_name. */
3857 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3858 location_t id_location
)
3861 if (identifier_p (id
))
3863 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3864 /*complain=*/tf_none
);
3865 if (result
== error_mark_node
)
3866 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3869 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3872 /* This is a wrapper around the
3873 make_{pointer,ptrmem,reference}_declarator functions that decides
3874 which one to call based on the CODE and CLASS_TYPE arguments. The
3875 CODE argument should be one of the values returned by
3876 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3877 appertain to the pointer or reference. */
3879 static cp_declarator
*
3880 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3881 cp_cv_quals cv_qualifiers
,
3882 cp_declarator
*target
,
3885 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3886 return cp_error_declarator
;
3888 if (code
== INDIRECT_REF
)
3889 if (class_type
== NULL_TREE
)
3890 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3892 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3893 target
, attributes
);
3894 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3895 return make_reference_declarator (cv_qualifiers
, target
,
3897 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3898 return make_reference_declarator (cv_qualifiers
, target
,
3903 /* Create a new C++ parser. */
3906 cp_parser_new (void)
3912 /* cp_lexer_new_main is called before doing GC allocation because
3913 cp_lexer_new_main might load a PCH file. */
3914 lexer
= cp_lexer_new_main ();
3916 /* Initialize the binops_by_token so that we can get the tree
3917 directly from the token. */
3918 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3919 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3921 parser
= ggc_cleared_alloc
<cp_parser
> ();
3922 parser
->lexer
= lexer
;
3923 parser
->context
= cp_parser_context_new (NULL
);
3925 /* For now, we always accept GNU extensions. */
3926 parser
->allow_gnu_extensions_p
= 1;
3928 /* The `>' token is a greater-than operator, not the end of a
3930 parser
->greater_than_is_operator_p
= true;
3932 parser
->default_arg_ok_p
= true;
3934 /* We are not parsing a constant-expression. */
3935 parser
->integral_constant_expression_p
= false;
3936 parser
->allow_non_integral_constant_expression_p
= false;
3937 parser
->non_integral_constant_expression_p
= false;
3939 /* Local variable names are not forbidden. */
3940 parser
->local_variables_forbidden_p
= false;
3942 /* We are not processing an `extern "C"' declaration. */
3943 parser
->in_unbraced_linkage_specification_p
= false;
3945 /* We are not processing a declarator. */
3946 parser
->in_declarator_p
= false;
3948 /* We are not processing a template-argument-list. */
3949 parser
->in_template_argument_list_p
= false;
3951 /* We are not in an iteration statement. */
3952 parser
->in_statement
= 0;
3954 /* We are not in a switch statement. */
3955 parser
->in_switch_statement_p
= false;
3957 /* We are not parsing a type-id inside an expression. */
3958 parser
->in_type_id_in_expr_p
= false;
3960 /* String literals should be translated to the execution character set. */
3961 parser
->translate_strings_p
= true;
3963 /* We are not parsing a function body. */
3964 parser
->in_function_body
= false;
3966 /* We can correct until told otherwise. */
3967 parser
->colon_corrects_to_scope_p
= true;
3969 /* The unparsed function queue is empty. */
3970 push_unparsed_function_queues (parser
);
3972 /* There are no classes being defined. */
3973 parser
->num_classes_being_defined
= 0;
3975 /* No template parameters apply. */
3976 parser
->num_template_parameter_lists
= 0;
3978 /* Special parsing data structures. */
3979 parser
->omp_declare_simd
= NULL
;
3980 parser
->oacc_routine
= NULL
;
3982 /* Not declaring an implicit function template. */
3983 parser
->auto_is_implicit_function_template_parm_p
= false;
3984 parser
->fully_implicit_function_template_p
= false;
3985 parser
->implicit_template_parms
= 0;
3986 parser
->implicit_template_scope
= 0;
3988 /* Allow constrained-type-specifiers. */
3989 parser
->prevent_constrained_type_specifiers
= 0;
3991 /* We haven't yet seen an 'extern "C"'. */
3992 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3997 /* Create a cp_lexer structure which will emit the tokens in CACHE
3998 and push it onto the parser's lexer stack. This is used for delayed
3999 parsing of in-class method bodies and default arguments, and should
4000 not be confused with tentative parsing. */
4002 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4004 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4005 lexer
->next
= parser
->lexer
;
4006 parser
->lexer
= lexer
;
4008 /* Move the current source position to that of the first token in the
4010 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4013 /* Pop the top lexer off the parser stack. This is never used for the
4014 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4016 cp_parser_pop_lexer (cp_parser
*parser
)
4018 cp_lexer
*lexer
= parser
->lexer
;
4019 parser
->lexer
= lexer
->next
;
4020 cp_lexer_destroy (lexer
);
4022 /* Put the current source position back where it was before this
4023 lexer was pushed. */
4024 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4027 /* Lexical conventions [gram.lex] */
4029 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4033 cp_parser_identifier (cp_parser
* parser
)
4037 /* Look for the identifier. */
4038 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4039 /* Return the value. */
4041 return cp_expr (token
->u
.value
, token
->location
);
4043 return error_mark_node
;
4046 /* Parse a sequence of adjacent string constants. Returns a
4047 TREE_STRING representing the combined, nul-terminated string
4048 constant. If TRANSLATE is true, translate the string to the
4049 execution character set. If WIDE_OK is true, a wide string is
4052 C++98 [lex.string] says that if a narrow string literal token is
4053 adjacent to a wide string literal token, the behavior is undefined.
4054 However, C99 6.4.5p4 says that this results in a wide string literal.
4055 We follow C99 here, for consistency with the C front end.
4057 This code is largely lifted from lex_string() in c-lex.c.
4059 FUTURE: ObjC++ will need to handle @-strings here. */
4061 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4062 bool lookup_udlit
= true)
4066 struct obstack str_ob
;
4067 struct obstack loc_ob
;
4068 cpp_string str
, istr
, *strs
;
4070 enum cpp_ttype type
, curr_type
;
4071 int have_suffix_p
= 0;
4073 tree suffix_id
= NULL_TREE
;
4074 bool curr_tok_is_userdef_p
= false;
4076 tok
= cp_lexer_peek_token (parser
->lexer
);
4077 if (!cp_parser_is_string_literal (tok
))
4079 cp_parser_error (parser
, "expected string-literal");
4080 return error_mark_node
;
4083 location_t loc
= tok
->location
;
4085 if (cpp_userdef_string_p (tok
->type
))
4087 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4088 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4089 curr_tok_is_userdef_p
= true;
4093 string_tree
= tok
->u
.value
;
4094 curr_type
= tok
->type
;
4098 /* Try to avoid the overhead of creating and destroying an obstack
4099 for the common case of just one string. */
4100 if (!cp_parser_is_string_literal
4101 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4103 cp_lexer_consume_token (parser
->lexer
);
4105 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4106 str
.len
= TREE_STRING_LENGTH (string_tree
);
4109 if (curr_tok_is_userdef_p
)
4111 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4113 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4116 curr_type
= tok
->type
;
4122 location_t last_tok_loc
= tok
->location
;
4123 gcc_obstack_init (&str_ob
);
4124 gcc_obstack_init (&loc_ob
);
4129 cp_lexer_consume_token (parser
->lexer
);
4131 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4132 str
.len
= TREE_STRING_LENGTH (string_tree
);
4134 if (curr_tok_is_userdef_p
)
4136 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4137 if (have_suffix_p
== 0)
4139 suffix_id
= curr_suffix_id
;
4142 else if (have_suffix_p
== 1
4143 && curr_suffix_id
!= suffix_id
)
4145 error ("inconsistent user-defined literal suffixes"
4146 " %qD and %qD in string literal",
4147 suffix_id
, curr_suffix_id
);
4150 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4153 curr_type
= tok
->type
;
4155 if (type
!= curr_type
)
4157 if (type
== CPP_STRING
)
4159 else if (curr_type
!= CPP_STRING
)
4161 rich_location
rich_loc (line_table
, tok
->location
);
4162 rich_loc
.add_range (last_tok_loc
);
4163 error_at (&rich_loc
,
4164 "unsupported non-standard concatenation "
4165 "of string literals");
4169 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4170 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4172 last_tok_loc
= tok
->location
;
4174 tok
= cp_lexer_peek_token (parser
->lexer
);
4175 if (cpp_userdef_string_p (tok
->type
))
4177 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4178 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4179 curr_tok_is_userdef_p
= true;
4183 string_tree
= tok
->u
.value
;
4184 curr_type
= tok
->type
;
4185 curr_tok_is_userdef_p
= false;
4188 while (cp_parser_is_string_literal (tok
));
4190 /* A string literal built by concatenation has its caret=start at
4191 the start of the initial string, and its finish at the finish of
4192 the final string literal. */
4193 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4195 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4198 if (type
!= CPP_STRING
&& !wide_ok
)
4200 cp_parser_error (parser
, "a wide string is invalid in this context");
4204 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4205 (parse_in
, strs
, count
, &istr
, type
))
4207 value
= build_string (istr
.len
, (const char *)istr
.text
);
4208 free (CONST_CAST (unsigned char *, istr
.text
));
4211 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4212 gcc_assert (g_string_concat_db
);
4213 g_string_concat_db
->record_string_concatenation (count
, locs
);
4220 case CPP_UTF8STRING
:
4221 TREE_TYPE (value
) = char_array_type_node
;
4224 TREE_TYPE (value
) = char16_array_type_node
;
4227 TREE_TYPE (value
) = char32_array_type_node
;
4230 TREE_TYPE (value
) = wchar_array_type_node
;
4234 value
= fix_string_type (value
);
4238 tree literal
= build_userdef_literal (suffix_id
, value
,
4239 OT_NONE
, NULL_TREE
);
4241 value
= cp_parser_userdef_string_literal (literal
);
4247 /* cpp_interpret_string has issued an error. */
4248 value
= error_mark_node
;
4252 obstack_free (&str_ob
, 0);
4253 obstack_free (&loc_ob
, 0);
4256 return cp_expr (value
, loc
);
4259 /* Look up a literal operator with the name and the exact arguments. */
4262 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4264 tree decl
= lookup_name (name
);
4265 if (!decl
|| !is_overloaded_fn (decl
))
4266 return error_mark_node
;
4268 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4272 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4278 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4279 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4281 tree tparm
= TREE_VALUE (parmtypes
);
4282 tree targ
= TREE_TYPE ((*args
)[ix
]);
4283 bool ptr
= TYPE_PTR_P (tparm
);
4284 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4285 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4287 || !same_type_p (TREE_TYPE (tparm
),
4293 && ix
== vec_safe_length (args
)
4294 /* May be this should be sufficient_parms_p instead,
4295 depending on how exactly should user-defined literals
4296 work in presence of default arguments on the literal
4297 operator parameters. */
4298 && parmtypes
== void_list_node
)
4303 return error_mark_node
;
4306 /* Parse a user-defined char constant. Returns a call to a user-defined
4307 literal operator taking the character as an argument. */
4310 cp_parser_userdef_char_literal (cp_parser
*parser
)
4312 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4313 tree literal
= token
->u
.value
;
4314 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4315 tree value
= USERDEF_LITERAL_VALUE (literal
);
4316 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4319 /* Build up a call to the user-defined operator */
4320 /* Lookup the name we got back from the id-expression. */
4321 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4322 vec_safe_push (args
, value
);
4323 decl
= lookup_literal_operator (name
, args
);
4324 if (!decl
|| decl
== error_mark_node
)
4326 error ("unable to find character literal operator %qD with %qT argument",
4327 name
, TREE_TYPE (value
));
4328 release_tree_vector (args
);
4329 return error_mark_node
;
4331 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4332 release_tree_vector (args
);
4336 /* A subroutine of cp_parser_userdef_numeric_literal to
4337 create a char... template parameter pack from a string node. */
4340 make_char_string_pack (tree value
)
4343 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4344 const char *str
= TREE_STRING_POINTER (value
);
4345 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4346 tree argvec
= make_tree_vec (1);
4348 /* Fill in CHARVEC with all of the parameters. */
4349 charvec
= make_tree_vec (len
);
4350 for (i
= 0; i
< len
; ++i
)
4352 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4353 cpp_string in
= { 3, s
};
4354 cpp_string out
= { 0, 0 };
4355 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4357 gcc_assert (out
.len
== 2);
4358 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4362 /* Build the argument packs. */
4363 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4365 TREE_VEC_ELT (argvec
, 0) = argpack
;
4370 /* A subroutine of cp_parser_userdef_numeric_literal to
4371 create a char... template parameter pack from a string node. */
4374 make_string_pack (tree value
)
4377 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4378 const unsigned char *str
4379 = (const unsigned char *) TREE_STRING_POINTER (value
);
4380 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4381 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4382 tree argvec
= make_tree_vec (2);
4384 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4385 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4387 /* First template parm is character type. */
4388 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4390 /* Fill in CHARVEC with all of the parameters. */
4391 charvec
= make_tree_vec (len
);
4392 for (int i
= 0; i
< len
; ++i
)
4393 TREE_VEC_ELT (charvec
, i
)
4394 = double_int_to_tree (str_char_type_node
,
4395 double_int::from_buffer (str
+ i
* sz
, sz
));
4397 /* Build the argument packs. */
4398 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4400 TREE_VEC_ELT (argvec
, 1) = argpack
;
4405 /* Parse a user-defined numeric constant. returns a call to a user-defined
4406 literal operator. */
4409 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4411 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4412 tree literal
= token
->u
.value
;
4413 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4414 tree value
= USERDEF_LITERAL_VALUE (literal
);
4415 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4416 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4417 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4419 vec
<tree
, va_gc
> *args
;
4421 /* Look for a literal operator taking the exact type of numeric argument
4422 as the literal value. */
4423 args
= make_tree_vector ();
4424 vec_safe_push (args
, value
);
4425 decl
= lookup_literal_operator (name
, args
);
4426 if (decl
&& decl
!= error_mark_node
)
4428 result
= finish_call_expr (decl
, &args
, false, true,
4429 tf_warning_or_error
);
4431 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4433 warning_at (token
->location
, OPT_Woverflow
,
4434 "integer literal exceeds range of %qT type",
4435 long_long_unsigned_type_node
);
4440 warning_at (token
->location
, OPT_Woverflow
,
4441 "floating literal exceeds range of %qT type",
4442 long_double_type_node
);
4443 else if (overflow
< 0)
4444 warning_at (token
->location
, OPT_Woverflow
,
4445 "floating literal truncated to zero");
4448 release_tree_vector (args
);
4451 release_tree_vector (args
);
4453 /* If the numeric argument didn't work, look for a raw literal
4454 operator taking a const char* argument consisting of the number
4455 in string format. */
4456 args
= make_tree_vector ();
4457 vec_safe_push (args
, num_string
);
4458 decl
= lookup_literal_operator (name
, args
);
4459 if (decl
&& decl
!= error_mark_node
)
4461 result
= finish_call_expr (decl
, &args
, false, true,
4462 tf_warning_or_error
);
4463 release_tree_vector (args
);
4466 release_tree_vector (args
);
4468 /* If the raw literal didn't work, look for a non-type template
4469 function with parameter pack char.... Call the function with
4470 template parameter characters representing the number. */
4471 args
= make_tree_vector ();
4472 decl
= lookup_literal_operator (name
, args
);
4473 if (decl
&& decl
!= error_mark_node
)
4475 tree tmpl_args
= make_char_string_pack (num_string
);
4476 if (tmpl_args
== NULL_TREE
)
4478 error ("failed to translate literal to execution character set %qT",
4480 return error_mark_node
;
4482 decl
= lookup_template_function (decl
, tmpl_args
);
4483 result
= finish_call_expr (decl
, &args
, false, true,
4484 tf_warning_or_error
);
4485 release_tree_vector (args
);
4489 release_tree_vector (args
);
4491 /* In C++14 the standard library defines complex number suffixes that
4492 conflict with GNU extensions. Prefer them if <complex> is #included. */
4493 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4494 bool i14
= (cxx_dialect
> cxx11
4495 && (id_equal (suffix_id
, "i")
4496 || id_equal (suffix_id
, "if")
4497 || id_equal (suffix_id
, "il")));
4498 diagnostic_t kind
= DK_ERROR
;
4503 tree cxlit
= lookup_qualified_name (std_node
,
4504 get_identifier ("complex_literals"),
4506 if (cxlit
== error_mark_node
)
4508 /* No <complex>, so pedwarn and use GNU semantics. */
4510 opt
= OPT_Wpedantic
;
4515 = emit_diagnostic (kind
, input_location
, opt
,
4516 "unable to find numeric literal operator %qD", name
);
4519 /* Don't inform either. */;
4522 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4523 "(from <complex>) to enable the C++14 user-defined literal "
4526 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4527 "GNU built-in suffix");
4530 inform (token
->location
, "use -fext-numeric-literals "
4531 "to enable more built-in suffixes");
4533 if (kind
== DK_ERROR
)
4534 value
= error_mark_node
;
4537 /* Use the built-in semantics. */
4539 if (id_equal (suffix_id
, "i"))
4541 if (TREE_CODE (value
) == INTEGER_CST
)
4542 type
= integer_type_node
;
4544 type
= double_type_node
;
4546 else if (id_equal (suffix_id
, "if"))
4547 type
= float_type_node
;
4548 else /* if (id_equal (suffix_id, "il")) */
4549 type
= long_double_type_node
;
4551 value
= build_complex (build_complex_type (type
),
4552 fold_convert (type
, integer_zero_node
),
4553 fold_convert (type
, value
));
4556 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4557 /* Avoid repeated diagnostics. */
4558 token
->u
.value
= value
;
4562 /* Parse a user-defined string constant. Returns a call to a user-defined
4563 literal operator taking a character pointer and the length of the string
4567 cp_parser_userdef_string_literal (tree literal
)
4569 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4570 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4571 tree value
= USERDEF_LITERAL_VALUE (literal
);
4572 int len
= TREE_STRING_LENGTH (value
)
4573 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4576 /* Build up a call to the user-defined operator. */
4577 /* Lookup the name we got back from the id-expression. */
4578 releasing_vec rargs
;
4579 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4580 vec_safe_push (args
, value
);
4581 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4582 decl
= lookup_literal_operator (name
, args
);
4584 if (decl
&& decl
!= error_mark_node
)
4585 return finish_call_expr (decl
, &args
, false, true,
4586 tf_warning_or_error
);
4588 /* Look for a suitable template function, either (C++20) with a single
4589 parameter of class type, or (N3599) with typename parameter CharT and
4590 parameter pack CharT... */
4592 decl
= lookup_literal_operator (name
, args
);
4593 if (decl
&& decl
!= error_mark_node
)
4595 /* Use resolve_nondeduced_context to try to choose one form of template
4597 tree tmpl_args
= make_tree_vec (1);
4598 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4599 decl
= lookup_template_function (decl
, tmpl_args
);
4600 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4605 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4606 res
= resolve_nondeduced_context (decl
, tf_none
);
4610 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4611 TREE_OPERAND (decl
, 1) = tmpl_args
;
4612 return finish_call_expr (decl
, &args
, false, true,
4613 tf_warning_or_error
);
4616 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4617 name
, TREE_TYPE (value
), size_type_node
);
4618 return error_mark_node
;
4622 /* Basic concepts [gram.basic] */
4624 /* Parse a translation-unit.
4627 declaration-seq [opt] */
4630 cp_parser_translation_unit (cp_parser
* parser
)
4632 gcc_checking_assert (!cp_error_declarator
);
4634 /* Create the declarator obstack. */
4635 gcc_obstack_init (&declarator_obstack
);
4636 /* Create the error declarator. */
4637 cp_error_declarator
= make_declarator (cdk_error
);
4638 /* Create the empty parameter list. */
4639 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4641 /* Remember where the base of the declarator obstack lies. */
4642 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4644 bool implicit_extern_c
= false;
4648 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4650 /* If we're entering or exiting a region that's implicitly
4651 extern "C", modify the lang context appropriately. */
4652 if (implicit_extern_c
4653 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4655 implicit_extern_c
= !implicit_extern_c
;
4656 if (implicit_extern_c
)
4657 push_lang_context (lang_name_c
);
4659 pop_lang_context ();
4662 if (token
->type
== CPP_EOF
)
4665 if (token
->type
== CPP_CLOSE_BRACE
)
4667 cp_parser_error (parser
, "expected declaration");
4668 cp_lexer_consume_token (parser
->lexer
);
4669 /* If the next token is now a `;', consume it. */
4670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4671 cp_lexer_consume_token (parser
->lexer
);
4674 cp_parser_toplevel_declaration (parser
);
4677 /* Get rid of the token array; we don't need it any more. */
4678 cp_lexer_destroy (parser
->lexer
);
4679 parser
->lexer
= NULL
;
4681 /* The EOF should have reset this. */
4682 gcc_checking_assert (!implicit_extern_c
);
4684 /* Make sure the declarator obstack was fully cleaned up. */
4685 gcc_assert (obstack_next_free (&declarator_obstack
)
4686 == declarator_obstack_base
);
4689 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4690 decltype context. */
4692 static inline tsubst_flags_t
4693 complain_flags (bool decltype_p
)
4695 tsubst_flags_t complain
= tf_warning_or_error
;
4697 complain
|= tf_decltype
;
4701 /* We're about to parse a collection of statements. If we're currently
4702 parsing tentatively, set up a firewall so that any nested
4703 cp_parser_commit_to_tentative_parse won't affect the current context. */
4705 static cp_token_position
4706 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4711 cp_parser_parse_tentatively (parser
);
4712 cp_parser_commit_to_topmost_tentative_parse (parser
);
4713 return cp_lexer_token_position (parser
->lexer
, false);
4716 /* We've finished parsing the collection of statements. Wrap up the
4717 firewall and replace the relevant tokens with the parsed form. */
4720 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4726 /* Finish the firewall level. */
4727 cp_parser_parse_definitely (parser
);
4728 /* And remember the result of the parse for when we try again. */
4729 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4730 token
->type
= CPP_PREPARSED_EXPR
;
4731 token
->u
.value
= expr
;
4732 token
->keyword
= RID_MAX
;
4733 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4736 /* Like the above functions, but let the user modify the tokens. Used by
4737 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4738 later parses, so it makes sense to localize the effects of
4739 cp_parser_commit_to_tentative_parse. */
4741 struct tentative_firewall
4746 tentative_firewall (cp_parser
*p
): parser(p
)
4748 /* If we're currently parsing tentatively, start a committed level as a
4749 firewall and then an inner tentative parse. */
4750 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4752 cp_parser_parse_tentatively (parser
);
4753 cp_parser_commit_to_topmost_tentative_parse (parser
);
4754 cp_parser_parse_tentatively (parser
);
4758 ~tentative_firewall()
4762 /* Finish the inner tentative parse and the firewall, propagating any
4763 uncommitted error state to the outer tentative parse. */
4764 bool err
= cp_parser_error_occurred (parser
);
4765 cp_parser_parse_definitely (parser
);
4766 cp_parser_parse_definitely (parser
);
4768 cp_parser_simulate_error (parser
);
4773 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4774 This class is for tracking such a matching pair of symbols.
4775 In particular, it tracks the location of the first token,
4776 so that if the second token is missing, we can highlight the
4777 location of the first token when notifying the user about the
4780 template <typename traits_t
>
4784 /* token_pair's ctor. */
4785 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4787 /* If the next token is the opening symbol for this pair, consume it and
4789 Otherwise, issue an error and return false.
4790 In either case, record the location of the opening token. */
4792 bool require_open (cp_parser
*parser
)
4794 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4795 return cp_parser_require (parser
, traits_t::open_token_type
,
4796 traits_t::required_token_open
);
4799 /* Consume the next token from PARSER, recording its location as
4800 that of the opening token within the pair. */
4802 cp_token
* consume_open (cp_parser
*parser
)
4804 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4805 gcc_assert (tok
->type
== traits_t::open_token_type
);
4806 m_open_loc
= tok
->location
;
4810 /* If the next token is the closing symbol for this pair, consume it
4812 Otherwise, issue an error, highlighting the location of the
4813 corresponding opening token, and return NULL. */
4815 cp_token
*require_close (cp_parser
*parser
) const
4817 return cp_parser_require (parser
, traits_t::close_token_type
,
4818 traits_t::required_token_close
,
4823 location_t m_open_loc
;
4826 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4828 struct matching_paren_traits
4830 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4831 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4832 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4833 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4836 /* "matching_parens" is a token_pair<T> class for tracking matching
4837 pairs of parentheses. */
4839 typedef token_pair
<matching_paren_traits
> matching_parens
;
4841 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4843 struct matching_brace_traits
4845 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4846 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4847 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4848 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4851 /* "matching_braces" is a token_pair<T> class for tracking matching
4854 typedef token_pair
<matching_brace_traits
> matching_braces
;
4857 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4858 enclosing parentheses. */
4861 cp_parser_statement_expr (cp_parser
*parser
)
4863 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4865 /* Consume the '('. */
4866 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4867 matching_parens parens
;
4868 parens
.consume_open (parser
);
4869 /* Start the statement-expression. */
4870 tree expr
= begin_stmt_expr ();
4871 /* Parse the compound-statement. */
4872 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4874 expr
= finish_stmt_expr (expr
, false);
4875 /* Consume the ')'. */
4876 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4877 if (!parens
.require_close (parser
))
4878 cp_parser_skip_to_end_of_statement (parser
);
4880 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4881 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4882 return cp_expr (expr
, combined_loc
);
4885 /* Expressions [gram.expr] */
4887 /* Parse a fold-operator.
4890 - * / % ^ & | = < > << >>
4891 = -= *= /= %= ^= &= |= <<= >>=
4892 == != <= >= && || , .* ->*
4894 This returns the tree code corresponding to the matched operator
4895 as an int. When the current token matches a compound assignment
4896 opertor, the resulting tree code is the negative value of the
4897 non-assignment operator. */
4900 cp_parser_fold_operator (cp_token
*token
)
4902 switch (token
->type
)
4904 case CPP_PLUS
: return PLUS_EXPR
;
4905 case CPP_MINUS
: return MINUS_EXPR
;
4906 case CPP_MULT
: return MULT_EXPR
;
4907 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4908 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4909 case CPP_XOR
: return BIT_XOR_EXPR
;
4910 case CPP_AND
: return BIT_AND_EXPR
;
4911 case CPP_OR
: return BIT_IOR_EXPR
;
4912 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4913 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4915 case CPP_EQ
: return -NOP_EXPR
;
4916 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4917 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4918 case CPP_MULT_EQ
: return -MULT_EXPR
;
4919 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4920 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4921 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4922 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4923 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4924 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4925 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4927 case CPP_EQ_EQ
: return EQ_EXPR
;
4928 case CPP_NOT_EQ
: return NE_EXPR
;
4929 case CPP_LESS
: return LT_EXPR
;
4930 case CPP_GREATER
: return GT_EXPR
;
4931 case CPP_LESS_EQ
: return LE_EXPR
;
4932 case CPP_GREATER_EQ
: return GE_EXPR
;
4934 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4935 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4937 case CPP_COMMA
: return COMPOUND_EXPR
;
4939 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4940 case CPP_DEREF_STAR
: return MEMBER_REF
;
4942 default: return ERROR_MARK
;
4946 /* Returns true if CODE indicates a binary expression, which is not allowed in
4947 the LHS of a fold-expression. More codes will need to be added to use this
4948 function in other contexts. */
4951 is_binary_op (tree_code code
)
4956 case POINTER_PLUS_EXPR
:
4959 case TRUNC_DIV_EXPR
:
4960 case TRUNC_MOD_EXPR
:
4976 case TRUTH_ANDIF_EXPR
:
4977 case TRUTH_ORIF_EXPR
:
4990 /* If the next token is a suitable fold operator, consume it and return as
4991 the function above. */
4994 cp_parser_fold_operator (cp_parser
*parser
)
4996 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4997 int code
= cp_parser_fold_operator (token
);
4998 if (code
!= ERROR_MARK
)
4999 cp_lexer_consume_token (parser
->lexer
);
5003 /* Parse a fold-expression.
5006 ( ... folding-operator cast-expression)
5007 ( cast-expression folding-operator ... )
5008 ( cast-expression folding operator ... folding-operator cast-expression)
5010 Note that the '(' and ')' are matched in primary expression. */
5013 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5020 cp_lexer_consume_token (parser
->lexer
);
5021 int op
= cp_parser_fold_operator (parser
);
5022 if (op
== ERROR_MARK
)
5024 cp_parser_error (parser
, "expected binary operator");
5025 return error_mark_node
;
5028 tree expr
= cp_parser_cast_expression (parser
, false, false,
5030 if (expr
== error_mark_node
)
5031 return error_mark_node
;
5032 return finish_left_unary_fold_expr (expr
, op
);
5035 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5036 int op
= cp_parser_fold_operator (parser
);
5037 if (op
== ERROR_MARK
)
5039 cp_parser_error (parser
, "expected binary operator");
5040 return error_mark_node
;
5043 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5045 cp_parser_error (parser
, "expected ...");
5046 return error_mark_node
;
5048 cp_lexer_consume_token (parser
->lexer
);
5050 /* The operands of a fold-expression are cast-expressions, so binary or
5051 conditional expressions are not allowed. We check this here to avoid
5052 tentative parsing. */
5053 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5054 /* OK, the expression was parenthesized. */;
5055 else if (is_binary_op (TREE_CODE (expr1
)))
5056 error_at (location_of (expr1
),
5057 "binary expression in operand of fold-expression");
5058 else if (TREE_CODE (expr1
) == COND_EXPR
5059 || (REFERENCE_REF_P (expr1
)
5060 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5061 error_at (location_of (expr1
),
5062 "conditional expression in operand of fold-expression");
5065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5066 return finish_right_unary_fold_expr (expr1
, op
);
5068 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5070 cp_parser_error (parser
, "mismatched operator in fold-expression");
5071 return error_mark_node
;
5073 cp_lexer_consume_token (parser
->lexer
);
5075 // Binary left or right fold.
5076 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5077 if (expr2
== error_mark_node
)
5078 return error_mark_node
;
5079 return finish_binary_fold_expr (expr1
, expr2
, op
);
5082 /* Parse a primary-expression.
5089 lambda-expression (C++11)
5094 ( compound-statement )
5095 __builtin_va_arg ( assignment-expression , type-id )
5096 __builtin_offsetof ( type-id , offsetof-expression )
5099 __has_nothrow_assign ( type-id )
5100 __has_nothrow_constructor ( type-id )
5101 __has_nothrow_copy ( type-id )
5102 __has_trivial_assign ( type-id )
5103 __has_trivial_constructor ( type-id )
5104 __has_trivial_copy ( type-id )
5105 __has_trivial_destructor ( type-id )
5106 __has_virtual_destructor ( type-id )
5107 __is_abstract ( type-id )
5108 __is_base_of ( type-id , type-id )
5109 __is_class ( type-id )
5110 __is_empty ( type-id )
5111 __is_enum ( type-id )
5112 __is_final ( type-id )
5113 __is_literal_type ( type-id )
5114 __is_pod ( type-id )
5115 __is_polymorphic ( type-id )
5116 __is_std_layout ( type-id )
5117 __is_trivial ( type-id )
5118 __is_union ( type-id )
5120 Objective-C++ Extension:
5128 ADDRESS_P is true iff this expression was immediately preceded by
5129 "&" and therefore might denote a pointer-to-member. CAST_P is true
5130 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5131 true iff this expression is a template argument.
5133 Returns a representation of the expression. Upon return, *IDK
5134 indicates what kind of id-expression (if any) was present. */
5137 cp_parser_primary_expression (cp_parser
*parser
,
5140 bool template_arg_p
,
5144 cp_token
*token
= NULL
;
5146 /* Assume the primary expression is not an id-expression. */
5147 *idk
= CP_ID_KIND_NONE
;
5149 /* Peek at the next token. */
5150 token
= cp_lexer_peek_token (parser
->lexer
);
5151 switch ((int) token
->type
)
5160 user-defined-literal */
5167 case CPP_PREPARSED_EXPR
:
5168 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5169 return cp_parser_userdef_numeric_literal (parser
);
5170 token
= cp_lexer_consume_token (parser
->lexer
);
5171 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5173 error_at (token
->location
,
5174 "fixed-point types not supported in C++");
5175 return error_mark_node
;
5177 /* Floating-point literals are only allowed in an integral
5178 constant expression if they are cast to an integral or
5179 enumeration type. */
5180 if (TREE_CODE (token
->u
.value
) == REAL_CST
5181 && parser
->integral_constant_expression_p
5184 /* CAST_P will be set even in invalid code like "int(2.7 +
5185 ...)". Therefore, we have to check that the next token
5186 is sure to end the cast. */
5189 cp_token
*next_token
;
5191 next_token
= cp_lexer_peek_token (parser
->lexer
);
5192 if (/* The comma at the end of an
5193 enumerator-definition. */
5194 next_token
->type
!= CPP_COMMA
5195 /* The curly brace at the end of an enum-specifier. */
5196 && next_token
->type
!= CPP_CLOSE_BRACE
5197 /* The end of a statement. */
5198 && next_token
->type
!= CPP_SEMICOLON
5199 /* The end of the cast-expression. */
5200 && next_token
->type
!= CPP_CLOSE_PAREN
5201 /* The end of an array bound. */
5202 && next_token
->type
!= CPP_CLOSE_SQUARE
5203 /* The closing ">" in a template-argument-list. */
5204 && (next_token
->type
!= CPP_GREATER
5205 || parser
->greater_than_is_operator_p
)
5206 /* C++0x only: A ">>" treated like two ">" tokens,
5207 in a template-argument-list. */
5208 && (next_token
->type
!= CPP_RSHIFT
5209 || (cxx_dialect
== cxx98
)
5210 || parser
->greater_than_is_operator_p
))
5214 /* If we are within a cast, then the constraint that the
5215 cast is to an integral or enumeration type will be
5216 checked at that point. If we are not within a cast, then
5217 this code is invalid. */
5219 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5221 return cp_expr (token
->u
.value
, token
->location
);
5223 case CPP_CHAR_USERDEF
:
5224 case CPP_CHAR16_USERDEF
:
5225 case CPP_CHAR32_USERDEF
:
5226 case CPP_WCHAR_USERDEF
:
5227 case CPP_UTF8CHAR_USERDEF
:
5228 return cp_parser_userdef_char_literal (parser
);
5234 case CPP_UTF8STRING
:
5235 case CPP_STRING_USERDEF
:
5236 case CPP_STRING16_USERDEF
:
5237 case CPP_STRING32_USERDEF
:
5238 case CPP_WSTRING_USERDEF
:
5239 case CPP_UTF8STRING_USERDEF
:
5240 /* ??? Should wide strings be allowed when parser->translate_strings_p
5241 is false (i.e. in attributes)? If not, we can kill the third
5242 argument to cp_parser_string_literal. */
5243 return cp_parser_string_literal (parser
,
5244 parser
->translate_strings_p
,
5247 case CPP_OPEN_PAREN
:
5248 /* If we see `( { ' then we are looking at the beginning of
5249 a GNU statement-expression. */
5250 if (cp_parser_allow_gnu_extensions_p (parser
)
5251 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5253 /* Statement-expressions are not allowed by the standard. */
5254 pedwarn (token
->location
, OPT_Wpedantic
,
5255 "ISO C++ forbids braced-groups within expressions");
5257 /* And they're not allowed outside of a function-body; you
5258 cannot, for example, write:
5260 int i = ({ int j = 3; j + 1; });
5262 at class or namespace scope. */
5263 if (!parser
->in_function_body
5264 || parser
->in_template_argument_list_p
)
5266 error_at (token
->location
,
5267 "statement-expressions are not allowed outside "
5268 "functions nor in template-argument lists");
5269 cp_parser_skip_to_end_of_block_or_statement (parser
);
5270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5271 cp_lexer_consume_token (parser
->lexer
);
5272 return error_mark_node
;
5275 return cp_parser_statement_expr (parser
);
5277 /* Otherwise it's a normal parenthesized expression. */
5280 bool saved_greater_than_is_operator_p
;
5282 location_t open_paren_loc
= token
->location
;
5284 /* Consume the `('. */
5285 matching_parens parens
;
5286 parens
.consume_open (parser
);
5287 /* Within a parenthesized expression, a `>' token is always
5288 the greater-than operator. */
5289 saved_greater_than_is_operator_p
5290 = parser
->greater_than_is_operator_p
;
5291 parser
->greater_than_is_operator_p
= true;
5293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5294 /* Left fold expression. */
5297 /* Parse the parenthesized expression. */
5298 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5300 token
= cp_lexer_peek_token (parser
->lexer
);
5301 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5303 expr
= cp_parser_fold_expression (parser
, expr
);
5304 if (expr
!= error_mark_node
5305 && cxx_dialect
< cxx17
5306 && !in_system_header_at (input_location
))
5307 pedwarn (input_location
, 0, "fold-expressions only available "
5308 "with -std=c++17 or -std=gnu++17");
5311 /* Let the front end know that this expression was
5312 enclosed in parentheses. This matters in case, for
5313 example, the expression is of the form `A::B', since
5314 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5316 expr
= finish_parenthesized_expr (expr
);
5318 /* DR 705: Wrapping an unqualified name in parentheses
5319 suppresses arg-dependent lookup. We want to pass back
5320 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5321 (c++/37862), but none of the others. */
5322 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5323 *idk
= CP_ID_KIND_NONE
;
5325 /* The `>' token might be the end of a template-id or
5326 template-parameter-list now. */
5327 parser
->greater_than_is_operator_p
5328 = saved_greater_than_is_operator_p
;
5330 /* Consume the `)'. */
5331 token
= cp_lexer_peek_token (parser
->lexer
);
5332 location_t close_paren_loc
= token
->location
;
5333 expr
.set_range (open_paren_loc
, close_paren_loc
);
5334 if (!parens
.require_close (parser
)
5335 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5336 cp_parser_skip_to_end_of_statement (parser
);
5341 case CPP_OPEN_SQUARE
:
5343 if (c_dialect_objc ())
5345 /* We might have an Objective-C++ message. */
5346 cp_parser_parse_tentatively (parser
);
5347 tree msg
= cp_parser_objc_message_expression (parser
);
5348 /* If that works out, we're done ... */
5349 if (cp_parser_parse_definitely (parser
))
5351 /* ... else, fall though to see if it's a lambda. */
5353 cp_expr lam
= cp_parser_lambda_expression (parser
);
5354 /* Don't warn about a failed tentative parse. */
5355 if (cp_parser_error_occurred (parser
))
5356 return error_mark_node
;
5357 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5361 case CPP_OBJC_STRING
:
5362 if (c_dialect_objc ())
5363 /* We have an Objective-C++ string literal. */
5364 return cp_parser_objc_expression (parser
);
5365 cp_parser_error (parser
, "expected primary-expression");
5366 return error_mark_node
;
5369 switch (token
->keyword
)
5371 /* These two are the boolean literals. */
5373 cp_lexer_consume_token (parser
->lexer
);
5374 return cp_expr (boolean_true_node
, token
->location
);
5376 cp_lexer_consume_token (parser
->lexer
);
5377 return cp_expr (boolean_false_node
, token
->location
);
5379 /* The `__null' literal. */
5381 cp_lexer_consume_token (parser
->lexer
);
5382 return cp_expr (null_node
, token
->location
);
5384 /* The `nullptr' literal. */
5386 cp_lexer_consume_token (parser
->lexer
);
5387 return cp_expr (nullptr_node
, token
->location
);
5389 /* Recognize the `this' keyword. */
5391 cp_lexer_consume_token (parser
->lexer
);
5392 if (parser
->local_variables_forbidden_p
)
5394 error_at (token
->location
,
5395 "%<this%> may not be used in this context");
5396 return error_mark_node
;
5398 /* Pointers cannot appear in constant-expressions. */
5399 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5400 return error_mark_node
;
5401 return cp_expr (finish_this_expr (), token
->location
);
5403 /* The `operator' keyword can be the beginning of an
5408 case RID_FUNCTION_NAME
:
5409 case RID_PRETTY_FUNCTION_NAME
:
5410 case RID_C99_FUNCTION_NAME
:
5412 non_integral_constant name
;
5414 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5415 __func__ are the names of variables -- but they are
5416 treated specially. Therefore, they are handled here,
5417 rather than relying on the generic id-expression logic
5418 below. Grammatically, these names are id-expressions.
5420 Consume the token. */
5421 token
= cp_lexer_consume_token (parser
->lexer
);
5423 switch (token
->keyword
)
5425 case RID_FUNCTION_NAME
:
5426 name
= NIC_FUNC_NAME
;
5428 case RID_PRETTY_FUNCTION_NAME
:
5429 name
= NIC_PRETTY_FUNC
;
5431 case RID_C99_FUNCTION_NAME
:
5432 name
= NIC_C99_FUNC
;
5438 if (cp_parser_non_integral_constant_expression (parser
, name
))
5439 return error_mark_node
;
5441 /* Look up the name. */
5442 return finish_fname (token
->u
.value
);
5449 location_t type_location
;
5450 location_t start_loc
5451 = cp_lexer_peek_token (parser
->lexer
)->location
;
5452 /* The `__builtin_va_arg' construct is used to handle
5453 `va_arg'. Consume the `__builtin_va_arg' token. */
5454 cp_lexer_consume_token (parser
->lexer
);
5455 /* Look for the opening `('. */
5456 matching_parens parens
;
5457 parens
.require_open (parser
);
5458 /* Now, parse the assignment-expression. */
5459 expression
= cp_parser_assignment_expression (parser
);
5460 /* Look for the `,'. */
5461 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5462 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5463 /* Parse the type-id. */
5465 type_id_in_expr_sentinel
s (parser
);
5466 type
= cp_parser_type_id (parser
);
5468 /* Look for the closing `)'. */
5469 location_t finish_loc
5470 = cp_lexer_peek_token (parser
->lexer
)->location
;
5471 parens
.require_close (parser
);
5472 /* Using `va_arg' in a constant-expression is not
5474 if (cp_parser_non_integral_constant_expression (parser
,
5476 return error_mark_node
;
5477 /* Construct a location of the form:
5478 __builtin_va_arg (v, int)
5479 ~~~~~~~~~~~~~~~~~~~~~^~~~
5480 with the caret at the type, ranging from the start of the
5481 "__builtin_va_arg" token to the close paren. */
5482 location_t combined_loc
5483 = make_location (type_location
, start_loc
, finish_loc
);
5484 return build_x_va_arg (combined_loc
, expression
, type
);
5488 return cp_parser_builtin_offsetof (parser
);
5490 case RID_HAS_NOTHROW_ASSIGN
:
5491 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5492 case RID_HAS_NOTHROW_COPY
:
5493 case RID_HAS_TRIVIAL_ASSIGN
:
5494 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5495 case RID_HAS_TRIVIAL_COPY
:
5496 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5497 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5498 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5499 case RID_IS_ABSTRACT
:
5500 case RID_IS_AGGREGATE
:
5501 case RID_IS_BASE_OF
:
5506 case RID_IS_LITERAL_TYPE
:
5508 case RID_IS_POLYMORPHIC
:
5509 case RID_IS_SAME_AS
:
5510 case RID_IS_STD_LAYOUT
:
5511 case RID_IS_TRIVIAL
:
5512 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5513 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5514 case RID_IS_TRIVIALLY_COPYABLE
:
5516 case RID_IS_ASSIGNABLE
:
5517 case RID_IS_CONSTRUCTIBLE
:
5518 return cp_parser_trait_expr (parser
, token
->keyword
);
5522 return cp_parser_requires_expression (parser
);
5524 /* Objective-C++ expressions. */
5526 case RID_AT_PROTOCOL
:
5527 case RID_AT_SELECTOR
:
5528 return cp_parser_objc_expression (parser
);
5531 if (parser
->in_function_body
5532 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5535 error_at (token
->location
,
5536 "a template declaration cannot appear at block scope");
5537 cp_parser_skip_to_end_of_block_or_statement (parser
);
5538 return error_mark_node
;
5542 cp_parser_error (parser
, "expected primary-expression");
5543 return error_mark_node
;
5546 /* An id-expression can start with either an identifier, a
5547 `::' as the beginning of a qualified-id, or the "operator"
5551 case CPP_TEMPLATE_ID
:
5552 case CPP_NESTED_NAME_SPECIFIER
:
5555 cp_expr id_expression
;
5557 const char *error_msg
;
5560 cp_token
*id_expr_token
;
5562 /* Parse the id-expression. */
5564 = cp_parser_id_expression (parser
,
5565 /*template_keyword_p=*/false,
5566 /*check_dependency_p=*/true,
5568 /*declarator_p=*/false,
5569 /*optional_p=*/false);
5570 if (id_expression
== error_mark_node
)
5571 return error_mark_node
;
5572 id_expr_token
= token
;
5573 token
= cp_lexer_peek_token (parser
->lexer
);
5574 done
= (token
->type
!= CPP_OPEN_SQUARE
5575 && token
->type
!= CPP_OPEN_PAREN
5576 && token
->type
!= CPP_DOT
5577 && token
->type
!= CPP_DEREF
5578 && token
->type
!= CPP_PLUS_PLUS
5579 && token
->type
!= CPP_MINUS_MINUS
);
5580 /* If we have a template-id, then no further lookup is
5581 required. If the template-id was for a template-class, we
5582 will sometimes have a TYPE_DECL at this point. */
5583 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5584 || TREE_CODE (id_expression
) == TYPE_DECL
)
5585 decl
= id_expression
;
5586 /* Look up the name. */
5589 tree ambiguous_decls
;
5591 /* If we already know that this lookup is ambiguous, then
5592 we've already issued an error message; there's no reason
5594 if (id_expr_token
->type
== CPP_NAME
5595 && id_expr_token
->error_reported
)
5597 cp_parser_simulate_error (parser
);
5598 return error_mark_node
;
5601 decl
= cp_parser_lookup_name (parser
, id_expression
,
5604 /*is_namespace=*/false,
5605 /*check_dependency=*/true,
5607 id_expression
.get_location ());
5608 /* If the lookup was ambiguous, an error will already have
5610 if (ambiguous_decls
)
5611 return error_mark_node
;
5613 /* In Objective-C++, we may have an Objective-C 2.0
5614 dot-syntax for classes here. */
5615 if (c_dialect_objc ()
5616 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5617 && TREE_CODE (decl
) == TYPE_DECL
5618 && objc_is_class_name (decl
))
5621 cp_lexer_consume_token (parser
->lexer
);
5622 component
= cp_parser_identifier (parser
);
5623 if (component
== error_mark_node
)
5624 return error_mark_node
;
5626 tree result
= objc_build_class_component_ref (id_expression
,
5628 /* Build a location of the form:
5631 with caret at the start of the component name (at
5632 input_location), ranging from the start of the id_expression
5633 to the end of the component name. */
5634 location_t combined_loc
5635 = make_location (input_location
, id_expression
.get_start (),
5636 get_finish (input_location
));
5637 protected_set_expr_location (result
, combined_loc
);
5641 /* In Objective-C++, an instance variable (ivar) may be preferred
5642 to whatever cp_parser_lookup_name() found.
5643 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5644 rest of c-family, we have to do a little extra work to preserve
5645 any location information in cp_expr "decl". Given that
5646 objc_lookup_ivar is implemented in "c-family" and "objc", we
5647 have a trip through the pure "tree" type, rather than cp_expr.
5648 Naively copying it back to "decl" would implicitly give the
5649 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5650 store an EXPR_LOCATION. Hence we only update "decl" (and
5651 hence its location_t) if we get back a different tree node. */
5652 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5654 if (decl_tree
!= decl
.get_value ())
5655 decl
= cp_expr (decl_tree
);
5657 /* If name lookup gives us a SCOPE_REF, then the
5658 qualifying scope was dependent. */
5659 if (TREE_CODE (decl
) == SCOPE_REF
)
5661 /* At this point, we do not know if DECL is a valid
5662 integral constant expression. We assume that it is
5663 in fact such an expression, so that code like:
5665 template <int N> struct A {
5669 is accepted. At template-instantiation time, we
5670 will check that B<N>::i is actually a constant. */
5673 /* Check to see if DECL is a local variable in a context
5674 where that is forbidden. */
5675 if (parser
->local_variables_forbidden_p
5676 && local_variable_p (decl
))
5678 error_at (id_expression
.get_location (),
5679 "local variable %qD may not appear in this context",
5681 return error_mark_node
;
5685 decl
= (finish_id_expression
5686 (id_expression
, decl
, parser
->scope
,
5688 parser
->integral_constant_expression_p
,
5689 parser
->allow_non_integral_constant_expression_p
,
5690 &parser
->non_integral_constant_expression_p
,
5691 template_p
, done
, address_p
,
5694 id_expression
.get_location ()));
5696 cp_parser_error (parser
, error_msg
);
5697 decl
.set_location (id_expression
.get_location ());
5698 decl
.set_range (id_expr_token
->location
, id_expression
.get_finish ());
5702 /* Anything else is an error. */
5704 cp_parser_error (parser
, "expected primary-expression");
5705 return error_mark_node
;
5709 static inline cp_expr
5710 cp_parser_primary_expression (cp_parser
*parser
,
5713 bool template_arg_p
,
5716 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5717 /*decltype*/false, idk
);
5720 /* Parse an id-expression.
5727 :: [opt] nested-name-specifier template [opt] unqualified-id
5729 :: operator-function-id
5732 Return a representation of the unqualified portion of the
5733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5734 a `::' or nested-name-specifier.
5736 Often, if the id-expression was a qualified-id, the caller will
5737 want to make a SCOPE_REF to represent the qualified-id. This
5738 function does not do this in order to avoid wastefully creating
5739 SCOPE_REFs when they are not required.
5741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5745 uninstantiated templates.
5747 If *TEMPLATE_P is non-NULL, it is set to true iff the
5748 `template' keyword is used to explicitly indicate that the entity
5749 named is a template.
5751 If DECLARATOR_P is true, the id-expression is appearing as part of
5752 a declarator, rather than as part of an expression. */
5755 cp_parser_id_expression (cp_parser
*parser
,
5756 bool template_keyword_p
,
5757 bool check_dependency_p
,
5762 bool global_scope_p
;
5763 bool nested_name_specifier_p
;
5765 /* Assume the `template' keyword was not used. */
5767 *template_p
= template_keyword_p
;
5769 /* Look for the optional `::' operator. */
5771 = (!template_keyword_p
5772 && (cp_parser_global_scope_opt (parser
,
5773 /*current_scope_valid_p=*/false)
5776 /* Look for the optional nested-name-specifier. */
5777 nested_name_specifier_p
5778 = (cp_parser_nested_name_specifier_opt (parser
,
5779 /*typename_keyword_p=*/false,
5786 /* If there is a nested-name-specifier, then we are looking at
5787 the first qualified-id production. */
5788 if (nested_name_specifier_p
)
5791 tree saved_object_scope
;
5792 tree saved_qualifying_scope
;
5793 cp_expr unqualified_id
;
5796 /* See if the next token is the `template' keyword. */
5798 template_p
= &is_template
;
5799 *template_p
= cp_parser_optional_template_keyword (parser
);
5800 /* Name lookup we do during the processing of the
5801 unqualified-id might obliterate SCOPE. */
5802 saved_scope
= parser
->scope
;
5803 saved_object_scope
= parser
->object_scope
;
5804 saved_qualifying_scope
= parser
->qualifying_scope
;
5805 /* Process the final unqualified-id. */
5806 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5809 /*optional_p=*/false);
5810 /* Restore the SAVED_SCOPE for our caller. */
5811 parser
->scope
= saved_scope
;
5812 parser
->object_scope
= saved_object_scope
;
5813 parser
->qualifying_scope
= saved_qualifying_scope
;
5815 return unqualified_id
;
5817 /* Otherwise, if we are in global scope, then we are looking at one
5818 of the other qualified-id productions. */
5819 else if (global_scope_p
)
5824 /* Peek at the next token. */
5825 token
= cp_lexer_peek_token (parser
->lexer
);
5827 /* If it's an identifier, and the next token is not a "<", then
5828 we can avoid the template-id case. This is an optimization
5829 for this common case. */
5830 if (token
->type
== CPP_NAME
5831 && !cp_parser_nth_token_starts_template_argument_list_p
5833 return cp_parser_identifier (parser
);
5835 cp_parser_parse_tentatively (parser
);
5836 /* Try a template-id. */
5837 id
= cp_parser_template_id (parser
,
5838 /*template_keyword_p=*/false,
5839 /*check_dependency_p=*/true,
5842 /* If that worked, we're done. */
5843 if (cp_parser_parse_definitely (parser
))
5846 /* Peek at the next token. (Changes in the token buffer may
5847 have invalidated the pointer obtained above.) */
5848 token
= cp_lexer_peek_token (parser
->lexer
);
5850 switch (token
->type
)
5853 return cp_parser_identifier (parser
);
5856 if (token
->keyword
== RID_OPERATOR
)
5857 return cp_parser_operator_function_id (parser
);
5861 cp_parser_error (parser
, "expected id-expression");
5862 return error_mark_node
;
5866 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5867 /*check_dependency_p=*/true,
5872 /* Parse an unqualified-id.
5876 operator-function-id
5877 conversion-function-id
5881 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5882 keyword, in a construct like `A::template ...'.
5884 Returns a representation of unqualified-id. For the `identifier'
5885 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5886 production a BIT_NOT_EXPR is returned; the operand of the
5887 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5888 other productions, see the documentation accompanying the
5889 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5890 names are looked up in uninstantiated templates. If DECLARATOR_P
5891 is true, the unqualified-id is appearing as part of a declarator,
5892 rather than as part of an expression. */
5895 cp_parser_unqualified_id (cp_parser
* parser
,
5896 bool template_keyword_p
,
5897 bool check_dependency_p
,
5903 /* Peek at the next token. */
5904 token
= cp_lexer_peek_token (parser
->lexer
);
5906 switch ((int) token
->type
)
5912 /* We don't know yet whether or not this will be a
5914 cp_parser_parse_tentatively (parser
);
5915 /* Try a template-id. */
5916 id
= cp_parser_template_id (parser
, template_keyword_p
,
5920 /* If it worked, we're done. */
5921 if (cp_parser_parse_definitely (parser
))
5923 /* Otherwise, it's an ordinary identifier. */
5924 return cp_parser_identifier (parser
);
5927 case CPP_TEMPLATE_ID
:
5928 return cp_parser_template_id (parser
, template_keyword_p
,
5936 tree qualifying_scope
;
5941 /* Consume the `~' token. */
5942 cp_lexer_consume_token (parser
->lexer
);
5943 /* Parse the class-name. The standard, as written, seems to
5946 template <typename T> struct S { ~S (); };
5947 template <typename T> S<T>::~S() {}
5949 is invalid, since `~' must be followed by a class-name, but
5950 `S<T>' is dependent, and so not known to be a class.
5951 That's not right; we need to look in uninstantiated
5952 templates. A further complication arises from:
5954 template <typename T> void f(T t) {
5958 Here, it is not possible to look up `T' in the scope of `T'
5959 itself. We must look in both the current scope, and the
5960 scope of the containing complete expression.
5962 Yet another issue is:
5971 The standard does not seem to say that the `S' in `~S'
5972 should refer to the type `S' and not the data member
5975 /* DR 244 says that we look up the name after the "~" in the
5976 same scope as we looked up the qualifying name. That idea
5977 isn't fully worked out; it's more complicated than that. */
5978 scope
= parser
->scope
;
5979 object_scope
= parser
->object_scope
;
5980 qualifying_scope
= parser
->qualifying_scope
;
5982 /* Check for invalid scopes. */
5983 if (scope
== error_mark_node
)
5985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5986 cp_lexer_consume_token (parser
->lexer
);
5987 return error_mark_node
;
5989 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5991 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5992 error_at (token
->location
,
5993 "scope %qT before %<~%> is not a class-name",
5995 cp_parser_simulate_error (parser
);
5996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5997 cp_lexer_consume_token (parser
->lexer
);
5998 return error_mark_node
;
6000 gcc_assert (!scope
|| TYPE_P (scope
));
6002 /* If the name is of the form "X::~X" it's OK even if X is a
6004 token
= cp_lexer_peek_token (parser
->lexer
);
6006 && token
->type
== CPP_NAME
6007 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6009 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6010 || (CLASS_TYPE_P (scope
)
6011 && constructor_name_p (token
->u
.value
, scope
))))
6013 cp_lexer_consume_token (parser
->lexer
);
6014 return build_nt (BIT_NOT_EXPR
, scope
);
6017 /* ~auto means the destructor of whatever the object is. */
6018 if (cp_parser_is_keyword (token
, RID_AUTO
))
6020 if (cxx_dialect
< cxx14
)
6021 pedwarn (input_location
, 0,
6022 "%<~auto%> only available with "
6023 "-std=c++14 or -std=gnu++14");
6024 cp_lexer_consume_token (parser
->lexer
);
6025 return build_nt (BIT_NOT_EXPR
, make_auto ());
6028 /* If there was an explicit qualification (S::~T), first look
6029 in the scope given by the qualification (i.e., S).
6031 Note: in the calls to cp_parser_class_name below we pass
6032 typename_type so that lookup finds the injected-class-name
6033 rather than the constructor. */
6035 type_decl
= NULL_TREE
;
6038 cp_parser_parse_tentatively (parser
);
6039 type_decl
= cp_parser_class_name (parser
,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6046 if (cp_parser_parse_definitely (parser
))
6049 /* In "N::S::~S", look in "N" as well. */
6050 if (!done
&& scope
&& qualifying_scope
)
6052 cp_parser_parse_tentatively (parser
);
6053 parser
->scope
= qualifying_scope
;
6054 parser
->object_scope
= NULL_TREE
;
6055 parser
->qualifying_scope
= NULL_TREE
;
6057 = cp_parser_class_name (parser
,
6058 /*typename_keyword_p=*/false,
6059 /*template_keyword_p=*/false,
6061 /*check_dependency=*/false,
6062 /*class_head_p=*/false,
6064 if (cp_parser_parse_definitely (parser
))
6067 /* In "p->S::~T", look in the scope given by "*p" as well. */
6068 else if (!done
&& object_scope
)
6070 cp_parser_parse_tentatively (parser
);
6071 parser
->scope
= object_scope
;
6072 parser
->object_scope
= NULL_TREE
;
6073 parser
->qualifying_scope
= NULL_TREE
;
6075 = cp_parser_class_name (parser
,
6076 /*typename_keyword_p=*/false,
6077 /*template_keyword_p=*/false,
6079 /*check_dependency=*/false,
6080 /*class_head_p=*/false,
6082 if (cp_parser_parse_definitely (parser
))
6085 /* Look in the surrounding context. */
6088 parser
->scope
= NULL_TREE
;
6089 parser
->object_scope
= NULL_TREE
;
6090 parser
->qualifying_scope
= NULL_TREE
;
6091 if (processing_template_decl
)
6092 cp_parser_parse_tentatively (parser
);
6094 = cp_parser_class_name (parser
,
6095 /*typename_keyword_p=*/false,
6096 /*template_keyword_p=*/false,
6098 /*check_dependency=*/false,
6099 /*class_head_p=*/false,
6101 if (processing_template_decl
6102 && ! cp_parser_parse_definitely (parser
))
6104 /* We couldn't find a type with this name. If we're parsing
6105 tentatively, fail and try something else. */
6106 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6108 cp_parser_simulate_error (parser
);
6109 return error_mark_node
;
6111 /* Otherwise, accept it and check for a match at instantiation
6113 type_decl
= cp_parser_identifier (parser
);
6114 if (type_decl
!= error_mark_node
)
6115 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6119 /* If an error occurred, assume that the name of the
6120 destructor is the same as the name of the qualifying
6121 class. That allows us to keep parsing after running
6122 into ill-formed destructor names. */
6123 if (type_decl
== error_mark_node
&& scope
)
6124 return build_nt (BIT_NOT_EXPR
, scope
);
6125 else if (type_decl
== error_mark_node
)
6126 return error_mark_node
;
6128 /* Check that destructor name and scope match. */
6129 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6131 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6132 error_at (token
->location
,
6133 "declaration of %<~%T%> as member of %qT",
6135 cp_parser_simulate_error (parser
);
6136 return error_mark_node
;
6141 A typedef-name that names a class shall not be used as the
6142 identifier in the declarator for a destructor declaration. */
6144 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6145 && !DECL_SELF_REFERENCE_P (type_decl
)
6146 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6147 error_at (token
->location
,
6148 "typedef-name %qD used as destructor declarator",
6151 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6155 if (token
->keyword
== RID_OPERATOR
)
6159 /* This could be a template-id, so we try that first. */
6160 cp_parser_parse_tentatively (parser
);
6161 /* Try a template-id. */
6162 id
= cp_parser_template_id (parser
, template_keyword_p
,
6163 /*check_dependency_p=*/true,
6166 /* If that worked, we're done. */
6167 if (cp_parser_parse_definitely (parser
))
6169 /* We still don't know whether we're looking at an
6170 operator-function-id or a conversion-function-id. */
6171 cp_parser_parse_tentatively (parser
);
6172 /* Try an operator-function-id. */
6173 id
= cp_parser_operator_function_id (parser
);
6174 /* If that didn't work, try a conversion-function-id. */
6175 if (!cp_parser_parse_definitely (parser
))
6176 id
= cp_parser_conversion_function_id (parser
);
6185 cp_parser_error (parser
, "expected unqualified-id");
6186 return error_mark_node
;
6190 /* Parse an (optional) nested-name-specifier.
6192 nested-name-specifier: [C++98]
6193 class-or-namespace-name :: nested-name-specifier [opt]
6194 class-or-namespace-name :: template nested-name-specifier [opt]
6196 nested-name-specifier: [C++0x]
6199 nested-name-specifier identifier ::
6200 nested-name-specifier template [opt] simple-template-id ::
6202 PARSER->SCOPE should be set appropriately before this function is
6203 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6204 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6207 Sets PARSER->SCOPE to the class (TYPE) or namespace
6208 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6209 it unchanged if there is no nested-name-specifier. Returns the new
6210 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6212 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6213 part of a declaration and/or decl-specifier. */
6216 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6217 bool typename_keyword_p
,
6218 bool check_dependency_p
,
6220 bool is_declaration
,
6221 bool template_keyword_p
/* = false */)
6223 bool success
= false;
6224 cp_token_position start
= 0;
6227 /* Remember where the nested-name-specifier starts. */
6228 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6230 start
= cp_lexer_token_position (parser
->lexer
, false);
6231 push_deferring_access_checks (dk_deferred
);
6238 tree saved_qualifying_scope
;
6240 /* Spot cases that cannot be the beginning of a
6241 nested-name-specifier. */
6242 token
= cp_lexer_peek_token (parser
->lexer
);
6244 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6245 the already parsed nested-name-specifier. */
6246 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6248 /* Grab the nested-name-specifier and continue the loop. */
6249 cp_parser_pre_parsed_nested_name_specifier (parser
);
6250 /* If we originally encountered this nested-name-specifier
6251 with IS_DECLARATION set to false, we will not have
6252 resolved TYPENAME_TYPEs, so we must do so here. */
6254 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6256 new_scope
= resolve_typename_type (parser
->scope
,
6257 /*only_current_p=*/false);
6258 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6259 parser
->scope
= new_scope
;
6265 /* Spot cases that cannot be the beginning of a
6266 nested-name-specifier. On the second and subsequent times
6267 through the loop, we look for the `template' keyword. */
6268 if (success
&& token
->keyword
== RID_TEMPLATE
)
6270 /* A template-id can start a nested-name-specifier. */
6271 else if (token
->type
== CPP_TEMPLATE_ID
)
6273 /* DR 743: decltype can be used in a nested-name-specifier. */
6274 else if (token_is_decltype (token
))
6278 /* If the next token is not an identifier, then it is
6279 definitely not a type-name or namespace-name. */
6280 if (token
->type
!= CPP_NAME
)
6282 /* If the following token is neither a `<' (to begin a
6283 template-id), nor a `::', then we are not looking at a
6284 nested-name-specifier. */
6285 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6287 if (token
->type
== CPP_COLON
6288 && parser
->colon_corrects_to_scope_p
6289 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6291 gcc_rich_location
richloc (token
->location
);
6292 richloc
.add_fixit_replace ("::");
6294 "found %<:%> in nested-name-specifier, "
6296 token
->type
= CPP_SCOPE
;
6299 if (token
->type
!= CPP_SCOPE
6300 && !cp_parser_nth_token_starts_template_argument_list_p
6305 /* The nested-name-specifier is optional, so we parse
6307 cp_parser_parse_tentatively (parser
);
6309 /* Look for the optional `template' keyword, if this isn't the
6310 first time through the loop. */
6312 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6314 /* Save the old scope since the name lookup we are about to do
6315 might destroy it. */
6316 old_scope
= parser
->scope
;
6317 saved_qualifying_scope
= parser
->qualifying_scope
;
6318 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6319 look up names in "X<T>::I" in order to determine that "Y" is
6320 a template. So, if we have a typename at this point, we make
6321 an effort to look through it. */
6323 && !typename_keyword_p
6325 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6326 parser
->scope
= resolve_typename_type (parser
->scope
,
6327 /*only_current_p=*/false);
6328 /* Parse the qualifying entity. */
6330 = cp_parser_qualifying_entity (parser
,
6336 /* Look for the `::' token. */
6337 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6339 /* If we found what we wanted, we keep going; otherwise, we're
6341 if (!cp_parser_parse_definitely (parser
))
6343 bool error_p
= false;
6345 /* Restore the OLD_SCOPE since it was valid before the
6346 failed attempt at finding the last
6347 class-or-namespace-name. */
6348 parser
->scope
= old_scope
;
6349 parser
->qualifying_scope
= saved_qualifying_scope
;
6351 /* If the next token is a decltype, and the one after that is a
6352 `::', then the decltype has failed to resolve to a class or
6353 enumeration type. Give this error even when parsing
6354 tentatively since it can't possibly be valid--and we're going
6355 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6356 won't get another chance.*/
6357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6358 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6361 token
= cp_lexer_consume_token (parser
->lexer
);
6362 error_at (token
->location
, "decltype evaluates to %qT, "
6363 "which is not a class or enumeration type",
6364 token
->u
.tree_check_value
->value
);
6365 parser
->scope
= error_mark_node
;
6369 cp_lexer_consume_token (parser
->lexer
);
6372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6373 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6375 /* If we have a non-type template-id followed by ::, it can't
6376 possibly be valid. */
6377 token
= cp_lexer_peek_token (parser
->lexer
);
6378 tree tid
= token
->u
.tree_check_value
->value
;
6379 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6380 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6382 tree tmpl
= NULL_TREE
;
6383 if (is_overloaded_fn (tid
))
6385 tree fns
= get_fns (tid
);
6386 if (OVL_SINGLE_P (fns
))
6387 tmpl
= OVL_FIRST (fns
);
6388 error_at (token
->location
, "function template-id %qD "
6389 "in nested-name-specifier", tid
);
6393 /* Variable template. */
6394 tmpl
= TREE_OPERAND (tid
, 0);
6395 gcc_assert (variable_template_p (tmpl
));
6396 error_at (token
->location
, "variable template-id %qD "
6397 "in nested-name-specifier", tid
);
6400 inform (DECL_SOURCE_LOCATION (tmpl
),
6401 "%qD declared here", tmpl
);
6403 parser
->scope
= error_mark_node
;
6407 cp_lexer_consume_token (parser
->lexer
);
6408 cp_lexer_consume_token (parser
->lexer
);
6412 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6414 /* If the next token is an identifier, and the one after
6415 that is a `::', then any valid interpretation would have
6416 found a class-or-namespace-name. */
6417 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6418 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6420 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6423 token
= cp_lexer_consume_token (parser
->lexer
);
6426 if (!token
->error_reported
)
6429 tree ambiguous_decls
;
6431 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6433 /*is_template=*/false,
6434 /*is_namespace=*/false,
6435 /*check_dependency=*/true,
6438 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6439 error_at (token
->location
,
6440 "%qD used without template arguments",
6442 else if (ambiguous_decls
)
6444 // cp_parser_lookup_name has the same diagnostic,
6445 // thus make sure to emit it at most once.
6446 if (cp_parser_uncommitted_to_tentative_parse_p
6449 error_at (token
->location
,
6450 "reference to %qD is ambiguous",
6452 print_candidates (ambiguous_decls
);
6454 decl
= error_mark_node
;
6458 if (cxx_dialect
!= cxx98
)
6459 cp_parser_name_lookup_error
6460 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6463 cp_parser_name_lookup_error
6464 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6468 parser
->scope
= error_mark_node
;
6470 /* Treat this as a successful nested-name-specifier
6475 If the name found is not a class-name (clause
6476 _class_) or namespace-name (_namespace.def_), the
6477 program is ill-formed. */
6480 cp_lexer_consume_token (parser
->lexer
);
6484 /* We've found one valid nested-name-specifier. */
6486 /* Name lookup always gives us a DECL. */
6487 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6488 new_scope
= TREE_TYPE (new_scope
);
6489 /* Uses of "template" must be followed by actual templates. */
6490 if (template_keyword_p
6491 && !(CLASS_TYPE_P (new_scope
)
6492 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6493 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6494 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6495 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6496 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6497 == TEMPLATE_ID_EXPR
)))
6498 permerror (input_location
, TYPE_P (new_scope
)
6499 ? G_("%qT is not a template")
6500 : G_("%qD is not a template"),
6502 /* If it is a class scope, try to complete it; we are about to
6503 be looking up names inside the class. */
6504 if (TYPE_P (new_scope
)
6505 /* Since checking types for dependency can be expensive,
6506 avoid doing it if the type is already complete. */
6507 && !COMPLETE_TYPE_P (new_scope
)
6508 /* Do not try to complete dependent types. */
6509 && !dependent_type_p (new_scope
))
6511 new_scope
= complete_type (new_scope
);
6512 /* If it is a typedef to current class, use the current
6513 class instead, as the typedef won't have any names inside
6515 if (!COMPLETE_TYPE_P (new_scope
)
6516 && currently_open_class (new_scope
))
6517 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6519 /* Make sure we look in the right scope the next time through
6521 parser
->scope
= new_scope
;
6524 /* If parsing tentatively, replace the sequence of tokens that makes
6525 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6526 token. That way, should we re-parse the token stream, we will
6527 not have to repeat the effort required to do the parse, nor will
6528 we issue duplicate error messages. */
6529 if (success
&& start
)
6533 token
= cp_lexer_token_at (parser
->lexer
, start
);
6534 /* Reset the contents of the START token. */
6535 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6536 /* Retrieve any deferred checks. Do not pop this access checks yet
6537 so the memory will not be reclaimed during token replacing below. */
6538 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6539 token
->u
.tree_check_value
->value
= parser
->scope
;
6540 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6541 token
->u
.tree_check_value
->qualifying_scope
=
6542 parser
->qualifying_scope
;
6543 token
->keyword
= RID_MAX
;
6545 /* Purge all subsequent tokens. */
6546 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6550 pop_to_parent_deferring_access_checks ();
6552 return success
? parser
->scope
: NULL_TREE
;
6555 /* Parse a nested-name-specifier. See
6556 cp_parser_nested_name_specifier_opt for details. This function
6557 behaves identically, except that it will an issue an error if no
6558 nested-name-specifier is present. */
6561 cp_parser_nested_name_specifier (cp_parser
*parser
,
6562 bool typename_keyword_p
,
6563 bool check_dependency_p
,
6565 bool is_declaration
)
6569 /* Look for the nested-name-specifier. */
6570 scope
= cp_parser_nested_name_specifier_opt (parser
,
6575 /* If it was not present, issue an error message. */
6578 cp_parser_error (parser
, "expected nested-name-specifier");
6579 parser
->scope
= NULL_TREE
;
6585 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6586 this is either a class-name or a namespace-name (which corresponds
6587 to the class-or-namespace-name production in the grammar). For
6588 C++0x, it can also be a type-name that refers to an enumeration
6589 type or a simple-template-id.
6591 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6592 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6593 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6594 TYPE_P is TRUE iff the next name should be taken as a class-name,
6595 even the same name is declared to be another entity in the same
6598 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6599 specified by the class-or-namespace-name. If neither is found the
6600 ERROR_MARK_NODE is returned. */
6603 cp_parser_qualifying_entity (cp_parser
*parser
,
6604 bool typename_keyword_p
,
6605 bool template_keyword_p
,
6606 bool check_dependency_p
,
6608 bool is_declaration
)
6611 tree saved_qualifying_scope
;
6612 tree saved_object_scope
;
6615 bool successful_parse_p
;
6617 /* DR 743: decltype can appear in a nested-name-specifier. */
6618 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6620 scope
= cp_parser_decltype (parser
);
6621 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6622 && !MAYBE_CLASS_TYPE_P (scope
))
6624 cp_parser_simulate_error (parser
);
6625 return error_mark_node
;
6627 if (TYPE_NAME (scope
))
6628 scope
= TYPE_NAME (scope
);
6632 /* Before we try to parse the class-name, we must save away the
6633 current PARSER->SCOPE since cp_parser_class_name will destroy
6635 saved_scope
= parser
->scope
;
6636 saved_qualifying_scope
= parser
->qualifying_scope
;
6637 saved_object_scope
= parser
->object_scope
;
6638 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6639 there is no need to look for a namespace-name. */
6640 only_class_p
= template_keyword_p
6641 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6643 cp_parser_parse_tentatively (parser
);
6644 scope
= cp_parser_class_name (parser
,
6647 type_p
? class_type
: none_type
,
6649 /*class_head_p=*/false,
6651 /*enum_ok=*/cxx_dialect
> cxx98
);
6652 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6653 /* If that didn't work, try for a namespace-name. */
6654 if (!only_class_p
&& !successful_parse_p
)
6656 /* Restore the saved scope. */
6657 parser
->scope
= saved_scope
;
6658 parser
->qualifying_scope
= saved_qualifying_scope
;
6659 parser
->object_scope
= saved_object_scope
;
6660 /* If we are not looking at an identifier followed by the scope
6661 resolution operator, then this is not part of a
6662 nested-name-specifier. (Note that this function is only used
6663 to parse the components of a nested-name-specifier.) */
6664 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6665 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6666 return error_mark_node
;
6667 scope
= cp_parser_namespace_name (parser
);
6673 /* Return true if we are looking at a compound-literal, false otherwise. */
6676 cp_parser_compound_literal_p (cp_parser
*parser
)
6678 cp_lexer_save_tokens (parser
->lexer
);
6680 /* Skip tokens until the next token is a closing parenthesis.
6681 If we find the closing `)', and the next token is a `{', then
6682 we are looking at a compound-literal. */
6683 bool compound_literal_p
6684 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6685 /*consume_paren=*/true)
6686 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6688 /* Roll back the tokens we skipped. */
6689 cp_lexer_rollback_tokens (parser
->lexer
);
6691 return compound_literal_p
;
6694 /* Return true if EXPR is the integer constant zero or a complex constant
6695 of zero, without any folding, but ignoring location wrappers. */
6698 literal_integer_zerop (const_tree expr
)
6700 return (location_wrapper_p (expr
)
6701 && integer_zerop (TREE_OPERAND (expr
, 0)));
6704 /* Parse a postfix-expression.
6708 postfix-expression [ expression ]
6709 postfix-expression ( expression-list [opt] )
6710 simple-type-specifier ( expression-list [opt] )
6711 typename :: [opt] nested-name-specifier identifier
6712 ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier template [opt] template-id
6714 ( expression-list [opt] )
6715 postfix-expression . template [opt] id-expression
6716 postfix-expression -> template [opt] id-expression
6717 postfix-expression . pseudo-destructor-name
6718 postfix-expression -> pseudo-destructor-name
6719 postfix-expression ++
6720 postfix-expression --
6721 dynamic_cast < type-id > ( expression )
6722 static_cast < type-id > ( expression )
6723 reinterpret_cast < type-id > ( expression )
6724 const_cast < type-id > ( expression )
6725 typeid ( expression )
6731 ( type-id ) { initializer-list , [opt] }
6733 This extension is a GNU version of the C99 compound-literal
6734 construct. (The C99 grammar uses `type-name' instead of `type-id',
6735 but they are essentially the same concept.)
6737 If ADDRESS_P is true, the postfix expression is the operand of the
6738 `&' operator. CAST_P is true if this expression is the target of a
6741 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6742 class member access expressions [expr.ref].
6744 Returns a representation of the expression. */
6747 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6748 bool member_access_only_p
, bool decltype_p
,
6749 cp_id_kind
* pidk_return
)
6754 cp_id_kind idk
= CP_ID_KIND_NONE
;
6755 cp_expr postfix_expression
= NULL_TREE
;
6756 bool is_member_access
= false;
6758 /* Peek at the next token. */
6759 token
= cp_lexer_peek_token (parser
->lexer
);
6760 loc
= token
->location
;
6761 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6763 /* Some of the productions are determined by keywords. */
6764 keyword
= token
->keyword
;
6774 const char *saved_message
;
6775 bool saved_in_type_id_in_expr_p
;
6777 /* All of these can be handled in the same way from the point
6778 of view of parsing. Begin by consuming the token
6779 identifying the cast. */
6780 cp_lexer_consume_token (parser
->lexer
);
6782 /* New types cannot be defined in the cast. */
6783 saved_message
= parser
->type_definition_forbidden_message
;
6784 parser
->type_definition_forbidden_message
6785 = G_("types may not be defined in casts");
6787 /* Look for the opening `<'. */
6788 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6789 /* Parse the type to which we are casting. */
6790 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6791 parser
->in_type_id_in_expr_p
= true;
6792 type
= cp_parser_type_id (parser
);
6793 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6794 /* Look for the closing `>'. */
6795 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6796 /* Restore the old message. */
6797 parser
->type_definition_forbidden_message
= saved_message
;
6799 bool saved_greater_than_is_operator_p
6800 = parser
->greater_than_is_operator_p
;
6801 parser
->greater_than_is_operator_p
= true;
6803 /* And the expression which is being cast. */
6804 matching_parens parens
;
6805 parens
.require_open (parser
);
6806 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6807 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6809 location_t end_loc
= close_paren
?
6810 close_paren
->location
: UNKNOWN_LOCATION
;
6812 parser
->greater_than_is_operator_p
6813 = saved_greater_than_is_operator_p
;
6815 /* Only type conversions to integral or enumeration types
6816 can be used in constant-expressions. */
6817 if (!cast_valid_in_integral_constant_expression_p (type
)
6818 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6820 postfix_expression
= error_mark_node
;
6828 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6832 = build_static_cast (type
, expression
, tf_warning_or_error
);
6836 = build_reinterpret_cast (type
, expression
,
6837 tf_warning_or_error
);
6841 = build_const_cast (type
, expression
, tf_warning_or_error
);
6847 /* Construct a location e.g. :
6848 reinterpret_cast <int *> (expr)
6849 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6850 ranging from the start of the "*_cast" token to the final closing
6851 paren, with the caret at the start. */
6852 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6853 postfix_expression
.set_location (cp_cast_loc
);
6860 const char *saved_message
;
6861 bool saved_in_type_id_in_expr_p
;
6863 /* Consume the `typeid' token. */
6864 cp_lexer_consume_token (parser
->lexer
);
6865 /* Look for the `(' token. */
6866 matching_parens parens
;
6867 parens
.require_open (parser
);
6868 /* Types cannot be defined in a `typeid' expression. */
6869 saved_message
= parser
->type_definition_forbidden_message
;
6870 parser
->type_definition_forbidden_message
6871 = G_("types may not be defined in a %<typeid%> expression");
6872 /* We can't be sure yet whether we're looking at a type-id or an
6874 cp_parser_parse_tentatively (parser
);
6875 /* Try a type-id first. */
6876 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6877 parser
->in_type_id_in_expr_p
= true;
6878 type
= cp_parser_type_id (parser
);
6879 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6880 /* Look for the `)' token. Otherwise, we can't be sure that
6881 we're not looking at an expression: consider `typeid (int
6882 (3))', for example. */
6883 cp_token
*close_paren
= parens
.require_close (parser
);
6884 /* If all went well, simply lookup the type-id. */
6885 if (cp_parser_parse_definitely (parser
))
6886 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6887 /* Otherwise, fall back to the expression variant. */
6892 /* Look for an expression. */
6893 expression
= cp_parser_expression (parser
, & idk
);
6894 /* Compute its typeid. */
6895 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6896 /* Look for the `)' token. */
6897 close_paren
= parens
.require_close (parser
);
6899 /* Restore the saved message. */
6900 parser
->type_definition_forbidden_message
= saved_message
;
6901 /* `typeid' may not appear in an integral constant expression. */
6902 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6903 postfix_expression
= error_mark_node
;
6905 /* Construct a location e.g. :
6908 ranging from the start of the "typeid" token to the final closing
6909 paren, with the caret at the start. */
6912 location_t typeid_loc
6913 = make_location (start_loc
, start_loc
, close_paren
->location
);
6914 postfix_expression
.set_location (typeid_loc
);
6915 postfix_expression
.maybe_add_location_wrapper ();
6923 /* The syntax permitted here is the same permitted for an
6924 elaborated-type-specifier. */
6925 ++parser
->prevent_constrained_type_specifiers
;
6926 type
= cp_parser_elaborated_type_specifier (parser
,
6927 /*is_friend=*/false,
6928 /*is_declaration=*/false);
6929 --parser
->prevent_constrained_type_specifiers
;
6930 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6935 case RID_BUILTIN_SHUFFLE
:
6936 case RID_BUILTIN_LAUNDER
:
6938 vec
<tree
, va_gc
> *vec
;
6942 cp_lexer_consume_token (parser
->lexer
);
6943 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6944 /*cast_p=*/false, /*allow_expansion_p=*/true,
6945 /*non_constant_p=*/NULL
);
6948 postfix_expression
= error_mark_node
;
6952 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6958 if (vec
->length () == 1)
6960 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6963 error_at (loc
, "wrong number of arguments to "
6964 "%<__builtin_addressof%>");
6965 postfix_expression
= error_mark_node
;
6969 case RID_BUILTIN_LAUNDER
:
6970 if (vec
->length () == 1)
6971 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6972 tf_warning_or_error
);
6975 error_at (loc
, "wrong number of arguments to "
6976 "%<__builtin_launder%>");
6977 postfix_expression
= error_mark_node
;
6981 case RID_BUILTIN_SHUFFLE
:
6982 if (vec
->length () == 2)
6984 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6985 (*vec
)[1], tf_warning_or_error
);
6986 else if (vec
->length () == 3)
6988 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6989 (*vec
)[2], tf_warning_or_error
);
6992 error_at (loc
, "wrong number of arguments to "
6993 "%<__builtin_shuffle%>");
6994 postfix_expression
= error_mark_node
;
7008 /* If the next thing is a simple-type-specifier, we may be
7009 looking at a functional cast. We could also be looking at
7010 an id-expression. So, we try the functional cast, and if
7011 that doesn't work we fall back to the primary-expression. */
7012 cp_parser_parse_tentatively (parser
);
7013 /* Look for the simple-type-specifier. */
7014 ++parser
->prevent_constrained_type_specifiers
;
7015 type
= cp_parser_simple_type_specifier (parser
,
7016 /*decl_specs=*/NULL
,
7017 CP_PARSER_FLAGS_NONE
);
7018 --parser
->prevent_constrained_type_specifiers
;
7019 /* Parse the cast itself. */
7020 if (!cp_parser_error_occurred (parser
))
7022 = cp_parser_functional_cast (parser
, type
);
7023 /* If that worked, we're done. */
7024 if (cp_parser_parse_definitely (parser
))
7027 /* If the functional-cast didn't work out, try a
7028 compound-literal. */
7029 if (cp_parser_allow_gnu_extensions_p (parser
)
7030 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7032 cp_expr initializer
= NULL_TREE
;
7034 cp_parser_parse_tentatively (parser
);
7036 matching_parens parens
;
7037 parens
.consume_open (parser
);
7039 /* Avoid calling cp_parser_type_id pointlessly, see comment
7040 in cp_parser_cast_expression about c++/29234. */
7041 if (!cp_parser_compound_literal_p (parser
))
7042 cp_parser_simulate_error (parser
);
7045 /* Parse the type. */
7046 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7047 parser
->in_type_id_in_expr_p
= true;
7048 type
= cp_parser_type_id (parser
);
7049 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7050 parens
.require_close (parser
);
7053 /* If things aren't going well, there's no need to
7055 if (!cp_parser_error_occurred (parser
))
7057 bool non_constant_p
;
7058 /* Parse the brace-enclosed initializer list. */
7059 initializer
= cp_parser_braced_list (parser
,
7062 /* If that worked, we're definitely looking at a
7063 compound-literal expression. */
7064 if (cp_parser_parse_definitely (parser
))
7066 /* Warn the user that a compound literal is not
7067 allowed in standard C++. */
7068 pedwarn (input_location
, OPT_Wpedantic
,
7069 "ISO C++ forbids compound-literals");
7070 /* For simplicity, we disallow compound literals in
7071 constant-expressions. We could
7072 allow compound literals of integer type, whose
7073 initializer was a constant, in constant
7074 expressions. Permitting that usage, as a further
7075 extension, would not change the meaning of any
7076 currently accepted programs. (Of course, as
7077 compound literals are not part of ISO C++, the
7078 standard has nothing to say.) */
7079 if (cp_parser_non_integral_constant_expression (parser
,
7082 postfix_expression
= error_mark_node
;
7085 /* Form the representation of the compound-literal. */
7087 = finish_compound_literal (type
, initializer
,
7088 tf_warning_or_error
, fcl_c99
);
7089 postfix_expression
.set_location (initializer
.get_location ());
7094 /* It must be a primary-expression. */
7096 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7097 /*template_arg_p=*/false,
7104 /* Note that we don't need to worry about calling build_cplus_new on a
7105 class-valued CALL_EXPR in decltype when it isn't the end of the
7106 postfix-expression; unary_complex_lvalue will take care of that for
7109 /* Keep looping until the postfix-expression is complete. */
7112 if (idk
== CP_ID_KIND_UNQUALIFIED
7113 && identifier_p (postfix_expression
)
7114 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7115 /* It is not a Koenig lookup function call. */
7117 = unqualified_name_lookup_error (postfix_expression
);
7119 /* Peek at the next token. */
7120 token
= cp_lexer_peek_token (parser
->lexer
);
7122 switch (token
->type
)
7124 case CPP_OPEN_SQUARE
:
7125 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7127 cp_parser_error (parser
,
7128 "two consecutive %<[%> shall "
7129 "only introduce an attribute");
7130 return error_mark_node
;
7133 = cp_parser_postfix_open_square_expression (parser
,
7137 postfix_expression
.set_range (start_loc
,
7138 postfix_expression
.get_location ());
7140 idk
= CP_ID_KIND_NONE
;
7141 is_member_access
= false;
7144 case CPP_OPEN_PAREN
:
7145 /* postfix-expression ( expression-list [opt] ) */
7148 bool is_builtin_constant_p
;
7149 bool saved_integral_constant_expression_p
= false;
7150 bool saved_non_integral_constant_expression_p
= false;
7151 tsubst_flags_t complain
= complain_flags (decltype_p
);
7152 vec
<tree
, va_gc
> *args
;
7153 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7155 is_member_access
= false;
7157 is_builtin_constant_p
7158 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7159 if (is_builtin_constant_p
)
7161 /* The whole point of __builtin_constant_p is to allow
7162 non-constant expressions to appear as arguments. */
7163 saved_integral_constant_expression_p
7164 = parser
->integral_constant_expression_p
;
7165 saved_non_integral_constant_expression_p
7166 = parser
->non_integral_constant_expression_p
;
7167 parser
->integral_constant_expression_p
= false;
7169 args
= (cp_parser_parenthesized_expression_list
7171 /*cast_p=*/false, /*allow_expansion_p=*/true,
7172 /*non_constant_p=*/NULL
,
7173 /*close_paren_loc=*/&close_paren_loc
,
7174 /*wrap_locations_p=*/true));
7175 if (is_builtin_constant_p
)
7177 parser
->integral_constant_expression_p
7178 = saved_integral_constant_expression_p
;
7179 parser
->non_integral_constant_expression_p
7180 = saved_non_integral_constant_expression_p
;
7185 postfix_expression
= error_mark_node
;
7189 /* Function calls are not permitted in
7190 constant-expressions. */
7191 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7192 && cp_parser_non_integral_constant_expression (parser
,
7195 postfix_expression
= error_mark_node
;
7196 release_tree_vector (args
);
7201 if (idk
== CP_ID_KIND_UNQUALIFIED
7202 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7204 if (identifier_p (postfix_expression
)
7205 /* In C++2A, we may need to perform ADL for a template
7207 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7208 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7210 if (!args
->is_empty ())
7213 if (!any_type_dependent_arguments_p (args
))
7215 = perform_koenig_lookup (postfix_expression
, args
,
7220 = unqualified_fn_lookup_error (postfix_expression
);
7222 /* We do not perform argument-dependent lookup if
7223 normal lookup finds a non-function, in accordance
7224 with the expected resolution of DR 218. */
7225 else if (!args
->is_empty ()
7226 && is_overloaded_fn (postfix_expression
))
7228 tree fn
= get_first_fn (postfix_expression
);
7229 fn
= STRIP_TEMPLATE (fn
);
7231 /* Do not do argument dependent lookup if regular
7232 lookup finds a member function or a block-scope
7233 function declaration. [basic.lookup.argdep]/3 */
7234 if (!DECL_FUNCTION_MEMBER_P (fn
)
7235 && !DECL_LOCAL_FUNCTION_P (fn
))
7238 if (!any_type_dependent_arguments_p (args
))
7240 = perform_koenig_lookup (postfix_expression
, args
,
7246 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7248 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7249 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7251 if (processing_template_decl
7252 && (type_dependent_object_expression_p (instance
)
7253 || (!BASELINK_P (fn
)
7254 && TREE_CODE (fn
) != FIELD_DECL
)
7255 || type_dependent_expression_p (fn
)
7256 || any_type_dependent_arguments_p (args
)))
7258 maybe_generic_this_capture (instance
, fn
);
7260 = build_min_nt_call_vec (postfix_expression
, args
);
7261 release_tree_vector (args
);
7265 if (BASELINK_P (fn
))
7268 = (build_new_method_call
7269 (instance
, fn
, &args
, NULL_TREE
,
7270 (idk
== CP_ID_KIND_QUALIFIED
7271 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7278 = finish_call_expr (postfix_expression
, &args
,
7279 /*disallow_virtual=*/false,
7283 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7284 || TREE_CODE (postfix_expression
) == MEMBER_REF
7285 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7286 postfix_expression
= (build_offset_ref_call_from_tree
7287 (postfix_expression
, &args
,
7289 else if (idk
== CP_ID_KIND_QUALIFIED
)
7290 /* A call to a static class member, or a namespace-scope
7293 = finish_call_expr (postfix_expression
, &args
,
7294 /*disallow_virtual=*/true,
7298 /* All other function calls. */
7300 = finish_call_expr (postfix_expression
, &args
,
7301 /*disallow_virtual=*/false,
7305 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7307 location_t combined_loc
= make_location (token
->location
,
7310 postfix_expression
.set_location (combined_loc
);
7313 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7314 idk
= CP_ID_KIND_NONE
;
7316 release_tree_vector (args
);
7322 /* postfix-expression . template [opt] id-expression
7323 postfix-expression . pseudo-destructor-name
7324 postfix-expression -> template [opt] id-expression
7325 postfix-expression -> pseudo-destructor-name */
7327 /* Consume the `.' or `->' operator. */
7328 cp_lexer_consume_token (parser
->lexer
);
7331 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7335 is_member_access
= true;
7339 /* postfix-expression ++ */
7340 /* Consume the `++' token. */
7341 cp_lexer_consume_token (parser
->lexer
);
7342 /* Generate a representation for the complete expression. */
7344 = finish_increment_expr (postfix_expression
,
7345 POSTINCREMENT_EXPR
);
7346 /* Increments may not appear in constant-expressions. */
7347 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7348 postfix_expression
= error_mark_node
;
7349 idk
= CP_ID_KIND_NONE
;
7350 is_member_access
= false;
7353 case CPP_MINUS_MINUS
:
7354 /* postfix-expression -- */
7355 /* Consume the `--' token. */
7356 cp_lexer_consume_token (parser
->lexer
);
7357 /* Generate a representation for the complete expression. */
7359 = finish_increment_expr (postfix_expression
,
7360 POSTDECREMENT_EXPR
);
7361 /* Decrements may not appear in constant-expressions. */
7362 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7363 postfix_expression
= error_mark_node
;
7364 idk
= CP_ID_KIND_NONE
;
7365 is_member_access
= false;
7369 if (pidk_return
!= NULL
)
7370 * pidk_return
= idk
;
7371 if (member_access_only_p
)
7372 return is_member_access
7373 ? postfix_expression
7374 : cp_expr (error_mark_node
);
7376 return postfix_expression
;
7380 /* We should never get here. */
7382 return error_mark_node
;
7385 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7386 by cp_parser_builtin_offsetof. We're looking for
7388 postfix-expression [ expression ]
7389 postfix-expression [ braced-init-list ] (C++11)
7391 FOR_OFFSETOF is set if we're being called in that context, which
7392 changes how we deal with integer constant expressions. */
7395 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7396 tree postfix_expression
,
7400 tree index
= NULL_TREE
;
7401 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7402 bool saved_greater_than_is_operator_p
;
7404 /* Consume the `[' token. */
7405 cp_lexer_consume_token (parser
->lexer
);
7407 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7408 parser
->greater_than_is_operator_p
= true;
7410 /* Parse the index expression. */
7411 /* ??? For offsetof, there is a question of what to allow here. If
7412 offsetof is not being used in an integral constant expression context,
7413 then we *could* get the right answer by computing the value at runtime.
7414 If we are in an integral constant expression context, then we might
7415 could accept any constant expression; hard to say without analysis.
7416 Rather than open the barn door too wide right away, allow only integer
7417 constant expressions here. */
7419 index
= cp_parser_constant_expression (parser
);
7422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7424 bool expr_nonconst_p
;
7425 cp_lexer_set_source_position (parser
->lexer
);
7426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7427 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7430 index
= cp_parser_expression (parser
);
7433 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7435 /* Look for the closing `]'. */
7436 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7438 /* Build the ARRAY_REF. */
7439 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7442 /* When not doing offsetof, array references are not permitted in
7443 constant-expressions. */
7445 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7446 postfix_expression
= error_mark_node
;
7448 return postfix_expression
;
7451 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7452 dereference of incomplete type, returns true if error_mark_node should
7453 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7454 and *DEPENDENT_P. */
7457 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7460 /* In a template, be permissive by treating an object expression
7461 of incomplete type as dependent (after a pedwarn). */
7462 diagnostic_t kind
= (processing_template_decl
7463 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7465 switch (TREE_CODE (*postfix_expression
))
7468 case REINTERPRET_CAST_EXPR
:
7469 case CONST_CAST_EXPR
:
7470 case STATIC_CAST_EXPR
:
7471 case DYNAMIC_CAST_EXPR
:
7472 case IMPLICIT_CONV_EXPR
:
7473 case VIEW_CONVERT_EXPR
:
7474 case NON_LVALUE_EXPR
:
7478 /* Don't emit any diagnostic for OVERLOADs. */
7482 /* Avoid clobbering e.g. DECLs. */
7483 if (!EXPR_P (*postfix_expression
))
7488 if (kind
== DK_IGNORED
)
7491 location_t exploc
= location_of (*postfix_expression
);
7492 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7493 if (!MAYBE_CLASS_TYPE_P (*scope
))
7495 if (kind
== DK_ERROR
)
7496 *scope
= *postfix_expression
= error_mark_node
;
7497 else if (processing_template_decl
)
7499 *dependent_p
= true;
7500 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7505 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7506 by cp_parser_builtin_offsetof. We're looking for
7508 postfix-expression . template [opt] id-expression
7509 postfix-expression . pseudo-destructor-name
7510 postfix-expression -> template [opt] id-expression
7511 postfix-expression -> pseudo-destructor-name
7513 FOR_OFFSETOF is set if we're being called in that context. That sorta
7514 limits what of the above we'll actually accept, but nevermind.
7515 TOKEN_TYPE is the "." or "->" token, which will already have been
7516 removed from the stream. */
7519 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7520 enum cpp_ttype token_type
,
7521 cp_expr postfix_expression
,
7522 bool for_offsetof
, cp_id_kind
*idk
,
7523 location_t location
)
7527 bool pseudo_destructor_p
;
7528 tree scope
= NULL_TREE
;
7529 location_t start_loc
= postfix_expression
.get_start ();
7531 /* If this is a `->' operator, dereference the pointer. */
7532 if (token_type
== CPP_DEREF
)
7533 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7534 tf_warning_or_error
);
7535 /* Check to see whether or not the expression is type-dependent and
7536 not the current instantiation. */
7537 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7538 /* The identifier following the `->' or `.' is not qualified. */
7539 parser
->scope
= NULL_TREE
;
7540 parser
->qualifying_scope
= NULL_TREE
;
7541 parser
->object_scope
= NULL_TREE
;
7542 *idk
= CP_ID_KIND_NONE
;
7544 /* Enter the scope corresponding to the type of the object
7545 given by the POSTFIX_EXPRESSION. */
7548 scope
= TREE_TYPE (postfix_expression
);
7549 /* According to the standard, no expression should ever have
7550 reference type. Unfortunately, we do not currently match
7551 the standard in this respect in that our internal representation
7552 of an expression may have reference type even when the standard
7553 says it does not. Therefore, we have to manually obtain the
7554 underlying type here. */
7555 scope
= non_reference (scope
);
7556 /* The type of the POSTFIX_EXPRESSION must be complete. */
7557 /* Unlike the object expression in other contexts, *this is not
7558 required to be of complete type for purposes of class member
7559 access (5.2.5) outside the member function body. */
7560 if (postfix_expression
!= current_class_ref
7561 && scope
!= error_mark_node
7562 && !currently_open_class (scope
))
7564 scope
= complete_type (scope
);
7565 if (!COMPLETE_TYPE_P (scope
)
7566 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7568 return error_mark_node
;
7573 /* Let the name lookup machinery know that we are processing a
7574 class member access expression. */
7575 parser
->context
->object_type
= scope
;
7576 /* If something went wrong, we want to be able to discern that case,
7577 as opposed to the case where there was no SCOPE due to the type
7578 of expression being dependent. */
7580 scope
= error_mark_node
;
7581 /* If the SCOPE was erroneous, make the various semantic analysis
7582 functions exit quickly -- and without issuing additional error
7584 if (scope
== error_mark_node
)
7585 postfix_expression
= error_mark_node
;
7590 /* Tell cp_parser_lookup_name that there was an object, even though it's
7592 parser
->context
->object_type
= unknown_type_node
;
7594 /* Assume this expression is not a pseudo-destructor access. */
7595 pseudo_destructor_p
= false;
7597 /* If the SCOPE is a scalar type, then, if this is a valid program,
7598 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7599 is type dependent, it can be pseudo-destructor-name or something else.
7600 Try to parse it as pseudo-destructor-name first. */
7601 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7606 cp_parser_parse_tentatively (parser
);
7607 /* Parse the pseudo-destructor-name. */
7609 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7612 && (cp_parser_error_occurred (parser
)
7613 || !SCALAR_TYPE_P (type
)))
7614 cp_parser_abort_tentative_parse (parser
);
7615 else if (cp_parser_parse_definitely (parser
))
7617 pseudo_destructor_p
= true;
7619 = finish_pseudo_destructor_expr (postfix_expression
,
7624 if (!pseudo_destructor_p
)
7626 /* If the SCOPE is not a scalar type, we are looking at an
7627 ordinary class member access expression, rather than a
7628 pseudo-destructor-name. */
7630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7631 /* Parse the id-expression. */
7632 name
= (cp_parser_id_expression
7634 cp_parser_optional_template_keyword (parser
),
7635 /*check_dependency_p=*/true,
7637 /*declarator_p=*/false,
7638 /*optional_p=*/false));
7639 /* In general, build a SCOPE_REF if the member name is qualified.
7640 However, if the name was not dependent and has already been
7641 resolved; there is no need to build the SCOPE_REF. For example;
7643 struct X { void f(); };
7644 template <typename T> void f(T* t) { t->X::f(); }
7646 Even though "t" is dependent, "X::f" is not and has been resolved
7647 to a BASELINK; there is no need to include scope information. */
7649 /* But we do need to remember that there was an explicit scope for
7650 virtual function calls. */
7652 *idk
= CP_ID_KIND_QUALIFIED
;
7654 /* If the name is a template-id that names a type, we will get a
7655 TYPE_DECL here. That is invalid code. */
7656 if (TREE_CODE (name
) == TYPE_DECL
)
7658 error_at (token
->location
, "invalid use of %qD", name
);
7659 postfix_expression
= error_mark_node
;
7663 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7665 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7667 error_at (token
->location
, "%<%D::%D%> is not a class member",
7668 parser
->scope
, name
);
7669 postfix_expression
= error_mark_node
;
7672 name
= build_qualified_name (/*type=*/NULL_TREE
,
7676 parser
->scope
= NULL_TREE
;
7677 parser
->qualifying_scope
= NULL_TREE
;
7678 parser
->object_scope
= NULL_TREE
;
7680 if (parser
->scope
&& name
&& BASELINK_P (name
))
7681 adjust_result_of_qualified_name_lookup
7682 (name
, parser
->scope
, scope
);
7684 = finish_class_member_access_expr (postfix_expression
, name
,
7686 tf_warning_or_error
);
7687 /* Build a location e.g.:
7690 where the caret is at the deref token, ranging from
7691 the start of postfix_expression to the end of the access expr. */
7693 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7694 location_t combined_loc
7695 = make_location (input_location
, start_loc
, end_loc
);
7696 protected_set_expr_location (postfix_expression
, combined_loc
);
7700 /* We no longer need to look up names in the scope of the object on
7701 the left-hand side of the `.' or `->' operator. */
7702 parser
->context
->object_type
= NULL_TREE
;
7704 /* Outside of offsetof, these operators may not appear in
7705 constant-expressions. */
7707 && (cp_parser_non_integral_constant_expression
7708 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7709 postfix_expression
= error_mark_node
;
7711 return postfix_expression
;
7714 /* Parse a parenthesized expression-list.
7717 assignment-expression
7718 expression-list, assignment-expression
7723 identifier, expression-list
7725 CAST_P is true if this expression is the target of a cast.
7727 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7730 WRAP_LOCATIONS_P is true if expressions within this list for which
7731 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7732 their source locations.
7734 Returns a vector of trees. Each element is a representation of an
7735 assignment-expression. NULL is returned if the ( and or ) are
7736 missing. An empty, but allocated, vector is returned on no
7737 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7738 if we are parsing an attribute list for an attribute that wants a
7739 plain identifier argument, normal_attr for an attribute that wants
7740 an expression, or non_attr if we aren't parsing an attribute list. If
7741 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7742 not all of the expressions in the list were constant.
7743 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7744 will be written to with the location of the closing parenthesis. If
7745 an error occurs, it may or may not be written to. */
7747 static vec
<tree
, va_gc
> *
7748 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7749 int is_attribute_list
,
7751 bool allow_expansion_p
,
7752 bool *non_constant_p
,
7753 location_t
*close_paren_loc
,
7754 bool wrap_locations_p
)
7756 vec
<tree
, va_gc
> *expression_list
;
7757 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7758 tree identifier
= NULL_TREE
;
7759 bool saved_greater_than_is_operator_p
;
7761 /* Assume all the expressions will be constant. */
7763 *non_constant_p
= false;
7765 matching_parens parens
;
7766 if (!parens
.require_open (parser
))
7769 expression_list
= make_tree_vector ();
7771 /* Within a parenthesized expression, a `>' token is always
7772 the greater-than operator. */
7773 saved_greater_than_is_operator_p
7774 = parser
->greater_than_is_operator_p
;
7775 parser
->greater_than_is_operator_p
= true;
7777 cp_expr
expr (NULL_TREE
);
7779 /* Consume expressions until there are no more. */
7780 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7783 /* At the beginning of attribute lists, check to see if the
7784 next token is an identifier. */
7785 if (is_attribute_list
== id_attr
7786 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7790 /* Consume the identifier. */
7791 token
= cp_lexer_consume_token (parser
->lexer
);
7792 /* Save the identifier. */
7793 identifier
= token
->u
.value
;
7797 bool expr_non_constant_p
;
7799 /* Parse the next assignment-expression. */
7800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7802 /* A braced-init-list. */
7803 cp_lexer_set_source_position (parser
->lexer
);
7804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7805 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7806 if (non_constant_p
&& expr_non_constant_p
)
7807 *non_constant_p
= true;
7809 else if (non_constant_p
)
7811 expr
= (cp_parser_constant_expression
7812 (parser
, /*allow_non_constant_p=*/true,
7813 &expr_non_constant_p
));
7814 if (expr_non_constant_p
)
7815 *non_constant_p
= true;
7818 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7822 expr
= instantiate_non_dependent_expr (expr
);
7824 /* If we have an ellipsis, then this is an expression
7826 if (allow_expansion_p
7827 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7829 /* Consume the `...'. */
7830 cp_lexer_consume_token (parser
->lexer
);
7832 /* Build the argument pack. */
7833 expr
= make_pack_expansion (expr
);
7836 if (wrap_locations_p
)
7837 expr
.maybe_add_location_wrapper ();
7839 /* Add it to the list. We add error_mark_node
7840 expressions to the list, so that we can still tell if
7841 the correct form for a parenthesized expression-list
7842 is found. That gives better errors. */
7843 vec_safe_push (expression_list
, expr
.get_value ());
7845 if (expr
== error_mark_node
)
7849 /* After the first item, attribute lists look the same as
7850 expression lists. */
7851 is_attribute_list
= non_attr
;
7854 /* If the next token isn't a `,', then we are done. */
7855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7858 /* Otherwise, consume the `,' and keep going. */
7859 cp_lexer_consume_token (parser
->lexer
);
7862 if (close_paren_loc
)
7863 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7865 if (!parens
.require_close (parser
))
7870 /* We try and resync to an unnested comma, as that will give the
7871 user better diagnostics. */
7872 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7873 /*recovering=*/true,
7875 /*consume_paren=*/true);
7880 parser
->greater_than_is_operator_p
7881 = saved_greater_than_is_operator_p
;
7886 parser
->greater_than_is_operator_p
7887 = saved_greater_than_is_operator_p
;
7890 vec_safe_insert (expression_list
, 0, identifier
);
7892 return expression_list
;
7895 /* Parse a pseudo-destructor-name.
7897 pseudo-destructor-name:
7898 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7899 :: [opt] nested-name-specifier template template-id :: ~ type-name
7900 :: [opt] nested-name-specifier [opt] ~ type-name
7902 If either of the first two productions is used, sets *SCOPE to the
7903 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7904 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7905 or ERROR_MARK_NODE if the parse fails. */
7908 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7913 bool nested_name_specifier_p
;
7916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7917 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7918 && !type_dependent_expression_p (object
))
7920 if (cxx_dialect
< cxx14
)
7921 pedwarn (input_location
, 0,
7922 "%<~auto%> only available with "
7923 "-std=c++14 or -std=gnu++14");
7924 cp_lexer_consume_token (parser
->lexer
);
7925 cp_lexer_consume_token (parser
->lexer
);
7927 *type
= TREE_TYPE (object
);
7931 /* Assume that things will not work out. */
7932 *type
= error_mark_node
;
7934 /* Look for the optional `::' operator. */
7935 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7936 /* Look for the optional nested-name-specifier. */
7937 nested_name_specifier_p
7938 = (cp_parser_nested_name_specifier_opt (parser
,
7939 /*typename_keyword_p=*/false,
7940 /*check_dependency_p=*/true,
7942 /*is_declaration=*/false)
7944 /* Now, if we saw a nested-name-specifier, we might be doing the
7945 second production. */
7946 if (nested_name_specifier_p
7947 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7949 /* Consume the `template' keyword. */
7950 cp_lexer_consume_token (parser
->lexer
);
7951 /* Parse the template-id. */
7952 cp_parser_template_id (parser
,
7953 /*template_keyword_p=*/true,
7954 /*check_dependency_p=*/false,
7956 /*is_declaration=*/true);
7957 /* Look for the `::' token. */
7958 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7960 /* If the next token is not a `~', then there might be some
7961 additional qualification. */
7962 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7964 /* At this point, we're looking for "type-name :: ~". The type-name
7965 must not be a class-name, since this is a pseudo-destructor. So,
7966 it must be either an enum-name, or a typedef-name -- both of which
7967 are just identifiers. So, we peek ahead to check that the "::"
7968 and "~" tokens are present; if they are not, then we can avoid
7969 calling type_name. */
7970 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7971 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7972 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7974 cp_parser_error (parser
, "non-scalar type");
7978 /* Look for the type-name. */
7979 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7980 if (*scope
== error_mark_node
)
7983 /* Look for the `::' token. */
7984 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7989 /* Look for the `~'. */
7990 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7992 /* Once we see the ~, this has to be a pseudo-destructor. */
7993 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7994 cp_parser_commit_to_topmost_tentative_parse (parser
);
7996 /* Look for the type-name again. We are not responsible for
7997 checking that it matches the first type-name. */
7998 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8001 /* Parse a unary-expression.
8007 unary-operator cast-expression
8008 sizeof unary-expression
8010 alignof ( type-id ) [C++0x]
8017 __extension__ cast-expression
8018 __alignof__ unary-expression
8019 __alignof__ ( type-id )
8020 alignof unary-expression [C++0x]
8021 __real__ cast-expression
8022 __imag__ cast-expression
8024 sizeof ( type-id ) { initializer-list , [opt] }
8025 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8026 __alignof__ ( type-id ) { initializer-list , [opt] }
8028 ADDRESS_P is true iff the unary-expression is appearing as the
8029 operand of the `&' operator. CAST_P is true if this expression is
8030 the target of a cast.
8032 Returns a representation of the expression. */
8035 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8036 bool address_p
, bool cast_p
, bool decltype_p
)
8039 enum tree_code unary_operator
;
8041 /* Peek at the next token. */
8042 token
= cp_lexer_peek_token (parser
->lexer
);
8043 /* Some keywords give away the kind of expression. */
8044 if (token
->type
== CPP_KEYWORD
)
8046 enum rid keyword
= token
->keyword
;
8055 location_t start_loc
= token
->location
;
8057 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8058 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8060 /* Consume the token. */
8061 cp_lexer_consume_token (parser
->lexer
);
8062 /* Parse the operand. */
8063 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8065 if (TYPE_P (operand
))
8066 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8074 pedwarn (token
->location
, OPT_Wpedantic
,
8075 "ISO C++ does not allow %<alignof%> "
8078 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8084 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8086 if (!processing_template_decl
&& TYPE_P (operand
))
8088 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8089 build1 (NOP_EXPR
, operand
,
8091 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8094 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8095 TREE_SIDE_EFFECTS (ret
) = 0;
8096 TREE_READONLY (ret
) = 1;
8100 /* Construct a location e.g. :
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser
->lexer
)->location
;
8107 location_t compound_loc
8108 = make_location (start_loc
, start_loc
, finish_loc
);
8110 cp_expr
ret_expr (ret
);
8111 ret_expr
.set_location (compound_loc
);
8112 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8116 case RID_BUILTIN_HAS_ATTRIBUTE
:
8117 return cp_parser_has_attribute_expression (parser
);
8120 return cp_parser_new_expression (parser
);
8123 return cp_parser_delete_expression (parser
);
8127 /* The saved value of the PEDANTIC flag. */
8131 /* Save away the PEDANTIC flag. */
8132 cp_parser_extension_opt (parser
, &saved_pedantic
);
8133 /* Parse the cast-expression. */
8134 expr
= cp_parser_simple_cast_expression (parser
);
8135 /* Restore the PEDANTIC flag. */
8136 pedantic
= saved_pedantic
;
8146 /* Consume the `__real__' or `__imag__' token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 /* Parse the cast-expression. */
8149 expression
= cp_parser_simple_cast_expression (parser
);
8150 /* Create the complete representation. */
8151 return build_x_unary_op (token
->location
,
8152 (keyword
== RID_REALPART
8153 ? REALPART_EXPR
: IMAGPART_EXPR
),
8155 tf_warning_or_error
);
8159 case RID_TRANSACTION_ATOMIC
:
8160 case RID_TRANSACTION_RELAXED
:
8161 return cp_parser_transaction_expression (parser
, keyword
);
8166 const char *saved_message
;
8167 bool saved_integral_constant_expression_p
;
8168 bool saved_non_integral_constant_expression_p
;
8169 bool saved_greater_than_is_operator_p
;
8171 location_t start_loc
= token
->location
;
8173 cp_lexer_consume_token (parser
->lexer
);
8174 matching_parens parens
;
8175 parens
.require_open (parser
);
8177 saved_message
= parser
->type_definition_forbidden_message
;
8178 parser
->type_definition_forbidden_message
8179 = G_("types may not be defined in %<noexcept%> expressions");
8181 saved_integral_constant_expression_p
8182 = parser
->integral_constant_expression_p
;
8183 saved_non_integral_constant_expression_p
8184 = parser
->non_integral_constant_expression_p
;
8185 parser
->integral_constant_expression_p
= false;
8187 saved_greater_than_is_operator_p
8188 = parser
->greater_than_is_operator_p
;
8189 parser
->greater_than_is_operator_p
= true;
8191 ++cp_unevaluated_operand
;
8192 ++c_inhibit_evaluation_warnings
;
8193 ++cp_noexcept_operand
;
8194 expr
= cp_parser_expression (parser
);
8195 --cp_noexcept_operand
;
8196 --c_inhibit_evaluation_warnings
;
8197 --cp_unevaluated_operand
;
8199 parser
->greater_than_is_operator_p
8200 = saved_greater_than_is_operator_p
;
8202 parser
->integral_constant_expression_p
8203 = saved_integral_constant_expression_p
;
8204 parser
->non_integral_constant_expression_p
8205 = saved_non_integral_constant_expression_p
;
8207 parser
->type_definition_forbidden_message
= saved_message
;
8209 location_t finish_loc
8210 = cp_lexer_peek_token (parser
->lexer
)->location
;
8211 parens
.require_close (parser
);
8213 /* Construct a location of the form:
8216 with start == caret, finishing at the close-paren. */
8217 location_t noexcept_loc
8218 = make_location (start_loc
, start_loc
, finish_loc
);
8220 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8229 /* Look for the `:: new' and `:: delete', which also signal the
8230 beginning of a new-expression, or delete-expression,
8231 respectively. If the next token is `::', then it might be one of
8233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8237 /* See if the token after the `::' is one of the keywords in
8238 which we're interested. */
8239 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8240 /* If it's `new', we have a new-expression. */
8241 if (keyword
== RID_NEW
)
8242 return cp_parser_new_expression (parser
);
8243 /* Similarly, for `delete'. */
8244 else if (keyword
== RID_DELETE
)
8245 return cp_parser_delete_expression (parser
);
8248 /* Look for a unary operator. */
8249 unary_operator
= cp_parser_unary_operator (token
);
8250 /* The `++' and `--' operators can be handled similarly, even though
8251 they are not technically unary-operators in the grammar. */
8252 if (unary_operator
== ERROR_MARK
)
8254 if (token
->type
== CPP_PLUS_PLUS
)
8255 unary_operator
= PREINCREMENT_EXPR
;
8256 else if (token
->type
== CPP_MINUS_MINUS
)
8257 unary_operator
= PREDECREMENT_EXPR
;
8258 /* Handle the GNU address-of-label extension. */
8259 else if (cp_parser_allow_gnu_extensions_p (parser
)
8260 && token
->type
== CPP_AND_AND
)
8264 location_t start_loc
= token
->location
;
8266 /* Consume the '&&' token. */
8267 cp_lexer_consume_token (parser
->lexer
);
8268 /* Look for the identifier. */
8269 location_t finish_loc
8270 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8271 identifier
= cp_parser_identifier (parser
);
8272 /* Construct a location of the form:
8275 with caret==start at the "&&", finish at the end of the label. */
8276 location_t combined_loc
8277 = make_location (start_loc
, start_loc
, finish_loc
);
8278 /* Create an expression representing the address. */
8279 expression
= finish_label_address_expr (identifier
, combined_loc
);
8280 if (cp_parser_non_integral_constant_expression (parser
,
8282 expression
= error_mark_node
;
8286 if (unary_operator
!= ERROR_MARK
)
8288 cp_expr cast_expression
;
8289 cp_expr expression
= error_mark_node
;
8290 non_integral_constant non_constant_p
= NIC_NONE
;
8291 location_t loc
= token
->location
;
8292 tsubst_flags_t complain
= complain_flags (decltype_p
);
8294 /* Consume the operator token. */
8295 token
= cp_lexer_consume_token (parser
->lexer
);
8296 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8298 /* Parse the cast-expression. */
8300 = cp_parser_cast_expression (parser
,
8301 unary_operator
== ADDR_EXPR
,
8307 OP_TOKEN CAST_EXPRESSION
8308 ^~~~~~~~~~~~~~~~~~~~~~~~~
8309 with start==caret at the operator token, and
8310 extending to the end of the cast_expression. */
8311 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8313 /* Now, build an appropriate representation. */
8314 switch (unary_operator
)
8317 non_constant_p
= NIC_STAR
;
8318 expression
= build_x_indirect_ref (loc
, cast_expression
,
8321 /* TODO: build_x_indirect_ref does not always honor the
8322 location, so ensure it is set. */
8323 expression
.set_location (loc
);
8327 non_constant_p
= NIC_ADDR
;
8330 expression
= build_x_unary_op (loc
, unary_operator
,
8333 /* TODO: build_x_unary_op does not always honor the location,
8334 so ensure it is set. */
8335 expression
.set_location (loc
);
8338 case PREINCREMENT_EXPR
:
8339 case PREDECREMENT_EXPR
:
8340 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8341 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8344 /* Immediately fold negation of a constant, unless the constant is 0
8345 (since -0 == 0) or it would overflow. */
8346 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8347 && CONSTANT_CLASS_P (cast_expression
)
8348 && !integer_zerop (cast_expression
)
8349 && !TREE_OVERFLOW (cast_expression
))
8351 tree folded
= fold_build1 (unary_operator
,
8352 TREE_TYPE (cast_expression
),
8354 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8356 expression
= cp_expr (folded
, loc
);
8361 case UNARY_PLUS_EXPR
:
8362 case TRUTH_NOT_EXPR
:
8363 expression
= finish_unary_op_expr (loc
, unary_operator
,
8364 cast_expression
, complain
);
8371 if (non_constant_p
!= NIC_NONE
8372 && cp_parser_non_integral_constant_expression (parser
,
8374 expression
= error_mark_node
;
8379 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8380 /*member_access_only_p=*/false,
8385 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8386 unary-operator, the corresponding tree code is returned. */
8388 static enum tree_code
8389 cp_parser_unary_operator (cp_token
* token
)
8391 switch (token
->type
)
8394 return INDIRECT_REF
;
8400 return UNARY_PLUS_EXPR
;
8406 return TRUTH_NOT_EXPR
;
8409 return BIT_NOT_EXPR
;
8416 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8417 Returns a representation of the expression. */
8420 cp_parser_has_attribute_expression (cp_parser
*parser
)
8422 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8424 /* Consume the __builtin_has_attribute token. */
8425 cp_lexer_consume_token (parser
->lexer
);
8427 matching_parens parens
;
8428 if (!parens
.require_open (parser
))
8429 return error_mark_node
;
8431 /* Types cannot be defined in a `sizeof' expression. Save away the
8433 const char *saved_message
= parser
->type_definition_forbidden_message
;
8434 /* And create the new one. */
8435 const int kwd
= RID_BUILTIN_HAS_ATTRIBUTE
;
8436 char *tmp
= concat ("types may not be defined in %<",
8437 IDENTIFIER_POINTER (ridpointers
[kwd
]),
8438 "%> expressions", NULL
);
8439 parser
->type_definition_forbidden_message
= tmp
;
8441 /* The restrictions on constant-expressions do not apply inside
8442 sizeof expressions. */
8443 bool saved_integral_constant_expression_p
8444 = parser
->integral_constant_expression_p
;
8445 bool saved_non_integral_constant_expression_p
8446 = parser
->non_integral_constant_expression_p
;
8447 parser
->integral_constant_expression_p
= false;
8449 /* Do not actually evaluate the expression. */
8450 ++cp_unevaluated_operand
;
8451 ++c_inhibit_evaluation_warnings
;
8453 tree oper
= NULL_TREE
;
8455 /* We can't be sure yet whether we're looking at a type-id or an
8457 cp_parser_parse_tentatively (parser
);
8459 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8460 parser
->in_type_id_in_expr_p
= true;
8461 /* Look for the type-id. */
8462 oper
= cp_parser_type_id (parser
);
8463 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8465 cp_parser_parse_definitely (parser
);
8467 /* If the type-id production did not work out, then we must be
8468 looking at the unary-expression production. */
8469 if (!oper
|| oper
== error_mark_node
)
8470 oper
= cp_parser_unary_expression (parser
);
8472 /* Go back to evaluating expressions. */
8473 --cp_unevaluated_operand
;
8474 --c_inhibit_evaluation_warnings
;
8476 /* Free the message we created. */
8478 /* And restore the old one. */
8479 parser
->type_definition_forbidden_message
= saved_message
;
8480 parser
->integral_constant_expression_p
8481 = saved_integral_constant_expression_p
;
8482 parser
->non_integral_constant_expression_p
8483 = saved_non_integral_constant_expression_p
;
8485 /* Consume the comma if it's there. */
8486 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8488 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8489 /*consume_paren=*/true);
8490 return error_mark_node
;
8493 /* Parse the attribute specification. */
8495 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8496 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8498 if (oper
!= error_mark_node
)
8500 /* Fold constant expressions used in attributes first. */
8501 cp_check_const_attributes (attr
);
8503 /* Finally, see if OPER has been declared with ATTR. */
8504 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8507 parens
.require_close (parser
);
8511 error_at (atloc
, "expected identifier");
8512 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8515 /* Construct a location e.g. :
8516 __builtin_has_attribute (oper, attr)
8517 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8518 with start == caret at the start of the built-in token,
8519 and with the endpoint at the final closing paren. */
8520 location_t finish_loc
8521 = cp_lexer_previous_token (parser
->lexer
)->location
;
8522 location_t compound_loc
8523 = make_location (start_loc
, start_loc
, finish_loc
);
8525 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8526 ret_expr
.set_location (compound_loc
);
8527 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8531 /* Parse a new-expression.
8534 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8535 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8537 Returns a representation of the expression. */
8540 cp_parser_new_expression (cp_parser
* parser
)
8542 bool global_scope_p
;
8543 vec
<tree
, va_gc
> *placement
;
8545 vec
<tree
, va_gc
> *initializer
;
8546 tree nelts
= NULL_TREE
;
8549 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8551 /* Look for the optional `::' operator. */
8553 = (cp_parser_global_scope_opt (parser
,
8554 /*current_scope_valid_p=*/false)
8556 /* Look for the `new' operator. */
8557 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8558 /* There's no easy way to tell a new-placement from the
8559 `( type-id )' construct. */
8560 cp_parser_parse_tentatively (parser
);
8561 /* Look for a new-placement. */
8562 placement
= cp_parser_new_placement (parser
);
8563 /* If that didn't work out, there's no new-placement. */
8564 if (!cp_parser_parse_definitely (parser
))
8566 if (placement
!= NULL
)
8567 release_tree_vector (placement
);
8571 /* If the next token is a `(', then we have a parenthesized
8573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8576 const char *saved_message
= parser
->type_definition_forbidden_message
;
8578 /* Consume the `('. */
8579 matching_parens parens
;
8580 parens
.consume_open (parser
);
8582 /* Parse the type-id. */
8583 parser
->type_definition_forbidden_message
8584 = G_("types may not be defined in a new-expression");
8586 type_id_in_expr_sentinel
s (parser
);
8587 type
= cp_parser_type_id (parser
);
8589 parser
->type_definition_forbidden_message
= saved_message
;
8591 /* Look for the closing `)'. */
8592 parens
.require_close (parser
);
8593 token
= cp_lexer_peek_token (parser
->lexer
);
8594 /* There should not be a direct-new-declarator in this production,
8595 but GCC used to allowed this, so we check and emit a sensible error
8596 message for this case. */
8597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8599 error_at (token
->location
,
8600 "array bound forbidden after parenthesized type-id");
8601 inform (token
->location
,
8602 "try removing the parentheses around the type-id");
8603 cp_parser_direct_new_declarator (parser
);
8606 /* Otherwise, there must be a new-type-id. */
8608 type
= cp_parser_new_type_id (parser
, &nelts
);
8610 /* If the next token is a `(' or '{', then we have a new-initializer. */
8611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8612 if (token
->type
== CPP_OPEN_PAREN
8613 || token
->type
== CPP_OPEN_BRACE
)
8614 initializer
= cp_parser_new_initializer (parser
);
8618 /* A new-expression may not appear in an integral constant
8620 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8621 ret
= error_mark_node
;
8622 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8623 of a new-type-id or type-id of a new-expression, the new-expression shall
8624 contain a new-initializer of the form ( assignment-expression )".
8625 Additionally, consistently with the spirit of DR 1467, we want to accept
8626 'new auto { 2 }' too. */
8627 else if ((ret
= type_uses_auto (type
))
8628 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8629 && (vec_safe_length (initializer
) != 1
8630 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8631 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8633 error_at (token
->location
,
8634 "initialization of new-expression for type %<auto%> "
8635 "requires exactly one element");
8636 ret
= error_mark_node
;
8640 /* Construct a location e.g.:
8643 with caret == start at the start of the "new" token, and the end
8644 at the end of the final token we consumed. */
8645 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8646 location_t end_loc
= get_finish (end_tok
->location
);
8647 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8649 /* Create a representation of the new-expression. */
8650 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8651 tf_warning_or_error
);
8652 protected_set_expr_location (ret
, combined_loc
);
8655 if (placement
!= NULL
)
8656 release_tree_vector (placement
);
8657 if (initializer
!= NULL
)
8658 release_tree_vector (initializer
);
8663 /* Parse a new-placement.
8668 Returns the same representation as for an expression-list. */
8670 static vec
<tree
, va_gc
> *
8671 cp_parser_new_placement (cp_parser
* parser
)
8673 vec
<tree
, va_gc
> *expression_list
;
8675 /* Parse the expression-list. */
8676 expression_list
= (cp_parser_parenthesized_expression_list
8677 (parser
, non_attr
, /*cast_p=*/false,
8678 /*allow_expansion_p=*/true,
8679 /*non_constant_p=*/NULL
));
8681 if (expression_list
&& expression_list
->is_empty ())
8682 error ("expected expression-list or type-id");
8684 return expression_list
;
8687 /* Parse a new-type-id.
8690 type-specifier-seq new-declarator [opt]
8692 Returns the TYPE allocated. If the new-type-id indicates an array
8693 type, *NELTS is set to the number of elements in the last array
8694 bound; the TYPE will not include the last array bound. */
8697 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8699 cp_decl_specifier_seq type_specifier_seq
;
8700 cp_declarator
*new_declarator
;
8701 cp_declarator
*declarator
;
8702 cp_declarator
*outer_declarator
;
8703 const char *saved_message
;
8705 /* The type-specifier sequence must not contain type definitions.
8706 (It cannot contain declarations of new types either, but if they
8707 are not definitions we will catch that because they are not
8709 saved_message
= parser
->type_definition_forbidden_message
;
8710 parser
->type_definition_forbidden_message
8711 = G_("types may not be defined in a new-type-id");
8712 /* Parse the type-specifier-seq. */
8713 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8714 /*is_trailing_return=*/false,
8715 &type_specifier_seq
);
8716 /* Restore the old message. */
8717 parser
->type_definition_forbidden_message
= saved_message
;
8719 if (type_specifier_seq
.type
== error_mark_node
)
8720 return error_mark_node
;
8722 /* Parse the new-declarator. */
8723 new_declarator
= cp_parser_new_declarator_opt (parser
);
8725 /* Determine the number of elements in the last array dimension, if
8728 /* Skip down to the last array dimension. */
8729 declarator
= new_declarator
;
8730 outer_declarator
= NULL
;
8731 while (declarator
&& (declarator
->kind
== cdk_pointer
8732 || declarator
->kind
== cdk_ptrmem
))
8734 outer_declarator
= declarator
;
8735 declarator
= declarator
->declarator
;
8738 && declarator
->kind
== cdk_array
8739 && declarator
->declarator
8740 && declarator
->declarator
->kind
== cdk_array
)
8742 outer_declarator
= declarator
;
8743 declarator
= declarator
->declarator
;
8746 if (declarator
&& declarator
->kind
== cdk_array
)
8748 *nelts
= declarator
->u
.array
.bounds
;
8749 if (*nelts
== error_mark_node
)
8750 *nelts
= integer_one_node
;
8752 if (outer_declarator
)
8753 outer_declarator
->declarator
= declarator
->declarator
;
8755 new_declarator
= NULL
;
8758 return groktypename (&type_specifier_seq
, new_declarator
, false);
8761 /* Parse an (optional) new-declarator.
8764 ptr-operator new-declarator [opt]
8765 direct-new-declarator
8767 Returns the declarator. */
8769 static cp_declarator
*
8770 cp_parser_new_declarator_opt (cp_parser
* parser
)
8772 enum tree_code code
;
8773 tree type
, std_attributes
= NULL_TREE
;
8774 cp_cv_quals cv_quals
;
8776 /* We don't know if there's a ptr-operator next, or not. */
8777 cp_parser_parse_tentatively (parser
);
8778 /* Look for a ptr-operator. */
8779 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8780 /* If that worked, look for more new-declarators. */
8781 if (cp_parser_parse_definitely (parser
))
8783 cp_declarator
*declarator
;
8785 /* Parse another optional declarator. */
8786 declarator
= cp_parser_new_declarator_opt (parser
);
8788 declarator
= cp_parser_make_indirect_declarator
8789 (code
, type
, cv_quals
, declarator
, std_attributes
);
8794 /* If the next token is a `[', there is a direct-new-declarator. */
8795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8796 return cp_parser_direct_new_declarator (parser
);
8801 /* Parse a direct-new-declarator.
8803 direct-new-declarator:
8805 direct-new-declarator [constant-expression]
8809 static cp_declarator
*
8810 cp_parser_direct_new_declarator (cp_parser
* parser
)
8812 cp_declarator
*declarator
= NULL
;
8819 /* Look for the opening `['. */
8820 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8822 token
= cp_lexer_peek_token (parser
->lexer
);
8823 expression
= cp_parser_expression (parser
);
8824 /* The standard requires that the expression have integral
8825 type. DR 74 adds enumeration types. We believe that the
8826 real intent is that these expressions be handled like the
8827 expression in a `switch' condition, which also allows
8828 classes with a single conversion to integral or
8829 enumeration type. */
8830 if (!processing_template_decl
)
8833 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8838 error_at (token
->location
,
8839 "expression in new-declarator must have integral "
8840 "or enumeration type");
8841 expression
= error_mark_node
;
8845 /* Look for the closing `]'. */
8846 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8848 /* Add this bound to the declarator. */
8849 declarator
= make_array_declarator (declarator
, expression
);
8851 /* If the next token is not a `[', then there are no more
8853 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8860 /* Parse a new-initializer.
8863 ( expression-list [opt] )
8866 Returns a representation of the expression-list. */
8868 static vec
<tree
, va_gc
> *
8869 cp_parser_new_initializer (cp_parser
* parser
)
8871 vec
<tree
, va_gc
> *expression_list
;
8873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8876 bool expr_non_constant_p
;
8877 cp_lexer_set_source_position (parser
->lexer
);
8878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8879 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8880 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8881 expression_list
= make_tree_vector_single (t
);
8884 expression_list
= (cp_parser_parenthesized_expression_list
8885 (parser
, non_attr
, /*cast_p=*/false,
8886 /*allow_expansion_p=*/true,
8887 /*non_constant_p=*/NULL
));
8889 return expression_list
;
8892 /* Parse a delete-expression.
8895 :: [opt] delete cast-expression
8896 :: [opt] delete [ ] cast-expression
8898 Returns a representation of the expression. */
8901 cp_parser_delete_expression (cp_parser
* parser
)
8903 bool global_scope_p
;
8907 /* Look for the optional `::' operator. */
8909 = (cp_parser_global_scope_opt (parser
,
8910 /*current_scope_valid_p=*/false)
8912 /* Look for the `delete' keyword. */
8913 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8914 /* See if the array syntax is in use. */
8915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8917 /* Consume the `[' token. */
8918 cp_lexer_consume_token (parser
->lexer
);
8919 /* Look for the `]' token. */
8920 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8921 /* Remember that this is the `[]' construct. */
8927 /* Parse the cast-expression. */
8928 expression
= cp_parser_simple_cast_expression (parser
);
8930 /* A delete-expression may not appear in an integral constant
8932 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8933 return error_mark_node
;
8935 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8936 tf_warning_or_error
);
8939 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8940 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8944 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8946 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8947 switch (token
->type
)
8953 case CPP_CLOSE_SQUARE
:
8954 case CPP_CLOSE_PAREN
:
8955 case CPP_CLOSE_BRACE
:
8956 case CPP_OPEN_BRACE
:
8960 case CPP_DEREF_STAR
:
8968 case CPP_GREATER_EQ
:
8989 case CPP_OPEN_PAREN
:
8990 /* In ((type ()) () the last () isn't a valid cast-expression,
8991 so the whole must be parsed as postfix-expression. */
8992 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8995 case CPP_OPEN_SQUARE
:
8996 /* '[' may start a primary-expression in obj-c++ and in C++11,
8997 as a lambda-expression, eg, '(void)[]{}'. */
8998 if (cxx_dialect
>= cxx11
)
9000 return c_dialect_objc ();
9003 case CPP_MINUS_MINUS
:
9004 /* '++' and '--' may or may not start a cast-expression:
9006 struct T { void operator++(int); };
9007 void f() { (T())++; }
9020 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9021 in the order: const_cast, static_cast, reinterpret_cast.
9023 Don't suggest dynamic_cast.
9025 Return the first legal cast kind found, or NULL otherwise. */
9028 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9032 /* Reuse the parser logic by attempting to build the various kinds of
9033 cast, with "complain" disabled.
9034 Identify the first such cast that is valid. */
9036 /* Don't attempt to run such logic within template processing. */
9037 if (processing_template_decl
)
9040 /* First try const_cast. */
9041 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9042 if (trial
!= error_mark_node
)
9043 return "const_cast";
9045 /* If that fails, try static_cast. */
9046 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9047 if (trial
!= error_mark_node
)
9048 return "static_cast";
9050 /* Finally, try reinterpret_cast. */
9051 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9052 if (trial
!= error_mark_node
)
9053 return "reinterpret_cast";
9055 /* No such cast possible. */
9059 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9060 suggesting how to convert a C-style cast of the form:
9064 to a C++-style cast.
9066 The primary range of RICHLOC is asssumed to be that of the original
9067 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9068 of the parens in the C-style cast. */
9071 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9072 location_t close_paren_loc
, tree orig_expr
,
9075 /* This function is non-trivial, so bail out now if the warning isn't
9076 going to be emitted. */
9077 if (!warn_old_style_cast
)
9080 /* Try to find a legal C++ cast, trying them in order:
9081 const_cast, static_cast, reinterpret_cast. */
9082 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9083 if (!cast_suggestion
)
9086 /* Replace the open paren with "CAST_SUGGESTION<". */
9088 pp_printf (&pp
, "%s<", cast_suggestion
);
9089 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9091 /* Replace the close paren with "> (". */
9092 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9094 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9095 rich_loc
->add_fixit_insert_after (")");
9099 /* Parse a cast-expression.
9103 ( type-id ) cast-expression
9105 ADDRESS_P is true iff the unary-expression is appearing as the
9106 operand of the `&' operator. CAST_P is true if this expression is
9107 the target of a cast.
9109 Returns a representation of the expression. */
9112 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9113 bool decltype_p
, cp_id_kind
* pidk
)
9115 /* If it's a `(', then we might be looking at a cast. */
9116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9118 tree type
= NULL_TREE
;
9119 cp_expr
expr (NULL_TREE
);
9120 int cast_expression
= 0;
9121 const char *saved_message
;
9123 /* There's no way to know yet whether or not this is a cast.
9124 For example, `(int (3))' is a unary-expression, while `(int)
9125 3' is a cast. So, we resort to parsing tentatively. */
9126 cp_parser_parse_tentatively (parser
);
9127 /* Types may not be defined in a cast. */
9128 saved_message
= parser
->type_definition_forbidden_message
;
9129 parser
->type_definition_forbidden_message
9130 = G_("types may not be defined in casts");
9131 /* Consume the `('. */
9132 matching_parens parens
;
9133 cp_token
*open_paren
= parens
.consume_open (parser
);
9134 location_t open_paren_loc
= open_paren
->location
;
9135 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9137 /* A very tricky bit is that `(struct S) { 3 }' is a
9138 compound-literal (which we permit in C++ as an extension).
9139 But, that construct is not a cast-expression -- it is a
9140 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9141 is legal; if the compound-literal were a cast-expression,
9142 you'd need an extra set of parentheses.) But, if we parse
9143 the type-id, and it happens to be a class-specifier, then we
9144 will commit to the parse at that point, because we cannot
9145 undo the action that is done when creating a new class. So,
9146 then we cannot back up and do a postfix-expression.
9148 Another tricky case is the following (c++/29234):
9150 struct S { void operator () (); };
9157 As a type-id we parse the parenthesized S()() as a function
9158 returning a function, groktypename complains and we cannot
9159 back up in this case either.
9161 Therefore, we scan ahead to the closing `)', and check to see
9162 if the tokens after the `)' can start a cast-expression. Otherwise
9163 we are dealing with an unary-expression, a postfix-expression
9166 Yet another tricky case, in C++11, is the following (c++/54891):
9170 The issue is that usually, besides the case of lambda-expressions,
9171 the parenthesized type-id cannot be followed by '[', and, eg, we
9172 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9173 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9174 we don't commit, we try a cast-expression, then an unary-expression.
9176 Save tokens so that we can put them back. */
9177 cp_lexer_save_tokens (parser
->lexer
);
9179 /* We may be looking at a cast-expression. */
9180 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9181 /*consume_paren=*/true))
9183 = cp_parser_tokens_start_cast_expression (parser
);
9185 /* Roll back the tokens we skipped. */
9186 cp_lexer_rollback_tokens (parser
->lexer
);
9187 /* If we aren't looking at a cast-expression, simulate an error so
9188 that the call to cp_parser_error_occurred below returns true. */
9189 if (!cast_expression
)
9190 cp_parser_simulate_error (parser
);
9193 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9194 parser
->in_type_id_in_expr_p
= true;
9195 /* Look for the type-id. */
9196 type
= cp_parser_type_id (parser
);
9197 /* Look for the closing `)'. */
9198 cp_token
*close_paren
= parens
.require_close (parser
);
9200 close_paren_loc
= close_paren
->location
;
9201 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9204 /* Restore the saved message. */
9205 parser
->type_definition_forbidden_message
= saved_message
;
9207 /* At this point this can only be either a cast or a
9208 parenthesized ctor such as `(T ())' that looks like a cast to
9209 function returning T. */
9210 if (!cp_parser_error_occurred (parser
))
9212 /* Only commit if the cast-expression doesn't start with
9213 '++', '--', or '[' in C++11. */
9214 if (cast_expression
> 0)
9215 cp_parser_commit_to_topmost_tentative_parse (parser
);
9217 expr
= cp_parser_cast_expression (parser
,
9218 /*address_p=*/false,
9220 /*decltype_p=*/false,
9223 if (cp_parser_parse_definitely (parser
))
9225 /* Warn about old-style casts, if so requested. */
9226 if (warn_old_style_cast
9227 && !in_system_header_at (input_location
)
9228 && !VOID_TYPE_P (type
)
9229 && current_lang_name
!= lang_name_c
)
9231 gcc_rich_location
rich_loc (input_location
);
9232 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9234 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9235 "use of old-style cast to %q#T", type
);
9238 /* Only type conversions to integral or enumeration types
9239 can be used in constant-expressions. */
9240 if (!cast_valid_in_integral_constant_expression_p (type
)
9241 && cp_parser_non_integral_constant_expression (parser
,
9243 return error_mark_node
;
9245 /* Perform the cast. */
9249 with start==caret at the open paren, extending to the
9251 location_t cast_loc
= make_location (open_paren_loc
,
9253 expr
.get_finish ());
9254 expr
= build_c_cast (cast_loc
, type
, expr
);
9259 cp_parser_abort_tentative_parse (parser
);
9262 /* If we get here, then it's not a cast, so it must be a
9263 unary-expression. */
9264 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9265 cast_p
, decltype_p
);
9268 /* Parse a binary expression of the general form:
9272 pm-expression .* cast-expression
9273 pm-expression ->* cast-expression
9275 multiplicative-expression:
9277 multiplicative-expression * pm-expression
9278 multiplicative-expression / pm-expression
9279 multiplicative-expression % pm-expression
9281 additive-expression:
9282 multiplicative-expression
9283 additive-expression + multiplicative-expression
9284 additive-expression - multiplicative-expression
9288 shift-expression << additive-expression
9289 shift-expression >> additive-expression
9291 relational-expression:
9293 relational-expression < shift-expression
9294 relational-expression > shift-expression
9295 relational-expression <= shift-expression
9296 relational-expression >= shift-expression
9300 relational-expression:
9301 relational-expression <? shift-expression
9302 relational-expression >? shift-expression
9304 equality-expression:
9305 relational-expression
9306 equality-expression == relational-expression
9307 equality-expression != relational-expression
9311 and-expression & equality-expression
9313 exclusive-or-expression:
9315 exclusive-or-expression ^ and-expression
9317 inclusive-or-expression:
9318 exclusive-or-expression
9319 inclusive-or-expression | exclusive-or-expression
9321 logical-and-expression:
9322 inclusive-or-expression
9323 logical-and-expression && inclusive-or-expression
9325 logical-or-expression:
9326 logical-and-expression
9327 logical-or-expression || logical-and-expression
9329 All these are implemented with a single function like:
9332 simple-cast-expression
9333 binary-expression <token> binary-expression
9335 CAST_P is true if this expression is the target of a cast.
9337 The binops_by_token map is used to get the tree codes for each <token> type.
9338 binary-expressions are associated according to a precedence table. */
9340 #define TOKEN_PRECEDENCE(token) \
9341 (((token->type == CPP_GREATER \
9342 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9343 && !parser->greater_than_is_operator_p) \
9344 ? PREC_NOT_OPERATOR \
9345 : binops_by_token[token->type].prec)
9348 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9349 bool no_toplevel_fold_p
,
9351 enum cp_parser_prec prec
,
9354 cp_parser_expression_stack stack
;
9355 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9356 cp_parser_expression_stack_entry current
;
9359 enum tree_code rhs_type
;
9360 enum cp_parser_prec new_prec
, lookahead_prec
;
9363 /* Parse the first expression. */
9364 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9365 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9366 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9367 cast_p
, decltype_p
, pidk
);
9368 current
.prec
= prec
;
9370 if (cp_parser_error_occurred (parser
))
9371 return error_mark_node
;
9375 /* Get an operator token. */
9376 token
= cp_lexer_peek_token (parser
->lexer
);
9378 if (warn_cxx11_compat
9379 && token
->type
== CPP_RSHIFT
9380 && !parser
->greater_than_is_operator_p
)
9382 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9383 "%<>>%> operator is treated"
9384 " as two right angle brackets in C++11"))
9385 inform (token
->location
,
9386 "suggest parentheses around %<>>%> expression");
9389 new_prec
= TOKEN_PRECEDENCE (token
);
9390 if (new_prec
!= PREC_NOT_OPERATOR
9391 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9392 /* This is a fold-expression; handle it later. */
9393 new_prec
= PREC_NOT_OPERATOR
;
9395 /* Popping an entry off the stack means we completed a subexpression:
9396 - either we found a token which is not an operator (`>' where it is not
9397 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9398 will happen repeatedly;
9399 - or, we found an operator which has lower priority. This is the case
9400 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9402 if (new_prec
<= current
.prec
)
9411 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9412 current
.loc
= token
->location
;
9414 /* We used the operator token. */
9415 cp_lexer_consume_token (parser
->lexer
);
9417 /* For "false && x" or "true || x", x will never be executed;
9418 disable warnings while evaluating it. */
9419 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9420 c_inhibit_evaluation_warnings
+=
9421 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9422 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9423 c_inhibit_evaluation_warnings
+=
9424 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9426 /* Extract another operand. It may be the RHS of this expression
9427 or the LHS of a new, higher priority expression. */
9428 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9429 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9430 rhs
= cp_parser_simple_cast_expression (parser
);
9432 /* Get another operator token. Look up its precedence to avoid
9433 building a useless (immediately popped) stack entry for common
9434 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9435 token
= cp_lexer_peek_token (parser
->lexer
);
9436 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9437 if (lookahead_prec
!= PREC_NOT_OPERATOR
9438 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9439 lookahead_prec
= PREC_NOT_OPERATOR
;
9440 if (lookahead_prec
> new_prec
)
9442 /* ... and prepare to parse the RHS of the new, higher priority
9443 expression. Since precedence levels on the stack are
9444 monotonically increasing, we do not have to care about
9449 current
.lhs_type
= rhs_type
;
9450 current
.prec
= new_prec
;
9451 new_prec
= lookahead_prec
;
9455 lookahead_prec
= new_prec
;
9456 /* If the stack is not empty, we have parsed into LHS the right side
9457 (`4' in the example above) of an expression we had suspended.
9458 We can use the information on the stack to recover the LHS (`3')
9459 from the stack together with the tree code (`MULT_EXPR'), and
9460 the precedence of the higher level subexpression
9461 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9462 which will be used to actually build the additive expression. */
9464 rhs_type
= current
.lhs_type
;
9469 /* Undo the disabling of warnings done above. */
9470 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9471 c_inhibit_evaluation_warnings
-=
9472 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9473 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9474 c_inhibit_evaluation_warnings
-=
9475 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9477 if (warn_logical_not_paren
9478 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9479 && current
.lhs_type
== TRUTH_NOT_EXPR
9480 /* Avoid warning for !!x == y. */
9481 && (TREE_CODE (current
.lhs
) != NE_EXPR
9482 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9483 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9484 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9485 /* Avoid warning for !b == y where b is boolean. */
9486 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9487 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9489 /* Avoid warning for !!b == y where b is boolean. */
9490 && (!DECL_P (current
.lhs
)
9491 || TREE_TYPE (current
.lhs
) == NULL_TREE
9492 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9493 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9494 current
.lhs
, maybe_constant_value (rhs
));
9498 location_t combined_loc
= make_location (current
.loc
,
9499 current
.lhs
.get_start (),
9502 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9503 ERROR_MARK for everything that is not a binary expression.
9504 This makes warn_about_parentheses miss some warnings that
9505 involve unary operators. For unary expressions we should
9506 pass the correct tree_code unless the unary expression was
9507 surrounded by parentheses.
9509 if (no_toplevel_fold_p
9510 && lookahead_prec
<= current
.prec
9513 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9514 current
.lhs
= error_mark_node
;
9518 = build_min (current
.tree_type
,
9519 TREE_CODE_CLASS (current
.tree_type
)
9521 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9522 current
.lhs
.get_value (), rhs
.get_value ());
9523 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9528 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9529 current
.lhs
, current
.lhs_type
,
9530 rhs
, rhs_type
, &overload
,
9531 complain_flags (decltype_p
));
9532 /* TODO: build_x_binary_op doesn't always honor the location. */
9533 current
.lhs
.set_location (combined_loc
);
9535 current
.lhs_type
= current
.tree_type
;
9537 /* If the binary operator required the use of an overloaded operator,
9538 then this expression cannot be an integral constant-expression.
9539 An overloaded operator can be used even if both operands are
9540 otherwise permissible in an integral constant-expression if at
9541 least one of the operands is of enumeration type. */
9544 && cp_parser_non_integral_constant_expression (parser
,
9546 return error_mark_node
;
9553 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9554 bool no_toplevel_fold_p
,
9555 enum cp_parser_prec prec
,
9558 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9559 /*decltype*/false, prec
, pidk
);
9562 /* Parse the `? expression : assignment-expression' part of a
9563 conditional-expression. The LOGICAL_OR_EXPR is the
9564 logical-or-expression that started the conditional-expression.
9565 Returns a representation of the entire conditional-expression.
9567 This routine is used by cp_parser_assignment_expression.
9569 ? expression : assignment-expression
9573 ? : assignment-expression */
9576 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9578 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9579 cp_expr assignment_expr
;
9580 struct cp_token
*token
;
9581 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9583 /* Consume the `?' token. */
9584 cp_lexer_consume_token (parser
->lexer
);
9585 token
= cp_lexer_peek_token (parser
->lexer
);
9586 if (cp_parser_allow_gnu_extensions_p (parser
)
9587 && token
->type
== CPP_COLON
)
9589 pedwarn (token
->location
, OPT_Wpedantic
,
9590 "ISO C++ does not allow ?: with omitted middle operand");
9591 /* Implicit true clause. */
9593 c_inhibit_evaluation_warnings
+=
9594 folded_logical_or_expr
== truthvalue_true_node
;
9595 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9599 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9600 parser
->colon_corrects_to_scope_p
= false;
9601 /* Parse the expression. */
9602 c_inhibit_evaluation_warnings
+=
9603 folded_logical_or_expr
== truthvalue_false_node
;
9604 expr
= cp_parser_expression (parser
);
9605 c_inhibit_evaluation_warnings
+=
9606 ((folded_logical_or_expr
== truthvalue_true_node
)
9607 - (folded_logical_or_expr
== truthvalue_false_node
));
9608 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9611 /* The next token should be a `:'. */
9612 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9613 /* Parse the assignment-expression. */
9614 assignment_expr
= cp_parser_assignment_expression (parser
);
9615 c_inhibit_evaluation_warnings
-=
9616 folded_logical_or_expr
== truthvalue_true_node
;
9619 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9620 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9621 with the caret at the "?", ranging from the start of
9622 the logical_or_expr to the end of the assignment_expr. */
9623 loc
= make_location (loc
,
9624 logical_or_expr
.get_start (),
9625 assignment_expr
.get_finish ());
9627 /* Build the conditional-expression. */
9628 return build_x_conditional_expr (loc
, logical_or_expr
,
9631 tf_warning_or_error
);
9634 /* Parse an assignment-expression.
9636 assignment-expression:
9637 conditional-expression
9638 logical-or-expression assignment-operator assignment_expression
9641 CAST_P is true if this expression is the target of a cast.
9642 DECLTYPE_P is true if this expression is the operand of decltype.
9644 Returns a representation for the expression. */
9647 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9648 bool cast_p
, bool decltype_p
)
9652 /* If the next token is the `throw' keyword, then we're looking at
9653 a throw-expression. */
9654 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9655 expr
= cp_parser_throw_expression (parser
);
9656 /* Otherwise, it must be that we are looking at a
9657 logical-or-expression. */
9660 /* Parse the binary expressions (logical-or-expression). */
9661 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9663 PREC_NOT_OPERATOR
, pidk
);
9664 /* If the next token is a `?' then we're actually looking at a
9665 conditional-expression. */
9666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9667 return cp_parser_question_colon_clause (parser
, expr
);
9670 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9672 /* If it's an assignment-operator, we're using the second
9674 enum tree_code assignment_operator
9675 = cp_parser_assignment_operator_opt (parser
);
9676 if (assignment_operator
!= ERROR_MARK
)
9678 bool non_constant_p
;
9680 /* Parse the right-hand side of the assignment. */
9681 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9684 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9685 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9687 /* An assignment may not appear in a
9688 constant-expression. */
9689 if (cp_parser_non_integral_constant_expression (parser
,
9691 return error_mark_node
;
9692 /* Build the assignment expression. Its default
9696 is the location of the '=' token as the
9697 caret, ranging from the start of the lhs to the
9699 loc
= make_location (loc
,
9702 expr
= build_x_modify_expr (loc
, expr
,
9703 assignment_operator
,
9705 complain_flags (decltype_p
));
9706 /* TODO: build_x_modify_expr doesn't honor the location,
9707 so we must set it here. */
9708 expr
.set_location (loc
);
9716 /* Parse an (optional) assignment-operator.
9718 assignment-operator: one of
9719 = *= /= %= += -= >>= <<= &= ^= |=
9723 assignment-operator: one of
9726 If the next token is an assignment operator, the corresponding tree
9727 code is returned, and the token is consumed. For example, for
9728 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9729 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9730 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9731 operator, ERROR_MARK is returned. */
9733 static enum tree_code
9734 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9739 /* Peek at the next token. */
9740 token
= cp_lexer_peek_token (parser
->lexer
);
9742 switch (token
->type
)
9753 op
= TRUNC_DIV_EXPR
;
9757 op
= TRUNC_MOD_EXPR
;
9789 /* Nothing else is an assignment operator. */
9793 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9794 if (op
!= ERROR_MARK
9795 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9798 /* If it was an assignment operator, consume it. */
9799 if (op
!= ERROR_MARK
)
9800 cp_lexer_consume_token (parser
->lexer
);
9805 /* Parse an expression.
9808 assignment-expression
9809 expression , assignment-expression
9811 CAST_P is true if this expression is the target of a cast.
9812 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9813 except possibly parenthesized or on the RHS of a comma (N3276).
9815 Returns a representation of the expression. */
9818 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9819 bool cast_p
, bool decltype_p
)
9821 cp_expr expression
= NULL_TREE
;
9822 location_t loc
= UNKNOWN_LOCATION
;
9826 cp_expr assignment_expression
;
9828 /* Parse the next assignment-expression. */
9829 assignment_expression
9830 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9832 /* We don't create a temporary for a call that is the immediate operand
9833 of decltype or on the RHS of a comma. But when we see a comma, we
9834 need to create a temporary for a call on the LHS. */
9835 if (decltype_p
&& !processing_template_decl
9836 && TREE_CODE (assignment_expression
) == CALL_EXPR
9837 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9838 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9839 assignment_expression
9840 = build_cplus_new (TREE_TYPE (assignment_expression
),
9841 assignment_expression
, tf_warning_or_error
);
9843 /* If this is the first assignment-expression, we can just
9846 expression
= assignment_expression
;
9849 /* Create a location with caret at the comma, ranging
9850 from the start of the LHS to the end of the RHS. */
9851 loc
= make_location (loc
,
9852 expression
.get_start (),
9853 assignment_expression
.get_finish ());
9854 expression
= build_x_compound_expr (loc
, expression
,
9855 assignment_expression
,
9856 complain_flags (decltype_p
));
9857 expression
.set_location (loc
);
9859 /* If the next token is not a comma, or we're in a fold-expression, then
9860 we are done with the expression. */
9861 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9862 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9864 /* Consume the `,'. */
9865 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9866 cp_lexer_consume_token (parser
->lexer
);
9867 /* A comma operator cannot appear in a constant-expression. */
9868 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9869 expression
= error_mark_node
;
9875 /* Parse a constant-expression.
9877 constant-expression:
9878 conditional-expression
9880 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9881 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9882 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9883 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9884 only parse a conditional-expression, otherwise parse an
9885 assignment-expression. See below for rationale. */
9888 cp_parser_constant_expression (cp_parser
* parser
,
9889 bool allow_non_constant_p
,
9890 bool *non_constant_p
,
9893 bool saved_integral_constant_expression_p
;
9894 bool saved_allow_non_integral_constant_expression_p
;
9895 bool saved_non_integral_constant_expression_p
;
9898 /* It might seem that we could simply parse the
9899 conditional-expression, and then check to see if it were
9900 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9901 one that the compiler can figure out is constant, possibly after
9902 doing some simplifications or optimizations. The standard has a
9903 precise definition of constant-expression, and we must honor
9904 that, even though it is somewhat more restrictive.
9910 is not a legal declaration, because `(2, 3)' is not a
9911 constant-expression. The `,' operator is forbidden in a
9912 constant-expression. However, GCC's constant-folding machinery
9913 will fold this operation to an INTEGER_CST for `3'. */
9915 /* Save the old settings. */
9916 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9917 saved_allow_non_integral_constant_expression_p
9918 = parser
->allow_non_integral_constant_expression_p
;
9919 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9920 /* We are now parsing a constant-expression. */
9921 parser
->integral_constant_expression_p
= true;
9922 parser
->allow_non_integral_constant_expression_p
9923 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9924 parser
->non_integral_constant_expression_p
= false;
9925 /* Although the grammar says "conditional-expression", when not STRICT_P,
9926 we parse an "assignment-expression", which also permits
9927 "throw-expression" and the use of assignment operators. In the case
9928 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9929 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9930 actually essential that we look for an assignment-expression.
9931 For example, cp_parser_initializer_clauses uses this function to
9932 determine whether a particular assignment-expression is in fact
9936 /* Parse the binary expressions (logical-or-expression). */
9937 expression
= cp_parser_binary_expression (parser
, false, false, false,
9938 PREC_NOT_OPERATOR
, NULL
);
9939 /* If the next token is a `?' then we're actually looking at
9940 a conditional-expression; otherwise we're done. */
9941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9942 expression
= cp_parser_question_colon_clause (parser
, expression
);
9945 expression
= cp_parser_assignment_expression (parser
);
9946 /* Restore the old settings. */
9947 parser
->integral_constant_expression_p
9948 = saved_integral_constant_expression_p
;
9949 parser
->allow_non_integral_constant_expression_p
9950 = saved_allow_non_integral_constant_expression_p
;
9951 if (cxx_dialect
>= cxx11
)
9953 /* Require an rvalue constant expression here; that's what our
9954 callers expect. Reference constant expressions are handled
9955 separately in e.g. cp_parser_template_argument. */
9956 tree decay
= expression
;
9957 if (TREE_TYPE (expression
)
9958 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9959 decay
= build_address (expression
);
9960 bool is_const
= potential_rvalue_constant_expression (decay
);
9961 parser
->non_integral_constant_expression_p
= !is_const
;
9962 if (!is_const
&& !allow_non_constant_p
)
9963 require_potential_rvalue_constant_expression (decay
);
9965 if (allow_non_constant_p
)
9966 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9967 parser
->non_integral_constant_expression_p
9968 = saved_non_integral_constant_expression_p
;
9973 /* Parse __builtin_offsetof.
9975 offsetof-expression:
9976 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9978 offsetof-member-designator:
9980 | offsetof-member-designator "." id-expression
9981 | offsetof-member-designator "[" expression "]"
9982 | offsetof-member-designator "->" id-expression */
9985 cp_parser_builtin_offsetof (cp_parser
*parser
)
9987 int save_ice_p
, save_non_ice_p
;
9992 location_t finish_loc
;
9994 /* We're about to accept non-integral-constant things, but will
9995 definitely yield an integral constant expression. Save and
9996 restore these values around our local parsing. */
9997 save_ice_p
= parser
->integral_constant_expression_p
;
9998 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10000 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10002 /* Consume the "__builtin_offsetof" token. */
10003 cp_lexer_consume_token (parser
->lexer
);
10004 /* Consume the opening `('. */
10005 matching_parens parens
;
10006 parens
.require_open (parser
);
10007 /* Parse the type-id. */
10008 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10010 const char *saved_message
= parser
->type_definition_forbidden_message
;
10011 parser
->type_definition_forbidden_message
10012 = G_("types may not be defined within __builtin_offsetof");
10013 type
= cp_parser_type_id (parser
);
10014 parser
->type_definition_forbidden_message
= saved_message
;
10016 /* Look for the `,'. */
10017 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10018 token
= cp_lexer_peek_token (parser
->lexer
);
10020 /* Build the (type *)null that begins the traditional offsetof macro. */
10022 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10023 tf_warning_or_error
);
10025 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10026 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10027 true, &dummy
, token
->location
);
10030 token
= cp_lexer_peek_token (parser
->lexer
);
10031 switch (token
->type
)
10033 case CPP_OPEN_SQUARE
:
10034 /* offsetof-member-designator "[" expression "]" */
10035 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10040 /* offsetof-member-designator "->" identifier */
10041 expr
= grok_array_decl (token
->location
, expr
,
10042 integer_zero_node
, false);
10046 /* offsetof-member-designator "." identifier */
10047 cp_lexer_consume_token (parser
->lexer
);
10048 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10049 expr
, true, &dummy
,
10053 case CPP_CLOSE_PAREN
:
10054 /* Consume the ")" token. */
10055 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10056 cp_lexer_consume_token (parser
->lexer
);
10060 /* Error. We know the following require will fail, but
10061 that gives the proper error message. */
10062 parens
.require_close (parser
);
10063 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10064 expr
= error_mark_node
;
10070 /* Make a location of the form:
10071 __builtin_offsetof (struct s, f)
10072 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10073 with caret at the type-id, ranging from the start of the
10074 "_builtin_offsetof" token to the close paren. */
10075 loc
= make_location (loc
, start_loc
, finish_loc
);
10076 /* The result will be an INTEGER_CST, so we need to explicitly
10077 preserve the location. */
10078 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10081 parser
->integral_constant_expression_p
= save_ice_p
;
10082 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10084 expr
= expr
.maybe_add_location_wrapper ();
10088 /* Parse a trait expression.
10090 Returns a representation of the expression, the underlying type
10091 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10094 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10096 cp_trait_kind kind
;
10097 tree type1
, type2
= NULL_TREE
;
10098 bool binary
= false;
10099 bool variadic
= false;
10103 case RID_HAS_NOTHROW_ASSIGN
:
10104 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10106 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10107 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10109 case RID_HAS_NOTHROW_COPY
:
10110 kind
= CPTK_HAS_NOTHROW_COPY
;
10112 case RID_HAS_TRIVIAL_ASSIGN
:
10113 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10115 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10116 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10118 case RID_HAS_TRIVIAL_COPY
:
10119 kind
= CPTK_HAS_TRIVIAL_COPY
;
10121 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10122 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10125 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10127 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10128 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10130 case RID_IS_ABSTRACT
:
10131 kind
= CPTK_IS_ABSTRACT
;
10133 case RID_IS_AGGREGATE
:
10134 kind
= CPTK_IS_AGGREGATE
;
10136 case RID_IS_BASE_OF
:
10137 kind
= CPTK_IS_BASE_OF
;
10141 kind
= CPTK_IS_CLASS
;
10144 kind
= CPTK_IS_EMPTY
;
10147 kind
= CPTK_IS_ENUM
;
10150 kind
= CPTK_IS_FINAL
;
10152 case RID_IS_LITERAL_TYPE
:
10153 kind
= CPTK_IS_LITERAL_TYPE
;
10156 kind
= CPTK_IS_POD
;
10158 case RID_IS_POLYMORPHIC
:
10159 kind
= CPTK_IS_POLYMORPHIC
;
10161 case RID_IS_SAME_AS
:
10162 kind
= CPTK_IS_SAME_AS
;
10165 case RID_IS_STD_LAYOUT
:
10166 kind
= CPTK_IS_STD_LAYOUT
;
10168 case RID_IS_TRIVIAL
:
10169 kind
= CPTK_IS_TRIVIAL
;
10171 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10172 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10175 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10176 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10179 case RID_IS_TRIVIALLY_COPYABLE
:
10180 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10183 kind
= CPTK_IS_UNION
;
10185 case RID_UNDERLYING_TYPE
:
10186 kind
= CPTK_UNDERLYING_TYPE
;
10191 case RID_DIRECT_BASES
:
10192 kind
= CPTK_DIRECT_BASES
;
10194 case RID_IS_ASSIGNABLE
:
10195 kind
= CPTK_IS_ASSIGNABLE
;
10198 case RID_IS_CONSTRUCTIBLE
:
10199 kind
= CPTK_IS_CONSTRUCTIBLE
;
10203 gcc_unreachable ();
10206 /* Get location of initial token. */
10207 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10209 /* Consume the token. */
10210 cp_lexer_consume_token (parser
->lexer
);
10212 matching_parens parens
;
10213 parens
.require_open (parser
);
10216 type_id_in_expr_sentinel
s (parser
);
10217 type1
= cp_parser_type_id (parser
);
10220 if (type1
== error_mark_node
)
10221 return error_mark_node
;
10225 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10228 type_id_in_expr_sentinel
s (parser
);
10229 type2
= cp_parser_type_id (parser
);
10232 if (type2
== error_mark_node
)
10233 return error_mark_node
;
10237 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10239 cp_lexer_consume_token (parser
->lexer
);
10240 tree elt
= cp_parser_type_id (parser
);
10241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10243 cp_lexer_consume_token (parser
->lexer
);
10244 elt
= make_pack_expansion (elt
);
10246 if (elt
== error_mark_node
)
10247 return error_mark_node
;
10248 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10252 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10253 parens
.require_close (parser
);
10255 /* Construct a location of the form:
10256 __is_trivially_copyable(_Tp)
10257 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10258 with start == caret, finishing at the close-paren. */
10259 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10261 /* Complete the trait expression, which may mean either processing
10262 the trait expr now or saving it for template instantiation. */
10265 case CPTK_UNDERLYING_TYPE
:
10266 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10268 return cp_expr (finish_bases (type1
, false), trait_loc
);
10269 case CPTK_DIRECT_BASES
:
10270 return cp_expr (finish_bases (type1
, true), trait_loc
);
10272 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10276 /* Parse a lambda expression.
10279 lambda-introducer lambda-declarator [opt] compound-statement
10281 Returns a representation of the expression. */
10284 cp_parser_lambda_expression (cp_parser
* parser
)
10286 tree lambda_expr
= build_lambda_expr ();
10289 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10290 cp_token_position start
= 0;
10292 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10294 if (cxx_dialect
>= cxx2a
)
10295 /* C++20 allows lambdas in unevaluated context. */;
10296 else if (cp_unevaluated_operand
)
10298 if (!token
->error_reported
)
10300 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10301 "lambda-expression in unevaluated context"
10302 " only available with -std=c++2a or -std=gnu++2a");
10303 token
->error_reported
= true;
10307 else if (parser
->in_template_argument_list_p
)
10309 if (!token
->error_reported
)
10311 error_at (token
->location
, "lambda-expression in template-argument"
10312 " only available with -std=c++2a or -std=gnu++2a");
10313 token
->error_reported
= true;
10318 /* We may be in the middle of deferred access check. Disable
10320 push_deferring_access_checks (dk_no_deferred
);
10322 cp_parser_lambda_introducer (parser
, lambda_expr
);
10323 if (cp_parser_error_occurred (parser
))
10324 return error_mark_node
;
10326 type
= begin_lambda_type (lambda_expr
);
10327 if (type
== error_mark_node
)
10328 return error_mark_node
;
10330 record_lambda_scope (lambda_expr
);
10332 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10333 determine_visibility (TYPE_NAME (type
));
10335 /* Now that we've started the type, add the capture fields for any
10336 explicit captures. */
10337 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10340 /* Inside the class, surrounding template-parameter-lists do not apply. */
10341 unsigned int saved_num_template_parameter_lists
10342 = parser
->num_template_parameter_lists
;
10343 unsigned char in_statement
= parser
->in_statement
;
10344 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10345 bool fully_implicit_function_template_p
10346 = parser
->fully_implicit_function_template_p
;
10347 tree implicit_template_parms
= parser
->implicit_template_parms
;
10348 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10349 bool auto_is_implicit_function_template_parm_p
10350 = parser
->auto_is_implicit_function_template_parm_p
;
10352 parser
->num_template_parameter_lists
= 0;
10353 parser
->in_statement
= 0;
10354 parser
->in_switch_statement_p
= false;
10355 parser
->fully_implicit_function_template_p
= false;
10356 parser
->implicit_template_parms
= 0;
10357 parser
->implicit_template_scope
= 0;
10358 parser
->auto_is_implicit_function_template_parm_p
= false;
10360 /* By virtue of defining a local class, a lambda expression has access to
10361 the private variables of enclosing classes. */
10363 if (cp_parser_start_tentative_firewall (parser
))
10366 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10368 if (ok
&& cp_parser_error_occurred (parser
))
10373 cp_parser_lambda_body (parser
, lambda_expr
);
10375 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10377 if (cp_parser_skip_to_closing_brace (parser
))
10378 cp_lexer_consume_token (parser
->lexer
);
10381 /* The capture list was built up in reverse order; fix that now. */
10382 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10383 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10386 maybe_add_lambda_conv_op (type
);
10388 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10390 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10391 parser
->in_statement
= in_statement
;
10392 parser
->in_switch_statement_p
= in_switch_statement_p
;
10393 parser
->fully_implicit_function_template_p
10394 = fully_implicit_function_template_p
;
10395 parser
->implicit_template_parms
= implicit_template_parms
;
10396 parser
->implicit_template_scope
= implicit_template_scope
;
10397 parser
->auto_is_implicit_function_template_parm_p
10398 = auto_is_implicit_function_template_parm_p
;
10401 /* This field is only used during parsing of the lambda. */
10402 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10404 /* This lambda shouldn't have any proxies left at this point. */
10405 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10406 /* And now that we're done, push proxies for an enclosing lambda. */
10407 insert_pending_capture_proxies ();
10409 /* Update the lambda expression to a range. */
10410 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10411 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10413 end_tok
->location
);
10416 lambda_expr
= build_lambda_object (lambda_expr
);
10418 lambda_expr
= error_mark_node
;
10420 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10422 pop_deferring_access_checks ();
10424 return lambda_expr
;
10427 /* Parse the beginning of a lambda expression.
10430 [ lambda-capture [opt] ]
10432 LAMBDA_EXPR is the current representation of the lambda expression. */
10435 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10437 /* Need commas after the first capture. */
10440 /* Eat the leading `['. */
10441 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10443 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10445 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10446 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10447 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10448 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10450 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10452 cp_lexer_consume_token (parser
->lexer
);
10455 if (!(at_function_scope_p () || parsing_nsdmi ()))
10456 error ("non-local lambda expression cannot have a capture-default");
10459 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10461 cp_token
* capture_token
;
10463 tree capture_init_expr
;
10464 cp_id_kind idk
= CP_ID_KIND_NONE
;
10465 bool explicit_init_p
= false;
10467 enum capture_kind_type
10472 enum capture_kind_type capture_kind
= BY_COPY
;
10474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10476 error ("expected end of capture-list");
10483 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10485 /* Possibly capture `this'. */
10486 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10488 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10489 if (cxx_dialect
< cxx2a
10490 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10491 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10492 "with by-copy capture default");
10493 cp_lexer_consume_token (parser
->lexer
);
10494 add_capture (lambda_expr
,
10495 /*id=*/this_identifier
,
10496 /*initializer=*/finish_this_expr (),
10497 /*by_reference_p=*/true,
10502 /* Possibly capture `*this'. */
10503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10504 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10506 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10507 if (cxx_dialect
< cxx17
)
10508 pedwarn (loc
, 0, "%<*this%> capture only available with "
10509 "-std=c++17 or -std=gnu++17");
10510 cp_lexer_consume_token (parser
->lexer
);
10511 cp_lexer_consume_token (parser
->lexer
);
10512 add_capture (lambda_expr
,
10513 /*id=*/this_identifier
,
10514 /*initializer=*/finish_this_expr (),
10515 /*by_reference_p=*/false,
10520 bool init_pack_expansion
= false;
10521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10523 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10524 if (cxx_dialect
< cxx2a
)
10525 pedwarn (loc
, 0, "pack init-capture only available with "
10526 "-std=c++2a or -std=gnu++2a");
10527 cp_lexer_consume_token (parser
->lexer
);
10528 init_pack_expansion
= true;
10531 /* Remember whether we want to capture as a reference or not. */
10532 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10534 capture_kind
= BY_REFERENCE
;
10535 cp_lexer_consume_token (parser
->lexer
);
10538 /* Get the identifier. */
10539 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10540 capture_id
= cp_parser_identifier (parser
);
10542 if (capture_id
== error_mark_node
)
10543 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10544 delimiters, but I modified this to stop on unnested ']' as well. It
10545 was already changed to stop on unnested '}', so the
10546 "closing_parenthesis" name is no more misleading with my change. */
10548 cp_parser_skip_to_closing_parenthesis (parser
,
10549 /*recovering=*/true,
10551 /*consume_paren=*/true);
10555 /* Find the initializer for this capture. */
10556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10557 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10558 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10560 bool direct
, non_constant
;
10561 /* An explicit initializer exists. */
10562 if (cxx_dialect
< cxx14
)
10563 pedwarn (input_location
, 0,
10564 "lambda capture initializers "
10565 "only available with -std=c++14 or -std=gnu++14");
10566 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10567 &non_constant
, true);
10568 explicit_init_p
= true;
10569 if (capture_init_expr
== NULL_TREE
)
10571 error ("empty initializer for lambda init-capture");
10572 capture_init_expr
= error_mark_node
;
10574 if (init_pack_expansion
)
10575 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10579 const char* error_msg
;
10581 /* Turn the identifier into an id-expression. */
10583 = cp_parser_lookup_name_simple (parser
, capture_id
,
10584 capture_token
->location
);
10586 if (capture_init_expr
== error_mark_node
)
10588 unqualified_name_lookup_error (capture_id
);
10591 else if (!VAR_P (capture_init_expr
)
10592 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10594 error_at (capture_token
->location
,
10595 "capture of non-variable %qE",
10596 capture_init_expr
);
10597 if (DECL_P (capture_init_expr
))
10598 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10599 "%q#D declared here", capture_init_expr
);
10602 if (VAR_P (capture_init_expr
)
10603 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10605 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10606 "%qD with non-automatic storage duration",
10607 capture_init_expr
))
10608 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10609 "%q#D declared here", capture_init_expr
);
10614 = finish_id_expression
10619 /*integral_constant_expression_p=*/false,
10620 /*allow_non_integral_constant_expression_p=*/false,
10621 /*non_integral_constant_expression_p=*/NULL
,
10622 /*template_p=*/false,
10624 /*address_p=*/false,
10625 /*template_arg_p=*/false,
10627 capture_token
->location
);
10629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10631 cp_lexer_consume_token (parser
->lexer
);
10632 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10636 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10637 && !explicit_init_p
)
10639 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10640 && capture_kind
== BY_COPY
)
10641 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10642 "of %qD redundant with by-copy capture default",
10644 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10645 && capture_kind
== BY_REFERENCE
)
10646 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10647 "capture of %qD redundant with by-reference capture "
10648 "default", capture_id
);
10651 add_capture (lambda_expr
,
10654 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10657 /* If there is any qualification still in effect, clear it
10658 now; we will be starting fresh with the next capture. */
10659 parser
->scope
= NULL_TREE
;
10660 parser
->qualifying_scope
= NULL_TREE
;
10661 parser
->object_scope
= NULL_TREE
;
10664 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10667 /* Parse the (optional) middle of a lambda expression.
10670 < template-parameter-list [opt] >
10671 ( parameter-declaration-clause [opt] )
10672 attribute-specifier [opt]
10673 decl-specifier-seq [opt]
10674 exception-specification [opt]
10675 lambda-return-type-clause [opt]
10677 LAMBDA_EXPR is the current representation of the lambda expression. */
10680 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10682 /* 5.1.1.4 of the standard says:
10683 If a lambda-expression does not include a lambda-declarator, it is as if
10684 the lambda-declarator were ().
10685 This means an empty parameter list, no attributes, and no exception
10687 tree param_list
= void_list_node
;
10688 tree attributes
= NULL_TREE
;
10689 tree exception_spec
= NULL_TREE
;
10690 tree template_param_list
= NULL_TREE
;
10691 tree tx_qual
= NULL_TREE
;
10692 tree return_type
= NULL_TREE
;
10693 cp_decl_specifier_seq lambda_specs
;
10694 clear_decl_specs (&lambda_specs
);
10696 /* The template-parameter-list is optional, but must begin with
10697 an opening angle if present. */
10698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10700 if (cxx_dialect
< cxx14
)
10701 pedwarn (parser
->lexer
->next_token
->location
, 0,
10702 "lambda templates are only available with "
10703 "-std=c++14 or -std=gnu++14");
10704 else if (cxx_dialect
< cxx2a
)
10705 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10706 "lambda templates are only available with "
10707 "-std=c++2a or -std=gnu++2a");
10709 cp_lexer_consume_token (parser
->lexer
);
10711 template_param_list
= cp_parser_template_parameter_list (parser
);
10713 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10715 /* We just processed one more parameter list. */
10716 ++parser
->num_template_parameter_lists
;
10719 /* The parameter-declaration-clause is optional (unless
10720 template-parameter-list was given), but must begin with an
10721 opening parenthesis if present. */
10722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10724 matching_parens parens
;
10725 parens
.consume_open (parser
);
10727 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10729 /* Parse parameters. */
10730 param_list
= cp_parser_parameter_declaration_clause (parser
);
10732 /* Default arguments shall not be specified in the
10733 parameter-declaration-clause of a lambda-declarator. */
10734 if (cxx_dialect
< cxx14
)
10735 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10736 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10737 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10738 "default argument specified for lambda parameter");
10740 parens
.require_close (parser
);
10742 /* In the decl-specifier-seq of the lambda-declarator, each
10743 decl-specifier shall either be mutable or constexpr. */
10744 int declares_class_or_enum
;
10745 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10746 cp_parser_decl_specifier_seq (parser
,
10747 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10748 &lambda_specs
, &declares_class_or_enum
);
10749 if (lambda_specs
.storage_class
== sc_mutable
)
10751 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10752 if (lambda_specs
.conflicting_specifiers_p
)
10753 error_at (lambda_specs
.locations
[ds_storage_class
],
10754 "duplicate %<mutable%>");
10757 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10759 /* Parse optional exception specification. */
10760 exception_spec
= cp_parser_exception_specification_opt (parser
);
10762 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10764 /* Parse optional trailing return type. */
10765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10767 cp_lexer_consume_token (parser
->lexer
);
10768 return_type
= cp_parser_trailing_type_id (parser
);
10771 /* The function parameters must be in scope all the way until after the
10772 trailing-return-type in case of decltype. */
10773 pop_bindings_and_leave_scope ();
10775 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10776 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10778 /* Create the function call operator.
10780 Messing with declarators like this is no uglier than building up the
10781 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10784 cp_decl_specifier_seq return_type_specs
;
10785 cp_declarator
* declarator
;
10790 clear_decl_specs (&return_type_specs
);
10791 return_type_specs
.type
= make_auto ();
10793 if (lambda_specs
.locations
[ds_constexpr
])
10795 if (cxx_dialect
>= cxx17
)
10796 return_type_specs
.locations
[ds_constexpr
]
10797 = lambda_specs
.locations
[ds_constexpr
];
10799 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10800 "lambda only available with -std=c++17 or -std=gnu++17");
10803 p
= obstack_alloc (&declarator_obstack
, 0);
10805 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10806 LAMBDA_EXPR_LOCATION (lambda_expr
));
10808 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10809 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10810 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10811 VIRT_SPEC_UNSPECIFIED
,
10816 /*requires_clause*/NULL_TREE
);
10817 declarator
->std_attributes
= attributes
;
10819 fco
= grokmethod (&return_type_specs
,
10822 if (fco
!= error_mark_node
)
10824 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10825 DECL_ARTIFICIAL (fco
) = 1;
10826 /* Give the object parameter a different name. */
10827 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10828 DECL_LAMBDA_FUNCTION (fco
) = 1;
10830 if (template_param_list
)
10832 fco
= finish_member_template_decl (fco
);
10833 finish_template_decl (template_param_list
);
10834 --parser
->num_template_parameter_lists
;
10836 else if (parser
->fully_implicit_function_template_p
)
10837 fco
= finish_fully_implicit_template (parser
, fco
);
10839 finish_member_declaration (fco
);
10841 obstack_free (&declarator_obstack
, p
);
10843 return (fco
!= error_mark_node
);
10847 /* Parse the body of a lambda expression, which is simply
10851 but which requires special handling.
10852 LAMBDA_EXPR is the current representation of the lambda expression. */
10855 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10857 bool nested
= (current_function_decl
!= NULL_TREE
);
10858 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10859 bool in_function_body
= parser
->in_function_body
;
10861 /* The body of a lambda-expression is not a subexpression of the enclosing
10866 push_function_context ();
10868 /* Still increment function_depth so that we don't GC in the
10869 middle of an expression. */
10872 vec
<tree
> omp_privatization_save
;
10873 save_omp_privatization_clauses (omp_privatization_save
);
10874 /* Clear this in case we're in the middle of a default argument. */
10875 parser
->local_variables_forbidden_p
= false;
10876 parser
->in_function_body
= true;
10879 local_specialization_stack
s (lss_copy
);
10880 tree fco
= lambda_function (lambda_expr
);
10881 tree body
= start_lambda_function (fco
, lambda_expr
);
10882 matching_braces braces
;
10884 if (braces
.require_open (parser
))
10886 tree compound_stmt
= begin_compound_stmt (0);
10888 /* Originally C++11 required us to peek for 'return expr'; and
10889 process it specially here to deduce the return type. N3638
10890 removed the need for that. */
10892 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10893 cp_parser_label_declaration (parser
);
10894 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10895 braces
.require_close (parser
);
10897 finish_compound_stmt (compound_stmt
);
10900 finish_lambda_function (body
);
10903 restore_omp_privatization_clauses (omp_privatization_save
);
10904 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10905 parser
->in_function_body
= in_function_body
;
10907 pop_function_context();
10912 /* Statements [gram.stmt.stmt] */
10914 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10917 add_debug_begin_stmt (location_t loc
)
10919 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10921 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10922 /* A concept is never expanded normally. */
10925 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10926 SET_EXPR_LOCATION (stmt
, loc
);
10930 /* Parse a statement.
10934 expression-statement
10936 selection-statement
10937 iteration-statement
10939 declaration-statement
10946 attribute-specifier-seq (opt) expression-statement
10947 attribute-specifier-seq (opt) compound-statement
10948 attribute-specifier-seq (opt) selection-statement
10949 attribute-specifier-seq (opt) iteration-statement
10950 attribute-specifier-seq (opt) jump-statement
10951 declaration-statement
10952 attribute-specifier-seq (opt) try-block
10955 expression-statement
10963 IN_COMPOUND is true when the statement is nested inside a
10964 cp_parser_compound_statement; this matters for certain pragmas.
10966 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10967 is a (possibly labeled) if statement which is not enclosed in braces
10968 and has an else clause. This is used to implement -Wparentheses.
10970 CHAIN is a vector of if-else-if conditions. */
10973 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10974 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10975 location_t
*loc_after_labels
)
10977 tree statement
, std_attrs
= NULL_TREE
;
10979 location_t statement_location
, attrs_location
;
10984 /* There is no statement yet. */
10985 statement
= NULL_TREE
;
10987 saved_token_sentinel
saved_tokens (parser
->lexer
);
10988 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10989 if (c_dialect_objc ())
10990 /* In obj-c++, seeing '[[' might be the either the beginning of
10991 c++11 attributes, or a nested objc-message-expression. So
10992 let's parse the c++11 attributes tentatively. */
10993 cp_parser_parse_tentatively (parser
);
10994 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10995 if (c_dialect_objc ())
10997 if (!cp_parser_parse_definitely (parser
))
10998 std_attrs
= NULL_TREE
;
11001 /* Peek at the next token. */
11002 token
= cp_lexer_peek_token (parser
->lexer
);
11003 /* Remember the location of the first token in the statement. */
11004 cp_token
*statement_token
= token
;
11005 statement_location
= token
->location
;
11006 add_debug_begin_stmt (statement_location
);
11007 /* If this is a keyword, then that will often determine what kind of
11008 statement we have. */
11009 if (token
->type
== CPP_KEYWORD
)
11011 enum rid keyword
= token
->keyword
;
11017 /* Looks like a labeled-statement with a case label.
11018 Parse the label, and then use tail recursion to parse
11020 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11021 in_compound
= false;
11026 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11027 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11033 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11034 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11041 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11042 statement
= cp_parser_jump_statement (parser
);
11045 /* Objective-C++ exception-handling constructs. */
11048 case RID_AT_FINALLY
:
11049 case RID_AT_SYNCHRONIZED
:
11051 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11052 statement
= cp_parser_objc_statement (parser
);
11056 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11057 statement
= cp_parser_try_block (parser
);
11060 case RID_NAMESPACE
:
11061 /* This must be a namespace alias definition. */
11062 if (std_attrs
!= NULL_TREE
)
11064 /* Attributes should be parsed as part of the the
11065 declaration, so let's un-parse them. */
11066 saved_tokens
.rollback();
11067 std_attrs
= NULL_TREE
;
11069 cp_parser_declaration_statement (parser
);
11072 case RID_TRANSACTION_ATOMIC
:
11073 case RID_TRANSACTION_RELAXED
:
11074 case RID_SYNCHRONIZED
:
11075 case RID_ATOMIC_NOEXCEPT
:
11076 case RID_ATOMIC_CANCEL
:
11077 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11078 statement
= cp_parser_transaction (parser
, token
);
11080 case RID_TRANSACTION_CANCEL
:
11081 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11082 statement
= cp_parser_transaction_cancel (parser
);
11086 /* It might be a keyword like `int' that can start a
11087 declaration-statement. */
11091 else if (token
->type
== CPP_NAME
)
11093 /* If the next token is a `:', then we are looking at a
11094 labeled-statement. */
11095 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11096 if (token
->type
== CPP_COLON
)
11098 /* Looks like a labeled-statement with an ordinary label.
11099 Parse the label, and then use tail recursion to parse
11102 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11103 in_compound
= false;
11107 /* Anything that starts with a `{' must be a compound-statement. */
11108 else if (token
->type
== CPP_OPEN_BRACE
)
11109 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11110 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11111 a statement all its own. */
11112 else if (token
->type
== CPP_PRAGMA
)
11114 /* Only certain OpenMP pragmas are attached to statements, and thus
11115 are considered statements themselves. All others are not. In
11116 the context of a compound, accept the pragma as a "statement" and
11117 return so that we can check for a close brace. Otherwise we
11118 require a real statement and must go back and read one. */
11120 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11121 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11125 else if (token
->type
== CPP_EOF
)
11127 cp_parser_error (parser
, "expected statement");
11131 /* Everything else must be a declaration-statement or an
11132 expression-statement. Try for the declaration-statement
11133 first, unless we are looking at a `;', in which case we know that
11134 we have an expression-statement. */
11137 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11139 if (std_attrs
!= NULL_TREE
)
11140 /* Attributes should be parsed as part of the declaration,
11141 so let's un-parse them. */
11142 saved_tokens
.rollback();
11144 cp_parser_parse_tentatively (parser
);
11145 /* Try to parse the declaration-statement. */
11146 cp_parser_declaration_statement (parser
);
11147 /* If that worked, we're done. */
11148 if (cp_parser_parse_definitely (parser
))
11150 /* It didn't work, restore the post-attribute position. */
11152 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11154 /* All preceding labels have been parsed at this point. */
11155 if (loc_after_labels
!= NULL
)
11156 *loc_after_labels
= statement_location
;
11158 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11160 /* Look for an expression-statement instead. */
11161 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11163 /* Handle [[fallthrough]];. */
11164 if (attribute_fallthrough_p (std_attrs
))
11166 /* The next token after the fallthrough attribute is ';'. */
11167 if (statement
== NULL_TREE
)
11169 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11170 statement
= build_call_expr_internal_loc (statement_location
,
11172 void_type_node
, 0);
11173 finish_expr_stmt (statement
);
11176 warning_at (statement_location
, OPT_Wattributes
,
11177 "%<fallthrough%> attribute not followed by %<;%>");
11178 std_attrs
= NULL_TREE
;
11182 /* Set the line number for the statement. */
11183 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11184 SET_EXPR_LOCATION (statement
, statement_location
);
11186 /* Allow "[[fallthrough]];", but warn otherwise. */
11187 if (std_attrs
!= NULL_TREE
)
11188 warning_at (attrs_location
,
11190 "attributes at the beginning of statement are ignored");
11193 /* Append ATTR to attribute list ATTRS. */
11196 attr_chainon (tree attrs
, tree attr
)
11198 if (attrs
== error_mark_node
)
11199 return error_mark_node
;
11200 if (attr
== error_mark_node
)
11201 return error_mark_node
;
11202 return chainon (attrs
, attr
);
11205 /* Parse the label for a labeled-statement, i.e.
11208 case constant-expression :
11212 case constant-expression ... constant-expression : statement
11214 When a label is parsed without errors, the label is added to the
11215 parse tree by the finish_* functions, so this function doesn't
11216 have to return the label. */
11219 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11222 tree label
= NULL_TREE
;
11223 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11225 /* The next token should be an identifier. */
11226 token
= cp_lexer_peek_token (parser
->lexer
);
11227 if (token
->type
!= CPP_NAME
11228 && token
->type
!= CPP_KEYWORD
)
11230 cp_parser_error (parser
, "expected labeled-statement");
11234 /* Remember whether this case or a user-defined label is allowed to fall
11236 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11238 parser
->colon_corrects_to_scope_p
= false;
11239 switch (token
->keyword
)
11243 tree expr
, expr_hi
;
11244 cp_token
*ellipsis
;
11246 /* Consume the `case' token. */
11247 cp_lexer_consume_token (parser
->lexer
);
11248 /* Parse the constant-expression. */
11249 expr
= cp_parser_constant_expression (parser
);
11250 if (check_for_bare_parameter_packs (expr
))
11251 expr
= error_mark_node
;
11253 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11254 if (ellipsis
->type
== CPP_ELLIPSIS
)
11256 /* Consume the `...' token. */
11257 cp_lexer_consume_token (parser
->lexer
);
11258 expr_hi
= cp_parser_constant_expression (parser
);
11259 if (check_for_bare_parameter_packs (expr_hi
))
11260 expr_hi
= error_mark_node
;
11262 /* We don't need to emit warnings here, as the common code
11263 will do this for us. */
11266 expr_hi
= NULL_TREE
;
11268 if (parser
->in_switch_statement_p
)
11270 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11271 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11273 label
= CASE_LABEL (l
);
11274 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11278 error_at (token
->location
,
11279 "case label %qE not within a switch statement",
11285 /* Consume the `default' token. */
11286 cp_lexer_consume_token (parser
->lexer
);
11288 if (parser
->in_switch_statement_p
)
11290 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11291 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11293 label
= CASE_LABEL (l
);
11294 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11298 error_at (token
->location
, "case label not within a switch statement");
11302 /* Anything else must be an ordinary label. */
11303 label
= finish_label_stmt (cp_parser_identifier (parser
));
11304 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11305 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11309 /* Require the `:' token. */
11310 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11312 /* An ordinary label may optionally be followed by attributes.
11313 However, this is only permitted if the attributes are then
11314 followed by a semicolon. This is because, for backward
11315 compatibility, when parsing
11316 lab: __attribute__ ((unused)) int i;
11317 we want the attribute to attach to "i", not "lab". */
11318 if (label
!= NULL_TREE
11319 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11322 cp_parser_parse_tentatively (parser
);
11323 attrs
= cp_parser_gnu_attributes_opt (parser
);
11324 if (attrs
== NULL_TREE
11325 /* And fallthrough always binds to the expression-statement. */
11326 || attribute_fallthrough_p (attrs
)
11327 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11328 cp_parser_abort_tentative_parse (parser
);
11329 else if (!cp_parser_parse_definitely (parser
))
11332 attributes
= attr_chainon (attributes
, attrs
);
11335 if (attributes
!= NULL_TREE
)
11336 cplus_decl_attributes (&label
, attributes
, 0);
11338 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11341 /* Parse an expression-statement.
11343 expression-statement:
11346 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11347 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11348 indicates whether this expression-statement is part of an
11349 expression statement. */
11352 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11354 tree statement
= NULL_TREE
;
11355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11356 location_t loc
= token
->location
;
11358 /* There might be attribute fallthrough. */
11359 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11361 /* If the next token is a ';', then there is no expression
11363 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11365 statement
= cp_parser_expression (parser
);
11366 if (statement
== error_mark_node
11367 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11369 cp_parser_skip_to_end_of_block_or_statement (parser
);
11370 return error_mark_node
;
11374 /* Handle [[fallthrough]];. */
11375 if (attribute_fallthrough_p (attr
))
11377 /* The next token after the fallthrough attribute is ';'. */
11378 if (statement
== NULL_TREE
)
11379 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11380 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11381 void_type_node
, 0);
11383 warning_at (loc
, OPT_Wattributes
,
11384 "%<fallthrough%> attribute not followed by %<;%>");
11388 /* Allow "[[fallthrough]];", but warn otherwise. */
11389 if (attr
!= NULL_TREE
)
11390 warning_at (loc
, OPT_Wattributes
,
11391 "attributes at the beginning of statement are ignored");
11393 /* Give a helpful message for "A<T>::type t;" and the like. */
11394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11395 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11397 if (TREE_CODE (statement
) == SCOPE_REF
)
11398 error_at (token
->location
, "need %<typename%> before %qE because "
11399 "%qT is a dependent scope",
11400 statement
, TREE_OPERAND (statement
, 0));
11401 else if (is_overloaded_fn (statement
)
11402 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11405 tree fn
= get_first_fn (statement
);
11406 error_at (token
->location
,
11407 "%<%T::%D%> names the constructor, not the type",
11408 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11412 /* Consume the final `;'. */
11413 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11415 if (in_statement_expr
11416 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11417 /* This is the final expression statement of a statement
11419 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11420 else if (statement
)
11421 statement
= finish_expr_stmt (statement
);
11426 /* Parse a compound-statement.
11428 compound-statement:
11429 { statement-seq [opt] }
11433 compound-statement:
11434 { label-declaration-seq [opt] statement-seq [opt] }
11436 label-declaration-seq:
11438 label-declaration-seq label-declaration
11440 Returns a tree representing the statement. */
11443 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11444 int bcs_flags
, bool function_body
)
11446 tree compound_stmt
;
11447 matching_braces braces
;
11449 /* Consume the `{'. */
11450 if (!braces
.require_open (parser
))
11451 return error_mark_node
;
11452 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11453 && !function_body
&& cxx_dialect
< cxx14
)
11454 pedwarn (input_location
, OPT_Wpedantic
,
11455 "compound-statement in %<constexpr%> function");
11456 /* Begin the compound-statement. */
11457 compound_stmt
= begin_compound_stmt (bcs_flags
);
11458 /* If the next keyword is `__label__' we have a label declaration. */
11459 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11460 cp_parser_label_declaration (parser
);
11461 /* Parse an (optional) statement-seq. */
11462 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11463 /* Finish the compound-statement. */
11464 finish_compound_stmt (compound_stmt
);
11465 /* Consume the `}'. */
11466 braces
.require_close (parser
);
11468 return compound_stmt
;
11471 /* Parse an (optional) statement-seq.
11475 statement-seq [opt] statement */
11478 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11480 /* Scan statements until there aren't any more. */
11483 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11485 /* If we are looking at a `}', then we have run out of
11486 statements; the same is true if we have reached the end
11487 of file, or have stumbled upon a stray '@end'. */
11488 if (token
->type
== CPP_CLOSE_BRACE
11489 || token
->type
== CPP_EOF
11490 || token
->type
== CPP_PRAGMA_EOL
11491 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11494 /* If we are in a compound statement and find 'else' then
11495 something went wrong. */
11496 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11498 if (parser
->in_statement
& IN_IF_STMT
)
11502 token
= cp_lexer_consume_token (parser
->lexer
);
11503 error_at (token
->location
, "%<else%> without a previous %<if%>");
11507 /* Parse the statement. */
11508 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11512 /* Return true if this is the C++20 version of range-based-for with
11516 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11520 /* Save tokens so that we can put them back. */
11521 cp_lexer_save_tokens (parser
->lexer
);
11523 /* There has to be an unnested ; followed by an unnested :. */
11524 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11525 /*recovering=*/false,
11527 /*consume_paren=*/false) != -1)
11530 /* We found the semicolon, eat it now. */
11531 cp_lexer_consume_token (parser
->lexer
);
11533 /* Now look for ':' that is not nested in () or {}. */
11534 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11535 /*recovering=*/false,
11537 /*consume_paren=*/false) == -1);
11540 /* Roll back the tokens we skipped. */
11541 cp_lexer_rollback_tokens (parser
->lexer
);
11546 /* Return true if we're looking at (init; cond), false otherwise. */
11549 cp_parser_init_statement_p (cp_parser
*parser
)
11551 /* Save tokens so that we can put them back. */
11552 cp_lexer_save_tokens (parser
->lexer
);
11554 /* Look for ';' that is not nested in () or {}. */
11555 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11556 /*recovering=*/false,
11558 /*consume_paren=*/false);
11560 /* Roll back the tokens we skipped. */
11561 cp_lexer_rollback_tokens (parser
->lexer
);
11566 /* Parse a selection-statement.
11568 selection-statement:
11569 if ( init-statement [opt] condition ) statement
11570 if ( init-statement [opt] condition ) statement else statement
11571 switch ( init-statement [opt] condition ) statement
11573 Returns the new IF_STMT or SWITCH_STMT.
11575 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11576 is a (possibly labeled) if statement which is not enclosed in
11577 braces and has an else clause. This is used to implement
11580 CHAIN is a vector of if-else-if conditions. This is used to implement
11581 -Wduplicated-cond. */
11584 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11589 token_indent_info guard_tinfo
;
11594 /* Peek at the next token. */
11595 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11596 guard_tinfo
= get_token_indent_info (token
);
11598 /* See what kind of keyword it is. */
11599 keyword
= token
->keyword
;
11609 if (keyword
== RID_IF
11610 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11614 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11615 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11616 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11617 "with -std=c++17 or -std=gnu++17");
11620 /* Look for the `('. */
11621 matching_parens parens
;
11622 if (!parens
.require_open (parser
))
11624 cp_parser_skip_to_end_of_statement (parser
);
11625 return error_mark_node
;
11628 /* Begin the selection-statement. */
11629 if (keyword
== RID_IF
)
11631 statement
= begin_if_stmt ();
11632 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11635 statement
= begin_switch_stmt ();
11637 /* Parse the optional init-statement. */
11638 if (cp_parser_init_statement_p (parser
))
11641 if (cxx_dialect
< cxx17
)
11642 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11643 "init-statement in selection statements only available "
11644 "with -std=c++17 or -std=gnu++17");
11645 cp_parser_init_statement (parser
, &decl
);
11648 /* Parse the condition. */
11649 condition
= cp_parser_condition (parser
);
11650 /* Look for the `)'. */
11651 if (!parens
.require_close (parser
))
11652 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11653 /*consume_paren=*/true);
11655 if (keyword
== RID_IF
)
11658 unsigned char in_statement
;
11660 /* Add the condition. */
11661 condition
= finish_if_stmt_cond (condition
, statement
);
11663 if (warn_duplicated_cond
)
11664 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11667 /* Parse the then-clause. */
11668 in_statement
= parser
->in_statement
;
11669 parser
->in_statement
|= IN_IF_STMT
;
11671 /* Outside a template, the non-selected branch of a constexpr
11672 if is a 'discarded statement', i.e. unevaluated. */
11673 bool was_discarded
= in_discarded_stmt
;
11674 bool discard_then
= (cx
&& !processing_template_decl
11675 && integer_zerop (condition
));
11678 in_discarded_stmt
= true;
11679 ++c_inhibit_evaluation_warnings
;
11682 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11685 parser
->in_statement
= in_statement
;
11687 finish_then_clause (statement
);
11691 THEN_CLAUSE (statement
) = NULL_TREE
;
11692 in_discarded_stmt
= was_discarded
;
11693 --c_inhibit_evaluation_warnings
;
11696 /* If the next token is `else', parse the else-clause. */
11697 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11700 bool discard_else
= (cx
&& !processing_template_decl
11701 && integer_nonzerop (condition
));
11704 in_discarded_stmt
= true;
11705 ++c_inhibit_evaluation_warnings
;
11709 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11710 /* Consume the `else' keyword. */
11711 cp_lexer_consume_token (parser
->lexer
);
11712 if (warn_duplicated_cond
)
11714 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11718 /* We've got "if (COND) else if (COND2)". Start
11719 the condition chain and add COND as the first
11721 chain
= new vec
<tree
> ();
11722 if (!CONSTANT_CLASS_P (condition
)
11723 && !TREE_SIDE_EFFECTS (condition
))
11725 /* Wrap it in a NOP_EXPR so that we can set the
11726 location of the condition. */
11727 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11729 SET_EXPR_LOCATION (e
, token
->location
);
11730 chain
->safe_push (e
);
11733 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11736 /* This is if-else without subsequent if. Zap the
11737 condition chain; we would have already warned at
11743 begin_else_clause (statement
);
11744 /* Parse the else-clause. */
11745 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11746 guard_tinfo
, chain
);
11748 finish_else_clause (statement
);
11750 /* If we are currently parsing a then-clause, then
11751 IF_P will not be NULL. We set it to true to
11752 indicate that this if statement has an else clause.
11753 This may trigger the Wparentheses warning below
11754 when we get back up to the parent if statement. */
11760 ELSE_CLAUSE (statement
) = NULL_TREE
;
11761 in_discarded_stmt
= was_discarded
;
11762 --c_inhibit_evaluation_warnings
;
11767 /* This if statement does not have an else clause. If
11768 NESTED_IF is true, then the then-clause has an if
11769 statement which does have an else clause. We warn
11770 about the potential ambiguity. */
11772 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11773 "suggest explicit braces to avoid ambiguous"
11775 if (warn_duplicated_cond
)
11777 /* We don't need the condition chain anymore. */
11783 /* Now we're all done with the if-statement. */
11784 finish_if_stmt (statement
);
11788 bool in_switch_statement_p
;
11789 unsigned char in_statement
;
11791 /* Add the condition. */
11792 finish_switch_cond (condition
, statement
);
11794 /* Parse the body of the switch-statement. */
11795 in_switch_statement_p
= parser
->in_switch_statement_p
;
11796 in_statement
= parser
->in_statement
;
11797 parser
->in_switch_statement_p
= true;
11798 parser
->in_statement
|= IN_SWITCH_STMT
;
11799 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11801 parser
->in_switch_statement_p
= in_switch_statement_p
;
11802 parser
->in_statement
= in_statement
;
11804 /* Now we're all done with the switch-statement. */
11805 finish_switch_stmt (statement
);
11813 cp_parser_error (parser
, "expected selection-statement");
11814 return error_mark_node
;
11818 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11819 If we have seen at least one decl-specifier, and the next token
11820 is not a parenthesis, then we must be looking at a declaration.
11821 (After "int (" we might be looking at a functional cast.) */
11824 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11825 bool any_specifiers_p
)
11827 if (any_specifiers_p
11828 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11829 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11830 && !cp_parser_error_occurred (parser
))
11831 cp_parser_commit_to_tentative_parse (parser
);
11834 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11835 The declarator shall not specify a function or an array. Returns
11836 TRUE if the declarator is valid, FALSE otherwise. */
11839 cp_parser_check_condition_declarator (cp_parser
* parser
,
11840 cp_declarator
*declarator
,
11843 if (declarator
== cp_error_declarator
11844 || function_declarator_p (declarator
)
11845 || declarator
->kind
== cdk_array
)
11847 if (declarator
== cp_error_declarator
)
11848 /* Already complained. */;
11849 else if (declarator
->kind
== cdk_array
)
11850 error_at (loc
, "condition declares an array");
11852 error_at (loc
, "condition declares a function");
11853 if (parser
->fully_implicit_function_template_p
)
11854 abort_fully_implicit_template (parser
);
11855 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11856 /*or_comma=*/false,
11857 /*consume_paren=*/false);
11864 /* Parse a condition.
11868 type-specifier-seq declarator = initializer-clause
11869 type-specifier-seq declarator braced-init-list
11874 type-specifier-seq declarator asm-specification [opt]
11875 attributes [opt] = assignment-expression
11877 Returns the expression that should be tested. */
11880 cp_parser_condition (cp_parser
* parser
)
11882 cp_decl_specifier_seq type_specifiers
;
11883 const char *saved_message
;
11884 int declares_class_or_enum
;
11886 /* Try the declaration first. */
11887 cp_parser_parse_tentatively (parser
);
11888 /* New types are not allowed in the type-specifier-seq for a
11890 saved_message
= parser
->type_definition_forbidden_message
;
11891 parser
->type_definition_forbidden_message
11892 = G_("types may not be defined in conditions");
11893 /* Parse the type-specifier-seq. */
11894 cp_parser_decl_specifier_seq (parser
,
11895 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11897 &declares_class_or_enum
);
11898 /* Restore the saved message. */
11899 parser
->type_definition_forbidden_message
= saved_message
;
11901 cp_parser_maybe_commit_to_declaration (parser
,
11902 type_specifiers
.any_specifiers_p
);
11904 /* If all is well, we might be looking at a declaration. */
11905 if (!cp_parser_error_occurred (parser
))
11908 tree asm_specification
;
11910 cp_declarator
*declarator
;
11911 tree initializer
= NULL_TREE
;
11912 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11914 /* Parse the declarator. */
11915 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11916 /*ctor_dtor_or_conv_p=*/NULL
,
11917 /*parenthesized_p=*/NULL
,
11918 /*member_p=*/false,
11919 /*friend_p=*/false);
11920 /* Parse the attributes. */
11921 attributes
= cp_parser_attributes_opt (parser
);
11922 /* Parse the asm-specification. */
11923 asm_specification
= cp_parser_asm_specification_opt (parser
);
11924 /* If the next token is not an `=' or '{', then we might still be
11925 looking at an expression. For example:
11929 looks like a decl-specifier-seq and a declarator -- but then
11930 there is no `=', so this is an expression. */
11931 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11932 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11933 cp_parser_simulate_error (parser
);
11935 /* If we did see an `=' or '{', then we are looking at a declaration
11937 if (cp_parser_parse_definitely (parser
))
11940 bool non_constant_p
= false;
11941 int flags
= LOOKUP_ONLYCONVERTING
;
11943 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11944 return error_mark_node
;
11946 /* Create the declaration. */
11947 decl
= start_decl (declarator
, &type_specifiers
,
11948 /*initialized_p=*/true,
11949 attributes
, /*prefix_attributes=*/NULL_TREE
,
11952 /* Parse the initializer. */
11953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11955 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11956 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11959 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11961 /* Consume the `='. */
11962 cp_lexer_consume_token (parser
->lexer
);
11963 initializer
= cp_parser_initializer_clause (parser
,
11968 cp_parser_error (parser
, "expected initializer");
11969 initializer
= error_mark_node
;
11971 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11974 /* Process the initializer. */
11975 cp_finish_decl (decl
,
11976 initializer
, !non_constant_p
,
11981 pop_scope (pushed_scope
);
11983 return convert_from_reference (decl
);
11986 /* If we didn't even get past the declarator successfully, we are
11987 definitely not looking at a declaration. */
11989 cp_parser_abort_tentative_parse (parser
);
11991 /* Otherwise, we are looking at an expression. */
11992 return cp_parser_expression (parser
);
11995 /* Parses a for-statement or range-for-statement until the closing ')',
11999 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12001 tree init
, scope
, decl
;
12004 /* Begin the for-statement. */
12005 scope
= begin_for_scope (&init
);
12007 /* Parse the initialization. */
12008 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12011 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12014 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12018 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12019 unsigned short unroll
)
12021 /* Normal for loop */
12022 tree condition
= NULL_TREE
;
12023 tree expression
= NULL_TREE
;
12026 stmt
= begin_for_stmt (scope
, init
);
12027 /* The init-statement has already been parsed in
12028 cp_parser_init_statement, so no work is needed here. */
12029 finish_init_stmt (stmt
);
12031 /* If there's a condition, process it. */
12032 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12033 condition
= cp_parser_condition (parser
);
12036 cp_parser_error (parser
, "missing loop condition in loop with "
12037 "%<GCC ivdep%> pragma");
12038 condition
= error_mark_node
;
12042 cp_parser_error (parser
, "missing loop condition in loop with "
12043 "%<GCC unroll%> pragma");
12044 condition
= error_mark_node
;
12046 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12047 /* Look for the `;'. */
12048 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12050 /* If there's an expression, process it. */
12051 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12052 expression
= cp_parser_expression (parser
);
12053 finish_for_expr (expression
, stmt
);
12058 /* Tries to parse a range-based for-statement:
12061 decl-specifier-seq declarator : expression
12063 The decl-specifier-seq declarator and the `:' are already parsed by
12064 cp_parser_init_statement. If processing_template_decl it returns a
12065 newly created RANGE_FOR_STMT; if not, it is converted to a
12066 regular FOR_STMT. */
12069 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12070 bool ivdep
, unsigned short unroll
, bool is_omp
)
12072 tree stmt
, range_expr
;
12073 auto_vec
<cxx_binding
*, 16> bindings
;
12074 auto_vec
<tree
, 16> names
;
12075 tree decomp_first_name
= NULL_TREE
;
12076 unsigned int decomp_cnt
= 0;
12078 /* Get the range declaration momentarily out of the way so that
12079 the range expression doesn't clash with it. */
12080 if (range_decl
!= error_mark_node
)
12082 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12084 tree v
= DECL_VALUE_EXPR (range_decl
);
12085 /* For decomposition declaration get all of the corresponding
12086 declarations out of the way. */
12087 if (TREE_CODE (v
) == ARRAY_REF
12088 && VAR_P (TREE_OPERAND (v
, 0))
12089 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12091 tree d
= range_decl
;
12092 range_decl
= TREE_OPERAND (v
, 0);
12093 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12094 decomp_first_name
= d
;
12095 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12097 tree name
= DECL_NAME (d
);
12098 names
.safe_push (name
);
12099 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12100 IDENTIFIER_BINDING (name
)
12101 = IDENTIFIER_BINDING (name
)->previous
;
12105 if (names
.is_empty ())
12107 tree name
= DECL_NAME (range_decl
);
12108 names
.safe_push (name
);
12109 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12110 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12116 bool expr_non_constant_p
;
12117 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12120 range_expr
= cp_parser_expression (parser
);
12122 /* Put the range declaration(s) back into scope. */
12123 for (unsigned int i
= 0; i
< names
.length (); i
++)
12125 cxx_binding
*binding
= bindings
[i
];
12126 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12127 IDENTIFIER_BINDING (names
[i
]) = binding
;
12130 /* finish_omp_for has its own code for the following, so just
12131 return the range_expr instead. */
12135 /* If in template, STMT is converted to a normal for-statement
12136 at instantiation. If not, it is done just ahead. */
12137 if (processing_template_decl
)
12139 if (check_for_bare_parameter_packs (range_expr
))
12140 range_expr
= error_mark_node
;
12141 stmt
= begin_range_for_stmt (scope
, init
);
12143 RANGE_FOR_IVDEP (stmt
) = 1;
12145 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12146 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12147 if (!type_dependent_expression_p (range_expr
)
12148 /* do_auto_deduction doesn't mess with template init-lists. */
12149 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12150 do_range_for_auto_deduction (range_decl
, range_expr
);
12154 stmt
= begin_for_stmt (scope
, init
);
12155 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12156 decomp_first_name
, decomp_cnt
, ivdep
,
12162 /* Subroutine of cp_convert_range_for: given the initializer expression,
12163 builds up the range temporary. */
12166 build_range_temp (tree range_expr
)
12168 tree range_type
, range_temp
;
12170 /* Find out the type deduced by the declaration
12171 `auto &&__range = range_expr'. */
12172 range_type
= cp_build_reference_type (make_auto (), true);
12173 range_type
= do_auto_deduction (range_type
, range_expr
,
12174 type_uses_auto (range_type
));
12176 /* Create the __range variable. */
12177 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12179 TREE_USED (range_temp
) = 1;
12180 DECL_ARTIFICIAL (range_temp
) = 1;
12185 /* Used by cp_parser_range_for in template context: we aren't going to
12186 do a full conversion yet, but we still need to resolve auto in the
12187 type of the for-range-declaration if present. This is basically
12188 a shortcut version of cp_convert_range_for. */
12191 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12193 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12196 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12197 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12198 iter_type
= (cp_parser_perform_range_for_lookup
12199 (range_temp
, &begin_dummy
, &end_dummy
));
12202 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12204 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12206 tf_warning_or_error
);
12207 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12208 iter_decl
, auto_node
);
12213 /* Converts a range-based for-statement into a normal
12214 for-statement, as per the definition.
12216 for (RANGE_DECL : RANGE_EXPR)
12219 should be equivalent to:
12222 auto &&__range = RANGE_EXPR;
12223 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12227 RANGE_DECL = *__begin;
12232 If RANGE_EXPR is an array:
12233 BEGIN_EXPR = __range
12234 END_EXPR = __range + ARRAY_SIZE(__range)
12235 Else if RANGE_EXPR has a member 'begin' or 'end':
12236 BEGIN_EXPR = __range.begin()
12237 END_EXPR = __range.end()
12239 BEGIN_EXPR = begin(__range)
12240 END_EXPR = end(__range);
12242 If __range has a member 'begin' but not 'end', or vice versa, we must
12243 still use the second alternative (it will surely fail, however).
12244 When calling begin()/end() in the third alternative we must use
12245 argument dependent lookup, but always considering 'std' as an associated
12249 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12250 tree decomp_first_name
, unsigned int decomp_cnt
,
12251 bool ivdep
, unsigned short unroll
)
12254 tree iter_type
, begin_expr
, end_expr
;
12255 tree condition
, expression
;
12257 range_expr
= mark_lvalue_use (range_expr
);
12259 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12260 /* If an error happened previously do nothing or else a lot of
12261 unhelpful errors would be issued. */
12262 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12267 if (VAR_P (range_expr
)
12268 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12269 /* Can't bind a reference to an array of runtime bound. */
12270 range_temp
= range_expr
;
12273 range_temp
= build_range_temp (range_expr
);
12274 pushdecl (range_temp
);
12275 cp_finish_decl (range_temp
, range_expr
,
12276 /*is_constant_init*/false, NULL_TREE
,
12277 LOOKUP_ONLYCONVERTING
);
12278 range_temp
= convert_from_reference (range_temp
);
12280 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12281 &begin_expr
, &end_expr
);
12284 /* The new for initialization statement. */
12285 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12287 TREE_USED (begin
) = 1;
12288 DECL_ARTIFICIAL (begin
) = 1;
12290 cp_finish_decl (begin
, begin_expr
,
12291 /*is_constant_init*/false, NULL_TREE
,
12292 LOOKUP_ONLYCONVERTING
);
12294 if (cxx_dialect
>= cxx17
)
12295 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12296 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12297 TREE_USED (end
) = 1;
12298 DECL_ARTIFICIAL (end
) = 1;
12300 cp_finish_decl (end
, end_expr
,
12301 /*is_constant_init*/false, NULL_TREE
,
12302 LOOKUP_ONLYCONVERTING
);
12304 finish_init_stmt (statement
);
12306 /* The new for condition. */
12307 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12310 NULL
, tf_warning_or_error
);
12311 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12313 /* The new increment expression. */
12314 expression
= finish_unary_op_expr (input_location
,
12315 PREINCREMENT_EXPR
, begin
,
12316 tf_warning_or_error
);
12317 finish_for_expr (expression
, statement
);
12319 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12320 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12322 /* The declaration is initialized with *__begin inside the loop body. */
12323 cp_finish_decl (range_decl
,
12324 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12325 tf_warning_or_error
),
12326 /*is_constant_init*/false, NULL_TREE
,
12327 LOOKUP_ONLYCONVERTING
);
12328 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12329 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12334 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12335 We need to solve both at the same time because the method used
12336 depends on the existence of members begin or end.
12337 Returns the type deduced for the iterator expression. */
12340 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12342 if (error_operand_p (range
))
12344 *begin
= *end
= error_mark_node
;
12345 return error_mark_node
;
12348 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12350 error ("range-based %<for%> expression of type %qT "
12351 "has incomplete type", TREE_TYPE (range
));
12352 *begin
= *end
= error_mark_node
;
12353 return error_mark_node
;
12355 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12357 /* If RANGE is an array, we will use pointer arithmetic. */
12358 *begin
= decay_conversion (range
, tf_warning_or_error
);
12359 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12361 array_type_nelts_top (TREE_TYPE (range
)),
12363 return TREE_TYPE (*begin
);
12367 /* If it is not an array, we must do a bit of magic. */
12368 tree id_begin
, id_end
;
12369 tree member_begin
, member_end
;
12371 *begin
= *end
= error_mark_node
;
12373 id_begin
= get_identifier ("begin");
12374 id_end
= get_identifier ("end");
12375 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12376 /*protect=*/2, /*want_type=*/false,
12377 tf_warning_or_error
);
12378 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12379 /*protect=*/2, /*want_type=*/false,
12380 tf_warning_or_error
);
12382 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12384 /* Use the member functions. */
12385 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12386 *end
= cp_parser_range_for_member_function (range
, id_end
);
12390 /* Use global functions with ADL. */
12391 vec
<tree
, va_gc
> *vec
;
12392 vec
= make_tree_vector ();
12394 vec_safe_push (vec
, range
);
12396 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12397 tf_warning_or_error
);
12398 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12399 tf_warning_or_error
);
12400 member_end
= perform_koenig_lookup (id_end
, vec
,
12401 tf_warning_or_error
);
12402 *end
= finish_call_expr (member_end
, &vec
, false, true,
12403 tf_warning_or_error
);
12405 release_tree_vector (vec
);
12408 /* Last common checks. */
12409 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12411 /* If one of the expressions is an error do no more checks. */
12412 *begin
= *end
= error_mark_node
;
12413 return error_mark_node
;
12415 else if (type_dependent_expression_p (*begin
)
12416 || type_dependent_expression_p (*end
))
12417 /* Can happen, when, eg, in a template context, Koenig lookup
12418 can't resolve begin/end (c++/58503). */
12422 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12423 /* The unqualified type of the __begin and __end temporaries should
12424 be the same, as required by the multiple auto declaration. */
12425 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12427 if (cxx_dialect
>= cxx17
12428 && (build_x_binary_op (input_location
, NE_EXPR
,
12429 *begin
, ERROR_MARK
,
12432 != error_mark_node
))
12433 /* P0184R0 allows __begin and __end to have different types,
12434 but make sure they are comparable so we can give a better
12437 error ("inconsistent begin/end types in range-based %<for%> "
12438 "statement: %qT and %qT",
12439 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12446 /* Helper function for cp_parser_perform_range_for_lookup.
12447 Builds a tree for RANGE.IDENTIFIER(). */
12450 cp_parser_range_for_member_function (tree range
, tree identifier
)
12453 vec
<tree
, va_gc
> *vec
;
12455 member
= finish_class_member_access_expr (range
, identifier
,
12456 false, tf_warning_or_error
);
12457 if (member
== error_mark_node
)
12458 return error_mark_node
;
12460 vec
= make_tree_vector ();
12461 res
= finish_call_expr (member
, &vec
,
12462 /*disallow_virtual=*/false,
12463 /*koenig_p=*/false,
12464 tf_warning_or_error
);
12465 release_tree_vector (vec
);
12469 /* Parse an iteration-statement.
12471 iteration-statement:
12472 while ( condition ) statement
12473 do statement while ( expression ) ;
12474 for ( init-statement condition [opt] ; expression [opt] )
12477 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12480 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12481 unsigned short unroll
)
12486 unsigned char in_statement
;
12487 token_indent_info guard_tinfo
;
12489 /* Peek at the next token. */
12490 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12492 return error_mark_node
;
12494 guard_tinfo
= get_token_indent_info (token
);
12496 /* Remember whether or not we are already within an iteration
12498 in_statement
= parser
->in_statement
;
12500 /* See what kind of keyword it is. */
12501 keyword
= token
->keyword
;
12508 /* Begin the while-statement. */
12509 statement
= begin_while_stmt ();
12510 /* Look for the `('. */
12511 matching_parens parens
;
12512 parens
.require_open (parser
);
12513 /* Parse the condition. */
12514 condition
= cp_parser_condition (parser
);
12515 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12516 /* Look for the `)'. */
12517 parens
.require_close (parser
);
12518 /* Parse the dependent statement. */
12519 parser
->in_statement
= IN_ITERATION_STMT
;
12520 bool prev
= note_iteration_stmt_body_start ();
12521 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12522 note_iteration_stmt_body_end (prev
);
12523 parser
->in_statement
= in_statement
;
12524 /* We're done with the while-statement. */
12525 finish_while_stmt (statement
);
12533 /* Begin the do-statement. */
12534 statement
= begin_do_stmt ();
12535 /* Parse the body of the do-statement. */
12536 parser
->in_statement
= IN_ITERATION_STMT
;
12537 bool prev
= note_iteration_stmt_body_start ();
12538 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12539 note_iteration_stmt_body_end (prev
);
12540 parser
->in_statement
= in_statement
;
12541 finish_do_body (statement
);
12542 /* Look for the `while' keyword. */
12543 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12544 /* Look for the `('. */
12545 matching_parens parens
;
12546 parens
.require_open (parser
);
12547 /* Parse the expression. */
12548 expression
= cp_parser_expression (parser
);
12549 /* We're done with the do-statement. */
12550 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12551 /* Look for the `)'. */
12552 parens
.require_close (parser
);
12553 /* Look for the `;'. */
12554 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12560 /* Look for the `('. */
12561 matching_parens parens
;
12562 parens
.require_open (parser
);
12564 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12566 /* Look for the `)'. */
12567 parens
.require_close (parser
);
12569 /* Parse the body of the for-statement. */
12570 parser
->in_statement
= IN_ITERATION_STMT
;
12571 bool prev
= note_iteration_stmt_body_start ();
12572 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12573 note_iteration_stmt_body_end (prev
);
12574 parser
->in_statement
= in_statement
;
12576 /* We're done with the for-statement. */
12577 finish_for_stmt (statement
);
12582 cp_parser_error (parser
, "expected iteration-statement");
12583 statement
= error_mark_node
;
12590 /* Parse a init-statement or the declarator of a range-based-for.
12591 Returns true if a range-based-for declaration is seen.
12594 expression-statement
12595 simple-declaration */
12598 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12600 /* If the next token is a `;', then we have an empty
12601 expression-statement. Grammatically, this is also a
12602 simple-declaration, but an invalid one, because it does not
12603 declare anything. Therefore, if we did not handle this case
12604 specially, we would issue an error message about an invalid
12606 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12608 bool is_range_for
= false;
12609 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12611 /* Try to parse the init-statement. */
12612 if (cp_parser_range_based_for_with_init_p (parser
))
12615 cp_parser_parse_tentatively (parser
);
12616 /* Parse the declaration. */
12617 cp_parser_simple_declaration (parser
,
12618 /*function_definition_allowed_p=*/false,
12620 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12621 if (!cp_parser_parse_definitely (parser
))
12622 /* That didn't work, try to parse it as an expression-statement. */
12623 cp_parser_expression_statement (parser
, NULL_TREE
);
12625 if (cxx_dialect
< cxx2a
)
12627 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12628 "range-based %<for%> loops with initializer only "
12629 "available with -std=c++2a or -std=gnu++2a");
12630 *decl
= error_mark_node
;
12634 /* A colon is used in range-based for. */
12635 parser
->colon_corrects_to_scope_p
= false;
12637 /* We're going to speculatively look for a declaration, falling back
12638 to an expression, if necessary. */
12639 cp_parser_parse_tentatively (parser
);
12640 /* Parse the declaration. */
12641 cp_parser_simple_declaration (parser
,
12642 /*function_definition_allowed_p=*/false,
12644 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12647 /* It is a range-for, consume the ':'. */
12648 cp_lexer_consume_token (parser
->lexer
);
12649 is_range_for
= true;
12650 if (cxx_dialect
< cxx11
)
12651 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12652 "range-based %<for%> loops only available with "
12653 "-std=c++11 or -std=gnu++11");
12656 /* The ';' is not consumed yet because we told
12657 cp_parser_simple_declaration not to. */
12658 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12660 if (cp_parser_parse_definitely (parser
))
12661 return is_range_for
;
12662 /* If the tentative parse failed, then we shall need to look for an
12663 expression-statement. */
12665 /* If we are here, it is an expression-statement. */
12666 cp_parser_expression_statement (parser
, NULL_TREE
);
12670 /* Parse a jump-statement.
12675 return expression [opt] ;
12676 return braced-init-list ;
12682 goto * expression ;
12684 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12687 cp_parser_jump_statement (cp_parser
* parser
)
12689 tree statement
= error_mark_node
;
12692 unsigned char in_statement
;
12694 /* Peek at the next token. */
12695 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12697 return error_mark_node
;
12699 /* See what kind of keyword it is. */
12700 keyword
= token
->keyword
;
12704 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12705 switch (in_statement
)
12708 error_at (token
->location
, "break statement not within loop or switch");
12711 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12712 || in_statement
== IN_ITERATION_STMT
);
12713 statement
= finish_break_stmt ();
12714 if (in_statement
== IN_ITERATION_STMT
)
12715 break_maybe_infinite_loop ();
12718 error_at (token
->location
, "invalid exit from OpenMP structured block");
12721 error_at (token
->location
, "break statement used with OpenMP for loop");
12724 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12728 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12731 error_at (token
->location
, "continue statement not within a loop");
12733 /* Fall through. */
12734 case IN_ITERATION_STMT
:
12736 statement
= finish_continue_stmt ();
12739 error_at (token
->location
, "invalid exit from OpenMP structured block");
12742 gcc_unreachable ();
12744 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12750 bool expr_non_constant_p
;
12752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12754 cp_lexer_set_source_position (parser
->lexer
);
12755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12756 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12758 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12759 expr
= cp_parser_expression (parser
);
12761 /* If the next token is a `;', then there is no
12764 /* Build the return-statement. */
12765 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12766 /* Don't deduce from a discarded return statement. */;
12768 statement
= finish_return_stmt (expr
);
12769 /* Look for the final `;'. */
12770 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12775 if (parser
->in_function_body
12776 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12778 error ("%<goto%> in %<constexpr%> function");
12779 cp_function_chain
->invalid_constexpr
= true;
12782 /* Create the goto-statement. */
12783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12785 /* Issue a warning about this use of a GNU extension. */
12786 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12787 /* Consume the '*' token. */
12788 cp_lexer_consume_token (parser
->lexer
);
12789 /* Parse the dependent expression. */
12790 finish_goto_stmt (cp_parser_expression (parser
));
12793 finish_goto_stmt (cp_parser_identifier (parser
));
12794 /* Look for the final `;'. */
12795 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12799 cp_parser_error (parser
, "expected jump-statement");
12806 /* Parse a declaration-statement.
12808 declaration-statement:
12809 block-declaration */
12812 cp_parser_declaration_statement (cp_parser
* parser
)
12816 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12817 p
= obstack_alloc (&declarator_obstack
, 0);
12819 /* Parse the block-declaration. */
12820 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12822 /* Free any declarators allocated. */
12823 obstack_free (&declarator_obstack
, p
);
12826 /* Some dependent statements (like `if (cond) statement'), are
12827 implicitly in their own scope. In other words, if the statement is
12828 a single statement (as opposed to a compound-statement), it is
12829 none-the-less treated as if it were enclosed in braces. Any
12830 declarations appearing in the dependent statement are out of scope
12831 after control passes that point. This function parses a statement,
12832 but ensures that is in its own scope, even if it is not a
12833 compound-statement.
12835 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12836 is a (possibly labeled) if statement which is not enclosed in
12837 braces and has an else clause. This is used to implement
12840 CHAIN is a vector of if-else-if conditions. This is used to implement
12843 Returns the new statement. */
12846 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12847 const token_indent_info
&guard_tinfo
,
12851 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12852 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12853 token_indent_info body_tinfo
12854 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12859 /* Mark if () ; with a special NOP_EXPR. */
12860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12862 cp_lexer_consume_token (parser
->lexer
);
12863 statement
= add_stmt (build_empty_stmt (body_loc
));
12865 if (guard_tinfo
.keyword
== RID_IF
12866 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12867 warning_at (body_loc
, OPT_Wempty_body
,
12868 "suggest braces around empty body in an %<if%> statement");
12869 else if (guard_tinfo
.keyword
== RID_ELSE
)
12870 warning_at (body_loc
, OPT_Wempty_body
,
12871 "suggest braces around empty body in an %<else%> statement");
12873 /* if a compound is opened, we simply parse the statement directly. */
12874 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12875 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12876 /* If the token is not a `{', then we must take special action. */
12879 /* Create a compound-statement. */
12880 statement
= begin_compound_stmt (0);
12881 /* Parse the dependent-statement. */
12882 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12883 &body_loc_after_labels
);
12884 /* Finish the dummy compound-statement. */
12885 finish_compound_stmt (statement
);
12888 token_indent_info next_tinfo
12889 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12890 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12892 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12893 && next_tinfo
.type
!= CPP_SEMICOLON
)
12894 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12895 guard_tinfo
.location
, guard_tinfo
.keyword
);
12897 /* Return the statement. */
12901 /* For some dependent statements (like `while (cond) statement'), we
12902 have already created a scope. Therefore, even if the dependent
12903 statement is a compound-statement, we do not want to create another
12907 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12908 const token_indent_info
&guard_tinfo
)
12910 /* If the token is a `{', then we must take special action. */
12911 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12913 token_indent_info body_tinfo
12914 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12915 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12917 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12918 &loc_after_labels
);
12919 token_indent_info next_tinfo
12920 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12921 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12923 if (loc_after_labels
!= UNKNOWN_LOCATION
12924 && next_tinfo
.type
!= CPP_SEMICOLON
)
12925 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12926 guard_tinfo
.location
,
12927 guard_tinfo
.keyword
);
12931 /* Avoid calling cp_parser_compound_statement, so that we
12932 don't create a new scope. Do everything else by hand. */
12933 matching_braces braces
;
12934 braces
.require_open (parser
);
12935 /* If the next keyword is `__label__' we have a label declaration. */
12936 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12937 cp_parser_label_declaration (parser
);
12938 /* Parse an (optional) statement-seq. */
12939 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12940 braces
.require_close (parser
);
12944 /* Declarations [gram.dcl.dcl] */
12946 /* Parse an optional declaration-sequence.
12950 declaration-seq declaration */
12953 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12957 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12959 if (token
->type
== CPP_CLOSE_BRACE
12960 || token
->type
== CPP_EOF
)
12963 cp_parser_toplevel_declaration (parser
);
12967 /* Parse a declaration.
12971 function-definition
12972 template-declaration
12973 explicit-instantiation
12974 explicit-specialization
12975 linkage-specification
12976 namespace-definition
12984 __extension__ declaration */
12987 cp_parser_declaration (cp_parser
* parser
)
12991 int saved_pedantic
;
12993 tree attributes
= NULL_TREE
;
12995 /* Check for the `__extension__' keyword. */
12996 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12998 /* Parse the qualified declaration. */
12999 cp_parser_declaration (parser
);
13000 /* Restore the PEDANTIC flag. */
13001 pedantic
= saved_pedantic
;
13006 /* Try to figure out what kind of declaration is present. */
13007 token1
= *cp_lexer_peek_token (parser
->lexer
);
13009 if (token1
.type
!= CPP_EOF
)
13010 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13013 token2
.type
= CPP_EOF
;
13014 token2
.keyword
= RID_MAX
;
13017 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13018 p
= obstack_alloc (&declarator_obstack
, 0);
13020 /* If the next token is `extern' and the following token is a string
13021 literal, then we have a linkage specification. */
13022 if (token1
.keyword
== RID_EXTERN
13023 && cp_parser_is_pure_string_literal (&token2
))
13024 cp_parser_linkage_specification (parser
);
13025 /* If the next token is `template', then we have either a template
13026 declaration, an explicit instantiation, or an explicit
13028 else if (token1
.keyword
== RID_TEMPLATE
)
13030 /* `template <>' indicates a template specialization. */
13031 if (token2
.type
== CPP_LESS
13032 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13033 cp_parser_explicit_specialization (parser
);
13034 /* `template <' indicates a template declaration. */
13035 else if (token2
.type
== CPP_LESS
)
13036 cp_parser_template_declaration (parser
, /*member_p=*/false);
13037 /* Anything else must be an explicit instantiation. */
13039 cp_parser_explicit_instantiation (parser
);
13041 /* If the next token is `export', then we have a template
13043 else if (token1
.keyword
== RID_EXPORT
)
13044 cp_parser_template_declaration (parser
, /*member_p=*/false);
13045 /* If the next token is `extern', 'static' or 'inline' and the one
13046 after that is `template', we have a GNU extended explicit
13047 instantiation directive. */
13048 else if (cp_parser_allow_gnu_extensions_p (parser
)
13049 && (token1
.keyword
== RID_EXTERN
13050 || token1
.keyword
== RID_STATIC
13051 || token1
.keyword
== RID_INLINE
)
13052 && token2
.keyword
== RID_TEMPLATE
)
13053 cp_parser_explicit_instantiation (parser
);
13054 /* If the next token is `namespace', check for a named or unnamed
13055 namespace definition. */
13056 else if (token1
.keyword
== RID_NAMESPACE
13057 && (/* A named namespace definition. */
13058 (token2
.type
== CPP_NAME
13059 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13061 || (token2
.type
== CPP_OPEN_SQUARE
13062 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13063 == CPP_OPEN_SQUARE
)
13064 /* An unnamed namespace definition. */
13065 || token2
.type
== CPP_OPEN_BRACE
13066 || token2
.keyword
== RID_ATTRIBUTE
))
13067 cp_parser_namespace_definition (parser
);
13068 /* An inline (associated) namespace definition. */
13069 else if (token1
.keyword
== RID_INLINE
13070 && token2
.keyword
== RID_NAMESPACE
)
13071 cp_parser_namespace_definition (parser
);
13072 /* Objective-C++ declaration/definition. */
13073 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13074 cp_parser_objc_declaration (parser
, NULL_TREE
);
13075 else if (c_dialect_objc ()
13076 && token1
.keyword
== RID_ATTRIBUTE
13077 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13078 cp_parser_objc_declaration (parser
, attributes
);
13079 /* At this point we may have a template declared by a concept
13081 else if (flag_concepts
13082 && cp_parser_template_declaration_after_export (parser
,
13083 /*member_p=*/false))
13086 /* Try to parse a block-declaration, or a function-definition. */
13087 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13089 /* Free any declarators allocated. */
13090 obstack_free (&declarator_obstack
, p
);
13093 /* Parse a namespace-scope declaration. */
13096 cp_parser_toplevel_declaration (cp_parser
* parser
)
13098 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13100 if (token
->type
== CPP_PRAGMA
)
13101 /* A top-level declaration can consist solely of a #pragma. A
13102 nested declaration cannot, so this is done here and not in
13103 cp_parser_declaration. (A #pragma at block scope is
13104 handled in cp_parser_statement.) */
13105 cp_parser_pragma (parser
, pragma_external
, NULL
);
13106 else if (token
->type
== CPP_SEMICOLON
)
13108 /* A declaration consisting of a single semicolon is
13109 invalid. Allow it unless we're being pedantic. */
13110 cp_lexer_consume_token (parser
->lexer
);
13111 if (!in_system_header_at (input_location
))
13112 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13115 /* Parse the declaration itself. */
13116 cp_parser_declaration (parser
);
13119 /* Parse a block-declaration.
13124 namespace-alias-definition
13131 __extension__ block-declaration
13136 static_assert-declaration
13138 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13139 part of a declaration-statement. */
13142 cp_parser_block_declaration (cp_parser
*parser
,
13146 int saved_pedantic
;
13148 /* Check for the `__extension__' keyword. */
13149 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13151 /* Parse the qualified declaration. */
13152 cp_parser_block_declaration (parser
, statement_p
);
13153 /* Restore the PEDANTIC flag. */
13154 pedantic
= saved_pedantic
;
13159 /* Peek at the next token to figure out which kind of declaration is
13161 token1
= cp_lexer_peek_token (parser
->lexer
);
13163 /* If the next keyword is `asm', we have an asm-definition. */
13164 if (token1
->keyword
== RID_ASM
)
13167 cp_parser_commit_to_tentative_parse (parser
);
13168 cp_parser_asm_definition (parser
);
13170 /* If the next keyword is `namespace', we have a
13171 namespace-alias-definition. */
13172 else if (token1
->keyword
== RID_NAMESPACE
)
13173 cp_parser_namespace_alias_definition (parser
);
13174 /* If the next keyword is `using', we have a
13175 using-declaration, a using-directive, or an alias-declaration. */
13176 else if (token1
->keyword
== RID_USING
)
13181 cp_parser_commit_to_tentative_parse (parser
);
13182 /* If the token after `using' is `namespace', then we have a
13183 using-directive. */
13184 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13185 if (token2
->keyword
== RID_NAMESPACE
)
13186 cp_parser_using_directive (parser
);
13187 /* If the second token after 'using' is '=', then we have an
13188 alias-declaration. */
13189 else if (cxx_dialect
>= cxx11
13190 && token2
->type
== CPP_NAME
13191 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13192 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13193 cp_parser_alias_declaration (parser
);
13194 /* Otherwise, it's a using-declaration. */
13196 cp_parser_using_declaration (parser
,
13197 /*access_declaration_p=*/false);
13199 /* If the next keyword is `__label__' we have a misplaced label
13201 else if (token1
->keyword
== RID_LABEL
)
13203 cp_lexer_consume_token (parser
->lexer
);
13204 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13205 cp_parser_skip_to_end_of_statement (parser
);
13206 /* If the next token is now a `;', consume it. */
13207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13208 cp_lexer_consume_token (parser
->lexer
);
13210 /* If the next token is `static_assert' we have a static assertion. */
13211 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13212 cp_parser_static_assert (parser
, /*member_p=*/false);
13213 /* Anything else must be a simple-declaration. */
13215 cp_parser_simple_declaration (parser
, !statement_p
,
13216 /*maybe_range_for_decl*/NULL
);
13219 /* Parse a simple-declaration.
13221 simple-declaration:
13222 decl-specifier-seq [opt] init-declarator-list [opt] ;
13223 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13224 brace-or-equal-initializer ;
13226 init-declarator-list:
13228 init-declarator-list , init-declarator
13230 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13231 function-definition as a simple-declaration.
13233 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13234 parsed declaration if it is an uninitialized single declarator not followed
13235 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13236 if present, will not be consumed. */
13239 cp_parser_simple_declaration (cp_parser
* parser
,
13240 bool function_definition_allowed_p
,
13241 tree
*maybe_range_for_decl
)
13243 cp_decl_specifier_seq decl_specifiers
;
13244 int declares_class_or_enum
;
13245 bool saw_declarator
;
13246 location_t comma_loc
= UNKNOWN_LOCATION
;
13247 location_t init_loc
= UNKNOWN_LOCATION
;
13249 if (maybe_range_for_decl
)
13250 *maybe_range_for_decl
= NULL_TREE
;
13252 /* Defer access checks until we know what is being declared; the
13253 checks for names appearing in the decl-specifier-seq should be
13254 done as if we were in the scope of the thing being declared. */
13255 push_deferring_access_checks (dk_deferred
);
13257 /* Parse the decl-specifier-seq. We have to keep track of whether
13258 or not the decl-specifier-seq declares a named class or
13259 enumeration type, since that is the only case in which the
13260 init-declarator-list is allowed to be empty.
13264 In a simple-declaration, the optional init-declarator-list can be
13265 omitted only when declaring a class or enumeration, that is when
13266 the decl-specifier-seq contains either a class-specifier, an
13267 elaborated-type-specifier, or an enum-specifier. */
13268 cp_parser_decl_specifier_seq (parser
,
13269 CP_PARSER_FLAGS_OPTIONAL
,
13271 &declares_class_or_enum
);
13272 /* We no longer need to defer access checks. */
13273 stop_deferring_access_checks ();
13275 /* In a block scope, a valid declaration must always have a
13276 decl-specifier-seq. By not trying to parse declarators, we can
13277 resolve the declaration/expression ambiguity more quickly. */
13278 if (!function_definition_allowed_p
13279 && !decl_specifiers
.any_specifiers_p
)
13281 cp_parser_error (parser
, "expected declaration");
13285 /* If the next two tokens are both identifiers, the code is
13286 erroneous. The usual cause of this situation is code like:
13290 where "T" should name a type -- but does not. */
13291 if (!decl_specifiers
.any_type_specifiers_p
13292 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13294 /* If parsing tentatively, we should commit; we really are
13295 looking at a declaration. */
13296 cp_parser_commit_to_tentative_parse (parser
);
13301 cp_parser_maybe_commit_to_declaration (parser
,
13302 decl_specifiers
.any_specifiers_p
);
13304 /* Look for C++17 decomposition declaration. */
13305 for (size_t n
= 1; ; n
++)
13306 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13307 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13309 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13310 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13311 && decl_specifiers
.any_specifiers_p
)
13314 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13315 maybe_range_for_decl
,
13318 /* The next token should be either a `,' or a `;'. */
13319 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13320 /* If it's a `;', we are done. */
13321 if (token
->type
== CPP_SEMICOLON
)
13323 else if (maybe_range_for_decl
)
13325 if (*maybe_range_for_decl
== NULL_TREE
)
13326 *maybe_range_for_decl
= error_mark_node
;
13329 /* Anything else is an error. */
13332 /* If we have already issued an error message we don't need
13333 to issue another one. */
13334 if ((decl
!= error_mark_node
13335 && DECL_INITIAL (decl
) != error_mark_node
)
13336 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13337 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13338 /* Skip tokens until we reach the end of the statement. */
13339 cp_parser_skip_to_end_of_statement (parser
);
13340 /* If the next token is now a `;', consume it. */
13341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13342 cp_lexer_consume_token (parser
->lexer
);
13350 bool auto_specifier_p
;
13351 /* NULL_TREE if both variable and function declaration are allowed,
13352 error_mark_node if function declaration are not allowed and
13353 a FUNCTION_DECL that should be diagnosed if it is followed by
13354 variable declarations. */
13355 tree auto_function_declaration
;
13357 last_type
= NULL_TREE
;
13359 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13360 auto_function_declaration
= NULL_TREE
;
13362 /* Keep going until we hit the `;' at the end of the simple
13364 saw_declarator
= false;
13365 while (cp_lexer_next_token_is_not (parser
->lexer
,
13369 bool function_definition_p
;
13371 tree auto_result
= NULL_TREE
;
13373 if (saw_declarator
)
13375 /* If we are processing next declarator, comma is expected */
13376 token
= cp_lexer_peek_token (parser
->lexer
);
13377 gcc_assert (token
->type
== CPP_COMMA
);
13378 cp_lexer_consume_token (parser
->lexer
);
13379 if (maybe_range_for_decl
)
13381 *maybe_range_for_decl
= error_mark_node
;
13382 if (comma_loc
== UNKNOWN_LOCATION
)
13383 comma_loc
= token
->location
;
13387 saw_declarator
= true;
13389 /* Parse the init-declarator. */
13390 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13392 function_definition_allowed_p
,
13393 /*member_p=*/false,
13394 declares_class_or_enum
,
13395 &function_definition_p
,
13396 maybe_range_for_decl
,
13399 /* If an error occurred while parsing tentatively, exit quickly.
13400 (That usually happens when in the body of a function; each
13401 statement is treated as a declaration-statement until proven
13403 if (cp_parser_error_occurred (parser
))
13406 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13408 /* If the init-declarator-list contains more than one
13409 init-declarator, they shall all form declarations of
13411 if (auto_function_declaration
== NULL_TREE
)
13412 auto_function_declaration
13413 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13414 else if (TREE_CODE (decl
) == FUNCTION_DECL
13415 || auto_function_declaration
!= error_mark_node
)
13417 error_at (decl_specifiers
.locations
[ds_type_spec
],
13418 "non-variable %qD in declaration with more than one "
13419 "declarator with placeholder type",
13420 TREE_CODE (decl
) == FUNCTION_DECL
13421 ? decl
: auto_function_declaration
);
13422 auto_function_declaration
= error_mark_node
;
13427 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13430 && last_type
!= error_mark_node
13431 && !same_type_p (auto_result
, last_type
))
13433 /* If the list of declarators contains more than one declarator,
13434 the type of each declared variable is determined as described
13435 above. If the type deduced for the template parameter U is not
13436 the same in each deduction, the program is ill-formed. */
13437 error_at (decl_specifiers
.locations
[ds_type_spec
],
13438 "inconsistent deduction for %qT: %qT and then %qT",
13439 decl_specifiers
.type
, last_type
, auto_result
);
13440 last_type
= error_mark_node
;
13443 last_type
= auto_result
;
13446 /* Handle function definitions specially. */
13447 if (function_definition_p
)
13449 /* If the next token is a `,', then we are probably
13450 processing something like:
13454 which is erroneous. */
13455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13458 error_at (token
->location
,
13460 " declarations and function-definitions is forbidden");
13462 /* Otherwise, we're done with the list of declarators. */
13465 pop_deferring_access_checks ();
13469 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13470 *maybe_range_for_decl
= decl
;
13471 /* The next token should be either a `,' or a `;'. */
13472 token
= cp_lexer_peek_token (parser
->lexer
);
13473 /* If it's a `,', there are more declarators to come. */
13474 if (token
->type
== CPP_COMMA
)
13475 /* will be consumed next time around */;
13476 /* If it's a `;', we are done. */
13477 else if (token
->type
== CPP_SEMICOLON
)
13479 else if (maybe_range_for_decl
)
13481 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13482 permerror (decl_specifiers
.locations
[ds_type_spec
],
13483 "types may not be defined in a for-range-declaration");
13486 /* Anything else is an error. */
13489 /* If we have already issued an error message we don't need
13490 to issue another one. */
13491 if ((decl
!= error_mark_node
13492 && DECL_INITIAL (decl
) != error_mark_node
)
13493 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13494 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13495 /* Skip tokens until we reach the end of the statement. */
13496 cp_parser_skip_to_end_of_statement (parser
);
13497 /* If the next token is now a `;', consume it. */
13498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13499 cp_lexer_consume_token (parser
->lexer
);
13502 /* After the first time around, a function-definition is not
13503 allowed -- even if it was OK at first. For example:
13508 function_definition_allowed_p
= false;
13511 /* Issue an error message if no declarators are present, and the
13512 decl-specifier-seq does not itself declare a class or
13513 enumeration: [dcl.dcl]/3. */
13514 if (!saw_declarator
)
13516 if (cp_parser_declares_only_class_p (parser
))
13518 if (!declares_class_or_enum
13519 && decl_specifiers
.type
13520 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13521 /* Ensure an error is issued anyway when finish_decltype_type,
13522 called via cp_parser_decl_specifier_seq, returns a class or
13523 an enumeration (c++/51786). */
13524 decl_specifiers
.type
= NULL_TREE
;
13525 shadow_tag (&decl_specifiers
);
13527 /* Perform any deferred access checks. */
13528 perform_deferred_access_checks (tf_warning_or_error
);
13531 /* Consume the `;'. */
13533 if (!maybe_range_for_decl
)
13534 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13535 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13537 if (init_loc
!= UNKNOWN_LOCATION
)
13538 error_at (init_loc
, "initializer in range-based %<for%> loop");
13539 if (comma_loc
!= UNKNOWN_LOCATION
)
13540 error_at (comma_loc
,
13541 "multiple declarations in range-based %<for%> loop");
13545 pop_deferring_access_checks ();
13548 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13549 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13553 cp_parser_decomposition_declaration (cp_parser
*parser
,
13554 cp_decl_specifier_seq
*decl_specifiers
,
13555 tree
*maybe_range_for_decl
,
13556 location_t
*init_loc
)
13558 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13559 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13560 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13562 /* Parse the identifier-list. */
13563 auto_vec
<cp_expr
, 10> v
;
13564 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13567 cp_expr e
= cp_parser_identifier (parser
);
13568 if (e
.get_value () == error_mark_node
)
13571 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13573 cp_lexer_consume_token (parser
->lexer
);
13576 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13577 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13579 end_loc
= UNKNOWN_LOCATION
;
13580 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13583 cp_lexer_consume_token (parser
->lexer
);
13586 cp_parser_skip_to_end_of_statement (parser
);
13587 return error_mark_node
;
13591 if (cxx_dialect
< cxx17
)
13592 pedwarn (loc
, 0, "structured bindings only available with "
13593 "-std=c++17 or -std=gnu++17");
13596 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13597 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13598 declarator
->id_loc
= loc
;
13599 if (ref_qual
!= REF_QUAL_NONE
)
13600 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13601 ref_qual
== REF_QUAL_RVALUE
,
13603 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13604 NULL_TREE
, decl_specifiers
->attributes
,
13606 tree orig_decl
= decl
;
13610 cp_decl_specifier_seq decl_specs
;
13611 clear_decl_specs (&decl_specs
);
13612 decl_specs
.type
= make_auto ();
13614 FOR_EACH_VEC_ELT (v
, i
, e
)
13617 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13618 sfk_none
, e
.get_location ());
13621 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13622 declarator
->id_loc
= e
.get_location ();
13624 tree elt_pushed_scope
;
13625 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13626 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13627 if (decl2
== error_mark_node
)
13628 decl
= error_mark_node
;
13629 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13631 /* Ensure we've diagnosed redeclaration if we aren't creating
13633 gcc_assert (errorcount
);
13634 decl
= error_mark_node
;
13638 if (elt_pushed_scope
)
13639 pop_scope (elt_pushed_scope
);
13644 error_at (loc
, "empty structured binding declaration");
13645 decl
= error_mark_node
;
13648 if (maybe_range_for_decl
== NULL
13649 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13651 bool non_constant_p
= false, is_direct_init
= false;
13652 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13653 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13655 if (initializer
== NULL_TREE
13656 || (TREE_CODE (initializer
) == TREE_LIST
13657 && TREE_CHAIN (initializer
))
13659 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13660 && CONSTRUCTOR_NELTS (initializer
) != 1))
13662 error_at (loc
, "invalid initializer for structured binding "
13664 initializer
= error_mark_node
;
13667 if (decl
!= error_mark_node
)
13669 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13670 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13671 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13672 cp_finish_decomp (decl
, prev
, v
.length ());
13675 else if (decl
!= error_mark_node
)
13677 *maybe_range_for_decl
= prev
;
13678 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13679 the underlying DECL. */
13680 cp_finish_decomp (decl
, prev
, v
.length ());
13684 pop_scope (pushed_scope
);
13686 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13688 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13689 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13695 /* Parse a decl-specifier-seq.
13697 decl-specifier-seq:
13698 decl-specifier-seq [opt] decl-specifier
13699 decl-specifier attribute-specifier-seq [opt] (C++11)
13702 storage-class-specifier
13713 Concepts Extension:
13718 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13720 The parser flags FLAGS is used to control type-specifier parsing.
13722 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13725 1: one of the decl-specifiers is an elaborated-type-specifier
13726 (i.e., a type declaration)
13727 2: one of the decl-specifiers is an enum-specifier or a
13728 class-specifier (i.e., a type definition)
13733 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13734 cp_parser_flags flags
,
13735 cp_decl_specifier_seq
*decl_specs
,
13736 int* declares_class_or_enum
)
13738 bool constructor_possible_p
= !parser
->in_declarator_p
;
13739 bool found_decl_spec
= false;
13740 cp_token
*start_token
= NULL
;
13743 /* Clear DECL_SPECS. */
13744 clear_decl_specs (decl_specs
);
13746 /* Assume no class or enumeration type is declared. */
13747 *declares_class_or_enum
= 0;
13749 /* Keep reading specifiers until there are no more to read. */
13752 bool constructor_p
;
13756 /* Peek at the next token. */
13757 token
= cp_lexer_peek_token (parser
->lexer
);
13759 /* Save the first token of the decl spec list for error
13762 start_token
= token
;
13763 /* Handle attributes. */
13764 if (cp_next_tokens_can_be_attribute_p (parser
))
13766 /* Parse the attributes. */
13767 tree attrs
= cp_parser_attributes_opt (parser
);
13769 /* In a sequence of declaration specifiers, c++11 attributes
13770 appertain to the type that precede them. In that case
13773 The attribute-specifier-seq affects the type only for
13774 the declaration it appears in, not other declarations
13775 involving the same type.
13777 But for now let's force the user to position the
13778 attribute either at the beginning of the declaration or
13779 after the declarator-id, which would clearly mean that it
13780 applies to the declarator. */
13781 if (cxx11_attribute_p (attrs
))
13783 if (!found_decl_spec
)
13784 /* The c++11 attribute is at the beginning of the
13785 declaration. It appertains to the entity being
13789 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13791 /* This is an attribute following a
13792 class-specifier. */
13793 if (decl_specs
->type_definition_p
)
13794 warn_misplaced_attr_for_class_type (token
->location
,
13800 decl_specs
->std_attributes
13801 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13802 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13803 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13809 decl_specs
->attributes
13810 = attr_chainon (decl_specs
->attributes
, attrs
);
13811 if (decl_specs
->locations
[ds_attribute
] == 0)
13812 decl_specs
->locations
[ds_attribute
] = token
->location
;
13815 /* Assume we will find a decl-specifier keyword. */
13816 found_decl_spec
= true;
13817 /* If the next token is an appropriate keyword, we can simply
13818 add it to the list. */
13819 switch (token
->keyword
)
13825 if (!at_class_scope_p ())
13827 gcc_rich_location
richloc (token
->location
);
13828 richloc
.add_fixit_remove ();
13829 error_at (&richloc
, "%<friend%> used outside of class");
13830 cp_lexer_purge_token (parser
->lexer
);
13835 /* Consume the token. */
13836 cp_lexer_consume_token (parser
->lexer
);
13840 case RID_CONSTEXPR
:
13842 cp_lexer_consume_token (parser
->lexer
);
13847 cp_lexer_consume_token (parser
->lexer
);
13850 /* function-specifier:
13857 cp_parser_function_specifier_opt (parser
, decl_specs
);
13864 /* Consume the token. */
13865 cp_lexer_consume_token (parser
->lexer
);
13866 /* A constructor declarator cannot appear in a typedef. */
13867 constructor_possible_p
= false;
13868 /* The "typedef" keyword can only occur in a declaration; we
13869 may as well commit at this point. */
13870 cp_parser_commit_to_tentative_parse (parser
);
13872 if (decl_specs
->storage_class
!= sc_none
)
13873 decl_specs
->conflicting_specifiers_p
= true;
13876 /* storage-class-specifier:
13886 if (cxx_dialect
== cxx98
)
13888 /* Consume the token. */
13889 cp_lexer_consume_token (parser
->lexer
);
13891 /* Complain about `auto' as a storage specifier, if
13892 we're complaining about C++0x compatibility. */
13893 gcc_rich_location
richloc (token
->location
);
13894 richloc
.add_fixit_remove ();
13895 warning_at (&richloc
, OPT_Wc__11_compat
,
13896 "%<auto%> changes meaning in C++11; "
13897 "please remove it");
13899 /* Set the storage class anyway. */
13900 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13904 /* C++0x auto type-specifier. */
13905 found_decl_spec
= false;
13912 /* Consume the token. */
13913 cp_lexer_consume_token (parser
->lexer
);
13914 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13918 /* Consume the token. */
13920 cp_lexer_consume_token (parser
->lexer
);
13924 /* We did not yet find a decl-specifier yet. */
13925 found_decl_spec
= false;
13929 if (found_decl_spec
13930 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13931 && token
->keyword
!= RID_CONSTEXPR
)
13932 error ("decl-specifier invalid in condition");
13934 if (found_decl_spec
13935 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13936 && token
->keyword
!= RID_MUTABLE
13937 && token
->keyword
!= RID_CONSTEXPR
)
13938 error_at (token
->location
, "%qD invalid in lambda",
13939 ridpointers
[token
->keyword
]);
13942 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13944 /* Constructors are a special case. The `S' in `S()' is not a
13945 decl-specifier; it is the beginning of the declarator. */
13947 = (!found_decl_spec
13948 && constructor_possible_p
13949 && (cp_parser_constructor_declarator_p
13950 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13952 /* If we don't have a DECL_SPEC yet, then we must be looking at
13953 a type-specifier. */
13954 if (!found_decl_spec
&& !constructor_p
)
13956 int decl_spec_declares_class_or_enum
;
13957 bool is_cv_qualifier
;
13961 = cp_parser_type_specifier (parser
, flags
,
13963 /*is_declaration=*/true,
13964 &decl_spec_declares_class_or_enum
,
13966 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13968 /* If this type-specifier referenced a user-defined type
13969 (a typedef, class-name, etc.), then we can't allow any
13970 more such type-specifiers henceforth.
13974 The longest sequence of decl-specifiers that could
13975 possibly be a type name is taken as the
13976 decl-specifier-seq of a declaration. The sequence shall
13977 be self-consistent as described below.
13981 As a general rule, at most one type-specifier is allowed
13982 in the complete decl-specifier-seq of a declaration. The
13983 only exceptions are the following:
13985 -- const or volatile can be combined with any other
13988 -- signed or unsigned can be combined with char, long,
13996 void g (const int Pc);
13998 Here, Pc is *not* part of the decl-specifier seq; it's
13999 the declarator. Therefore, once we see a type-specifier
14000 (other than a cv-qualifier), we forbid any additional
14001 user-defined types. We *do* still allow things like `int
14002 int' to be considered a decl-specifier-seq, and issue the
14003 error message later. */
14004 if (type_spec
&& !is_cv_qualifier
)
14005 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14006 /* A constructor declarator cannot follow a type-specifier. */
14009 constructor_possible_p
= false;
14010 found_decl_spec
= true;
14011 if (!is_cv_qualifier
)
14012 decl_specs
->any_type_specifiers_p
= true;
14014 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14015 error_at (token
->location
, "type-specifier invalid in lambda");
14019 /* If we still do not have a DECL_SPEC, then there are no more
14020 decl-specifiers. */
14021 if (!found_decl_spec
)
14024 decl_specs
->any_specifiers_p
= true;
14025 /* After we see one decl-specifier, further decl-specifiers are
14026 always optional. */
14027 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14030 /* Don't allow a friend specifier with a class definition. */
14031 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14032 && (*declares_class_or_enum
& 2))
14033 error_at (decl_specs
->locations
[ds_friend
],
14034 "class definition may not be declared a friend");
14037 /* Parse an (optional) storage-class-specifier.
14039 storage-class-specifier:
14048 storage-class-specifier:
14051 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14054 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14056 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14059 if (cxx_dialect
!= cxx98
)
14061 /* Fall through for C++98. */
14062 gcc_fallthrough ();
14069 /* Consume the token. */
14070 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14077 /* Parse an (optional) function-specifier.
14079 function-specifier:
14085 explicit(constant-expression)
14087 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14088 Updates DECL_SPECS, if it is non-NULL. */
14091 cp_parser_function_specifier_opt (cp_parser
* parser
,
14092 cp_decl_specifier_seq
*decl_specs
)
14094 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14095 switch (token
->keyword
)
14098 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14102 /* 14.5.2.3 [temp.mem]
14104 A member function template shall not be virtual. */
14105 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14106 && current_class_type
)
14107 error_at (token
->location
, "templates may not be %<virtual%>");
14109 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14114 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14115 /* If we see '(', it's C++20 explicit(bool). */
14117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14119 matching_parens parens
;
14120 parens
.consume_open (parser
);
14122 /* New types are not allowed in an explicit-specifier. */
14123 const char *saved_message
14124 = parser
->type_definition_forbidden_message
;
14125 parser
->type_definition_forbidden_message
14126 = G_("types may not be defined in explicit-specifier");
14128 if (cxx_dialect
< cxx2a
)
14129 pedwarn (token
->location
, 0,
14130 "%<explicit(bool)%> only available with -std=c++2a "
14131 "or -std=gnu++2a");
14133 /* Parse the constant-expression. */
14134 expr
= cp_parser_constant_expression (parser
);
14136 /* Restore the saved message. */
14137 parser
->type_definition_forbidden_message
= saved_message
;
14138 parens
.require_close (parser
);
14141 /* The explicit-specifier explicit without a constant-expression is
14142 equivalent to the explicit-specifier explicit(true). */
14143 expr
= boolean_true_node
;
14146 "the constant-expression, if supplied, shall be a contextually
14147 converted constant expression of type bool." */
14148 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14149 /* We could evaluate it -- mark the decl as appropriate. */
14150 if (expr
== boolean_true_node
)
14151 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14152 else if (expr
== boolean_false_node
)
14153 /* Don't mark the decl as explicit. */;
14154 else if (decl_specs
)
14155 /* The expression was value-dependent. Remember it so that we can
14156 substitute it later. */
14157 decl_specs
->explicit_specifier
= expr
;
14165 /* Consume the token. */
14166 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14169 /* Parse a linkage-specification.
14171 linkage-specification:
14172 extern string-literal { declaration-seq [opt] }
14173 extern string-literal declaration */
14176 cp_parser_linkage_specification (cp_parser
* parser
)
14180 /* Look for the `extern' keyword. */
14181 cp_token
*extern_token
14182 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14184 /* Look for the string-literal. */
14185 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14186 linkage
= cp_parser_string_literal (parser
, false, false);
14188 /* Transform the literal into an identifier. If the literal is a
14189 wide-character string, or contains embedded NULs, then we can't
14190 handle it as the user wants. */
14191 if (strlen (TREE_STRING_POINTER (linkage
))
14192 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14194 cp_parser_error (parser
, "invalid linkage-specification");
14195 /* Assume C++ linkage. */
14196 linkage
= lang_name_cplusplus
;
14199 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14201 /* We're now using the new linkage. */
14202 push_lang_context (linkage
);
14204 /* Preserve the location of the the innermost linkage specification,
14205 tracking the locations of nested specifications via a local. */
14206 location_t saved_location
14207 = parser
->innermost_linkage_specification_location
;
14208 /* Construct a location ranging from the start of the "extern" to
14209 the end of the string-literal, with the caret at the start, e.g.:
14213 parser
->innermost_linkage_specification_location
14214 = make_location (extern_token
->location
,
14215 extern_token
->location
,
14216 get_finish (string_token
->location
));
14218 /* If the next token is a `{', then we're using the first
14220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14222 cp_ensure_no_omp_declare_simd (parser
);
14223 cp_ensure_no_oacc_routine (parser
);
14225 /* Consume the `{' token. */
14226 matching_braces braces
;
14227 braces
.consume_open (parser
);
14228 /* Parse the declarations. */
14229 cp_parser_declaration_seq_opt (parser
);
14230 /* Look for the closing `}'. */
14231 braces
.require_close (parser
);
14233 /* Otherwise, there's just one declaration. */
14236 bool saved_in_unbraced_linkage_specification_p
;
14238 saved_in_unbraced_linkage_specification_p
14239 = parser
->in_unbraced_linkage_specification_p
;
14240 parser
->in_unbraced_linkage_specification_p
= true;
14241 cp_parser_declaration (parser
);
14242 parser
->in_unbraced_linkage_specification_p
14243 = saved_in_unbraced_linkage_specification_p
;
14246 /* We're done with the linkage-specification. */
14247 pop_lang_context ();
14249 /* Restore location of parent linkage specification, if any. */
14250 parser
->innermost_linkage_specification_location
= saved_location
;
14253 /* Parse a static_assert-declaration.
14255 static_assert-declaration:
14256 static_assert ( constant-expression , string-literal ) ;
14257 static_assert ( constant-expression ) ; (C++17)
14259 If MEMBER_P, this static_assert is a class member. */
14262 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14265 location_t token_loc
;
14269 /* Peek at the `static_assert' token so we can keep track of exactly
14270 where the static assertion started. */
14271 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14273 /* Look for the `static_assert' keyword. */
14274 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14278 /* We know we are in a static assertion; commit to any tentative
14280 if (cp_parser_parsing_tentatively (parser
))
14281 cp_parser_commit_to_tentative_parse (parser
);
14283 /* Parse the `(' starting the static assertion condition. */
14284 matching_parens parens
;
14285 parens
.require_open (parser
);
14287 /* Parse the constant-expression. Allow a non-constant expression
14288 here in order to give better diagnostics in finish_static_assert. */
14290 cp_parser_constant_expression (parser
,
14291 /*allow_non_constant_p=*/true,
14292 /*non_constant_p=*/&dummy
);
14294 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14296 if (cxx_dialect
< cxx17
)
14297 pedwarn (input_location
, OPT_Wpedantic
,
14298 "static_assert without a message "
14299 "only available with -std=c++17 or -std=gnu++17");
14301 cp_lexer_consume_token (parser
->lexer
);
14302 message
= build_string (1, "");
14303 TREE_TYPE (message
) = char_array_type_node
;
14304 fix_string_type (message
);
14308 /* Parse the separating `,'. */
14309 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14311 /* Parse the string-literal message. */
14312 message
= cp_parser_string_literal (parser
,
14313 /*translate=*/false,
14316 /* A `)' completes the static assertion. */
14317 if (!parens
.require_close (parser
))
14318 cp_parser_skip_to_closing_parenthesis (parser
,
14319 /*recovering=*/true,
14320 /*or_comma=*/false,
14321 /*consume_paren=*/true);
14324 /* A semicolon terminates the declaration. */
14325 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14327 /* Get the location for the static assertion. Use that of the
14328 condition if available, otherwise, use that of the "static_assert"
14330 location_t assert_loc
= condition
.get_location ();
14331 if (assert_loc
== UNKNOWN_LOCATION
)
14332 assert_loc
= token_loc
;
14334 /* Complete the static assertion, which may mean either processing
14335 the static assert now or saving it for template instantiation. */
14336 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14339 /* Parse the expression in decltype ( expression ). */
14342 cp_parser_decltype_expr (cp_parser
*parser
,
14343 bool &id_expression_or_member_access_p
)
14345 cp_token
*id_expr_start_token
;
14348 /* Since we're going to preserve any side-effects from this parse, set up a
14349 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14350 in the expression. */
14351 tentative_firewall
firewall (parser
);
14353 /* First, try parsing an id-expression. */
14354 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14355 cp_parser_parse_tentatively (parser
);
14356 expr
= cp_parser_id_expression (parser
,
14357 /*template_keyword_p=*/false,
14358 /*check_dependency_p=*/true,
14359 /*template_p=*/NULL
,
14360 /*declarator_p=*/false,
14361 /*optional_p=*/false);
14363 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14365 bool non_integral_constant_expression_p
= false;
14366 tree id_expression
= expr
;
14368 const char *error_msg
;
14370 if (identifier_p (expr
))
14371 /* Lookup the name we got back from the id-expression. */
14372 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14373 id_expr_start_token
->location
);
14375 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14376 /* A template without args is not a complete id-expression. */
14377 expr
= error_mark_node
;
14380 && expr
!= error_mark_node
14381 && TREE_CODE (expr
) != TYPE_DECL
14382 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14383 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14384 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14386 /* Complete lookup of the id-expression. */
14387 expr
= (finish_id_expression
14388 (id_expression
, expr
, parser
->scope
, &idk
,
14389 /*integral_constant_expression_p=*/false,
14390 /*allow_non_integral_constant_expression_p=*/true,
14391 &non_integral_constant_expression_p
,
14392 /*template_p=*/false,
14394 /*address_p=*/false,
14395 /*template_arg_p=*/false,
14397 id_expr_start_token
->location
));
14399 if (expr
== error_mark_node
)
14400 /* We found an id-expression, but it was something that we
14401 should not have found. This is an error, not something
14402 we can recover from, so note that we found an
14403 id-expression and we'll recover as gracefully as
14405 id_expression_or_member_access_p
= true;
14409 && expr
!= error_mark_node
14410 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14411 /* We have an id-expression. */
14412 id_expression_or_member_access_p
= true;
14415 if (!id_expression_or_member_access_p
)
14417 /* Abort the id-expression parse. */
14418 cp_parser_abort_tentative_parse (parser
);
14420 /* Parsing tentatively, again. */
14421 cp_parser_parse_tentatively (parser
);
14423 /* Parse a class member access. */
14424 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14425 /*cast_p=*/false, /*decltype*/true,
14426 /*member_access_only_p=*/true, NULL
);
14429 && expr
!= error_mark_node
14430 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14431 /* We have an id-expression. */
14432 id_expression_or_member_access_p
= true;
14435 if (id_expression_or_member_access_p
)
14436 /* We have parsed the complete id-expression or member access. */
14437 cp_parser_parse_definitely (parser
);
14440 /* Abort our attempt to parse an id-expression or member access
14442 cp_parser_abort_tentative_parse (parser
);
14444 /* Commit to the tentative_firewall so we get syntax errors. */
14445 cp_parser_commit_to_tentative_parse (parser
);
14447 /* Parse a full expression. */
14448 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14449 /*decltype_p=*/true);
14455 /* Parse a `decltype' type. Returns the type.
14457 simple-type-specifier:
14458 decltype ( expression )
14460 decltype ( auto ) */
14463 cp_parser_decltype (cp_parser
*parser
)
14465 bool id_expression_or_member_access_p
= false;
14466 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14468 if (start_token
->type
== CPP_DECLTYPE
)
14470 /* Already parsed. */
14471 cp_lexer_consume_token (parser
->lexer
);
14472 return saved_checks_value (start_token
->u
.tree_check_value
);
14475 /* Look for the `decltype' token. */
14476 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14477 return error_mark_node
;
14479 /* Parse the opening `('. */
14480 matching_parens parens
;
14481 if (!parens
.require_open (parser
))
14482 return error_mark_node
;
14484 push_deferring_access_checks (dk_deferred
);
14486 tree expr
= NULL_TREE
;
14488 if (cxx_dialect
>= cxx14
14489 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14490 /* decltype (auto) */
14491 cp_lexer_consume_token (parser
->lexer
);
14494 /* decltype (expression) */
14496 /* Types cannot be defined in a `decltype' expression. Save away the
14497 old message and set the new one. */
14498 const char *saved_message
= parser
->type_definition_forbidden_message
;
14499 parser
->type_definition_forbidden_message
14500 = G_("types may not be defined in %<decltype%> expressions");
14502 /* The restrictions on constant-expressions do not apply inside
14503 decltype expressions. */
14504 bool saved_integral_constant_expression_p
14505 = parser
->integral_constant_expression_p
;
14506 bool saved_non_integral_constant_expression_p
14507 = parser
->non_integral_constant_expression_p
;
14508 parser
->integral_constant_expression_p
= false;
14510 /* Within a parenthesized expression, a `>' token is always
14511 the greater-than operator. */
14512 bool saved_greater_than_is_operator_p
14513 = parser
->greater_than_is_operator_p
;
14514 parser
->greater_than_is_operator_p
= true;
14516 /* Do not actually evaluate the expression. */
14517 ++cp_unevaluated_operand
;
14519 /* Do not warn about problems with the expression. */
14520 ++c_inhibit_evaluation_warnings
;
14522 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14524 /* Go back to evaluating expressions. */
14525 --cp_unevaluated_operand
;
14526 --c_inhibit_evaluation_warnings
;
14528 /* The `>' token might be the end of a template-id or
14529 template-parameter-list now. */
14530 parser
->greater_than_is_operator_p
14531 = saved_greater_than_is_operator_p
;
14533 /* Restore the old message and the integral constant expression
14535 parser
->type_definition_forbidden_message
= saved_message
;
14536 parser
->integral_constant_expression_p
14537 = saved_integral_constant_expression_p
;
14538 parser
->non_integral_constant_expression_p
14539 = saved_non_integral_constant_expression_p
;
14542 /* Parse to the closing `)'. */
14543 if (!parens
.require_close (parser
))
14545 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14546 /*consume_paren=*/true);
14547 pop_deferring_access_checks ();
14548 return error_mark_node
;
14554 expr
= make_decltype_auto ();
14555 AUTO_IS_DECLTYPE (expr
) = true;
14558 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14559 tf_warning_or_error
);
14561 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14563 start_token
->type
= CPP_DECLTYPE
;
14564 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14565 start_token
->u
.tree_check_value
->value
= expr
;
14566 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14567 start_token
->keyword
= RID_MAX
;
14568 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14570 pop_to_parent_deferring_access_checks ();
14575 /* Special member functions [gram.special] */
14577 /* Parse a conversion-function-id.
14579 conversion-function-id:
14580 operator conversion-type-id
14582 Returns an IDENTIFIER_NODE representing the operator. */
14585 cp_parser_conversion_function_id (cp_parser
* parser
)
14589 tree saved_qualifying_scope
;
14590 tree saved_object_scope
;
14591 tree pushed_scope
= NULL_TREE
;
14593 /* Look for the `operator' token. */
14594 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14595 return error_mark_node
;
14596 /* When we parse the conversion-type-id, the current scope will be
14597 reset. However, we need that information in able to look up the
14598 conversion function later, so we save it here. */
14599 saved_scope
= parser
->scope
;
14600 saved_qualifying_scope
= parser
->qualifying_scope
;
14601 saved_object_scope
= parser
->object_scope
;
14602 /* We must enter the scope of the class so that the names of
14603 entities declared within the class are available in the
14604 conversion-type-id. For example, consider:
14611 S::operator I() { ... }
14613 In order to see that `I' is a type-name in the definition, we
14614 must be in the scope of `S'. */
14616 pushed_scope
= push_scope (saved_scope
);
14617 /* Parse the conversion-type-id. */
14618 type
= cp_parser_conversion_type_id (parser
);
14619 /* Leave the scope of the class, if any. */
14621 pop_scope (pushed_scope
);
14622 /* Restore the saved scope. */
14623 parser
->scope
= saved_scope
;
14624 parser
->qualifying_scope
= saved_qualifying_scope
;
14625 parser
->object_scope
= saved_object_scope
;
14626 /* If the TYPE is invalid, indicate failure. */
14627 if (type
== error_mark_node
)
14628 return error_mark_node
;
14629 return make_conv_op_name (type
);
14632 /* Parse a conversion-type-id:
14634 conversion-type-id:
14635 type-specifier-seq conversion-declarator [opt]
14637 Returns the TYPE specified. */
14640 cp_parser_conversion_type_id (cp_parser
* parser
)
14643 cp_decl_specifier_seq type_specifiers
;
14644 cp_declarator
*declarator
;
14645 tree type_specified
;
14646 const char *saved_message
;
14648 /* Parse the attributes. */
14649 attributes
= cp_parser_attributes_opt (parser
);
14651 saved_message
= parser
->type_definition_forbidden_message
;
14652 parser
->type_definition_forbidden_message
14653 = G_("types may not be defined in a conversion-type-id");
14655 /* Parse the type-specifiers. */
14656 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14657 /*is_trailing_return=*/false,
14660 parser
->type_definition_forbidden_message
= saved_message
;
14662 /* If that didn't work, stop. */
14663 if (type_specifiers
.type
== error_mark_node
)
14664 return error_mark_node
;
14665 /* Parse the conversion-declarator. */
14666 declarator
= cp_parser_conversion_declarator_opt (parser
);
14668 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14669 /*initialized=*/0, &attributes
);
14671 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14673 /* Don't give this error when parsing tentatively. This happens to
14674 work because we always parse this definitively once. */
14675 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14676 && type_uses_auto (type_specified
))
14678 if (cxx_dialect
< cxx14
)
14680 error ("invalid use of %<auto%> in conversion operator");
14681 return error_mark_node
;
14683 else if (template_parm_scope_p ())
14684 warning (0, "use of %<auto%> in member template "
14685 "conversion operator can never be deduced");
14688 return type_specified
;
14691 /* Parse an (optional) conversion-declarator.
14693 conversion-declarator:
14694 ptr-operator conversion-declarator [opt]
14698 static cp_declarator
*
14699 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14701 enum tree_code code
;
14702 tree class_type
, std_attributes
= NULL_TREE
;
14703 cp_cv_quals cv_quals
;
14705 /* We don't know if there's a ptr-operator next, or not. */
14706 cp_parser_parse_tentatively (parser
);
14707 /* Try the ptr-operator. */
14708 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14710 /* If it worked, look for more conversion-declarators. */
14711 if (cp_parser_parse_definitely (parser
))
14713 cp_declarator
*declarator
;
14715 /* Parse another optional declarator. */
14716 declarator
= cp_parser_conversion_declarator_opt (parser
);
14718 declarator
= cp_parser_make_indirect_declarator
14719 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14727 /* Parse an (optional) ctor-initializer.
14730 : mem-initializer-list */
14733 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14735 /* If the next token is not a `:', then there is no
14736 ctor-initializer. */
14737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14739 /* Do default initialization of any bases and members. */
14740 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14741 finish_mem_initializers (NULL_TREE
);
14745 /* Consume the `:' token. */
14746 cp_lexer_consume_token (parser
->lexer
);
14747 /* And the mem-initializer-list. */
14748 cp_parser_mem_initializer_list (parser
);
14751 /* Parse a mem-initializer-list.
14753 mem-initializer-list:
14754 mem-initializer ... [opt]
14755 mem-initializer ... [opt] , mem-initializer-list */
14758 cp_parser_mem_initializer_list (cp_parser
* parser
)
14760 tree mem_initializer_list
= NULL_TREE
;
14761 tree target_ctor
= error_mark_node
;
14762 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14764 /* Let the semantic analysis code know that we are starting the
14765 mem-initializer-list. */
14766 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14767 error_at (token
->location
,
14768 "only constructors take member initializers");
14770 /* Loop through the list. */
14773 tree mem_initializer
;
14775 token
= cp_lexer_peek_token (parser
->lexer
);
14776 /* Parse the mem-initializer. */
14777 mem_initializer
= cp_parser_mem_initializer (parser
);
14778 /* If the next token is a `...', we're expanding member initializers. */
14779 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14781 || (mem_initializer
!= error_mark_node
14782 && check_for_bare_parameter_packs (TREE_PURPOSE
14783 (mem_initializer
))))
14785 /* Consume the `...'. */
14787 cp_lexer_consume_token (parser
->lexer
);
14789 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14790 can be expanded but members cannot. */
14791 if (mem_initializer
!= error_mark_node
14792 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14794 error_at (token
->location
,
14795 "cannot expand initializer for member %qD",
14796 TREE_PURPOSE (mem_initializer
));
14797 mem_initializer
= error_mark_node
;
14800 /* Construct the pack expansion type. */
14801 if (mem_initializer
!= error_mark_node
)
14802 mem_initializer
= make_pack_expansion (mem_initializer
);
14804 if (target_ctor
!= error_mark_node
14805 && mem_initializer
!= error_mark_node
)
14807 error ("mem-initializer for %qD follows constructor delegation",
14808 TREE_PURPOSE (mem_initializer
));
14809 mem_initializer
= error_mark_node
;
14811 /* Look for a target constructor. */
14812 if (mem_initializer
!= error_mark_node
14813 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14814 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14816 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14817 if (mem_initializer_list
)
14819 error ("constructor delegation follows mem-initializer for %qD",
14820 TREE_PURPOSE (mem_initializer_list
));
14821 mem_initializer
= error_mark_node
;
14823 target_ctor
= mem_initializer
;
14825 /* Add it to the list, unless it was erroneous. */
14826 if (mem_initializer
!= error_mark_node
)
14828 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14829 mem_initializer_list
= mem_initializer
;
14831 /* If the next token is not a `,', we're done. */
14832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14834 /* Consume the `,' token. */
14835 cp_lexer_consume_token (parser
->lexer
);
14838 /* Perform semantic analysis. */
14839 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14840 finish_mem_initializers (mem_initializer_list
);
14843 /* Parse a mem-initializer.
14846 mem-initializer-id ( expression-list [opt] )
14847 mem-initializer-id braced-init-list
14852 ( expression-list [opt] )
14854 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14855 class) or FIELD_DECL (for a non-static data member) to initialize;
14856 the TREE_VALUE is the expression-list. An empty initialization
14857 list is represented by void_list_node. */
14860 cp_parser_mem_initializer (cp_parser
* parser
)
14862 tree mem_initializer_id
;
14863 tree expression_list
;
14865 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14867 /* Find out what is being initialized. */
14868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14870 permerror (token
->location
,
14871 "anachronistic old-style base class initializer");
14872 mem_initializer_id
= NULL_TREE
;
14876 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14877 if (mem_initializer_id
== error_mark_node
)
14878 return mem_initializer_id
;
14880 member
= expand_member_init (mem_initializer_id
);
14881 if (member
&& !DECL_P (member
))
14882 in_base_initializer
= 1;
14884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14886 bool expr_non_constant_p
;
14887 cp_lexer_set_source_position (parser
->lexer
);
14888 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14889 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14890 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14891 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14895 vec
<tree
, va_gc
> *vec
;
14896 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14898 /*allow_expansion_p=*/true,
14899 /*non_constant_p=*/NULL
);
14901 return error_mark_node
;
14902 expression_list
= build_tree_list_vec (vec
);
14903 release_tree_vector (vec
);
14906 if (expression_list
== error_mark_node
)
14907 return error_mark_node
;
14908 if (!expression_list
)
14909 expression_list
= void_type_node
;
14911 in_base_initializer
= 0;
14913 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14916 /* Parse a mem-initializer-id.
14918 mem-initializer-id:
14919 :: [opt] nested-name-specifier [opt] class-name
14920 decltype-specifier (C++11)
14923 Returns a TYPE indicating the class to be initialized for the first
14924 production (and the second in C++11). Returns an IDENTIFIER_NODE
14925 indicating the data member to be initialized for the last production. */
14928 cp_parser_mem_initializer_id (cp_parser
* parser
)
14930 bool global_scope_p
;
14931 bool nested_name_specifier_p
;
14932 bool template_p
= false;
14935 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14937 /* `typename' is not allowed in this context ([temp.res]). */
14938 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14940 error_at (token
->location
,
14941 "keyword %<typename%> not allowed in this context (a qualified "
14942 "member initializer is implicitly a type)");
14943 cp_lexer_consume_token (parser
->lexer
);
14945 /* Look for the optional `::' operator. */
14947 = (cp_parser_global_scope_opt (parser
,
14948 /*current_scope_valid_p=*/false)
14950 /* Look for the optional nested-name-specifier. The simplest way to
14955 The keyword `typename' is not permitted in a base-specifier or
14956 mem-initializer; in these contexts a qualified name that
14957 depends on a template-parameter is implicitly assumed to be a
14960 is to assume that we have seen the `typename' keyword at this
14962 nested_name_specifier_p
14963 = (cp_parser_nested_name_specifier_opt (parser
,
14964 /*typename_keyword_p=*/true,
14965 /*check_dependency_p=*/true,
14967 /*is_declaration=*/true)
14969 if (nested_name_specifier_p
)
14970 template_p
= cp_parser_optional_template_keyword (parser
);
14971 /* If there is a `::' operator or a nested-name-specifier, then we
14972 are definitely looking for a class-name. */
14973 if (global_scope_p
|| nested_name_specifier_p
)
14974 return cp_parser_class_name (parser
,
14975 /*typename_keyword_p=*/true,
14976 /*template_keyword_p=*/template_p
,
14978 /*check_dependency_p=*/true,
14979 /*class_head_p=*/false,
14980 /*is_declaration=*/true);
14981 /* Otherwise, we could also be looking for an ordinary identifier. */
14982 cp_parser_parse_tentatively (parser
);
14983 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14984 /* Try a decltype-specifier. */
14985 id
= cp_parser_decltype (parser
);
14987 /* Otherwise, try a class-name. */
14988 id
= cp_parser_class_name (parser
,
14989 /*typename_keyword_p=*/true,
14990 /*template_keyword_p=*/false,
14992 /*check_dependency_p=*/true,
14993 /*class_head_p=*/false,
14994 /*is_declaration=*/true);
14995 /* If we found one, we're done. */
14996 if (cp_parser_parse_definitely (parser
))
14998 /* Otherwise, look for an ordinary identifier. */
14999 return cp_parser_identifier (parser
);
15002 /* Overloading [gram.over] */
15004 /* Parse an operator-function-id.
15006 operator-function-id:
15009 Returns an IDENTIFIER_NODE for the operator which is a
15010 human-readable spelling of the identifier, e.g., `operator +'. */
15013 cp_parser_operator_function_id (cp_parser
* parser
)
15015 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15016 /* Look for the `operator' keyword. */
15017 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15018 return error_mark_node
;
15019 /* And then the name of the operator itself. */
15020 return cp_parser_operator (parser
, start_loc
);
15023 /* Return an identifier node for a user-defined literal operator.
15024 The suffix identifier is chained to the operator name identifier. */
15027 cp_literal_operator_id (const char* name
)
15030 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15031 + strlen (name
) + 10);
15032 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15033 identifier
= get_identifier (buffer
);
15038 /* Parse an operator.
15041 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15042 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15043 || ++ -- , ->* -> () []
15050 Returns an IDENTIFIER_NODE for the operator which is a
15051 human-readable spelling of the identifier, e.g., `operator +'. */
15054 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15056 tree id
= NULL_TREE
;
15060 /* Peek at the next token. */
15061 token
= cp_lexer_peek_token (parser
->lexer
);
15063 location_t end_loc
= token
->location
;
15065 /* Figure out which operator we have. */
15066 enum tree_code op
= ERROR_MARK
;
15067 bool assop
= false;
15068 bool consumed
= false;
15069 switch (token
->type
)
15073 /* The keyword should be either `new' or `delete'. */
15074 if (token
->keyword
== RID_NEW
)
15076 else if (token
->keyword
== RID_DELETE
)
15081 /* Consume the `new' or `delete' token. */
15082 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15084 /* Peek at the next token. */
15085 token
= cp_lexer_peek_token (parser
->lexer
);
15086 /* If it's a `[' token then this is the array variant of the
15088 if (token
->type
== CPP_OPEN_SQUARE
)
15090 /* Consume the `[' token. */
15091 cp_lexer_consume_token (parser
->lexer
);
15092 /* Look for the `]' token. */
15093 if (cp_token
*close_token
15094 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15095 end_loc
= close_token
->location
;
15096 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15115 op
= TRUNC_DIV_EXPR
;
15119 op
= TRUNC_MOD_EXPR
;
15139 op
= TRUTH_NOT_EXPR
;
15172 op
= TRUNC_DIV_EXPR
;
15177 op
= TRUNC_MOD_EXPR
;
15203 case CPP_LSHIFT_EQ
:
15208 case CPP_RSHIFT_EQ
:
15225 case CPP_GREATER_EQ
:
15230 op
= TRUTH_ANDIF_EXPR
;
15234 op
= TRUTH_ORIF_EXPR
;
15237 case CPP_PLUS_PLUS
:
15238 op
= POSTINCREMENT_EXPR
;
15241 case CPP_MINUS_MINUS
:
15242 op
= PREDECREMENT_EXPR
;
15246 op
= COMPOUND_EXPR
;
15249 case CPP_DEREF_STAR
:
15254 op
= COMPONENT_REF
;
15257 case CPP_OPEN_PAREN
:
15259 /* Consume the `('. */
15260 matching_parens parens
;
15261 parens
.consume_open (parser
);
15262 /* Look for the matching `)'. */
15263 token
= parens
.require_close (parser
);
15265 end_loc
= token
->location
;
15271 case CPP_OPEN_SQUARE
:
15272 /* Consume the `['. */
15273 cp_lexer_consume_token (parser
->lexer
);
15274 /* Look for the matching `]'. */
15275 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15277 end_loc
= token
->location
;
15282 case CPP_UTF8STRING
:
15283 case CPP_UTF8STRING_USERDEF
:
15290 case CPP_STRING_USERDEF
:
15291 case CPP_WSTRING_USERDEF
:
15292 case CPP_STRING16_USERDEF
:
15293 case CPP_STRING32_USERDEF
:
15299 if (cxx_dialect
== cxx98
)
15300 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15302 /* Consume the string. */
15303 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15304 /*wide_ok=*/true, /*lookup_udlit=*/false);
15305 if (str
== error_mark_node
)
15306 return error_mark_node
;
15307 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15309 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15310 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15311 end_loc
= str
.get_location ();
15316 /* Look for the suffix identifier. */
15317 token
= cp_lexer_peek_token (parser
->lexer
);
15318 if (token
->type
== CPP_NAME
)
15320 id
= cp_parser_identifier (parser
);
15321 end_loc
= token
->location
;
15323 else if (token
->type
== CPP_KEYWORD
)
15325 error ("unexpected keyword;"
15326 " remove space between quotes and suffix identifier");
15327 return error_mark_node
;
15331 error ("expected suffix identifier");
15332 return error_mark_node
;
15335 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15336 (TREE_TYPE (TREE_TYPE (string_tree
))));
15337 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15340 error ("expected empty string after %<operator%> keyword");
15341 return error_mark_node
;
15343 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15346 error ("invalid encoding prefix in literal operator");
15347 return error_mark_node
;
15349 if (id
!= error_mark_node
)
15351 const char *name
= IDENTIFIER_POINTER (id
);
15352 id
= cp_literal_operator_id (name
);
15354 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15355 return cp_expr (id
, start_loc
);
15359 /* Anything else is an error. */
15363 /* If we have selected an identifier, we need to consume the
15365 if (op
!= ERROR_MARK
)
15367 id
= ovl_op_identifier (assop
, op
);
15369 cp_lexer_consume_token (parser
->lexer
);
15371 /* Otherwise, no valid operator name was present. */
15374 cp_parser_error (parser
, "expected operator");
15375 id
= error_mark_node
;
15378 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15379 return cp_expr (id
, start_loc
);
15382 /* Parse a template-declaration.
15384 template-declaration:
15385 export [opt] template < template-parameter-list > declaration
15387 If MEMBER_P is TRUE, this template-declaration occurs within a
15390 The grammar rule given by the standard isn't correct. What
15391 is really meant is:
15393 template-declaration:
15394 export [opt] template-parameter-list-seq
15395 decl-specifier-seq [opt] init-declarator [opt] ;
15396 export [opt] template-parameter-list-seq
15397 function-definition
15399 template-parameter-list-seq:
15400 template-parameter-list-seq [opt]
15401 template < template-parameter-list >
15403 Concept Extensions:
15405 template-parameter-list-seq:
15406 template < template-parameter-list > requires-clause [opt]
15409 requires logical-or-expression */
15412 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15414 /* Check for `export'. */
15415 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15417 /* Consume the `export' token. */
15418 cp_lexer_consume_token (parser
->lexer
);
15419 /* Warn that we do not support `export'. */
15420 warning (0, "keyword %<export%> not implemented, and will be ignored");
15423 cp_parser_template_declaration_after_export (parser
, member_p
);
15426 /* Parse a template-parameter-list.
15428 template-parameter-list:
15430 template-parameter-list , template-parameter
15432 Returns a TREE_LIST. Each node represents a template parameter.
15433 The nodes are connected via their TREE_CHAINs. */
15436 cp_parser_template_parameter_list (cp_parser
* parser
)
15438 tree parameter_list
= NULL_TREE
;
15440 begin_template_parm_list ();
15442 /* The loop below parses the template parms. We first need to know
15443 the total number of template parms to be able to compute proper
15444 canonical types of each dependent type. So after the loop, when
15445 we know the total number of template parms,
15446 end_template_parm_list computes the proper canonical types and
15447 fixes up the dependent types accordingly. */
15452 bool is_parameter_pack
;
15453 location_t parm_loc
;
15455 /* Parse the template-parameter. */
15456 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15457 parameter
= cp_parser_template_parameter (parser
,
15459 &is_parameter_pack
);
15460 /* Add it to the list. */
15461 if (parameter
!= error_mark_node
)
15462 parameter_list
= process_template_parm (parameter_list
,
15466 is_parameter_pack
);
15469 tree err_parm
= build_tree_list (parameter
, parameter
);
15470 parameter_list
= chainon (parameter_list
, err_parm
);
15473 /* If the next token is not a `,', we're done. */
15474 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15476 /* Otherwise, consume the `,' token. */
15477 cp_lexer_consume_token (parser
->lexer
);
15480 return end_template_parm_list (parameter_list
);
15483 /* Parse a introduction-list.
15486 introduced-parameter
15487 introduction-list , introduced-parameter
15489 introduced-parameter:
15490 ...[opt] identifier
15492 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15493 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15494 WILDCARD_DECL will also have DECL_NAME set and token location in
15495 DECL_SOURCE_LOCATION. */
15498 cp_parser_introduction_list (cp_parser
*parser
)
15500 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15504 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15506 cp_lexer_consume_token (parser
->lexer
);
15508 tree identifier
= cp_parser_identifier (parser
);
15509 if (identifier
== error_mark_node
)
15512 /* Build placeholder. */
15513 tree parm
= build_nt (WILDCARD_DECL
);
15514 DECL_SOURCE_LOCATION (parm
)
15515 = cp_lexer_peek_token (parser
->lexer
)->location
;
15516 DECL_NAME (parm
) = identifier
;
15517 WILDCARD_PACK_P (parm
) = is_pack
;
15518 vec_safe_push (introduction_vec
, parm
);
15520 /* If the next token is not a `,', we're done. */
15521 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15523 /* Otherwise, consume the `,' token. */
15524 cp_lexer_consume_token (parser
->lexer
);
15527 /* Convert the vec into a TREE_VEC. */
15528 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15531 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15532 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15534 release_tree_vector (introduction_vec
);
15535 return introduction_list
;
15538 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15539 is an abstract declarator. */
15541 static inline cp_declarator
*
15542 get_id_declarator (cp_declarator
*declarator
)
15544 cp_declarator
*d
= declarator
;
15545 while (d
&& d
->kind
!= cdk_id
)
15550 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15551 is an abstract declarator. */
15554 get_unqualified_id (cp_declarator
*declarator
)
15556 declarator
= get_id_declarator (declarator
);
15558 return declarator
->u
.id
.unqualified_name
;
15563 /* Returns true if DECL represents a constrained-parameter. */
15566 is_constrained_parameter (tree decl
)
15569 && TREE_CODE (decl
) == TYPE_DECL
15570 && CONSTRAINED_PARM_CONCEPT (decl
)
15571 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15574 /* Returns true if PARM declares a constrained-parameter. */
15577 is_constrained_parameter (cp_parameter_declarator
*parm
)
15579 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15582 /* Check that the type parameter is only a declarator-id, and that its
15583 type is not cv-qualified. */
15586 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15587 cp_parameter_declarator
*parm
)
15589 if (!parm
->declarator
)
15592 if (parm
->declarator
->kind
!= cdk_id
)
15594 cp_parser_error (parser
, "invalid constrained type parameter");
15598 /* Don't allow cv-qualified type parameters. */
15599 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15600 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15602 cp_parser_error (parser
, "cv-qualified type parameter");
15609 /* Finish parsing/processing a template type parameter and checking
15610 various restrictions. */
15613 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15615 cp_parameter_declarator
* parmdecl
)
15617 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15618 return finish_template_type_parm (class_type_node
, id
);
15620 return error_mark_node
;
15624 finish_constrained_template_template_parm (tree proto
, tree id
)
15626 /* FIXME: This should probably be copied, and we may need to adjust
15627 the template parameter depths. */
15628 tree saved_parms
= current_template_parms
;
15629 begin_template_parm_list ();
15630 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15631 end_template_parm_list ();
15633 tree parm
= finish_template_template_parm (class_type_node
, id
);
15634 current_template_parms
= saved_parms
;
15639 /* Finish parsing/processing a template template parameter by borrowing
15640 the template parameter list from the prototype parameter. */
15643 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15646 cp_parameter_declarator
*parmdecl
)
15648 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15649 return error_mark_node
;
15650 return finish_constrained_template_template_parm (proto
, id
);
15653 /* Create a new non-type template parameter from the given PARM
15657 constrained_non_type_template_parm (bool *is_non_type
,
15658 cp_parameter_declarator
*parm
)
15660 *is_non_type
= true;
15661 cp_declarator
*decl
= parm
->declarator
;
15662 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15663 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15664 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15667 /* Build a constrained template parameter based on the PARMDECL
15668 declarator. The type of PARMDECL is the constrained type, which
15669 refers to the prototype template parameter that ultimately
15670 specifies the type of the declared parameter. */
15673 finish_constrained_parameter (cp_parser
*parser
,
15674 cp_parameter_declarator
*parmdecl
,
15676 bool *is_parameter_pack
)
15678 tree decl
= parmdecl
->decl_specifiers
.type
;
15679 tree id
= get_unqualified_id (parmdecl
->declarator
);
15680 tree def
= parmdecl
->default_argument
;
15681 tree proto
= DECL_INITIAL (decl
);
15683 /* A template parameter constrained by a variadic concept shall also
15684 be declared as a template parameter pack. */
15685 bool is_variadic
= template_parameter_pack_p (proto
);
15686 if (is_variadic
&& !*is_parameter_pack
)
15687 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15689 /* Build the parameter. Return an error if the declarator was invalid. */
15691 if (TREE_CODE (proto
) == TYPE_DECL
)
15692 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15693 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15694 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15697 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15698 if (parm
== error_mark_node
)
15699 return error_mark_node
;
15701 /* Finish the parameter decl and create a node attaching the
15702 default argument and constraint. */
15703 parm
= build_tree_list (def
, parm
);
15704 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15709 /* Returns true if the parsed type actually represents the declaration
15710 of a type template-parameter. */
15713 declares_constrained_type_template_parameter (tree type
)
15715 return (is_constrained_parameter (type
)
15716 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15720 /* Returns true if the parsed type actually represents the declaration of
15721 a template template-parameter. */
15724 declares_constrained_template_template_parameter (tree type
)
15726 return (is_constrained_parameter (type
)
15727 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15730 /* Parse a default argument for a type template-parameter.
15731 Note that diagnostics are handled in cp_parser_template_parameter. */
15734 cp_parser_default_type_template_argument (cp_parser
*parser
)
15736 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15738 /* Consume the `=' token. */
15739 cp_lexer_consume_token (parser
->lexer
);
15741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15743 /* Parse the default-argument. */
15744 push_deferring_access_checks (dk_no_deferred
);
15745 tree default_argument
= cp_parser_type_id (parser
);
15746 pop_deferring_access_checks ();
15748 if (flag_concepts
&& type_uses_auto (default_argument
))
15750 error_at (token
->location
,
15751 "invalid use of %<auto%> in default template argument");
15752 return error_mark_node
;
15755 return default_argument
;
15758 /* Parse a default argument for a template template-parameter. */
15761 cp_parser_default_template_template_argument (cp_parser
*parser
)
15763 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15767 /* Consume the `='. */
15768 cp_lexer_consume_token (parser
->lexer
);
15769 /* Parse the id-expression. */
15770 push_deferring_access_checks (dk_no_deferred
);
15771 /* save token before parsing the id-expression, for error
15773 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15774 tree default_argument
15775 = cp_parser_id_expression (parser
,
15776 /*template_keyword_p=*/false,
15777 /*check_dependency_p=*/true,
15778 /*template_p=*/&is_template
,
15779 /*declarator_p=*/false,
15780 /*optional_p=*/false);
15781 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15782 /* If the id-expression was a template-id that refers to
15783 a template-class, we already have the declaration here,
15784 so no further lookup is needed. */
15787 /* Look up the name. */
15789 = cp_parser_lookup_name (parser
, default_argument
,
15791 /*is_template=*/is_template
,
15792 /*is_namespace=*/false,
15793 /*check_dependency=*/true,
15794 /*ambiguous_decls=*/NULL
,
15796 /* See if the default argument is valid. */
15797 default_argument
= check_template_template_default_arg (default_argument
);
15798 pop_deferring_access_checks ();
15799 return default_argument
;
15802 /* Parse a template-parameter.
15804 template-parameter:
15806 parameter-declaration
15808 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15809 the parameter. The TREE_PURPOSE is the default value, if any.
15810 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15811 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15812 set to true iff this parameter is a parameter pack. */
15815 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15816 bool *is_parameter_pack
)
15819 cp_parameter_declarator
*parameter_declarator
;
15822 /* Assume it is a type parameter or a template parameter. */
15823 *is_non_type
= false;
15824 /* Assume it not a parameter pack. */
15825 *is_parameter_pack
= false;
15826 /* Peek at the next token. */
15827 token
= cp_lexer_peek_token (parser
->lexer
);
15828 /* If it is `template', we have a type-parameter. */
15829 if (token
->keyword
== RID_TEMPLATE
)
15830 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15831 /* If it is `class' or `typename' we do not know yet whether it is a
15832 type parameter or a non-type parameter. Consider:
15834 template <typename T, typename T::X X> ...
15838 template <class C, class D*> ...
15840 Here, the first parameter is a type parameter, and the second is
15841 a non-type parameter. We can tell by looking at the token after
15842 the identifier -- if it is a `,', `=', or `>' then we have a type
15844 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15846 /* Peek at the token after `class' or `typename'. */
15847 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15848 /* If it's an ellipsis, we have a template type parameter
15850 if (token
->type
== CPP_ELLIPSIS
)
15851 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15852 /* If it's an identifier, skip it. */
15853 if (token
->type
== CPP_NAME
)
15854 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15855 /* Now, see if the token looks like the end of a template
15857 if (token
->type
== CPP_COMMA
15858 || token
->type
== CPP_EQ
15859 || token
->type
== CPP_GREATER
)
15860 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15863 /* Otherwise, it is a non-type parameter or a constrained parameter.
15867 When parsing a default template-argument for a non-type
15868 template-parameter, the first non-nested `>' is taken as the end
15869 of the template parameter-list rather than a greater-than
15871 parameter_declarator
15872 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15873 /*parenthesized_p=*/NULL
);
15875 if (!parameter_declarator
)
15876 return error_mark_node
;
15878 /* If the parameter declaration is marked as a parameter pack, set
15879 *IS_PARAMETER_PACK to notify the caller. */
15880 if (parameter_declarator
->template_parameter_pack_p
)
15881 *is_parameter_pack
= true;
15883 if (parameter_declarator
->default_argument
)
15885 /* Can happen in some cases of erroneous input (c++/34892). */
15886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15887 /* Consume the `...' for better error recovery. */
15888 cp_lexer_consume_token (parser
->lexer
);
15891 // The parameter may have been constrained.
15892 if (is_constrained_parameter (parameter_declarator
))
15893 return finish_constrained_parameter (parser
,
15894 parameter_declarator
,
15896 is_parameter_pack
);
15898 // Now we're sure that the parameter is a non-type parameter.
15899 *is_non_type
= true;
15901 parm
= grokdeclarator (parameter_declarator
->declarator
,
15902 ¶meter_declarator
->decl_specifiers
,
15903 TPARM
, /*initialized=*/0,
15904 /*attrlist=*/NULL
);
15905 if (parm
== error_mark_node
)
15906 return error_mark_node
;
15908 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15911 /* Parse a type-parameter.
15914 class identifier [opt]
15915 class identifier [opt] = type-id
15916 typename identifier [opt]
15917 typename identifier [opt] = type-id
15918 template < template-parameter-list > class identifier [opt]
15919 template < template-parameter-list > class identifier [opt]
15922 GNU Extension (variadic templates):
15925 class ... identifier [opt]
15926 typename ... identifier [opt]
15928 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15929 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15930 the declaration of the parameter.
15932 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15935 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15940 /* Look for a keyword to tell us what kind of parameter this is. */
15941 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15943 return error_mark_node
;
15945 switch (token
->keyword
)
15951 tree default_argument
;
15953 /* If the next token is an ellipsis, we have a template
15955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15957 /* Consume the `...' token. */
15958 cp_lexer_consume_token (parser
->lexer
);
15959 maybe_warn_variadic_templates ();
15961 *is_parameter_pack
= true;
15964 /* If the next token is an identifier, then it names the
15966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15967 identifier
= cp_parser_identifier (parser
);
15969 identifier
= NULL_TREE
;
15971 /* Create the parameter. */
15972 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15974 /* If the next token is an `=', we have a default argument. */
15975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15978 = cp_parser_default_type_template_argument (parser
);
15980 /* Template parameter packs cannot have default
15982 if (*is_parameter_pack
)
15985 error_at (token
->location
,
15986 "template parameter pack %qD cannot have a "
15987 "default argument", identifier
);
15989 error_at (token
->location
,
15990 "template parameter packs cannot have "
15991 "default arguments");
15992 default_argument
= NULL_TREE
;
15994 else if (check_for_bare_parameter_packs (default_argument
))
15995 default_argument
= error_mark_node
;
15998 default_argument
= NULL_TREE
;
16000 /* Create the combined representation of the parameter and the
16001 default argument. */
16002 parameter
= build_tree_list (default_argument
, parameter
);
16009 tree default_argument
;
16011 /* Look for the `<'. */
16012 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16013 /* Parse the template-parameter-list. */
16014 cp_parser_template_parameter_list (parser
);
16015 /* Look for the `>'. */
16016 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16018 // If template requirements are present, parse them.
16021 tree reqs
= get_shorthand_constraints (current_template_parms
);
16022 if (tree r
= cp_parser_requires_clause_opt (parser
))
16023 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16024 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16027 /* Look for the `class' or 'typename' keywords. */
16028 cp_parser_type_parameter_key (parser
);
16029 /* If the next token is an ellipsis, we have a template
16031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16033 /* Consume the `...' token. */
16034 cp_lexer_consume_token (parser
->lexer
);
16035 maybe_warn_variadic_templates ();
16037 *is_parameter_pack
= true;
16039 /* If the next token is an `=', then there is a
16040 default-argument. If the next token is a `>', we are at
16041 the end of the parameter-list. If the next token is a `,',
16042 then we are at the end of this parameter. */
16043 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16044 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16045 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16047 identifier
= cp_parser_identifier (parser
);
16048 /* Treat invalid names as if the parameter were nameless. */
16049 if (identifier
== error_mark_node
)
16050 identifier
= NULL_TREE
;
16053 identifier
= NULL_TREE
;
16055 /* Create the template parameter. */
16056 parameter
= finish_template_template_parm (class_type_node
,
16059 /* If the next token is an `=', then there is a
16060 default-argument. */
16061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16064 = cp_parser_default_template_template_argument (parser
);
16066 /* Template parameter packs cannot have default
16068 if (*is_parameter_pack
)
16071 error_at (token
->location
,
16072 "template parameter pack %qD cannot "
16073 "have a default argument",
16076 error_at (token
->location
, "template parameter packs cannot "
16077 "have default arguments");
16078 default_argument
= NULL_TREE
;
16082 default_argument
= NULL_TREE
;
16084 /* Create the combined representation of the parameter and the
16085 default argument. */
16086 parameter
= build_tree_list (default_argument
, parameter
);
16091 gcc_unreachable ();
16098 /* Parse a template-id.
16101 template-name < template-argument-list [opt] >
16103 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16104 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16105 returned. Otherwise, if the template-name names a function, or set
16106 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16107 names a class, returns a TYPE_DECL for the specialization.
16109 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16110 uninstantiated templates. */
16113 cp_parser_template_id (cp_parser
*parser
,
16114 bool template_keyword_p
,
16115 bool check_dependency_p
,
16116 enum tag_types tag_type
,
16117 bool is_declaration
)
16122 cp_token_position start_of_id
= 0;
16123 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16124 bool is_identifier
;
16126 /* If the next token corresponds to a template-id, there is no need
16128 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16129 if (token
->type
== CPP_TEMPLATE_ID
)
16131 cp_lexer_consume_token (parser
->lexer
);
16132 return saved_checks_value (token
->u
.tree_check_value
);
16135 /* Avoid performing name lookup if there is no possibility of
16136 finding a template-id. */
16137 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16138 || (token
->type
== CPP_NAME
16139 && !cp_parser_nth_token_starts_template_argument_list_p
16142 cp_parser_error (parser
, "expected template-id");
16143 return error_mark_node
;
16146 /* Remember where the template-id starts. */
16147 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16148 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16150 push_deferring_access_checks (dk_deferred
);
16152 /* Parse the template-name. */
16153 is_identifier
= false;
16154 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16155 check_dependency_p
,
16159 if (templ
== error_mark_node
|| is_identifier
)
16161 pop_deferring_access_checks ();
16165 /* Since we're going to preserve any side-effects from this parse, set up a
16166 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16167 in the template arguments. */
16168 tentative_firewall
firewall (parser
);
16170 /* If we find the sequence `[:' after a template-name, it's probably
16171 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16172 parse correctly the argument list. */
16173 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16174 == CPP_OPEN_SQUARE
)
16175 && next_token
->flags
& DIGRAPH
16176 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16178 && !(next_token_2
->flags
& PREV_WHITE
))
16180 cp_parser_parse_tentatively (parser
);
16181 /* Change `:' into `::'. */
16182 next_token_2
->type
= CPP_SCOPE
;
16183 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16185 cp_lexer_consume_token (parser
->lexer
);
16187 /* Parse the arguments. */
16188 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16189 if (!cp_parser_parse_definitely (parser
))
16191 /* If we couldn't parse an argument list, then we revert our changes
16192 and return simply an error. Maybe this is not a template-id
16194 next_token_2
->type
= CPP_COLON
;
16195 cp_parser_error (parser
, "expected %<<%>");
16196 pop_deferring_access_checks ();
16197 return error_mark_node
;
16199 /* Otherwise, emit an error about the invalid digraph, but continue
16200 parsing because we got our argument list. */
16201 if (permerror (next_token
->location
,
16202 "%<<::%> cannot begin a template-argument list"))
16204 static bool hint
= false;
16205 inform (next_token
->location
,
16206 "%<<:%> is an alternate spelling for %<[%>."
16207 " Insert whitespace between %<<%> and %<::%>");
16208 if (!hint
&& !flag_permissive
)
16210 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16211 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16212 "accept your code)");
16219 /* Look for the `<' that starts the template-argument-list. */
16220 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16222 pop_deferring_access_checks ();
16223 return error_mark_node
;
16225 /* Parse the arguments. */
16226 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16228 if ((cxx_dialect
> cxx17
)
16229 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16230 && !template_keyword_p
16231 && (cp_parser_error_occurred (parser
)
16232 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16234 /* This didn't go well. */
16235 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16237 /* C++2A says that "function-name < a;" is now ill-formed. */
16238 if (cp_parser_error_occurred (parser
))
16240 error_at (token
->location
, "invalid template-argument-list");
16241 inform (token
->location
, "function name as the left hand "
16242 "operand of %<<%> is ill-formed in C++2a; wrap the "
16243 "function name in %<()%>");
16246 /* We expect "f<targs>" to be followed by "(args)". */
16247 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16248 "expected %<(%> after template-argument-list");
16250 /* Purge all subsequent tokens. */
16251 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16254 cp_parser_simulate_error (parser
);
16255 pop_deferring_access_checks ();
16256 return error_mark_node
;
16260 /* Set the location to be of the form:
16261 template-name < template-argument-list [opt] >
16262 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16263 with caret == start at the start of the template-name,
16264 ranging until the closing '>'. */
16265 location_t finish_loc
16266 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16267 location_t combined_loc
16268 = make_location (token
->location
, token
->location
, finish_loc
);
16270 /* Check for concepts autos where they don't belong. We could
16271 identify types in some cases of idnetifier TEMPL, looking ahead
16272 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16273 types. We reject them in functions, but if what we have is an
16274 identifier, even with none_type we can't conclude it's NOT a
16275 type, we have to wait for template substitution. */
16276 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16277 template_id
= error_mark_node
;
16278 /* Build a representation of the specialization. */
16279 else if (identifier_p (templ
))
16280 template_id
= build_min_nt_loc (combined_loc
,
16283 else if (DECL_TYPE_TEMPLATE_P (templ
)
16284 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16286 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16287 template (rather than some instantiation thereof) only if
16288 is not nested within some other construct. For example, in
16289 "template <typename T> void f(T) { A<T>::", A<T> is just an
16290 instantiation of A. */
16291 bool entering_scope
16292 = (template_parm_scope_p ()
16293 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16295 = finish_template_type (templ
, arguments
, entering_scope
);
16297 /* A template-like identifier may be a partial concept id. */
16298 else if (flag_concepts
16299 && (template_id
= (cp_parser_maybe_partial_concept_id
16300 (parser
, templ
, arguments
))))
16301 return template_id
;
16302 else if (variable_template_p (templ
))
16304 template_id
= lookup_template_variable (templ
, arguments
);
16305 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16306 SET_EXPR_LOCATION (template_id
, combined_loc
);
16310 /* If it's not a class-template or a template-template, it should be
16311 a function-template. */
16312 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16313 || TREE_CODE (templ
) == OVERLOAD
16314 || TREE_CODE (templ
) == FUNCTION_DECL
16315 || BASELINK_P (templ
)));
16317 template_id
= lookup_template_function (templ
, arguments
);
16318 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16319 SET_EXPR_LOCATION (template_id
, combined_loc
);
16322 /* If parsing tentatively, replace the sequence of tokens that makes
16323 up the template-id with a CPP_TEMPLATE_ID token. That way,
16324 should we re-parse the token stream, we will not have to repeat
16325 the effort required to do the parse, nor will we issue duplicate
16326 error messages about problems during instantiation of the
16329 /* Don't do this if we had a parse error in a declarator; re-parsing
16330 might succeed if a name changes meaning (60361). */
16331 && !(cp_parser_error_occurred (parser
)
16332 && cp_parser_parsing_tentatively (parser
)
16333 && parser
->in_declarator_p
))
16335 /* Reset the contents of the START_OF_ID token. */
16336 token
->type
= CPP_TEMPLATE_ID
;
16337 token
->location
= combined_loc
;
16339 /* Retrieve any deferred checks. Do not pop this access checks yet
16340 so the memory will not be reclaimed during token replacing below. */
16341 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16342 token
->u
.tree_check_value
->value
= template_id
;
16343 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16344 token
->keyword
= RID_MAX
;
16346 /* Purge all subsequent tokens. */
16347 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16349 /* ??? Can we actually assume that, if template_id ==
16350 error_mark_node, we will have issued a diagnostic to the
16351 user, as opposed to simply marking the tentative parse as
16353 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16354 error_at (token
->location
, "parse error in template argument list");
16357 pop_to_parent_deferring_access_checks ();
16358 return template_id
;
16361 /* Parse a template-name.
16366 The standard should actually say:
16370 operator-function-id
16372 A defect report has been filed about this issue.
16374 A conversion-function-id cannot be a template name because they cannot
16375 be part of a template-id. In fact, looking at this code:
16377 a.operator K<int>()
16379 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16380 It is impossible to call a templated conversion-function-id with an
16381 explicit argument list, since the only allowed template parameter is
16382 the type to which it is converting.
16384 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16385 `template' keyword, in a construction like:
16389 In that case `f' is taken to be a template-name, even though there
16390 is no way of knowing for sure.
16392 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16393 name refers to a set of overloaded functions, at least one of which
16394 is a template, or an IDENTIFIER_NODE with the name of the template,
16395 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16396 names are looked up inside uninstantiated templates. */
16399 cp_parser_template_name (cp_parser
* parser
,
16400 bool template_keyword_p
,
16401 bool check_dependency_p
,
16402 bool is_declaration
,
16403 enum tag_types tag_type
,
16404 bool *is_identifier
)
16408 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16410 /* If the next token is `operator', then we have either an
16411 operator-function-id or a conversion-function-id. */
16412 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16414 /* We don't know whether we're looking at an
16415 operator-function-id or a conversion-function-id. */
16416 cp_parser_parse_tentatively (parser
);
16417 /* Try an operator-function-id. */
16418 identifier
= cp_parser_operator_function_id (parser
);
16419 /* If that didn't work, try a conversion-function-id. */
16420 if (!cp_parser_parse_definitely (parser
))
16422 cp_parser_error (parser
, "expected template-name");
16423 return error_mark_node
;
16426 /* Look for the identifier. */
16428 identifier
= cp_parser_identifier (parser
);
16430 /* If we didn't find an identifier, we don't have a template-id. */
16431 if (identifier
== error_mark_node
)
16432 return error_mark_node
;
16434 /* If the name immediately followed the `template' keyword, then it
16435 is a template-name. However, if the next token is not `<', then
16436 we do not treat it as a template-name, since it is not being used
16437 as part of a template-id. This enables us to handle constructs
16440 template <typename T> struct S { S(); };
16441 template <typename T> S<T>::S();
16443 correctly. We would treat `S' as a template -- if it were `S<T>'
16444 -- but we do not if there is no `<'. */
16446 if (processing_template_decl
16447 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16449 /* In a declaration, in a dependent context, we pretend that the
16450 "template" keyword was present in order to improve error
16451 recovery. For example, given:
16453 template <typename T> void f(T::X<int>);
16455 we want to treat "X<int>" as a template-id. */
16457 && !template_keyword_p
16458 && parser
->scope
&& TYPE_P (parser
->scope
)
16459 && check_dependency_p
16460 && dependent_scope_p (parser
->scope
)
16461 /* Do not do this for dtors (or ctors), since they never
16462 need the template keyword before their name. */
16463 && !constructor_name_p (identifier
, parser
->scope
))
16465 cp_token_position start
= 0;
16467 /* Explain what went wrong. */
16468 error_at (token
->location
, "non-template %qD used as template",
16470 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16471 parser
->scope
, identifier
);
16472 /* If parsing tentatively, find the location of the "<" token. */
16473 if (cp_parser_simulate_error (parser
))
16474 start
= cp_lexer_token_position (parser
->lexer
, true);
16475 /* Parse the template arguments so that we can issue error
16476 messages about them. */
16477 cp_lexer_consume_token (parser
->lexer
);
16478 cp_parser_enclosed_template_argument_list (parser
);
16479 /* Skip tokens until we find a good place from which to
16480 continue parsing. */
16481 cp_parser_skip_to_closing_parenthesis (parser
,
16482 /*recovering=*/true,
16484 /*consume_paren=*/false);
16485 /* If parsing tentatively, permanently remove the
16486 template argument list. That will prevent duplicate
16487 error messages from being issued about the missing
16488 "template" keyword. */
16490 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16492 *is_identifier
= true;
16493 parser
->context
->object_type
= NULL_TREE
;
16497 /* If the "template" keyword is present, then there is generally
16498 no point in doing name-lookup, so we just return IDENTIFIER.
16499 But, if the qualifying scope is non-dependent then we can
16500 (and must) do name-lookup normally. */
16501 if (template_keyword_p
)
16503 tree scope
= (parser
->scope
? parser
->scope
16504 : parser
->context
->object_type
);
16505 if (scope
&& TYPE_P (scope
)
16506 && (!CLASS_TYPE_P (scope
)
16507 || (check_dependency_p
&& dependent_type_p (scope
))))
16509 /* We're optimizing away the call to cp_parser_lookup_name, but
16510 we still need to do this. */
16511 parser
->context
->object_type
= NULL_TREE
;
16517 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16518 const bool scoped_p
= ((parser
->scope
? parser
->scope
16519 : parser
->context
->object_type
) != NULL_TREE
);
16521 /* Look up the name. */
16522 decl
= cp_parser_lookup_name (parser
, identifier
,
16524 /*is_template=*/true,
16525 /*is_namespace=*/false,
16526 check_dependency_p
,
16527 /*ambiguous_decls=*/NULL
,
16530 decl
= strip_using_decl (decl
);
16532 /* If DECL is a template, then the name was a template-name. */
16533 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16535 if (TREE_DEPRECATED (decl
)
16536 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16537 warn_deprecated_use (decl
, NULL_TREE
);
16541 /* The standard does not explicitly indicate whether a name that
16542 names a set of overloaded declarations, some of which are
16543 templates, is a template-name. However, such a name should
16544 be a template-name; otherwise, there is no way to form a
16545 template-id for the overloaded templates. */
16546 bool found
= false;
16548 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16549 !found
&& iter
; ++iter
)
16550 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16554 && (cxx_dialect
> cxx17
)
16556 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
16558 /* [temp.names] says "A name is also considered to refer to a template
16559 if it is an unqualified-id followed by a < and name lookup finds
16560 either one or more functions or finds nothing." */
16562 /* The "more functions" case. Just use the OVERLOAD as normally.
16563 We don't use is_overloaded_fn here to avoid considering
16565 if (TREE_CODE (decl
) == OVERLOAD
16566 /* Name lookup found one function. */
16567 || TREE_CODE (decl
) == FUNCTION_DECL
)
16569 /* Name lookup found nothing. */
16570 else if (decl
== error_mark_node
)
16576 /* The name does not name a template. */
16577 cp_parser_error (parser
, "expected template-name");
16578 return error_mark_node
;
16585 /* Parse a template-argument-list.
16587 template-argument-list:
16588 template-argument ... [opt]
16589 template-argument-list , template-argument ... [opt]
16591 Returns a TREE_VEC containing the arguments. */
16594 cp_parser_template_argument_list (cp_parser
* parser
)
16596 tree fixed_args
[10];
16597 unsigned n_args
= 0;
16598 unsigned alloced
= 10;
16599 tree
*arg_ary
= fixed_args
;
16601 bool saved_in_template_argument_list_p
;
16603 bool saved_non_ice_p
;
16605 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16606 parser
->in_template_argument_list_p
= true;
16607 /* Even if the template-id appears in an integral
16608 constant-expression, the contents of the argument list do
16610 saved_ice_p
= parser
->integral_constant_expression_p
;
16611 parser
->integral_constant_expression_p
= false;
16612 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16613 parser
->non_integral_constant_expression_p
= false;
16615 /* Parse the arguments. */
16621 /* Consume the comma. */
16622 cp_lexer_consume_token (parser
->lexer
);
16624 /* Parse the template-argument. */
16625 argument
= cp_parser_template_argument (parser
);
16627 /* If the next token is an ellipsis, we're expanding a template
16629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16631 if (argument
== error_mark_node
)
16633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16634 error_at (token
->location
,
16635 "expected parameter pack before %<...%>");
16637 /* Consume the `...' token. */
16638 cp_lexer_consume_token (parser
->lexer
);
16640 /* Make the argument into a TYPE_PACK_EXPANSION or
16641 EXPR_PACK_EXPANSION. */
16642 argument
= make_pack_expansion (argument
);
16645 if (n_args
== alloced
)
16649 if (arg_ary
== fixed_args
)
16651 arg_ary
= XNEWVEC (tree
, alloced
);
16652 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16655 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16657 arg_ary
[n_args
++] = argument
;
16659 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16661 vec
= make_tree_vec (n_args
);
16664 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16666 if (arg_ary
!= fixed_args
)
16668 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16669 parser
->integral_constant_expression_p
= saved_ice_p
;
16670 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16672 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16676 /* Parse a template-argument.
16679 assignment-expression
16683 The representation is that of an assignment-expression, type-id, or
16684 id-expression -- except that the qualified id-expression is
16685 evaluated, so that the value returned is either a DECL or an
16688 Although the standard says "assignment-expression", it forbids
16689 throw-expressions or assignments in the template argument.
16690 Therefore, we use "conditional-expression" instead. */
16693 cp_parser_template_argument (cp_parser
* parser
)
16698 bool maybe_type_id
= false;
16699 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16700 location_t loc
= 0;
16703 /* There's really no way to know what we're looking at, so we just
16704 try each alternative in order.
16708 In a template-argument, an ambiguity between a type-id and an
16709 expression is resolved to a type-id, regardless of the form of
16710 the corresponding template-parameter.
16712 Therefore, we try a type-id first. */
16713 cp_parser_parse_tentatively (parser
);
16714 argument
= cp_parser_template_type_arg (parser
);
16715 /* If there was no error parsing the type-id but the next token is a
16716 '>>', our behavior depends on which dialect of C++ we're
16717 parsing. In C++98, we probably found a typo for '> >'. But there
16718 are type-id which are also valid expressions. For instance:
16720 struct X { int operator >> (int); };
16721 template <int V> struct Foo {};
16724 Here 'X()' is a valid type-id of a function type, but the user just
16725 wanted to write the expression "X() >> 5". Thus, we remember that we
16726 found a valid type-id, but we still try to parse the argument as an
16727 expression to see what happens.
16729 In C++0x, the '>>' will be considered two separate '>'
16731 if (!cp_parser_error_occurred (parser
)
16732 && cxx_dialect
== cxx98
16733 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16735 maybe_type_id
= true;
16736 cp_parser_abort_tentative_parse (parser
);
16740 /* If the next token isn't a `,' or a `>', then this argument wasn't
16741 really finished. This means that the argument is not a valid
16743 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16744 cp_parser_error (parser
, "expected template-argument");
16745 /* If that worked, we're done. */
16746 if (cp_parser_parse_definitely (parser
))
16749 /* We're still not sure what the argument will be. */
16750 cp_parser_parse_tentatively (parser
);
16751 /* Try a template. */
16752 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16753 argument
= cp_parser_id_expression (parser
,
16754 /*template_keyword_p=*/false,
16755 /*check_dependency_p=*/true,
16757 /*declarator_p=*/false,
16758 /*optional_p=*/false);
16759 /* If the next token isn't a `,' or a `>', then this argument wasn't
16760 really finished. */
16761 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16762 cp_parser_error (parser
, "expected template-argument");
16763 if (!cp_parser_error_occurred (parser
))
16765 /* Figure out what is being referred to. If the id-expression
16766 was for a class template specialization, then we will have a
16767 TYPE_DECL at this point. There is no need to do name lookup
16768 at this point in that case. */
16769 if (TREE_CODE (argument
) != TYPE_DECL
)
16770 argument
= cp_parser_lookup_name (parser
, argument
,
16772 /*is_template=*/template_p
,
16773 /*is_namespace=*/false,
16774 /*check_dependency=*/true,
16775 /*ambiguous_decls=*/NULL
,
16776 argument_start_token
->location
);
16777 /* Handle a constrained-type-specifier for a non-type template
16779 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16781 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16782 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16783 cp_parser_error (parser
, "expected template-name");
16785 if (cp_parser_parse_definitely (parser
))
16787 if (TREE_DEPRECATED (argument
))
16788 warn_deprecated_use (argument
, NULL_TREE
);
16791 /* It must be a non-type argument. In C++17 any constant-expression is
16793 if (cxx_dialect
> cxx14
)
16796 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16798 -- an integral constant-expression of integral or enumeration
16801 -- the name of a non-type template-parameter; or
16803 -- the name of an object or function with external linkage...
16805 -- the address of an object or function with external linkage...
16807 -- a pointer to member... */
16808 /* Look for a non-type template parameter. */
16809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16811 cp_parser_parse_tentatively (parser
);
16812 argument
= cp_parser_primary_expression (parser
,
16813 /*address_p=*/false,
16815 /*template_arg_p=*/true,
16817 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16818 || !cp_parser_next_token_ends_template_argument_p (parser
))
16819 cp_parser_simulate_error (parser
);
16820 if (cp_parser_parse_definitely (parser
))
16824 /* If the next token is "&", the argument must be the address of an
16825 object or function with external linkage. */
16826 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16829 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16830 cp_lexer_consume_token (parser
->lexer
);
16832 /* See if we might have an id-expression. */
16833 token
= cp_lexer_peek_token (parser
->lexer
);
16834 if (token
->type
== CPP_NAME
16835 || token
->keyword
== RID_OPERATOR
16836 || token
->type
== CPP_SCOPE
16837 || token
->type
== CPP_TEMPLATE_ID
16838 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16840 cp_parser_parse_tentatively (parser
);
16841 argument
= cp_parser_primary_expression (parser
,
16844 /*template_arg_p=*/true,
16846 if (cp_parser_error_occurred (parser
)
16847 || !cp_parser_next_token_ends_template_argument_p (parser
))
16848 cp_parser_abort_tentative_parse (parser
);
16853 if (INDIRECT_REF_P (argument
))
16855 /* Strip the dereference temporarily. */
16856 gcc_assert (REFERENCE_REF_P (argument
));
16857 argument
= TREE_OPERAND (argument
, 0);
16860 /* If we're in a template, we represent a qualified-id referring
16861 to a static data member as a SCOPE_REF even if the scope isn't
16862 dependent so that we can check access control later. */
16864 if (TREE_CODE (probe
) == SCOPE_REF
)
16865 probe
= TREE_OPERAND (probe
, 1);
16868 /* A variable without external linkage might still be a
16869 valid constant-expression, so no error is issued here
16870 if the external-linkage check fails. */
16871 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16872 cp_parser_simulate_error (parser
);
16874 else if (is_overloaded_fn (argument
))
16875 /* All overloaded functions are allowed; if the external
16876 linkage test does not pass, an error will be issued
16880 && (TREE_CODE (argument
) == OFFSET_REF
16881 || TREE_CODE (argument
) == SCOPE_REF
))
16882 /* A pointer-to-member. */
16884 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16887 cp_parser_simulate_error (parser
);
16889 if (cp_parser_parse_definitely (parser
))
16892 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16893 tf_warning_or_error
);
16895 argument
= convert_from_reference (argument
);
16900 /* If the argument started with "&", there are no other valid
16901 alternatives at this point. */
16904 cp_parser_error (parser
, "invalid non-type template argument");
16905 return error_mark_node
;
16909 /* If the argument wasn't successfully parsed as a type-id followed
16910 by '>>', the argument can only be a constant expression now.
16911 Otherwise, we try parsing the constant-expression tentatively,
16912 because the argument could really be a type-id. */
16914 cp_parser_parse_tentatively (parser
);
16916 if (cxx_dialect
<= cxx14
)
16917 argument
= cp_parser_constant_expression (parser
);
16920 /* With C++17 generalized non-type template arguments we need to handle
16921 lvalue constant expressions, too. */
16922 argument
= cp_parser_assignment_expression (parser
);
16923 require_potential_constant_expression (argument
);
16926 if (!maybe_type_id
)
16928 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16929 cp_parser_error (parser
, "expected template-argument");
16930 if (cp_parser_parse_definitely (parser
))
16932 /* We did our best to parse the argument as a non type-id, but that
16933 was the only alternative that matched (albeit with a '>' after
16934 it). We can assume it's just a typo from the user, and a
16935 diagnostic will then be issued. */
16936 return cp_parser_template_type_arg (parser
);
16939 /* Parse an explicit-instantiation.
16941 explicit-instantiation:
16942 template declaration
16944 Although the standard says `declaration', what it really means is:
16946 explicit-instantiation:
16947 template decl-specifier-seq [opt] declarator [opt] ;
16949 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16950 supposed to be allowed. A defect report has been filed about this
16955 explicit-instantiation:
16956 storage-class-specifier template
16957 decl-specifier-seq [opt] declarator [opt] ;
16958 function-specifier template
16959 decl-specifier-seq [opt] declarator [opt] ; */
16962 cp_parser_explicit_instantiation (cp_parser
* parser
)
16964 int declares_class_or_enum
;
16965 cp_decl_specifier_seq decl_specifiers
;
16966 tree extension_specifier
= NULL_TREE
;
16968 timevar_push (TV_TEMPLATE_INST
);
16970 /* Look for an (optional) storage-class-specifier or
16971 function-specifier. */
16972 if (cp_parser_allow_gnu_extensions_p (parser
))
16974 extension_specifier
16975 = cp_parser_storage_class_specifier_opt (parser
);
16976 if (!extension_specifier
)
16977 extension_specifier
16978 = cp_parser_function_specifier_opt (parser
,
16979 /*decl_specs=*/NULL
);
16982 /* Look for the `template' keyword. */
16983 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16984 /* Let the front end know that we are processing an explicit
16986 begin_explicit_instantiation ();
16987 /* [temp.explicit] says that we are supposed to ignore access
16988 control while processing explicit instantiation directives. */
16989 push_deferring_access_checks (dk_no_check
);
16990 /* Parse a decl-specifier-seq. */
16991 cp_parser_decl_specifier_seq (parser
,
16992 CP_PARSER_FLAGS_OPTIONAL
,
16994 &declares_class_or_enum
);
16995 /* If there was exactly one decl-specifier, and it declared a class,
16996 and there's no declarator, then we have an explicit type
16998 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17002 type
= check_tag_decl (&decl_specifiers
,
17003 /*explicit_type_instantiation_p=*/true);
17004 /* Turn access control back on for names used during
17005 template instantiation. */
17006 pop_deferring_access_checks ();
17008 do_type_instantiation (type
, extension_specifier
,
17009 /*complain=*/tf_error
);
17013 cp_declarator
*declarator
;
17016 /* Parse the declarator. */
17018 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17019 /*ctor_dtor_or_conv_p=*/NULL
,
17020 /*parenthesized_p=*/NULL
,
17021 /*member_p=*/false,
17022 /*friend_p=*/false);
17023 if (declares_class_or_enum
& 2)
17024 cp_parser_check_for_definition_in_return_type (declarator
,
17025 decl_specifiers
.type
,
17026 decl_specifiers
.locations
[ds_type_spec
]);
17027 if (declarator
!= cp_error_declarator
)
17029 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17030 permerror (decl_specifiers
.locations
[ds_inline
],
17031 "explicit instantiation shall not use"
17032 " %<inline%> specifier");
17033 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17034 permerror (decl_specifiers
.locations
[ds_constexpr
],
17035 "explicit instantiation shall not use"
17036 " %<constexpr%> specifier");
17038 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17039 NORMAL
, 0, &decl_specifiers
.attributes
);
17040 /* Turn access control back on for names used during
17041 template instantiation. */
17042 pop_deferring_access_checks ();
17043 /* Do the explicit instantiation. */
17044 do_decl_instantiation (decl
, extension_specifier
);
17048 pop_deferring_access_checks ();
17049 /* Skip the body of the explicit instantiation. */
17050 cp_parser_skip_to_end_of_statement (parser
);
17053 /* We're done with the instantiation. */
17054 end_explicit_instantiation ();
17056 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17058 timevar_pop (TV_TEMPLATE_INST
);
17061 /* Parse an explicit-specialization.
17063 explicit-specialization:
17064 template < > declaration
17066 Although the standard says `declaration', what it really means is:
17068 explicit-specialization:
17069 template <> decl-specifier [opt] init-declarator [opt] ;
17070 template <> function-definition
17071 template <> explicit-specialization
17072 template <> template-declaration */
17075 cp_parser_explicit_specialization (cp_parser
* parser
)
17077 bool need_lang_pop
;
17078 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17080 /* Look for the `template' keyword. */
17081 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17082 /* Look for the `<'. */
17083 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17084 /* Look for the `>'. */
17085 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17086 /* We have processed another parameter list. */
17087 ++parser
->num_template_parameter_lists
;
17090 A template ... explicit specialization ... shall not have C
17092 if (current_lang_name
== lang_name_c
)
17094 error_at (token
->location
, "template specialization with C linkage");
17095 maybe_show_extern_c_location ();
17096 /* Give it C++ linkage to avoid confusing other parts of the
17098 push_lang_context (lang_name_cplusplus
);
17099 need_lang_pop
= true;
17102 need_lang_pop
= false;
17103 /* Let the front end know that we are beginning a specialization. */
17104 if (!begin_specialization ())
17106 end_specialization ();
17110 /* If the next keyword is `template', we need to figure out whether
17111 or not we're looking a template-declaration. */
17112 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17114 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17115 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17116 cp_parser_template_declaration_after_export (parser
,
17117 /*member_p=*/false);
17119 cp_parser_explicit_specialization (parser
);
17122 /* Parse the dependent declaration. */
17123 cp_parser_single_declaration (parser
,
17125 /*member_p=*/false,
17126 /*explicit_specialization_p=*/true,
17127 /*friend_p=*/NULL
);
17128 /* We're done with the specialization. */
17129 end_specialization ();
17130 /* For the erroneous case of a template with C linkage, we pushed an
17131 implicit C++ linkage scope; exit that scope now. */
17133 pop_lang_context ();
17134 /* We're done with this parameter list. */
17135 --parser
->num_template_parameter_lists
;
17138 /* Parse a type-specifier.
17141 simple-type-specifier
17144 elaborated-type-specifier
17152 Returns a representation of the type-specifier. For a
17153 class-specifier, enum-specifier, or elaborated-type-specifier, a
17154 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17156 The parser flags FLAGS is used to control type-specifier parsing.
17158 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17159 in a decl-specifier-seq.
17161 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17162 class-specifier, enum-specifier, or elaborated-type-specifier, then
17163 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17164 if a type is declared; 2 if it is defined. Otherwise, it is set to
17167 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17168 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17169 is set to FALSE. */
17172 cp_parser_type_specifier (cp_parser
* parser
,
17173 cp_parser_flags flags
,
17174 cp_decl_specifier_seq
*decl_specs
,
17175 bool is_declaration
,
17176 int* declares_class_or_enum
,
17177 bool* is_cv_qualifier
)
17179 tree type_spec
= NULL_TREE
;
17182 cp_decl_spec ds
= ds_last
;
17184 /* Assume this type-specifier does not declare a new type. */
17185 if (declares_class_or_enum
)
17186 *declares_class_or_enum
= 0;
17187 /* And that it does not specify a cv-qualifier. */
17188 if (is_cv_qualifier
)
17189 *is_cv_qualifier
= false;
17190 /* Peek at the next token. */
17191 token
= cp_lexer_peek_token (parser
->lexer
);
17193 /* If we're looking at a keyword, we can use that to guide the
17194 production we choose. */
17195 keyword
= token
->keyword
;
17199 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17200 goto elaborated_type_specifier
;
17202 /* Look for the enum-specifier. */
17203 type_spec
= cp_parser_enum_specifier (parser
);
17204 /* If that worked, we're done. */
17207 if (declares_class_or_enum
)
17208 *declares_class_or_enum
= 2;
17210 cp_parser_set_decl_spec_type (decl_specs
,
17213 /*type_definition_p=*/true);
17217 goto elaborated_type_specifier
;
17219 /* Any of these indicate either a class-specifier, or an
17220 elaborated-type-specifier. */
17224 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17225 goto elaborated_type_specifier
;
17227 /* Parse tentatively so that we can back up if we don't find a
17228 class-specifier. */
17229 cp_parser_parse_tentatively (parser
);
17230 /* Look for the class-specifier. */
17231 type_spec
= cp_parser_class_specifier (parser
);
17232 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17233 /* If that worked, we're done. */
17234 if (cp_parser_parse_definitely (parser
))
17236 if (declares_class_or_enum
)
17237 *declares_class_or_enum
= 2;
17239 cp_parser_set_decl_spec_type (decl_specs
,
17242 /*type_definition_p=*/true);
17246 /* Fall through. */
17247 elaborated_type_specifier
:
17248 /* We're declaring (not defining) a class or enum. */
17249 if (declares_class_or_enum
)
17250 *declares_class_or_enum
= 1;
17252 /* Fall through. */
17254 /* Look for an elaborated-type-specifier. */
17256 = (cp_parser_elaborated_type_specifier
17258 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17261 cp_parser_set_decl_spec_type (decl_specs
,
17264 /*type_definition_p=*/false);
17269 if (is_cv_qualifier
)
17270 *is_cv_qualifier
= true;
17275 if (is_cv_qualifier
)
17276 *is_cv_qualifier
= true;
17281 if (is_cv_qualifier
)
17282 *is_cv_qualifier
= true;
17286 /* The `__complex__' keyword is a GNU extension. */
17294 /* Handle simple keywords. */
17299 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17300 decl_specs
->any_specifiers_p
= true;
17302 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17305 /* If we do not already have a type-specifier, assume we are looking
17306 at a simple-type-specifier. */
17307 type_spec
= cp_parser_simple_type_specifier (parser
,
17311 /* If we didn't find a type-specifier, and a type-specifier was not
17312 optional in this context, issue an error message. */
17313 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17315 cp_parser_error (parser
, "expected type specifier");
17316 return error_mark_node
;
17322 /* Parse a simple-type-specifier.
17324 simple-type-specifier:
17325 :: [opt] nested-name-specifier [opt] type-name
17326 :: [opt] nested-name-specifier template template-id
17341 simple-type-specifier:
17343 decltype ( expression )
17346 __underlying_type ( type-id )
17350 nested-name-specifier(opt) template-name
17354 simple-type-specifier:
17356 __typeof__ unary-expression
17357 __typeof__ ( type-id )
17358 __typeof__ ( type-id ) { initializer-list , [opt] }
17360 Concepts Extension:
17362 simple-type-specifier:
17363 constrained-type-specifier
17365 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17366 appropriately updated. */
17369 cp_parser_simple_type_specifier (cp_parser
* parser
,
17370 cp_decl_specifier_seq
*decl_specs
,
17371 cp_parser_flags flags
)
17373 tree type
= NULL_TREE
;
17377 /* Peek at the next token. */
17378 token
= cp_lexer_peek_token (parser
->lexer
);
17380 /* If we're looking at a keyword, things are easy. */
17381 switch (token
->keyword
)
17385 decl_specs
->explicit_char_p
= true;
17386 type
= char_type_node
;
17389 type
= char16_type_node
;
17392 type
= char32_type_node
;
17395 type
= wchar_type_node
;
17398 type
= boolean_type_node
;
17401 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17402 type
= short_integer_type_node
;
17406 decl_specs
->explicit_int_p
= true;
17407 type
= integer_type_node
;
17413 idx
= token
->keyword
- RID_INT_N_0
;
17414 if (! int_n_enabled_p
[idx
])
17418 decl_specs
->explicit_intN_p
= true;
17419 decl_specs
->int_n_idx
= idx
;
17421 type
= int_n_trees
[idx
].signed_type
;
17425 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17426 type
= long_integer_type_node
;
17429 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17430 type
= integer_type_node
;
17433 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17434 type
= unsigned_type_node
;
17437 type
= float_type_node
;
17440 type
= double_type_node
;
17443 type
= void_type_node
;
17447 maybe_warn_cpp0x (CPP0X_AUTO
);
17448 if (parser
->auto_is_implicit_function_template_parm_p
)
17450 /* The 'auto' might be the placeholder return type for a function decl
17451 with trailing return type. */
17452 bool have_trailing_return_fn_decl
= false;
17454 cp_parser_parse_tentatively (parser
);
17455 cp_lexer_consume_token (parser
->lexer
);
17456 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17457 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17458 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17459 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17463 cp_lexer_consume_token (parser
->lexer
);
17464 cp_parser_skip_to_closing_parenthesis (parser
,
17465 /*recovering*/false,
17467 /*consume_paren*/true);
17471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17473 have_trailing_return_fn_decl
= true;
17477 cp_lexer_consume_token (parser
->lexer
);
17479 cp_parser_abort_tentative_parse (parser
);
17481 if (have_trailing_return_fn_decl
)
17483 type
= make_auto ();
17487 if (cxx_dialect
>= cxx14
)
17489 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17490 type
= TREE_TYPE (type
);
17493 type
= error_mark_node
;
17495 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17497 if (cxx_dialect
< cxx14
)
17498 error_at (token
->location
,
17499 "use of %<auto%> in lambda parameter declaration "
17500 "only available with "
17501 "-std=c++14 or -std=gnu++14");
17503 else if (cxx_dialect
< cxx14
)
17504 error_at (token
->location
,
17505 "use of %<auto%> in parameter declaration "
17506 "only available with "
17507 "-std=c++14 or -std=gnu++14");
17508 else if (!flag_concepts
)
17509 pedwarn (token
->location
, 0,
17510 "use of %<auto%> in parameter declaration "
17511 "only available with -fconcepts");
17514 type
= make_auto ();
17518 /* Since DR 743, decltype can either be a simple-type-specifier by
17519 itself or begin a nested-name-specifier. Parsing it will replace
17520 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17521 handling below decide what to do. */
17522 cp_parser_decltype (parser
);
17523 cp_lexer_set_token_position (parser
->lexer
, token
);
17527 /* Consume the `typeof' token. */
17528 cp_lexer_consume_token (parser
->lexer
);
17529 /* Parse the operand to `typeof'. */
17530 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17531 /* If it is not already a TYPE, take its type. */
17532 if (!TYPE_P (type
))
17533 type
= finish_typeof (type
);
17536 cp_parser_set_decl_spec_type (decl_specs
, type
,
17538 /*type_definition_p=*/false);
17542 case RID_UNDERLYING_TYPE
:
17543 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17545 cp_parser_set_decl_spec_type (decl_specs
, type
,
17547 /*type_definition_p=*/false);
17552 case RID_DIRECT_BASES
:
17553 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17555 cp_parser_set_decl_spec_type (decl_specs
, type
,
17557 /*type_definition_p=*/false);
17563 /* If token is an already-parsed decltype not followed by ::,
17564 it's a simple-type-specifier. */
17565 if (token
->type
== CPP_DECLTYPE
17566 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17568 type
= saved_checks_value (token
->u
.tree_check_value
);
17571 cp_parser_set_decl_spec_type (decl_specs
, type
,
17573 /*type_definition_p=*/false);
17574 /* Remember that we are handling a decltype in order to
17575 implement the resolution of DR 1510 when the argument
17576 isn't instantiation dependent. */
17577 decl_specs
->decltype_p
= true;
17579 cp_lexer_consume_token (parser
->lexer
);
17583 /* If the type-specifier was for a built-in type, we're done. */
17586 /* Record the type. */
17588 && (token
->keyword
!= RID_SIGNED
17589 && token
->keyword
!= RID_UNSIGNED
17590 && token
->keyword
!= RID_SHORT
17591 && token
->keyword
!= RID_LONG
))
17592 cp_parser_set_decl_spec_type (decl_specs
,
17595 /*type_definition_p=*/false);
17597 decl_specs
->any_specifiers_p
= true;
17599 /* Consume the token. */
17600 cp_lexer_consume_token (parser
->lexer
);
17602 if (type
== error_mark_node
)
17603 return error_mark_node
;
17605 /* There is no valid C++ program where a non-template type is
17606 followed by a "<". That usually indicates that the user thought
17607 that the type was a template. */
17608 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17611 return TYPE_NAME (type
);
17614 /* The type-specifier must be a user-defined type. */
17615 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17620 /* Don't gobble tokens or issue error messages if this is an
17621 optional type-specifier. */
17622 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17623 cp_parser_parse_tentatively (parser
);
17625 token
= cp_lexer_peek_token (parser
->lexer
);
17627 /* Look for the optional `::' operator. */
17629 = (cp_parser_global_scope_opt (parser
,
17630 /*current_scope_valid_p=*/false)
17632 /* Look for the nested-name specifier. */
17634 = (cp_parser_nested_name_specifier_opt (parser
,
17635 /*typename_keyword_p=*/false,
17636 /*check_dependency_p=*/true,
17638 /*is_declaration=*/false)
17640 /* If we have seen a nested-name-specifier, and the next token
17641 is `template', then we are using the template-id production. */
17643 && cp_parser_optional_template_keyword (parser
))
17645 /* Look for the template-id. */
17646 type
= cp_parser_template_id (parser
,
17647 /*template_keyword_p=*/true,
17648 /*check_dependency_p=*/true,
17650 /*is_declaration=*/false);
17651 /* If the template-id did not name a type, we are out of
17653 if (TREE_CODE (type
) != TYPE_DECL
)
17655 cp_parser_error (parser
, "expected template-id for type");
17659 /* Otherwise, look for a type-name. */
17661 type
= cp_parser_type_name (parser
);
17662 /* Keep track of all name-lookups performed in class scopes. */
17666 && TREE_CODE (type
) == TYPE_DECL
17667 && identifier_p (DECL_NAME (type
)))
17668 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17669 /* If it didn't work out, we don't have a TYPE. */
17670 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17671 && !cp_parser_parse_definitely (parser
))
17673 if (!type
&& cxx_dialect
>= cxx17
)
17675 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17676 cp_parser_parse_tentatively (parser
);
17678 cp_parser_global_scope_opt (parser
,
17679 /*current_scope_valid_p=*/false);
17680 cp_parser_nested_name_specifier_opt (parser
,
17681 /*typename_keyword_p=*/false,
17682 /*check_dependency_p=*/true,
17684 /*is_declaration=*/false);
17685 tree name
= cp_parser_identifier (parser
);
17686 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17687 && parser
->scope
!= error_mark_node
)
17689 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17691 /*is_template=*/false,
17692 /*is_namespace=*/false,
17693 /*check_dependency=*/true,
17694 /*ambiguous_decls=*/NULL
,
17696 if (tmpl
&& tmpl
!= error_mark_node
17697 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17698 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17699 type
= make_template_placeholder (tmpl
);
17702 type
= error_mark_node
;
17703 if (!cp_parser_simulate_error (parser
))
17704 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17705 NLE_TYPE
, token
->location
);
17709 type
= error_mark_node
;
17711 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17712 && !cp_parser_parse_definitely (parser
))
17715 if (type
&& decl_specs
)
17716 cp_parser_set_decl_spec_type (decl_specs
, type
,
17718 /*type_definition_p=*/false);
17721 /* If we didn't get a type-name, issue an error message. */
17722 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17724 cp_parser_error (parser
, "expected type-name");
17725 return error_mark_node
;
17728 if (type
&& type
!= error_mark_node
)
17730 /* See if TYPE is an Objective-C type, and if so, parse and
17731 accept any protocol references following it. Do this before
17732 the cp_parser_check_for_invalid_template_id() call, because
17733 Objective-C types can be followed by '<...>' which would
17734 enclose protocol names rather than template arguments, and so
17735 everything is fine. */
17736 if (c_dialect_objc () && !parser
->scope
17737 && (objc_is_id (type
) || objc_is_class_name (type
)))
17739 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17740 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17742 /* Clobber the "unqualified" type previously entered into
17743 DECL_SPECS with the new, improved protocol-qualified version. */
17745 decl_specs
->type
= qual_type
;
17750 /* There is no valid C++ program where a non-template type is
17751 followed by a "<". That usually indicates that the user
17752 thought that the type was a template. */
17753 cp_parser_check_for_invalid_template_id (parser
, type
,
17761 /* Parse a type-name.
17767 simple-template-id [in c++0x]
17784 Returns a TYPE_DECL for the type. */
17787 cp_parser_type_name (cp_parser
* parser
)
17789 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17794 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17798 /* We can't know yet whether it is a class-name or not. */
17799 cp_parser_parse_tentatively (parser
);
17800 /* Try a class-name. */
17801 type_decl
= cp_parser_class_name (parser
,
17802 typename_keyword_p
,
17803 /*template_keyword_p=*/false,
17805 /*check_dependency_p=*/true,
17806 /*class_head_p=*/false,
17807 /*is_declaration=*/false);
17808 /* If it's not a class-name, keep looking. */
17809 if (!cp_parser_parse_definitely (parser
))
17811 if (cxx_dialect
< cxx11
)
17812 /* It must be a typedef-name or an enum-name. */
17813 return cp_parser_nonclass_name (parser
);
17815 cp_parser_parse_tentatively (parser
);
17816 /* It is either a simple-template-id representing an
17817 instantiation of an alias template... */
17818 type_decl
= cp_parser_template_id (parser
,
17819 /*template_keyword_p=*/false,
17820 /*check_dependency_p=*/true,
17822 /*is_declaration=*/false);
17823 /* Note that this must be an instantiation of an alias template
17824 because [temp.names]/6 says:
17826 A template-id that names an alias template specialization
17829 Whereas [temp.names]/7 says:
17831 A simple-template-id that names a class template
17832 specialization is a class-name.
17834 With concepts, this could also be a partial-concept-id that
17835 declares a non-type template parameter. */
17836 if (type_decl
!= NULL_TREE
17837 && TREE_CODE (type_decl
) == TYPE_DECL
17838 && TYPE_DECL_ALIAS_P (type_decl
))
17839 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17840 else if (is_constrained_parameter (type_decl
))
17841 /* Don't do anything. */ ;
17843 cp_parser_simulate_error (parser
);
17845 if (!cp_parser_parse_definitely (parser
))
17846 /* ... Or a typedef-name or an enum-name. */
17847 return cp_parser_nonclass_name (parser
);
17853 /* Check if DECL and ARGS can form a constrained-type-specifier.
17854 If ARGS is non-null, we try to form a concept check of the
17855 form DECL<?, ARGS> where ? is a wildcard that matches any
17856 kind of template argument. If ARGS is NULL, then we try to
17857 form a concept check of the form DECL<?>. */
17860 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17861 tree decl
, tree args
)
17863 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17865 /* If we a constrained-type-specifier cannot be deduced. */
17866 if (parser
->prevent_constrained_type_specifiers
)
17869 /* A constrained type specifier can only be found in an
17870 overload set or as a reference to a template declaration.
17872 FIXME: This might be masking a bug. It's possible that
17873 that the deduction below is causing template specializations
17874 to be formed with the wildcard as an argument. */
17875 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17878 /* Try to build a call expression that evaluates the
17879 concept. This can fail if the overload set refers
17880 only to non-templates. */
17881 tree placeholder
= build_nt (WILDCARD_DECL
);
17882 tree check
= build_concept_check (decl
, placeholder
, args
);
17883 if (check
== error_mark_node
)
17886 /* Deduce the checked constraint and the prototype parameter.
17888 FIXME: In certain cases, failure to deduce should be a
17889 diagnosable error. */
17892 if (!deduce_constrained_parameter (check
, conc
, proto
))
17895 /* In template parameter scope, this results in a constrained
17896 parameter. Return a descriptor of that parm. */
17897 if (processing_template_parmlist
)
17898 return build_constrained_parameter (conc
, proto
, args
);
17900 /* In a parameter-declaration-clause, constrained-type
17901 specifiers result in invented template parameters. */
17902 if (parser
->auto_is_implicit_function_template_parm_p
)
17904 tree x
= build_constrained_parameter (conc
, proto
, args
);
17905 return synthesize_implicit_template_parm (parser
, x
);
17909 /* Otherwise, we're in a context where the constrained
17910 type name is deduced and the constraint applies
17911 after deduction. */
17912 return make_constrained_auto (conc
, args
);
17918 /* If DECL refers to a concept, return a TYPE_DECL representing
17919 the result of using the constrained type specifier in the
17920 current context. DECL refers to a concept if
17922 - it is an overload set containing a function concept taking a single
17925 - it is a variable concept taking a single type argument. */
17928 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17931 && (TREE_CODE (decl
) == OVERLOAD
17932 || BASELINK_P (decl
)
17933 || variable_concept_p (decl
)))
17934 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17939 /* Check if DECL and ARGS form a partial-concept-id. If so,
17940 assign ID to the resulting constrained placeholder.
17942 Returns true if the partial-concept-id designates a placeholder
17943 and false otherwise. Note that *id is set to NULL_TREE in
17947 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17949 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17952 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17964 Returns a TYPE_DECL for the type. */
17967 cp_parser_nonclass_name (cp_parser
* parser
)
17972 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17973 identifier
= cp_parser_identifier (parser
);
17974 if (identifier
== error_mark_node
)
17975 return error_mark_node
;
17977 /* Look up the type-name. */
17978 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17980 type_decl
= strip_using_decl (type_decl
);
17982 /* If we found an overload set, then it may refer to a concept-name. */
17983 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17986 if (TREE_CODE (type_decl
) != TYPE_DECL
17987 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17989 /* See if this is an Objective-C type. */
17990 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17991 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17993 type_decl
= TYPE_NAME (type
);
17996 /* Issue an error if we did not find a type-name. */
17997 if (TREE_CODE (type_decl
) != TYPE_DECL
17998 /* In Objective-C, we have the complication that class names are
17999 normally type names and start declarations (eg, the
18000 "NSObject" in "NSObject *object;"), but can be used in an
18001 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18002 is an expression. So, a classname followed by a dot is not a
18003 valid type-name. */
18004 || (objc_is_class_name (TREE_TYPE (type_decl
))
18005 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18007 if (!cp_parser_simulate_error (parser
))
18008 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18009 NLE_TYPE
, token
->location
);
18010 return error_mark_node
;
18012 /* Remember that the name was used in the definition of the
18013 current class so that we can check later to see if the
18014 meaning would have been different after the class was
18015 entirely defined. */
18016 else if (type_decl
!= error_mark_node
18018 maybe_note_name_used_in_class (identifier
, type_decl
);
18023 /* Parse an elaborated-type-specifier. Note that the grammar given
18024 here incorporates the resolution to DR68.
18026 elaborated-type-specifier:
18027 class-key :: [opt] nested-name-specifier [opt] identifier
18028 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18029 enum-key :: [opt] nested-name-specifier [opt] identifier
18030 typename :: [opt] nested-name-specifier identifier
18031 typename :: [opt] nested-name-specifier template [opt]
18036 elaborated-type-specifier:
18037 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18038 class-key attributes :: [opt] nested-name-specifier [opt]
18039 template [opt] template-id
18040 enum attributes :: [opt] nested-name-specifier [opt] identifier
18042 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18043 declared `friend'. If IS_DECLARATION is TRUE, then this
18044 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18045 something is being declared.
18047 Returns the TYPE specified. */
18050 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18052 bool is_declaration
)
18054 enum tag_types tag_type
;
18056 tree type
= NULL_TREE
;
18057 tree attributes
= NULL_TREE
;
18059 cp_token
*token
= NULL
;
18061 /* See if we're looking at the `enum' keyword. */
18062 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18064 /* Consume the `enum' token. */
18065 cp_lexer_consume_token (parser
->lexer
);
18066 /* Remember that it's an enumeration type. */
18067 tag_type
= enum_type
;
18068 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18069 enums) is used here. */
18070 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18071 if (cp_parser_is_keyword (token
, RID_CLASS
)
18072 || cp_parser_is_keyword (token
, RID_STRUCT
))
18074 gcc_rich_location
richloc (token
->location
);
18075 richloc
.add_range (input_location
);
18076 richloc
.add_fixit_remove ();
18077 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18078 "a scoped enum must not use the %qD keyword",
18080 /* Consume the `struct' or `class' and parse it anyway. */
18081 cp_lexer_consume_token (parser
->lexer
);
18083 /* Parse the attributes. */
18084 attributes
= cp_parser_attributes_opt (parser
);
18086 /* Or, it might be `typename'. */
18087 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18090 /* Consume the `typename' token. */
18091 cp_lexer_consume_token (parser
->lexer
);
18092 /* Remember that it's a `typename' type. */
18093 tag_type
= typename_type
;
18095 /* Otherwise it must be a class-key. */
18098 tag_type
= cp_parser_class_key (parser
);
18099 if (tag_type
== none_type
)
18100 return error_mark_node
;
18101 /* Parse the attributes. */
18102 attributes
= cp_parser_attributes_opt (parser
);
18105 /* Look for the `::' operator. */
18106 globalscope
= cp_parser_global_scope_opt (parser
,
18107 /*current_scope_valid_p=*/false);
18108 /* Look for the nested-name-specifier. */
18109 tree nested_name_specifier
;
18110 if (tag_type
== typename_type
&& !globalscope
)
18112 nested_name_specifier
18113 = cp_parser_nested_name_specifier (parser
,
18114 /*typename_keyword_p=*/true,
18115 /*check_dependency_p=*/true,
18118 if (!nested_name_specifier
)
18119 return error_mark_node
;
18122 /* Even though `typename' is not present, the proposed resolution
18123 to Core Issue 180 says that in `class A<T>::B', `B' should be
18124 considered a type-name, even if `A<T>' is dependent. */
18125 nested_name_specifier
18126 = cp_parser_nested_name_specifier_opt (parser
,
18127 /*typename_keyword_p=*/true,
18128 /*check_dependency_p=*/true,
18131 /* For everything but enumeration types, consider a template-id.
18132 For an enumeration type, consider only a plain identifier. */
18133 if (tag_type
!= enum_type
)
18135 bool template_p
= false;
18138 /* Allow the `template' keyword. */
18139 template_p
= cp_parser_optional_template_keyword (parser
);
18140 /* If we didn't see `template', we don't know if there's a
18141 template-id or not. */
18143 cp_parser_parse_tentatively (parser
);
18144 /* The `template' keyword must follow a nested-name-specifier. */
18145 else if (!nested_name_specifier
)
18147 cp_parser_error (parser
, "%<template%> must follow a nested-"
18149 return error_mark_node
;
18152 /* Parse the template-id. */
18153 token
= cp_lexer_peek_token (parser
->lexer
);
18154 decl
= cp_parser_template_id (parser
, template_p
,
18155 /*check_dependency_p=*/true,
18158 /* If we didn't find a template-id, look for an ordinary
18160 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18162 /* We can get here when cp_parser_template_id, called by
18163 cp_parser_class_name with tag_type == none_type, succeeds
18164 and caches a BASELINK. Then, when called again here,
18165 instead of failing and returning an error_mark_node
18166 returns it (see template/typename17.C in C++11).
18167 ??? Could we diagnose this earlier? */
18168 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18170 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18171 type
= error_mark_node
;
18173 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18174 in effect, then we must assume that, upon instantiation, the
18175 template will correspond to a class. */
18176 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18177 && tag_type
== typename_type
)
18178 type
= make_typename_type (parser
->scope
, decl
,
18180 /*complain=*/tf_error
);
18181 /* If the `typename' keyword is in effect and DECL is not a type
18182 decl, then type is non existent. */
18183 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18185 else if (TREE_CODE (decl
) == TYPE_DECL
)
18187 type
= check_elaborated_type_specifier (tag_type
, decl
,
18188 /*allow_template_p=*/true);
18190 /* If the next token is a semicolon, this must be a specialization,
18191 instantiation, or friend declaration. Check the scope while we
18192 still know whether or not we had a nested-name-specifier. */
18193 if (type
!= error_mark_node
18194 && !nested_name_specifier
&& !is_friend
18195 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18196 check_unqualified_spec_or_inst (type
, token
->location
);
18198 else if (decl
== error_mark_node
)
18199 type
= error_mark_node
;
18204 token
= cp_lexer_peek_token (parser
->lexer
);
18205 identifier
= cp_parser_identifier (parser
);
18207 if (identifier
== error_mark_node
)
18209 parser
->scope
= NULL_TREE
;
18210 return error_mark_node
;
18213 /* For a `typename', we needn't call xref_tag. */
18214 if (tag_type
== typename_type
18215 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18216 return cp_parser_make_typename_type (parser
, identifier
,
18219 /* Template parameter lists apply only if we are not within a
18220 function parameter list. */
18221 bool template_parm_lists_apply
18222 = parser
->num_template_parameter_lists
;
18223 if (template_parm_lists_apply
)
18224 for (cp_binding_level
*s
= current_binding_level
;
18225 s
&& s
->kind
!= sk_template_parms
;
18226 s
= s
->level_chain
)
18227 if (s
->kind
== sk_function_parms
)
18228 template_parm_lists_apply
= false;
18230 /* Look up a qualified name in the usual way. */
18234 tree ambiguous_decls
;
18236 decl
= cp_parser_lookup_name (parser
, identifier
,
18238 /*is_template=*/false,
18239 /*is_namespace=*/false,
18240 /*check_dependency=*/true,
18244 /* If the lookup was ambiguous, an error will already have been
18246 if (ambiguous_decls
)
18247 return error_mark_node
;
18249 /* If we are parsing friend declaration, DECL may be a
18250 TEMPLATE_DECL tree node here. However, we need to check
18251 whether this TEMPLATE_DECL results in valid code. Consider
18252 the following example:
18255 template <class T> class C {};
18258 template <class T> friend class N::C; // #1, valid code
18260 template <class T> class Y {
18261 friend class N::C; // #2, invalid code
18264 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18265 name lookup of `N::C'. We see that friend declaration must
18266 be template for the code to be valid. Note that
18267 processing_template_decl does not work here since it is
18268 always 1 for the above two cases. */
18270 decl
= (cp_parser_maybe_treat_template_as_class
18271 (decl
, /*tag_name_p=*/is_friend
18272 && template_parm_lists_apply
));
18274 if (TREE_CODE (decl
) != TYPE_DECL
)
18276 cp_parser_diagnose_invalid_type_name (parser
,
18279 return error_mark_node
;
18282 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18284 bool allow_template
= (template_parm_lists_apply
18285 || DECL_SELF_REFERENCE_P (decl
));
18286 type
= check_elaborated_type_specifier (tag_type
, decl
,
18289 if (type
== error_mark_node
)
18290 return error_mark_node
;
18293 /* Forward declarations of nested types, such as
18298 are invalid unless all components preceding the final '::'
18299 are complete. If all enclosing types are complete, these
18300 declarations become merely pointless.
18302 Invalid forward declarations of nested types are errors
18303 caught elsewhere in parsing. Those that are pointless arrive
18306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18307 && !is_friend
&& !processing_explicit_instantiation
)
18308 warning (0, "declaration %qD does not declare anything", decl
);
18310 type
= TREE_TYPE (decl
);
18314 /* An elaborated-type-specifier sometimes introduces a new type and
18315 sometimes names an existing type. Normally, the rule is that it
18316 introduces a new type only if there is not an existing type of
18317 the same name already in scope. For example, given:
18320 void f() { struct S s; }
18322 the `struct S' in the body of `f' is the same `struct S' as in
18323 the global scope; the existing definition is used. However, if
18324 there were no global declaration, this would introduce a new
18325 local class named `S'.
18327 An exception to this rule applies to the following code:
18329 namespace N { struct S; }
18331 Here, the elaborated-type-specifier names a new type
18332 unconditionally; even if there is already an `S' in the
18333 containing scope this declaration names a new type.
18334 This exception only applies if the elaborated-type-specifier
18335 forms the complete declaration:
18339 A declaration consisting solely of `class-key identifier ;' is
18340 either a redeclaration of the name in the current scope or a
18341 forward declaration of the identifier as a class name. It
18342 introduces the name into the current scope.
18344 We are in this situation precisely when the next token is a `;'.
18346 An exception to the exception is that a `friend' declaration does
18347 *not* name a new type; i.e., given:
18349 struct S { friend struct T; };
18351 `T' is not a new type in the scope of `S'.
18353 Also, `new struct S' or `sizeof (struct S)' never results in the
18354 definition of a new type; a new type can only be declared in a
18355 declaration context. */
18361 /* Friends have special name lookup rules. */
18362 ts
= ts_within_enclosing_non_class
;
18363 else if (is_declaration
18364 && cp_lexer_next_token_is (parser
->lexer
,
18366 /* This is a `class-key identifier ;' */
18372 (template_parm_lists_apply
18373 && (cp_parser_next_token_starts_class_definition_p (parser
)
18374 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18375 /* An unqualified name was used to reference this type, so
18376 there were no qualifying templates. */
18377 if (template_parm_lists_apply
18378 && !cp_parser_check_template_parameters (parser
,
18379 /*num_templates=*/0,
18380 /*template_id*/false,
18382 /*declarator=*/NULL
))
18383 return error_mark_node
;
18384 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18388 if (type
== error_mark_node
)
18389 return error_mark_node
;
18391 /* Allow attributes on forward declarations of classes. */
18394 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18395 warning (OPT_Wattributes
,
18396 "attributes ignored on uninstantiated type");
18397 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18398 && ! processing_explicit_instantiation
)
18399 warning (OPT_Wattributes
,
18400 "attributes ignored on template instantiation");
18401 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18402 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18404 warning (OPT_Wattributes
,
18405 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18408 if (tag_type
!= enum_type
)
18410 /* Indicate whether this class was declared as a `class' or as a
18412 if (CLASS_TYPE_P (type
))
18413 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18414 cp_parser_check_class_key (tag_type
, type
);
18417 /* A "<" cannot follow an elaborated type specifier. If that
18418 happens, the user was probably trying to form a template-id. */
18419 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18425 /* Parse an enum-specifier.
18428 enum-head { enumerator-list [opt] }
18429 enum-head { enumerator-list , } [C++0x]
18432 enum-key identifier [opt] enum-base [opt]
18433 enum-key nested-name-specifier identifier enum-base [opt]
18438 enum struct [C++0x]
18441 : type-specifier-seq
18443 opaque-enum-specifier:
18444 enum-key identifier enum-base [opt] ;
18447 enum-key attributes[opt] identifier [opt] enum-base [opt]
18448 { enumerator-list [opt] }attributes[opt]
18449 enum-key attributes[opt] identifier [opt] enum-base [opt]
18450 { enumerator-list, }attributes[opt] [C++0x]
18452 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18453 if the token stream isn't an enum-specifier after all. */
18456 cp_parser_enum_specifier (cp_parser
* parser
)
18459 tree type
= NULL_TREE
;
18461 tree nested_name_specifier
= NULL_TREE
;
18463 bool scoped_enum_p
= false;
18464 bool has_underlying_type
= false;
18465 bool nested_being_defined
= false;
18466 bool new_value_list
= false;
18467 bool is_new_type
= false;
18468 bool is_unnamed
= false;
18469 tree underlying_type
= NULL_TREE
;
18470 cp_token
*type_start_token
= NULL
;
18471 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18473 parser
->colon_corrects_to_scope_p
= false;
18475 /* Parse tentatively so that we can back up if we don't find a
18477 cp_parser_parse_tentatively (parser
);
18479 /* Caller guarantees that the current token is 'enum', an identifier
18480 possibly follows, and the token after that is an opening brace.
18481 If we don't have an identifier, fabricate an anonymous name for
18482 the enumeration being defined. */
18483 cp_lexer_consume_token (parser
->lexer
);
18485 /* Parse the "class" or "struct", which indicates a scoped
18486 enumeration type in C++0x. */
18487 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18488 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18490 if (cxx_dialect
< cxx11
)
18491 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18493 /* Consume the `struct' or `class' token. */
18494 cp_lexer_consume_token (parser
->lexer
);
18496 scoped_enum_p
= true;
18499 attributes
= cp_parser_attributes_opt (parser
);
18501 /* Clear the qualification. */
18502 parser
->scope
= NULL_TREE
;
18503 parser
->qualifying_scope
= NULL_TREE
;
18504 parser
->object_scope
= NULL_TREE
;
18506 /* Figure out in what scope the declaration is being placed. */
18507 prev_scope
= current_scope ();
18509 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18511 push_deferring_access_checks (dk_no_check
);
18512 nested_name_specifier
18513 = cp_parser_nested_name_specifier_opt (parser
,
18514 /*typename_keyword_p=*/true,
18515 /*check_dependency_p=*/false,
18517 /*is_declaration=*/false);
18519 if (nested_name_specifier
)
18523 identifier
= cp_parser_identifier (parser
);
18524 name
= cp_parser_lookup_name (parser
, identifier
,
18526 /*is_template=*/false,
18527 /*is_namespace=*/false,
18528 /*check_dependency=*/true,
18529 /*ambiguous_decls=*/NULL
,
18531 if (name
&& name
!= error_mark_node
)
18533 type
= TREE_TYPE (name
);
18534 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18536 /* Are template enums allowed in ISO? */
18537 if (template_parm_scope_p ())
18538 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18539 "%qD is an enumeration template", name
);
18540 /* ignore a typename reference, for it will be solved by name
18545 else if (nested_name_specifier
== error_mark_node
)
18546 /* We already issued an error. */;
18549 error_at (type_start_token
->location
,
18550 "%qD does not name an enumeration in %qT",
18551 identifier
, nested_name_specifier
);
18552 nested_name_specifier
= error_mark_node
;
18557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18558 identifier
= cp_parser_identifier (parser
);
18561 identifier
= make_anon_name ();
18564 error_at (type_start_token
->location
,
18565 "unnamed scoped enum is not allowed");
18568 pop_deferring_access_checks ();
18570 /* Check for the `:' that denotes a specified underlying type in C++0x.
18571 Note that a ':' could also indicate a bitfield width, however. */
18572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18574 cp_decl_specifier_seq type_specifiers
;
18576 /* Consume the `:'. */
18577 cp_lexer_consume_token (parser
->lexer
);
18579 /* Parse the type-specifier-seq. */
18580 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18581 /*is_trailing_return=*/false,
18584 /* At this point this is surely not elaborated type specifier. */
18585 if (!cp_parser_parse_definitely (parser
))
18588 if (cxx_dialect
< cxx11
)
18589 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18591 has_underlying_type
= true;
18593 /* If that didn't work, stop. */
18594 if (type_specifiers
.type
!= error_mark_node
)
18596 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18597 /*initialized=*/0, NULL
);
18598 if (underlying_type
== error_mark_node
18599 || check_for_bare_parameter_packs (underlying_type
))
18600 underlying_type
= NULL_TREE
;
18604 /* Look for the `{' but don't consume it yet. */
18605 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18607 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18609 cp_parser_error (parser
, "expected %<{%>");
18610 if (has_underlying_type
)
18616 /* An opaque-enum-specifier must have a ';' here. */
18617 if ((scoped_enum_p
|| underlying_type
)
18618 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18620 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18621 if (has_underlying_type
)
18629 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18632 if (nested_name_specifier
)
18634 if (CLASS_TYPE_P (nested_name_specifier
))
18636 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18637 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18638 push_scope (nested_name_specifier
);
18640 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18642 push_nested_namespace (nested_name_specifier
);
18646 /* Issue an error message if type-definitions are forbidden here. */
18647 if (!cp_parser_check_type_definition (parser
))
18648 type
= error_mark_node
;
18650 /* Create the new type. We do this before consuming the opening
18651 brace so the enum will be recorded as being on the line of its
18652 tag (or the 'enum' keyword, if there is no tag). */
18653 type
= start_enum (identifier
, type
, underlying_type
,
18654 attributes
, scoped_enum_p
, &is_new_type
);
18656 /* If the next token is not '{' it is an opaque-enum-specifier or an
18657 elaborated-type-specifier. */
18658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18660 timevar_push (TV_PARSE_ENUM
);
18661 if (nested_name_specifier
18662 && nested_name_specifier
!= error_mark_node
)
18664 /* The following catches invalid code such as:
18665 enum class S<int>::E { A, B, C }; */
18666 if (!processing_specialization
18667 && CLASS_TYPE_P (nested_name_specifier
)
18668 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18669 error_at (type_start_token
->location
, "cannot add an enumerator "
18670 "list to a template instantiation");
18672 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18674 error_at (type_start_token
->location
,
18675 "%<%T::%E%> has not been declared",
18676 TYPE_CONTEXT (nested_name_specifier
),
18677 nested_name_specifier
);
18678 type
= error_mark_node
;
18680 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18681 && !CLASS_TYPE_P (nested_name_specifier
))
18683 error_at (type_start_token
->location
, "nested name specifier "
18684 "%qT for enum declaration does not name a class "
18685 "or namespace", nested_name_specifier
);
18686 type
= error_mark_node
;
18688 /* If that scope does not contain the scope in which the
18689 class was originally declared, the program is invalid. */
18690 else if (prev_scope
&& !is_ancestor (prev_scope
,
18691 nested_name_specifier
))
18693 if (at_namespace_scope_p ())
18694 error_at (type_start_token
->location
,
18695 "declaration of %qD in namespace %qD which does not "
18697 type
, prev_scope
, nested_name_specifier
);
18699 error_at (type_start_token
->location
,
18700 "declaration of %qD in %qD which does not "
18702 type
, prev_scope
, nested_name_specifier
);
18703 type
= error_mark_node
;
18705 /* If that scope is the scope where the declaration is being placed
18706 the program is invalid. */
18707 else if (CLASS_TYPE_P (nested_name_specifier
)
18708 && CLASS_TYPE_P (prev_scope
)
18709 && same_type_p (nested_name_specifier
, prev_scope
))
18711 permerror (type_start_token
->location
,
18712 "extra qualification not allowed");
18713 nested_name_specifier
= NULL_TREE
;
18718 begin_scope (sk_scoped_enum
, type
);
18720 /* Consume the opening brace. */
18721 matching_braces braces
;
18722 braces
.consume_open (parser
);
18724 if (type
== error_mark_node
)
18725 ; /* Nothing to add */
18726 else if (OPAQUE_ENUM_P (type
)
18727 || (cxx_dialect
> cxx98
&& processing_specialization
))
18729 new_value_list
= true;
18730 SET_OPAQUE_ENUM_P (type
, false);
18731 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18735 error_at (type_start_token
->location
,
18736 "multiple definition of %q#T", type
);
18737 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18738 "previous definition here");
18739 type
= error_mark_node
;
18742 if (type
== error_mark_node
)
18743 cp_parser_skip_to_end_of_block_or_statement (parser
);
18744 /* If the next token is not '}', then there are some enumerators. */
18745 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18747 if (is_unnamed
&& !scoped_enum_p
)
18748 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18749 "ISO C++ forbids empty unnamed enum");
18752 cp_parser_enumerator_list (parser
, type
);
18754 /* Consume the final '}'. */
18755 braces
.require_close (parser
);
18759 timevar_pop (TV_PARSE_ENUM
);
18763 /* If a ';' follows, then it is an opaque-enum-specifier
18764 and additional restrictions apply. */
18765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18768 error_at (type_start_token
->location
,
18769 "opaque-enum-specifier without name");
18770 else if (nested_name_specifier
)
18771 error_at (type_start_token
->location
,
18772 "opaque-enum-specifier must use a simple identifier");
18776 /* Look for trailing attributes to apply to this enumeration, and
18777 apply them if appropriate. */
18778 if (cp_parser_allow_gnu_extensions_p (parser
))
18780 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18781 cplus_decl_attributes (&type
,
18783 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18786 /* Finish up the enumeration. */
18787 if (type
!= error_mark_node
)
18789 if (new_value_list
)
18790 finish_enum_value_list (type
);
18792 finish_enum (type
);
18795 if (nested_name_specifier
)
18797 if (CLASS_TYPE_P (nested_name_specifier
))
18799 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18800 pop_scope (nested_name_specifier
);
18802 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18804 pop_nested_namespace (nested_name_specifier
);
18808 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18812 /* Parse an enumerator-list. The enumerators all have the indicated
18816 enumerator-definition
18817 enumerator-list , enumerator-definition */
18820 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18824 /* Parse an enumerator-definition. */
18825 cp_parser_enumerator_definition (parser
, type
);
18827 /* If the next token is not a ',', we've reached the end of
18829 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18831 /* Otherwise, consume the `,' and keep going. */
18832 cp_lexer_consume_token (parser
->lexer
);
18833 /* If the next token is a `}', there is a trailing comma. */
18834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18836 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18837 pedwarn (input_location
, OPT_Wpedantic
,
18838 "comma at end of enumerator list");
18844 /* Parse an enumerator-definition. The enumerator has the indicated
18847 enumerator-definition:
18849 enumerator = constant-expression
18856 enumerator-definition:
18857 enumerator attributes [opt]
18858 enumerator attributes [opt] = constant-expression */
18861 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18867 /* Save the input location because we are interested in the location
18868 of the identifier and not the location of the explicit value. */
18869 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18871 /* Look for the identifier. */
18872 identifier
= cp_parser_identifier (parser
);
18873 if (identifier
== error_mark_node
)
18876 /* Parse any specified attributes. */
18877 tree attrs
= cp_parser_attributes_opt (parser
);
18879 /* If the next token is an '=', then there is an explicit value. */
18880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18882 /* Consume the `=' token. */
18883 cp_lexer_consume_token (parser
->lexer
);
18884 /* Parse the value. */
18885 value
= cp_parser_constant_expression (parser
);
18890 /* If we are processing a template, make sure the initializer of the
18891 enumerator doesn't contain any bare template parameter pack. */
18892 if (check_for_bare_parameter_packs (value
))
18893 value
= error_mark_node
;
18895 /* Create the enumerator. */
18896 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18899 /* Parse a namespace-name.
18902 original-namespace-name
18905 Returns the NAMESPACE_DECL for the namespace. */
18908 cp_parser_namespace_name (cp_parser
* parser
)
18911 tree namespace_decl
;
18913 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18915 /* Get the name of the namespace. */
18916 identifier
= cp_parser_identifier (parser
);
18917 if (identifier
== error_mark_node
)
18918 return error_mark_node
;
18920 /* Look up the identifier in the currently active scope. Look only
18921 for namespaces, due to:
18923 [basic.lookup.udir]
18925 When looking up a namespace-name in a using-directive or alias
18926 definition, only namespace names are considered.
18930 [basic.lookup.qual]
18932 During the lookup of a name preceding the :: scope resolution
18933 operator, object, function, and enumerator names are ignored.
18935 (Note that cp_parser_qualifying_entity only calls this
18936 function if the token after the name is the scope resolution
18938 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18940 /*is_template=*/false,
18941 /*is_namespace=*/true,
18942 /*check_dependency=*/true,
18943 /*ambiguous_decls=*/NULL
,
18945 /* If it's not a namespace, issue an error. */
18946 if (namespace_decl
== error_mark_node
18947 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18949 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18951 auto_diagnostic_group d
;
18953 if (namespace_decl
== error_mark_node
18954 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18955 hint
= suggest_alternative_in_explicit_scope (token
->location
,
18958 if (const char *suggestion
= hint
.suggestion ())
18960 gcc_rich_location
richloc (token
->location
);
18961 richloc
.add_fixit_replace (suggestion
);
18962 error_at (&richloc
,
18963 "%qD is not a namespace-name; did you mean %qs?",
18964 identifier
, suggestion
);
18967 error_at (token
->location
, "%qD is not a namespace-name",
18971 cp_parser_error (parser
, "expected namespace-name");
18972 namespace_decl
= error_mark_node
;
18975 return namespace_decl
;
18978 /* Parse a namespace-definition.
18980 namespace-definition:
18981 named-namespace-definition
18982 unnamed-namespace-definition
18984 named-namespace-definition:
18985 original-namespace-definition
18986 extension-namespace-definition
18988 original-namespace-definition:
18989 namespace identifier { namespace-body }
18991 extension-namespace-definition:
18992 namespace original-namespace-name { namespace-body }
18994 unnamed-namespace-definition:
18995 namespace { namespace-body } */
18998 cp_parser_namespace_definition (cp_parser
* parser
)
19001 int nested_definition_count
= 0;
19003 cp_ensure_no_omp_declare_simd (parser
);
19004 cp_ensure_no_oacc_routine (parser
);
19006 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19007 const bool topmost_inline_p
= is_inline
;
19011 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19012 cp_lexer_consume_token (parser
->lexer
);
19015 /* Look for the `namespace' keyword. */
19017 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19019 /* Parse any specified attributes before the identifier. */
19020 tree attribs
= cp_parser_attributes_opt (parser
);
19024 identifier
= NULL_TREE
;
19026 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19028 if (nested_inline_p
&& nested_definition_count
!= 0)
19030 if (cxx_dialect
< cxx2a
)
19031 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19032 OPT_Wpedantic
, "nested inline namespace definitions only "
19033 "available with -std=c++2a or -std=gnu++2a");
19034 cp_lexer_consume_token (parser
->lexer
);
19037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19039 identifier
= cp_parser_identifier (parser
);
19041 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19042 pedwarn (input_location
, OPT_Wpedantic
,
19043 "standard attributes on namespaces must precede "
19044 "the namespace name");
19046 /* Parse any attributes specified after the identifier. */
19047 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19050 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19052 /* Don't forget that the innermost namespace might have been
19053 marked as inline. Use |= because we cannot overwrite
19054 IS_INLINE in case the outermost namespace is inline, but
19055 there are no nested inlines. */
19056 is_inline
|= nested_inline_p
;
19060 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19061 pedwarn (input_location
, OPT_Wpedantic
,
19062 "nested namespace definitions only available with "
19063 "-std=c++17 or -std=gnu++17");
19065 /* Nested namespace names can create new namespaces (unlike
19066 other qualified-ids). */
19067 if (int count
= (identifier
19068 ? push_namespace (identifier
, nested_inline_p
)
19070 nested_definition_count
+= count
;
19072 cp_parser_error (parser
, "nested namespace name required");
19073 cp_lexer_consume_token (parser
->lexer
);
19076 if (nested_definition_count
&& !identifier
)
19077 cp_parser_error (parser
, "namespace name required");
19079 if (nested_definition_count
&& attribs
)
19080 error_at (token
->location
,
19081 "a nested namespace definition cannot have attributes");
19082 if (nested_definition_count
&& topmost_inline_p
)
19083 error_at (token
->location
,
19084 "a nested namespace definition cannot be inline");
19086 /* Start the namespace. */
19087 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19089 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19091 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19093 /* Look for the `{' to validate starting the namespace. */
19094 matching_braces braces
;
19095 if (braces
.require_open (parser
))
19097 /* Parse the body of the namespace. */
19098 cp_parser_namespace_body (parser
);
19100 /* Look for the final `}'. */
19101 braces
.require_close (parser
);
19104 if (has_visibility
)
19105 pop_visibility (1);
19107 /* Pop the nested namespace definitions. */
19108 while (nested_definition_count
--)
19112 /* Parse a namespace-body.
19115 declaration-seq [opt] */
19118 cp_parser_namespace_body (cp_parser
* parser
)
19120 cp_parser_declaration_seq_opt (parser
);
19123 /* Parse a namespace-alias-definition.
19125 namespace-alias-definition:
19126 namespace identifier = qualified-namespace-specifier ; */
19129 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19132 tree namespace_specifier
;
19134 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19136 /* Look for the `namespace' keyword. */
19137 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19138 /* Look for the identifier. */
19139 identifier
= cp_parser_identifier (parser
);
19140 if (identifier
== error_mark_node
)
19142 /* Look for the `=' token. */
19143 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19144 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19146 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19147 /* Skip the definition. */
19148 cp_lexer_consume_token (parser
->lexer
);
19149 if (cp_parser_skip_to_closing_brace (parser
))
19150 cp_lexer_consume_token (parser
->lexer
);
19153 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19154 /* Look for the qualified-namespace-specifier. */
19155 namespace_specifier
19156 = cp_parser_qualified_namespace_specifier (parser
);
19157 /* Look for the `;' token. */
19158 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19160 /* Register the alias in the symbol table. */
19161 do_namespace_alias (identifier
, namespace_specifier
);
19164 /* Parse a qualified-namespace-specifier.
19166 qualified-namespace-specifier:
19167 :: [opt] nested-name-specifier [opt] namespace-name
19169 Returns a NAMESPACE_DECL corresponding to the specified
19173 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19175 /* Look for the optional `::'. */
19176 cp_parser_global_scope_opt (parser
,
19177 /*current_scope_valid_p=*/false);
19179 /* Look for the optional nested-name-specifier. */
19180 cp_parser_nested_name_specifier_opt (parser
,
19181 /*typename_keyword_p=*/false,
19182 /*check_dependency_p=*/true,
19184 /*is_declaration=*/true);
19186 return cp_parser_namespace_name (parser
);
19189 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19190 access declaration.
19193 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19194 using :: unqualified-id ;
19196 access-declaration:
19202 cp_parser_using_declaration (cp_parser
* parser
,
19203 bool access_declaration_p
)
19206 bool typename_p
= false;
19207 bool global_scope_p
;
19211 int oldcount
= errorcount
;
19212 cp_token
*diag_token
= NULL
;
19214 if (access_declaration_p
)
19216 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19217 cp_parser_parse_tentatively (parser
);
19221 /* Look for the `using' keyword. */
19222 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19225 /* Peek at the next token. */
19226 token
= cp_lexer_peek_token (parser
->lexer
);
19227 /* See if it's `typename'. */
19228 if (token
->keyword
== RID_TYPENAME
)
19230 /* Remember that we've seen it. */
19232 /* Consume the `typename' token. */
19233 cp_lexer_consume_token (parser
->lexer
);
19237 /* Look for the optional global scope qualification. */
19239 = (cp_parser_global_scope_opt (parser
,
19240 /*current_scope_valid_p=*/false)
19243 /* If we saw `typename', or didn't see `::', then there must be a
19244 nested-name-specifier present. */
19245 if (typename_p
|| !global_scope_p
)
19247 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19248 /*check_dependency_p=*/true,
19250 /*is_declaration=*/true);
19251 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19253 cp_parser_skip_to_end_of_block_or_statement (parser
);
19257 /* Otherwise, we could be in either of the two productions. In that
19258 case, treat the nested-name-specifier as optional. */
19260 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19261 /*typename_keyword_p=*/false,
19262 /*check_dependency_p=*/true,
19264 /*is_declaration=*/true);
19266 qscope
= global_namespace
;
19267 else if (UNSCOPED_ENUM_P (qscope
))
19268 qscope
= CP_TYPE_CONTEXT (qscope
);
19270 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19271 /* Something has already gone wrong; there's no need to parse
19272 further. Since an error has occurred, the return value of
19273 cp_parser_parse_definitely will be false, as required. */
19274 return cp_parser_parse_definitely (parser
);
19276 token
= cp_lexer_peek_token (parser
->lexer
);
19277 /* Parse the unqualified-id. */
19278 identifier
= cp_parser_unqualified_id (parser
,
19279 /*template_keyword_p=*/false,
19280 /*check_dependency_p=*/true,
19281 /*declarator_p=*/true,
19282 /*optional_p=*/false);
19284 if (access_declaration_p
)
19286 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19287 cp_parser_simulate_error (parser
);
19288 if (!cp_parser_parse_definitely (parser
))
19291 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19293 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19294 if (cxx_dialect
< cxx17
19295 && !in_system_header_at (ell
->location
))
19296 pedwarn (ell
->location
, 0,
19297 "pack expansion in using-declaration only available "
19298 "with -std=c++17 or -std=gnu++17");
19299 qscope
= make_pack_expansion (qscope
);
19302 /* The function we call to handle a using-declaration is different
19303 depending on what scope we are in. */
19304 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19306 else if (!identifier_p (identifier
)
19307 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19308 /* [namespace.udecl]
19310 A using declaration shall not name a template-id. */
19311 error_at (token
->location
,
19312 "a template-id may not appear in a using-declaration");
19315 if (at_class_scope_p ())
19317 /* Create the USING_DECL. */
19318 decl
= do_class_using_decl (qscope
, identifier
);
19320 if (decl
&& typename_p
)
19321 USING_DECL_TYPENAME_P (decl
) = 1;
19323 if (check_for_bare_parameter_packs (decl
))
19325 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19329 /* Add it to the list of members in this class. */
19330 finish_member_declaration (decl
);
19334 decl
= cp_parser_lookup_name_simple (parser
,
19337 if (decl
== error_mark_node
)
19338 cp_parser_name_lookup_error (parser
, identifier
,
19341 else if (check_for_bare_parameter_packs (decl
))
19343 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19346 else if (!at_namespace_scope_p ())
19347 finish_local_using_decl (decl
, qscope
, identifier
);
19349 finish_namespace_using_decl (decl
, qscope
, identifier
);
19353 if (!access_declaration_p
19354 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19356 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19357 if (cxx_dialect
< cxx17
)
19358 pedwarn (comma
->location
, 0,
19359 "comma-separated list in using-declaration only available "
19360 "with -std=c++17 or -std=gnu++17");
19364 /* Look for the final `;'. */
19365 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19367 if (access_declaration_p
&& errorcount
== oldcount
)
19368 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19369 "access declarations are deprecated "
19370 "in favour of using-declarations; "
19371 "suggestion: add the %<using%> keyword");
19376 /* Parse an alias-declaration.
19379 using identifier attribute-specifier-seq [opt] = type-id */
19382 cp_parser_alias_declaration (cp_parser
* parser
)
19384 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19385 location_t id_location
, type_location
;
19386 cp_declarator
*declarator
;
19387 cp_decl_specifier_seq decl_specs
;
19389 const char *saved_message
= NULL
;
19391 /* Look for the `using' keyword. */
19392 cp_token
*using_token
19393 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19394 if (using_token
== NULL
)
19395 return error_mark_node
;
19397 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19398 id
= cp_parser_identifier (parser
);
19399 if (id
== error_mark_node
)
19400 return error_mark_node
;
19402 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19403 attributes
= cp_parser_attributes_opt (parser
);
19404 if (attributes
== error_mark_node
)
19405 return error_mark_node
;
19407 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19409 if (cp_parser_error_occurred (parser
))
19410 return error_mark_node
;
19412 cp_parser_commit_to_tentative_parse (parser
);
19414 /* Now we are going to parse the type-id of the declaration. */
19419 "A type-specifier-seq shall not define a class or enumeration
19420 unless it appears in the type-id of an alias-declaration (7.1.3) that
19421 is not the declaration of a template-declaration."
19423 In other words, if we currently are in an alias template, the
19424 type-id should not define a type.
19426 So let's set parser->type_definition_forbidden_message in that
19427 case; cp_parser_check_type_definition (called by
19428 cp_parser_class_specifier) will then emit an error if a type is
19429 defined in the type-id. */
19430 if (parser
->num_template_parameter_lists
)
19432 saved_message
= parser
->type_definition_forbidden_message
;
19433 parser
->type_definition_forbidden_message
=
19434 G_("types may not be defined in alias template declarations");
19437 type
= cp_parser_type_id (parser
, &type_location
);
19439 /* Restore the error message if need be. */
19440 if (parser
->num_template_parameter_lists
)
19441 parser
->type_definition_forbidden_message
= saved_message
;
19443 if (type
== error_mark_node
19444 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19446 cp_parser_skip_to_end_of_block_or_statement (parser
);
19447 return error_mark_node
;
19450 /* A typedef-name can also be introduced by an alias-declaration. The
19451 identifier following the using keyword becomes a typedef-name. It has
19452 the same semantics as if it were introduced by the typedef
19453 specifier. In particular, it does not define a new type and it shall
19454 not appear in the type-id. */
19456 clear_decl_specs (&decl_specs
);
19457 decl_specs
.type
= type
;
19458 if (attributes
!= NULL_TREE
)
19460 decl_specs
.attributes
= attributes
;
19461 set_and_check_decl_spec_loc (&decl_specs
,
19465 set_and_check_decl_spec_loc (&decl_specs
,
19468 set_and_check_decl_spec_loc (&decl_specs
,
19471 decl_specs
.locations
[ds_type_spec
] = type_location
;
19473 if (parser
->num_template_parameter_lists
19474 && !cp_parser_check_template_parameters (parser
,
19475 /*num_templates=*/0,
19476 /*template_id*/false,
19478 /*declarator=*/NULL
))
19479 return error_mark_node
;
19481 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19483 member_p
= at_class_scope_p ();
19485 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19486 NULL_TREE
, attributes
);
19488 decl
= start_decl (declarator
, &decl_specs
, 0,
19489 attributes
, NULL_TREE
, &pushed_scope
);
19490 if (decl
== error_mark_node
)
19493 // Attach constraints to the alias declaration.
19494 if (flag_concepts
&& current_template_parms
)
19496 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19497 tree constr
= build_constraints (reqs
, NULL_TREE
);
19498 set_constraints (decl
, constr
);
19501 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19504 pop_scope (pushed_scope
);
19506 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19507 added into the symbol table; otherwise, return the TYPE_DECL. */
19508 if (DECL_LANG_SPECIFIC (decl
)
19509 && DECL_TEMPLATE_INFO (decl
)
19510 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19512 decl
= DECL_TI_TEMPLATE (decl
);
19514 check_member_template (decl
);
19520 /* Parse a using-directive.
19523 using namespace :: [opt] nested-name-specifier [opt]
19524 namespace-name ; */
19527 cp_parser_using_directive (cp_parser
* parser
)
19529 tree namespace_decl
;
19532 /* Look for the `using' keyword. */
19533 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19534 /* And the `namespace' keyword. */
19535 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19536 /* Look for the optional `::' operator. */
19537 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19538 /* And the optional nested-name-specifier. */
19539 cp_parser_nested_name_specifier_opt (parser
,
19540 /*typename_keyword_p=*/false,
19541 /*check_dependency_p=*/true,
19543 /*is_declaration=*/true);
19544 /* Get the namespace being used. */
19545 namespace_decl
= cp_parser_namespace_name (parser
);
19546 /* And any specified attributes. */
19547 attribs
= cp_parser_attributes_opt (parser
);
19549 /* Update the symbol table. */
19550 if (namespace_bindings_p ())
19551 finish_namespace_using_directive (namespace_decl
, attribs
);
19553 finish_local_using_directive (namespace_decl
, attribs
);
19555 /* Look for the final `;'. */
19556 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19559 /* Parse an asm-definition.
19562 asm ( string-literal ) ;
19567 asm volatile [opt] ( string-literal ) ;
19568 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19569 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19570 : asm-operand-list [opt] ) ;
19571 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19572 : asm-operand-list [opt]
19573 : asm-clobber-list [opt] ) ;
19574 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19575 : asm-clobber-list [opt]
19576 : asm-goto-list ) ; */
19579 cp_parser_asm_definition (cp_parser
* parser
)
19582 tree outputs
= NULL_TREE
;
19583 tree inputs
= NULL_TREE
;
19584 tree clobbers
= NULL_TREE
;
19585 tree labels
= NULL_TREE
;
19587 bool volatile_p
= false;
19588 bool extended_p
= false;
19589 bool invalid_inputs_p
= false;
19590 bool invalid_outputs_p
= false;
19591 bool goto_p
= false;
19592 required_token missing
= RT_NONE
;
19594 /* Look for the `asm' keyword. */
19595 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19597 if (parser
->in_function_body
19598 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19600 error ("%<asm%> in %<constexpr%> function");
19601 cp_function_chain
->invalid_constexpr
= true;
19604 /* See if the next token is `volatile'. */
19605 if (cp_parser_allow_gnu_extensions_p (parser
)
19606 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19608 /* Remember that we saw the `volatile' keyword. */
19610 /* Consume the token. */
19611 cp_lexer_consume_token (parser
->lexer
);
19613 if (cp_parser_allow_gnu_extensions_p (parser
)
19614 && parser
->in_function_body
19615 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19617 /* Remember that we saw the `goto' keyword. */
19619 /* Consume the token. */
19620 cp_lexer_consume_token (parser
->lexer
);
19622 /* Look for the opening `('. */
19623 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19625 /* Look for the string. */
19626 string
= cp_parser_string_literal (parser
, false, false);
19627 if (string
== error_mark_node
)
19629 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19630 /*consume_paren=*/true);
19634 /* If we're allowing GNU extensions, check for the extended assembly
19635 syntax. Unfortunately, the `:' tokens need not be separated by
19636 a space in C, and so, for compatibility, we tolerate that here
19637 too. Doing that means that we have to treat the `::' operator as
19639 if (cp_parser_allow_gnu_extensions_p (parser
)
19640 && parser
->in_function_body
19641 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19642 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19644 bool inputs_p
= false;
19645 bool clobbers_p
= false;
19646 bool labels_p
= false;
19648 /* The extended syntax was used. */
19651 /* Look for outputs. */
19652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19654 /* Consume the `:'. */
19655 cp_lexer_consume_token (parser
->lexer
);
19656 /* Parse the output-operands. */
19657 if (cp_lexer_next_token_is_not (parser
->lexer
,
19659 && cp_lexer_next_token_is_not (parser
->lexer
,
19661 && cp_lexer_next_token_is_not (parser
->lexer
,
19665 outputs
= cp_parser_asm_operand_list (parser
);
19666 if (outputs
== error_mark_node
)
19667 invalid_outputs_p
= true;
19670 /* If the next token is `::', there are no outputs, and the
19671 next token is the beginning of the inputs. */
19672 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19673 /* The inputs are coming next. */
19676 /* Look for inputs. */
19678 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19680 /* Consume the `:' or `::'. */
19681 cp_lexer_consume_token (parser
->lexer
);
19682 /* Parse the output-operands. */
19683 if (cp_lexer_next_token_is_not (parser
->lexer
,
19685 && cp_lexer_next_token_is_not (parser
->lexer
,
19687 && cp_lexer_next_token_is_not (parser
->lexer
,
19690 inputs
= cp_parser_asm_operand_list (parser
);
19691 if (inputs
== error_mark_node
)
19692 invalid_inputs_p
= true;
19695 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19696 /* The clobbers are coming next. */
19699 /* Look for clobbers. */
19701 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19704 /* Consume the `:' or `::'. */
19705 cp_lexer_consume_token (parser
->lexer
);
19706 /* Parse the clobbers. */
19707 if (cp_lexer_next_token_is_not (parser
->lexer
,
19709 && cp_lexer_next_token_is_not (parser
->lexer
,
19711 clobbers
= cp_parser_asm_clobber_list (parser
);
19714 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19715 /* The labels are coming next. */
19718 /* Look for labels. */
19720 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19723 /* Consume the `:' or `::'. */
19724 cp_lexer_consume_token (parser
->lexer
);
19725 /* Parse the labels. */
19726 labels
= cp_parser_asm_label_list (parser
);
19729 if (goto_p
&& !labels_p
)
19730 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19733 missing
= RT_COLON_SCOPE
;
19735 /* Look for the closing `)'. */
19736 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19737 missing
? missing
: RT_CLOSE_PAREN
))
19738 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19739 /*consume_paren=*/true);
19740 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19742 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19744 /* Create the ASM_EXPR. */
19745 if (parser
->in_function_body
)
19747 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19748 inputs
, clobbers
, labels
);
19749 /* If the extended syntax was not used, mark the ASM_EXPR. */
19752 tree temp
= asm_stmt
;
19753 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19754 temp
= TREE_OPERAND (temp
, 0);
19756 ASM_INPUT_P (temp
) = 1;
19760 symtab
->finalize_toplevel_asm (string
);
19764 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19765 type that comes from the decl-specifier-seq. */
19768 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19770 for (cp_declarator
*d
= declarator
; d
;)
19780 if (TYPE_PTRMEMFUNC_P (type
))
19781 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19782 type
= TREE_TYPE (type
);
19790 /* Declarators [gram.dcl.decl] */
19792 /* Parse an init-declarator.
19795 declarator initializer [opt]
19800 declarator asm-specification [opt] attributes [opt] initializer [opt]
19802 function-definition:
19803 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19805 decl-specifier-seq [opt] declarator function-try-block
19809 function-definition:
19810 __extension__ function-definition
19814 function-definition:
19815 decl-specifier-seq [opt] declarator function-transaction-block
19817 The DECL_SPECIFIERS apply to this declarator. Returns a
19818 representation of the entity declared. If MEMBER_P is TRUE, then
19819 this declarator appears in a class scope. The new DECL created by
19820 this declarator is returned.
19822 The CHECKS are access checks that should be performed once we know
19823 what entity is being declared (and, therefore, what classes have
19826 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19827 for a function-definition here as well. If the declarator is a
19828 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19829 be TRUE upon return. By that point, the function-definition will
19830 have been completely parsed.
19832 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19835 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19836 parsed declaration if it is an uninitialized single declarator not followed
19837 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19838 if present, will not be consumed. If returned, this declarator will be
19839 created with SD_INITIALIZED but will not call cp_finish_decl.
19841 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19842 and there is an initializer, the pointed location_t is set to the
19843 location of the '=' or `(', or '{' in C++11 token introducing the
19847 cp_parser_init_declarator (cp_parser
* parser
,
19848 cp_decl_specifier_seq
*decl_specifiers
,
19849 vec
<deferred_access_check
, va_gc
> *checks
,
19850 bool function_definition_allowed_p
,
19852 int declares_class_or_enum
,
19853 bool* function_definition_p
,
19854 tree
* maybe_range_for_decl
,
19855 location_t
* init_loc
,
19858 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19859 *attributes_start_token
= NULL
;
19860 cp_declarator
*declarator
;
19861 tree prefix_attributes
;
19862 tree attributes
= NULL
;
19863 tree asm_specification
;
19865 tree decl
= NULL_TREE
;
19867 int is_initialized
;
19868 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19869 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19871 enum cpp_ttype initialization_kind
;
19872 bool is_direct_init
= false;
19873 bool is_non_constant_init
;
19874 int ctor_dtor_or_conv_p
;
19875 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19876 tree pushed_scope
= NULL_TREE
;
19877 bool range_for_decl_p
= false;
19878 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19879 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19881 /* Gather the attributes that were provided with the
19882 decl-specifiers. */
19883 prefix_attributes
= decl_specifiers
->attributes
;
19885 /* Assume that this is not the declarator for a function
19887 if (function_definition_p
)
19888 *function_definition_p
= false;
19890 /* Default arguments are only permitted for function parameters. */
19891 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19892 parser
->default_arg_ok_p
= false;
19894 /* Defer access checks while parsing the declarator; we cannot know
19895 what names are accessible until we know what is being
19897 resume_deferring_access_checks ();
19899 token
= cp_lexer_peek_token (parser
->lexer
);
19901 /* Parse the declarator. */
19903 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19904 &ctor_dtor_or_conv_p
,
19905 /*parenthesized_p=*/NULL
,
19906 member_p
, friend_p
);
19907 /* Gather up the deferred checks. */
19908 stop_deferring_access_checks ();
19910 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19912 /* If the DECLARATOR was erroneous, there's no need to go
19914 if (declarator
== cp_error_declarator
)
19915 return error_mark_node
;
19917 /* Check that the number of template-parameter-lists is OK. */
19918 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19920 return error_mark_node
;
19922 if (declares_class_or_enum
& 2)
19923 cp_parser_check_for_definition_in_return_type (declarator
,
19924 decl_specifiers
->type
,
19925 decl_specifiers
->locations
[ds_type_spec
]);
19927 /* Figure out what scope the entity declared by the DECLARATOR is
19928 located in. `grokdeclarator' sometimes changes the scope, so
19929 we compute it now. */
19930 scope
= get_scope_of_declarator (declarator
);
19932 /* Perform any lookups in the declared type which were thought to be
19933 dependent, but are not in the scope of the declarator. */
19934 decl_specifiers
->type
19935 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19937 /* If we're allowing GNU extensions, look for an
19938 asm-specification. */
19939 if (cp_parser_allow_gnu_extensions_p (parser
))
19941 /* Look for an asm-specification. */
19942 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19943 asm_specification
= cp_parser_asm_specification_opt (parser
);
19946 asm_specification
= NULL_TREE
;
19948 /* Look for attributes. */
19949 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19950 attributes
= cp_parser_attributes_opt (parser
);
19952 /* Peek at the next token. */
19953 token
= cp_lexer_peek_token (parser
->lexer
);
19955 bool bogus_implicit_tmpl
= false;
19957 if (function_declarator_p (declarator
))
19959 /* Handle C++17 deduction guides. */
19960 if (!decl_specifiers
->type
19961 && ctor_dtor_or_conv_p
<= 0
19962 && cxx_dialect
>= cxx17
)
19964 cp_declarator
*id
= get_id_declarator (declarator
);
19965 tree name
= id
->u
.id
.unqualified_name
;
19966 parser
->scope
= id
->u
.id
.qualifying_scope
;
19967 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19969 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19970 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19972 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19973 id
->u
.id
.sfk
= sfk_deduction_guide
;
19974 ctor_dtor_or_conv_p
= 1;
19978 /* Check to see if the token indicates the start of a
19979 function-definition. */
19980 if (cp_parser_token_starts_function_definition_p (token
))
19982 if (!function_definition_allowed_p
)
19984 /* If a function-definition should not appear here, issue an
19986 cp_parser_error (parser
,
19987 "a function-definition is not allowed here");
19988 return error_mark_node
;
19991 location_t func_brace_location
19992 = cp_lexer_peek_token (parser
->lexer
)->location
;
19994 /* Neither attributes nor an asm-specification are allowed
19995 on a function-definition. */
19996 if (asm_specification
)
19997 error_at (asm_spec_start_token
->location
,
19998 "an asm-specification is not allowed "
19999 "on a function-definition");
20001 error_at (attributes_start_token
->location
,
20002 "attributes are not allowed "
20003 "on a function-definition");
20004 /* This is a function-definition. */
20005 *function_definition_p
= true;
20007 /* Parse the function definition. */
20009 decl
= cp_parser_save_member_function_body (parser
,
20012 prefix_attributes
);
20015 (cp_parser_function_definition_from_specifiers_and_declarator
20016 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20018 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20020 /* This is where the prologue starts... */
20021 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20022 = func_brace_location
;
20028 else if (parser
->fully_implicit_function_template_p
)
20030 /* A non-template declaration involving a function parameter list
20031 containing an implicit template parameter will be made into a
20032 template. If the resulting declaration is not going to be an
20033 actual function then finish the template scope here to prevent it.
20034 An error message will be issued once we have a decl to talk about.
20036 FIXME probably we should do type deduction rather than create an
20037 implicit template, but the standard currently doesn't allow it. */
20038 bogus_implicit_tmpl
= true;
20039 finish_fully_implicit_template (parser
, NULL_TREE
);
20044 Only in function declarations for constructors, destructors, type
20045 conversions, and deduction guides can the decl-specifier-seq be omitted.
20047 We explicitly postpone this check past the point where we handle
20048 function-definitions because we tolerate function-definitions
20049 that are missing their return types in some modes. */
20050 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20052 cp_parser_error (parser
,
20053 "expected constructor, destructor, or type conversion");
20054 return error_mark_node
;
20057 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20058 if (token
->type
== CPP_EQ
20059 || token
->type
== CPP_OPEN_PAREN
20060 || token
->type
== CPP_OPEN_BRACE
)
20062 is_initialized
= SD_INITIALIZED
;
20063 initialization_kind
= token
->type
;
20064 if (maybe_range_for_decl
)
20065 *maybe_range_for_decl
= error_mark_node
;
20066 tmp_init_loc
= token
->location
;
20067 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20068 *init_loc
= tmp_init_loc
;
20070 if (token
->type
== CPP_EQ
20071 && function_declarator_p (declarator
))
20073 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20074 if (t2
->keyword
== RID_DEFAULT
)
20075 is_initialized
= SD_DEFAULTED
;
20076 else if (t2
->keyword
== RID_DELETE
)
20077 is_initialized
= SD_DELETED
;
20082 /* If the init-declarator isn't initialized and isn't followed by a
20083 `,' or `;', it's not a valid init-declarator. */
20084 if (token
->type
!= CPP_COMMA
20085 && token
->type
!= CPP_SEMICOLON
)
20087 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20088 range_for_decl_p
= true;
20091 if (!maybe_range_for_decl
)
20092 cp_parser_error (parser
, "expected initializer");
20093 return error_mark_node
;
20096 is_initialized
= SD_UNINITIALIZED
;
20097 initialization_kind
= CPP_EOF
;
20100 /* Because start_decl has side-effects, we should only call it if we
20101 know we're going ahead. By this point, we know that we cannot
20102 possibly be looking at any other construct. */
20103 cp_parser_commit_to_tentative_parse (parser
);
20105 /* Enter the newly declared entry in the symbol table. If we're
20106 processing a declaration in a class-specifier, we wait until
20107 after processing the initializer. */
20110 if (parser
->in_unbraced_linkage_specification_p
)
20111 decl_specifiers
->storage_class
= sc_extern
;
20112 decl
= start_decl (declarator
, decl_specifiers
,
20113 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20114 attributes
, prefix_attributes
, &pushed_scope
);
20115 cp_finalize_omp_declare_simd (parser
, decl
);
20116 cp_finalize_oacc_routine (parser
, decl
, false);
20117 /* Adjust location of decl if declarator->id_loc is more appropriate:
20118 set, and decl wasn't merged with another decl, in which case its
20119 location would be different from input_location, and more accurate. */
20121 && declarator
->id_loc
!= UNKNOWN_LOCATION
20122 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20123 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20126 /* Enter the SCOPE. That way unqualified names appearing in the
20127 initializer will be looked up in SCOPE. */
20128 pushed_scope
= push_scope (scope
);
20130 /* Perform deferred access control checks, now that we know in which
20131 SCOPE the declared entity resides. */
20132 if (!member_p
&& decl
)
20134 tree saved_current_function_decl
= NULL_TREE
;
20136 /* If the entity being declared is a function, pretend that we
20137 are in its scope. If it is a `friend', it may have access to
20138 things that would not otherwise be accessible. */
20139 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20141 saved_current_function_decl
= current_function_decl
;
20142 current_function_decl
= decl
;
20145 /* Perform access checks for template parameters. */
20146 cp_parser_perform_template_parameter_access_checks (checks
);
20148 /* Perform the access control checks for the declarator and the
20149 decl-specifiers. */
20150 perform_deferred_access_checks (tf_warning_or_error
);
20152 /* Restore the saved value. */
20153 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20154 current_function_decl
= saved_current_function_decl
;
20157 /* Parse the initializer. */
20158 initializer
= NULL_TREE
;
20159 is_direct_init
= false;
20160 is_non_constant_init
= true;
20161 if (is_initialized
)
20163 if (function_declarator_p (declarator
))
20165 if (initialization_kind
== CPP_EQ
)
20166 initializer
= cp_parser_pure_specifier (parser
);
20169 /* If the declaration was erroneous, we don't really
20170 know what the user intended, so just silently
20171 consume the initializer. */
20172 if (decl
!= error_mark_node
)
20173 error_at (tmp_init_loc
, "initializer provided for function");
20174 cp_parser_skip_to_closing_parenthesis (parser
,
20175 /*recovering=*/true,
20176 /*or_comma=*/false,
20177 /*consume_paren=*/true);
20182 /* We want to record the extra mangling scope for in-class
20183 initializers of class members and initializers of static data
20184 member templates. The former involves deferring
20185 parsing of the initializer until end of class as with default
20186 arguments. So right here we only handle the latter. */
20187 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20188 start_lambda_scope (decl
);
20189 initializer
= cp_parser_initializer (parser
,
20191 &is_non_constant_init
);
20192 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20193 finish_lambda_scope ();
20194 if (initializer
== error_mark_node
)
20195 cp_parser_skip_to_end_of_statement (parser
);
20199 /* The old parser allows attributes to appear after a parenthesized
20200 initializer. Mark Mitchell proposed removing this functionality
20201 on the GCC mailing lists on 2002-08-13. This parser accepts the
20202 attributes -- but ignores them. Made a permerror in GCC 8. */
20203 if (cp_parser_allow_gnu_extensions_p (parser
)
20204 && initialization_kind
== CPP_OPEN_PAREN
20205 && cp_parser_attributes_opt (parser
)
20206 && permerror (input_location
,
20207 "attributes after parenthesized initializer ignored"))
20210 if (flag_permissive
&& !hint
)
20213 inform (input_location
,
20214 "this flexibility is deprecated and will be removed");
20218 /* And now complain about a non-function implicit template. */
20219 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20220 error_at (DECL_SOURCE_LOCATION (decl
),
20221 "non-function %qD declared as implicit template", decl
);
20223 /* For an in-class declaration, use `grokfield' to create the
20229 pop_scope (pushed_scope
);
20230 pushed_scope
= NULL_TREE
;
20232 decl
= grokfield (declarator
, decl_specifiers
,
20233 initializer
, !is_non_constant_init
,
20234 /*asmspec=*/NULL_TREE
,
20235 attr_chainon (attributes
, prefix_attributes
));
20236 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20237 cp_parser_save_default_args (parser
, decl
);
20238 cp_finalize_omp_declare_simd (parser
, decl
);
20239 cp_finalize_oacc_routine (parser
, decl
, false);
20242 /* Finish processing the declaration. But, skip member
20244 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20246 cp_finish_decl (decl
,
20247 initializer
, !is_non_constant_init
,
20249 /* If the initializer is in parentheses, then this is
20250 a direct-initialization, which means that an
20251 `explicit' constructor is OK. Otherwise, an
20252 `explicit' constructor cannot be used. */
20253 ((is_direct_init
|| !is_initialized
)
20254 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20256 else if ((cxx_dialect
!= cxx98
) && friend_p
20257 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20258 /* Core issue #226 (C++0x only): A default template-argument
20259 shall not be specified in a friend class template
20261 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20262 /*is_partial=*/false, /*is_friend_decl=*/1);
20264 if (!friend_p
&& pushed_scope
)
20265 pop_scope (pushed_scope
);
20267 if (function_declarator_p (declarator
)
20268 && parser
->fully_implicit_function_template_p
)
20271 decl
= finish_fully_implicit_template (parser
, decl
);
20273 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20276 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20277 && type_uses_auto (decl_specifiers
->type
))
20278 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20283 /* Parse a declarator.
20287 ptr-operator declarator
20289 abstract-declarator:
20290 ptr-operator abstract-declarator [opt]
20291 direct-abstract-declarator
20296 attributes [opt] direct-declarator
20297 attributes [opt] ptr-operator declarator
20299 abstract-declarator:
20300 attributes [opt] ptr-operator abstract-declarator [opt]
20301 attributes [opt] direct-abstract-declarator
20303 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20304 detect constructors, destructors, deduction guides, or conversion operators.
20305 It is set to -1 if the declarator is a name, and +1 if it is a
20306 function. Otherwise it is set to zero. Usually you just want to
20307 test for >0, but internally the negative value is used.
20309 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20310 a decl-specifier-seq unless it declares a constructor, destructor,
20311 or conversion. It might seem that we could check this condition in
20312 semantic analysis, rather than parsing, but that makes it difficult
20313 to handle something like `f()'. We want to notice that there are
20314 no decl-specifiers, and therefore realize that this is an
20315 expression, not a declaration.)
20317 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20318 the declarator is a direct-declarator of the form "(...)".
20320 MEMBER_P is true iff this declarator is a member-declarator.
20322 FRIEND_P is true iff this declarator is a friend. */
20324 static cp_declarator
*
20325 cp_parser_declarator (cp_parser
* parser
,
20326 cp_parser_declarator_kind dcl_kind
,
20327 int* ctor_dtor_or_conv_p
,
20328 bool* parenthesized_p
,
20329 bool member_p
, bool friend_p
)
20331 cp_declarator
*declarator
;
20332 enum tree_code code
;
20333 cp_cv_quals cv_quals
;
20335 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20337 /* Assume this is not a constructor, destructor, or type-conversion
20339 if (ctor_dtor_or_conv_p
)
20340 *ctor_dtor_or_conv_p
= 0;
20342 if (cp_parser_allow_gnu_extensions_p (parser
))
20343 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20345 /* Check for the ptr-operator production. */
20346 cp_parser_parse_tentatively (parser
);
20347 /* Parse the ptr-operator. */
20348 code
= cp_parser_ptr_operator (parser
,
20353 /* If that worked, then we have a ptr-operator. */
20354 if (cp_parser_parse_definitely (parser
))
20356 /* If a ptr-operator was found, then this declarator was not
20358 if (parenthesized_p
)
20359 *parenthesized_p
= true;
20360 /* The dependent declarator is optional if we are parsing an
20361 abstract-declarator. */
20362 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20363 cp_parser_parse_tentatively (parser
);
20365 /* Parse the dependent declarator. */
20366 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20367 /*ctor_dtor_or_conv_p=*/NULL
,
20368 /*parenthesized_p=*/NULL
,
20369 /*member_p=*/false,
20372 /* If we are parsing an abstract-declarator, we must handle the
20373 case where the dependent declarator is absent. */
20374 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20375 && !cp_parser_parse_definitely (parser
))
20378 declarator
= cp_parser_make_indirect_declarator
20379 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20381 /* Everything else is a direct-declarator. */
20384 if (parenthesized_p
)
20385 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20387 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20388 ctor_dtor_or_conv_p
,
20389 member_p
, friend_p
);
20392 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20393 declarator
->attributes
= gnu_attributes
;
20397 /* Parse a direct-declarator or direct-abstract-declarator.
20401 direct-declarator ( parameter-declaration-clause )
20402 cv-qualifier-seq [opt]
20403 ref-qualifier [opt]
20404 exception-specification [opt]
20405 direct-declarator [ constant-expression [opt] ]
20408 direct-abstract-declarator:
20409 direct-abstract-declarator [opt]
20410 ( parameter-declaration-clause )
20411 cv-qualifier-seq [opt]
20412 ref-qualifier [opt]
20413 exception-specification [opt]
20414 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20415 ( abstract-declarator )
20417 Returns a representation of the declarator. DCL_KIND is
20418 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20419 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20420 we are parsing a direct-declarator. It is
20421 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20422 of ambiguity we prefer an abstract declarator, as per
20423 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20424 as for cp_parser_declarator. */
20426 static cp_declarator
*
20427 cp_parser_direct_declarator (cp_parser
* parser
,
20428 cp_parser_declarator_kind dcl_kind
,
20429 int* ctor_dtor_or_conv_p
,
20430 bool member_p
, bool friend_p
)
20433 cp_declarator
*declarator
= NULL
;
20434 tree scope
= NULL_TREE
;
20435 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20436 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20438 tree pushed_scope
= NULL_TREE
;
20439 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20443 /* Peek at the next token. */
20444 token
= cp_lexer_peek_token (parser
->lexer
);
20445 if (token
->type
== CPP_OPEN_PAREN
)
20447 /* This is either a parameter-declaration-clause, or a
20448 parenthesized declarator. When we know we are parsing a
20449 named declarator, it must be a parenthesized declarator
20450 if FIRST is true. For instance, `(int)' is a
20451 parameter-declaration-clause, with an omitted
20452 direct-abstract-declarator. But `((*))', is a
20453 parenthesized abstract declarator. Finally, when T is a
20454 template parameter `(T)' is a
20455 parameter-declaration-clause, and not a parenthesized
20458 We first try and parse a parameter-declaration-clause,
20459 and then try a nested declarator (if FIRST is true).
20461 It is not an error for it not to be a
20462 parameter-declaration-clause, even when FIRST is
20468 The first is the declaration of a function while the
20469 second is the definition of a variable, including its
20472 Having seen only the parenthesis, we cannot know which of
20473 these two alternatives should be selected. Even more
20474 complex are examples like:
20479 The former is a function-declaration; the latter is a
20480 variable initialization.
20482 Thus again, we try a parameter-declaration-clause, and if
20483 that fails, we back out and return. */
20485 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20488 bool is_declarator
= false;
20492 /* In a member-declarator, the only valid interpretation
20493 of a parenthesis is the start of a
20494 parameter-declaration-clause. (It is invalid to
20495 initialize a static data member with a parenthesized
20496 initializer; only the "=" form of initialization is
20499 cp_parser_parse_tentatively (parser
);
20501 /* Consume the `('. */
20502 matching_parens parens
;
20503 parens
.consume_open (parser
);
20506 /* If this is going to be an abstract declarator, we're
20507 in a declarator and we can't have default args. */
20508 parser
->default_arg_ok_p
= false;
20509 parser
->in_declarator_p
= true;
20512 begin_scope (sk_function_parms
, NULL_TREE
);
20514 /* Parse the parameter-declaration-clause. */
20515 params
= cp_parser_parameter_declaration_clause (parser
);
20517 /* Consume the `)'. */
20518 parens
.require_close (parser
);
20520 /* If all went well, parse the cv-qualifier-seq,
20521 ref-qualifier and the exception-specification. */
20522 if (member_p
|| cp_parser_parse_definitely (parser
))
20524 cp_cv_quals cv_quals
;
20525 cp_virt_specifiers virt_specifiers
;
20526 cp_ref_qualifier ref_qual
;
20527 tree exception_specification
;
20530 bool memfn
= (member_p
|| (pushed_scope
20531 && CLASS_TYPE_P (pushed_scope
)));
20533 is_declarator
= true;
20535 if (ctor_dtor_or_conv_p
)
20536 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20539 /* Parse the cv-qualifier-seq. */
20540 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20541 /* Parse the ref-qualifier. */
20542 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20543 /* Parse the tx-qualifier. */
20544 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20545 /* And the exception-specification. */
20546 exception_specification
20547 = cp_parser_exception_specification_opt (parser
);
20549 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20551 /* In here, we handle cases where attribute is used after
20552 the function declaration. For example:
20553 void func (int x) __attribute__((vector(..))); */
20554 tree gnu_attrs
= NULL_TREE
;
20555 tree requires_clause
= NULL_TREE
;
20556 late_return
= (cp_parser_late_return_type_opt
20557 (parser
, declarator
, requires_clause
,
20558 memfn
? cv_quals
: -1));
20560 /* Parse the virt-specifier-seq. */
20561 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20563 /* Create the function-declarator. */
20564 declarator
= make_call_declarator (declarator
,
20570 exception_specification
,
20573 declarator
->std_attributes
= attrs
;
20574 declarator
->attributes
= gnu_attrs
;
20575 /* Any subsequent parameter lists are to do with
20576 return type, so are not those of the declared
20578 parser
->default_arg_ok_p
= false;
20581 /* Remove the function parms from scope. */
20582 pop_bindings_and_leave_scope ();
20585 /* Repeat the main loop. */
20589 /* If this is the first, we can try a parenthesized
20593 bool saved_in_type_id_in_expr_p
;
20595 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20596 parser
->in_declarator_p
= saved_in_declarator_p
;
20598 open_paren
= token
;
20599 /* Consume the `('. */
20600 matching_parens parens
;
20601 parens
.consume_open (parser
);
20602 /* Parse the nested declarator. */
20603 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20604 parser
->in_type_id_in_expr_p
= true;
20606 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20607 /*parenthesized_p=*/NULL
,
20608 member_p
, friend_p
);
20609 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20611 /* Expect a `)'. */
20612 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20613 if (!parens
.require_close (parser
))
20614 declarator
= cp_error_declarator
;
20615 if (declarator
== cp_error_declarator
)
20618 goto handle_declarator
;
20620 /* Otherwise, we must be done. */
20624 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20625 && token
->type
== CPP_OPEN_SQUARE
20626 && !cp_next_tokens_can_be_attribute_p (parser
))
20628 /* Parse an array-declarator. */
20629 tree bounds
, attrs
;
20631 if (ctor_dtor_or_conv_p
)
20632 *ctor_dtor_or_conv_p
= 0;
20636 parser
->default_arg_ok_p
= false;
20637 parser
->in_declarator_p
= true;
20638 /* Consume the `['. */
20639 cp_lexer_consume_token (parser
->lexer
);
20640 /* Peek at the next token. */
20641 token
= cp_lexer_peek_token (parser
->lexer
);
20642 /* If the next token is `]', then there is no
20643 constant-expression. */
20644 if (token
->type
!= CPP_CLOSE_SQUARE
)
20646 bool non_constant_p
;
20648 = cp_parser_constant_expression (parser
,
20649 /*allow_non_constant=*/true,
20651 if (!non_constant_p
)
20653 else if (error_operand_p (bounds
))
20654 /* Already gave an error. */;
20655 else if (!parser
->in_function_body
20656 || current_binding_level
->kind
== sk_function_parms
)
20658 /* Normally, the array bound must be an integral constant
20659 expression. However, as an extension, we allow VLAs
20660 in function scopes as long as they aren't part of a
20661 parameter declaration. */
20662 cp_parser_error (parser
,
20663 "array bound is not an integer constant");
20664 bounds
= error_mark_node
;
20666 else if (processing_template_decl
20667 && !type_dependent_expression_p (bounds
))
20669 /* Remember this wasn't a constant-expression. */
20670 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20671 TREE_SIDE_EFFECTS (bounds
) = 1;
20675 bounds
= NULL_TREE
;
20676 /* Look for the closing `]'. */
20677 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20679 declarator
= cp_error_declarator
;
20683 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20684 declarator
= make_array_declarator (declarator
, bounds
);
20685 declarator
->std_attributes
= attrs
;
20687 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20690 tree qualifying_scope
;
20691 tree unqualified_name
;
20693 special_function_kind sfk
;
20695 bool pack_expansion_p
= false;
20696 cp_token
*declarator_id_start_token
;
20698 /* Parse a declarator-id */
20699 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20702 cp_parser_parse_tentatively (parser
);
20704 /* If we see an ellipsis, we should be looking at a
20706 if (token
->type
== CPP_ELLIPSIS
)
20708 /* Consume the `...' */
20709 cp_lexer_consume_token (parser
->lexer
);
20711 pack_expansion_p
= true;
20715 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20717 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20718 qualifying_scope
= parser
->scope
;
20723 if (!unqualified_name
&& pack_expansion_p
)
20725 /* Check whether an error occurred. */
20726 okay
= !cp_parser_error_occurred (parser
);
20728 /* We already consumed the ellipsis to mark a
20729 parameter pack, but we have no way to report it,
20730 so abort the tentative parse. We will be exiting
20731 immediately anyway. */
20732 cp_parser_abort_tentative_parse (parser
);
20735 okay
= cp_parser_parse_definitely (parser
);
20738 unqualified_name
= error_mark_node
;
20739 else if (unqualified_name
20740 && (qualifying_scope
20741 || (!identifier_p (unqualified_name
))))
20743 cp_parser_error (parser
, "expected unqualified-id");
20744 unqualified_name
= error_mark_node
;
20748 if (!unqualified_name
)
20750 if (unqualified_name
== error_mark_node
)
20752 declarator
= cp_error_declarator
;
20753 pack_expansion_p
= false;
20754 declarator
->parameter_pack_p
= false;
20758 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20760 if (qualifying_scope
&& at_namespace_scope_p ()
20761 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20763 /* In the declaration of a member of a template class
20764 outside of the class itself, the SCOPE will sometimes
20765 be a TYPENAME_TYPE. For example, given:
20767 template <typename T>
20768 int S<T>::R::i = 3;
20770 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20771 this context, we must resolve S<T>::R to an ordinary
20772 type, rather than a typename type.
20774 The reason we normally avoid resolving TYPENAME_TYPEs
20775 is that a specialization of `S' might render
20776 `S<T>::R' not a type. However, if `S' is
20777 specialized, then this `i' will not be used, so there
20778 is no harm in resolving the types here. */
20781 /* Resolve the TYPENAME_TYPE. */
20782 type
= resolve_typename_type (qualifying_scope
,
20783 /*only_current_p=*/false);
20784 /* If that failed, the declarator is invalid. */
20785 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20787 if (typedef_variant_p (type
))
20788 error_at (declarator_id_start_token
->location
,
20789 "cannot define member of dependent typedef "
20792 error_at (declarator_id_start_token
->location
,
20793 "%<%T::%E%> is not a type",
20794 TYPE_CONTEXT (qualifying_scope
),
20795 TYPE_IDENTIFIER (qualifying_scope
));
20797 qualifying_scope
= type
;
20802 if (unqualified_name
)
20806 if (qualifying_scope
20807 && CLASS_TYPE_P (qualifying_scope
))
20808 class_type
= qualifying_scope
;
20810 class_type
= current_class_type
;
20812 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20814 tree name_type
= TREE_TYPE (unqualified_name
);
20816 if (!class_type
|| !same_type_p (name_type
, class_type
))
20818 /* We do not attempt to print the declarator
20819 here because we do not have enough
20820 information about its original syntactic
20822 cp_parser_error (parser
, "invalid declarator");
20823 declarator
= cp_error_declarator
;
20826 else if (qualifying_scope
20827 && CLASSTYPE_USE_TEMPLATE (name_type
))
20829 error_at (declarator_id_start_token
->location
,
20830 "invalid use of constructor as a template");
20831 inform (declarator_id_start_token
->location
,
20832 "use %<%T::%D%> instead of %<%T::%D%> to "
20833 "name the constructor in a qualified name",
20835 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20836 class_type
, name_type
);
20837 declarator
= cp_error_declarator
;
20840 unqualified_name
= constructor_name (class_type
);
20845 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20846 sfk
= sfk_destructor
;
20847 else if (identifier_p (unqualified_name
)
20848 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20849 sfk
= sfk_conversion
;
20850 else if (/* There's no way to declare a constructor
20851 for an unnamed type, even if the type
20852 got a name for linkage purposes. */
20853 !TYPE_WAS_UNNAMED (class_type
)
20854 /* Handle correctly (c++/19200):
20868 friend void N::S();
20870 && (!friend_p
|| class_type
== qualifying_scope
)
20871 && constructor_name_p (unqualified_name
,
20873 sfk
= sfk_constructor
;
20874 else if (is_overloaded_fn (unqualified_name
)
20875 && DECL_CONSTRUCTOR_P (get_first_fn
20876 (unqualified_name
)))
20877 sfk
= sfk_constructor
;
20879 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20880 *ctor_dtor_or_conv_p
= -1;
20883 declarator
= make_id_declarator (qualifying_scope
,
20885 sfk
, token
->location
);
20886 declarator
->std_attributes
= attrs
;
20887 declarator
->parameter_pack_p
= pack_expansion_p
;
20889 if (pack_expansion_p
)
20890 maybe_warn_variadic_templates ();
20893 handle_declarator
:;
20894 scope
= get_scope_of_declarator (declarator
);
20897 /* Any names that appear after the declarator-id for a
20898 member are looked up in the containing scope. */
20899 if (at_function_scope_p ())
20901 /* But declarations with qualified-ids can't appear in a
20903 cp_parser_error (parser
, "qualified-id in declaration");
20904 declarator
= cp_error_declarator
;
20907 pushed_scope
= push_scope (scope
);
20909 parser
->in_declarator_p
= true;
20910 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20911 || (declarator
&& declarator
->kind
== cdk_id
))
20912 /* Default args are only allowed on function
20914 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20916 parser
->default_arg_ok_p
= false;
20925 /* For an abstract declarator, we might wind up with nothing at this
20926 point. That's an error; the declarator is not optional. */
20928 cp_parser_error (parser
, "expected declarator");
20929 else if (open_paren
)
20931 /* Record overly parenthesized declarator so we can give a
20932 diagnostic about confusing decl/expr disambiguation. */
20933 if (declarator
->kind
== cdk_array
)
20935 /* If the open and close parens are on different lines, this
20936 is probably a formatting thing, so ignore. */
20937 expanded_location open
= expand_location (open_paren
->location
);
20938 expanded_location close
= expand_location (close_paren
->location
);
20939 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20943 declarator
->parenthesized
= open_paren
->location
;
20946 /* If we entered a scope, we must exit it now. */
20948 pop_scope (pushed_scope
);
20950 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20951 parser
->in_declarator_p
= saved_in_declarator_p
;
20956 /* Parse a ptr-operator.
20959 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20960 * cv-qualifier-seq [opt]
20962 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20963 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20968 & cv-qualifier-seq [opt]
20970 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20971 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20972 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20973 filled in with the TYPE containing the member. *CV_QUALS is
20974 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20975 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20976 Note that the tree codes returned by this function have nothing
20977 to do with the types of trees that will be eventually be created
20978 to represent the pointer or reference type being parsed. They are
20979 just constants with suggestive names. */
20980 static enum tree_code
20981 cp_parser_ptr_operator (cp_parser
* parser
,
20983 cp_cv_quals
*cv_quals
,
20986 enum tree_code code
= ERROR_MARK
;
20988 tree attrs
= NULL_TREE
;
20990 /* Assume that it's not a pointer-to-member. */
20992 /* And that there are no cv-qualifiers. */
20993 *cv_quals
= TYPE_UNQUALIFIED
;
20995 /* Peek at the next token. */
20996 token
= cp_lexer_peek_token (parser
->lexer
);
20998 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20999 if (token
->type
== CPP_MULT
)
21000 code
= INDIRECT_REF
;
21001 else if (token
->type
== CPP_AND
)
21003 else if ((cxx_dialect
!= cxx98
) &&
21004 token
->type
== CPP_AND_AND
) /* C++0x only */
21005 code
= NON_LVALUE_EXPR
;
21007 if (code
!= ERROR_MARK
)
21009 /* Consume the `*', `&' or `&&'. */
21010 cp_lexer_consume_token (parser
->lexer
);
21012 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21013 `&', if we are allowing GNU extensions. (The only qualifier
21014 that can legally appear after `&' is `restrict', but that is
21015 enforced during semantic analysis. */
21016 if (code
== INDIRECT_REF
21017 || cp_parser_allow_gnu_extensions_p (parser
))
21018 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21020 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21021 if (attributes
!= NULL
)
21022 *attributes
= attrs
;
21026 /* Try the pointer-to-member case. */
21027 cp_parser_parse_tentatively (parser
);
21028 /* Look for the optional `::' operator. */
21029 cp_parser_global_scope_opt (parser
,
21030 /*current_scope_valid_p=*/false);
21031 /* Look for the nested-name specifier. */
21032 token
= cp_lexer_peek_token (parser
->lexer
);
21033 cp_parser_nested_name_specifier (parser
,
21034 /*typename_keyword_p=*/false,
21035 /*check_dependency_p=*/true,
21037 /*is_declaration=*/false);
21038 /* If we found it, and the next token is a `*', then we are
21039 indeed looking at a pointer-to-member operator. */
21040 if (!cp_parser_error_occurred (parser
)
21041 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21043 /* Indicate that the `*' operator was used. */
21044 code
= INDIRECT_REF
;
21046 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21047 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21048 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21049 error_at (token
->location
, "cannot form pointer to member of "
21050 "non-class %q#T", parser
->scope
);
21053 /* The type of which the member is a member is given by the
21055 *type
= parser
->scope
;
21056 /* The next name will not be qualified. */
21057 parser
->scope
= NULL_TREE
;
21058 parser
->qualifying_scope
= NULL_TREE
;
21059 parser
->object_scope
= NULL_TREE
;
21060 /* Look for optional c++11 attributes. */
21061 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21062 if (attributes
!= NULL
)
21063 *attributes
= attrs
;
21064 /* Look for the optional cv-qualifier-seq. */
21065 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21068 /* If that didn't work we don't have a ptr-operator. */
21069 if (!cp_parser_parse_definitely (parser
))
21070 cp_parser_error (parser
, "expected ptr-operator");
21076 /* Parse an (optional) cv-qualifier-seq.
21079 cv-qualifier cv-qualifier-seq [opt]
21090 Returns a bitmask representing the cv-qualifiers. */
21093 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21095 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21100 cp_cv_quals cv_qualifier
;
21102 /* Peek at the next token. */
21103 token
= cp_lexer_peek_token (parser
->lexer
);
21104 /* See if it's a cv-qualifier. */
21105 switch (token
->keyword
)
21108 cv_qualifier
= TYPE_QUAL_CONST
;
21112 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21116 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21120 cv_qualifier
= TYPE_UNQUALIFIED
;
21127 if (cv_quals
& cv_qualifier
)
21129 gcc_rich_location
richloc (token
->location
);
21130 richloc
.add_fixit_remove ();
21131 error_at (&richloc
, "duplicate cv-qualifier");
21132 cp_lexer_purge_token (parser
->lexer
);
21136 cp_lexer_consume_token (parser
->lexer
);
21137 cv_quals
|= cv_qualifier
;
21144 /* Parse an (optional) ref-qualifier
21150 Returns cp_ref_qualifier representing ref-qualifier. */
21152 static cp_ref_qualifier
21153 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21155 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21157 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21158 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21163 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21164 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21166 switch (token
->type
)
21169 curr_ref_qual
= REF_QUAL_LVALUE
;
21173 curr_ref_qual
= REF_QUAL_RVALUE
;
21177 curr_ref_qual
= REF_QUAL_NONE
;
21181 if (!curr_ref_qual
)
21185 error_at (token
->location
, "multiple ref-qualifiers");
21186 cp_lexer_purge_token (parser
->lexer
);
21190 ref_qual
= curr_ref_qual
;
21191 cp_lexer_consume_token (parser
->lexer
);
21198 /* Parse an optional tx-qualifier.
21202 transaction_safe_dynamic */
21205 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21207 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21208 if (token
->type
== CPP_NAME
)
21210 tree name
= token
->u
.value
;
21211 const char *p
= IDENTIFIER_POINTER (name
);
21212 const int len
= strlen ("transaction_safe");
21213 if (!strncmp (p
, "transaction_safe", len
))
21217 || !strcmp (p
, "_dynamic"))
21219 cp_lexer_consume_token (parser
->lexer
);
21222 error ("%qE requires %<-fgnu-tm%>", name
);
21233 /* Parse an (optional) virt-specifier-seq.
21235 virt-specifier-seq:
21236 virt-specifier virt-specifier-seq [opt]
21242 Returns a bitmask representing the virt-specifiers. */
21244 static cp_virt_specifiers
21245 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21247 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21252 cp_virt_specifiers virt_specifier
;
21254 /* Peek at the next token. */
21255 token
= cp_lexer_peek_token (parser
->lexer
);
21256 /* See if it's a virt-specifier-qualifier. */
21257 if (token
->type
!= CPP_NAME
)
21259 if (id_equal (token
->u
.value
, "override"))
21261 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21262 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21264 else if (id_equal (token
->u
.value
, "final"))
21266 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21267 virt_specifier
= VIRT_SPEC_FINAL
;
21269 else if (id_equal (token
->u
.value
, "__final"))
21271 virt_specifier
= VIRT_SPEC_FINAL
;
21276 if (virt_specifiers
& virt_specifier
)
21278 gcc_rich_location
richloc (token
->location
);
21279 richloc
.add_fixit_remove ();
21280 error_at (&richloc
, "duplicate virt-specifier");
21281 cp_lexer_purge_token (parser
->lexer
);
21285 cp_lexer_consume_token (parser
->lexer
);
21286 virt_specifiers
|= virt_specifier
;
21289 return virt_specifiers
;
21292 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21293 is in scope even though it isn't real. */
21296 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21300 if (current_class_ptr
)
21302 /* We don't clear this between NSDMIs. Is it already what we want? */
21303 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21304 if (DECL_P (current_class_ptr
)
21305 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21306 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21307 && cp_type_quals (type
) == quals
)
21311 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21312 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21313 current_class_ptr
= NULL_TREE
;
21315 = cp_build_fold_indirect_ref (this_parm
);
21316 current_class_ptr
= this_parm
;
21319 /* Return true iff our current scope is a non-static data member
21323 parsing_nsdmi (void)
21325 /* We recognize NSDMI context by the context-less 'this' pointer set up
21326 by the function above. */
21327 if (current_class_ptr
21328 && TREE_CODE (current_class_ptr
) == PARM_DECL
21329 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21334 /* Parse a late-specified return type, if any. This is not a separate
21335 non-terminal, but part of a function declarator, which looks like
21337 -> trailing-type-specifier-seq abstract-declarator(opt)
21339 Returns the type indicated by the type-id.
21341 In addition to this, parse any queued up #pragma omp declare simd
21342 clauses, and #pragma acc routine clauses.
21344 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21348 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21349 tree
& requires_clause
, cp_cv_quals quals
)
21352 tree type
= NULL_TREE
;
21353 bool declare_simd_p
= (parser
->omp_declare_simd
21355 && declarator
->kind
== cdk_id
);
21357 bool oacc_routine_p
= (parser
->oacc_routine
21359 && declarator
->kind
== cdk_id
);
21361 /* Peek at the next token. */
21362 token
= cp_lexer_peek_token (parser
->lexer
);
21363 /* A late-specified return type is indicated by an initial '->'. */
21364 if (token
->type
!= CPP_DEREF
21365 && token
->keyword
!= RID_REQUIRES
21366 && !(token
->type
== CPP_NAME
21367 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21368 && !(declare_simd_p
|| oacc_routine_p
))
21371 tree save_ccp
= current_class_ptr
;
21372 tree save_ccr
= current_class_ref
;
21375 /* DR 1207: 'this' is in scope in the trailing return type. */
21376 inject_this_parameter (current_class_type
, quals
);
21379 if (token
->type
== CPP_DEREF
)
21381 /* Consume the ->. */
21382 cp_lexer_consume_token (parser
->lexer
);
21384 type
= cp_parser_trailing_type_id (parser
);
21387 /* Function declarations may be followed by a trailing
21388 requires-clause. */
21389 requires_clause
= cp_parser_requires_clause_opt (parser
);
21391 if (declare_simd_p
)
21392 declarator
->attributes
21393 = cp_parser_late_parsing_omp_declare_simd (parser
,
21394 declarator
->attributes
);
21395 if (oacc_routine_p
)
21396 declarator
->attributes
21397 = cp_parser_late_parsing_oacc_routine (parser
,
21398 declarator
->attributes
);
21402 current_class_ptr
= save_ccp
;
21403 current_class_ref
= save_ccr
;
21409 /* Parse a declarator-id.
21413 :: [opt] nested-name-specifier [opt] type-name
21415 In the `id-expression' case, the value returned is as for
21416 cp_parser_id_expression if the id-expression was an unqualified-id.
21417 If the id-expression was a qualified-id, then a SCOPE_REF is
21418 returned. The first operand is the scope (either a NAMESPACE_DECL
21419 or TREE_TYPE), but the second is still just a representation of an
21423 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21426 /* The expression must be an id-expression. Assume that qualified
21427 names are the names of types so that:
21430 int S<T>::R::i = 3;
21432 will work; we must treat `S<T>::R' as the name of a type.
21433 Similarly, assume that qualified names are templates, where
21437 int S<T>::R<T>::i = 3;
21440 id
= cp_parser_id_expression (parser
,
21441 /*template_keyword_p=*/false,
21442 /*check_dependency_p=*/false,
21443 /*template_p=*/NULL
,
21444 /*declarator_p=*/true,
21446 if (id
&& BASELINK_P (id
))
21447 id
= BASELINK_FUNCTIONS (id
);
21451 /* Parse a type-id.
21454 type-specifier-seq abstract-declarator [opt]
21456 Returns the TYPE specified. */
21459 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21460 bool is_trailing_return
, location_t
* type_location
)
21462 cp_decl_specifier_seq type_specifier_seq
;
21463 cp_declarator
*abstract_declarator
;
21465 /* Parse the type-specifier-seq. */
21466 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21467 is_trailing_return
,
21468 &type_specifier_seq
);
21470 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21472 if (is_template_arg
&& type_specifier_seq
.type
21473 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21474 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21475 /* A bare template name as a template argument is a template template
21476 argument, not a placeholder, so fail parsing it as a type argument. */
21478 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21479 cp_parser_simulate_error (parser
);
21480 return error_mark_node
;
21482 if (type_specifier_seq
.type
== error_mark_node
)
21483 return error_mark_node
;
21485 /* There might or might not be an abstract declarator. */
21486 cp_parser_parse_tentatively (parser
);
21487 /* Look for the declarator. */
21488 abstract_declarator
21489 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21490 /*parenthesized_p=*/NULL
,
21491 /*member_p=*/false,
21492 /*friend_p=*/false);
21493 /* Check to see if there really was a declarator. */
21494 if (!cp_parser_parse_definitely (parser
))
21495 abstract_declarator
= NULL
;
21497 if (type_specifier_seq
.type
21498 /* The concepts TS allows 'auto' as a type-id. */
21499 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21500 /* None of the valid uses of 'auto' in C++14 involve the type-id
21501 nonterminal, but it is valid in a trailing-return-type. */
21502 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21503 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21505 /* A type-id with type 'auto' is only ok if the abstract declarator
21506 is a function declarator with a late-specified return type.
21508 A type-id with 'auto' is also valid in a trailing-return-type
21509 in a compound-requirement. */
21510 if (abstract_declarator
21511 && abstract_declarator
->kind
== cdk_function
21512 && abstract_declarator
->u
.function
.late_return_type
)
21514 else if (parser
->in_result_type_constraint_p
)
21518 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21519 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21521 error_at (loc
, "missing template arguments after %qT",
21523 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21527 error_at (loc
, "invalid use of %qT", auto_node
);
21528 return error_mark_node
;
21532 return groktypename (&type_specifier_seq
, abstract_declarator
,
21537 cp_parser_type_id (cp_parser
*parser
, location_t
* type_location
)
21539 return cp_parser_type_id_1 (parser
, false, false, type_location
);
21543 cp_parser_template_type_arg (cp_parser
*parser
)
21546 const char *saved_message
= parser
->type_definition_forbidden_message
;
21547 parser
->type_definition_forbidden_message
21548 = G_("types may not be defined in template arguments");
21549 r
= cp_parser_type_id_1 (parser
, true, false, NULL
);
21550 parser
->type_definition_forbidden_message
= saved_message
;
21551 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21553 error ("invalid use of %<auto%> in template argument");
21554 r
= error_mark_node
;
21560 cp_parser_trailing_type_id (cp_parser
*parser
)
21562 return cp_parser_type_id_1 (parser
, false, true, NULL
);
21565 /* Parse a type-specifier-seq.
21567 type-specifier-seq:
21568 type-specifier type-specifier-seq [opt]
21572 type-specifier-seq:
21573 attributes type-specifier-seq [opt]
21575 If IS_DECLARATION is true, we are at the start of a "condition" or
21576 exception-declaration, so we might be followed by a declarator-id.
21578 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21579 i.e. we've just seen "->".
21581 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21584 cp_parser_type_specifier_seq (cp_parser
* parser
,
21585 bool is_declaration
,
21586 bool is_trailing_return
,
21587 cp_decl_specifier_seq
*type_specifier_seq
)
21589 bool seen_type_specifier
= false;
21590 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21591 cp_token
*start_token
= NULL
;
21593 /* Clear the TYPE_SPECIFIER_SEQ. */
21594 clear_decl_specs (type_specifier_seq
);
21596 /* In the context of a trailing return type, enum E { } is an
21597 elaborated-type-specifier followed by a function-body, not an
21599 if (is_trailing_return
)
21600 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21602 /* Parse the type-specifiers and attributes. */
21605 tree type_specifier
;
21606 bool is_cv_qualifier
;
21608 /* Check for attributes first. */
21609 if (cp_next_tokens_can_be_attribute_p (parser
))
21611 type_specifier_seq
->attributes
21612 = attr_chainon (type_specifier_seq
->attributes
,
21613 cp_parser_attributes_opt (parser
));
21617 /* record the token of the beginning of the type specifier seq,
21618 for error reporting purposes*/
21620 start_token
= cp_lexer_peek_token (parser
->lexer
);
21622 /* Look for the type-specifier. */
21623 type_specifier
= cp_parser_type_specifier (parser
,
21625 type_specifier_seq
,
21626 /*is_declaration=*/false,
21629 if (!type_specifier
)
21631 /* If the first type-specifier could not be found, this is not a
21632 type-specifier-seq at all. */
21633 if (!seen_type_specifier
)
21635 /* Set in_declarator_p to avoid skipping to the semicolon. */
21636 int in_decl
= parser
->in_declarator_p
;
21637 parser
->in_declarator_p
= true;
21639 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21640 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21641 cp_parser_error (parser
, "expected type-specifier");
21643 parser
->in_declarator_p
= in_decl
;
21645 type_specifier_seq
->type
= error_mark_node
;
21648 /* If subsequent type-specifiers could not be found, the
21649 type-specifier-seq is complete. */
21653 seen_type_specifier
= true;
21654 /* The standard says that a condition can be:
21656 type-specifier-seq declarator = assignment-expression
21663 we should treat the "S" as a declarator, not as a
21664 type-specifier. The standard doesn't say that explicitly for
21665 type-specifier-seq, but it does say that for
21666 decl-specifier-seq in an ordinary declaration. Perhaps it
21667 would be clearer just to allow a decl-specifier-seq here, and
21668 then add a semantic restriction that if any decl-specifiers
21669 that are not type-specifiers appear, the program is invalid. */
21670 if (is_declaration
&& !is_cv_qualifier
)
21671 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21675 /* Return whether the function currently being declared has an associated
21676 template parameter list. */
21679 function_being_declared_is_template_p (cp_parser
* parser
)
21681 if (!current_template_parms
|| processing_template_parmlist
)
21684 if (parser
->implicit_template_scope
)
21687 if (at_class_scope_p ()
21688 && TYPE_BEING_DEFINED (current_class_type
))
21689 return parser
->num_template_parameter_lists
!= 0;
21691 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21692 (current_class_type
));
21695 /* Parse a parameter-declaration-clause.
21697 parameter-declaration-clause:
21698 parameter-declaration-list [opt] ... [opt]
21699 parameter-declaration-list , ...
21701 Returns a representation for the parameter declarations. A return
21702 value of NULL indicates a parameter-declaration-clause consisting
21703 only of an ellipsis. */
21706 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21712 temp_override
<bool> cleanup
21713 (parser
->auto_is_implicit_function_template_parm_p
);
21715 if (!processing_specialization
21716 && !processing_template_parmlist
21717 && !processing_explicit_instantiation
21718 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21719 actual function or a random abstract declarator. */
21720 && parser
->default_arg_ok_p
)
21721 if (!current_function_decl
21722 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21723 parser
->auto_is_implicit_function_template_parm_p
= true;
21725 /* Peek at the next token. */
21726 token
= cp_lexer_peek_token (parser
->lexer
);
21727 /* Check for trivial parameter-declaration-clauses. */
21728 if (token
->type
== CPP_ELLIPSIS
)
21730 /* Consume the `...' token. */
21731 cp_lexer_consume_token (parser
->lexer
);
21734 else if (token
->type
== CPP_CLOSE_PAREN
)
21735 /* There are no parameters. */
21736 return void_list_node
;
21737 /* Check for `(void)', too, which is a special case. */
21738 else if (token
->keyword
== RID_VOID
21739 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21740 == CPP_CLOSE_PAREN
))
21742 /* Consume the `void' token. */
21743 cp_lexer_consume_token (parser
->lexer
);
21744 /* There are no parameters. */
21745 return void_list_node
;
21748 /* Parse the parameter-declaration-list. */
21749 parameters
= cp_parser_parameter_declaration_list (parser
);
21750 /* If a parse error occurred while parsing the
21751 parameter-declaration-list, then the entire
21752 parameter-declaration-clause is erroneous. */
21753 if (parameters
== error_mark_node
)
21756 /* Peek at the next token. */
21757 token
= cp_lexer_peek_token (parser
->lexer
);
21758 /* If it's a `,', the clause should terminate with an ellipsis. */
21759 if (token
->type
== CPP_COMMA
)
21761 /* Consume the `,'. */
21762 cp_lexer_consume_token (parser
->lexer
);
21763 /* Expect an ellipsis. */
21765 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21767 /* It might also be `...' if the optional trailing `,' was
21769 else if (token
->type
== CPP_ELLIPSIS
)
21771 /* Consume the `...' token. */
21772 cp_lexer_consume_token (parser
->lexer
);
21773 /* And remember that we saw it. */
21777 ellipsis_p
= false;
21779 /* Finish the parameter list. */
21781 parameters
= chainon (parameters
, void_list_node
);
21786 /* Parse a parameter-declaration-list.
21788 parameter-declaration-list:
21789 parameter-declaration
21790 parameter-declaration-list , parameter-declaration
21792 Returns a representation of the parameter-declaration-list, as for
21793 cp_parser_parameter_declaration_clause. However, the
21794 `void_list_node' is never appended to the list. */
21797 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21799 tree parameters
= NULL_TREE
;
21800 tree
*tail
= ¶meters
;
21801 bool saved_in_unbraced_linkage_specification_p
;
21804 /* The special considerations that apply to a function within an
21805 unbraced linkage specifications do not apply to the parameters
21806 to the function. */
21807 saved_in_unbraced_linkage_specification_p
21808 = parser
->in_unbraced_linkage_specification_p
;
21809 parser
->in_unbraced_linkage_specification_p
= false;
21811 /* Look for more parameters. */
21814 cp_parameter_declarator
*parameter
;
21815 tree decl
= error_mark_node
;
21816 bool parenthesized_p
= false;
21818 /* Parse the parameter. */
21820 = cp_parser_parameter_declaration (parser
,
21821 /*template_parm_p=*/false,
21824 /* We don't know yet if the enclosing context is deprecated, so wait
21825 and warn in grokparms if appropriate. */
21826 deprecated_state
= DEPRECATED_SUPPRESS
;
21830 decl
= grokdeclarator (parameter
->declarator
,
21831 ¶meter
->decl_specifiers
,
21833 parameter
->default_argument
!= NULL_TREE
,
21834 ¶meter
->decl_specifiers
.attributes
);
21835 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21836 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21839 deprecated_state
= DEPRECATED_NORMAL
;
21841 /* If a parse error occurred parsing the parameter declaration,
21842 then the entire parameter-declaration-list is erroneous. */
21843 if (decl
== error_mark_node
)
21845 parameters
= error_mark_node
;
21849 if (parameter
->decl_specifiers
.attributes
)
21850 cplus_decl_attributes (&decl
,
21851 parameter
->decl_specifiers
.attributes
,
21853 if (DECL_NAME (decl
))
21854 decl
= pushdecl (decl
);
21856 if (decl
!= error_mark_node
)
21858 retrofit_lang_decl (decl
);
21859 DECL_PARM_INDEX (decl
) = ++index
;
21860 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21863 /* Add the new parameter to the list. */
21864 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21865 tail
= &TREE_CHAIN (*tail
);
21867 /* Peek at the next token. */
21868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21869 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21870 /* These are for Objective-C++ */
21871 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21872 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21873 /* The parameter-declaration-list is complete. */
21875 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21879 /* Peek at the next token. */
21880 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21881 /* If it's an ellipsis, then the list is complete. */
21882 if (token
->type
== CPP_ELLIPSIS
)
21884 /* Otherwise, there must be more parameters. Consume the
21886 cp_lexer_consume_token (parser
->lexer
);
21887 /* When parsing something like:
21889 int i(float f, double d)
21891 we can tell after seeing the declaration for "f" that we
21892 are not looking at an initialization of a variable "i",
21893 but rather at the declaration of a function "i".
21895 Due to the fact that the parsing of template arguments
21896 (as specified to a template-id) requires backtracking we
21897 cannot use this technique when inside a template argument
21899 if (!parser
->in_template_argument_list_p
21900 && !parser
->in_type_id_in_expr_p
21901 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21902 /* However, a parameter-declaration of the form
21903 "float(f)" (which is a valid declaration of a
21904 parameter "f") can also be interpreted as an
21905 expression (the conversion of "f" to "float"). */
21906 && !parenthesized_p
)
21907 cp_parser_commit_to_tentative_parse (parser
);
21911 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21913 cp_parser_skip_to_closing_parenthesis (parser
,
21914 /*recovering=*/true,
21915 /*or_comma=*/false,
21916 /*consume_paren=*/false);
21921 parser
->in_unbraced_linkage_specification_p
21922 = saved_in_unbraced_linkage_specification_p
;
21924 /* Reset implicit_template_scope if we are about to leave the function
21925 parameter list that introduced it. Note that for out-of-line member
21926 definitions, there will be one or more class scopes before we get to
21927 the template parameter scope. */
21929 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21930 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21932 while (maybe_its
->kind
== sk_class
)
21933 maybe_its
= maybe_its
->level_chain
;
21934 if (maybe_its
== its
)
21936 parser
->implicit_template_parms
= 0;
21937 parser
->implicit_template_scope
= 0;
21944 /* Parse a parameter declaration.
21946 parameter-declaration:
21947 decl-specifier-seq ... [opt] declarator
21948 decl-specifier-seq declarator = assignment-expression
21949 decl-specifier-seq ... [opt] abstract-declarator [opt]
21950 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21952 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21953 declares a template parameter. (In that case, a non-nested `>'
21954 token encountered during the parsing of the assignment-expression
21955 is not interpreted as a greater-than operator.)
21957 Returns a representation of the parameter, or NULL if an error
21958 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21959 true iff the declarator is of the form "(p)". */
21961 static cp_parameter_declarator
*
21962 cp_parser_parameter_declaration (cp_parser
*parser
,
21963 bool template_parm_p
,
21964 bool *parenthesized_p
)
21966 int declares_class_or_enum
;
21967 cp_decl_specifier_seq decl_specifiers
;
21968 cp_declarator
*declarator
;
21969 tree default_argument
;
21970 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21971 const char *saved_message
;
21972 bool template_parameter_pack_p
= false;
21974 /* In a template parameter, `>' is not an operator.
21978 When parsing a default template-argument for a non-type
21979 template-parameter, the first non-nested `>' is taken as the end
21980 of the template parameter-list rather than a greater-than
21983 /* Type definitions may not appear in parameter types. */
21984 saved_message
= parser
->type_definition_forbidden_message
;
21985 parser
->type_definition_forbidden_message
21986 = G_("types may not be defined in parameter types");
21988 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21989 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21990 (current_template_parms
)) : 0);
21992 /* Parse the declaration-specifiers. */
21993 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21994 cp_parser_decl_specifier_seq (parser
,
21995 CP_PARSER_FLAGS_NONE
,
21997 &declares_class_or_enum
);
21999 /* Complain about missing 'typename' or other invalid type names. */
22000 if (!decl_specifiers
.any_type_specifiers_p
22001 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22002 decl_specifiers
.type
= error_mark_node
;
22004 /* If an error occurred, there's no reason to attempt to parse the
22005 rest of the declaration. */
22006 if (cp_parser_error_occurred (parser
))
22008 parser
->type_definition_forbidden_message
= saved_message
;
22012 /* Peek at the next token. */
22013 token
= cp_lexer_peek_token (parser
->lexer
);
22015 /* If the next token is a `)', `,', `=', `>', or `...', then there
22016 is no declarator. However, when variadic templates are enabled,
22017 there may be a declarator following `...'. */
22018 if (token
->type
== CPP_CLOSE_PAREN
22019 || token
->type
== CPP_COMMA
22020 || token
->type
== CPP_EQ
22021 || token
->type
== CPP_GREATER
)
22024 if (parenthesized_p
)
22025 *parenthesized_p
= false;
22027 /* Otherwise, there should be a declarator. */
22030 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22031 parser
->default_arg_ok_p
= false;
22033 /* After seeing a decl-specifier-seq, if the next token is not a
22034 "(", there is no possibility that the code is a valid
22035 expression. Therefore, if parsing tentatively, we commit at
22037 if (!parser
->in_template_argument_list_p
22038 /* In an expression context, having seen:
22042 we cannot be sure whether we are looking at a
22043 function-type (taking a "char" as a parameter) or a cast
22044 of some object of type "char" to "int". */
22045 && !parser
->in_type_id_in_expr_p
22046 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22047 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22048 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22049 cp_parser_commit_to_tentative_parse (parser
);
22050 /* Parse the declarator. */
22051 declarator_token_start
= token
;
22052 declarator
= cp_parser_declarator (parser
,
22053 CP_PARSER_DECLARATOR_EITHER
,
22054 /*ctor_dtor_or_conv_p=*/NULL
,
22056 /*member_p=*/false,
22057 /*friend_p=*/false);
22058 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22059 /* After the declarator, allow more attributes. */
22060 decl_specifiers
.attributes
22061 = attr_chainon (decl_specifiers
.attributes
,
22062 cp_parser_attributes_opt (parser
));
22064 /* If the declarator is a template parameter pack, remember that and
22065 clear the flag in the declarator itself so we don't get errors
22066 from grokdeclarator. */
22067 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22069 declarator
->parameter_pack_p
= false;
22070 template_parameter_pack_p
= true;
22074 /* If the next token is an ellipsis, and we have not seen a declarator
22075 name, and if either the type of the declarator contains parameter
22076 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22077 for, eg, abbreviated integral type names), then we actually have a
22078 parameter pack expansion expression. Otherwise, leave the ellipsis
22079 for a C-style variadic function. */
22080 token
= cp_lexer_peek_token (parser
->lexer
);
22082 /* If a function parameter pack was specified and an implicit template
22083 parameter was introduced during cp_parser_parameter_declaration,
22084 change any implicit parameters introduced into packs. */
22085 if (parser
->implicit_template_parms
22086 && ((token
->type
== CPP_ELLIPSIS
22087 && declarator_can_be_parameter_pack (declarator
))
22088 || (declarator
&& declarator
->parameter_pack_p
)))
22090 int latest_template_parm_idx
= TREE_VEC_LENGTH
22091 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22093 if (latest_template_parm_idx
!= template_parm_idx
)
22094 decl_specifiers
.type
= convert_generic_types_to_packs
22095 (decl_specifiers
.type
,
22096 template_parm_idx
, latest_template_parm_idx
);
22099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22101 tree type
= decl_specifiers
.type
;
22103 if (type
&& DECL_P (type
))
22104 type
= TREE_TYPE (type
);
22107 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22108 && (template_parm_p
|| uses_parameter_packs (type
)))
22109 || (!type
&& template_parm_p
))
22110 && declarator_can_be_parameter_pack (declarator
))
22112 /* Consume the `...'. */
22113 cp_lexer_consume_token (parser
->lexer
);
22114 maybe_warn_variadic_templates ();
22116 /* Build a pack expansion type */
22117 if (template_parm_p
)
22118 template_parameter_pack_p
= true;
22119 else if (declarator
)
22120 declarator
->parameter_pack_p
= true;
22122 decl_specifiers
.type
= make_pack_expansion (type
);
22126 /* The restriction on defining new types applies only to the type
22127 of the parameter, not to the default argument. */
22128 parser
->type_definition_forbidden_message
= saved_message
;
22130 /* If the next token is `=', then process a default argument. */
22131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22133 tree type
= decl_specifiers
.type
;
22134 token
= cp_lexer_peek_token (parser
->lexer
);
22135 /* If we are defining a class, then the tokens that make up the
22136 default argument must be saved and processed later. */
22137 if (!template_parm_p
&& at_class_scope_p ()
22138 && TYPE_BEING_DEFINED (current_class_type
)
22139 && !LAMBDA_TYPE_P (current_class_type
))
22140 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22142 // A constrained-type-specifier may declare a type template-parameter.
22143 else if (declares_constrained_type_template_parameter (type
))
22145 = cp_parser_default_type_template_argument (parser
);
22147 // A constrained-type-specifier may declare a template-template-parameter.
22148 else if (declares_constrained_template_template_parameter (type
))
22150 = cp_parser_default_template_template_argument (parser
);
22152 /* Outside of a class definition, we can just parse the
22153 assignment-expression. */
22156 = cp_parser_default_argument (parser
, template_parm_p
);
22158 if (!parser
->default_arg_ok_p
)
22160 permerror (token
->location
,
22161 "default arguments are only "
22162 "permitted for function parameters");
22164 else if ((declarator
&& declarator
->parameter_pack_p
)
22165 || template_parameter_pack_p
22166 || (decl_specifiers
.type
22167 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22169 /* Find the name of the parameter pack. */
22170 cp_declarator
*id_declarator
= declarator
;
22171 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22172 id_declarator
= id_declarator
->declarator
;
22174 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22175 error_at (declarator_token_start
->location
,
22177 ? G_("template parameter pack %qD "
22178 "cannot have a default argument")
22179 : G_("parameter pack %qD cannot have "
22180 "a default argument"),
22181 id_declarator
->u
.id
.unqualified_name
);
22183 error_at (declarator_token_start
->location
,
22185 ? G_("template parameter pack cannot have "
22186 "a default argument")
22187 : G_("parameter pack cannot have a "
22188 "default argument"));
22190 default_argument
= NULL_TREE
;
22194 default_argument
= NULL_TREE
;
22196 /* Generate a location for the parameter, ranging from the start of the
22197 initial token to the end of the final token (using input_location for
22198 the latter, set up by cp_lexer_set_source_position_from_token when
22201 If we have a identifier, then use it for the caret location, e.g.
22203 extern int callee (int one, int (*two)(int, int), float three);
22204 ~~~~~~^~~~~~~~~~~~~~
22206 otherwise, reuse the start location for the caret location e.g.:
22208 extern int callee (int one, int (*)(int, int), float three);
22212 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22213 ? declarator
->id_loc
22214 : decl_spec_token_start
->location
);
22215 location_t param_loc
= make_location (caret_loc
,
22216 decl_spec_token_start
->location
,
22219 return make_parameter_declarator (&decl_specifiers
,
22223 template_parameter_pack_p
);
22226 /* Parse a default argument and return it.
22228 TEMPLATE_PARM_P is true if this is a default argument for a
22229 non-type template parameter. */
22231 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22233 tree default_argument
= NULL_TREE
;
22234 bool saved_greater_than_is_operator_p
;
22235 bool saved_local_variables_forbidden_p
;
22236 bool non_constant_p
, is_direct_init
;
22238 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22240 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22241 parser
->greater_than_is_operator_p
= !template_parm_p
;
22242 /* Local variable names (and the `this' keyword) may not
22243 appear in a default argument. */
22244 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22245 parser
->local_variables_forbidden_p
= true;
22246 /* Parse the assignment-expression. */
22247 if (template_parm_p
)
22248 push_deferring_access_checks (dk_no_deferred
);
22249 tree saved_class_ptr
= NULL_TREE
;
22250 tree saved_class_ref
= NULL_TREE
;
22251 /* The "this" pointer is not valid in a default argument. */
22254 saved_class_ptr
= current_class_ptr
;
22255 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22256 saved_class_ref
= current_class_ref
;
22257 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22260 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22261 /* Restore the "this" pointer. */
22264 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22265 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22267 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22268 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22269 if (template_parm_p
)
22270 pop_deferring_access_checks ();
22271 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22272 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22274 return default_argument
;
22277 /* Parse a function-body.
22280 compound_statement */
22283 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22285 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22286 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22290 /* Parse a ctor-initializer-opt followed by a function-body. Return
22291 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22292 is true we are parsing a function-try-block. */
22295 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22296 bool in_function_try_block
)
22299 const bool check_body_p
=
22300 DECL_CONSTRUCTOR_P (current_function_decl
)
22301 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22304 /* Begin the function body. */
22305 body
= begin_function_body ();
22306 /* Parse the optional ctor-initializer. */
22307 cp_parser_ctor_initializer_opt (parser
);
22309 /* If we're parsing a constexpr constructor definition, we need
22310 to check that the constructor body is indeed empty. However,
22311 before we get to cp_parser_function_body lot of junk has been
22312 generated, so we can't just check that we have an empty block.
22313 Rather we take a snapshot of the outermost block, and check whether
22314 cp_parser_function_body changed its state. */
22317 list
= cur_stmt_list
;
22318 if (STATEMENT_LIST_TAIL (list
))
22319 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22321 /* Parse the function-body. */
22322 cp_parser_function_body (parser
, in_function_try_block
);
22324 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22325 /* Finish the function body. */
22326 finish_function_body (body
);
22329 /* Parse an initializer.
22332 = initializer-clause
22333 ( expression-list )
22335 Returns an expression representing the initializer. If no
22336 initializer is present, NULL_TREE is returned.
22338 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22339 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22340 set to TRUE if there is no initializer present. If there is an
22341 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22342 is set to true; otherwise it is set to false. */
22345 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22346 bool* non_constant_p
, bool subexpression_p
)
22351 /* Peek at the next token. */
22352 token
= cp_lexer_peek_token (parser
->lexer
);
22354 /* Let our caller know whether or not this initializer was
22356 *is_direct_init
= (token
->type
!= CPP_EQ
);
22357 /* Assume that the initializer is constant. */
22358 *non_constant_p
= false;
22360 if (token
->type
== CPP_EQ
)
22362 /* Consume the `='. */
22363 cp_lexer_consume_token (parser
->lexer
);
22364 /* Parse the initializer-clause. */
22365 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22367 else if (token
->type
== CPP_OPEN_PAREN
)
22369 vec
<tree
, va_gc
> *vec
;
22370 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22372 /*allow_expansion_p=*/true,
22375 return error_mark_node
;
22376 init
= build_tree_list_vec (vec
);
22377 release_tree_vector (vec
);
22379 else if (token
->type
== CPP_OPEN_BRACE
)
22381 cp_lexer_set_source_position (parser
->lexer
);
22382 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22383 init
= cp_parser_braced_list (parser
, non_constant_p
);
22384 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22388 /* Anything else is an error. */
22389 cp_parser_error (parser
, "expected initializer");
22390 init
= error_mark_node
;
22393 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22394 init
= error_mark_node
;
22399 /* Parse an initializer-clause.
22401 initializer-clause:
22402 assignment-expression
22405 Returns an expression representing the initializer.
22407 If the `assignment-expression' production is used the value
22408 returned is simply a representation for the expression.
22410 Otherwise, calls cp_parser_braced_list. */
22413 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22415 cp_expr initializer
;
22417 /* Assume the expression is constant. */
22418 *non_constant_p
= false;
22420 /* If it is not a `{', then we are looking at an
22421 assignment-expression. */
22422 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22425 = cp_parser_constant_expression (parser
,
22426 /*allow_non_constant_p=*/true,
22430 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22432 return initializer
;
22435 /* Parse a brace-enclosed initializer list.
22438 { initializer-list , [opt] }
22439 { designated-initializer-list , [opt] }
22442 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22443 the elements of the initializer-list (or NULL, if the last
22444 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22445 NULL_TREE. There is no way to detect whether or not the optional
22446 trailing `,' was provided. NON_CONSTANT_P is as for
22447 cp_parser_initializer. */
22450 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22453 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22455 /* Consume the `{' token. */
22456 matching_braces braces
;
22457 braces
.require_open (parser
);
22458 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22459 initializer
= make_node (CONSTRUCTOR
);
22460 /* If it's not a `}', then there is a non-trivial initializer. */
22461 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22463 /* Parse the initializer list. */
22464 CONSTRUCTOR_ELTS (initializer
)
22465 = cp_parser_initializer_list (parser
, non_constant_p
);
22466 /* A trailing `,' token is allowed. */
22467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22468 cp_lexer_consume_token (parser
->lexer
);
22471 *non_constant_p
= false;
22472 /* Now, there should be a trailing `}'. */
22473 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22474 braces
.require_close (parser
);
22475 TREE_TYPE (initializer
) = init_list_type_node
;
22477 cp_expr
result (initializer
);
22478 /* Build a location of the form:
22481 with caret==start at the open brace, finish at the close brace. */
22482 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22483 result
.set_location (combined_loc
);
22487 /* Consume tokens up to, and including, the next non-nested closing `]'.
22488 Returns true iff we found a closing `]'. */
22491 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22493 unsigned square_depth
= 0;
22497 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22499 switch (token
->type
)
22502 case CPP_PRAGMA_EOL
:
22503 /* If we've run out of tokens, then there is no closing `]'. */
22506 case CPP_OPEN_SQUARE
:
22510 case CPP_CLOSE_SQUARE
:
22511 if (!square_depth
--)
22513 cp_lexer_consume_token (parser
->lexer
);
22522 /* Consume the token. */
22523 cp_lexer_consume_token (parser
->lexer
);
22527 /* Return true if we are looking at an array-designator, false otherwise. */
22530 cp_parser_array_designator_p (cp_parser
*parser
)
22532 /* Consume the `['. */
22533 cp_lexer_consume_token (parser
->lexer
);
22535 cp_lexer_save_tokens (parser
->lexer
);
22537 /* Skip tokens until the next token is a closing square bracket.
22538 If we find the closing `]', and the next token is a `=', then
22539 we are looking at an array designator. */
22540 bool array_designator_p
22541 = (cp_parser_skip_to_closing_square_bracket (parser
)
22542 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22544 /* Roll back the tokens we skipped. */
22545 cp_lexer_rollback_tokens (parser
->lexer
);
22547 return array_designator_p
;
22550 /* Parse an initializer-list.
22553 initializer-clause ... [opt]
22554 initializer-list , initializer-clause ... [opt]
22558 designated-initializer-list:
22559 designated-initializer-clause
22560 designated-initializer-list , designated-initializer-clause
22562 designated-initializer-clause:
22563 designator brace-or-equal-initializer
22571 designation initializer-clause ...[opt]
22572 initializer-list , designation initializer-clause ...[opt]
22577 [ constant-expression ] =
22579 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22580 for the initializer. If the INDEX of the elt is non-NULL, it is the
22581 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22582 as for cp_parser_initializer. */
22584 static vec
<constructor_elt
, va_gc
> *
22585 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22587 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22588 bool first_p
= true;
22589 tree first_designator
= NULL_TREE
;
22591 /* Assume all of the expressions are constant. */
22592 *non_constant_p
= false;
22594 /* Parse the rest of the list. */
22600 bool clause_non_constant_p
;
22601 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22603 /* Handle the C++2A syntax, '. id ='. */
22604 if ((cxx_dialect
>= cxx2a
22605 || cp_parser_allow_gnu_extensions_p (parser
))
22606 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22607 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22608 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22609 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22610 == CPP_OPEN_BRACE
)))
22612 if (cxx_dialect
< cxx2a
)
22613 pedwarn (loc
, OPT_Wpedantic
,
22614 "C++ designated initializers only available with "
22615 "-std=c++2a or -std=gnu++2a");
22616 /* Consume the `.'. */
22617 cp_lexer_consume_token (parser
->lexer
);
22618 /* Consume the identifier. */
22619 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22621 /* Consume the `='. */
22622 cp_lexer_consume_token (parser
->lexer
);
22624 /* Also, if the next token is an identifier and the following one is a
22625 colon, we are looking at the GNU designated-initializer
22627 else if (cp_parser_allow_gnu_extensions_p (parser
)
22628 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22629 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22632 /* Warn the user that they are using an extension. */
22633 pedwarn (loc
, OPT_Wpedantic
,
22634 "ISO C++ does not allow GNU designated initializers");
22635 /* Consume the identifier. */
22636 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22637 /* Consume the `:'. */
22638 cp_lexer_consume_token (parser
->lexer
);
22640 /* Also handle C99 array designators, '[ const ] ='. */
22641 else if (cp_parser_allow_gnu_extensions_p (parser
)
22642 && !c_dialect_objc ()
22643 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22645 /* In C++11, [ could start a lambda-introducer. */
22646 bool non_const
= false;
22648 cp_parser_parse_tentatively (parser
);
22650 if (!cp_parser_array_designator_p (parser
))
22652 cp_parser_simulate_error (parser
);
22653 designator
= NULL_TREE
;
22657 designator
= cp_parser_constant_expression (parser
, true,
22659 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22660 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22663 if (!cp_parser_parse_definitely (parser
))
22664 designator
= NULL_TREE
;
22666 && (!require_potential_rvalue_constant_expression
22668 designator
= NULL_TREE
;
22670 /* Warn the user that they are using an extension. */
22671 pedwarn (loc
, OPT_Wpedantic
,
22672 "ISO C++ does not allow C99 designated initializers");
22675 designator
= NULL_TREE
;
22679 first_designator
= designator
;
22682 else if (cxx_dialect
>= cxx2a
22683 && first_designator
!= error_mark_node
22684 && (!first_designator
!= !designator
))
22686 error_at (loc
, "either all initializer clauses should be designated "
22687 "or none of them should be");
22688 first_designator
= error_mark_node
;
22690 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22691 first_designator
= designator
;
22693 /* Parse the initializer. */
22694 initializer
= cp_parser_initializer_clause (parser
,
22695 &clause_non_constant_p
);
22696 /* If any clause is non-constant, so is the entire initializer. */
22697 if (clause_non_constant_p
)
22698 *non_constant_p
= true;
22700 /* If we have an ellipsis, this is an initializer pack
22702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22704 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22706 /* Consume the `...'. */
22707 cp_lexer_consume_token (parser
->lexer
);
22709 if (designator
&& cxx_dialect
>= cxx2a
)
22711 "%<...%> not allowed in designated initializer list");
22713 /* Turn the initializer into an initializer expansion. */
22714 initializer
= make_pack_expansion (initializer
);
22717 /* Add it to the vector. */
22718 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22720 /* If the next token is not a comma, we have reached the end of
22722 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22725 /* Peek at the next token. */
22726 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22727 /* If the next token is a `}', then we're still done. An
22728 initializer-clause can have a trailing `,' after the
22729 initializer-list and before the closing `}'. */
22730 if (token
->type
== CPP_CLOSE_BRACE
)
22733 /* Consume the `,' token. */
22734 cp_lexer_consume_token (parser
->lexer
);
22737 /* The same identifier shall not appear in multiple designators
22738 of a designated-initializer-list. */
22739 if (first_designator
)
22742 tree designator
, val
;
22743 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22744 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22746 if (IDENTIFIER_MARKED (designator
))
22748 error_at (cp_expr_loc_or_loc (val
, input_location
),
22749 "%<.%s%> designator used multiple times in "
22750 "the same initializer list",
22751 IDENTIFIER_POINTER (designator
));
22752 (*v
)[i
].index
= error_mark_node
;
22755 IDENTIFIER_MARKED (designator
) = 1;
22757 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22758 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22759 IDENTIFIER_MARKED (designator
) = 0;
22765 /* Classes [gram.class] */
22767 /* Parse a class-name.
22773 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22774 to indicate that names looked up in dependent types should be
22775 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22776 keyword has been used to indicate that the name that appears next
22777 is a template. TAG_TYPE indicates the explicit tag given before
22778 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22779 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22780 is the class being defined in a class-head. If ENUM_OK is TRUE,
22781 enum-names are also accepted.
22783 Returns the TYPE_DECL representing the class. */
22786 cp_parser_class_name (cp_parser
*parser
,
22787 bool typename_keyword_p
,
22788 bool template_keyword_p
,
22789 enum tag_types tag_type
,
22790 bool check_dependency_p
,
22792 bool is_declaration
,
22799 tree identifier
= NULL_TREE
;
22801 /* All class-names start with an identifier. */
22802 token
= cp_lexer_peek_token (parser
->lexer
);
22803 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22805 cp_parser_error (parser
, "expected class-name");
22806 return error_mark_node
;
22809 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22810 to a template-id, so we save it here. */
22811 scope
= parser
->scope
;
22812 if (scope
== error_mark_node
)
22813 return error_mark_node
;
22815 /* Any name names a type if we're following the `typename' keyword
22816 in a qualified name where the enclosing scope is type-dependent. */
22817 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22818 && dependent_type_p (scope
));
22819 /* Handle the common case (an identifier, but not a template-id)
22821 if (token
->type
== CPP_NAME
22822 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22824 cp_token
*identifier_token
;
22827 /* Look for the identifier. */
22828 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22829 ambiguous_p
= identifier_token
->error_reported
;
22830 identifier
= cp_parser_identifier (parser
);
22831 /* If the next token isn't an identifier, we are certainly not
22832 looking at a class-name. */
22833 if (identifier
== error_mark_node
)
22834 decl
= error_mark_node
;
22835 /* If we know this is a type-name, there's no need to look it
22837 else if (typename_p
)
22841 tree ambiguous_decls
;
22842 /* If we already know that this lookup is ambiguous, then
22843 we've already issued an error message; there's no reason
22847 cp_parser_simulate_error (parser
);
22848 return error_mark_node
;
22850 /* If the next token is a `::', then the name must be a type
22853 [basic.lookup.qual]
22855 During the lookup for a name preceding the :: scope
22856 resolution operator, object, function, and enumerator
22857 names are ignored. */
22858 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22859 tag_type
= scope_type
;
22860 /* Look up the name. */
22861 decl
= cp_parser_lookup_name (parser
, identifier
,
22863 /*is_template=*/false,
22864 /*is_namespace=*/false,
22865 check_dependency_p
,
22867 identifier_token
->location
);
22868 if (ambiguous_decls
)
22870 if (cp_parser_parsing_tentatively (parser
))
22871 cp_parser_simulate_error (parser
);
22872 return error_mark_node
;
22878 /* Try a template-id. */
22879 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22880 check_dependency_p
,
22883 if (decl
== error_mark_node
)
22884 return error_mark_node
;
22887 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22889 /* If this is a typename, create a TYPENAME_TYPE. */
22890 if (typename_p
&& decl
!= error_mark_node
)
22892 decl
= make_typename_type (scope
, decl
, typename_type
,
22893 /*complain=*/tf_error
);
22894 if (decl
!= error_mark_node
)
22895 decl
= TYPE_NAME (decl
);
22898 decl
= strip_using_decl (decl
);
22900 /* Check to see that it is really the name of a class. */
22901 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22902 && identifier_p (TREE_OPERAND (decl
, 0))
22903 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22904 /* Situations like this:
22906 template <typename T> struct A {
22907 typename T::template X<int>::I i;
22910 are problematic. Is `T::template X<int>' a class-name? The
22911 standard does not seem to be definitive, but there is no other
22912 valid interpretation of the following `::'. Therefore, those
22913 names are considered class-names. */
22915 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22916 if (decl
!= error_mark_node
)
22917 decl
= TYPE_NAME (decl
);
22919 else if (TREE_CODE (decl
) != TYPE_DECL
22920 || TREE_TYPE (decl
) == error_mark_node
22921 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22922 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22923 /* In Objective-C 2.0, a classname followed by '.' starts a
22924 dot-syntax expression, and it's not a type-name. */
22925 || (c_dialect_objc ()
22926 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22927 && objc_is_class_name (decl
)))
22928 decl
= error_mark_node
;
22930 if (decl
== error_mark_node
)
22931 cp_parser_error (parser
, "expected class-name");
22932 else if (identifier
&& !parser
->scope
)
22933 maybe_note_name_used_in_class (identifier
, decl
);
22938 /* Parse a class-specifier.
22941 class-head { member-specification [opt] }
22943 Returns the TREE_TYPE representing the class. */
22946 cp_parser_class_specifier_1 (cp_parser
* parser
)
22949 tree attributes
= NULL_TREE
;
22950 bool nested_name_specifier_p
;
22951 unsigned saved_num_template_parameter_lists
;
22952 bool saved_in_function_body
;
22953 unsigned char in_statement
;
22954 bool in_switch_statement_p
;
22955 bool saved_in_unbraced_linkage_specification_p
;
22956 tree old_scope
= NULL_TREE
;
22957 tree scope
= NULL_TREE
;
22958 cp_token
*closing_brace
;
22960 push_deferring_access_checks (dk_no_deferred
);
22962 /* Parse the class-head. */
22963 type
= cp_parser_class_head (parser
,
22964 &nested_name_specifier_p
);
22965 /* If the class-head was a semantic disaster, skip the entire body
22969 cp_parser_skip_to_end_of_block_or_statement (parser
);
22970 pop_deferring_access_checks ();
22971 return error_mark_node
;
22974 /* Look for the `{'. */
22975 matching_braces braces
;
22976 if (!braces
.require_open (parser
))
22978 pop_deferring_access_checks ();
22979 return error_mark_node
;
22982 cp_ensure_no_omp_declare_simd (parser
);
22983 cp_ensure_no_oacc_routine (parser
);
22985 /* Issue an error message if type-definitions are forbidden here. */
22986 cp_parser_check_type_definition (parser
);
22987 /* Remember that we are defining one more class. */
22988 ++parser
->num_classes_being_defined
;
22989 /* Inside the class, surrounding template-parameter-lists do not
22991 saved_num_template_parameter_lists
22992 = parser
->num_template_parameter_lists
;
22993 parser
->num_template_parameter_lists
= 0;
22994 /* We are not in a function body. */
22995 saved_in_function_body
= parser
->in_function_body
;
22996 parser
->in_function_body
= false;
22997 /* Or in a loop. */
22998 in_statement
= parser
->in_statement
;
22999 parser
->in_statement
= 0;
23000 /* Or in a switch. */
23001 in_switch_statement_p
= parser
->in_switch_statement_p
;
23002 parser
->in_switch_statement_p
= false;
23003 /* We are not immediately inside an extern "lang" block. */
23004 saved_in_unbraced_linkage_specification_p
23005 = parser
->in_unbraced_linkage_specification_p
;
23006 parser
->in_unbraced_linkage_specification_p
= false;
23008 // Associate constraints with the type.
23010 type
= associate_classtype_constraints (type
);
23012 /* Start the class. */
23013 if (nested_name_specifier_p
)
23015 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23016 old_scope
= push_inner_scope (scope
);
23018 type
= begin_class_definition (type
);
23020 if (type
== error_mark_node
)
23021 /* If the type is erroneous, skip the entire body of the class. */
23022 cp_parser_skip_to_closing_brace (parser
);
23024 /* Parse the member-specification. */
23025 cp_parser_member_specification_opt (parser
);
23027 /* Look for the trailing `}'. */
23028 closing_brace
= braces
.require_close (parser
);
23029 /* Look for trailing attributes to apply to this class. */
23030 if (cp_parser_allow_gnu_extensions_p (parser
))
23031 attributes
= cp_parser_gnu_attributes_opt (parser
);
23032 if (type
!= error_mark_node
)
23033 type
= finish_struct (type
, attributes
);
23034 if (nested_name_specifier_p
)
23035 pop_inner_scope (old_scope
, scope
);
23037 /* We've finished a type definition. Check for the common syntax
23038 error of forgetting a semicolon after the definition. We need to
23039 be careful, as we can't just check for not-a-semicolon and be done
23040 with it; the user might have typed:
23042 class X { } c = ...;
23043 class X { } *p = ...;
23045 and so forth. Instead, enumerate all the possible tokens that
23046 might follow this production; if we don't see one of them, then
23047 complain and silently insert the semicolon. */
23049 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23050 bool want_semicolon
= true;
23052 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23053 /* Don't try to parse c++11 attributes here. As per the
23054 grammar, that should be a task for
23055 cp_parser_decl_specifier_seq. */
23056 want_semicolon
= false;
23058 switch (token
->type
)
23061 case CPP_SEMICOLON
:
23064 case CPP_OPEN_PAREN
:
23065 case CPP_CLOSE_PAREN
:
23067 want_semicolon
= false;
23070 /* While it's legal for type qualifiers and storage class
23071 specifiers to follow type definitions in the grammar, only
23072 compiler testsuites contain code like that. Assume that if
23073 we see such code, then what we're really seeing is a case
23077 const <type> var = ...;
23082 static <type> func (...) ...
23084 i.e. the qualifier or specifier applies to the next
23085 declaration. To do so, however, we need to look ahead one
23086 more token to see if *that* token is a type specifier.
23088 This code could be improved to handle:
23091 static const <type> var = ...; */
23093 if (keyword_is_decl_specifier (token
->keyword
))
23095 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23097 /* Handling user-defined types here would be nice, but very
23100 = (lookahead
->type
== CPP_KEYWORD
23101 && keyword_begins_type_specifier (lookahead
->keyword
));
23108 /* If we don't have a type, then something is very wrong and we
23109 shouldn't try to do anything clever. Likewise for not seeing the
23111 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23113 /* Locate the closing brace. */
23114 cp_token_position prev
23115 = cp_lexer_previous_token_position (parser
->lexer
);
23116 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23117 location_t loc
= prev_token
->location
;
23119 /* We want to suggest insertion of a ';' immediately *after* the
23120 closing brace, so, if we can, offset the location by 1 column. */
23121 location_t next_loc
= loc
;
23122 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23123 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23125 rich_location
richloc (line_table
, next_loc
);
23127 /* If we successfully offset the location, suggest the fix-it. */
23128 if (next_loc
!= loc
)
23129 richloc
.add_fixit_insert_before (next_loc
, ";");
23131 if (CLASSTYPE_DECLARED_CLASS (type
))
23132 error_at (&richloc
,
23133 "expected %<;%> after class definition");
23134 else if (TREE_CODE (type
) == RECORD_TYPE
)
23135 error_at (&richloc
,
23136 "expected %<;%> after struct definition");
23137 else if (TREE_CODE (type
) == UNION_TYPE
)
23138 error_at (&richloc
,
23139 "expected %<;%> after union definition");
23141 gcc_unreachable ();
23143 /* Unget one token and smash it to look as though we encountered
23144 a semicolon in the input stream. */
23145 cp_lexer_set_token_position (parser
->lexer
, prev
);
23146 token
= cp_lexer_peek_token (parser
->lexer
);
23147 token
->type
= CPP_SEMICOLON
;
23148 token
->keyword
= RID_MAX
;
23152 /* If this class is not itself within the scope of another class,
23153 then we need to parse the bodies of all of the queued function
23154 definitions. Note that the queued functions defined in a class
23155 are not always processed immediately following the
23156 class-specifier for that class. Consider:
23159 struct B { void f() { sizeof (A); } };
23162 If `f' were processed before the processing of `A' were
23163 completed, there would be no way to compute the size of `A'.
23164 Note that the nesting we are interested in here is lexical --
23165 not the semantic nesting given by TYPE_CONTEXT. In particular,
23168 struct A { struct B; };
23169 struct A::B { void f() { } };
23171 there is no need to delay the parsing of `A::B::f'. */
23172 if (--parser
->num_classes_being_defined
== 0)
23175 tree class_type
= NULL_TREE
;
23176 tree pushed_scope
= NULL_TREE
;
23178 cp_default_arg_entry
*e
;
23179 tree save_ccp
, save_ccr
;
23181 if (any_erroneous_template_args_p (type
))
23183 /* Skip default arguments, NSDMIs, etc, in order to improve
23184 error recovery (c++/71169, c++/71832). */
23185 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23186 vec_safe_truncate (unparsed_nsdmis
, 0);
23187 vec_safe_truncate (unparsed_classes
, 0);
23188 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23191 /* In a first pass, parse default arguments to the functions.
23192 Then, in a second pass, parse the bodies of the functions.
23193 This two-phased approach handles cases like:
23201 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23204 /* If there are default arguments that have not yet been processed,
23205 take care of them now. */
23206 if (class_type
!= e
->class_type
)
23209 pop_scope (pushed_scope
);
23210 class_type
= e
->class_type
;
23211 pushed_scope
= push_scope (class_type
);
23213 /* Make sure that any template parameters are in scope. */
23214 maybe_begin_member_template_processing (decl
);
23215 /* Parse the default argument expressions. */
23216 cp_parser_late_parsing_default_args (parser
, decl
);
23217 /* Remove any template parameters from the symbol table. */
23218 maybe_end_member_template_processing ();
23220 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23221 /* Now parse any NSDMIs. */
23222 save_ccp
= current_class_ptr
;
23223 save_ccr
= current_class_ref
;
23224 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23226 if (class_type
!= DECL_CONTEXT (decl
))
23229 pop_scope (pushed_scope
);
23230 class_type
= DECL_CONTEXT (decl
);
23231 pushed_scope
= push_scope (class_type
);
23233 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23234 cp_parser_late_parsing_nsdmi (parser
, decl
);
23236 vec_safe_truncate (unparsed_nsdmis
, 0);
23237 current_class_ptr
= save_ccp
;
23238 current_class_ref
= save_ccr
;
23240 pop_scope (pushed_scope
);
23242 /* Now do some post-NSDMI bookkeeping. */
23243 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23244 after_nsdmi_defaulted_late_checks (class_type
);
23245 vec_safe_truncate (unparsed_classes
, 0);
23246 after_nsdmi_defaulted_late_checks (type
);
23248 /* Now parse the body of the functions. */
23251 /* OpenMP UDRs need to be parsed before all other functions. */
23252 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23253 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23254 cp_parser_late_parsing_for_member (parser
, decl
);
23255 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23256 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23257 cp_parser_late_parsing_for_member (parser
, decl
);
23260 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23261 cp_parser_late_parsing_for_member (parser
, decl
);
23262 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23265 vec_safe_push (unparsed_classes
, type
);
23267 /* Put back any saved access checks. */
23268 pop_deferring_access_checks ();
23270 /* Restore saved state. */
23271 parser
->in_switch_statement_p
= in_switch_statement_p
;
23272 parser
->in_statement
= in_statement
;
23273 parser
->in_function_body
= saved_in_function_body
;
23274 parser
->num_template_parameter_lists
23275 = saved_num_template_parameter_lists
;
23276 parser
->in_unbraced_linkage_specification_p
23277 = saved_in_unbraced_linkage_specification_p
;
23283 cp_parser_class_specifier (cp_parser
* parser
)
23286 timevar_push (TV_PARSE_STRUCT
);
23287 ret
= cp_parser_class_specifier_1 (parser
);
23288 timevar_pop (TV_PARSE_STRUCT
);
23292 /* Parse a class-head.
23295 class-key identifier [opt] base-clause [opt]
23296 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23297 class-key nested-name-specifier [opt] template-id
23300 class-virt-specifier:
23304 class-key attributes identifier [opt] base-clause [opt]
23305 class-key attributes nested-name-specifier identifier base-clause [opt]
23306 class-key attributes nested-name-specifier [opt] template-id
23309 Upon return BASES is initialized to the list of base classes (or
23310 NULL, if there are none) in the same form returned by
23311 cp_parser_base_clause.
23313 Returns the TYPE of the indicated class. Sets
23314 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23315 involving a nested-name-specifier was used, and FALSE otherwise.
23317 Returns error_mark_node if this is not a class-head.
23319 Returns NULL_TREE if the class-head is syntactically valid, but
23320 semantically invalid in a way that means we should skip the entire
23321 body of the class. */
23324 cp_parser_class_head (cp_parser
* parser
,
23325 bool* nested_name_specifier_p
)
23327 tree nested_name_specifier
;
23328 enum tag_types class_key
;
23329 tree id
= NULL_TREE
;
23330 tree type
= NULL_TREE
;
23333 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23334 bool template_id_p
= false;
23335 bool qualified_p
= false;
23336 bool invalid_nested_name_p
= false;
23337 bool invalid_explicit_specialization_p
= false;
23338 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23339 tree pushed_scope
= NULL_TREE
;
23340 unsigned num_templates
;
23341 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23342 /* Assume no nested-name-specifier will be present. */
23343 *nested_name_specifier_p
= false;
23344 /* Assume no template parameter lists will be used in defining the
23347 parser
->colon_corrects_to_scope_p
= false;
23349 /* Look for the class-key. */
23350 class_key
= cp_parser_class_key (parser
);
23351 if (class_key
== none_type
)
23352 return error_mark_node
;
23354 location_t class_head_start_location
= input_location
;
23356 /* Parse the attributes. */
23357 attributes
= cp_parser_attributes_opt (parser
);
23359 /* If the next token is `::', that is invalid -- but sometimes
23360 people do try to write:
23364 Handle this gracefully by accepting the extra qualifier, and then
23365 issuing an error about it later if this really is a
23366 class-head. If it turns out just to be an elaborated type
23367 specifier, remain silent. */
23368 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23369 qualified_p
= true;
23371 push_deferring_access_checks (dk_no_check
);
23373 /* Determine the name of the class. Begin by looking for an
23374 optional nested-name-specifier. */
23375 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23376 nested_name_specifier
23377 = cp_parser_nested_name_specifier_opt (parser
,
23378 /*typename_keyword_p=*/false,
23379 /*check_dependency_p=*/false,
23381 /*is_declaration=*/false);
23382 /* If there was a nested-name-specifier, then there *must* be an
23385 cp_token
*bad_template_keyword
= NULL
;
23387 if (nested_name_specifier
)
23389 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23390 /* Although the grammar says `identifier', it really means
23391 `class-name' or `template-name'. You are only allowed to
23392 define a class that has already been declared with this
23395 The proposed resolution for Core Issue 180 says that wherever
23396 you see `class T::X' you should treat `X' as a type-name.
23398 It is OK to define an inaccessible class; for example:
23400 class A { class B; };
23403 We do not know if we will see a class-name, or a
23404 template-name. We look for a class-name first, in case the
23405 class-name is a template-id; if we looked for the
23406 template-name first we would stop after the template-name. */
23407 cp_parser_parse_tentatively (parser
);
23408 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23409 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23410 type
= cp_parser_class_name (parser
,
23411 /*typename_keyword_p=*/false,
23412 /*template_keyword_p=*/false,
23414 /*check_dependency_p=*/false,
23415 /*class_head_p=*/true,
23416 /*is_declaration=*/false);
23417 /* If that didn't work, ignore the nested-name-specifier. */
23418 if (!cp_parser_parse_definitely (parser
))
23420 invalid_nested_name_p
= true;
23421 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23422 id
= cp_parser_identifier (parser
);
23423 if (id
== error_mark_node
)
23426 /* If we could not find a corresponding TYPE, treat this
23427 declaration like an unqualified declaration. */
23428 if (type
== error_mark_node
)
23429 nested_name_specifier
= NULL_TREE
;
23430 /* Otherwise, count the number of templates used in TYPE and its
23431 containing scopes. */
23433 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23435 /* Otherwise, the identifier is optional. */
23438 /* We don't know whether what comes next is a template-id,
23439 an identifier, or nothing at all. */
23440 cp_parser_parse_tentatively (parser
);
23441 /* Check for a template-id. */
23442 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23443 id
= cp_parser_template_id (parser
,
23444 /*template_keyword_p=*/false,
23445 /*check_dependency_p=*/true,
23447 /*is_declaration=*/true);
23448 /* If that didn't work, it could still be an identifier. */
23449 if (!cp_parser_parse_definitely (parser
))
23451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23453 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23454 id
= cp_parser_identifier (parser
);
23461 template_id_p
= true;
23466 pop_deferring_access_checks ();
23470 cp_parser_check_for_invalid_template_id (parser
, id
,
23472 type_start_token
->location
);
23474 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23476 /* If it's not a `:' or a `{' then we can't really be looking at a
23477 class-head, since a class-head only appears as part of a
23478 class-specifier. We have to detect this situation before calling
23479 xref_tag, since that has irreversible side-effects. */
23480 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23482 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23483 type
= error_mark_node
;
23487 /* At this point, we're going ahead with the class-specifier, even
23488 if some other problem occurs. */
23489 cp_parser_commit_to_tentative_parse (parser
);
23490 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23492 cp_parser_error (parser
,
23493 "cannot specify %<override%> for a class");
23494 type
= error_mark_node
;
23497 /* Issue the error about the overly-qualified name now. */
23500 cp_parser_error (parser
,
23501 "global qualification of class name is invalid");
23502 type
= error_mark_node
;
23505 else if (invalid_nested_name_p
)
23507 cp_parser_error (parser
,
23508 "qualified name does not name a class");
23509 type
= error_mark_node
;
23512 else if (nested_name_specifier
)
23516 if (bad_template_keyword
)
23517 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23518 keyword template shall not appear at the top level. */
23519 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23520 "keyword %<template%> not allowed in class-head-name");
23522 /* Reject typedef-names in class heads. */
23523 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23525 error_at (type_start_token
->location
,
23526 "invalid class name in declaration of %qD",
23532 /* Figure out in what scope the declaration is being placed. */
23533 scope
= current_scope ();
23534 /* If that scope does not contain the scope in which the
23535 class was originally declared, the program is invalid. */
23536 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23538 if (at_namespace_scope_p ())
23539 error_at (type_start_token
->location
,
23540 "declaration of %qD in namespace %qD which does not "
23542 type
, scope
, nested_name_specifier
);
23544 error_at (type_start_token
->location
,
23545 "declaration of %qD in %qD which does not enclose %qD",
23546 type
, scope
, nested_name_specifier
);
23552 A declarator-id shall not be qualified except for the
23553 definition of a ... nested class outside of its class
23554 ... [or] the definition or explicit instantiation of a
23555 class member of a namespace outside of its namespace. */
23556 if (scope
== nested_name_specifier
)
23558 permerror (nested_name_specifier_token_start
->location
,
23559 "extra qualification not allowed");
23560 nested_name_specifier
= NULL_TREE
;
23564 /* An explicit-specialization must be preceded by "template <>". If
23565 it is not, try to recover gracefully. */
23566 if (at_namespace_scope_p ()
23567 && parser
->num_template_parameter_lists
== 0
23568 && !processing_template_parmlist
23571 /* Build a location of this form:
23572 struct typename <ARGS>
23573 ^~~~~~~~~~~~~~~~~~~~~~
23574 with caret==start at the start token, and
23575 finishing at the end of the type. */
23576 location_t reported_loc
23577 = make_location (class_head_start_location
,
23578 class_head_start_location
,
23579 get_finish (type_start_token
->location
));
23580 rich_location
richloc (line_table
, reported_loc
);
23581 richloc
.add_fixit_insert_before (class_head_start_location
,
23583 error_at (&richloc
,
23584 "an explicit specialization must be preceded by"
23585 " %<template <>%>");
23586 invalid_explicit_specialization_p
= true;
23587 /* Take the same action that would have been taken by
23588 cp_parser_explicit_specialization. */
23589 ++parser
->num_template_parameter_lists
;
23590 begin_specialization ();
23592 /* There must be no "return" statements between this point and the
23593 end of this function; set "type "to the correct return value and
23594 use "goto done;" to return. */
23595 /* Make sure that the right number of template parameters were
23597 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23599 type_start_token
->location
,
23600 /*declarator=*/NULL
))
23602 /* If something went wrong, there is no point in even trying to
23603 process the class-definition. */
23608 /* Look up the type. */
23611 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23612 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23613 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23615 error_at (type_start_token
->location
,
23616 "function template %qD redeclared as a class template", id
);
23617 type
= error_mark_node
;
23621 type
= TREE_TYPE (id
);
23622 type
= maybe_process_partial_specialization (type
);
23624 /* Check the scope while we still know whether or not we had a
23625 nested-name-specifier. */
23626 if (type
!= error_mark_node
)
23627 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23629 if (nested_name_specifier
)
23630 pushed_scope
= push_scope (nested_name_specifier
);
23632 else if (nested_name_specifier
)
23638 template <typename T> struct S { struct T };
23639 template <typename T> struct S<T>::T { };
23641 we will get a TYPENAME_TYPE when processing the definition of
23642 `S::T'. We need to resolve it to the actual type before we
23643 try to define it. */
23644 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23646 class_type
= resolve_typename_type (TREE_TYPE (type
),
23647 /*only_current_p=*/false);
23648 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23649 type
= TYPE_NAME (class_type
);
23652 cp_parser_error (parser
, "could not resolve typename type");
23653 type
= error_mark_node
;
23657 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23658 == error_mark_node
)
23664 class_type
= current_class_type
;
23665 /* Enter the scope indicated by the nested-name-specifier. */
23666 pushed_scope
= push_scope (nested_name_specifier
);
23667 /* Get the canonical version of this type. */
23668 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23669 /* Call push_template_decl if it seems like we should be defining a
23670 template either from the template headers or the type we're
23671 defining, so that we diagnose both extra and missing headers. */
23672 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23673 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23674 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23676 type
= push_template_decl (type
);
23677 if (type
== error_mark_node
)
23684 type
= TREE_TYPE (type
);
23685 *nested_name_specifier_p
= true;
23687 else /* The name is not a nested name. */
23689 /* If the class was unnamed, create a dummy name. */
23691 id
= make_anon_name ();
23692 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23693 ? ts_within_enclosing_non_class
23695 type
= xref_tag (class_key
, id
, tag_scope
,
23696 parser
->num_template_parameter_lists
);
23699 /* Indicate whether this class was declared as a `class' or as a
23701 if (TREE_CODE (type
) == RECORD_TYPE
)
23702 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23703 cp_parser_check_class_key (class_key
, type
);
23705 /* If this type was already complete, and we see another definition,
23706 that's an error. */
23707 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23709 error_at (type_start_token
->location
, "redefinition of %q#T",
23711 inform (location_of (type
), "previous definition of %q#T",
23716 else if (type
== error_mark_node
)
23721 /* Apply attributes now, before any use of the class as a template
23722 argument in its base list. */
23723 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23724 fixup_attribute_variants (type
);
23727 /* We will have entered the scope containing the class; the names of
23728 base classes should be looked up in that context. For example:
23730 struct A { struct B {}; struct C; };
23731 struct A::C : B {};
23735 /* Get the list of base-classes, if there is one. */
23736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23738 /* PR59482: enter the class scope so that base-specifiers are looked
23742 bases
= cp_parser_base_clause (parser
);
23743 /* PR59482: get out of the previously pushed class scope so that the
23744 subsequent pops pop the right thing. */
23751 /* If we're really defining a class, process the base classes.
23752 If they're invalid, fail. */
23753 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23754 xref_basetypes (type
, bases
);
23757 /* Leave the scope given by the nested-name-specifier. We will
23758 enter the class scope itself while processing the members. */
23760 pop_scope (pushed_scope
);
23762 if (invalid_explicit_specialization_p
)
23764 end_specialization ();
23765 --parser
->num_template_parameter_lists
;
23769 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23770 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23771 CLASSTYPE_FINAL (type
) = 1;
23773 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23777 /* Parse a class-key.
23784 Returns the kind of class-key specified, or none_type to indicate
23787 static enum tag_types
23788 cp_parser_class_key (cp_parser
* parser
)
23791 enum tag_types tag_type
;
23793 /* Look for the class-key. */
23794 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23798 /* Check to see if the TOKEN is a class-key. */
23799 tag_type
= cp_parser_token_is_class_key (token
);
23801 cp_parser_error (parser
, "expected class-key");
23805 /* Parse a type-parameter-key.
23807 type-parameter-key:
23813 cp_parser_type_parameter_key (cp_parser
* parser
)
23815 /* Look for the type-parameter-key. */
23816 enum tag_types tag_type
= none_type
;
23817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23818 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23820 cp_lexer_consume_token (parser
->lexer
);
23821 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23822 /* typename is not allowed in a template template parameter
23823 by the standard until C++17. */
23824 pedwarn (token
->location
, OPT_Wpedantic
,
23825 "ISO C++ forbids typename key in template template parameter;"
23826 " use -std=c++17 or -std=gnu++17");
23829 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23834 /* Parse an (optional) member-specification.
23836 member-specification:
23837 member-declaration member-specification [opt]
23838 access-specifier : member-specification [opt] */
23841 cp_parser_member_specification_opt (cp_parser
* parser
)
23848 /* Peek at the next token. */
23849 token
= cp_lexer_peek_token (parser
->lexer
);
23850 /* If it's a `}', or EOF then we've seen all the members. */
23851 if (token
->type
== CPP_CLOSE_BRACE
23852 || token
->type
== CPP_EOF
23853 || token
->type
== CPP_PRAGMA_EOL
)
23856 /* See if this token is a keyword. */
23857 keyword
= token
->keyword
;
23861 case RID_PROTECTED
:
23863 /* Consume the access-specifier. */
23864 cp_lexer_consume_token (parser
->lexer
);
23865 /* Remember which access-specifier is active. */
23866 current_access_specifier
= token
->u
.value
;
23867 /* Look for the `:'. */
23868 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23872 /* Accept #pragmas at class scope. */
23873 if (token
->type
== CPP_PRAGMA
)
23875 cp_parser_pragma (parser
, pragma_member
, NULL
);
23879 /* Otherwise, the next construction must be a
23880 member-declaration. */
23881 cp_parser_member_declaration (parser
);
23886 /* Parse a member-declaration.
23888 member-declaration:
23889 decl-specifier-seq [opt] member-declarator-list [opt] ;
23890 function-definition ; [opt]
23891 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23893 template-declaration
23896 member-declarator-list:
23898 member-declarator-list , member-declarator
23901 declarator pure-specifier [opt]
23902 declarator constant-initializer [opt]
23903 identifier [opt] : constant-expression
23907 member-declaration:
23908 __extension__ member-declaration
23911 declarator attributes [opt] pure-specifier [opt]
23912 declarator attributes [opt] constant-initializer [opt]
23913 identifier [opt] attributes [opt] : constant-expression
23917 member-declaration:
23918 static_assert-declaration */
23921 cp_parser_member_declaration (cp_parser
* parser
)
23923 cp_decl_specifier_seq decl_specifiers
;
23924 tree prefix_attributes
;
23926 int declares_class_or_enum
;
23928 cp_token
*token
= NULL
;
23929 cp_token
*decl_spec_token_start
= NULL
;
23930 cp_token
*initializer_token_start
= NULL
;
23931 int saved_pedantic
;
23932 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23934 /* Check for the `__extension__' keyword. */
23935 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23938 cp_parser_member_declaration (parser
);
23939 /* Restore the old value of the PEDANTIC flag. */
23940 pedantic
= saved_pedantic
;
23945 /* Check for a template-declaration. */
23946 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23948 /* An explicit specialization here is an error condition, and we
23949 expect the specialization handler to detect and report this. */
23950 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23951 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23952 cp_parser_explicit_specialization (parser
);
23954 cp_parser_template_declaration (parser
, /*member_p=*/true);
23958 /* Check for a template introduction. */
23959 else if (cp_parser_template_declaration_after_export (parser
, true))
23962 /* Check for a using-declaration. */
23963 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23965 if (cxx_dialect
< cxx11
)
23967 /* Parse the using-declaration. */
23968 cp_parser_using_declaration (parser
,
23969 /*access_declaration_p=*/false);
23975 bool alias_decl_expected
;
23976 cp_parser_parse_tentatively (parser
);
23977 decl
= cp_parser_alias_declaration (parser
);
23978 /* Note that if we actually see the '=' token after the
23979 identifier, cp_parser_alias_declaration commits the
23980 tentative parse. In that case, we really expect an
23981 alias-declaration. Otherwise, we expect a using
23983 alias_decl_expected
=
23984 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23985 cp_parser_parse_definitely (parser
);
23987 if (alias_decl_expected
)
23988 finish_member_declaration (decl
);
23990 cp_parser_using_declaration (parser
,
23991 /*access_declaration_p=*/false);
23996 /* Check for @defs. */
23997 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24000 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24001 ivar
= ivar_chains
;
24005 ivar
= TREE_CHAIN (member
);
24006 TREE_CHAIN (member
) = NULL_TREE
;
24007 finish_member_declaration (member
);
24012 /* If the next token is `static_assert' we have a static assertion. */
24013 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24015 cp_parser_static_assert (parser
, /*member_p=*/true);
24019 parser
->colon_corrects_to_scope_p
= false;
24021 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24024 /* Parse the decl-specifier-seq. */
24025 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24026 cp_parser_decl_specifier_seq (parser
,
24027 CP_PARSER_FLAGS_OPTIONAL
,
24029 &declares_class_or_enum
);
24030 /* Check for an invalid type-name. */
24031 if (!decl_specifiers
.any_type_specifiers_p
24032 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24034 /* If there is no declarator, then the decl-specifier-seq should
24036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24038 /* If there was no decl-specifier-seq, and the next token is a
24039 `;', then we have something like:
24045 Each member-declaration shall declare at least one member
24046 name of the class. */
24047 if (!decl_specifiers
.any_specifiers_p
)
24049 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24050 if (!in_system_header_at (token
->location
))
24052 gcc_rich_location
richloc (token
->location
);
24053 richloc
.add_fixit_remove ();
24054 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24061 /* See if this declaration is a friend. */
24062 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24063 /* If there were decl-specifiers, check to see if there was
24064 a class-declaration. */
24065 type
= check_tag_decl (&decl_specifiers
,
24066 /*explicit_type_instantiation_p=*/false);
24067 /* Nested classes have already been added to the class, but
24068 a `friend' needs to be explicitly registered. */
24071 /* If the `friend' keyword was present, the friend must
24072 be introduced with a class-key. */
24073 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24074 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24075 "in C++03 a class-key must be used "
24076 "when declaring a friend");
24079 template <typename T> struct A {
24080 friend struct A<T>::B;
24083 A<T>::B will be represented by a TYPENAME_TYPE, and
24084 therefore not recognized by check_tag_decl. */
24087 type
= decl_specifiers
.type
;
24088 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24089 type
= TREE_TYPE (type
);
24091 if (!type
|| !TYPE_P (type
))
24092 error_at (decl_spec_token_start
->location
,
24093 "friend declaration does not name a class or "
24096 make_friend_class (current_class_type
, type
,
24097 /*complain=*/true);
24099 /* If there is no TYPE, an error message will already have
24101 else if (!type
|| type
== error_mark_node
)
24103 /* An anonymous aggregate has to be handled specially; such
24104 a declaration really declares a data member (with a
24105 particular type), as opposed to a nested class. */
24106 else if (ANON_AGGR_TYPE_P (type
))
24109 if (decl_specifiers
.storage_class
!= sc_none
)
24110 error_at (decl_spec_token_start
->location
,
24111 "a storage class on an anonymous aggregate "
24112 "in class scope is not allowed");
24114 /* Remove constructors and such from TYPE, now that we
24115 know it is an anonymous aggregate. */
24116 fixup_anonymous_aggr (type
);
24117 /* And make the corresponding data member. */
24118 decl
= build_decl (decl_spec_token_start
->location
,
24119 FIELD_DECL
, NULL_TREE
, type
);
24120 /* Add it to the class. */
24121 finish_member_declaration (decl
);
24124 cp_parser_check_access_in_redeclaration
24126 decl_spec_token_start
->location
);
24131 bool assume_semicolon
= false;
24133 /* Clear attributes from the decl_specifiers but keep them
24134 around as prefix attributes that apply them to the entity
24136 prefix_attributes
= decl_specifiers
.attributes
;
24137 decl_specifiers
.attributes
= NULL_TREE
;
24139 /* See if these declarations will be friends. */
24140 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24142 /* Keep going until we hit the `;' at the end of the
24144 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24146 tree attributes
= NULL_TREE
;
24147 tree first_attribute
;
24149 bool named_bitfld
= false;
24151 /* Peek at the next token. */
24152 token
= cp_lexer_peek_token (parser
->lexer
);
24154 /* The following code wants to know early if it is a bit-field
24155 or some other declaration. Attributes can appear before
24156 the `:' token. Skip over them without consuming any tokens
24157 to peek if they are followed by `:'. */
24158 if (cp_next_tokens_can_be_attribute_p (parser
)
24159 || (token
->type
== CPP_NAME
24160 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24161 && (named_bitfld
= true)))
24164 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24165 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24168 /* Check for a bitfield declaration. */
24169 if (token
->type
== CPP_COLON
24170 || (token
->type
== CPP_NAME
24171 && token
== cp_lexer_peek_token (parser
->lexer
)
24172 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24173 && (named_bitfld
= true)))
24177 tree late_attributes
= NULL_TREE
;
24178 location_t id_location
24179 = cp_lexer_peek_token (parser
->lexer
)->location
;
24182 identifier
= cp_parser_identifier (parser
);
24184 identifier
= NULL_TREE
;
24186 /* Look for attributes that apply to the bitfield. */
24187 attributes
= cp_parser_attributes_opt (parser
);
24189 /* Consume the `:' token. */
24190 cp_lexer_consume_token (parser
->lexer
);
24192 /* Get the width of the bitfield. */
24193 width
= cp_parser_constant_expression (parser
, false, NULL
,
24194 cxx_dialect
>= cxx11
);
24196 /* In C++2A and as extension for C++11 and above we allow
24197 default member initializers for bit-fields. */
24198 initializer
= NULL_TREE
;
24199 if (cxx_dialect
>= cxx11
24200 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24201 || cp_lexer_next_token_is (parser
->lexer
,
24205 = cp_lexer_peek_token (parser
->lexer
)->location
;
24206 if (cxx_dialect
< cxx2a
24207 && !in_system_header_at (loc
)
24208 && identifier
!= NULL_TREE
)
24210 "default member initializers for bit-fields "
24211 "only available with -std=c++2a or "
24214 initializer
= cp_parser_save_nsdmi (parser
);
24215 if (identifier
== NULL_TREE
)
24217 error_at (loc
, "default member initializer for "
24218 "unnamed bit-field");
24219 initializer
= NULL_TREE
;
24224 /* Look for attributes that apply to the bitfield after
24225 the `:' token and width. This is where GCC used to
24226 parse attributes in the past, pedwarn if there is
24227 a std attribute. */
24228 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24229 pedwarn (input_location
, OPT_Wpedantic
,
24230 "ISO C++ allows bit-field attributes only "
24231 "before the %<:%> token");
24233 late_attributes
= cp_parser_attributes_opt (parser
);
24236 attributes
= attr_chainon (attributes
, late_attributes
);
24238 /* Remember which attributes are prefix attributes and
24240 first_attribute
= attributes
;
24241 /* Combine the attributes. */
24242 attributes
= attr_chainon (prefix_attributes
, attributes
);
24244 /* Create the bitfield declaration. */
24245 decl
= grokbitfield (identifier
24246 ? make_id_declarator (NULL_TREE
,
24252 width
, initializer
,
24257 cp_declarator
*declarator
;
24258 tree asm_specification
;
24259 int ctor_dtor_or_conv_p
;
24261 /* Parse the declarator. */
24263 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24264 &ctor_dtor_or_conv_p
,
24265 /*parenthesized_p=*/NULL
,
24269 /* If something went wrong parsing the declarator, make sure
24270 that we at least consume some tokens. */
24271 if (declarator
== cp_error_declarator
)
24273 /* Skip to the end of the statement. */
24274 cp_parser_skip_to_end_of_statement (parser
);
24275 /* If the next token is not a semicolon, that is
24276 probably because we just skipped over the body of
24277 a function. So, we consume a semicolon if
24278 present, but do not issue an error message if it
24280 if (cp_lexer_next_token_is (parser
->lexer
,
24282 cp_lexer_consume_token (parser
->lexer
);
24286 if (declares_class_or_enum
& 2)
24287 cp_parser_check_for_definition_in_return_type
24288 (declarator
, decl_specifiers
.type
,
24289 decl_specifiers
.locations
[ds_type_spec
]);
24291 /* Look for an asm-specification. */
24292 asm_specification
= cp_parser_asm_specification_opt (parser
);
24293 /* Look for attributes that apply to the declaration. */
24294 attributes
= cp_parser_attributes_opt (parser
);
24295 /* Remember which attributes are prefix attributes and
24297 first_attribute
= attributes
;
24298 /* Combine the attributes. */
24299 attributes
= attr_chainon (prefix_attributes
, attributes
);
24301 /* If it's an `=', then we have a constant-initializer or a
24302 pure-specifier. It is not correct to parse the
24303 initializer before registering the member declaration
24304 since the member declaration should be in scope while
24305 its initializer is processed. However, the rest of the
24306 front end does not yet provide an interface that allows
24307 us to handle this correctly. */
24308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24312 A pure-specifier shall be used only in the declaration of
24313 a virtual function.
24315 A member-declarator can contain a constant-initializer
24316 only if it declares a static member of integral or
24319 Therefore, if the DECLARATOR is for a function, we look
24320 for a pure-specifier; otherwise, we look for a
24321 constant-initializer. When we call `grokfield', it will
24322 perform more stringent semantics checks. */
24323 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24324 if (function_declarator_p (declarator
)
24325 || (decl_specifiers
.type
24326 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24327 && declarator
->kind
== cdk_id
24328 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24329 == FUNCTION_TYPE
)))
24330 initializer
= cp_parser_pure_specifier (parser
);
24331 else if (decl_specifiers
.storage_class
!= sc_static
)
24332 initializer
= cp_parser_save_nsdmi (parser
);
24333 else if (cxx_dialect
>= cxx11
)
24336 /* Don't require a constant rvalue in C++11, since we
24337 might want a reference constant. We'll enforce
24338 constancy later. */
24339 cp_lexer_consume_token (parser
->lexer
);
24340 /* Parse the initializer. */
24341 initializer
= cp_parser_initializer_clause (parser
,
24345 /* Parse the initializer. */
24346 initializer
= cp_parser_constant_initializer (parser
);
24348 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24349 && !function_declarator_p (declarator
))
24352 if (decl_specifiers
.storage_class
!= sc_static
)
24353 initializer
= cp_parser_save_nsdmi (parser
);
24355 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24357 /* Otherwise, there is no initializer. */
24359 initializer
= NULL_TREE
;
24361 /* See if we are probably looking at a function
24362 definition. We are certainly not looking at a
24363 member-declarator. Calling `grokfield' has
24364 side-effects, so we must not do it unless we are sure
24365 that we are looking at a member-declarator. */
24366 if (cp_parser_token_starts_function_definition_p
24367 (cp_lexer_peek_token (parser
->lexer
)))
24369 /* The grammar does not allow a pure-specifier to be
24370 used when a member function is defined. (It is
24371 possible that this fact is an oversight in the
24372 standard, since a pure function may be defined
24373 outside of the class-specifier. */
24374 if (initializer
&& initializer_token_start
)
24375 error_at (initializer_token_start
->location
,
24376 "pure-specifier on function-definition");
24377 decl
= cp_parser_save_member_function_body (parser
,
24381 if (parser
->fully_implicit_function_template_p
)
24382 decl
= finish_fully_implicit_template (parser
, decl
);
24383 /* If the member was not a friend, declare it here. */
24385 finish_member_declaration (decl
);
24386 /* Peek at the next token. */
24387 token
= cp_lexer_peek_token (parser
->lexer
);
24388 /* If the next token is a semicolon, consume it. */
24389 if (token
->type
== CPP_SEMICOLON
)
24391 location_t semicolon_loc
24392 = cp_lexer_consume_token (parser
->lexer
)->location
;
24393 gcc_rich_location
richloc (semicolon_loc
);
24394 richloc
.add_fixit_remove ();
24395 warning_at (&richloc
, OPT_Wextra_semi
,
24396 "extra %<;%> after in-class "
24397 "function definition");
24402 if (declarator
->kind
== cdk_function
)
24403 declarator
->id_loc
= token
->location
;
24404 /* Create the declaration. */
24405 decl
= grokfield (declarator
, &decl_specifiers
,
24406 initializer
, /*init_const_expr_p=*/true,
24407 asm_specification
, attributes
);
24408 if (parser
->fully_implicit_function_template_p
)
24411 finish_fully_implicit_template (parser
, 0);
24413 decl
= finish_fully_implicit_template (parser
, decl
);
24417 cp_finalize_omp_declare_simd (parser
, decl
);
24418 cp_finalize_oacc_routine (parser
, decl
, false);
24420 /* Reset PREFIX_ATTRIBUTES. */
24421 if (attributes
!= error_mark_node
)
24423 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24424 attributes
= TREE_CHAIN (attributes
);
24426 TREE_CHAIN (attributes
) = NULL_TREE
;
24429 /* If there is any qualification still in effect, clear it
24430 now; we will be starting fresh with the next declarator. */
24431 parser
->scope
= NULL_TREE
;
24432 parser
->qualifying_scope
= NULL_TREE
;
24433 parser
->object_scope
= NULL_TREE
;
24434 /* If it's a `,', then there are more declarators. */
24435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24437 cp_lexer_consume_token (parser
->lexer
);
24438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24440 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24441 gcc_rich_location
richloc (token
->location
);
24442 richloc
.add_fixit_remove ();
24443 error_at (&richloc
, "stray %<,%> at end of "
24444 "member declaration");
24447 /* If the next token isn't a `;', then we have a parse error. */
24448 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24451 /* The next token might be a ways away from where the
24452 actual semicolon is missing. Find the previous token
24453 and use that for our error position. */
24454 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24455 gcc_rich_location
richloc (token
->location
);
24456 richloc
.add_fixit_insert_after (";");
24457 error_at (&richloc
, "expected %<;%> at end of "
24458 "member declaration");
24460 /* Assume that the user meant to provide a semicolon. If
24461 we were to cp_parser_skip_to_end_of_statement, we might
24462 skip to a semicolon inside a member function definition
24463 and issue nonsensical error messages. */
24464 assume_semicolon
= true;
24469 /* Add DECL to the list of members. */
24471 /* Explicitly include, eg, NSDMIs, for better error
24472 recovery (c++/58650). */
24473 || !DECL_DECLARES_FUNCTION_P (decl
))
24474 finish_member_declaration (decl
);
24476 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24477 cp_parser_save_default_args (parser
, decl
);
24478 else if (TREE_CODE (decl
) == FIELD_DECL
24479 && DECL_INITIAL (decl
))
24480 /* Add DECL to the queue of NSDMI to be parsed later. */
24481 vec_safe_push (unparsed_nsdmis
, decl
);
24484 if (assume_semicolon
)
24489 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24491 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24494 /* Parse a pure-specifier.
24499 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24500 Otherwise, ERROR_MARK_NODE is returned. */
24503 cp_parser_pure_specifier (cp_parser
* parser
)
24507 /* Look for the `=' token. */
24508 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24509 return error_mark_node
;
24510 /* Look for the `0' token. */
24511 token
= cp_lexer_peek_token (parser
->lexer
);
24513 if (token
->type
== CPP_EOF
24514 || token
->type
== CPP_PRAGMA_EOL
)
24515 return error_mark_node
;
24517 cp_lexer_consume_token (parser
->lexer
);
24519 /* Accept = default or = delete in c++0x mode. */
24520 if (token
->keyword
== RID_DEFAULT
24521 || token
->keyword
== RID_DELETE
)
24523 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24524 return token
->u
.value
;
24527 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24528 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24530 cp_parser_error (parser
,
24531 "invalid pure specifier (only %<= 0%> is allowed)");
24532 cp_parser_skip_to_end_of_statement (parser
);
24533 return error_mark_node
;
24535 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24537 error_at (token
->location
, "templates may not be %<virtual%>");
24538 return error_mark_node
;
24541 return integer_zero_node
;
24544 /* Parse a constant-initializer.
24546 constant-initializer:
24547 = constant-expression
24549 Returns a representation of the constant-expression. */
24552 cp_parser_constant_initializer (cp_parser
* parser
)
24554 /* Look for the `=' token. */
24555 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24556 return error_mark_node
;
24558 /* It is invalid to write:
24560 struct S { static const int i = { 7 }; };
24563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24565 cp_parser_error (parser
,
24566 "a brace-enclosed initializer is not allowed here");
24567 /* Consume the opening brace. */
24568 matching_braces braces
;
24569 braces
.consume_open (parser
);
24570 /* Skip the initializer. */
24571 cp_parser_skip_to_closing_brace (parser
);
24572 /* Look for the trailing `}'. */
24573 braces
.require_close (parser
);
24575 return error_mark_node
;
24578 return cp_parser_constant_expression (parser
);
24581 /* Derived classes [gram.class.derived] */
24583 /* Parse a base-clause.
24586 : base-specifier-list
24588 base-specifier-list:
24589 base-specifier ... [opt]
24590 base-specifier-list , base-specifier ... [opt]
24592 Returns a TREE_LIST representing the base-classes, in the order in
24593 which they were declared. The representation of each node is as
24594 described by cp_parser_base_specifier.
24596 In the case that no bases are specified, this function will return
24597 NULL_TREE, not ERROR_MARK_NODE. */
24600 cp_parser_base_clause (cp_parser
* parser
)
24602 tree bases
= NULL_TREE
;
24604 /* Look for the `:' that begins the list. */
24605 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24607 /* Scan the base-specifier-list. */
24612 bool pack_expansion_p
= false;
24614 /* Look for the base-specifier. */
24615 base
= cp_parser_base_specifier (parser
);
24616 /* Look for the (optional) ellipsis. */
24617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24619 /* Consume the `...'. */
24620 cp_lexer_consume_token (parser
->lexer
);
24622 pack_expansion_p
= true;
24625 /* Add BASE to the front of the list. */
24626 if (base
&& base
!= error_mark_node
)
24628 if (pack_expansion_p
)
24629 /* Make this a pack expansion type. */
24630 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24632 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24634 TREE_CHAIN (base
) = bases
;
24638 /* Peek at the next token. */
24639 token
= cp_lexer_peek_token (parser
->lexer
);
24640 /* If it's not a comma, then the list is complete. */
24641 if (token
->type
!= CPP_COMMA
)
24643 /* Consume the `,'. */
24644 cp_lexer_consume_token (parser
->lexer
);
24647 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24648 base class had a qualified name. However, the next name that
24649 appears is certainly not qualified. */
24650 parser
->scope
= NULL_TREE
;
24651 parser
->qualifying_scope
= NULL_TREE
;
24652 parser
->object_scope
= NULL_TREE
;
24654 return nreverse (bases
);
24657 /* Parse a base-specifier.
24660 :: [opt] nested-name-specifier [opt] class-name
24661 virtual access-specifier [opt] :: [opt] nested-name-specifier
24663 access-specifier virtual [opt] :: [opt] nested-name-specifier
24666 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24667 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24668 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24669 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24672 cp_parser_base_specifier (cp_parser
* parser
)
24676 bool virtual_p
= false;
24677 bool duplicate_virtual_error_issued_p
= false;
24678 bool duplicate_access_error_issued_p
= false;
24679 bool class_scope_p
, template_p
;
24680 tree access
= access_default_node
;
24683 /* Process the optional `virtual' and `access-specifier'. */
24686 /* Peek at the next token. */
24687 token
= cp_lexer_peek_token (parser
->lexer
);
24688 /* Process `virtual'. */
24689 switch (token
->keyword
)
24692 /* If `virtual' appears more than once, issue an error. */
24693 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24695 cp_parser_error (parser
,
24696 "%<virtual%> specified more than once in base-specifier");
24697 duplicate_virtual_error_issued_p
= true;
24702 /* Consume the `virtual' token. */
24703 cp_lexer_consume_token (parser
->lexer
);
24708 case RID_PROTECTED
:
24710 /* If more than one access specifier appears, issue an
24712 if (access
!= access_default_node
24713 && !duplicate_access_error_issued_p
)
24715 cp_parser_error (parser
,
24716 "more than one access specifier in base-specifier");
24717 duplicate_access_error_issued_p
= true;
24720 access
= ridpointers
[(int) token
->keyword
];
24722 /* Consume the access-specifier. */
24723 cp_lexer_consume_token (parser
->lexer
);
24732 /* It is not uncommon to see programs mechanically, erroneously, use
24733 the 'typename' keyword to denote (dependent) qualified types
24734 as base classes. */
24735 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24737 token
= cp_lexer_peek_token (parser
->lexer
);
24738 if (!processing_template_decl
)
24739 error_at (token
->location
,
24740 "keyword %<typename%> not allowed outside of templates");
24742 error_at (token
->location
,
24743 "keyword %<typename%> not allowed in this context "
24744 "(the base class is implicitly a type)");
24745 cp_lexer_consume_token (parser
->lexer
);
24748 /* Look for the optional `::' operator. */
24749 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24750 /* Look for the nested-name-specifier. The simplest way to
24755 The keyword `typename' is not permitted in a base-specifier or
24756 mem-initializer; in these contexts a qualified name that
24757 depends on a template-parameter is implicitly assumed to be a
24760 is to pretend that we have seen the `typename' keyword at this
24762 cp_parser_nested_name_specifier_opt (parser
,
24763 /*typename_keyword_p=*/true,
24764 /*check_dependency_p=*/true,
24766 /*is_declaration=*/true);
24767 /* If the base class is given by a qualified name, assume that names
24768 we see are type names or templates, as appropriate. */
24769 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24770 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24773 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24774 /* DR 950 allows decltype as a base-specifier. */
24775 type
= cp_parser_decltype (parser
);
24778 /* Otherwise, look for the class-name. */
24779 type
= cp_parser_class_name (parser
,
24783 /*check_dependency_p=*/true,
24784 /*class_head_p=*/false,
24785 /*is_declaration=*/true);
24786 type
= TREE_TYPE (type
);
24789 if (type
== error_mark_node
)
24790 return error_mark_node
;
24792 return finish_base_specifier (type
, access
, virtual_p
);
24795 /* Exception handling [gram.exception] */
24797 /* Parse an (optional) noexcept-specification.
24799 noexcept-specification:
24800 noexcept ( constant-expression ) [opt]
24802 If no noexcept-specification is present, returns NULL_TREE.
24803 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24804 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24805 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24806 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24807 in which case a boolean condition is returned instead. */
24810 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24811 bool require_constexpr
,
24812 bool* consumed_expr
,
24816 const char *saved_message
;
24818 /* Peek at the next token. */
24819 token
= cp_lexer_peek_token (parser
->lexer
);
24821 /* Is it a noexcept-specification? */
24822 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24825 cp_lexer_consume_token (parser
->lexer
);
24827 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24829 matching_parens parens
;
24830 parens
.consume_open (parser
);
24832 tree save_ccp
= current_class_ptr
;
24833 tree save_ccr
= current_class_ref
;
24835 if (current_class_type
)
24836 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
24838 if (require_constexpr
)
24840 /* Types may not be defined in an exception-specification. */
24841 saved_message
= parser
->type_definition_forbidden_message
;
24842 parser
->type_definition_forbidden_message
24843 = G_("types may not be defined in an exception-specification");
24845 expr
= cp_parser_constant_expression (parser
);
24847 /* Restore the saved message. */
24848 parser
->type_definition_forbidden_message
= saved_message
;
24852 expr
= cp_parser_expression (parser
);
24853 *consumed_expr
= true;
24856 parens
.require_close (parser
);
24858 current_class_ptr
= save_ccp
;
24859 current_class_ref
= save_ccr
;
24863 expr
= boolean_true_node
;
24864 if (!require_constexpr
)
24865 *consumed_expr
= false;
24868 /* We cannot build a noexcept-spec right away because this will check
24869 that expr is a constexpr. */
24871 return build_noexcept_spec (expr
, tf_warning_or_error
);
24879 /* Parse an (optional) exception-specification.
24881 exception-specification:
24882 throw ( type-id-list [opt] )
24884 Returns a TREE_LIST representing the exception-specification. The
24885 TREE_VALUE of each node is a type. */
24888 cp_parser_exception_specification_opt (cp_parser
* parser
)
24892 const char *saved_message
;
24894 /* Peek at the next token. */
24895 token
= cp_lexer_peek_token (parser
->lexer
);
24897 /* Is it a noexcept-specification? */
24898 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24900 if (type_id_list
!= NULL_TREE
)
24901 return type_id_list
;
24903 /* If it's not `throw', then there's no exception-specification. */
24904 if (!cp_parser_is_keyword (token
, RID_THROW
))
24907 location_t loc
= token
->location
;
24909 /* Consume the `throw'. */
24910 cp_lexer_consume_token (parser
->lexer
);
24912 /* Look for the `('. */
24913 matching_parens parens
;
24914 parens
.require_open (parser
);
24916 /* Peek at the next token. */
24917 token
= cp_lexer_peek_token (parser
->lexer
);
24918 /* If it's not a `)', then there is a type-id-list. */
24919 if (token
->type
!= CPP_CLOSE_PAREN
)
24921 /* Types may not be defined in an exception-specification. */
24922 saved_message
= parser
->type_definition_forbidden_message
;
24923 parser
->type_definition_forbidden_message
24924 = G_("types may not be defined in an exception-specification");
24925 /* Parse the type-id-list. */
24926 type_id_list
= cp_parser_type_id_list (parser
);
24927 /* Restore the saved message. */
24928 parser
->type_definition_forbidden_message
= saved_message
;
24930 if (cxx_dialect
>= cxx17
)
24932 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24934 type_id_list
= NULL_TREE
;
24936 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24937 warning_at (loc
, OPT_Wdeprecated
,
24938 "dynamic exception specifications are deprecated in "
24941 /* In C++17, throw() is equivalent to noexcept (true). throw()
24942 is deprecated in C++11 and above as well, but is still widely used,
24943 so don't warn about it yet. */
24944 else if (cxx_dialect
>= cxx17
)
24945 type_id_list
= noexcept_true_spec
;
24947 type_id_list
= empty_except_spec
;
24949 /* Look for the `)'. */
24950 parens
.require_close (parser
);
24952 return type_id_list
;
24955 /* Parse an (optional) type-id-list.
24959 type-id-list , type-id ... [opt]
24961 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24962 in the order that the types were presented. */
24965 cp_parser_type_id_list (cp_parser
* parser
)
24967 tree types
= NULL_TREE
;
24974 token
= cp_lexer_peek_token (parser
->lexer
);
24976 /* Get the next type-id. */
24977 type
= cp_parser_type_id (parser
);
24978 /* Check for invalid 'auto'. */
24979 if (flag_concepts
&& type_uses_auto (type
))
24981 error_at (token
->location
,
24982 "invalid use of %<auto%> in exception-specification");
24983 type
= error_mark_node
;
24985 /* Parse the optional ellipsis. */
24986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24988 /* Consume the `...'. */
24989 cp_lexer_consume_token (parser
->lexer
);
24991 /* Turn the type into a pack expansion expression. */
24992 type
= make_pack_expansion (type
);
24994 /* Add it to the list. */
24995 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24996 /* Peek at the next token. */
24997 token
= cp_lexer_peek_token (parser
->lexer
);
24998 /* If it is not a `,', we are done. */
24999 if (token
->type
!= CPP_COMMA
)
25001 /* Consume the `,'. */
25002 cp_lexer_consume_token (parser
->lexer
);
25005 return nreverse (types
);
25008 /* Parse a try-block.
25011 try compound-statement handler-seq */
25014 cp_parser_try_block (cp_parser
* parser
)
25018 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25019 if (parser
->in_function_body
25020 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
25021 error ("%<try%> in %<constexpr%> function");
25023 try_block
= begin_try_block ();
25024 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25025 finish_try_block (try_block
);
25026 cp_parser_handler_seq (parser
);
25027 finish_handler_sequence (try_block
);
25032 /* Parse a function-try-block.
25034 function-try-block:
25035 try ctor-initializer [opt] function-body handler-seq */
25038 cp_parser_function_try_block (cp_parser
* parser
)
25040 tree compound_stmt
;
25043 /* Look for the `try' keyword. */
25044 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25046 /* Let the rest of the front end know where we are. */
25047 try_block
= begin_function_try_block (&compound_stmt
);
25048 /* Parse the function-body. */
25049 cp_parser_ctor_initializer_opt_and_function_body
25050 (parser
, /*in_function_try_block=*/true);
25051 /* We're done with the `try' part. */
25052 finish_function_try_block (try_block
);
25053 /* Parse the handlers. */
25054 cp_parser_handler_seq (parser
);
25055 /* We're done with the handlers. */
25056 finish_function_handler_sequence (try_block
, compound_stmt
);
25059 /* Parse a handler-seq.
25062 handler handler-seq [opt] */
25065 cp_parser_handler_seq (cp_parser
* parser
)
25071 /* Parse the handler. */
25072 cp_parser_handler (parser
);
25073 /* Peek at the next token. */
25074 token
= cp_lexer_peek_token (parser
->lexer
);
25075 /* If it's not `catch' then there are no more handlers. */
25076 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25081 /* Parse a handler.
25084 catch ( exception-declaration ) compound-statement */
25087 cp_parser_handler (cp_parser
* parser
)
25092 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25093 handler
= begin_handler ();
25094 matching_parens parens
;
25095 parens
.require_open (parser
);
25096 declaration
= cp_parser_exception_declaration (parser
);
25097 finish_handler_parms (declaration
, handler
);
25098 parens
.require_close (parser
);
25099 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25100 finish_handler (handler
);
25103 /* Parse an exception-declaration.
25105 exception-declaration:
25106 type-specifier-seq declarator
25107 type-specifier-seq abstract-declarator
25111 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25112 ellipsis variant is used. */
25115 cp_parser_exception_declaration (cp_parser
* parser
)
25117 cp_decl_specifier_seq type_specifiers
;
25118 cp_declarator
*declarator
;
25119 const char *saved_message
;
25121 /* If it's an ellipsis, it's easy to handle. */
25122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25124 /* Consume the `...' token. */
25125 cp_lexer_consume_token (parser
->lexer
);
25129 /* Types may not be defined in exception-declarations. */
25130 saved_message
= parser
->type_definition_forbidden_message
;
25131 parser
->type_definition_forbidden_message
25132 = G_("types may not be defined in exception-declarations");
25134 /* Parse the type-specifier-seq. */
25135 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
25136 /*is_trailing_return=*/false,
25138 /* If it's a `)', then there is no declarator. */
25139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25142 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25143 /*ctor_dtor_or_conv_p=*/NULL
,
25144 /*parenthesized_p=*/NULL
,
25145 /*member_p=*/false,
25146 /*friend_p=*/false);
25148 /* Restore the saved message. */
25149 parser
->type_definition_forbidden_message
= saved_message
;
25151 if (!type_specifiers
.any_specifiers_p
)
25152 return error_mark_node
;
25154 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25157 /* Parse a throw-expression.
25160 throw assignment-expression [opt]
25162 Returns a THROW_EXPR representing the throw-expression. */
25165 cp_parser_throw_expression (cp_parser
* parser
)
25170 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25171 token
= cp_lexer_peek_token (parser
->lexer
);
25172 /* Figure out whether or not there is an assignment-expression
25173 following the "throw" keyword. */
25174 if (token
->type
== CPP_COMMA
25175 || token
->type
== CPP_SEMICOLON
25176 || token
->type
== CPP_CLOSE_PAREN
25177 || token
->type
== CPP_CLOSE_SQUARE
25178 || token
->type
== CPP_CLOSE_BRACE
25179 || token
->type
== CPP_COLON
)
25180 expression
= NULL_TREE
;
25182 expression
= cp_parser_assignment_expression (parser
);
25184 return build_throw (expression
);
25187 /* GNU Extensions */
25189 /* Parse an (optional) asm-specification.
25192 asm ( string-literal )
25194 If the asm-specification is present, returns a STRING_CST
25195 corresponding to the string-literal. Otherwise, returns
25199 cp_parser_asm_specification_opt (cp_parser
* parser
)
25202 tree asm_specification
;
25204 /* Peek at the next token. */
25205 token
= cp_lexer_peek_token (parser
->lexer
);
25206 /* If the next token isn't the `asm' keyword, then there's no
25207 asm-specification. */
25208 if (!cp_parser_is_keyword (token
, RID_ASM
))
25211 /* Consume the `asm' token. */
25212 cp_lexer_consume_token (parser
->lexer
);
25213 /* Look for the `('. */
25214 matching_parens parens
;
25215 parens
.require_open (parser
);
25217 /* Look for the string-literal. */
25218 asm_specification
= cp_parser_string_literal (parser
, false, false);
25220 /* Look for the `)'. */
25221 parens
.require_close (parser
);
25223 return asm_specification
;
25226 /* Parse an asm-operand-list.
25230 asm-operand-list , asm-operand
25233 string-literal ( expression )
25234 [ string-literal ] string-literal ( expression )
25236 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25237 each node is the expression. The TREE_PURPOSE is itself a
25238 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25239 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25240 is a STRING_CST for the string literal before the parenthesis. Returns
25241 ERROR_MARK_NODE if any of the operands are invalid. */
25244 cp_parser_asm_operand_list (cp_parser
* parser
)
25246 tree asm_operands
= NULL_TREE
;
25247 bool invalid_operands
= false;
25251 tree string_literal
;
25255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25257 /* Consume the `[' token. */
25258 cp_lexer_consume_token (parser
->lexer
);
25259 /* Read the operand name. */
25260 name
= cp_parser_identifier (parser
);
25261 if (name
!= error_mark_node
)
25262 name
= build_string (IDENTIFIER_LENGTH (name
),
25263 IDENTIFIER_POINTER (name
));
25264 /* Look for the closing `]'. */
25265 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25269 /* Look for the string-literal. */
25270 string_literal
= cp_parser_string_literal (parser
, false, false);
25272 /* Look for the `('. */
25273 matching_parens parens
;
25274 parens
.require_open (parser
);
25275 /* Parse the expression. */
25276 expression
= cp_parser_expression (parser
);
25277 /* Look for the `)'. */
25278 parens
.require_close (parser
);
25280 if (name
== error_mark_node
25281 || string_literal
== error_mark_node
25282 || expression
== error_mark_node
)
25283 invalid_operands
= true;
25285 /* Add this operand to the list. */
25286 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25289 /* If the next token is not a `,', there are no more
25291 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25293 /* Consume the `,'. */
25294 cp_lexer_consume_token (parser
->lexer
);
25297 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25300 /* Parse an asm-clobber-list.
25304 asm-clobber-list , string-literal
25306 Returns a TREE_LIST, indicating the clobbers in the order that they
25307 appeared. The TREE_VALUE of each node is a STRING_CST. */
25310 cp_parser_asm_clobber_list (cp_parser
* parser
)
25312 tree clobbers
= NULL_TREE
;
25316 tree string_literal
;
25318 /* Look for the string literal. */
25319 string_literal
= cp_parser_string_literal (parser
, false, false);
25320 /* Add it to the list. */
25321 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25322 /* If the next token is not a `,', then the list is
25324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25326 /* Consume the `,' token. */
25327 cp_lexer_consume_token (parser
->lexer
);
25333 /* Parse an asm-label-list.
25337 asm-label-list , identifier
25339 Returns a TREE_LIST, indicating the labels in the order that they
25340 appeared. The TREE_VALUE of each node is a label. */
25343 cp_parser_asm_label_list (cp_parser
* parser
)
25345 tree labels
= NULL_TREE
;
25349 tree identifier
, label
, name
;
25351 /* Look for the identifier. */
25352 identifier
= cp_parser_identifier (parser
);
25353 if (!error_operand_p (identifier
))
25355 label
= lookup_label (identifier
);
25356 if (TREE_CODE (label
) == LABEL_DECL
)
25358 TREE_USED (label
) = 1;
25359 check_goto (label
);
25360 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25361 IDENTIFIER_POINTER (identifier
));
25362 labels
= tree_cons (name
, label
, labels
);
25365 /* If the next token is not a `,', then the list is
25367 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25369 /* Consume the `,' token. */
25370 cp_lexer_consume_token (parser
->lexer
);
25373 return nreverse (labels
);
25376 /* Return TRUE iff the next tokens in the stream are possibly the
25377 beginning of a GNU extension attribute. */
25380 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25382 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25385 /* Return TRUE iff the next tokens in the stream are possibly the
25386 beginning of a standard C++-11 attribute specifier. */
25389 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25391 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25394 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25395 beginning of a standard C++-11 attribute specifier. */
25398 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25400 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25402 return (cxx_dialect
>= cxx11
25403 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25404 || (token
->type
== CPP_OPEN_SQUARE
25405 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25406 && token
->type
== CPP_OPEN_SQUARE
)));
25409 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25410 beginning of a GNU extension attribute. */
25413 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25415 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25417 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25420 /* Return true iff the next tokens can be the beginning of either a
25421 GNU attribute list, or a standard C++11 attribute sequence. */
25424 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25426 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25427 || cp_next_tokens_can_be_std_attribute_p (parser
));
25430 /* Return true iff the next Nth tokens can be the beginning of either
25431 a GNU attribute list, or a standard C++11 attribute sequence. */
25434 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25436 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25437 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25440 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25441 of GNU attributes, or return NULL. */
25444 cp_parser_attributes_opt (cp_parser
*parser
)
25446 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25447 return cp_parser_gnu_attributes_opt (parser
);
25448 return cp_parser_std_attribute_spec_seq (parser
);
25451 /* Parse an (optional) series of attributes.
25454 attributes attribute
25457 __attribute__ (( attribute-list [opt] ))
25459 The return value is as for cp_parser_gnu_attribute_list. */
25462 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25464 tree attributes
= NULL_TREE
;
25466 temp_override
<bool> cleanup
25467 (parser
->auto_is_implicit_function_template_parm_p
, false);
25472 tree attribute_list
;
25475 /* Peek at the next token. */
25476 token
= cp_lexer_peek_token (parser
->lexer
);
25477 /* If it's not `__attribute__', then we're done. */
25478 if (token
->keyword
!= RID_ATTRIBUTE
)
25481 /* Consume the `__attribute__' keyword. */
25482 cp_lexer_consume_token (parser
->lexer
);
25483 /* Look for the two `(' tokens. */
25484 matching_parens outer_parens
;
25485 outer_parens
.require_open (parser
);
25486 matching_parens inner_parens
;
25487 inner_parens
.require_open (parser
);
25489 /* Peek at the next token. */
25490 token
= cp_lexer_peek_token (parser
->lexer
);
25491 if (token
->type
!= CPP_CLOSE_PAREN
)
25492 /* Parse the attribute-list. */
25493 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25495 /* If the next token is a `)', then there is no attribute
25497 attribute_list
= NULL
;
25499 /* Look for the two `)' tokens. */
25500 if (!inner_parens
.require_close (parser
))
25502 if (!outer_parens
.require_close (parser
))
25505 cp_parser_skip_to_end_of_statement (parser
);
25507 /* Add these new attributes to the list. */
25508 attributes
= attr_chainon (attributes
, attribute_list
);
25514 /* Parse a GNU attribute-list.
25518 attribute-list , attribute
25522 identifier ( identifier )
25523 identifier ( identifier , expression-list )
25524 identifier ( expression-list )
25526 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25527 to an attribute. The TREE_PURPOSE of each node is the identifier
25528 indicating which attribute is in use. The TREE_VALUE represents
25529 the arguments, if any. */
25532 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25534 tree attribute_list
= NULL_TREE
;
25535 bool save_translate_strings_p
= parser
->translate_strings_p
;
25537 parser
->translate_strings_p
= false;
25544 /* Look for the identifier. We also allow keywords here; for
25545 example `__attribute__ ((const))' is legal. */
25546 token
= cp_lexer_peek_token (parser
->lexer
);
25547 if (token
->type
== CPP_NAME
25548 || token
->type
== CPP_KEYWORD
)
25550 tree arguments
= NULL_TREE
;
25552 /* Consume the token, but save it since we need it for the
25553 SIMD enabled function parsing. */
25554 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25556 /* Save away the identifier that indicates which attribute
25558 identifier
= (token
->type
== CPP_KEYWORD
)
25559 /* For keywords, use the canonical spelling, not the
25560 parsed identifier. */
25561 ? ridpointers
[(int) token
->keyword
]
25562 : id_token
->u
.value
;
25564 identifier
= canonicalize_attr_name (identifier
);
25565 attribute
= build_tree_list (identifier
, NULL_TREE
);
25567 /* Peek at the next token. */
25568 token
= cp_lexer_peek_token (parser
->lexer
);
25569 /* If it's an `(', then parse the attribute arguments. */
25570 if (token
->type
== CPP_OPEN_PAREN
)
25572 vec
<tree
, va_gc
> *vec
;
25573 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25574 ? id_attr
: normal_attr
);
25575 vec
= cp_parser_parenthesized_expression_list
25576 (parser
, attr_flag
, /*cast_p=*/false,
25577 /*allow_expansion_p=*/false,
25578 /*non_constant_p=*/NULL
);
25580 arguments
= error_mark_node
;
25583 arguments
= build_tree_list_vec (vec
);
25584 release_tree_vector (vec
);
25586 /* Save the arguments away. */
25587 TREE_VALUE (attribute
) = arguments
;
25590 if (arguments
!= error_mark_node
)
25592 /* Add this attribute to the list. */
25593 TREE_CHAIN (attribute
) = attribute_list
;
25594 attribute_list
= attribute
;
25597 token
= cp_lexer_peek_token (parser
->lexer
);
25599 /* Unless EXACTLY_ONE is set look for more attributes.
25600 If the next token isn't a `,', we're done. */
25601 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25604 /* Consume the comma and keep going. */
25605 cp_lexer_consume_token (parser
->lexer
);
25607 parser
->translate_strings_p
= save_translate_strings_p
;
25609 /* We built up the list in reverse order. */
25610 return nreverse (attribute_list
);
25613 /* Parse a standard C++11 attribute.
25615 The returned representation is a TREE_LIST which TREE_PURPOSE is
25616 the scoped name of the attribute, and the TREE_VALUE is its
25619 Note that the scoped name of the attribute is itself a TREE_LIST
25620 which TREE_PURPOSE is the namespace of the attribute, and
25621 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25622 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25623 and which TREE_PURPOSE is directly the attribute name.
25625 Clients of the attribute code should use get_attribute_namespace
25626 and get_attribute_name to get the actual namespace and name of
25627 attributes, regardless of their being GNU or C++11 attributes.
25630 attribute-token attribute-argument-clause [opt]
25634 attribute-scoped-token
25636 attribute-scoped-token:
25637 attribute-namespace :: identifier
25639 attribute-namespace:
25642 attribute-argument-clause:
25643 ( balanced-token-seq )
25645 balanced-token-seq:
25646 balanced-token [opt]
25647 balanced-token-seq balanced-token
25650 ( balanced-token-seq )
25651 [ balanced-token-seq ]
25652 { balanced-token-seq }. */
25655 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25657 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25660 temp_override
<bool> cleanup
25661 (parser
->auto_is_implicit_function_template_parm_p
, false);
25663 /* First, parse name of the attribute, a.k.a attribute-token. */
25665 token
= cp_lexer_peek_token (parser
->lexer
);
25666 if (token
->type
== CPP_NAME
)
25667 attr_id
= token
->u
.value
;
25668 else if (token
->type
== CPP_KEYWORD
)
25669 attr_id
= ridpointers
[(int) token
->keyword
];
25670 else if (token
->flags
& NAMED_OP
)
25671 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25673 if (attr_id
== NULL_TREE
)
25676 cp_lexer_consume_token (parser
->lexer
);
25678 token
= cp_lexer_peek_token (parser
->lexer
);
25679 if (token
->type
== CPP_SCOPE
)
25681 /* We are seeing a scoped attribute token. */
25683 cp_lexer_consume_token (parser
->lexer
);
25685 error_at (token
->location
, "attribute using prefix used together "
25686 "with scoped attribute token");
25689 token
= cp_lexer_consume_token (parser
->lexer
);
25690 if (token
->type
== CPP_NAME
)
25691 attr_id
= token
->u
.value
;
25692 else if (token
->type
== CPP_KEYWORD
)
25693 attr_id
= ridpointers
[(int) token
->keyword
];
25694 else if (token
->flags
& NAMED_OP
)
25695 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25698 error_at (token
->location
,
25699 "expected an identifier for the attribute name");
25700 return error_mark_node
;
25703 attr_ns
= canonicalize_attr_name (attr_ns
);
25704 attr_id
= canonicalize_attr_name (attr_id
);
25705 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25707 token
= cp_lexer_peek_token (parser
->lexer
);
25711 attr_ns
= canonicalize_attr_name (attr_ns
);
25712 attr_id
= canonicalize_attr_name (attr_id
);
25713 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25718 attr_id
= canonicalize_attr_name (attr_id
);
25719 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25721 /* C++11 noreturn attribute is equivalent to GNU's. */
25722 if (is_attribute_p ("noreturn", attr_id
))
25723 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25724 /* C++14 deprecated attribute is equivalent to GNU's. */
25725 else if (is_attribute_p ("deprecated", attr_id
))
25726 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25727 /* C++17 fallthrough attribute is equivalent to GNU's. */
25728 else if (is_attribute_p ("fallthrough", attr_id
))
25729 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25730 /* Transactional Memory TS optimize_for_synchronized attribute is
25731 equivalent to GNU transaction_callable. */
25732 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25733 TREE_PURPOSE (attribute
)
25734 = get_identifier ("transaction_callable");
25735 /* Transactional Memory attributes are GNU attributes. */
25736 else if (tm_attr_to_mask (attr_id
))
25737 TREE_PURPOSE (attribute
) = attr_id
;
25740 /* Now parse the optional argument clause of the attribute. */
25742 if (token
->type
!= CPP_OPEN_PAREN
)
25746 vec
<tree
, va_gc
> *vec
;
25747 int attr_flag
= normal_attr
;
25749 if (attr_ns
== gnu_identifier
25750 && attribute_takes_identifier_p (attr_id
))
25751 /* A GNU attribute that takes an identifier in parameter. */
25752 attr_flag
= id_attr
;
25754 vec
= cp_parser_parenthesized_expression_list
25755 (parser
, attr_flag
, /*cast_p=*/false,
25756 /*allow_expansion_p=*/true,
25757 /*non_constant_p=*/NULL
);
25759 arguments
= error_mark_node
;
25762 arguments
= build_tree_list_vec (vec
);
25763 release_tree_vector (vec
);
25766 if (arguments
== error_mark_node
)
25767 attribute
= error_mark_node
;
25769 TREE_VALUE (attribute
) = arguments
;
25775 /* Check that the attribute ATTRIBUTE appears at most once in the
25776 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25777 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25778 isn't implemented yet in GCC. */
25781 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25785 tree name
= get_attribute_name (attribute
);
25786 if (is_attribute_p ("noreturn", name
)
25787 && lookup_attribute ("noreturn", attributes
))
25788 error ("attribute %<noreturn%> can appear at most once "
25789 "in an attribute-list");
25790 else if (is_attribute_p ("deprecated", name
)
25791 && lookup_attribute ("deprecated", attributes
))
25792 error ("attribute %<deprecated%> can appear at most once "
25793 "in an attribute-list");
25797 /* Parse a list of standard C++-11 attributes.
25801 attribute-list , attribute[opt]
25803 attribute-list , attribute ...
25807 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25809 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25810 cp_token
*token
= NULL
;
25814 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25815 if (attribute
== error_mark_node
)
25817 if (attribute
!= NULL_TREE
)
25819 cp_parser_check_std_attribute (attributes
, attribute
);
25820 TREE_CHAIN (attribute
) = attributes
;
25821 attributes
= attribute
;
25823 token
= cp_lexer_peek_token (parser
->lexer
);
25824 if (token
->type
== CPP_ELLIPSIS
)
25826 cp_lexer_consume_token (parser
->lexer
);
25827 if (attribute
== NULL_TREE
)
25828 error_at (token
->location
,
25829 "expected attribute before %<...%>");
25832 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25833 if (pack
== error_mark_node
)
25834 return error_mark_node
;
25835 TREE_VALUE (attribute
) = pack
;
25837 token
= cp_lexer_peek_token (parser
->lexer
);
25839 if (token
->type
!= CPP_COMMA
)
25841 cp_lexer_consume_token (parser
->lexer
);
25843 attributes
= nreverse (attributes
);
25847 /* Parse a standard C++-11 attribute specifier.
25849 attribute-specifier:
25850 [ [ attribute-using-prefix [opt] attribute-list ] ]
25851 alignment-specifier
25853 attribute-using-prefix:
25854 using attribute-namespace :
25856 alignment-specifier:
25857 alignas ( type-id ... [opt] )
25858 alignas ( alignment-expression ... [opt] ). */
25861 cp_parser_std_attribute_spec (cp_parser
*parser
)
25863 tree attributes
= NULL_TREE
;
25864 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25866 if (token
->type
== CPP_OPEN_SQUARE
25867 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25869 tree attr_ns
= NULL_TREE
;
25871 cp_lexer_consume_token (parser
->lexer
);
25872 cp_lexer_consume_token (parser
->lexer
);
25874 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25876 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25877 if (token
->type
== CPP_NAME
)
25878 attr_ns
= token
->u
.value
;
25879 else if (token
->type
== CPP_KEYWORD
)
25880 attr_ns
= ridpointers
[(int) token
->keyword
];
25881 else if (token
->flags
& NAMED_OP
)
25882 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25885 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25887 if (cxx_dialect
< cxx17
25888 && !in_system_header_at (input_location
))
25889 pedwarn (input_location
, 0,
25890 "attribute using prefix only available "
25891 "with -std=c++17 or -std=gnu++17");
25893 cp_lexer_consume_token (parser
->lexer
);
25894 cp_lexer_consume_token (parser
->lexer
);
25895 cp_lexer_consume_token (parser
->lexer
);
25898 attr_ns
= NULL_TREE
;
25901 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25903 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25904 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25905 cp_parser_skip_to_end_of_statement (parser
);
25907 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25908 when we are sure that we have actually parsed them. */
25909 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25915 /* Look for an alignment-specifier. */
25917 token
= cp_lexer_peek_token (parser
->lexer
);
25919 if (token
->type
!= CPP_KEYWORD
25920 || token
->keyword
!= RID_ALIGNAS
)
25923 cp_lexer_consume_token (parser
->lexer
);
25924 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25926 matching_parens parens
;
25927 if (!parens
.require_open (parser
))
25928 return error_mark_node
;
25930 cp_parser_parse_tentatively (parser
);
25931 alignas_expr
= cp_parser_type_id (parser
);
25933 if (!cp_parser_parse_definitely (parser
))
25935 alignas_expr
= cp_parser_assignment_expression (parser
);
25936 if (alignas_expr
== error_mark_node
)
25937 cp_parser_skip_to_end_of_statement (parser
);
25938 if (alignas_expr
== NULL_TREE
25939 || alignas_expr
== error_mark_node
)
25940 return alignas_expr
;
25943 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25944 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25946 /* Handle alignas (pack...). */
25947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25949 cp_lexer_consume_token (parser
->lexer
);
25950 alignas_expr
= make_pack_expansion (alignas_expr
);
25953 /* Something went wrong, so don't build the attribute. */
25954 if (alignas_expr
== error_mark_node
)
25955 return error_mark_node
;
25957 if (!parens
.require_close (parser
))
25958 return error_mark_node
;
25960 /* Build the C++-11 representation of an 'aligned'
25963 = build_tree_list (build_tree_list (gnu_identifier
,
25964 aligned_identifier
), alignas_expr
);
25970 /* Parse a standard C++-11 attribute-specifier-seq.
25972 attribute-specifier-seq:
25973 attribute-specifier-seq [opt] attribute-specifier
25977 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25979 tree attr_specs
= NULL_TREE
;
25980 tree attr_last
= NULL_TREE
;
25984 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25985 if (attr_spec
== NULL_TREE
)
25987 if (attr_spec
== error_mark_node
)
25988 return error_mark_node
;
25991 TREE_CHAIN (attr_last
) = attr_spec
;
25993 attr_specs
= attr_last
= attr_spec
;
25994 attr_last
= tree_last (attr_last
);
26000 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26001 return index of the first token after balanced-token, or N on failure. */
26004 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26007 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26009 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26012 case CPP_PRAGMA_EOL
:
26013 /* Ran out of tokens. */
26015 case CPP_OPEN_PAREN
:
26018 case CPP_OPEN_BRACE
:
26021 case CPP_OPEN_SQUARE
:
26024 case CPP_CLOSE_PAREN
:
26027 case CPP_CLOSE_BRACE
:
26030 case CPP_CLOSE_SQUARE
:
26036 while (nparens
|| nbraces
|| nsquares
);
26040 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26041 return index of the first token after the GNU attribute tokens, or N on
26045 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26049 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26050 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26051 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26054 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26057 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26064 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26065 next token), return index of the first token after the standard C++11
26066 attribute tokens, or N on failure. */
26069 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26073 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26074 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26076 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26079 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26083 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26084 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26086 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26097 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26098 as the next token), return index of the first token after the attribute
26099 tokens, or N on failure. */
26102 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26104 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26105 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26106 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26109 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26110 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26111 current value of the PEDANTIC flag, regardless of whether or not
26112 the `__extension__' keyword is present. The caller is responsible
26113 for restoring the value of the PEDANTIC flag. */
26116 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26118 /* Save the old value of the PEDANTIC flag. */
26119 *saved_pedantic
= pedantic
;
26121 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26123 /* Consume the `__extension__' token. */
26124 cp_lexer_consume_token (parser
->lexer
);
26125 /* We're not being pedantic while the `__extension__' keyword is
26135 /* Parse a label declaration.
26138 __label__ label-declarator-seq ;
26140 label-declarator-seq:
26141 identifier , label-declarator-seq
26145 cp_parser_label_declaration (cp_parser
* parser
)
26147 /* Look for the `__label__' keyword. */
26148 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26154 /* Look for an identifier. */
26155 identifier
= cp_parser_identifier (parser
);
26156 /* If we failed, stop. */
26157 if (identifier
== error_mark_node
)
26159 /* Declare it as a label. */
26160 finish_label_decl (identifier
);
26161 /* If the next token is a `;', stop. */
26162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26164 /* Look for the `,' separating the label declarations. */
26165 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26168 /* Look for the final `;'. */
26169 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26172 // -------------------------------------------------------------------------- //
26175 // Parse a requires clause.
26177 // requires-clause:
26178 // 'requires' logical-or-expression
26180 // The required logical-or-expression must be a constant expression. Note
26181 // that we don't check that the expression is constepxr here. We defer until
26182 // we analyze constraints and then, we only check atomic constraints.
26184 cp_parser_requires_clause (cp_parser
*parser
)
26186 // Parse the requires clause so that it is not automatically folded.
26187 ++processing_template_decl
;
26188 tree expr
= cp_parser_binary_expression (parser
, false, false,
26189 PREC_NOT_OPERATOR
, NULL
);
26190 if (check_for_bare_parameter_packs (expr
))
26191 expr
= error_mark_node
;
26192 --processing_template_decl
;
26196 // Optionally parse a requires clause:
26198 cp_parser_requires_clause_opt (cp_parser
*parser
)
26200 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26201 if (tok
->keyword
!= RID_REQUIRES
)
26203 if (!flag_concepts
&& tok
->type
== CPP_NAME
26204 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26206 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26207 "%<requires%> only available with -fconcepts");
26208 /* Parse and discard the requires-clause. */
26209 cp_lexer_consume_token (parser
->lexer
);
26210 cp_parser_requires_clause (parser
);
26214 cp_lexer_consume_token (parser
->lexer
);
26215 return cp_parser_requires_clause (parser
);
26219 /*---------------------------------------------------------------------------
26220 Requires expressions
26221 ---------------------------------------------------------------------------*/
26223 /* Parse a requires expression
26225 requirement-expression:
26226 'requires' requirement-parameter-list [opt] requirement-body */
26228 cp_parser_requires_expression (cp_parser
*parser
)
26230 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26231 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26233 /* A requires-expression shall appear only within a concept
26234 definition or a requires-clause.
26236 TODO: Implement this diagnostic correctly. */
26237 if (!processing_template_decl
)
26239 error_at (loc
, "a requires expression cannot appear outside a template");
26240 cp_parser_skip_to_end_of_statement (parser
);
26241 return error_mark_node
;
26246 /* Local parameters are delared as variables within the scope
26247 of the expression. They are not visible past the end of
26248 the expression. Expressions within the requires-expression
26249 are unevaluated. */
26250 struct scope_sentinel
26254 ++cp_unevaluated_operand
;
26255 begin_scope (sk_block
, NULL_TREE
);
26260 pop_bindings_and_leave_scope ();
26261 --cp_unevaluated_operand
;
26265 /* Parse the optional parameter list. */
26266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26268 parms
= cp_parser_requirement_parameter_list (parser
);
26269 if (parms
== error_mark_node
)
26270 return error_mark_node
;
26275 /* Parse the requirement body. */
26276 reqs
= cp_parser_requirement_body (parser
);
26277 if (reqs
== error_mark_node
)
26278 return error_mark_node
;
26281 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26283 grokparms (parms
, &parms
);
26284 return finish_requires_expr (parms
, reqs
);
26287 /* Parse a parameterized requirement.
26289 requirement-parameter-list:
26290 '(' parameter-declaration-clause ')' */
26292 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26294 matching_parens parens
;
26295 if (!parens
.require_open (parser
))
26296 return error_mark_node
;
26298 tree parms
= cp_parser_parameter_declaration_clause (parser
);
26300 if (!parens
.require_close (parser
))
26301 return error_mark_node
;
26306 /* Parse the body of a requirement.
26309 '{' requirement-list '}' */
26311 cp_parser_requirement_body (cp_parser
*parser
)
26313 matching_braces braces
;
26314 if (!braces
.require_open (parser
))
26315 return error_mark_node
;
26317 tree reqs
= cp_parser_requirement_list (parser
);
26319 if (!braces
.require_close (parser
))
26320 return error_mark_node
;
26325 /* Parse a list of requirements.
26329 requirement-list ';' requirement[opt] */
26331 cp_parser_requirement_list (cp_parser
*parser
)
26333 tree result
= NULL_TREE
;
26336 tree req
= cp_parser_requirement (parser
);
26337 if (req
== error_mark_node
)
26338 return error_mark_node
;
26340 result
= tree_cons (NULL_TREE
, req
, result
);
26342 /* If we see a semi-colon, consume it. */
26343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26344 cp_lexer_consume_token (parser
->lexer
);
26346 /* Stop processing at the end of the list. */
26347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26351 /* Reverse the order of requirements so they are analyzed in
26352 declaration order. */
26353 return nreverse (result
);
26356 /* Parse a syntactic requirement or type requirement.
26360 compound-requirement
26362 nested-requirement */
26364 cp_parser_requirement (cp_parser
*parser
)
26366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26367 return cp_parser_compound_requirement (parser
);
26368 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26369 return cp_parser_type_requirement (parser
);
26370 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26371 return cp_parser_nested_requirement (parser
);
26373 return cp_parser_simple_requirement (parser
);
26376 /* Parse a simple requirement.
26378 simple-requirement:
26381 cp_parser_simple_requirement (cp_parser
*parser
)
26383 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26384 if (!expr
|| expr
== error_mark_node
)
26385 return error_mark_node
;
26387 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26388 return error_mark_node
;
26390 return finish_simple_requirement (expr
);
26393 /* Parse a type requirement
26396 nested-name-specifier [opt] required-type-name ';'
26398 required-type-name:
26400 'template' [opt] simple-template-id */
26402 cp_parser_type_requirement (cp_parser
*parser
)
26404 cp_lexer_consume_token (parser
->lexer
);
26406 // Save the scope before parsing name specifiers.
26407 tree saved_scope
= parser
->scope
;
26408 tree saved_object_scope
= parser
->object_scope
;
26409 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26410 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26411 cp_parser_nested_name_specifier_opt (parser
,
26412 /*typename_keyword_p=*/true,
26413 /*check_dependency_p=*/false,
26415 /*is_declaration=*/false);
26418 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26420 cp_lexer_consume_token (parser
->lexer
);
26421 type
= cp_parser_template_id (parser
,
26422 /*template_keyword_p=*/true,
26423 /*check_dependency=*/false,
26424 /*tag_type=*/none_type
,
26425 /*is_declaration=*/false);
26426 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26427 /*complain=*/tf_error
);
26430 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26432 if (TREE_CODE (type
) == TYPE_DECL
)
26433 type
= TREE_TYPE (type
);
26435 parser
->scope
= saved_scope
;
26436 parser
->object_scope
= saved_object_scope
;
26437 parser
->qualifying_scope
= saved_qualifying_scope
;
26439 if (type
== error_mark_node
)
26440 cp_parser_skip_to_end_of_statement (parser
);
26442 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26443 return error_mark_node
;
26444 if (type
== error_mark_node
)
26445 return error_mark_node
;
26447 return finish_type_requirement (type
);
26450 /* Parse a compound requirement
26452 compound-requirement:
26453 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26455 cp_parser_compound_requirement (cp_parser
*parser
)
26457 /* Parse an expression enclosed in '{ }'s. */
26458 matching_braces braces
;
26459 if (!braces
.require_open (parser
))
26460 return error_mark_node
;
26462 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26463 if (!expr
|| expr
== error_mark_node
)
26464 return error_mark_node
;
26466 if (!braces
.require_close (parser
))
26467 return error_mark_node
;
26469 /* Parse the optional noexcept. */
26470 bool noexcept_p
= false;
26471 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26473 cp_lexer_consume_token (parser
->lexer
);
26477 /* Parse the optional trailing return type. */
26478 tree type
= NULL_TREE
;
26479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26481 cp_lexer_consume_token (parser
->lexer
);
26482 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26483 parser
->in_result_type_constraint_p
= true;
26484 type
= cp_parser_trailing_type_id (parser
);
26485 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26486 if (type
== error_mark_node
)
26487 return error_mark_node
;
26490 return finish_compound_requirement (expr
, type
, noexcept_p
);
26493 /* Parse a nested requirement. This is the same as a requires clause.
26495 nested-requirement:
26498 cp_parser_nested_requirement (cp_parser
*parser
)
26500 cp_lexer_consume_token (parser
->lexer
);
26501 tree req
= cp_parser_requires_clause (parser
);
26502 if (req
== error_mark_node
)
26503 return error_mark_node
;
26504 return finish_nested_requirement (req
);
26507 /* Support Functions */
26509 /* Return the appropriate prefer_type argument for lookup_name_real based on
26510 tag_type and template_mem_access. */
26513 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26515 /* DR 141: When looking in the current enclosing context for a template-name
26516 after -> or ., only consider class templates. */
26517 if (template_mem_access
)
26521 case none_type
: return 0; // No preference.
26522 case scope_type
: return 1; // Type or namespace.
26523 default: return 2; // Type only.
26527 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26528 NAME should have one of the representations used for an
26529 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26530 is returned. If PARSER->SCOPE is a dependent type, then a
26531 SCOPE_REF is returned.
26533 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26534 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26535 was formed. Abstractly, such entities should not be passed to this
26536 function, because they do not need to be looked up, but it is
26537 simpler to check for this special case here, rather than at the
26540 In cases not explicitly covered above, this function returns a
26541 DECL, OVERLOAD, or baselink representing the result of the lookup.
26542 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26545 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26546 (e.g., "struct") that was used. In that case bindings that do not
26547 refer to types are ignored.
26549 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26552 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26555 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26558 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26559 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26560 NULL_TREE otherwise. */
26563 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26564 enum tag_types tag_type
,
26567 bool check_dependency
,
26568 tree
*ambiguous_decls
,
26569 location_t name_location
)
26572 tree object_type
= parser
->context
->object_type
;
26574 /* Assume that the lookup will be unambiguous. */
26575 if (ambiguous_decls
)
26576 *ambiguous_decls
= NULL_TREE
;
26578 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26579 no longer valid. Note that if we are parsing tentatively, and
26580 the parse fails, OBJECT_TYPE will be automatically restored. */
26581 parser
->context
->object_type
= NULL_TREE
;
26583 if (name
== error_mark_node
)
26584 return error_mark_node
;
26586 /* A template-id has already been resolved; there is no lookup to
26588 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26590 if (BASELINK_P (name
))
26592 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26593 == TEMPLATE_ID_EXPR
);
26597 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26598 it should already have been checked to make sure that the name
26599 used matches the type being destroyed. */
26600 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26604 /* Figure out to which type this destructor applies. */
26606 type
= parser
->scope
;
26607 else if (object_type
)
26608 type
= object_type
;
26610 type
= current_class_type
;
26611 /* If that's not a class type, there is no destructor. */
26612 if (!type
|| !CLASS_TYPE_P (type
))
26613 return error_mark_node
;
26615 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26616 lazily_declare_fn (sfk_destructor
, type
);
26618 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26621 return error_mark_node
;
26624 /* By this point, the NAME should be an ordinary identifier. If
26625 the id-expression was a qualified name, the qualifying scope is
26626 stored in PARSER->SCOPE at this point. */
26627 gcc_assert (identifier_p (name
));
26629 /* Perform the lookup. */
26634 if (parser
->scope
== error_mark_node
)
26635 return error_mark_node
;
26637 /* If the SCOPE is dependent, the lookup must be deferred until
26638 the template is instantiated -- unless we are explicitly
26639 looking up names in uninstantiated templates. Even then, we
26640 cannot look up the name if the scope is not a class type; it
26641 might, for example, be a template type parameter. */
26642 dependent_p
= (TYPE_P (parser
->scope
)
26643 && dependent_scope_p (parser
->scope
));
26644 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26646 /* Defer lookup. */
26647 decl
= error_mark_node
;
26650 tree pushed_scope
= NULL_TREE
;
26652 /* If PARSER->SCOPE is a dependent type, then it must be a
26653 class type, and we must not be checking dependencies;
26654 otherwise, we would have processed this lookup above. So
26655 that PARSER->SCOPE is not considered a dependent base by
26656 lookup_member, we must enter the scope here. */
26658 pushed_scope
= push_scope (parser
->scope
);
26660 /* If the PARSER->SCOPE is a template specialization, it
26661 may be instantiated during name lookup. In that case,
26662 errors may be issued. Even if we rollback the current
26663 tentative parse, those errors are valid. */
26664 decl
= lookup_qualified_name (parser
->scope
, name
,
26665 prefer_type_arg (tag_type
),
26666 /*complain=*/true);
26668 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26669 lookup result and the nested-name-specifier nominates a class C:
26670 * if the name specified after the nested-name-specifier, when
26671 looked up in C, is the injected-class-name of C (Clause 9), or
26672 * if the name specified after the nested-name-specifier is the
26673 same as the identifier or the simple-template-id's template-
26674 name in the last component of the nested-name-specifier,
26675 the name is instead considered to name the constructor of
26676 class C. [ Note: for example, the constructor is not an
26677 acceptable lookup result in an elaborated-type-specifier so
26678 the constructor would not be used in place of the
26679 injected-class-name. --end note ] Such a constructor name
26680 shall be used only in the declarator-id of a declaration that
26681 names a constructor or in a using-declaration. */
26682 if (tag_type
== none_type
26683 && DECL_SELF_REFERENCE_P (decl
)
26684 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26685 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26686 prefer_type_arg (tag_type
),
26687 /*complain=*/true);
26689 /* If we have a single function from a using decl, pull it out. */
26690 if (TREE_CODE (decl
) == OVERLOAD
26691 && !really_overloaded_fn (decl
))
26692 decl
= OVL_FUNCTION (decl
);
26695 pop_scope (pushed_scope
);
26698 /* If the scope is a dependent type and either we deferred lookup or
26699 we did lookup but didn't find the name, rememeber the name. */
26700 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26701 && dependent_type_p (parser
->scope
))
26707 /* The resolution to Core Issue 180 says that `struct
26708 A::B' should be considered a type-name, even if `A'
26710 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26711 /*complain=*/tf_error
);
26712 if (type
!= error_mark_node
)
26713 decl
= TYPE_NAME (type
);
26715 else if (is_template
26716 && (cp_parser_next_token_ends_template_argument_p (parser
)
26717 || cp_lexer_next_token_is (parser
->lexer
,
26719 decl
= make_unbound_class_template (parser
->scope
,
26721 /*complain=*/tf_error
);
26723 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26724 parser
->scope
, name
,
26727 parser
->qualifying_scope
= parser
->scope
;
26728 parser
->object_scope
= NULL_TREE
;
26730 else if (object_type
)
26732 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26733 OBJECT_TYPE is not a class. */
26734 if (CLASS_TYPE_P (object_type
))
26735 /* If the OBJECT_TYPE is a template specialization, it may
26736 be instantiated during name lookup. In that case, errors
26737 may be issued. Even if we rollback the current tentative
26738 parse, those errors are valid. */
26739 decl
= lookup_member (object_type
,
26742 prefer_type_arg (tag_type
),
26743 tf_warning_or_error
);
26748 /* Look it up in the enclosing context. DR 141: When looking for a
26749 template-name after -> or ., only consider class templates. */
26750 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26752 /*block_p=*/true, is_namespace
, 0);
26753 if (object_type
== unknown_type_node
)
26754 /* The object is type-dependent, so we can't look anything up; we used
26755 this to get the DR 141 behavior. */
26756 object_type
= NULL_TREE
;
26757 parser
->object_scope
= object_type
;
26758 parser
->qualifying_scope
= NULL_TREE
;
26762 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26764 /*block_p=*/true, is_namespace
, 0);
26765 parser
->qualifying_scope
= NULL_TREE
;
26766 parser
->object_scope
= NULL_TREE
;
26769 /* If the lookup failed, let our caller know. */
26770 if (!decl
|| decl
== error_mark_node
)
26771 return error_mark_node
;
26773 /* Pull out the template from an injected-class-name (or multiple). */
26775 decl
= maybe_get_template_decl_from_type_decl (decl
);
26777 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26778 if (TREE_CODE (decl
) == TREE_LIST
)
26780 if (ambiguous_decls
)
26781 *ambiguous_decls
= decl
;
26782 /* The error message we have to print is too complicated for
26783 cp_parser_error, so we incorporate its actions directly. */
26784 if (!cp_parser_simulate_error (parser
))
26786 error_at (name_location
, "reference to %qD is ambiguous",
26788 print_candidates (decl
);
26790 return error_mark_node
;
26793 gcc_assert (DECL_P (decl
)
26794 || TREE_CODE (decl
) == OVERLOAD
26795 || TREE_CODE (decl
) == SCOPE_REF
26796 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26797 || BASELINK_P (decl
));
26799 /* If we have resolved the name of a member declaration, check to
26800 see if the declaration is accessible. When the name resolves to
26801 set of overloaded functions, accessibility is checked when
26802 overload resolution is done.
26804 During an explicit instantiation, access is not checked at all,
26805 as per [temp.explicit]. */
26807 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26809 maybe_record_typedef_use (decl
);
26811 return cp_expr (decl
, name_location
);
26814 /* Like cp_parser_lookup_name, but for use in the typical case where
26815 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26816 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26819 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26821 return cp_parser_lookup_name (parser
, name
,
26823 /*is_template=*/false,
26824 /*is_namespace=*/false,
26825 /*check_dependency=*/true,
26826 /*ambiguous_decls=*/NULL
,
26830 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26831 the current context, return the TYPE_DECL. If TAG_NAME_P is
26832 true, the DECL indicates the class being defined in a class-head,
26833 or declared in an elaborated-type-specifier.
26835 Otherwise, return DECL. */
26838 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26840 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26841 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26844 template <typename T> struct B;
26847 template <typename T> struct A::B {};
26849 Similarly, in an elaborated-type-specifier:
26851 namespace N { struct X{}; }
26854 template <typename T> friend struct N::X;
26857 However, if the DECL refers to a class type, and we are in
26858 the scope of the class, then the name lookup automatically
26859 finds the TYPE_DECL created by build_self_reference rather
26860 than a TEMPLATE_DECL. For example, in:
26862 template <class T> struct S {
26866 there is no need to handle such case. */
26868 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26869 return DECL_TEMPLATE_RESULT (decl
);
26874 /* If too many, or too few, template-parameter lists apply to the
26875 declarator, issue an error message. Returns TRUE if all went well,
26876 and FALSE otherwise. */
26879 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26880 cp_declarator
*declarator
,
26881 location_t declarator_location
)
26883 switch (declarator
->kind
)
26887 unsigned num_templates
= 0;
26888 tree scope
= declarator
->u
.id
.qualifying_scope
;
26889 bool template_id_p
= false;
26892 num_templates
= num_template_headers_for_class (scope
);
26893 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26894 == TEMPLATE_ID_EXPR
)
26896 /* If the DECLARATOR has the form `X<y>' then it uses one
26897 additional level of template parameters. */
26899 template_id_p
= true;
26902 return cp_parser_check_template_parameters
26903 (parser
, num_templates
, template_id_p
, declarator_location
,
26910 case cdk_reference
:
26912 return (cp_parser_check_declarator_template_parameters
26913 (parser
, declarator
->declarator
, declarator_location
));
26920 gcc_unreachable ();
26925 /* NUM_TEMPLATES were used in the current declaration. If that is
26926 invalid, return FALSE and issue an error messages. Otherwise,
26927 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26928 declarator and we can print more accurate diagnostics. */
26931 cp_parser_check_template_parameters (cp_parser
* parser
,
26932 unsigned num_templates
,
26933 bool template_id_p
,
26934 location_t location
,
26935 cp_declarator
*declarator
)
26937 /* If there are the same number of template classes and parameter
26938 lists, that's OK. */
26939 if (parser
->num_template_parameter_lists
== num_templates
)
26941 /* If there are more, but only one more, and the name ends in an identifier,
26942 then we are declaring a primary template. That's OK too. */
26944 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26946 /* If there are more template classes than parameter lists, we have
26949 template <class T> void S<T>::R<T>::f (); */
26950 if (parser
->num_template_parameter_lists
< num_templates
)
26952 if (declarator
&& !current_function_decl
)
26953 error_at (location
, "specializing member %<%T::%E%> "
26954 "requires %<template<>%> syntax",
26955 declarator
->u
.id
.qualifying_scope
,
26956 declarator
->u
.id
.unqualified_name
);
26957 else if (declarator
)
26958 error_at (location
, "invalid declaration of %<%T::%E%>",
26959 declarator
->u
.id
.qualifying_scope
,
26960 declarator
->u
.id
.unqualified_name
);
26962 error_at (location
, "too few template-parameter-lists");
26965 /* Otherwise, there are too many template parameter lists. We have
26968 template <class T> template <class U> void S::f(); */
26969 error_at (location
, "too many template-parameter-lists");
26973 /* Parse an optional `::' token indicating that the following name is
26974 from the global namespace. If so, PARSER->SCOPE is set to the
26975 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26976 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26977 Returns the new value of PARSER->SCOPE, if the `::' token is
26978 present, and NULL_TREE otherwise. */
26981 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26985 /* Peek at the next token. */
26986 token
= cp_lexer_peek_token (parser
->lexer
);
26987 /* If we're looking at a `::' token then we're starting from the
26988 global namespace, not our current location. */
26989 if (token
->type
== CPP_SCOPE
)
26991 /* Consume the `::' token. */
26992 cp_lexer_consume_token (parser
->lexer
);
26993 /* Set the SCOPE so that we know where to start the lookup. */
26994 parser
->scope
= global_namespace
;
26995 parser
->qualifying_scope
= global_namespace
;
26996 parser
->object_scope
= NULL_TREE
;
26998 return parser
->scope
;
27000 else if (!current_scope_valid_p
)
27002 parser
->scope
= NULL_TREE
;
27003 parser
->qualifying_scope
= NULL_TREE
;
27004 parser
->object_scope
= NULL_TREE
;
27010 /* Returns TRUE if the upcoming token sequence is the start of a
27011 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27012 declarator is preceded by the `friend' specifier. */
27015 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
27017 bool constructor_p
;
27018 bool outside_class_specifier_p
;
27019 tree nested_name_specifier
;
27020 cp_token
*next_token
;
27022 /* The common case is that this is not a constructor declarator, so
27023 try to avoid doing lots of work if at all possible. It's not
27024 valid declare a constructor at function scope. */
27025 if (parser
->in_function_body
)
27027 /* And only certain tokens can begin a constructor declarator. */
27028 next_token
= cp_lexer_peek_token (parser
->lexer
);
27029 if (next_token
->type
!= CPP_NAME
27030 && next_token
->type
!= CPP_SCOPE
27031 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27032 && next_token
->type
!= CPP_TEMPLATE_ID
)
27035 /* Parse tentatively; we are going to roll back all of the tokens
27037 cp_parser_parse_tentatively (parser
);
27038 /* Assume that we are looking at a constructor declarator. */
27039 constructor_p
= true;
27041 /* Look for the optional `::' operator. */
27042 cp_parser_global_scope_opt (parser
,
27043 /*current_scope_valid_p=*/false);
27044 /* Look for the nested-name-specifier. */
27045 nested_name_specifier
27046 = (cp_parser_nested_name_specifier_opt (parser
,
27047 /*typename_keyword_p=*/false,
27048 /*check_dependency_p=*/false,
27050 /*is_declaration=*/false));
27052 outside_class_specifier_p
= (!at_class_scope_p ()
27053 || !TYPE_BEING_DEFINED (current_class_type
)
27056 /* Outside of a class-specifier, there must be a
27057 nested-name-specifier. Except in C++17 mode, where we
27058 might be declaring a guiding declaration. */
27059 if (!nested_name_specifier
&& outside_class_specifier_p
27060 && cxx_dialect
< cxx17
)
27061 constructor_p
= false;
27062 else if (nested_name_specifier
== error_mark_node
)
27063 constructor_p
= false;
27065 /* If we have a class scope, this is easy; DR 147 says that S::S always
27066 names the constructor, and no other qualified name could. */
27067 if (constructor_p
&& nested_name_specifier
27068 && CLASS_TYPE_P (nested_name_specifier
))
27070 tree id
= cp_parser_unqualified_id (parser
,
27071 /*template_keyword_p=*/false,
27072 /*check_dependency_p=*/false,
27073 /*declarator_p=*/true,
27074 /*optional_p=*/false);
27075 if (is_overloaded_fn (id
))
27076 id
= DECL_NAME (get_first_fn (id
));
27077 if (!constructor_name_p (id
, nested_name_specifier
))
27078 constructor_p
= false;
27080 /* If we still think that this might be a constructor-declarator,
27081 look for a class-name. */
27082 else if (constructor_p
)
27086 template <typename T> struct S {
27090 we must recognize that the nested `S' names a class. */
27091 if (cxx_dialect
>= cxx17
)
27092 cp_parser_parse_tentatively (parser
);
27095 type_decl
= cp_parser_class_name (parser
,
27096 /*typename_keyword_p=*/false,
27097 /*template_keyword_p=*/false,
27099 /*check_dependency_p=*/false,
27100 /*class_head_p=*/false,
27101 /*is_declaration=*/false);
27103 if (cxx_dialect
>= cxx17
27104 && !cp_parser_parse_definitely (parser
))
27106 type_decl
= NULL_TREE
;
27107 tree tmpl
= cp_parser_template_name (parser
,
27108 /*template_keyword*/false,
27109 /*check_dependency_p*/false,
27110 /*is_declaration*/false,
27112 /*is_identifier*/NULL
);
27113 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27114 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27115 /* It's a deduction guide, return true. */;
27117 cp_parser_simulate_error (parser
);
27120 /* If there was no class-name, then this is not a constructor.
27121 Otherwise, if we are in a class-specifier and we aren't
27122 handling a friend declaration, check that its type matches
27123 current_class_type (c++/38313). Note: error_mark_node
27124 is left alone for error recovery purposes. */
27125 constructor_p
= (!cp_parser_error_occurred (parser
)
27126 && (outside_class_specifier_p
27127 || type_decl
== NULL_TREE
27128 || type_decl
== error_mark_node
27129 || same_type_p (current_class_type
,
27130 TREE_TYPE (type_decl
))));
27132 /* If we're still considering a constructor, we have to see a `(',
27133 to begin the parameter-declaration-clause, followed by either a
27134 `)', an `...', or a decl-specifier. We need to check for a
27135 type-specifier to avoid being fooled into thinking that:
27139 is a constructor. (It is actually a function named `f' that
27140 takes one parameter (of type `int') and returns a value of type
27143 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27144 constructor_p
= false;
27147 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27148 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27149 /* A parameter declaration begins with a decl-specifier,
27150 which is either the "attribute" keyword, a storage class
27151 specifier, or (usually) a type-specifier. */
27152 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27155 tree pushed_scope
= NULL_TREE
;
27156 unsigned saved_num_template_parameter_lists
;
27158 /* Names appearing in the type-specifier should be looked up
27159 in the scope of the class. */
27160 if (current_class_type
)
27162 else if (type_decl
)
27164 type
= TREE_TYPE (type_decl
);
27165 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27167 type
= resolve_typename_type (type
,
27168 /*only_current_p=*/false);
27169 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27171 cp_parser_abort_tentative_parse (parser
);
27175 pushed_scope
= push_scope (type
);
27178 /* Inside the constructor parameter list, surrounding
27179 template-parameter-lists do not apply. */
27180 saved_num_template_parameter_lists
27181 = parser
->num_template_parameter_lists
;
27182 parser
->num_template_parameter_lists
= 0;
27184 /* Look for the type-specifier. */
27185 cp_parser_type_specifier (parser
,
27186 CP_PARSER_FLAGS_NONE
,
27187 /*decl_specs=*/NULL
,
27188 /*is_declarator=*/true,
27189 /*declares_class_or_enum=*/NULL
,
27190 /*is_cv_qualifier=*/NULL
);
27192 parser
->num_template_parameter_lists
27193 = saved_num_template_parameter_lists
;
27195 /* Leave the scope of the class. */
27197 pop_scope (pushed_scope
);
27199 constructor_p
= !cp_parser_error_occurred (parser
);
27203 /* We did not really want to consume any tokens. */
27204 cp_parser_abort_tentative_parse (parser
);
27206 return constructor_p
;
27209 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27210 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27211 they must be performed once we are in the scope of the function.
27213 Returns the function defined. */
27216 cp_parser_function_definition_from_specifiers_and_declarator
27217 (cp_parser
* parser
,
27218 cp_decl_specifier_seq
*decl_specifiers
,
27220 const cp_declarator
*declarator
)
27225 /* Begin the function-definition. */
27226 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27228 /* The things we're about to see are not directly qualified by any
27229 template headers we've seen thus far. */
27230 reset_specialization ();
27232 /* If there were names looked up in the decl-specifier-seq that we
27233 did not check, check them now. We must wait until we are in the
27234 scope of the function to perform the checks, since the function
27235 might be a friend. */
27236 perform_deferred_access_checks (tf_warning_or_error
);
27240 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27241 parser
->omp_declare_simd
= NULL
;
27242 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27243 parser
->oacc_routine
= NULL
;
27248 /* Skip the entire function. */
27249 cp_parser_skip_to_end_of_block_or_statement (parser
);
27250 fn
= error_mark_node
;
27252 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27254 /* Seen already, skip it. An error message has already been output. */
27255 cp_parser_skip_to_end_of_block_or_statement (parser
);
27256 fn
= current_function_decl
;
27257 current_function_decl
= NULL_TREE
;
27258 /* If this is a function from a class, pop the nested class. */
27259 if (current_class_name
)
27260 pop_nested_class ();
27265 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27266 tv
= TV_PARSE_INLINE
;
27268 tv
= TV_PARSE_FUNC
;
27270 fn
= cp_parser_function_definition_after_declarator (parser
,
27271 /*inline_p=*/false);
27278 /* Parse the part of a function-definition that follows the
27279 declarator. INLINE_P is TRUE iff this function is an inline
27280 function defined within a class-specifier.
27282 Returns the function defined. */
27285 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27289 bool saved_in_unbraced_linkage_specification_p
;
27290 bool saved_in_function_body
;
27291 unsigned saved_num_template_parameter_lists
;
27293 bool fully_implicit_function_template_p
27294 = parser
->fully_implicit_function_template_p
;
27295 parser
->fully_implicit_function_template_p
= false;
27296 tree implicit_template_parms
27297 = parser
->implicit_template_parms
;
27298 parser
->implicit_template_parms
= 0;
27299 cp_binding_level
* implicit_template_scope
27300 = parser
->implicit_template_scope
;
27301 parser
->implicit_template_scope
= 0;
27303 saved_in_function_body
= parser
->in_function_body
;
27304 parser
->in_function_body
= true;
27305 /* If the next token is `return', then the code may be trying to
27306 make use of the "named return value" extension that G++ used to
27308 token
= cp_lexer_peek_token (parser
->lexer
);
27309 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27311 /* Consume the `return' keyword. */
27312 cp_lexer_consume_token (parser
->lexer
);
27313 /* Look for the identifier that indicates what value is to be
27315 cp_parser_identifier (parser
);
27316 /* Issue an error message. */
27317 error_at (token
->location
,
27318 "named return values are no longer supported");
27319 /* Skip tokens until we reach the start of the function body. */
27322 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27323 if (token
->type
== CPP_OPEN_BRACE
27324 || token
->type
== CPP_EOF
27325 || token
->type
== CPP_PRAGMA_EOL
)
27327 cp_lexer_consume_token (parser
->lexer
);
27330 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27331 anything declared inside `f'. */
27332 saved_in_unbraced_linkage_specification_p
27333 = parser
->in_unbraced_linkage_specification_p
;
27334 parser
->in_unbraced_linkage_specification_p
= false;
27335 /* Inside the function, surrounding template-parameter-lists do not
27337 saved_num_template_parameter_lists
27338 = parser
->num_template_parameter_lists
;
27339 parser
->num_template_parameter_lists
= 0;
27341 /* If the next token is `try', `__transaction_atomic', or
27342 `__transaction_relaxed`, then we are looking at either function-try-block
27343 or function-transaction-block. Note that all of these include the
27345 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27346 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27347 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27348 RID_TRANSACTION_RELAXED
))
27349 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27350 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27351 cp_parser_function_try_block (parser
);
27353 cp_parser_ctor_initializer_opt_and_function_body
27354 (parser
, /*in_function_try_block=*/false);
27356 /* Finish the function. */
27357 fn
= finish_function (inline_p
);
27358 /* Generate code for it, if necessary. */
27359 expand_or_defer_fn (fn
);
27360 /* Restore the saved values. */
27361 parser
->in_unbraced_linkage_specification_p
27362 = saved_in_unbraced_linkage_specification_p
;
27363 parser
->num_template_parameter_lists
27364 = saved_num_template_parameter_lists
;
27365 parser
->in_function_body
= saved_in_function_body
;
27367 parser
->fully_implicit_function_template_p
27368 = fully_implicit_function_template_p
;
27369 parser
->implicit_template_parms
27370 = implicit_template_parms
;
27371 parser
->implicit_template_scope
27372 = implicit_template_scope
;
27374 if (parser
->fully_implicit_function_template_p
)
27375 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27380 /* Parse a template-declaration body (following argument list). */
27383 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27384 tree parameter_list
,
27387 tree decl
= NULL_TREE
;
27388 bool friend_p
= false;
27390 /* We just processed one more parameter list. */
27391 ++parser
->num_template_parameter_lists
;
27393 /* Get the deferred access checks from the parameter list. These
27394 will be checked once we know what is being declared, as for a
27395 member template the checks must be performed in the scope of the
27396 class containing the member. */
27397 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27399 /* Tentatively parse for a new template parameter list, which can either be
27400 the template keyword or a template introduction. */
27401 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27403 else if (cxx_dialect
>= cxx11
27404 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27405 decl
= cp_parser_alias_declaration (parser
);
27408 /* There are no access checks when parsing a template, as we do not
27409 know if a specialization will be a friend. */
27410 push_deferring_access_checks (dk_no_check
);
27411 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27412 decl
= cp_parser_single_declaration (parser
,
27415 /*explicit_specialization_p=*/false,
27417 pop_deferring_access_checks ();
27419 /* If this is a member template declaration, let the front
27421 if (member_p
&& !friend_p
&& decl
)
27423 if (TREE_CODE (decl
) == TYPE_DECL
)
27424 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27426 decl
= finish_member_template_decl (decl
);
27428 else if (friend_p
&& decl
27429 && DECL_DECLARES_TYPE_P (decl
))
27430 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27431 /*complain=*/true);
27433 /* We are done with the current parameter list. */
27434 --parser
->num_template_parameter_lists
;
27436 pop_deferring_access_checks ();
27439 finish_template_decl (parameter_list
);
27441 /* Check the template arguments for a literal operator template. */
27443 && DECL_DECLARES_FUNCTION_P (decl
)
27444 && UDLIT_OPER_P (DECL_NAME (decl
)))
27447 if (parameter_list
== NULL_TREE
)
27451 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27452 if (num_parms
== 1)
27454 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27455 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27456 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27457 /* OK, C++20 string literal operator template. We don't need
27458 to warn in lower dialects here because we will have already
27459 warned about the template parameter. */;
27460 else if (TREE_TYPE (parm
) != char_type_node
27461 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27464 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27466 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27467 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27468 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27469 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27470 if (parm
== error_mark_node
27471 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27472 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27475 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27476 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27477 "ISO C++ did not adopt string literal operator templa"
27478 "tes taking an argument pack of characters");
27485 if (cxx_dialect
> cxx17
)
27486 error ("literal operator template %qD has invalid parameter list;"
27487 " Expected non-type template parameter pack <char...> "
27488 " or single non-type parameter of class type",
27491 error ("literal operator template %qD has invalid parameter list."
27492 " Expected non-type template parameter pack <char...>",
27497 /* Register member declarations. */
27498 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27499 finish_member_declaration (decl
);
27500 /* If DECL is a function template, we must return to parse it later.
27501 (Even though there is no definition, there might be default
27502 arguments that need handling.) */
27503 if (member_p
&& decl
27504 && DECL_DECLARES_FUNCTION_P (decl
))
27505 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27508 /* Parse a template introduction header for a template-declaration. Returns
27509 false if tentative parse fails. */
27512 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27514 cp_parser_parse_tentatively (parser
);
27516 tree saved_scope
= parser
->scope
;
27517 tree saved_object_scope
= parser
->object_scope
;
27518 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27520 /* Look for the optional `::' operator. */
27521 cp_parser_global_scope_opt (parser
,
27522 /*current_scope_valid_p=*/false);
27523 /* Look for the nested-name-specifier. */
27524 cp_parser_nested_name_specifier_opt (parser
,
27525 /*typename_keyword_p=*/false,
27526 /*check_dependency_p=*/true,
27528 /*is_declaration=*/false);
27530 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27531 tree concept_name
= cp_parser_identifier (parser
);
27533 /* Look up the concept for which we will be matching
27534 template parameters. */
27535 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27537 parser
->scope
= saved_scope
;
27538 parser
->object_scope
= saved_object_scope
;
27539 parser
->qualifying_scope
= saved_qualifying_scope
;
27541 if (concept_name
== error_mark_node
)
27542 cp_parser_simulate_error (parser
);
27544 /* Look for opening brace for introduction. */
27545 matching_braces braces
;
27546 braces
.require_open (parser
);
27548 if (!cp_parser_parse_definitely (parser
))
27551 push_deferring_access_checks (dk_deferred
);
27553 /* Build vector of placeholder parameters and grab
27554 matching identifiers. */
27555 tree introduction_list
= cp_parser_introduction_list (parser
);
27557 /* Look for closing brace for introduction. */
27558 if (!braces
.require_close (parser
))
27561 /* The introduction-list shall not be empty. */
27562 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27565 /* In cp_parser_introduction_list we have already issued an error. */
27569 if (tmpl_decl
== error_mark_node
)
27571 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27576 /* Build and associate the constraint. */
27577 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27578 if (parms
&& parms
!= error_mark_node
)
27580 cp_parser_template_declaration_after_parameters (parser
, parms
,
27585 error_at (token
->location
, "no matching concept for template-introduction");
27589 /* Parse a normal template-declaration following the template keyword. */
27592 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27594 tree parameter_list
;
27595 bool need_lang_pop
;
27596 location_t location
= input_location
;
27598 /* Look for the `<' token. */
27599 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27601 if (at_class_scope_p () && current_function_decl
)
27603 /* 14.5.2.2 [temp.mem]
27605 A local class shall not have member templates. */
27606 error_at (location
,
27607 "invalid declaration of member template in local class");
27608 cp_parser_skip_to_end_of_block_or_statement (parser
);
27613 A template ... shall not have C linkage. */
27614 if (current_lang_name
== lang_name_c
)
27616 error_at (location
, "template with C linkage");
27617 maybe_show_extern_c_location ();
27618 /* Give it C++ linkage to avoid confusing other parts of the
27620 push_lang_context (lang_name_cplusplus
);
27621 need_lang_pop
= true;
27624 need_lang_pop
= false;
27626 /* We cannot perform access checks on the template parameter
27627 declarations until we know what is being declared, just as we
27628 cannot check the decl-specifier list. */
27629 push_deferring_access_checks (dk_deferred
);
27631 /* If the next token is `>', then we have an invalid
27632 specialization. Rather than complain about an invalid template
27633 parameter, issue an error message here. */
27634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27636 cp_parser_error (parser
, "invalid explicit specialization");
27637 begin_specialization ();
27638 parameter_list
= NULL_TREE
;
27642 /* Parse the template parameters. */
27643 parameter_list
= cp_parser_template_parameter_list (parser
);
27646 /* Look for the `>'. */
27647 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27649 /* Manage template requirements */
27652 tree reqs
= get_shorthand_constraints (current_template_parms
);
27653 if (tree r
= cp_parser_requires_clause_opt (parser
))
27654 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27655 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27658 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27661 /* For the erroneous case of a template with C linkage, we pushed an
27662 implicit C++ linkage scope; exit that scope now. */
27664 pop_lang_context ();
27667 /* Parse a template-declaration, assuming that the `export' (and
27668 `extern') keywords, if present, has already been scanned. MEMBER_P
27669 is as for cp_parser_template_declaration. */
27672 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27674 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27676 cp_lexer_consume_token (parser
->lexer
);
27677 cp_parser_explicit_template_declaration (parser
, member_p
);
27680 else if (flag_concepts
)
27681 return cp_parser_template_introduction (parser
, member_p
);
27686 /* Perform the deferred access checks from a template-parameter-list.
27687 CHECKS is a TREE_LIST of access checks, as returned by
27688 get_deferred_access_checks. */
27691 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27693 ++processing_template_parmlist
;
27694 perform_access_checks (checks
, tf_warning_or_error
);
27695 --processing_template_parmlist
;
27698 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27699 `function-definition' sequence that follows a template header.
27700 If MEMBER_P is true, this declaration appears in a class scope.
27702 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27703 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27706 cp_parser_single_declaration (cp_parser
* parser
,
27707 vec
<deferred_access_check
, va_gc
> *checks
,
27709 bool explicit_specialization_p
,
27712 int declares_class_or_enum
;
27713 tree decl
= NULL_TREE
;
27714 cp_decl_specifier_seq decl_specifiers
;
27715 bool function_definition_p
= false;
27716 cp_token
*decl_spec_token_start
;
27718 /* This function is only used when processing a template
27720 gcc_assert (innermost_scope_kind () == sk_template_parms
27721 || innermost_scope_kind () == sk_template_spec
);
27723 /* Defer access checks until we know what is being declared. */
27724 push_deferring_access_checks (dk_deferred
);
27726 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27728 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27729 cp_parser_decl_specifier_seq (parser
,
27730 CP_PARSER_FLAGS_OPTIONAL
,
27732 &declares_class_or_enum
);
27734 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27736 /* There are no template typedefs. */
27737 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27739 error_at (decl_spec_token_start
->location
,
27740 "template declaration of %<typedef%>");
27741 decl
= error_mark_node
;
27744 /* Gather up the access checks that occurred the
27745 decl-specifier-seq. */
27746 stop_deferring_access_checks ();
27748 /* Check for the declaration of a template class. */
27749 if (declares_class_or_enum
)
27751 if (cp_parser_declares_only_class_p (parser
)
27752 || (declares_class_or_enum
& 2))
27754 // If this is a declaration, but not a definition, associate
27755 // any constraints with the type declaration. Constraints
27756 // are associated with definitions in cp_parser_class_specifier.
27757 if (declares_class_or_enum
== 1)
27758 associate_classtype_constraints (decl_specifiers
.type
);
27760 decl
= shadow_tag (&decl_specifiers
);
27765 friend template <typename T> struct A<T>::B;
27768 A<T>::B will be represented by a TYPENAME_TYPE, and
27769 therefore not recognized by shadow_tag. */
27770 if (friend_p
&& *friend_p
27772 && decl_specifiers
.type
27773 && TYPE_P (decl_specifiers
.type
))
27774 decl
= decl_specifiers
.type
;
27776 if (decl
&& decl
!= error_mark_node
)
27777 decl
= TYPE_NAME (decl
);
27779 decl
= error_mark_node
;
27781 /* Perform access checks for template parameters. */
27782 cp_parser_perform_template_parameter_access_checks (checks
);
27784 /* Give a helpful diagnostic for
27785 template <class T> struct A { } a;
27786 if we aren't already recovering from an error. */
27787 if (!cp_parser_declares_only_class_p (parser
)
27790 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27791 "a class template declaration must not declare "
27793 cp_parser_skip_to_end_of_block_or_statement (parser
);
27799 /* Complain about missing 'typename' or other invalid type names. */
27800 if (!decl_specifiers
.any_type_specifiers_p
27801 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27803 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27804 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27805 the rest of this declaration. */
27806 decl
= error_mark_node
;
27810 /* If it's not a template class, try for a template function. If
27811 the next token is a `;', then this declaration does not declare
27812 anything. But, if there were errors in the decl-specifiers, then
27813 the error might well have come from an attempted class-specifier.
27814 In that case, there's no need to warn about a missing declarator. */
27816 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27817 || decl_specifiers
.type
!= error_mark_node
))
27819 decl
= cp_parser_init_declarator (parser
,
27822 /*function_definition_allowed_p=*/true,
27824 declares_class_or_enum
,
27825 &function_definition_p
,
27828 /* 7.1.1-1 [dcl.stc]
27830 A storage-class-specifier shall not be specified in an explicit
27831 specialization... */
27833 && explicit_specialization_p
27834 && decl_specifiers
.storage_class
!= sc_none
)
27836 error_at (decl_spec_token_start
->location
,
27837 "explicit template specialization cannot have a storage class");
27838 decl
= error_mark_node
;
27841 if (decl
&& VAR_P (decl
))
27842 check_template_variable (decl
);
27845 /* Look for a trailing `;' after the declaration. */
27846 if (!function_definition_p
27847 && (decl
== error_mark_node
27848 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27849 cp_parser_skip_to_end_of_block_or_statement (parser
);
27852 pop_deferring_access_checks ();
27854 /* Clear any current qualification; whatever comes next is the start
27855 of something new. */
27856 parser
->scope
= NULL_TREE
;
27857 parser
->qualifying_scope
= NULL_TREE
;
27858 parser
->object_scope
= NULL_TREE
;
27863 /* Parse a cast-expression that is not the operand of a unary "&". */
27866 cp_parser_simple_cast_expression (cp_parser
*parser
)
27868 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27869 /*cast_p=*/false, /*decltype*/false, NULL
);
27872 /* Parse a functional cast to TYPE. Returns an expression
27873 representing the cast. */
27876 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27878 vec
<tree
, va_gc
> *vec
;
27879 tree expression_list
;
27883 location_t start_loc
= input_location
;
27886 type
= error_mark_node
;
27888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27890 cp_lexer_set_source_position (parser
->lexer
);
27891 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27892 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27893 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27894 if (TREE_CODE (type
) == TYPE_DECL
)
27895 type
= TREE_TYPE (type
);
27897 cast
= finish_compound_literal (type
, expression_list
,
27898 tf_warning_or_error
, fcl_functional
);
27899 /* Create a location of the form:
27902 with caret == start at the start of the type name,
27903 finishing at the closing brace. */
27904 location_t finish_loc
27905 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27906 location_t combined_loc
= make_location (start_loc
, start_loc
,
27908 cast
.set_location (combined_loc
);
27913 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27915 /*allow_expansion_p=*/true,
27916 /*non_constant_p=*/NULL
);
27918 expression_list
= error_mark_node
;
27921 expression_list
= build_tree_list_vec (vec
);
27922 release_tree_vector (vec
);
27925 cast
= build_functional_cast (type
, expression_list
,
27926 tf_warning_or_error
);
27927 /* [expr.const]/1: In an integral constant expression "only type
27928 conversions to integral or enumeration type can be used". */
27929 if (TREE_CODE (type
) == TYPE_DECL
)
27930 type
= TREE_TYPE (type
);
27931 if (cast
!= error_mark_node
27932 && !cast_valid_in_integral_constant_expression_p (type
)
27933 && cp_parser_non_integral_constant_expression (parser
,
27935 return error_mark_node
;
27937 /* Create a location of the form:
27940 with caret == start at the start of the type name,
27941 finishing at the closing paren. */
27942 location_t finish_loc
27943 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27944 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27945 cast
.set_location (combined_loc
);
27949 /* Save the tokens that make up the body of a member function defined
27950 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27951 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27952 specifiers applied to the declaration. Returns the FUNCTION_DECL
27953 for the member function. */
27956 cp_parser_save_member_function_body (cp_parser
* parser
,
27957 cp_decl_specifier_seq
*decl_specifiers
,
27958 cp_declarator
*declarator
,
27964 bool function_try_block
= false;
27966 /* Create the FUNCTION_DECL. */
27967 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27968 cp_finalize_omp_declare_simd (parser
, fn
);
27969 cp_finalize_oacc_routine (parser
, fn
, true);
27970 /* If something went badly wrong, bail out now. */
27971 if (fn
== error_mark_node
)
27973 /* If there's a function-body, skip it. */
27974 if (cp_parser_token_starts_function_definition_p
27975 (cp_lexer_peek_token (parser
->lexer
)))
27976 cp_parser_skip_to_end_of_block_or_statement (parser
);
27977 return error_mark_node
;
27980 /* Remember it, if there default args to post process. */
27981 cp_parser_save_default_args (parser
, fn
);
27983 /* Save away the tokens that make up the body of the
27985 first
= parser
->lexer
->next_token
;
27987 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27988 cp_lexer_consume_token (parser
->lexer
);
27989 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27990 RID_TRANSACTION_ATOMIC
))
27992 cp_lexer_consume_token (parser
->lexer
);
27993 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27995 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27996 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27997 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27998 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27999 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28001 cp_lexer_consume_token (parser
->lexer
);
28002 cp_lexer_consume_token (parser
->lexer
);
28003 cp_lexer_consume_token (parser
->lexer
);
28004 cp_lexer_consume_token (parser
->lexer
);
28005 cp_lexer_consume_token (parser
->lexer
);
28008 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28009 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28011 cp_lexer_consume_token (parser
->lexer
);
28012 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28017 /* Handle function try blocks. */
28018 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28020 cp_lexer_consume_token (parser
->lexer
);
28021 function_try_block
= true;
28023 /* We can have braced-init-list mem-initializers before the fn body. */
28024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28026 cp_lexer_consume_token (parser
->lexer
);
28027 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28029 /* cache_group will stop after an un-nested { } pair, too. */
28030 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28033 /* variadic mem-inits have ... after the ')'. */
28034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28035 cp_lexer_consume_token (parser
->lexer
);
28038 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28039 /* Handle function try blocks. */
28040 if (function_try_block
)
28041 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28042 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28043 last
= parser
->lexer
->next_token
;
28045 /* Save away the inline definition; we will process it when the
28046 class is complete. */
28047 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28048 DECL_PENDING_INLINE_P (fn
) = 1;
28050 /* We need to know that this was defined in the class, so that
28051 friend templates are handled correctly. */
28052 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28054 /* Add FN to the queue of functions to be parsed later. */
28055 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28060 /* Save the tokens that make up the in-class initializer for a non-static
28061 data member. Returns a DEFAULT_ARG. */
28064 cp_parser_save_nsdmi (cp_parser
* parser
)
28066 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28069 /* Parse a template-argument-list, as well as the trailing ">" (but
28070 not the opening "<"). See cp_parser_template_argument_list for the
28074 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28078 tree saved_qualifying_scope
;
28079 tree saved_object_scope
;
28080 bool saved_greater_than_is_operator_p
;
28084 When parsing a template-id, the first non-nested `>' is taken as
28085 the end of the template-argument-list rather than a greater-than
28087 saved_greater_than_is_operator_p
28088 = parser
->greater_than_is_operator_p
;
28089 parser
->greater_than_is_operator_p
= false;
28090 /* Parsing the argument list may modify SCOPE, so we save it
28092 saved_scope
= parser
->scope
;
28093 saved_qualifying_scope
= parser
->qualifying_scope
;
28094 saved_object_scope
= parser
->object_scope
;
28095 /* We need to evaluate the template arguments, even though this
28096 template-id may be nested within a "sizeof". */
28098 /* Parse the template-argument-list itself. */
28099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28100 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28101 arguments
= NULL_TREE
;
28103 arguments
= cp_parser_template_argument_list (parser
);
28104 /* Look for the `>' that ends the template-argument-list. If we find
28105 a '>>' instead, it's probably just a typo. */
28106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28108 if (cxx_dialect
!= cxx98
)
28110 /* In C++0x, a `>>' in a template argument list or cast
28111 expression is considered to be two separate `>'
28112 tokens. So, change the current token to a `>', but don't
28113 consume it: it will be consumed later when the outer
28114 template argument list (or cast expression) is parsed.
28115 Note that this replacement of `>' for `>>' is necessary
28116 even if we are parsing tentatively: in the tentative
28117 case, after calling
28118 cp_parser_enclosed_template_argument_list we will always
28119 throw away all of the template arguments and the first
28120 closing `>', either because the template argument list
28121 was erroneous or because we are replacing those tokens
28122 with a CPP_TEMPLATE_ID token. The second `>' (which will
28123 not have been thrown away) is needed either to close an
28124 outer template argument list or to complete a new-style
28126 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28127 token
->type
= CPP_GREATER
;
28129 else if (!saved_greater_than_is_operator_p
)
28131 /* If we're in a nested template argument list, the '>>' has
28132 to be a typo for '> >'. We emit the error message, but we
28133 continue parsing and we push a '>' as next token, so that
28134 the argument list will be parsed correctly. Note that the
28135 global source location is still on the token before the
28136 '>>', so we need to say explicitly where we want it. */
28137 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28138 gcc_rich_location
richloc (token
->location
);
28139 richloc
.add_fixit_replace ("> >");
28140 error_at (&richloc
, "%<>>%> should be %<> >%> "
28141 "within a nested template argument list");
28143 token
->type
= CPP_GREATER
;
28147 /* If this is not a nested template argument list, the '>>'
28148 is a typo for '>'. Emit an error message and continue.
28149 Same deal about the token location, but here we can get it
28150 right by consuming the '>>' before issuing the diagnostic. */
28151 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28152 error_at (token
->location
,
28153 "spurious %<>>%>, use %<>%> to terminate "
28154 "a template argument list");
28158 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28159 /* The `>' token might be a greater-than operator again now. */
28160 parser
->greater_than_is_operator_p
28161 = saved_greater_than_is_operator_p
;
28162 /* Restore the SAVED_SCOPE. */
28163 parser
->scope
= saved_scope
;
28164 parser
->qualifying_scope
= saved_qualifying_scope
;
28165 parser
->object_scope
= saved_object_scope
;
28170 /* MEMBER_FUNCTION is a member function, or a friend. If default
28171 arguments, or the body of the function have not yet been parsed,
28175 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28177 timevar_push (TV_PARSE_INMETH
);
28178 /* If this member is a template, get the underlying
28180 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28181 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28183 /* There should not be any class definitions in progress at this
28184 point; the bodies of members are only parsed outside of all class
28186 gcc_assert (parser
->num_classes_being_defined
== 0);
28187 /* While we're parsing the member functions we might encounter more
28188 classes. We want to handle them right away, but we don't want
28189 them getting mixed up with functions that are currently in the
28191 push_unparsed_function_queues (parser
);
28193 /* Make sure that any template parameters are in scope. */
28194 maybe_begin_member_template_processing (member_function
);
28196 /* If the body of the function has not yet been parsed, parse it
28198 if (DECL_PENDING_INLINE_P (member_function
))
28200 tree function_scope
;
28201 cp_token_cache
*tokens
;
28203 /* The function is no longer pending; we are processing it. */
28204 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28205 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28206 DECL_PENDING_INLINE_P (member_function
) = 0;
28208 /* If this is a local class, enter the scope of the containing
28210 function_scope
= current_function_decl
;
28211 if (function_scope
)
28212 push_function_context ();
28214 /* Push the body of the function onto the lexer stack. */
28215 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28217 /* Let the front end know that we going to be defining this
28219 start_preparsed_function (member_function
, NULL_TREE
,
28220 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28222 /* Don't do access checking if it is a templated function. */
28223 if (processing_template_decl
)
28224 push_deferring_access_checks (dk_no_check
);
28226 /* #pragma omp declare reduction needs special parsing. */
28227 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28229 parser
->lexer
->in_pragma
= true;
28230 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28231 finish_function (/*inline_p=*/true);
28232 cp_check_omp_declare_reduction (member_function
);
28235 /* Now, parse the body of the function. */
28236 cp_parser_function_definition_after_declarator (parser
,
28237 /*inline_p=*/true);
28239 if (processing_template_decl
)
28240 pop_deferring_access_checks ();
28242 /* Leave the scope of the containing function. */
28243 if (function_scope
)
28244 pop_function_context ();
28245 cp_parser_pop_lexer (parser
);
28248 /* Remove any template parameters from the symbol table. */
28249 maybe_end_member_template_processing ();
28251 /* Restore the queue. */
28252 pop_unparsed_function_queues (parser
);
28253 timevar_pop (TV_PARSE_INMETH
);
28256 /* If DECL contains any default args, remember it on the unparsed
28257 functions queue. */
28260 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28264 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28266 probe
= TREE_CHAIN (probe
))
28267 if (TREE_PURPOSE (probe
))
28269 cp_default_arg_entry entry
= {current_class_type
, decl
};
28270 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28275 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28276 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28277 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28278 from the parameter-type-list. */
28281 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28282 tree default_arg
, tree parmtype
)
28284 cp_token_cache
*tokens
;
28288 if (default_arg
== error_mark_node
)
28289 return error_mark_node
;
28291 /* Push the saved tokens for the default argument onto the parser's
28293 tokens
= DEFARG_TOKENS (default_arg
);
28294 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28296 start_lambda_scope (decl
);
28298 /* Parse the default argument. */
28299 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28300 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28301 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28303 finish_lambda_scope ();
28305 if (parsed_arg
== error_mark_node
)
28306 cp_parser_skip_to_end_of_statement (parser
);
28308 if (!processing_template_decl
)
28310 /* In a non-template class, check conversions now. In a template,
28311 we'll wait and instantiate these as needed. */
28312 if (TREE_CODE (decl
) == PARM_DECL
)
28313 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28314 tf_warning_or_error
);
28315 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28316 parsed_arg
= error_mark_node
;
28318 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28321 /* If the token stream has not been completely used up, then
28322 there was extra junk after the end of the default
28324 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28326 if (TREE_CODE (decl
) == PARM_DECL
)
28327 cp_parser_error (parser
, "expected %<,%>");
28329 cp_parser_error (parser
, "expected %<;%>");
28332 /* Revert to the main lexer. */
28333 cp_parser_pop_lexer (parser
);
28338 /* FIELD is a non-static data member with an initializer which we saved for
28339 later; parse it now. */
28342 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28346 maybe_begin_member_template_processing (field
);
28348 push_unparsed_function_queues (parser
);
28349 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28350 DECL_INITIAL (field
),
28352 pop_unparsed_function_queues (parser
);
28354 maybe_end_member_template_processing ();
28356 DECL_INITIAL (field
) = def
;
28359 /* FN is a FUNCTION_DECL which may contains a parameter with an
28360 unparsed DEFAULT_ARG. Parse the default args now. This function
28361 assumes that the current scope is the scope in which the default
28362 argument should be processed. */
28365 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28367 bool saved_local_variables_forbidden_p
;
28368 tree parm
, parmdecl
;
28370 /* While we're parsing the default args, we might (due to the
28371 statement expression extension) encounter more classes. We want
28372 to handle them right away, but we don't want them getting mixed
28373 up with default args that are currently in the queue. */
28374 push_unparsed_function_queues (parser
);
28376 /* Local variable names (and the `this' keyword) may not appear
28377 in a default argument. */
28378 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28379 parser
->local_variables_forbidden_p
= true;
28381 push_defarg_context (fn
);
28383 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28384 parmdecl
= DECL_ARGUMENTS (fn
);
28385 parm
&& parm
!= void_list_node
;
28386 parm
= TREE_CHAIN (parm
),
28387 parmdecl
= DECL_CHAIN (parmdecl
))
28389 tree default_arg
= TREE_PURPOSE (parm
);
28391 vec
<tree
, va_gc
> *insts
;
28398 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28399 /* This can happen for a friend declaration for a function
28400 already declared with default arguments. */
28404 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28406 TREE_VALUE (parm
));
28407 TREE_PURPOSE (parm
) = parsed_arg
;
28409 /* Update any instantiations we've already created. */
28410 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28411 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28412 TREE_PURPOSE (copy
) = parsed_arg
;
28415 pop_defarg_context ();
28417 /* Make sure no default arg is missing. */
28418 check_default_args (fn
);
28420 /* Restore the state of local_variables_forbidden_p. */
28421 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28423 /* Restore the queue. */
28424 pop_unparsed_function_queues (parser
);
28427 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28429 sizeof ... ( identifier )
28431 where the 'sizeof' token has already been consumed. */
28434 cp_parser_sizeof_pack (cp_parser
*parser
)
28436 /* Consume the `...'. */
28437 cp_lexer_consume_token (parser
->lexer
);
28438 maybe_warn_variadic_templates ();
28440 matching_parens parens
;
28441 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28443 parens
.consume_open (parser
);
28445 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28446 "%<sizeof...%> argument must be surrounded by parentheses");
28448 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28449 tree name
= cp_parser_identifier (parser
);
28450 if (name
== error_mark_node
)
28451 return error_mark_node
;
28452 /* The name is not qualified. */
28453 parser
->scope
= NULL_TREE
;
28454 parser
->qualifying_scope
= NULL_TREE
;
28455 parser
->object_scope
= NULL_TREE
;
28456 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28457 if (expr
== error_mark_node
)
28458 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28460 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28461 expr
= TREE_TYPE (expr
);
28462 else if (TREE_CODE (expr
) == CONST_DECL
)
28463 expr
= DECL_INITIAL (expr
);
28464 expr
= make_pack_expansion (expr
);
28465 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28468 parens
.require_close (parser
);
28473 /* Parse the operand of `sizeof' (or a similar operator). Returns
28474 either a TYPE or an expression, depending on the form of the
28475 input. The KEYWORD indicates which kind of expression we have
28479 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28481 tree expr
= NULL_TREE
;
28482 const char *saved_message
;
28484 bool saved_integral_constant_expression_p
;
28485 bool saved_non_integral_constant_expression_p
;
28487 /* If it's a `...', then we are computing the length of a parameter
28489 if (keyword
== RID_SIZEOF
28490 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28491 return cp_parser_sizeof_pack (parser
);
28493 /* Types cannot be defined in a `sizeof' expression. Save away the
28495 saved_message
= parser
->type_definition_forbidden_message
;
28496 /* And create the new one. */
28497 tmp
= concat ("types may not be defined in %<",
28498 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28499 "%> expressions", NULL
);
28500 parser
->type_definition_forbidden_message
= tmp
;
28502 /* The restrictions on constant-expressions do not apply inside
28503 sizeof expressions. */
28504 saved_integral_constant_expression_p
28505 = parser
->integral_constant_expression_p
;
28506 saved_non_integral_constant_expression_p
28507 = parser
->non_integral_constant_expression_p
;
28508 parser
->integral_constant_expression_p
= false;
28510 /* Do not actually evaluate the expression. */
28511 ++cp_unevaluated_operand
;
28512 ++c_inhibit_evaluation_warnings
;
28513 /* If it's a `(', then we might be looking at the type-id
28515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28517 tree type
= NULL_TREE
;
28519 /* We can't be sure yet whether we're looking at a type-id or an
28521 cp_parser_parse_tentatively (parser
);
28523 matching_parens parens
;
28524 parens
.consume_open (parser
);
28526 /* Note: as a GNU Extension, compound literals are considered
28527 postfix-expressions as they are in C99, so they are valid
28528 arguments to sizeof. See comment in cp_parser_cast_expression
28530 if (cp_parser_compound_literal_p (parser
))
28531 cp_parser_simulate_error (parser
);
28534 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28535 parser
->in_type_id_in_expr_p
= true;
28536 /* Look for the type-id. */
28537 type
= cp_parser_type_id (parser
);
28538 /* Look for the closing `)'. */
28539 parens
.require_close (parser
);
28540 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28543 /* If all went well, then we're done. */
28544 if (cp_parser_parse_definitely (parser
))
28548 /* If the type-id production did not work out, then we must be
28549 looking at the unary-expression production. */
28551 expr
= cp_parser_unary_expression (parser
);
28553 /* Go back to evaluating expressions. */
28554 --cp_unevaluated_operand
;
28555 --c_inhibit_evaluation_warnings
;
28557 /* Free the message we created. */
28559 /* And restore the old one. */
28560 parser
->type_definition_forbidden_message
= saved_message
;
28561 parser
->integral_constant_expression_p
28562 = saved_integral_constant_expression_p
;
28563 parser
->non_integral_constant_expression_p
28564 = saved_non_integral_constant_expression_p
;
28569 /* If the current declaration has no declarator, return true. */
28572 cp_parser_declares_only_class_p (cp_parser
*parser
)
28574 /* If the next token is a `;' or a `,' then there is no
28576 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28577 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28580 /* Update the DECL_SPECS to reflect the storage class indicated by
28584 cp_parser_set_storage_class (cp_parser
*parser
,
28585 cp_decl_specifier_seq
*decl_specs
,
28589 cp_storage_class storage_class
;
28591 if (parser
->in_unbraced_linkage_specification_p
)
28593 error_at (token
->location
, "invalid use of %qD in linkage specification",
28594 ridpointers
[keyword
]);
28597 else if (decl_specs
->storage_class
!= sc_none
)
28599 decl_specs
->conflicting_specifiers_p
= true;
28603 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28604 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28605 && decl_specs
->gnu_thread_keyword_p
)
28607 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28608 "%<__thread%> before %qD", ridpointers
[keyword
]);
28614 storage_class
= sc_auto
;
28617 storage_class
= sc_register
;
28620 storage_class
= sc_static
;
28623 storage_class
= sc_extern
;
28626 storage_class
= sc_mutable
;
28629 gcc_unreachable ();
28631 decl_specs
->storage_class
= storage_class
;
28632 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28634 /* A storage class specifier cannot be applied alongside a typedef
28635 specifier. If there is a typedef specifier present then set
28636 conflicting_specifiers_p which will trigger an error later
28637 on in grokdeclarator. */
28638 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28639 decl_specs
->conflicting_specifiers_p
= true;
28642 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28643 is true, the type is a class or enum definition. */
28646 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28649 bool type_definition_p
)
28651 decl_specs
->any_specifiers_p
= true;
28653 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28654 (with, for example, in "typedef int wchar_t;") we remember that
28655 this is what happened. In system headers, we ignore these
28656 declarations so that G++ can work with system headers that are not
28658 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28659 && !type_definition_p
28660 && (type_spec
== boolean_type_node
28661 || type_spec
== char16_type_node
28662 || type_spec
== char32_type_node
28663 || type_spec
== wchar_type_node
)
28664 && (decl_specs
->type
28665 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28666 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28667 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28668 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28670 decl_specs
->redefined_builtin_type
= type_spec
;
28671 set_and_check_decl_spec_loc (decl_specs
,
28672 ds_redefined_builtin_type_spec
,
28674 if (!decl_specs
->type
)
28676 decl_specs
->type
= type_spec
;
28677 decl_specs
->type_definition_p
= false;
28678 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28681 else if (decl_specs
->type
)
28682 decl_specs
->multiple_types_p
= true;
28685 decl_specs
->type
= type_spec
;
28686 decl_specs
->type_definition_p
= type_definition_p
;
28687 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28688 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28692 /* True iff TOKEN is the GNU keyword __thread. */
28695 token_is__thread (cp_token
*token
)
28697 gcc_assert (token
->keyword
== RID_THREAD
);
28698 return id_equal (token
->u
.value
, "__thread");
28701 /* Set the location for a declarator specifier and check if it is
28704 DECL_SPECS is the sequence of declarator specifiers onto which to
28707 DS is the single declarator specifier to set which location is to
28708 be set onto the existing sequence of declarators.
28710 LOCATION is the location for the declarator specifier to
28714 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28715 cp_decl_spec ds
, cp_token
*token
)
28717 gcc_assert (ds
< ds_last
);
28719 if (decl_specs
== NULL
)
28722 location_t location
= token
->location
;
28724 if (decl_specs
->locations
[ds
] == 0)
28726 decl_specs
->locations
[ds
] = location
;
28727 if (ds
== ds_thread
)
28728 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28734 if (decl_specs
->locations
[ds_long_long
] != 0)
28735 error_at (location
,
28736 "%<long long long%> is too long for GCC");
28739 decl_specs
->locations
[ds_long_long
] = location
;
28740 pedwarn_cxx98 (location
,
28742 "ISO C++ 1998 does not support %<long long%>");
28745 else if (ds
== ds_thread
)
28747 bool gnu
= token_is__thread (token
);
28748 gcc_rich_location
richloc (location
);
28749 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28751 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28752 error_at (&richloc
,
28753 "both %<__thread%> and %<thread_local%> specified");
28757 richloc
.add_fixit_remove ();
28758 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28763 static const char *const decl_spec_names
[] = {
28780 gcc_rich_location
richloc (location
);
28781 richloc
.add_fixit_remove ();
28782 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28787 /* Return true iff the declarator specifier DS is present in the
28788 sequence of declarator specifiers DECL_SPECS. */
28791 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28794 gcc_assert (ds
< ds_last
);
28796 if (decl_specs
== NULL
)
28799 return decl_specs
->locations
[ds
] != 0;
28802 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28803 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28806 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28808 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28811 /* Issue an error message indicating that TOKEN_DESC was expected.
28812 If KEYWORD is true, it indicated this function is called by
28813 cp_parser_require_keword and the required token can only be
28814 a indicated keyword.
28816 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28817 within any error as the location of an "opening" token matching
28818 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28819 RT_CLOSE_PAREN). */
28822 cp_parser_required_error (cp_parser
*parser
,
28823 required_token token_desc
,
28825 location_t matching_location
)
28827 if (cp_parser_simulate_error (parser
))
28830 const char *gmsgid
= NULL
;
28831 switch (token_desc
)
28834 gmsgid
= G_("expected %<new%>");
28837 gmsgid
= G_("expected %<delete%>");
28840 gmsgid
= G_("expected %<return%>");
28843 gmsgid
= G_("expected %<while%>");
28846 gmsgid
= G_("expected %<extern%>");
28848 case RT_STATIC_ASSERT
:
28849 gmsgid
= G_("expected %<static_assert%>");
28852 gmsgid
= G_("expected %<decltype%>");
28855 gmsgid
= G_("expected %<operator%>");
28858 gmsgid
= G_("expected %<class%>");
28861 gmsgid
= G_("expected %<template%>");
28864 gmsgid
= G_("expected %<namespace%>");
28867 gmsgid
= G_("expected %<using%>");
28870 gmsgid
= G_("expected %<asm%>");
28873 gmsgid
= G_("expected %<try%>");
28876 gmsgid
= G_("expected %<catch%>");
28879 gmsgid
= G_("expected %<throw%>");
28882 gmsgid
= G_("expected %<__label__%>");
28885 gmsgid
= G_("expected %<@try%>");
28887 case RT_AT_SYNCHRONIZED
:
28888 gmsgid
= G_("expected %<@synchronized%>");
28891 gmsgid
= G_("expected %<@throw%>");
28893 case RT_TRANSACTION_ATOMIC
:
28894 gmsgid
= G_("expected %<__transaction_atomic%>");
28896 case RT_TRANSACTION_RELAXED
:
28897 gmsgid
= G_("expected %<__transaction_relaxed%>");
28903 if (!gmsgid
&& !keyword
)
28905 switch (token_desc
)
28908 gmsgid
= G_("expected %<;%>");
28910 case RT_OPEN_PAREN
:
28911 gmsgid
= G_("expected %<(%>");
28913 case RT_CLOSE_BRACE
:
28914 gmsgid
= G_("expected %<}%>");
28916 case RT_OPEN_BRACE
:
28917 gmsgid
= G_("expected %<{%>");
28919 case RT_CLOSE_SQUARE
:
28920 gmsgid
= G_("expected %<]%>");
28922 case RT_OPEN_SQUARE
:
28923 gmsgid
= G_("expected %<[%>");
28926 gmsgid
= G_("expected %<,%>");
28929 gmsgid
= G_("expected %<::%>");
28932 gmsgid
= G_("expected %<<%>");
28935 gmsgid
= G_("expected %<>%>");
28938 gmsgid
= G_("expected %<=%>");
28941 gmsgid
= G_("expected %<...%>");
28944 gmsgid
= G_("expected %<*%>");
28947 gmsgid
= G_("expected %<~%>");
28950 gmsgid
= G_("expected %<:%>");
28952 case RT_COLON_SCOPE
:
28953 gmsgid
= G_("expected %<:%> or %<::%>");
28955 case RT_CLOSE_PAREN
:
28956 gmsgid
= G_("expected %<)%>");
28958 case RT_COMMA_CLOSE_PAREN
:
28959 gmsgid
= G_("expected %<,%> or %<)%>");
28961 case RT_PRAGMA_EOL
:
28962 gmsgid
= G_("expected end of line");
28965 gmsgid
= G_("expected identifier");
28968 gmsgid
= G_("expected selection-statement");
28971 gmsgid
= G_("expected iteration-statement");
28974 gmsgid
= G_("expected jump-statement");
28977 gmsgid
= G_("expected class-key");
28979 case RT_CLASS_TYPENAME_TEMPLATE
:
28980 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28983 gcc_unreachable ();
28988 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28992 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28993 issue an error message indicating that TOKEN_DESC was expected.
28995 Returns the token consumed, if the token had the appropriate type.
28996 Otherwise, returns NULL.
28998 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28999 within any error as the location of an "opening" token matching
29000 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29001 RT_CLOSE_PAREN). */
29004 cp_parser_require (cp_parser
* parser
,
29005 enum cpp_ttype type
,
29006 required_token token_desc
,
29007 location_t matching_location
)
29009 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29010 return cp_lexer_consume_token (parser
->lexer
);
29013 /* Output the MESSAGE -- unless we're parsing tentatively. */
29014 if (!cp_parser_simulate_error (parser
))
29015 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29016 matching_location
);
29021 /* An error message is produced if the next token is not '>'.
29022 All further tokens are skipped until the desired token is
29023 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29026 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29028 /* Current level of '< ... >'. */
29029 unsigned level
= 0;
29030 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29031 unsigned nesting_depth
= 0;
29033 /* Are we ready, yet? If not, issue error message. */
29034 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29037 /* Skip tokens until the desired token is found. */
29040 /* Peek at the next token. */
29041 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29044 if (!nesting_depth
)
29049 if (cxx_dialect
== cxx98
)
29050 /* C++0x views the `>>' operator as two `>' tokens, but
29053 else if (!nesting_depth
&& level
-- == 0)
29055 /* We've hit a `>>' where the first `>' closes the
29056 template argument list, and the second `>' is
29057 spurious. Just consume the `>>' and stop; we've
29058 already produced at least one error. */
29059 cp_lexer_consume_token (parser
->lexer
);
29062 /* Fall through for C++0x, so we handle the second `>' in
29064 gcc_fallthrough ();
29067 if (!nesting_depth
&& level
-- == 0)
29069 /* We've reached the token we want, consume it and stop. */
29070 cp_lexer_consume_token (parser
->lexer
);
29075 case CPP_OPEN_PAREN
:
29076 case CPP_OPEN_SQUARE
:
29080 case CPP_CLOSE_PAREN
:
29081 case CPP_CLOSE_SQUARE
:
29082 if (nesting_depth
-- == 0)
29087 case CPP_PRAGMA_EOL
:
29088 case CPP_SEMICOLON
:
29089 case CPP_OPEN_BRACE
:
29090 case CPP_CLOSE_BRACE
:
29091 /* The '>' was probably forgotten, don't look further. */
29098 /* Consume this token. */
29099 cp_lexer_consume_token (parser
->lexer
);
29103 /* If the next token is the indicated keyword, consume it. Otherwise,
29104 issue an error message indicating that TOKEN_DESC was expected.
29106 Returns the token consumed, if the token had the appropriate type.
29107 Otherwise, returns NULL. */
29110 cp_parser_require_keyword (cp_parser
* parser
,
29112 required_token token_desc
)
29114 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29116 if (token
&& token
->keyword
!= keyword
)
29118 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29126 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29127 function-definition. */
29130 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29132 return (/* An ordinary function-body begins with an `{'. */
29133 token
->type
== CPP_OPEN_BRACE
29134 /* A ctor-initializer begins with a `:'. */
29135 || token
->type
== CPP_COLON
29136 /* A function-try-block begins with `try'. */
29137 || token
->keyword
== RID_TRY
29138 /* A function-transaction-block begins with `__transaction_atomic'
29139 or `__transaction_relaxed'. */
29140 || token
->keyword
== RID_TRANSACTION_ATOMIC
29141 || token
->keyword
== RID_TRANSACTION_RELAXED
29142 /* The named return value extension begins with `return'. */
29143 || token
->keyword
== RID_RETURN
);
29146 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29150 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29154 token
= cp_lexer_peek_token (parser
->lexer
);
29155 return (token
->type
== CPP_OPEN_BRACE
29156 || (token
->type
== CPP_COLON
29157 && !parser
->colon_doesnt_start_class_def_p
));
29160 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29161 C++0x) ending a template-argument. */
29164 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29168 token
= cp_lexer_peek_token (parser
->lexer
);
29169 return (token
->type
== CPP_COMMA
29170 || token
->type
== CPP_GREATER
29171 || token
->type
== CPP_ELLIPSIS
29172 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29175 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29176 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29179 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29184 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29185 if (token
->type
== CPP_LESS
)
29187 /* Check for the sequence `<::' in the original code. It would be lexed as
29188 `[:', where `[' is a digraph, and there is no whitespace before
29190 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29193 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29194 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29200 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29201 or none_type otherwise. */
29203 static enum tag_types
29204 cp_parser_token_is_class_key (cp_token
* token
)
29206 switch (token
->keyword
)
29211 return record_type
;
29220 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29221 or none_type otherwise or if the token is null. */
29223 static enum tag_types
29224 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29229 switch (token
->keyword
)
29234 return typename_type
;
29241 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29244 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29246 if (type
== error_mark_node
)
29248 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29250 if (permerror (input_location
, "%qs tag used in naming %q#T",
29251 class_key
== union_type
? "union"
29252 : class_key
== record_type
? "struct" : "class",
29254 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29255 "%q#T was previously declared here", type
);
29259 /* Issue an error message if DECL is redeclared with different
29260 access than its original declaration [class.access.spec/3].
29261 This applies to nested classes, nested class templates and
29262 enumerations [class.mem/1]. */
29265 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29268 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29269 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29272 if ((TREE_PRIVATE (decl
)
29273 != (current_access_specifier
== access_private_node
))
29274 || (TREE_PROTECTED (decl
)
29275 != (current_access_specifier
== access_protected_node
)))
29276 error_at (location
, "%qD redeclared with different access", decl
);
29279 /* Look for the `template' keyword, as a syntactic disambiguator.
29280 Return TRUE iff it is present, in which case it will be
29284 cp_parser_optional_template_keyword (cp_parser
*parser
)
29286 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29288 /* In C++98 the `template' keyword can only be used within templates;
29289 outside templates the parser can always figure out what is a
29290 template and what is not. In C++11, per the resolution of DR 468,
29291 `template' is allowed in cases where it is not strictly necessary. */
29292 if (!processing_template_decl
29293 && pedantic
&& cxx_dialect
== cxx98
)
29295 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29296 pedwarn (token
->location
, OPT_Wpedantic
,
29297 "in C++98 %<template%> (as a disambiguator) is only "
29298 "allowed within templates");
29299 /* If this part of the token stream is rescanned, the same
29300 error message would be generated. So, we purge the token
29301 from the stream. */
29302 cp_lexer_purge_token (parser
->lexer
);
29307 /* Consume the `template' keyword. */
29308 cp_lexer_consume_token (parser
->lexer
);
29315 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29316 set PARSER->SCOPE, and perform other related actions. */
29319 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29321 struct tree_check
*check_value
;
29323 /* Get the stored value. */
29324 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29325 /* Set the scope from the stored value. */
29326 parser
->scope
= saved_checks_value (check_value
);
29327 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29328 parser
->object_scope
= NULL_TREE
;
29331 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29332 encounter the end of a block before what we were looking for. */
29335 cp_parser_cache_group (cp_parser
*parser
,
29336 enum cpp_ttype end
,
29341 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29343 /* Abort a parenthesized expression if we encounter a semicolon. */
29344 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29345 && token
->type
== CPP_SEMICOLON
)
29347 /* If we've reached the end of the file, stop. */
29348 if (token
->type
== CPP_EOF
29349 || (end
!= CPP_PRAGMA_EOL
29350 && token
->type
== CPP_PRAGMA_EOL
))
29352 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29353 /* We've hit the end of an enclosing block, so there's been some
29354 kind of syntax error. */
29357 /* Consume the token. */
29358 cp_lexer_consume_token (parser
->lexer
);
29359 /* See if it starts a new group. */
29360 if (token
->type
== CPP_OPEN_BRACE
)
29362 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29363 /* In theory this should probably check end == '}', but
29364 cp_parser_save_member_function_body needs it to exit
29365 after either '}' or ')' when called with ')'. */
29369 else if (token
->type
== CPP_OPEN_PAREN
)
29371 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29372 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29375 else if (token
->type
== CPP_PRAGMA
)
29376 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29377 else if (token
->type
== end
)
29382 /* Like above, for caching a default argument or NSDMI. Both of these are
29383 terminated by a non-nested comma, but it can be unclear whether or not a
29384 comma is nested in a template argument list unless we do more parsing.
29385 In order to handle this ambiguity, when we encounter a ',' after a '<'
29386 we try to parse what follows as a parameter-declaration-list (in the
29387 case of a default argument) or a member-declarator (in the case of an
29388 NSDMI). If that succeeds, then we stop caching. */
29391 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29393 unsigned depth
= 0;
29394 int maybe_template_id
= 0;
29395 cp_token
*first_token
;
29397 tree default_argument
;
29399 /* Add tokens until we have processed the entire default
29400 argument. We add the range [first_token, token). */
29401 first_token
= cp_lexer_peek_token (parser
->lexer
);
29402 if (first_token
->type
== CPP_OPEN_BRACE
)
29404 /* For list-initialization, this is straightforward. */
29405 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29406 token
= cp_lexer_peek_token (parser
->lexer
);
29412 /* Peek at the next token. */
29413 token
= cp_lexer_peek_token (parser
->lexer
);
29414 /* What we do depends on what token we have. */
29415 switch (token
->type
)
29417 /* In valid code, a default argument must be
29418 immediately followed by a `,' `)', or `...'. */
29420 if (depth
== 0 && maybe_template_id
)
29422 /* If we've seen a '<', we might be in a
29423 template-argument-list. Until Core issue 325 is
29424 resolved, we don't know how this situation ought
29425 to be handled, so try to DTRT. We check whether
29426 what comes after the comma is a valid parameter
29427 declaration list. If it is, then the comma ends
29428 the default argument; otherwise the default
29429 argument continues. */
29430 bool error
= false;
29433 /* Set ITALP so cp_parser_parameter_declaration_list
29434 doesn't decide to commit to this parse. */
29435 bool saved_italp
= parser
->in_template_argument_list_p
;
29436 parser
->in_template_argument_list_p
= true;
29438 cp_parser_parse_tentatively (parser
);
29442 /* Parse declarators until we reach a non-comma or
29443 somthing that cannot be an initializer.
29444 Just checking whether we're looking at a single
29445 declarator is insufficient. Consider:
29446 int var = tuple<T,U>::x;
29447 The template parameter 'U' looks exactly like a
29451 int ctor_dtor_or_conv_p
;
29452 cp_lexer_consume_token (parser
->lexer
);
29453 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29454 &ctor_dtor_or_conv_p
,
29455 /*parenthesized_p=*/NULL
,
29457 /*friend_p=*/false);
29458 peek
= cp_lexer_peek_token (parser
->lexer
);
29459 if (cp_parser_error_occurred (parser
))
29462 while (peek
->type
== CPP_COMMA
);
29463 /* If we met an '=' or ';' then the original comma
29464 was the end of the NSDMI. Otherwise assume
29465 we're still in the NSDMI. */
29466 error
= (peek
->type
!= CPP_EQ
29467 && peek
->type
!= CPP_SEMICOLON
);
29471 cp_lexer_consume_token (parser
->lexer
);
29472 begin_scope (sk_function_parms
, NULL_TREE
);
29473 if (cp_parser_parameter_declaration_list (parser
)
29474 == error_mark_node
)
29476 pop_bindings_and_leave_scope ();
29478 if (!cp_parser_error_occurred (parser
) && !error
)
29480 cp_parser_abort_tentative_parse (parser
);
29482 parser
->in_template_argument_list_p
= saved_italp
;
29486 case CPP_CLOSE_PAREN
:
29488 /* If we run into a non-nested `;', `}', or `]',
29489 then the code is invalid -- but the default
29490 argument is certainly over. */
29491 case CPP_SEMICOLON
:
29492 case CPP_CLOSE_BRACE
:
29493 case CPP_CLOSE_SQUARE
:
29495 /* Handle correctly int n = sizeof ... ( p ); */
29496 && token
->type
!= CPP_ELLIPSIS
)
29498 /* Update DEPTH, if necessary. */
29499 else if (token
->type
== CPP_CLOSE_PAREN
29500 || token
->type
== CPP_CLOSE_BRACE
29501 || token
->type
== CPP_CLOSE_SQUARE
)
29505 case CPP_OPEN_PAREN
:
29506 case CPP_OPEN_SQUARE
:
29507 case CPP_OPEN_BRACE
:
29513 /* This might be the comparison operator, or it might
29514 start a template argument list. */
29515 ++maybe_template_id
;
29519 if (cxx_dialect
== cxx98
)
29521 /* Fall through for C++0x, which treats the `>>'
29522 operator like two `>' tokens in certain
29524 gcc_fallthrough ();
29529 /* This might be an operator, or it might close a
29530 template argument list. But if a previous '<'
29531 started a template argument list, this will have
29532 closed it, so we can't be in one anymore. */
29533 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29534 if (maybe_template_id
< 0)
29535 maybe_template_id
= 0;
29539 /* If we run out of tokens, issue an error message. */
29541 case CPP_PRAGMA_EOL
:
29542 error_at (token
->location
, "file ends in default argument");
29543 return error_mark_node
;
29547 /* In these cases, we should look for template-ids.
29548 For example, if the default argument is
29549 `X<int, double>()', we need to do name lookup to
29550 figure out whether or not `X' is a template; if
29551 so, the `,' does not end the default argument.
29553 That is not yet done. */
29560 /* If we've reached the end, stop. */
29564 /* Add the token to the token block. */
29565 token
= cp_lexer_consume_token (parser
->lexer
);
29568 /* Create a DEFAULT_ARG to represent the unparsed default
29570 default_argument
= make_node (DEFAULT_ARG
);
29571 DEFARG_TOKENS (default_argument
)
29572 = cp_token_cache_new (first_token
, token
);
29573 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29575 return default_argument
;
29578 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29581 defarg_location (tree default_argument
)
29583 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29584 location_t start
= tokens
->first
->location
;
29585 location_t end
= tokens
->last
->location
;
29586 return make_location (start
, start
, end
);
29589 /* Begin parsing tentatively. We always save tokens while parsing
29590 tentatively so that if the tentative parsing fails we can restore the
29594 cp_parser_parse_tentatively (cp_parser
* parser
)
29596 /* Enter a new parsing context. */
29597 parser
->context
= cp_parser_context_new (parser
->context
);
29598 /* Begin saving tokens. */
29599 cp_lexer_save_tokens (parser
->lexer
);
29600 /* In order to avoid repetitive access control error messages,
29601 access checks are queued up until we are no longer parsing
29603 push_deferring_access_checks (dk_deferred
);
29606 /* Commit to the currently active tentative parse. */
29609 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29611 cp_parser_context
*context
;
29614 /* Mark all of the levels as committed. */
29615 lexer
= parser
->lexer
;
29616 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29618 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29620 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29621 while (!cp_lexer_saving_tokens (lexer
))
29622 lexer
= lexer
->next
;
29623 cp_lexer_commit_tokens (lexer
);
29627 /* Commit to the topmost currently active tentative parse.
29629 Note that this function shouldn't be called when there are
29630 irreversible side-effects while in a tentative state. For
29631 example, we shouldn't create a permanent entry in the symbol
29632 table, or issue an error message that might not apply if the
29633 tentative parse is aborted. */
29636 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29638 cp_parser_context
*context
= parser
->context
;
29639 cp_lexer
*lexer
= parser
->lexer
;
29643 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29645 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29647 while (!cp_lexer_saving_tokens (lexer
))
29648 lexer
= lexer
->next
;
29649 cp_lexer_commit_tokens (lexer
);
29653 /* Abort the currently active tentative parse. All consumed tokens
29654 will be rolled back, and no diagnostics will be issued. */
29657 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29659 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29660 || errorcount
> 0);
29661 cp_parser_simulate_error (parser
);
29662 /* Now, pretend that we want to see if the construct was
29663 successfully parsed. */
29664 cp_parser_parse_definitely (parser
);
29667 /* Stop parsing tentatively. If a parse error has occurred, restore the
29668 token stream. Otherwise, commit to the tokens we have consumed.
29669 Returns true if no error occurred; false otherwise. */
29672 cp_parser_parse_definitely (cp_parser
* parser
)
29674 bool error_occurred
;
29675 cp_parser_context
*context
;
29677 /* Remember whether or not an error occurred, since we are about to
29678 destroy that information. */
29679 error_occurred
= cp_parser_error_occurred (parser
);
29680 /* Remove the topmost context from the stack. */
29681 context
= parser
->context
;
29682 parser
->context
= context
->next
;
29683 /* If no parse errors occurred, commit to the tentative parse. */
29684 if (!error_occurred
)
29686 /* Commit to the tokens read tentatively, unless that was
29688 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29689 cp_lexer_commit_tokens (parser
->lexer
);
29691 pop_to_parent_deferring_access_checks ();
29693 /* Otherwise, if errors occurred, roll back our state so that things
29694 are just as they were before we began the tentative parse. */
29697 cp_lexer_rollback_tokens (parser
->lexer
);
29698 pop_deferring_access_checks ();
29700 /* Add the context to the front of the free list. */
29701 context
->next
= cp_parser_context_free_list
;
29702 cp_parser_context_free_list
= context
;
29704 return !error_occurred
;
29707 /* Returns true if we are parsing tentatively and are not committed to
29708 this tentative parse. */
29711 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29713 return (cp_parser_parsing_tentatively (parser
)
29714 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29717 /* Returns nonzero iff an error has occurred during the most recent
29718 tentative parse. */
29721 cp_parser_error_occurred (cp_parser
* parser
)
29723 return (cp_parser_parsing_tentatively (parser
)
29724 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29727 /* Returns nonzero if GNU extensions are allowed. */
29730 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29732 return parser
->allow_gnu_extensions_p
;
29735 /* Objective-C++ Productions */
29738 /* Parse an Objective-C expression, which feeds into a primary-expression
29742 objc-message-expression
29743 objc-string-literal
29744 objc-encode-expression
29745 objc-protocol-expression
29746 objc-selector-expression
29748 Returns a tree representation of the expression. */
29751 cp_parser_objc_expression (cp_parser
* parser
)
29753 /* Try to figure out what kind of declaration is present. */
29754 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29758 case CPP_OPEN_SQUARE
:
29759 return cp_parser_objc_message_expression (parser
);
29761 case CPP_OBJC_STRING
:
29762 kwd
= cp_lexer_consume_token (parser
->lexer
);
29763 return objc_build_string_object (kwd
->u
.value
);
29766 switch (kwd
->keyword
)
29768 case RID_AT_ENCODE
:
29769 return cp_parser_objc_encode_expression (parser
);
29771 case RID_AT_PROTOCOL
:
29772 return cp_parser_objc_protocol_expression (parser
);
29774 case RID_AT_SELECTOR
:
29775 return cp_parser_objc_selector_expression (parser
);
29782 error_at (kwd
->location
,
29783 "misplaced %<@%D%> Objective-C++ construct",
29785 cp_parser_skip_to_end_of_block_or_statement (parser
);
29788 return error_mark_node
;
29791 /* Parse an Objective-C message expression.
29793 objc-message-expression:
29794 [ objc-message-receiver objc-message-args ]
29796 Returns a representation of an Objective-C message. */
29799 cp_parser_objc_message_expression (cp_parser
* parser
)
29801 tree receiver
, messageargs
;
29803 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29804 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29805 receiver
= cp_parser_objc_message_receiver (parser
);
29806 messageargs
= cp_parser_objc_message_args (parser
);
29807 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29808 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29810 tree result
= objc_build_message_expr (receiver
, messageargs
);
29812 /* Construct a location e.g.
29815 ranging from the '[' to the ']', with the caret at the start. */
29816 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29817 protected_set_expr_location (result
, combined_loc
);
29822 /* Parse an objc-message-receiver.
29824 objc-message-receiver:
29826 simple-type-specifier
29828 Returns a representation of the type or expression. */
29831 cp_parser_objc_message_receiver (cp_parser
* parser
)
29835 /* An Objective-C message receiver may be either (1) a type
29836 or (2) an expression. */
29837 cp_parser_parse_tentatively (parser
);
29838 rcv
= cp_parser_expression (parser
);
29840 /* If that worked out, fine. */
29841 if (cp_parser_parse_definitely (parser
))
29844 cp_parser_parse_tentatively (parser
);
29845 rcv
= cp_parser_simple_type_specifier (parser
,
29846 /*decl_specs=*/NULL
,
29847 CP_PARSER_FLAGS_NONE
);
29849 if (cp_parser_parse_definitely (parser
))
29850 return objc_get_class_reference (rcv
);
29852 cp_parser_error (parser
, "objective-c++ message receiver expected");
29853 return error_mark_node
;
29856 /* Parse the arguments and selectors comprising an Objective-C message.
29861 objc-selector-args , objc-comma-args
29863 objc-selector-args:
29864 objc-selector [opt] : assignment-expression
29865 objc-selector-args objc-selector [opt] : assignment-expression
29868 assignment-expression
29869 objc-comma-args , assignment-expression
29871 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29872 selector arguments and TREE_VALUE containing a list of comma
29876 cp_parser_objc_message_args (cp_parser
* parser
)
29878 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29879 bool maybe_unary_selector_p
= true;
29880 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29882 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29884 tree selector
= NULL_TREE
, arg
;
29886 if (token
->type
!= CPP_COLON
)
29887 selector
= cp_parser_objc_selector (parser
);
29889 /* Detect if we have a unary selector. */
29890 if (maybe_unary_selector_p
29891 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29892 return build_tree_list (selector
, NULL_TREE
);
29894 maybe_unary_selector_p
= false;
29895 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29896 arg
= cp_parser_assignment_expression (parser
);
29899 = chainon (sel_args
,
29900 build_tree_list (selector
, arg
));
29902 token
= cp_lexer_peek_token (parser
->lexer
);
29905 /* Handle non-selector arguments, if any. */
29906 while (token
->type
== CPP_COMMA
)
29910 cp_lexer_consume_token (parser
->lexer
);
29911 arg
= cp_parser_assignment_expression (parser
);
29914 = chainon (addl_args
,
29915 build_tree_list (NULL_TREE
, arg
));
29917 token
= cp_lexer_peek_token (parser
->lexer
);
29920 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29922 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29923 return build_tree_list (error_mark_node
, error_mark_node
);
29926 return build_tree_list (sel_args
, addl_args
);
29929 /* Parse an Objective-C encode expression.
29931 objc-encode-expression:
29932 @encode objc-typename
29934 Returns an encoded representation of the type argument. */
29937 cp_parser_objc_encode_expression (cp_parser
* parser
)
29941 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29943 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29944 matching_parens parens
;
29945 parens
.require_open (parser
);
29946 token
= cp_lexer_peek_token (parser
->lexer
);
29947 type
= complete_type (cp_parser_type_id (parser
));
29948 parens
.require_close (parser
);
29952 error_at (token
->location
,
29953 "%<@encode%> must specify a type as an argument");
29954 return error_mark_node
;
29957 /* This happens if we find @encode(T) (where T is a template
29958 typename or something dependent on a template typename) when
29959 parsing a template. In that case, we can't compile it
29960 immediately, but we rather create an AT_ENCODE_EXPR which will
29961 need to be instantiated when the template is used.
29963 if (dependent_type_p (type
))
29965 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29966 TREE_READONLY (value
) = 1;
29971 /* Build a location of the form:
29974 with caret==start at the @ token, finishing at the close paren. */
29975 location_t combined_loc
29976 = make_location (start_loc
, start_loc
,
29977 cp_lexer_previous_token (parser
->lexer
)->location
);
29979 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29982 /* Parse an Objective-C @defs expression. */
29985 cp_parser_objc_defs_expression (cp_parser
*parser
)
29989 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29990 matching_parens parens
;
29991 parens
.require_open (parser
);
29992 name
= cp_parser_identifier (parser
);
29993 parens
.require_close (parser
);
29995 return objc_get_class_ivars (name
);
29998 /* Parse an Objective-C protocol expression.
30000 objc-protocol-expression:
30001 @protocol ( identifier )
30003 Returns a representation of the protocol expression. */
30006 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30009 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30011 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30012 matching_parens parens
;
30013 parens
.require_open (parser
);
30014 proto
= cp_parser_identifier (parser
);
30015 parens
.require_close (parser
);
30017 /* Build a location of the form:
30020 with caret==start at the @ token, finishing at the close paren. */
30021 location_t combined_loc
30022 = make_location (start_loc
, start_loc
,
30023 cp_lexer_previous_token (parser
->lexer
)->location
);
30024 tree result
= objc_build_protocol_expr (proto
);
30025 protected_set_expr_location (result
, combined_loc
);
30029 /* Parse an Objective-C selector expression.
30031 objc-selector-expression:
30032 @selector ( objc-method-signature )
30034 objc-method-signature:
30040 objc-selector-seq objc-selector :
30042 Returns a representation of the method selector. */
30045 cp_parser_objc_selector_expression (cp_parser
* parser
)
30047 tree sel_seq
= NULL_TREE
;
30048 bool maybe_unary_selector_p
= true;
30050 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30052 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30053 matching_parens parens
;
30054 parens
.require_open (parser
);
30055 token
= cp_lexer_peek_token (parser
->lexer
);
30057 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30058 || token
->type
== CPP_SCOPE
)
30060 tree selector
= NULL_TREE
;
30062 if (token
->type
!= CPP_COLON
30063 || token
->type
== CPP_SCOPE
)
30064 selector
= cp_parser_objc_selector (parser
);
30066 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30067 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30069 /* Detect if we have a unary selector. */
30070 if (maybe_unary_selector_p
)
30072 sel_seq
= selector
;
30073 goto finish_selector
;
30077 cp_parser_error (parser
, "expected %<:%>");
30080 maybe_unary_selector_p
= false;
30081 token
= cp_lexer_consume_token (parser
->lexer
);
30083 if (token
->type
== CPP_SCOPE
)
30086 = chainon (sel_seq
,
30087 build_tree_list (selector
, NULL_TREE
));
30089 = chainon (sel_seq
,
30090 build_tree_list (NULL_TREE
, NULL_TREE
));
30094 = chainon (sel_seq
,
30095 build_tree_list (selector
, NULL_TREE
));
30097 token
= cp_lexer_peek_token (parser
->lexer
);
30101 parens
.require_close (parser
);
30104 /* Build a location of the form:
30107 with caret==start at the @ token, finishing at the close paren. */
30108 location_t combined_loc
30109 = make_location (loc
, loc
,
30110 cp_lexer_previous_token (parser
->lexer
)->location
);
30111 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30112 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30113 protected_set_expr_location (result
, combined_loc
);
30117 /* Parse a list of identifiers.
30119 objc-identifier-list:
30121 objc-identifier-list , identifier
30123 Returns a TREE_LIST of identifier nodes. */
30126 cp_parser_objc_identifier_list (cp_parser
* parser
)
30132 identifier
= cp_parser_identifier (parser
);
30133 if (identifier
== error_mark_node
)
30134 return error_mark_node
;
30136 list
= build_tree_list (NULL_TREE
, identifier
);
30137 sep
= cp_lexer_peek_token (parser
->lexer
);
30139 while (sep
->type
== CPP_COMMA
)
30141 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30142 identifier
= cp_parser_identifier (parser
);
30143 if (identifier
== error_mark_node
)
30146 list
= chainon (list
, build_tree_list (NULL_TREE
,
30148 sep
= cp_lexer_peek_token (parser
->lexer
);
30154 /* Parse an Objective-C alias declaration.
30156 objc-alias-declaration:
30157 @compatibility_alias identifier identifier ;
30159 This function registers the alias mapping with the Objective-C front end.
30160 It returns nothing. */
30163 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30167 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30168 alias
= cp_parser_identifier (parser
);
30169 orig
= cp_parser_identifier (parser
);
30170 objc_declare_alias (alias
, orig
);
30171 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30174 /* Parse an Objective-C class forward-declaration.
30176 objc-class-declaration:
30177 @class objc-identifier-list ;
30179 The function registers the forward declarations with the Objective-C
30180 front end. It returns nothing. */
30183 cp_parser_objc_class_declaration (cp_parser
* parser
)
30185 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30190 id
= cp_parser_identifier (parser
);
30191 if (id
== error_mark_node
)
30194 objc_declare_class (id
);
30196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30197 cp_lexer_consume_token (parser
->lexer
);
30201 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30204 /* Parse a list of Objective-C protocol references.
30206 objc-protocol-refs-opt:
30207 objc-protocol-refs [opt]
30209 objc-protocol-refs:
30210 < objc-identifier-list >
30212 Returns a TREE_LIST of identifiers, if any. */
30215 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30217 tree protorefs
= NULL_TREE
;
30219 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30221 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30222 protorefs
= cp_parser_objc_identifier_list (parser
);
30223 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30229 /* Parse a Objective-C visibility specification. */
30232 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30234 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30236 switch (vis
->keyword
)
30238 case RID_AT_PRIVATE
:
30239 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30241 case RID_AT_PROTECTED
:
30242 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30244 case RID_AT_PUBLIC
:
30245 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30247 case RID_AT_PACKAGE
:
30248 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30254 /* Eat '@private'/'@protected'/'@public'. */
30255 cp_lexer_consume_token (parser
->lexer
);
30258 /* Parse an Objective-C method type. Return 'true' if it is a class
30259 (+) method, and 'false' if it is an instance (-) method. */
30262 cp_parser_objc_method_type (cp_parser
* parser
)
30264 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30270 /* Parse an Objective-C protocol qualifier. */
30273 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30275 tree quals
= NULL_TREE
, node
;
30276 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30278 node
= token
->u
.value
;
30280 while (node
&& identifier_p (node
)
30281 && (node
== ridpointers
[(int) RID_IN
]
30282 || node
== ridpointers
[(int) RID_OUT
]
30283 || node
== ridpointers
[(int) RID_INOUT
]
30284 || node
== ridpointers
[(int) RID_BYCOPY
]
30285 || node
== ridpointers
[(int) RID_BYREF
]
30286 || node
== ridpointers
[(int) RID_ONEWAY
]))
30288 quals
= tree_cons (NULL_TREE
, node
, quals
);
30289 cp_lexer_consume_token (parser
->lexer
);
30290 token
= cp_lexer_peek_token (parser
->lexer
);
30291 node
= token
->u
.value
;
30297 /* Parse an Objective-C typename. */
30300 cp_parser_objc_typename (cp_parser
* parser
)
30302 tree type_name
= NULL_TREE
;
30304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30306 tree proto_quals
, cp_type
= NULL_TREE
;
30308 matching_parens parens
;
30309 parens
.consume_open (parser
); /* Eat '('. */
30310 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30312 /* An ObjC type name may consist of just protocol qualifiers, in which
30313 case the type shall default to 'id'. */
30314 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30316 cp_type
= cp_parser_type_id (parser
);
30318 /* If the type could not be parsed, an error has already
30319 been produced. For error recovery, behave as if it had
30320 not been specified, which will use the default type
30322 if (cp_type
== error_mark_node
)
30324 cp_type
= NULL_TREE
;
30325 /* We need to skip to the closing parenthesis as
30326 cp_parser_type_id() does not seem to do it for
30328 cp_parser_skip_to_closing_parenthesis (parser
,
30329 /*recovering=*/true,
30330 /*or_comma=*/false,
30331 /*consume_paren=*/false);
30335 parens
.require_close (parser
);
30336 type_name
= build_tree_list (proto_quals
, cp_type
);
30342 /* Check to see if TYPE refers to an Objective-C selector name. */
30345 cp_parser_objc_selector_p (enum cpp_ttype type
)
30347 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30348 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30349 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30350 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30351 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30354 /* Parse an Objective-C selector. */
30357 cp_parser_objc_selector (cp_parser
* parser
)
30359 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30361 if (!cp_parser_objc_selector_p (token
->type
))
30363 error_at (token
->location
, "invalid Objective-C++ selector name");
30364 return error_mark_node
;
30367 /* C++ operator names are allowed to appear in ObjC selectors. */
30368 switch (token
->type
)
30370 case CPP_AND_AND
: return get_identifier ("and");
30371 case CPP_AND_EQ
: return get_identifier ("and_eq");
30372 case CPP_AND
: return get_identifier ("bitand");
30373 case CPP_OR
: return get_identifier ("bitor");
30374 case CPP_COMPL
: return get_identifier ("compl");
30375 case CPP_NOT
: return get_identifier ("not");
30376 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30377 case CPP_OR_OR
: return get_identifier ("or");
30378 case CPP_OR_EQ
: return get_identifier ("or_eq");
30379 case CPP_XOR
: return get_identifier ("xor");
30380 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30381 default: return token
->u
.value
;
30385 /* Parse an Objective-C params list. */
30388 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30390 tree params
= NULL_TREE
;
30391 bool maybe_unary_selector_p
= true;
30392 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30394 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30396 tree selector
= NULL_TREE
, type_name
, identifier
;
30397 tree parm_attr
= NULL_TREE
;
30399 if (token
->keyword
== RID_ATTRIBUTE
)
30402 if (token
->type
!= CPP_COLON
)
30403 selector
= cp_parser_objc_selector (parser
);
30405 /* Detect if we have a unary selector. */
30406 if (maybe_unary_selector_p
30407 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30409 params
= selector
; /* Might be followed by attributes. */
30413 maybe_unary_selector_p
= false;
30414 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30416 /* Something went quite wrong. There should be a colon
30417 here, but there is not. Stop parsing parameters. */
30420 type_name
= cp_parser_objc_typename (parser
);
30421 /* New ObjC allows attributes on parameters too. */
30422 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30423 parm_attr
= cp_parser_attributes_opt (parser
);
30424 identifier
= cp_parser_identifier (parser
);
30428 objc_build_keyword_decl (selector
,
30433 token
= cp_lexer_peek_token (parser
->lexer
);
30436 if (params
== NULL_TREE
)
30438 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30439 return error_mark_node
;
30442 /* We allow tail attributes for the method. */
30443 if (token
->keyword
== RID_ATTRIBUTE
)
30445 *attributes
= cp_parser_attributes_opt (parser
);
30446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30447 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30449 cp_parser_error (parser
,
30450 "method attributes must be specified at the end");
30451 return error_mark_node
;
30454 if (params
== NULL_TREE
)
30456 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30457 return error_mark_node
;
30462 /* Parse the non-keyword Objective-C params. */
30465 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30468 tree params
= make_node (TREE_LIST
);
30469 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30470 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30472 while (token
->type
== CPP_COMMA
)
30474 cp_parameter_declarator
*parmdecl
;
30477 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30478 token
= cp_lexer_peek_token (parser
->lexer
);
30480 if (token
->type
== CPP_ELLIPSIS
)
30482 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30484 token
= cp_lexer_peek_token (parser
->lexer
);
30488 /* TODO: parse attributes for tail parameters. */
30489 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30490 parm
= grokdeclarator (parmdecl
->declarator
,
30491 &parmdecl
->decl_specifiers
,
30492 PARM
, /*initialized=*/0,
30493 /*attrlist=*/NULL
);
30495 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30496 token
= cp_lexer_peek_token (parser
->lexer
);
30499 /* We allow tail attributes for the method. */
30500 if (token
->keyword
== RID_ATTRIBUTE
)
30502 if (*attributes
== NULL_TREE
)
30504 *attributes
= cp_parser_attributes_opt (parser
);
30505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30506 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30510 /* We have an error, but parse the attributes, so that we can
30512 *attributes
= cp_parser_attributes_opt (parser
);
30514 cp_parser_error (parser
,
30515 "method attributes must be specified at the end");
30516 return error_mark_node
;
30522 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30525 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30527 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30529 /* If the next token is `extern' and the following token is a string
30530 literal, then we have a linkage specification. */
30531 if (token
->keyword
== RID_EXTERN
30532 && cp_parser_is_pure_string_literal
30533 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30534 cp_parser_linkage_specification (parser
);
30535 /* Handle #pragma, if any. */
30536 else if (token
->type
== CPP_PRAGMA
)
30537 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30538 /* Allow stray semicolons. */
30539 else if (token
->type
== CPP_SEMICOLON
)
30540 cp_lexer_consume_token (parser
->lexer
);
30541 /* Mark methods as optional or required, when building protocols. */
30542 else if (token
->keyword
== RID_AT_OPTIONAL
)
30544 cp_lexer_consume_token (parser
->lexer
);
30545 objc_set_method_opt (true);
30547 else if (token
->keyword
== RID_AT_REQUIRED
)
30549 cp_lexer_consume_token (parser
->lexer
);
30550 objc_set_method_opt (false);
30552 else if (token
->keyword
== RID_NAMESPACE
)
30553 cp_parser_namespace_definition (parser
);
30554 /* Other stray characters must generate errors. */
30555 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30557 cp_lexer_consume_token (parser
->lexer
);
30558 error ("stray %qs between Objective-C++ methods",
30559 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30561 /* Finally, try to parse a block-declaration, or a function-definition. */
30563 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30566 /* Parse a method signature. */
30569 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30571 tree rettype
, kwdparms
, optparms
;
30572 bool ellipsis
= false;
30573 bool is_class_method
;
30575 is_class_method
= cp_parser_objc_method_type (parser
);
30576 rettype
= cp_parser_objc_typename (parser
);
30577 *attributes
= NULL_TREE
;
30578 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30579 if (kwdparms
== error_mark_node
)
30580 return error_mark_node
;
30581 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30582 if (optparms
== error_mark_node
)
30583 return error_mark_node
;
30585 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30589 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30592 cp_lexer_save_tokens (parser
->lexer
);
30593 tattr
= cp_parser_attributes_opt (parser
);
30594 gcc_assert (tattr
) ;
30596 /* If the attributes are followed by a method introducer, this is not allowed.
30597 Dump the attributes and flag the situation. */
30598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30599 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30602 /* Otherwise, the attributes introduce some interstitial code, possibly so
30603 rewind to allow that check. */
30604 cp_lexer_rollback_tokens (parser
->lexer
);
30608 /* Parse an Objective-C method prototype list. */
30611 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30613 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30615 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30617 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30619 tree attributes
, sig
;
30620 bool is_class_method
;
30621 if (token
->type
== CPP_PLUS
)
30622 is_class_method
= true;
30624 is_class_method
= false;
30625 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30626 if (sig
== error_mark_node
)
30628 cp_parser_skip_to_end_of_block_or_statement (parser
);
30629 token
= cp_lexer_peek_token (parser
->lexer
);
30632 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30633 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30635 else if (token
->keyword
== RID_AT_PROPERTY
)
30636 cp_parser_objc_at_property_declaration (parser
);
30637 else if (token
->keyword
== RID_ATTRIBUTE
30638 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30639 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30641 "prefix attributes are ignored for methods");
30643 /* Allow for interspersed non-ObjC++ code. */
30644 cp_parser_objc_interstitial_code (parser
);
30646 token
= cp_lexer_peek_token (parser
->lexer
);
30649 if (token
->type
!= CPP_EOF
)
30650 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30652 cp_parser_error (parser
, "expected %<@end%>");
30654 objc_finish_interface ();
30657 /* Parse an Objective-C method definition list. */
30660 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30662 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30664 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30668 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30671 tree sig
, attribute
;
30672 bool is_class_method
;
30673 if (token
->type
== CPP_PLUS
)
30674 is_class_method
= true;
30676 is_class_method
= false;
30677 push_deferring_access_checks (dk_deferred
);
30678 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30679 if (sig
== error_mark_node
)
30681 cp_parser_skip_to_end_of_block_or_statement (parser
);
30682 token
= cp_lexer_peek_token (parser
->lexer
);
30685 objc_start_method_definition (is_class_method
, sig
, attribute
,
30688 /* For historical reasons, we accept an optional semicolon. */
30689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30690 cp_lexer_consume_token (parser
->lexer
);
30692 ptk
= cp_lexer_peek_token (parser
->lexer
);
30693 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30694 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30696 perform_deferred_access_checks (tf_warning_or_error
);
30697 stop_deferring_access_checks ();
30698 meth
= cp_parser_function_definition_after_declarator (parser
,
30700 pop_deferring_access_checks ();
30701 objc_finish_method_definition (meth
);
30704 /* The following case will be removed once @synthesize is
30705 completely implemented. */
30706 else if (token
->keyword
== RID_AT_PROPERTY
)
30707 cp_parser_objc_at_property_declaration (parser
);
30708 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30709 cp_parser_objc_at_synthesize_declaration (parser
);
30710 else if (token
->keyword
== RID_AT_DYNAMIC
)
30711 cp_parser_objc_at_dynamic_declaration (parser
);
30712 else if (token
->keyword
== RID_ATTRIBUTE
30713 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30714 warning_at (token
->location
, OPT_Wattributes
,
30715 "prefix attributes are ignored for methods");
30717 /* Allow for interspersed non-ObjC++ code. */
30718 cp_parser_objc_interstitial_code (parser
);
30720 token
= cp_lexer_peek_token (parser
->lexer
);
30723 if (token
->type
!= CPP_EOF
)
30724 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30726 cp_parser_error (parser
, "expected %<@end%>");
30728 objc_finish_implementation ();
30731 /* Parse Objective-C ivars. */
30734 cp_parser_objc_class_ivars (cp_parser
* parser
)
30736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30738 if (token
->type
!= CPP_OPEN_BRACE
)
30739 return; /* No ivars specified. */
30741 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30742 token
= cp_lexer_peek_token (parser
->lexer
);
30744 while (token
->type
!= CPP_CLOSE_BRACE
30745 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30747 cp_decl_specifier_seq declspecs
;
30748 int decl_class_or_enum_p
;
30749 tree prefix_attributes
;
30751 cp_parser_objc_visibility_spec (parser
);
30753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30756 cp_parser_decl_specifier_seq (parser
,
30757 CP_PARSER_FLAGS_OPTIONAL
,
30759 &decl_class_or_enum_p
);
30761 /* auto, register, static, extern, mutable. */
30762 if (declspecs
.storage_class
!= sc_none
)
30764 cp_parser_error (parser
, "invalid type for instance variable");
30765 declspecs
.storage_class
= sc_none
;
30768 /* thread_local. */
30769 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30771 cp_parser_error (parser
, "invalid type for instance variable");
30772 declspecs
.locations
[ds_thread
] = 0;
30776 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30778 cp_parser_error (parser
, "invalid type for instance variable");
30779 declspecs
.locations
[ds_typedef
] = 0;
30782 prefix_attributes
= declspecs
.attributes
;
30783 declspecs
.attributes
= NULL_TREE
;
30785 /* Keep going until we hit the `;' at the end of the
30787 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30789 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30790 cp_declarator
*declarator
= NULL
;
30791 int ctor_dtor_or_conv_p
;
30793 /* Check for a (possibly unnamed) bitfield declaration. */
30794 token
= cp_lexer_peek_token (parser
->lexer
);
30795 if (token
->type
== CPP_COLON
)
30798 if (token
->type
== CPP_NAME
30799 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30802 /* Get the name of the bitfield. */
30803 declarator
= make_id_declarator (NULL_TREE
,
30804 cp_parser_identifier (parser
),
30805 sfk_none
, token
->location
);
30808 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30809 /* Get the width of the bitfield. */
30811 = cp_parser_constant_expression (parser
);
30815 /* Parse the declarator. */
30817 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30818 &ctor_dtor_or_conv_p
,
30819 /*parenthesized_p=*/NULL
,
30820 /*member_p=*/false,
30821 /*friend_p=*/false);
30824 /* Look for attributes that apply to the ivar. */
30825 attributes
= cp_parser_attributes_opt (parser
);
30826 /* Remember which attributes are prefix attributes and
30828 first_attribute
= attributes
;
30829 /* Combine the attributes. */
30830 attributes
= attr_chainon (prefix_attributes
, attributes
);
30833 /* Create the bitfield declaration. */
30834 decl
= grokbitfield (declarator
, &declspecs
,
30835 width
, NULL_TREE
, attributes
);
30837 decl
= grokfield (declarator
, &declspecs
,
30838 NULL_TREE
, /*init_const_expr_p=*/false,
30839 NULL_TREE
, attributes
);
30841 /* Add the instance variable. */
30842 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30843 objc_add_instance_variable (decl
);
30845 /* Reset PREFIX_ATTRIBUTES. */
30846 if (attributes
!= error_mark_node
)
30848 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30849 attributes
= TREE_CHAIN (attributes
);
30851 TREE_CHAIN (attributes
) = NULL_TREE
;
30854 token
= cp_lexer_peek_token (parser
->lexer
);
30856 if (token
->type
== CPP_COMMA
)
30858 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30864 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30865 token
= cp_lexer_peek_token (parser
->lexer
);
30868 if (token
->keyword
== RID_AT_END
)
30869 cp_parser_error (parser
, "expected %<}%>");
30871 /* Do not consume the RID_AT_END, so it will be read again as terminating
30872 the @interface of @implementation. */
30873 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30874 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30876 /* For historical reasons, we accept an optional semicolon. */
30877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30878 cp_lexer_consume_token (parser
->lexer
);
30881 /* Parse an Objective-C protocol declaration. */
30884 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30886 tree proto
, protorefs
;
30889 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30890 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30892 tok
= cp_lexer_peek_token (parser
->lexer
);
30893 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30894 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30898 /* See if we have a forward declaration or a definition. */
30899 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30901 /* Try a forward declaration first. */
30902 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30908 id
= cp_parser_identifier (parser
);
30909 if (id
== error_mark_node
)
30912 objc_declare_protocol (id
, attributes
);
30914 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30915 cp_lexer_consume_token (parser
->lexer
);
30919 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30922 /* Ok, we got a full-fledged definition (or at least should). */
30925 proto
= cp_parser_identifier (parser
);
30926 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30927 objc_start_protocol (proto
, protorefs
, attributes
);
30928 cp_parser_objc_method_prototype_list (parser
);
30932 /* Parse an Objective-C superclass or category. */
30935 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30938 tree
*categ
, bool *is_class_extension
)
30940 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30942 *super
= *categ
= NULL_TREE
;
30943 *is_class_extension
= false;
30944 if (next
->type
== CPP_COLON
)
30946 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30947 *super
= cp_parser_identifier (parser
);
30949 else if (next
->type
== CPP_OPEN_PAREN
)
30951 matching_parens parens
;
30952 parens
.consume_open (parser
); /* Eat '('. */
30954 /* If there is no category name, and this is an @interface, we
30955 have a class extension. */
30956 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30958 *categ
= NULL_TREE
;
30959 *is_class_extension
= true;
30962 *categ
= cp_parser_identifier (parser
);
30964 parens
.require_close (parser
);
30968 /* Parse an Objective-C class interface. */
30971 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30973 tree name
, super
, categ
, protos
;
30974 bool is_class_extension
;
30976 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30977 name
= cp_parser_identifier (parser
);
30978 if (name
== error_mark_node
)
30980 /* It's hard to recover because even if valid @interface stuff
30981 is to follow, we can't compile it (or validate it) if we
30982 don't even know which class it refers to. Let's assume this
30983 was a stray '@interface' token in the stream and skip it.
30987 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30988 &is_class_extension
);
30989 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30991 /* We have either a class or a category on our hands. */
30992 if (categ
|| is_class_extension
)
30993 objc_start_category_interface (name
, categ
, protos
, attributes
);
30996 objc_start_class_interface (name
, super
, protos
, attributes
);
30997 /* Handle instance variable declarations, if any. */
30998 cp_parser_objc_class_ivars (parser
);
30999 objc_continue_interface ();
31002 cp_parser_objc_method_prototype_list (parser
);
31005 /* Parse an Objective-C class implementation. */
31008 cp_parser_objc_class_implementation (cp_parser
* parser
)
31010 tree name
, super
, categ
;
31011 bool is_class_extension
;
31013 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31014 name
= cp_parser_identifier (parser
);
31015 if (name
== error_mark_node
)
31017 /* It's hard to recover because even if valid @implementation
31018 stuff is to follow, we can't compile it (or validate it) if
31019 we don't even know which class it refers to. Let's assume
31020 this was a stray '@implementation' token in the stream and
31025 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31026 &is_class_extension
);
31028 /* We have either a class or a category on our hands. */
31030 objc_start_category_implementation (name
, categ
);
31033 objc_start_class_implementation (name
, super
);
31034 /* Handle instance variable declarations, if any. */
31035 cp_parser_objc_class_ivars (parser
);
31036 objc_continue_implementation ();
31039 cp_parser_objc_method_definition_list (parser
);
31042 /* Consume the @end token and finish off the implementation. */
31045 cp_parser_objc_end_implementation (cp_parser
* parser
)
31047 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31048 objc_finish_implementation ();
31051 /* Parse an Objective-C declaration. */
31054 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31056 /* Try to figure out what kind of declaration is present. */
31057 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31060 switch (kwd
->keyword
)
31065 error_at (kwd
->location
, "attributes may not be specified before"
31066 " the %<@%D%> Objective-C++ keyword",
31070 case RID_AT_IMPLEMENTATION
:
31071 warning_at (kwd
->location
, OPT_Wattributes
,
31072 "prefix attributes are ignored before %<@%D%>",
31079 switch (kwd
->keyword
)
31082 cp_parser_objc_alias_declaration (parser
);
31085 cp_parser_objc_class_declaration (parser
);
31087 case RID_AT_PROTOCOL
:
31088 cp_parser_objc_protocol_declaration (parser
, attributes
);
31090 case RID_AT_INTERFACE
:
31091 cp_parser_objc_class_interface (parser
, attributes
);
31093 case RID_AT_IMPLEMENTATION
:
31094 cp_parser_objc_class_implementation (parser
);
31097 cp_parser_objc_end_implementation (parser
);
31100 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31102 cp_parser_skip_to_end_of_block_or_statement (parser
);
31106 /* Parse an Objective-C try-catch-finally statement.
31108 objc-try-catch-finally-stmt:
31109 @try compound-statement objc-catch-clause-seq [opt]
31110 objc-finally-clause [opt]
31112 objc-catch-clause-seq:
31113 objc-catch-clause objc-catch-clause-seq [opt]
31116 @catch ( objc-exception-declaration ) compound-statement
31118 objc-finally-clause:
31119 @finally compound-statement
31121 objc-exception-declaration:
31122 parameter-declaration
31125 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31129 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31130 for C. Keep them in sync. */
31133 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31135 location_t location
;
31138 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31139 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31140 objc_maybe_warn_exceptions (location
);
31141 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31142 node, lest it get absorbed into the surrounding block. */
31143 stmt
= push_stmt_list ();
31144 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31145 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31147 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31149 cp_parameter_declarator
*parm
;
31150 tree parameter_declaration
= error_mark_node
;
31151 bool seen_open_paren
= false;
31152 matching_parens parens
;
31154 cp_lexer_consume_token (parser
->lexer
);
31155 if (parens
.require_open (parser
))
31156 seen_open_paren
= true;
31157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31159 /* We have "@catch (...)" (where the '...' are literally
31160 what is in the code). Skip the '...'.
31161 parameter_declaration is set to NULL_TREE, and
31162 objc_being_catch_clauses() knows that that means
31164 cp_lexer_consume_token (parser
->lexer
);
31165 parameter_declaration
= NULL_TREE
;
31169 /* We have "@catch (NSException *exception)" or something
31170 like that. Parse the parameter declaration. */
31171 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
31173 parameter_declaration
= error_mark_node
;
31175 parameter_declaration
= grokdeclarator (parm
->declarator
,
31176 &parm
->decl_specifiers
,
31177 PARM
, /*initialized=*/0,
31178 /*attrlist=*/NULL
);
31180 if (seen_open_paren
)
31181 parens
.require_close (parser
);
31184 /* If there was no open parenthesis, we are recovering from
31185 an error, and we are trying to figure out what mistake
31186 the user has made. */
31188 /* If there is an immediate closing parenthesis, the user
31189 probably forgot the opening one (ie, they typed "@catch
31190 NSException *e)". Parse the closing parenthesis and keep
31192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31193 cp_lexer_consume_token (parser
->lexer
);
31195 /* If these is no immediate closing parenthesis, the user
31196 probably doesn't know that parenthesis are required at
31197 all (ie, they typed "@catch NSException *e"). So, just
31198 forget about the closing parenthesis and keep going. */
31200 objc_begin_catch_clause (parameter_declaration
);
31201 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31202 objc_finish_catch_clause ();
31204 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31206 cp_lexer_consume_token (parser
->lexer
);
31207 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31208 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31209 node, lest it get absorbed into the surrounding block. */
31210 stmt
= push_stmt_list ();
31211 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31212 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31215 return objc_finish_try_stmt ();
31218 /* Parse an Objective-C synchronized statement.
31220 objc-synchronized-stmt:
31221 @synchronized ( expression ) compound-statement
31223 Returns NULL_TREE. */
31226 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31228 location_t location
;
31231 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31233 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31234 objc_maybe_warn_exceptions (location
);
31235 matching_parens parens
;
31236 parens
.require_open (parser
);
31237 lock
= cp_parser_expression (parser
);
31238 parens
.require_close (parser
);
31240 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31241 node, lest it get absorbed into the surrounding block. */
31242 stmt
= push_stmt_list ();
31243 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31245 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31248 /* Parse an Objective-C throw statement.
31251 @throw assignment-expression [opt] ;
31253 Returns a constructed '@throw' statement. */
31256 cp_parser_objc_throw_statement (cp_parser
*parser
)
31258 tree expr
= NULL_TREE
;
31259 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31261 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31263 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31264 expr
= cp_parser_expression (parser
);
31266 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31268 return objc_build_throw_stmt (loc
, expr
);
31271 /* Parse an Objective-C statement. */
31274 cp_parser_objc_statement (cp_parser
* parser
)
31276 /* Try to figure out what kind of declaration is present. */
31277 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31279 switch (kwd
->keyword
)
31282 return cp_parser_objc_try_catch_finally_statement (parser
);
31283 case RID_AT_SYNCHRONIZED
:
31284 return cp_parser_objc_synchronized_statement (parser
);
31286 return cp_parser_objc_throw_statement (parser
);
31288 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31290 cp_parser_skip_to_end_of_block_or_statement (parser
);
31293 return error_mark_node
;
31296 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31297 look ahead to see if an objc keyword follows the attributes. This
31298 is to detect the use of prefix attributes on ObjC @interface and
31302 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31304 cp_lexer_save_tokens (parser
->lexer
);
31305 *attrib
= cp_parser_attributes_opt (parser
);
31306 gcc_assert (*attrib
);
31307 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31309 cp_lexer_commit_tokens (parser
->lexer
);
31312 cp_lexer_rollback_tokens (parser
->lexer
);
31316 /* This routine is a minimal replacement for
31317 c_parser_struct_declaration () used when parsing the list of
31318 types/names or ObjC++ properties. For example, when parsing the
31321 @property (readonly) int a, b, c;
31323 this function is responsible for parsing "int a, int b, int c" and
31324 returning the declarations as CHAIN of DECLs.
31326 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31327 similar parsing. */
31329 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31331 tree decls
= NULL_TREE
;
31332 cp_decl_specifier_seq declspecs
;
31333 int decl_class_or_enum_p
;
31334 tree prefix_attributes
;
31336 cp_parser_decl_specifier_seq (parser
,
31337 CP_PARSER_FLAGS_NONE
,
31339 &decl_class_or_enum_p
);
31341 if (declspecs
.type
== error_mark_node
)
31342 return error_mark_node
;
31344 /* auto, register, static, extern, mutable. */
31345 if (declspecs
.storage_class
!= sc_none
)
31347 cp_parser_error (parser
, "invalid type for property");
31348 declspecs
.storage_class
= sc_none
;
31351 /* thread_local. */
31352 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31354 cp_parser_error (parser
, "invalid type for property");
31355 declspecs
.locations
[ds_thread
] = 0;
31359 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31361 cp_parser_error (parser
, "invalid type for property");
31362 declspecs
.locations
[ds_typedef
] = 0;
31365 prefix_attributes
= declspecs
.attributes
;
31366 declspecs
.attributes
= NULL_TREE
;
31368 /* Keep going until we hit the `;' at the end of the declaration. */
31369 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31371 tree attributes
, first_attribute
, decl
;
31372 cp_declarator
*declarator
;
31375 /* Parse the declarator. */
31376 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31377 NULL
, NULL
, false, false);
31379 /* Look for attributes that apply to the ivar. */
31380 attributes
= cp_parser_attributes_opt (parser
);
31381 /* Remember which attributes are prefix attributes and
31383 first_attribute
= attributes
;
31384 /* Combine the attributes. */
31385 attributes
= attr_chainon (prefix_attributes
, attributes
);
31387 decl
= grokfield (declarator
, &declspecs
,
31388 NULL_TREE
, /*init_const_expr_p=*/false,
31389 NULL_TREE
, attributes
);
31391 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31392 return error_mark_node
;
31394 /* Reset PREFIX_ATTRIBUTES. */
31395 if (attributes
!= error_mark_node
)
31397 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31398 attributes
= TREE_CHAIN (attributes
);
31400 TREE_CHAIN (attributes
) = NULL_TREE
;
31403 DECL_CHAIN (decl
) = decls
;
31406 token
= cp_lexer_peek_token (parser
->lexer
);
31407 if (token
->type
== CPP_COMMA
)
31409 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31418 /* Parse an Objective-C @property declaration. The syntax is:
31420 objc-property-declaration:
31421 '@property' objc-property-attributes[opt] struct-declaration ;
31423 objc-property-attributes:
31424 '(' objc-property-attribute-list ')'
31426 objc-property-attribute-list:
31427 objc-property-attribute
31428 objc-property-attribute-list, objc-property-attribute
31430 objc-property-attribute
31431 'getter' = identifier
31432 'setter' = identifier
31441 @property NSString *name;
31442 @property (readonly) id object;
31443 @property (retain, nonatomic, getter=getTheName) id name;
31444 @property int a, b, c;
31446 PS: This function is identical to
31447 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31449 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31451 /* The following variables hold the attributes of the properties as
31452 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31453 seen. When we see an attribute, we set them to 'true' (if they
31454 are boolean properties) or to the identifier (if they have an
31455 argument, ie, for getter and setter). Note that here we only
31456 parse the list of attributes, check the syntax and accumulate the
31457 attributes that we find. objc_add_property_declaration() will
31458 then process the information. */
31459 bool property_assign
= false;
31460 bool property_copy
= false;
31461 tree property_getter_ident
= NULL_TREE
;
31462 bool property_nonatomic
= false;
31463 bool property_readonly
= false;
31464 bool property_readwrite
= false;
31465 bool property_retain
= false;
31466 tree property_setter_ident
= NULL_TREE
;
31468 /* 'properties' is the list of properties that we read. Usually a
31469 single one, but maybe more (eg, in "@property int a, b, c;" there
31474 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31476 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31478 /* Parse the optional attribute list... */
31479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31482 matching_parens parens
;
31483 parens
.consume_open (parser
);
31487 bool syntax_error
= false;
31488 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31491 if (token
->type
!= CPP_NAME
)
31493 cp_parser_error (parser
, "expected identifier");
31496 keyword
= C_RID_CODE (token
->u
.value
);
31497 cp_lexer_consume_token (parser
->lexer
);
31500 case RID_ASSIGN
: property_assign
= true; break;
31501 case RID_COPY
: property_copy
= true; break;
31502 case RID_NONATOMIC
: property_nonatomic
= true; break;
31503 case RID_READONLY
: property_readonly
= true; break;
31504 case RID_READWRITE
: property_readwrite
= true; break;
31505 case RID_RETAIN
: property_retain
= true; break;
31509 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31511 if (keyword
== RID_GETTER
)
31512 cp_parser_error (parser
,
31513 "missing %<=%> (after %<getter%> attribute)");
31515 cp_parser_error (parser
,
31516 "missing %<=%> (after %<setter%> attribute)");
31517 syntax_error
= true;
31520 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31521 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31523 cp_parser_error (parser
, "expected identifier");
31524 syntax_error
= true;
31527 if (keyword
== RID_SETTER
)
31529 if (property_setter_ident
!= NULL_TREE
)
31531 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31532 cp_lexer_consume_token (parser
->lexer
);
31535 property_setter_ident
= cp_parser_objc_selector (parser
);
31536 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31537 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31539 cp_lexer_consume_token (parser
->lexer
);
31543 if (property_getter_ident
!= NULL_TREE
)
31545 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31546 cp_lexer_consume_token (parser
->lexer
);
31549 property_getter_ident
= cp_parser_objc_selector (parser
);
31553 cp_parser_error (parser
, "unknown property attribute");
31554 syntax_error
= true;
31561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31562 cp_lexer_consume_token (parser
->lexer
);
31567 /* FIXME: "@property (setter, assign);" will generate a spurious
31568 "error: expected ‘)’ before ‘,’ token". This is because
31569 cp_parser_require, unlike the C counterpart, will produce an
31570 error even if we are in error recovery. */
31571 if (!parens
.require_close (parser
))
31573 cp_parser_skip_to_closing_parenthesis (parser
,
31574 /*recovering=*/true,
31575 /*or_comma=*/false,
31576 /*consume_paren=*/true);
31580 /* ... and the property declaration(s). */
31581 properties
= cp_parser_objc_struct_declaration (parser
);
31583 if (properties
== error_mark_node
)
31585 cp_parser_skip_to_end_of_statement (parser
);
31586 /* If the next token is now a `;', consume it. */
31587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31588 cp_lexer_consume_token (parser
->lexer
);
31592 if (properties
== NULL_TREE
)
31593 cp_parser_error (parser
, "expected identifier");
31596 /* Comma-separated properties are chained together in
31597 reverse order; add them one by one. */
31598 properties
= nreverse (properties
);
31600 for (; properties
; properties
= TREE_CHAIN (properties
))
31601 objc_add_property_declaration (loc
, copy_node (properties
),
31602 property_readonly
, property_readwrite
,
31603 property_assign
, property_retain
,
31604 property_copy
, property_nonatomic
,
31605 property_getter_ident
, property_setter_ident
);
31608 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31611 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31613 objc-synthesize-declaration:
31614 @synthesize objc-synthesize-identifier-list ;
31616 objc-synthesize-identifier-list:
31617 objc-synthesize-identifier
31618 objc-synthesize-identifier-list, objc-synthesize-identifier
31620 objc-synthesize-identifier
31622 identifier = identifier
31625 @synthesize MyProperty;
31626 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31628 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31629 for C. Keep them in sync.
31632 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31634 tree list
= NULL_TREE
;
31636 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31638 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31641 tree property
, ivar
;
31642 property
= cp_parser_identifier (parser
);
31643 if (property
== error_mark_node
)
31645 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31650 cp_lexer_consume_token (parser
->lexer
);
31651 ivar
= cp_parser_identifier (parser
);
31652 if (ivar
== error_mark_node
)
31654 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31660 list
= chainon (list
, build_tree_list (ivar
, property
));
31661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31662 cp_lexer_consume_token (parser
->lexer
);
31666 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31667 objc_add_synthesize_declaration (loc
, list
);
31670 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31672 objc-dynamic-declaration:
31673 @dynamic identifier-list ;
31676 @dynamic MyProperty;
31677 @dynamic MyProperty, AnotherProperty;
31679 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31680 for C. Keep them in sync.
31683 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31685 tree list
= NULL_TREE
;
31687 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31689 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31693 property
= cp_parser_identifier (parser
);
31694 if (property
== error_mark_node
)
31696 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31699 list
= chainon (list
, build_tree_list (NULL
, property
));
31700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31701 cp_lexer_consume_token (parser
->lexer
);
31705 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31706 objc_add_dynamic_declaration (loc
, list
);
31710 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31712 /* Returns name of the next clause.
31713 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31714 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31715 returned and the token is consumed. */
31717 static pragma_omp_clause
31718 cp_parser_omp_clause_name (cp_parser
*parser
)
31720 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31722 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31723 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31724 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31725 result
= PRAGMA_OMP_CLAUSE_IF
;
31726 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31727 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31728 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31729 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31730 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31731 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31732 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31733 result
= PRAGMA_OMP_CLAUSE_FOR
;
31734 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31736 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31737 const char *p
= IDENTIFIER_POINTER (id
);
31742 if (!strcmp ("aligned", p
))
31743 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31744 else if (!strcmp ("async", p
))
31745 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31748 if (!strcmp ("collapse", p
))
31749 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31750 else if (!strcmp ("copy", p
))
31751 result
= PRAGMA_OACC_CLAUSE_COPY
;
31752 else if (!strcmp ("copyin", p
))
31753 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31754 else if (!strcmp ("copyout", p
))
31755 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31756 else if (!strcmp ("copyprivate", p
))
31757 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31758 else if (!strcmp ("create", p
))
31759 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31762 if (!strcmp ("defaultmap", p
))
31763 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31764 else if (!strcmp ("depend", p
))
31765 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31766 else if (!strcmp ("device", p
))
31767 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31768 else if (!strcmp ("deviceptr", p
))
31769 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31770 else if (!strcmp ("device_resident", p
))
31771 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31772 else if (!strcmp ("dist_schedule", p
))
31773 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31776 if (!strcmp ("final", p
))
31777 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31778 else if (!strcmp ("finalize", p
))
31779 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31780 else if (!strcmp ("firstprivate", p
))
31781 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31782 else if (!strcmp ("from", p
))
31783 result
= PRAGMA_OMP_CLAUSE_FROM
;
31786 if (!strcmp ("gang", p
))
31787 result
= PRAGMA_OACC_CLAUSE_GANG
;
31788 else if (!strcmp ("grainsize", p
))
31789 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31792 if (!strcmp ("hint", p
))
31793 result
= PRAGMA_OMP_CLAUSE_HINT
;
31794 else if (!strcmp ("host", p
))
31795 result
= PRAGMA_OACC_CLAUSE_HOST
;
31798 if (!strcmp ("if_present", p
))
31799 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31800 else if (!strcmp ("in_reduction", p
))
31801 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
31802 else if (!strcmp ("inbranch", p
))
31803 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31804 else if (!strcmp ("independent", p
))
31805 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31806 else if (!strcmp ("is_device_ptr", p
))
31807 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31810 if (!strcmp ("lastprivate", p
))
31811 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31812 else if (!strcmp ("linear", p
))
31813 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31814 else if (!strcmp ("link", p
))
31815 result
= PRAGMA_OMP_CLAUSE_LINK
;
31818 if (!strcmp ("map", p
))
31819 result
= PRAGMA_OMP_CLAUSE_MAP
;
31820 else if (!strcmp ("mergeable", p
))
31821 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31824 if (!strcmp ("nogroup", p
))
31825 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31826 else if (!strcmp ("nontemporal", p
))
31827 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
31828 else if (!strcmp ("notinbranch", p
))
31829 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31830 else if (!strcmp ("nowait", p
))
31831 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31832 else if (!strcmp ("num_gangs", p
))
31833 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31834 else if (!strcmp ("num_tasks", p
))
31835 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31836 else if (!strcmp ("num_teams", p
))
31837 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31838 else if (!strcmp ("num_threads", p
))
31839 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31840 else if (!strcmp ("num_workers", p
))
31841 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31844 if (!strcmp ("ordered", p
))
31845 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31848 if (!strcmp ("parallel", p
))
31849 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31850 else if (!strcmp ("present", p
))
31851 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31852 else if (!strcmp ("present_or_copy", p
)
31853 || !strcmp ("pcopy", p
))
31854 result
= PRAGMA_OACC_CLAUSE_COPY
;
31855 else if (!strcmp ("present_or_copyin", p
)
31856 || !strcmp ("pcopyin", p
))
31857 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31858 else if (!strcmp ("present_or_copyout", p
)
31859 || !strcmp ("pcopyout", p
))
31860 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31861 else if (!strcmp ("present_or_create", p
)
31862 || !strcmp ("pcreate", p
))
31863 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31864 else if (!strcmp ("priority", p
))
31865 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31866 else if (!strcmp ("proc_bind", p
))
31867 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31870 if (!strcmp ("reduction", p
))
31871 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31874 if (!strcmp ("safelen", p
))
31875 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31876 else if (!strcmp ("schedule", p
))
31877 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31878 else if (!strcmp ("sections", p
))
31879 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31880 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31881 result
= PRAGMA_OACC_CLAUSE_HOST
;
31882 else if (!strcmp ("seq", p
))
31883 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31884 else if (!strcmp ("shared", p
))
31885 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31886 else if (!strcmp ("simd", p
))
31887 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31888 else if (!strcmp ("simdlen", p
))
31889 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31892 if (!strcmp ("task_reduction", p
))
31893 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
31894 else if (!strcmp ("taskgroup", p
))
31895 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31896 else if (!strcmp ("thread_limit", p
))
31897 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31898 else if (!strcmp ("threads", p
))
31899 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31900 else if (!strcmp ("tile", p
))
31901 result
= PRAGMA_OACC_CLAUSE_TILE
;
31902 else if (!strcmp ("to", p
))
31903 result
= PRAGMA_OMP_CLAUSE_TO
;
31906 if (!strcmp ("uniform", p
))
31907 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31908 else if (!strcmp ("untied", p
))
31909 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31910 else if (!strcmp ("use_device", p
))
31911 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31912 else if (!strcmp ("use_device_ptr", p
))
31913 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31916 if (!strcmp ("vector", p
))
31917 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31918 else if (!strcmp ("vector_length", p
))
31919 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31922 if (!strcmp ("wait", p
))
31923 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31924 else if (!strcmp ("worker", p
))
31925 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31930 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31931 cp_lexer_consume_token (parser
->lexer
);
31936 /* Validate that a clause of the given type does not already exist. */
31939 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31940 const char *name
, location_t location
)
31944 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31945 if (OMP_CLAUSE_CODE (c
) == code
)
31947 error_at (location
, "too many %qs clauses", name
);
31955 variable-list , identifier
31957 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31958 colon). An opening parenthesis will have been consumed by the caller.
31960 If KIND is nonzero, create the appropriate node and install the decl
31961 in OMP_CLAUSE_DECL and add the node to the head of the list.
31963 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31964 return the list created.
31966 COLON can be NULL if only closing parenthesis should end the list,
31967 or pointer to bool which will receive false if the list is terminated
31968 by closing parenthesis or true if the list is terminated by colon. */
31971 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31972 tree list
, bool *colon
)
31975 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31978 parser
->colon_corrects_to_scope_p
= false;
31985 if (kind
== OMP_CLAUSE_DEPEND
)
31986 cp_parser_parse_tentatively (parser
);
31987 token
= cp_lexer_peek_token (parser
->lexer
);
31989 && current_class_ptr
31990 && cp_parser_is_keyword (token
, RID_THIS
))
31992 decl
= finish_this_expr ();
31993 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31994 || CONVERT_EXPR_P (decl
))
31995 decl
= TREE_OPERAND (decl
, 0);
31996 cp_lexer_consume_token (parser
->lexer
);
32000 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32001 /*check_dependency_p=*/true,
32002 /*template_p=*/NULL
,
32003 /*declarator_p=*/false,
32004 /*optional_p=*/false);
32005 if (name
== error_mark_node
)
32007 if (kind
== OMP_CLAUSE_DEPEND
32008 && cp_parser_simulate_error (parser
))
32009 goto depend_lvalue
;
32013 if (identifier_p (name
))
32014 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32017 if (decl
== error_mark_node
)
32019 if (kind
== OMP_CLAUSE_DEPEND
32020 && cp_parser_simulate_error (parser
))
32021 goto depend_lvalue
;
32022 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32026 if (decl
== error_mark_node
)
32028 else if (kind
!= 0)
32032 case OMP_CLAUSE__CACHE_
:
32033 /* The OpenACC cache directive explicitly only allows "array
32034 elements or subarrays". */
32035 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32037 error_at (token
->location
, "expected %<[%>");
32038 decl
= error_mark_node
;
32042 case OMP_CLAUSE_MAP
:
32043 case OMP_CLAUSE_FROM
:
32044 case OMP_CLAUSE_TO
:
32045 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32048 = cp_lexer_peek_token (parser
->lexer
)->location
;
32049 cp_id_kind idk
= CP_ID_KIND_NONE
;
32050 cp_lexer_consume_token (parser
->lexer
);
32051 decl
= convert_from_reference (decl
);
32053 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32058 case OMP_CLAUSE_DEPEND
:
32059 case OMP_CLAUSE_REDUCTION
:
32060 case OMP_CLAUSE_IN_REDUCTION
:
32061 case OMP_CLAUSE_TASK_REDUCTION
:
32062 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32064 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32066 parser
->colon_corrects_to_scope_p
= false;
32067 cp_lexer_consume_token (parser
->lexer
);
32068 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32069 low_bound
= cp_parser_expression (parser
);
32071 parser
->colon_corrects_to_scope_p
32072 = saved_colon_corrects_to_scope_p
;
32073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32074 length
= integer_one_node
;
32077 /* Look for `:'. */
32078 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32080 if (kind
== OMP_CLAUSE_DEPEND
32081 && cp_parser_simulate_error (parser
))
32082 goto depend_lvalue
;
32085 if (kind
== OMP_CLAUSE_DEPEND
)
32086 cp_parser_commit_to_tentative_parse (parser
);
32087 if (!cp_lexer_next_token_is (parser
->lexer
,
32089 length
= cp_parser_expression (parser
);
32091 /* Look for the closing `]'. */
32092 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32095 if (kind
== OMP_CLAUSE_DEPEND
32096 && cp_parser_simulate_error (parser
))
32097 goto depend_lvalue
;
32101 decl
= tree_cons (low_bound
, length
, decl
);
32108 if (kind
== OMP_CLAUSE_DEPEND
)
32110 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32111 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32112 && cp_parser_simulate_error (parser
))
32115 cp_parser_abort_tentative_parse (parser
);
32116 decl
= cp_parser_assignment_expression (parser
, NULL
,
32120 cp_parser_parse_definitely (parser
);
32123 tree u
= build_omp_clause (token
->location
, kind
);
32124 OMP_CLAUSE_DECL (u
) = decl
;
32125 OMP_CLAUSE_CHAIN (u
) = list
;
32129 list
= tree_cons (decl
, NULL_TREE
, list
);
32132 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32134 cp_lexer_consume_token (parser
->lexer
);
32138 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32140 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32143 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32147 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32151 /* Try to resync to an unnested comma. Copied from
32152 cp_parser_parenthesized_expression_list. */
32155 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32156 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32157 /*recovering=*/true,
32159 /*consume_paren=*/true);
32167 /* Similarly, but expect leading and trailing parenthesis. This is a very
32168 common case for omp clauses. */
32171 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32173 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32174 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32179 copy ( variable-list )
32180 copyin ( variable-list )
32181 copyout ( variable-list )
32182 create ( variable-list )
32183 delete ( variable-list )
32184 present ( variable-list ) */
32187 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32190 enum gomp_map_kind kind
;
32193 case PRAGMA_OACC_CLAUSE_COPY
:
32194 kind
= GOMP_MAP_TOFROM
;
32196 case PRAGMA_OACC_CLAUSE_COPYIN
:
32197 kind
= GOMP_MAP_TO
;
32199 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32200 kind
= GOMP_MAP_FROM
;
32202 case PRAGMA_OACC_CLAUSE_CREATE
:
32203 kind
= GOMP_MAP_ALLOC
;
32205 case PRAGMA_OACC_CLAUSE_DELETE
:
32206 kind
= GOMP_MAP_RELEASE
;
32208 case PRAGMA_OACC_CLAUSE_DEVICE
:
32209 kind
= GOMP_MAP_FORCE_TO
;
32211 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32212 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32214 case PRAGMA_OACC_CLAUSE_HOST
:
32215 kind
= GOMP_MAP_FORCE_FROM
;
32217 case PRAGMA_OACC_CLAUSE_LINK
:
32218 kind
= GOMP_MAP_LINK
;
32220 case PRAGMA_OACC_CLAUSE_PRESENT
:
32221 kind
= GOMP_MAP_FORCE_PRESENT
;
32224 gcc_unreachable ();
32227 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32229 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32230 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32236 deviceptr ( variable-list ) */
32239 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32241 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32244 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32245 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32246 variable-list must only allow for pointer variables. */
32247 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32248 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32250 tree v
= TREE_PURPOSE (t
);
32251 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32252 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32253 OMP_CLAUSE_DECL (u
) = v
;
32254 OMP_CLAUSE_CHAIN (u
) = list
;
32269 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32270 enum omp_clause_code code
,
32271 tree list
, location_t location
)
32273 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32274 tree c
= build_omp_clause (location
, code
);
32275 OMP_CLAUSE_CHAIN (c
) = list
;
32280 num_gangs ( expression )
32281 num_workers ( expression )
32282 vector_length ( expression ) */
32285 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32286 const char *str
, tree list
)
32288 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32290 matching_parens parens
;
32291 if (!parens
.require_open (parser
))
32294 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32296 if (t
== error_mark_node
32297 || !parens
.require_close (parser
))
32299 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32300 /*or_comma=*/false,
32301 /*consume_paren=*/true);
32305 check_no_duplicate_clause (list
, code
, str
, loc
);
32307 tree c
= build_omp_clause (loc
, code
);
32308 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32309 OMP_CLAUSE_CHAIN (c
) = list
;
32315 gang [( gang-arg-list )]
32316 worker [( [num:] int-expr )]
32317 vector [( [length:] int-expr )]
32319 where gang-arg is one of:
32324 and size-expr may be:
32331 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32332 const char *str
, tree list
)
32334 const char *id
= "num";
32335 cp_lexer
*lexer
= parser
->lexer
;
32336 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32337 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32339 if (kind
== OMP_CLAUSE_VECTOR
)
32342 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32344 matching_parens parens
;
32345 parens
.consume_open (parser
);
32349 cp_token
*next
= cp_lexer_peek_token (lexer
);
32352 /* Gang static argument. */
32353 if (kind
== OMP_CLAUSE_GANG
32354 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32356 cp_lexer_consume_token (lexer
);
32358 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32359 goto cleanup_error
;
32362 if (ops
[idx
] != NULL
)
32364 cp_parser_error (parser
, "too many %<static%> arguments");
32365 goto cleanup_error
;
32368 /* Check for the '*' argument. */
32369 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32370 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32371 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32374 cp_lexer_consume_token (lexer
);
32375 ops
[idx
] = integer_minus_one_node
;
32377 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32379 cp_lexer_consume_token (lexer
);
32385 /* Worker num: argument and vector length: arguments. */
32386 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32387 && id_equal (next
->u
.value
, id
)
32388 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32390 cp_lexer_consume_token (lexer
); /* id */
32391 cp_lexer_consume_token (lexer
); /* ':' */
32394 /* Now collect the actual argument. */
32395 if (ops
[idx
] != NULL_TREE
)
32397 cp_parser_error (parser
, "unexpected argument");
32398 goto cleanup_error
;
32401 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32403 if (expr
== error_mark_node
)
32404 goto cleanup_error
;
32406 mark_exp_read (expr
);
32409 if (kind
== OMP_CLAUSE_GANG
32410 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32412 cp_lexer_consume_token (lexer
);
32419 if (!parens
.require_close (parser
))
32420 goto cleanup_error
;
32423 check_no_duplicate_clause (list
, kind
, str
, loc
);
32425 c
= build_omp_clause (loc
, kind
);
32428 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32430 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32431 OMP_CLAUSE_CHAIN (c
) = list
;
32436 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32441 tile ( size-expr-list ) */
32444 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32446 tree c
, expr
= error_mark_node
;
32447 tree tile
= NULL_TREE
;
32449 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32450 so, but the spec authors never considered such a case and have
32451 differing opinions on what it might mean, including 'not
32453 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32454 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32457 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32462 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32466 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32467 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32469 cp_lexer_consume_token (parser
->lexer
);
32470 expr
= integer_zero_node
;
32473 expr
= cp_parser_constant_expression (parser
);
32475 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32477 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32479 /* Consume the trailing ')'. */
32480 cp_lexer_consume_token (parser
->lexer
);
32482 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32483 tile
= nreverse (tile
);
32484 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32485 OMP_CLAUSE_CHAIN (c
) = list
;
32490 Parse wait clause or directive parameters. */
32493 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32495 vec
<tree
, va_gc
> *args
;
32498 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32500 /*allow_expansion_p=*/true,
32501 /*non_constant_p=*/NULL
);
32503 if (args
== NULL
|| args
->length () == 0)
32505 cp_parser_error (parser
, "expected integer expression before ')'");
32507 release_tree_vector (args
);
32511 args_tree
= build_tree_list_vec (args
);
32513 release_tree_vector (args
);
32515 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32517 tree targ
= TREE_VALUE (t
);
32519 if (targ
!= error_mark_node
)
32521 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32522 error ("%<wait%> expression must be integral");
32525 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32527 targ
= mark_rvalue_use (targ
);
32528 OMP_CLAUSE_DECL (c
) = targ
;
32529 OMP_CLAUSE_CHAIN (c
) = list
;
32539 wait ( int-expr-list ) */
32542 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32544 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32546 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32549 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32555 collapse ( constant-expression ) */
32558 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32564 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32565 matching_parens parens
;
32566 if (!parens
.require_open (parser
))
32569 num
= cp_parser_constant_expression (parser
);
32571 if (!parens
.require_close (parser
))
32572 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32573 /*or_comma=*/false,
32574 /*consume_paren=*/true);
32576 if (num
== error_mark_node
)
32578 num
= fold_non_dependent_expr (num
);
32579 if (!tree_fits_shwi_p (num
)
32580 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32581 || (n
= tree_to_shwi (num
)) <= 0
32584 error_at (loc
, "collapse argument needs positive constant integer expression");
32588 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32589 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32590 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32591 OMP_CLAUSE_CHAIN (c
) = list
;
32592 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32598 default ( none | shared )
32601 default ( none | present ) */
32604 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32605 location_t location
, bool is_oacc
)
32607 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32610 matching_parens parens
;
32611 if (!parens
.require_open (parser
))
32613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32615 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32616 const char *p
= IDENTIFIER_POINTER (id
);
32621 if (strcmp ("none", p
) != 0)
32623 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32627 if (strcmp ("present", p
) != 0 || !is_oacc
)
32629 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32633 if (strcmp ("shared", p
) != 0 || is_oacc
)
32635 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32642 cp_lexer_consume_token (parser
->lexer
);
32648 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32650 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32653 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32654 || !parens
.require_close (parser
))
32655 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32656 /*or_comma=*/false,
32657 /*consume_paren=*/true);
32659 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32662 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32663 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32664 OMP_CLAUSE_CHAIN (c
) = list
;
32665 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32671 final ( expression ) */
32674 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32678 matching_parens parens
;
32679 if (!parens
.require_open (parser
))
32682 t
= cp_parser_assignment_expression (parser
);
32684 if (t
== error_mark_node
32685 || !parens
.require_close (parser
))
32686 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32687 /*or_comma=*/false,
32688 /*consume_paren=*/true);
32690 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32692 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32693 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32694 OMP_CLAUSE_CHAIN (c
) = list
;
32703 if ( directive-name-modifier : expression )
32705 directive-name-modifier:
32706 parallel | task | taskloop | target data | target | target update
32707 | target enter data | target exit data
32710 directive-name-modifier:
32711 ... | simd | cancel */
32714 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32718 enum tree_code if_modifier
= ERROR_MARK
;
32720 matching_parens parens
;
32721 if (!parens
.require_open (parser
))
32724 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32726 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32727 const char *p
= IDENTIFIER_POINTER (id
);
32730 if (strcmp ("cancel", p
) == 0)
32731 if_modifier
= VOID_CST
;
32732 else if (strcmp ("parallel", p
) == 0)
32733 if_modifier
= OMP_PARALLEL
;
32734 else if (strcmp ("simd", p
) == 0)
32735 if_modifier
= OMP_SIMD
;
32736 else if (strcmp ("task", p
) == 0)
32737 if_modifier
= OMP_TASK
;
32738 else if (strcmp ("taskloop", p
) == 0)
32739 if_modifier
= OMP_TASKLOOP
;
32740 else if (strcmp ("target", p
) == 0)
32742 if_modifier
= OMP_TARGET
;
32743 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32745 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32746 p
= IDENTIFIER_POINTER (id
);
32747 if (strcmp ("data", p
) == 0)
32748 if_modifier
= OMP_TARGET_DATA
;
32749 else if (strcmp ("update", p
) == 0)
32750 if_modifier
= OMP_TARGET_UPDATE
;
32751 else if (strcmp ("enter", p
) == 0)
32752 if_modifier
= OMP_TARGET_ENTER_DATA
;
32753 else if (strcmp ("exit", p
) == 0)
32754 if_modifier
= OMP_TARGET_EXIT_DATA
;
32755 if (if_modifier
!= OMP_TARGET
)
32760 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32761 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32763 if_modifier
= ERROR_MARK
;
32765 if (if_modifier
== OMP_TARGET_ENTER_DATA
32766 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32768 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32770 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32771 p
= IDENTIFIER_POINTER (id
);
32772 if (strcmp ("data", p
) == 0)
32778 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32779 error_at (loc
, "expected %<data%>");
32780 if_modifier
= ERROR_MARK
;
32785 if (if_modifier
!= ERROR_MARK
)
32787 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32790 cp_lexer_consume_token (parser
->lexer
);
32797 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32798 error_at (loc
, "expected %<:%>");
32800 if_modifier
= ERROR_MARK
;
32805 t
= cp_parser_assignment_expression (parser
);
32807 if (t
== error_mark_node
32808 || !parens
.require_close (parser
))
32809 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32810 /*or_comma=*/false,
32811 /*consume_paren=*/true);
32813 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32814 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32816 if (if_modifier
!= ERROR_MARK
32817 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32819 const char *p
= NULL
;
32820 switch (if_modifier
)
32822 case VOID_CST
: p
= "cancel"; break;
32823 case OMP_PARALLEL
: p
= "parallel"; break;
32824 case OMP_SIMD
: p
= "simd"; break;
32825 case OMP_TASK
: p
= "task"; break;
32826 case OMP_TASKLOOP
: p
= "taskloop"; break;
32827 case OMP_TARGET_DATA
: p
= "target data"; break;
32828 case OMP_TARGET
: p
= "target"; break;
32829 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32830 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32831 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32832 default: gcc_unreachable ();
32834 error_at (location
, "too many %<if%> clauses with %qs modifier",
32838 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32841 error_at (location
, "too many %<if%> clauses");
32843 error_at (location
, "too many %<if%> clauses without modifier");
32846 else if (if_modifier
== ERROR_MARK
32847 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32849 error_at (location
, "if any %<if%> clause has modifier, then all "
32850 "%<if%> clauses have to use modifier");
32855 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32856 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32857 OMP_CLAUSE_IF_EXPR (c
) = t
;
32858 OMP_CLAUSE_CHAIN (c
) = list
;
32867 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32868 tree list
, location_t location
)
32872 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32875 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32876 OMP_CLAUSE_CHAIN (c
) = list
;
32884 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32885 tree list
, location_t location
)
32889 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32891 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32892 OMP_CLAUSE_CHAIN (c
) = list
;
32897 num_threads ( expression ) */
32900 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32901 location_t location
)
32905 matching_parens parens
;
32906 if (!parens
.require_open (parser
))
32909 t
= cp_parser_assignment_expression (parser
);
32911 if (t
== error_mark_node
32912 || !parens
.require_close (parser
))
32913 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32914 /*or_comma=*/false,
32915 /*consume_paren=*/true);
32917 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32918 "num_threads", location
);
32920 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32921 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32922 OMP_CLAUSE_CHAIN (c
) = list
;
32928 num_tasks ( expression ) */
32931 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32932 location_t location
)
32936 matching_parens parens
;
32937 if (!parens
.require_open (parser
))
32940 t
= cp_parser_assignment_expression (parser
);
32942 if (t
== error_mark_node
32943 || !parens
.require_close (parser
))
32944 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32945 /*or_comma=*/false,
32946 /*consume_paren=*/true);
32948 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32949 "num_tasks", location
);
32951 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32952 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32953 OMP_CLAUSE_CHAIN (c
) = list
;
32959 grainsize ( expression ) */
32962 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32963 location_t location
)
32967 matching_parens parens
;
32968 if (!parens
.require_open (parser
))
32971 t
= cp_parser_assignment_expression (parser
);
32973 if (t
== error_mark_node
32974 || !parens
.require_close (parser
))
32975 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32976 /*or_comma=*/false,
32977 /*consume_paren=*/true);
32979 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32980 "grainsize", location
);
32982 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32983 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32984 OMP_CLAUSE_CHAIN (c
) = list
;
32990 priority ( expression ) */
32993 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32994 location_t location
)
32998 matching_parens parens
;
32999 if (!parens
.require_open (parser
))
33002 t
= cp_parser_assignment_expression (parser
);
33004 if (t
== error_mark_node
33005 || !parens
.require_close (parser
))
33006 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33007 /*or_comma=*/false,
33008 /*consume_paren=*/true);
33010 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33011 "priority", location
);
33013 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33014 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33015 OMP_CLAUSE_CHAIN (c
) = list
;
33021 hint ( expression ) */
33024 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33028 matching_parens parens
;
33029 if (!parens
.require_open (parser
))
33032 t
= cp_parser_assignment_expression (parser
);
33034 if (t
== error_mark_node
33035 || !parens
.require_close (parser
))
33036 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33037 /*or_comma=*/false,
33038 /*consume_paren=*/true);
33040 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33042 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33043 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33044 OMP_CLAUSE_CHAIN (c
) = list
;
33050 defaultmap ( tofrom : scalar )
33053 defaultmap ( implicit-behavior [ : variable-category ] ) */
33056 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33057 location_t location
)
33061 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33062 enum omp_clause_defaultmap_kind category
33063 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33065 matching_parens parens
;
33066 if (!parens
.require_open (parser
))
33069 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33071 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33074 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33075 "%<tofrom%>, %<firstprivate%>, %<none%> "
33081 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33082 p
= IDENTIFIER_POINTER (id
);
33088 if (strcmp ("alloc", p
) == 0)
33089 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33091 goto invalid_behavior
;
33095 if (strcmp ("default", p
) == 0)
33096 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33098 goto invalid_behavior
;
33102 if (strcmp ("firstprivate", p
) == 0)
33103 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33104 else if (strcmp ("from", p
) == 0)
33105 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33107 goto invalid_behavior
;
33111 if (strcmp ("none", p
) == 0)
33112 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33114 goto invalid_behavior
;
33118 if (strcmp ("tofrom", p
) == 0)
33119 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33120 else if (strcmp ("to", p
) == 0)
33121 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33123 goto invalid_behavior
;
33127 goto invalid_behavior
;
33129 cp_lexer_consume_token (parser
->lexer
);
33131 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33133 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33136 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33139 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33143 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33144 p
= IDENTIFIER_POINTER (id
);
33149 if (strcmp ("aggregate", p
) == 0)
33150 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33152 goto invalid_category
;
33156 if (strcmp ("pointer", p
) == 0)
33157 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33159 goto invalid_category
;
33163 if (strcmp ("scalar", p
) == 0)
33164 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33166 goto invalid_category
;
33170 goto invalid_category
;
33173 cp_lexer_consume_token (parser
->lexer
);
33175 if (!parens
.require_close (parser
))
33178 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33179 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33180 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33181 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33182 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33183 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33185 enum omp_clause_defaultmap_kind cat
= category
;
33186 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33187 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33188 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33192 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33195 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33198 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33201 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33205 gcc_unreachable ();
33208 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33211 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33216 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33217 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33218 OMP_CLAUSE_CHAIN (c
) = list
;
33222 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33223 /*or_comma=*/false,
33224 /*consume_paren=*/true);
33232 ordered ( constant-expression ) */
33235 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33236 tree list
, location_t location
)
33238 tree c
, num
= NULL_TREE
;
33241 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33242 "ordered", location
);
33244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33246 matching_parens parens
;
33247 parens
.consume_open (parser
);
33249 num
= cp_parser_constant_expression (parser
);
33251 if (!parens
.require_close (parser
))
33252 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33253 /*or_comma=*/false,
33254 /*consume_paren=*/true);
33256 if (num
== error_mark_node
)
33258 num
= fold_non_dependent_expr (num
);
33259 if (!tree_fits_shwi_p (num
)
33260 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33261 || (n
= tree_to_shwi (num
)) <= 0
33264 error_at (location
,
33265 "ordered argument needs positive constant integer "
33271 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33272 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33273 OMP_CLAUSE_CHAIN (c
) = list
;
33278 reduction ( reduction-operator : variable-list )
33280 reduction-operator:
33281 One of: + * - & ^ | && ||
33285 reduction-operator:
33286 One of: + * - & ^ | && || min max
33290 reduction-operator:
33291 One of: + * - & ^ | && ||
33295 reduction ( reduction-modifier, reduction-operator : variable-list )
33296 in_reduction ( reduction-operator : variable-list )
33297 task_reduction ( reduction-operator : variable-list ) */
33300 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33301 bool is_omp
, tree list
)
33303 enum tree_code code
= ERROR_MARK
;
33304 tree nlist
, c
, id
= NULL_TREE
;
33306 bool inscan
= false;
33308 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33311 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33313 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33314 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33316 cp_lexer_consume_token (parser
->lexer
);
33317 cp_lexer_consume_token (parser
->lexer
);
33319 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33320 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33322 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33323 const char *p
= IDENTIFIER_POINTER (id
);
33324 if (strcmp (p
, "task") == 0)
33326 else if (strcmp (p
, "inscan") == 0)
33329 sorry ("%<inscan%> modifier on %<reduction%> clause "
33330 "not supported yet");
33332 if (task
|| inscan
)
33334 cp_lexer_consume_token (parser
->lexer
);
33335 cp_lexer_consume_token (parser
->lexer
);
33340 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33342 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33343 case CPP_MULT
: code
= MULT_EXPR
; break;
33344 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33345 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33346 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33347 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33348 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33349 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33353 if (code
!= ERROR_MARK
)
33354 cp_lexer_consume_token (parser
->lexer
);
33357 bool saved_colon_corrects_to_scope_p
;
33358 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33359 parser
->colon_corrects_to_scope_p
= false;
33360 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33361 /*check_dependency_p=*/true,
33362 /*template_p=*/NULL
,
33363 /*declarator_p=*/false,
33364 /*optional_p=*/false);
33365 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33366 if (identifier_p (id
))
33368 const char *p
= IDENTIFIER_POINTER (id
);
33370 if (strcmp (p
, "min") == 0)
33372 else if (strcmp (p
, "max") == 0)
33374 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33376 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33378 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33380 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33381 code
= BIT_AND_EXPR
;
33382 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33383 code
= BIT_IOR_EXPR
;
33384 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33385 code
= BIT_XOR_EXPR
;
33386 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33387 code
= TRUTH_ANDIF_EXPR
;
33388 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33389 code
= TRUTH_ORIF_EXPR
;
33390 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33391 tree scope
= parser
->scope
;
33393 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33394 parser
->scope
= NULL_TREE
;
33395 parser
->qualifying_scope
= NULL_TREE
;
33396 parser
->object_scope
= NULL_TREE
;
33400 error ("invalid reduction-identifier");
33402 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33403 /*or_comma=*/false,
33404 /*consume_paren=*/true);
33409 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33412 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33414 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33416 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33418 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33420 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33421 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33428 schedule ( schedule-kind )
33429 schedule ( schedule-kind , expression )
33432 static | dynamic | guided | runtime | auto
33435 schedule ( schedule-modifier : schedule-kind )
33436 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33444 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33447 int modifiers
= 0, nmodifiers
= 0;
33449 matching_parens parens
;
33450 if (!parens
.require_open (parser
))
33453 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33455 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33457 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33458 const char *p
= IDENTIFIER_POINTER (id
);
33459 if (strcmp ("simd", p
) == 0)
33460 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33461 else if (strcmp ("monotonic", p
) == 0)
33462 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33463 else if (strcmp ("nonmonotonic", p
) == 0)
33464 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33467 cp_lexer_consume_token (parser
->lexer
);
33468 if (nmodifiers
++ == 0
33469 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33470 cp_lexer_consume_token (parser
->lexer
);
33473 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33480 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33481 const char *p
= IDENTIFIER_POINTER (id
);
33486 if (strcmp ("dynamic", p
) != 0)
33488 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33492 if (strcmp ("guided", p
) != 0)
33494 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33498 if (strcmp ("runtime", p
) != 0)
33500 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33507 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33508 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33509 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33510 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33513 cp_lexer_consume_token (parser
->lexer
);
33515 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33516 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33517 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33518 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33520 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33528 cp_lexer_consume_token (parser
->lexer
);
33530 token
= cp_lexer_peek_token (parser
->lexer
);
33531 t
= cp_parser_assignment_expression (parser
);
33533 if (t
== error_mark_node
)
33535 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33536 error_at (token
->location
, "schedule %<runtime%> does not take "
33537 "a %<chunk_size%> parameter");
33538 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33539 error_at (token
->location
, "schedule %<auto%> does not take "
33540 "a %<chunk_size%> parameter");
33542 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33544 if (!parens
.require_close (parser
))
33547 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33550 OMP_CLAUSE_SCHEDULE_KIND (c
)
33551 = (enum omp_clause_schedule_kind
)
33552 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33554 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33555 OMP_CLAUSE_CHAIN (c
) = list
;
33559 cp_parser_error (parser
, "invalid schedule kind");
33561 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33562 /*or_comma=*/false,
33563 /*consume_paren=*/true);
33571 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33572 tree list
, location_t location
)
33576 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33578 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33579 OMP_CLAUSE_CHAIN (c
) = list
;
33588 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33589 tree list
, location_t location
)
33591 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33592 tree c
= build_omp_clause (location
, code
);
33593 OMP_CLAUSE_CHAIN (c
) = list
;
33604 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33605 enum omp_clause_code code
,
33606 tree list
, location_t location
)
33608 tree c
= build_omp_clause (location
, code
);
33609 OMP_CLAUSE_CHAIN (c
) = list
;
33617 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33618 tree list
, location_t location
)
33620 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33621 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33622 OMP_CLAUSE_CHAIN (c
) = list
;
33631 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33632 enum omp_clause_code code
,
33633 tree list
, location_t location
)
33635 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33636 tree c
= build_omp_clause (location
, code
);
33637 OMP_CLAUSE_CHAIN (c
) = list
;
33642 num_teams ( expression ) */
33645 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33646 location_t location
)
33650 matching_parens parens
;
33651 if (!parens
.require_open (parser
))
33654 t
= cp_parser_assignment_expression (parser
);
33656 if (t
== error_mark_node
33657 || !parens
.require_close (parser
))
33658 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33659 /*or_comma=*/false,
33660 /*consume_paren=*/true);
33662 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33663 "num_teams", location
);
33665 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33666 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33667 OMP_CLAUSE_CHAIN (c
) = list
;
33673 thread_limit ( expression ) */
33676 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33677 location_t location
)
33681 matching_parens parens
;
33682 if (!parens
.require_open (parser
))
33685 t
= cp_parser_assignment_expression (parser
);
33687 if (t
== error_mark_node
33688 || !parens
.require_close (parser
))
33689 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33690 /*or_comma=*/false,
33691 /*consume_paren=*/true);
33693 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33694 "thread_limit", location
);
33696 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33697 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33698 OMP_CLAUSE_CHAIN (c
) = list
;
33704 aligned ( variable-list )
33705 aligned ( variable-list : constant-expression ) */
33708 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33710 tree nlist
, c
, alignment
= NULL_TREE
;
33713 matching_parens parens
;
33714 if (!parens
.require_open (parser
))
33717 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33722 alignment
= cp_parser_constant_expression (parser
);
33724 if (!parens
.require_close (parser
))
33725 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33726 /*or_comma=*/false,
33727 /*consume_paren=*/true);
33729 if (alignment
== error_mark_node
)
33730 alignment
= NULL_TREE
;
33733 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33734 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33740 lastprivate ( variable-list )
33743 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33746 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
33748 bool conditional
= false;
33750 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33754 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
33756 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33757 const char *p
= IDENTIFIER_POINTER (id
);
33759 if (strcmp ("conditional", p
) == 0)
33761 conditional
= true;
33762 cp_lexer_consume_token (parser
->lexer
);
33763 cp_lexer_consume_token (parser
->lexer
);
33767 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
33771 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33772 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
33777 linear ( variable-list )
33778 linear ( variable-list : expression )
33781 linear ( modifier ( variable-list ) )
33782 linear ( modifier ( variable-list ) : expression ) */
33785 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33788 tree nlist
, c
, step
= integer_one_node
;
33790 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33792 matching_parens parens
;
33793 if (!parens
.require_open (parser
))
33796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33798 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33799 const char *p
= IDENTIFIER_POINTER (id
);
33801 if (strcmp ("ref", p
) == 0)
33802 kind
= OMP_CLAUSE_LINEAR_REF
;
33803 else if (strcmp ("val", p
) == 0)
33804 kind
= OMP_CLAUSE_LINEAR_VAL
;
33805 else if (strcmp ("uval", p
) == 0)
33806 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33807 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33808 cp_lexer_consume_token (parser
->lexer
);
33810 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33813 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33814 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33818 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33819 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33821 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33822 else if (!parens
.require_close (parser
))
33823 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33824 /*or_comma=*/false,
33825 /*consume_paren=*/true);
33832 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33833 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33835 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33836 cp_parser_parse_tentatively (parser
);
33837 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33838 /*check_dependency_p=*/true,
33839 /*template_p=*/NULL
,
33840 /*declarator_p=*/false,
33841 /*optional_p=*/false);
33842 if (step
!= error_mark_node
)
33843 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33844 if (step
== error_mark_node
)
33847 cp_parser_abort_tentative_parse (parser
);
33849 else if (!cp_parser_parse_definitely (parser
))
33853 step
= cp_parser_assignment_expression (parser
);
33855 if (!parens
.require_close (parser
))
33856 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33857 /*or_comma=*/false,
33858 /*consume_paren=*/true);
33860 if (step
== error_mark_node
)
33864 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33866 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33867 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33874 safelen ( constant-expression ) */
33877 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33878 location_t location
)
33882 matching_parens parens
;
33883 if (!parens
.require_open (parser
))
33886 t
= cp_parser_constant_expression (parser
);
33888 if (t
== error_mark_node
33889 || !parens
.require_close (parser
))
33890 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33891 /*or_comma=*/false,
33892 /*consume_paren=*/true);
33894 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33896 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33897 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33898 OMP_CLAUSE_CHAIN (c
) = list
;
33904 simdlen ( constant-expression ) */
33907 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33908 location_t location
)
33912 matching_parens parens
;
33913 if (!parens
.require_open (parser
))
33916 t
= cp_parser_constant_expression (parser
);
33918 if (t
== error_mark_node
33919 || !parens
.require_close (parser
))
33920 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33921 /*or_comma=*/false,
33922 /*consume_paren=*/true);
33924 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33926 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33927 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33928 OMP_CLAUSE_CHAIN (c
) = list
;
33935 identifier [+/- integer]
33936 vec , identifier [+/- integer]
33940 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33947 cp_parser_error (parser
, "expected identifier");
33951 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33953 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33954 tree t
, identifier
= cp_parser_identifier (parser
);
33955 tree addend
= NULL
;
33957 if (identifier
== error_mark_node
)
33958 t
= error_mark_node
;
33961 t
= cp_parser_lookup_name_simple
33962 (parser
, identifier
,
33963 cp_lexer_peek_token (parser
->lexer
)->location
);
33964 if (t
== error_mark_node
)
33965 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33972 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33974 addend
= integer_zero_node
;
33975 goto add_to_vector
;
33977 cp_lexer_consume_token (parser
->lexer
);
33979 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33981 cp_parser_error (parser
, "expected integer");
33985 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33986 if (TREE_CODE (addend
) != INTEGER_CST
)
33988 cp_parser_error (parser
, "expected integer");
33991 cp_lexer_consume_token (parser
->lexer
);
33994 if (t
!= error_mark_node
)
33996 vec
= tree_cons (addend
, t
, vec
);
33998 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34001 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34004 cp_lexer_consume_token (parser
->lexer
);
34007 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34009 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34010 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34011 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34012 OMP_CLAUSE_CHAIN (u
) = list
;
34019 iterators ( iterators-definition )
34021 iterators-definition:
34023 iterator-specifier , iterators-definition
34025 iterator-specifier:
34026 identifier = range-specification
34027 iterator-type identifier = range-specification
34029 range-specification:
34031 begin : end : step */
34034 cp_parser_omp_iterators (cp_parser
*parser
)
34036 tree ret
= NULL_TREE
, *last
= &ret
;
34037 cp_lexer_consume_token (parser
->lexer
);
34039 matching_parens parens
;
34040 if (!parens
.require_open (parser
))
34041 return error_mark_node
;
34043 bool saved_colon_corrects_to_scope_p
34044 = parser
->colon_corrects_to_scope_p
;
34045 bool saved_colon_doesnt_start_class_def_p
34046 = parser
->colon_doesnt_start_class_def_p
;
34051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34052 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34053 iter_type
= integer_type_node
;
34056 const char *saved_message
34057 = parser
->type_definition_forbidden_message
;
34058 parser
->type_definition_forbidden_message
34059 = G_("types may not be defined in iterator type");
34061 iter_type
= cp_parser_type_id (parser
);
34063 parser
->type_definition_forbidden_message
= saved_message
;
34066 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34067 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34069 cp_parser_error (parser
, "expected identifier");
34073 tree id
= cp_parser_identifier (parser
);
34074 if (id
== error_mark_node
)
34077 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34080 parser
->colon_corrects_to_scope_p
= false;
34081 parser
->colon_doesnt_start_class_def_p
= true;
34082 tree begin
= cp_parser_assignment_expression (parser
);
34084 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34087 tree end
= cp_parser_assignment_expression (parser
);
34089 tree step
= integer_one_node
;
34090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34092 cp_lexer_consume_token (parser
->lexer
);
34093 step
= cp_parser_assignment_expression (parser
);
34096 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34097 DECL_ARTIFICIAL (iter_var
) = 1;
34098 DECL_CONTEXT (iter_var
) = current_function_decl
;
34099 pushdecl (iter_var
);
34101 *last
= make_tree_vec (6);
34102 TREE_VEC_ELT (*last
, 0) = iter_var
;
34103 TREE_VEC_ELT (*last
, 1) = begin
;
34104 TREE_VEC_ELT (*last
, 2) = end
;
34105 TREE_VEC_ELT (*last
, 3) = step
;
34106 last
= &TREE_CHAIN (*last
);
34108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34110 cp_lexer_consume_token (parser
->lexer
);
34117 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34118 parser
->colon_doesnt_start_class_def_p
34119 = saved_colon_doesnt_start_class_def_p
;
34121 if (!parens
.require_close (parser
))
34122 cp_parser_skip_to_closing_parenthesis (parser
,
34123 /*recovering=*/true,
34124 /*or_comma=*/false,
34125 /*consume_paren=*/true);
34127 return ret
? ret
: error_mark_node
;
34131 depend ( depend-kind : variable-list )
34139 depend ( sink : vec )
34142 depend ( depend-modifier , depend-kind: variable-list )
34145 in | out | inout | mutexinoutset | depobj
34148 iterator ( iterators-definition ) */
34151 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34153 tree nlist
, c
, iterators
= NULL_TREE
;
34154 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34156 matching_parens parens
;
34157 if (!parens
.require_open (parser
))
34162 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34165 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34166 const char *p
= IDENTIFIER_POINTER (id
);
34168 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34170 begin_scope (sk_omp
, NULL
);
34171 iterators
= cp_parser_omp_iterators (parser
);
34172 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34175 if (strcmp ("in", p
) == 0)
34176 kind
= OMP_CLAUSE_DEPEND_IN
;
34177 else if (strcmp ("inout", p
) == 0)
34178 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34179 else if (strcmp ("mutexinoutset", p
) == 0)
34180 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34181 else if (strcmp ("out", p
) == 0)
34182 kind
= OMP_CLAUSE_DEPEND_OUT
;
34183 else if (strcmp ("depobj", p
) == 0)
34184 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34185 else if (strcmp ("sink", p
) == 0)
34186 kind
= OMP_CLAUSE_DEPEND_SINK
;
34187 else if (strcmp ("source", p
) == 0)
34188 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34195 cp_lexer_consume_token (parser
->lexer
);
34198 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34200 poplevel (0, 1, 0);
34201 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34202 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34203 iterators
= NULL_TREE
;
34206 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34208 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34209 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34210 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34211 OMP_CLAUSE_CHAIN (c
) = list
;
34212 if (!parens
.require_close (parser
))
34213 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34214 /*or_comma=*/false,
34215 /*consume_paren=*/true);
34219 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34222 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34223 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34226 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34231 tree block
= poplevel (1, 1, 0);
34232 if (iterators
== error_mark_node
)
34233 iterators
= NULL_TREE
;
34235 TREE_VEC_ELT (iterators
, 5) = block
;
34238 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34240 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34242 OMP_CLAUSE_DECL (c
)
34243 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34249 cp_parser_error (parser
, "invalid depend kind");
34252 poplevel (0, 1, 0);
34253 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34254 /*or_comma=*/false,
34255 /*consume_paren=*/true);
34260 map ( map-kind : variable-list )
34261 map ( variable-list )
34264 alloc | to | from | tofrom
34268 alloc | to | from | tofrom | release | delete
34270 map ( always [,] map-kind: variable-list ) */
34273 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34276 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34277 bool always
= false;
34279 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34284 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34285 const char *p
= IDENTIFIER_POINTER (id
);
34287 if (strcmp ("always", p
) == 0)
34290 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34292 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34293 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34295 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34299 cp_lexer_consume_token (parser
->lexer
);
34301 cp_lexer_consume_token (parser
->lexer
);
34306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34307 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34309 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34310 const char *p
= IDENTIFIER_POINTER (id
);
34312 if (strcmp ("alloc", p
) == 0)
34313 kind
= GOMP_MAP_ALLOC
;
34314 else if (strcmp ("to", p
) == 0)
34315 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34316 else if (strcmp ("from", p
) == 0)
34317 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34318 else if (strcmp ("tofrom", p
) == 0)
34319 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34320 else if (strcmp ("release", p
) == 0)
34321 kind
= GOMP_MAP_RELEASE
;
34324 cp_parser_error (parser
, "invalid map kind");
34325 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34326 /*or_comma=*/false,
34327 /*consume_paren=*/true);
34330 cp_lexer_consume_token (parser
->lexer
);
34331 cp_lexer_consume_token (parser
->lexer
);
34333 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34334 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34336 kind
= GOMP_MAP_DELETE
;
34337 cp_lexer_consume_token (parser
->lexer
);
34338 cp_lexer_consume_token (parser
->lexer
);
34341 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34344 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34345 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34351 device ( expression ) */
34354 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34355 location_t location
)
34359 matching_parens parens
;
34360 if (!parens
.require_open (parser
))
34363 t
= cp_parser_assignment_expression (parser
);
34365 if (t
== error_mark_node
34366 || !parens
.require_close (parser
))
34367 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34368 /*or_comma=*/false,
34369 /*consume_paren=*/true);
34371 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34372 "device", location
);
34374 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34375 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34376 OMP_CLAUSE_CHAIN (c
) = list
;
34382 dist_schedule ( static )
34383 dist_schedule ( static , expression ) */
34386 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34387 location_t location
)
34391 matching_parens parens
;
34392 if (!parens
.require_open (parser
))
34395 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34397 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34399 cp_lexer_consume_token (parser
->lexer
);
34401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34403 cp_lexer_consume_token (parser
->lexer
);
34405 t
= cp_parser_assignment_expression (parser
);
34407 if (t
== error_mark_node
)
34409 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34411 if (!parens
.require_close (parser
))
34414 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34417 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34419 OMP_CLAUSE_CHAIN (c
) = list
;
34423 cp_parser_error (parser
, "invalid dist_schedule kind");
34425 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34426 /*or_comma=*/false,
34427 /*consume_paren=*/true);
34432 proc_bind ( proc-bind-kind )
34435 master | close | spread */
34438 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34439 location_t location
)
34442 enum omp_clause_proc_bind_kind kind
;
34444 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34449 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34450 const char *p
= IDENTIFIER_POINTER (id
);
34452 if (strcmp ("master", p
) == 0)
34453 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34454 else if (strcmp ("close", p
) == 0)
34455 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34456 else if (strcmp ("spread", p
) == 0)
34457 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34464 cp_lexer_consume_token (parser
->lexer
);
34465 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34468 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34469 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34471 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34472 OMP_CLAUSE_CHAIN (c
) = list
;
34476 cp_parser_error (parser
, "invalid depend kind");
34478 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34479 /*or_comma=*/false,
34480 /*consume_paren=*/true);
34485 async [( int-expr )] */
34488 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34491 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34493 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34495 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34497 matching_parens parens
;
34498 parens
.consume_open (parser
);
34500 t
= cp_parser_expression (parser
);
34501 if (t
== error_mark_node
34502 || !parens
.require_close (parser
))
34503 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34504 /*or_comma=*/false,
34505 /*consume_paren=*/true);
34508 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34510 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34511 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34512 OMP_CLAUSE_CHAIN (c
) = list
;
34518 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34519 is a bitmask in MASK. Return the list of clauses found. */
34522 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34523 const char *where
, cp_token
*pragma_tok
,
34524 bool finish_p
= true)
34526 tree clauses
= NULL
;
34529 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34532 pragma_omp_clause c_kind
;
34533 omp_clause_code code
;
34534 const char *c_name
;
34535 tree prev
= clauses
;
34537 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34538 cp_lexer_consume_token (parser
->lexer
);
34540 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34541 c_kind
= cp_parser_omp_clause_name (parser
);
34545 case PRAGMA_OACC_CLAUSE_ASYNC
:
34546 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34549 case PRAGMA_OACC_CLAUSE_AUTO
:
34550 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34554 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34555 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34556 c_name
= "collapse";
34558 case PRAGMA_OACC_CLAUSE_COPY
:
34559 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34562 case PRAGMA_OACC_CLAUSE_COPYIN
:
34563 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34566 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34567 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34568 c_name
= "copyout";
34570 case PRAGMA_OACC_CLAUSE_CREATE
:
34571 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34574 case PRAGMA_OACC_CLAUSE_DELETE
:
34575 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34578 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34579 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34580 c_name
= "default";
34582 case PRAGMA_OACC_CLAUSE_DEVICE
:
34583 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34586 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34587 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34588 c_name
= "deviceptr";
34590 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34591 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34592 c_name
= "device_resident";
34594 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34595 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34597 c_name
= "finalize";
34599 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34600 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34602 c_name
= "firstprivate";
34604 case PRAGMA_OACC_CLAUSE_GANG
:
34606 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34609 case PRAGMA_OACC_CLAUSE_HOST
:
34610 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34613 case PRAGMA_OACC_CLAUSE_IF
:
34614 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34617 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34618 clauses
= cp_parser_oacc_simple_clause (parser
,
34619 OMP_CLAUSE_IF_PRESENT
,
34621 c_name
= "if_present";
34623 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34624 clauses
= cp_parser_oacc_simple_clause (parser
,
34625 OMP_CLAUSE_INDEPENDENT
,
34627 c_name
= "independent";
34629 case PRAGMA_OACC_CLAUSE_LINK
:
34630 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34633 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34634 code
= OMP_CLAUSE_NUM_GANGS
;
34635 c_name
= "num_gangs";
34636 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34639 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34640 c_name
= "num_workers";
34641 code
= OMP_CLAUSE_NUM_WORKERS
;
34642 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34645 case PRAGMA_OACC_CLAUSE_PRESENT
:
34646 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34647 c_name
= "present";
34649 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34650 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34652 c_name
= "private";
34654 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34656 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34658 c_name
= "reduction";
34660 case PRAGMA_OACC_CLAUSE_SEQ
:
34661 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34665 case PRAGMA_OACC_CLAUSE_TILE
:
34666 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34669 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34670 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34672 c_name
= "use_device";
34674 case PRAGMA_OACC_CLAUSE_VECTOR
:
34676 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34679 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34680 c_name
= "vector_length";
34681 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34682 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34685 case PRAGMA_OACC_CLAUSE_WAIT
:
34686 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34689 case PRAGMA_OACC_CLAUSE_WORKER
:
34691 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34695 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34701 if (((mask
>> c_kind
) & 1) == 0)
34703 /* Remove the invalid clause(s) from the list to avoid
34704 confusing the rest of the compiler. */
34706 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34711 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34714 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34719 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34720 is a bitmask in MASK. Return the list of clauses found; the result
34721 of clause default goes in *pdefault. */
34724 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34725 const char *where
, cp_token
*pragma_tok
,
34726 bool finish_p
= true)
34728 tree clauses
= NULL
;
34730 cp_token
*token
= NULL
;
34732 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34734 pragma_omp_clause c_kind
;
34735 const char *c_name
;
34736 tree prev
= clauses
;
34738 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34739 cp_lexer_consume_token (parser
->lexer
);
34741 token
= cp_lexer_peek_token (parser
->lexer
);
34742 c_kind
= cp_parser_omp_clause_name (parser
);
34746 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
34747 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
34749 c_name
= "collapse";
34751 case PRAGMA_OMP_CLAUSE_COPYIN
:
34752 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
34755 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
34756 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
34758 c_name
= "copyprivate";
34760 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34761 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
34762 token
->location
, false);
34763 c_name
= "default";
34765 case PRAGMA_OMP_CLAUSE_FINAL
:
34766 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
34769 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
34770 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34772 c_name
= "firstprivate";
34774 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34775 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34777 c_name
= "grainsize";
34779 case PRAGMA_OMP_CLAUSE_HINT
:
34780 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34784 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34785 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34787 c_name
= "defaultmap";
34789 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34790 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34792 c_name
= "use_device_ptr";
34794 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34795 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34797 c_name
= "is_device_ptr";
34799 case PRAGMA_OMP_CLAUSE_IF
:
34800 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34804 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
34806 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
34808 c_name
= "in_reduction";
34810 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34811 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
34812 c_name
= "lastprivate";
34814 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34815 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34817 c_name
= "mergeable";
34819 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34820 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34823 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34824 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34826 c_name
= "num_tasks";
34828 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34829 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34831 c_name
= "num_threads";
34833 case PRAGMA_OMP_CLAUSE_ORDERED
:
34834 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34836 c_name
= "ordered";
34838 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34839 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34841 c_name
= "priority";
34843 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34844 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34846 c_name
= "private";
34848 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34850 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34852 c_name
= "reduction";
34854 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34855 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34857 c_name
= "schedule";
34859 case PRAGMA_OMP_CLAUSE_SHARED
:
34860 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34864 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
34866 = cp_parser_omp_clause_reduction (parser
,
34867 OMP_CLAUSE_TASK_REDUCTION
,
34869 c_name
= "task_reduction";
34871 case PRAGMA_OMP_CLAUSE_UNTIED
:
34872 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34876 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34877 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34878 clauses
, token
->location
);
34879 c_name
= "inbranch";
34881 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
34882 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
34884 c_name
= "nontemporal";
34886 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34887 clauses
= cp_parser_omp_clause_branch (parser
,
34888 OMP_CLAUSE_NOTINBRANCH
,
34889 clauses
, token
->location
);
34890 c_name
= "notinbranch";
34892 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34893 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34894 clauses
, token
->location
);
34895 c_name
= "parallel";
34899 error_at (token
->location
, "%qs must be the first clause of %qs",
34904 case PRAGMA_OMP_CLAUSE_FOR
:
34905 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34906 clauses
, token
->location
);
34909 goto clause_not_first
;
34911 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34912 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34913 clauses
, token
->location
);
34914 c_name
= "sections";
34916 goto clause_not_first
;
34918 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34919 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34920 clauses
, token
->location
);
34921 c_name
= "taskgroup";
34923 goto clause_not_first
;
34925 case PRAGMA_OMP_CLAUSE_LINK
:
34926 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34929 case PRAGMA_OMP_CLAUSE_TO
:
34930 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34931 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34934 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34937 case PRAGMA_OMP_CLAUSE_FROM
:
34938 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34941 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34942 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34944 c_name
= "uniform";
34946 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34947 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34949 c_name
= "num_teams";
34951 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34952 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34954 c_name
= "thread_limit";
34956 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34957 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34958 c_name
= "aligned";
34960 case PRAGMA_OMP_CLAUSE_LINEAR
:
34962 bool declare_simd
= false;
34963 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34964 declare_simd
= true;
34965 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34969 case PRAGMA_OMP_CLAUSE_DEPEND
:
34970 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34974 case PRAGMA_OMP_CLAUSE_MAP
:
34975 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34978 case PRAGMA_OMP_CLAUSE_DEVICE
:
34979 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34983 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34984 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34986 c_name
= "dist_schedule";
34988 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34989 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34991 c_name
= "proc_bind";
34993 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34994 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34996 c_name
= "safelen";
34998 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34999 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35001 c_name
= "simdlen";
35003 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35004 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35006 c_name
= "nogroup";
35008 case PRAGMA_OMP_CLAUSE_THREADS
:
35010 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35011 clauses
, token
->location
);
35012 c_name
= "threads";
35014 case PRAGMA_OMP_CLAUSE_SIMD
:
35016 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35017 clauses
, token
->location
);
35021 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35027 if (((mask
>> c_kind
) & 1) == 0)
35029 /* Remove the invalid clause(s) from the list to avoid
35030 confusing the rest of the compiler. */
35032 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35036 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35039 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35040 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35042 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35051 In practice, we're also interested in adding the statement to an
35052 outer node. So it is convenient if we work around the fact that
35053 cp_parser_statement calls add_stmt. */
35056 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35058 unsigned save
= parser
->in_statement
;
35060 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35061 This preserves the "not within loop or switch" style error messages
35062 for nonsense cases like
35068 if (parser
->in_statement
)
35069 parser
->in_statement
= IN_OMP_BLOCK
;
35075 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35077 parser
->in_statement
= save
;
35081 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35083 tree stmt
= begin_omp_structured_block ();
35084 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35086 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35088 cp_parser_end_omp_structured_block (parser
, save
);
35089 return finish_omp_structured_block (stmt
);
35093 # pragma omp atomic new-line
35097 x binop= expr | x++ | ++x | x-- | --x
35099 +, *, -, /, &, ^, |, <<, >>
35101 where x is an lvalue expression with scalar type.
35104 # pragma omp atomic new-line
35107 # pragma omp atomic read new-line
35110 # pragma omp atomic write new-line
35113 # pragma omp atomic update new-line
35116 # pragma omp atomic capture new-line
35119 # pragma omp atomic capture new-line
35127 expression-stmt | x = x binop expr
35129 v = expression-stmt
35131 { v = x; update-stmt; } | { update-stmt; v = x; }
35135 expression-stmt | x = x binop expr | x = expr binop x
35139 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35141 where x and v are lvalue expressions with scalar type. */
35144 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35146 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35147 tree rhs1
= NULL_TREE
, orig_lhs
;
35148 location_t loc
= pragma_tok
->location
;
35149 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35150 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35151 bool structured_block
= false;
35153 tree clauses
= NULL_TREE
;
35155 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35157 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35158 cp_lexer_consume_token (parser
->lexer
);
35162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35164 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35165 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35166 const char *p
= IDENTIFIER_POINTER (id
);
35167 enum tree_code new_code
= ERROR_MARK
;
35168 enum omp_memory_order new_memory_order
35169 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35171 if (!strcmp (p
, "read"))
35172 new_code
= OMP_ATOMIC_READ
;
35173 else if (!strcmp (p
, "write"))
35174 new_code
= NOP_EXPR
;
35175 else if (!strcmp (p
, "update"))
35176 new_code
= OMP_ATOMIC
;
35177 else if (!strcmp (p
, "capture"))
35178 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35179 else if (!strcmp (p
, "seq_cst"))
35180 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35181 else if (!strcmp (p
, "acq_rel"))
35182 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35183 else if (!strcmp (p
, "release"))
35184 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35185 else if (!strcmp (p
, "acquire"))
35186 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35187 else if (!strcmp (p
, "relaxed"))
35188 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35189 else if (!strcmp (p
, "hint"))
35191 cp_lexer_consume_token (parser
->lexer
);
35192 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35198 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35199 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35200 "%<release%>, %<relaxed%> or %<hint%> clause");
35204 if (new_code
!= ERROR_MARK
)
35206 if (code
!= ERROR_MARK
)
35207 error_at (cloc
, "too many atomic clauses");
35211 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35213 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35214 error_at (cloc
, "too many memory order clauses");
35216 memory_order
= new_memory_order
;
35218 cp_lexer_consume_token (parser
->lexer
);
35224 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35226 if (code
== ERROR_MARK
)
35228 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35231 = (enum omp_requires
) (omp_requires_mask
35232 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35233 switch ((enum omp_memory_order
)
35234 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35236 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35237 case OMP_MEMORY_ORDER_RELAXED
:
35238 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35240 case OMP_MEMORY_ORDER_SEQ_CST
:
35241 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35243 case OMP_MEMORY_ORDER_ACQ_REL
:
35246 case OMP_ATOMIC_READ
:
35247 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35249 case NOP_EXPR
: /* atomic write */
35251 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35254 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35259 gcc_unreachable ();
35265 case OMP_ATOMIC_READ
:
35266 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35267 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35269 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35270 "%<acq_rel%> or %<release%> clauses");
35271 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35274 case NOP_EXPR
: /* atomic write */
35275 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35276 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35278 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35279 "%<acq_rel%> or %<acquire%> clauses");
35280 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35284 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35285 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35287 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35288 "%<acq_rel%> or %<acquire%> clauses");
35289 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35298 case OMP_ATOMIC_READ
:
35299 case NOP_EXPR
: /* atomic write */
35300 v
= cp_parser_unary_expression (parser
);
35301 if (v
== error_mark_node
)
35303 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35305 if (code
== NOP_EXPR
)
35306 lhs
= cp_parser_expression (parser
);
35308 lhs
= cp_parser_unary_expression (parser
);
35309 if (lhs
== error_mark_node
)
35311 if (code
== NOP_EXPR
)
35313 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35321 case OMP_ATOMIC_CAPTURE_NEW
:
35322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35324 cp_lexer_consume_token (parser
->lexer
);
35325 structured_block
= true;
35329 v
= cp_parser_unary_expression (parser
);
35330 if (v
== error_mark_node
)
35332 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35340 lhs
= cp_parser_unary_expression (parser
);
35342 switch (TREE_CODE (lhs
))
35347 case POSTINCREMENT_EXPR
:
35348 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35349 code
= OMP_ATOMIC_CAPTURE_OLD
;
35351 case PREINCREMENT_EXPR
:
35352 lhs
= TREE_OPERAND (lhs
, 0);
35353 opcode
= PLUS_EXPR
;
35354 rhs
= integer_one_node
;
35357 case POSTDECREMENT_EXPR
:
35358 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35359 code
= OMP_ATOMIC_CAPTURE_OLD
;
35361 case PREDECREMENT_EXPR
:
35362 lhs
= TREE_OPERAND (lhs
, 0);
35363 opcode
= MINUS_EXPR
;
35364 rhs
= integer_one_node
;
35367 case COMPOUND_EXPR
:
35368 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35369 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35370 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35371 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35372 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35373 (TREE_OPERAND (lhs
, 1), 0), 0)))
35375 /* Undo effects of boolean_increment for post {in,de}crement. */
35376 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35379 if (TREE_CODE (lhs
) == MODIFY_EXPR
35380 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35382 /* Undo effects of boolean_increment. */
35383 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35385 /* This is pre or post increment. */
35386 rhs
= TREE_OPERAND (lhs
, 1);
35387 lhs
= TREE_OPERAND (lhs
, 0);
35389 if (code
== OMP_ATOMIC_CAPTURE_NEW
35390 && !structured_block
35391 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35392 code
= OMP_ATOMIC_CAPTURE_OLD
;
35398 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35401 opcode
= MULT_EXPR
;
35404 opcode
= TRUNC_DIV_EXPR
;
35407 opcode
= PLUS_EXPR
;
35410 opcode
= MINUS_EXPR
;
35412 case CPP_LSHIFT_EQ
:
35413 opcode
= LSHIFT_EXPR
;
35415 case CPP_RSHIFT_EQ
:
35416 opcode
= RSHIFT_EXPR
;
35419 opcode
= BIT_AND_EXPR
;
35422 opcode
= BIT_IOR_EXPR
;
35425 opcode
= BIT_XOR_EXPR
;
35428 enum cp_parser_prec oprec
;
35430 cp_lexer_consume_token (parser
->lexer
);
35431 cp_parser_parse_tentatively (parser
);
35432 rhs1
= cp_parser_simple_cast_expression (parser
);
35433 if (rhs1
== error_mark_node
)
35435 cp_parser_abort_tentative_parse (parser
);
35436 cp_parser_simple_cast_expression (parser
);
35439 token
= cp_lexer_peek_token (parser
->lexer
);
35440 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35442 cp_parser_abort_tentative_parse (parser
);
35443 cp_parser_parse_tentatively (parser
);
35444 rhs
= cp_parser_binary_expression (parser
, false, true,
35445 PREC_NOT_OPERATOR
, NULL
);
35446 if (rhs
== error_mark_node
)
35448 cp_parser_abort_tentative_parse (parser
);
35449 cp_parser_binary_expression (parser
, false, true,
35450 PREC_NOT_OPERATOR
, NULL
);
35453 switch (TREE_CODE (rhs
))
35456 case TRUNC_DIV_EXPR
:
35465 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35467 if (cp_parser_parse_definitely (parser
))
35469 opcode
= TREE_CODE (rhs
);
35470 rhs1
= TREE_OPERAND (rhs
, 0);
35471 rhs
= TREE_OPERAND (rhs
, 1);
35481 cp_parser_abort_tentative_parse (parser
);
35482 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35484 rhs
= cp_parser_expression (parser
);
35485 if (rhs
== error_mark_node
)
35491 cp_parser_error (parser
,
35492 "invalid form of %<#pragma omp atomic%>");
35495 if (!cp_parser_parse_definitely (parser
))
35497 switch (token
->type
)
35499 case CPP_SEMICOLON
:
35500 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35502 code
= OMP_ATOMIC_CAPTURE_OLD
;
35507 cp_lexer_consume_token (parser
->lexer
);
35510 else if (structured_block
)
35517 cp_parser_error (parser
,
35518 "invalid form of %<#pragma omp atomic%>");
35521 opcode
= MULT_EXPR
;
35524 opcode
= TRUNC_DIV_EXPR
;
35527 opcode
= PLUS_EXPR
;
35530 opcode
= MINUS_EXPR
;
35533 opcode
= LSHIFT_EXPR
;
35536 opcode
= RSHIFT_EXPR
;
35539 opcode
= BIT_AND_EXPR
;
35542 opcode
= BIT_IOR_EXPR
;
35545 opcode
= BIT_XOR_EXPR
;
35548 cp_parser_error (parser
,
35549 "invalid operator for %<#pragma omp atomic%>");
35552 oprec
= TOKEN_PRECEDENCE (token
);
35553 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35554 if (commutative_tree_code (opcode
))
35555 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35556 cp_lexer_consume_token (parser
->lexer
);
35557 rhs
= cp_parser_binary_expression (parser
, false, false,
35559 if (rhs
== error_mark_node
)
35564 cp_parser_error (parser
,
35565 "invalid operator for %<#pragma omp atomic%>");
35568 cp_lexer_consume_token (parser
->lexer
);
35570 rhs
= cp_parser_expression (parser
);
35571 if (rhs
== error_mark_node
)
35576 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35578 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35580 v
= cp_parser_unary_expression (parser
);
35581 if (v
== error_mark_node
)
35583 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35585 lhs1
= cp_parser_unary_expression (parser
);
35586 if (lhs1
== error_mark_node
)
35589 if (structured_block
)
35591 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35592 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35595 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35596 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35597 rhs1
, clauses
, memory_order
);
35598 if (!structured_block
)
35599 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35603 cp_parser_skip_to_end_of_block_or_statement (parser
);
35604 if (structured_block
)
35606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35607 cp_lexer_consume_token (parser
->lexer
);
35608 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35610 cp_parser_skip_to_end_of_block_or_statement (parser
);
35611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35612 cp_lexer_consume_token (parser
->lexer
);
35619 # pragma omp barrier new-line */
35622 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35624 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35625 finish_omp_barrier ();
35629 # pragma omp critical [(name)] new-line
35633 # pragma omp critical [(name) [hint(expression)]] new-line
35634 structured-block */
35636 #define OMP_CRITICAL_CLAUSE_MASK \
35637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35640 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35642 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35646 matching_parens parens
;
35647 parens
.consume_open (parser
);
35649 name
= cp_parser_identifier (parser
);
35651 if (name
== error_mark_node
35652 || !parens
.require_close (parser
))
35653 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35654 /*or_comma=*/false,
35655 /*consume_paren=*/true);
35656 if (name
== error_mark_node
)
35659 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35660 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35661 cp_lexer_consume_token (parser
->lexer
);
35663 clauses
= cp_parser_omp_all_clauses (parser
,
35664 OMP_CRITICAL_CLAUSE_MASK
,
35665 "#pragma omp critical", pragma_tok
);
35668 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35670 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35671 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35675 # pragma omp depobj ( depobj ) depobj-clause new-line
35678 depend (dependence-type : locator)
35680 update (dependence-type)
35689 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
35691 location_t loc
= pragma_tok
->location
;
35692 matching_parens parens
;
35693 if (!parens
.require_open (parser
))
35695 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35699 tree depobj
= cp_parser_assignment_expression (parser
);
35701 if (!parens
.require_close (parser
))
35702 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35703 /*or_comma=*/false,
35704 /*consume_paren=*/true);
35706 tree clause
= NULL_TREE
;
35707 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35708 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35711 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35712 const char *p
= IDENTIFIER_POINTER (id
);
35714 cp_lexer_consume_token (parser
->lexer
);
35715 if (!strcmp ("depend", p
))
35717 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
35719 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
35721 clause
= error_mark_node
;
35723 else if (!strcmp ("destroy", p
))
35724 kind
= OMP_CLAUSE_DEPEND_LAST
;
35725 else if (!strcmp ("update", p
))
35727 matching_parens c_parens
;
35728 if (c_parens
.require_open (parser
))
35731 = cp_lexer_peek_token (parser
->lexer
)->location
;
35732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35734 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35735 const char *p2
= IDENTIFIER_POINTER (id2
);
35737 cp_lexer_consume_token (parser
->lexer
);
35738 if (!strcmp ("in", p2
))
35739 kind
= OMP_CLAUSE_DEPEND_IN
;
35740 else if (!strcmp ("out", p2
))
35741 kind
= OMP_CLAUSE_DEPEND_OUT
;
35742 else if (!strcmp ("inout", p2
))
35743 kind
= OMP_CLAUSE_DEPEND_INOUT
;
35744 else if (!strcmp ("mutexinoutset", p2
))
35745 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
35747 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35749 clause
= error_mark_node
;
35750 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
35751 "%<mutexinoutset%>");
35753 if (!c_parens
.require_close (parser
))
35754 cp_parser_skip_to_closing_parenthesis (parser
,
35755 /*recovering=*/true,
35756 /*or_comma=*/false,
35757 /*consume_paren=*/true);
35760 clause
= error_mark_node
;
35763 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35765 clause
= error_mark_node
;
35766 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
35768 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35770 finish_omp_depobj (loc
, depobj
, kind
, clause
);
35775 # pragma omp flush flush-vars[opt] new-line
35781 # pragma omp flush memory-order-clause new-line */
35784 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
35786 enum memmodel mo
= MEMMODEL_LAST
;
35787 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35789 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35790 const char *p
= IDENTIFIER_POINTER (id
);
35791 if (!strcmp (p
, "acq_rel"))
35792 mo
= MEMMODEL_ACQ_REL
;
35793 else if (!strcmp (p
, "release"))
35794 mo
= MEMMODEL_RELEASE
;
35795 else if (!strcmp (p
, "acquire"))
35796 mo
= MEMMODEL_ACQUIRE
;
35798 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35799 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35800 cp_lexer_consume_token (parser
->lexer
);
35802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35804 if (mo
!= MEMMODEL_LAST
)
35805 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35806 "%<flush%> list specified together with memory order "
35808 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35810 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35812 finish_omp_flush (mo
);
35815 /* Helper function, to parse omp for increment expression. */
35818 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
35820 tree cond
= cp_parser_binary_expression (parser
, false, true,
35821 PREC_NOT_OPERATOR
, NULL
);
35822 if (cond
== error_mark_node
35823 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35825 cp_parser_skip_to_end_of_statement (parser
);
35826 return error_mark_node
;
35829 switch (TREE_CODE (cond
))
35837 if (code
!= OACC_LOOP
)
35839 gcc_fallthrough ();
35841 return error_mark_node
;
35844 /* If decl is an iterator, preserve LHS and RHS of the relational
35845 expr until finish_omp_for. */
35847 && (type_dependent_expression_p (decl
)
35848 || CLASS_TYPE_P (TREE_TYPE (decl
))))
35851 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
35853 TREE_OPERAND (cond
, 0), ERROR_MARK
,
35854 TREE_OPERAND (cond
, 1), ERROR_MARK
,
35855 /*overload=*/NULL
, tf_warning_or_error
);
35858 /* Helper function, to parse omp for increment expression. */
35861 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
35863 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35869 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35871 op
= (token
->type
== CPP_PLUS_PLUS
35872 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
35873 cp_lexer_consume_token (parser
->lexer
);
35874 lhs
= cp_parser_simple_cast_expression (parser
);
35876 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35877 return error_mark_node
;
35878 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35881 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
35883 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
35884 return error_mark_node
;
35886 token
= cp_lexer_peek_token (parser
->lexer
);
35887 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
35889 op
= (token
->type
== CPP_PLUS_PLUS
35890 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
35891 cp_lexer_consume_token (parser
->lexer
);
35892 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35895 op
= cp_parser_assignment_operator_opt (parser
);
35896 if (op
== ERROR_MARK
)
35897 return error_mark_node
;
35899 if (op
!= NOP_EXPR
)
35901 rhs
= cp_parser_assignment_expression (parser
);
35902 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
35903 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35906 lhs
= cp_parser_binary_expression (parser
, false, false,
35907 PREC_ADDITIVE_EXPRESSION
, NULL
);
35908 token
= cp_lexer_peek_token (parser
->lexer
);
35909 decl_first
= (lhs
== decl
35910 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
35913 if (token
->type
!= CPP_PLUS
35914 && token
->type
!= CPP_MINUS
)
35915 return error_mark_node
;
35919 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
35920 cp_lexer_consume_token (parser
->lexer
);
35921 rhs
= cp_parser_binary_expression (parser
, false, false,
35922 PREC_ADDITIVE_EXPRESSION
, NULL
);
35923 token
= cp_lexer_peek_token (parser
->lexer
);
35924 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
35926 if (lhs
== NULL_TREE
)
35928 if (op
== PLUS_EXPR
)
35931 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
35932 tf_warning_or_error
);
35935 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
35936 ERROR_MARK
, NULL
, tf_warning_or_error
);
35939 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
35944 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
35945 || op
== MINUS_EXPR
)
35946 return error_mark_node
;
35947 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
35950 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
35952 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35955 /* Parse the initialization statement of an OpenMP for loop.
35957 Return true if the resulting construct should have an
35958 OMP_CLAUSE_PRIVATE added to it. */
35961 cp_parser_omp_for_loop_init (cp_parser
*parser
,
35962 tree
&this_pre_body
,
35963 vec
<tree
, va_gc
> *&for_block
,
35969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35972 tree add_private_clause
= NULL_TREE
;
35974 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35978 integer-type var = lb
35979 random-access-iterator-type var = lb
35980 pointer-type var = lb
35982 cp_decl_specifier_seq type_specifiers
;
35984 /* First, try to parse as an initialized declaration. See
35985 cp_parser_condition, from whence the bulk of this is copied. */
35987 cp_parser_parse_tentatively (parser
);
35988 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
35989 /*is_trailing_return=*/false,
35991 if (cp_parser_parse_definitely (parser
))
35993 /* If parsing a type specifier seq succeeded, then this
35994 MUST be a initialized declaration. */
35995 tree asm_specification
, attributes
;
35996 cp_declarator
*declarator
;
35998 declarator
= cp_parser_declarator (parser
,
35999 CP_PARSER_DECLARATOR_NAMED
,
36000 /*ctor_dtor_or_conv_p=*/NULL
,
36001 /*parenthesized_p=*/NULL
,
36002 /*member_p=*/false,
36003 /*friend_p=*/false);
36004 attributes
= cp_parser_attributes_opt (parser
);
36005 asm_specification
= cp_parser_asm_specification_opt (parser
);
36007 if (declarator
== cp_error_declarator
)
36008 cp_parser_skip_to_end_of_statement (parser
);
36012 tree pushed_scope
, auto_node
;
36014 decl
= start_decl (declarator
, &type_specifiers
,
36015 SD_INITIALIZED
, attributes
,
36016 /*prefix_attributes=*/NULL_TREE
,
36019 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36020 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36022 if (cp_lexer_next_token_is (parser
->lexer
,
36024 error ("parenthesized initialization is not allowed in "
36025 "OpenMP %<for%> loop");
36027 /* Trigger an error. */
36028 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36030 init
= error_mark_node
;
36031 cp_parser_skip_to_end_of_statement (parser
);
36033 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36034 || type_dependent_expression_p (decl
)
36037 bool is_direct_init
, is_non_constant_init
;
36039 init
= cp_parser_initializer (parser
,
36041 &is_non_constant_init
);
36046 = do_auto_deduction (TREE_TYPE (decl
), init
,
36049 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36050 && !type_dependent_expression_p (decl
))
36054 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36056 LOOKUP_ONLYCONVERTING
);
36058 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36060 vec_safe_push (for_block
, this_pre_body
);
36065 init
= pop_stmt_list (this_pre_body
);
36066 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36068 tree_stmt_iterator i
= tsi_start (init
);
36069 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36070 while (!tsi_end_p (i
))
36072 tree t
= tsi_stmt (i
);
36073 if (TREE_CODE (t
) == DECL_EXPR
36074 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36077 vec_safe_push (for_block
, t
);
36082 if (tsi_one_before_end_p (i
))
36084 tree t
= tsi_stmt (i
);
36086 free_stmt_list (init
);
36091 this_pre_body
= NULL_TREE
;
36096 cp_lexer_consume_token (parser
->lexer
);
36097 init
= cp_parser_assignment_expression (parser
);
36100 if (TYPE_REF_P (TREE_TYPE (decl
)))
36101 init
= error_mark_node
;
36103 cp_finish_decl (decl
, NULL_TREE
,
36104 /*init_const_expr_p=*/false,
36106 LOOKUP_ONLYCONVERTING
);
36110 pop_scope (pushed_scope
);
36116 /* If parsing a type specifier sequence failed, then
36117 this MUST be a simple expression. */
36118 cp_parser_parse_tentatively (parser
);
36119 decl
= cp_parser_primary_expression (parser
, false, false,
36121 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36122 if (!cp_parser_error_occurred (parser
)
36124 && (TREE_CODE (decl
) == COMPONENT_REF
36125 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36127 cp_parser_abort_tentative_parse (parser
);
36128 cp_parser_parse_tentatively (parser
);
36129 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36130 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36131 /*check_dependency_p=*/true,
36132 /*template_p=*/NULL
,
36133 /*declarator_p=*/false,
36134 /*optional_p=*/false);
36135 if (name
!= error_mark_node
36136 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36138 decl
= cp_parser_lookup_name_simple (parser
, name
,
36140 if (TREE_CODE (decl
) == FIELD_DECL
)
36141 add_private_clause
= omp_privatize_field (decl
, false);
36143 cp_parser_abort_tentative_parse (parser
);
36144 cp_parser_parse_tentatively (parser
);
36145 decl
= cp_parser_primary_expression (parser
, false, false,
36148 if (!cp_parser_error_occurred (parser
)
36151 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36155 cp_parser_parse_definitely (parser
);
36156 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36157 rhs
= cp_parser_assignment_expression (parser
);
36159 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36162 tf_warning_or_error
));
36163 if (!add_private_clause
)
36164 add_private_clause
= decl
;
36169 cp_parser_abort_tentative_parse (parser
);
36170 init
= cp_parser_expression (parser
);
36173 if (TREE_CODE (init
) == MODIFY_EXPR
36174 || TREE_CODE (init
) == MODOP_EXPR
)
36175 real_decl
= TREE_OPERAND (init
, 0);
36179 return add_private_clause
;
36182 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36185 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36186 tree
&decl
, tree
&orig_decl
, tree
&init
,
36187 tree
&orig_init
, tree
&cond
, tree
&incr
)
36189 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36190 tree iter_type
, begin_expr
, end_expr
;
36192 if (processing_template_decl
)
36194 if (check_for_bare_parameter_packs (init
))
36195 init
= error_mark_node
;
36196 if (!type_dependent_expression_p (init
)
36197 /* do_auto_deduction doesn't mess with template init-lists. */
36198 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36201 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36203 tree v
= DECL_VALUE_EXPR (decl
);
36204 if (TREE_CODE (v
) == ARRAY_REF
36205 && VAR_P (TREE_OPERAND (v
, 0))
36206 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36207 d
= TREE_OPERAND (v
, 0);
36209 do_range_for_auto_deduction (d
, init
);
36211 cond
= global_namespace
;
36215 this_pre_body
= pop_stmt_list (this_pre_body
);
36219 init
= mark_lvalue_use (init
);
36221 if (decl
== error_mark_node
|| init
== error_mark_node
)
36222 /* If an error happened previously do nothing or else a lot of
36223 unhelpful errors would be issued. */
36224 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36230 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36231 /* Can't bind a reference to an array of runtime bound. */
36235 range_temp
= build_range_temp (init
);
36236 DECL_NAME (range_temp
) = NULL_TREE
;
36237 pushdecl (range_temp
);
36238 cp_finish_decl (range_temp
, init
,
36239 /*is_constant_init*/false, NULL_TREE
,
36240 LOOKUP_ONLYCONVERTING
);
36241 range_temp_decl
= range_temp
;
36242 range_temp
= convert_from_reference (range_temp
);
36244 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36245 &begin_expr
, &end_expr
);
36248 tree end_iter_type
= iter_type
;
36249 if (cxx_dialect
>= cxx17
)
36250 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36251 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36252 TREE_USED (end
) = 1;
36253 DECL_ARTIFICIAL (end
) = 1;
36255 cp_finish_decl (end
, end_expr
,
36256 /*is_constant_init*/false, NULL_TREE
,
36257 LOOKUP_ONLYCONVERTING
);
36259 /* The new for initialization statement. */
36260 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36261 TREE_USED (begin
) = 1;
36262 DECL_ARTIFICIAL (begin
) = 1;
36265 if (CLASS_TYPE_P (iter_type
))
36270 begin_expr
= NULL_TREE
;
36272 cp_finish_decl (begin
, begin_expr
,
36273 /*is_constant_init*/false, NULL_TREE
,
36274 LOOKUP_ONLYCONVERTING
);
36276 /* The new for condition. */
36277 if (CLASS_TYPE_P (iter_type
))
36278 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36280 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36283 NULL
, tf_warning_or_error
);
36285 /* The new increment expression. */
36286 if (CLASS_TYPE_P (iter_type
))
36287 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36289 incr
= finish_unary_op_expr (input_location
,
36290 PREINCREMENT_EXPR
, begin
,
36291 tf_warning_or_error
);
36297 vec_safe_push (for_block
, this_pre_body
);
36298 this_pre_body
= NULL_TREE
;
36301 tree decomp_first_name
= NULL_TREE
;
36302 unsigned decomp_cnt
= 0;
36303 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36305 tree v
= DECL_VALUE_EXPR (orig_decl
);
36306 if (TREE_CODE (v
) == ARRAY_REF
36307 && VAR_P (TREE_OPERAND (v
, 0))
36308 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36310 tree d
= orig_decl
;
36311 orig_decl
= TREE_OPERAND (v
, 0);
36312 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36313 decomp_first_name
= d
;
36317 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36320 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36322 if (!error_operand_p (t
))
36323 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36327 tree v
= make_tree_vec (decomp_cnt
+ 3);
36328 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36329 TREE_VEC_ELT (v
, 1) = end
;
36330 TREE_VEC_ELT (v
, 2) = orig_decl
;
36331 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36333 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36334 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36336 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36339 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36340 inside of the collapsed body. */
36343 cp_finish_omp_range_for (tree orig
, tree begin
)
36345 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36346 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36347 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36348 tree decomp_first_name
= NULL_TREE
;
36349 unsigned int decomp_cnt
= 0;
36351 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36353 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36354 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36355 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36358 /* The declaration is initialized with *__begin inside the loop body. */
36359 cp_finish_decl (decl
,
36360 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36361 tf_warning_or_error
),
36362 /*is_constant_init*/false, NULL_TREE
,
36363 LOOKUP_ONLYCONVERTING
);
36364 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36365 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36368 /* Parse the restricted form of the for statement allowed by OpenMP. */
36371 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36372 tree
*cclauses
, bool *if_p
)
36374 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36376 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36377 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36378 location_t loc_first
;
36379 bool collapse_err
= false;
36380 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36381 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36382 auto_vec
<tree
, 4> orig_inits
;
36383 bool tiling
= false;
36385 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36386 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36387 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36388 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36391 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36393 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36394 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36397 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36400 if (ordered
&& ordered
< collapse
)
36402 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36403 "%<ordered%> clause parameter is less than %<collapse%>");
36404 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36405 = build_int_cst (NULL_TREE
, collapse
);
36406 ordered
= collapse
;
36410 for (tree
*pc
= &clauses
; *pc
; )
36411 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36413 error_at (OMP_CLAUSE_LOCATION (*pc
),
36414 "%<linear%> clause may not be specified together "
36415 "with %<ordered%> clause with a parameter");
36416 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36419 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36422 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36423 count
= ordered
? ordered
: collapse
;
36425 declv
= make_tree_vec (count
);
36426 initv
= make_tree_vec (count
);
36427 condv
= make_tree_vec (count
);
36428 incrv
= make_tree_vec (count
);
36429 orig_declv
= NULL_TREE
;
36431 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36433 for (i
= 0; i
< count
; i
++)
36435 int bracecount
= 0;
36436 tree add_private_clause
= NULL_TREE
;
36439 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36442 cp_parser_error (parser
, "for statement expected");
36445 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36447 matching_parens parens
;
36448 if (!parens
.require_open (parser
))
36451 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36452 this_pre_body
= push_stmt_list ();
36454 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36456 /* Save tokens so that we can put them back. */
36457 cp_lexer_save_tokens (parser
->lexer
);
36459 /* Look for ':' that is not nested in () or {}. */
36461 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36462 /*recovering=*/false,
36467 /* Roll back the tokens we skipped. */
36468 cp_lexer_rollback_tokens (parser
->lexer
);
36472 bool saved_colon_corrects_to_scope_p
36473 = parser
->colon_corrects_to_scope_p
;
36475 /* A colon is used in range-based for. */
36476 parser
->colon_corrects_to_scope_p
= false;
36478 /* Parse the declaration. */
36479 cp_parser_simple_declaration (parser
,
36480 /*function_definition_allowed_p=*/
36482 parser
->colon_corrects_to_scope_p
36483 = saved_colon_corrects_to_scope_p
;
36485 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36487 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36490 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36491 orig_decl
, init
, orig_init
,
36498 pre_body
= push_stmt_list ();
36500 add_stmt (this_pre_body
);
36501 pre_body
= pop_stmt_list (pre_body
);
36504 pre_body
= this_pre_body
;
36508 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36509 "%<ordered%> clause with parameter on "
36510 "range-based %<for%> loop");
36512 goto parse_close_paren
;
36517 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36518 init
, orig_init
, decl
, real_decl
);
36520 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36523 this_pre_body
= pop_stmt_list (this_pre_body
);
36527 pre_body
= push_stmt_list ();
36529 add_stmt (this_pre_body
);
36530 pre_body
= pop_stmt_list (pre_body
);
36533 pre_body
= this_pre_body
;
36538 if (cclauses
!= NULL
36539 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36540 && real_decl
!= NULL_TREE
)
36543 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36544 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36545 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36547 error_at (loc
, "iteration variable %qD"
36548 " should not be firstprivate", real_decl
);
36549 *c
= OMP_CLAUSE_CHAIN (*c
);
36551 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36552 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36554 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36556 *c
= OMP_CLAUSE_CHAIN (*c
);
36557 if (code
== OMP_SIMD
)
36559 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36560 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36564 OMP_CLAUSE_CHAIN (l
) = clauses
;
36567 add_private_clause
= NULL_TREE
;
36571 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36572 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36573 add_private_clause
= NULL_TREE
;
36574 c
= &OMP_CLAUSE_CHAIN (*c
);
36578 if (add_private_clause
)
36581 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36583 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36584 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36585 && OMP_CLAUSE_DECL (c
) == decl
)
36587 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36588 && OMP_CLAUSE_DECL (c
) == decl
)
36589 error_at (loc
, "iteration variable %qD "
36590 "should not be firstprivate",
36592 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36593 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36594 && OMP_CLAUSE_DECL (c
) == decl
)
36595 error_at (loc
, "iteration variable %qD should not be reduction",
36600 if (code
!= OMP_SIMD
)
36601 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36602 else if (collapse
== 1)
36603 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36605 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36606 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36607 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36610 OMP_CLAUSE_CHAIN (c
) = clauses
;
36612 /* For linear, signal that we need to fill up
36613 the so far unknown linear step. */
36614 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36615 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36621 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36622 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36623 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36628 /* If decl is an iterator, preserve the operator on decl
36629 until finish_omp_for. */
36631 && ((processing_template_decl
36632 && (TREE_TYPE (real_decl
) == NULL_TREE
36633 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36634 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36635 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36637 incr
= cp_parser_expression (parser
);
36638 if (!EXPR_HAS_LOCATION (incr
))
36639 protected_set_expr_location (incr
, input_location
);
36643 if (!parens
.require_close (parser
))
36644 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36645 /*or_comma=*/false,
36646 /*consume_paren=*/true);
36648 TREE_VEC_ELT (declv
, i
) = decl
;
36649 TREE_VEC_ELT (initv
, i
) = init
;
36650 TREE_VEC_ELT (condv
, i
) = cond
;
36651 TREE_VEC_ELT (incrv
, i
) = incr
;
36654 orig_inits
.safe_grow_cleared (i
+ 1);
36655 orig_inits
[i
] = orig_init
;
36660 orig_declv
= copy_node (declv
);
36661 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36663 else if (orig_declv
)
36664 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36666 if (i
== count
- 1)
36669 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36670 in between the collapsed for loops to be still considered perfectly
36671 nested. Hopefully the final version clarifies this.
36672 For now handle (multiple) {'s and empty statements. */
36673 cp_parser_parse_tentatively (parser
);
36676 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36678 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36680 cp_lexer_consume_token (parser
->lexer
);
36683 else if (bracecount
36684 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36685 cp_lexer_consume_token (parser
->lexer
);
36688 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36689 error_at (loc
, "not enough for loops to collapse");
36690 collapse_err
= true;
36691 cp_parser_abort_tentative_parse (parser
);
36699 cp_parser_parse_definitely (parser
);
36700 nbraces
+= bracecount
;
36707 /* Note that we saved the original contents of this flag when we entered
36708 the structured block, and so we don't need to re-save it here. */
36709 parser
->in_statement
= IN_OMP_FOR
;
36711 /* Note that the grammar doesn't call for a structured block here,
36712 though the loop as a whole is a structured block. */
36715 body
= begin_omp_structured_block ();
36716 for (i
= 0; i
< count
; i
++)
36717 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
36718 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
36719 TREE_VEC_ELT (declv
, i
));
36722 body
= push_stmt_list ();
36723 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36725 body
= finish_omp_structured_block (body
);
36727 body
= pop_stmt_list (body
);
36729 if (declv
== NULL_TREE
)
36732 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
36733 incrv
, body
, pre_body
, &orig_inits
, clauses
);
36737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36739 cp_lexer_consume_token (parser
->lexer
);
36742 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36743 cp_lexer_consume_token (parser
->lexer
);
36748 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36749 "collapsed loops not perfectly nested");
36751 collapse_err
= true;
36752 cp_parser_statement_seq_opt (parser
, NULL
);
36753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
36758 while (!for_block
->is_empty ())
36760 tree t
= for_block
->pop ();
36761 if (TREE_CODE (t
) == STATEMENT_LIST
)
36762 add_stmt (pop_stmt_list (t
));
36766 release_tree_vector (for_block
);
36771 /* Helper function for OpenMP parsing, split clauses and call
36772 finish_omp_clauses on each of the set of clauses afterwards. */
36775 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
36776 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
36779 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
36780 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
36782 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
36786 #pragma omp simd simd-clause[optseq] new-line
36789 #define OMP_SIMD_CLAUSE_MASK \
36790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36802 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36803 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36806 tree clauses
, sb
, ret
;
36808 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36810 strcat (p_name
, " simd");
36811 mask
|= OMP_SIMD_CLAUSE_MASK
;
36813 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36817 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
36818 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
36819 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
36820 OMP_CLAUSE_ORDERED
);
36821 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
36823 error_at (OMP_CLAUSE_LOCATION (c
),
36824 "%<ordered%> clause with parameter may not be specified "
36825 "on %qs construct", p_name
);
36826 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
36830 keep_next_level (true);
36831 sb
= begin_omp_structured_block ();
36832 save
= cp_parser_begin_omp_structured_block (parser
);
36834 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
36836 cp_parser_end_omp_structured_block (parser
, save
);
36837 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36843 #pragma omp for for-clause[optseq] new-line
36847 #pragma omp for simd for-simd-clause[optseq] new-line
36850 #define OMP_FOR_CLAUSE_MASK \
36851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36862 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
36863 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36866 tree clauses
, sb
, ret
;
36868 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36870 strcat (p_name
, " for");
36871 mask
|= OMP_FOR_CLAUSE_MASK
;
36872 /* parallel for{, simd} disallows nowait clause, but for
36873 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36874 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
36875 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
36876 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36877 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36878 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
36880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36882 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36883 const char *p
= IDENTIFIER_POINTER (id
);
36885 if (strcmp (p
, "simd") == 0)
36887 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36888 if (cclauses
== NULL
)
36889 cclauses
= cclauses_buf
;
36891 cp_lexer_consume_token (parser
->lexer
);
36892 if (!flag_openmp
) /* flag_openmp_simd */
36893 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36895 sb
= begin_omp_structured_block ();
36896 save
= cp_parser_begin_omp_structured_block (parser
);
36897 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36899 cp_parser_end_omp_structured_block (parser
, save
);
36900 tree body
= finish_omp_structured_block (sb
);
36903 ret
= make_node (OMP_FOR
);
36904 TREE_TYPE (ret
) = void_type_node
;
36905 OMP_FOR_BODY (ret
) = body
;
36906 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36907 SET_EXPR_LOCATION (ret
, loc
);
36912 if (!flag_openmp
) /* flag_openmp_simd */
36914 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36918 /* Composite distribute parallel for disallows linear clause. */
36919 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36920 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
36922 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36926 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
36927 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36930 keep_next_level (true);
36931 sb
= begin_omp_structured_block ();
36932 save
= cp_parser_begin_omp_structured_block (parser
);
36934 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
36936 cp_parser_end_omp_structured_block (parser
, save
);
36937 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
36942 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
36943 omp_clause_mask
, tree
*, bool *);
36946 # pragma omp master new-line
36947 structured-block */
36950 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
36951 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36954 tree clauses
, sb
, ret
;
36956 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36958 strcat (p_name
, " master");
36960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36962 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36963 const char *p
= IDENTIFIER_POINTER (id
);
36965 if (strcmp (p
, "taskloop") == 0)
36967 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36968 if (cclauses
== NULL
)
36969 cclauses
= cclauses_buf
;
36971 cp_lexer_consume_token (parser
->lexer
);
36972 if (!flag_openmp
) /* flag_openmp_simd */
36973 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36975 sb
= begin_omp_structured_block ();
36976 save
= cp_parser_begin_omp_structured_block (parser
);
36977 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
36979 cp_parser_end_omp_structured_block (parser
, save
);
36980 tree body
= finish_omp_structured_block (sb
);
36983 return c_finish_omp_master (loc
, body
);
36986 if (!flag_openmp
) /* flag_openmp_simd */
36988 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36994 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36996 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
36999 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37001 return c_finish_omp_master (loc
,
37002 cp_parser_omp_structured_block (parser
, if_p
));
37006 # pragma omp ordered new-line
37010 # pragma omp ordered ordered-clauses new-line
37011 structured-block */
37013 #define OMP_ORDERED_CLAUSE_MASK \
37014 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37017 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37018 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37021 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37022 enum pragma_context context
, bool *if_p
)
37024 location_t loc
= pragma_tok
->location
;
37026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37028 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37029 const char *p
= IDENTIFIER_POINTER (id
);
37031 if (strcmp (p
, "depend") == 0)
37033 if (!flag_openmp
) /* flag_openmp_simd */
37035 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37038 if (context
== pragma_stmt
)
37040 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37041 "%<depend%> clause may only be used in compound "
37043 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37047 = cp_parser_omp_all_clauses (parser
,
37048 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37049 "#pragma omp ordered", pragma_tok
);
37050 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37056 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37057 "#pragma omp ordered", pragma_tok
);
37059 if (!flag_openmp
/* flag_openmp_simd */
37060 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37063 c_finish_omp_ordered (loc
, clauses
,
37064 cp_parser_omp_structured_block (parser
, if_p
));
37071 { section-sequence }
37074 section-directive[opt] structured-block
37075 section-sequence section-directive structured-block */
37078 cp_parser_omp_sections_scope (cp_parser
*parser
)
37080 tree stmt
, substmt
;
37081 bool error_suppress
= false;
37084 matching_braces braces
;
37085 if (!braces
.require_open (parser
))
37088 stmt
= push_stmt_list ();
37090 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37091 != PRAGMA_OMP_SECTION
)
37093 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37094 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37095 add_stmt (substmt
);
37100 tok
= cp_lexer_peek_token (parser
->lexer
);
37101 if (tok
->type
== CPP_CLOSE_BRACE
)
37103 if (tok
->type
== CPP_EOF
)
37106 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37108 cp_lexer_consume_token (parser
->lexer
);
37109 cp_parser_require_pragma_eol (parser
, tok
);
37110 error_suppress
= false;
37112 else if (!error_suppress
)
37114 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37115 error_suppress
= true;
37118 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37119 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37120 add_stmt (substmt
);
37122 braces
.require_close (parser
);
37124 substmt
= pop_stmt_list (stmt
);
37126 stmt
= make_node (OMP_SECTIONS
);
37127 TREE_TYPE (stmt
) = void_type_node
;
37128 OMP_SECTIONS_BODY (stmt
) = substmt
;
37135 # pragma omp sections sections-clause[optseq] newline
37138 #define OMP_SECTIONS_CLAUSE_MASK \
37139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37146 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37147 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37150 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37152 strcat (p_name
, " sections");
37153 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37155 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37157 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37161 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37162 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37165 ret
= cp_parser_omp_sections_scope (parser
);
37167 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37173 # pragma omp parallel parallel-clause[optseq] new-line
37175 # pragma omp parallel for parallel-for-clause[optseq] new-line
37177 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37181 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37182 structured-block */
37184 #define OMP_PARALLEL_CLAUSE_MASK \
37185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37196 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37197 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37200 tree stmt
, clauses
, block
;
37202 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37204 strcat (p_name
, " parallel");
37205 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37206 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37207 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37208 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37209 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37211 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37213 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37214 if (cclauses
== NULL
)
37215 cclauses
= cclauses_buf
;
37217 cp_lexer_consume_token (parser
->lexer
);
37218 if (!flag_openmp
) /* flag_openmp_simd */
37219 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37221 block
= begin_omp_parallel ();
37222 save
= cp_parser_begin_omp_structured_block (parser
);
37223 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37225 cp_parser_end_omp_structured_block (parser
, save
);
37226 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37228 if (ret
== NULL_TREE
)
37230 OMP_PARALLEL_COMBINED (stmt
) = 1;
37233 /* When combined with distribute, parallel has to be followed by for.
37234 #pragma omp target parallel is allowed though. */
37236 && (mask
& (OMP_CLAUSE_MASK_1
37237 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37239 error_at (loc
, "expected %<for%> after %qs", p_name
);
37240 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37243 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37245 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37246 const char *p
= IDENTIFIER_POINTER (id
);
37247 if (strcmp (p
, "master") == 0)
37249 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37250 cclauses
= cclauses_buf
;
37252 cp_lexer_consume_token (parser
->lexer
);
37253 block
= begin_omp_parallel ();
37254 save
= cp_parser_begin_omp_structured_block (parser
);
37255 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37257 cp_parser_end_omp_structured_block (parser
, save
);
37258 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37260 OMP_PARALLEL_COMBINED (stmt
) = 1;
37261 if (ret
== NULL_TREE
)
37265 else if (!flag_openmp
) /* flag_openmp_simd */
37267 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37270 else if (strcmp (p
, "sections") == 0)
37272 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37273 cclauses
= cclauses_buf
;
37275 cp_lexer_consume_token (parser
->lexer
);
37276 block
= begin_omp_parallel ();
37277 save
= cp_parser_begin_omp_structured_block (parser
);
37278 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37279 cp_parser_end_omp_structured_block (parser
, save
);
37280 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37282 OMP_PARALLEL_COMBINED (stmt
) = 1;
37286 else if (!flag_openmp
) /* flag_openmp_simd */
37288 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37292 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37296 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37297 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37300 block
= begin_omp_parallel ();
37301 save
= cp_parser_begin_omp_structured_block (parser
);
37302 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37303 cp_parser_end_omp_structured_block (parser
, save
);
37304 stmt
= finish_omp_parallel (clauses
, block
);
37309 # pragma omp single single-clause[optseq] new-line
37310 structured-block */
37312 #define OMP_SINGLE_CLAUSE_MASK \
37313 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37319 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37321 tree stmt
= make_node (OMP_SINGLE
);
37322 TREE_TYPE (stmt
) = void_type_node
;
37323 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37325 OMP_SINGLE_CLAUSES (stmt
)
37326 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37327 "#pragma omp single", pragma_tok
);
37328 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37330 return add_stmt (stmt
);
37334 # pragma omp task task-clause[optseq] new-line
37335 structured-block */
37337 #define OMP_TASK_CLAUSE_MASK \
37338 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37351 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37353 tree clauses
, block
;
37356 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37357 "#pragma omp task", pragma_tok
);
37358 block
= begin_omp_task ();
37359 save
= cp_parser_begin_omp_structured_block (parser
);
37360 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37361 cp_parser_end_omp_structured_block (parser
, save
);
37362 return finish_omp_task (clauses
, block
);
37366 # pragma omp taskwait new-line
37369 # pragma omp taskwait taskwait-clause[opt] new-line */
37371 #define OMP_TASKWAIT_CLAUSE_MASK \
37372 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37375 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37378 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37379 "#pragma omp taskwait", pragma_tok
);
37383 tree stmt
= make_node (OMP_TASK
);
37384 TREE_TYPE (stmt
) = void_node
;
37385 OMP_TASK_CLAUSES (stmt
) = clauses
;
37386 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37387 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37391 finish_omp_taskwait ();
37395 # pragma omp taskyield new-line */
37398 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37400 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37401 finish_omp_taskyield ();
37405 # pragma omp taskgroup new-line
37409 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37411 #define OMP_TASKGROUP_CLAUSE_MASK \
37412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37415 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37418 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37419 "#pragma omp taskgroup", pragma_tok
);
37420 return c_finish_omp_taskgroup (input_location
,
37421 cp_parser_omp_structured_block (parser
,
37428 # pragma omp threadprivate (variable-list) */
37431 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37435 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37436 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37438 finish_omp_threadprivate (vars
);
37442 # pragma omp cancel cancel-clause[optseq] new-line */
37444 #define OMP_CANCEL_CLAUSE_MASK \
37445 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37452 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37454 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37455 "#pragma omp cancel", pragma_tok
);
37456 finish_omp_cancel (clauses
);
37460 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37462 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37469 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37470 enum pragma_context context
)
37473 bool point_seen
= false;
37475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37477 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37478 const char *p
= IDENTIFIER_POINTER (id
);
37480 if (strcmp (p
, "point") == 0)
37482 cp_lexer_consume_token (parser
->lexer
);
37488 cp_parser_error (parser
, "expected %<point%>");
37489 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37493 if (context
!= pragma_compound
)
37495 if (context
== pragma_stmt
)
37496 error_at (pragma_tok
->location
,
37497 "%<#pragma %s%> may only be used in compound statements",
37498 "omp cancellation point");
37500 cp_parser_error (parser
, "expected declaration specifiers");
37501 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37505 clauses
= cp_parser_omp_all_clauses (parser
,
37506 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37507 "#pragma omp cancellation point",
37509 finish_omp_cancellation_point (clauses
);
37513 #pragma omp distribute distribute-clause[optseq] new-line
37516 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37524 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37525 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37528 tree clauses
, sb
, ret
;
37530 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37532 strcat (p_name
, " distribute");
37533 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37537 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37538 const char *p
= IDENTIFIER_POINTER (id
);
37540 bool parallel
= false;
37542 if (strcmp (p
, "simd") == 0)
37545 parallel
= strcmp (p
, "parallel") == 0;
37546 if (parallel
|| simd
)
37548 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37549 if (cclauses
== NULL
)
37550 cclauses
= cclauses_buf
;
37551 cp_lexer_consume_token (parser
->lexer
);
37552 if (!flag_openmp
) /* flag_openmp_simd */
37555 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37558 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37561 sb
= begin_omp_structured_block ();
37562 save
= cp_parser_begin_omp_structured_block (parser
);
37564 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37567 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37569 cp_parser_end_omp_structured_block (parser
, save
);
37570 tree body
= finish_omp_structured_block (sb
);
37573 ret
= make_node (OMP_DISTRIBUTE
);
37574 TREE_TYPE (ret
) = void_type_node
;
37575 OMP_FOR_BODY (ret
) = body
;
37576 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37577 SET_EXPR_LOCATION (ret
, loc
);
37582 if (!flag_openmp
) /* flag_openmp_simd */
37584 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37588 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37592 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37593 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37596 keep_next_level (true);
37597 sb
= begin_omp_structured_block ();
37598 save
= cp_parser_begin_omp_structured_block (parser
);
37600 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37602 cp_parser_end_omp_structured_block (parser
, save
);
37603 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37609 # pragma omp teams teams-clause[optseq] new-line
37610 structured-block */
37612 #define OMP_TEAMS_CLAUSE_MASK \
37613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37622 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37623 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37626 tree clauses
, sb
, ret
;
37628 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37630 strcat (p_name
, " teams");
37631 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37635 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37636 const char *p
= IDENTIFIER_POINTER (id
);
37637 if (strcmp (p
, "distribute") == 0)
37639 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37640 if (cclauses
== NULL
)
37641 cclauses
= cclauses_buf
;
37643 cp_lexer_consume_token (parser
->lexer
);
37644 if (!flag_openmp
) /* flag_openmp_simd */
37645 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37647 keep_next_level (true);
37648 sb
= begin_omp_structured_block ();
37649 save
= cp_parser_begin_omp_structured_block (parser
);
37650 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37652 cp_parser_end_omp_structured_block (parser
, save
);
37653 tree body
= finish_omp_structured_block (sb
);
37656 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37657 ret
= make_node (OMP_TEAMS
);
37658 TREE_TYPE (ret
) = void_type_node
;
37659 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37660 OMP_TEAMS_BODY (ret
) = body
;
37661 OMP_TEAMS_COMBINED (ret
) = 1;
37662 SET_EXPR_LOCATION (ret
, loc
);
37663 return add_stmt (ret
);
37666 if (!flag_openmp
) /* flag_openmp_simd */
37668 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37672 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37676 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
37677 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37680 tree stmt
= make_node (OMP_TEAMS
);
37681 TREE_TYPE (stmt
) = void_type_node
;
37682 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
37683 keep_next_level (true);
37684 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37685 SET_EXPR_LOCATION (stmt
, loc
);
37687 return add_stmt (stmt
);
37691 # pragma omp target data target-data-clause[optseq] new-line
37692 structured-block */
37694 #define OMP_TARGET_DATA_CLAUSE_MASK \
37695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37701 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37704 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
37705 "#pragma omp target data", pragma_tok
);
37707 for (tree
*pc
= &clauses
; *pc
;)
37709 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37710 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37713 case GOMP_MAP_ALWAYS_TO
:
37714 case GOMP_MAP_FROM
:
37715 case GOMP_MAP_ALWAYS_FROM
:
37716 case GOMP_MAP_TOFROM
:
37717 case GOMP_MAP_ALWAYS_TOFROM
:
37718 case GOMP_MAP_ALLOC
:
37721 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37722 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37723 case GOMP_MAP_ALWAYS_POINTER
:
37727 error_at (OMP_CLAUSE_LOCATION (*pc
),
37728 "%<#pragma omp target data%> with map-type other "
37729 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37730 "on %<map%> clause");
37731 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37734 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
37736 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37742 error_at (pragma_tok
->location
,
37743 "%<#pragma omp target data%> must contain at least "
37744 "one %<map%> or %<use_device_ptr%> clause");
37748 tree stmt
= make_node (OMP_TARGET_DATA
);
37749 TREE_TYPE (stmt
) = void_type_node
;
37750 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
37752 keep_next_level (true);
37753 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37755 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37756 return add_stmt (stmt
);
37760 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37761 structured-block */
37763 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37771 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37772 enum pragma_context context
)
37774 bool data_seen
= false;
37775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37777 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37778 const char *p
= IDENTIFIER_POINTER (id
);
37780 if (strcmp (p
, "data") == 0)
37782 cp_lexer_consume_token (parser
->lexer
);
37788 cp_parser_error (parser
, "expected %<data%>");
37789 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37793 if (context
== pragma_stmt
)
37795 error_at (pragma_tok
->location
,
37796 "%<#pragma %s%> may only be used in compound statements",
37797 "omp target enter data");
37798 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37803 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
37804 "#pragma omp target enter data", pragma_tok
);
37806 for (tree
*pc
= &clauses
; *pc
;)
37808 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37809 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37812 case GOMP_MAP_ALWAYS_TO
:
37813 case GOMP_MAP_ALLOC
:
37816 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37817 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37818 case GOMP_MAP_ALWAYS_POINTER
:
37822 error_at (OMP_CLAUSE_LOCATION (*pc
),
37823 "%<#pragma omp target enter data%> with map-type other "
37824 "than %<to%> or %<alloc%> on %<map%> clause");
37825 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37828 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37834 error_at (pragma_tok
->location
,
37835 "%<#pragma omp target enter data%> must contain at least "
37836 "one %<map%> clause");
37840 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
37841 TREE_TYPE (stmt
) = void_type_node
;
37842 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
37843 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37844 return add_stmt (stmt
);
37848 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37849 structured-block */
37851 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37852 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37859 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37860 enum pragma_context context
)
37862 bool data_seen
= false;
37863 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37865 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37866 const char *p
= IDENTIFIER_POINTER (id
);
37868 if (strcmp (p
, "data") == 0)
37870 cp_lexer_consume_token (parser
->lexer
);
37876 cp_parser_error (parser
, "expected %<data%>");
37877 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37881 if (context
== pragma_stmt
)
37883 error_at (pragma_tok
->location
,
37884 "%<#pragma %s%> may only be used in compound statements",
37885 "omp target exit data");
37886 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37891 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
37892 "#pragma omp target exit data", pragma_tok
);
37894 for (tree
*pc
= &clauses
; *pc
;)
37896 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37897 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37899 case GOMP_MAP_FROM
:
37900 case GOMP_MAP_ALWAYS_FROM
:
37901 case GOMP_MAP_RELEASE
:
37902 case GOMP_MAP_DELETE
:
37905 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37906 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37907 case GOMP_MAP_ALWAYS_POINTER
:
37911 error_at (OMP_CLAUSE_LOCATION (*pc
),
37912 "%<#pragma omp target exit data%> with map-type other "
37913 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37915 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37918 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37924 error_at (pragma_tok
->location
,
37925 "%<#pragma omp target exit data%> must contain at least "
37926 "one %<map%> clause");
37930 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
37931 TREE_TYPE (stmt
) = void_type_node
;
37932 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
37933 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37934 return add_stmt (stmt
);
37938 # pragma omp target update target-update-clause[optseq] new-line */
37940 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37941 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37949 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
37950 enum pragma_context context
)
37952 if (context
== pragma_stmt
)
37954 error_at (pragma_tok
->location
,
37955 "%<#pragma %s%> may only be used in compound statements",
37956 "omp target update");
37957 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37962 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
37963 "#pragma omp target update", pragma_tok
);
37964 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
37965 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
37967 error_at (pragma_tok
->location
,
37968 "%<#pragma omp target update%> must contain at least one "
37969 "%<from%> or %<to%> clauses");
37973 tree stmt
= make_node (OMP_TARGET_UPDATE
);
37974 TREE_TYPE (stmt
) = void_type_node
;
37975 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
37976 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37982 # pragma omp target target-clause[optseq] new-line
37983 structured-block */
37985 #define OMP_TARGET_CLAUSE_MASK \
37986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37997 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
37998 enum pragma_context context
, bool *if_p
)
38000 tree
*pc
= NULL
, stmt
;
38004 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38008 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38009 const char *p
= IDENTIFIER_POINTER (id
);
38010 enum tree_code ccode
= ERROR_MARK
;
38012 if (strcmp (p
, "teams") == 0)
38014 else if (strcmp (p
, "parallel") == 0)
38015 ccode
= OMP_PARALLEL
;
38016 else if (strcmp (p
, "simd") == 0)
38018 if (ccode
!= ERROR_MARK
)
38020 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38021 char p_name
[sizeof ("#pragma omp target teams distribute "
38022 "parallel for simd")];
38024 cp_lexer_consume_token (parser
->lexer
);
38025 strcpy (p_name
, "#pragma omp target");
38026 if (!flag_openmp
) /* flag_openmp_simd */
38032 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38033 OMP_TARGET_CLAUSE_MASK
,
38037 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38038 OMP_TARGET_CLAUSE_MASK
,
38042 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38043 OMP_TARGET_CLAUSE_MASK
,
38047 gcc_unreachable ();
38049 return stmt
!= NULL_TREE
;
38051 keep_next_level (true);
38052 tree sb
= begin_omp_structured_block (), ret
;
38053 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38057 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38058 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38062 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38063 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38067 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38068 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38072 gcc_unreachable ();
38074 cp_parser_end_omp_structured_block (parser
, save
);
38075 tree body
= finish_omp_structured_block (sb
);
38076 if (ret
== NULL_TREE
)
38078 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38080 /* For combined target teams, ensure the num_teams and
38081 thread_limit clause expressions are evaluated on the host,
38082 before entering the target construct. */
38084 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38085 c
; c
= OMP_CLAUSE_CHAIN (c
))
38086 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38087 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38088 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38090 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38091 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38092 if (expr
== error_mark_node
)
38094 tree tmp
= TARGET_EXPR_SLOT (expr
);
38096 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38097 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38098 OMP_CLAUSE_FIRSTPRIVATE
);
38099 OMP_CLAUSE_DECL (tc
) = tmp
;
38100 OMP_CLAUSE_CHAIN (tc
)
38101 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38102 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38105 tree stmt
= make_node (OMP_TARGET
);
38106 TREE_TYPE (stmt
) = void_type_node
;
38107 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38108 OMP_TARGET_BODY (stmt
) = body
;
38109 OMP_TARGET_COMBINED (stmt
) = 1;
38110 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38112 pc
= &OMP_TARGET_CLAUSES (stmt
);
38113 goto check_clauses
;
38115 else if (!flag_openmp
) /* flag_openmp_simd */
38117 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38120 else if (strcmp (p
, "data") == 0)
38122 cp_lexer_consume_token (parser
->lexer
);
38123 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38126 else if (strcmp (p
, "enter") == 0)
38128 cp_lexer_consume_token (parser
->lexer
);
38129 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38132 else if (strcmp (p
, "exit") == 0)
38134 cp_lexer_consume_token (parser
->lexer
);
38135 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38138 else if (strcmp (p
, "update") == 0)
38140 cp_lexer_consume_token (parser
->lexer
);
38141 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38144 if (!flag_openmp
) /* flag_openmp_simd */
38146 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38150 stmt
= make_node (OMP_TARGET
);
38151 TREE_TYPE (stmt
) = void_type_node
;
38153 OMP_TARGET_CLAUSES (stmt
)
38154 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38155 "#pragma omp target", pragma_tok
);
38156 pc
= &OMP_TARGET_CLAUSES (stmt
);
38157 keep_next_level (true);
38158 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38160 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38166 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38167 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38170 case GOMP_MAP_ALWAYS_TO
:
38171 case GOMP_MAP_FROM
:
38172 case GOMP_MAP_ALWAYS_FROM
:
38173 case GOMP_MAP_TOFROM
:
38174 case GOMP_MAP_ALWAYS_TOFROM
:
38175 case GOMP_MAP_ALLOC
:
38176 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38177 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38178 case GOMP_MAP_ALWAYS_POINTER
:
38181 error_at (OMP_CLAUSE_LOCATION (*pc
),
38182 "%<#pragma omp target%> with map-type other "
38183 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38184 "on %<map%> clause");
38185 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38188 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38194 # pragma acc cache (variable-list) new-line
38198 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38200 tree stmt
, clauses
;
38202 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38203 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38205 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38207 stmt
= make_node (OACC_CACHE
);
38208 TREE_TYPE (stmt
) = void_type_node
;
38209 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38210 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38217 # pragma acc data oacc-data-clause[optseq] new-line
38218 structured-block */
38220 #define OACC_DATA_CLAUSE_MASK \
38221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38230 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38232 tree stmt
, clauses
, block
;
38235 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38236 "#pragma acc data", pragma_tok
);
38238 block
= begin_omp_parallel ();
38239 save
= cp_parser_begin_omp_structured_block (parser
);
38240 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38241 cp_parser_end_omp_structured_block (parser
, save
);
38242 stmt
= finish_oacc_data (clauses
, block
);
38247 # pragma acc host_data <clauses> new-line
38248 structured-block */
38250 #define OACC_HOST_DATA_CLAUSE_MASK \
38251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38254 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38256 tree stmt
, clauses
, block
;
38259 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38260 "#pragma acc host_data", pragma_tok
);
38262 block
= begin_omp_parallel ();
38263 save
= cp_parser_begin_omp_structured_block (parser
);
38264 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38265 cp_parser_end_omp_structured_block (parser
, save
);
38266 stmt
= finish_oacc_host_data (clauses
, block
);
38271 # pragma acc declare oacc-data-clause[optseq] new-line
38274 #define OACC_DECLARE_CLAUSE_MASK \
38275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38285 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38287 tree clauses
, stmt
;
38288 bool error
= false;
38290 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38291 "#pragma acc declare", pragma_tok
, true);
38294 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38296 error_at (pragma_tok
->location
,
38297 "no valid clauses specified in %<#pragma acc declare%>");
38301 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38303 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38304 tree decl
= OMP_CLAUSE_DECL (t
);
38305 if (!DECL_P (decl
))
38307 error_at (loc
, "array section in %<#pragma acc declare%>");
38311 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38312 switch (OMP_CLAUSE_MAP_KIND (t
))
38314 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38315 case GOMP_MAP_ALLOC
:
38317 case GOMP_MAP_FORCE_DEVICEPTR
:
38318 case GOMP_MAP_DEVICE_RESIDENT
:
38321 case GOMP_MAP_LINK
:
38322 if (!global_bindings_p ()
38323 && (TREE_STATIC (decl
)
38324 || !DECL_EXTERNAL (decl
)))
38327 "%qD must be a global variable in "
38328 "%<#pragma acc declare link%>",
38336 if (global_bindings_p ())
38338 error_at (loc
, "invalid OpenACC clause at file scope");
38342 if (DECL_EXTERNAL (decl
))
38345 "invalid use of %<extern%> variable %qD "
38346 "in %<#pragma acc declare%>", decl
);
38350 else if (TREE_PUBLIC (decl
))
38353 "invalid use of %<global%> variable %qD "
38354 "in %<#pragma acc declare%>", decl
);
38361 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38362 || lookup_attribute ("omp declare target link",
38363 DECL_ATTRIBUTES (decl
)))
38365 error_at (loc
, "variable %qD used more than once with "
38366 "%<#pragma acc declare%>", decl
);
38375 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38376 id
= get_identifier ("omp declare target link");
38378 id
= get_identifier ("omp declare target");
38380 DECL_ATTRIBUTES (decl
)
38381 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38382 if (global_bindings_p ())
38384 symtab_node
*node
= symtab_node::get (decl
);
38387 node
->offloadable
= 1;
38388 if (ENABLE_OFFLOADING
)
38390 g
->have_offload
= true;
38391 if (is_a
<varpool_node
*> (node
))
38392 vec_safe_push (offload_vars
, decl
);
38399 if (error
|| global_bindings_p ())
38402 stmt
= make_node (OACC_DECLARE
);
38403 TREE_TYPE (stmt
) = void_type_node
;
38404 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38405 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38413 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38417 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38419 LOC is the location of the #pragma token.
38422 #define OACC_ENTER_DATA_CLAUSE_MASK \
38423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38429 #define OACC_EXIT_DATA_CLAUSE_MASK \
38430 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38438 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38441 location_t loc
= pragma_tok
->location
;
38442 tree stmt
, clauses
;
38443 const char *p
= "";
38445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38446 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38448 if (strcmp (p
, "data") != 0)
38450 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38451 enter
? "enter" : "exit");
38452 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38456 cp_lexer_consume_token (parser
->lexer
);
38459 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38460 "#pragma acc enter data", pragma_tok
);
38462 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38463 "#pragma acc exit data", pragma_tok
);
38465 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38467 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38468 enter
? "enter" : "exit");
38472 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38473 TREE_TYPE (stmt
) = void_type_node
;
38474 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38475 SET_EXPR_LOCATION (stmt
, loc
);
38481 # pragma acc loop oacc-loop-clause[optseq] new-line
38482 structured-block */
38484 #define OACC_LOOP_CLAUSE_MASK \
38485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38497 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38498 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38500 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38502 strcat (p_name
, " loop");
38503 mask
|= OACC_LOOP_CLAUSE_MASK
;
38505 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38509 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38511 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38513 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38516 tree block
= begin_omp_structured_block ();
38517 int save
= cp_parser_begin_omp_structured_block (parser
);
38518 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38519 cp_parser_end_omp_structured_block (parser
, save
);
38520 add_stmt (finish_omp_structured_block (block
));
38526 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38531 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38535 #define OACC_KERNELS_CLAUSE_MASK \
38536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38550 #define OACC_PARALLEL_CLAUSE_MASK \
38551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38569 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38570 char *p_name
, bool *if_p
)
38572 omp_clause_mask mask
;
38573 enum tree_code code
;
38574 switch (cp_parser_pragma_kind (pragma_tok
))
38576 case PRAGMA_OACC_KERNELS
:
38577 strcat (p_name
, " kernels");
38578 mask
= OACC_KERNELS_CLAUSE_MASK
;
38579 code
= OACC_KERNELS
;
38581 case PRAGMA_OACC_PARALLEL
:
38582 strcat (p_name
, " parallel");
38583 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38584 code
= OACC_PARALLEL
;
38587 gcc_unreachable ();
38590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38593 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38594 if (strcmp (p
, "loop") == 0)
38596 cp_lexer_consume_token (parser
->lexer
);
38597 tree block
= begin_omp_parallel ();
38599 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
38601 return finish_omp_construct (code
, block
, clauses
);
38605 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38607 tree block
= begin_omp_parallel ();
38608 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38609 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38610 cp_parser_end_omp_structured_block (parser
, save
);
38611 return finish_omp_construct (code
, block
, clauses
);
38615 # pragma acc update oacc-update-clause[optseq] new-line
38618 #define OACC_UPDATE_CLAUSE_MASK \
38619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38627 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38629 tree stmt
, clauses
;
38631 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38632 "#pragma acc update", pragma_tok
);
38634 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38636 error_at (pragma_tok
->location
,
38637 "%<#pragma acc update%> must contain at least one "
38638 "%<device%> or %<host%> or %<self%> clause");
38642 stmt
= make_node (OACC_UPDATE
);
38643 TREE_TYPE (stmt
) = void_type_node
;
38644 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38645 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38651 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38653 LOC is the location of the #pragma token.
38656 #define OACC_WAIT_CLAUSE_MASK \
38657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38660 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38662 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38663 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38665 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38666 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
38668 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
38669 "#pragma acc wait", pragma_tok
);
38671 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
38672 stmt
= finish_expr_stmt (stmt
);
38678 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38680 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38689 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38690 enum pragma_context context
)
38692 bool first_p
= parser
->omp_declare_simd
== NULL
;
38693 cp_omp_declare_simd_data data
;
38696 data
.error_seen
= false;
38697 data
.fndecl_seen
= false;
38698 data
.tokens
= vNULL
;
38699 data
.clauses
= NULL_TREE
;
38700 /* It is safe to take the address of a local variable; it will only be
38701 used while this scope is live. */
38702 parser
->omp_declare_simd
= &data
;
38705 /* Store away all pragma tokens. */
38706 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38707 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38708 cp_lexer_consume_token (parser
->lexer
);
38709 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38710 parser
->omp_declare_simd
->error_seen
= true;
38711 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38712 struct cp_token_cache
*cp
38713 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38714 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
38718 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38719 cp_parser_pragma (parser
, context
, NULL
);
38722 case pragma_external
:
38723 cp_parser_declaration (parser
);
38725 case pragma_member
:
38726 cp_parser_member_declaration (parser
);
38728 case pragma_objc_icode
:
38729 cp_parser_block_declaration (parser
, /*statement_p=*/false);
38732 cp_parser_declaration_statement (parser
);
38735 if (parser
->omp_declare_simd
38736 && !parser
->omp_declare_simd
->error_seen
38737 && !parser
->omp_declare_simd
->fndecl_seen
)
38738 error_at (pragma_tok
->location
,
38739 "%<#pragma omp declare simd%> not immediately followed by "
38740 "function declaration or definition");
38741 data
.tokens
.release ();
38742 parser
->omp_declare_simd
= NULL
;
38746 /* Finalize #pragma omp declare simd clauses after direct declarator has
38747 been parsed, and put that into "omp declare simd" attribute. */
38750 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
38752 struct cp_token_cache
*ce
;
38753 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
38756 if (!data
->error_seen
&& data
->fndecl_seen
)
38758 error ("%<#pragma omp declare simd%> not immediately followed by "
38759 "a single function declaration or definition");
38760 data
->error_seen
= true;
38762 if (data
->error_seen
)
38765 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
38769 cp_parser_push_lexer_for_tokens (parser
, ce
);
38770 parser
->lexer
->in_pragma
= true;
38771 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38772 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38773 cp_lexer_consume_token (parser
->lexer
);
38774 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
38775 "#pragma omp declare simd", pragma_tok
);
38776 cp_parser_pop_lexer (parser
);
38778 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
38779 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
38780 TREE_CHAIN (c
) = attrs
;
38781 if (processing_template_decl
)
38782 ATTR_IS_DEPENDENT (c
) = 1;
38786 data
->fndecl_seen
= true;
38792 # pragma omp declare target new-line
38793 declarations and definitions
38794 # pragma omp end declare target new-line
38797 # pragma omp declare target ( extended-list ) new-line
38799 # pragma omp declare target declare-target-clauses[seq] new-line */
38801 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38806 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38808 tree clauses
= NULL_TREE
;
38809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38811 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
38812 "#pragma omp declare target", pragma_tok
);
38813 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38815 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
38817 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38818 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38822 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38823 scope_chain
->omp_declare_target_attribute
++;
38826 if (scope_chain
->omp_declare_target_attribute
)
38827 error_at (pragma_tok
->location
,
38828 "%<#pragma omp declare target%> with clauses in between "
38829 "%<#pragma omp declare target%> without clauses and "
38830 "%<#pragma omp end declare target%>");
38831 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
38833 tree t
= OMP_CLAUSE_DECL (c
), id
;
38834 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
38835 tree at2
= lookup_attribute ("omp declare target link",
38836 DECL_ATTRIBUTES (t
));
38837 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
38839 id
= get_identifier ("omp declare target link");
38840 std::swap (at1
, at2
);
38843 id
= get_identifier ("omp declare target");
38846 error_at (OMP_CLAUSE_LOCATION (c
),
38847 "%qD specified both in declare target %<link%> and %<to%>"
38853 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
38854 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
38857 symtab_node
*node
= symtab_node::get (t
);
38860 node
->offloadable
= 1;
38861 if (ENABLE_OFFLOADING
)
38863 g
->have_offload
= true;
38864 if (is_a
<varpool_node
*> (node
))
38865 vec_safe_push (offload_vars
, t
);
38873 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
38875 const char *p
= "";
38876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38878 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38879 p
= IDENTIFIER_POINTER (id
);
38881 if (strcmp (p
, "declare") == 0)
38883 cp_lexer_consume_token (parser
->lexer
);
38885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38887 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38888 p
= IDENTIFIER_POINTER (id
);
38890 if (strcmp (p
, "target") == 0)
38891 cp_lexer_consume_token (parser
->lexer
);
38894 cp_parser_error (parser
, "expected %<target%>");
38895 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38901 cp_parser_error (parser
, "expected %<declare%>");
38902 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38905 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38906 if (!scope_chain
->omp_declare_target_attribute
)
38907 error_at (pragma_tok
->location
,
38908 "%<#pragma omp end declare target%> without corresponding "
38909 "%<#pragma omp declare target%>");
38911 scope_chain
->omp_declare_target_attribute
--;
38914 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38915 expression and optional initializer clause of
38916 #pragma omp declare reduction. We store the expression(s) as
38917 either 3, 6 or 7 special statements inside of the artificial function's
38918 body. The first two statements are DECL_EXPRs for the artificial
38919 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38920 expression that uses those variables.
38921 If there was any INITIALIZER clause, this is followed by further statements,
38922 the fourth and fifth statements are DECL_EXPRs for the artificial
38923 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38924 constructor variant (first token after open paren is not omp_priv),
38925 then the sixth statement is a statement with the function call expression
38926 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38927 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38928 to initialize the OMP_PRIV artificial variable and there is seventh
38929 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38932 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
38934 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
38935 gcc_assert (TYPE_REF_P (type
));
38936 type
= TREE_TYPE (type
);
38937 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
38938 DECL_ARTIFICIAL (omp_out
) = 1;
38939 pushdecl (omp_out
);
38940 add_decl_expr (omp_out
);
38941 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
38942 DECL_ARTIFICIAL (omp_in
) = 1;
38944 add_decl_expr (omp_in
);
38946 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
38948 keep_next_level (true);
38949 tree block
= begin_omp_structured_block ();
38950 combiner
= cp_parser_expression (parser
);
38951 finish_expr_stmt (combiner
);
38952 block
= finish_omp_structured_block (block
);
38955 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38958 const char *p
= "";
38959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38961 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38962 p
= IDENTIFIER_POINTER (id
);
38965 if (strcmp (p
, "initializer") == 0)
38967 cp_lexer_consume_token (parser
->lexer
);
38968 matching_parens parens
;
38969 if (!parens
.require_open (parser
))
38973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38976 p
= IDENTIFIER_POINTER (id
);
38979 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
38980 DECL_ARTIFICIAL (omp_priv
) = 1;
38981 pushdecl (omp_priv
);
38982 add_decl_expr (omp_priv
);
38983 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
38984 DECL_ARTIFICIAL (omp_orig
) = 1;
38985 pushdecl (omp_orig
);
38986 add_decl_expr (omp_orig
);
38988 keep_next_level (true);
38989 block
= begin_omp_structured_block ();
38992 if (strcmp (p
, "omp_priv") == 0)
38994 bool is_direct_init
, is_non_constant_init
;
38996 cp_lexer_consume_token (parser
->lexer
);
38997 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
38999 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39000 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39002 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39003 == CPP_CLOSE_PAREN
))
39005 finish_omp_structured_block (block
);
39006 error ("invalid initializer clause");
39009 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39010 &is_non_constant_init
);
39011 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39012 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39016 cp_parser_parse_tentatively (parser
);
39017 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39018 /*check_dependency_p=*/true,
39019 /*template_p=*/NULL
,
39020 /*declarator_p=*/false,
39021 /*optional_p=*/false);
39022 vec
<tree
, va_gc
> *args
;
39023 if (fn_name
== error_mark_node
39024 || cp_parser_error_occurred (parser
)
39025 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39026 || ((args
= cp_parser_parenthesized_expression_list
39027 (parser
, non_attr
, /*cast_p=*/false,
39028 /*allow_expansion_p=*/true,
39029 /*non_constant_p=*/NULL
)),
39030 cp_parser_error_occurred (parser
)))
39032 finish_omp_structured_block (block
);
39033 cp_parser_abort_tentative_parse (parser
);
39034 cp_parser_error (parser
, "expected id-expression (arguments)");
39039 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39040 if (arg
== omp_priv
39041 || (TREE_CODE (arg
) == ADDR_EXPR
39042 && TREE_OPERAND (arg
, 0) == omp_priv
))
39044 cp_parser_abort_tentative_parse (parser
);
39045 if (arg
== NULL_TREE
)
39046 error ("one of the initializer call arguments should be %<omp_priv%>"
39047 " or %<&omp_priv%>");
39048 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39050 finish_expr_stmt (initializer
);
39053 block
= finish_omp_structured_block (block
);
39054 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39058 add_decl_expr (omp_orig
);
39060 if (!parens
.require_close (parser
))
39064 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39065 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39072 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39073 initializer-clause[opt] new-line
39075 initializer-clause:
39076 initializer (omp_priv initializer)
39077 initializer (function-name (argument-list)) */
39080 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39081 enum pragma_context
)
39083 auto_vec
<tree
> types
;
39084 enum tree_code reduc_code
= ERROR_MARK
;
39085 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39087 cp_token
*first_token
;
39088 cp_token_cache
*cp
;
39092 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39093 p
= obstack_alloc (&declarator_obstack
, 0);
39095 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39098 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39101 reduc_code
= PLUS_EXPR
;
39104 reduc_code
= MULT_EXPR
;
39107 reduc_code
= MINUS_EXPR
;
39110 reduc_code
= BIT_AND_EXPR
;
39113 reduc_code
= BIT_XOR_EXPR
;
39116 reduc_code
= BIT_IOR_EXPR
;
39119 reduc_code
= TRUTH_ANDIF_EXPR
;
39122 reduc_code
= TRUTH_ORIF_EXPR
;
39125 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39128 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39129 "%<|%>, %<&&%>, %<||%> or identifier");
39133 if (reduc_code
!= ERROR_MARK
)
39134 cp_lexer_consume_token (parser
->lexer
);
39136 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39137 if (reduc_id
== error_mark_node
)
39140 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39143 /* Types may not be defined in declare reduction type list. */
39144 const char *saved_message
;
39145 saved_message
= parser
->type_definition_forbidden_message
;
39146 parser
->type_definition_forbidden_message
39147 = G_("types may not be defined in declare reduction type list");
39148 bool saved_colon_corrects_to_scope_p
;
39149 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39150 parser
->colon_corrects_to_scope_p
= false;
39151 bool saved_colon_doesnt_start_class_def_p
;
39152 saved_colon_doesnt_start_class_def_p
39153 = parser
->colon_doesnt_start_class_def_p
;
39154 parser
->colon_doesnt_start_class_def_p
= true;
39158 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39159 type
= cp_parser_type_id (parser
);
39160 if (type
== error_mark_node
)
39162 else if (ARITHMETIC_TYPE_P (type
)
39163 && (orig_reduc_id
== NULL_TREE
39164 || (TREE_CODE (type
) != COMPLEX_TYPE
39165 && (id_equal (orig_reduc_id
, "min")
39166 || id_equal (orig_reduc_id
, "max")))))
39167 error_at (loc
, "predeclared arithmetic type %qT in "
39168 "%<#pragma omp declare reduction%>", type
);
39169 else if (TREE_CODE (type
) == FUNCTION_TYPE
39170 || TREE_CODE (type
) == METHOD_TYPE
39171 || TREE_CODE (type
) == ARRAY_TYPE
)
39172 error_at (loc
, "function or array type %qT in "
39173 "%<#pragma omp declare reduction%>", type
);
39174 else if (TYPE_REF_P (type
))
39175 error_at (loc
, "reference type %qT in "
39176 "%<#pragma omp declare reduction%>", type
);
39177 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39178 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39179 "%<#pragma omp declare reduction%>", type
);
39181 types
.safe_push (type
);
39183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39184 cp_lexer_consume_token (parser
->lexer
);
39189 /* Restore the saved message. */
39190 parser
->type_definition_forbidden_message
= saved_message
;
39191 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39192 parser
->colon_doesnt_start_class_def_p
39193 = saved_colon_doesnt_start_class_def_p
;
39195 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39196 || types
.is_empty ())
39199 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39203 first_token
= cp_lexer_peek_token (parser
->lexer
);
39206 FOR_EACH_VEC_ELT (types
, i
, type
)
39209 = build_function_type_list (void_type_node
,
39210 cp_build_reference_type (type
, false),
39212 tree this_reduc_id
= reduc_id
;
39213 if (!dependent_type_p (type
))
39214 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39215 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39216 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39217 DECL_ARTIFICIAL (fndecl
) = 1;
39218 DECL_EXTERNAL (fndecl
) = 1;
39219 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39220 DECL_IGNORED_P (fndecl
) = 1;
39221 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39222 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39223 DECL_ATTRIBUTES (fndecl
)
39224 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39225 DECL_ATTRIBUTES (fndecl
));
39226 if (processing_template_decl
)
39227 fndecl
= push_template_decl (fndecl
);
39228 bool block_scope
= false;
39229 tree block
= NULL_TREE
;
39230 if (current_function_decl
)
39232 block_scope
= true;
39233 DECL_CONTEXT (fndecl
) = global_namespace
;
39234 if (!processing_template_decl
)
39237 else if (current_class_type
)
39241 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39242 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39243 cp_lexer_consume_token (parser
->lexer
);
39244 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39246 cp
= cp_token_cache_new (first_token
,
39247 cp_lexer_peek_nth_token (parser
->lexer
,
39250 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39251 finish_member_declaration (fndecl
);
39252 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39253 DECL_PENDING_INLINE_P (fndecl
) = 1;
39254 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39259 DECL_CONTEXT (fndecl
) = current_namespace
;
39263 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39265 block
= begin_omp_structured_block ();
39268 cp_parser_push_lexer_for_tokens (parser
, cp
);
39269 parser
->lexer
->in_pragma
= true;
39271 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39274 finish_function (/*inline_p=*/false);
39276 DECL_CONTEXT (fndecl
) = current_function_decl
;
39278 cp_parser_pop_lexer (parser
);
39282 cp_parser_pop_lexer (parser
);
39284 finish_function (/*inline_p=*/false);
39287 DECL_CONTEXT (fndecl
) = current_function_decl
;
39288 block
= finish_omp_structured_block (block
);
39289 if (TREE_CODE (block
) == BIND_EXPR
)
39290 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39291 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39292 DECL_SAVED_TREE (fndecl
) = block
;
39293 if (processing_template_decl
)
39294 add_decl_expr (fndecl
);
39296 cp_check_omp_declare_reduction (fndecl
);
39297 if (cp
== NULL
&& types
.length () > 1)
39298 cp
= cp_token_cache_new (first_token
,
39299 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39300 if (errs
!= errorcount
)
39304 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39307 /* Free any declarators allocated. */
39308 obstack_free (&declarator_obstack
, p
);
39312 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39313 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39314 initializer-clause[opt] new-line
39315 #pragma omp declare target new-line */
39318 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39319 enum pragma_context context
)
39321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39323 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39324 const char *p
= IDENTIFIER_POINTER (id
);
39326 if (strcmp (p
, "simd") == 0)
39328 cp_lexer_consume_token (parser
->lexer
);
39329 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39333 cp_ensure_no_omp_declare_simd (parser
);
39334 if (strcmp (p
, "reduction") == 0)
39336 cp_lexer_consume_token (parser
->lexer
);
39337 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39341 if (!flag_openmp
) /* flag_openmp_simd */
39343 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39346 if (strcmp (p
, "target") == 0)
39348 cp_lexer_consume_token (parser
->lexer
);
39349 cp_parser_omp_declare_target (parser
, pragma_tok
);
39353 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39355 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39360 #pragma omp requires clauses[optseq] new-line */
39363 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39366 enum omp_requires new_req
= (enum omp_requires
) 0;
39368 location_t loc
= pragma_tok
->location
;
39369 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39371 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39372 cp_lexer_consume_token (parser
->lexer
);
39376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39378 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39379 const char *p
= IDENTIFIER_POINTER (id
);
39380 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39381 enum omp_requires this_req
= (enum omp_requires
) 0;
39383 if (!strcmp (p
, "unified_address"))
39384 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39385 else if (!strcmp (p
, "unified_shared_memory"))
39386 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39387 else if (!strcmp (p
, "dynamic_allocators"))
39388 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39389 else if (!strcmp (p
, "reverse_offload"))
39390 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39391 else if (!strcmp (p
, "atomic_default_mem_order"))
39393 cp_lexer_consume_token (parser
->lexer
);
39395 matching_parens parens
;
39396 if (parens
.require_open (parser
))
39398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39400 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39401 p
= IDENTIFIER_POINTER (id
);
39403 if (!strcmp (p
, "seq_cst"))
39405 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39406 else if (!strcmp (p
, "relaxed"))
39408 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39409 else if (!strcmp (p
, "acq_rel"))
39411 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39415 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39416 "expected %<seq_cst%>, %<relaxed%> or "
39418 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39420 cp_lexer_consume_token (parser
->lexer
);
39423 cp_lexer_consume_token (parser
->lexer
);
39425 if (!parens
.require_close (parser
))
39426 cp_parser_skip_to_closing_parenthesis (parser
,
39427 /*recovering=*/true,
39428 /*or_comma=*/false,
39434 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39442 error_at (cloc
, "expected %<unified_address%>, "
39443 "%<unified_shared_memory%>, "
39444 "%<dynamic_allocators%>, "
39445 "%<reverse_offload%> "
39446 "or %<atomic_default_mem_order%> clause");
39447 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39451 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39452 "supported yet", p
);
39454 cp_lexer_consume_token (parser
->lexer
);
39457 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39459 if ((this_req
& new_req
) != 0)
39460 error_at (cloc
, "too many %qs clauses", p
);
39461 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39462 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39463 error_at (cloc
, "%qs clause used lexically after first "
39464 "target construct or offloading API", p
);
39466 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39468 error_at (cloc
, "too many %qs clauses",
39469 "atomic_default_mem_order");
39470 this_req
= (enum omp_requires
) 0;
39472 else if ((omp_requires_mask
39473 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39475 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39476 " clause in a single compilation unit");
39478 = (enum omp_requires
)
39480 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39482 else if ((omp_requires_mask
39483 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39484 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39485 "lexically after first %<atomic%> construct "
39486 "without memory order clause");
39487 new_req
= (enum omp_requires
) (new_req
| this_req
);
39489 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39495 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39498 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39504 #pragma omp taskloop taskloop-clause[optseq] new-line
39507 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39510 #define OMP_TASKLOOP_CLAUSE_MASK \
39511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39529 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39530 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39533 tree clauses
, sb
, ret
;
39535 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39537 strcat (p_name
, " taskloop");
39538 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39539 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39541 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39542 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39546 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39547 const char *p
= IDENTIFIER_POINTER (id
);
39549 if (strcmp (p
, "simd") == 0)
39551 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39552 if (cclauses
== NULL
)
39553 cclauses
= cclauses_buf
;
39555 cp_lexer_consume_token (parser
->lexer
);
39556 if (!flag_openmp
) /* flag_openmp_simd */
39557 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39559 sb
= begin_omp_structured_block ();
39560 save
= cp_parser_begin_omp_structured_block (parser
);
39561 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39563 cp_parser_end_omp_structured_block (parser
, save
);
39564 tree body
= finish_omp_structured_block (sb
);
39567 ret
= make_node (OMP_TASKLOOP
);
39568 TREE_TYPE (ret
) = void_type_node
;
39569 OMP_FOR_BODY (ret
) = body
;
39570 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39571 SET_EXPR_LOCATION (ret
, loc
);
39576 if (!flag_openmp
) /* flag_openmp_simd */
39578 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39582 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39586 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39587 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39590 keep_next_level (true);
39591 sb
= begin_omp_structured_block ();
39592 save
= cp_parser_begin_omp_structured_block (parser
);
39594 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39597 cp_parser_end_omp_structured_block (parser
, save
);
39598 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39605 # pragma acc routine oacc-routine-clause[optseq] new-line
39606 function-definition
39608 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39611 #define OACC_ROUTINE_CLAUSE_MASK \
39612 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39618 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39619 component, which must resolve to a declared namespace-scope
39620 function. The clauses are either processed directly (for a named
39621 function), or defered until the immediatley following declaration
39625 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39626 enum pragma_context context
)
39628 gcc_checking_assert (context
== pragma_external
);
39629 /* The checking for "another pragma following this one" in the "no optional
39630 '( name )'" case makes sure that we dont re-enter. */
39631 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39633 cp_oacc_routine_data data
;
39634 data
.error_seen
= false;
39635 data
.fndecl_seen
= false;
39636 data
.tokens
= vNULL
;
39637 data
.clauses
= NULL_TREE
;
39638 data
.loc
= pragma_tok
->location
;
39639 /* It is safe to take the address of a local variable; it will only be
39640 used while this scope is live. */
39641 parser
->oacc_routine
= &data
;
39643 /* Look for optional '( name )'. */
39644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39646 matching_parens parens
;
39647 parens
.consume_open (parser
); /* '(' */
39649 /* We parse the name as an id-expression. If it resolves to
39650 anything other than a non-overloaded function at namespace
39651 scope, it's an error. */
39652 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39653 tree name
= cp_parser_id_expression (parser
,
39654 /*template_keyword_p=*/false,
39655 /*check_dependency_p=*/false,
39656 /*template_p=*/NULL
,
39657 /*declarator_p=*/false,
39658 /*optional_p=*/false);
39659 tree decl
= (identifier_p (name
)
39660 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39662 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39663 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39665 if (decl
== error_mark_node
39666 || !parens
.require_close (parser
))
39668 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39669 parser
->oacc_routine
= NULL
;
39674 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39675 "#pragma acc routine",
39676 cp_lexer_peek_token (parser
->lexer
));
39678 if (decl
&& is_overloaded_fn (decl
)
39679 && (TREE_CODE (decl
) != FUNCTION_DECL
39680 || DECL_FUNCTION_TEMPLATE_P (decl
)))
39682 error_at (name_loc
,
39683 "%<#pragma acc routine%> names a set of overloads");
39684 parser
->oacc_routine
= NULL
;
39688 /* Perhaps we should use the same rule as declarations in different
39690 if (!DECL_NAMESPACE_SCOPE_P (decl
))
39692 error_at (name_loc
,
39693 "%qD does not refer to a namespace scope function", decl
);
39694 parser
->oacc_routine
= NULL
;
39698 if (TREE_CODE (decl
) != FUNCTION_DECL
)
39700 error_at (name_loc
, "%qD does not refer to a function", decl
);
39701 parser
->oacc_routine
= NULL
;
39705 cp_finalize_oacc_routine (parser
, decl
, false);
39706 parser
->oacc_routine
= NULL
;
39708 else /* No optional '( name )'. */
39710 /* Store away all pragma tokens. */
39711 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39712 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39713 cp_lexer_consume_token (parser
->lexer
);
39714 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39715 parser
->oacc_routine
->error_seen
= true;
39716 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39717 struct cp_token_cache
*cp
39718 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39719 parser
->oacc_routine
->tokens
.safe_push (cp
);
39721 /* Emit a helpful diagnostic if there's another pragma following this
39723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39725 cp_ensure_no_oacc_routine (parser
);
39726 data
.tokens
.release ();
39727 /* ..., and then just keep going. */
39731 /* We only have to consider the pragma_external case here. */
39732 cp_parser_declaration (parser
);
39733 if (parser
->oacc_routine
39734 && !parser
->oacc_routine
->fndecl_seen
)
39735 cp_ensure_no_oacc_routine (parser
);
39737 parser
->oacc_routine
= NULL
;
39738 data
.tokens
.release ();
39742 /* Finalize #pragma acc routine clauses after direct declarator has
39746 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
39748 struct cp_token_cache
*ce
;
39749 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
39751 if (!data
->error_seen
&& data
->fndecl_seen
)
39753 error_at (data
->loc
,
39754 "%<#pragma acc routine%> not immediately followed by "
39755 "a single function declaration or definition");
39756 data
->error_seen
= true;
39758 if (data
->error_seen
)
39761 gcc_checking_assert (data
->tokens
.length () == 1);
39762 ce
= data
->tokens
[0];
39764 cp_parser_push_lexer_for_tokens (parser
, ce
);
39765 parser
->lexer
->in_pragma
= true;
39766 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39768 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39769 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
39770 parser
->oacc_routine
->clauses
39771 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39772 "#pragma acc routine", pragma_tok
);
39773 cp_parser_pop_lexer (parser
);
39774 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39780 /* Apply any saved OpenACC routine clauses to a just-parsed
39784 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
39786 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
39788 /* Keep going if we're in error reporting mode. */
39789 if (parser
->oacc_routine
->error_seen
39790 || fndecl
== error_mark_node
)
39793 if (parser
->oacc_routine
->fndecl_seen
)
39795 error_at (parser
->oacc_routine
->loc
,
39796 "%<#pragma acc routine%> not immediately followed by"
39797 " a single function declaration or definition");
39798 parser
->oacc_routine
= NULL
;
39801 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
39803 cp_ensure_no_oacc_routine (parser
);
39807 if (oacc_get_fn_attrib (fndecl
))
39809 error_at (parser
->oacc_routine
->loc
,
39810 "%<#pragma acc routine%> already applied to %qD", fndecl
);
39811 parser
->oacc_routine
= NULL
;
39815 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
39817 error_at (parser
->oacc_routine
->loc
,
39819 ? G_("%<#pragma acc routine%> must be applied before use")
39820 : G_("%<#pragma acc routine%> must be applied before "
39822 parser
->oacc_routine
= NULL
;
39826 /* Process the routine's dimension clauses. */
39827 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
39828 oacc_replace_fn_attrib (fndecl
, dims
);
39830 /* Add an "omp declare target" attribute. */
39831 DECL_ATTRIBUTES (fndecl
)
39832 = tree_cons (get_identifier ("omp declare target"),
39833 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
39835 /* Don't unset parser->oacc_routine here: we may still need it to
39836 diagnose wrong usage. But, remember that we've used this "#pragma acc
39838 parser
->oacc_routine
->fndecl_seen
= true;
39842 /* Main entry point to OpenMP statement pragmas. */
39845 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39848 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
39849 omp_clause_mask
mask (0);
39851 switch (cp_parser_pragma_kind (pragma_tok
))
39853 case PRAGMA_OACC_ATOMIC
:
39854 cp_parser_omp_atomic (parser
, pragma_tok
);
39856 case PRAGMA_OACC_CACHE
:
39857 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
39859 case PRAGMA_OACC_DATA
:
39860 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
39862 case PRAGMA_OACC_ENTER_DATA
:
39863 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
39865 case PRAGMA_OACC_EXIT_DATA
:
39866 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
39868 case PRAGMA_OACC_HOST_DATA
:
39869 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
39871 case PRAGMA_OACC_KERNELS
:
39872 case PRAGMA_OACC_PARALLEL
:
39873 strcpy (p_name
, "#pragma acc");
39874 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
39877 case PRAGMA_OACC_LOOP
:
39878 strcpy (p_name
, "#pragma acc");
39879 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39882 case PRAGMA_OACC_UPDATE
:
39883 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
39885 case PRAGMA_OACC_WAIT
:
39886 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
39888 case PRAGMA_OMP_ATOMIC
:
39889 cp_parser_omp_atomic (parser
, pragma_tok
);
39891 case PRAGMA_OMP_CRITICAL
:
39892 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
39894 case PRAGMA_OMP_DISTRIBUTE
:
39895 strcpy (p_name
, "#pragma omp");
39896 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
39899 case PRAGMA_OMP_FOR
:
39900 strcpy (p_name
, "#pragma omp");
39901 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
39904 case PRAGMA_OMP_MASTER
:
39905 strcpy (p_name
, "#pragma omp");
39906 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
39909 case PRAGMA_OMP_PARALLEL
:
39910 strcpy (p_name
, "#pragma omp");
39911 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
39914 case PRAGMA_OMP_SECTIONS
:
39915 strcpy (p_name
, "#pragma omp");
39916 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
39918 case PRAGMA_OMP_SIMD
:
39919 strcpy (p_name
, "#pragma omp");
39920 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
39923 case PRAGMA_OMP_SINGLE
:
39924 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
39926 case PRAGMA_OMP_TASK
:
39927 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
39929 case PRAGMA_OMP_TASKGROUP
:
39930 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
39932 case PRAGMA_OMP_TASKLOOP
:
39933 strcpy (p_name
, "#pragma omp");
39934 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
39937 case PRAGMA_OMP_TEAMS
:
39938 strcpy (p_name
, "#pragma omp");
39939 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
39943 gcc_unreachable ();
39946 protected_set_expr_location (stmt
, pragma_tok
->location
);
39949 /* Transactional Memory parsing routines. */
39951 /* Parse a transaction attribute.
39957 We use this instead of cp_parser_attributes_opt for transactions to avoid
39958 the pedwarn in C++98 mode. */
39961 cp_parser_txn_attribute_opt (cp_parser
*parser
)
39964 tree attr_name
, attr
= NULL
;
39966 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
39967 return cp_parser_attributes_opt (parser
);
39969 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
39971 cp_lexer_consume_token (parser
->lexer
);
39972 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
39975 token
= cp_lexer_peek_token (parser
->lexer
);
39976 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
39978 token
= cp_lexer_consume_token (parser
->lexer
);
39980 attr_name
= (token
->type
== CPP_KEYWORD
39981 /* For keywords, use the canonical spelling,
39982 not the parsed identifier. */
39983 ? ridpointers
[(int) token
->keyword
]
39985 attr
= build_tree_list (attr_name
, NULL_TREE
);
39988 cp_parser_error (parser
, "expected identifier");
39990 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39992 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
39996 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39998 transaction-statement:
39999 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40001 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40005 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40007 unsigned char old_in
= parser
->in_transaction
;
40008 unsigned char this_in
= 1, new_in
;
40009 enum rid keyword
= token
->keyword
;
40010 tree stmt
, attrs
, noex
;
40012 cp_lexer_consume_token (parser
->lexer
);
40014 if (keyword
== RID_TRANSACTION_RELAXED
40015 || keyword
== RID_SYNCHRONIZED
)
40016 this_in
|= TM_STMT_ATTR_RELAXED
;
40019 attrs
= cp_parser_txn_attribute_opt (parser
);
40021 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40024 /* Parse a noexcept specification. */
40025 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40026 noex
= boolean_true_node
;
40027 else if (keyword
== RID_ATOMIC_CANCEL
)
40029 /* cancel-and-throw is unimplemented. */
40030 sorry ("atomic_cancel");
40034 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40036 /* Keep track if we're in the lexical scope of an outer transaction. */
40037 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40039 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40041 parser
->in_transaction
= new_in
;
40042 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40043 parser
->in_transaction
= old_in
;
40045 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40050 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40052 transaction-expression:
40053 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40054 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40058 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40060 unsigned char old_in
= parser
->in_transaction
;
40061 unsigned char this_in
= 1;
40065 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40067 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40068 || keyword
== RID_TRANSACTION_RELAXED
);
40072 keyword
== RID_TRANSACTION_RELAXED
40073 ? G_("%<__transaction_relaxed%> without transactional memory "
40075 : G_("%<__transaction_atomic%> without transactional memory "
40076 "support enabled"));
40078 token
= cp_parser_require_keyword (parser
, keyword
,
40079 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40080 : RT_TRANSACTION_RELAXED
));
40081 gcc_assert (token
!= NULL
);
40083 if (keyword
== RID_TRANSACTION_RELAXED
)
40084 this_in
|= TM_STMT_ATTR_RELAXED
;
40086 /* Set this early. This might mean that we allow transaction_cancel in
40087 an expression that we find out later actually has to be a constexpr.
40088 However, we expect that cxx_constant_value will be able to deal with
40089 this; also, if the noexcept has no constexpr, then what we parse next
40090 really is a transaction's body. */
40091 parser
->in_transaction
= this_in
;
40093 /* Parse a noexcept specification. */
40094 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40097 if (!noex
|| !noex_expr
40098 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40100 matching_parens parens
;
40101 parens
.require_open (parser
);
40103 expr
= cp_parser_expression (parser
);
40104 expr
= finish_parenthesized_expr (expr
);
40106 parens
.require_close (parser
);
40110 /* The only expression that is available got parsed for the noexcept
40111 already. noexcept is true then. */
40113 noex
= boolean_true_node
;
40116 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40117 parser
->in_transaction
= old_in
;
40119 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40120 return error_mark_node
;
40122 return (flag_tm
? expr
: error_mark_node
);
40125 /* Parse a function-transaction-block.
40127 function-transaction-block:
40128 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40130 __transaction_atomic txn-attribute[opt] function-try-block
40131 __transaction_relaxed ctor-initializer[opt] function-body
40132 __transaction_relaxed function-try-block
40136 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40138 unsigned char old_in
= parser
->in_transaction
;
40139 unsigned char new_in
= 1;
40140 tree compound_stmt
, stmt
, attrs
;
40143 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40144 || keyword
== RID_TRANSACTION_RELAXED
);
40145 token
= cp_parser_require_keyword (parser
, keyword
,
40146 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40147 : RT_TRANSACTION_RELAXED
));
40148 gcc_assert (token
!= NULL
);
40150 if (keyword
== RID_TRANSACTION_RELAXED
)
40151 new_in
|= TM_STMT_ATTR_RELAXED
;
40154 attrs
= cp_parser_txn_attribute_opt (parser
);
40156 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40159 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40161 parser
->in_transaction
= new_in
;
40163 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40164 cp_parser_function_try_block (parser
);
40166 cp_parser_ctor_initializer_opt_and_function_body
40167 (parser
, /*in_function_try_block=*/false);
40169 parser
->in_transaction
= old_in
;
40171 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40174 /* Parse a __transaction_cancel statement.
40177 __transaction_cancel txn-attribute[opt] ;
40178 __transaction_cancel txn-attribute[opt] throw-expression ;
40180 ??? Cancel and throw is not yet implemented. */
40183 cp_parser_transaction_cancel (cp_parser
*parser
)
40186 bool is_outer
= false;
40189 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40190 RT_TRANSACTION_CANCEL
);
40191 gcc_assert (token
!= NULL
);
40193 attrs
= cp_parser_txn_attribute_opt (parser
);
40195 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40197 /* ??? Parse cancel-and-throw here. */
40199 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40203 error_at (token
->location
, "%<__transaction_cancel%> without "
40204 "transactional memory support enabled");
40205 return error_mark_node
;
40207 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40209 error_at (token
->location
, "%<__transaction_cancel%> within a "
40210 "%<__transaction_relaxed%>");
40211 return error_mark_node
;
40215 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40216 && !is_tm_may_cancel_outer (current_function_decl
))
40218 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40219 "within outer %<__transaction_atomic%>");
40220 error_at (token
->location
,
40221 " or a %<transaction_may_cancel_outer%> function");
40222 return error_mark_node
;
40225 else if (parser
->in_transaction
== 0)
40227 error_at (token
->location
, "%<__transaction_cancel%> not within "
40228 "%<__transaction_atomic%>");
40229 return error_mark_node
;
40232 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40240 static GTY (()) cp_parser
*the_parser
;
40243 /* Special handling for the first token or line in the file. The first
40244 thing in the file might be #pragma GCC pch_preprocess, which loads a
40245 PCH file, which is a GC collection point. So we need to handle this
40246 first pragma without benefit of an existing lexer structure.
40248 Always returns one token to the caller in *FIRST_TOKEN. This is
40249 either the true first token of the file, or the first token after
40250 the initial pragma. */
40253 cp_parser_initial_pragma (cp_token
*first_token
)
40257 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40258 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40261 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40262 if (first_token
->type
== CPP_STRING
)
40264 name
= first_token
->u
.value
;
40266 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40267 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40268 error_at (first_token
->location
,
40269 "junk at end of %<#pragma GCC pch_preprocess%>");
40272 error_at (first_token
->location
, "expected string literal");
40274 /* Skip to the end of the pragma. */
40275 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40276 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40278 /* Now actually load the PCH file. */
40280 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40282 /* Read one more token to return to our caller. We have to do this
40283 after reading the PCH file in, since its pointers have to be
40285 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40288 /* Parse a pragma GCC ivdep. */
40291 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40293 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40297 /* Parse a pragma GCC unroll. */
40299 static unsigned short
40300 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40302 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40303 tree expr
= cp_parser_constant_expression (parser
);
40304 unsigned short unroll
;
40305 expr
= maybe_constant_value (expr
);
40306 HOST_WIDE_INT lunroll
= 0;
40307 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40308 || TREE_CODE (expr
) != INTEGER_CST
40309 || (lunroll
= tree_to_shwi (expr
)) < 0
40310 || lunroll
>= USHRT_MAX
)
40312 error_at (location
, "%<#pragma GCC unroll%> requires an"
40313 " assignment-expression that evaluates to a non-negative"
40314 " integral constant less than %u", USHRT_MAX
);
40319 unroll
= (unsigned short)lunroll
;
40323 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40327 /* Normal parsing of a pragma token. Here we can (and must) use the
40331 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40333 cp_token
*pragma_tok
;
40338 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40339 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40340 parser
->lexer
->in_pragma
= true;
40342 id
= cp_parser_pragma_kind (pragma_tok
);
40343 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40344 cp_ensure_no_omp_declare_simd (parser
);
40347 case PRAGMA_GCC_PCH_PREPROCESS
:
40348 error_at (pragma_tok
->location
,
40349 "%<#pragma GCC pch_preprocess%> must be first");
40352 case PRAGMA_OMP_BARRIER
:
40355 case pragma_compound
:
40356 cp_parser_omp_barrier (parser
, pragma_tok
);
40359 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40360 "used in compound statements", "omp barrier");
40367 case PRAGMA_OMP_DEPOBJ
:
40370 case pragma_compound
:
40371 cp_parser_omp_depobj (parser
, pragma_tok
);
40374 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40375 "used in compound statements", "omp depobj");
40382 case PRAGMA_OMP_FLUSH
:
40385 case pragma_compound
:
40386 cp_parser_omp_flush (parser
, pragma_tok
);
40389 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40390 "used in compound statements", "omp flush");
40397 case PRAGMA_OMP_TASKWAIT
:
40400 case pragma_compound
:
40401 cp_parser_omp_taskwait (parser
, pragma_tok
);
40404 error_at (pragma_tok
->location
,
40405 "%<#pragma %s%> may only be used in compound statements",
40413 case PRAGMA_OMP_TASKYIELD
:
40416 case pragma_compound
:
40417 cp_parser_omp_taskyield (parser
, pragma_tok
);
40420 error_at (pragma_tok
->location
,
40421 "%<#pragma %s%> may only be used in compound statements",
40429 case PRAGMA_OMP_CANCEL
:
40432 case pragma_compound
:
40433 cp_parser_omp_cancel (parser
, pragma_tok
);
40436 error_at (pragma_tok
->location
,
40437 "%<#pragma %s%> may only be used in compound statements",
40445 case PRAGMA_OMP_CANCELLATION_POINT
:
40446 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40449 case PRAGMA_OMP_THREADPRIVATE
:
40450 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40453 case PRAGMA_OMP_DECLARE
:
40454 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40456 case PRAGMA_OACC_DECLARE
:
40457 cp_parser_oacc_declare (parser
, pragma_tok
);
40460 case PRAGMA_OACC_ENTER_DATA
:
40461 if (context
== pragma_stmt
)
40463 error_at (pragma_tok
->location
,
40464 "%<#pragma %s%> may only be used in compound statements",
40468 else if (context
!= pragma_compound
)
40470 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40473 case PRAGMA_OACC_EXIT_DATA
:
40474 if (context
== pragma_stmt
)
40476 error_at (pragma_tok
->location
,
40477 "%<#pragma %s%> may only be used in compound statements",
40481 else if (context
!= pragma_compound
)
40483 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40486 case PRAGMA_OACC_ROUTINE
:
40487 if (context
!= pragma_external
)
40489 error_at (pragma_tok
->location
,
40490 "%<#pragma acc routine%> must be at file scope");
40493 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40496 case PRAGMA_OACC_UPDATE
:
40497 if (context
== pragma_stmt
)
40499 error_at (pragma_tok
->location
,
40500 "%<#pragma %s%> may only be used in compound statements",
40504 else if (context
!= pragma_compound
)
40506 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40509 case PRAGMA_OACC_WAIT
:
40510 if (context
== pragma_stmt
)
40512 error_at (pragma_tok
->location
,
40513 "%<#pragma %s%> may only be used in compound statements",
40517 else if (context
!= pragma_compound
)
40519 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40522 case PRAGMA_OACC_ATOMIC
:
40523 case PRAGMA_OACC_CACHE
:
40524 case PRAGMA_OACC_DATA
:
40525 case PRAGMA_OACC_HOST_DATA
:
40526 case PRAGMA_OACC_KERNELS
:
40527 case PRAGMA_OACC_PARALLEL
:
40528 case PRAGMA_OACC_LOOP
:
40529 case PRAGMA_OMP_ATOMIC
:
40530 case PRAGMA_OMP_CRITICAL
:
40531 case PRAGMA_OMP_DISTRIBUTE
:
40532 case PRAGMA_OMP_FOR
:
40533 case PRAGMA_OMP_MASTER
:
40534 case PRAGMA_OMP_PARALLEL
:
40535 case PRAGMA_OMP_SECTIONS
:
40536 case PRAGMA_OMP_SIMD
:
40537 case PRAGMA_OMP_SINGLE
:
40538 case PRAGMA_OMP_TASK
:
40539 case PRAGMA_OMP_TASKGROUP
:
40540 case PRAGMA_OMP_TASKLOOP
:
40541 case PRAGMA_OMP_TEAMS
:
40542 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40544 stmt
= push_omp_privatization_clauses (false);
40545 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40546 pop_omp_privatization_clauses (stmt
);
40549 case PRAGMA_OMP_REQUIRES
:
40550 return cp_parser_omp_requires (parser
, pragma_tok
);
40552 case PRAGMA_OMP_ORDERED
:
40553 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40555 stmt
= push_omp_privatization_clauses (false);
40556 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40557 pop_omp_privatization_clauses (stmt
);
40560 case PRAGMA_OMP_TARGET
:
40561 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40563 stmt
= push_omp_privatization_clauses (false);
40564 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40565 pop_omp_privatization_clauses (stmt
);
40568 case PRAGMA_OMP_END_DECLARE_TARGET
:
40569 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40572 case PRAGMA_OMP_SECTION
:
40573 error_at (pragma_tok
->location
,
40574 "%<#pragma omp section%> may only be used in "
40575 "%<#pragma omp sections%> construct");
40580 if (context
== pragma_external
)
40582 error_at (pragma_tok
->location
,
40583 "%<#pragma GCC ivdep%> must be inside a function");
40586 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40587 unsigned short unroll
;
40588 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40589 if (tok
->type
== CPP_PRAGMA
40590 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40592 tok
= cp_lexer_consume_token (parser
->lexer
);
40593 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40594 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40598 if (tok
->type
!= CPP_KEYWORD
40599 || (tok
->keyword
!= RID_FOR
40600 && tok
->keyword
!= RID_WHILE
40601 && tok
->keyword
!= RID_DO
))
40603 cp_parser_error (parser
, "for, while or do statement expected");
40606 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40610 case PRAGMA_UNROLL
:
40612 if (context
== pragma_external
)
40614 error_at (pragma_tok
->location
,
40615 "%<#pragma GCC unroll%> must be inside a function");
40618 const unsigned short unroll
40619 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40621 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40622 if (tok
->type
== CPP_PRAGMA
40623 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40625 tok
= cp_lexer_consume_token (parser
->lexer
);
40626 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40627 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40631 if (tok
->type
!= CPP_KEYWORD
40632 || (tok
->keyword
!= RID_FOR
40633 && tok
->keyword
!= RID_WHILE
40634 && tok
->keyword
!= RID_DO
))
40636 cp_parser_error (parser
, "for, while or do statement expected");
40639 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40644 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40645 c_invoke_pragma_handler (id
);
40649 cp_parser_error (parser
, "expected declaration specifiers");
40653 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40657 /* The interface the pragma parsers have to the lexer. */
40660 pragma_lex (tree
*value
, location_t
*loc
)
40662 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40663 enum cpp_ttype ret
= tok
->type
;
40665 *value
= tok
->u
.value
;
40667 *loc
= tok
->location
;
40669 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
40671 else if (ret
== CPP_STRING
)
40672 *value
= cp_parser_string_literal (the_parser
, false, false);
40675 if (ret
== CPP_KEYWORD
)
40677 cp_lexer_consume_token (the_parser
->lexer
);
40684 /* External interface. */
40686 /* Parse one entire translation unit. */
40689 c_parse_file (void)
40691 static bool already_called
= false;
40693 if (already_called
)
40694 fatal_error (input_location
,
40695 "inter-module optimizations not implemented for C++");
40696 already_called
= true;
40698 the_parser
= cp_parser_new ();
40699 push_deferring_access_checks (flag_access_control
40700 ? dk_no_deferred
: dk_no_check
);
40701 cp_parser_translation_unit (the_parser
);
40704 finish_translation_unit ();
40707 /* Create an identifier for a generic parameter type (a synthesized
40708 template parameter implied by `auto' or a concept identifier). */
40710 static GTY(()) int generic_parm_count
;
40712 make_generic_type_name ()
40715 sprintf (buf
, "auto:%d", ++generic_parm_count
);
40716 return get_identifier (buf
);
40719 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40720 (creating a new template parameter list if necessary). Returns the newly
40721 created template type parm. */
40724 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
40726 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
40728 /* Before committing to modifying any scope, if we're in an
40729 implicit template scope, and we're trying to synthesize a
40730 constrained parameter, try to find a previous parameter with
40731 the same name. This is the same-type rule for abbreviated
40732 function templates.
40734 NOTE: We can generate implicit parameters when tentatively
40735 parsing a nested name specifier, only to reject that parse
40736 later. However, matching the same template-id as part of a
40737 direct-declarator should generate an identical template
40738 parameter, so this rule will merge them. */
40739 if (parser
->implicit_template_scope
&& constr
)
40741 tree t
= parser
->implicit_template_parms
;
40744 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
40746 tree d
= TREE_VALUE (t
);
40747 if (TREE_CODE (d
) == PARM_DECL
)
40748 /* Return the TEMPLATE_PARM_INDEX. */
40749 d
= DECL_INITIAL (d
);
40752 t
= TREE_CHAIN (t
);
40756 /* We are either continuing a function template that already contains implicit
40757 template parameters, creating a new fully-implicit function template, or
40758 extending an existing explicit function template with implicit template
40761 cp_binding_level
*const entry_scope
= current_binding_level
;
40763 bool become_template
= false;
40764 cp_binding_level
*parent_scope
= 0;
40766 if (parser
->implicit_template_scope
)
40768 gcc_assert (parser
->implicit_template_parms
);
40770 current_binding_level
= parser
->implicit_template_scope
;
40774 /* Roll back to the existing template parameter scope (in the case of
40775 extending an explicit function template) or introduce a new template
40776 parameter scope ahead of the function parameter scope (or class scope
40777 in the case of out-of-line member definitions). The function scope is
40778 added back after template parameter synthesis below. */
40780 cp_binding_level
*scope
= entry_scope
;
40782 while (scope
->kind
== sk_function_parms
)
40784 parent_scope
= scope
;
40785 scope
= scope
->level_chain
;
40787 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
40789 /* If not defining a class, then any class scope is a scope level in
40790 an out-of-line member definition. In this case simply wind back
40791 beyond the first such scope to inject the template parameter list.
40792 Otherwise wind back to the class being defined. The latter can
40793 occur in class member friend declarations such as:
40799 friend void A::foo (auto);
40802 The template parameter list synthesized for the friend declaration
40803 must be injected in the scope of 'B'. This can also occur in
40804 erroneous cases such as:
40810 void B::foo (auto) {}
40813 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40814 but, nevertheless, the template parameter list synthesized for the
40815 declarator should be injected into the scope of 'A' as if the
40816 ill-formed template was specified explicitly. */
40818 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
40820 parent_scope
= scope
;
40821 scope
= scope
->level_chain
;
40825 current_binding_level
= scope
;
40827 if (scope
->kind
!= sk_template_parms
40828 || !function_being_declared_is_template_p (parser
))
40830 /* Introduce a new template parameter list for implicit template
40833 become_template
= true;
40835 parser
->implicit_template_scope
40836 = begin_scope (sk_template_parms
, NULL
);
40838 ++processing_template_decl
;
40840 parser
->fully_implicit_function_template_p
= true;
40841 ++parser
->num_template_parameter_lists
;
40845 /* Synthesize implicit template parameters at the end of the explicit
40846 template parameter list. */
40848 gcc_assert (current_template_parms
);
40850 parser
->implicit_template_scope
= scope
;
40852 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40853 parser
->implicit_template_parms
40854 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
40858 /* Synthesize a new template parameter and track the current template
40859 parameter chain with implicit_template_parms. */
40861 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
40862 tree synth_id
= make_generic_type_name ();
40863 tree synth_tmpl_parm
;
40864 bool non_type
= false;
40866 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
40868 = finish_template_type_parm (class_type_node
, synth_id
);
40869 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
40871 = finish_constrained_template_template_parm (proto
, synth_id
);
40874 synth_tmpl_parm
= copy_decl (proto
);
40875 DECL_NAME (synth_tmpl_parm
) = synth_id
;
40879 // Attach the constraint to the parm before processing.
40880 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
40881 TREE_TYPE (node
) = constr
;
40883 = process_template_parm (parser
->implicit_template_parms
,
40886 /*non_type=*/non_type
,
40887 /*param_pack=*/false);
40889 // Chain the new parameter to the list of implicit parameters.
40890 if (parser
->implicit_template_parms
)
40891 parser
->implicit_template_parms
40892 = TREE_CHAIN (parser
->implicit_template_parms
);
40894 parser
->implicit_template_parms
= new_parm
;
40896 tree new_decl
= get_local_decls ();
40898 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40899 new_decl
= DECL_INITIAL (new_decl
);
40901 /* If creating a fully implicit function template, start the new implicit
40902 template parameter list with this synthesized type, otherwise grow the
40903 current template parameter list. */
40905 if (become_template
)
40907 parent_scope
->level_chain
= current_binding_level
;
40909 tree new_parms
= make_tree_vec (1);
40910 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
40911 current_template_parms
= tree_cons (size_int (processing_template_decl
),
40912 new_parms
, current_template_parms
);
40916 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
40917 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
40918 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
40919 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
40922 // If the new parameter was constrained, we need to add that to the
40923 // constraints in the template parameter list.
40924 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
40926 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
40927 reqs
= conjoin_constraints (reqs
, req
);
40928 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
40931 current_binding_level
= entry_scope
;
40936 /* Finish the declaration of a fully implicit function template. Such a
40937 template has no explicit template parameter list so has not been through the
40938 normal template head and tail processing. synthesize_implicit_template_parm
40939 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40940 provided if the declaration is a class member such that its template
40941 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40942 form is returned. Otherwise NULL_TREE is returned. */
40945 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
40947 gcc_assert (parser
->fully_implicit_function_template_p
);
40949 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
40950 && DECL_VIRTUAL_P (member_decl_opt
))
40952 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
40953 "implicit templates may not be %<virtual%>");
40954 DECL_VIRTUAL_P (member_decl_opt
) = false;
40957 if (member_decl_opt
)
40958 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
40959 end_template_decl ();
40961 parser
->fully_implicit_function_template_p
= false;
40962 parser
->implicit_template_parms
= 0;
40963 parser
->implicit_template_scope
= 0;
40964 --parser
->num_template_parameter_lists
;
40966 return member_decl_opt
;
40969 /* Like finish_fully_implicit_template, but to be used in error
40970 recovery, rearranging scopes so that we restore the state we had
40971 before synthesize_implicit_template_parm inserted the implement
40972 template parms scope. */
40975 abort_fully_implicit_template (cp_parser
*parser
)
40977 cp_binding_level
*return_to_scope
= current_binding_level
;
40979 if (parser
->implicit_template_scope
40980 && return_to_scope
!= parser
->implicit_template_scope
)
40982 cp_binding_level
*child
= return_to_scope
;
40983 for (cp_binding_level
*scope
= child
->level_chain
;
40984 scope
!= parser
->implicit_template_scope
;
40985 scope
= child
->level_chain
)
40987 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
40988 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
40989 current_binding_level
= parser
->implicit_template_scope
;
40992 return_to_scope
= return_to_scope
->level_chain
;
40994 finish_fully_implicit_template (parser
, NULL
);
40996 gcc_assert (current_binding_level
== return_to_scope
);
40999 /* Helper function for diagnostics that have complained about things
41000 being used with 'extern "C"' linkage.
41002 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41005 maybe_show_extern_c_location (void)
41007 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41008 inform (the_parser
->innermost_linkage_specification_location
,
41009 "%<extern \"C\"%> linkage started here");
41012 #include "gt-cp-parser.h"