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,
1795 /* When parsing a decl-specifier-seq, allow missing typename. */
1796 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20
1799 /* This type is used for parameters and variables which hold
1800 combinations of the above flags. */
1801 typedef int cp_parser_flags
;
1803 /* The different kinds of declarators we want to parse. */
1805 enum cp_parser_declarator_kind
1807 /* We want an abstract declarator. */
1808 CP_PARSER_DECLARATOR_ABSTRACT
,
1809 /* We want a named declarator. */
1810 CP_PARSER_DECLARATOR_NAMED
,
1811 /* We don't mind, but the name must be an unqualified-id. */
1812 CP_PARSER_DECLARATOR_EITHER
1815 /* The precedence values used to parse binary expressions. The minimum value
1816 of PREC must be 1, because zero is reserved to quickly discriminate
1817 binary operators from other tokens. */
1822 PREC_LOGICAL_OR_EXPRESSION
,
1823 PREC_LOGICAL_AND_EXPRESSION
,
1824 PREC_INCLUSIVE_OR_EXPRESSION
,
1825 PREC_EXCLUSIVE_OR_EXPRESSION
,
1826 PREC_AND_EXPRESSION
,
1827 PREC_EQUALITY_EXPRESSION
,
1828 PREC_RELATIONAL_EXPRESSION
,
1829 PREC_SHIFT_EXPRESSION
,
1830 PREC_ADDITIVE_EXPRESSION
,
1831 PREC_MULTIPLICATIVE_EXPRESSION
,
1833 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1836 /* A mapping from a token type to a corresponding tree node type, with a
1837 precedence value. */
1839 struct cp_parser_binary_operations_map_node
1841 /* The token type. */
1842 enum cpp_ttype token_type
;
1843 /* The corresponding tree code. */
1844 enum tree_code tree_type
;
1845 /* The precedence of this operator. */
1846 enum cp_parser_prec prec
;
1849 struct cp_parser_expression_stack_entry
1851 /* Left hand side of the binary operation we are currently
1854 /* Original tree code for left hand side, if it was a binary
1855 expression itself (used for -Wparentheses). */
1856 enum tree_code lhs_type
;
1857 /* Tree code for the binary operation we are parsing. */
1858 enum tree_code tree_type
;
1859 /* Precedence of the binary operation we are parsing. */
1860 enum cp_parser_prec prec
;
1861 /* Location of the binary operation we are parsing. */
1865 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1866 entries because precedence levels on the stack are monotonically
1868 typedef struct cp_parser_expression_stack_entry
1869 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1873 /* Constructors and destructors. */
1875 static cp_parser_context
*cp_parser_context_new
1876 (cp_parser_context
*);
1878 /* Class variables. */
1880 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1882 /* The operator-precedence table used by cp_parser_binary_expression.
1883 Transformed into an associative array (binops_by_token) by
1886 static const cp_parser_binary_operations_map_node binops
[] = {
1887 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1888 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1890 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1891 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1892 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1894 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1895 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1897 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1898 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1900 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1901 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1902 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1903 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1905 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1906 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1908 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1910 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1912 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1914 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1916 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1919 /* The same as binops, but initialized by cp_parser_new so that
1920 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1922 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1924 /* Constructors and destructors. */
1926 /* Construct a new context. The context below this one on the stack
1927 is given by NEXT. */
1929 static cp_parser_context
*
1930 cp_parser_context_new (cp_parser_context
* next
)
1932 cp_parser_context
*context
;
1934 /* Allocate the storage. */
1935 if (cp_parser_context_free_list
!= NULL
)
1937 /* Pull the first entry from the free list. */
1938 context
= cp_parser_context_free_list
;
1939 cp_parser_context_free_list
= context
->next
;
1940 memset (context
, 0, sizeof (*context
));
1943 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1945 /* No errors have occurred yet in this context. */
1946 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1947 /* If this is not the bottommost context, copy information that we
1948 need from the previous context. */
1951 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1952 expression, then we are parsing one in this context, too. */
1953 context
->object_type
= next
->object_type
;
1954 /* Thread the stack. */
1955 context
->next
= next
;
1961 /* Managing the unparsed function queues. */
1963 #define unparsed_funs_with_default_args \
1964 parser->unparsed_queues->last ().funs_with_default_args
1965 #define unparsed_funs_with_definitions \
1966 parser->unparsed_queues->last ().funs_with_definitions
1967 #define unparsed_nsdmis \
1968 parser->unparsed_queues->last ().nsdmis
1969 #define unparsed_classes \
1970 parser->unparsed_queues->last ().classes
1973 push_unparsed_function_queues (cp_parser
*parser
)
1975 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1976 vec_safe_push (parser
->unparsed_queues
, e
);
1980 pop_unparsed_function_queues (cp_parser
*parser
)
1982 release_tree_vector (unparsed_funs_with_definitions
);
1983 parser
->unparsed_queues
->pop ();
1988 /* Constructors and destructors. */
1990 static cp_parser
*cp_parser_new
1993 /* Routines to parse various constructs.
1995 Those that return `tree' will return the error_mark_node (rather
1996 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1997 Sometimes, they will return an ordinary node if error-recovery was
1998 attempted, even though a parse error occurred. So, to check
1999 whether or not a parse error occurred, you should always use
2000 cp_parser_error_occurred. If the construct is optional (indicated
2001 either by an `_opt' in the name of the function that does the
2002 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2003 the construct is not present. */
2005 /* Lexical conventions [gram.lex] */
2007 static cp_expr cp_parser_identifier
2009 static cp_expr cp_parser_string_literal
2010 (cp_parser
*, bool, bool, bool);
2011 static cp_expr cp_parser_userdef_char_literal
2013 static tree cp_parser_userdef_string_literal
2015 static cp_expr cp_parser_userdef_numeric_literal
2018 /* Basic concepts [gram.basic] */
2020 static void cp_parser_translation_unit (cp_parser
*);
2022 /* Expressions [gram.expr] */
2024 static cp_expr cp_parser_primary_expression
2025 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2026 static cp_expr cp_parser_id_expression
2027 (cp_parser
*, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029 (cp_parser
*, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031 (cp_parser
*, bool, bool, bool, bool, bool = false);
2032 static tree cp_parser_nested_name_specifier
2033 (cp_parser
*, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035 (cp_parser
*, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2038 static tree cp_parser_postfix_open_square_expression
2039 (cp_parser
*, tree
, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2042 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2043 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2045 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2046 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2047 static void cp_parser_pseudo_destructor_name
2048 (cp_parser
*, tree
, tree
*, tree
*);
2049 static cp_expr cp_parser_unary_expression
2050 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2051 static enum tree_code cp_parser_unary_operator
2053 static tree cp_parser_has_attribute_expression
2055 static tree cp_parser_new_expression
2057 static vec
<tree
, va_gc
> *cp_parser_new_placement
2059 static tree cp_parser_new_type_id
2060 (cp_parser
*, tree
*);
2061 static cp_declarator
*cp_parser_new_declarator_opt
2063 static cp_declarator
*cp_parser_direct_new_declarator
2065 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2067 static tree cp_parser_delete_expression
2069 static cp_expr cp_parser_cast_expression
2070 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2071 static cp_expr cp_parser_binary_expression
2072 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2073 static tree cp_parser_question_colon_clause
2074 (cp_parser
*, cp_expr
);
2075 static cp_expr cp_parser_assignment_expression
2076 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2077 static enum tree_code cp_parser_assignment_operator_opt
2079 static cp_expr cp_parser_expression
2080 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2081 static cp_expr cp_parser_constant_expression
2082 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2083 static cp_expr cp_parser_builtin_offsetof
2085 static cp_expr cp_parser_lambda_expression
2087 static void cp_parser_lambda_introducer
2088 (cp_parser
*, tree
);
2089 static bool cp_parser_lambda_declarator_opt
2090 (cp_parser
*, tree
);
2091 static void cp_parser_lambda_body
2092 (cp_parser
*, tree
);
2094 /* Statements [gram.stmt.stmt] */
2096 static void cp_parser_statement
2097 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2098 static void cp_parser_label_for_labeled_statement
2099 (cp_parser
*, tree
);
2100 static tree cp_parser_expression_statement
2101 (cp_parser
*, tree
);
2102 static tree cp_parser_compound_statement
2103 (cp_parser
*, tree
, int, bool);
2104 static void cp_parser_statement_seq_opt
2105 (cp_parser
*, tree
);
2106 static tree cp_parser_selection_statement
2107 (cp_parser
*, bool *, vec
<tree
> *);
2108 static tree cp_parser_condition
2110 static tree cp_parser_iteration_statement
2111 (cp_parser
*, bool *, bool, unsigned short);
2112 static bool cp_parser_init_statement
2113 (cp_parser
*, tree
*decl
);
2114 static tree cp_parser_for
2115 (cp_parser
*, bool, unsigned short);
2116 static tree cp_parser_c_for
2117 (cp_parser
*, tree
, tree
, bool, unsigned short);
2118 static tree cp_parser_range_for
2119 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2120 static void do_range_for_auto_deduction
2122 static tree cp_parser_perform_range_for_lookup
2123 (tree
, tree
*, tree
*);
2124 static tree cp_parser_range_for_member_function
2126 static tree cp_parser_jump_statement
2128 static void cp_parser_declaration_statement
2131 static tree cp_parser_implicitly_scoped_statement
2132 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2133 static void cp_parser_already_scoped_statement
2134 (cp_parser
*, bool *, const token_indent_info
&);
2136 /* Declarations [gram.dcl.dcl] */
2138 static void cp_parser_declaration_seq_opt
2140 static void cp_parser_declaration
2142 static void cp_parser_toplevel_declaration
2144 static void cp_parser_block_declaration
2145 (cp_parser
*, bool);
2146 static void cp_parser_simple_declaration
2147 (cp_parser
*, bool, tree
*);
2148 static void cp_parser_decl_specifier_seq
2149 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2150 static tree cp_parser_storage_class_specifier_opt
2152 static tree cp_parser_function_specifier_opt
2153 (cp_parser
*, cp_decl_specifier_seq
*);
2154 static tree cp_parser_type_specifier
2155 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2157 static tree cp_parser_simple_type_specifier
2158 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2159 static tree cp_parser_type_name
2160 (cp_parser
*, bool);
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_parser_flags
, cp_decl_specifier_seq
*,
2202 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2203 location_t
*, tree
*);
2204 static cp_declarator
*cp_parser_declarator
2205 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2207 static cp_declarator
*cp_parser_direct_declarator
2208 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool);
2209 static enum tree_code cp_parser_ptr_operator
2210 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2211 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2213 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2215 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2217 static tree cp_parser_tx_qualifier_opt
2219 static tree cp_parser_late_return_type_opt
2220 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2221 static tree cp_parser_declarator_id
2222 (cp_parser
*, bool);
2223 static tree cp_parser_type_id
2224 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2225 static tree cp_parser_template_type_arg
2227 static tree
cp_parser_trailing_type_id (cp_parser
*);
2228 static tree cp_parser_type_id_1
2229 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2230 static void cp_parser_type_specifier_seq
2231 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2232 static tree cp_parser_parameter_declaration_clause
2233 (cp_parser
*, cp_parser_flags
);
2234 static tree cp_parser_parameter_declaration_list
2235 (cp_parser
*, cp_parser_flags
);
2236 static cp_parameter_declarator
*cp_parser_parameter_declaration
2237 (cp_parser
*, cp_parser_flags
, bool, bool *);
2238 static tree cp_parser_default_argument
2239 (cp_parser
*, bool);
2240 static void cp_parser_function_body
2241 (cp_parser
*, bool);
2242 static tree cp_parser_initializer
2243 (cp_parser
*, bool *, bool *, bool = false);
2244 static cp_expr cp_parser_initializer_clause
2245 (cp_parser
*, bool *);
2246 static cp_expr cp_parser_braced_list
2247 (cp_parser
*, bool*);
2248 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2249 (cp_parser
*, bool *);
2251 static void cp_parser_ctor_initializer_opt_and_function_body
2252 (cp_parser
*, bool);
2254 static tree cp_parser_late_parsing_omp_declare_simd
2255 (cp_parser
*, tree
);
2257 static tree cp_parser_late_parsing_oacc_routine
2258 (cp_parser
*, tree
);
2260 static tree synthesize_implicit_template_parm
2261 (cp_parser
*, tree
);
2262 static tree finish_fully_implicit_template
2263 (cp_parser
*, tree
);
2264 static void abort_fully_implicit_template
2267 /* Classes [gram.class] */
2269 static tree cp_parser_class_name
2270 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2273 static tree cp_parser_class_head
2274 (cp_parser
*, bool *);
2275 static enum tag_types cp_parser_class_key
2277 static void cp_parser_type_parameter_key
2278 (cp_parser
* parser
);
2279 static void cp_parser_member_specification_opt
2281 static void cp_parser_member_declaration
2283 static tree cp_parser_pure_specifier
2285 static tree cp_parser_constant_initializer
2288 /* Derived classes [gram.class.derived] */
2290 static tree cp_parser_base_clause
2292 static tree cp_parser_base_specifier
2295 /* Special member functions [gram.special] */
2297 static tree cp_parser_conversion_function_id
2299 static tree cp_parser_conversion_type_id
2301 static cp_declarator
*cp_parser_conversion_declarator_opt
2303 static void cp_parser_ctor_initializer_opt
2305 static void cp_parser_mem_initializer_list
2307 static tree cp_parser_mem_initializer
2309 static tree cp_parser_mem_initializer_id
2312 /* Overloading [gram.over] */
2314 static cp_expr cp_parser_operator_function_id
2316 static cp_expr cp_parser_operator
2317 (cp_parser
*, location_t
);
2319 /* Templates [gram.temp] */
2321 static void cp_parser_template_declaration
2322 (cp_parser
*, bool);
2323 static tree cp_parser_template_parameter_list
2325 static tree cp_parser_template_parameter
2326 (cp_parser
*, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser
*, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser
*, bool, bool, enum tag_types
, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2333 static tree cp_parser_template_argument_list
2335 static tree cp_parser_template_argument
2337 static void cp_parser_explicit_instantiation
2339 static void cp_parser_explicit_specialization
2342 /* Exception handling [gram.exception] */
2344 static tree cp_parser_try_block
2346 static void cp_parser_function_try_block
2348 static void cp_parser_handler_seq
2350 static void cp_parser_handler
2352 static tree cp_parser_exception_declaration
2354 static tree cp_parser_throw_expression
2356 static tree cp_parser_exception_specification_opt
2358 static tree cp_parser_type_id_list
2361 /* GNU Extensions */
2363 static tree cp_parser_asm_specification_opt
2365 static tree cp_parser_asm_operand_list
2367 static tree cp_parser_asm_clobber_list
2369 static tree cp_parser_asm_label_list
2371 static bool cp_next_tokens_can_be_attribute_p
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 static bool cp_next_tokens_can_be_std_attribute_p
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser
*, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser
*, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser
*, size_t);
2383 static tree cp_parser_attributes_opt
2385 static tree cp_parser_gnu_attributes_opt
2387 static tree cp_parser_gnu_attribute_list
2388 (cp_parser
*, bool = false);
2389 static tree cp_parser_std_attribute
2390 (cp_parser
*, tree
);
2391 static tree cp_parser_std_attribute_spec
2393 static tree cp_parser_std_attribute_spec_seq
2395 static size_t cp_parser_skip_attributes_opt
2396 (cp_parser
*, size_t);
2397 static bool cp_parser_extension_opt
2398 (cp_parser
*, int *);
2399 static void cp_parser_label_declaration
2402 /* Concept Extensions */
2404 static tree cp_parser_requires_clause
2406 static tree cp_parser_requires_clause_opt
2408 static tree cp_parser_requires_expression
2410 static tree cp_parser_requirement_parameter_list
2412 static tree cp_parser_requirement_body
2414 static tree cp_parser_requirement_list
2416 static tree cp_parser_requirement
2418 static tree cp_parser_simple_requirement
2420 static tree cp_parser_compound_requirement
2422 static tree cp_parser_type_requirement
2424 static tree cp_parser_nested_requirement
2427 /* Transactional Memory Extensions */
2429 static tree cp_parser_transaction
2430 (cp_parser
*, cp_token
*);
2431 static tree cp_parser_transaction_expression
2432 (cp_parser
*, enum rid
);
2433 static void cp_parser_function_transaction
2434 (cp_parser
*, enum rid
);
2435 static tree cp_parser_transaction_cancel
2438 enum pragma_context
{
2445 static bool cp_parser_pragma
2446 (cp_parser
*, enum pragma_context
, bool *);
2448 /* Objective-C++ Productions */
2450 static tree cp_parser_objc_message_receiver
2452 static tree cp_parser_objc_message_args
2454 static tree cp_parser_objc_message_expression
2456 static cp_expr cp_parser_objc_encode_expression
2458 static tree cp_parser_objc_defs_expression
2460 static tree cp_parser_objc_protocol_expression
2462 static tree cp_parser_objc_selector_expression
2464 static cp_expr cp_parser_objc_expression
2466 static bool cp_parser_objc_selector_p
2468 static tree cp_parser_objc_selector
2470 static tree cp_parser_objc_protocol_refs_opt
2472 static void cp_parser_objc_declaration
2473 (cp_parser
*, tree
);
2474 static tree cp_parser_objc_statement
2476 static bool cp_parser_objc_valid_prefix_attributes
2477 (cp_parser
*, tree
*);
2478 static void cp_parser_objc_at_property_declaration
2480 static void cp_parser_objc_at_synthesize_declaration
2482 static void cp_parser_objc_at_dynamic_declaration
2484 static tree cp_parser_objc_struct_declaration
2487 /* Utility Routines */
2489 static cp_expr cp_parser_lookup_name
2490 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2491 static tree cp_parser_lookup_name_simple
2492 (cp_parser
*, tree
, location_t
);
2493 static tree cp_parser_maybe_treat_template_as_class
2495 static bool cp_parser_check_declarator_template_parameters
2496 (cp_parser
*, cp_declarator
*, location_t
);
2497 static bool cp_parser_check_template_parameters
2498 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2499 static cp_expr cp_parser_simple_cast_expression
2501 static tree cp_parser_global_scope_opt
2502 (cp_parser
*, bool);
2503 static bool cp_parser_constructor_declarator_p
2504 (cp_parser
*, bool);
2505 static tree cp_parser_function_definition_from_specifiers_and_declarator
2506 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2507 static tree cp_parser_function_definition_after_declarator
2508 (cp_parser
*, bool);
2509 static bool cp_parser_template_declaration_after_export
2510 (cp_parser
*, bool);
2511 static void cp_parser_perform_template_parameter_access_checks
2512 (vec
<deferred_access_check
, va_gc
> *);
2513 static tree cp_parser_single_declaration
2514 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2515 static cp_expr cp_parser_functional_cast
2516 (cp_parser
*, tree
);
2517 static tree cp_parser_save_member_function_body
2518 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2519 static tree cp_parser_save_nsdmi
2521 static tree cp_parser_enclosed_template_argument_list
2523 static void cp_parser_save_default_args
2524 (cp_parser
*, tree
);
2525 static void cp_parser_late_parsing_for_member
2526 (cp_parser
*, tree
);
2527 static tree cp_parser_late_parse_one_default_arg
2528 (cp_parser
*, tree
, tree
, tree
);
2529 static void cp_parser_late_parsing_nsdmi
2530 (cp_parser
*, tree
);
2531 static void cp_parser_late_parsing_default_args
2532 (cp_parser
*, tree
);
2533 static tree cp_parser_sizeof_operand
2534 (cp_parser
*, enum rid
);
2535 static cp_expr cp_parser_trait_expr
2536 (cp_parser
*, enum rid
);
2537 static bool cp_parser_declares_only_class_p
2539 static void cp_parser_set_storage_class
2540 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2541 static void cp_parser_set_decl_spec_type
2542 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2543 static void set_and_check_decl_spec_loc
2544 (cp_decl_specifier_seq
*decl_specs
,
2545 cp_decl_spec ds
, cp_token
*);
2546 static bool cp_parser_friend_p
2547 (const cp_decl_specifier_seq
*);
2548 static void cp_parser_required_error
2549 (cp_parser
*, required_token
, bool, location_t
);
2550 static cp_token
*cp_parser_require
2551 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2552 static cp_token
*cp_parser_require_keyword
2553 (cp_parser
*, enum rid
, required_token
);
2554 static bool cp_parser_token_starts_function_definition_p
2556 static bool cp_parser_next_token_starts_class_definition_p
2558 static bool cp_parser_next_token_ends_template_argument_p
2560 static bool cp_parser_nth_token_starts_template_argument_list_p
2561 (cp_parser
*, size_t);
2562 static enum tag_types cp_parser_token_is_class_key
2564 static enum tag_types cp_parser_token_is_type_parameter_key
2566 static void cp_parser_check_class_key
2567 (enum tag_types
, tree type
);
2568 static void cp_parser_check_access_in_redeclaration
2569 (tree type
, location_t location
);
2570 static bool cp_parser_optional_template_keyword
2572 static void cp_parser_pre_parsed_nested_name_specifier
2574 static bool cp_parser_cache_group
2575 (cp_parser
*, enum cpp_ttype
, unsigned);
2576 static tree cp_parser_cache_defarg
2577 (cp_parser
*parser
, bool nsdmi
);
2578 static void cp_parser_parse_tentatively
2580 static void cp_parser_commit_to_tentative_parse
2582 static void cp_parser_commit_to_topmost_tentative_parse
2584 static void cp_parser_abort_tentative_parse
2586 static bool cp_parser_parse_definitely
2588 static inline bool cp_parser_parsing_tentatively
2590 static bool cp_parser_uncommitted_to_tentative_parse_p
2592 static void cp_parser_error
2593 (cp_parser
*, const char *);
2594 static void cp_parser_name_lookup_error
2595 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2596 static bool cp_parser_simulate_error
2598 static bool cp_parser_check_type_definition
2600 static void cp_parser_check_for_definition_in_return_type
2601 (cp_declarator
*, tree
, location_t type_location
);
2602 static void cp_parser_check_for_invalid_template_id
2603 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2604 static bool cp_parser_non_integral_constant_expression
2605 (cp_parser
*, non_integral_constant
);
2606 static void cp_parser_diagnose_invalid_type_name
2607 (cp_parser
*, tree
, location_t
);
2608 static bool cp_parser_parse_and_diagnose_invalid_type_name
2610 static int cp_parser_skip_to_closing_parenthesis
2611 (cp_parser
*, bool, bool, bool);
2612 static void cp_parser_skip_to_end_of_statement
2614 static void cp_parser_consume_semicolon_at_end_of_statement
2616 static void cp_parser_skip_to_end_of_block_or_statement
2618 static bool cp_parser_skip_to_closing_brace
2620 static void cp_parser_skip_to_end_of_template_parameter_list
2622 static void cp_parser_skip_to_pragma_eol
2623 (cp_parser
*, cp_token
*);
2624 static bool cp_parser_error_occurred
2626 static bool cp_parser_allow_gnu_extensions_p
2628 static bool cp_parser_is_pure_string_literal
2630 static bool cp_parser_is_string_literal
2632 static bool cp_parser_is_keyword
2633 (cp_token
*, enum rid
);
2634 static tree cp_parser_make_typename_type
2635 (cp_parser
*, tree
, location_t location
);
2636 static cp_declarator
* cp_parser_make_indirect_declarator
2637 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2638 static bool cp_parser_compound_literal_p
2640 static bool cp_parser_array_designator_p
2642 static bool cp_parser_init_statement_p
2644 static bool cp_parser_skip_to_closing_square_bracket
2647 /* Concept-related syntactic transformations */
2649 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2650 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2652 // -------------------------------------------------------------------------- //
2653 // Unevaluated Operand Guard
2655 // Implementation of an RAII helper for unevaluated operand parsing.
2656 cp_unevaluated::cp_unevaluated ()
2658 ++cp_unevaluated_operand
;
2659 ++c_inhibit_evaluation_warnings
;
2662 cp_unevaluated::~cp_unevaluated ()
2664 --c_inhibit_evaluation_warnings
;
2665 --cp_unevaluated_operand
;
2668 // -------------------------------------------------------------------------- //
2669 // Tentative Parsing
2671 /* Returns nonzero if we are parsing tentatively. */
2674 cp_parser_parsing_tentatively (cp_parser
* parser
)
2676 return parser
->context
->next
!= NULL
;
2679 /* Returns nonzero if TOKEN is a string literal. */
2682 cp_parser_is_pure_string_literal (cp_token
* token
)
2684 return (token
->type
== CPP_STRING
||
2685 token
->type
== CPP_STRING16
||
2686 token
->type
== CPP_STRING32
||
2687 token
->type
== CPP_WSTRING
||
2688 token
->type
== CPP_UTF8STRING
);
2691 /* Returns nonzero if TOKEN is a string literal
2692 of a user-defined string literal. */
2695 cp_parser_is_string_literal (cp_token
* token
)
2697 return (cp_parser_is_pure_string_literal (token
) ||
2698 token
->type
== CPP_STRING_USERDEF
||
2699 token
->type
== CPP_STRING16_USERDEF
||
2700 token
->type
== CPP_STRING32_USERDEF
||
2701 token
->type
== CPP_WSTRING_USERDEF
||
2702 token
->type
== CPP_UTF8STRING_USERDEF
);
2705 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2708 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2710 return token
->keyword
== keyword
;
2713 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2716 static enum pragma_kind
2717 cp_parser_pragma_kind (cp_token
*token
)
2719 if (token
->type
!= CPP_PRAGMA
)
2721 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2722 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2725 /* Helper function for cp_parser_error.
2726 Having peeked a token of kind TOK1_KIND that might signify
2727 a conflict marker, peek successor tokens to determine
2728 if we actually do have a conflict marker.
2729 Specifically, we consider a run of 7 '<', '=' or '>' characters
2730 at the start of a line as a conflict marker.
2731 These come through the lexer as three pairs and a single,
2732 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2733 If it returns true, *OUT_LOC is written to with the location/range
2737 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2738 location_t
*out_loc
)
2740 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2741 if (token2
->type
!= tok1_kind
)
2743 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2744 if (token3
->type
!= tok1_kind
)
2746 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2747 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2750 /* It must be at the start of the line. */
2751 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2752 if (LOCATION_COLUMN (start_loc
) != 1)
2755 /* We have a conflict marker. Construct a location of the form:
2758 with start == caret, finishing at the end of the marker. */
2759 location_t finish_loc
= get_finish (token4
->location
);
2760 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2765 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 get_matching_symbol (required_token token_desc
)
2776 case RT_CLOSE_BRACE
:
2778 case RT_CLOSE_PAREN
:
2783 /* Attempt to convert TOKEN_DESC from a required_token to an
2784 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2786 static enum cpp_ttype
2787 get_required_cpp_ttype (required_token token_desc
)
2792 return CPP_SEMICOLON
;
2794 return CPP_OPEN_PAREN
;
2795 case RT_CLOSE_BRACE
:
2796 return CPP_CLOSE_BRACE
;
2798 return CPP_OPEN_BRACE
;
2799 case RT_CLOSE_SQUARE
:
2800 return CPP_CLOSE_SQUARE
;
2801 case RT_OPEN_SQUARE
:
2802 return CPP_OPEN_SQUARE
;
2807 case RT_CLOSE_PAREN
:
2808 return CPP_CLOSE_PAREN
;
2811 /* Use CPP_EOF as a "no completions possible" code. */
2817 /* Subroutine of cp_parser_error and cp_parser_required_error.
2819 Issue a diagnostic of the form
2820 FILE:LINE: MESSAGE before TOKEN
2821 where TOKEN is the next token in the input stream. MESSAGE
2822 (specified by the caller) is usually of the form "expected
2825 This bypasses the check for tentative passing, and potentially
2826 adds material needed by cp_parser_required_error.
2828 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2829 suggesting insertion of the missing token.
2831 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2832 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2837 required_token missing_token_desc
,
2838 location_t matching_location
)
2840 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2841 /* This diagnostic makes more sense if it is tagged to the line
2842 of the token we just peeked at. */
2843 cp_lexer_set_source_position_from_token (token
);
2845 if (token
->type
== CPP_PRAGMA
)
2847 error_at (token
->location
,
2848 "%<#pragma%> is not allowed here");
2849 cp_parser_skip_to_pragma_eol (parser
, token
);
2853 /* If this is actually a conflict marker, report it as such. */
2854 if (token
->type
== CPP_LSHIFT
2855 || token
->type
== CPP_RSHIFT
2856 || token
->type
== CPP_EQ_EQ
)
2859 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2861 error_at (loc
, "version control conflict marker in file");
2862 expanded_location token_exploc
= expand_location (token
->location
);
2863 /* Consume tokens until the end of the source line. */
2866 cp_lexer_consume_token (parser
->lexer
);
2867 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2870 expanded_location next_exploc
= expand_location (next
->location
);
2871 if (next_exploc
.file
!= token_exploc
.file
)
2873 if (next_exploc
.line
!= token_exploc
.line
)
2880 gcc_rich_location
richloc (input_location
);
2882 bool added_matching_location
= false;
2884 if (missing_token_desc
!= RT_NONE
)
2886 /* Potentially supply a fix-it hint, suggesting to add the
2887 missing token immediately after the *previous* token.
2888 This may move the primary location within richloc. */
2889 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2890 location_t prev_token_loc
2891 = cp_lexer_previous_token (parser
->lexer
)->location
;
2892 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2894 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2895 Attempt to consolidate diagnostics by printing it as a
2896 secondary range within the main diagnostic. */
2897 if (matching_location
!= UNKNOWN_LOCATION
)
2898 added_matching_location
2899 = richloc
.add_location_if_nearby (matching_location
);
2902 /* Actually emit the error. */
2903 c_parse_error (gmsgid
,
2904 /* Because c_parser_error does not understand
2905 CPP_KEYWORD, keywords are treated like
2907 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2908 token
->u
.value
, token
->flags
, &richloc
);
2910 if (missing_token_desc
!= RT_NONE
)
2912 /* If we weren't able to consolidate matching_location, then
2913 print it as a secondary diagnostic. */
2914 if (matching_location
!= UNKNOWN_LOCATION
2915 && !added_matching_location
)
2916 inform (matching_location
, "to match this %qs",
2917 get_matching_symbol (missing_token_desc
));
2921 /* If not parsing tentatively, issue a diagnostic of the form
2922 FILE:LINE: MESSAGE before TOKEN
2923 where TOKEN is the next token in the input stream. MESSAGE
2924 (specified by the caller) is usually of the form "expected
2928 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2930 if (!cp_parser_simulate_error (parser
))
2931 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2934 /* Issue an error about name-lookup failing. NAME is the
2935 IDENTIFIER_NODE DECL is the result of
2936 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2937 the thing that we hoped to find. */
2940 cp_parser_name_lookup_error (cp_parser
* parser
,
2943 name_lookup_error desired
,
2944 location_t location
)
2946 /* If name lookup completely failed, tell the user that NAME was not
2948 if (decl
== error_mark_node
)
2950 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2951 error_at (location
, "%<%E::%E%> has not been declared",
2952 parser
->scope
, name
);
2953 else if (parser
->scope
== global_namespace
)
2954 error_at (location
, "%<::%E%> has not been declared", name
);
2955 else if (parser
->object_scope
2956 && !CLASS_TYPE_P (parser
->object_scope
))
2957 error_at (location
, "request for member %qE in non-class type %qT",
2958 name
, parser
->object_scope
);
2959 else if (parser
->object_scope
)
2960 error_at (location
, "%<%T::%E%> has not been declared",
2961 parser
->object_scope
, name
);
2963 error_at (location
, "%qE has not been declared", name
);
2965 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2970 error_at (location
, "%<%E::%E%> is not a type",
2971 parser
->scope
, name
);
2974 error_at (location
, "%<%E::%E%> is not a class or namespace",
2975 parser
->scope
, name
);
2979 "%<%E::%E%> is not a class, namespace, or enumeration",
2980 parser
->scope
, name
);
2987 else if (parser
->scope
== global_namespace
)
2992 error_at (location
, "%<::%E%> is not a type", name
);
2995 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2999 "%<::%E%> is not a class, namespace, or enumeration",
3011 error_at (location
, "%qE is not a type", name
);
3014 error_at (location
, "%qE is not a class or namespace", name
);
3018 "%qE is not a class, namespace, or enumeration", name
);
3026 /* If we are parsing tentatively, remember that an error has occurred
3027 during this tentative parse. Returns true if the error was
3028 simulated; false if a message should be issued by the caller. */
3031 cp_parser_simulate_error (cp_parser
* parser
)
3033 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3035 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3041 /* This function is called when a type is defined. If type
3042 definitions are forbidden at this point, an error message is
3046 cp_parser_check_type_definition (cp_parser
* parser
)
3048 /* If types are forbidden here, issue a message. */
3049 if (parser
->type_definition_forbidden_message
)
3051 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3052 in the message need to be interpreted. */
3053 error (parser
->type_definition_forbidden_message
);
3059 /* This function is called when the DECLARATOR is processed. The TYPE
3060 was a type defined in the decl-specifiers. If it is invalid to
3061 define a type in the decl-specifiers for DECLARATOR, an error is
3062 issued. TYPE_LOCATION is the location of TYPE and is used
3063 for error reporting. */
3066 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3067 tree type
, location_t type_location
)
3069 /* [dcl.fct] forbids type definitions in return types.
3070 Unfortunately, it's not easy to know whether or not we are
3071 processing a return type until after the fact. */
3073 && (declarator
->kind
== cdk_pointer
3074 || declarator
->kind
== cdk_reference
3075 || declarator
->kind
== cdk_ptrmem
))
3076 declarator
= declarator
->declarator
;
3078 && declarator
->kind
== cdk_function
)
3080 error_at (type_location
,
3081 "new types may not be defined in a return type");
3082 inform (type_location
,
3083 "(perhaps a semicolon is missing after the definition of %qT)",
3088 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3089 "<" in any valid C++ program. If the next token is indeed "<",
3090 issue a message warning the user about what appears to be an
3091 invalid attempt to form a template-id. LOCATION is the location
3092 of the type-specifier (TYPE) */
3095 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3097 enum tag_types tag_type
,
3098 location_t location
)
3100 cp_token_position start
= 0;
3102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3104 if (TREE_CODE (type
) == TYPE_DECL
)
3105 type
= TREE_TYPE (type
);
3106 if (TYPE_P (type
) && !template_placeholder_p (type
))
3107 error_at (location
, "%qT is not a template", type
);
3108 else if (identifier_p (type
))
3110 if (tag_type
!= none_type
)
3111 error_at (location
, "%qE is not a class template", type
);
3113 error_at (location
, "%qE is not a template", type
);
3116 error_at (location
, "invalid template-id");
3117 /* Remember the location of the invalid "<". */
3118 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3119 start
= cp_lexer_token_position (parser
->lexer
, true);
3120 /* Consume the "<". */
3121 cp_lexer_consume_token (parser
->lexer
);
3122 /* Parse the template arguments. */
3123 cp_parser_enclosed_template_argument_list (parser
);
3124 /* Permanently remove the invalid template arguments so that
3125 this error message is not issued again. */
3127 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3131 /* If parsing an integral constant-expression, issue an error message
3132 about the fact that THING appeared and return true. Otherwise,
3133 return false. In either case, set
3134 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3137 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3138 non_integral_constant thing
)
3140 parser
->non_integral_constant_expression_p
= true;
3141 if (parser
->integral_constant_expression_p
)
3143 if (!parser
->allow_non_integral_constant_expression_p
)
3145 const char *msg
= NULL
;
3149 pedwarn (input_location
, OPT_Wpedantic
,
3150 "ISO C++ forbids using a floating-point literal "
3151 "in a constant-expression");
3154 error ("a cast to a type other than an integral or "
3155 "enumeration type cannot appear in a "
3156 "constant-expression");
3159 error ("%<typeid%> operator "
3160 "cannot appear in a constant-expression");
3163 error ("non-constant compound literals "
3164 "cannot appear in a constant-expression");
3167 error ("a function call "
3168 "cannot appear in a constant-expression");
3171 error ("an increment "
3172 "cannot appear in a constant-expression");
3175 error ("an decrement "
3176 "cannot appear in a constant-expression");
3179 error ("an array reference "
3180 "cannot appear in a constant-expression");
3182 case NIC_ADDR_LABEL
:
3183 error ("the address of a label "
3184 "cannot appear in a constant-expression");
3186 case NIC_OVERLOADED
:
3187 error ("calls to overloaded operators "
3188 "cannot appear in a constant-expression");
3190 case NIC_ASSIGNMENT
:
3191 error ("an assignment cannot appear in a constant-expression");
3194 error ("a comma operator "
3195 "cannot appear in a constant-expression");
3197 case NIC_CONSTRUCTOR
:
3198 error ("a call to a constructor "
3199 "cannot appear in a constant-expression");
3201 case NIC_TRANSACTION
:
3202 error ("a transaction expression "
3203 "cannot appear in a constant-expression");
3209 msg
= "__FUNCTION__";
3211 case NIC_PRETTY_FUNC
:
3212 msg
= "__PRETTY_FUNCTION__";
3232 case NIC_PREINCREMENT
:
3235 case NIC_PREDECREMENT
:
3248 error ("%qs cannot appear in a constant-expression", msg
);
3255 /* Emit a diagnostic for an invalid type name. This function commits
3256 to the current active tentative parse, if any. (Otherwise, the
3257 problematic construct might be encountered again later, resulting
3258 in duplicate error messages.) LOCATION is the location of ID. */
3261 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3262 location_t location
)
3264 tree decl
, ambiguous_decls
;
3265 cp_parser_commit_to_tentative_parse (parser
);
3266 /* Try to lookup the identifier. */
3267 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3268 /*is_template=*/false,
3269 /*is_namespace=*/false,
3270 /*check_dependency=*/true,
3271 &ambiguous_decls
, location
);
3272 if (ambiguous_decls
)
3273 /* If the lookup was ambiguous, an error will already have
3276 /* If the lookup found a template-name, it means that the user forgot
3277 to specify an argument list. Emit a useful error message. */
3278 if (DECL_TYPE_TEMPLATE_P (decl
))
3280 auto_diagnostic_group d
;
3282 "invalid use of template-name %qE without an argument list",
3284 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3285 inform (location
, "class template argument deduction is only available "
3286 "with -std=c++17 or -std=gnu++17");
3287 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3289 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3290 error_at (location
, "invalid use of destructor %qD as a type", id
);
3291 else if (TREE_CODE (decl
) == TYPE_DECL
)
3292 /* Something like 'unsigned A a;' */
3293 error_at (location
, "invalid combination of multiple type-specifiers");
3294 else if (!parser
->scope
)
3296 /* Issue an error message. */
3297 auto_diagnostic_group d
;
3299 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3300 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3301 if (const char *suggestion
= hint
.suggestion ())
3303 gcc_rich_location
richloc (location
);
3304 richloc
.add_fixit_replace (suggestion
);
3306 "%qE does not name a type; did you mean %qs?",
3310 error_at (location
, "%qE does not name a type", id
);
3311 /* If we're in a template class, it's possible that the user was
3312 referring to a type from a base class. For example:
3314 template <typename T> struct A { typedef T X; };
3315 template <typename T> struct B : public A<T> { X x; };
3317 The user should have said "typename A<T>::X". */
3318 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3319 inform (location
, "C++11 %<constexpr%> only available with "
3320 "-std=c++11 or -std=gnu++11");
3321 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3322 inform (location
, "C++11 %<noexcept%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (cxx_dialect
< cxx11
3325 && TREE_CODE (id
) == IDENTIFIER_NODE
3326 && id_equal (id
, "thread_local"))
3327 inform (location
, "C++11 %<thread_local%> only available with "
3328 "-std=c++11 or -std=gnu++11");
3329 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3330 inform (location
, "%<concept%> only available with -fconcepts");
3331 else if (processing_template_decl
&& current_class_type
3332 && TYPE_BINFO (current_class_type
))
3336 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3340 tree base_type
= BINFO_TYPE (b
);
3341 if (CLASS_TYPE_P (base_type
)
3342 && dependent_type_p (base_type
))
3345 /* Go from a particular instantiation of the
3346 template (which will have an empty TYPE_FIELDs),
3347 to the main version. */
3348 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3349 for (field
= TYPE_FIELDS (base_type
);
3351 field
= DECL_CHAIN (field
))
3352 if (TREE_CODE (field
) == TYPE_DECL
3353 && DECL_NAME (field
) == id
)
3356 "(perhaps %<typename %T::%E%> was intended)",
3357 BINFO_TYPE (b
), id
);
3366 /* Here we diagnose qualified-ids where the scope is actually correct,
3367 but the identifier does not resolve to a valid type name. */
3368 else if (parser
->scope
!= error_mark_node
)
3370 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3372 auto_diagnostic_group d
;
3374 if (decl
== error_mark_node
)
3375 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3377 const char *suggestion
= hint
.suggestion ();
3378 gcc_rich_location
richloc (location_of (id
));
3380 richloc
.add_fixit_replace (suggestion
);
3381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3385 "%qE in namespace %qE does not name a template"
3386 " type; did you mean %qs?",
3387 id
, parser
->scope
, suggestion
);
3390 "%qE in namespace %qE does not name a template type",
3393 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3397 "%qE in namespace %qE does not name a template"
3398 " type; did you mean %qs?",
3399 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3402 "%qE in namespace %qE does not name a template"
3404 TREE_OPERAND (id
, 0), parser
->scope
);
3410 "%qE in namespace %qE does not name a type"
3411 "; did you mean %qs?",
3412 id
, parser
->scope
, suggestion
);
3415 "%qE in namespace %qE does not name a type",
3419 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3421 else if (CLASS_TYPE_P (parser
->scope
)
3422 && constructor_name_p (id
, parser
->scope
))
3425 auto_diagnostic_group d
;
3426 error_at (location
, "%<%T::%E%> names the constructor, not"
3427 " the type", parser
->scope
, id
);
3428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3429 error_at (location
, "and %qT has no template constructors",
3432 else if (TYPE_P (parser
->scope
)
3433 && dependent_scope_p (parser
->scope
))
3435 gcc_rich_location
richloc (location
);
3436 richloc
.add_fixit_insert_before ("typename ");
3437 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3439 "need %<typename%> before %<%T::%D::%E%> because "
3440 "%<%T::%D%> is a dependent scope",
3441 TYPE_CONTEXT (parser
->scope
),
3442 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3444 TYPE_CONTEXT (parser
->scope
),
3445 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3447 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3448 "%qT is a dependent scope",
3449 parser
->scope
, id
, parser
->scope
);
3451 else if (TYPE_P (parser
->scope
))
3453 auto_diagnostic_group d
;
3454 if (!COMPLETE_TYPE_P (parser
->scope
))
3455 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3457 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3458 error_at (location_of (id
),
3459 "%qE in %q#T does not name a template type",
3461 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3462 error_at (location_of (id
),
3463 "%qE in %q#T does not name a template type",
3464 TREE_OPERAND (id
, 0), parser
->scope
);
3466 error_at (location_of (id
),
3467 "%qE in %q#T does not name a type",
3470 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3477 /* Check for a common situation where a type-name should be present,
3478 but is not, and issue a sensible error message. Returns true if an
3479 invalid type-name was detected.
3481 The situation handled by this function are variable declarations of the
3482 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3483 Usually, `ID' should name a type, but if we got here it means that it
3484 does not. We try to emit the best possible error message depending on
3485 how exactly the id-expression looks like. */
3488 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3491 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3493 /* Avoid duplicate error about ambiguous lookup. */
3494 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3496 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3497 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3501 cp_parser_parse_tentatively (parser
);
3502 id
= cp_parser_id_expression (parser
,
3503 /*template_keyword_p=*/false,
3504 /*check_dependency_p=*/true,
3505 /*template_p=*/NULL
,
3506 /*declarator_p=*/false,
3507 /*optional_p=*/false);
3508 /* If the next token is a (, this is a function with no explicit return
3509 type, i.e. constructor, destructor or conversion op. */
3510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3511 || TREE_CODE (id
) == TYPE_DECL
)
3513 cp_parser_abort_tentative_parse (parser
);
3516 if (!cp_parser_parse_definitely (parser
))
3519 /* Emit a diagnostic for the invalid type. */
3520 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3522 /* If we aren't in the middle of a declarator (i.e. in a
3523 parameter-declaration-clause), skip to the end of the declaration;
3524 there's no point in trying to process it. */
3525 if (!parser
->in_declarator_p
)
3526 cp_parser_skip_to_end_of_block_or_statement (parser
);
3530 /* Consume tokens up to, and including, the next non-nested closing `)'.
3531 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3532 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3533 found an unnested token of that type. */
3536 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3541 unsigned paren_depth
= 0;
3542 unsigned brace_depth
= 0;
3543 unsigned square_depth
= 0;
3544 unsigned condop_depth
= 0;
3546 if (recovering
&& or_ttype
== CPP_EOF
3547 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3552 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3554 /* Have we found what we're looking for before the closing paren? */
3555 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3556 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3559 switch (token
->type
)
3561 case CPP_PRAGMA_EOL
:
3562 if (!parser
->lexer
->in_pragma
)
3566 /* If we've run out of tokens, then there is no closing `)'. */
3569 /* This is good for lambda expression capture-lists. */
3570 case CPP_OPEN_SQUARE
:
3573 case CPP_CLOSE_SQUARE
:
3574 if (!square_depth
--)
3579 /* This matches the processing in skip_to_end_of_statement. */
3584 case CPP_OPEN_BRACE
:
3587 case CPP_CLOSE_BRACE
:
3592 case CPP_OPEN_PAREN
:
3597 case CPP_CLOSE_PAREN
:
3598 if (!brace_depth
&& !paren_depth
--)
3601 cp_lexer_consume_token (parser
->lexer
);
3607 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3612 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3620 /* Consume the token. */
3621 cp_lexer_consume_token (parser
->lexer
);
3625 /* Consume tokens up to, and including, the next non-nested closing `)'.
3626 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3627 are doing error recovery. Returns -1 if OR_COMMA is true and we
3628 found an unnested token of that type. */
3631 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3636 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3637 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3638 ttype
, consume_paren
);
3641 /* Consume tokens until we reach the end of the current statement.
3642 Normally, that will be just before consuming a `;'. However, if a
3643 non-nested `}' comes first, then we stop before consuming that. */
3646 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3648 unsigned nesting_depth
= 0;
3650 /* Unwind generic function template scope if necessary. */
3651 if (parser
->fully_implicit_function_template_p
)
3652 abort_fully_implicit_template (parser
);
3656 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3658 switch (token
->type
)
3660 case CPP_PRAGMA_EOL
:
3661 if (!parser
->lexer
->in_pragma
)
3665 /* If we've run out of tokens, stop. */
3669 /* If the next token is a `;', we have reached the end of the
3675 case CPP_CLOSE_BRACE
:
3676 /* If this is a non-nested '}', stop before consuming it.
3677 That way, when confronted with something like:
3681 we stop before consuming the closing '}', even though we
3682 have not yet reached a `;'. */
3683 if (nesting_depth
== 0)
3686 /* If it is the closing '}' for a block that we have
3687 scanned, stop -- but only after consuming the token.
3693 we will stop after the body of the erroneously declared
3694 function, but before consuming the following `typedef'
3696 if (--nesting_depth
== 0)
3698 cp_lexer_consume_token (parser
->lexer
);
3703 case CPP_OPEN_BRACE
:
3711 /* Consume the token. */
3712 cp_lexer_consume_token (parser
->lexer
);
3716 /* This function is called at the end of a statement or declaration.
3717 If the next token is a semicolon, it is consumed; otherwise, error
3718 recovery is attempted. */
3721 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3723 /* Look for the trailing `;'. */
3724 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3726 /* If there is additional (erroneous) input, skip to the end of
3728 cp_parser_skip_to_end_of_statement (parser
);
3729 /* If the next token is now a `;', consume it. */
3730 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3731 cp_lexer_consume_token (parser
->lexer
);
3735 /* Skip tokens until we have consumed an entire block, or until we
3736 have consumed a non-nested `;'. */
3739 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3741 int nesting_depth
= 0;
3743 /* Unwind generic function template scope if necessary. */
3744 if (parser
->fully_implicit_function_template_p
)
3745 abort_fully_implicit_template (parser
);
3747 while (nesting_depth
>= 0)
3749 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3751 switch (token
->type
)
3753 case CPP_PRAGMA_EOL
:
3754 if (!parser
->lexer
->in_pragma
)
3758 /* If we've run out of tokens, stop. */
3762 /* Stop if this is an unnested ';'. */
3767 case CPP_CLOSE_BRACE
:
3768 /* Stop if this is an unnested '}', or closes the outermost
3771 if (nesting_depth
< 0)
3777 case CPP_OPEN_BRACE
:
3786 /* Consume the token. */
3787 cp_lexer_consume_token (parser
->lexer
);
3791 /* Skip tokens until a non-nested closing curly brace is the next
3792 token, or there are no more tokens. Return true in the first case,
3796 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3798 unsigned nesting_depth
= 0;
3802 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3804 switch (token
->type
)
3806 case CPP_PRAGMA_EOL
:
3807 if (!parser
->lexer
->in_pragma
)
3811 /* If we've run out of tokens, stop. */
3814 case CPP_CLOSE_BRACE
:
3815 /* If the next token is a non-nested `}', then we have reached
3816 the end of the current block. */
3817 if (nesting_depth
-- == 0)
3821 case CPP_OPEN_BRACE
:
3822 /* If it the next token is a `{', then we are entering a new
3823 block. Consume the entire block. */
3831 /* Consume the token. */
3832 cp_lexer_consume_token (parser
->lexer
);
3836 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3837 parameter is the PRAGMA token, allowing us to purge the entire pragma
3841 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3845 parser
->lexer
->in_pragma
= false;
3848 token
= cp_lexer_consume_token (parser
->lexer
);
3849 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3851 /* Ensure that the pragma is not parsed again. */
3852 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3855 /* Require pragma end of line, resyncing with it as necessary. The
3856 arguments are as for cp_parser_skip_to_pragma_eol. */
3859 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3861 parser
->lexer
->in_pragma
= false;
3862 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3863 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3866 /* This is a simple wrapper around make_typename_type. When the id is
3867 an unresolved identifier node, we can provide a superior diagnostic
3868 using cp_parser_diagnose_invalid_type_name. */
3871 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3872 location_t id_location
)
3875 if (identifier_p (id
))
3877 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3878 /*complain=*/tf_none
);
3879 if (result
== error_mark_node
)
3880 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3883 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3886 /* This is a wrapper around the
3887 make_{pointer,ptrmem,reference}_declarator functions that decides
3888 which one to call based on the CODE and CLASS_TYPE arguments. The
3889 CODE argument should be one of the values returned by
3890 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3891 appertain to the pointer or reference. */
3893 static cp_declarator
*
3894 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3895 cp_cv_quals cv_qualifiers
,
3896 cp_declarator
*target
,
3899 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3900 return cp_error_declarator
;
3902 if (code
== INDIRECT_REF
)
3903 if (class_type
== NULL_TREE
)
3904 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3906 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3907 target
, attributes
);
3908 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3909 return make_reference_declarator (cv_qualifiers
, target
,
3911 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3912 return make_reference_declarator (cv_qualifiers
, target
,
3917 /* Create a new C++ parser. */
3920 cp_parser_new (void)
3926 /* cp_lexer_new_main is called before doing GC allocation because
3927 cp_lexer_new_main might load a PCH file. */
3928 lexer
= cp_lexer_new_main ();
3930 /* Initialize the binops_by_token so that we can get the tree
3931 directly from the token. */
3932 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3933 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3935 parser
= ggc_cleared_alloc
<cp_parser
> ();
3936 parser
->lexer
= lexer
;
3937 parser
->context
= cp_parser_context_new (NULL
);
3939 /* For now, we always accept GNU extensions. */
3940 parser
->allow_gnu_extensions_p
= 1;
3942 /* The `>' token is a greater-than operator, not the end of a
3944 parser
->greater_than_is_operator_p
= true;
3946 parser
->default_arg_ok_p
= true;
3948 /* We are not parsing a constant-expression. */
3949 parser
->integral_constant_expression_p
= false;
3950 parser
->allow_non_integral_constant_expression_p
= false;
3951 parser
->non_integral_constant_expression_p
= false;
3953 /* Local variable names are not forbidden. */
3954 parser
->local_variables_forbidden_p
= false;
3956 /* We are not processing an `extern "C"' declaration. */
3957 parser
->in_unbraced_linkage_specification_p
= false;
3959 /* We are not processing a declarator. */
3960 parser
->in_declarator_p
= false;
3962 /* We are not processing a template-argument-list. */
3963 parser
->in_template_argument_list_p
= false;
3965 /* We are not in an iteration statement. */
3966 parser
->in_statement
= 0;
3968 /* We are not in a switch statement. */
3969 parser
->in_switch_statement_p
= false;
3971 /* We are not parsing a type-id inside an expression. */
3972 parser
->in_type_id_in_expr_p
= false;
3974 /* String literals should be translated to the execution character set. */
3975 parser
->translate_strings_p
= true;
3977 /* We are not parsing a function body. */
3978 parser
->in_function_body
= false;
3980 /* We can correct until told otherwise. */
3981 parser
->colon_corrects_to_scope_p
= true;
3983 /* The unparsed function queue is empty. */
3984 push_unparsed_function_queues (parser
);
3986 /* There are no classes being defined. */
3987 parser
->num_classes_being_defined
= 0;
3989 /* No template parameters apply. */
3990 parser
->num_template_parameter_lists
= 0;
3992 /* Special parsing data structures. */
3993 parser
->omp_declare_simd
= NULL
;
3994 parser
->oacc_routine
= NULL
;
3996 /* Not declaring an implicit function template. */
3997 parser
->auto_is_implicit_function_template_parm_p
= false;
3998 parser
->fully_implicit_function_template_p
= false;
3999 parser
->implicit_template_parms
= 0;
4000 parser
->implicit_template_scope
= 0;
4002 /* Allow constrained-type-specifiers. */
4003 parser
->prevent_constrained_type_specifiers
= 0;
4005 /* We haven't yet seen an 'extern "C"'. */
4006 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4011 /* Create a cp_lexer structure which will emit the tokens in CACHE
4012 and push it onto the parser's lexer stack. This is used for delayed
4013 parsing of in-class method bodies and default arguments, and should
4014 not be confused with tentative parsing. */
4016 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4018 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4019 lexer
->next
= parser
->lexer
;
4020 parser
->lexer
= lexer
;
4022 /* Move the current source position to that of the first token in the
4024 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4027 /* Pop the top lexer off the parser stack. This is never used for the
4028 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4030 cp_parser_pop_lexer (cp_parser
*parser
)
4032 cp_lexer
*lexer
= parser
->lexer
;
4033 parser
->lexer
= lexer
->next
;
4034 cp_lexer_destroy (lexer
);
4036 /* Put the current source position back where it was before this
4037 lexer was pushed. */
4038 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4041 /* Lexical conventions [gram.lex] */
4043 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4047 cp_parser_identifier (cp_parser
* parser
)
4051 /* Look for the identifier. */
4052 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4053 /* Return the value. */
4055 return cp_expr (token
->u
.value
, token
->location
);
4057 return error_mark_node
;
4060 /* Parse a sequence of adjacent string constants. Returns a
4061 TREE_STRING representing the combined, nul-terminated string
4062 constant. If TRANSLATE is true, translate the string to the
4063 execution character set. If WIDE_OK is true, a wide string is
4066 C++98 [lex.string] says that if a narrow string literal token is
4067 adjacent to a wide string literal token, the behavior is undefined.
4068 However, C99 6.4.5p4 says that this results in a wide string literal.
4069 We follow C99 here, for consistency with the C front end.
4071 This code is largely lifted from lex_string() in c-lex.c.
4073 FUTURE: ObjC++ will need to handle @-strings here. */
4075 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4076 bool lookup_udlit
= true)
4080 struct obstack str_ob
;
4081 struct obstack loc_ob
;
4082 cpp_string str
, istr
, *strs
;
4084 enum cpp_ttype type
, curr_type
;
4085 int have_suffix_p
= 0;
4087 tree suffix_id
= NULL_TREE
;
4088 bool curr_tok_is_userdef_p
= false;
4090 tok
= cp_lexer_peek_token (parser
->lexer
);
4091 if (!cp_parser_is_string_literal (tok
))
4093 cp_parser_error (parser
, "expected string-literal");
4094 return error_mark_node
;
4097 location_t loc
= tok
->location
;
4099 if (cpp_userdef_string_p (tok
->type
))
4101 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4102 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4103 curr_tok_is_userdef_p
= true;
4107 string_tree
= tok
->u
.value
;
4108 curr_type
= tok
->type
;
4112 /* Try to avoid the overhead of creating and destroying an obstack
4113 for the common case of just one string. */
4114 if (!cp_parser_is_string_literal
4115 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4117 cp_lexer_consume_token (parser
->lexer
);
4119 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4120 str
.len
= TREE_STRING_LENGTH (string_tree
);
4123 if (curr_tok_is_userdef_p
)
4125 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4127 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4130 curr_type
= tok
->type
;
4136 location_t last_tok_loc
= tok
->location
;
4137 gcc_obstack_init (&str_ob
);
4138 gcc_obstack_init (&loc_ob
);
4143 cp_lexer_consume_token (parser
->lexer
);
4145 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4146 str
.len
= TREE_STRING_LENGTH (string_tree
);
4148 if (curr_tok_is_userdef_p
)
4150 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4151 if (have_suffix_p
== 0)
4153 suffix_id
= curr_suffix_id
;
4156 else if (have_suffix_p
== 1
4157 && curr_suffix_id
!= suffix_id
)
4159 error ("inconsistent user-defined literal suffixes"
4160 " %qD and %qD in string literal",
4161 suffix_id
, curr_suffix_id
);
4164 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4167 curr_type
= tok
->type
;
4169 if (type
!= curr_type
)
4171 if (type
== CPP_STRING
)
4173 else if (curr_type
!= CPP_STRING
)
4175 rich_location
rich_loc (line_table
, tok
->location
);
4176 rich_loc
.add_range (last_tok_loc
);
4177 error_at (&rich_loc
,
4178 "unsupported non-standard concatenation "
4179 "of string literals");
4183 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4184 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4186 last_tok_loc
= tok
->location
;
4188 tok
= cp_lexer_peek_token (parser
->lexer
);
4189 if (cpp_userdef_string_p (tok
->type
))
4191 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4192 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4193 curr_tok_is_userdef_p
= true;
4197 string_tree
= tok
->u
.value
;
4198 curr_type
= tok
->type
;
4199 curr_tok_is_userdef_p
= false;
4202 while (cp_parser_is_string_literal (tok
));
4204 /* A string literal built by concatenation has its caret=start at
4205 the start of the initial string, and its finish at the finish of
4206 the final string literal. */
4207 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4209 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4212 if (type
!= CPP_STRING
&& !wide_ok
)
4214 cp_parser_error (parser
, "a wide string is invalid in this context");
4218 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4219 (parse_in
, strs
, count
, &istr
, type
))
4221 value
= build_string (istr
.len
, (const char *)istr
.text
);
4222 free (CONST_CAST (unsigned char *, istr
.text
));
4225 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4226 gcc_assert (g_string_concat_db
);
4227 g_string_concat_db
->record_string_concatenation (count
, locs
);
4234 case CPP_UTF8STRING
:
4235 TREE_TYPE (value
) = char_array_type_node
;
4238 TREE_TYPE (value
) = char16_array_type_node
;
4241 TREE_TYPE (value
) = char32_array_type_node
;
4244 TREE_TYPE (value
) = wchar_array_type_node
;
4248 value
= fix_string_type (value
);
4252 tree literal
= build_userdef_literal (suffix_id
, value
,
4253 OT_NONE
, NULL_TREE
);
4255 value
= cp_parser_userdef_string_literal (literal
);
4261 /* cpp_interpret_string has issued an error. */
4262 value
= error_mark_node
;
4266 obstack_free (&str_ob
, 0);
4267 obstack_free (&loc_ob
, 0);
4270 return cp_expr (value
, loc
);
4273 /* Look up a literal operator with the name and the exact arguments. */
4276 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4278 tree decl
= lookup_name (name
);
4279 if (!decl
|| !is_overloaded_fn (decl
))
4280 return error_mark_node
;
4282 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4286 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4292 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4293 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4295 tree tparm
= TREE_VALUE (parmtypes
);
4296 tree targ
= TREE_TYPE ((*args
)[ix
]);
4297 bool ptr
= TYPE_PTR_P (tparm
);
4298 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4299 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4301 || !same_type_p (TREE_TYPE (tparm
),
4307 && ix
== vec_safe_length (args
)
4308 /* May be this should be sufficient_parms_p instead,
4309 depending on how exactly should user-defined literals
4310 work in presence of default arguments on the literal
4311 operator parameters. */
4312 && parmtypes
== void_list_node
)
4317 return error_mark_node
;
4320 /* Parse a user-defined char constant. Returns a call to a user-defined
4321 literal operator taking the character as an argument. */
4324 cp_parser_userdef_char_literal (cp_parser
*parser
)
4326 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4327 tree literal
= token
->u
.value
;
4328 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4329 tree value
= USERDEF_LITERAL_VALUE (literal
);
4330 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4333 /* Build up a call to the user-defined operator */
4334 /* Lookup the name we got back from the id-expression. */
4335 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4336 vec_safe_push (args
, value
);
4337 decl
= lookup_literal_operator (name
, args
);
4338 if (!decl
|| decl
== error_mark_node
)
4340 error ("unable to find character literal operator %qD with %qT argument",
4341 name
, TREE_TYPE (value
));
4342 release_tree_vector (args
);
4343 return error_mark_node
;
4345 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4346 release_tree_vector (args
);
4350 /* A subroutine of cp_parser_userdef_numeric_literal to
4351 create a char... template parameter pack from a string node. */
4354 make_char_string_pack (tree value
)
4357 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4358 const char *str
= TREE_STRING_POINTER (value
);
4359 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4360 tree argvec
= make_tree_vec (1);
4362 /* Fill in CHARVEC with all of the parameters. */
4363 charvec
= make_tree_vec (len
);
4364 for (i
= 0; i
< len
; ++i
)
4366 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4367 cpp_string in
= { 3, s
};
4368 cpp_string out
= { 0, 0 };
4369 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4371 gcc_assert (out
.len
== 2);
4372 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4376 /* Build the argument packs. */
4377 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4379 TREE_VEC_ELT (argvec
, 0) = argpack
;
4384 /* A subroutine of cp_parser_userdef_numeric_literal to
4385 create a char... template parameter pack from a string node. */
4388 make_string_pack (tree value
)
4391 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4392 const unsigned char *str
4393 = (const unsigned char *) TREE_STRING_POINTER (value
);
4394 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4395 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4396 tree argvec
= make_tree_vec (2);
4398 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4399 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4401 /* First template parm is character type. */
4402 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4404 /* Fill in CHARVEC with all of the parameters. */
4405 charvec
= make_tree_vec (len
);
4406 for (int i
= 0; i
< len
; ++i
)
4407 TREE_VEC_ELT (charvec
, i
)
4408 = double_int_to_tree (str_char_type_node
,
4409 double_int::from_buffer (str
+ i
* sz
, sz
));
4411 /* Build the argument packs. */
4412 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4414 TREE_VEC_ELT (argvec
, 1) = argpack
;
4419 /* Parse a user-defined numeric constant. returns a call to a user-defined
4420 literal operator. */
4423 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4425 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4426 tree literal
= token
->u
.value
;
4427 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4428 tree value
= USERDEF_LITERAL_VALUE (literal
);
4429 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4430 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4431 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4433 vec
<tree
, va_gc
> *args
;
4435 /* Look for a literal operator taking the exact type of numeric argument
4436 as the literal value. */
4437 args
= make_tree_vector ();
4438 vec_safe_push (args
, value
);
4439 decl
= lookup_literal_operator (name
, args
);
4440 if (decl
&& decl
!= error_mark_node
)
4442 result
= finish_call_expr (decl
, &args
, false, true,
4443 tf_warning_or_error
);
4445 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4447 warning_at (token
->location
, OPT_Woverflow
,
4448 "integer literal exceeds range of %qT type",
4449 long_long_unsigned_type_node
);
4454 warning_at (token
->location
, OPT_Woverflow
,
4455 "floating literal exceeds range of %qT type",
4456 long_double_type_node
);
4457 else if (overflow
< 0)
4458 warning_at (token
->location
, OPT_Woverflow
,
4459 "floating literal truncated to zero");
4462 release_tree_vector (args
);
4465 release_tree_vector (args
);
4467 /* If the numeric argument didn't work, look for a raw literal
4468 operator taking a const char* argument consisting of the number
4469 in string format. */
4470 args
= make_tree_vector ();
4471 vec_safe_push (args
, num_string
);
4472 decl
= lookup_literal_operator (name
, args
);
4473 if (decl
&& decl
!= error_mark_node
)
4475 result
= finish_call_expr (decl
, &args
, false, true,
4476 tf_warning_or_error
);
4477 release_tree_vector (args
);
4480 release_tree_vector (args
);
4482 /* If the raw literal didn't work, look for a non-type template
4483 function with parameter pack char.... Call the function with
4484 template parameter characters representing the number. */
4485 args
= make_tree_vector ();
4486 decl
= lookup_literal_operator (name
, args
);
4487 if (decl
&& decl
!= error_mark_node
)
4489 tree tmpl_args
= make_char_string_pack (num_string
);
4490 if (tmpl_args
== NULL_TREE
)
4492 error ("failed to translate literal to execution character set %qT",
4494 return error_mark_node
;
4496 decl
= lookup_template_function (decl
, tmpl_args
);
4497 result
= finish_call_expr (decl
, &args
, false, true,
4498 tf_warning_or_error
);
4499 release_tree_vector (args
);
4503 release_tree_vector (args
);
4505 /* In C++14 the standard library defines complex number suffixes that
4506 conflict with GNU extensions. Prefer them if <complex> is #included. */
4507 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4508 bool i14
= (cxx_dialect
> cxx11
4509 && (id_equal (suffix_id
, "i")
4510 || id_equal (suffix_id
, "if")
4511 || id_equal (suffix_id
, "il")));
4512 diagnostic_t kind
= DK_ERROR
;
4517 tree cxlit
= lookup_qualified_name (std_node
,
4518 get_identifier ("complex_literals"),
4520 if (cxlit
== error_mark_node
)
4522 /* No <complex>, so pedwarn and use GNU semantics. */
4524 opt
= OPT_Wpedantic
;
4529 = emit_diagnostic (kind
, input_location
, opt
,
4530 "unable to find numeric literal operator %qD", name
);
4533 /* Don't inform either. */;
4536 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4537 "(from <complex>) to enable the C++14 user-defined literal "
4540 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4541 "GNU built-in suffix");
4544 inform (token
->location
, "use -fext-numeric-literals "
4545 "to enable more built-in suffixes");
4547 if (kind
== DK_ERROR
)
4548 value
= error_mark_node
;
4551 /* Use the built-in semantics. */
4553 if (id_equal (suffix_id
, "i"))
4555 if (TREE_CODE (value
) == INTEGER_CST
)
4556 type
= integer_type_node
;
4558 type
= double_type_node
;
4560 else if (id_equal (suffix_id
, "if"))
4561 type
= float_type_node
;
4562 else /* if (id_equal (suffix_id, "il")) */
4563 type
= long_double_type_node
;
4565 value
= build_complex (build_complex_type (type
),
4566 fold_convert (type
, integer_zero_node
),
4567 fold_convert (type
, value
));
4570 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4571 /* Avoid repeated diagnostics. */
4572 token
->u
.value
= value
;
4576 /* Parse a user-defined string constant. Returns a call to a user-defined
4577 literal operator taking a character pointer and the length of the string
4581 cp_parser_userdef_string_literal (tree literal
)
4583 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4584 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4585 tree value
= USERDEF_LITERAL_VALUE (literal
);
4586 int len
= TREE_STRING_LENGTH (value
)
4587 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4590 /* Build up a call to the user-defined operator. */
4591 /* Lookup the name we got back from the id-expression. */
4592 releasing_vec rargs
;
4593 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4594 vec_safe_push (args
, value
);
4595 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4596 decl
= lookup_literal_operator (name
, args
);
4598 if (decl
&& decl
!= error_mark_node
)
4599 return finish_call_expr (decl
, &args
, false, true,
4600 tf_warning_or_error
);
4602 /* Look for a suitable template function, either (C++20) with a single
4603 parameter of class type, or (N3599) with typename parameter CharT and
4604 parameter pack CharT... */
4606 decl
= lookup_literal_operator (name
, args
);
4607 if (decl
&& decl
!= error_mark_node
)
4609 /* Use resolve_nondeduced_context to try to choose one form of template
4611 tree tmpl_args
= make_tree_vec (1);
4612 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4613 decl
= lookup_template_function (decl
, tmpl_args
);
4614 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4619 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4620 res
= resolve_nondeduced_context (decl
, tf_none
);
4624 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4625 TREE_OPERAND (decl
, 1) = tmpl_args
;
4626 return finish_call_expr (decl
, &args
, false, true,
4627 tf_warning_or_error
);
4630 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4631 name
, TREE_TYPE (value
), size_type_node
);
4632 return error_mark_node
;
4636 /* Basic concepts [gram.basic] */
4638 /* Parse a translation-unit.
4641 declaration-seq [opt] */
4644 cp_parser_translation_unit (cp_parser
* parser
)
4646 gcc_checking_assert (!cp_error_declarator
);
4648 /* Create the declarator obstack. */
4649 gcc_obstack_init (&declarator_obstack
);
4650 /* Create the error declarator. */
4651 cp_error_declarator
= make_declarator (cdk_error
);
4652 /* Create the empty parameter list. */
4653 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4655 /* Remember where the base of the declarator obstack lies. */
4656 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4658 bool implicit_extern_c
= false;
4662 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4664 /* If we're entering or exiting a region that's implicitly
4665 extern "C", modify the lang context appropriately. */
4666 if (implicit_extern_c
4667 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4669 implicit_extern_c
= !implicit_extern_c
;
4670 if (implicit_extern_c
)
4671 push_lang_context (lang_name_c
);
4673 pop_lang_context ();
4676 if (token
->type
== CPP_EOF
)
4679 if (token
->type
== CPP_CLOSE_BRACE
)
4681 cp_parser_error (parser
, "expected declaration");
4682 cp_lexer_consume_token (parser
->lexer
);
4683 /* If the next token is now a `;', consume it. */
4684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4685 cp_lexer_consume_token (parser
->lexer
);
4688 cp_parser_toplevel_declaration (parser
);
4691 /* Get rid of the token array; we don't need it any more. */
4692 cp_lexer_destroy (parser
->lexer
);
4693 parser
->lexer
= NULL
;
4695 /* The EOF should have reset this. */
4696 gcc_checking_assert (!implicit_extern_c
);
4698 /* Make sure the declarator obstack was fully cleaned up. */
4699 gcc_assert (obstack_next_free (&declarator_obstack
)
4700 == declarator_obstack_base
);
4703 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4704 decltype context. */
4706 static inline tsubst_flags_t
4707 complain_flags (bool decltype_p
)
4709 tsubst_flags_t complain
= tf_warning_or_error
;
4711 complain
|= tf_decltype
;
4715 /* We're about to parse a collection of statements. If we're currently
4716 parsing tentatively, set up a firewall so that any nested
4717 cp_parser_commit_to_tentative_parse won't affect the current context. */
4719 static cp_token_position
4720 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4722 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4725 cp_parser_parse_tentatively (parser
);
4726 cp_parser_commit_to_topmost_tentative_parse (parser
);
4727 return cp_lexer_token_position (parser
->lexer
, false);
4730 /* We've finished parsing the collection of statements. Wrap up the
4731 firewall and replace the relevant tokens with the parsed form. */
4734 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4740 /* Finish the firewall level. */
4741 cp_parser_parse_definitely (parser
);
4742 /* And remember the result of the parse for when we try again. */
4743 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4744 token
->type
= CPP_PREPARSED_EXPR
;
4745 token
->u
.value
= expr
;
4746 token
->keyword
= RID_MAX
;
4747 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4750 /* Like the above functions, but let the user modify the tokens. Used by
4751 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4752 later parses, so it makes sense to localize the effects of
4753 cp_parser_commit_to_tentative_parse. */
4755 struct tentative_firewall
4760 tentative_firewall (cp_parser
*p
): parser(p
)
4762 /* If we're currently parsing tentatively, start a committed level as a
4763 firewall and then an inner tentative parse. */
4764 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4766 cp_parser_parse_tentatively (parser
);
4767 cp_parser_commit_to_topmost_tentative_parse (parser
);
4768 cp_parser_parse_tentatively (parser
);
4772 ~tentative_firewall()
4776 /* Finish the inner tentative parse and the firewall, propagating any
4777 uncommitted error state to the outer tentative parse. */
4778 bool err
= cp_parser_error_occurred (parser
);
4779 cp_parser_parse_definitely (parser
);
4780 cp_parser_parse_definitely (parser
);
4782 cp_parser_simulate_error (parser
);
4787 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4788 This class is for tracking such a matching pair of symbols.
4789 In particular, it tracks the location of the first token,
4790 so that if the second token is missing, we can highlight the
4791 location of the first token when notifying the user about the
4794 template <typename traits_t
>
4798 /* token_pair's ctor. */
4799 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4801 /* If the next token is the opening symbol for this pair, consume it and
4803 Otherwise, issue an error and return false.
4804 In either case, record the location of the opening token. */
4806 bool require_open (cp_parser
*parser
)
4808 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4809 return cp_parser_require (parser
, traits_t::open_token_type
,
4810 traits_t::required_token_open
);
4813 /* Consume the next token from PARSER, recording its location as
4814 that of the opening token within the pair. */
4816 cp_token
* consume_open (cp_parser
*parser
)
4818 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4819 gcc_assert (tok
->type
== traits_t::open_token_type
);
4820 m_open_loc
= tok
->location
;
4824 /* If the next token is the closing symbol for this pair, consume it
4826 Otherwise, issue an error, highlighting the location of the
4827 corresponding opening token, and return NULL. */
4829 cp_token
*require_close (cp_parser
*parser
) const
4831 return cp_parser_require (parser
, traits_t::close_token_type
,
4832 traits_t::required_token_close
,
4837 location_t m_open_loc
;
4840 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4842 struct matching_paren_traits
4844 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4845 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4846 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4847 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4850 /* "matching_parens" is a token_pair<T> class for tracking matching
4851 pairs of parentheses. */
4853 typedef token_pair
<matching_paren_traits
> matching_parens
;
4855 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4857 struct matching_brace_traits
4859 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4860 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4861 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4862 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4865 /* "matching_braces" is a token_pair<T> class for tracking matching
4868 typedef token_pair
<matching_brace_traits
> matching_braces
;
4871 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4872 enclosing parentheses. */
4875 cp_parser_statement_expr (cp_parser
*parser
)
4877 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4879 /* Consume the '('. */
4880 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4881 matching_parens parens
;
4882 parens
.consume_open (parser
);
4883 /* Start the statement-expression. */
4884 tree expr
= begin_stmt_expr ();
4885 /* Parse the compound-statement. */
4886 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4888 expr
= finish_stmt_expr (expr
, false);
4889 /* Consume the ')'. */
4890 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4891 if (!parens
.require_close (parser
))
4892 cp_parser_skip_to_end_of_statement (parser
);
4894 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4895 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4896 return cp_expr (expr
, combined_loc
);
4899 /* Expressions [gram.expr] */
4901 /* Parse a fold-operator.
4904 - * / % ^ & | = < > << >>
4905 = -= *= /= %= ^= &= |= <<= >>=
4906 == != <= >= && || , .* ->*
4908 This returns the tree code corresponding to the matched operator
4909 as an int. When the current token matches a compound assignment
4910 opertor, the resulting tree code is the negative value of the
4911 non-assignment operator. */
4914 cp_parser_fold_operator (cp_token
*token
)
4916 switch (token
->type
)
4918 case CPP_PLUS
: return PLUS_EXPR
;
4919 case CPP_MINUS
: return MINUS_EXPR
;
4920 case CPP_MULT
: return MULT_EXPR
;
4921 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4922 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4923 case CPP_XOR
: return BIT_XOR_EXPR
;
4924 case CPP_AND
: return BIT_AND_EXPR
;
4925 case CPP_OR
: return BIT_IOR_EXPR
;
4926 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4927 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4929 case CPP_EQ
: return -NOP_EXPR
;
4930 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4931 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4932 case CPP_MULT_EQ
: return -MULT_EXPR
;
4933 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4934 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4935 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4936 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4937 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4938 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4939 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4941 case CPP_EQ_EQ
: return EQ_EXPR
;
4942 case CPP_NOT_EQ
: return NE_EXPR
;
4943 case CPP_LESS
: return LT_EXPR
;
4944 case CPP_GREATER
: return GT_EXPR
;
4945 case CPP_LESS_EQ
: return LE_EXPR
;
4946 case CPP_GREATER_EQ
: return GE_EXPR
;
4948 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4949 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4951 case CPP_COMMA
: return COMPOUND_EXPR
;
4953 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4954 case CPP_DEREF_STAR
: return MEMBER_REF
;
4956 default: return ERROR_MARK
;
4960 /* Returns true if CODE indicates a binary expression, which is not allowed in
4961 the LHS of a fold-expression. More codes will need to be added to use this
4962 function in other contexts. */
4965 is_binary_op (tree_code code
)
4970 case POINTER_PLUS_EXPR
:
4973 case TRUNC_DIV_EXPR
:
4974 case TRUNC_MOD_EXPR
:
4990 case TRUTH_ANDIF_EXPR
:
4991 case TRUTH_ORIF_EXPR
:
5004 /* If the next token is a suitable fold operator, consume it and return as
5005 the function above. */
5008 cp_parser_fold_operator (cp_parser
*parser
)
5010 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5011 int code
= cp_parser_fold_operator (token
);
5012 if (code
!= ERROR_MARK
)
5013 cp_lexer_consume_token (parser
->lexer
);
5017 /* Parse a fold-expression.
5020 ( ... folding-operator cast-expression)
5021 ( cast-expression folding-operator ... )
5022 ( cast-expression folding operator ... folding-operator cast-expression)
5024 Note that the '(' and ')' are matched in primary expression. */
5027 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5034 cp_lexer_consume_token (parser
->lexer
);
5035 int op
= cp_parser_fold_operator (parser
);
5036 if (op
== ERROR_MARK
)
5038 cp_parser_error (parser
, "expected binary operator");
5039 return error_mark_node
;
5042 tree expr
= cp_parser_cast_expression (parser
, false, false,
5044 if (expr
== error_mark_node
)
5045 return error_mark_node
;
5046 return finish_left_unary_fold_expr (expr
, op
);
5049 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5050 int op
= cp_parser_fold_operator (parser
);
5051 if (op
== ERROR_MARK
)
5053 cp_parser_error (parser
, "expected binary operator");
5054 return error_mark_node
;
5057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5059 cp_parser_error (parser
, "expected ...");
5060 return error_mark_node
;
5062 cp_lexer_consume_token (parser
->lexer
);
5064 /* The operands of a fold-expression are cast-expressions, so binary or
5065 conditional expressions are not allowed. We check this here to avoid
5066 tentative parsing. */
5067 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5068 /* OK, the expression was parenthesized. */;
5069 else if (is_binary_op (TREE_CODE (expr1
)))
5070 error_at (location_of (expr1
),
5071 "binary expression in operand of fold-expression");
5072 else if (TREE_CODE (expr1
) == COND_EXPR
5073 || (REFERENCE_REF_P (expr1
)
5074 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5075 error_at (location_of (expr1
),
5076 "conditional expression in operand of fold-expression");
5079 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5080 return finish_right_unary_fold_expr (expr1
, op
);
5082 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5084 cp_parser_error (parser
, "mismatched operator in fold-expression");
5085 return error_mark_node
;
5087 cp_lexer_consume_token (parser
->lexer
);
5089 // Binary left or right fold.
5090 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5091 if (expr2
== error_mark_node
)
5092 return error_mark_node
;
5093 return finish_binary_fold_expr (expr1
, expr2
, op
);
5096 /* Parse a primary-expression.
5103 lambda-expression (C++11)
5108 ( compound-statement )
5109 __builtin_va_arg ( assignment-expression , type-id )
5110 __builtin_offsetof ( type-id , offsetof-expression )
5113 __has_nothrow_assign ( type-id )
5114 __has_nothrow_constructor ( type-id )
5115 __has_nothrow_copy ( type-id )
5116 __has_trivial_assign ( type-id )
5117 __has_trivial_constructor ( type-id )
5118 __has_trivial_copy ( type-id )
5119 __has_trivial_destructor ( type-id )
5120 __has_virtual_destructor ( type-id )
5121 __is_abstract ( type-id )
5122 __is_base_of ( type-id , type-id )
5123 __is_class ( type-id )
5124 __is_empty ( type-id )
5125 __is_enum ( type-id )
5126 __is_final ( type-id )
5127 __is_literal_type ( type-id )
5128 __is_pod ( type-id )
5129 __is_polymorphic ( type-id )
5130 __is_std_layout ( type-id )
5131 __is_trivial ( type-id )
5132 __is_union ( type-id )
5134 Objective-C++ Extension:
5142 ADDRESS_P is true iff this expression was immediately preceded by
5143 "&" and therefore might denote a pointer-to-member. CAST_P is true
5144 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5145 true iff this expression is a template argument.
5147 Returns a representation of the expression. Upon return, *IDK
5148 indicates what kind of id-expression (if any) was present. */
5151 cp_parser_primary_expression (cp_parser
*parser
,
5154 bool template_arg_p
,
5158 cp_token
*token
= NULL
;
5160 /* Assume the primary expression is not an id-expression. */
5161 *idk
= CP_ID_KIND_NONE
;
5163 /* Peek at the next token. */
5164 token
= cp_lexer_peek_token (parser
->lexer
);
5165 switch ((int) token
->type
)
5174 user-defined-literal */
5181 case CPP_PREPARSED_EXPR
:
5182 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5183 return cp_parser_userdef_numeric_literal (parser
);
5184 token
= cp_lexer_consume_token (parser
->lexer
);
5185 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5187 error_at (token
->location
,
5188 "fixed-point types not supported in C++");
5189 return error_mark_node
;
5191 /* Floating-point literals are only allowed in an integral
5192 constant expression if they are cast to an integral or
5193 enumeration type. */
5194 if (TREE_CODE (token
->u
.value
) == REAL_CST
5195 && parser
->integral_constant_expression_p
5198 /* CAST_P will be set even in invalid code like "int(2.7 +
5199 ...)". Therefore, we have to check that the next token
5200 is sure to end the cast. */
5203 cp_token
*next_token
;
5205 next_token
= cp_lexer_peek_token (parser
->lexer
);
5206 if (/* The comma at the end of an
5207 enumerator-definition. */
5208 next_token
->type
!= CPP_COMMA
5209 /* The curly brace at the end of an enum-specifier. */
5210 && next_token
->type
!= CPP_CLOSE_BRACE
5211 /* The end of a statement. */
5212 && next_token
->type
!= CPP_SEMICOLON
5213 /* The end of the cast-expression. */
5214 && next_token
->type
!= CPP_CLOSE_PAREN
5215 /* The end of an array bound. */
5216 && next_token
->type
!= CPP_CLOSE_SQUARE
5217 /* The closing ">" in a template-argument-list. */
5218 && (next_token
->type
!= CPP_GREATER
5219 || parser
->greater_than_is_operator_p
)
5220 /* C++0x only: A ">>" treated like two ">" tokens,
5221 in a template-argument-list. */
5222 && (next_token
->type
!= CPP_RSHIFT
5223 || (cxx_dialect
== cxx98
)
5224 || parser
->greater_than_is_operator_p
))
5228 /* If we are within a cast, then the constraint that the
5229 cast is to an integral or enumeration type will be
5230 checked at that point. If we are not within a cast, then
5231 this code is invalid. */
5233 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5235 return (cp_expr (token
->u
.value
, token
->location
)
5236 .maybe_add_location_wrapper ());
5238 case CPP_CHAR_USERDEF
:
5239 case CPP_CHAR16_USERDEF
:
5240 case CPP_CHAR32_USERDEF
:
5241 case CPP_WCHAR_USERDEF
:
5242 case CPP_UTF8CHAR_USERDEF
:
5243 return cp_parser_userdef_char_literal (parser
);
5249 case CPP_UTF8STRING
:
5250 case CPP_STRING_USERDEF
:
5251 case CPP_STRING16_USERDEF
:
5252 case CPP_STRING32_USERDEF
:
5253 case CPP_WSTRING_USERDEF
:
5254 case CPP_UTF8STRING_USERDEF
:
5255 /* ??? Should wide strings be allowed when parser->translate_strings_p
5256 is false (i.e. in attributes)? If not, we can kill the third
5257 argument to cp_parser_string_literal. */
5258 return (cp_parser_string_literal (parser
,
5259 parser
->translate_strings_p
,
5261 .maybe_add_location_wrapper ());
5263 case CPP_OPEN_PAREN
:
5264 /* If we see `( { ' then we are looking at the beginning of
5265 a GNU statement-expression. */
5266 if (cp_parser_allow_gnu_extensions_p (parser
)
5267 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5269 /* Statement-expressions are not allowed by the standard. */
5270 pedwarn (token
->location
, OPT_Wpedantic
,
5271 "ISO C++ forbids braced-groups within expressions");
5273 /* And they're not allowed outside of a function-body; you
5274 cannot, for example, write:
5276 int i = ({ int j = 3; j + 1; });
5278 at class or namespace scope. */
5279 if (!parser
->in_function_body
5280 || parser
->in_template_argument_list_p
)
5282 error_at (token
->location
,
5283 "statement-expressions are not allowed outside "
5284 "functions nor in template-argument lists");
5285 cp_parser_skip_to_end_of_block_or_statement (parser
);
5286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5287 cp_lexer_consume_token (parser
->lexer
);
5288 return error_mark_node
;
5291 return cp_parser_statement_expr (parser
);
5293 /* Otherwise it's a normal parenthesized expression. */
5296 bool saved_greater_than_is_operator_p
;
5298 location_t open_paren_loc
= token
->location
;
5300 /* Consume the `('. */
5301 matching_parens parens
;
5302 parens
.consume_open (parser
);
5303 /* Within a parenthesized expression, a `>' token is always
5304 the greater-than operator. */
5305 saved_greater_than_is_operator_p
5306 = parser
->greater_than_is_operator_p
;
5307 parser
->greater_than_is_operator_p
= true;
5309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5310 /* Left fold expression. */
5313 /* Parse the parenthesized expression. */
5314 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5316 token
= cp_lexer_peek_token (parser
->lexer
);
5317 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5319 expr
= cp_parser_fold_expression (parser
, expr
);
5320 if (expr
!= error_mark_node
5321 && cxx_dialect
< cxx17
5322 && !in_system_header_at (input_location
))
5323 pedwarn (input_location
, 0, "fold-expressions only available "
5324 "with -std=c++17 or -std=gnu++17");
5327 /* Let the front end know that this expression was
5328 enclosed in parentheses. This matters in case, for
5329 example, the expression is of the form `A::B', since
5330 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5332 expr
= finish_parenthesized_expr (expr
);
5334 /* DR 705: Wrapping an unqualified name in parentheses
5335 suppresses arg-dependent lookup. We want to pass back
5336 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5337 (c++/37862), but none of the others. */
5338 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5339 *idk
= CP_ID_KIND_NONE
;
5341 /* The `>' token might be the end of a template-id or
5342 template-parameter-list now. */
5343 parser
->greater_than_is_operator_p
5344 = saved_greater_than_is_operator_p
;
5346 /* Consume the `)'. */
5347 token
= cp_lexer_peek_token (parser
->lexer
);
5348 location_t close_paren_loc
= token
->location
;
5349 expr
.set_range (open_paren_loc
, close_paren_loc
);
5350 if (!parens
.require_close (parser
)
5351 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5352 cp_parser_skip_to_end_of_statement (parser
);
5357 case CPP_OPEN_SQUARE
:
5359 if (c_dialect_objc ())
5361 /* We might have an Objective-C++ message. */
5362 cp_parser_parse_tentatively (parser
);
5363 tree msg
= cp_parser_objc_message_expression (parser
);
5364 /* If that works out, we're done ... */
5365 if (cp_parser_parse_definitely (parser
))
5367 /* ... else, fall though to see if it's a lambda. */
5369 cp_expr lam
= cp_parser_lambda_expression (parser
);
5370 /* Don't warn about a failed tentative parse. */
5371 if (cp_parser_error_occurred (parser
))
5372 return error_mark_node
;
5373 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5377 case CPP_OBJC_STRING
:
5378 if (c_dialect_objc ())
5379 /* We have an Objective-C++ string literal. */
5380 return cp_parser_objc_expression (parser
);
5381 cp_parser_error (parser
, "expected primary-expression");
5382 return error_mark_node
;
5385 switch (token
->keyword
)
5387 /* These two are the boolean literals. */
5389 cp_lexer_consume_token (parser
->lexer
);
5390 return cp_expr (boolean_true_node
, token
->location
);
5392 cp_lexer_consume_token (parser
->lexer
);
5393 return cp_expr (boolean_false_node
, token
->location
);
5395 /* The `__null' literal. */
5397 cp_lexer_consume_token (parser
->lexer
);
5398 return cp_expr (null_node
, token
->location
);
5400 /* The `nullptr' literal. */
5402 cp_lexer_consume_token (parser
->lexer
);
5403 return cp_expr (nullptr_node
, token
->location
);
5405 /* Recognize the `this' keyword. */
5407 cp_lexer_consume_token (parser
->lexer
);
5408 if (parser
->local_variables_forbidden_p
)
5410 error_at (token
->location
,
5411 "%<this%> may not be used in this context");
5412 return error_mark_node
;
5414 /* Pointers cannot appear in constant-expressions. */
5415 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5416 return error_mark_node
;
5417 return cp_expr (finish_this_expr (), token
->location
);
5419 /* The `operator' keyword can be the beginning of an
5424 case RID_FUNCTION_NAME
:
5425 case RID_PRETTY_FUNCTION_NAME
:
5426 case RID_C99_FUNCTION_NAME
:
5428 non_integral_constant name
;
5430 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5431 __func__ are the names of variables -- but they are
5432 treated specially. Therefore, they are handled here,
5433 rather than relying on the generic id-expression logic
5434 below. Grammatically, these names are id-expressions.
5436 Consume the token. */
5437 token
= cp_lexer_consume_token (parser
->lexer
);
5439 switch (token
->keyword
)
5441 case RID_FUNCTION_NAME
:
5442 name
= NIC_FUNC_NAME
;
5444 case RID_PRETTY_FUNCTION_NAME
:
5445 name
= NIC_PRETTY_FUNC
;
5447 case RID_C99_FUNCTION_NAME
:
5448 name
= NIC_C99_FUNC
;
5454 if (cp_parser_non_integral_constant_expression (parser
, name
))
5455 return error_mark_node
;
5457 /* Look up the name. */
5458 return finish_fname (token
->u
.value
);
5465 location_t type_location
;
5466 location_t start_loc
5467 = cp_lexer_peek_token (parser
->lexer
)->location
;
5468 /* The `__builtin_va_arg' construct is used to handle
5469 `va_arg'. Consume the `__builtin_va_arg' token. */
5470 cp_lexer_consume_token (parser
->lexer
);
5471 /* Look for the opening `('. */
5472 matching_parens parens
;
5473 parens
.require_open (parser
);
5474 /* Now, parse the assignment-expression. */
5475 expression
= cp_parser_assignment_expression (parser
);
5476 /* Look for the `,'. */
5477 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5478 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5479 /* Parse the type-id. */
5481 type_id_in_expr_sentinel
s (parser
);
5482 type
= cp_parser_type_id (parser
);
5484 /* Look for the closing `)'. */
5485 location_t finish_loc
5486 = cp_lexer_peek_token (parser
->lexer
)->location
;
5487 parens
.require_close (parser
);
5488 /* Using `va_arg' in a constant-expression is not
5490 if (cp_parser_non_integral_constant_expression (parser
,
5492 return error_mark_node
;
5493 /* Construct a location of the form:
5494 __builtin_va_arg (v, int)
5495 ~~~~~~~~~~~~~~~~~~~~~^~~~
5496 with the caret at the type, ranging from the start of the
5497 "__builtin_va_arg" token to the close paren. */
5498 location_t combined_loc
5499 = make_location (type_location
, start_loc
, finish_loc
);
5500 return build_x_va_arg (combined_loc
, expression
, type
);
5504 return cp_parser_builtin_offsetof (parser
);
5506 case RID_HAS_NOTHROW_ASSIGN
:
5507 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5508 case RID_HAS_NOTHROW_COPY
:
5509 case RID_HAS_TRIVIAL_ASSIGN
:
5510 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5511 case RID_HAS_TRIVIAL_COPY
:
5512 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5513 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5514 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5515 case RID_IS_ABSTRACT
:
5516 case RID_IS_AGGREGATE
:
5517 case RID_IS_BASE_OF
:
5522 case RID_IS_LITERAL_TYPE
:
5524 case RID_IS_POLYMORPHIC
:
5525 case RID_IS_SAME_AS
:
5526 case RID_IS_STD_LAYOUT
:
5527 case RID_IS_TRIVIAL
:
5528 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5529 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5530 case RID_IS_TRIVIALLY_COPYABLE
:
5532 case RID_IS_ASSIGNABLE
:
5533 case RID_IS_CONSTRUCTIBLE
:
5534 return cp_parser_trait_expr (parser
, token
->keyword
);
5538 return cp_parser_requires_expression (parser
);
5540 /* Objective-C++ expressions. */
5542 case RID_AT_PROTOCOL
:
5543 case RID_AT_SELECTOR
:
5544 return cp_parser_objc_expression (parser
);
5547 if (parser
->in_function_body
5548 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5551 error_at (token
->location
,
5552 "a template declaration cannot appear at block scope");
5553 cp_parser_skip_to_end_of_block_or_statement (parser
);
5554 return error_mark_node
;
5558 cp_parser_error (parser
, "expected primary-expression");
5559 return error_mark_node
;
5562 /* An id-expression can start with either an identifier, a
5563 `::' as the beginning of a qualified-id, or the "operator"
5567 case CPP_TEMPLATE_ID
:
5568 case CPP_NESTED_NAME_SPECIFIER
:
5571 cp_expr id_expression
;
5573 const char *error_msg
;
5576 cp_token
*id_expr_token
;
5578 /* Parse the id-expression. */
5580 = cp_parser_id_expression (parser
,
5581 /*template_keyword_p=*/false,
5582 /*check_dependency_p=*/true,
5584 /*declarator_p=*/false,
5585 /*optional_p=*/false);
5586 if (id_expression
== error_mark_node
)
5587 return error_mark_node
;
5588 id_expr_token
= token
;
5589 token
= cp_lexer_peek_token (parser
->lexer
);
5590 done
= (token
->type
!= CPP_OPEN_SQUARE
5591 && token
->type
!= CPP_OPEN_PAREN
5592 && token
->type
!= CPP_DOT
5593 && token
->type
!= CPP_DEREF
5594 && token
->type
!= CPP_PLUS_PLUS
5595 && token
->type
!= CPP_MINUS_MINUS
);
5596 /* If we have a template-id, then no further lookup is
5597 required. If the template-id was for a template-class, we
5598 will sometimes have a TYPE_DECL at this point. */
5599 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5600 || TREE_CODE (id_expression
) == TYPE_DECL
)
5601 decl
= id_expression
;
5602 /* Look up the name. */
5605 tree ambiguous_decls
;
5607 /* If we already know that this lookup is ambiguous, then
5608 we've already issued an error message; there's no reason
5610 if (id_expr_token
->type
== CPP_NAME
5611 && id_expr_token
->error_reported
)
5613 cp_parser_simulate_error (parser
);
5614 return error_mark_node
;
5617 decl
= cp_parser_lookup_name (parser
, id_expression
,
5620 /*is_namespace=*/false,
5621 /*check_dependency=*/true,
5623 id_expression
.get_location ());
5624 /* If the lookup was ambiguous, an error will already have
5626 if (ambiguous_decls
)
5627 return error_mark_node
;
5629 /* In Objective-C++, we may have an Objective-C 2.0
5630 dot-syntax for classes here. */
5631 if (c_dialect_objc ()
5632 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5633 && TREE_CODE (decl
) == TYPE_DECL
5634 && objc_is_class_name (decl
))
5637 cp_lexer_consume_token (parser
->lexer
);
5638 component
= cp_parser_identifier (parser
);
5639 if (component
== error_mark_node
)
5640 return error_mark_node
;
5642 tree result
= objc_build_class_component_ref (id_expression
,
5644 /* Build a location of the form:
5647 with caret at the start of the component name (at
5648 input_location), ranging from the start of the id_expression
5649 to the end of the component name. */
5650 location_t combined_loc
5651 = make_location (input_location
, id_expression
.get_start (),
5652 get_finish (input_location
));
5653 protected_set_expr_location (result
, combined_loc
);
5657 /* In Objective-C++, an instance variable (ivar) may be preferred
5658 to whatever cp_parser_lookup_name() found.
5659 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5660 rest of c-family, we have to do a little extra work to preserve
5661 any location information in cp_expr "decl". Given that
5662 objc_lookup_ivar is implemented in "c-family" and "objc", we
5663 have a trip through the pure "tree" type, rather than cp_expr.
5664 Naively copying it back to "decl" would implicitly give the
5665 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5666 store an EXPR_LOCATION. Hence we only update "decl" (and
5667 hence its location_t) if we get back a different tree node. */
5668 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5670 if (decl_tree
!= decl
.get_value ())
5671 decl
= cp_expr (decl_tree
);
5673 /* If name lookup gives us a SCOPE_REF, then the
5674 qualifying scope was dependent. */
5675 if (TREE_CODE (decl
) == SCOPE_REF
)
5677 /* At this point, we do not know if DECL is a valid
5678 integral constant expression. We assume that it is
5679 in fact such an expression, so that code like:
5681 template <int N> struct A {
5685 is accepted. At template-instantiation time, we
5686 will check that B<N>::i is actually a constant. */
5689 /* Check to see if DECL is a local variable in a context
5690 where that is forbidden. */
5691 if (parser
->local_variables_forbidden_p
5692 && local_variable_p (decl
))
5694 error_at (id_expression
.get_location (),
5695 "local variable %qD may not appear in this context",
5697 return error_mark_node
;
5701 decl
= (finish_id_expression
5702 (id_expression
, decl
, parser
->scope
,
5704 parser
->integral_constant_expression_p
,
5705 parser
->allow_non_integral_constant_expression_p
,
5706 &parser
->non_integral_constant_expression_p
,
5707 template_p
, done
, address_p
,
5710 id_expression
.get_location ()));
5712 cp_parser_error (parser
, error_msg
);
5713 decl
.set_location (id_expression
.get_location ());
5714 decl
.set_range (id_expr_token
->location
, id_expression
.get_finish ());
5718 /* Anything else is an error. */
5720 cp_parser_error (parser
, "expected primary-expression");
5721 return error_mark_node
;
5725 static inline cp_expr
5726 cp_parser_primary_expression (cp_parser
*parser
,
5729 bool template_arg_p
,
5732 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5733 /*decltype*/false, idk
);
5736 /* Parse an id-expression.
5743 :: [opt] nested-name-specifier template [opt] unqualified-id
5745 :: operator-function-id
5748 Return a representation of the unqualified portion of the
5749 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5750 a `::' or nested-name-specifier.
5752 Often, if the id-expression was a qualified-id, the caller will
5753 want to make a SCOPE_REF to represent the qualified-id. This
5754 function does not do this in order to avoid wastefully creating
5755 SCOPE_REFs when they are not required.
5757 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5760 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5761 uninstantiated templates.
5763 If *TEMPLATE_P is non-NULL, it is set to true iff the
5764 `template' keyword is used to explicitly indicate that the entity
5765 named is a template.
5767 If DECLARATOR_P is true, the id-expression is appearing as part of
5768 a declarator, rather than as part of an expression. */
5771 cp_parser_id_expression (cp_parser
*parser
,
5772 bool template_keyword_p
,
5773 bool check_dependency_p
,
5778 bool global_scope_p
;
5779 bool nested_name_specifier_p
;
5781 /* Assume the `template' keyword was not used. */
5783 *template_p
= template_keyword_p
;
5785 /* Look for the optional `::' operator. */
5787 = (!template_keyword_p
5788 && (cp_parser_global_scope_opt (parser
,
5789 /*current_scope_valid_p=*/false)
5792 /* Look for the optional nested-name-specifier. */
5793 nested_name_specifier_p
5794 = (cp_parser_nested_name_specifier_opt (parser
,
5795 /*typename_keyword_p=*/false,
5802 /* If there is a nested-name-specifier, then we are looking at
5803 the first qualified-id production. */
5804 if (nested_name_specifier_p
)
5807 tree saved_object_scope
;
5808 tree saved_qualifying_scope
;
5809 cp_expr unqualified_id
;
5812 /* See if the next token is the `template' keyword. */
5814 template_p
= &is_template
;
5815 *template_p
= cp_parser_optional_template_keyword (parser
);
5816 /* Name lookup we do during the processing of the
5817 unqualified-id might obliterate SCOPE. */
5818 saved_scope
= parser
->scope
;
5819 saved_object_scope
= parser
->object_scope
;
5820 saved_qualifying_scope
= parser
->qualifying_scope
;
5821 /* Process the final unqualified-id. */
5822 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5825 /*optional_p=*/false);
5826 /* Restore the SAVED_SCOPE for our caller. */
5827 parser
->scope
= saved_scope
;
5828 parser
->object_scope
= saved_object_scope
;
5829 parser
->qualifying_scope
= saved_qualifying_scope
;
5831 return unqualified_id
;
5833 /* Otherwise, if we are in global scope, then we are looking at one
5834 of the other qualified-id productions. */
5835 else if (global_scope_p
)
5840 /* Peek at the next token. */
5841 token
= cp_lexer_peek_token (parser
->lexer
);
5843 /* If it's an identifier, and the next token is not a "<", then
5844 we can avoid the template-id case. This is an optimization
5845 for this common case. */
5846 if (token
->type
== CPP_NAME
5847 && !cp_parser_nth_token_starts_template_argument_list_p
5849 return cp_parser_identifier (parser
);
5851 cp_parser_parse_tentatively (parser
);
5852 /* Try a template-id. */
5853 id
= cp_parser_template_id (parser
,
5854 /*template_keyword_p=*/false,
5855 /*check_dependency_p=*/true,
5858 /* If that worked, we're done. */
5859 if (cp_parser_parse_definitely (parser
))
5862 /* Peek at the next token. (Changes in the token buffer may
5863 have invalidated the pointer obtained above.) */
5864 token
= cp_lexer_peek_token (parser
->lexer
);
5866 switch (token
->type
)
5869 return cp_parser_identifier (parser
);
5872 if (token
->keyword
== RID_OPERATOR
)
5873 return cp_parser_operator_function_id (parser
);
5877 cp_parser_error (parser
, "expected id-expression");
5878 return error_mark_node
;
5882 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5883 /*check_dependency_p=*/true,
5888 /* Parse an unqualified-id.
5892 operator-function-id
5893 conversion-function-id
5897 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5898 keyword, in a construct like `A::template ...'.
5900 Returns a representation of unqualified-id. For the `identifier'
5901 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5902 production a BIT_NOT_EXPR is returned; the operand of the
5903 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5904 other productions, see the documentation accompanying the
5905 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5906 names are looked up in uninstantiated templates. If DECLARATOR_P
5907 is true, the unqualified-id is appearing as part of a declarator,
5908 rather than as part of an expression. */
5911 cp_parser_unqualified_id (cp_parser
* parser
,
5912 bool template_keyword_p
,
5913 bool check_dependency_p
,
5919 /* Peek at the next token. */
5920 token
= cp_lexer_peek_token (parser
->lexer
);
5922 switch ((int) token
->type
)
5928 /* We don't know yet whether or not this will be a
5930 cp_parser_parse_tentatively (parser
);
5931 /* Try a template-id. */
5932 id
= cp_parser_template_id (parser
, template_keyword_p
,
5936 /* If it worked, we're done. */
5937 if (cp_parser_parse_definitely (parser
))
5939 /* Otherwise, it's an ordinary identifier. */
5940 return cp_parser_identifier (parser
);
5943 case CPP_TEMPLATE_ID
:
5944 return cp_parser_template_id (parser
, template_keyword_p
,
5952 tree qualifying_scope
;
5957 /* Consume the `~' token. */
5958 cp_lexer_consume_token (parser
->lexer
);
5959 /* Parse the class-name. The standard, as written, seems to
5962 template <typename T> struct S { ~S (); };
5963 template <typename T> S<T>::~S() {}
5965 is invalid, since `~' must be followed by a class-name, but
5966 `S<T>' is dependent, and so not known to be a class.
5967 That's not right; we need to look in uninstantiated
5968 templates. A further complication arises from:
5970 template <typename T> void f(T t) {
5974 Here, it is not possible to look up `T' in the scope of `T'
5975 itself. We must look in both the current scope, and the
5976 scope of the containing complete expression.
5978 Yet another issue is:
5987 The standard does not seem to say that the `S' in `~S'
5988 should refer to the type `S' and not the data member
5991 /* DR 244 says that we look up the name after the "~" in the
5992 same scope as we looked up the qualifying name. That idea
5993 isn't fully worked out; it's more complicated than that. */
5994 scope
= parser
->scope
;
5995 object_scope
= parser
->object_scope
;
5996 qualifying_scope
= parser
->qualifying_scope
;
5998 /* Check for invalid scopes. */
5999 if (scope
== error_mark_node
)
6001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6002 cp_lexer_consume_token (parser
->lexer
);
6003 return error_mark_node
;
6005 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6007 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6008 error_at (token
->location
,
6009 "scope %qT before %<~%> is not a class-name",
6011 cp_parser_simulate_error (parser
);
6012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6013 cp_lexer_consume_token (parser
->lexer
);
6014 return error_mark_node
;
6016 gcc_assert (!scope
|| TYPE_P (scope
));
6018 /* If the name is of the form "X::~X" it's OK even if X is a
6020 token
= cp_lexer_peek_token (parser
->lexer
);
6022 && token
->type
== CPP_NAME
6023 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6025 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6026 || (CLASS_TYPE_P (scope
)
6027 && constructor_name_p (token
->u
.value
, scope
))))
6029 cp_lexer_consume_token (parser
->lexer
);
6030 return build_nt (BIT_NOT_EXPR
, scope
);
6033 /* ~auto means the destructor of whatever the object is. */
6034 if (cp_parser_is_keyword (token
, RID_AUTO
))
6036 if (cxx_dialect
< cxx14
)
6037 pedwarn (input_location
, 0,
6038 "%<~auto%> only available with "
6039 "-std=c++14 or -std=gnu++14");
6040 cp_lexer_consume_token (parser
->lexer
);
6041 return build_nt (BIT_NOT_EXPR
, make_auto ());
6044 /* If there was an explicit qualification (S::~T), first look
6045 in the scope given by the qualification (i.e., S).
6047 Note: in the calls to cp_parser_class_name below we pass
6048 typename_type so that lookup finds the injected-class-name
6049 rather than the constructor. */
6051 type_decl
= NULL_TREE
;
6054 cp_parser_parse_tentatively (parser
);
6055 type_decl
= cp_parser_class_name (parser
,
6056 /*typename_keyword_p=*/false,
6057 /*template_keyword_p=*/false,
6059 /*check_dependency=*/false,
6060 /*class_head_p=*/false,
6062 if (cp_parser_parse_definitely (parser
))
6065 /* In "N::S::~S", look in "N" as well. */
6066 if (!done
&& scope
&& qualifying_scope
)
6068 cp_parser_parse_tentatively (parser
);
6069 parser
->scope
= qualifying_scope
;
6070 parser
->object_scope
= NULL_TREE
;
6071 parser
->qualifying_scope
= NULL_TREE
;
6073 = cp_parser_class_name (parser
,
6074 /*typename_keyword_p=*/false,
6075 /*template_keyword_p=*/false,
6077 /*check_dependency=*/false,
6078 /*class_head_p=*/false,
6080 if (cp_parser_parse_definitely (parser
))
6083 /* In "p->S::~T", look in the scope given by "*p" as well. */
6084 else if (!done
&& object_scope
)
6086 cp_parser_parse_tentatively (parser
);
6087 parser
->scope
= object_scope
;
6088 parser
->object_scope
= NULL_TREE
;
6089 parser
->qualifying_scope
= NULL_TREE
;
6091 = cp_parser_class_name (parser
,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6098 if (cp_parser_parse_definitely (parser
))
6101 /* Look in the surrounding context. */
6104 parser
->scope
= NULL_TREE
;
6105 parser
->object_scope
= NULL_TREE
;
6106 parser
->qualifying_scope
= NULL_TREE
;
6107 if (processing_template_decl
)
6108 cp_parser_parse_tentatively (parser
);
6110 = cp_parser_class_name (parser
,
6111 /*typename_keyword_p=*/false,
6112 /*template_keyword_p=*/false,
6114 /*check_dependency=*/false,
6115 /*class_head_p=*/false,
6117 if (processing_template_decl
6118 && ! cp_parser_parse_definitely (parser
))
6120 /* We couldn't find a type with this name. If we're parsing
6121 tentatively, fail and try something else. */
6122 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6124 cp_parser_simulate_error (parser
);
6125 return error_mark_node
;
6127 /* Otherwise, accept it and check for a match at instantiation
6129 type_decl
= cp_parser_identifier (parser
);
6130 if (type_decl
!= error_mark_node
)
6131 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6135 /* If an error occurred, assume that the name of the
6136 destructor is the same as the name of the qualifying
6137 class. That allows us to keep parsing after running
6138 into ill-formed destructor names. */
6139 if (type_decl
== error_mark_node
&& scope
)
6140 return build_nt (BIT_NOT_EXPR
, scope
);
6141 else if (type_decl
== error_mark_node
)
6142 return error_mark_node
;
6144 /* Check that destructor name and scope match. */
6145 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6147 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6148 error_at (token
->location
,
6149 "declaration of %<~%T%> as member of %qT",
6151 cp_parser_simulate_error (parser
);
6152 return error_mark_node
;
6157 A typedef-name that names a class shall not be used as the
6158 identifier in the declarator for a destructor declaration. */
6160 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6161 && !DECL_SELF_REFERENCE_P (type_decl
)
6162 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6163 error_at (token
->location
,
6164 "typedef-name %qD used as destructor declarator",
6167 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6171 if (token
->keyword
== RID_OPERATOR
)
6175 /* This could be a template-id, so we try that first. */
6176 cp_parser_parse_tentatively (parser
);
6177 /* Try a template-id. */
6178 id
= cp_parser_template_id (parser
, template_keyword_p
,
6179 /*check_dependency_p=*/true,
6182 /* If that worked, we're done. */
6183 if (cp_parser_parse_definitely (parser
))
6185 /* We still don't know whether we're looking at an
6186 operator-function-id or a conversion-function-id. */
6187 cp_parser_parse_tentatively (parser
);
6188 /* Try an operator-function-id. */
6189 id
= cp_parser_operator_function_id (parser
);
6190 /* If that didn't work, try a conversion-function-id. */
6191 if (!cp_parser_parse_definitely (parser
))
6192 id
= cp_parser_conversion_function_id (parser
);
6201 cp_parser_error (parser
, "expected unqualified-id");
6202 return error_mark_node
;
6206 /* Parse an (optional) nested-name-specifier.
6208 nested-name-specifier: [C++98]
6209 class-or-namespace-name :: nested-name-specifier [opt]
6210 class-or-namespace-name :: template nested-name-specifier [opt]
6212 nested-name-specifier: [C++0x]
6215 nested-name-specifier identifier ::
6216 nested-name-specifier template [opt] simple-template-id ::
6218 PARSER->SCOPE should be set appropriately before this function is
6219 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6220 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6223 Sets PARSER->SCOPE to the class (TYPE) or namespace
6224 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6225 it unchanged if there is no nested-name-specifier. Returns the new
6226 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6228 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6229 part of a declaration and/or decl-specifier. */
6232 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6233 bool typename_keyword_p
,
6234 bool check_dependency_p
,
6236 bool is_declaration
,
6237 bool template_keyword_p
/* = false */)
6239 bool success
= false;
6240 cp_token_position start
= 0;
6243 /* Remember where the nested-name-specifier starts. */
6244 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6246 start
= cp_lexer_token_position (parser
->lexer
, false);
6247 push_deferring_access_checks (dk_deferred
);
6254 tree saved_qualifying_scope
;
6256 /* Spot cases that cannot be the beginning of a
6257 nested-name-specifier. */
6258 token
= cp_lexer_peek_token (parser
->lexer
);
6260 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6261 the already parsed nested-name-specifier. */
6262 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6264 /* Grab the nested-name-specifier and continue the loop. */
6265 cp_parser_pre_parsed_nested_name_specifier (parser
);
6266 /* If we originally encountered this nested-name-specifier
6267 with IS_DECLARATION set to false, we will not have
6268 resolved TYPENAME_TYPEs, so we must do so here. */
6270 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6272 new_scope
= resolve_typename_type (parser
->scope
,
6273 /*only_current_p=*/false);
6274 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6275 parser
->scope
= new_scope
;
6281 /* Spot cases that cannot be the beginning of a
6282 nested-name-specifier. On the second and subsequent times
6283 through the loop, we look for the `template' keyword. */
6284 if (success
&& token
->keyword
== RID_TEMPLATE
)
6286 /* A template-id can start a nested-name-specifier. */
6287 else if (token
->type
== CPP_TEMPLATE_ID
)
6289 /* DR 743: decltype can be used in a nested-name-specifier. */
6290 else if (token_is_decltype (token
))
6294 /* If the next token is not an identifier, then it is
6295 definitely not a type-name or namespace-name. */
6296 if (token
->type
!= CPP_NAME
)
6298 /* If the following token is neither a `<' (to begin a
6299 template-id), nor a `::', then we are not looking at a
6300 nested-name-specifier. */
6301 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6303 if (token
->type
== CPP_COLON
6304 && parser
->colon_corrects_to_scope_p
6305 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6307 gcc_rich_location
richloc (token
->location
);
6308 richloc
.add_fixit_replace ("::");
6310 "found %<:%> in nested-name-specifier, "
6312 token
->type
= CPP_SCOPE
;
6315 if (token
->type
!= CPP_SCOPE
6316 && !cp_parser_nth_token_starts_template_argument_list_p
6321 /* The nested-name-specifier is optional, so we parse
6323 cp_parser_parse_tentatively (parser
);
6325 /* Look for the optional `template' keyword, if this isn't the
6326 first time through the loop. */
6328 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6330 /* Save the old scope since the name lookup we are about to do
6331 might destroy it. */
6332 old_scope
= parser
->scope
;
6333 saved_qualifying_scope
= parser
->qualifying_scope
;
6334 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6335 look up names in "X<T>::I" in order to determine that "Y" is
6336 a template. So, if we have a typename at this point, we make
6337 an effort to look through it. */
6339 && !typename_keyword_p
6341 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6342 parser
->scope
= resolve_typename_type (parser
->scope
,
6343 /*only_current_p=*/false);
6344 /* Parse the qualifying entity. */
6346 = cp_parser_qualifying_entity (parser
,
6352 /* Look for the `::' token. */
6353 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6355 /* If we found what we wanted, we keep going; otherwise, we're
6357 if (!cp_parser_parse_definitely (parser
))
6359 bool error_p
= false;
6361 /* Restore the OLD_SCOPE since it was valid before the
6362 failed attempt at finding the last
6363 class-or-namespace-name. */
6364 parser
->scope
= old_scope
;
6365 parser
->qualifying_scope
= saved_qualifying_scope
;
6367 /* If the next token is a decltype, and the one after that is a
6368 `::', then the decltype has failed to resolve to a class or
6369 enumeration type. Give this error even when parsing
6370 tentatively since it can't possibly be valid--and we're going
6371 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6372 won't get another chance.*/
6373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6374 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6377 token
= cp_lexer_consume_token (parser
->lexer
);
6378 error_at (token
->location
, "decltype evaluates to %qT, "
6379 "which is not a class or enumeration type",
6380 token
->u
.tree_check_value
->value
);
6381 parser
->scope
= error_mark_node
;
6385 cp_lexer_consume_token (parser
->lexer
);
6388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6389 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6391 /* If we have a non-type template-id followed by ::, it can't
6392 possibly be valid. */
6393 token
= cp_lexer_peek_token (parser
->lexer
);
6394 tree tid
= token
->u
.tree_check_value
->value
;
6395 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6396 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6398 tree tmpl
= NULL_TREE
;
6399 if (is_overloaded_fn (tid
))
6401 tree fns
= get_fns (tid
);
6402 if (OVL_SINGLE_P (fns
))
6403 tmpl
= OVL_FIRST (fns
);
6404 error_at (token
->location
, "function template-id %qD "
6405 "in nested-name-specifier", tid
);
6409 /* Variable template. */
6410 tmpl
= TREE_OPERAND (tid
, 0);
6411 gcc_assert (variable_template_p (tmpl
));
6412 error_at (token
->location
, "variable template-id %qD "
6413 "in nested-name-specifier", tid
);
6416 inform (DECL_SOURCE_LOCATION (tmpl
),
6417 "%qD declared here", tmpl
);
6419 parser
->scope
= error_mark_node
;
6423 cp_lexer_consume_token (parser
->lexer
);
6424 cp_lexer_consume_token (parser
->lexer
);
6428 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6430 /* If the next token is an identifier, and the one after
6431 that is a `::', then any valid interpretation would have
6432 found a class-or-namespace-name. */
6433 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6434 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6436 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6439 token
= cp_lexer_consume_token (parser
->lexer
);
6442 if (!token
->error_reported
)
6445 tree ambiguous_decls
;
6447 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6449 /*is_template=*/false,
6450 /*is_namespace=*/false,
6451 /*check_dependency=*/true,
6454 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6455 error_at (token
->location
,
6456 "%qD used without template arguments",
6458 else if (ambiguous_decls
)
6460 // cp_parser_lookup_name has the same diagnostic,
6461 // thus make sure to emit it at most once.
6462 if (cp_parser_uncommitted_to_tentative_parse_p
6465 error_at (token
->location
,
6466 "reference to %qD is ambiguous",
6468 print_candidates (ambiguous_decls
);
6470 decl
= error_mark_node
;
6474 if (cxx_dialect
!= cxx98
)
6475 cp_parser_name_lookup_error
6476 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6479 cp_parser_name_lookup_error
6480 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6484 parser
->scope
= error_mark_node
;
6486 /* Treat this as a successful nested-name-specifier
6491 If the name found is not a class-name (clause
6492 _class_) or namespace-name (_namespace.def_), the
6493 program is ill-formed. */
6496 cp_lexer_consume_token (parser
->lexer
);
6500 /* We've found one valid nested-name-specifier. */
6502 /* Name lookup always gives us a DECL. */
6503 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6504 new_scope
= TREE_TYPE (new_scope
);
6505 /* Uses of "template" must be followed by actual templates. */
6506 if (template_keyword_p
6507 && !(CLASS_TYPE_P (new_scope
)
6508 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6509 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6510 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6511 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6512 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6513 == TEMPLATE_ID_EXPR
)))
6514 permerror (input_location
, TYPE_P (new_scope
)
6515 ? G_("%qT is not a template")
6516 : G_("%qD is not a template"),
6518 /* If it is a class scope, try to complete it; we are about to
6519 be looking up names inside the class. */
6520 if (TYPE_P (new_scope
)
6521 /* Since checking types for dependency can be expensive,
6522 avoid doing it if the type is already complete. */
6523 && !COMPLETE_TYPE_P (new_scope
)
6524 /* Do not try to complete dependent types. */
6525 && !dependent_type_p (new_scope
))
6527 new_scope
= complete_type (new_scope
);
6528 /* If it is a typedef to current class, use the current
6529 class instead, as the typedef won't have any names inside
6531 if (!COMPLETE_TYPE_P (new_scope
)
6532 && currently_open_class (new_scope
))
6533 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6535 /* Make sure we look in the right scope the next time through
6537 parser
->scope
= new_scope
;
6540 /* If parsing tentatively, replace the sequence of tokens that makes
6541 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6542 token. That way, should we re-parse the token stream, we will
6543 not have to repeat the effort required to do the parse, nor will
6544 we issue duplicate error messages. */
6545 if (success
&& start
)
6549 token
= cp_lexer_token_at (parser
->lexer
, start
);
6550 /* Reset the contents of the START token. */
6551 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6552 /* Retrieve any deferred checks. Do not pop this access checks yet
6553 so the memory will not be reclaimed during token replacing below. */
6554 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6555 token
->u
.tree_check_value
->value
= parser
->scope
;
6556 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6557 token
->u
.tree_check_value
->qualifying_scope
=
6558 parser
->qualifying_scope
;
6559 token
->keyword
= RID_MAX
;
6561 /* Purge all subsequent tokens. */
6562 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6566 pop_to_parent_deferring_access_checks ();
6568 return success
? parser
->scope
: NULL_TREE
;
6571 /* Parse a nested-name-specifier. See
6572 cp_parser_nested_name_specifier_opt for details. This function
6573 behaves identically, except that it will an issue an error if no
6574 nested-name-specifier is present. */
6577 cp_parser_nested_name_specifier (cp_parser
*parser
,
6578 bool typename_keyword_p
,
6579 bool check_dependency_p
,
6581 bool is_declaration
)
6585 /* Look for the nested-name-specifier. */
6586 scope
= cp_parser_nested_name_specifier_opt (parser
,
6591 /* If it was not present, issue an error message. */
6594 cp_parser_error (parser
, "expected nested-name-specifier");
6595 parser
->scope
= NULL_TREE
;
6601 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6602 this is either a class-name or a namespace-name (which corresponds
6603 to the class-or-namespace-name production in the grammar). For
6604 C++0x, it can also be a type-name that refers to an enumeration
6605 type or a simple-template-id.
6607 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6608 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6609 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6610 TYPE_P is TRUE iff the next name should be taken as a class-name,
6611 even the same name is declared to be another entity in the same
6614 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6615 specified by the class-or-namespace-name. If neither is found the
6616 ERROR_MARK_NODE is returned. */
6619 cp_parser_qualifying_entity (cp_parser
*parser
,
6620 bool typename_keyword_p
,
6621 bool template_keyword_p
,
6622 bool check_dependency_p
,
6624 bool is_declaration
)
6627 tree saved_qualifying_scope
;
6628 tree saved_object_scope
;
6631 bool successful_parse_p
;
6633 /* DR 743: decltype can appear in a nested-name-specifier. */
6634 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6636 scope
= cp_parser_decltype (parser
);
6637 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6638 && !MAYBE_CLASS_TYPE_P (scope
))
6640 cp_parser_simulate_error (parser
);
6641 return error_mark_node
;
6643 if (TYPE_NAME (scope
))
6644 scope
= TYPE_NAME (scope
);
6648 /* Before we try to parse the class-name, we must save away the
6649 current PARSER->SCOPE since cp_parser_class_name will destroy
6651 saved_scope
= parser
->scope
;
6652 saved_qualifying_scope
= parser
->qualifying_scope
;
6653 saved_object_scope
= parser
->object_scope
;
6654 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6655 there is no need to look for a namespace-name. */
6656 only_class_p
= template_keyword_p
6657 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6659 cp_parser_parse_tentatively (parser
);
6660 scope
= cp_parser_class_name (parser
,
6663 type_p
? class_type
: none_type
,
6665 /*class_head_p=*/false,
6667 /*enum_ok=*/cxx_dialect
> cxx98
);
6668 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6669 /* If that didn't work, try for a namespace-name. */
6670 if (!only_class_p
&& !successful_parse_p
)
6672 /* Restore the saved scope. */
6673 parser
->scope
= saved_scope
;
6674 parser
->qualifying_scope
= saved_qualifying_scope
;
6675 parser
->object_scope
= saved_object_scope
;
6676 /* If we are not looking at an identifier followed by the scope
6677 resolution operator, then this is not part of a
6678 nested-name-specifier. (Note that this function is only used
6679 to parse the components of a nested-name-specifier.) */
6680 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6681 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6682 return error_mark_node
;
6683 scope
= cp_parser_namespace_name (parser
);
6689 /* Return true if we are looking at a compound-literal, false otherwise. */
6692 cp_parser_compound_literal_p (cp_parser
*parser
)
6694 cp_lexer_save_tokens (parser
->lexer
);
6696 /* Skip tokens until the next token is a closing parenthesis.
6697 If we find the closing `)', and the next token is a `{', then
6698 we are looking at a compound-literal. */
6699 bool compound_literal_p
6700 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6701 /*consume_paren=*/true)
6702 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6704 /* Roll back the tokens we skipped. */
6705 cp_lexer_rollback_tokens (parser
->lexer
);
6707 return compound_literal_p
;
6710 /* Return true if EXPR is the integer constant zero or a complex constant
6711 of zero, without any folding, but ignoring location wrappers. */
6714 literal_integer_zerop (const_tree expr
)
6716 return (location_wrapper_p (expr
)
6717 && integer_zerop (TREE_OPERAND (expr
, 0)));
6720 /* Parse a postfix-expression.
6724 postfix-expression [ expression ]
6725 postfix-expression ( expression-list [opt] )
6726 simple-type-specifier ( expression-list [opt] )
6727 typename :: [opt] nested-name-specifier identifier
6728 ( expression-list [opt] )
6729 typename :: [opt] nested-name-specifier template [opt] template-id
6730 ( expression-list [opt] )
6731 postfix-expression . template [opt] id-expression
6732 postfix-expression -> template [opt] id-expression
6733 postfix-expression . pseudo-destructor-name
6734 postfix-expression -> pseudo-destructor-name
6735 postfix-expression ++
6736 postfix-expression --
6737 dynamic_cast < type-id > ( expression )
6738 static_cast < type-id > ( expression )
6739 reinterpret_cast < type-id > ( expression )
6740 const_cast < type-id > ( expression )
6741 typeid ( expression )
6747 ( type-id ) { initializer-list , [opt] }
6749 This extension is a GNU version of the C99 compound-literal
6750 construct. (The C99 grammar uses `type-name' instead of `type-id',
6751 but they are essentially the same concept.)
6753 If ADDRESS_P is true, the postfix expression is the operand of the
6754 `&' operator. CAST_P is true if this expression is the target of a
6757 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6758 class member access expressions [expr.ref].
6760 Returns a representation of the expression. */
6763 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6764 bool member_access_only_p
, bool decltype_p
,
6765 cp_id_kind
* pidk_return
)
6770 cp_id_kind idk
= CP_ID_KIND_NONE
;
6771 cp_expr postfix_expression
= NULL_TREE
;
6772 bool is_member_access
= false;
6774 /* Peek at the next token. */
6775 token
= cp_lexer_peek_token (parser
->lexer
);
6776 loc
= token
->location
;
6777 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6779 /* Some of the productions are determined by keywords. */
6780 keyword
= token
->keyword
;
6790 const char *saved_message
;
6791 bool saved_in_type_id_in_expr_p
;
6793 /* All of these can be handled in the same way from the point
6794 of view of parsing. Begin by consuming the token
6795 identifying the cast. */
6796 cp_lexer_consume_token (parser
->lexer
);
6798 /* New types cannot be defined in the cast. */
6799 saved_message
= parser
->type_definition_forbidden_message
;
6800 parser
->type_definition_forbidden_message
6801 = G_("types may not be defined in casts");
6803 /* Look for the opening `<'. */
6804 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6805 /* Parse the type to which we are casting. */
6806 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6807 parser
->in_type_id_in_expr_p
= true;
6808 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6810 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6811 /* Look for the closing `>'. */
6812 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6813 /* Restore the old message. */
6814 parser
->type_definition_forbidden_message
= saved_message
;
6816 bool saved_greater_than_is_operator_p
6817 = parser
->greater_than_is_operator_p
;
6818 parser
->greater_than_is_operator_p
= true;
6820 /* And the expression which is being cast. */
6821 matching_parens parens
;
6822 parens
.require_open (parser
);
6823 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6824 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6826 location_t end_loc
= close_paren
?
6827 close_paren
->location
: UNKNOWN_LOCATION
;
6829 parser
->greater_than_is_operator_p
6830 = saved_greater_than_is_operator_p
;
6832 /* Only type conversions to integral or enumeration types
6833 can be used in constant-expressions. */
6834 if (!cast_valid_in_integral_constant_expression_p (type
)
6835 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6837 postfix_expression
= error_mark_node
;
6845 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6849 = build_static_cast (type
, expression
, tf_warning_or_error
);
6853 = build_reinterpret_cast (type
, expression
,
6854 tf_warning_or_error
);
6858 = build_const_cast (type
, expression
, tf_warning_or_error
);
6864 /* Construct a location e.g. :
6865 reinterpret_cast <int *> (expr)
6866 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6867 ranging from the start of the "*_cast" token to the final closing
6868 paren, with the caret at the start. */
6869 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6870 postfix_expression
.set_location (cp_cast_loc
);
6877 const char *saved_message
;
6878 bool saved_in_type_id_in_expr_p
;
6880 /* Consume the `typeid' token. */
6881 cp_lexer_consume_token (parser
->lexer
);
6882 /* Look for the `(' token. */
6883 matching_parens parens
;
6884 parens
.require_open (parser
);
6885 /* Types cannot be defined in a `typeid' expression. */
6886 saved_message
= parser
->type_definition_forbidden_message
;
6887 parser
->type_definition_forbidden_message
6888 = G_("types may not be defined in a %<typeid%> expression");
6889 /* We can't be sure yet whether we're looking at a type-id or an
6891 cp_parser_parse_tentatively (parser
);
6892 /* Try a type-id first. */
6893 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6894 parser
->in_type_id_in_expr_p
= true;
6895 type
= cp_parser_type_id (parser
);
6896 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6897 /* Look for the `)' token. Otherwise, we can't be sure that
6898 we're not looking at an expression: consider `typeid (int
6899 (3))', for example. */
6900 cp_token
*close_paren
= parens
.require_close (parser
);
6901 /* If all went well, simply lookup the type-id. */
6902 if (cp_parser_parse_definitely (parser
))
6903 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6904 /* Otherwise, fall back to the expression variant. */
6909 /* Look for an expression. */
6910 expression
= cp_parser_expression (parser
, & idk
);
6911 /* Compute its typeid. */
6912 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6913 /* Look for the `)' token. */
6914 close_paren
= parens
.require_close (parser
);
6916 /* Restore the saved message. */
6917 parser
->type_definition_forbidden_message
= saved_message
;
6918 /* `typeid' may not appear in an integral constant expression. */
6919 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6920 postfix_expression
= error_mark_node
;
6922 /* Construct a location e.g. :
6925 ranging from the start of the "typeid" token to the final closing
6926 paren, with the caret at the start. */
6929 location_t typeid_loc
6930 = make_location (start_loc
, start_loc
, close_paren
->location
);
6931 postfix_expression
.set_location (typeid_loc
);
6932 postfix_expression
.maybe_add_location_wrapper ();
6940 /* The syntax permitted here is the same permitted for an
6941 elaborated-type-specifier. */
6942 ++parser
->prevent_constrained_type_specifiers
;
6943 type
= cp_parser_elaborated_type_specifier (parser
,
6944 /*is_friend=*/false,
6945 /*is_declaration=*/false);
6946 --parser
->prevent_constrained_type_specifiers
;
6947 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6952 case RID_BUILTIN_SHUFFLE
:
6953 case RID_BUILTIN_LAUNDER
:
6955 vec
<tree
, va_gc
> *vec
;
6959 cp_lexer_consume_token (parser
->lexer
);
6960 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6961 /*cast_p=*/false, /*allow_expansion_p=*/true,
6962 /*non_constant_p=*/NULL
);
6965 postfix_expression
= error_mark_node
;
6969 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6975 if (vec
->length () == 1)
6977 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6980 error_at (loc
, "wrong number of arguments to "
6981 "%<__builtin_addressof%>");
6982 postfix_expression
= error_mark_node
;
6986 case RID_BUILTIN_LAUNDER
:
6987 if (vec
->length () == 1)
6988 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6989 tf_warning_or_error
);
6992 error_at (loc
, "wrong number of arguments to "
6993 "%<__builtin_launder%>");
6994 postfix_expression
= error_mark_node
;
6998 case RID_BUILTIN_SHUFFLE
:
6999 if (vec
->length () == 2)
7001 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7002 (*vec
)[1], tf_warning_or_error
);
7003 else if (vec
->length () == 3)
7005 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7006 (*vec
)[2], tf_warning_or_error
);
7009 error_at (loc
, "wrong number of arguments to "
7010 "%<__builtin_shuffle%>");
7011 postfix_expression
= error_mark_node
;
7025 /* If the next thing is a simple-type-specifier, we may be
7026 looking at a functional cast. We could also be looking at
7027 an id-expression. So, we try the functional cast, and if
7028 that doesn't work we fall back to the primary-expression. */
7029 cp_parser_parse_tentatively (parser
);
7030 /* Look for the simple-type-specifier. */
7031 ++parser
->prevent_constrained_type_specifiers
;
7032 type
= cp_parser_simple_type_specifier (parser
,
7033 /*decl_specs=*/NULL
,
7034 CP_PARSER_FLAGS_NONE
);
7035 --parser
->prevent_constrained_type_specifiers
;
7036 /* Parse the cast itself. */
7037 if (!cp_parser_error_occurred (parser
))
7039 = cp_parser_functional_cast (parser
, type
);
7040 /* If that worked, we're done. */
7041 if (cp_parser_parse_definitely (parser
))
7044 /* If the functional-cast didn't work out, try a
7045 compound-literal. */
7046 if (cp_parser_allow_gnu_extensions_p (parser
)
7047 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7049 cp_expr initializer
= NULL_TREE
;
7051 cp_parser_parse_tentatively (parser
);
7053 matching_parens parens
;
7054 parens
.consume_open (parser
);
7056 /* Avoid calling cp_parser_type_id pointlessly, see comment
7057 in cp_parser_cast_expression about c++/29234. */
7058 if (!cp_parser_compound_literal_p (parser
))
7059 cp_parser_simulate_error (parser
);
7062 /* Parse the type. */
7063 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7064 parser
->in_type_id_in_expr_p
= true;
7065 type
= cp_parser_type_id (parser
);
7066 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7067 parens
.require_close (parser
);
7070 /* If things aren't going well, there's no need to
7072 if (!cp_parser_error_occurred (parser
))
7074 bool non_constant_p
;
7075 /* Parse the brace-enclosed initializer list. */
7076 initializer
= cp_parser_braced_list (parser
,
7079 /* If that worked, we're definitely looking at a
7080 compound-literal expression. */
7081 if (cp_parser_parse_definitely (parser
))
7083 /* Warn the user that a compound literal is not
7084 allowed in standard C++. */
7085 pedwarn (input_location
, OPT_Wpedantic
,
7086 "ISO C++ forbids compound-literals");
7087 /* For simplicity, we disallow compound literals in
7088 constant-expressions. We could
7089 allow compound literals of integer type, whose
7090 initializer was a constant, in constant
7091 expressions. Permitting that usage, as a further
7092 extension, would not change the meaning of any
7093 currently accepted programs. (Of course, as
7094 compound literals are not part of ISO C++, the
7095 standard has nothing to say.) */
7096 if (cp_parser_non_integral_constant_expression (parser
,
7099 postfix_expression
= error_mark_node
;
7102 /* Form the representation of the compound-literal. */
7104 = finish_compound_literal (type
, initializer
,
7105 tf_warning_or_error
, fcl_c99
);
7106 postfix_expression
.set_location (initializer
.get_location ());
7111 /* It must be a primary-expression. */
7113 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7114 /*template_arg_p=*/false,
7121 /* Note that we don't need to worry about calling build_cplus_new on a
7122 class-valued CALL_EXPR in decltype when it isn't the end of the
7123 postfix-expression; unary_complex_lvalue will take care of that for
7126 /* Keep looping until the postfix-expression is complete. */
7129 if (idk
== CP_ID_KIND_UNQUALIFIED
7130 && identifier_p (postfix_expression
)
7131 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7132 /* It is not a Koenig lookup function call. */
7134 = unqualified_name_lookup_error (postfix_expression
);
7136 /* Peek at the next token. */
7137 token
= cp_lexer_peek_token (parser
->lexer
);
7139 switch (token
->type
)
7141 case CPP_OPEN_SQUARE
:
7142 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7144 cp_parser_error (parser
,
7145 "two consecutive %<[%> shall "
7146 "only introduce an attribute");
7147 return error_mark_node
;
7150 = cp_parser_postfix_open_square_expression (parser
,
7154 postfix_expression
.set_range (start_loc
,
7155 postfix_expression
.get_location ());
7157 idk
= CP_ID_KIND_NONE
;
7158 is_member_access
= false;
7161 case CPP_OPEN_PAREN
:
7162 /* postfix-expression ( expression-list [opt] ) */
7165 bool is_builtin_constant_p
;
7166 bool saved_integral_constant_expression_p
= false;
7167 bool saved_non_integral_constant_expression_p
= false;
7168 tsubst_flags_t complain
= complain_flags (decltype_p
);
7169 vec
<tree
, va_gc
> *args
;
7170 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7172 is_member_access
= false;
7174 tree stripped_expression
7175 = tree_strip_any_location_wrapper (postfix_expression
);
7176 is_builtin_constant_p
7177 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7178 if (is_builtin_constant_p
)
7180 /* The whole point of __builtin_constant_p is to allow
7181 non-constant expressions to appear as arguments. */
7182 saved_integral_constant_expression_p
7183 = parser
->integral_constant_expression_p
;
7184 saved_non_integral_constant_expression_p
7185 = parser
->non_integral_constant_expression_p
;
7186 parser
->integral_constant_expression_p
= false;
7188 args
= (cp_parser_parenthesized_expression_list
7190 /*cast_p=*/false, /*allow_expansion_p=*/true,
7191 /*non_constant_p=*/NULL
,
7192 /*close_paren_loc=*/&close_paren_loc
,
7193 /*wrap_locations_p=*/true));
7194 if (is_builtin_constant_p
)
7196 parser
->integral_constant_expression_p
7197 = saved_integral_constant_expression_p
;
7198 parser
->non_integral_constant_expression_p
7199 = saved_non_integral_constant_expression_p
;
7204 postfix_expression
= error_mark_node
;
7208 /* Function calls are not permitted in
7209 constant-expressions. */
7210 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7211 && cp_parser_non_integral_constant_expression (parser
,
7214 postfix_expression
= error_mark_node
;
7215 release_tree_vector (args
);
7220 if (idk
== CP_ID_KIND_UNQUALIFIED
7221 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7223 if (identifier_p (postfix_expression
)
7224 /* In C++2A, we may need to perform ADL for a template
7226 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7227 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7229 if (!args
->is_empty ())
7232 if (!any_type_dependent_arguments_p (args
))
7234 = perform_koenig_lookup (postfix_expression
, args
,
7239 = unqualified_fn_lookup_error (postfix_expression
);
7241 /* We do not perform argument-dependent lookup if
7242 normal lookup finds a non-function, in accordance
7243 with the expected resolution of DR 218. */
7244 else if (!args
->is_empty ()
7245 && is_overloaded_fn (postfix_expression
))
7247 /* We only need to look at the first function,
7248 because all the fns share the attribute we're
7249 concerned with (all member fns or all local
7251 tree fn
= get_first_fn (postfix_expression
);
7252 fn
= STRIP_TEMPLATE (fn
);
7254 /* Do not do argument dependent lookup if regular
7255 lookup finds a member function or a block-scope
7256 function declaration. [basic.lookup.argdep]/3 */
7257 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7258 || DECL_FUNCTION_MEMBER_P (fn
)
7259 || DECL_LOCAL_FUNCTION_P (fn
)))
7262 if (!any_type_dependent_arguments_p (args
))
7264 = perform_koenig_lookup (postfix_expression
, args
,
7270 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7272 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7273 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7275 if (processing_template_decl
7276 && (type_dependent_object_expression_p (instance
)
7277 || (!BASELINK_P (fn
)
7278 && TREE_CODE (fn
) != FIELD_DECL
)
7279 || type_dependent_expression_p (fn
)
7280 || any_type_dependent_arguments_p (args
)))
7282 maybe_generic_this_capture (instance
, fn
);
7284 = build_min_nt_call_vec (postfix_expression
, args
);
7285 release_tree_vector (args
);
7289 if (BASELINK_P (fn
))
7292 = (build_new_method_call
7293 (instance
, fn
, &args
, NULL_TREE
,
7294 (idk
== CP_ID_KIND_QUALIFIED
7295 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7302 = finish_call_expr (postfix_expression
, &args
,
7303 /*disallow_virtual=*/false,
7307 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7308 || TREE_CODE (postfix_expression
) == MEMBER_REF
7309 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7310 postfix_expression
= (build_offset_ref_call_from_tree
7311 (postfix_expression
, &args
,
7313 else if (idk
== CP_ID_KIND_QUALIFIED
)
7314 /* A call to a static class member, or a namespace-scope
7317 = finish_call_expr (postfix_expression
, &args
,
7318 /*disallow_virtual=*/true,
7322 /* All other function calls. */
7324 = finish_call_expr (postfix_expression
, &args
,
7325 /*disallow_virtual=*/false,
7329 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7331 location_t combined_loc
= make_location (token
->location
,
7334 postfix_expression
.set_location (combined_loc
);
7337 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7338 idk
= CP_ID_KIND_NONE
;
7340 release_tree_vector (args
);
7346 /* postfix-expression . template [opt] id-expression
7347 postfix-expression . pseudo-destructor-name
7348 postfix-expression -> template [opt] id-expression
7349 postfix-expression -> pseudo-destructor-name */
7351 /* Consume the `.' or `->' operator. */
7352 cp_lexer_consume_token (parser
->lexer
);
7355 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7359 is_member_access
= true;
7363 /* postfix-expression ++ */
7364 /* Consume the `++' token. */
7365 cp_lexer_consume_token (parser
->lexer
);
7366 /* Generate a representation for the complete expression. */
7368 = finish_increment_expr (postfix_expression
,
7369 POSTINCREMENT_EXPR
);
7370 /* Increments may not appear in constant-expressions. */
7371 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7372 postfix_expression
= error_mark_node
;
7373 idk
= CP_ID_KIND_NONE
;
7374 is_member_access
= false;
7377 case CPP_MINUS_MINUS
:
7378 /* postfix-expression -- */
7379 /* Consume the `--' token. */
7380 cp_lexer_consume_token (parser
->lexer
);
7381 /* Generate a representation for the complete expression. */
7383 = finish_increment_expr (postfix_expression
,
7384 POSTDECREMENT_EXPR
);
7385 /* Decrements may not appear in constant-expressions. */
7386 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7387 postfix_expression
= error_mark_node
;
7388 idk
= CP_ID_KIND_NONE
;
7389 is_member_access
= false;
7393 if (pidk_return
!= NULL
)
7394 * pidk_return
= idk
;
7395 if (member_access_only_p
)
7396 return is_member_access
7397 ? postfix_expression
7398 : cp_expr (error_mark_node
);
7400 return postfix_expression
;
7404 /* We should never get here. */
7406 return error_mark_node
;
7409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7410 by cp_parser_builtin_offsetof. We're looking for
7412 postfix-expression [ expression ]
7413 postfix-expression [ braced-init-list ] (C++11)
7415 FOR_OFFSETOF is set if we're being called in that context, which
7416 changes how we deal with integer constant expressions. */
7419 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7420 tree postfix_expression
,
7424 tree index
= NULL_TREE
;
7425 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7426 bool saved_greater_than_is_operator_p
;
7428 /* Consume the `[' token. */
7429 cp_lexer_consume_token (parser
->lexer
);
7431 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7432 parser
->greater_than_is_operator_p
= true;
7434 /* Parse the index expression. */
7435 /* ??? For offsetof, there is a question of what to allow here. If
7436 offsetof is not being used in an integral constant expression context,
7437 then we *could* get the right answer by computing the value at runtime.
7438 If we are in an integral constant expression context, then we might
7439 could accept any constant expression; hard to say without analysis.
7440 Rather than open the barn door too wide right away, allow only integer
7441 constant expressions here. */
7443 index
= cp_parser_constant_expression (parser
);
7446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7448 bool expr_nonconst_p
;
7449 cp_lexer_set_source_position (parser
->lexer
);
7450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7451 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7454 index
= cp_parser_expression (parser
);
7457 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7459 /* Look for the closing `]'. */
7460 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7462 /* Build the ARRAY_REF. */
7463 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7466 /* When not doing offsetof, array references are not permitted in
7467 constant-expressions. */
7469 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7470 postfix_expression
= error_mark_node
;
7472 return postfix_expression
;
7475 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7476 dereference of incomplete type, returns true if error_mark_node should
7477 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7478 and *DEPENDENT_P. */
7481 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7484 /* In a template, be permissive by treating an object expression
7485 of incomplete type as dependent (after a pedwarn). */
7486 diagnostic_t kind
= (processing_template_decl
7487 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7489 switch (TREE_CODE (*postfix_expression
))
7492 case REINTERPRET_CAST_EXPR
:
7493 case CONST_CAST_EXPR
:
7494 case STATIC_CAST_EXPR
:
7495 case DYNAMIC_CAST_EXPR
:
7496 case IMPLICIT_CONV_EXPR
:
7497 case VIEW_CONVERT_EXPR
:
7498 case NON_LVALUE_EXPR
:
7502 /* Don't emit any diagnostic for OVERLOADs. */
7506 /* Avoid clobbering e.g. DECLs. */
7507 if (!EXPR_P (*postfix_expression
))
7512 if (kind
== DK_IGNORED
)
7515 location_t exploc
= location_of (*postfix_expression
);
7516 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7517 if (!MAYBE_CLASS_TYPE_P (*scope
))
7519 if (kind
== DK_ERROR
)
7520 *scope
= *postfix_expression
= error_mark_node
;
7521 else if (processing_template_decl
)
7523 *dependent_p
= true;
7524 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7529 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7530 by cp_parser_builtin_offsetof. We're looking for
7532 postfix-expression . template [opt] id-expression
7533 postfix-expression . pseudo-destructor-name
7534 postfix-expression -> template [opt] id-expression
7535 postfix-expression -> pseudo-destructor-name
7537 FOR_OFFSETOF is set if we're being called in that context. That sorta
7538 limits what of the above we'll actually accept, but nevermind.
7539 TOKEN_TYPE is the "." or "->" token, which will already have been
7540 removed from the stream. */
7543 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7544 enum cpp_ttype token_type
,
7545 cp_expr postfix_expression
,
7546 bool for_offsetof
, cp_id_kind
*idk
,
7547 location_t location
)
7551 bool pseudo_destructor_p
;
7552 tree scope
= NULL_TREE
;
7553 location_t start_loc
= postfix_expression
.get_start ();
7555 /* If this is a `->' operator, dereference the pointer. */
7556 if (token_type
== CPP_DEREF
)
7557 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7558 tf_warning_or_error
);
7559 /* Check to see whether or not the expression is type-dependent and
7560 not the current instantiation. */
7561 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7562 /* The identifier following the `->' or `.' is not qualified. */
7563 parser
->scope
= NULL_TREE
;
7564 parser
->qualifying_scope
= NULL_TREE
;
7565 parser
->object_scope
= NULL_TREE
;
7566 *idk
= CP_ID_KIND_NONE
;
7568 /* Enter the scope corresponding to the type of the object
7569 given by the POSTFIX_EXPRESSION. */
7572 scope
= TREE_TYPE (postfix_expression
);
7573 /* According to the standard, no expression should ever have
7574 reference type. Unfortunately, we do not currently match
7575 the standard in this respect in that our internal representation
7576 of an expression may have reference type even when the standard
7577 says it does not. Therefore, we have to manually obtain the
7578 underlying type here. */
7579 scope
= non_reference (scope
);
7580 /* The type of the POSTFIX_EXPRESSION must be complete. */
7581 /* Unlike the object expression in other contexts, *this is not
7582 required to be of complete type for purposes of class member
7583 access (5.2.5) outside the member function body. */
7584 if (postfix_expression
!= current_class_ref
7585 && scope
!= error_mark_node
7586 && !currently_open_class (scope
))
7588 scope
= complete_type (scope
);
7589 if (!COMPLETE_TYPE_P (scope
)
7590 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7592 return error_mark_node
;
7597 /* Let the name lookup machinery know that we are processing a
7598 class member access expression. */
7599 parser
->context
->object_type
= scope
;
7600 /* If something went wrong, we want to be able to discern that case,
7601 as opposed to the case where there was no SCOPE due to the type
7602 of expression being dependent. */
7604 scope
= error_mark_node
;
7605 /* If the SCOPE was erroneous, make the various semantic analysis
7606 functions exit quickly -- and without issuing additional error
7608 if (scope
== error_mark_node
)
7609 postfix_expression
= error_mark_node
;
7614 /* Tell cp_parser_lookup_name that there was an object, even though it's
7616 parser
->context
->object_type
= unknown_type_node
;
7618 /* Assume this expression is not a pseudo-destructor access. */
7619 pseudo_destructor_p
= false;
7621 /* If the SCOPE is a scalar type, then, if this is a valid program,
7622 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7623 is type dependent, it can be pseudo-destructor-name or something else.
7624 Try to parse it as pseudo-destructor-name first. */
7625 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7630 cp_parser_parse_tentatively (parser
);
7631 /* Parse the pseudo-destructor-name. */
7633 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7636 && (cp_parser_error_occurred (parser
)
7637 || !SCALAR_TYPE_P (type
)))
7638 cp_parser_abort_tentative_parse (parser
);
7639 else if (cp_parser_parse_definitely (parser
))
7641 pseudo_destructor_p
= true;
7643 = finish_pseudo_destructor_expr (postfix_expression
,
7648 if (!pseudo_destructor_p
)
7650 /* If the SCOPE is not a scalar type, we are looking at an
7651 ordinary class member access expression, rather than a
7652 pseudo-destructor-name. */
7654 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7655 /* Parse the id-expression. */
7656 name
= (cp_parser_id_expression
7658 cp_parser_optional_template_keyword (parser
),
7659 /*check_dependency_p=*/true,
7661 /*declarator_p=*/false,
7662 /*optional_p=*/false));
7663 /* In general, build a SCOPE_REF if the member name is qualified.
7664 However, if the name was not dependent and has already been
7665 resolved; there is no need to build the SCOPE_REF. For example;
7667 struct X { void f(); };
7668 template <typename T> void f(T* t) { t->X::f(); }
7670 Even though "t" is dependent, "X::f" is not and has been resolved
7671 to a BASELINK; there is no need to include scope information. */
7673 /* But we do need to remember that there was an explicit scope for
7674 virtual function calls. */
7676 *idk
= CP_ID_KIND_QUALIFIED
;
7678 /* If the name is a template-id that names a type, we will get a
7679 TYPE_DECL here. That is invalid code. */
7680 if (TREE_CODE (name
) == TYPE_DECL
)
7682 error_at (token
->location
, "invalid use of %qD", name
);
7683 postfix_expression
= error_mark_node
;
7687 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7689 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7691 error_at (token
->location
, "%<%D::%D%> is not a class member",
7692 parser
->scope
, name
);
7693 postfix_expression
= error_mark_node
;
7696 name
= build_qualified_name (/*type=*/NULL_TREE
,
7700 parser
->scope
= NULL_TREE
;
7701 parser
->qualifying_scope
= NULL_TREE
;
7702 parser
->object_scope
= NULL_TREE
;
7704 if (parser
->scope
&& name
&& BASELINK_P (name
))
7705 adjust_result_of_qualified_name_lookup
7706 (name
, parser
->scope
, scope
);
7708 = finish_class_member_access_expr (postfix_expression
, name
,
7710 tf_warning_or_error
);
7711 /* Build a location e.g.:
7714 where the caret is at the deref token, ranging from
7715 the start of postfix_expression to the end of the access expr. */
7717 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7718 location_t combined_loc
7719 = make_location (input_location
, start_loc
, end_loc
);
7720 protected_set_expr_location (postfix_expression
, combined_loc
);
7724 /* We no longer need to look up names in the scope of the object on
7725 the left-hand side of the `.' or `->' operator. */
7726 parser
->context
->object_type
= NULL_TREE
;
7728 /* Outside of offsetof, these operators may not appear in
7729 constant-expressions. */
7731 && (cp_parser_non_integral_constant_expression
7732 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7733 postfix_expression
= error_mark_node
;
7735 return postfix_expression
;
7738 /* Parse a parenthesized expression-list.
7741 assignment-expression
7742 expression-list, assignment-expression
7747 identifier, expression-list
7749 CAST_P is true if this expression is the target of a cast.
7751 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7754 WRAP_LOCATIONS_P is true if expressions within this list for which
7755 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7756 their source locations.
7758 Returns a vector of trees. Each element is a representation of an
7759 assignment-expression. NULL is returned if the ( and or ) are
7760 missing. An empty, but allocated, vector is returned on no
7761 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7762 if we are parsing an attribute list for an attribute that wants a
7763 plain identifier argument, normal_attr for an attribute that wants
7764 an expression, or non_attr if we aren't parsing an attribute list. If
7765 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7766 not all of the expressions in the list were constant.
7767 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7768 will be written to with the location of the closing parenthesis. If
7769 an error occurs, it may or may not be written to. */
7771 static vec
<tree
, va_gc
> *
7772 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7773 int is_attribute_list
,
7775 bool allow_expansion_p
,
7776 bool *non_constant_p
,
7777 location_t
*close_paren_loc
,
7778 bool wrap_locations_p
)
7780 vec
<tree
, va_gc
> *expression_list
;
7781 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7782 tree identifier
= NULL_TREE
;
7783 bool saved_greater_than_is_operator_p
;
7785 /* Assume all the expressions will be constant. */
7787 *non_constant_p
= false;
7789 matching_parens parens
;
7790 if (!parens
.require_open (parser
))
7793 expression_list
= make_tree_vector ();
7795 /* Within a parenthesized expression, a `>' token is always
7796 the greater-than operator. */
7797 saved_greater_than_is_operator_p
7798 = parser
->greater_than_is_operator_p
;
7799 parser
->greater_than_is_operator_p
= true;
7801 cp_expr
expr (NULL_TREE
);
7803 /* Consume expressions until there are no more. */
7804 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7807 /* At the beginning of attribute lists, check to see if the
7808 next token is an identifier. */
7809 if (is_attribute_list
== id_attr
7810 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7814 /* Consume the identifier. */
7815 token
= cp_lexer_consume_token (parser
->lexer
);
7816 /* Save the identifier. */
7817 identifier
= token
->u
.value
;
7821 bool expr_non_constant_p
;
7823 /* Parse the next assignment-expression. */
7824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7826 /* A braced-init-list. */
7827 cp_lexer_set_source_position (parser
->lexer
);
7828 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7829 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7830 if (non_constant_p
&& expr_non_constant_p
)
7831 *non_constant_p
= true;
7833 else if (non_constant_p
)
7835 expr
= (cp_parser_constant_expression
7836 (parser
, /*allow_non_constant_p=*/true,
7837 &expr_non_constant_p
));
7838 if (expr_non_constant_p
)
7839 *non_constant_p
= true;
7842 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7846 expr
= instantiate_non_dependent_expr (expr
);
7848 /* If we have an ellipsis, then this is an expression
7850 if (allow_expansion_p
7851 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7853 /* Consume the `...'. */
7854 cp_lexer_consume_token (parser
->lexer
);
7856 /* Build the argument pack. */
7857 expr
= make_pack_expansion (expr
);
7860 if (wrap_locations_p
)
7861 expr
.maybe_add_location_wrapper ();
7863 /* Add it to the list. We add error_mark_node
7864 expressions to the list, so that we can still tell if
7865 the correct form for a parenthesized expression-list
7866 is found. That gives better errors. */
7867 vec_safe_push (expression_list
, expr
.get_value ());
7869 if (expr
== error_mark_node
)
7873 /* After the first item, attribute lists look the same as
7874 expression lists. */
7875 is_attribute_list
= non_attr
;
7878 /* If the next token isn't a `,', then we are done. */
7879 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7882 /* Otherwise, consume the `,' and keep going. */
7883 cp_lexer_consume_token (parser
->lexer
);
7886 if (close_paren_loc
)
7887 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7889 if (!parens
.require_close (parser
))
7894 /* We try and resync to an unnested comma, as that will give the
7895 user better diagnostics. */
7896 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7897 /*recovering=*/true,
7899 /*consume_paren=*/true);
7904 parser
->greater_than_is_operator_p
7905 = saved_greater_than_is_operator_p
;
7910 parser
->greater_than_is_operator_p
7911 = saved_greater_than_is_operator_p
;
7914 vec_safe_insert (expression_list
, 0, identifier
);
7916 return expression_list
;
7919 /* Parse a pseudo-destructor-name.
7921 pseudo-destructor-name:
7922 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7923 :: [opt] nested-name-specifier template template-id :: ~ type-name
7924 :: [opt] nested-name-specifier [opt] ~ type-name
7926 If either of the first two productions is used, sets *SCOPE to the
7927 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7928 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7929 or ERROR_MARK_NODE if the parse fails. */
7932 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7937 bool nested_name_specifier_p
;
7940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7941 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7942 && !type_dependent_expression_p (object
))
7944 if (cxx_dialect
< cxx14
)
7945 pedwarn (input_location
, 0,
7946 "%<~auto%> only available with "
7947 "-std=c++14 or -std=gnu++14");
7948 cp_lexer_consume_token (parser
->lexer
);
7949 cp_lexer_consume_token (parser
->lexer
);
7951 *type
= TREE_TYPE (object
);
7955 /* Assume that things will not work out. */
7956 *type
= error_mark_node
;
7958 /* Look for the optional `::' operator. */
7959 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7960 /* Look for the optional nested-name-specifier. */
7961 nested_name_specifier_p
7962 = (cp_parser_nested_name_specifier_opt (parser
,
7963 /*typename_keyword_p=*/false,
7964 /*check_dependency_p=*/true,
7966 /*is_declaration=*/false)
7968 /* Now, if we saw a nested-name-specifier, we might be doing the
7969 second production. */
7970 if (nested_name_specifier_p
7971 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7973 /* Consume the `template' keyword. */
7974 cp_lexer_consume_token (parser
->lexer
);
7975 /* Parse the template-id. */
7976 cp_parser_template_id (parser
,
7977 /*template_keyword_p=*/true,
7978 /*check_dependency_p=*/false,
7980 /*is_declaration=*/true);
7981 /* Look for the `::' token. */
7982 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7984 /* If the next token is not a `~', then there might be some
7985 additional qualification. */
7986 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7988 /* At this point, we're looking for "type-name :: ~". The type-name
7989 must not be a class-name, since this is a pseudo-destructor. So,
7990 it must be either an enum-name, or a typedef-name -- both of which
7991 are just identifiers. So, we peek ahead to check that the "::"
7992 and "~" tokens are present; if they are not, then we can avoid
7993 calling type_name. */
7994 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7995 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7996 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7998 cp_parser_error (parser
, "non-scalar type");
8002 /* Look for the type-name. */
8003 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8004 if (*scope
== error_mark_node
)
8007 /* Look for the `::' token. */
8008 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8013 /* Look for the `~'. */
8014 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8016 /* Once we see the ~, this has to be a pseudo-destructor. */
8017 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8018 cp_parser_commit_to_topmost_tentative_parse (parser
);
8020 /* Look for the type-name again. We are not responsible for
8021 checking that it matches the first type-name. */
8022 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8025 /* Parse a unary-expression.
8031 unary-operator cast-expression
8032 sizeof unary-expression
8034 alignof ( type-id ) [C++0x]
8041 __extension__ cast-expression
8042 __alignof__ unary-expression
8043 __alignof__ ( type-id )
8044 alignof unary-expression [C++0x]
8045 __real__ cast-expression
8046 __imag__ cast-expression
8048 sizeof ( type-id ) { initializer-list , [opt] }
8049 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8050 __alignof__ ( type-id ) { initializer-list , [opt] }
8052 ADDRESS_P is true iff the unary-expression is appearing as the
8053 operand of the `&' operator. CAST_P is true if this expression is
8054 the target of a cast.
8056 Returns a representation of the expression. */
8059 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8060 bool address_p
, bool cast_p
, bool decltype_p
)
8063 enum tree_code unary_operator
;
8065 /* Peek at the next token. */
8066 token
= cp_lexer_peek_token (parser
->lexer
);
8067 /* Some keywords give away the kind of expression. */
8068 if (token
->type
== CPP_KEYWORD
)
8070 enum rid keyword
= token
->keyword
;
8079 location_t start_loc
= token
->location
;
8081 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8082 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8084 /* Consume the token. */
8085 cp_lexer_consume_token (parser
->lexer
);
8086 /* Parse the operand. */
8087 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8089 if (TYPE_P (operand
))
8090 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8094 /* ISO C++ defines alignof only with types, not with
8095 expressions. So pedwarn if alignof is used with a non-
8096 type expression. However, __alignof__ is ok. */
8098 pedwarn (token
->location
, OPT_Wpedantic
,
8099 "ISO C++ does not allow %<alignof%> "
8102 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8104 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8105 SIZEOF_EXPR with the original operand. */
8106 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8108 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8110 if (!processing_template_decl
&& TYPE_P (operand
))
8112 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8113 build1 (NOP_EXPR
, operand
,
8115 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8118 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8119 TREE_SIDE_EFFECTS (ret
) = 0;
8120 TREE_READONLY (ret
) = 1;
8124 /* Construct a location e.g. :
8127 with start == caret at the start of the "alignof"/"sizeof"
8128 token, with the endpoint at the final closing paren. */
8129 location_t finish_loc
8130 = cp_lexer_previous_token (parser
->lexer
)->location
;
8131 location_t compound_loc
8132 = make_location (start_loc
, start_loc
, finish_loc
);
8134 cp_expr
ret_expr (ret
);
8135 ret_expr
.set_location (compound_loc
);
8136 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8140 case RID_BUILTIN_HAS_ATTRIBUTE
:
8141 return cp_parser_has_attribute_expression (parser
);
8144 return cp_parser_new_expression (parser
);
8147 return cp_parser_delete_expression (parser
);
8151 /* The saved value of the PEDANTIC flag. */
8155 /* Save away the PEDANTIC flag. */
8156 cp_parser_extension_opt (parser
, &saved_pedantic
);
8157 /* Parse the cast-expression. */
8158 expr
= cp_parser_simple_cast_expression (parser
);
8159 /* Restore the PEDANTIC flag. */
8160 pedantic
= saved_pedantic
;
8170 /* Consume the `__real__' or `__imag__' token. */
8171 cp_lexer_consume_token (parser
->lexer
);
8172 /* Parse the cast-expression. */
8173 expression
= cp_parser_simple_cast_expression (parser
);
8174 /* Create the complete representation. */
8175 return build_x_unary_op (token
->location
,
8176 (keyword
== RID_REALPART
8177 ? REALPART_EXPR
: IMAGPART_EXPR
),
8179 tf_warning_or_error
);
8183 case RID_TRANSACTION_ATOMIC
:
8184 case RID_TRANSACTION_RELAXED
:
8185 return cp_parser_transaction_expression (parser
, keyword
);
8190 const char *saved_message
;
8191 bool saved_integral_constant_expression_p
;
8192 bool saved_non_integral_constant_expression_p
;
8193 bool saved_greater_than_is_operator_p
;
8195 location_t start_loc
= token
->location
;
8197 cp_lexer_consume_token (parser
->lexer
);
8198 matching_parens parens
;
8199 parens
.require_open (parser
);
8201 saved_message
= parser
->type_definition_forbidden_message
;
8202 parser
->type_definition_forbidden_message
8203 = G_("types may not be defined in %<noexcept%> expressions");
8205 saved_integral_constant_expression_p
8206 = parser
->integral_constant_expression_p
;
8207 saved_non_integral_constant_expression_p
8208 = parser
->non_integral_constant_expression_p
;
8209 parser
->integral_constant_expression_p
= false;
8211 saved_greater_than_is_operator_p
8212 = parser
->greater_than_is_operator_p
;
8213 parser
->greater_than_is_operator_p
= true;
8215 ++cp_unevaluated_operand
;
8216 ++c_inhibit_evaluation_warnings
;
8217 ++cp_noexcept_operand
;
8218 expr
= cp_parser_expression (parser
);
8219 --cp_noexcept_operand
;
8220 --c_inhibit_evaluation_warnings
;
8221 --cp_unevaluated_operand
;
8223 parser
->greater_than_is_operator_p
8224 = saved_greater_than_is_operator_p
;
8226 parser
->integral_constant_expression_p
8227 = saved_integral_constant_expression_p
;
8228 parser
->non_integral_constant_expression_p
8229 = saved_non_integral_constant_expression_p
;
8231 parser
->type_definition_forbidden_message
= saved_message
;
8233 location_t finish_loc
8234 = cp_lexer_peek_token (parser
->lexer
)->location
;
8235 parens
.require_close (parser
);
8237 /* Construct a location of the form:
8240 with start == caret, finishing at the close-paren. */
8241 location_t noexcept_loc
8242 = make_location (start_loc
, start_loc
, finish_loc
);
8244 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8253 /* Look for the `:: new' and `:: delete', which also signal the
8254 beginning of a new-expression, or delete-expression,
8255 respectively. If the next token is `::', then it might be one of
8257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8261 /* See if the token after the `::' is one of the keywords in
8262 which we're interested. */
8263 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8264 /* If it's `new', we have a new-expression. */
8265 if (keyword
== RID_NEW
)
8266 return cp_parser_new_expression (parser
);
8267 /* Similarly, for `delete'. */
8268 else if (keyword
== RID_DELETE
)
8269 return cp_parser_delete_expression (parser
);
8272 /* Look for a unary operator. */
8273 unary_operator
= cp_parser_unary_operator (token
);
8274 /* The `++' and `--' operators can be handled similarly, even though
8275 they are not technically unary-operators in the grammar. */
8276 if (unary_operator
== ERROR_MARK
)
8278 if (token
->type
== CPP_PLUS_PLUS
)
8279 unary_operator
= PREINCREMENT_EXPR
;
8280 else if (token
->type
== CPP_MINUS_MINUS
)
8281 unary_operator
= PREDECREMENT_EXPR
;
8282 /* Handle the GNU address-of-label extension. */
8283 else if (cp_parser_allow_gnu_extensions_p (parser
)
8284 && token
->type
== CPP_AND_AND
)
8288 location_t start_loc
= token
->location
;
8290 /* Consume the '&&' token. */
8291 cp_lexer_consume_token (parser
->lexer
);
8292 /* Look for the identifier. */
8293 location_t finish_loc
8294 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8295 identifier
= cp_parser_identifier (parser
);
8296 /* Construct a location of the form:
8299 with caret==start at the "&&", finish at the end of the label. */
8300 location_t combined_loc
8301 = make_location (start_loc
, start_loc
, finish_loc
);
8302 /* Create an expression representing the address. */
8303 expression
= finish_label_address_expr (identifier
, combined_loc
);
8304 if (cp_parser_non_integral_constant_expression (parser
,
8306 expression
= error_mark_node
;
8310 if (unary_operator
!= ERROR_MARK
)
8312 cp_expr cast_expression
;
8313 cp_expr expression
= error_mark_node
;
8314 non_integral_constant non_constant_p
= NIC_NONE
;
8315 location_t loc
= token
->location
;
8316 tsubst_flags_t complain
= complain_flags (decltype_p
);
8318 /* Consume the operator token. */
8319 token
= cp_lexer_consume_token (parser
->lexer
);
8320 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8322 /* Parse the cast-expression. */
8324 = cp_parser_cast_expression (parser
,
8325 unary_operator
== ADDR_EXPR
,
8331 OP_TOKEN CAST_EXPRESSION
8332 ^~~~~~~~~~~~~~~~~~~~~~~~~
8333 with start==caret at the operator token, and
8334 extending to the end of the cast_expression. */
8335 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8337 /* Now, build an appropriate representation. */
8338 switch (unary_operator
)
8341 non_constant_p
= NIC_STAR
;
8342 expression
= build_x_indirect_ref (loc
, cast_expression
,
8345 /* TODO: build_x_indirect_ref does not always honor the
8346 location, so ensure it is set. */
8347 expression
.set_location (loc
);
8351 non_constant_p
= NIC_ADDR
;
8354 expression
= build_x_unary_op (loc
, unary_operator
,
8357 /* TODO: build_x_unary_op does not always honor the location,
8358 so ensure it is set. */
8359 expression
.set_location (loc
);
8362 case PREINCREMENT_EXPR
:
8363 case PREDECREMENT_EXPR
:
8364 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8365 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8368 /* Immediately fold negation of a constant, unless the constant is 0
8369 (since -0 == 0) or it would overflow. */
8370 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8373 = tree_strip_any_location_wrapper (cast_expression
);
8374 if (CONSTANT_CLASS_P (stripped_expr
)
8375 && !integer_zerop (stripped_expr
)
8376 && !TREE_OVERFLOW (stripped_expr
))
8378 tree folded
= fold_build1 (unary_operator
,
8379 TREE_TYPE (stripped_expr
),
8381 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8383 expression
= maybe_wrap_with_location (folded
, loc
);
8389 case UNARY_PLUS_EXPR
:
8390 case TRUTH_NOT_EXPR
:
8391 expression
= finish_unary_op_expr (loc
, unary_operator
,
8392 cast_expression
, complain
);
8399 if (non_constant_p
!= NIC_NONE
8400 && cp_parser_non_integral_constant_expression (parser
,
8402 expression
= error_mark_node
;
8407 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8408 /*member_access_only_p=*/false,
8413 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8414 unary-operator, the corresponding tree code is returned. */
8416 static enum tree_code
8417 cp_parser_unary_operator (cp_token
* token
)
8419 switch (token
->type
)
8422 return INDIRECT_REF
;
8428 return UNARY_PLUS_EXPR
;
8434 return TRUTH_NOT_EXPR
;
8437 return BIT_NOT_EXPR
;
8444 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8445 Returns a representation of the expression. */
8448 cp_parser_has_attribute_expression (cp_parser
*parser
)
8450 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8452 /* Consume the __builtin_has_attribute token. */
8453 cp_lexer_consume_token (parser
->lexer
);
8455 matching_parens parens
;
8456 if (!parens
.require_open (parser
))
8457 return error_mark_node
;
8459 /* Types cannot be defined in a `sizeof' expression. Save away the
8461 const char *saved_message
= parser
->type_definition_forbidden_message
;
8462 /* And create the new one. */
8463 const int kwd
= RID_BUILTIN_HAS_ATTRIBUTE
;
8464 char *tmp
= concat ("types may not be defined in %<",
8465 IDENTIFIER_POINTER (ridpointers
[kwd
]),
8466 "%> expressions", NULL
);
8467 parser
->type_definition_forbidden_message
= tmp
;
8469 /* The restrictions on constant-expressions do not apply inside
8470 sizeof expressions. */
8471 bool saved_integral_constant_expression_p
8472 = parser
->integral_constant_expression_p
;
8473 bool saved_non_integral_constant_expression_p
8474 = parser
->non_integral_constant_expression_p
;
8475 parser
->integral_constant_expression_p
= false;
8477 /* Do not actually evaluate the expression. */
8478 ++cp_unevaluated_operand
;
8479 ++c_inhibit_evaluation_warnings
;
8481 tree oper
= NULL_TREE
;
8483 /* We can't be sure yet whether we're looking at a type-id or an
8485 cp_parser_parse_tentatively (parser
);
8487 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8488 parser
->in_type_id_in_expr_p
= true;
8489 /* Look for the type-id. */
8490 oper
= cp_parser_type_id (parser
);
8491 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8493 cp_parser_parse_definitely (parser
);
8495 /* If the type-id production did not work out, then we must be
8496 looking at the unary-expression production. */
8497 if (!oper
|| oper
== error_mark_node
)
8498 oper
= cp_parser_unary_expression (parser
);
8500 STRIP_ANY_LOCATION_WRAPPER (oper
);
8502 /* Go back to evaluating expressions. */
8503 --cp_unevaluated_operand
;
8504 --c_inhibit_evaluation_warnings
;
8506 /* Free the message we created. */
8508 /* And restore the old one. */
8509 parser
->type_definition_forbidden_message
= saved_message
;
8510 parser
->integral_constant_expression_p
8511 = saved_integral_constant_expression_p
;
8512 parser
->non_integral_constant_expression_p
8513 = saved_non_integral_constant_expression_p
;
8515 /* Consume the comma if it's there. */
8516 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8518 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8519 /*consume_paren=*/true);
8520 return error_mark_node
;
8523 /* Parse the attribute specification. */
8525 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8526 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8528 if (oper
!= error_mark_node
)
8530 /* Fold constant expressions used in attributes first. */
8531 cp_check_const_attributes (attr
);
8533 /* Finally, see if OPER has been declared with ATTR. */
8534 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8537 parens
.require_close (parser
);
8541 error_at (atloc
, "expected identifier");
8542 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8545 /* Construct a location e.g. :
8546 __builtin_has_attribute (oper, attr)
8547 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8548 with start == caret at the start of the built-in token,
8549 and with the endpoint at the final closing paren. */
8550 location_t finish_loc
8551 = cp_lexer_previous_token (parser
->lexer
)->location
;
8552 location_t compound_loc
8553 = make_location (start_loc
, start_loc
, finish_loc
);
8555 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8556 ret_expr
.set_location (compound_loc
);
8557 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8561 /* Parse a new-expression.
8564 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8565 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8567 Returns a representation of the expression. */
8570 cp_parser_new_expression (cp_parser
* parser
)
8572 bool global_scope_p
;
8573 vec
<tree
, va_gc
> *placement
;
8575 vec
<tree
, va_gc
> *initializer
;
8576 tree nelts
= NULL_TREE
;
8579 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8581 /* Look for the optional `::' operator. */
8583 = (cp_parser_global_scope_opt (parser
,
8584 /*current_scope_valid_p=*/false)
8586 /* Look for the `new' operator. */
8587 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8588 /* There's no easy way to tell a new-placement from the
8589 `( type-id )' construct. */
8590 cp_parser_parse_tentatively (parser
);
8591 /* Look for a new-placement. */
8592 placement
= cp_parser_new_placement (parser
);
8593 /* If that didn't work out, there's no new-placement. */
8594 if (!cp_parser_parse_definitely (parser
))
8596 if (placement
!= NULL
)
8597 release_tree_vector (placement
);
8601 /* If the next token is a `(', then we have a parenthesized
8603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8606 const char *saved_message
= parser
->type_definition_forbidden_message
;
8608 /* Consume the `('. */
8609 matching_parens parens
;
8610 parens
.consume_open (parser
);
8612 /* Parse the type-id. */
8613 parser
->type_definition_forbidden_message
8614 = G_("types may not be defined in a new-expression");
8616 type_id_in_expr_sentinel
s (parser
);
8617 type
= cp_parser_type_id (parser
);
8619 parser
->type_definition_forbidden_message
= saved_message
;
8621 /* Look for the closing `)'. */
8622 parens
.require_close (parser
);
8623 token
= cp_lexer_peek_token (parser
->lexer
);
8624 /* There should not be a direct-new-declarator in this production,
8625 but GCC used to allowed this, so we check and emit a sensible error
8626 message for this case. */
8627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8629 error_at (token
->location
,
8630 "array bound forbidden after parenthesized type-id");
8631 inform (token
->location
,
8632 "try removing the parentheses around the type-id");
8633 cp_parser_direct_new_declarator (parser
);
8636 /* Otherwise, there must be a new-type-id. */
8638 type
= cp_parser_new_type_id (parser
, &nelts
);
8640 /* If the next token is a `(' or '{', then we have a new-initializer. */
8641 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8642 if (token
->type
== CPP_OPEN_PAREN
8643 || token
->type
== CPP_OPEN_BRACE
)
8644 initializer
= cp_parser_new_initializer (parser
);
8648 /* A new-expression may not appear in an integral constant
8650 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8651 ret
= error_mark_node
;
8652 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8653 of a new-type-id or type-id of a new-expression, the new-expression shall
8654 contain a new-initializer of the form ( assignment-expression )".
8655 Additionally, consistently with the spirit of DR 1467, we want to accept
8656 'new auto { 2 }' too. */
8657 else if ((ret
= type_uses_auto (type
))
8658 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8659 && (vec_safe_length (initializer
) != 1
8660 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8661 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8663 error_at (token
->location
,
8664 "initialization of new-expression for type %<auto%> "
8665 "requires exactly one element");
8666 ret
= error_mark_node
;
8670 /* Construct a location e.g.:
8673 with caret == start at the start of the "new" token, and the end
8674 at the end of the final token we consumed. */
8675 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8676 location_t end_loc
= get_finish (end_tok
->location
);
8677 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8679 /* Create a representation of the new-expression. */
8680 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8681 tf_warning_or_error
);
8682 protected_set_expr_location (ret
, combined_loc
);
8685 if (placement
!= NULL
)
8686 release_tree_vector (placement
);
8687 if (initializer
!= NULL
)
8688 release_tree_vector (initializer
);
8693 /* Parse a new-placement.
8698 Returns the same representation as for an expression-list. */
8700 static vec
<tree
, va_gc
> *
8701 cp_parser_new_placement (cp_parser
* parser
)
8703 vec
<tree
, va_gc
> *expression_list
;
8705 /* Parse the expression-list. */
8706 expression_list
= (cp_parser_parenthesized_expression_list
8707 (parser
, non_attr
, /*cast_p=*/false,
8708 /*allow_expansion_p=*/true,
8709 /*non_constant_p=*/NULL
));
8711 if (expression_list
&& expression_list
->is_empty ())
8712 error ("expected expression-list or type-id");
8714 return expression_list
;
8717 /* Parse a new-type-id.
8720 type-specifier-seq new-declarator [opt]
8722 Returns the TYPE allocated. If the new-type-id indicates an array
8723 type, *NELTS is set to the number of elements in the last array
8724 bound; the TYPE will not include the last array bound. */
8727 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8729 cp_decl_specifier_seq type_specifier_seq
;
8730 cp_declarator
*new_declarator
;
8731 cp_declarator
*declarator
;
8732 cp_declarator
*outer_declarator
;
8733 const char *saved_message
;
8735 /* The type-specifier sequence must not contain type definitions.
8736 (It cannot contain declarations of new types either, but if they
8737 are not definitions we will catch that because they are not
8739 saved_message
= parser
->type_definition_forbidden_message
;
8740 parser
->type_definition_forbidden_message
8741 = G_("types may not be defined in a new-type-id");
8742 /* Parse the type-specifier-seq. */
8743 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8744 /*is_declaration=*/false,
8745 /*is_trailing_return=*/false,
8746 &type_specifier_seq
);
8747 /* Restore the old message. */
8748 parser
->type_definition_forbidden_message
= saved_message
;
8750 if (type_specifier_seq
.type
== error_mark_node
)
8751 return error_mark_node
;
8753 /* Parse the new-declarator. */
8754 new_declarator
= cp_parser_new_declarator_opt (parser
);
8756 /* Determine the number of elements in the last array dimension, if
8759 /* Skip down to the last array dimension. */
8760 declarator
= new_declarator
;
8761 outer_declarator
= NULL
;
8762 while (declarator
&& (declarator
->kind
== cdk_pointer
8763 || declarator
->kind
== cdk_ptrmem
))
8765 outer_declarator
= declarator
;
8766 declarator
= declarator
->declarator
;
8769 && declarator
->kind
== cdk_array
8770 && declarator
->declarator
8771 && declarator
->declarator
->kind
== cdk_array
)
8773 outer_declarator
= declarator
;
8774 declarator
= declarator
->declarator
;
8777 if (declarator
&& declarator
->kind
== cdk_array
)
8779 *nelts
= declarator
->u
.array
.bounds
;
8780 if (*nelts
== error_mark_node
)
8781 *nelts
= integer_one_node
;
8783 if (outer_declarator
)
8784 outer_declarator
->declarator
= declarator
->declarator
;
8786 new_declarator
= NULL
;
8789 return groktypename (&type_specifier_seq
, new_declarator
, false);
8792 /* Parse an (optional) new-declarator.
8795 ptr-operator new-declarator [opt]
8796 direct-new-declarator
8798 Returns the declarator. */
8800 static cp_declarator
*
8801 cp_parser_new_declarator_opt (cp_parser
* parser
)
8803 enum tree_code code
;
8804 tree type
, std_attributes
= NULL_TREE
;
8805 cp_cv_quals cv_quals
;
8807 /* We don't know if there's a ptr-operator next, or not. */
8808 cp_parser_parse_tentatively (parser
);
8809 /* Look for a ptr-operator. */
8810 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8811 /* If that worked, look for more new-declarators. */
8812 if (cp_parser_parse_definitely (parser
))
8814 cp_declarator
*declarator
;
8816 /* Parse another optional declarator. */
8817 declarator
= cp_parser_new_declarator_opt (parser
);
8819 declarator
= cp_parser_make_indirect_declarator
8820 (code
, type
, cv_quals
, declarator
, std_attributes
);
8825 /* If the next token is a `[', there is a direct-new-declarator. */
8826 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8827 return cp_parser_direct_new_declarator (parser
);
8832 /* Parse a direct-new-declarator.
8834 direct-new-declarator:
8836 direct-new-declarator [constant-expression]
8840 static cp_declarator
*
8841 cp_parser_direct_new_declarator (cp_parser
* parser
)
8843 cp_declarator
*declarator
= NULL
;
8850 /* Look for the opening `['. */
8851 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8853 token
= cp_lexer_peek_token (parser
->lexer
);
8854 expression
= cp_parser_expression (parser
);
8855 /* The standard requires that the expression have integral
8856 type. DR 74 adds enumeration types. We believe that the
8857 real intent is that these expressions be handled like the
8858 expression in a `switch' condition, which also allows
8859 classes with a single conversion to integral or
8860 enumeration type. */
8861 if (!processing_template_decl
)
8864 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8869 error_at (token
->location
,
8870 "expression in new-declarator must have integral "
8871 "or enumeration type");
8872 expression
= error_mark_node
;
8876 /* Look for the closing `]'. */
8877 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8879 /* Add this bound to the declarator. */
8880 declarator
= make_array_declarator (declarator
, expression
);
8882 /* If the next token is not a `[', then there are no more
8884 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8891 /* Parse a new-initializer.
8894 ( expression-list [opt] )
8897 Returns a representation of the expression-list. */
8899 static vec
<tree
, va_gc
> *
8900 cp_parser_new_initializer (cp_parser
* parser
)
8902 vec
<tree
, va_gc
> *expression_list
;
8904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8907 bool expr_non_constant_p
;
8908 cp_lexer_set_source_position (parser
->lexer
);
8909 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8910 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8911 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8912 expression_list
= make_tree_vector_single (t
);
8915 expression_list
= (cp_parser_parenthesized_expression_list
8916 (parser
, non_attr
, /*cast_p=*/false,
8917 /*allow_expansion_p=*/true,
8918 /*non_constant_p=*/NULL
));
8920 return expression_list
;
8923 /* Parse a delete-expression.
8926 :: [opt] delete cast-expression
8927 :: [opt] delete [ ] cast-expression
8929 Returns a representation of the expression. */
8932 cp_parser_delete_expression (cp_parser
* parser
)
8934 bool global_scope_p
;
8938 /* Look for the optional `::' operator. */
8940 = (cp_parser_global_scope_opt (parser
,
8941 /*current_scope_valid_p=*/false)
8943 /* Look for the `delete' keyword. */
8944 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8945 /* See if the array syntax is in use. */
8946 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8948 /* Consume the `[' token. */
8949 cp_lexer_consume_token (parser
->lexer
);
8950 /* Look for the `]' token. */
8951 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8952 /* Remember that this is the `[]' construct. */
8958 /* Parse the cast-expression. */
8959 expression
= cp_parser_simple_cast_expression (parser
);
8961 /* A delete-expression may not appear in an integral constant
8963 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8964 return error_mark_node
;
8966 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8967 tf_warning_or_error
);
8970 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8971 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8975 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8977 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8978 switch (token
->type
)
8984 case CPP_CLOSE_SQUARE
:
8985 case CPP_CLOSE_PAREN
:
8986 case CPP_CLOSE_BRACE
:
8987 case CPP_OPEN_BRACE
:
8991 case CPP_DEREF_STAR
:
8999 case CPP_GREATER_EQ
:
9020 case CPP_OPEN_PAREN
:
9021 /* In ((type ()) () the last () isn't a valid cast-expression,
9022 so the whole must be parsed as postfix-expression. */
9023 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9026 case CPP_OPEN_SQUARE
:
9027 /* '[' may start a primary-expression in obj-c++ and in C++11,
9028 as a lambda-expression, eg, '(void)[]{}'. */
9029 if (cxx_dialect
>= cxx11
)
9031 return c_dialect_objc ();
9034 case CPP_MINUS_MINUS
:
9035 /* '++' and '--' may or may not start a cast-expression:
9037 struct T { void operator++(int); };
9038 void f() { (T())++; }
9051 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9052 in the order: const_cast, static_cast, reinterpret_cast.
9054 Don't suggest dynamic_cast.
9056 Return the first legal cast kind found, or NULL otherwise. */
9059 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9063 /* Reuse the parser logic by attempting to build the various kinds of
9064 cast, with "complain" disabled.
9065 Identify the first such cast that is valid. */
9067 /* Don't attempt to run such logic within template processing. */
9068 if (processing_template_decl
)
9071 /* First try const_cast. */
9072 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9073 if (trial
!= error_mark_node
)
9074 return "const_cast";
9076 /* If that fails, try static_cast. */
9077 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9078 if (trial
!= error_mark_node
)
9079 return "static_cast";
9081 /* Finally, try reinterpret_cast. */
9082 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9083 if (trial
!= error_mark_node
)
9084 return "reinterpret_cast";
9086 /* No such cast possible. */
9090 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9091 suggesting how to convert a C-style cast of the form:
9095 to a C++-style cast.
9097 The primary range of RICHLOC is asssumed to be that of the original
9098 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9099 of the parens in the C-style cast. */
9102 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9103 location_t close_paren_loc
, tree orig_expr
,
9106 /* This function is non-trivial, so bail out now if the warning isn't
9107 going to be emitted. */
9108 if (!warn_old_style_cast
)
9111 /* Try to find a legal C++ cast, trying them in order:
9112 const_cast, static_cast, reinterpret_cast. */
9113 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9114 if (!cast_suggestion
)
9117 /* Replace the open paren with "CAST_SUGGESTION<". */
9119 pp_printf (&pp
, "%s<", cast_suggestion
);
9120 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9122 /* Replace the close paren with "> (". */
9123 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9125 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9126 rich_loc
->add_fixit_insert_after (")");
9130 /* Parse a cast-expression.
9134 ( type-id ) cast-expression
9136 ADDRESS_P is true iff the unary-expression is appearing as the
9137 operand of the `&' operator. CAST_P is true if this expression is
9138 the target of a cast.
9140 Returns a representation of the expression. */
9143 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9144 bool decltype_p
, cp_id_kind
* pidk
)
9146 /* If it's a `(', then we might be looking at a cast. */
9147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9149 tree type
= NULL_TREE
;
9150 cp_expr
expr (NULL_TREE
);
9151 int cast_expression
= 0;
9152 const char *saved_message
;
9154 /* There's no way to know yet whether or not this is a cast.
9155 For example, `(int (3))' is a unary-expression, while `(int)
9156 3' is a cast. So, we resort to parsing tentatively. */
9157 cp_parser_parse_tentatively (parser
);
9158 /* Types may not be defined in a cast. */
9159 saved_message
= parser
->type_definition_forbidden_message
;
9160 parser
->type_definition_forbidden_message
9161 = G_("types may not be defined in casts");
9162 /* Consume the `('. */
9163 matching_parens parens
;
9164 cp_token
*open_paren
= parens
.consume_open (parser
);
9165 location_t open_paren_loc
= open_paren
->location
;
9166 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9168 /* A very tricky bit is that `(struct S) { 3 }' is a
9169 compound-literal (which we permit in C++ as an extension).
9170 But, that construct is not a cast-expression -- it is a
9171 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9172 is legal; if the compound-literal were a cast-expression,
9173 you'd need an extra set of parentheses.) But, if we parse
9174 the type-id, and it happens to be a class-specifier, then we
9175 will commit to the parse at that point, because we cannot
9176 undo the action that is done when creating a new class. So,
9177 then we cannot back up and do a postfix-expression.
9179 Another tricky case is the following (c++/29234):
9181 struct S { void operator () (); };
9188 As a type-id we parse the parenthesized S()() as a function
9189 returning a function, groktypename complains and we cannot
9190 back up in this case either.
9192 Therefore, we scan ahead to the closing `)', and check to see
9193 if the tokens after the `)' can start a cast-expression. Otherwise
9194 we are dealing with an unary-expression, a postfix-expression
9197 Yet another tricky case, in C++11, is the following (c++/54891):
9201 The issue is that usually, besides the case of lambda-expressions,
9202 the parenthesized type-id cannot be followed by '[', and, eg, we
9203 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9204 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9205 we don't commit, we try a cast-expression, then an unary-expression.
9207 Save tokens so that we can put them back. */
9208 cp_lexer_save_tokens (parser
->lexer
);
9210 /* We may be looking at a cast-expression. */
9211 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9212 /*consume_paren=*/true))
9214 = cp_parser_tokens_start_cast_expression (parser
);
9216 /* Roll back the tokens we skipped. */
9217 cp_lexer_rollback_tokens (parser
->lexer
);
9218 /* If we aren't looking at a cast-expression, simulate an error so
9219 that the call to cp_parser_error_occurred below returns true. */
9220 if (!cast_expression
)
9221 cp_parser_simulate_error (parser
);
9224 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9225 parser
->in_type_id_in_expr_p
= true;
9226 /* Look for the type-id. */
9227 type
= cp_parser_type_id (parser
);
9228 /* Look for the closing `)'. */
9229 cp_token
*close_paren
= parens
.require_close (parser
);
9231 close_paren_loc
= close_paren
->location
;
9232 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9235 /* Restore the saved message. */
9236 parser
->type_definition_forbidden_message
= saved_message
;
9238 /* At this point this can only be either a cast or a
9239 parenthesized ctor such as `(T ())' that looks like a cast to
9240 function returning T. */
9241 if (!cp_parser_error_occurred (parser
))
9243 /* Only commit if the cast-expression doesn't start with
9244 '++', '--', or '[' in C++11. */
9245 if (cast_expression
> 0)
9246 cp_parser_commit_to_topmost_tentative_parse (parser
);
9248 expr
= cp_parser_cast_expression (parser
,
9249 /*address_p=*/false,
9251 /*decltype_p=*/false,
9254 if (cp_parser_parse_definitely (parser
))
9256 /* Warn about old-style casts, if so requested. */
9257 if (warn_old_style_cast
9258 && !in_system_header_at (input_location
)
9259 && !VOID_TYPE_P (type
)
9260 && current_lang_name
!= lang_name_c
)
9262 gcc_rich_location
rich_loc (input_location
);
9263 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9265 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9266 "use of old-style cast to %q#T", type
);
9269 /* Only type conversions to integral or enumeration types
9270 can be used in constant-expressions. */
9271 if (!cast_valid_in_integral_constant_expression_p (type
)
9272 && cp_parser_non_integral_constant_expression (parser
,
9274 return error_mark_node
;
9276 /* Perform the cast. */
9280 with start==caret at the open paren, extending to the
9282 location_t cast_loc
= make_location (open_paren_loc
,
9284 expr
.get_finish ());
9285 expr
= build_c_cast (cast_loc
, type
, expr
);
9290 cp_parser_abort_tentative_parse (parser
);
9293 /* If we get here, then it's not a cast, so it must be a
9294 unary-expression. */
9295 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9296 cast_p
, decltype_p
);
9299 /* Parse a binary expression of the general form:
9303 pm-expression .* cast-expression
9304 pm-expression ->* cast-expression
9306 multiplicative-expression:
9308 multiplicative-expression * pm-expression
9309 multiplicative-expression / pm-expression
9310 multiplicative-expression % pm-expression
9312 additive-expression:
9313 multiplicative-expression
9314 additive-expression + multiplicative-expression
9315 additive-expression - multiplicative-expression
9319 shift-expression << additive-expression
9320 shift-expression >> additive-expression
9322 relational-expression:
9324 relational-expression < shift-expression
9325 relational-expression > shift-expression
9326 relational-expression <= shift-expression
9327 relational-expression >= shift-expression
9331 relational-expression:
9332 relational-expression <? shift-expression
9333 relational-expression >? shift-expression
9335 equality-expression:
9336 relational-expression
9337 equality-expression == relational-expression
9338 equality-expression != relational-expression
9342 and-expression & equality-expression
9344 exclusive-or-expression:
9346 exclusive-or-expression ^ and-expression
9348 inclusive-or-expression:
9349 exclusive-or-expression
9350 inclusive-or-expression | exclusive-or-expression
9352 logical-and-expression:
9353 inclusive-or-expression
9354 logical-and-expression && inclusive-or-expression
9356 logical-or-expression:
9357 logical-and-expression
9358 logical-or-expression || logical-and-expression
9360 All these are implemented with a single function like:
9363 simple-cast-expression
9364 binary-expression <token> binary-expression
9366 CAST_P is true if this expression is the target of a cast.
9368 The binops_by_token map is used to get the tree codes for each <token> type.
9369 binary-expressions are associated according to a precedence table. */
9371 #define TOKEN_PRECEDENCE(token) \
9372 (((token->type == CPP_GREATER \
9373 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9374 && !parser->greater_than_is_operator_p) \
9375 ? PREC_NOT_OPERATOR \
9376 : binops_by_token[token->type].prec)
9379 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9380 bool no_toplevel_fold_p
,
9382 enum cp_parser_prec prec
,
9385 cp_parser_expression_stack stack
;
9386 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9387 cp_parser_expression_stack_entry current
;
9390 enum tree_code rhs_type
;
9391 enum cp_parser_prec new_prec
, lookahead_prec
;
9394 /* Parse the first expression. */
9395 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9396 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9397 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9398 cast_p
, decltype_p
, pidk
);
9399 current
.prec
= prec
;
9401 if (cp_parser_error_occurred (parser
))
9402 return error_mark_node
;
9406 /* Get an operator token. */
9407 token
= cp_lexer_peek_token (parser
->lexer
);
9409 if (warn_cxx11_compat
9410 && token
->type
== CPP_RSHIFT
9411 && !parser
->greater_than_is_operator_p
)
9413 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9414 "%<>>%> operator is treated"
9415 " as two right angle brackets in C++11"))
9416 inform (token
->location
,
9417 "suggest parentheses around %<>>%> expression");
9420 new_prec
= TOKEN_PRECEDENCE (token
);
9421 if (new_prec
!= PREC_NOT_OPERATOR
9422 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9423 /* This is a fold-expression; handle it later. */
9424 new_prec
= PREC_NOT_OPERATOR
;
9426 /* Popping an entry off the stack means we completed a subexpression:
9427 - either we found a token which is not an operator (`>' where it is not
9428 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9429 will happen repeatedly;
9430 - or, we found an operator which has lower priority. This is the case
9431 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9433 if (new_prec
<= current
.prec
)
9442 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9443 current
.loc
= token
->location
;
9445 /* We used the operator token. */
9446 cp_lexer_consume_token (parser
->lexer
);
9448 /* For "false && x" or "true || x", x will never be executed;
9449 disable warnings while evaluating it. */
9450 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9451 c_inhibit_evaluation_warnings
+=
9452 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9453 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9454 c_inhibit_evaluation_warnings
+=
9455 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9457 /* Extract another operand. It may be the RHS of this expression
9458 or the LHS of a new, higher priority expression. */
9459 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9460 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9461 rhs
= cp_parser_simple_cast_expression (parser
);
9463 /* Get another operator token. Look up its precedence to avoid
9464 building a useless (immediately popped) stack entry for common
9465 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9466 token
= cp_lexer_peek_token (parser
->lexer
);
9467 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9468 if (lookahead_prec
!= PREC_NOT_OPERATOR
9469 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9470 lookahead_prec
= PREC_NOT_OPERATOR
;
9471 if (lookahead_prec
> new_prec
)
9473 /* ... and prepare to parse the RHS of the new, higher priority
9474 expression. Since precedence levels on the stack are
9475 monotonically increasing, we do not have to care about
9480 current
.lhs_type
= rhs_type
;
9481 current
.prec
= new_prec
;
9482 new_prec
= lookahead_prec
;
9486 lookahead_prec
= new_prec
;
9487 /* If the stack is not empty, we have parsed into LHS the right side
9488 (`4' in the example above) of an expression we had suspended.
9489 We can use the information on the stack to recover the LHS (`3')
9490 from the stack together with the tree code (`MULT_EXPR'), and
9491 the precedence of the higher level subexpression
9492 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9493 which will be used to actually build the additive expression. */
9495 rhs_type
= current
.lhs_type
;
9500 /* Undo the disabling of warnings done above. */
9501 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9502 c_inhibit_evaluation_warnings
-=
9503 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9504 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9505 c_inhibit_evaluation_warnings
-=
9506 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9508 if (warn_logical_not_paren
9509 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9510 && current
.lhs_type
== TRUTH_NOT_EXPR
9511 /* Avoid warning for !!x == y. */
9512 && (TREE_CODE (current
.lhs
) != NE_EXPR
9513 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9514 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9515 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9516 /* Avoid warning for !b == y where b is boolean. */
9517 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9518 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9520 /* Avoid warning for !!b == y where b is boolean. */
9521 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9522 || TREE_TYPE (current
.lhs
) == NULL_TREE
9523 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9524 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9525 current
.lhs
, maybe_constant_value (rhs
));
9529 location_t combined_loc
= make_location (current
.loc
,
9530 current
.lhs
.get_start (),
9533 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9534 ERROR_MARK for everything that is not a binary expression.
9535 This makes warn_about_parentheses miss some warnings that
9536 involve unary operators. For unary expressions we should
9537 pass the correct tree_code unless the unary expression was
9538 surrounded by parentheses.
9540 if (no_toplevel_fold_p
9541 && lookahead_prec
<= current
.prec
9544 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9545 current
.lhs
= error_mark_node
;
9549 = build_min (current
.tree_type
,
9550 TREE_CODE_CLASS (current
.tree_type
)
9552 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9553 current
.lhs
.get_value (), rhs
.get_value ());
9554 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9559 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9560 current
.lhs
, current
.lhs_type
,
9561 rhs
, rhs_type
, &overload
,
9562 complain_flags (decltype_p
));
9563 /* TODO: build_x_binary_op doesn't always honor the location. */
9564 current
.lhs
.set_location (combined_loc
);
9566 current
.lhs_type
= current
.tree_type
;
9568 /* If the binary operator required the use of an overloaded operator,
9569 then this expression cannot be an integral constant-expression.
9570 An overloaded operator can be used even if both operands are
9571 otherwise permissible in an integral constant-expression if at
9572 least one of the operands is of enumeration type. */
9575 && cp_parser_non_integral_constant_expression (parser
,
9577 return error_mark_node
;
9584 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9585 bool no_toplevel_fold_p
,
9586 enum cp_parser_prec prec
,
9589 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9590 /*decltype*/false, prec
, pidk
);
9593 /* Parse the `? expression : assignment-expression' part of a
9594 conditional-expression. The LOGICAL_OR_EXPR is the
9595 logical-or-expression that started the conditional-expression.
9596 Returns a representation of the entire conditional-expression.
9598 This routine is used by cp_parser_assignment_expression.
9600 ? expression : assignment-expression
9604 ? : assignment-expression */
9607 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9609 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9610 cp_expr assignment_expr
;
9611 struct cp_token
*token
;
9612 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9614 /* Consume the `?' token. */
9615 cp_lexer_consume_token (parser
->lexer
);
9616 token
= cp_lexer_peek_token (parser
->lexer
);
9617 if (cp_parser_allow_gnu_extensions_p (parser
)
9618 && token
->type
== CPP_COLON
)
9620 pedwarn (token
->location
, OPT_Wpedantic
,
9621 "ISO C++ does not allow ?: with omitted middle operand");
9622 /* Implicit true clause. */
9624 c_inhibit_evaluation_warnings
+=
9625 folded_logical_or_expr
== truthvalue_true_node
;
9626 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9630 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9631 parser
->colon_corrects_to_scope_p
= false;
9632 /* Parse the expression. */
9633 c_inhibit_evaluation_warnings
+=
9634 folded_logical_or_expr
== truthvalue_false_node
;
9635 expr
= cp_parser_expression (parser
);
9636 c_inhibit_evaluation_warnings
+=
9637 ((folded_logical_or_expr
== truthvalue_true_node
)
9638 - (folded_logical_or_expr
== truthvalue_false_node
));
9639 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9642 /* The next token should be a `:'. */
9643 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9644 /* Parse the assignment-expression. */
9645 assignment_expr
= cp_parser_assignment_expression (parser
);
9646 c_inhibit_evaluation_warnings
-=
9647 folded_logical_or_expr
== truthvalue_true_node
;
9650 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9651 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9652 with the caret at the "?", ranging from the start of
9653 the logical_or_expr to the end of the assignment_expr. */
9654 loc
= make_location (loc
,
9655 logical_or_expr
.get_start (),
9656 assignment_expr
.get_finish ());
9658 /* Build the conditional-expression. */
9659 return build_x_conditional_expr (loc
, logical_or_expr
,
9662 tf_warning_or_error
);
9665 /* Parse an assignment-expression.
9667 assignment-expression:
9668 conditional-expression
9669 logical-or-expression assignment-operator assignment_expression
9672 CAST_P is true if this expression is the target of a cast.
9673 DECLTYPE_P is true if this expression is the operand of decltype.
9675 Returns a representation for the expression. */
9678 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9679 bool cast_p
, bool decltype_p
)
9683 /* If the next token is the `throw' keyword, then we're looking at
9684 a throw-expression. */
9685 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9686 expr
= cp_parser_throw_expression (parser
);
9687 /* Otherwise, it must be that we are looking at a
9688 logical-or-expression. */
9691 /* Parse the binary expressions (logical-or-expression). */
9692 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9694 PREC_NOT_OPERATOR
, pidk
);
9695 /* If the next token is a `?' then we're actually looking at a
9696 conditional-expression. */
9697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9698 return cp_parser_question_colon_clause (parser
, expr
);
9701 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9703 /* If it's an assignment-operator, we're using the second
9705 enum tree_code assignment_operator
9706 = cp_parser_assignment_operator_opt (parser
);
9707 if (assignment_operator
!= ERROR_MARK
)
9709 bool non_constant_p
;
9711 /* Parse the right-hand side of the assignment. */
9712 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9715 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9716 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9718 /* An assignment may not appear in a
9719 constant-expression. */
9720 if (cp_parser_non_integral_constant_expression (parser
,
9722 return error_mark_node
;
9723 /* Build the assignment expression. Its default
9727 is the location of the '=' token as the
9728 caret, ranging from the start of the lhs to the
9730 loc
= make_location (loc
,
9733 expr
= build_x_modify_expr (loc
, expr
,
9734 assignment_operator
,
9736 complain_flags (decltype_p
));
9737 /* TODO: build_x_modify_expr doesn't honor the location,
9738 so we must set it here. */
9739 expr
.set_location (loc
);
9747 /* Parse an (optional) assignment-operator.
9749 assignment-operator: one of
9750 = *= /= %= += -= >>= <<= &= ^= |=
9754 assignment-operator: one of
9757 If the next token is an assignment operator, the corresponding tree
9758 code is returned, and the token is consumed. For example, for
9759 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9760 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9761 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9762 operator, ERROR_MARK is returned. */
9764 static enum tree_code
9765 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9770 /* Peek at the next token. */
9771 token
= cp_lexer_peek_token (parser
->lexer
);
9773 switch (token
->type
)
9784 op
= TRUNC_DIV_EXPR
;
9788 op
= TRUNC_MOD_EXPR
;
9820 /* Nothing else is an assignment operator. */
9824 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9825 if (op
!= ERROR_MARK
9826 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9829 /* If it was an assignment operator, consume it. */
9830 if (op
!= ERROR_MARK
)
9831 cp_lexer_consume_token (parser
->lexer
);
9836 /* Parse an expression.
9839 assignment-expression
9840 expression , assignment-expression
9842 CAST_P is true if this expression is the target of a cast.
9843 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9844 except possibly parenthesized or on the RHS of a comma (N3276).
9846 Returns a representation of the expression. */
9849 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9850 bool cast_p
, bool decltype_p
)
9852 cp_expr expression
= NULL_TREE
;
9853 location_t loc
= UNKNOWN_LOCATION
;
9857 cp_expr assignment_expression
;
9859 /* Parse the next assignment-expression. */
9860 assignment_expression
9861 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9863 /* We don't create a temporary for a call that is the immediate operand
9864 of decltype or on the RHS of a comma. But when we see a comma, we
9865 need to create a temporary for a call on the LHS. */
9866 if (decltype_p
&& !processing_template_decl
9867 && TREE_CODE (assignment_expression
) == CALL_EXPR
9868 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9869 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9870 assignment_expression
9871 = build_cplus_new (TREE_TYPE (assignment_expression
),
9872 assignment_expression
, tf_warning_or_error
);
9874 /* If this is the first assignment-expression, we can just
9877 expression
= assignment_expression
;
9880 /* Create a location with caret at the comma, ranging
9881 from the start of the LHS to the end of the RHS. */
9882 loc
= make_location (loc
,
9883 expression
.get_start (),
9884 assignment_expression
.get_finish ());
9885 expression
= build_x_compound_expr (loc
, expression
,
9886 assignment_expression
,
9887 complain_flags (decltype_p
));
9888 expression
.set_location (loc
);
9890 /* If the next token is not a comma, or we're in a fold-expression, then
9891 we are done with the expression. */
9892 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9893 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9895 /* Consume the `,'. */
9896 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9897 cp_lexer_consume_token (parser
->lexer
);
9898 /* A comma operator cannot appear in a constant-expression. */
9899 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9900 expression
= error_mark_node
;
9906 /* Parse a constant-expression.
9908 constant-expression:
9909 conditional-expression
9911 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9912 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9913 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9914 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9915 only parse a conditional-expression, otherwise parse an
9916 assignment-expression. See below for rationale. */
9919 cp_parser_constant_expression (cp_parser
* parser
,
9920 bool allow_non_constant_p
,
9921 bool *non_constant_p
,
9924 bool saved_integral_constant_expression_p
;
9925 bool saved_allow_non_integral_constant_expression_p
;
9926 bool saved_non_integral_constant_expression_p
;
9929 /* It might seem that we could simply parse the
9930 conditional-expression, and then check to see if it were
9931 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9932 one that the compiler can figure out is constant, possibly after
9933 doing some simplifications or optimizations. The standard has a
9934 precise definition of constant-expression, and we must honor
9935 that, even though it is somewhat more restrictive.
9941 is not a legal declaration, because `(2, 3)' is not a
9942 constant-expression. The `,' operator is forbidden in a
9943 constant-expression. However, GCC's constant-folding machinery
9944 will fold this operation to an INTEGER_CST for `3'. */
9946 /* Save the old settings. */
9947 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9948 saved_allow_non_integral_constant_expression_p
9949 = parser
->allow_non_integral_constant_expression_p
;
9950 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9951 /* We are now parsing a constant-expression. */
9952 parser
->integral_constant_expression_p
= true;
9953 parser
->allow_non_integral_constant_expression_p
9954 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9955 parser
->non_integral_constant_expression_p
= false;
9956 /* Although the grammar says "conditional-expression", when not STRICT_P,
9957 we parse an "assignment-expression", which also permits
9958 "throw-expression" and the use of assignment operators. In the case
9959 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9960 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9961 actually essential that we look for an assignment-expression.
9962 For example, cp_parser_initializer_clauses uses this function to
9963 determine whether a particular assignment-expression is in fact
9967 /* Parse the binary expressions (logical-or-expression). */
9968 expression
= cp_parser_binary_expression (parser
, false, false, false,
9969 PREC_NOT_OPERATOR
, NULL
);
9970 /* If the next token is a `?' then we're actually looking at
9971 a conditional-expression; otherwise we're done. */
9972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9973 expression
= cp_parser_question_colon_clause (parser
, expression
);
9976 expression
= cp_parser_assignment_expression (parser
);
9977 /* Restore the old settings. */
9978 parser
->integral_constant_expression_p
9979 = saved_integral_constant_expression_p
;
9980 parser
->allow_non_integral_constant_expression_p
9981 = saved_allow_non_integral_constant_expression_p
;
9982 if (cxx_dialect
>= cxx11
)
9984 /* Require an rvalue constant expression here; that's what our
9985 callers expect. Reference constant expressions are handled
9986 separately in e.g. cp_parser_template_argument. */
9987 tree decay
= expression
;
9988 if (TREE_TYPE (expression
)
9989 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9990 decay
= build_address (expression
);
9991 bool is_const
= potential_rvalue_constant_expression (decay
);
9992 parser
->non_integral_constant_expression_p
= !is_const
;
9993 if (!is_const
&& !allow_non_constant_p
)
9994 require_potential_rvalue_constant_expression (decay
);
9996 if (allow_non_constant_p
)
9997 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9998 parser
->non_integral_constant_expression_p
9999 = saved_non_integral_constant_expression_p
;
10004 /* Parse __builtin_offsetof.
10006 offsetof-expression:
10007 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10009 offsetof-member-designator:
10011 | offsetof-member-designator "." id-expression
10012 | offsetof-member-designator "[" expression "]"
10013 | offsetof-member-designator "->" id-expression */
10016 cp_parser_builtin_offsetof (cp_parser
*parser
)
10018 int save_ice_p
, save_non_ice_p
;
10023 location_t finish_loc
;
10025 /* We're about to accept non-integral-constant things, but will
10026 definitely yield an integral constant expression. Save and
10027 restore these values around our local parsing. */
10028 save_ice_p
= parser
->integral_constant_expression_p
;
10029 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10031 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10033 /* Consume the "__builtin_offsetof" token. */
10034 cp_lexer_consume_token (parser
->lexer
);
10035 /* Consume the opening `('. */
10036 matching_parens parens
;
10037 parens
.require_open (parser
);
10038 /* Parse the type-id. */
10039 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10041 const char *saved_message
= parser
->type_definition_forbidden_message
;
10042 parser
->type_definition_forbidden_message
10043 = G_("types may not be defined within __builtin_offsetof");
10044 type
= cp_parser_type_id (parser
);
10045 parser
->type_definition_forbidden_message
= saved_message
;
10047 /* Look for the `,'. */
10048 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10049 token
= cp_lexer_peek_token (parser
->lexer
);
10051 /* Build the (type *)null that begins the traditional offsetof macro. */
10053 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10054 tf_warning_or_error
);
10056 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10057 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10058 true, &dummy
, token
->location
);
10061 token
= cp_lexer_peek_token (parser
->lexer
);
10062 switch (token
->type
)
10064 case CPP_OPEN_SQUARE
:
10065 /* offsetof-member-designator "[" expression "]" */
10066 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10071 /* offsetof-member-designator "->" identifier */
10072 expr
= grok_array_decl (token
->location
, expr
,
10073 integer_zero_node
, false);
10077 /* offsetof-member-designator "." identifier */
10078 cp_lexer_consume_token (parser
->lexer
);
10079 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10080 expr
, true, &dummy
,
10084 case CPP_CLOSE_PAREN
:
10085 /* Consume the ")" token. */
10086 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10087 cp_lexer_consume_token (parser
->lexer
);
10091 /* Error. We know the following require will fail, but
10092 that gives the proper error message. */
10093 parens
.require_close (parser
);
10094 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10095 expr
= error_mark_node
;
10101 /* Make a location of the form:
10102 __builtin_offsetof (struct s, f)
10103 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10104 with caret at the type-id, ranging from the start of the
10105 "_builtin_offsetof" token to the close paren. */
10106 loc
= make_location (loc
, start_loc
, finish_loc
);
10107 /* The result will be an INTEGER_CST, so we need to explicitly
10108 preserve the location. */
10109 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10112 parser
->integral_constant_expression_p
= save_ice_p
;
10113 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10115 expr
= expr
.maybe_add_location_wrapper ();
10119 /* Parse a trait expression.
10121 Returns a representation of the expression, the underlying type
10122 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10125 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10127 cp_trait_kind kind
;
10128 tree type1
, type2
= NULL_TREE
;
10129 bool binary
= false;
10130 bool variadic
= false;
10134 case RID_HAS_NOTHROW_ASSIGN
:
10135 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10137 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10138 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10140 case RID_HAS_NOTHROW_COPY
:
10141 kind
= CPTK_HAS_NOTHROW_COPY
;
10143 case RID_HAS_TRIVIAL_ASSIGN
:
10144 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10146 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10147 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10149 case RID_HAS_TRIVIAL_COPY
:
10150 kind
= CPTK_HAS_TRIVIAL_COPY
;
10152 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10153 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10155 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10156 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10158 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10159 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10161 case RID_IS_ABSTRACT
:
10162 kind
= CPTK_IS_ABSTRACT
;
10164 case RID_IS_AGGREGATE
:
10165 kind
= CPTK_IS_AGGREGATE
;
10167 case RID_IS_BASE_OF
:
10168 kind
= CPTK_IS_BASE_OF
;
10172 kind
= CPTK_IS_CLASS
;
10175 kind
= CPTK_IS_EMPTY
;
10178 kind
= CPTK_IS_ENUM
;
10181 kind
= CPTK_IS_FINAL
;
10183 case RID_IS_LITERAL_TYPE
:
10184 kind
= CPTK_IS_LITERAL_TYPE
;
10187 kind
= CPTK_IS_POD
;
10189 case RID_IS_POLYMORPHIC
:
10190 kind
= CPTK_IS_POLYMORPHIC
;
10192 case RID_IS_SAME_AS
:
10193 kind
= CPTK_IS_SAME_AS
;
10196 case RID_IS_STD_LAYOUT
:
10197 kind
= CPTK_IS_STD_LAYOUT
;
10199 case RID_IS_TRIVIAL
:
10200 kind
= CPTK_IS_TRIVIAL
;
10202 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10203 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10206 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10207 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10210 case RID_IS_TRIVIALLY_COPYABLE
:
10211 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10214 kind
= CPTK_IS_UNION
;
10216 case RID_UNDERLYING_TYPE
:
10217 kind
= CPTK_UNDERLYING_TYPE
;
10222 case RID_DIRECT_BASES
:
10223 kind
= CPTK_DIRECT_BASES
;
10225 case RID_IS_ASSIGNABLE
:
10226 kind
= CPTK_IS_ASSIGNABLE
;
10229 case RID_IS_CONSTRUCTIBLE
:
10230 kind
= CPTK_IS_CONSTRUCTIBLE
;
10234 gcc_unreachable ();
10237 /* Get location of initial token. */
10238 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10240 /* Consume the token. */
10241 cp_lexer_consume_token (parser
->lexer
);
10243 matching_parens parens
;
10244 parens
.require_open (parser
);
10247 type_id_in_expr_sentinel
s (parser
);
10248 type1
= cp_parser_type_id (parser
);
10251 if (type1
== error_mark_node
)
10252 return error_mark_node
;
10256 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10259 type_id_in_expr_sentinel
s (parser
);
10260 type2
= cp_parser_type_id (parser
);
10263 if (type2
== error_mark_node
)
10264 return error_mark_node
;
10268 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10270 cp_lexer_consume_token (parser
->lexer
);
10271 tree elt
= cp_parser_type_id (parser
);
10272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10274 cp_lexer_consume_token (parser
->lexer
);
10275 elt
= make_pack_expansion (elt
);
10277 if (elt
== error_mark_node
)
10278 return error_mark_node
;
10279 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10283 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10284 parens
.require_close (parser
);
10286 /* Construct a location of the form:
10287 __is_trivially_copyable(_Tp)
10288 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10289 with start == caret, finishing at the close-paren. */
10290 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10292 /* Complete the trait expression, which may mean either processing
10293 the trait expr now or saving it for template instantiation. */
10296 case CPTK_UNDERLYING_TYPE
:
10297 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10299 return cp_expr (finish_bases (type1
, false), trait_loc
);
10300 case CPTK_DIRECT_BASES
:
10301 return cp_expr (finish_bases (type1
, true), trait_loc
);
10303 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10307 /* Parse a lambda expression.
10310 lambda-introducer lambda-declarator [opt] compound-statement
10312 Returns a representation of the expression. */
10315 cp_parser_lambda_expression (cp_parser
* parser
)
10317 tree lambda_expr
= build_lambda_expr ();
10320 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10321 cp_token_position start
= 0;
10323 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10325 if (cxx_dialect
>= cxx2a
)
10326 /* C++20 allows lambdas in unevaluated context. */;
10327 else if (cp_unevaluated_operand
)
10329 if (!token
->error_reported
)
10331 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10332 "lambda-expression in unevaluated context"
10333 " only available with -std=c++2a or -std=gnu++2a");
10334 token
->error_reported
= true;
10338 else if (parser
->in_template_argument_list_p
)
10340 if (!token
->error_reported
)
10342 error_at (token
->location
, "lambda-expression in template-argument"
10343 " only available with -std=c++2a or -std=gnu++2a");
10344 token
->error_reported
= true;
10349 /* We may be in the middle of deferred access check. Disable
10351 push_deferring_access_checks (dk_no_deferred
);
10353 cp_parser_lambda_introducer (parser
, lambda_expr
);
10354 if (cp_parser_error_occurred (parser
))
10355 return error_mark_node
;
10357 type
= begin_lambda_type (lambda_expr
);
10358 if (type
== error_mark_node
)
10359 return error_mark_node
;
10361 record_lambda_scope (lambda_expr
);
10363 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10364 determine_visibility (TYPE_NAME (type
));
10366 /* Now that we've started the type, add the capture fields for any
10367 explicit captures. */
10368 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10371 /* Inside the class, surrounding template-parameter-lists do not apply. */
10372 unsigned int saved_num_template_parameter_lists
10373 = parser
->num_template_parameter_lists
;
10374 unsigned char in_statement
= parser
->in_statement
;
10375 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10376 bool fully_implicit_function_template_p
10377 = parser
->fully_implicit_function_template_p
;
10378 tree implicit_template_parms
= parser
->implicit_template_parms
;
10379 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10380 bool auto_is_implicit_function_template_parm_p
10381 = parser
->auto_is_implicit_function_template_parm_p
;
10383 parser
->num_template_parameter_lists
= 0;
10384 parser
->in_statement
= 0;
10385 parser
->in_switch_statement_p
= false;
10386 parser
->fully_implicit_function_template_p
= false;
10387 parser
->implicit_template_parms
= 0;
10388 parser
->implicit_template_scope
= 0;
10389 parser
->auto_is_implicit_function_template_parm_p
= false;
10391 /* By virtue of defining a local class, a lambda expression has access to
10392 the private variables of enclosing classes. */
10394 if (cp_parser_start_tentative_firewall (parser
))
10397 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10399 if (ok
&& cp_parser_error_occurred (parser
))
10404 cp_parser_lambda_body (parser
, lambda_expr
);
10406 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10408 if (cp_parser_skip_to_closing_brace (parser
))
10409 cp_lexer_consume_token (parser
->lexer
);
10412 /* The capture list was built up in reverse order; fix that now. */
10413 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10414 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10417 maybe_add_lambda_conv_op (type
);
10419 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10421 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10422 parser
->in_statement
= in_statement
;
10423 parser
->in_switch_statement_p
= in_switch_statement_p
;
10424 parser
->fully_implicit_function_template_p
10425 = fully_implicit_function_template_p
;
10426 parser
->implicit_template_parms
= implicit_template_parms
;
10427 parser
->implicit_template_scope
= implicit_template_scope
;
10428 parser
->auto_is_implicit_function_template_parm_p
10429 = auto_is_implicit_function_template_parm_p
;
10432 /* This field is only used during parsing of the lambda. */
10433 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10435 /* This lambda shouldn't have any proxies left at this point. */
10436 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10437 /* And now that we're done, push proxies for an enclosing lambda. */
10438 insert_pending_capture_proxies ();
10440 /* Update the lambda expression to a range. */
10441 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10442 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10444 end_tok
->location
);
10447 lambda_expr
= build_lambda_object (lambda_expr
);
10449 lambda_expr
= error_mark_node
;
10451 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10453 pop_deferring_access_checks ();
10455 return lambda_expr
;
10458 /* Parse the beginning of a lambda expression.
10461 [ lambda-capture [opt] ]
10463 LAMBDA_EXPR is the current representation of the lambda expression. */
10466 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10468 /* Need commas after the first capture. */
10471 /* Eat the leading `['. */
10472 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10474 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10476 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10477 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10478 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10479 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10481 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10483 cp_lexer_consume_token (parser
->lexer
);
10486 if (!(at_function_scope_p () || parsing_nsdmi ()))
10487 error ("non-local lambda expression cannot have a capture-default");
10490 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10492 cp_token
* capture_token
;
10494 tree capture_init_expr
;
10495 cp_id_kind idk
= CP_ID_KIND_NONE
;
10496 bool explicit_init_p
= false;
10498 enum capture_kind_type
10503 enum capture_kind_type capture_kind
= BY_COPY
;
10505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10507 error ("expected end of capture-list");
10514 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10516 /* Possibly capture `this'. */
10517 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10519 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10520 if (cxx_dialect
< cxx2a
10521 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10522 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10523 "with by-copy capture default");
10524 cp_lexer_consume_token (parser
->lexer
);
10525 add_capture (lambda_expr
,
10526 /*id=*/this_identifier
,
10527 /*initializer=*/finish_this_expr (),
10528 /*by_reference_p=*/true,
10533 /* Possibly capture `*this'. */
10534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10535 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10537 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10538 if (cxx_dialect
< cxx17
)
10539 pedwarn (loc
, 0, "%<*this%> capture only available with "
10540 "-std=c++17 or -std=gnu++17");
10541 cp_lexer_consume_token (parser
->lexer
);
10542 cp_lexer_consume_token (parser
->lexer
);
10543 add_capture (lambda_expr
,
10544 /*id=*/this_identifier
,
10545 /*initializer=*/finish_this_expr (),
10546 /*by_reference_p=*/false,
10551 bool init_pack_expansion
= false;
10552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10554 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10555 if (cxx_dialect
< cxx2a
)
10556 pedwarn (loc
, 0, "pack init-capture only available with "
10557 "-std=c++2a or -std=gnu++2a");
10558 cp_lexer_consume_token (parser
->lexer
);
10559 init_pack_expansion
= true;
10562 /* Remember whether we want to capture as a reference or not. */
10563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10565 capture_kind
= BY_REFERENCE
;
10566 cp_lexer_consume_token (parser
->lexer
);
10569 /* Get the identifier. */
10570 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10571 capture_id
= cp_parser_identifier (parser
);
10573 if (capture_id
== error_mark_node
)
10574 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10575 delimiters, but I modified this to stop on unnested ']' as well. It
10576 was already changed to stop on unnested '}', so the
10577 "closing_parenthesis" name is no more misleading with my change. */
10579 cp_parser_skip_to_closing_parenthesis (parser
,
10580 /*recovering=*/true,
10582 /*consume_paren=*/true);
10586 /* Find the initializer for this capture. */
10587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10588 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10589 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10591 bool direct
, non_constant
;
10592 /* An explicit initializer exists. */
10593 if (cxx_dialect
< cxx14
)
10594 pedwarn (input_location
, 0,
10595 "lambda capture initializers "
10596 "only available with -std=c++14 or -std=gnu++14");
10597 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10598 &non_constant
, true);
10599 explicit_init_p
= true;
10600 if (capture_init_expr
== NULL_TREE
)
10602 error ("empty initializer for lambda init-capture");
10603 capture_init_expr
= error_mark_node
;
10605 if (init_pack_expansion
)
10606 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10610 const char* error_msg
;
10612 /* Turn the identifier into an id-expression. */
10614 = cp_parser_lookup_name_simple (parser
, capture_id
,
10615 capture_token
->location
);
10617 if (capture_init_expr
== error_mark_node
)
10619 unqualified_name_lookup_error (capture_id
);
10622 else if (!VAR_P (capture_init_expr
)
10623 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10625 error_at (capture_token
->location
,
10626 "capture of non-variable %qE",
10627 capture_init_expr
);
10628 if (DECL_P (capture_init_expr
))
10629 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10630 "%q#D declared here", capture_init_expr
);
10633 if (VAR_P (capture_init_expr
)
10634 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10636 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10637 "%qD with non-automatic storage duration",
10638 capture_init_expr
))
10639 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10640 "%q#D declared here", capture_init_expr
);
10645 = finish_id_expression
10650 /*integral_constant_expression_p=*/false,
10651 /*allow_non_integral_constant_expression_p=*/false,
10652 /*non_integral_constant_expression_p=*/NULL
,
10653 /*template_p=*/false,
10655 /*address_p=*/false,
10656 /*template_arg_p=*/false,
10658 capture_token
->location
);
10660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10662 cp_lexer_consume_token (parser
->lexer
);
10663 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10667 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10668 && !explicit_init_p
)
10670 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10671 && capture_kind
== BY_COPY
)
10672 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10673 "of %qD redundant with by-copy capture default",
10675 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10676 && capture_kind
== BY_REFERENCE
)
10677 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10678 "capture of %qD redundant with by-reference capture "
10679 "default", capture_id
);
10682 add_capture (lambda_expr
,
10685 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10688 /* If there is any qualification still in effect, clear it
10689 now; we will be starting fresh with the next capture. */
10690 parser
->scope
= NULL_TREE
;
10691 parser
->qualifying_scope
= NULL_TREE
;
10692 parser
->object_scope
= NULL_TREE
;
10695 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10698 /* Parse the (optional) middle of a lambda expression.
10701 < template-parameter-list [opt] >
10702 ( parameter-declaration-clause [opt] )
10703 attribute-specifier [opt]
10704 decl-specifier-seq [opt]
10705 exception-specification [opt]
10706 lambda-return-type-clause [opt]
10708 LAMBDA_EXPR is the current representation of the lambda expression. */
10711 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10713 /* 5.1.1.4 of the standard says:
10714 If a lambda-expression does not include a lambda-declarator, it is as if
10715 the lambda-declarator were ().
10716 This means an empty parameter list, no attributes, and no exception
10718 tree param_list
= void_list_node
;
10719 tree attributes
= NULL_TREE
;
10720 tree exception_spec
= NULL_TREE
;
10721 tree template_param_list
= NULL_TREE
;
10722 tree tx_qual
= NULL_TREE
;
10723 tree return_type
= NULL_TREE
;
10724 cp_decl_specifier_seq lambda_specs
;
10725 clear_decl_specs (&lambda_specs
);
10727 /* The template-parameter-list is optional, but must begin with
10728 an opening angle if present. */
10729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10731 if (cxx_dialect
< cxx14
)
10732 pedwarn (parser
->lexer
->next_token
->location
, 0,
10733 "lambda templates are only available with "
10734 "-std=c++14 or -std=gnu++14");
10735 else if (cxx_dialect
< cxx2a
)
10736 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10737 "lambda templates are only available with "
10738 "-std=c++2a or -std=gnu++2a");
10740 cp_lexer_consume_token (parser
->lexer
);
10742 template_param_list
= cp_parser_template_parameter_list (parser
);
10744 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10746 /* We just processed one more parameter list. */
10747 ++parser
->num_template_parameter_lists
;
10750 /* The parameter-declaration-clause is optional (unless
10751 template-parameter-list was given), but must begin with an
10752 opening parenthesis if present. */
10753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10755 matching_parens parens
;
10756 parens
.consume_open (parser
);
10758 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10760 /* Parse parameters. */
10762 = cp_parser_parameter_declaration_clause
10763 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10765 /* Default arguments shall not be specified in the
10766 parameter-declaration-clause of a lambda-declarator. */
10767 if (cxx_dialect
< cxx14
)
10768 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10769 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10770 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10771 "default argument specified for lambda parameter");
10773 parens
.require_close (parser
);
10775 /* In the decl-specifier-seq of the lambda-declarator, each
10776 decl-specifier shall either be mutable or constexpr. */
10777 int declares_class_or_enum
;
10778 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10779 cp_parser_decl_specifier_seq (parser
,
10780 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10781 &lambda_specs
, &declares_class_or_enum
);
10782 if (lambda_specs
.storage_class
== sc_mutable
)
10784 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10785 if (lambda_specs
.conflicting_specifiers_p
)
10786 error_at (lambda_specs
.locations
[ds_storage_class
],
10787 "duplicate %<mutable%>");
10790 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10792 /* Parse optional exception specification. */
10793 exception_spec
= cp_parser_exception_specification_opt (parser
);
10795 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10797 /* Parse optional trailing return type. */
10798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10800 cp_lexer_consume_token (parser
->lexer
);
10801 return_type
= cp_parser_trailing_type_id (parser
);
10804 /* The function parameters must be in scope all the way until after the
10805 trailing-return-type in case of decltype. */
10806 pop_bindings_and_leave_scope ();
10808 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10809 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10811 /* Create the function call operator.
10813 Messing with declarators like this is no uglier than building up the
10814 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10817 cp_decl_specifier_seq return_type_specs
;
10818 cp_declarator
* declarator
;
10823 clear_decl_specs (&return_type_specs
);
10824 return_type_specs
.type
= make_auto ();
10826 if (lambda_specs
.locations
[ds_constexpr
])
10828 if (cxx_dialect
>= cxx17
)
10829 return_type_specs
.locations
[ds_constexpr
]
10830 = lambda_specs
.locations
[ds_constexpr
];
10832 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10833 "lambda only available with -std=c++17 or -std=gnu++17");
10836 p
= obstack_alloc (&declarator_obstack
, 0);
10838 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10839 LAMBDA_EXPR_LOCATION (lambda_expr
));
10841 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10842 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10843 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10844 VIRT_SPEC_UNSPECIFIED
,
10849 /*requires_clause*/NULL_TREE
);
10850 declarator
->std_attributes
= attributes
;
10852 fco
= grokmethod (&return_type_specs
,
10855 if (fco
!= error_mark_node
)
10857 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10858 DECL_ARTIFICIAL (fco
) = 1;
10859 /* Give the object parameter a different name. */
10860 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10861 DECL_LAMBDA_FUNCTION (fco
) = 1;
10863 if (template_param_list
)
10865 fco
= finish_member_template_decl (fco
);
10866 finish_template_decl (template_param_list
);
10867 --parser
->num_template_parameter_lists
;
10869 else if (parser
->fully_implicit_function_template_p
)
10870 fco
= finish_fully_implicit_template (parser
, fco
);
10872 finish_member_declaration (fco
);
10874 obstack_free (&declarator_obstack
, p
);
10876 return (fco
!= error_mark_node
);
10880 /* Parse the body of a lambda expression, which is simply
10884 but which requires special handling.
10885 LAMBDA_EXPR is the current representation of the lambda expression. */
10888 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10890 bool nested
= (current_function_decl
!= NULL_TREE
);
10891 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10892 bool in_function_body
= parser
->in_function_body
;
10894 /* The body of a lambda-expression is not a subexpression of the enclosing
10899 push_function_context ();
10901 /* Still increment function_depth so that we don't GC in the
10902 middle of an expression. */
10905 vec
<tree
> omp_privatization_save
;
10906 save_omp_privatization_clauses (omp_privatization_save
);
10907 /* Clear this in case we're in the middle of a default argument. */
10908 parser
->local_variables_forbidden_p
= false;
10909 parser
->in_function_body
= true;
10912 local_specialization_stack
s (lss_copy
);
10913 tree fco
= lambda_function (lambda_expr
);
10914 tree body
= start_lambda_function (fco
, lambda_expr
);
10915 matching_braces braces
;
10917 if (braces
.require_open (parser
))
10919 tree compound_stmt
= begin_compound_stmt (0);
10921 /* Originally C++11 required us to peek for 'return expr'; and
10922 process it specially here to deduce the return type. N3638
10923 removed the need for that. */
10925 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10926 cp_parser_label_declaration (parser
);
10927 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10928 braces
.require_close (parser
);
10930 finish_compound_stmt (compound_stmt
);
10933 finish_lambda_function (body
);
10936 restore_omp_privatization_clauses (omp_privatization_save
);
10937 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10938 parser
->in_function_body
= in_function_body
;
10940 pop_function_context();
10945 /* Statements [gram.stmt.stmt] */
10947 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10950 add_debug_begin_stmt (location_t loc
)
10952 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10954 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10955 /* A concept is never expanded normally. */
10958 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10959 SET_EXPR_LOCATION (stmt
, loc
);
10963 /* Parse a statement.
10967 expression-statement
10969 selection-statement
10970 iteration-statement
10972 declaration-statement
10979 attribute-specifier-seq (opt) expression-statement
10980 attribute-specifier-seq (opt) compound-statement
10981 attribute-specifier-seq (opt) selection-statement
10982 attribute-specifier-seq (opt) iteration-statement
10983 attribute-specifier-seq (opt) jump-statement
10984 declaration-statement
10985 attribute-specifier-seq (opt) try-block
10988 expression-statement
10996 IN_COMPOUND is true when the statement is nested inside a
10997 cp_parser_compound_statement; this matters for certain pragmas.
10999 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11000 is a (possibly labeled) if statement which is not enclosed in braces
11001 and has an else clause. This is used to implement -Wparentheses.
11003 CHAIN is a vector of if-else-if conditions. */
11006 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11007 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11008 location_t
*loc_after_labels
)
11010 tree statement
, std_attrs
= NULL_TREE
;
11012 location_t statement_location
, attrs_location
;
11017 /* There is no statement yet. */
11018 statement
= NULL_TREE
;
11020 saved_token_sentinel
saved_tokens (parser
->lexer
);
11021 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
11022 if (c_dialect_objc ())
11023 /* In obj-c++, seeing '[[' might be the either the beginning of
11024 c++11 attributes, or a nested objc-message-expression. So
11025 let's parse the c++11 attributes tentatively. */
11026 cp_parser_parse_tentatively (parser
);
11027 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11028 if (c_dialect_objc ())
11030 if (!cp_parser_parse_definitely (parser
))
11031 std_attrs
= NULL_TREE
;
11034 /* Peek at the next token. */
11035 token
= cp_lexer_peek_token (parser
->lexer
);
11036 /* Remember the location of the first token in the statement. */
11037 cp_token
*statement_token
= token
;
11038 statement_location
= token
->location
;
11039 add_debug_begin_stmt (statement_location
);
11040 /* If this is a keyword, then that will often determine what kind of
11041 statement we have. */
11042 if (token
->type
== CPP_KEYWORD
)
11044 enum rid keyword
= token
->keyword
;
11050 /* Looks like a labeled-statement with a case label.
11051 Parse the label, and then use tail recursion to parse
11053 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11054 in_compound
= false;
11059 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11060 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11066 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11067 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11074 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11075 statement
= cp_parser_jump_statement (parser
);
11078 /* Objective-C++ exception-handling constructs. */
11081 case RID_AT_FINALLY
:
11082 case RID_AT_SYNCHRONIZED
:
11084 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11085 statement
= cp_parser_objc_statement (parser
);
11089 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11090 statement
= cp_parser_try_block (parser
);
11093 case RID_NAMESPACE
:
11094 /* This must be a namespace alias definition. */
11095 if (std_attrs
!= NULL_TREE
)
11097 /* Attributes should be parsed as part of the the
11098 declaration, so let's un-parse them. */
11099 saved_tokens
.rollback();
11100 std_attrs
= NULL_TREE
;
11102 cp_parser_declaration_statement (parser
);
11105 case RID_TRANSACTION_ATOMIC
:
11106 case RID_TRANSACTION_RELAXED
:
11107 case RID_SYNCHRONIZED
:
11108 case RID_ATOMIC_NOEXCEPT
:
11109 case RID_ATOMIC_CANCEL
:
11110 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11111 statement
= cp_parser_transaction (parser
, token
);
11113 case RID_TRANSACTION_CANCEL
:
11114 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11115 statement
= cp_parser_transaction_cancel (parser
);
11119 /* It might be a keyword like `int' that can start a
11120 declaration-statement. */
11124 else if (token
->type
== CPP_NAME
)
11126 /* If the next token is a `:', then we are looking at a
11127 labeled-statement. */
11128 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11129 if (token
->type
== CPP_COLON
)
11131 /* Looks like a labeled-statement with an ordinary label.
11132 Parse the label, and then use tail recursion to parse
11135 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11136 in_compound
= false;
11140 /* Anything that starts with a `{' must be a compound-statement. */
11141 else if (token
->type
== CPP_OPEN_BRACE
)
11142 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11143 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11144 a statement all its own. */
11145 else if (token
->type
== CPP_PRAGMA
)
11147 /* Only certain OpenMP pragmas are attached to statements, and thus
11148 are considered statements themselves. All others are not. In
11149 the context of a compound, accept the pragma as a "statement" and
11150 return so that we can check for a close brace. Otherwise we
11151 require a real statement and must go back and read one. */
11153 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11154 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11158 else if (token
->type
== CPP_EOF
)
11160 cp_parser_error (parser
, "expected statement");
11164 /* Everything else must be a declaration-statement or an
11165 expression-statement. Try for the declaration-statement
11166 first, unless we are looking at a `;', in which case we know that
11167 we have an expression-statement. */
11170 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11172 if (std_attrs
!= NULL_TREE
)
11173 /* Attributes should be parsed as part of the declaration,
11174 so let's un-parse them. */
11175 saved_tokens
.rollback();
11177 cp_parser_parse_tentatively (parser
);
11178 /* Try to parse the declaration-statement. */
11179 cp_parser_declaration_statement (parser
);
11180 /* If that worked, we're done. */
11181 if (cp_parser_parse_definitely (parser
))
11183 /* It didn't work, restore the post-attribute position. */
11185 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11187 /* All preceding labels have been parsed at this point. */
11188 if (loc_after_labels
!= NULL
)
11189 *loc_after_labels
= statement_location
;
11191 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11193 /* Look for an expression-statement instead. */
11194 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11196 /* Handle [[fallthrough]];. */
11197 if (attribute_fallthrough_p (std_attrs
))
11199 /* The next token after the fallthrough attribute is ';'. */
11200 if (statement
== NULL_TREE
)
11202 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11203 statement
= build_call_expr_internal_loc (statement_location
,
11205 void_type_node
, 0);
11206 finish_expr_stmt (statement
);
11209 warning_at (statement_location
, OPT_Wattributes
,
11210 "%<fallthrough%> attribute not followed by %<;%>");
11211 std_attrs
= NULL_TREE
;
11215 /* Set the line number for the statement. */
11216 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11217 SET_EXPR_LOCATION (statement
, statement_location
);
11219 /* Allow "[[fallthrough]];", but warn otherwise. */
11220 if (std_attrs
!= NULL_TREE
)
11221 warning_at (attrs_location
,
11223 "attributes at the beginning of statement are ignored");
11226 /* Append ATTR to attribute list ATTRS. */
11229 attr_chainon (tree attrs
, tree attr
)
11231 if (attrs
== error_mark_node
)
11232 return error_mark_node
;
11233 if (attr
== error_mark_node
)
11234 return error_mark_node
;
11235 return chainon (attrs
, attr
);
11238 /* Parse the label for a labeled-statement, i.e.
11241 case constant-expression :
11245 case constant-expression ... constant-expression : statement
11247 When a label is parsed without errors, the label is added to the
11248 parse tree by the finish_* functions, so this function doesn't
11249 have to return the label. */
11252 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11255 tree label
= NULL_TREE
;
11256 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11258 /* The next token should be an identifier. */
11259 token
= cp_lexer_peek_token (parser
->lexer
);
11260 if (token
->type
!= CPP_NAME
11261 && token
->type
!= CPP_KEYWORD
)
11263 cp_parser_error (parser
, "expected labeled-statement");
11267 /* Remember whether this case or a user-defined label is allowed to fall
11269 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11271 parser
->colon_corrects_to_scope_p
= false;
11272 switch (token
->keyword
)
11276 tree expr
, expr_hi
;
11277 cp_token
*ellipsis
;
11279 /* Consume the `case' token. */
11280 cp_lexer_consume_token (parser
->lexer
);
11281 /* Parse the constant-expression. */
11282 expr
= cp_parser_constant_expression (parser
);
11283 if (check_for_bare_parameter_packs (expr
))
11284 expr
= error_mark_node
;
11286 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11287 if (ellipsis
->type
== CPP_ELLIPSIS
)
11289 /* Consume the `...' token. */
11290 cp_lexer_consume_token (parser
->lexer
);
11291 expr_hi
= cp_parser_constant_expression (parser
);
11292 if (check_for_bare_parameter_packs (expr_hi
))
11293 expr_hi
= error_mark_node
;
11295 /* We don't need to emit warnings here, as the common code
11296 will do this for us. */
11299 expr_hi
= NULL_TREE
;
11301 if (parser
->in_switch_statement_p
)
11303 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11304 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11306 label
= CASE_LABEL (l
);
11307 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11311 error_at (token
->location
,
11312 "case label %qE not within a switch statement",
11318 /* Consume the `default' token. */
11319 cp_lexer_consume_token (parser
->lexer
);
11321 if (parser
->in_switch_statement_p
)
11323 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11324 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11326 label
= CASE_LABEL (l
);
11327 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11331 error_at (token
->location
, "case label not within a switch statement");
11335 /* Anything else must be an ordinary label. */
11336 label
= finish_label_stmt (cp_parser_identifier (parser
));
11337 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11338 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11342 /* Require the `:' token. */
11343 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11345 /* An ordinary label may optionally be followed by attributes.
11346 However, this is only permitted if the attributes are then
11347 followed by a semicolon. This is because, for backward
11348 compatibility, when parsing
11349 lab: __attribute__ ((unused)) int i;
11350 we want the attribute to attach to "i", not "lab". */
11351 if (label
!= NULL_TREE
11352 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11355 cp_parser_parse_tentatively (parser
);
11356 attrs
= cp_parser_gnu_attributes_opt (parser
);
11357 if (attrs
== NULL_TREE
11358 /* And fallthrough always binds to the expression-statement. */
11359 || attribute_fallthrough_p (attrs
)
11360 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11361 cp_parser_abort_tentative_parse (parser
);
11362 else if (!cp_parser_parse_definitely (parser
))
11365 attributes
= attr_chainon (attributes
, attrs
);
11368 if (attributes
!= NULL_TREE
)
11369 cplus_decl_attributes (&label
, attributes
, 0);
11371 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11374 /* Parse an expression-statement.
11376 expression-statement:
11379 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11380 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11381 indicates whether this expression-statement is part of an
11382 expression statement. */
11385 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11387 tree statement
= NULL_TREE
;
11388 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11389 location_t loc
= token
->location
;
11391 /* There might be attribute fallthrough. */
11392 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11394 /* If the next token is a ';', then there is no expression
11396 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11398 statement
= cp_parser_expression (parser
);
11399 if (statement
== error_mark_node
11400 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11402 cp_parser_skip_to_end_of_block_or_statement (parser
);
11403 return error_mark_node
;
11407 /* Handle [[fallthrough]];. */
11408 if (attribute_fallthrough_p (attr
))
11410 /* The next token after the fallthrough attribute is ';'. */
11411 if (statement
== NULL_TREE
)
11412 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11413 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11414 void_type_node
, 0);
11416 warning_at (loc
, OPT_Wattributes
,
11417 "%<fallthrough%> attribute not followed by %<;%>");
11421 /* Allow "[[fallthrough]];", but warn otherwise. */
11422 if (attr
!= NULL_TREE
)
11423 warning_at (loc
, OPT_Wattributes
,
11424 "attributes at the beginning of statement are ignored");
11426 /* Give a helpful message for "A<T>::type t;" and the like. */
11427 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11428 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11430 if (TREE_CODE (statement
) == SCOPE_REF
)
11431 error_at (token
->location
, "need %<typename%> before %qE because "
11432 "%qT is a dependent scope",
11433 statement
, TREE_OPERAND (statement
, 0));
11434 else if (is_overloaded_fn (statement
)
11435 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11438 tree fn
= get_first_fn (statement
);
11439 error_at (token
->location
,
11440 "%<%T::%D%> names the constructor, not the type",
11441 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11445 /* Consume the final `;'. */
11446 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11448 if (in_statement_expr
11449 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11450 /* This is the final expression statement of a statement
11452 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11453 else if (statement
)
11454 statement
= finish_expr_stmt (statement
);
11459 /* Parse a compound-statement.
11461 compound-statement:
11462 { statement-seq [opt] }
11466 compound-statement:
11467 { label-declaration-seq [opt] statement-seq [opt] }
11469 label-declaration-seq:
11471 label-declaration-seq label-declaration
11473 Returns a tree representing the statement. */
11476 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11477 int bcs_flags
, bool function_body
)
11479 tree compound_stmt
;
11480 matching_braces braces
;
11482 /* Consume the `{'. */
11483 if (!braces
.require_open (parser
))
11484 return error_mark_node
;
11485 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11486 && !function_body
&& cxx_dialect
< cxx14
)
11487 pedwarn (input_location
, OPT_Wpedantic
,
11488 "compound-statement in %<constexpr%> function");
11489 /* Begin the compound-statement. */
11490 compound_stmt
= begin_compound_stmt (bcs_flags
);
11491 /* If the next keyword is `__label__' we have a label declaration. */
11492 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11493 cp_parser_label_declaration (parser
);
11494 /* Parse an (optional) statement-seq. */
11495 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11496 /* Finish the compound-statement. */
11497 finish_compound_stmt (compound_stmt
);
11498 /* Consume the `}'. */
11499 braces
.require_close (parser
);
11501 return compound_stmt
;
11504 /* Parse an (optional) statement-seq.
11508 statement-seq [opt] statement */
11511 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11513 /* Scan statements until there aren't any more. */
11516 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11518 /* If we are looking at a `}', then we have run out of
11519 statements; the same is true if we have reached the end
11520 of file, or have stumbled upon a stray '@end'. */
11521 if (token
->type
== CPP_CLOSE_BRACE
11522 || token
->type
== CPP_EOF
11523 || token
->type
== CPP_PRAGMA_EOL
11524 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11527 /* If we are in a compound statement and find 'else' then
11528 something went wrong. */
11529 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11531 if (parser
->in_statement
& IN_IF_STMT
)
11535 token
= cp_lexer_consume_token (parser
->lexer
);
11536 error_at (token
->location
, "%<else%> without a previous %<if%>");
11540 /* Parse the statement. */
11541 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11545 /* Return true if this is the C++20 version of range-based-for with
11549 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11553 /* Save tokens so that we can put them back. */
11554 cp_lexer_save_tokens (parser
->lexer
);
11556 /* There has to be an unnested ; followed by an unnested :. */
11557 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11558 /*recovering=*/false,
11560 /*consume_paren=*/false) != -1)
11563 /* We found the semicolon, eat it now. */
11564 cp_lexer_consume_token (parser
->lexer
);
11566 /* Now look for ':' that is not nested in () or {}. */
11567 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11568 /*recovering=*/false,
11570 /*consume_paren=*/false) == -1);
11573 /* Roll back the tokens we skipped. */
11574 cp_lexer_rollback_tokens (parser
->lexer
);
11579 /* Return true if we're looking at (init; cond), false otherwise. */
11582 cp_parser_init_statement_p (cp_parser
*parser
)
11584 /* Save tokens so that we can put them back. */
11585 cp_lexer_save_tokens (parser
->lexer
);
11587 /* Look for ';' that is not nested in () or {}. */
11588 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11589 /*recovering=*/false,
11591 /*consume_paren=*/false);
11593 /* Roll back the tokens we skipped. */
11594 cp_lexer_rollback_tokens (parser
->lexer
);
11599 /* Parse a selection-statement.
11601 selection-statement:
11602 if ( init-statement [opt] condition ) statement
11603 if ( init-statement [opt] condition ) statement else statement
11604 switch ( init-statement [opt] condition ) statement
11606 Returns the new IF_STMT or SWITCH_STMT.
11608 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11609 is a (possibly labeled) if statement which is not enclosed in
11610 braces and has an else clause. This is used to implement
11613 CHAIN is a vector of if-else-if conditions. This is used to implement
11614 -Wduplicated-cond. */
11617 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11622 token_indent_info guard_tinfo
;
11627 /* Peek at the next token. */
11628 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11629 guard_tinfo
= get_token_indent_info (token
);
11631 /* See what kind of keyword it is. */
11632 keyword
= token
->keyword
;
11642 if (keyword
== RID_IF
11643 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11647 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11648 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11649 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11650 "with -std=c++17 or -std=gnu++17");
11653 /* Look for the `('. */
11654 matching_parens parens
;
11655 if (!parens
.require_open (parser
))
11657 cp_parser_skip_to_end_of_statement (parser
);
11658 return error_mark_node
;
11661 /* Begin the selection-statement. */
11662 if (keyword
== RID_IF
)
11664 statement
= begin_if_stmt ();
11665 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11668 statement
= begin_switch_stmt ();
11670 /* Parse the optional init-statement. */
11671 if (cp_parser_init_statement_p (parser
))
11674 if (cxx_dialect
< cxx17
)
11675 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11676 "init-statement in selection statements only available "
11677 "with -std=c++17 or -std=gnu++17");
11678 cp_parser_init_statement (parser
, &decl
);
11681 /* Parse the condition. */
11682 condition
= cp_parser_condition (parser
);
11683 /* Look for the `)'. */
11684 if (!parens
.require_close (parser
))
11685 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11686 /*consume_paren=*/true);
11688 if (keyword
== RID_IF
)
11691 unsigned char in_statement
;
11693 /* Add the condition. */
11694 condition
= finish_if_stmt_cond (condition
, statement
);
11696 if (warn_duplicated_cond
)
11697 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11700 /* Parse the then-clause. */
11701 in_statement
= parser
->in_statement
;
11702 parser
->in_statement
|= IN_IF_STMT
;
11704 /* Outside a template, the non-selected branch of a constexpr
11705 if is a 'discarded statement', i.e. unevaluated. */
11706 bool was_discarded
= in_discarded_stmt
;
11707 bool discard_then
= (cx
&& !processing_template_decl
11708 && integer_zerop (condition
));
11711 in_discarded_stmt
= true;
11712 ++c_inhibit_evaluation_warnings
;
11715 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11718 parser
->in_statement
= in_statement
;
11720 finish_then_clause (statement
);
11724 THEN_CLAUSE (statement
) = NULL_TREE
;
11725 in_discarded_stmt
= was_discarded
;
11726 --c_inhibit_evaluation_warnings
;
11729 /* If the next token is `else', parse the else-clause. */
11730 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11733 bool discard_else
= (cx
&& !processing_template_decl
11734 && integer_nonzerop (condition
));
11737 in_discarded_stmt
= true;
11738 ++c_inhibit_evaluation_warnings
;
11742 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11743 /* Consume the `else' keyword. */
11744 cp_lexer_consume_token (parser
->lexer
);
11745 if (warn_duplicated_cond
)
11747 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11751 /* We've got "if (COND) else if (COND2)". Start
11752 the condition chain and add COND as the first
11754 chain
= new vec
<tree
> ();
11755 if (!CONSTANT_CLASS_P (condition
)
11756 && !TREE_SIDE_EFFECTS (condition
))
11758 /* Wrap it in a NOP_EXPR so that we can set the
11759 location of the condition. */
11760 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11762 SET_EXPR_LOCATION (e
, token
->location
);
11763 chain
->safe_push (e
);
11766 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11769 /* This is if-else without subsequent if. Zap the
11770 condition chain; we would have already warned at
11776 begin_else_clause (statement
);
11777 /* Parse the else-clause. */
11778 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11779 guard_tinfo
, chain
);
11781 finish_else_clause (statement
);
11783 /* If we are currently parsing a then-clause, then
11784 IF_P will not be NULL. We set it to true to
11785 indicate that this if statement has an else clause.
11786 This may trigger the Wparentheses warning below
11787 when we get back up to the parent if statement. */
11793 ELSE_CLAUSE (statement
) = NULL_TREE
;
11794 in_discarded_stmt
= was_discarded
;
11795 --c_inhibit_evaluation_warnings
;
11800 /* This if statement does not have an else clause. If
11801 NESTED_IF is true, then the then-clause has an if
11802 statement which does have an else clause. We warn
11803 about the potential ambiguity. */
11805 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11806 "suggest explicit braces to avoid ambiguous"
11808 if (warn_duplicated_cond
)
11810 /* We don't need the condition chain anymore. */
11816 /* Now we're all done with the if-statement. */
11817 finish_if_stmt (statement
);
11821 bool in_switch_statement_p
;
11822 unsigned char in_statement
;
11824 /* Add the condition. */
11825 finish_switch_cond (condition
, statement
);
11827 /* Parse the body of the switch-statement. */
11828 in_switch_statement_p
= parser
->in_switch_statement_p
;
11829 in_statement
= parser
->in_statement
;
11830 parser
->in_switch_statement_p
= true;
11831 parser
->in_statement
|= IN_SWITCH_STMT
;
11832 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11834 parser
->in_switch_statement_p
= in_switch_statement_p
;
11835 parser
->in_statement
= in_statement
;
11837 /* Now we're all done with the switch-statement. */
11838 finish_switch_stmt (statement
);
11846 cp_parser_error (parser
, "expected selection-statement");
11847 return error_mark_node
;
11851 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11852 If we have seen at least one decl-specifier, and the next token
11853 is not a parenthesis, then we must be looking at a declaration.
11854 (After "int (" we might be looking at a functional cast.) */
11857 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11858 bool any_specifiers_p
)
11860 if (any_specifiers_p
11861 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11862 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11863 && !cp_parser_error_occurred (parser
))
11864 cp_parser_commit_to_tentative_parse (parser
);
11867 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11868 The declarator shall not specify a function or an array. Returns
11869 TRUE if the declarator is valid, FALSE otherwise. */
11872 cp_parser_check_condition_declarator (cp_parser
* parser
,
11873 cp_declarator
*declarator
,
11876 if (declarator
== cp_error_declarator
11877 || function_declarator_p (declarator
)
11878 || declarator
->kind
== cdk_array
)
11880 if (declarator
== cp_error_declarator
)
11881 /* Already complained. */;
11882 else if (declarator
->kind
== cdk_array
)
11883 error_at (loc
, "condition declares an array");
11885 error_at (loc
, "condition declares a function");
11886 if (parser
->fully_implicit_function_template_p
)
11887 abort_fully_implicit_template (parser
);
11888 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11889 /*or_comma=*/false,
11890 /*consume_paren=*/false);
11897 /* Parse a condition.
11901 type-specifier-seq declarator = initializer-clause
11902 type-specifier-seq declarator braced-init-list
11907 type-specifier-seq declarator asm-specification [opt]
11908 attributes [opt] = assignment-expression
11910 Returns the expression that should be tested. */
11913 cp_parser_condition (cp_parser
* parser
)
11915 cp_decl_specifier_seq type_specifiers
;
11916 const char *saved_message
;
11917 int declares_class_or_enum
;
11919 /* Try the declaration first. */
11920 cp_parser_parse_tentatively (parser
);
11921 /* New types are not allowed in the type-specifier-seq for a
11923 saved_message
= parser
->type_definition_forbidden_message
;
11924 parser
->type_definition_forbidden_message
11925 = G_("types may not be defined in conditions");
11926 /* Parse the type-specifier-seq. */
11927 cp_parser_decl_specifier_seq (parser
,
11928 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11930 &declares_class_or_enum
);
11931 /* Restore the saved message. */
11932 parser
->type_definition_forbidden_message
= saved_message
;
11934 cp_parser_maybe_commit_to_declaration (parser
,
11935 type_specifiers
.any_specifiers_p
);
11937 /* If all is well, we might be looking at a declaration. */
11938 if (!cp_parser_error_occurred (parser
))
11941 tree asm_specification
;
11943 cp_declarator
*declarator
;
11944 tree initializer
= NULL_TREE
;
11945 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11947 /* Parse the declarator. */
11948 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11949 CP_PARSER_FLAGS_NONE
,
11950 /*ctor_dtor_or_conv_p=*/NULL
,
11951 /*parenthesized_p=*/NULL
,
11952 /*member_p=*/false,
11953 /*friend_p=*/false);
11954 /* Parse the attributes. */
11955 attributes
= cp_parser_attributes_opt (parser
);
11956 /* Parse the asm-specification. */
11957 asm_specification
= cp_parser_asm_specification_opt (parser
);
11958 /* If the next token is not an `=' or '{', then we might still be
11959 looking at an expression. For example:
11963 looks like a decl-specifier-seq and a declarator -- but then
11964 there is no `=', so this is an expression. */
11965 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11966 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11967 cp_parser_simulate_error (parser
);
11969 /* If we did see an `=' or '{', then we are looking at a declaration
11971 if (cp_parser_parse_definitely (parser
))
11974 bool non_constant_p
= false;
11975 int flags
= LOOKUP_ONLYCONVERTING
;
11977 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11978 return error_mark_node
;
11980 /* Create the declaration. */
11981 decl
= start_decl (declarator
, &type_specifiers
,
11982 /*initialized_p=*/true,
11983 attributes
, /*prefix_attributes=*/NULL_TREE
,
11986 /* Parse the initializer. */
11987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11989 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11990 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11993 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11995 /* Consume the `='. */
11996 cp_lexer_consume_token (parser
->lexer
);
11997 initializer
= cp_parser_initializer_clause (parser
,
12002 cp_parser_error (parser
, "expected initializer");
12003 initializer
= error_mark_node
;
12005 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12006 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12008 /* Process the initializer. */
12009 cp_finish_decl (decl
,
12010 initializer
, !non_constant_p
,
12015 pop_scope (pushed_scope
);
12017 return convert_from_reference (decl
);
12020 /* If we didn't even get past the declarator successfully, we are
12021 definitely not looking at a declaration. */
12023 cp_parser_abort_tentative_parse (parser
);
12025 /* Otherwise, we are looking at an expression. */
12026 return cp_parser_expression (parser
);
12029 /* Parses a for-statement or range-for-statement until the closing ')',
12033 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12035 tree init
, scope
, decl
;
12038 /* Begin the for-statement. */
12039 scope
= begin_for_scope (&init
);
12041 /* Parse the initialization. */
12042 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12045 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12048 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12052 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12053 unsigned short unroll
)
12055 /* Normal for loop */
12056 tree condition
= NULL_TREE
;
12057 tree expression
= NULL_TREE
;
12060 stmt
= begin_for_stmt (scope
, init
);
12061 /* The init-statement has already been parsed in
12062 cp_parser_init_statement, so no work is needed here. */
12063 finish_init_stmt (stmt
);
12065 /* If there's a condition, process it. */
12066 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12067 condition
= cp_parser_condition (parser
);
12070 cp_parser_error (parser
, "missing loop condition in loop with "
12071 "%<GCC ivdep%> pragma");
12072 condition
= error_mark_node
;
12076 cp_parser_error (parser
, "missing loop condition in loop with "
12077 "%<GCC unroll%> pragma");
12078 condition
= error_mark_node
;
12080 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12081 /* Look for the `;'. */
12082 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12084 /* If there's an expression, process it. */
12085 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12086 expression
= cp_parser_expression (parser
);
12087 finish_for_expr (expression
, stmt
);
12092 /* Tries to parse a range-based for-statement:
12095 decl-specifier-seq declarator : expression
12097 The decl-specifier-seq declarator and the `:' are already parsed by
12098 cp_parser_init_statement. If processing_template_decl it returns a
12099 newly created RANGE_FOR_STMT; if not, it is converted to a
12100 regular FOR_STMT. */
12103 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12104 bool ivdep
, unsigned short unroll
, bool is_omp
)
12106 tree stmt
, range_expr
;
12107 auto_vec
<cxx_binding
*, 16> bindings
;
12108 auto_vec
<tree
, 16> names
;
12109 tree decomp_first_name
= NULL_TREE
;
12110 unsigned int decomp_cnt
= 0;
12112 /* Get the range declaration momentarily out of the way so that
12113 the range expression doesn't clash with it. */
12114 if (range_decl
!= error_mark_node
)
12116 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12118 tree v
= DECL_VALUE_EXPR (range_decl
);
12119 /* For decomposition declaration get all of the corresponding
12120 declarations out of the way. */
12121 if (TREE_CODE (v
) == ARRAY_REF
12122 && VAR_P (TREE_OPERAND (v
, 0))
12123 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12125 tree d
= range_decl
;
12126 range_decl
= TREE_OPERAND (v
, 0);
12127 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12128 decomp_first_name
= d
;
12129 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12131 tree name
= DECL_NAME (d
);
12132 names
.safe_push (name
);
12133 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12134 IDENTIFIER_BINDING (name
)
12135 = IDENTIFIER_BINDING (name
)->previous
;
12139 if (names
.is_empty ())
12141 tree name
= DECL_NAME (range_decl
);
12142 names
.safe_push (name
);
12143 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12144 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12150 bool expr_non_constant_p
;
12151 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12154 range_expr
= cp_parser_expression (parser
);
12156 /* Put the range declaration(s) back into scope. */
12157 for (unsigned int i
= 0; i
< names
.length (); i
++)
12159 cxx_binding
*binding
= bindings
[i
];
12160 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12161 IDENTIFIER_BINDING (names
[i
]) = binding
;
12164 /* finish_omp_for has its own code for the following, so just
12165 return the range_expr instead. */
12169 /* If in template, STMT is converted to a normal for-statement
12170 at instantiation. If not, it is done just ahead. */
12171 if (processing_template_decl
)
12173 if (check_for_bare_parameter_packs (range_expr
))
12174 range_expr
= error_mark_node
;
12175 stmt
= begin_range_for_stmt (scope
, init
);
12177 RANGE_FOR_IVDEP (stmt
) = 1;
12179 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12180 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12181 if (!type_dependent_expression_p (range_expr
)
12182 /* do_auto_deduction doesn't mess with template init-lists. */
12183 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12184 do_range_for_auto_deduction (range_decl
, range_expr
);
12188 stmt
= begin_for_stmt (scope
, init
);
12189 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12190 decomp_first_name
, decomp_cnt
, ivdep
,
12196 /* Subroutine of cp_convert_range_for: given the initializer expression,
12197 builds up the range temporary. */
12200 build_range_temp (tree range_expr
)
12202 tree range_type
, range_temp
;
12204 /* Find out the type deduced by the declaration
12205 `auto &&__range = range_expr'. */
12206 range_type
= cp_build_reference_type (make_auto (), true);
12207 range_type
= do_auto_deduction (range_type
, range_expr
,
12208 type_uses_auto (range_type
));
12210 /* Create the __range variable. */
12211 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12213 TREE_USED (range_temp
) = 1;
12214 DECL_ARTIFICIAL (range_temp
) = 1;
12219 /* Used by cp_parser_range_for in template context: we aren't going to
12220 do a full conversion yet, but we still need to resolve auto in the
12221 type of the for-range-declaration if present. This is basically
12222 a shortcut version of cp_convert_range_for. */
12225 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12227 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12230 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12231 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12232 iter_type
= (cp_parser_perform_range_for_lookup
12233 (range_temp
, &begin_dummy
, &end_dummy
));
12236 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12238 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12240 tf_warning_or_error
);
12241 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12242 iter_decl
, auto_node
);
12247 /* Converts a range-based for-statement into a normal
12248 for-statement, as per the definition.
12250 for (RANGE_DECL : RANGE_EXPR)
12253 should be equivalent to:
12256 auto &&__range = RANGE_EXPR;
12257 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12261 RANGE_DECL = *__begin;
12266 If RANGE_EXPR is an array:
12267 BEGIN_EXPR = __range
12268 END_EXPR = __range + ARRAY_SIZE(__range)
12269 Else if RANGE_EXPR has a member 'begin' or 'end':
12270 BEGIN_EXPR = __range.begin()
12271 END_EXPR = __range.end()
12273 BEGIN_EXPR = begin(__range)
12274 END_EXPR = end(__range);
12276 If __range has a member 'begin' but not 'end', or vice versa, we must
12277 still use the second alternative (it will surely fail, however).
12278 When calling begin()/end() in the third alternative we must use
12279 argument dependent lookup, but always considering 'std' as an associated
12283 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12284 tree decomp_first_name
, unsigned int decomp_cnt
,
12285 bool ivdep
, unsigned short unroll
)
12288 tree iter_type
, begin_expr
, end_expr
;
12289 tree condition
, expression
;
12291 range_expr
= mark_lvalue_use (range_expr
);
12293 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12294 /* If an error happened previously do nothing or else a lot of
12295 unhelpful errors would be issued. */
12296 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12301 if (VAR_P (range_expr
)
12302 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12303 /* Can't bind a reference to an array of runtime bound. */
12304 range_temp
= range_expr
;
12307 range_temp
= build_range_temp (range_expr
);
12308 pushdecl (range_temp
);
12309 cp_finish_decl (range_temp
, range_expr
,
12310 /*is_constant_init*/false, NULL_TREE
,
12311 LOOKUP_ONLYCONVERTING
);
12312 range_temp
= convert_from_reference (range_temp
);
12314 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12315 &begin_expr
, &end_expr
);
12318 /* The new for initialization statement. */
12319 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12321 TREE_USED (begin
) = 1;
12322 DECL_ARTIFICIAL (begin
) = 1;
12324 cp_finish_decl (begin
, begin_expr
,
12325 /*is_constant_init*/false, NULL_TREE
,
12326 LOOKUP_ONLYCONVERTING
);
12328 if (cxx_dialect
>= cxx17
)
12329 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12330 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12331 TREE_USED (end
) = 1;
12332 DECL_ARTIFICIAL (end
) = 1;
12334 cp_finish_decl (end
, end_expr
,
12335 /*is_constant_init*/false, NULL_TREE
,
12336 LOOKUP_ONLYCONVERTING
);
12338 finish_init_stmt (statement
);
12340 /* The new for condition. */
12341 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12344 NULL
, tf_warning_or_error
);
12345 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12347 /* The new increment expression. */
12348 expression
= finish_unary_op_expr (input_location
,
12349 PREINCREMENT_EXPR
, begin
,
12350 tf_warning_or_error
);
12351 finish_for_expr (expression
, statement
);
12353 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12354 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12356 /* The declaration is initialized with *__begin inside the loop body. */
12357 cp_finish_decl (range_decl
,
12358 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12359 tf_warning_or_error
),
12360 /*is_constant_init*/false, NULL_TREE
,
12361 LOOKUP_ONLYCONVERTING
);
12362 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12363 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12368 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12369 We need to solve both at the same time because the method used
12370 depends on the existence of members begin or end.
12371 Returns the type deduced for the iterator expression. */
12374 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12376 if (error_operand_p (range
))
12378 *begin
= *end
= error_mark_node
;
12379 return error_mark_node
;
12382 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12384 error ("range-based %<for%> expression of type %qT "
12385 "has incomplete type", TREE_TYPE (range
));
12386 *begin
= *end
= error_mark_node
;
12387 return error_mark_node
;
12389 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12391 /* If RANGE is an array, we will use pointer arithmetic. */
12392 *begin
= decay_conversion (range
, tf_warning_or_error
);
12393 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12395 array_type_nelts_top (TREE_TYPE (range
)),
12397 return TREE_TYPE (*begin
);
12401 /* If it is not an array, we must do a bit of magic. */
12402 tree id_begin
, id_end
;
12403 tree member_begin
, member_end
;
12405 *begin
= *end
= error_mark_node
;
12407 id_begin
= get_identifier ("begin");
12408 id_end
= get_identifier ("end");
12409 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12410 /*protect=*/2, /*want_type=*/false,
12411 tf_warning_or_error
);
12412 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12413 /*protect=*/2, /*want_type=*/false,
12414 tf_warning_or_error
);
12416 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12418 /* Use the member functions. */
12419 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12420 *end
= cp_parser_range_for_member_function (range
, id_end
);
12424 /* Use global functions with ADL. */
12425 vec
<tree
, va_gc
> *vec
;
12426 vec
= make_tree_vector ();
12428 vec_safe_push (vec
, range
);
12430 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12431 tf_warning_or_error
);
12432 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12433 tf_warning_or_error
);
12434 member_end
= perform_koenig_lookup (id_end
, vec
,
12435 tf_warning_or_error
);
12436 *end
= finish_call_expr (member_end
, &vec
, false, true,
12437 tf_warning_or_error
);
12439 release_tree_vector (vec
);
12442 /* Last common checks. */
12443 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12445 /* If one of the expressions is an error do no more checks. */
12446 *begin
= *end
= error_mark_node
;
12447 return error_mark_node
;
12449 else if (type_dependent_expression_p (*begin
)
12450 || type_dependent_expression_p (*end
))
12451 /* Can happen, when, eg, in a template context, Koenig lookup
12452 can't resolve begin/end (c++/58503). */
12456 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12457 /* The unqualified type of the __begin and __end temporaries should
12458 be the same, as required by the multiple auto declaration. */
12459 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12461 if (cxx_dialect
>= cxx17
12462 && (build_x_binary_op (input_location
, NE_EXPR
,
12463 *begin
, ERROR_MARK
,
12466 != error_mark_node
))
12467 /* P0184R0 allows __begin and __end to have different types,
12468 but make sure they are comparable so we can give a better
12471 error ("inconsistent begin/end types in range-based %<for%> "
12472 "statement: %qT and %qT",
12473 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12480 /* Helper function for cp_parser_perform_range_for_lookup.
12481 Builds a tree for RANGE.IDENTIFIER(). */
12484 cp_parser_range_for_member_function (tree range
, tree identifier
)
12487 vec
<tree
, va_gc
> *vec
;
12489 member
= finish_class_member_access_expr (range
, identifier
,
12490 false, tf_warning_or_error
);
12491 if (member
== error_mark_node
)
12492 return error_mark_node
;
12494 vec
= make_tree_vector ();
12495 res
= finish_call_expr (member
, &vec
,
12496 /*disallow_virtual=*/false,
12497 /*koenig_p=*/false,
12498 tf_warning_or_error
);
12499 release_tree_vector (vec
);
12503 /* Parse an iteration-statement.
12505 iteration-statement:
12506 while ( condition ) statement
12507 do statement while ( expression ) ;
12508 for ( init-statement condition [opt] ; expression [opt] )
12511 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12514 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12515 unsigned short unroll
)
12520 unsigned char in_statement
;
12521 token_indent_info guard_tinfo
;
12523 /* Peek at the next token. */
12524 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12526 return error_mark_node
;
12528 guard_tinfo
= get_token_indent_info (token
);
12530 /* Remember whether or not we are already within an iteration
12532 in_statement
= parser
->in_statement
;
12534 /* See what kind of keyword it is. */
12535 keyword
= token
->keyword
;
12542 /* Begin the while-statement. */
12543 statement
= begin_while_stmt ();
12544 /* Look for the `('. */
12545 matching_parens parens
;
12546 parens
.require_open (parser
);
12547 /* Parse the condition. */
12548 condition
= cp_parser_condition (parser
);
12549 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12550 /* Look for the `)'. */
12551 parens
.require_close (parser
);
12552 /* Parse the dependent statement. */
12553 parser
->in_statement
= IN_ITERATION_STMT
;
12554 bool prev
= note_iteration_stmt_body_start ();
12555 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12556 note_iteration_stmt_body_end (prev
);
12557 parser
->in_statement
= in_statement
;
12558 /* We're done with the while-statement. */
12559 finish_while_stmt (statement
);
12567 /* Begin the do-statement. */
12568 statement
= begin_do_stmt ();
12569 /* Parse the body of the do-statement. */
12570 parser
->in_statement
= IN_ITERATION_STMT
;
12571 bool prev
= note_iteration_stmt_body_start ();
12572 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12573 note_iteration_stmt_body_end (prev
);
12574 parser
->in_statement
= in_statement
;
12575 finish_do_body (statement
);
12576 /* Look for the `while' keyword. */
12577 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12578 /* Look for the `('. */
12579 matching_parens parens
;
12580 parens
.require_open (parser
);
12581 /* Parse the expression. */
12582 expression
= cp_parser_expression (parser
);
12583 /* We're done with the do-statement. */
12584 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12585 /* Look for the `)'. */
12586 parens
.require_close (parser
);
12587 /* Look for the `;'. */
12588 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12594 /* Look for the `('. */
12595 matching_parens parens
;
12596 parens
.require_open (parser
);
12598 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12600 /* Look for the `)'. */
12601 parens
.require_close (parser
);
12603 /* Parse the body of the for-statement. */
12604 parser
->in_statement
= IN_ITERATION_STMT
;
12605 bool prev
= note_iteration_stmt_body_start ();
12606 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12607 note_iteration_stmt_body_end (prev
);
12608 parser
->in_statement
= in_statement
;
12610 /* We're done with the for-statement. */
12611 finish_for_stmt (statement
);
12616 cp_parser_error (parser
, "expected iteration-statement");
12617 statement
= error_mark_node
;
12624 /* Parse a init-statement or the declarator of a range-based-for.
12625 Returns true if a range-based-for declaration is seen.
12628 expression-statement
12629 simple-declaration */
12632 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12634 /* If the next token is a `;', then we have an empty
12635 expression-statement. Grammatically, this is also a
12636 simple-declaration, but an invalid one, because it does not
12637 declare anything. Therefore, if we did not handle this case
12638 specially, we would issue an error message about an invalid
12640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12642 bool is_range_for
= false;
12643 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12645 /* Try to parse the init-statement. */
12646 if (cp_parser_range_based_for_with_init_p (parser
))
12649 cp_parser_parse_tentatively (parser
);
12650 /* Parse the declaration. */
12651 cp_parser_simple_declaration (parser
,
12652 /*function_definition_allowed_p=*/false,
12654 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12655 if (!cp_parser_parse_definitely (parser
))
12656 /* That didn't work, try to parse it as an expression-statement. */
12657 cp_parser_expression_statement (parser
, NULL_TREE
);
12659 if (cxx_dialect
< cxx2a
)
12661 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12662 "range-based %<for%> loops with initializer only "
12663 "available with -std=c++2a or -std=gnu++2a");
12664 *decl
= error_mark_node
;
12668 /* A colon is used in range-based for. */
12669 parser
->colon_corrects_to_scope_p
= false;
12671 /* We're going to speculatively look for a declaration, falling back
12672 to an expression, if necessary. */
12673 cp_parser_parse_tentatively (parser
);
12674 /* Parse the declaration. */
12675 cp_parser_simple_declaration (parser
,
12676 /*function_definition_allowed_p=*/false,
12678 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12681 /* It is a range-for, consume the ':'. */
12682 cp_lexer_consume_token (parser
->lexer
);
12683 is_range_for
= true;
12684 if (cxx_dialect
< cxx11
)
12685 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12686 "range-based %<for%> loops only available with "
12687 "-std=c++11 or -std=gnu++11");
12690 /* The ';' is not consumed yet because we told
12691 cp_parser_simple_declaration not to. */
12692 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12694 if (cp_parser_parse_definitely (parser
))
12695 return is_range_for
;
12696 /* If the tentative parse failed, then we shall need to look for an
12697 expression-statement. */
12699 /* If we are here, it is an expression-statement. */
12700 cp_parser_expression_statement (parser
, NULL_TREE
);
12704 /* Parse a jump-statement.
12709 return expression [opt] ;
12710 return braced-init-list ;
12716 goto * expression ;
12718 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12721 cp_parser_jump_statement (cp_parser
* parser
)
12723 tree statement
= error_mark_node
;
12726 unsigned char in_statement
;
12728 /* Peek at the next token. */
12729 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12731 return error_mark_node
;
12733 /* See what kind of keyword it is. */
12734 keyword
= token
->keyword
;
12738 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12739 switch (in_statement
)
12742 error_at (token
->location
, "break statement not within loop or switch");
12745 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12746 || in_statement
== IN_ITERATION_STMT
);
12747 statement
= finish_break_stmt ();
12748 if (in_statement
== IN_ITERATION_STMT
)
12749 break_maybe_infinite_loop ();
12752 error_at (token
->location
, "invalid exit from OpenMP structured block");
12755 error_at (token
->location
, "break statement used with OpenMP for loop");
12758 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12762 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12765 error_at (token
->location
, "continue statement not within a loop");
12767 /* Fall through. */
12768 case IN_ITERATION_STMT
:
12770 statement
= finish_continue_stmt ();
12773 error_at (token
->location
, "invalid exit from OpenMP structured block");
12776 gcc_unreachable ();
12778 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12784 bool expr_non_constant_p
;
12786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12788 cp_lexer_set_source_position (parser
->lexer
);
12789 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12790 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12792 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12793 expr
= cp_parser_expression (parser
);
12795 /* If the next token is a `;', then there is no
12798 /* Build the return-statement. */
12799 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12800 /* Don't deduce from a discarded return statement. */;
12802 statement
= finish_return_stmt (expr
);
12803 /* Look for the final `;'. */
12804 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12809 if (parser
->in_function_body
12810 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12812 error ("%<goto%> in %<constexpr%> function");
12813 cp_function_chain
->invalid_constexpr
= true;
12816 /* Create the goto-statement. */
12817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12819 /* Issue a warning about this use of a GNU extension. */
12820 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12821 /* Consume the '*' token. */
12822 cp_lexer_consume_token (parser
->lexer
);
12823 /* Parse the dependent expression. */
12824 finish_goto_stmt (cp_parser_expression (parser
));
12827 finish_goto_stmt (cp_parser_identifier (parser
));
12828 /* Look for the final `;'. */
12829 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12833 cp_parser_error (parser
, "expected jump-statement");
12840 /* Parse a declaration-statement.
12842 declaration-statement:
12843 block-declaration */
12846 cp_parser_declaration_statement (cp_parser
* parser
)
12850 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12851 p
= obstack_alloc (&declarator_obstack
, 0);
12853 /* Parse the block-declaration. */
12854 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12856 /* Free any declarators allocated. */
12857 obstack_free (&declarator_obstack
, p
);
12860 /* Some dependent statements (like `if (cond) statement'), are
12861 implicitly in their own scope. In other words, if the statement is
12862 a single statement (as opposed to a compound-statement), it is
12863 none-the-less treated as if it were enclosed in braces. Any
12864 declarations appearing in the dependent statement are out of scope
12865 after control passes that point. This function parses a statement,
12866 but ensures that is in its own scope, even if it is not a
12867 compound-statement.
12869 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12870 is a (possibly labeled) if statement which is not enclosed in
12871 braces and has an else clause. This is used to implement
12874 CHAIN is a vector of if-else-if conditions. This is used to implement
12877 Returns the new statement. */
12880 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12881 const token_indent_info
&guard_tinfo
,
12885 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12886 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12887 token_indent_info body_tinfo
12888 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12893 /* Mark if () ; with a special NOP_EXPR. */
12894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12896 cp_lexer_consume_token (parser
->lexer
);
12897 statement
= add_stmt (build_empty_stmt (body_loc
));
12899 if (guard_tinfo
.keyword
== RID_IF
12900 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12901 warning_at (body_loc
, OPT_Wempty_body
,
12902 "suggest braces around empty body in an %<if%> statement");
12903 else if (guard_tinfo
.keyword
== RID_ELSE
)
12904 warning_at (body_loc
, OPT_Wempty_body
,
12905 "suggest braces around empty body in an %<else%> statement");
12907 /* if a compound is opened, we simply parse the statement directly. */
12908 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12909 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12910 /* If the token is not a `{', then we must take special action. */
12913 /* Create a compound-statement. */
12914 statement
= begin_compound_stmt (0);
12915 /* Parse the dependent-statement. */
12916 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12917 &body_loc_after_labels
);
12918 /* Finish the dummy compound-statement. */
12919 finish_compound_stmt (statement
);
12922 token_indent_info next_tinfo
12923 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12924 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12926 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12927 && next_tinfo
.type
!= CPP_SEMICOLON
)
12928 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12929 guard_tinfo
.location
, guard_tinfo
.keyword
);
12931 /* Return the statement. */
12935 /* For some dependent statements (like `while (cond) statement'), we
12936 have already created a scope. Therefore, even if the dependent
12937 statement is a compound-statement, we do not want to create another
12941 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12942 const token_indent_info
&guard_tinfo
)
12944 /* If the token is a `{', then we must take special action. */
12945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12947 token_indent_info body_tinfo
12948 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12949 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12951 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12952 &loc_after_labels
);
12953 token_indent_info next_tinfo
12954 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12955 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12957 if (loc_after_labels
!= UNKNOWN_LOCATION
12958 && next_tinfo
.type
!= CPP_SEMICOLON
)
12959 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12960 guard_tinfo
.location
,
12961 guard_tinfo
.keyword
);
12965 /* Avoid calling cp_parser_compound_statement, so that we
12966 don't create a new scope. Do everything else by hand. */
12967 matching_braces braces
;
12968 braces
.require_open (parser
);
12969 /* If the next keyword is `__label__' we have a label declaration. */
12970 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12971 cp_parser_label_declaration (parser
);
12972 /* Parse an (optional) statement-seq. */
12973 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12974 braces
.require_close (parser
);
12978 /* Declarations [gram.dcl.dcl] */
12980 /* Parse an optional declaration-sequence.
12984 declaration-seq declaration */
12987 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12991 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12993 if (token
->type
== CPP_CLOSE_BRACE
12994 || token
->type
== CPP_EOF
)
12997 cp_parser_toplevel_declaration (parser
);
13001 /* Parse a declaration.
13005 function-definition
13006 template-declaration
13007 explicit-instantiation
13008 explicit-specialization
13009 linkage-specification
13010 namespace-definition
13018 __extension__ declaration */
13021 cp_parser_declaration (cp_parser
* parser
)
13025 int saved_pedantic
;
13027 tree attributes
= NULL_TREE
;
13029 /* Check for the `__extension__' keyword. */
13030 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13032 /* Parse the qualified declaration. */
13033 cp_parser_declaration (parser
);
13034 /* Restore the PEDANTIC flag. */
13035 pedantic
= saved_pedantic
;
13040 /* Try to figure out what kind of declaration is present. */
13041 token1
= *cp_lexer_peek_token (parser
->lexer
);
13043 if (token1
.type
!= CPP_EOF
)
13044 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13047 token2
.type
= CPP_EOF
;
13048 token2
.keyword
= RID_MAX
;
13051 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13052 p
= obstack_alloc (&declarator_obstack
, 0);
13054 /* If the next token is `extern' and the following token is a string
13055 literal, then we have a linkage specification. */
13056 if (token1
.keyword
== RID_EXTERN
13057 && cp_parser_is_pure_string_literal (&token2
))
13058 cp_parser_linkage_specification (parser
);
13059 /* If the next token is `template', then we have either a template
13060 declaration, an explicit instantiation, or an explicit
13062 else if (token1
.keyword
== RID_TEMPLATE
)
13064 /* `template <>' indicates a template specialization. */
13065 if (token2
.type
== CPP_LESS
13066 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13067 cp_parser_explicit_specialization (parser
);
13068 /* `template <' indicates a template declaration. */
13069 else if (token2
.type
== CPP_LESS
)
13070 cp_parser_template_declaration (parser
, /*member_p=*/false);
13071 /* Anything else must be an explicit instantiation. */
13073 cp_parser_explicit_instantiation (parser
);
13075 /* If the next token is `export', then we have a template
13077 else if (token1
.keyword
== RID_EXPORT
)
13078 cp_parser_template_declaration (parser
, /*member_p=*/false);
13079 /* If the next token is `extern', 'static' or 'inline' and the one
13080 after that is `template', we have a GNU extended explicit
13081 instantiation directive. */
13082 else if (cp_parser_allow_gnu_extensions_p (parser
)
13083 && (token1
.keyword
== RID_EXTERN
13084 || token1
.keyword
== RID_STATIC
13085 || token1
.keyword
== RID_INLINE
)
13086 && token2
.keyword
== RID_TEMPLATE
)
13087 cp_parser_explicit_instantiation (parser
);
13088 /* If the next token is `namespace', check for a named or unnamed
13089 namespace definition. */
13090 else if (token1
.keyword
== RID_NAMESPACE
13091 && (/* A named namespace definition. */
13092 (token2
.type
== CPP_NAME
13093 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13095 || (token2
.type
== CPP_OPEN_SQUARE
13096 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13097 == CPP_OPEN_SQUARE
)
13098 /* An unnamed namespace definition. */
13099 || token2
.type
== CPP_OPEN_BRACE
13100 || token2
.keyword
== RID_ATTRIBUTE
))
13101 cp_parser_namespace_definition (parser
);
13102 /* An inline (associated) namespace definition. */
13103 else if (token1
.keyword
== RID_INLINE
13104 && token2
.keyword
== RID_NAMESPACE
)
13105 cp_parser_namespace_definition (parser
);
13106 /* Objective-C++ declaration/definition. */
13107 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13108 cp_parser_objc_declaration (parser
, NULL_TREE
);
13109 else if (c_dialect_objc ()
13110 && token1
.keyword
== RID_ATTRIBUTE
13111 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13112 cp_parser_objc_declaration (parser
, attributes
);
13113 /* At this point we may have a template declared by a concept
13115 else if (flag_concepts
13116 && cp_parser_template_declaration_after_export (parser
,
13117 /*member_p=*/false))
13120 /* Try to parse a block-declaration, or a function-definition. */
13121 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13123 /* Free any declarators allocated. */
13124 obstack_free (&declarator_obstack
, p
);
13127 /* Parse a namespace-scope declaration. */
13130 cp_parser_toplevel_declaration (cp_parser
* parser
)
13132 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13134 if (token
->type
== CPP_PRAGMA
)
13135 /* A top-level declaration can consist solely of a #pragma. A
13136 nested declaration cannot, so this is done here and not in
13137 cp_parser_declaration. (A #pragma at block scope is
13138 handled in cp_parser_statement.) */
13139 cp_parser_pragma (parser
, pragma_external
, NULL
);
13140 else if (token
->type
== CPP_SEMICOLON
)
13142 /* A declaration consisting of a single semicolon is
13143 invalid. Allow it unless we're being pedantic. */
13144 cp_lexer_consume_token (parser
->lexer
);
13145 if (!in_system_header_at (input_location
))
13146 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13149 /* Parse the declaration itself. */
13150 cp_parser_declaration (parser
);
13153 /* Parse a block-declaration.
13158 namespace-alias-definition
13165 __extension__ block-declaration
13170 static_assert-declaration
13172 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13173 part of a declaration-statement. */
13176 cp_parser_block_declaration (cp_parser
*parser
,
13180 int saved_pedantic
;
13182 /* Check for the `__extension__' keyword. */
13183 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13185 /* Parse the qualified declaration. */
13186 cp_parser_block_declaration (parser
, statement_p
);
13187 /* Restore the PEDANTIC flag. */
13188 pedantic
= saved_pedantic
;
13193 /* Peek at the next token to figure out which kind of declaration is
13195 token1
= cp_lexer_peek_token (parser
->lexer
);
13197 /* If the next keyword is `asm', we have an asm-definition. */
13198 if (token1
->keyword
== RID_ASM
)
13201 cp_parser_commit_to_tentative_parse (parser
);
13202 cp_parser_asm_definition (parser
);
13204 /* If the next keyword is `namespace', we have a
13205 namespace-alias-definition. */
13206 else if (token1
->keyword
== RID_NAMESPACE
)
13207 cp_parser_namespace_alias_definition (parser
);
13208 /* If the next keyword is `using', we have a
13209 using-declaration, a using-directive, or an alias-declaration. */
13210 else if (token1
->keyword
== RID_USING
)
13215 cp_parser_commit_to_tentative_parse (parser
);
13216 /* If the token after `using' is `namespace', then we have a
13217 using-directive. */
13218 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13219 if (token2
->keyword
== RID_NAMESPACE
)
13220 cp_parser_using_directive (parser
);
13221 /* If the second token after 'using' is '=', then we have an
13222 alias-declaration. */
13223 else if (cxx_dialect
>= cxx11
13224 && token2
->type
== CPP_NAME
13225 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13226 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13227 cp_parser_alias_declaration (parser
);
13228 /* Otherwise, it's a using-declaration. */
13230 cp_parser_using_declaration (parser
,
13231 /*access_declaration_p=*/false);
13233 /* If the next keyword is `__label__' we have a misplaced label
13235 else if (token1
->keyword
== RID_LABEL
)
13237 cp_lexer_consume_token (parser
->lexer
);
13238 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13239 cp_parser_skip_to_end_of_statement (parser
);
13240 /* If the next token is now a `;', consume it. */
13241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13242 cp_lexer_consume_token (parser
->lexer
);
13244 /* If the next token is `static_assert' we have a static assertion. */
13245 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13246 cp_parser_static_assert (parser
, /*member_p=*/false);
13247 /* Anything else must be a simple-declaration. */
13249 cp_parser_simple_declaration (parser
, !statement_p
,
13250 /*maybe_range_for_decl*/NULL
);
13253 /* Parse a simple-declaration.
13255 simple-declaration:
13256 decl-specifier-seq [opt] init-declarator-list [opt] ;
13257 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13258 brace-or-equal-initializer ;
13260 init-declarator-list:
13262 init-declarator-list , init-declarator
13264 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13265 function-definition as a simple-declaration.
13267 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13268 parsed declaration if it is an uninitialized single declarator not followed
13269 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13270 if present, will not be consumed. */
13273 cp_parser_simple_declaration (cp_parser
* parser
,
13274 bool function_definition_allowed_p
,
13275 tree
*maybe_range_for_decl
)
13277 cp_decl_specifier_seq decl_specifiers
;
13278 int declares_class_or_enum
;
13279 bool saw_declarator
;
13280 location_t comma_loc
= UNKNOWN_LOCATION
;
13281 location_t init_loc
= UNKNOWN_LOCATION
;
13283 if (maybe_range_for_decl
)
13284 *maybe_range_for_decl
= NULL_TREE
;
13286 /* Defer access checks until we know what is being declared; the
13287 checks for names appearing in the decl-specifier-seq should be
13288 done as if we were in the scope of the thing being declared. */
13289 push_deferring_access_checks (dk_deferred
);
13291 /* Parse the decl-specifier-seq. We have to keep track of whether
13292 or not the decl-specifier-seq declares a named class or
13293 enumeration type, since that is the only case in which the
13294 init-declarator-list is allowed to be empty.
13298 In a simple-declaration, the optional init-declarator-list can be
13299 omitted only when declaring a class or enumeration, that is when
13300 the decl-specifier-seq contains either a class-specifier, an
13301 elaborated-type-specifier, or an enum-specifier. */
13302 cp_parser_decl_specifier_seq (parser
,
13303 CP_PARSER_FLAGS_OPTIONAL
,
13305 &declares_class_or_enum
);
13306 /* We no longer need to defer access checks. */
13307 stop_deferring_access_checks ();
13309 /* In a block scope, a valid declaration must always have a
13310 decl-specifier-seq. By not trying to parse declarators, we can
13311 resolve the declaration/expression ambiguity more quickly. */
13312 if (!function_definition_allowed_p
13313 && !decl_specifiers
.any_specifiers_p
)
13315 cp_parser_error (parser
, "expected declaration");
13319 /* If the next two tokens are both identifiers, the code is
13320 erroneous. The usual cause of this situation is code like:
13324 where "T" should name a type -- but does not. */
13325 if (!decl_specifiers
.any_type_specifiers_p
13326 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13328 /* If parsing tentatively, we should commit; we really are
13329 looking at a declaration. */
13330 cp_parser_commit_to_tentative_parse (parser
);
13335 cp_parser_maybe_commit_to_declaration (parser
,
13336 decl_specifiers
.any_specifiers_p
);
13338 /* Look for C++17 decomposition declaration. */
13339 for (size_t n
= 1; ; n
++)
13340 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13341 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13343 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13344 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13345 && decl_specifiers
.any_specifiers_p
)
13348 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13349 maybe_range_for_decl
,
13352 /* The next token should be either a `,' or a `;'. */
13353 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13354 /* If it's a `;', we are done. */
13355 if (token
->type
== CPP_SEMICOLON
)
13357 else if (maybe_range_for_decl
)
13359 if (*maybe_range_for_decl
== NULL_TREE
)
13360 *maybe_range_for_decl
= error_mark_node
;
13363 /* Anything else is an error. */
13366 /* If we have already issued an error message we don't need
13367 to issue another one. */
13368 if ((decl
!= error_mark_node
13369 && DECL_INITIAL (decl
) != error_mark_node
)
13370 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13371 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13372 /* Skip tokens until we reach the end of the statement. */
13373 cp_parser_skip_to_end_of_statement (parser
);
13374 /* If the next token is now a `;', consume it. */
13375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13376 cp_lexer_consume_token (parser
->lexer
);
13384 bool auto_specifier_p
;
13385 /* NULL_TREE if both variable and function declaration are allowed,
13386 error_mark_node if function declaration are not allowed and
13387 a FUNCTION_DECL that should be diagnosed if it is followed by
13388 variable declarations. */
13389 tree auto_function_declaration
;
13391 last_type
= NULL_TREE
;
13393 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13394 auto_function_declaration
= NULL_TREE
;
13396 /* Keep going until we hit the `;' at the end of the simple
13398 saw_declarator
= false;
13399 while (cp_lexer_next_token_is_not (parser
->lexer
,
13403 bool function_definition_p
;
13405 tree auto_result
= NULL_TREE
;
13407 if (saw_declarator
)
13409 /* If we are processing next declarator, comma is expected */
13410 token
= cp_lexer_peek_token (parser
->lexer
);
13411 gcc_assert (token
->type
== CPP_COMMA
);
13412 cp_lexer_consume_token (parser
->lexer
);
13413 if (maybe_range_for_decl
)
13415 *maybe_range_for_decl
= error_mark_node
;
13416 if (comma_loc
== UNKNOWN_LOCATION
)
13417 comma_loc
= token
->location
;
13421 saw_declarator
= true;
13423 /* Parse the init-declarator. */
13424 decl
= cp_parser_init_declarator (parser
,
13425 CP_PARSER_FLAGS_NONE
,
13428 function_definition_allowed_p
,
13429 /*member_p=*/false,
13430 declares_class_or_enum
,
13431 &function_definition_p
,
13432 maybe_range_for_decl
,
13435 /* If an error occurred while parsing tentatively, exit quickly.
13436 (That usually happens when in the body of a function; each
13437 statement is treated as a declaration-statement until proven
13439 if (cp_parser_error_occurred (parser
))
13442 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13444 /* If the init-declarator-list contains more than one
13445 init-declarator, they shall all form declarations of
13447 if (auto_function_declaration
== NULL_TREE
)
13448 auto_function_declaration
13449 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13450 else if (TREE_CODE (decl
) == FUNCTION_DECL
13451 || auto_function_declaration
!= error_mark_node
)
13453 error_at (decl_specifiers
.locations
[ds_type_spec
],
13454 "non-variable %qD in declaration with more than one "
13455 "declarator with placeholder type",
13456 TREE_CODE (decl
) == FUNCTION_DECL
13457 ? decl
: auto_function_declaration
);
13458 auto_function_declaration
= error_mark_node
;
13463 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13466 && last_type
!= error_mark_node
13467 && !same_type_p (auto_result
, last_type
))
13469 /* If the list of declarators contains more than one declarator,
13470 the type of each declared variable is determined as described
13471 above. If the type deduced for the template parameter U is not
13472 the same in each deduction, the program is ill-formed. */
13473 error_at (decl_specifiers
.locations
[ds_type_spec
],
13474 "inconsistent deduction for %qT: %qT and then %qT",
13475 decl_specifiers
.type
, last_type
, auto_result
);
13476 last_type
= error_mark_node
;
13479 last_type
= auto_result
;
13482 /* Handle function definitions specially. */
13483 if (function_definition_p
)
13485 /* If the next token is a `,', then we are probably
13486 processing something like:
13490 which is erroneous. */
13491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13493 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13494 error_at (token
->location
,
13496 " declarations and function-definitions is forbidden");
13498 /* Otherwise, we're done with the list of declarators. */
13501 pop_deferring_access_checks ();
13505 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13506 *maybe_range_for_decl
= decl
;
13507 /* The next token should be either a `,' or a `;'. */
13508 token
= cp_lexer_peek_token (parser
->lexer
);
13509 /* If it's a `,', there are more declarators to come. */
13510 if (token
->type
== CPP_COMMA
)
13511 /* will be consumed next time around */;
13512 /* If it's a `;', we are done. */
13513 else if (token
->type
== CPP_SEMICOLON
)
13515 else if (maybe_range_for_decl
)
13517 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13518 permerror (decl_specifiers
.locations
[ds_type_spec
],
13519 "types may not be defined in a for-range-declaration");
13522 /* Anything else is an error. */
13525 /* If we have already issued an error message we don't need
13526 to issue another one. */
13527 if ((decl
!= error_mark_node
13528 && DECL_INITIAL (decl
) != error_mark_node
)
13529 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13530 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13531 /* Skip tokens until we reach the end of the statement. */
13532 cp_parser_skip_to_end_of_statement (parser
);
13533 /* If the next token is now a `;', consume it. */
13534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13535 cp_lexer_consume_token (parser
->lexer
);
13538 /* After the first time around, a function-definition is not
13539 allowed -- even if it was OK at first. For example:
13544 function_definition_allowed_p
= false;
13547 /* Issue an error message if no declarators are present, and the
13548 decl-specifier-seq does not itself declare a class or
13549 enumeration: [dcl.dcl]/3. */
13550 if (!saw_declarator
)
13552 if (cp_parser_declares_only_class_p (parser
))
13554 if (!declares_class_or_enum
13555 && decl_specifiers
.type
13556 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13557 /* Ensure an error is issued anyway when finish_decltype_type,
13558 called via cp_parser_decl_specifier_seq, returns a class or
13559 an enumeration (c++/51786). */
13560 decl_specifiers
.type
= NULL_TREE
;
13561 shadow_tag (&decl_specifiers
);
13563 /* Perform any deferred access checks. */
13564 perform_deferred_access_checks (tf_warning_or_error
);
13567 /* Consume the `;'. */
13569 if (!maybe_range_for_decl
)
13570 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13571 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13573 if (init_loc
!= UNKNOWN_LOCATION
)
13574 error_at (init_loc
, "initializer in range-based %<for%> loop");
13575 if (comma_loc
!= UNKNOWN_LOCATION
)
13576 error_at (comma_loc
,
13577 "multiple declarations in range-based %<for%> loop");
13581 pop_deferring_access_checks ();
13584 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13585 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13589 cp_parser_decomposition_declaration (cp_parser
*parser
,
13590 cp_decl_specifier_seq
*decl_specifiers
,
13591 tree
*maybe_range_for_decl
,
13592 location_t
*init_loc
)
13594 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13595 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13596 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13598 /* Parse the identifier-list. */
13599 auto_vec
<cp_expr
, 10> v
;
13600 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13603 cp_expr e
= cp_parser_identifier (parser
);
13604 if (e
.get_value () == error_mark_node
)
13607 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13609 cp_lexer_consume_token (parser
->lexer
);
13612 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13613 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13615 end_loc
= UNKNOWN_LOCATION
;
13616 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13619 cp_lexer_consume_token (parser
->lexer
);
13622 cp_parser_skip_to_end_of_statement (parser
);
13623 return error_mark_node
;
13627 if (cxx_dialect
< cxx17
)
13628 pedwarn (loc
, 0, "structured bindings only available with "
13629 "-std=c++17 or -std=gnu++17");
13632 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13633 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13634 declarator
->id_loc
= loc
;
13635 if (ref_qual
!= REF_QUAL_NONE
)
13636 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13637 ref_qual
== REF_QUAL_RVALUE
,
13639 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13640 NULL_TREE
, decl_specifiers
->attributes
,
13642 tree orig_decl
= decl
;
13646 cp_decl_specifier_seq decl_specs
;
13647 clear_decl_specs (&decl_specs
);
13648 decl_specs
.type
= make_auto ();
13650 FOR_EACH_VEC_ELT (v
, i
, e
)
13653 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13654 sfk_none
, e
.get_location ());
13657 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13658 declarator
->id_loc
= e
.get_location ();
13660 tree elt_pushed_scope
;
13661 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13662 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13663 if (decl2
== error_mark_node
)
13664 decl
= error_mark_node
;
13665 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13667 /* Ensure we've diagnosed redeclaration if we aren't creating
13669 gcc_assert (errorcount
);
13670 decl
= error_mark_node
;
13674 if (elt_pushed_scope
)
13675 pop_scope (elt_pushed_scope
);
13680 error_at (loc
, "empty structured binding declaration");
13681 decl
= error_mark_node
;
13684 if (maybe_range_for_decl
== NULL
13685 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13687 bool non_constant_p
= false, is_direct_init
= false;
13688 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13689 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13691 if (initializer
== NULL_TREE
13692 || (TREE_CODE (initializer
) == TREE_LIST
13693 && TREE_CHAIN (initializer
))
13695 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13696 && CONSTRUCTOR_NELTS (initializer
) != 1))
13698 error_at (loc
, "invalid initializer for structured binding "
13700 initializer
= error_mark_node
;
13703 if (decl
!= error_mark_node
)
13705 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13706 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13707 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13708 cp_finish_decomp (decl
, prev
, v
.length ());
13711 else if (decl
!= error_mark_node
)
13713 *maybe_range_for_decl
= prev
;
13714 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13715 the underlying DECL. */
13716 cp_finish_decomp (decl
, prev
, v
.length ());
13720 pop_scope (pushed_scope
);
13722 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13724 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13725 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13731 /* Parse a decl-specifier-seq.
13733 decl-specifier-seq:
13734 decl-specifier-seq [opt] decl-specifier
13735 decl-specifier attribute-specifier-seq [opt] (C++11)
13738 storage-class-specifier
13749 Concepts Extension:
13754 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13756 The parser flags FLAGS is used to control type-specifier parsing.
13758 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13761 1: one of the decl-specifiers is an elaborated-type-specifier
13762 (i.e., a type declaration)
13763 2: one of the decl-specifiers is an enum-specifier or a
13764 class-specifier (i.e., a type definition)
13769 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13770 cp_parser_flags flags
,
13771 cp_decl_specifier_seq
*decl_specs
,
13772 int* declares_class_or_enum
)
13774 bool constructor_possible_p
= !parser
->in_declarator_p
;
13775 bool found_decl_spec
= false;
13776 cp_token
*start_token
= NULL
;
13779 /* Clear DECL_SPECS. */
13780 clear_decl_specs (decl_specs
);
13782 /* Assume no class or enumeration type is declared. */
13783 *declares_class_or_enum
= 0;
13785 /* Keep reading specifiers until there are no more to read. */
13788 bool constructor_p
;
13792 /* Peek at the next token. */
13793 token
= cp_lexer_peek_token (parser
->lexer
);
13795 /* Save the first token of the decl spec list for error
13798 start_token
= token
;
13799 /* Handle attributes. */
13800 if (cp_next_tokens_can_be_attribute_p (parser
))
13802 /* Parse the attributes. */
13803 tree attrs
= cp_parser_attributes_opt (parser
);
13805 /* In a sequence of declaration specifiers, c++11 attributes
13806 appertain to the type that precede them. In that case
13809 The attribute-specifier-seq affects the type only for
13810 the declaration it appears in, not other declarations
13811 involving the same type.
13813 But for now let's force the user to position the
13814 attribute either at the beginning of the declaration or
13815 after the declarator-id, which would clearly mean that it
13816 applies to the declarator. */
13817 if (cxx11_attribute_p (attrs
))
13819 if (!found_decl_spec
)
13820 /* The c++11 attribute is at the beginning of the
13821 declaration. It appertains to the entity being
13825 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13827 /* This is an attribute following a
13828 class-specifier. */
13829 if (decl_specs
->type_definition_p
)
13830 warn_misplaced_attr_for_class_type (token
->location
,
13836 decl_specs
->std_attributes
13837 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13838 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13839 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13845 decl_specs
->attributes
13846 = attr_chainon (decl_specs
->attributes
, attrs
);
13847 if (decl_specs
->locations
[ds_attribute
] == 0)
13848 decl_specs
->locations
[ds_attribute
] = token
->location
;
13851 /* Assume we will find a decl-specifier keyword. */
13852 found_decl_spec
= true;
13853 /* If the next token is an appropriate keyword, we can simply
13854 add it to the list. */
13855 switch (token
->keyword
)
13861 if (!at_class_scope_p ())
13863 gcc_rich_location
richloc (token
->location
);
13864 richloc
.add_fixit_remove ();
13865 error_at (&richloc
, "%<friend%> used outside of class");
13866 cp_lexer_purge_token (parser
->lexer
);
13871 /* Consume the token. */
13872 cp_lexer_consume_token (parser
->lexer
);
13876 case RID_CONSTEXPR
:
13878 cp_lexer_consume_token (parser
->lexer
);
13883 cp_lexer_consume_token (parser
->lexer
);
13886 /* function-specifier:
13893 cp_parser_function_specifier_opt (parser
, decl_specs
);
13900 /* Consume the token. */
13901 cp_lexer_consume_token (parser
->lexer
);
13902 /* A constructor declarator cannot appear in a typedef. */
13903 constructor_possible_p
= false;
13904 /* The "typedef" keyword can only occur in a declaration; we
13905 may as well commit at this point. */
13906 cp_parser_commit_to_tentative_parse (parser
);
13908 if (decl_specs
->storage_class
!= sc_none
)
13909 decl_specs
->conflicting_specifiers_p
= true;
13912 /* storage-class-specifier:
13922 if (cxx_dialect
== cxx98
)
13924 /* Consume the token. */
13925 cp_lexer_consume_token (parser
->lexer
);
13927 /* Complain about `auto' as a storage specifier, if
13928 we're complaining about C++0x compatibility. */
13929 gcc_rich_location
richloc (token
->location
);
13930 richloc
.add_fixit_remove ();
13931 warning_at (&richloc
, OPT_Wc__11_compat
,
13932 "%<auto%> changes meaning in C++11; "
13933 "please remove it");
13935 /* Set the storage class anyway. */
13936 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13940 /* C++0x auto type-specifier. */
13941 found_decl_spec
= false;
13948 /* Consume the token. */
13949 cp_lexer_consume_token (parser
->lexer
);
13950 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13954 /* Consume the token. */
13956 cp_lexer_consume_token (parser
->lexer
);
13960 /* We did not yet find a decl-specifier yet. */
13961 found_decl_spec
= false;
13965 if (found_decl_spec
13966 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13967 && token
->keyword
!= RID_CONSTEXPR
)
13968 error ("decl-specifier invalid in condition");
13970 if (found_decl_spec
13971 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13972 && token
->keyword
!= RID_MUTABLE
13973 && token
->keyword
!= RID_CONSTEXPR
)
13974 error_at (token
->location
, "%qD invalid in lambda",
13975 ridpointers
[token
->keyword
]);
13978 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13980 /* Constructors are a special case. The `S' in `S()' is not a
13981 decl-specifier; it is the beginning of the declarator. */
13983 = (!found_decl_spec
13984 && constructor_possible_p
13985 && (cp_parser_constructor_declarator_p
13986 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13988 /* If we don't have a DECL_SPEC yet, then we must be looking at
13989 a type-specifier. */
13990 if (!found_decl_spec
&& !constructor_p
)
13992 int decl_spec_declares_class_or_enum
;
13993 bool is_cv_qualifier
;
13997 = cp_parser_type_specifier (parser
, flags
,
13999 /*is_declaration=*/true,
14000 &decl_spec_declares_class_or_enum
,
14002 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14004 /* If this type-specifier referenced a user-defined type
14005 (a typedef, class-name, etc.), then we can't allow any
14006 more such type-specifiers henceforth.
14010 The longest sequence of decl-specifiers that could
14011 possibly be a type name is taken as the
14012 decl-specifier-seq of a declaration. The sequence shall
14013 be self-consistent as described below.
14017 As a general rule, at most one type-specifier is allowed
14018 in the complete decl-specifier-seq of a declaration. The
14019 only exceptions are the following:
14021 -- const or volatile can be combined with any other
14024 -- signed or unsigned can be combined with char, long,
14032 void g (const int Pc);
14034 Here, Pc is *not* part of the decl-specifier seq; it's
14035 the declarator. Therefore, once we see a type-specifier
14036 (other than a cv-qualifier), we forbid any additional
14037 user-defined types. We *do* still allow things like `int
14038 int' to be considered a decl-specifier-seq, and issue the
14039 error message later. */
14040 if (type_spec
&& !is_cv_qualifier
)
14041 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14042 /* A constructor declarator cannot follow a type-specifier. */
14045 constructor_possible_p
= false;
14046 found_decl_spec
= true;
14047 if (!is_cv_qualifier
)
14048 decl_specs
->any_type_specifiers_p
= true;
14050 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14051 error_at (token
->location
, "type-specifier invalid in lambda");
14055 /* If we still do not have a DECL_SPEC, then there are no more
14056 decl-specifiers. */
14057 if (!found_decl_spec
)
14060 decl_specs
->any_specifiers_p
= true;
14061 /* After we see one decl-specifier, further decl-specifiers are
14062 always optional. */
14063 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14066 /* Don't allow a friend specifier with a class definition. */
14067 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14068 && (*declares_class_or_enum
& 2))
14069 error_at (decl_specs
->locations
[ds_friend
],
14070 "class definition may not be declared a friend");
14073 /* Parse an (optional) storage-class-specifier.
14075 storage-class-specifier:
14084 storage-class-specifier:
14087 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14090 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14092 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14095 if (cxx_dialect
!= cxx98
)
14097 /* Fall through for C++98. */
14098 gcc_fallthrough ();
14105 /* Consume the token. */
14106 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14113 /* Parse an (optional) function-specifier.
14115 function-specifier:
14121 explicit(constant-expression)
14123 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14124 Updates DECL_SPECS, if it is non-NULL. */
14127 cp_parser_function_specifier_opt (cp_parser
* parser
,
14128 cp_decl_specifier_seq
*decl_specs
)
14130 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14131 switch (token
->keyword
)
14134 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14138 /* 14.5.2.3 [temp.mem]
14140 A member function template shall not be virtual. */
14141 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14142 && current_class_type
)
14143 error_at (token
->location
, "templates may not be %<virtual%>");
14145 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14150 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14151 /* If we see '(', it's C++20 explicit(bool). */
14153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14155 matching_parens parens
;
14156 parens
.consume_open (parser
);
14158 /* New types are not allowed in an explicit-specifier. */
14159 const char *saved_message
14160 = parser
->type_definition_forbidden_message
;
14161 parser
->type_definition_forbidden_message
14162 = G_("types may not be defined in explicit-specifier");
14164 if (cxx_dialect
< cxx2a
)
14165 pedwarn (token
->location
, 0,
14166 "%<explicit(bool)%> only available with -std=c++2a "
14167 "or -std=gnu++2a");
14169 /* Parse the constant-expression. */
14170 expr
= cp_parser_constant_expression (parser
);
14172 /* Restore the saved message. */
14173 parser
->type_definition_forbidden_message
= saved_message
;
14174 parens
.require_close (parser
);
14177 /* The explicit-specifier explicit without a constant-expression is
14178 equivalent to the explicit-specifier explicit(true). */
14179 expr
= boolean_true_node
;
14182 "the constant-expression, if supplied, shall be a contextually
14183 converted constant expression of type bool." */
14184 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14185 /* We could evaluate it -- mark the decl as appropriate. */
14186 if (expr
== boolean_true_node
)
14187 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14188 else if (expr
== boolean_false_node
)
14189 /* Don't mark the decl as explicit. */;
14190 else if (decl_specs
)
14191 /* The expression was value-dependent. Remember it so that we can
14192 substitute it later. */
14193 decl_specs
->explicit_specifier
= expr
;
14201 /* Consume the token. */
14202 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14205 /* Parse a linkage-specification.
14207 linkage-specification:
14208 extern string-literal { declaration-seq [opt] }
14209 extern string-literal declaration */
14212 cp_parser_linkage_specification (cp_parser
* parser
)
14216 /* Look for the `extern' keyword. */
14217 cp_token
*extern_token
14218 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14220 /* Look for the string-literal. */
14221 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14222 linkage
= cp_parser_string_literal (parser
, false, false);
14224 /* Transform the literal into an identifier. If the literal is a
14225 wide-character string, or contains embedded NULs, then we can't
14226 handle it as the user wants. */
14227 if (strlen (TREE_STRING_POINTER (linkage
))
14228 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14230 cp_parser_error (parser
, "invalid linkage-specification");
14231 /* Assume C++ linkage. */
14232 linkage
= lang_name_cplusplus
;
14235 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14237 /* We're now using the new linkage. */
14238 push_lang_context (linkage
);
14240 /* Preserve the location of the the innermost linkage specification,
14241 tracking the locations of nested specifications via a local. */
14242 location_t saved_location
14243 = parser
->innermost_linkage_specification_location
;
14244 /* Construct a location ranging from the start of the "extern" to
14245 the end of the string-literal, with the caret at the start, e.g.:
14249 parser
->innermost_linkage_specification_location
14250 = make_location (extern_token
->location
,
14251 extern_token
->location
,
14252 get_finish (string_token
->location
));
14254 /* If the next token is a `{', then we're using the first
14256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14258 cp_ensure_no_omp_declare_simd (parser
);
14259 cp_ensure_no_oacc_routine (parser
);
14261 /* Consume the `{' token. */
14262 matching_braces braces
;
14263 braces
.consume_open (parser
);
14264 /* Parse the declarations. */
14265 cp_parser_declaration_seq_opt (parser
);
14266 /* Look for the closing `}'. */
14267 braces
.require_close (parser
);
14269 /* Otherwise, there's just one declaration. */
14272 bool saved_in_unbraced_linkage_specification_p
;
14274 saved_in_unbraced_linkage_specification_p
14275 = parser
->in_unbraced_linkage_specification_p
;
14276 parser
->in_unbraced_linkage_specification_p
= true;
14277 cp_parser_declaration (parser
);
14278 parser
->in_unbraced_linkage_specification_p
14279 = saved_in_unbraced_linkage_specification_p
;
14282 /* We're done with the linkage-specification. */
14283 pop_lang_context ();
14285 /* Restore location of parent linkage specification, if any. */
14286 parser
->innermost_linkage_specification_location
= saved_location
;
14289 /* Parse a static_assert-declaration.
14291 static_assert-declaration:
14292 static_assert ( constant-expression , string-literal ) ;
14293 static_assert ( constant-expression ) ; (C++17)
14295 If MEMBER_P, this static_assert is a class member. */
14298 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14301 location_t token_loc
;
14305 /* Peek at the `static_assert' token so we can keep track of exactly
14306 where the static assertion started. */
14307 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14309 /* Look for the `static_assert' keyword. */
14310 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14314 /* We know we are in a static assertion; commit to any tentative
14316 if (cp_parser_parsing_tentatively (parser
))
14317 cp_parser_commit_to_tentative_parse (parser
);
14319 /* Parse the `(' starting the static assertion condition. */
14320 matching_parens parens
;
14321 parens
.require_open (parser
);
14323 /* Parse the constant-expression. Allow a non-constant expression
14324 here in order to give better diagnostics in finish_static_assert. */
14326 cp_parser_constant_expression (parser
,
14327 /*allow_non_constant_p=*/true,
14328 /*non_constant_p=*/&dummy
);
14330 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14332 if (cxx_dialect
< cxx17
)
14333 pedwarn (input_location
, OPT_Wpedantic
,
14334 "static_assert without a message "
14335 "only available with -std=c++17 or -std=gnu++17");
14337 cp_lexer_consume_token (parser
->lexer
);
14338 message
= build_string (1, "");
14339 TREE_TYPE (message
) = char_array_type_node
;
14340 fix_string_type (message
);
14344 /* Parse the separating `,'. */
14345 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14347 /* Parse the string-literal message. */
14348 message
= cp_parser_string_literal (parser
,
14349 /*translate=*/false,
14352 /* A `)' completes the static assertion. */
14353 if (!parens
.require_close (parser
))
14354 cp_parser_skip_to_closing_parenthesis (parser
,
14355 /*recovering=*/true,
14356 /*or_comma=*/false,
14357 /*consume_paren=*/true);
14360 /* A semicolon terminates the declaration. */
14361 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14363 /* Get the location for the static assertion. Use that of the
14364 condition if available, otherwise, use that of the "static_assert"
14366 location_t assert_loc
= condition
.get_location ();
14367 if (assert_loc
== UNKNOWN_LOCATION
)
14368 assert_loc
= token_loc
;
14370 /* Complete the static assertion, which may mean either processing
14371 the static assert now or saving it for template instantiation. */
14372 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14375 /* Parse the expression in decltype ( expression ). */
14378 cp_parser_decltype_expr (cp_parser
*parser
,
14379 bool &id_expression_or_member_access_p
)
14381 cp_token
*id_expr_start_token
;
14384 /* Since we're going to preserve any side-effects from this parse, set up a
14385 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14386 in the expression. */
14387 tentative_firewall
firewall (parser
);
14389 /* First, try parsing an id-expression. */
14390 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14391 cp_parser_parse_tentatively (parser
);
14392 expr
= cp_parser_id_expression (parser
,
14393 /*template_keyword_p=*/false,
14394 /*check_dependency_p=*/true,
14395 /*template_p=*/NULL
,
14396 /*declarator_p=*/false,
14397 /*optional_p=*/false);
14399 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14401 bool non_integral_constant_expression_p
= false;
14402 tree id_expression
= expr
;
14404 const char *error_msg
;
14406 if (identifier_p (expr
))
14407 /* Lookup the name we got back from the id-expression. */
14408 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14409 id_expr_start_token
->location
);
14411 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14412 /* A template without args is not a complete id-expression. */
14413 expr
= error_mark_node
;
14416 && expr
!= error_mark_node
14417 && TREE_CODE (expr
) != TYPE_DECL
14418 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14419 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14420 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14422 /* Complete lookup of the id-expression. */
14423 expr
= (finish_id_expression
14424 (id_expression
, expr
, parser
->scope
, &idk
,
14425 /*integral_constant_expression_p=*/false,
14426 /*allow_non_integral_constant_expression_p=*/true,
14427 &non_integral_constant_expression_p
,
14428 /*template_p=*/false,
14430 /*address_p=*/false,
14431 /*template_arg_p=*/false,
14433 id_expr_start_token
->location
));
14435 if (expr
== error_mark_node
)
14436 /* We found an id-expression, but it was something that we
14437 should not have found. This is an error, not something
14438 we can recover from, so note that we found an
14439 id-expression and we'll recover as gracefully as
14441 id_expression_or_member_access_p
= true;
14445 && expr
!= error_mark_node
14446 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14447 /* We have an id-expression. */
14448 id_expression_or_member_access_p
= true;
14451 if (!id_expression_or_member_access_p
)
14453 /* Abort the id-expression parse. */
14454 cp_parser_abort_tentative_parse (parser
);
14456 /* Parsing tentatively, again. */
14457 cp_parser_parse_tentatively (parser
);
14459 /* Parse a class member access. */
14460 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14461 /*cast_p=*/false, /*decltype*/true,
14462 /*member_access_only_p=*/true, NULL
);
14465 && expr
!= error_mark_node
14466 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14467 /* We have an id-expression. */
14468 id_expression_or_member_access_p
= true;
14471 if (id_expression_or_member_access_p
)
14472 /* We have parsed the complete id-expression or member access. */
14473 cp_parser_parse_definitely (parser
);
14476 /* Abort our attempt to parse an id-expression or member access
14478 cp_parser_abort_tentative_parse (parser
);
14480 /* Commit to the tentative_firewall so we get syntax errors. */
14481 cp_parser_commit_to_tentative_parse (parser
);
14483 /* Parse a full expression. */
14484 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14485 /*decltype_p=*/true);
14491 /* Parse a `decltype' type. Returns the type.
14493 simple-type-specifier:
14494 decltype ( expression )
14496 decltype ( auto ) */
14499 cp_parser_decltype (cp_parser
*parser
)
14501 bool id_expression_or_member_access_p
= false;
14502 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14504 if (start_token
->type
== CPP_DECLTYPE
)
14506 /* Already parsed. */
14507 cp_lexer_consume_token (parser
->lexer
);
14508 return saved_checks_value (start_token
->u
.tree_check_value
);
14511 /* Look for the `decltype' token. */
14512 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14513 return error_mark_node
;
14515 /* Parse the opening `('. */
14516 matching_parens parens
;
14517 if (!parens
.require_open (parser
))
14518 return error_mark_node
;
14520 push_deferring_access_checks (dk_deferred
);
14522 tree expr
= NULL_TREE
;
14524 if (cxx_dialect
>= cxx14
14525 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14526 /* decltype (auto) */
14527 cp_lexer_consume_token (parser
->lexer
);
14530 /* decltype (expression) */
14532 /* Types cannot be defined in a `decltype' expression. Save away the
14533 old message and set the new one. */
14534 const char *saved_message
= parser
->type_definition_forbidden_message
;
14535 parser
->type_definition_forbidden_message
14536 = G_("types may not be defined in %<decltype%> expressions");
14538 /* The restrictions on constant-expressions do not apply inside
14539 decltype expressions. */
14540 bool saved_integral_constant_expression_p
14541 = parser
->integral_constant_expression_p
;
14542 bool saved_non_integral_constant_expression_p
14543 = parser
->non_integral_constant_expression_p
;
14544 parser
->integral_constant_expression_p
= false;
14546 /* Within a parenthesized expression, a `>' token is always
14547 the greater-than operator. */
14548 bool saved_greater_than_is_operator_p
14549 = parser
->greater_than_is_operator_p
;
14550 parser
->greater_than_is_operator_p
= true;
14552 /* Do not actually evaluate the expression. */
14553 ++cp_unevaluated_operand
;
14555 /* Do not warn about problems with the expression. */
14556 ++c_inhibit_evaluation_warnings
;
14558 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14559 STRIP_ANY_LOCATION_WRAPPER (expr
);
14561 /* Go back to evaluating expressions. */
14562 --cp_unevaluated_operand
;
14563 --c_inhibit_evaluation_warnings
;
14565 /* The `>' token might be the end of a template-id or
14566 template-parameter-list now. */
14567 parser
->greater_than_is_operator_p
14568 = saved_greater_than_is_operator_p
;
14570 /* Restore the old message and the integral constant expression
14572 parser
->type_definition_forbidden_message
= saved_message
;
14573 parser
->integral_constant_expression_p
14574 = saved_integral_constant_expression_p
;
14575 parser
->non_integral_constant_expression_p
14576 = saved_non_integral_constant_expression_p
;
14579 /* Parse to the closing `)'. */
14580 if (!parens
.require_close (parser
))
14582 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14583 /*consume_paren=*/true);
14584 pop_deferring_access_checks ();
14585 return error_mark_node
;
14591 expr
= make_decltype_auto ();
14592 AUTO_IS_DECLTYPE (expr
) = true;
14595 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14596 tf_warning_or_error
);
14598 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14600 start_token
->type
= CPP_DECLTYPE
;
14601 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14602 start_token
->u
.tree_check_value
->value
= expr
;
14603 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14604 start_token
->keyword
= RID_MAX
;
14605 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14607 pop_to_parent_deferring_access_checks ();
14612 /* Special member functions [gram.special] */
14614 /* Parse a conversion-function-id.
14616 conversion-function-id:
14617 operator conversion-type-id
14619 Returns an IDENTIFIER_NODE representing the operator. */
14622 cp_parser_conversion_function_id (cp_parser
* parser
)
14626 tree saved_qualifying_scope
;
14627 tree saved_object_scope
;
14628 tree pushed_scope
= NULL_TREE
;
14630 /* Look for the `operator' token. */
14631 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14632 return error_mark_node
;
14633 /* When we parse the conversion-type-id, the current scope will be
14634 reset. However, we need that information in able to look up the
14635 conversion function later, so we save it here. */
14636 saved_scope
= parser
->scope
;
14637 saved_qualifying_scope
= parser
->qualifying_scope
;
14638 saved_object_scope
= parser
->object_scope
;
14639 /* We must enter the scope of the class so that the names of
14640 entities declared within the class are available in the
14641 conversion-type-id. For example, consider:
14648 S::operator I() { ... }
14650 In order to see that `I' is a type-name in the definition, we
14651 must be in the scope of `S'. */
14653 pushed_scope
= push_scope (saved_scope
);
14654 /* Parse the conversion-type-id. */
14655 type
= cp_parser_conversion_type_id (parser
);
14656 /* Leave the scope of the class, if any. */
14658 pop_scope (pushed_scope
);
14659 /* Restore the saved scope. */
14660 parser
->scope
= saved_scope
;
14661 parser
->qualifying_scope
= saved_qualifying_scope
;
14662 parser
->object_scope
= saved_object_scope
;
14663 /* If the TYPE is invalid, indicate failure. */
14664 if (type
== error_mark_node
)
14665 return error_mark_node
;
14666 return make_conv_op_name (type
);
14669 /* Parse a conversion-type-id:
14671 conversion-type-id:
14672 type-specifier-seq conversion-declarator [opt]
14674 Returns the TYPE specified. */
14677 cp_parser_conversion_type_id (cp_parser
* parser
)
14680 cp_decl_specifier_seq type_specifiers
;
14681 cp_declarator
*declarator
;
14682 tree type_specified
;
14683 const char *saved_message
;
14685 /* Parse the attributes. */
14686 attributes
= cp_parser_attributes_opt (parser
);
14688 saved_message
= parser
->type_definition_forbidden_message
;
14689 parser
->type_definition_forbidden_message
14690 = G_("types may not be defined in a conversion-type-id");
14692 /* Parse the type-specifiers. */
14693 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14694 /*is_declaration=*/false,
14695 /*is_trailing_return=*/false,
14698 parser
->type_definition_forbidden_message
= saved_message
;
14700 /* If that didn't work, stop. */
14701 if (type_specifiers
.type
== error_mark_node
)
14702 return error_mark_node
;
14703 /* Parse the conversion-declarator. */
14704 declarator
= cp_parser_conversion_declarator_opt (parser
);
14706 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14707 /*initialized=*/0, &attributes
);
14709 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14711 /* Don't give this error when parsing tentatively. This happens to
14712 work because we always parse this definitively once. */
14713 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14714 && type_uses_auto (type_specified
))
14716 if (cxx_dialect
< cxx14
)
14718 error ("invalid use of %<auto%> in conversion operator");
14719 return error_mark_node
;
14721 else if (template_parm_scope_p ())
14722 warning (0, "use of %<auto%> in member template "
14723 "conversion operator can never be deduced");
14726 return type_specified
;
14729 /* Parse an (optional) conversion-declarator.
14731 conversion-declarator:
14732 ptr-operator conversion-declarator [opt]
14736 static cp_declarator
*
14737 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14739 enum tree_code code
;
14740 tree class_type
, std_attributes
= NULL_TREE
;
14741 cp_cv_quals cv_quals
;
14743 /* We don't know if there's a ptr-operator next, or not. */
14744 cp_parser_parse_tentatively (parser
);
14745 /* Try the ptr-operator. */
14746 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14748 /* If it worked, look for more conversion-declarators. */
14749 if (cp_parser_parse_definitely (parser
))
14751 cp_declarator
*declarator
;
14753 /* Parse another optional declarator. */
14754 declarator
= cp_parser_conversion_declarator_opt (parser
);
14756 declarator
= cp_parser_make_indirect_declarator
14757 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14765 /* Parse an (optional) ctor-initializer.
14768 : mem-initializer-list */
14771 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14773 /* If the next token is not a `:', then there is no
14774 ctor-initializer. */
14775 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14777 /* Do default initialization of any bases and members. */
14778 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14779 finish_mem_initializers (NULL_TREE
);
14783 /* Consume the `:' token. */
14784 cp_lexer_consume_token (parser
->lexer
);
14785 /* And the mem-initializer-list. */
14786 cp_parser_mem_initializer_list (parser
);
14789 /* Parse a mem-initializer-list.
14791 mem-initializer-list:
14792 mem-initializer ... [opt]
14793 mem-initializer ... [opt] , mem-initializer-list */
14796 cp_parser_mem_initializer_list (cp_parser
* parser
)
14798 tree mem_initializer_list
= NULL_TREE
;
14799 tree target_ctor
= error_mark_node
;
14800 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14802 /* Let the semantic analysis code know that we are starting the
14803 mem-initializer-list. */
14804 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14805 error_at (token
->location
,
14806 "only constructors take member initializers");
14808 /* Loop through the list. */
14811 tree mem_initializer
;
14813 token
= cp_lexer_peek_token (parser
->lexer
);
14814 /* Parse the mem-initializer. */
14815 mem_initializer
= cp_parser_mem_initializer (parser
);
14816 /* If the next token is a `...', we're expanding member initializers. */
14817 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14819 || (mem_initializer
!= error_mark_node
14820 && check_for_bare_parameter_packs (TREE_PURPOSE
14821 (mem_initializer
))))
14823 /* Consume the `...'. */
14825 cp_lexer_consume_token (parser
->lexer
);
14827 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14828 can be expanded but members cannot. */
14829 if (mem_initializer
!= error_mark_node
14830 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14832 error_at (token
->location
,
14833 "cannot expand initializer for member %qD",
14834 TREE_PURPOSE (mem_initializer
));
14835 mem_initializer
= error_mark_node
;
14838 /* Construct the pack expansion type. */
14839 if (mem_initializer
!= error_mark_node
)
14840 mem_initializer
= make_pack_expansion (mem_initializer
);
14842 if (target_ctor
!= error_mark_node
14843 && mem_initializer
!= error_mark_node
)
14845 error ("mem-initializer for %qD follows constructor delegation",
14846 TREE_PURPOSE (mem_initializer
));
14847 mem_initializer
= error_mark_node
;
14849 /* Look for a target constructor. */
14850 if (mem_initializer
!= error_mark_node
14851 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14852 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14854 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14855 if (mem_initializer_list
)
14857 error ("constructor delegation follows mem-initializer for %qD",
14858 TREE_PURPOSE (mem_initializer_list
));
14859 mem_initializer
= error_mark_node
;
14861 target_ctor
= mem_initializer
;
14863 /* Add it to the list, unless it was erroneous. */
14864 if (mem_initializer
!= error_mark_node
)
14866 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14867 mem_initializer_list
= mem_initializer
;
14869 /* If the next token is not a `,', we're done. */
14870 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14872 /* Consume the `,' token. */
14873 cp_lexer_consume_token (parser
->lexer
);
14876 /* Perform semantic analysis. */
14877 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14878 finish_mem_initializers (mem_initializer_list
);
14881 /* Parse a mem-initializer.
14884 mem-initializer-id ( expression-list [opt] )
14885 mem-initializer-id braced-init-list
14890 ( expression-list [opt] )
14892 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14893 class) or FIELD_DECL (for a non-static data member) to initialize;
14894 the TREE_VALUE is the expression-list. An empty initialization
14895 list is represented by void_list_node. */
14898 cp_parser_mem_initializer (cp_parser
* parser
)
14900 tree mem_initializer_id
;
14901 tree expression_list
;
14903 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14905 /* Find out what is being initialized. */
14906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14908 permerror (token
->location
,
14909 "anachronistic old-style base class initializer");
14910 mem_initializer_id
= NULL_TREE
;
14914 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14915 if (mem_initializer_id
== error_mark_node
)
14916 return mem_initializer_id
;
14918 member
= expand_member_init (mem_initializer_id
);
14919 if (member
&& !DECL_P (member
))
14920 in_base_initializer
= 1;
14922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14924 bool expr_non_constant_p
;
14925 cp_lexer_set_source_position (parser
->lexer
);
14926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14927 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14928 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14929 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14933 vec
<tree
, va_gc
> *vec
;
14934 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14936 /*allow_expansion_p=*/true,
14937 /*non_constant_p=*/NULL
,
14938 /*close_paren_loc=*/NULL
,
14939 /*wrap_locations_p=*/true);
14941 return error_mark_node
;
14942 expression_list
= build_tree_list_vec (vec
);
14943 release_tree_vector (vec
);
14946 if (expression_list
== error_mark_node
)
14947 return error_mark_node
;
14948 if (!expression_list
)
14949 expression_list
= void_type_node
;
14951 in_base_initializer
= 0;
14953 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14956 /* Parse a mem-initializer-id.
14958 mem-initializer-id:
14959 :: [opt] nested-name-specifier [opt] class-name
14960 decltype-specifier (C++11)
14963 Returns a TYPE indicating the class to be initialized for the first
14964 production (and the second in C++11). Returns an IDENTIFIER_NODE
14965 indicating the data member to be initialized for the last production. */
14968 cp_parser_mem_initializer_id (cp_parser
* parser
)
14970 bool global_scope_p
;
14971 bool nested_name_specifier_p
;
14972 bool template_p
= false;
14975 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14977 /* `typename' is not allowed in this context ([temp.res]). */
14978 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14980 error_at (token
->location
,
14981 "keyword %<typename%> not allowed in this context (a qualified "
14982 "member initializer is implicitly a type)");
14983 cp_lexer_consume_token (parser
->lexer
);
14985 /* Look for the optional `::' operator. */
14987 = (cp_parser_global_scope_opt (parser
,
14988 /*current_scope_valid_p=*/false)
14990 /* Look for the optional nested-name-specifier. The simplest way to
14995 The keyword `typename' is not permitted in a base-specifier or
14996 mem-initializer; in these contexts a qualified name that
14997 depends on a template-parameter is implicitly assumed to be a
15000 is to assume that we have seen the `typename' keyword at this
15002 nested_name_specifier_p
15003 = (cp_parser_nested_name_specifier_opt (parser
,
15004 /*typename_keyword_p=*/true,
15005 /*check_dependency_p=*/true,
15007 /*is_declaration=*/true)
15009 if (nested_name_specifier_p
)
15010 template_p
= cp_parser_optional_template_keyword (parser
);
15011 /* If there is a `::' operator or a nested-name-specifier, then we
15012 are definitely looking for a class-name. */
15013 if (global_scope_p
|| nested_name_specifier_p
)
15014 return cp_parser_class_name (parser
,
15015 /*typename_keyword_p=*/true,
15016 /*template_keyword_p=*/template_p
,
15018 /*check_dependency_p=*/true,
15019 /*class_head_p=*/false,
15020 /*is_declaration=*/true);
15021 /* Otherwise, we could also be looking for an ordinary identifier. */
15022 cp_parser_parse_tentatively (parser
);
15023 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15024 /* Try a decltype-specifier. */
15025 id
= cp_parser_decltype (parser
);
15027 /* Otherwise, try a class-name. */
15028 id
= cp_parser_class_name (parser
,
15029 /*typename_keyword_p=*/true,
15030 /*template_keyword_p=*/false,
15032 /*check_dependency_p=*/true,
15033 /*class_head_p=*/false,
15034 /*is_declaration=*/true);
15035 /* If we found one, we're done. */
15036 if (cp_parser_parse_definitely (parser
))
15038 /* Otherwise, look for an ordinary identifier. */
15039 return cp_parser_identifier (parser
);
15042 /* Overloading [gram.over] */
15044 /* Parse an operator-function-id.
15046 operator-function-id:
15049 Returns an IDENTIFIER_NODE for the operator which is a
15050 human-readable spelling of the identifier, e.g., `operator +'. */
15053 cp_parser_operator_function_id (cp_parser
* parser
)
15055 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15056 /* Look for the `operator' keyword. */
15057 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15058 return error_mark_node
;
15059 /* And then the name of the operator itself. */
15060 return cp_parser_operator (parser
, start_loc
);
15063 /* Return an identifier node for a user-defined literal operator.
15064 The suffix identifier is chained to the operator name identifier. */
15067 cp_literal_operator_id (const char* name
)
15070 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15071 + strlen (name
) + 10);
15072 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15073 identifier
= get_identifier (buffer
);
15078 /* Parse an operator.
15081 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15082 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15083 || ++ -- , ->* -> () []
15090 Returns an IDENTIFIER_NODE for the operator which is a
15091 human-readable spelling of the identifier, e.g., `operator +'. */
15094 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15096 tree id
= NULL_TREE
;
15100 /* Peek at the next token. */
15101 token
= cp_lexer_peek_token (parser
->lexer
);
15103 location_t end_loc
= token
->location
;
15105 /* Figure out which operator we have. */
15106 enum tree_code op
= ERROR_MARK
;
15107 bool assop
= false;
15108 bool consumed
= false;
15109 switch (token
->type
)
15113 /* The keyword should be either `new' or `delete'. */
15114 if (token
->keyword
== RID_NEW
)
15116 else if (token
->keyword
== RID_DELETE
)
15121 /* Consume the `new' or `delete' token. */
15122 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15124 /* Peek at the next token. */
15125 token
= cp_lexer_peek_token (parser
->lexer
);
15126 /* If it's a `[' token then this is the array variant of the
15128 if (token
->type
== CPP_OPEN_SQUARE
)
15130 /* Consume the `[' token. */
15131 cp_lexer_consume_token (parser
->lexer
);
15132 /* Look for the `]' token. */
15133 if (cp_token
*close_token
15134 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15135 end_loc
= close_token
->location
;
15136 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15155 op
= TRUNC_DIV_EXPR
;
15159 op
= TRUNC_MOD_EXPR
;
15179 op
= TRUTH_NOT_EXPR
;
15212 op
= TRUNC_DIV_EXPR
;
15217 op
= TRUNC_MOD_EXPR
;
15243 case CPP_LSHIFT_EQ
:
15248 case CPP_RSHIFT_EQ
:
15265 case CPP_GREATER_EQ
:
15270 op
= TRUTH_ANDIF_EXPR
;
15274 op
= TRUTH_ORIF_EXPR
;
15277 case CPP_PLUS_PLUS
:
15278 op
= POSTINCREMENT_EXPR
;
15281 case CPP_MINUS_MINUS
:
15282 op
= PREDECREMENT_EXPR
;
15286 op
= COMPOUND_EXPR
;
15289 case CPP_DEREF_STAR
:
15294 op
= COMPONENT_REF
;
15297 case CPP_OPEN_PAREN
:
15299 /* Consume the `('. */
15300 matching_parens parens
;
15301 parens
.consume_open (parser
);
15302 /* Look for the matching `)'. */
15303 token
= parens
.require_close (parser
);
15305 end_loc
= token
->location
;
15311 case CPP_OPEN_SQUARE
:
15312 /* Consume the `['. */
15313 cp_lexer_consume_token (parser
->lexer
);
15314 /* Look for the matching `]'. */
15315 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15317 end_loc
= token
->location
;
15322 case CPP_UTF8STRING
:
15323 case CPP_UTF8STRING_USERDEF
:
15330 case CPP_STRING_USERDEF
:
15331 case CPP_WSTRING_USERDEF
:
15332 case CPP_STRING16_USERDEF
:
15333 case CPP_STRING32_USERDEF
:
15339 if (cxx_dialect
== cxx98
)
15340 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15342 /* Consume the string. */
15343 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15344 /*wide_ok=*/true, /*lookup_udlit=*/false);
15345 if (str
== error_mark_node
)
15346 return error_mark_node
;
15347 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15349 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15350 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15351 end_loc
= str
.get_location ();
15356 /* Look for the suffix identifier. */
15357 token
= cp_lexer_peek_token (parser
->lexer
);
15358 if (token
->type
== CPP_NAME
)
15360 id
= cp_parser_identifier (parser
);
15361 end_loc
= token
->location
;
15363 else if (token
->type
== CPP_KEYWORD
)
15365 error ("unexpected keyword;"
15366 " remove space between quotes and suffix identifier");
15367 return error_mark_node
;
15371 error ("expected suffix identifier");
15372 return error_mark_node
;
15375 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15376 (TREE_TYPE (TREE_TYPE (string_tree
))));
15377 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15380 error ("expected empty string after %<operator%> keyword");
15381 return error_mark_node
;
15383 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15386 error ("invalid encoding prefix in literal operator");
15387 return error_mark_node
;
15389 if (id
!= error_mark_node
)
15391 const char *name
= IDENTIFIER_POINTER (id
);
15392 id
= cp_literal_operator_id (name
);
15394 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15395 return cp_expr (id
, start_loc
);
15399 /* Anything else is an error. */
15403 /* If we have selected an identifier, we need to consume the
15405 if (op
!= ERROR_MARK
)
15407 id
= ovl_op_identifier (assop
, op
);
15409 cp_lexer_consume_token (parser
->lexer
);
15411 /* Otherwise, no valid operator name was present. */
15414 cp_parser_error (parser
, "expected operator");
15415 id
= error_mark_node
;
15418 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15419 return cp_expr (id
, start_loc
);
15422 /* Parse a template-declaration.
15424 template-declaration:
15425 export [opt] template < template-parameter-list > declaration
15427 If MEMBER_P is TRUE, this template-declaration occurs within a
15430 The grammar rule given by the standard isn't correct. What
15431 is really meant is:
15433 template-declaration:
15434 export [opt] template-parameter-list-seq
15435 decl-specifier-seq [opt] init-declarator [opt] ;
15436 export [opt] template-parameter-list-seq
15437 function-definition
15439 template-parameter-list-seq:
15440 template-parameter-list-seq [opt]
15441 template < template-parameter-list >
15443 Concept Extensions:
15445 template-parameter-list-seq:
15446 template < template-parameter-list > requires-clause [opt]
15449 requires logical-or-expression */
15452 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15454 /* Check for `export'. */
15455 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15457 /* Consume the `export' token. */
15458 cp_lexer_consume_token (parser
->lexer
);
15459 /* Warn that we do not support `export'. */
15460 warning (0, "keyword %<export%> not implemented, and will be ignored");
15463 cp_parser_template_declaration_after_export (parser
, member_p
);
15466 /* Parse a template-parameter-list.
15468 template-parameter-list:
15470 template-parameter-list , template-parameter
15472 Returns a TREE_LIST. Each node represents a template parameter.
15473 The nodes are connected via their TREE_CHAINs. */
15476 cp_parser_template_parameter_list (cp_parser
* parser
)
15478 tree parameter_list
= NULL_TREE
;
15480 /* Don't create wrapper nodes within a template-parameter-list,
15481 since we don't want to have different types based on the
15482 spelling location of constants and decls within them. */
15483 auto_suppress_location_wrappers sentinel
;
15485 begin_template_parm_list ();
15487 /* The loop below parses the template parms. We first need to know
15488 the total number of template parms to be able to compute proper
15489 canonical types of each dependent type. So after the loop, when
15490 we know the total number of template parms,
15491 end_template_parm_list computes the proper canonical types and
15492 fixes up the dependent types accordingly. */
15497 bool is_parameter_pack
;
15498 location_t parm_loc
;
15500 /* Parse the template-parameter. */
15501 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15502 parameter
= cp_parser_template_parameter (parser
,
15504 &is_parameter_pack
);
15505 /* Add it to the list. */
15506 if (parameter
!= error_mark_node
)
15507 parameter_list
= process_template_parm (parameter_list
,
15511 is_parameter_pack
);
15514 tree err_parm
= build_tree_list (parameter
, parameter
);
15515 parameter_list
= chainon (parameter_list
, err_parm
);
15518 /* If the next token is not a `,', we're done. */
15519 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15521 /* Otherwise, consume the `,' token. */
15522 cp_lexer_consume_token (parser
->lexer
);
15525 return end_template_parm_list (parameter_list
);
15528 /* Parse a introduction-list.
15531 introduced-parameter
15532 introduction-list , introduced-parameter
15534 introduced-parameter:
15535 ...[opt] identifier
15537 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15538 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15539 WILDCARD_DECL will also have DECL_NAME set and token location in
15540 DECL_SOURCE_LOCATION. */
15543 cp_parser_introduction_list (cp_parser
*parser
)
15545 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15549 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15551 cp_lexer_consume_token (parser
->lexer
);
15553 tree identifier
= cp_parser_identifier (parser
);
15554 if (identifier
== error_mark_node
)
15557 /* Build placeholder. */
15558 tree parm
= build_nt (WILDCARD_DECL
);
15559 DECL_SOURCE_LOCATION (parm
)
15560 = cp_lexer_peek_token (parser
->lexer
)->location
;
15561 DECL_NAME (parm
) = identifier
;
15562 WILDCARD_PACK_P (parm
) = is_pack
;
15563 vec_safe_push (introduction_vec
, parm
);
15565 /* If the next token is not a `,', we're done. */
15566 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15568 /* Otherwise, consume the `,' token. */
15569 cp_lexer_consume_token (parser
->lexer
);
15572 /* Convert the vec into a TREE_VEC. */
15573 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15576 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15577 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15579 release_tree_vector (introduction_vec
);
15580 return introduction_list
;
15583 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15584 is an abstract declarator. */
15586 static inline cp_declarator
*
15587 get_id_declarator (cp_declarator
*declarator
)
15589 cp_declarator
*d
= declarator
;
15590 while (d
&& d
->kind
!= cdk_id
)
15595 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15596 is an abstract declarator. */
15599 get_unqualified_id (cp_declarator
*declarator
)
15601 declarator
= get_id_declarator (declarator
);
15603 return declarator
->u
.id
.unqualified_name
;
15608 /* Returns true if DECL represents a constrained-parameter. */
15611 is_constrained_parameter (tree decl
)
15614 && TREE_CODE (decl
) == TYPE_DECL
15615 && CONSTRAINED_PARM_CONCEPT (decl
)
15616 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15619 /* Returns true if PARM declares a constrained-parameter. */
15622 is_constrained_parameter (cp_parameter_declarator
*parm
)
15624 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15627 /* Check that the type parameter is only a declarator-id, and that its
15628 type is not cv-qualified. */
15631 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15632 cp_parameter_declarator
*parm
)
15634 if (!parm
->declarator
)
15637 if (parm
->declarator
->kind
!= cdk_id
)
15639 cp_parser_error (parser
, "invalid constrained type parameter");
15643 /* Don't allow cv-qualified type parameters. */
15644 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15645 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15647 cp_parser_error (parser
, "cv-qualified type parameter");
15654 /* Finish parsing/processing a template type parameter and checking
15655 various restrictions. */
15658 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15660 cp_parameter_declarator
* parmdecl
)
15662 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15663 return finish_template_type_parm (class_type_node
, id
);
15665 return error_mark_node
;
15669 finish_constrained_template_template_parm (tree proto
, tree id
)
15671 /* FIXME: This should probably be copied, and we may need to adjust
15672 the template parameter depths. */
15673 tree saved_parms
= current_template_parms
;
15674 begin_template_parm_list ();
15675 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15676 end_template_parm_list ();
15678 tree parm
= finish_template_template_parm (class_type_node
, id
);
15679 current_template_parms
= saved_parms
;
15684 /* Finish parsing/processing a template template parameter by borrowing
15685 the template parameter list from the prototype parameter. */
15688 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15691 cp_parameter_declarator
*parmdecl
)
15693 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15694 return error_mark_node
;
15695 return finish_constrained_template_template_parm (proto
, id
);
15698 /* Create a new non-type template parameter from the given PARM
15702 constrained_non_type_template_parm (bool *is_non_type
,
15703 cp_parameter_declarator
*parm
)
15705 *is_non_type
= true;
15706 cp_declarator
*decl
= parm
->declarator
;
15707 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15708 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15709 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15712 /* Build a constrained template parameter based on the PARMDECL
15713 declarator. The type of PARMDECL is the constrained type, which
15714 refers to the prototype template parameter that ultimately
15715 specifies the type of the declared parameter. */
15718 finish_constrained_parameter (cp_parser
*parser
,
15719 cp_parameter_declarator
*parmdecl
,
15721 bool *is_parameter_pack
)
15723 tree decl
= parmdecl
->decl_specifiers
.type
;
15724 tree id
= get_unqualified_id (parmdecl
->declarator
);
15725 tree def
= parmdecl
->default_argument
;
15726 tree proto
= DECL_INITIAL (decl
);
15728 /* A template parameter constrained by a variadic concept shall also
15729 be declared as a template parameter pack. */
15730 bool is_variadic
= template_parameter_pack_p (proto
);
15731 if (is_variadic
&& !*is_parameter_pack
)
15732 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15734 /* Build the parameter. Return an error if the declarator was invalid. */
15736 if (TREE_CODE (proto
) == TYPE_DECL
)
15737 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15738 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15739 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15742 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15743 if (parm
== error_mark_node
)
15744 return error_mark_node
;
15746 /* Finish the parameter decl and create a node attaching the
15747 default argument and constraint. */
15748 parm
= build_tree_list (def
, parm
);
15749 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15754 /* Returns true if the parsed type actually represents the declaration
15755 of a type template-parameter. */
15758 declares_constrained_type_template_parameter (tree type
)
15760 return (is_constrained_parameter (type
)
15761 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15765 /* Returns true if the parsed type actually represents the declaration of
15766 a template template-parameter. */
15769 declares_constrained_template_template_parameter (tree type
)
15771 return (is_constrained_parameter (type
)
15772 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15775 /* Parse a default argument for a type template-parameter.
15776 Note that diagnostics are handled in cp_parser_template_parameter. */
15779 cp_parser_default_type_template_argument (cp_parser
*parser
)
15781 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15783 /* Consume the `=' token. */
15784 cp_lexer_consume_token (parser
->lexer
);
15786 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15788 /* Parse the default-argument. */
15789 push_deferring_access_checks (dk_no_deferred
);
15790 tree default_argument
= cp_parser_type_id (parser
,
15791 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15793 pop_deferring_access_checks ();
15795 if (flag_concepts
&& type_uses_auto (default_argument
))
15797 error_at (token
->location
,
15798 "invalid use of %<auto%> in default template argument");
15799 return error_mark_node
;
15802 return default_argument
;
15805 /* Parse a default argument for a template template-parameter. */
15808 cp_parser_default_template_template_argument (cp_parser
*parser
)
15810 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15814 /* Consume the `='. */
15815 cp_lexer_consume_token (parser
->lexer
);
15816 /* Parse the id-expression. */
15817 push_deferring_access_checks (dk_no_deferred
);
15818 /* save token before parsing the id-expression, for error
15820 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15821 tree default_argument
15822 = cp_parser_id_expression (parser
,
15823 /*template_keyword_p=*/false,
15824 /*check_dependency_p=*/true,
15825 /*template_p=*/&is_template
,
15826 /*declarator_p=*/false,
15827 /*optional_p=*/false);
15828 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15829 /* If the id-expression was a template-id that refers to
15830 a template-class, we already have the declaration here,
15831 so no further lookup is needed. */
15834 /* Look up the name. */
15836 = cp_parser_lookup_name (parser
, default_argument
,
15838 /*is_template=*/is_template
,
15839 /*is_namespace=*/false,
15840 /*check_dependency=*/true,
15841 /*ambiguous_decls=*/NULL
,
15843 /* See if the default argument is valid. */
15844 default_argument
= check_template_template_default_arg (default_argument
);
15845 pop_deferring_access_checks ();
15846 return default_argument
;
15849 /* Parse a template-parameter.
15851 template-parameter:
15853 parameter-declaration
15855 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15856 the parameter. The TREE_PURPOSE is the default value, if any.
15857 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15858 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15859 set to true iff this parameter is a parameter pack. */
15862 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15863 bool *is_parameter_pack
)
15866 cp_parameter_declarator
*parameter_declarator
;
15869 /* Assume it is a type parameter or a template parameter. */
15870 *is_non_type
= false;
15871 /* Assume it not a parameter pack. */
15872 *is_parameter_pack
= false;
15873 /* Peek at the next token. */
15874 token
= cp_lexer_peek_token (parser
->lexer
);
15875 /* If it is `template', we have a type-parameter. */
15876 if (token
->keyword
== RID_TEMPLATE
)
15877 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15878 /* If it is `class' or `typename' we do not know yet whether it is a
15879 type parameter or a non-type parameter. Consider:
15881 template <typename T, typename T::X X> ...
15885 template <class C, class D*> ...
15887 Here, the first parameter is a type parameter, and the second is
15888 a non-type parameter. We can tell by looking at the token after
15889 the identifier -- if it is a `,', `=', or `>' then we have a type
15891 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15893 /* Peek at the token after `class' or `typename'. */
15894 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15895 /* If it's an ellipsis, we have a template type parameter
15897 if (token
->type
== CPP_ELLIPSIS
)
15898 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15899 /* If it's an identifier, skip it. */
15900 if (token
->type
== CPP_NAME
)
15901 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15902 /* Now, see if the token looks like the end of a template
15904 if (token
->type
== CPP_COMMA
15905 || token
->type
== CPP_EQ
15906 || token
->type
== CPP_GREATER
)
15907 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15910 /* Otherwise, it is a non-type parameter or a constrained parameter.
15914 When parsing a default template-argument for a non-type
15915 template-parameter, the first non-nested `>' is taken as the end
15916 of the template parameter-list rather than a greater-than
15918 parameter_declarator
15919 = cp_parser_parameter_declaration (parser
,
15920 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15921 /*template_parm_p=*/true,
15922 /*parenthesized_p=*/NULL
);
15924 if (!parameter_declarator
)
15925 return error_mark_node
;
15927 /* If the parameter declaration is marked as a parameter pack, set
15928 *IS_PARAMETER_PACK to notify the caller. */
15929 if (parameter_declarator
->template_parameter_pack_p
)
15930 *is_parameter_pack
= true;
15932 if (parameter_declarator
->default_argument
)
15934 /* Can happen in some cases of erroneous input (c++/34892). */
15935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15936 /* Consume the `...' for better error recovery. */
15937 cp_lexer_consume_token (parser
->lexer
);
15940 // The parameter may have been constrained.
15941 if (is_constrained_parameter (parameter_declarator
))
15942 return finish_constrained_parameter (parser
,
15943 parameter_declarator
,
15945 is_parameter_pack
);
15947 // Now we're sure that the parameter is a non-type parameter.
15948 *is_non_type
= true;
15950 parm
= grokdeclarator (parameter_declarator
->declarator
,
15951 ¶meter_declarator
->decl_specifiers
,
15952 TPARM
, /*initialized=*/0,
15953 /*attrlist=*/NULL
);
15954 if (parm
== error_mark_node
)
15955 return error_mark_node
;
15957 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15960 /* Parse a type-parameter.
15963 class identifier [opt]
15964 class identifier [opt] = type-id
15965 typename identifier [opt]
15966 typename identifier [opt] = type-id
15967 template < template-parameter-list > class identifier [opt]
15968 template < template-parameter-list > class identifier [opt]
15971 GNU Extension (variadic templates):
15974 class ... identifier [opt]
15975 typename ... identifier [opt]
15977 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15978 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15979 the declaration of the parameter.
15981 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15984 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15989 /* Look for a keyword to tell us what kind of parameter this is. */
15990 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15992 return error_mark_node
;
15994 switch (token
->keyword
)
16000 tree default_argument
;
16002 /* If the next token is an ellipsis, we have a template
16004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16006 /* Consume the `...' token. */
16007 cp_lexer_consume_token (parser
->lexer
);
16008 maybe_warn_variadic_templates ();
16010 *is_parameter_pack
= true;
16013 /* If the next token is an identifier, then it names the
16015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16016 identifier
= cp_parser_identifier (parser
);
16018 identifier
= NULL_TREE
;
16020 /* Create the parameter. */
16021 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16023 /* If the next token is an `=', we have a default argument. */
16024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16027 = cp_parser_default_type_template_argument (parser
);
16029 /* Template parameter packs cannot have default
16031 if (*is_parameter_pack
)
16034 error_at (token
->location
,
16035 "template parameter pack %qD cannot have a "
16036 "default argument", identifier
);
16038 error_at (token
->location
,
16039 "template parameter packs cannot have "
16040 "default arguments");
16041 default_argument
= NULL_TREE
;
16043 else if (check_for_bare_parameter_packs (default_argument
))
16044 default_argument
= error_mark_node
;
16047 default_argument
= NULL_TREE
;
16049 /* Create the combined representation of the parameter and the
16050 default argument. */
16051 parameter
= build_tree_list (default_argument
, parameter
);
16058 tree default_argument
;
16060 /* Look for the `<'. */
16061 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16062 /* Parse the template-parameter-list. */
16063 cp_parser_template_parameter_list (parser
);
16064 /* Look for the `>'. */
16065 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16067 // If template requirements are present, parse them.
16070 tree reqs
= get_shorthand_constraints (current_template_parms
);
16071 if (tree r
= cp_parser_requires_clause_opt (parser
))
16072 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16073 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16076 /* Look for the `class' or 'typename' keywords. */
16077 cp_parser_type_parameter_key (parser
);
16078 /* If the next token is an ellipsis, we have a template
16080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16082 /* Consume the `...' token. */
16083 cp_lexer_consume_token (parser
->lexer
);
16084 maybe_warn_variadic_templates ();
16086 *is_parameter_pack
= true;
16088 /* If the next token is an `=', then there is a
16089 default-argument. If the next token is a `>', we are at
16090 the end of the parameter-list. If the next token is a `,',
16091 then we are at the end of this parameter. */
16092 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16093 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16094 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16096 identifier
= cp_parser_identifier (parser
);
16097 /* Treat invalid names as if the parameter were nameless. */
16098 if (identifier
== error_mark_node
)
16099 identifier
= NULL_TREE
;
16102 identifier
= NULL_TREE
;
16104 /* Create the template parameter. */
16105 parameter
= finish_template_template_parm (class_type_node
,
16108 /* If the next token is an `=', then there is a
16109 default-argument. */
16110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16113 = cp_parser_default_template_template_argument (parser
);
16115 /* Template parameter packs cannot have default
16117 if (*is_parameter_pack
)
16120 error_at (token
->location
,
16121 "template parameter pack %qD cannot "
16122 "have a default argument",
16125 error_at (token
->location
, "template parameter packs cannot "
16126 "have default arguments");
16127 default_argument
= NULL_TREE
;
16131 default_argument
= NULL_TREE
;
16133 /* Create the combined representation of the parameter and the
16134 default argument. */
16135 parameter
= build_tree_list (default_argument
, parameter
);
16140 gcc_unreachable ();
16147 /* Parse a template-id.
16150 template-name < template-argument-list [opt] >
16152 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16153 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16154 returned. Otherwise, if the template-name names a function, or set
16155 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16156 names a class, returns a TYPE_DECL for the specialization.
16158 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16159 uninstantiated templates. */
16162 cp_parser_template_id (cp_parser
*parser
,
16163 bool template_keyword_p
,
16164 bool check_dependency_p
,
16165 enum tag_types tag_type
,
16166 bool is_declaration
)
16171 cp_token_position start_of_id
= 0;
16172 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16173 bool is_identifier
;
16175 /* If the next token corresponds to a template-id, there is no need
16177 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16178 if (token
->type
== CPP_TEMPLATE_ID
)
16180 cp_lexer_consume_token (parser
->lexer
);
16181 return saved_checks_value (token
->u
.tree_check_value
);
16184 /* Avoid performing name lookup if there is no possibility of
16185 finding a template-id. */
16186 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16187 || (token
->type
== CPP_NAME
16188 && !cp_parser_nth_token_starts_template_argument_list_p
16191 cp_parser_error (parser
, "expected template-id");
16192 return error_mark_node
;
16195 /* Remember where the template-id starts. */
16196 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16197 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16199 push_deferring_access_checks (dk_deferred
);
16201 /* Parse the template-name. */
16202 is_identifier
= false;
16203 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16204 check_dependency_p
,
16209 /* Push any access checks inside the firewall we're about to create. */
16210 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16211 pop_deferring_access_checks ();
16212 if (templ
== error_mark_node
|| is_identifier
)
16215 /* Since we're going to preserve any side-effects from this parse, set up a
16216 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16217 in the template arguments. */
16218 tentative_firewall
firewall (parser
);
16219 reopen_deferring_access_checks (checks
);
16221 /* If we find the sequence `[:' after a template-name, it's probably
16222 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16223 parse correctly the argument list. */
16224 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16225 == CPP_OPEN_SQUARE
)
16226 && next_token
->flags
& DIGRAPH
16227 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16229 && !(next_token_2
->flags
& PREV_WHITE
))
16231 cp_parser_parse_tentatively (parser
);
16232 /* Change `:' into `::'. */
16233 next_token_2
->type
= CPP_SCOPE
;
16234 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16236 cp_lexer_consume_token (parser
->lexer
);
16238 /* Parse the arguments. */
16239 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16240 if (!cp_parser_parse_definitely (parser
))
16242 /* If we couldn't parse an argument list, then we revert our changes
16243 and return simply an error. Maybe this is not a template-id
16245 next_token_2
->type
= CPP_COLON
;
16246 cp_parser_error (parser
, "expected %<<%>");
16247 pop_deferring_access_checks ();
16248 return error_mark_node
;
16250 /* Otherwise, emit an error about the invalid digraph, but continue
16251 parsing because we got our argument list. */
16252 if (permerror (next_token
->location
,
16253 "%<<::%> cannot begin a template-argument list"))
16255 static bool hint
= false;
16256 inform (next_token
->location
,
16257 "%<<:%> is an alternate spelling for %<[%>."
16258 " Insert whitespace between %<<%> and %<::%>");
16259 if (!hint
&& !flag_permissive
)
16261 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16262 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16263 "accept your code)");
16270 /* Look for the `<' that starts the template-argument-list. */
16271 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16273 pop_deferring_access_checks ();
16274 return error_mark_node
;
16276 /* Parse the arguments. */
16277 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16279 if ((cxx_dialect
> cxx17
)
16280 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16281 && !template_keyword_p
16282 && (cp_parser_error_occurred (parser
)
16283 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16285 /* This didn't go well. */
16286 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16288 /* C++2A says that "function-name < a;" is now ill-formed. */
16289 if (cp_parser_error_occurred (parser
))
16291 error_at (token
->location
, "invalid template-argument-list");
16292 inform (token
->location
, "function name as the left hand "
16293 "operand of %<<%> is ill-formed in C++2a; wrap the "
16294 "function name in %<()%>");
16297 /* We expect "f<targs>" to be followed by "(args)". */
16298 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16299 "expected %<(%> after template-argument-list");
16301 /* Purge all subsequent tokens. */
16302 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16305 cp_parser_simulate_error (parser
);
16306 pop_deferring_access_checks ();
16307 return error_mark_node
;
16311 /* Set the location to be of the form:
16312 template-name < template-argument-list [opt] >
16313 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16314 with caret == start at the start of the template-name,
16315 ranging until the closing '>'. */
16316 location_t finish_loc
16317 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16318 location_t combined_loc
16319 = make_location (token
->location
, token
->location
, finish_loc
);
16321 /* Check for concepts autos where they don't belong. We could
16322 identify types in some cases of idnetifier TEMPL, looking ahead
16323 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16324 types. We reject them in functions, but if what we have is an
16325 identifier, even with none_type we can't conclude it's NOT a
16326 type, we have to wait for template substitution. */
16327 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16328 template_id
= error_mark_node
;
16329 /* Build a representation of the specialization. */
16330 else if (identifier_p (templ
))
16331 template_id
= build_min_nt_loc (combined_loc
,
16334 else if (DECL_TYPE_TEMPLATE_P (templ
)
16335 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16337 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16338 template (rather than some instantiation thereof) only if
16339 is not nested within some other construct. For example, in
16340 "template <typename T> void f(T) { A<T>::", A<T> is just an
16341 instantiation of A. */
16342 bool entering_scope
16343 = (template_parm_scope_p ()
16344 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16346 = finish_template_type (templ
, arguments
, entering_scope
);
16348 /* A template-like identifier may be a partial concept id. */
16349 else if (flag_concepts
16350 && (template_id
= (cp_parser_maybe_partial_concept_id
16351 (parser
, templ
, arguments
))))
16352 return template_id
;
16353 else if (variable_template_p (templ
))
16355 template_id
= lookup_template_variable (templ
, arguments
);
16356 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16357 SET_EXPR_LOCATION (template_id
, combined_loc
);
16361 /* If it's not a class-template or a template-template, it should be
16362 a function-template. */
16363 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16364 || TREE_CODE (templ
) == OVERLOAD
16365 || TREE_CODE (templ
) == FUNCTION_DECL
16366 || BASELINK_P (templ
)));
16368 template_id
= lookup_template_function (templ
, arguments
);
16369 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16370 SET_EXPR_LOCATION (template_id
, combined_loc
);
16373 /* If parsing tentatively, replace the sequence of tokens that makes
16374 up the template-id with a CPP_TEMPLATE_ID token. That way,
16375 should we re-parse the token stream, we will not have to repeat
16376 the effort required to do the parse, nor will we issue duplicate
16377 error messages about problems during instantiation of the
16380 /* Don't do this if we had a parse error in a declarator; re-parsing
16381 might succeed if a name changes meaning (60361). */
16382 && !(cp_parser_error_occurred (parser
)
16383 && cp_parser_parsing_tentatively (parser
)
16384 && parser
->in_declarator_p
))
16386 /* Reset the contents of the START_OF_ID token. */
16387 token
->type
= CPP_TEMPLATE_ID
;
16388 token
->location
= combined_loc
;
16390 /* Retrieve any deferred checks. Do not pop this access checks yet
16391 so the memory will not be reclaimed during token replacing below. */
16392 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16393 token
->u
.tree_check_value
->value
= template_id
;
16394 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16395 token
->keyword
= RID_MAX
;
16397 /* Purge all subsequent tokens. */
16398 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16400 /* ??? Can we actually assume that, if template_id ==
16401 error_mark_node, we will have issued a diagnostic to the
16402 user, as opposed to simply marking the tentative parse as
16404 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16405 error_at (token
->location
, "parse error in template argument list");
16408 pop_to_parent_deferring_access_checks ();
16409 return template_id
;
16412 /* Parse a template-name.
16417 The standard should actually say:
16421 operator-function-id
16423 A defect report has been filed about this issue.
16425 A conversion-function-id cannot be a template name because they cannot
16426 be part of a template-id. In fact, looking at this code:
16428 a.operator K<int>()
16430 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16431 It is impossible to call a templated conversion-function-id with an
16432 explicit argument list, since the only allowed template parameter is
16433 the type to which it is converting.
16435 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16436 `template' keyword, in a construction like:
16440 In that case `f' is taken to be a template-name, even though there
16441 is no way of knowing for sure.
16443 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16444 name refers to a set of overloaded functions, at least one of which
16445 is a template, or an IDENTIFIER_NODE with the name of the template,
16446 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16447 names are looked up inside uninstantiated templates. */
16450 cp_parser_template_name (cp_parser
* parser
,
16451 bool template_keyword_p
,
16452 bool check_dependency_p
,
16453 bool is_declaration
,
16454 enum tag_types tag_type
,
16455 bool *is_identifier
)
16459 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16461 /* If the next token is `operator', then we have either an
16462 operator-function-id or a conversion-function-id. */
16463 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16465 /* We don't know whether we're looking at an
16466 operator-function-id or a conversion-function-id. */
16467 cp_parser_parse_tentatively (parser
);
16468 /* Try an operator-function-id. */
16469 identifier
= cp_parser_operator_function_id (parser
);
16470 /* If that didn't work, try a conversion-function-id. */
16471 if (!cp_parser_parse_definitely (parser
))
16473 cp_parser_error (parser
, "expected template-name");
16474 return error_mark_node
;
16477 /* Look for the identifier. */
16479 identifier
= cp_parser_identifier (parser
);
16481 /* If we didn't find an identifier, we don't have a template-id. */
16482 if (identifier
== error_mark_node
)
16483 return error_mark_node
;
16485 /* If the name immediately followed the `template' keyword, then it
16486 is a template-name. However, if the next token is not `<', then
16487 we do not treat it as a template-name, since it is not being used
16488 as part of a template-id. This enables us to handle constructs
16491 template <typename T> struct S { S(); };
16492 template <typename T> S<T>::S();
16494 correctly. We would treat `S' as a template -- if it were `S<T>'
16495 -- but we do not if there is no `<'. */
16497 if (processing_template_decl
16498 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16500 /* In a declaration, in a dependent context, we pretend that the
16501 "template" keyword was present in order to improve error
16502 recovery. For example, given:
16504 template <typename T> void f(T::X<int>);
16506 we want to treat "X<int>" as a template-id. */
16508 && !template_keyword_p
16509 && parser
->scope
&& TYPE_P (parser
->scope
)
16510 && check_dependency_p
16511 && dependent_scope_p (parser
->scope
)
16512 /* Do not do this for dtors (or ctors), since they never
16513 need the template keyword before their name. */
16514 && !constructor_name_p (identifier
, parser
->scope
))
16516 cp_token_position start
= 0;
16518 /* Explain what went wrong. */
16519 error_at (token
->location
, "non-template %qD used as template",
16521 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16522 parser
->scope
, identifier
);
16523 /* If parsing tentatively, find the location of the "<" token. */
16524 if (cp_parser_simulate_error (parser
))
16525 start
= cp_lexer_token_position (parser
->lexer
, true);
16526 /* Parse the template arguments so that we can issue error
16527 messages about them. */
16528 cp_lexer_consume_token (parser
->lexer
);
16529 cp_parser_enclosed_template_argument_list (parser
);
16530 /* Skip tokens until we find a good place from which to
16531 continue parsing. */
16532 cp_parser_skip_to_closing_parenthesis (parser
,
16533 /*recovering=*/true,
16535 /*consume_paren=*/false);
16536 /* If parsing tentatively, permanently remove the
16537 template argument list. That will prevent duplicate
16538 error messages from being issued about the missing
16539 "template" keyword. */
16541 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16543 *is_identifier
= true;
16544 parser
->context
->object_type
= NULL_TREE
;
16548 /* If the "template" keyword is present, then there is generally
16549 no point in doing name-lookup, so we just return IDENTIFIER.
16550 But, if the qualifying scope is non-dependent then we can
16551 (and must) do name-lookup normally. */
16552 if (template_keyword_p
)
16554 tree scope
= (parser
->scope
? parser
->scope
16555 : parser
->context
->object_type
);
16556 if (scope
&& TYPE_P (scope
)
16557 && (!CLASS_TYPE_P (scope
)
16558 || (check_dependency_p
&& dependent_type_p (scope
))))
16560 /* We're optimizing away the call to cp_parser_lookup_name, but
16561 we still need to do this. */
16562 parser
->context
->object_type
= NULL_TREE
;
16568 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16569 const bool scoped_p
= ((parser
->scope
? parser
->scope
16570 : parser
->context
->object_type
) != NULL_TREE
);
16572 /* Look up the name. */
16573 decl
= cp_parser_lookup_name (parser
, identifier
,
16575 /*is_template=*/true,
16576 /*is_namespace=*/false,
16577 check_dependency_p
,
16578 /*ambiguous_decls=*/NULL
,
16581 decl
= strip_using_decl (decl
);
16583 /* If DECL is a template, then the name was a template-name. */
16584 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16586 if (TREE_DEPRECATED (decl
)
16587 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16588 warn_deprecated_use (decl
, NULL_TREE
);
16592 /* The standard does not explicitly indicate whether a name that
16593 names a set of overloaded declarations, some of which are
16594 templates, is a template-name. However, such a name should
16595 be a template-name; otherwise, there is no way to form a
16596 template-id for the overloaded templates. */
16597 bool found
= false;
16599 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16600 !found
&& iter
; ++iter
)
16601 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16605 && (cxx_dialect
> cxx17
)
16607 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16608 && tag_type
== none_type
)
16610 /* [temp.names] says "A name is also considered to refer to a template
16611 if it is an unqualified-id followed by a < and name lookup finds
16612 either one or more functions or finds nothing." */
16614 /* The "more functions" case. Just use the OVERLOAD as normally.
16615 We don't use is_overloaded_fn here to avoid considering
16617 if (TREE_CODE (decl
) == OVERLOAD
16618 /* Name lookup found one function. */
16619 || TREE_CODE (decl
) == FUNCTION_DECL
)
16621 /* Name lookup found nothing. */
16622 else if (decl
== error_mark_node
)
16628 /* The name does not name a template. */
16629 cp_parser_error (parser
, "expected template-name");
16630 return error_mark_node
;
16637 /* Parse a template-argument-list.
16639 template-argument-list:
16640 template-argument ... [opt]
16641 template-argument-list , template-argument ... [opt]
16643 Returns a TREE_VEC containing the arguments. */
16646 cp_parser_template_argument_list (cp_parser
* parser
)
16648 tree fixed_args
[10];
16649 unsigned n_args
= 0;
16650 unsigned alloced
= 10;
16651 tree
*arg_ary
= fixed_args
;
16653 bool saved_in_template_argument_list_p
;
16655 bool saved_non_ice_p
;
16657 /* Don't create location wrapper nodes within a template-argument-list. */
16658 auto_suppress_location_wrappers sentinel
;
16660 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16661 parser
->in_template_argument_list_p
= true;
16662 /* Even if the template-id appears in an integral
16663 constant-expression, the contents of the argument list do
16665 saved_ice_p
= parser
->integral_constant_expression_p
;
16666 parser
->integral_constant_expression_p
= false;
16667 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16668 parser
->non_integral_constant_expression_p
= false;
16670 /* Parse the arguments. */
16676 /* Consume the comma. */
16677 cp_lexer_consume_token (parser
->lexer
);
16679 /* Parse the template-argument. */
16680 argument
= cp_parser_template_argument (parser
);
16682 /* If the next token is an ellipsis, we're expanding a template
16684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16686 if (argument
== error_mark_node
)
16688 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16689 error_at (token
->location
,
16690 "expected parameter pack before %<...%>");
16692 /* Consume the `...' token. */
16693 cp_lexer_consume_token (parser
->lexer
);
16695 /* Make the argument into a TYPE_PACK_EXPANSION or
16696 EXPR_PACK_EXPANSION. */
16697 argument
= make_pack_expansion (argument
);
16700 if (n_args
== alloced
)
16704 if (arg_ary
== fixed_args
)
16706 arg_ary
= XNEWVEC (tree
, alloced
);
16707 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16710 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16712 arg_ary
[n_args
++] = argument
;
16714 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16716 vec
= make_tree_vec (n_args
);
16719 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16721 if (arg_ary
!= fixed_args
)
16723 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16724 parser
->integral_constant_expression_p
= saved_ice_p
;
16725 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16727 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16731 /* Parse a template-argument.
16734 assignment-expression
16738 The representation is that of an assignment-expression, type-id, or
16739 id-expression -- except that the qualified id-expression is
16740 evaluated, so that the value returned is either a DECL or an
16743 Although the standard says "assignment-expression", it forbids
16744 throw-expressions or assignments in the template argument.
16745 Therefore, we use "conditional-expression" instead. */
16748 cp_parser_template_argument (cp_parser
* parser
)
16753 bool maybe_type_id
= false;
16754 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16755 location_t loc
= 0;
16758 /* There's really no way to know what we're looking at, so we just
16759 try each alternative in order.
16763 In a template-argument, an ambiguity between a type-id and an
16764 expression is resolved to a type-id, regardless of the form of
16765 the corresponding template-parameter.
16767 Therefore, we try a type-id first. */
16768 cp_parser_parse_tentatively (parser
);
16769 argument
= cp_parser_template_type_arg (parser
);
16770 /* If there was no error parsing the type-id but the next token is a
16771 '>>', our behavior depends on which dialect of C++ we're
16772 parsing. In C++98, we probably found a typo for '> >'. But there
16773 are type-id which are also valid expressions. For instance:
16775 struct X { int operator >> (int); };
16776 template <int V> struct Foo {};
16779 Here 'X()' is a valid type-id of a function type, but the user just
16780 wanted to write the expression "X() >> 5". Thus, we remember that we
16781 found a valid type-id, but we still try to parse the argument as an
16782 expression to see what happens.
16784 In C++0x, the '>>' will be considered two separate '>'
16786 if (!cp_parser_error_occurred (parser
)
16787 && cxx_dialect
== cxx98
16788 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16790 maybe_type_id
= true;
16791 cp_parser_abort_tentative_parse (parser
);
16795 /* If the next token isn't a `,' or a `>', then this argument wasn't
16796 really finished. This means that the argument is not a valid
16798 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16799 cp_parser_error (parser
, "expected template-argument");
16800 /* If that worked, we're done. */
16801 if (cp_parser_parse_definitely (parser
))
16804 /* We're still not sure what the argument will be. */
16805 cp_parser_parse_tentatively (parser
);
16806 /* Try a template. */
16807 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16808 argument
= cp_parser_id_expression (parser
,
16809 /*template_keyword_p=*/false,
16810 /*check_dependency_p=*/true,
16812 /*declarator_p=*/false,
16813 /*optional_p=*/false);
16814 /* If the next token isn't a `,' or a `>', then this argument wasn't
16815 really finished. */
16816 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16817 cp_parser_error (parser
, "expected template-argument");
16818 if (!cp_parser_error_occurred (parser
))
16820 /* Figure out what is being referred to. If the id-expression
16821 was for a class template specialization, then we will have a
16822 TYPE_DECL at this point. There is no need to do name lookup
16823 at this point in that case. */
16824 if (TREE_CODE (argument
) != TYPE_DECL
)
16825 argument
= cp_parser_lookup_name (parser
, argument
,
16827 /*is_template=*/template_p
,
16828 /*is_namespace=*/false,
16829 /*check_dependency=*/true,
16830 /*ambiguous_decls=*/NULL
,
16831 argument_start_token
->location
);
16832 /* Handle a constrained-type-specifier for a non-type template
16834 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16836 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16837 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16838 cp_parser_error (parser
, "expected template-name");
16840 if (cp_parser_parse_definitely (parser
))
16842 if (TREE_DEPRECATED (argument
))
16843 warn_deprecated_use (argument
, NULL_TREE
);
16846 /* It must be a non-type argument. In C++17 any constant-expression is
16848 if (cxx_dialect
> cxx14
)
16851 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16853 -- an integral constant-expression of integral or enumeration
16856 -- the name of a non-type template-parameter; or
16858 -- the name of an object or function with external linkage...
16860 -- the address of an object or function with external linkage...
16862 -- a pointer to member... */
16863 /* Look for a non-type template parameter. */
16864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16866 cp_parser_parse_tentatively (parser
);
16867 argument
= cp_parser_primary_expression (parser
,
16868 /*address_p=*/false,
16870 /*template_arg_p=*/true,
16872 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16873 || !cp_parser_next_token_ends_template_argument_p (parser
))
16874 cp_parser_simulate_error (parser
);
16875 if (cp_parser_parse_definitely (parser
))
16879 /* If the next token is "&", the argument must be the address of an
16880 object or function with external linkage. */
16881 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16884 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16885 cp_lexer_consume_token (parser
->lexer
);
16887 /* See if we might have an id-expression. */
16888 token
= cp_lexer_peek_token (parser
->lexer
);
16889 if (token
->type
== CPP_NAME
16890 || token
->keyword
== RID_OPERATOR
16891 || token
->type
== CPP_SCOPE
16892 || token
->type
== CPP_TEMPLATE_ID
16893 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16895 cp_parser_parse_tentatively (parser
);
16896 argument
= cp_parser_primary_expression (parser
,
16899 /*template_arg_p=*/true,
16901 if (cp_parser_error_occurred (parser
)
16902 || !cp_parser_next_token_ends_template_argument_p (parser
))
16903 cp_parser_abort_tentative_parse (parser
);
16908 if (INDIRECT_REF_P (argument
))
16910 /* Strip the dereference temporarily. */
16911 gcc_assert (REFERENCE_REF_P (argument
));
16912 argument
= TREE_OPERAND (argument
, 0);
16915 /* If we're in a template, we represent a qualified-id referring
16916 to a static data member as a SCOPE_REF even if the scope isn't
16917 dependent so that we can check access control later. */
16919 if (TREE_CODE (probe
) == SCOPE_REF
)
16920 probe
= TREE_OPERAND (probe
, 1);
16923 /* A variable without external linkage might still be a
16924 valid constant-expression, so no error is issued here
16925 if the external-linkage check fails. */
16926 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16927 cp_parser_simulate_error (parser
);
16929 else if (is_overloaded_fn (argument
))
16930 /* All overloaded functions are allowed; if the external
16931 linkage test does not pass, an error will be issued
16935 && (TREE_CODE (argument
) == OFFSET_REF
16936 || TREE_CODE (argument
) == SCOPE_REF
))
16937 /* A pointer-to-member. */
16939 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16942 cp_parser_simulate_error (parser
);
16944 if (cp_parser_parse_definitely (parser
))
16947 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16948 tf_warning_or_error
);
16950 argument
= convert_from_reference (argument
);
16955 /* If the argument started with "&", there are no other valid
16956 alternatives at this point. */
16959 cp_parser_error (parser
, "invalid non-type template argument");
16960 return error_mark_node
;
16964 /* If the argument wasn't successfully parsed as a type-id followed
16965 by '>>', the argument can only be a constant expression now.
16966 Otherwise, we try parsing the constant-expression tentatively,
16967 because the argument could really be a type-id. */
16969 cp_parser_parse_tentatively (parser
);
16971 if (cxx_dialect
<= cxx14
)
16972 argument
= cp_parser_constant_expression (parser
);
16975 /* With C++17 generalized non-type template arguments we need to handle
16976 lvalue constant expressions, too. */
16977 argument
= cp_parser_assignment_expression (parser
);
16978 require_potential_constant_expression (argument
);
16981 if (!maybe_type_id
)
16983 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16984 cp_parser_error (parser
, "expected template-argument");
16985 if (cp_parser_parse_definitely (parser
))
16987 /* We did our best to parse the argument as a non type-id, but that
16988 was the only alternative that matched (albeit with a '>' after
16989 it). We can assume it's just a typo from the user, and a
16990 diagnostic will then be issued. */
16991 return cp_parser_template_type_arg (parser
);
16994 /* Parse an explicit-instantiation.
16996 explicit-instantiation:
16997 template declaration
16999 Although the standard says `declaration', what it really means is:
17001 explicit-instantiation:
17002 template decl-specifier-seq [opt] declarator [opt] ;
17004 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17005 supposed to be allowed. A defect report has been filed about this
17010 explicit-instantiation:
17011 storage-class-specifier template
17012 decl-specifier-seq [opt] declarator [opt] ;
17013 function-specifier template
17014 decl-specifier-seq [opt] declarator [opt] ; */
17017 cp_parser_explicit_instantiation (cp_parser
* parser
)
17019 int declares_class_or_enum
;
17020 cp_decl_specifier_seq decl_specifiers
;
17021 tree extension_specifier
= NULL_TREE
;
17023 timevar_push (TV_TEMPLATE_INST
);
17025 /* Look for an (optional) storage-class-specifier or
17026 function-specifier. */
17027 if (cp_parser_allow_gnu_extensions_p (parser
))
17029 extension_specifier
17030 = cp_parser_storage_class_specifier_opt (parser
);
17031 if (!extension_specifier
)
17032 extension_specifier
17033 = cp_parser_function_specifier_opt (parser
,
17034 /*decl_specs=*/NULL
);
17037 /* Look for the `template' keyword. */
17038 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17039 /* Let the front end know that we are processing an explicit
17041 begin_explicit_instantiation ();
17042 /* [temp.explicit] says that we are supposed to ignore access
17043 control while processing explicit instantiation directives. */
17044 push_deferring_access_checks (dk_no_check
);
17045 /* Parse a decl-specifier-seq. */
17046 cp_parser_decl_specifier_seq (parser
,
17047 CP_PARSER_FLAGS_OPTIONAL
,
17049 &declares_class_or_enum
);
17050 /* If there was exactly one decl-specifier, and it declared a class,
17051 and there's no declarator, then we have an explicit type
17053 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17057 type
= check_tag_decl (&decl_specifiers
,
17058 /*explicit_type_instantiation_p=*/true);
17059 /* Turn access control back on for names used during
17060 template instantiation. */
17061 pop_deferring_access_checks ();
17063 do_type_instantiation (type
, extension_specifier
,
17064 /*complain=*/tf_error
);
17068 cp_declarator
*declarator
;
17071 /* Parse the declarator. */
17073 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17074 CP_PARSER_FLAGS_NONE
,
17075 /*ctor_dtor_or_conv_p=*/NULL
,
17076 /*parenthesized_p=*/NULL
,
17077 /*member_p=*/false,
17078 /*friend_p=*/false);
17079 if (declares_class_or_enum
& 2)
17080 cp_parser_check_for_definition_in_return_type (declarator
,
17081 decl_specifiers
.type
,
17082 decl_specifiers
.locations
[ds_type_spec
]);
17083 if (declarator
!= cp_error_declarator
)
17085 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17086 permerror (decl_specifiers
.locations
[ds_inline
],
17087 "explicit instantiation shall not use"
17088 " %<inline%> specifier");
17089 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17090 permerror (decl_specifiers
.locations
[ds_constexpr
],
17091 "explicit instantiation shall not use"
17092 " %<constexpr%> specifier");
17094 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17095 NORMAL
, 0, &decl_specifiers
.attributes
);
17096 /* Turn access control back on for names used during
17097 template instantiation. */
17098 pop_deferring_access_checks ();
17099 /* Do the explicit instantiation. */
17100 do_decl_instantiation (decl
, extension_specifier
);
17104 pop_deferring_access_checks ();
17105 /* Skip the body of the explicit instantiation. */
17106 cp_parser_skip_to_end_of_statement (parser
);
17109 /* We're done with the instantiation. */
17110 end_explicit_instantiation ();
17112 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17114 timevar_pop (TV_TEMPLATE_INST
);
17117 /* Parse an explicit-specialization.
17119 explicit-specialization:
17120 template < > declaration
17122 Although the standard says `declaration', what it really means is:
17124 explicit-specialization:
17125 template <> decl-specifier [opt] init-declarator [opt] ;
17126 template <> function-definition
17127 template <> explicit-specialization
17128 template <> template-declaration */
17131 cp_parser_explicit_specialization (cp_parser
* parser
)
17133 bool need_lang_pop
;
17134 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17136 /* Look for the `template' keyword. */
17137 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17138 /* Look for the `<'. */
17139 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17140 /* Look for the `>'. */
17141 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17142 /* We have processed another parameter list. */
17143 ++parser
->num_template_parameter_lists
;
17146 A template ... explicit specialization ... shall not have C
17148 if (current_lang_name
== lang_name_c
)
17150 error_at (token
->location
, "template specialization with C linkage");
17151 maybe_show_extern_c_location ();
17152 /* Give it C++ linkage to avoid confusing other parts of the
17154 push_lang_context (lang_name_cplusplus
);
17155 need_lang_pop
= true;
17158 need_lang_pop
= false;
17159 /* Let the front end know that we are beginning a specialization. */
17160 if (!begin_specialization ())
17162 end_specialization ();
17166 /* If the next keyword is `template', we need to figure out whether
17167 or not we're looking a template-declaration. */
17168 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17170 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17171 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17172 cp_parser_template_declaration_after_export (parser
,
17173 /*member_p=*/false);
17175 cp_parser_explicit_specialization (parser
);
17178 /* Parse the dependent declaration. */
17179 cp_parser_single_declaration (parser
,
17181 /*member_p=*/false,
17182 /*explicit_specialization_p=*/true,
17183 /*friend_p=*/NULL
);
17184 /* We're done with the specialization. */
17185 end_specialization ();
17186 /* For the erroneous case of a template with C linkage, we pushed an
17187 implicit C++ linkage scope; exit that scope now. */
17189 pop_lang_context ();
17190 /* We're done with this parameter list. */
17191 --parser
->num_template_parameter_lists
;
17194 /* Parse a type-specifier.
17197 simple-type-specifier
17200 elaborated-type-specifier
17208 Returns a representation of the type-specifier. For a
17209 class-specifier, enum-specifier, or elaborated-type-specifier, a
17210 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17212 The parser flags FLAGS is used to control type-specifier parsing.
17214 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17215 in a decl-specifier-seq.
17217 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17218 class-specifier, enum-specifier, or elaborated-type-specifier, then
17219 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17220 if a type is declared; 2 if it is defined. Otherwise, it is set to
17223 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17224 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17225 is set to FALSE. */
17228 cp_parser_type_specifier (cp_parser
* parser
,
17229 cp_parser_flags flags
,
17230 cp_decl_specifier_seq
*decl_specs
,
17231 bool is_declaration
,
17232 int* declares_class_or_enum
,
17233 bool* is_cv_qualifier
)
17235 tree type_spec
= NULL_TREE
;
17238 cp_decl_spec ds
= ds_last
;
17240 /* Assume this type-specifier does not declare a new type. */
17241 if (declares_class_or_enum
)
17242 *declares_class_or_enum
= 0;
17243 /* And that it does not specify a cv-qualifier. */
17244 if (is_cv_qualifier
)
17245 *is_cv_qualifier
= false;
17246 /* Peek at the next token. */
17247 token
= cp_lexer_peek_token (parser
->lexer
);
17249 /* If we're looking at a keyword, we can use that to guide the
17250 production we choose. */
17251 keyword
= token
->keyword
;
17255 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17256 goto elaborated_type_specifier
;
17258 /* Look for the enum-specifier. */
17259 type_spec
= cp_parser_enum_specifier (parser
);
17260 /* If that worked, we're done. */
17263 if (declares_class_or_enum
)
17264 *declares_class_or_enum
= 2;
17266 cp_parser_set_decl_spec_type (decl_specs
,
17269 /*type_definition_p=*/true);
17273 goto elaborated_type_specifier
;
17275 /* Any of these indicate either a class-specifier, or an
17276 elaborated-type-specifier. */
17280 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17281 goto elaborated_type_specifier
;
17283 /* Parse tentatively so that we can back up if we don't find a
17284 class-specifier. */
17285 cp_parser_parse_tentatively (parser
);
17286 /* Look for the class-specifier. */
17287 type_spec
= cp_parser_class_specifier (parser
);
17288 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17289 /* If that worked, we're done. */
17290 if (cp_parser_parse_definitely (parser
))
17292 if (declares_class_or_enum
)
17293 *declares_class_or_enum
= 2;
17295 cp_parser_set_decl_spec_type (decl_specs
,
17298 /*type_definition_p=*/true);
17302 /* Fall through. */
17303 elaborated_type_specifier
:
17304 /* We're declaring (not defining) a class or enum. */
17305 if (declares_class_or_enum
)
17306 *declares_class_or_enum
= 1;
17308 /* Fall through. */
17310 /* Look for an elaborated-type-specifier. */
17312 = (cp_parser_elaborated_type_specifier
17314 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17317 cp_parser_set_decl_spec_type (decl_specs
,
17320 /*type_definition_p=*/false);
17325 if (is_cv_qualifier
)
17326 *is_cv_qualifier
= true;
17331 if (is_cv_qualifier
)
17332 *is_cv_qualifier
= true;
17337 if (is_cv_qualifier
)
17338 *is_cv_qualifier
= true;
17342 /* The `__complex__' keyword is a GNU extension. */
17350 /* Handle simple keywords. */
17355 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17356 decl_specs
->any_specifiers_p
= true;
17358 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17361 /* If we do not already have a type-specifier, assume we are looking
17362 at a simple-type-specifier. */
17363 type_spec
= cp_parser_simple_type_specifier (parser
,
17367 /* If we didn't find a type-specifier, and a type-specifier was not
17368 optional in this context, issue an error message. */
17369 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17371 cp_parser_error (parser
, "expected type specifier");
17372 return error_mark_node
;
17378 /* Parse a simple-type-specifier.
17380 simple-type-specifier:
17381 :: [opt] nested-name-specifier [opt] type-name
17382 :: [opt] nested-name-specifier template template-id
17397 simple-type-specifier:
17399 decltype ( expression )
17402 __underlying_type ( type-id )
17406 nested-name-specifier(opt) template-name
17410 simple-type-specifier:
17412 __typeof__ unary-expression
17413 __typeof__ ( type-id )
17414 __typeof__ ( type-id ) { initializer-list , [opt] }
17416 Concepts Extension:
17418 simple-type-specifier:
17419 constrained-type-specifier
17421 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17422 appropriately updated. */
17425 cp_parser_simple_type_specifier (cp_parser
* parser
,
17426 cp_decl_specifier_seq
*decl_specs
,
17427 cp_parser_flags flags
)
17429 tree type
= NULL_TREE
;
17433 /* Peek at the next token. */
17434 token
= cp_lexer_peek_token (parser
->lexer
);
17436 /* If we're looking at a keyword, things are easy. */
17437 switch (token
->keyword
)
17441 decl_specs
->explicit_char_p
= true;
17442 type
= char_type_node
;
17445 type
= char16_type_node
;
17448 type
= char32_type_node
;
17451 type
= wchar_type_node
;
17454 type
= boolean_type_node
;
17457 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17458 type
= short_integer_type_node
;
17462 decl_specs
->explicit_int_p
= true;
17463 type
= integer_type_node
;
17469 idx
= token
->keyword
- RID_INT_N_0
;
17470 if (! int_n_enabled_p
[idx
])
17474 decl_specs
->explicit_intN_p
= true;
17475 decl_specs
->int_n_idx
= idx
;
17477 type
= int_n_trees
[idx
].signed_type
;
17481 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17482 type
= long_integer_type_node
;
17485 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17486 type
= integer_type_node
;
17489 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17490 type
= unsigned_type_node
;
17493 type
= float_type_node
;
17496 type
= double_type_node
;
17499 type
= void_type_node
;
17503 maybe_warn_cpp0x (CPP0X_AUTO
);
17504 if (parser
->auto_is_implicit_function_template_parm_p
)
17506 /* The 'auto' might be the placeholder return type for a function decl
17507 with trailing return type. */
17508 bool have_trailing_return_fn_decl
= false;
17510 cp_parser_parse_tentatively (parser
);
17511 cp_lexer_consume_token (parser
->lexer
);
17512 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17513 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17514 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17515 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17519 cp_lexer_consume_token (parser
->lexer
);
17520 cp_parser_skip_to_closing_parenthesis (parser
,
17521 /*recovering*/false,
17523 /*consume_paren*/true);
17527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17529 have_trailing_return_fn_decl
= true;
17533 cp_lexer_consume_token (parser
->lexer
);
17535 cp_parser_abort_tentative_parse (parser
);
17537 if (have_trailing_return_fn_decl
)
17539 type
= make_auto ();
17543 if (cxx_dialect
>= cxx14
)
17545 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17546 type
= TREE_TYPE (type
);
17549 type
= error_mark_node
;
17551 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17553 if (cxx_dialect
< cxx14
)
17554 error_at (token
->location
,
17555 "use of %<auto%> in lambda parameter declaration "
17556 "only available with "
17557 "-std=c++14 or -std=gnu++14");
17559 else if (cxx_dialect
< cxx14
)
17560 error_at (token
->location
,
17561 "use of %<auto%> in parameter declaration "
17562 "only available with "
17563 "-std=c++14 or -std=gnu++14");
17564 else if (!flag_concepts
)
17565 pedwarn (token
->location
, 0,
17566 "use of %<auto%> in parameter declaration "
17567 "only available with -fconcepts");
17570 type
= make_auto ();
17574 /* Since DR 743, decltype can either be a simple-type-specifier by
17575 itself or begin a nested-name-specifier. Parsing it will replace
17576 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17577 handling below decide what to do. */
17578 cp_parser_decltype (parser
);
17579 cp_lexer_set_token_position (parser
->lexer
, token
);
17583 /* Consume the `typeof' token. */
17584 cp_lexer_consume_token (parser
->lexer
);
17585 /* Parse the operand to `typeof'. */
17586 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17587 /* If it is not already a TYPE, take its type. */
17588 if (!TYPE_P (type
))
17589 type
= finish_typeof (type
);
17592 cp_parser_set_decl_spec_type (decl_specs
, type
,
17594 /*type_definition_p=*/false);
17598 case RID_UNDERLYING_TYPE
:
17599 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17601 cp_parser_set_decl_spec_type (decl_specs
, type
,
17603 /*type_definition_p=*/false);
17608 case RID_DIRECT_BASES
:
17609 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17611 cp_parser_set_decl_spec_type (decl_specs
, type
,
17613 /*type_definition_p=*/false);
17619 /* If token is an already-parsed decltype not followed by ::,
17620 it's a simple-type-specifier. */
17621 if (token
->type
== CPP_DECLTYPE
17622 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17624 type
= saved_checks_value (token
->u
.tree_check_value
);
17627 cp_parser_set_decl_spec_type (decl_specs
, type
,
17629 /*type_definition_p=*/false);
17630 /* Remember that we are handling a decltype in order to
17631 implement the resolution of DR 1510 when the argument
17632 isn't instantiation dependent. */
17633 decl_specs
->decltype_p
= true;
17635 cp_lexer_consume_token (parser
->lexer
);
17639 /* If the type-specifier was for a built-in type, we're done. */
17642 /* Record the type. */
17644 && (token
->keyword
!= RID_SIGNED
17645 && token
->keyword
!= RID_UNSIGNED
17646 && token
->keyword
!= RID_SHORT
17647 && token
->keyword
!= RID_LONG
))
17648 cp_parser_set_decl_spec_type (decl_specs
,
17651 /*type_definition_p=*/false);
17653 decl_specs
->any_specifiers_p
= true;
17655 /* Consume the token. */
17656 cp_lexer_consume_token (parser
->lexer
);
17658 if (type
== error_mark_node
)
17659 return error_mark_node
;
17661 /* There is no valid C++ program where a non-template type is
17662 followed by a "<". That usually indicates that the user thought
17663 that the type was a template. */
17664 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17667 return TYPE_NAME (type
);
17670 /* The type-specifier must be a user-defined type. */
17671 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17675 const bool typename_p
= (cxx_dialect
>= cxx2a
17676 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17678 /* Don't gobble tokens or issue error messages if this is an
17679 optional type-specifier. */
17680 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17681 cp_parser_parse_tentatively (parser
);
17683 token
= cp_lexer_peek_token (parser
->lexer
);
17685 /* Look for the optional `::' operator. */
17687 = (cp_parser_global_scope_opt (parser
,
17688 /*current_scope_valid_p=*/false)
17690 /* Look for the nested-name specifier. */
17692 = (cp_parser_nested_name_specifier_opt (parser
,
17693 /*typename_keyword_p=*/false,
17694 /*check_dependency_p=*/true,
17696 /*is_declaration=*/false)
17698 /* If we have seen a nested-name-specifier, and the next token
17699 is `template', then we are using the template-id production. */
17701 && cp_parser_optional_template_keyword (parser
))
17703 /* Look for the template-id. */
17704 type
= cp_parser_template_id (parser
,
17705 /*template_keyword_p=*/true,
17706 /*check_dependency_p=*/true,
17708 /*is_declaration=*/false);
17709 /* If the template-id did not name a type, we are out of
17711 if (TREE_CODE (type
) != TYPE_DECL
)
17713 /* ...unless we pretend we have seen 'typename'. */
17715 type
= cp_parser_make_typename_type (parser
, type
,
17719 cp_parser_error (parser
, "expected template-id for type");
17724 /* Otherwise, look for a type-name. */
17726 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17728 /* Keep track of all name-lookups performed in class scopes. */
17732 && TREE_CODE (type
) == TYPE_DECL
17733 && identifier_p (DECL_NAME (type
)))
17734 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17735 /* If it didn't work out, we don't have a TYPE. */
17736 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17737 && !cp_parser_parse_definitely (parser
))
17739 if (!type
&& cxx_dialect
>= cxx17
)
17741 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17742 cp_parser_parse_tentatively (parser
);
17744 cp_parser_global_scope_opt (parser
,
17745 /*current_scope_valid_p=*/false);
17746 cp_parser_nested_name_specifier_opt (parser
,
17747 /*typename_keyword_p=*/false,
17748 /*check_dependency_p=*/true,
17750 /*is_declaration=*/false);
17751 tree name
= cp_parser_identifier (parser
);
17752 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17753 && parser
->scope
!= error_mark_node
)
17755 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17757 /*is_template=*/false,
17758 /*is_namespace=*/false,
17759 /*check_dependency=*/true,
17760 /*ambiguous_decls=*/NULL
,
17762 if (tmpl
&& tmpl
!= error_mark_node
17763 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17764 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17765 type
= make_template_placeholder (tmpl
);
17768 type
= error_mark_node
;
17769 if (!cp_parser_simulate_error (parser
))
17770 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17771 NLE_TYPE
, token
->location
);
17775 type
= error_mark_node
;
17777 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17778 && !cp_parser_parse_definitely (parser
))
17781 if (type
&& decl_specs
)
17782 cp_parser_set_decl_spec_type (decl_specs
, type
,
17784 /*type_definition_p=*/false);
17787 /* If we didn't get a type-name, issue an error message. */
17788 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17790 cp_parser_error (parser
, "expected type-name");
17791 return error_mark_node
;
17794 if (type
&& type
!= error_mark_node
)
17796 /* See if TYPE is an Objective-C type, and if so, parse and
17797 accept any protocol references following it. Do this before
17798 the cp_parser_check_for_invalid_template_id() call, because
17799 Objective-C types can be followed by '<...>' which would
17800 enclose protocol names rather than template arguments, and so
17801 everything is fine. */
17802 if (c_dialect_objc () && !parser
->scope
17803 && (objc_is_id (type
) || objc_is_class_name (type
)))
17805 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17806 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17808 /* Clobber the "unqualified" type previously entered into
17809 DECL_SPECS with the new, improved protocol-qualified version. */
17811 decl_specs
->type
= qual_type
;
17816 /* There is no valid C++ program where a non-template type is
17817 followed by a "<". That usually indicates that the user
17818 thought that the type was a template. */
17819 cp_parser_check_for_invalid_template_id (parser
, type
,
17827 /* Parse a type-name.
17833 simple-template-id [in c++0x]
17850 Returns a TYPE_DECL for the type. */
17853 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17857 /* We can't know yet whether it is a class-name or not. */
17858 cp_parser_parse_tentatively (parser
);
17859 /* Try a class-name. */
17860 type_decl
= cp_parser_class_name (parser
,
17861 typename_keyword_p
,
17862 /*template_keyword_p=*/false,
17864 /*check_dependency_p=*/true,
17865 /*class_head_p=*/false,
17866 /*is_declaration=*/false);
17867 /* If it's not a class-name, keep looking. */
17868 if (!cp_parser_parse_definitely (parser
))
17870 if (cxx_dialect
< cxx11
)
17871 /* It must be a typedef-name or an enum-name. */
17872 return cp_parser_nonclass_name (parser
);
17874 cp_parser_parse_tentatively (parser
);
17875 /* It is either a simple-template-id representing an
17876 instantiation of an alias template... */
17877 type_decl
= cp_parser_template_id (parser
,
17878 /*template_keyword_p=*/false,
17879 /*check_dependency_p=*/true,
17881 /*is_declaration=*/false);
17882 /* Note that this must be an instantiation of an alias template
17883 because [temp.names]/6 says:
17885 A template-id that names an alias template specialization
17888 Whereas [temp.names]/7 says:
17890 A simple-template-id that names a class template
17891 specialization is a class-name.
17893 With concepts, this could also be a partial-concept-id that
17894 declares a non-type template parameter. */
17895 if (type_decl
!= NULL_TREE
17896 && TREE_CODE (type_decl
) == TYPE_DECL
17897 && TYPE_DECL_ALIAS_P (type_decl
))
17898 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17899 else if (is_constrained_parameter (type_decl
))
17900 /* Don't do anything. */ ;
17902 cp_parser_simulate_error (parser
);
17904 if (!cp_parser_parse_definitely (parser
))
17905 /* ... Or a typedef-name or an enum-name. */
17906 return cp_parser_nonclass_name (parser
);
17912 /* Check if DECL and ARGS can form a constrained-type-specifier.
17913 If ARGS is non-null, we try to form a concept check of the
17914 form DECL<?, ARGS> where ? is a wildcard that matches any
17915 kind of template argument. If ARGS is NULL, then we try to
17916 form a concept check of the form DECL<?>. */
17919 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17920 tree decl
, tree args
)
17922 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17924 /* If we a constrained-type-specifier cannot be deduced. */
17925 if (parser
->prevent_constrained_type_specifiers
)
17928 /* A constrained type specifier can only be found in an
17929 overload set or as a reference to a template declaration.
17931 FIXME: This might be masking a bug. It's possible that
17932 that the deduction below is causing template specializations
17933 to be formed with the wildcard as an argument. */
17934 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17937 /* Try to build a call expression that evaluates the
17938 concept. This can fail if the overload set refers
17939 only to non-templates. */
17940 tree placeholder
= build_nt (WILDCARD_DECL
);
17941 tree check
= build_concept_check (decl
, placeholder
, args
);
17942 if (check
== error_mark_node
)
17945 /* Deduce the checked constraint and the prototype parameter.
17947 FIXME: In certain cases, failure to deduce should be a
17948 diagnosable error. */
17951 if (!deduce_constrained_parameter (check
, conc
, proto
))
17954 /* In template parameter scope, this results in a constrained
17955 parameter. Return a descriptor of that parm. */
17956 if (processing_template_parmlist
)
17957 return build_constrained_parameter (conc
, proto
, args
);
17959 /* In a parameter-declaration-clause, constrained-type
17960 specifiers result in invented template parameters. */
17961 if (parser
->auto_is_implicit_function_template_parm_p
)
17963 tree x
= build_constrained_parameter (conc
, proto
, args
);
17964 return synthesize_implicit_template_parm (parser
, x
);
17968 /* Otherwise, we're in a context where the constrained
17969 type name is deduced and the constraint applies
17970 after deduction. */
17971 return make_constrained_auto (conc
, args
);
17977 /* If DECL refers to a concept, return a TYPE_DECL representing
17978 the result of using the constrained type specifier in the
17979 current context. DECL refers to a concept if
17981 - it is an overload set containing a function concept taking a single
17984 - it is a variable concept taking a single type argument. */
17987 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17990 && (TREE_CODE (decl
) == OVERLOAD
17991 || BASELINK_P (decl
)
17992 || variable_concept_p (decl
)))
17993 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17998 /* Check if DECL and ARGS form a partial-concept-id. If so,
17999 assign ID to the resulting constrained placeholder.
18001 Returns true if the partial-concept-id designates a placeholder
18002 and false otherwise. Note that *id is set to NULL_TREE in
18006 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18008 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18011 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18023 Returns a TYPE_DECL for the type. */
18026 cp_parser_nonclass_name (cp_parser
* parser
)
18031 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18032 identifier
= cp_parser_identifier (parser
);
18033 if (identifier
== error_mark_node
)
18034 return error_mark_node
;
18036 /* Look up the type-name. */
18037 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18039 type_decl
= strip_using_decl (type_decl
);
18041 /* If we found an overload set, then it may refer to a concept-name. */
18042 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18045 if (TREE_CODE (type_decl
) != TYPE_DECL
18046 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18048 /* See if this is an Objective-C type. */
18049 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18050 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18052 type_decl
= TYPE_NAME (type
);
18055 /* Issue an error if we did not find a type-name. */
18056 if (TREE_CODE (type_decl
) != TYPE_DECL
18057 /* In Objective-C, we have the complication that class names are
18058 normally type names and start declarations (eg, the
18059 "NSObject" in "NSObject *object;"), but can be used in an
18060 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18061 is an expression. So, a classname followed by a dot is not a
18062 valid type-name. */
18063 || (objc_is_class_name (TREE_TYPE (type_decl
))
18064 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18066 if (!cp_parser_simulate_error (parser
))
18067 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18068 NLE_TYPE
, token
->location
);
18069 return error_mark_node
;
18071 /* Remember that the name was used in the definition of the
18072 current class so that we can check later to see if the
18073 meaning would have been different after the class was
18074 entirely defined. */
18075 else if (type_decl
!= error_mark_node
18077 maybe_note_name_used_in_class (identifier
, type_decl
);
18082 /* Parse an elaborated-type-specifier. Note that the grammar given
18083 here incorporates the resolution to DR68.
18085 elaborated-type-specifier:
18086 class-key :: [opt] nested-name-specifier [opt] identifier
18087 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18088 enum-key :: [opt] nested-name-specifier [opt] identifier
18089 typename :: [opt] nested-name-specifier identifier
18090 typename :: [opt] nested-name-specifier template [opt]
18095 elaborated-type-specifier:
18096 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18097 class-key attributes :: [opt] nested-name-specifier [opt]
18098 template [opt] template-id
18099 enum attributes :: [opt] nested-name-specifier [opt] identifier
18101 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18102 declared `friend'. If IS_DECLARATION is TRUE, then this
18103 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18104 something is being declared.
18106 Returns the TYPE specified. */
18109 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18111 bool is_declaration
)
18113 enum tag_types tag_type
;
18115 tree type
= NULL_TREE
;
18116 tree attributes
= NULL_TREE
;
18118 cp_token
*token
= NULL
;
18120 /* See if we're looking at the `enum' keyword. */
18121 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18123 /* Consume the `enum' token. */
18124 cp_lexer_consume_token (parser
->lexer
);
18125 /* Remember that it's an enumeration type. */
18126 tag_type
= enum_type
;
18127 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18128 enums) is used here. */
18129 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18130 if (cp_parser_is_keyword (token
, RID_CLASS
)
18131 || cp_parser_is_keyword (token
, RID_STRUCT
))
18133 gcc_rich_location
richloc (token
->location
);
18134 richloc
.add_range (input_location
);
18135 richloc
.add_fixit_remove ();
18136 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18137 "a scoped enum must not use the %qD keyword",
18139 /* Consume the `struct' or `class' and parse it anyway. */
18140 cp_lexer_consume_token (parser
->lexer
);
18142 /* Parse the attributes. */
18143 attributes
= cp_parser_attributes_opt (parser
);
18145 /* Or, it might be `typename'. */
18146 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18149 /* Consume the `typename' token. */
18150 cp_lexer_consume_token (parser
->lexer
);
18151 /* Remember that it's a `typename' type. */
18152 tag_type
= typename_type
;
18154 /* Otherwise it must be a class-key. */
18157 tag_type
= cp_parser_class_key (parser
);
18158 if (tag_type
== none_type
)
18159 return error_mark_node
;
18160 /* Parse the attributes. */
18161 attributes
= cp_parser_attributes_opt (parser
);
18164 /* Look for the `::' operator. */
18165 globalscope
= cp_parser_global_scope_opt (parser
,
18166 /*current_scope_valid_p=*/false);
18167 /* Look for the nested-name-specifier. */
18168 tree nested_name_specifier
;
18169 if (tag_type
== typename_type
&& !globalscope
)
18171 nested_name_specifier
18172 = cp_parser_nested_name_specifier (parser
,
18173 /*typename_keyword_p=*/true,
18174 /*check_dependency_p=*/true,
18177 if (!nested_name_specifier
)
18178 return error_mark_node
;
18181 /* Even though `typename' is not present, the proposed resolution
18182 to Core Issue 180 says that in `class A<T>::B', `B' should be
18183 considered a type-name, even if `A<T>' is dependent. */
18184 nested_name_specifier
18185 = cp_parser_nested_name_specifier_opt (parser
,
18186 /*typename_keyword_p=*/true,
18187 /*check_dependency_p=*/true,
18190 /* For everything but enumeration types, consider a template-id.
18191 For an enumeration type, consider only a plain identifier. */
18192 if (tag_type
!= enum_type
)
18194 bool template_p
= false;
18197 /* Allow the `template' keyword. */
18198 template_p
= cp_parser_optional_template_keyword (parser
);
18199 /* If we didn't see `template', we don't know if there's a
18200 template-id or not. */
18202 cp_parser_parse_tentatively (parser
);
18203 /* The `template' keyword must follow a nested-name-specifier. */
18204 else if (!nested_name_specifier
)
18206 cp_parser_error (parser
, "%<template%> must follow a nested-"
18208 return error_mark_node
;
18211 /* Parse the template-id. */
18212 token
= cp_lexer_peek_token (parser
->lexer
);
18213 decl
= cp_parser_template_id (parser
, template_p
,
18214 /*check_dependency_p=*/true,
18217 /* If we didn't find a template-id, look for an ordinary
18219 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18221 /* We can get here when cp_parser_template_id, called by
18222 cp_parser_class_name with tag_type == none_type, succeeds
18223 and caches a BASELINK. Then, when called again here,
18224 instead of failing and returning an error_mark_node
18225 returns it (see template/typename17.C in C++11).
18226 ??? Could we diagnose this earlier? */
18227 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18229 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18230 type
= error_mark_node
;
18232 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18233 in effect, then we must assume that, upon instantiation, the
18234 template will correspond to a class. */
18235 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18236 && tag_type
== typename_type
)
18237 type
= make_typename_type (parser
->scope
, decl
,
18239 /*complain=*/tf_error
);
18240 /* If the `typename' keyword is in effect and DECL is not a type
18241 decl, then type is non existent. */
18242 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18244 else if (TREE_CODE (decl
) == TYPE_DECL
)
18246 type
= check_elaborated_type_specifier (tag_type
, decl
,
18247 /*allow_template_p=*/true);
18249 /* If the next token is a semicolon, this must be a specialization,
18250 instantiation, or friend declaration. Check the scope while we
18251 still know whether or not we had a nested-name-specifier. */
18252 if (type
!= error_mark_node
18253 && !nested_name_specifier
&& !is_friend
18254 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18255 check_unqualified_spec_or_inst (type
, token
->location
);
18257 else if (decl
== error_mark_node
)
18258 type
= error_mark_node
;
18263 token
= cp_lexer_peek_token (parser
->lexer
);
18264 identifier
= cp_parser_identifier (parser
);
18266 if (identifier
== error_mark_node
)
18268 parser
->scope
= NULL_TREE
;
18269 return error_mark_node
;
18272 /* For a `typename', we needn't call xref_tag. */
18273 if (tag_type
== typename_type
18274 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18275 return cp_parser_make_typename_type (parser
, identifier
,
18278 /* Template parameter lists apply only if we are not within a
18279 function parameter list. */
18280 bool template_parm_lists_apply
18281 = parser
->num_template_parameter_lists
;
18282 if (template_parm_lists_apply
)
18283 for (cp_binding_level
*s
= current_binding_level
;
18284 s
&& s
->kind
!= sk_template_parms
;
18285 s
= s
->level_chain
)
18286 if (s
->kind
== sk_function_parms
)
18287 template_parm_lists_apply
= false;
18289 /* Look up a qualified name in the usual way. */
18293 tree ambiguous_decls
;
18295 decl
= cp_parser_lookup_name (parser
, identifier
,
18297 /*is_template=*/false,
18298 /*is_namespace=*/false,
18299 /*check_dependency=*/true,
18303 /* If the lookup was ambiguous, an error will already have been
18305 if (ambiguous_decls
)
18306 return error_mark_node
;
18308 /* If we are parsing friend declaration, DECL may be a
18309 TEMPLATE_DECL tree node here. However, we need to check
18310 whether this TEMPLATE_DECL results in valid code. Consider
18311 the following example:
18314 template <class T> class C {};
18317 template <class T> friend class N::C; // #1, valid code
18319 template <class T> class Y {
18320 friend class N::C; // #2, invalid code
18323 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18324 name lookup of `N::C'. We see that friend declaration must
18325 be template for the code to be valid. Note that
18326 processing_template_decl does not work here since it is
18327 always 1 for the above two cases. */
18329 decl
= (cp_parser_maybe_treat_template_as_class
18330 (decl
, /*tag_name_p=*/is_friend
18331 && template_parm_lists_apply
));
18333 if (TREE_CODE (decl
) != TYPE_DECL
)
18335 cp_parser_diagnose_invalid_type_name (parser
,
18338 return error_mark_node
;
18341 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18343 bool allow_template
= (template_parm_lists_apply
18344 || DECL_SELF_REFERENCE_P (decl
));
18345 type
= check_elaborated_type_specifier (tag_type
, decl
,
18348 if (type
== error_mark_node
)
18349 return error_mark_node
;
18352 /* Forward declarations of nested types, such as
18357 are invalid unless all components preceding the final '::'
18358 are complete. If all enclosing types are complete, these
18359 declarations become merely pointless.
18361 Invalid forward declarations of nested types are errors
18362 caught elsewhere in parsing. Those that are pointless arrive
18365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18366 && !is_friend
&& !processing_explicit_instantiation
)
18367 warning (0, "declaration %qD does not declare anything", decl
);
18369 type
= TREE_TYPE (decl
);
18373 /* An elaborated-type-specifier sometimes introduces a new type and
18374 sometimes names an existing type. Normally, the rule is that it
18375 introduces a new type only if there is not an existing type of
18376 the same name already in scope. For example, given:
18379 void f() { struct S s; }
18381 the `struct S' in the body of `f' is the same `struct S' as in
18382 the global scope; the existing definition is used. However, if
18383 there were no global declaration, this would introduce a new
18384 local class named `S'.
18386 An exception to this rule applies to the following code:
18388 namespace N { struct S; }
18390 Here, the elaborated-type-specifier names a new type
18391 unconditionally; even if there is already an `S' in the
18392 containing scope this declaration names a new type.
18393 This exception only applies if the elaborated-type-specifier
18394 forms the complete declaration:
18398 A declaration consisting solely of `class-key identifier ;' is
18399 either a redeclaration of the name in the current scope or a
18400 forward declaration of the identifier as a class name. It
18401 introduces the name into the current scope.
18403 We are in this situation precisely when the next token is a `;'.
18405 An exception to the exception is that a `friend' declaration does
18406 *not* name a new type; i.e., given:
18408 struct S { friend struct T; };
18410 `T' is not a new type in the scope of `S'.
18412 Also, `new struct S' or `sizeof (struct S)' never results in the
18413 definition of a new type; a new type can only be declared in a
18414 declaration context. */
18420 /* Friends have special name lookup rules. */
18421 ts
= ts_within_enclosing_non_class
;
18422 else if (is_declaration
18423 && cp_lexer_next_token_is (parser
->lexer
,
18425 /* This is a `class-key identifier ;' */
18431 (template_parm_lists_apply
18432 && (cp_parser_next_token_starts_class_definition_p (parser
)
18433 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18434 /* An unqualified name was used to reference this type, so
18435 there were no qualifying templates. */
18436 if (template_parm_lists_apply
18437 && !cp_parser_check_template_parameters (parser
,
18438 /*num_templates=*/0,
18439 /*template_id*/false,
18441 /*declarator=*/NULL
))
18442 return error_mark_node
;
18443 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18447 if (type
== error_mark_node
)
18448 return error_mark_node
;
18450 /* Allow attributes on forward declarations of classes. */
18453 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18454 warning (OPT_Wattributes
,
18455 "attributes ignored on uninstantiated type");
18456 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18457 && ! processing_explicit_instantiation
)
18458 warning (OPT_Wattributes
,
18459 "attributes ignored on template instantiation");
18460 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18461 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18463 warning (OPT_Wattributes
,
18464 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18467 if (tag_type
!= enum_type
)
18469 /* Indicate whether this class was declared as a `class' or as a
18471 if (CLASS_TYPE_P (type
))
18472 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18473 cp_parser_check_class_key (tag_type
, type
);
18476 /* A "<" cannot follow an elaborated type specifier. If that
18477 happens, the user was probably trying to form a template-id. */
18478 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18484 /* Parse an enum-specifier.
18487 enum-head { enumerator-list [opt] }
18488 enum-head { enumerator-list , } [C++0x]
18491 enum-key identifier [opt] enum-base [opt]
18492 enum-key nested-name-specifier identifier enum-base [opt]
18497 enum struct [C++0x]
18500 : type-specifier-seq
18502 opaque-enum-specifier:
18503 enum-key identifier enum-base [opt] ;
18506 enum-key attributes[opt] identifier [opt] enum-base [opt]
18507 { enumerator-list [opt] }attributes[opt]
18508 enum-key attributes[opt] identifier [opt] enum-base [opt]
18509 { enumerator-list, }attributes[opt] [C++0x]
18511 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18512 if the token stream isn't an enum-specifier after all. */
18515 cp_parser_enum_specifier (cp_parser
* parser
)
18518 tree type
= NULL_TREE
;
18520 tree nested_name_specifier
= NULL_TREE
;
18522 bool scoped_enum_p
= false;
18523 bool has_underlying_type
= false;
18524 bool nested_being_defined
= false;
18525 bool new_value_list
= false;
18526 bool is_new_type
= false;
18527 bool is_unnamed
= false;
18528 tree underlying_type
= NULL_TREE
;
18529 cp_token
*type_start_token
= NULL
;
18530 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18532 parser
->colon_corrects_to_scope_p
= false;
18534 /* Parse tentatively so that we can back up if we don't find a
18536 cp_parser_parse_tentatively (parser
);
18538 /* Caller guarantees that the current token is 'enum', an identifier
18539 possibly follows, and the token after that is an opening brace.
18540 If we don't have an identifier, fabricate an anonymous name for
18541 the enumeration being defined. */
18542 cp_lexer_consume_token (parser
->lexer
);
18544 /* Parse the "class" or "struct", which indicates a scoped
18545 enumeration type in C++0x. */
18546 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18547 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18549 if (cxx_dialect
< cxx11
)
18550 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18552 /* Consume the `struct' or `class' token. */
18553 cp_lexer_consume_token (parser
->lexer
);
18555 scoped_enum_p
= true;
18558 attributes
= cp_parser_attributes_opt (parser
);
18560 /* Clear the qualification. */
18561 parser
->scope
= NULL_TREE
;
18562 parser
->qualifying_scope
= NULL_TREE
;
18563 parser
->object_scope
= NULL_TREE
;
18565 /* Figure out in what scope the declaration is being placed. */
18566 prev_scope
= current_scope ();
18568 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18570 push_deferring_access_checks (dk_no_check
);
18571 nested_name_specifier
18572 = cp_parser_nested_name_specifier_opt (parser
,
18573 /*typename_keyword_p=*/true,
18574 /*check_dependency_p=*/false,
18576 /*is_declaration=*/false);
18578 if (nested_name_specifier
)
18582 identifier
= cp_parser_identifier (parser
);
18583 name
= cp_parser_lookup_name (parser
, identifier
,
18585 /*is_template=*/false,
18586 /*is_namespace=*/false,
18587 /*check_dependency=*/true,
18588 /*ambiguous_decls=*/NULL
,
18590 if (name
&& name
!= error_mark_node
)
18592 type
= TREE_TYPE (name
);
18593 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18595 /* Are template enums allowed in ISO? */
18596 if (template_parm_scope_p ())
18597 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18598 "%qD is an enumeration template", name
);
18599 /* ignore a typename reference, for it will be solved by name
18604 else if (nested_name_specifier
== error_mark_node
)
18605 /* We already issued an error. */;
18608 error_at (type_start_token
->location
,
18609 "%qD does not name an enumeration in %qT",
18610 identifier
, nested_name_specifier
);
18611 nested_name_specifier
= error_mark_node
;
18616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18617 identifier
= cp_parser_identifier (parser
);
18620 identifier
= make_anon_name ();
18623 error_at (type_start_token
->location
,
18624 "unnamed scoped enum is not allowed");
18627 pop_deferring_access_checks ();
18629 /* Check for the `:' that denotes a specified underlying type in C++0x.
18630 Note that a ':' could also indicate a bitfield width, however. */
18631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18633 cp_decl_specifier_seq type_specifiers
;
18635 /* Consume the `:'. */
18636 cp_lexer_consume_token (parser
->lexer
);
18638 /* Parse the type-specifier-seq. */
18639 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18640 /*is_declaration=*/false,
18641 /*is_trailing_return=*/false,
18644 /* At this point this is surely not elaborated type specifier. */
18645 if (!cp_parser_parse_definitely (parser
))
18648 if (cxx_dialect
< cxx11
)
18649 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18651 has_underlying_type
= true;
18653 /* If that didn't work, stop. */
18654 if (type_specifiers
.type
!= error_mark_node
)
18656 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18657 /*initialized=*/0, NULL
);
18658 if (underlying_type
== error_mark_node
18659 || check_for_bare_parameter_packs (underlying_type
))
18660 underlying_type
= NULL_TREE
;
18664 /* Look for the `{' but don't consume it yet. */
18665 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18667 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18669 cp_parser_error (parser
, "expected %<{%>");
18670 if (has_underlying_type
)
18676 /* An opaque-enum-specifier must have a ';' here. */
18677 if ((scoped_enum_p
|| underlying_type
)
18678 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18680 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18681 if (has_underlying_type
)
18689 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18692 if (nested_name_specifier
)
18694 if (CLASS_TYPE_P (nested_name_specifier
))
18696 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18697 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18698 push_scope (nested_name_specifier
);
18700 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18702 push_nested_namespace (nested_name_specifier
);
18706 /* Issue an error message if type-definitions are forbidden here. */
18707 if (!cp_parser_check_type_definition (parser
))
18708 type
= error_mark_node
;
18710 /* Create the new type. We do this before consuming the opening
18711 brace so the enum will be recorded as being on the line of its
18712 tag (or the 'enum' keyword, if there is no tag). */
18713 type
= start_enum (identifier
, type
, underlying_type
,
18714 attributes
, scoped_enum_p
, &is_new_type
);
18716 /* If the next token is not '{' it is an opaque-enum-specifier or an
18717 elaborated-type-specifier. */
18718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18720 timevar_push (TV_PARSE_ENUM
);
18721 if (nested_name_specifier
18722 && nested_name_specifier
!= error_mark_node
)
18724 /* The following catches invalid code such as:
18725 enum class S<int>::E { A, B, C }; */
18726 if (!processing_specialization
18727 && CLASS_TYPE_P (nested_name_specifier
)
18728 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18729 error_at (type_start_token
->location
, "cannot add an enumerator "
18730 "list to a template instantiation");
18732 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18734 error_at (type_start_token
->location
,
18735 "%<%T::%E%> has not been declared",
18736 TYPE_CONTEXT (nested_name_specifier
),
18737 nested_name_specifier
);
18738 type
= error_mark_node
;
18740 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18741 && !CLASS_TYPE_P (nested_name_specifier
))
18743 error_at (type_start_token
->location
, "nested name specifier "
18744 "%qT for enum declaration does not name a class "
18745 "or namespace", nested_name_specifier
);
18746 type
= error_mark_node
;
18748 /* If that scope does not contain the scope in which the
18749 class was originally declared, the program is invalid. */
18750 else if (prev_scope
&& !is_ancestor (prev_scope
,
18751 nested_name_specifier
))
18753 if (at_namespace_scope_p ())
18754 error_at (type_start_token
->location
,
18755 "declaration of %qD in namespace %qD which does not "
18757 type
, prev_scope
, nested_name_specifier
);
18759 error_at (type_start_token
->location
,
18760 "declaration of %qD in %qD which does not "
18762 type
, prev_scope
, nested_name_specifier
);
18763 type
= error_mark_node
;
18765 /* If that scope is the scope where the declaration is being placed
18766 the program is invalid. */
18767 else if (CLASS_TYPE_P (nested_name_specifier
)
18768 && CLASS_TYPE_P (prev_scope
)
18769 && same_type_p (nested_name_specifier
, prev_scope
))
18771 permerror (type_start_token
->location
,
18772 "extra qualification not allowed");
18773 nested_name_specifier
= NULL_TREE
;
18778 begin_scope (sk_scoped_enum
, type
);
18780 /* Consume the opening brace. */
18781 matching_braces braces
;
18782 braces
.consume_open (parser
);
18784 if (type
== error_mark_node
)
18785 ; /* Nothing to add */
18786 else if (OPAQUE_ENUM_P (type
)
18787 || (cxx_dialect
> cxx98
&& processing_specialization
))
18789 new_value_list
= true;
18790 SET_OPAQUE_ENUM_P (type
, false);
18791 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18795 error_at (type_start_token
->location
,
18796 "multiple definition of %q#T", type
);
18797 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18798 "previous definition here");
18799 type
= error_mark_node
;
18802 if (type
== error_mark_node
)
18803 cp_parser_skip_to_end_of_block_or_statement (parser
);
18804 /* If the next token is not '}', then there are some enumerators. */
18805 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18807 if (is_unnamed
&& !scoped_enum_p
)
18808 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18809 "ISO C++ forbids empty unnamed enum");
18812 cp_parser_enumerator_list (parser
, type
);
18814 /* Consume the final '}'. */
18815 braces
.require_close (parser
);
18819 timevar_pop (TV_PARSE_ENUM
);
18823 /* If a ';' follows, then it is an opaque-enum-specifier
18824 and additional restrictions apply. */
18825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18828 error_at (type_start_token
->location
,
18829 "opaque-enum-specifier without name");
18830 else if (nested_name_specifier
)
18831 error_at (type_start_token
->location
,
18832 "opaque-enum-specifier must use a simple identifier");
18836 /* Look for trailing attributes to apply to this enumeration, and
18837 apply them if appropriate. */
18838 if (cp_parser_allow_gnu_extensions_p (parser
))
18840 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18841 cplus_decl_attributes (&type
,
18843 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18846 /* Finish up the enumeration. */
18847 if (type
!= error_mark_node
)
18849 if (new_value_list
)
18850 finish_enum_value_list (type
);
18852 finish_enum (type
);
18855 if (nested_name_specifier
)
18857 if (CLASS_TYPE_P (nested_name_specifier
))
18859 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18860 pop_scope (nested_name_specifier
);
18862 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18864 pop_nested_namespace (nested_name_specifier
);
18868 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18872 /* Parse an enumerator-list. The enumerators all have the indicated
18876 enumerator-definition
18877 enumerator-list , enumerator-definition */
18880 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18884 /* Parse an enumerator-definition. */
18885 cp_parser_enumerator_definition (parser
, type
);
18887 /* If the next token is not a ',', we've reached the end of
18889 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18891 /* Otherwise, consume the `,' and keep going. */
18892 cp_lexer_consume_token (parser
->lexer
);
18893 /* If the next token is a `}', there is a trailing comma. */
18894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18896 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18897 pedwarn (input_location
, OPT_Wpedantic
,
18898 "comma at end of enumerator list");
18904 /* Parse an enumerator-definition. The enumerator has the indicated
18907 enumerator-definition:
18909 enumerator = constant-expression
18916 enumerator-definition:
18917 enumerator attributes [opt]
18918 enumerator attributes [opt] = constant-expression */
18921 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18927 /* Save the input location because we are interested in the location
18928 of the identifier and not the location of the explicit value. */
18929 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18931 /* Look for the identifier. */
18932 identifier
= cp_parser_identifier (parser
);
18933 if (identifier
== error_mark_node
)
18936 /* Parse any specified attributes. */
18937 tree attrs
= cp_parser_attributes_opt (parser
);
18939 /* If the next token is an '=', then there is an explicit value. */
18940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18942 /* Consume the `=' token. */
18943 cp_lexer_consume_token (parser
->lexer
);
18944 /* Parse the value. */
18945 value
= cp_parser_constant_expression (parser
);
18950 /* If we are processing a template, make sure the initializer of the
18951 enumerator doesn't contain any bare template parameter pack. */
18952 if (check_for_bare_parameter_packs (value
))
18953 value
= error_mark_node
;
18955 /* Create the enumerator. */
18956 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18959 /* Parse a namespace-name.
18962 original-namespace-name
18965 Returns the NAMESPACE_DECL for the namespace. */
18968 cp_parser_namespace_name (cp_parser
* parser
)
18971 tree namespace_decl
;
18973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18975 /* Get the name of the namespace. */
18976 identifier
= cp_parser_identifier (parser
);
18977 if (identifier
== error_mark_node
)
18978 return error_mark_node
;
18980 /* Look up the identifier in the currently active scope. Look only
18981 for namespaces, due to:
18983 [basic.lookup.udir]
18985 When looking up a namespace-name in a using-directive or alias
18986 definition, only namespace names are considered.
18990 [basic.lookup.qual]
18992 During the lookup of a name preceding the :: scope resolution
18993 operator, object, function, and enumerator names are ignored.
18995 (Note that cp_parser_qualifying_entity only calls this
18996 function if the token after the name is the scope resolution
18998 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19000 /*is_template=*/false,
19001 /*is_namespace=*/true,
19002 /*check_dependency=*/true,
19003 /*ambiguous_decls=*/NULL
,
19005 /* If it's not a namespace, issue an error. */
19006 if (namespace_decl
== error_mark_node
19007 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19009 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19011 auto_diagnostic_group d
;
19013 if (namespace_decl
== error_mark_node
19014 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19015 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19018 if (const char *suggestion
= hint
.suggestion ())
19020 gcc_rich_location
richloc (token
->location
);
19021 richloc
.add_fixit_replace (suggestion
);
19022 error_at (&richloc
,
19023 "%qD is not a namespace-name; did you mean %qs?",
19024 identifier
, suggestion
);
19027 error_at (token
->location
, "%qD is not a namespace-name",
19031 cp_parser_error (parser
, "expected namespace-name");
19032 namespace_decl
= error_mark_node
;
19035 return namespace_decl
;
19038 /* Parse a namespace-definition.
19040 namespace-definition:
19041 named-namespace-definition
19042 unnamed-namespace-definition
19044 named-namespace-definition:
19045 original-namespace-definition
19046 extension-namespace-definition
19048 original-namespace-definition:
19049 namespace identifier { namespace-body }
19051 extension-namespace-definition:
19052 namespace original-namespace-name { namespace-body }
19054 unnamed-namespace-definition:
19055 namespace { namespace-body } */
19058 cp_parser_namespace_definition (cp_parser
* parser
)
19061 int nested_definition_count
= 0;
19063 cp_ensure_no_omp_declare_simd (parser
);
19064 cp_ensure_no_oacc_routine (parser
);
19066 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19067 const bool topmost_inline_p
= is_inline
;
19071 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19072 cp_lexer_consume_token (parser
->lexer
);
19075 /* Look for the `namespace' keyword. */
19077 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19079 /* Parse any specified attributes before the identifier. */
19080 tree attribs
= cp_parser_attributes_opt (parser
);
19084 identifier
= NULL_TREE
;
19086 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19088 if (nested_inline_p
&& nested_definition_count
!= 0)
19090 if (cxx_dialect
< cxx2a
)
19091 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19092 OPT_Wpedantic
, "nested inline namespace definitions only "
19093 "available with -std=c++2a or -std=gnu++2a");
19094 cp_lexer_consume_token (parser
->lexer
);
19097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19099 identifier
= cp_parser_identifier (parser
);
19101 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19102 pedwarn (input_location
, OPT_Wpedantic
,
19103 "standard attributes on namespaces must precede "
19104 "the namespace name");
19106 /* Parse any attributes specified after the identifier. */
19107 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19110 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19112 /* Don't forget that the innermost namespace might have been
19113 marked as inline. Use |= because we cannot overwrite
19114 IS_INLINE in case the outermost namespace is inline, but
19115 there are no nested inlines. */
19116 is_inline
|= nested_inline_p
;
19120 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19121 pedwarn (input_location
, OPT_Wpedantic
,
19122 "nested namespace definitions only available with "
19123 "-std=c++17 or -std=gnu++17");
19125 /* Nested namespace names can create new namespaces (unlike
19126 other qualified-ids). */
19127 if (int count
= (identifier
19128 ? push_namespace (identifier
, nested_inline_p
)
19130 nested_definition_count
+= count
;
19132 cp_parser_error (parser
, "nested namespace name required");
19133 cp_lexer_consume_token (parser
->lexer
);
19136 if (nested_definition_count
&& !identifier
)
19137 cp_parser_error (parser
, "namespace name required");
19139 if (nested_definition_count
&& attribs
)
19140 error_at (token
->location
,
19141 "a nested namespace definition cannot have attributes");
19142 if (nested_definition_count
&& topmost_inline_p
)
19143 error_at (token
->location
,
19144 "a nested namespace definition cannot be inline");
19146 /* Start the namespace. */
19147 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19149 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19151 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19153 /* Look for the `{' to validate starting the namespace. */
19154 matching_braces braces
;
19155 if (braces
.require_open (parser
))
19157 /* Parse the body of the namespace. */
19158 cp_parser_namespace_body (parser
);
19160 /* Look for the final `}'. */
19161 braces
.require_close (parser
);
19164 if (has_visibility
)
19165 pop_visibility (1);
19167 /* Pop the nested namespace definitions. */
19168 while (nested_definition_count
--)
19172 /* Parse a namespace-body.
19175 declaration-seq [opt] */
19178 cp_parser_namespace_body (cp_parser
* parser
)
19180 cp_parser_declaration_seq_opt (parser
);
19183 /* Parse a namespace-alias-definition.
19185 namespace-alias-definition:
19186 namespace identifier = qualified-namespace-specifier ; */
19189 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19192 tree namespace_specifier
;
19194 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19196 /* Look for the `namespace' keyword. */
19197 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19198 /* Look for the identifier. */
19199 identifier
= cp_parser_identifier (parser
);
19200 if (identifier
== error_mark_node
)
19202 /* Look for the `=' token. */
19203 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19204 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19206 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19207 /* Skip the definition. */
19208 cp_lexer_consume_token (parser
->lexer
);
19209 if (cp_parser_skip_to_closing_brace (parser
))
19210 cp_lexer_consume_token (parser
->lexer
);
19213 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19214 /* Look for the qualified-namespace-specifier. */
19215 namespace_specifier
19216 = cp_parser_qualified_namespace_specifier (parser
);
19217 /* Look for the `;' token. */
19218 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19220 /* Register the alias in the symbol table. */
19221 do_namespace_alias (identifier
, namespace_specifier
);
19224 /* Parse a qualified-namespace-specifier.
19226 qualified-namespace-specifier:
19227 :: [opt] nested-name-specifier [opt] namespace-name
19229 Returns a NAMESPACE_DECL corresponding to the specified
19233 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19235 /* Look for the optional `::'. */
19236 cp_parser_global_scope_opt (parser
,
19237 /*current_scope_valid_p=*/false);
19239 /* Look for the optional nested-name-specifier. */
19240 cp_parser_nested_name_specifier_opt (parser
,
19241 /*typename_keyword_p=*/false,
19242 /*check_dependency_p=*/true,
19244 /*is_declaration=*/true);
19246 return cp_parser_namespace_name (parser
);
19249 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19250 access declaration.
19253 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19254 using :: unqualified-id ;
19256 access-declaration:
19262 cp_parser_using_declaration (cp_parser
* parser
,
19263 bool access_declaration_p
)
19266 bool typename_p
= false;
19267 bool global_scope_p
;
19271 int oldcount
= errorcount
;
19272 cp_token
*diag_token
= NULL
;
19274 if (access_declaration_p
)
19276 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19277 cp_parser_parse_tentatively (parser
);
19281 /* Look for the `using' keyword. */
19282 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19285 /* Peek at the next token. */
19286 token
= cp_lexer_peek_token (parser
->lexer
);
19287 /* See if it's `typename'. */
19288 if (token
->keyword
== RID_TYPENAME
)
19290 /* Remember that we've seen it. */
19292 /* Consume the `typename' token. */
19293 cp_lexer_consume_token (parser
->lexer
);
19297 /* Look for the optional global scope qualification. */
19299 = (cp_parser_global_scope_opt (parser
,
19300 /*current_scope_valid_p=*/false)
19303 /* If we saw `typename', or didn't see `::', then there must be a
19304 nested-name-specifier present. */
19305 if (typename_p
|| !global_scope_p
)
19307 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19308 /*check_dependency_p=*/true,
19310 /*is_declaration=*/true);
19311 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19313 cp_parser_skip_to_end_of_block_or_statement (parser
);
19317 /* Otherwise, we could be in either of the two productions. In that
19318 case, treat the nested-name-specifier as optional. */
19320 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19321 /*typename_keyword_p=*/false,
19322 /*check_dependency_p=*/true,
19324 /*is_declaration=*/true);
19326 qscope
= global_namespace
;
19327 else if (UNSCOPED_ENUM_P (qscope
))
19328 qscope
= CP_TYPE_CONTEXT (qscope
);
19330 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19331 /* Something has already gone wrong; there's no need to parse
19332 further. Since an error has occurred, the return value of
19333 cp_parser_parse_definitely will be false, as required. */
19334 return cp_parser_parse_definitely (parser
);
19336 token
= cp_lexer_peek_token (parser
->lexer
);
19337 /* Parse the unqualified-id. */
19338 identifier
= cp_parser_unqualified_id (parser
,
19339 /*template_keyword_p=*/false,
19340 /*check_dependency_p=*/true,
19341 /*declarator_p=*/true,
19342 /*optional_p=*/false);
19344 if (access_declaration_p
)
19346 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19347 cp_parser_simulate_error (parser
);
19348 if (!cp_parser_parse_definitely (parser
))
19351 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19353 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19354 if (cxx_dialect
< cxx17
19355 && !in_system_header_at (ell
->location
))
19356 pedwarn (ell
->location
, 0,
19357 "pack expansion in using-declaration only available "
19358 "with -std=c++17 or -std=gnu++17");
19359 qscope
= make_pack_expansion (qscope
);
19362 /* The function we call to handle a using-declaration is different
19363 depending on what scope we are in. */
19364 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19366 else if (!identifier_p (identifier
)
19367 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19368 /* [namespace.udecl]
19370 A using declaration shall not name a template-id. */
19371 error_at (token
->location
,
19372 "a template-id may not appear in a using-declaration");
19375 if (at_class_scope_p ())
19377 /* Create the USING_DECL. */
19378 decl
= do_class_using_decl (qscope
, identifier
);
19380 if (decl
&& typename_p
)
19381 USING_DECL_TYPENAME_P (decl
) = 1;
19383 if (check_for_bare_parameter_packs (decl
))
19385 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19389 /* Add it to the list of members in this class. */
19390 finish_member_declaration (decl
);
19394 decl
= cp_parser_lookup_name_simple (parser
,
19397 if (decl
== error_mark_node
)
19398 cp_parser_name_lookup_error (parser
, identifier
,
19401 else if (check_for_bare_parameter_packs (decl
))
19403 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19406 else if (!at_namespace_scope_p ())
19407 finish_local_using_decl (decl
, qscope
, identifier
);
19409 finish_namespace_using_decl (decl
, qscope
, identifier
);
19413 if (!access_declaration_p
19414 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19416 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19417 if (cxx_dialect
< cxx17
)
19418 pedwarn (comma
->location
, 0,
19419 "comma-separated list in using-declaration only available "
19420 "with -std=c++17 or -std=gnu++17");
19424 /* Look for the final `;'. */
19425 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19427 if (access_declaration_p
&& errorcount
== oldcount
)
19428 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19429 "access declarations are deprecated "
19430 "in favour of using-declarations; "
19431 "suggestion: add the %<using%> keyword");
19436 /* Parse an alias-declaration.
19439 using identifier attribute-specifier-seq [opt] = type-id */
19442 cp_parser_alias_declaration (cp_parser
* parser
)
19444 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19445 location_t id_location
, type_location
;
19446 cp_declarator
*declarator
;
19447 cp_decl_specifier_seq decl_specs
;
19449 const char *saved_message
= NULL
;
19451 /* Look for the `using' keyword. */
19452 cp_token
*using_token
19453 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19454 if (using_token
== NULL
)
19455 return error_mark_node
;
19457 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19458 id
= cp_parser_identifier (parser
);
19459 if (id
== error_mark_node
)
19460 return error_mark_node
;
19462 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19463 attributes
= cp_parser_attributes_opt (parser
);
19464 if (attributes
== error_mark_node
)
19465 return error_mark_node
;
19467 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19469 if (cp_parser_error_occurred (parser
))
19470 return error_mark_node
;
19472 cp_parser_commit_to_tentative_parse (parser
);
19474 /* Now we are going to parse the type-id of the declaration. */
19479 "A type-specifier-seq shall not define a class or enumeration
19480 unless it appears in the type-id of an alias-declaration (7.1.3) that
19481 is not the declaration of a template-declaration."
19483 In other words, if we currently are in an alias template, the
19484 type-id should not define a type.
19486 So let's set parser->type_definition_forbidden_message in that
19487 case; cp_parser_check_type_definition (called by
19488 cp_parser_class_specifier) will then emit an error if a type is
19489 defined in the type-id. */
19490 if (parser
->num_template_parameter_lists
)
19492 saved_message
= parser
->type_definition_forbidden_message
;
19493 parser
->type_definition_forbidden_message
=
19494 G_("types may not be defined in alias template declarations");
19497 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19500 /* Restore the error message if need be. */
19501 if (parser
->num_template_parameter_lists
)
19502 parser
->type_definition_forbidden_message
= saved_message
;
19504 if (type
== error_mark_node
19505 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19507 cp_parser_skip_to_end_of_block_or_statement (parser
);
19508 return error_mark_node
;
19511 /* A typedef-name can also be introduced by an alias-declaration. The
19512 identifier following the using keyword becomes a typedef-name. It has
19513 the same semantics as if it were introduced by the typedef
19514 specifier. In particular, it does not define a new type and it shall
19515 not appear in the type-id. */
19517 clear_decl_specs (&decl_specs
);
19518 decl_specs
.type
= type
;
19519 if (attributes
!= NULL_TREE
)
19521 decl_specs
.attributes
= attributes
;
19522 set_and_check_decl_spec_loc (&decl_specs
,
19526 set_and_check_decl_spec_loc (&decl_specs
,
19529 set_and_check_decl_spec_loc (&decl_specs
,
19532 decl_specs
.locations
[ds_type_spec
] = type_location
;
19534 if (parser
->num_template_parameter_lists
19535 && !cp_parser_check_template_parameters (parser
,
19536 /*num_templates=*/0,
19537 /*template_id*/false,
19539 /*declarator=*/NULL
))
19540 return error_mark_node
;
19542 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19544 member_p
= at_class_scope_p ();
19546 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19547 NULL_TREE
, attributes
);
19549 decl
= start_decl (declarator
, &decl_specs
, 0,
19550 attributes
, NULL_TREE
, &pushed_scope
);
19551 if (decl
== error_mark_node
)
19554 // Attach constraints to the alias declaration.
19555 if (flag_concepts
&& current_template_parms
)
19557 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19558 tree constr
= build_constraints (reqs
, NULL_TREE
);
19559 set_constraints (decl
, constr
);
19562 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19565 pop_scope (pushed_scope
);
19567 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19568 added into the symbol table; otherwise, return the TYPE_DECL. */
19569 if (DECL_LANG_SPECIFIC (decl
)
19570 && DECL_TEMPLATE_INFO (decl
)
19571 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19573 decl
= DECL_TI_TEMPLATE (decl
);
19575 check_member_template (decl
);
19581 /* Parse a using-directive.
19584 using namespace :: [opt] nested-name-specifier [opt]
19585 namespace-name ; */
19588 cp_parser_using_directive (cp_parser
* parser
)
19590 tree namespace_decl
;
19593 /* Look for the `using' keyword. */
19594 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19595 /* And the `namespace' keyword. */
19596 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19597 /* Look for the optional `::' operator. */
19598 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19599 /* And the optional nested-name-specifier. */
19600 cp_parser_nested_name_specifier_opt (parser
,
19601 /*typename_keyword_p=*/false,
19602 /*check_dependency_p=*/true,
19604 /*is_declaration=*/true);
19605 /* Get the namespace being used. */
19606 namespace_decl
= cp_parser_namespace_name (parser
);
19607 /* And any specified attributes. */
19608 attribs
= cp_parser_attributes_opt (parser
);
19610 /* Update the symbol table. */
19611 if (namespace_bindings_p ())
19612 finish_namespace_using_directive (namespace_decl
, attribs
);
19614 finish_local_using_directive (namespace_decl
, attribs
);
19616 /* Look for the final `;'. */
19617 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19620 /* Parse an asm-definition.
19627 asm-qualifier-list:
19629 asm-qualifier-list asm-qualifier
19632 asm ( string-literal ) ;
19637 asm asm-qualifier-list [opt] ( string-literal ) ;
19638 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19639 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19640 : asm-operand-list [opt] ) ;
19641 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19642 : asm-operand-list [opt]
19643 : asm-clobber-list [opt] ) ;
19644 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19645 : asm-clobber-list [opt]
19646 : asm-goto-list ) ;
19648 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19649 contains goto, and is the only allowed form in that case. No duplicates are
19650 allowed in an asm-qualifier-list. */
19653 cp_parser_asm_definition (cp_parser
* parser
)
19656 tree outputs
= NULL_TREE
;
19657 tree inputs
= NULL_TREE
;
19658 tree clobbers
= NULL_TREE
;
19659 tree labels
= NULL_TREE
;
19661 bool volatile_p
= false;
19662 bool extended_p
= false;
19663 bool invalid_inputs_p
= false;
19664 bool invalid_outputs_p
= false;
19665 bool inline_p
= false;
19666 bool goto_p
= false;
19667 required_token missing
= RT_NONE
;
19669 /* Look for the `asm' keyword. */
19670 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19672 if (parser
->in_function_body
19673 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19675 error ("%<asm%> in %<constexpr%> function");
19676 cp_function_chain
->invalid_constexpr
= true;
19679 /* See if the next token is `volatile'. */
19680 if (cp_parser_allow_gnu_extensions_p (parser
))
19681 for (bool done
= false; !done
; )
19682 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19687 /* Remember that we saw the `volatile' keyword. */
19689 /* Consume the token. */
19690 cp_lexer_consume_token (parser
->lexer
);
19696 if (!inline_p
&& parser
->in_function_body
)
19698 /* Remember that we saw the `inline' keyword. */
19700 /* Consume the token. */
19701 cp_lexer_consume_token (parser
->lexer
);
19707 if (!goto_p
&& parser
->in_function_body
)
19709 /* Remember that we saw the `goto' keyword. */
19711 /* Consume the token. */
19712 cp_lexer_consume_token (parser
->lexer
);
19721 /* Look for the opening `('. */
19722 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19724 /* Look for the string. */
19725 string
= cp_parser_string_literal (parser
, false, false);
19726 if (string
== error_mark_node
)
19728 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19729 /*consume_paren=*/true);
19733 /* If we're allowing GNU extensions, check for the extended assembly
19734 syntax. Unfortunately, the `:' tokens need not be separated by
19735 a space in C, and so, for compatibility, we tolerate that here
19736 too. Doing that means that we have to treat the `::' operator as
19738 if (cp_parser_allow_gnu_extensions_p (parser
)
19739 && parser
->in_function_body
19740 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19741 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19743 bool inputs_p
= false;
19744 bool clobbers_p
= false;
19745 bool labels_p
= false;
19747 /* The extended syntax was used. */
19750 /* Look for outputs. */
19751 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19753 /* Consume the `:'. */
19754 cp_lexer_consume_token (parser
->lexer
);
19755 /* Parse the output-operands. */
19756 if (cp_lexer_next_token_is_not (parser
->lexer
,
19758 && cp_lexer_next_token_is_not (parser
->lexer
,
19760 && cp_lexer_next_token_is_not (parser
->lexer
,
19764 outputs
= cp_parser_asm_operand_list (parser
);
19765 if (outputs
== error_mark_node
)
19766 invalid_outputs_p
= true;
19769 /* If the next token is `::', there are no outputs, and the
19770 next token is the beginning of the inputs. */
19771 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19772 /* The inputs are coming next. */
19775 /* Look for inputs. */
19777 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19779 /* Consume the `:' or `::'. */
19780 cp_lexer_consume_token (parser
->lexer
);
19781 /* Parse the output-operands. */
19782 if (cp_lexer_next_token_is_not (parser
->lexer
,
19784 && cp_lexer_next_token_is_not (parser
->lexer
,
19786 && cp_lexer_next_token_is_not (parser
->lexer
,
19789 inputs
= cp_parser_asm_operand_list (parser
);
19790 if (inputs
== error_mark_node
)
19791 invalid_inputs_p
= true;
19794 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19795 /* The clobbers are coming next. */
19798 /* Look for clobbers. */
19800 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19803 /* Consume the `:' or `::'. */
19804 cp_lexer_consume_token (parser
->lexer
);
19805 /* Parse the clobbers. */
19806 if (cp_lexer_next_token_is_not (parser
->lexer
,
19808 && cp_lexer_next_token_is_not (parser
->lexer
,
19810 clobbers
= cp_parser_asm_clobber_list (parser
);
19813 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19814 /* The labels are coming next. */
19817 /* Look for labels. */
19819 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19822 /* Consume the `:' or `::'. */
19823 cp_lexer_consume_token (parser
->lexer
);
19824 /* Parse the labels. */
19825 labels
= cp_parser_asm_label_list (parser
);
19828 if (goto_p
&& !labels_p
)
19829 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19832 missing
= RT_COLON_SCOPE
;
19834 /* Look for the closing `)'. */
19835 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19836 missing
? missing
: RT_CLOSE_PAREN
))
19837 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19838 /*consume_paren=*/true);
19839 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19841 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19843 /* Create the ASM_EXPR. */
19844 if (parser
->in_function_body
)
19846 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19847 inputs
, clobbers
, labels
, inline_p
);
19848 /* If the extended syntax was not used, mark the ASM_EXPR. */
19851 tree temp
= asm_stmt
;
19852 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19853 temp
= TREE_OPERAND (temp
, 0);
19855 ASM_INPUT_P (temp
) = 1;
19859 symtab
->finalize_toplevel_asm (string
);
19863 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19864 type that comes from the decl-specifier-seq. */
19867 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19869 for (cp_declarator
*d
= declarator
; d
;)
19879 if (TYPE_PTRMEMFUNC_P (type
))
19880 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19881 type
= TREE_TYPE (type
);
19889 /* Declarators [gram.dcl.decl] */
19891 /* Parse an init-declarator.
19894 declarator initializer [opt]
19899 declarator asm-specification [opt] attributes [opt] initializer [opt]
19901 function-definition:
19902 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19904 decl-specifier-seq [opt] declarator function-try-block
19908 function-definition:
19909 __extension__ function-definition
19913 function-definition:
19914 decl-specifier-seq [opt] declarator function-transaction-block
19916 The parser flags FLAGS is used to control type-specifier parsing.
19918 The DECL_SPECIFIERS apply to this declarator. Returns a
19919 representation of the entity declared. If MEMBER_P is TRUE, then
19920 this declarator appears in a class scope. The new DECL created by
19921 this declarator is returned.
19923 The CHECKS are access checks that should be performed once we know
19924 what entity is being declared (and, therefore, what classes have
19927 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19928 for a function-definition here as well. If the declarator is a
19929 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19930 be TRUE upon return. By that point, the function-definition will
19931 have been completely parsed.
19933 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19936 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19937 parsed declaration if it is an uninitialized single declarator not followed
19938 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19939 if present, will not be consumed. If returned, this declarator will be
19940 created with SD_INITIALIZED but will not call cp_finish_decl.
19942 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19943 and there is an initializer, the pointed location_t is set to the
19944 location of the '=' or `(', or '{' in C++11 token introducing the
19948 cp_parser_init_declarator (cp_parser
* parser
,
19949 cp_parser_flags flags
,
19950 cp_decl_specifier_seq
*decl_specifiers
,
19951 vec
<deferred_access_check
, va_gc
> *checks
,
19952 bool function_definition_allowed_p
,
19954 int declares_class_or_enum
,
19955 bool* function_definition_p
,
19956 tree
* maybe_range_for_decl
,
19957 location_t
* init_loc
,
19960 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19961 *attributes_start_token
= NULL
;
19962 cp_declarator
*declarator
;
19963 tree prefix_attributes
;
19964 tree attributes
= NULL
;
19965 tree asm_specification
;
19967 tree decl
= NULL_TREE
;
19969 int is_initialized
;
19970 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19971 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19973 enum cpp_ttype initialization_kind
;
19974 bool is_direct_init
= false;
19975 bool is_non_constant_init
;
19976 int ctor_dtor_or_conv_p
;
19977 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19978 tree pushed_scope
= NULL_TREE
;
19979 bool range_for_decl_p
= false;
19980 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19981 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19983 /* Gather the attributes that were provided with the
19984 decl-specifiers. */
19985 prefix_attributes
= decl_specifiers
->attributes
;
19987 /* Assume that this is not the declarator for a function
19989 if (function_definition_p
)
19990 *function_definition_p
= false;
19992 /* Default arguments are only permitted for function parameters. */
19993 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19994 parser
->default_arg_ok_p
= false;
19996 /* Defer access checks while parsing the declarator; we cannot know
19997 what names are accessible until we know what is being
19999 resume_deferring_access_checks ();
20001 token
= cp_lexer_peek_token (parser
->lexer
);
20003 /* Parse the declarator. */
20005 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20006 flags
, &ctor_dtor_or_conv_p
,
20007 /*parenthesized_p=*/NULL
,
20008 member_p
, friend_p
);
20009 /* Gather up the deferred checks. */
20010 stop_deferring_access_checks ();
20012 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20014 /* If the DECLARATOR was erroneous, there's no need to go
20016 if (declarator
== cp_error_declarator
)
20017 return error_mark_node
;
20019 /* Check that the number of template-parameter-lists is OK. */
20020 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20022 return error_mark_node
;
20024 if (declares_class_or_enum
& 2)
20025 cp_parser_check_for_definition_in_return_type (declarator
,
20026 decl_specifiers
->type
,
20027 decl_specifiers
->locations
[ds_type_spec
]);
20029 /* Figure out what scope the entity declared by the DECLARATOR is
20030 located in. `grokdeclarator' sometimes changes the scope, so
20031 we compute it now. */
20032 scope
= get_scope_of_declarator (declarator
);
20034 /* Perform any lookups in the declared type which were thought to be
20035 dependent, but are not in the scope of the declarator. */
20036 decl_specifiers
->type
20037 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20039 /* If we're allowing GNU extensions, look for an
20040 asm-specification. */
20041 if (cp_parser_allow_gnu_extensions_p (parser
))
20043 /* Look for an asm-specification. */
20044 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20045 asm_specification
= cp_parser_asm_specification_opt (parser
);
20048 asm_specification
= NULL_TREE
;
20050 /* Look for attributes. */
20051 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20052 attributes
= cp_parser_attributes_opt (parser
);
20054 /* Peek at the next token. */
20055 token
= cp_lexer_peek_token (parser
->lexer
);
20057 bool bogus_implicit_tmpl
= false;
20059 if (function_declarator_p (declarator
))
20061 /* Handle C++17 deduction guides. */
20062 if (!decl_specifiers
->type
20063 && ctor_dtor_or_conv_p
<= 0
20064 && cxx_dialect
>= cxx17
)
20066 cp_declarator
*id
= get_id_declarator (declarator
);
20067 tree name
= id
->u
.id
.unqualified_name
;
20068 parser
->scope
= id
->u
.id
.qualifying_scope
;
20069 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20071 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20072 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20074 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20075 id
->u
.id
.sfk
= sfk_deduction_guide
;
20076 ctor_dtor_or_conv_p
= 1;
20080 /* Check to see if the token indicates the start of a
20081 function-definition. */
20082 if (cp_parser_token_starts_function_definition_p (token
))
20084 if (!function_definition_allowed_p
)
20086 /* If a function-definition should not appear here, issue an
20088 cp_parser_error (parser
,
20089 "a function-definition is not allowed here");
20090 return error_mark_node
;
20093 location_t func_brace_location
20094 = cp_lexer_peek_token (parser
->lexer
)->location
;
20096 /* Neither attributes nor an asm-specification are allowed
20097 on a function-definition. */
20098 if (asm_specification
)
20099 error_at (asm_spec_start_token
->location
,
20100 "an asm-specification is not allowed "
20101 "on a function-definition");
20103 error_at (attributes_start_token
->location
,
20104 "attributes are not allowed "
20105 "on a function-definition");
20106 /* This is a function-definition. */
20107 *function_definition_p
= true;
20109 /* Parse the function definition. */
20111 decl
= cp_parser_save_member_function_body (parser
,
20114 prefix_attributes
);
20117 (cp_parser_function_definition_from_specifiers_and_declarator
20118 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20120 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20122 /* This is where the prologue starts... */
20123 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20124 = func_brace_location
;
20130 else if (parser
->fully_implicit_function_template_p
)
20132 /* A non-template declaration involving a function parameter list
20133 containing an implicit template parameter will be made into a
20134 template. If the resulting declaration is not going to be an
20135 actual function then finish the template scope here to prevent it.
20136 An error message will be issued once we have a decl to talk about.
20138 FIXME probably we should do type deduction rather than create an
20139 implicit template, but the standard currently doesn't allow it. */
20140 bogus_implicit_tmpl
= true;
20141 finish_fully_implicit_template (parser
, NULL_TREE
);
20146 Only in function declarations for constructors, destructors, type
20147 conversions, and deduction guides can the decl-specifier-seq be omitted.
20149 We explicitly postpone this check past the point where we handle
20150 function-definitions because we tolerate function-definitions
20151 that are missing their return types in some modes. */
20152 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20154 cp_parser_error (parser
,
20155 "expected constructor, destructor, or type conversion");
20156 return error_mark_node
;
20159 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20160 if (token
->type
== CPP_EQ
20161 || token
->type
== CPP_OPEN_PAREN
20162 || token
->type
== CPP_OPEN_BRACE
)
20164 is_initialized
= SD_INITIALIZED
;
20165 initialization_kind
= token
->type
;
20166 if (maybe_range_for_decl
)
20167 *maybe_range_for_decl
= error_mark_node
;
20168 tmp_init_loc
= token
->location
;
20169 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20170 *init_loc
= tmp_init_loc
;
20172 if (token
->type
== CPP_EQ
20173 && function_declarator_p (declarator
))
20175 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20176 if (t2
->keyword
== RID_DEFAULT
)
20177 is_initialized
= SD_DEFAULTED
;
20178 else if (t2
->keyword
== RID_DELETE
)
20179 is_initialized
= SD_DELETED
;
20184 /* If the init-declarator isn't initialized and isn't followed by a
20185 `,' or `;', it's not a valid init-declarator. */
20186 if (token
->type
!= CPP_COMMA
20187 && token
->type
!= CPP_SEMICOLON
)
20189 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20190 range_for_decl_p
= true;
20193 if (!maybe_range_for_decl
)
20194 cp_parser_error (parser
, "expected initializer");
20195 return error_mark_node
;
20198 is_initialized
= SD_UNINITIALIZED
;
20199 initialization_kind
= CPP_EOF
;
20202 /* Because start_decl has side-effects, we should only call it if we
20203 know we're going ahead. By this point, we know that we cannot
20204 possibly be looking at any other construct. */
20205 cp_parser_commit_to_tentative_parse (parser
);
20207 /* Enter the newly declared entry in the symbol table. If we're
20208 processing a declaration in a class-specifier, we wait until
20209 after processing the initializer. */
20212 if (parser
->in_unbraced_linkage_specification_p
)
20213 decl_specifiers
->storage_class
= sc_extern
;
20214 decl
= start_decl (declarator
, decl_specifiers
,
20215 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20216 attributes
, prefix_attributes
, &pushed_scope
);
20217 cp_finalize_omp_declare_simd (parser
, decl
);
20218 cp_finalize_oacc_routine (parser
, decl
, false);
20219 /* Adjust location of decl if declarator->id_loc is more appropriate:
20220 set, and decl wasn't merged with another decl, in which case its
20221 location would be different from input_location, and more accurate. */
20223 && declarator
->id_loc
!= UNKNOWN_LOCATION
20224 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20225 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20228 /* Enter the SCOPE. That way unqualified names appearing in the
20229 initializer will be looked up in SCOPE. */
20230 pushed_scope
= push_scope (scope
);
20232 /* Perform deferred access control checks, now that we know in which
20233 SCOPE the declared entity resides. */
20234 if (!member_p
&& decl
)
20236 tree saved_current_function_decl
= NULL_TREE
;
20238 /* If the entity being declared is a function, pretend that we
20239 are in its scope. If it is a `friend', it may have access to
20240 things that would not otherwise be accessible. */
20241 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20243 saved_current_function_decl
= current_function_decl
;
20244 current_function_decl
= decl
;
20247 /* Perform access checks for template parameters. */
20248 cp_parser_perform_template_parameter_access_checks (checks
);
20250 /* Perform the access control checks for the declarator and the
20251 decl-specifiers. */
20252 perform_deferred_access_checks (tf_warning_or_error
);
20254 /* Restore the saved value. */
20255 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20256 current_function_decl
= saved_current_function_decl
;
20259 /* Parse the initializer. */
20260 initializer
= NULL_TREE
;
20261 is_direct_init
= false;
20262 is_non_constant_init
= true;
20263 if (is_initialized
)
20265 if (function_declarator_p (declarator
))
20267 if (initialization_kind
== CPP_EQ
)
20268 initializer
= cp_parser_pure_specifier (parser
);
20271 /* If the declaration was erroneous, we don't really
20272 know what the user intended, so just silently
20273 consume the initializer. */
20274 if (decl
!= error_mark_node
)
20275 error_at (tmp_init_loc
, "initializer provided for function");
20276 cp_parser_skip_to_closing_parenthesis (parser
,
20277 /*recovering=*/true,
20278 /*or_comma=*/false,
20279 /*consume_paren=*/true);
20284 /* We want to record the extra mangling scope for in-class
20285 initializers of class members and initializers of static data
20286 member templates. The former involves deferring
20287 parsing of the initializer until end of class as with default
20288 arguments. So right here we only handle the latter. */
20289 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20290 start_lambda_scope (decl
);
20291 initializer
= cp_parser_initializer (parser
,
20293 &is_non_constant_init
);
20294 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20295 finish_lambda_scope ();
20296 if (initializer
== error_mark_node
)
20297 cp_parser_skip_to_end_of_statement (parser
);
20301 /* The old parser allows attributes to appear after a parenthesized
20302 initializer. Mark Mitchell proposed removing this functionality
20303 on the GCC mailing lists on 2002-08-13. This parser accepts the
20304 attributes -- but ignores them. Made a permerror in GCC 8. */
20305 if (cp_parser_allow_gnu_extensions_p (parser
)
20306 && initialization_kind
== CPP_OPEN_PAREN
20307 && cp_parser_attributes_opt (parser
)
20308 && permerror (input_location
,
20309 "attributes after parenthesized initializer ignored"))
20312 if (flag_permissive
&& !hint
)
20315 inform (input_location
,
20316 "this flexibility is deprecated and will be removed");
20320 /* And now complain about a non-function implicit template. */
20321 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20322 error_at (DECL_SOURCE_LOCATION (decl
),
20323 "non-function %qD declared as implicit template", decl
);
20325 /* For an in-class declaration, use `grokfield' to create the
20331 pop_scope (pushed_scope
);
20332 pushed_scope
= NULL_TREE
;
20334 decl
= grokfield (declarator
, decl_specifiers
,
20335 initializer
, !is_non_constant_init
,
20336 /*asmspec=*/NULL_TREE
,
20337 attr_chainon (attributes
, prefix_attributes
));
20338 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20339 cp_parser_save_default_args (parser
, decl
);
20340 cp_finalize_omp_declare_simd (parser
, decl
);
20341 cp_finalize_oacc_routine (parser
, decl
, false);
20344 /* Finish processing the declaration. But, skip member
20346 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20348 cp_finish_decl (decl
,
20349 initializer
, !is_non_constant_init
,
20351 /* If the initializer is in parentheses, then this is
20352 a direct-initialization, which means that an
20353 `explicit' constructor is OK. Otherwise, an
20354 `explicit' constructor cannot be used. */
20355 ((is_direct_init
|| !is_initialized
)
20356 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20358 else if ((cxx_dialect
!= cxx98
) && friend_p
20359 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20360 /* Core issue #226 (C++0x only): A default template-argument
20361 shall not be specified in a friend class template
20363 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20364 /*is_partial=*/false, /*is_friend_decl=*/1);
20366 if (!friend_p
&& pushed_scope
)
20367 pop_scope (pushed_scope
);
20369 if (function_declarator_p (declarator
)
20370 && parser
->fully_implicit_function_template_p
)
20373 decl
= finish_fully_implicit_template (parser
, decl
);
20375 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20378 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20379 && type_uses_auto (decl_specifiers
->type
))
20380 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20385 /* Parse a declarator.
20389 ptr-operator declarator
20391 abstract-declarator:
20392 ptr-operator abstract-declarator [opt]
20393 direct-abstract-declarator
20398 attributes [opt] direct-declarator
20399 attributes [opt] ptr-operator declarator
20401 abstract-declarator:
20402 attributes [opt] ptr-operator abstract-declarator [opt]
20403 attributes [opt] direct-abstract-declarator
20405 The parser flags FLAGS is used to control type-specifier parsing.
20407 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20408 detect constructors, destructors, deduction guides, or conversion operators.
20409 It is set to -1 if the declarator is a name, and +1 if it is a
20410 function. Otherwise it is set to zero. Usually you just want to
20411 test for >0, but internally the negative value is used.
20413 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20414 a decl-specifier-seq unless it declares a constructor, destructor,
20415 or conversion. It might seem that we could check this condition in
20416 semantic analysis, rather than parsing, but that makes it difficult
20417 to handle something like `f()'. We want to notice that there are
20418 no decl-specifiers, and therefore realize that this is an
20419 expression, not a declaration.)
20421 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20422 the declarator is a direct-declarator of the form "(...)".
20424 MEMBER_P is true iff this declarator is a member-declarator.
20426 FRIEND_P is true iff this declarator is a friend. */
20428 static cp_declarator
*
20429 cp_parser_declarator (cp_parser
* parser
,
20430 cp_parser_declarator_kind dcl_kind
,
20431 cp_parser_flags flags
,
20432 int* ctor_dtor_or_conv_p
,
20433 bool* parenthesized_p
,
20434 bool member_p
, bool friend_p
)
20436 cp_declarator
*declarator
;
20437 enum tree_code code
;
20438 cp_cv_quals cv_quals
;
20440 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20442 /* Assume this is not a constructor, destructor, or type-conversion
20444 if (ctor_dtor_or_conv_p
)
20445 *ctor_dtor_or_conv_p
= 0;
20447 if (cp_parser_allow_gnu_extensions_p (parser
))
20448 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20450 /* Check for the ptr-operator production. */
20451 cp_parser_parse_tentatively (parser
);
20452 /* Parse the ptr-operator. */
20453 code
= cp_parser_ptr_operator (parser
,
20458 /* If that worked, then we have a ptr-operator. */
20459 if (cp_parser_parse_definitely (parser
))
20461 /* If a ptr-operator was found, then this declarator was not
20463 if (parenthesized_p
)
20464 *parenthesized_p
= true;
20465 /* The dependent declarator is optional if we are parsing an
20466 abstract-declarator. */
20467 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20468 cp_parser_parse_tentatively (parser
);
20470 /* Parse the dependent declarator. */
20471 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20472 CP_PARSER_FLAGS_NONE
,
20473 /*ctor_dtor_or_conv_p=*/NULL
,
20474 /*parenthesized_p=*/NULL
,
20475 /*member_p=*/false,
20478 /* If we are parsing an abstract-declarator, we must handle the
20479 case where the dependent declarator is absent. */
20480 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20481 && !cp_parser_parse_definitely (parser
))
20484 declarator
= cp_parser_make_indirect_declarator
20485 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20487 /* Everything else is a direct-declarator. */
20490 if (parenthesized_p
)
20491 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20493 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20494 flags
, ctor_dtor_or_conv_p
,
20495 member_p
, friend_p
);
20498 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20499 declarator
->attributes
= gnu_attributes
;
20503 /* Parse a direct-declarator or direct-abstract-declarator.
20507 direct-declarator ( parameter-declaration-clause )
20508 cv-qualifier-seq [opt]
20509 ref-qualifier [opt]
20510 exception-specification [opt]
20511 direct-declarator [ constant-expression [opt] ]
20514 direct-abstract-declarator:
20515 direct-abstract-declarator [opt]
20516 ( parameter-declaration-clause )
20517 cv-qualifier-seq [opt]
20518 ref-qualifier [opt]
20519 exception-specification [opt]
20520 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20521 ( abstract-declarator )
20523 Returns a representation of the declarator. DCL_KIND is
20524 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20525 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20526 we are parsing a direct-declarator. It is
20527 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20528 of ambiguity we prefer an abstract declarator, as per
20530 The parser flags FLAGS is used to control type-specifier parsing.
20531 CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20532 as for cp_parser_declarator. */
20534 static cp_declarator
*
20535 cp_parser_direct_declarator (cp_parser
* parser
,
20536 cp_parser_declarator_kind dcl_kind
,
20537 cp_parser_flags flags
,
20538 int* ctor_dtor_or_conv_p
,
20539 bool member_p
, bool friend_p
)
20542 cp_declarator
*declarator
= NULL
;
20543 tree scope
= NULL_TREE
;
20544 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20545 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20547 tree pushed_scope
= NULL_TREE
;
20548 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20552 /* Peek at the next token. */
20553 token
= cp_lexer_peek_token (parser
->lexer
);
20554 if (token
->type
== CPP_OPEN_PAREN
)
20556 /* This is either a parameter-declaration-clause, or a
20557 parenthesized declarator. When we know we are parsing a
20558 named declarator, it must be a parenthesized declarator
20559 if FIRST is true. For instance, `(int)' is a
20560 parameter-declaration-clause, with an omitted
20561 direct-abstract-declarator. But `((*))', is a
20562 parenthesized abstract declarator. Finally, when T is a
20563 template parameter `(T)' is a
20564 parameter-declaration-clause, and not a parenthesized
20567 We first try and parse a parameter-declaration-clause,
20568 and then try a nested declarator (if FIRST is true).
20570 It is not an error for it not to be a
20571 parameter-declaration-clause, even when FIRST is
20577 The first is the declaration of a function while the
20578 second is the definition of a variable, including its
20581 Having seen only the parenthesis, we cannot know which of
20582 these two alternatives should be selected. Even more
20583 complex are examples like:
20588 The former is a function-declaration; the latter is a
20589 variable initialization.
20591 Thus again, we try a parameter-declaration-clause, and if
20592 that fails, we back out and return. */
20594 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20597 bool is_declarator
= false;
20601 /* In a member-declarator, the only valid interpretation
20602 of a parenthesis is the start of a
20603 parameter-declaration-clause. (It is invalid to
20604 initialize a static data member with a parenthesized
20605 initializer; only the "=" form of initialization is
20608 cp_parser_parse_tentatively (parser
);
20610 /* Consume the `('. */
20611 matching_parens parens
;
20612 parens
.consume_open (parser
);
20615 /* If this is going to be an abstract declarator, we're
20616 in a declarator and we can't have default args. */
20617 parser
->default_arg_ok_p
= false;
20618 parser
->in_declarator_p
= true;
20621 begin_scope (sk_function_parms
, NULL_TREE
);
20623 /* Parse the parameter-declaration-clause. */
20625 = cp_parser_parameter_declaration_clause (parser
, flags
);
20627 /* Consume the `)'. */
20628 parens
.require_close (parser
);
20630 /* If all went well, parse the cv-qualifier-seq,
20631 ref-qualifier and the exception-specification. */
20632 if (member_p
|| cp_parser_parse_definitely (parser
))
20634 cp_cv_quals cv_quals
;
20635 cp_virt_specifiers virt_specifiers
;
20636 cp_ref_qualifier ref_qual
;
20637 tree exception_specification
;
20640 bool memfn
= (member_p
|| (pushed_scope
20641 && CLASS_TYPE_P (pushed_scope
)));
20643 is_declarator
= true;
20645 if (ctor_dtor_or_conv_p
)
20646 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20649 /* Parse the cv-qualifier-seq. */
20650 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20651 /* Parse the ref-qualifier. */
20652 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20653 /* Parse the tx-qualifier. */
20654 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20655 /* And the exception-specification. */
20656 exception_specification
20657 = cp_parser_exception_specification_opt (parser
);
20659 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20661 /* In here, we handle cases where attribute is used after
20662 the function declaration. For example:
20663 void func (int x) __attribute__((vector(..))); */
20664 tree gnu_attrs
= NULL_TREE
;
20665 tree requires_clause
= NULL_TREE
;
20666 late_return
= (cp_parser_late_return_type_opt
20667 (parser
, declarator
, requires_clause
,
20668 memfn
? cv_quals
: -1));
20670 /* Parse the virt-specifier-seq. */
20671 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20673 /* Create the function-declarator. */
20674 declarator
= make_call_declarator (declarator
,
20680 exception_specification
,
20683 declarator
->std_attributes
= attrs
;
20684 declarator
->attributes
= gnu_attrs
;
20685 /* Any subsequent parameter lists are to do with
20686 return type, so are not those of the declared
20688 parser
->default_arg_ok_p
= false;
20691 /* Remove the function parms from scope. */
20692 pop_bindings_and_leave_scope ();
20695 /* Repeat the main loop. */
20699 /* If this is the first, we can try a parenthesized
20703 bool saved_in_type_id_in_expr_p
;
20705 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20706 parser
->in_declarator_p
= saved_in_declarator_p
;
20708 open_paren
= token
;
20709 /* Consume the `('. */
20710 matching_parens parens
;
20711 parens
.consume_open (parser
);
20712 /* Parse the nested declarator. */
20713 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20714 parser
->in_type_id_in_expr_p
= true;
20716 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20717 ctor_dtor_or_conv_p
,
20718 /*parenthesized_p=*/NULL
,
20719 member_p
, friend_p
);
20720 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20722 /* Expect a `)'. */
20723 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20724 if (!parens
.require_close (parser
))
20725 declarator
= cp_error_declarator
;
20726 if (declarator
== cp_error_declarator
)
20729 goto handle_declarator
;
20731 /* Otherwise, we must be done. */
20735 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20736 && token
->type
== CPP_OPEN_SQUARE
20737 && !cp_next_tokens_can_be_attribute_p (parser
))
20739 /* Parse an array-declarator. */
20740 tree bounds
, attrs
;
20742 if (ctor_dtor_or_conv_p
)
20743 *ctor_dtor_or_conv_p
= 0;
20747 parser
->default_arg_ok_p
= false;
20748 parser
->in_declarator_p
= true;
20749 /* Consume the `['. */
20750 cp_lexer_consume_token (parser
->lexer
);
20751 /* Peek at the next token. */
20752 token
= cp_lexer_peek_token (parser
->lexer
);
20753 /* If the next token is `]', then there is no
20754 constant-expression. */
20755 if (token
->type
!= CPP_CLOSE_SQUARE
)
20757 bool non_constant_p
;
20759 = cp_parser_constant_expression (parser
,
20760 /*allow_non_constant=*/true,
20762 if (!non_constant_p
)
20764 else if (error_operand_p (bounds
))
20765 /* Already gave an error. */;
20766 else if (!parser
->in_function_body
20767 || current_binding_level
->kind
== sk_function_parms
)
20769 /* Normally, the array bound must be an integral constant
20770 expression. However, as an extension, we allow VLAs
20771 in function scopes as long as they aren't part of a
20772 parameter declaration. */
20773 cp_parser_error (parser
,
20774 "array bound is not an integer constant");
20775 bounds
= error_mark_node
;
20777 else if (processing_template_decl
20778 && !type_dependent_expression_p (bounds
))
20780 /* Remember this wasn't a constant-expression. */
20781 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20782 TREE_SIDE_EFFECTS (bounds
) = 1;
20786 bounds
= NULL_TREE
;
20787 /* Look for the closing `]'. */
20788 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20790 declarator
= cp_error_declarator
;
20794 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20795 declarator
= make_array_declarator (declarator
, bounds
);
20796 declarator
->std_attributes
= attrs
;
20798 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20801 tree qualifying_scope
;
20802 tree unqualified_name
;
20804 special_function_kind sfk
;
20806 bool pack_expansion_p
= false;
20807 cp_token
*declarator_id_start_token
;
20809 /* Parse a declarator-id */
20810 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20813 cp_parser_parse_tentatively (parser
);
20815 /* If we see an ellipsis, we should be looking at a
20817 if (token
->type
== CPP_ELLIPSIS
)
20819 /* Consume the `...' */
20820 cp_lexer_consume_token (parser
->lexer
);
20822 pack_expansion_p
= true;
20826 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20828 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20829 qualifying_scope
= parser
->scope
;
20834 if (!unqualified_name
&& pack_expansion_p
)
20836 /* Check whether an error occurred. */
20837 okay
= !cp_parser_error_occurred (parser
);
20839 /* We already consumed the ellipsis to mark a
20840 parameter pack, but we have no way to report it,
20841 so abort the tentative parse. We will be exiting
20842 immediately anyway. */
20843 cp_parser_abort_tentative_parse (parser
);
20846 okay
= cp_parser_parse_definitely (parser
);
20849 unqualified_name
= error_mark_node
;
20850 else if (unqualified_name
20851 && (qualifying_scope
20852 || (!identifier_p (unqualified_name
))))
20854 cp_parser_error (parser
, "expected unqualified-id");
20855 unqualified_name
= error_mark_node
;
20859 if (!unqualified_name
)
20861 if (unqualified_name
== error_mark_node
)
20863 declarator
= cp_error_declarator
;
20864 pack_expansion_p
= false;
20865 declarator
->parameter_pack_p
= false;
20869 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20871 if (qualifying_scope
&& at_namespace_scope_p ()
20872 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20874 /* In the declaration of a member of a template class
20875 outside of the class itself, the SCOPE will sometimes
20876 be a TYPENAME_TYPE. For example, given:
20878 template <typename T>
20879 int S<T>::R::i = 3;
20881 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20882 this context, we must resolve S<T>::R to an ordinary
20883 type, rather than a typename type.
20885 The reason we normally avoid resolving TYPENAME_TYPEs
20886 is that a specialization of `S' might render
20887 `S<T>::R' not a type. However, if `S' is
20888 specialized, then this `i' will not be used, so there
20889 is no harm in resolving the types here. */
20892 /* Resolve the TYPENAME_TYPE. */
20893 type
= resolve_typename_type (qualifying_scope
,
20894 /*only_current_p=*/false);
20895 /* If that failed, the declarator is invalid. */
20896 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20898 if (typedef_variant_p (type
))
20899 error_at (declarator_id_start_token
->location
,
20900 "cannot define member of dependent typedef "
20903 error_at (declarator_id_start_token
->location
,
20904 "%<%T::%E%> is not a type",
20905 TYPE_CONTEXT (qualifying_scope
),
20906 TYPE_IDENTIFIER (qualifying_scope
));
20908 qualifying_scope
= type
;
20913 if (unqualified_name
)
20917 if (qualifying_scope
20918 && CLASS_TYPE_P (qualifying_scope
))
20919 class_type
= qualifying_scope
;
20921 class_type
= current_class_type
;
20923 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20925 tree name_type
= TREE_TYPE (unqualified_name
);
20927 if (!class_type
|| !same_type_p (name_type
, class_type
))
20929 /* We do not attempt to print the declarator
20930 here because we do not have enough
20931 information about its original syntactic
20933 cp_parser_error (parser
, "invalid declarator");
20934 declarator
= cp_error_declarator
;
20937 else if (qualifying_scope
20938 && CLASSTYPE_USE_TEMPLATE (name_type
))
20940 error_at (declarator_id_start_token
->location
,
20941 "invalid use of constructor as a template");
20942 inform (declarator_id_start_token
->location
,
20943 "use %<%T::%D%> instead of %<%T::%D%> to "
20944 "name the constructor in a qualified name",
20946 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20947 class_type
, name_type
);
20948 declarator
= cp_error_declarator
;
20951 unqualified_name
= constructor_name (class_type
);
20956 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20957 sfk
= sfk_destructor
;
20958 else if (identifier_p (unqualified_name
)
20959 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20960 sfk
= sfk_conversion
;
20961 else if (/* There's no way to declare a constructor
20962 for an unnamed type, even if the type
20963 got a name for linkage purposes. */
20964 !TYPE_WAS_UNNAMED (class_type
)
20965 /* Handle correctly (c++/19200):
20979 friend void N::S();
20981 && (!friend_p
|| class_type
== qualifying_scope
)
20982 && constructor_name_p (unqualified_name
,
20984 sfk
= sfk_constructor
;
20985 else if (is_overloaded_fn (unqualified_name
)
20986 && DECL_CONSTRUCTOR_P (get_first_fn
20987 (unqualified_name
)))
20988 sfk
= sfk_constructor
;
20990 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20991 *ctor_dtor_or_conv_p
= -1;
20994 declarator
= make_id_declarator (qualifying_scope
,
20996 sfk
, token
->location
);
20997 declarator
->std_attributes
= attrs
;
20998 declarator
->parameter_pack_p
= pack_expansion_p
;
21000 if (pack_expansion_p
)
21001 maybe_warn_variadic_templates ();
21004 handle_declarator
:;
21005 scope
= get_scope_of_declarator (declarator
);
21008 /* Any names that appear after the declarator-id for a
21009 member are looked up in the containing scope. */
21010 if (at_function_scope_p ())
21012 /* But declarations with qualified-ids can't appear in a
21014 cp_parser_error (parser
, "qualified-id in declaration");
21015 declarator
= cp_error_declarator
;
21018 pushed_scope
= push_scope (scope
);
21020 parser
->in_declarator_p
= true;
21021 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21022 || (declarator
&& declarator
->kind
== cdk_id
))
21023 /* Default args are only allowed on function
21025 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21027 parser
->default_arg_ok_p
= false;
21036 /* For an abstract declarator, we might wind up with nothing at this
21037 point. That's an error; the declarator is not optional. */
21039 cp_parser_error (parser
, "expected declarator");
21040 else if (open_paren
)
21042 /* Record overly parenthesized declarator so we can give a
21043 diagnostic about confusing decl/expr disambiguation. */
21044 if (declarator
->kind
== cdk_array
)
21046 /* If the open and close parens are on different lines, this
21047 is probably a formatting thing, so ignore. */
21048 expanded_location open
= expand_location (open_paren
->location
);
21049 expanded_location close
= expand_location (close_paren
->location
);
21050 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21054 declarator
->parenthesized
= open_paren
->location
;
21057 /* If we entered a scope, we must exit it now. */
21059 pop_scope (pushed_scope
);
21061 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21062 parser
->in_declarator_p
= saved_in_declarator_p
;
21067 /* Parse a ptr-operator.
21070 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21071 * cv-qualifier-seq [opt]
21073 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21074 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21079 & cv-qualifier-seq [opt]
21081 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21082 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21083 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21084 filled in with the TYPE containing the member. *CV_QUALS is
21085 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21086 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21087 Note that the tree codes returned by this function have nothing
21088 to do with the types of trees that will be eventually be created
21089 to represent the pointer or reference type being parsed. They are
21090 just constants with suggestive names. */
21091 static enum tree_code
21092 cp_parser_ptr_operator (cp_parser
* parser
,
21094 cp_cv_quals
*cv_quals
,
21097 enum tree_code code
= ERROR_MARK
;
21099 tree attrs
= NULL_TREE
;
21101 /* Assume that it's not a pointer-to-member. */
21103 /* And that there are no cv-qualifiers. */
21104 *cv_quals
= TYPE_UNQUALIFIED
;
21106 /* Peek at the next token. */
21107 token
= cp_lexer_peek_token (parser
->lexer
);
21109 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21110 if (token
->type
== CPP_MULT
)
21111 code
= INDIRECT_REF
;
21112 else if (token
->type
== CPP_AND
)
21114 else if ((cxx_dialect
!= cxx98
) &&
21115 token
->type
== CPP_AND_AND
) /* C++0x only */
21116 code
= NON_LVALUE_EXPR
;
21118 if (code
!= ERROR_MARK
)
21120 /* Consume the `*', `&' or `&&'. */
21121 cp_lexer_consume_token (parser
->lexer
);
21123 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21124 `&', if we are allowing GNU extensions. (The only qualifier
21125 that can legally appear after `&' is `restrict', but that is
21126 enforced during semantic analysis. */
21127 if (code
== INDIRECT_REF
21128 || cp_parser_allow_gnu_extensions_p (parser
))
21129 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21131 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21132 if (attributes
!= NULL
)
21133 *attributes
= attrs
;
21137 /* Try the pointer-to-member case. */
21138 cp_parser_parse_tentatively (parser
);
21139 /* Look for the optional `::' operator. */
21140 cp_parser_global_scope_opt (parser
,
21141 /*current_scope_valid_p=*/false);
21142 /* Look for the nested-name specifier. */
21143 token
= cp_lexer_peek_token (parser
->lexer
);
21144 cp_parser_nested_name_specifier (parser
,
21145 /*typename_keyword_p=*/false,
21146 /*check_dependency_p=*/true,
21148 /*is_declaration=*/false);
21149 /* If we found it, and the next token is a `*', then we are
21150 indeed looking at a pointer-to-member operator. */
21151 if (!cp_parser_error_occurred (parser
)
21152 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21154 /* Indicate that the `*' operator was used. */
21155 code
= INDIRECT_REF
;
21157 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21158 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21159 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21160 error_at (token
->location
, "cannot form pointer to member of "
21161 "non-class %q#T", parser
->scope
);
21164 /* The type of which the member is a member is given by the
21166 *type
= parser
->scope
;
21167 /* The next name will not be qualified. */
21168 parser
->scope
= NULL_TREE
;
21169 parser
->qualifying_scope
= NULL_TREE
;
21170 parser
->object_scope
= NULL_TREE
;
21171 /* Look for optional c++11 attributes. */
21172 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21173 if (attributes
!= NULL
)
21174 *attributes
= attrs
;
21175 /* Look for the optional cv-qualifier-seq. */
21176 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21179 /* If that didn't work we don't have a ptr-operator. */
21180 if (!cp_parser_parse_definitely (parser
))
21181 cp_parser_error (parser
, "expected ptr-operator");
21187 /* Parse an (optional) cv-qualifier-seq.
21190 cv-qualifier cv-qualifier-seq [opt]
21201 Returns a bitmask representing the cv-qualifiers. */
21204 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21206 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21211 cp_cv_quals cv_qualifier
;
21213 /* Peek at the next token. */
21214 token
= cp_lexer_peek_token (parser
->lexer
);
21215 /* See if it's a cv-qualifier. */
21216 switch (token
->keyword
)
21219 cv_qualifier
= TYPE_QUAL_CONST
;
21223 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21227 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21231 cv_qualifier
= TYPE_UNQUALIFIED
;
21238 if (cv_quals
& cv_qualifier
)
21240 gcc_rich_location
richloc (token
->location
);
21241 richloc
.add_fixit_remove ();
21242 error_at (&richloc
, "duplicate cv-qualifier");
21243 cp_lexer_purge_token (parser
->lexer
);
21247 cp_lexer_consume_token (parser
->lexer
);
21248 cv_quals
|= cv_qualifier
;
21255 /* Parse an (optional) ref-qualifier
21261 Returns cp_ref_qualifier representing ref-qualifier. */
21263 static cp_ref_qualifier
21264 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21266 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21268 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21269 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21274 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21275 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21277 switch (token
->type
)
21280 curr_ref_qual
= REF_QUAL_LVALUE
;
21284 curr_ref_qual
= REF_QUAL_RVALUE
;
21288 curr_ref_qual
= REF_QUAL_NONE
;
21292 if (!curr_ref_qual
)
21296 error_at (token
->location
, "multiple ref-qualifiers");
21297 cp_lexer_purge_token (parser
->lexer
);
21301 ref_qual
= curr_ref_qual
;
21302 cp_lexer_consume_token (parser
->lexer
);
21309 /* Parse an optional tx-qualifier.
21313 transaction_safe_dynamic */
21316 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21318 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21319 if (token
->type
== CPP_NAME
)
21321 tree name
= token
->u
.value
;
21322 const char *p
= IDENTIFIER_POINTER (name
);
21323 const int len
= strlen ("transaction_safe");
21324 if (!strncmp (p
, "transaction_safe", len
))
21328 || !strcmp (p
, "_dynamic"))
21330 cp_lexer_consume_token (parser
->lexer
);
21333 error ("%qE requires %<-fgnu-tm%>", name
);
21344 /* Parse an (optional) virt-specifier-seq.
21346 virt-specifier-seq:
21347 virt-specifier virt-specifier-seq [opt]
21353 Returns a bitmask representing the virt-specifiers. */
21355 static cp_virt_specifiers
21356 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21358 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21363 cp_virt_specifiers virt_specifier
;
21365 /* Peek at the next token. */
21366 token
= cp_lexer_peek_token (parser
->lexer
);
21367 /* See if it's a virt-specifier-qualifier. */
21368 if (token
->type
!= CPP_NAME
)
21370 if (id_equal (token
->u
.value
, "override"))
21372 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21373 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21375 else if (id_equal (token
->u
.value
, "final"))
21377 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21378 virt_specifier
= VIRT_SPEC_FINAL
;
21380 else if (id_equal (token
->u
.value
, "__final"))
21382 virt_specifier
= VIRT_SPEC_FINAL
;
21387 if (virt_specifiers
& virt_specifier
)
21389 gcc_rich_location
richloc (token
->location
);
21390 richloc
.add_fixit_remove ();
21391 error_at (&richloc
, "duplicate virt-specifier");
21392 cp_lexer_purge_token (parser
->lexer
);
21396 cp_lexer_consume_token (parser
->lexer
);
21397 virt_specifiers
|= virt_specifier
;
21400 return virt_specifiers
;
21403 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21404 is in scope even though it isn't real. */
21407 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21411 if (current_class_ptr
)
21413 /* We don't clear this between NSDMIs. Is it already what we want? */
21414 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21415 if (DECL_P (current_class_ptr
)
21416 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21417 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21418 && cp_type_quals (type
) == quals
)
21422 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21423 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21424 current_class_ptr
= NULL_TREE
;
21426 = cp_build_fold_indirect_ref (this_parm
);
21427 current_class_ptr
= this_parm
;
21430 /* Return true iff our current scope is a non-static data member
21434 parsing_nsdmi (void)
21436 /* We recognize NSDMI context by the context-less 'this' pointer set up
21437 by the function above. */
21438 if (current_class_ptr
21439 && TREE_CODE (current_class_ptr
) == PARM_DECL
21440 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21445 /* Parse a late-specified return type, if any. This is not a separate
21446 non-terminal, but part of a function declarator, which looks like
21448 -> trailing-type-specifier-seq abstract-declarator(opt)
21450 Returns the type indicated by the type-id.
21452 In addition to this, parse any queued up #pragma omp declare simd
21453 clauses, and #pragma acc routine clauses.
21455 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21459 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21460 tree
& requires_clause
, cp_cv_quals quals
)
21463 tree type
= NULL_TREE
;
21464 bool declare_simd_p
= (parser
->omp_declare_simd
21466 && declarator
->kind
== cdk_id
);
21468 bool oacc_routine_p
= (parser
->oacc_routine
21470 && declarator
->kind
== cdk_id
);
21472 /* Peek at the next token. */
21473 token
= cp_lexer_peek_token (parser
->lexer
);
21474 /* A late-specified return type is indicated by an initial '->'. */
21475 if (token
->type
!= CPP_DEREF
21476 && token
->keyword
!= RID_REQUIRES
21477 && !(token
->type
== CPP_NAME
21478 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21479 && !(declare_simd_p
|| oacc_routine_p
))
21482 tree save_ccp
= current_class_ptr
;
21483 tree save_ccr
= current_class_ref
;
21486 /* DR 1207: 'this' is in scope in the trailing return type. */
21487 inject_this_parameter (current_class_type
, quals
);
21490 if (token
->type
== CPP_DEREF
)
21492 /* Consume the ->. */
21493 cp_lexer_consume_token (parser
->lexer
);
21495 type
= cp_parser_trailing_type_id (parser
);
21498 /* Function declarations may be followed by a trailing
21499 requires-clause. */
21500 requires_clause
= cp_parser_requires_clause_opt (parser
);
21502 if (declare_simd_p
)
21503 declarator
->attributes
21504 = cp_parser_late_parsing_omp_declare_simd (parser
,
21505 declarator
->attributes
);
21506 if (oacc_routine_p
)
21507 declarator
->attributes
21508 = cp_parser_late_parsing_oacc_routine (parser
,
21509 declarator
->attributes
);
21513 current_class_ptr
= save_ccp
;
21514 current_class_ref
= save_ccr
;
21520 /* Parse a declarator-id.
21524 :: [opt] nested-name-specifier [opt] type-name
21526 In the `id-expression' case, the value returned is as for
21527 cp_parser_id_expression if the id-expression was an unqualified-id.
21528 If the id-expression was a qualified-id, then a SCOPE_REF is
21529 returned. The first operand is the scope (either a NAMESPACE_DECL
21530 or TREE_TYPE), but the second is still just a representation of an
21534 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21537 /* The expression must be an id-expression. Assume that qualified
21538 names are the names of types so that:
21541 int S<T>::R::i = 3;
21543 will work; we must treat `S<T>::R' as the name of a type.
21544 Similarly, assume that qualified names are templates, where
21548 int S<T>::R<T>::i = 3;
21551 id
= cp_parser_id_expression (parser
,
21552 /*template_keyword_p=*/false,
21553 /*check_dependency_p=*/false,
21554 /*template_p=*/NULL
,
21555 /*declarator_p=*/true,
21557 if (id
&& BASELINK_P (id
))
21558 id
= BASELINK_FUNCTIONS (id
);
21562 /* Parse a type-id.
21565 type-specifier-seq abstract-declarator [opt]
21567 The parser flags FLAGS is used to control type-specifier parsing.
21569 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21571 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21572 i.e. we've just seen "->".
21574 Returns the TYPE specified. */
21577 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21578 bool is_template_arg
, bool is_trailing_return
,
21579 location_t
*type_location
)
21581 cp_decl_specifier_seq type_specifier_seq
;
21582 cp_declarator
*abstract_declarator
;
21584 /* Parse the type-specifier-seq. */
21585 cp_parser_type_specifier_seq (parser
, flags
,
21586 /*is_declaration=*/false,
21587 is_trailing_return
,
21588 &type_specifier_seq
);
21590 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21592 if (is_template_arg
&& type_specifier_seq
.type
21593 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21594 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21595 /* A bare template name as a template argument is a template template
21596 argument, not a placeholder, so fail parsing it as a type argument. */
21598 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21599 cp_parser_simulate_error (parser
);
21600 return error_mark_node
;
21602 if (type_specifier_seq
.type
== error_mark_node
)
21603 return error_mark_node
;
21605 /* There might or might not be an abstract declarator. */
21606 cp_parser_parse_tentatively (parser
);
21607 /* Look for the declarator. */
21608 abstract_declarator
21609 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21610 CP_PARSER_FLAGS_NONE
, NULL
,
21611 /*parenthesized_p=*/NULL
,
21612 /*member_p=*/false,
21613 /*friend_p=*/false);
21614 /* Check to see if there really was a declarator. */
21615 if (!cp_parser_parse_definitely (parser
))
21616 abstract_declarator
= NULL
;
21618 if (type_specifier_seq
.type
21619 /* The concepts TS allows 'auto' as a type-id. */
21620 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21621 /* None of the valid uses of 'auto' in C++14 involve the type-id
21622 nonterminal, but it is valid in a trailing-return-type. */
21623 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21624 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21626 /* A type-id with type 'auto' is only ok if the abstract declarator
21627 is a function declarator with a late-specified return type.
21629 A type-id with 'auto' is also valid in a trailing-return-type
21630 in a compound-requirement. */
21631 if (abstract_declarator
21632 && abstract_declarator
->kind
== cdk_function
21633 && abstract_declarator
->u
.function
.late_return_type
)
21635 else if (parser
->in_result_type_constraint_p
)
21639 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21640 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21642 error_at (loc
, "missing template arguments after %qT",
21644 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21648 error_at (loc
, "invalid use of %qT", auto_node
);
21649 return error_mark_node
;
21653 return groktypename (&type_specifier_seq
, abstract_declarator
,
21657 /* Wrapper for cp_parser_type_id_1. */
21660 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21661 location_t
*type_location
)
21663 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21666 /* Wrapper for cp_parser_type_id_1. */
21669 cp_parser_template_type_arg (cp_parser
*parser
)
21672 const char *saved_message
= parser
->type_definition_forbidden_message
;
21673 parser
->type_definition_forbidden_message
21674 = G_("types may not be defined in template arguments");
21675 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21676 parser
->type_definition_forbidden_message
= saved_message
;
21677 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21679 error ("invalid use of %<auto%> in template argument");
21680 r
= error_mark_node
;
21685 /* Wrapper for cp_parser_type_id_1. */
21688 cp_parser_trailing_type_id (cp_parser
*parser
)
21690 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21691 false, true, NULL
);
21694 /* Parse a type-specifier-seq.
21696 type-specifier-seq:
21697 type-specifier type-specifier-seq [opt]
21701 type-specifier-seq:
21702 attributes type-specifier-seq [opt]
21704 The parser flags FLAGS is used to control type-specifier parsing.
21706 If IS_DECLARATION is true, we are at the start of a "condition" or
21707 exception-declaration, so we might be followed by a declarator-id.
21709 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21710 i.e. we've just seen "->".
21712 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21715 cp_parser_type_specifier_seq (cp_parser
* parser
,
21716 cp_parser_flags flags
,
21717 bool is_declaration
,
21718 bool is_trailing_return
,
21719 cp_decl_specifier_seq
*type_specifier_seq
)
21721 bool seen_type_specifier
= false;
21722 cp_token
*start_token
= NULL
;
21724 /* Clear the TYPE_SPECIFIER_SEQ. */
21725 clear_decl_specs (type_specifier_seq
);
21727 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21728 /* In the context of a trailing return type, enum E { } is an
21729 elaborated-type-specifier followed by a function-body, not an
21731 if (is_trailing_return
)
21732 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21734 /* Parse the type-specifiers and attributes. */
21737 tree type_specifier
;
21738 bool is_cv_qualifier
;
21740 /* Check for attributes first. */
21741 if (cp_next_tokens_can_be_attribute_p (parser
))
21743 type_specifier_seq
->attributes
21744 = attr_chainon (type_specifier_seq
->attributes
,
21745 cp_parser_attributes_opt (parser
));
21749 /* record the token of the beginning of the type specifier seq,
21750 for error reporting purposes*/
21752 start_token
= cp_lexer_peek_token (parser
->lexer
);
21754 /* Look for the type-specifier. */
21755 type_specifier
= cp_parser_type_specifier (parser
,
21757 type_specifier_seq
,
21758 /*is_declaration=*/false,
21761 if (!type_specifier
)
21763 /* If the first type-specifier could not be found, this is not a
21764 type-specifier-seq at all. */
21765 if (!seen_type_specifier
)
21767 /* Set in_declarator_p to avoid skipping to the semicolon. */
21768 int in_decl
= parser
->in_declarator_p
;
21769 parser
->in_declarator_p
= true;
21771 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21772 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21773 cp_parser_error (parser
, "expected type-specifier");
21775 parser
->in_declarator_p
= in_decl
;
21777 type_specifier_seq
->type
= error_mark_node
;
21780 /* If subsequent type-specifiers could not be found, the
21781 type-specifier-seq is complete. */
21785 seen_type_specifier
= true;
21786 /* The standard says that a condition can be:
21788 type-specifier-seq declarator = assignment-expression
21795 we should treat the "S" as a declarator, not as a
21796 type-specifier. The standard doesn't say that explicitly for
21797 type-specifier-seq, but it does say that for
21798 decl-specifier-seq in an ordinary declaration. Perhaps it
21799 would be clearer just to allow a decl-specifier-seq here, and
21800 then add a semantic restriction that if any decl-specifiers
21801 that are not type-specifiers appear, the program is invalid. */
21802 if (is_declaration
&& !is_cv_qualifier
)
21803 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21807 /* Return whether the function currently being declared has an associated
21808 template parameter list. */
21811 function_being_declared_is_template_p (cp_parser
* parser
)
21813 if (!current_template_parms
|| processing_template_parmlist
)
21816 if (parser
->implicit_template_scope
)
21819 if (at_class_scope_p ()
21820 && TYPE_BEING_DEFINED (current_class_type
))
21821 return parser
->num_template_parameter_lists
!= 0;
21823 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21824 (current_class_type
));
21827 /* Parse a parameter-declaration-clause.
21829 parameter-declaration-clause:
21830 parameter-declaration-list [opt] ... [opt]
21831 parameter-declaration-list , ...
21833 The parser flags FLAGS is used to control type-specifier parsing.
21835 Returns a representation for the parameter declarations. A return
21836 value of NULL indicates a parameter-declaration-clause consisting
21837 only of an ellipsis. */
21840 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
21841 cp_parser_flags flags
)
21847 temp_override
<bool> cleanup
21848 (parser
->auto_is_implicit_function_template_parm_p
);
21850 if (!processing_specialization
21851 && !processing_template_parmlist
21852 && !processing_explicit_instantiation
21853 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21854 actual function or a random abstract declarator. */
21855 && parser
->default_arg_ok_p
)
21856 if (!current_function_decl
21857 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21858 parser
->auto_is_implicit_function_template_parm_p
= true;
21860 /* Peek at the next token. */
21861 token
= cp_lexer_peek_token (parser
->lexer
);
21862 /* Check for trivial parameter-declaration-clauses. */
21863 if (token
->type
== CPP_ELLIPSIS
)
21865 /* Consume the `...' token. */
21866 cp_lexer_consume_token (parser
->lexer
);
21869 else if (token
->type
== CPP_CLOSE_PAREN
)
21870 /* There are no parameters. */
21871 return void_list_node
;
21872 /* Check for `(void)', too, which is a special case. */
21873 else if (token
->keyword
== RID_VOID
21874 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21875 == CPP_CLOSE_PAREN
))
21877 /* Consume the `void' token. */
21878 cp_lexer_consume_token (parser
->lexer
);
21879 /* There are no parameters. */
21880 return void_list_node
;
21883 /* Parse the parameter-declaration-list. */
21884 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
21885 /* If a parse error occurred while parsing the
21886 parameter-declaration-list, then the entire
21887 parameter-declaration-clause is erroneous. */
21888 if (parameters
== error_mark_node
)
21891 /* Peek at the next token. */
21892 token
= cp_lexer_peek_token (parser
->lexer
);
21893 /* If it's a `,', the clause should terminate with an ellipsis. */
21894 if (token
->type
== CPP_COMMA
)
21896 /* Consume the `,'. */
21897 cp_lexer_consume_token (parser
->lexer
);
21898 /* Expect an ellipsis. */
21900 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21902 /* It might also be `...' if the optional trailing `,' was
21904 else if (token
->type
== CPP_ELLIPSIS
)
21906 /* Consume the `...' token. */
21907 cp_lexer_consume_token (parser
->lexer
);
21908 /* And remember that we saw it. */
21912 ellipsis_p
= false;
21914 /* Finish the parameter list. */
21916 parameters
= chainon (parameters
, void_list_node
);
21921 /* Parse a parameter-declaration-list.
21923 parameter-declaration-list:
21924 parameter-declaration
21925 parameter-declaration-list , parameter-declaration
21927 The parser flags FLAGS is used to control type-specifier parsing.
21929 Returns a representation of the parameter-declaration-list, as for
21930 cp_parser_parameter_declaration_clause. However, the
21931 `void_list_node' is never appended to the list. */
21934 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
21936 tree parameters
= NULL_TREE
;
21937 tree
*tail
= ¶meters
;
21938 bool saved_in_unbraced_linkage_specification_p
;
21941 /* The special considerations that apply to a function within an
21942 unbraced linkage specifications do not apply to the parameters
21943 to the function. */
21944 saved_in_unbraced_linkage_specification_p
21945 = parser
->in_unbraced_linkage_specification_p
;
21946 parser
->in_unbraced_linkage_specification_p
= false;
21948 /* Look for more parameters. */
21951 cp_parameter_declarator
*parameter
;
21952 tree decl
= error_mark_node
;
21953 bool parenthesized_p
= false;
21955 /* Parse the parameter. */
21957 = cp_parser_parameter_declaration (parser
, flags
,
21958 /*template_parm_p=*/false,
21961 /* We don't know yet if the enclosing context is deprecated, so wait
21962 and warn in grokparms if appropriate. */
21963 deprecated_state
= DEPRECATED_SUPPRESS
;
21967 decl
= grokdeclarator (parameter
->declarator
,
21968 ¶meter
->decl_specifiers
,
21970 parameter
->default_argument
!= NULL_TREE
,
21971 ¶meter
->decl_specifiers
.attributes
);
21972 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21973 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21976 deprecated_state
= DEPRECATED_NORMAL
;
21978 /* If a parse error occurred parsing the parameter declaration,
21979 then the entire parameter-declaration-list is erroneous. */
21980 if (decl
== error_mark_node
)
21982 parameters
= error_mark_node
;
21986 if (parameter
->decl_specifiers
.attributes
)
21987 cplus_decl_attributes (&decl
,
21988 parameter
->decl_specifiers
.attributes
,
21990 if (DECL_NAME (decl
))
21991 decl
= pushdecl (decl
);
21993 if (decl
!= error_mark_node
)
21995 retrofit_lang_decl (decl
);
21996 DECL_PARM_INDEX (decl
) = ++index
;
21997 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22000 /* Add the new parameter to the list. */
22001 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22002 tail
= &TREE_CHAIN (*tail
);
22004 /* Peek at the next token. */
22005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22006 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22007 /* These are for Objective-C++ */
22008 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22009 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22010 /* The parameter-declaration-list is complete. */
22012 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22016 /* Peek at the next token. */
22017 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22018 /* If it's an ellipsis, then the list is complete. */
22019 if (token
->type
== CPP_ELLIPSIS
)
22021 /* Otherwise, there must be more parameters. Consume the
22023 cp_lexer_consume_token (parser
->lexer
);
22024 /* When parsing something like:
22026 int i(float f, double d)
22028 we can tell after seeing the declaration for "f" that we
22029 are not looking at an initialization of a variable "i",
22030 but rather at the declaration of a function "i".
22032 Due to the fact that the parsing of template arguments
22033 (as specified to a template-id) requires backtracking we
22034 cannot use this technique when inside a template argument
22036 if (!parser
->in_template_argument_list_p
22037 && !parser
->in_type_id_in_expr_p
22038 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22039 /* However, a parameter-declaration of the form
22040 "float(f)" (which is a valid declaration of a
22041 parameter "f") can also be interpreted as an
22042 expression (the conversion of "f" to "float"). */
22043 && !parenthesized_p
)
22044 cp_parser_commit_to_tentative_parse (parser
);
22048 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22049 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22050 cp_parser_skip_to_closing_parenthesis (parser
,
22051 /*recovering=*/true,
22052 /*or_comma=*/false,
22053 /*consume_paren=*/false);
22058 parser
->in_unbraced_linkage_specification_p
22059 = saved_in_unbraced_linkage_specification_p
;
22061 /* Reset implicit_template_scope if we are about to leave the function
22062 parameter list that introduced it. Note that for out-of-line member
22063 definitions, there will be one or more class scopes before we get to
22064 the template parameter scope. */
22066 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22067 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22069 while (maybe_its
->kind
== sk_class
)
22070 maybe_its
= maybe_its
->level_chain
;
22071 if (maybe_its
== its
)
22073 parser
->implicit_template_parms
= 0;
22074 parser
->implicit_template_scope
= 0;
22081 /* Parse a parameter declaration.
22083 parameter-declaration:
22084 decl-specifier-seq ... [opt] declarator
22085 decl-specifier-seq declarator = assignment-expression
22086 decl-specifier-seq ... [opt] abstract-declarator [opt]
22087 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22089 The parser flags FLAGS is used to control type-specifier parsing.
22091 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22092 declares a template parameter. (In that case, a non-nested `>'
22093 token encountered during the parsing of the assignment-expression
22094 is not interpreted as a greater-than operator.)
22096 Returns a representation of the parameter, or NULL if an error
22097 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22098 true iff the declarator is of the form "(p)". */
22100 static cp_parameter_declarator
*
22101 cp_parser_parameter_declaration (cp_parser
*parser
,
22102 cp_parser_flags flags
,
22103 bool template_parm_p
,
22104 bool *parenthesized_p
)
22106 int declares_class_or_enum
;
22107 cp_decl_specifier_seq decl_specifiers
;
22108 cp_declarator
*declarator
;
22109 tree default_argument
;
22110 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22111 const char *saved_message
;
22112 bool template_parameter_pack_p
= false;
22114 /* In a template parameter, `>' is not an operator.
22118 When parsing a default template-argument for a non-type
22119 template-parameter, the first non-nested `>' is taken as the end
22120 of the template parameter-list rather than a greater-than
22123 /* Type definitions may not appear in parameter types. */
22124 saved_message
= parser
->type_definition_forbidden_message
;
22125 parser
->type_definition_forbidden_message
22126 = G_("types may not be defined in parameter types");
22128 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22129 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22130 (current_template_parms
)) : 0);
22132 /* Parse the declaration-specifiers. */
22133 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22134 cp_parser_decl_specifier_seq (parser
,
22137 &declares_class_or_enum
);
22139 /* Complain about missing 'typename' or other invalid type names. */
22140 if (!decl_specifiers
.any_type_specifiers_p
22141 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22142 decl_specifiers
.type
= error_mark_node
;
22144 /* If an error occurred, there's no reason to attempt to parse the
22145 rest of the declaration. */
22146 if (cp_parser_error_occurred (parser
))
22148 parser
->type_definition_forbidden_message
= saved_message
;
22152 /* Peek at the next token. */
22153 token
= cp_lexer_peek_token (parser
->lexer
);
22155 /* If the next token is a `)', `,', `=', `>', or `...', then there
22156 is no declarator. However, when variadic templates are enabled,
22157 there may be a declarator following `...'. */
22158 if (token
->type
== CPP_CLOSE_PAREN
22159 || token
->type
== CPP_COMMA
22160 || token
->type
== CPP_EQ
22161 || token
->type
== CPP_GREATER
)
22164 if (parenthesized_p
)
22165 *parenthesized_p
= false;
22167 /* Otherwise, there should be a declarator. */
22170 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22171 parser
->default_arg_ok_p
= false;
22173 /* After seeing a decl-specifier-seq, if the next token is not a
22174 "(", there is no possibility that the code is a valid
22175 expression. Therefore, if parsing tentatively, we commit at
22177 if (!parser
->in_template_argument_list_p
22178 /* In an expression context, having seen:
22182 we cannot be sure whether we are looking at a
22183 function-type (taking a "char" as a parameter) or a cast
22184 of some object of type "char" to "int". */
22185 && !parser
->in_type_id_in_expr_p
22186 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22187 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22188 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22189 cp_parser_commit_to_tentative_parse (parser
);
22190 /* Parse the declarator. */
22191 declarator_token_start
= token
;
22192 declarator
= cp_parser_declarator (parser
,
22193 CP_PARSER_DECLARATOR_EITHER
,
22194 CP_PARSER_FLAGS_NONE
,
22195 /*ctor_dtor_or_conv_p=*/NULL
,
22197 /*member_p=*/false,
22198 /*friend_p=*/false);
22199 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22200 /* After the declarator, allow more attributes. */
22201 decl_specifiers
.attributes
22202 = attr_chainon (decl_specifiers
.attributes
,
22203 cp_parser_attributes_opt (parser
));
22205 /* If the declarator is a template parameter pack, remember that and
22206 clear the flag in the declarator itself so we don't get errors
22207 from grokdeclarator. */
22208 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22210 declarator
->parameter_pack_p
= false;
22211 template_parameter_pack_p
= true;
22215 /* If the next token is an ellipsis, and we have not seen a declarator
22216 name, and if either the type of the declarator contains parameter
22217 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22218 for, eg, abbreviated integral type names), then we actually have a
22219 parameter pack expansion expression. Otherwise, leave the ellipsis
22220 for a C-style variadic function. */
22221 token
= cp_lexer_peek_token (parser
->lexer
);
22223 /* If a function parameter pack was specified and an implicit template
22224 parameter was introduced during cp_parser_parameter_declaration,
22225 change any implicit parameters introduced into packs. */
22226 if (parser
->implicit_template_parms
22227 && ((token
->type
== CPP_ELLIPSIS
22228 && declarator_can_be_parameter_pack (declarator
))
22229 || (declarator
&& declarator
->parameter_pack_p
)))
22231 int latest_template_parm_idx
= TREE_VEC_LENGTH
22232 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22234 if (latest_template_parm_idx
!= template_parm_idx
)
22235 decl_specifiers
.type
= convert_generic_types_to_packs
22236 (decl_specifiers
.type
,
22237 template_parm_idx
, latest_template_parm_idx
);
22240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22242 tree type
= decl_specifiers
.type
;
22244 if (type
&& DECL_P (type
))
22245 type
= TREE_TYPE (type
);
22248 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22249 && (template_parm_p
|| uses_parameter_packs (type
)))
22250 || (!type
&& template_parm_p
))
22251 && declarator_can_be_parameter_pack (declarator
))
22253 /* Consume the `...'. */
22254 cp_lexer_consume_token (parser
->lexer
);
22255 maybe_warn_variadic_templates ();
22257 /* Build a pack expansion type */
22258 if (template_parm_p
)
22259 template_parameter_pack_p
= true;
22260 else if (declarator
)
22261 declarator
->parameter_pack_p
= true;
22263 decl_specifiers
.type
= make_pack_expansion (type
);
22267 /* The restriction on defining new types applies only to the type
22268 of the parameter, not to the default argument. */
22269 parser
->type_definition_forbidden_message
= saved_message
;
22271 /* If the next token is `=', then process a default argument. */
22272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22274 tree type
= decl_specifiers
.type
;
22275 token
= cp_lexer_peek_token (parser
->lexer
);
22276 /* If we are defining a class, then the tokens that make up the
22277 default argument must be saved and processed later. */
22278 if (!template_parm_p
&& at_class_scope_p ()
22279 && TYPE_BEING_DEFINED (current_class_type
)
22280 && !LAMBDA_TYPE_P (current_class_type
))
22281 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22283 // A constrained-type-specifier may declare a type template-parameter.
22284 else if (declares_constrained_type_template_parameter (type
))
22286 = cp_parser_default_type_template_argument (parser
);
22288 // A constrained-type-specifier may declare a template-template-parameter.
22289 else if (declares_constrained_template_template_parameter (type
))
22291 = cp_parser_default_template_template_argument (parser
);
22293 /* Outside of a class definition, we can just parse the
22294 assignment-expression. */
22297 = cp_parser_default_argument (parser
, template_parm_p
);
22299 if (!parser
->default_arg_ok_p
)
22301 permerror (token
->location
,
22302 "default arguments are only "
22303 "permitted for function parameters");
22305 else if ((declarator
&& declarator
->parameter_pack_p
)
22306 || template_parameter_pack_p
22307 || (decl_specifiers
.type
22308 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22310 /* Find the name of the parameter pack. */
22311 cp_declarator
*id_declarator
= declarator
;
22312 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22313 id_declarator
= id_declarator
->declarator
;
22315 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22316 error_at (declarator_token_start
->location
,
22318 ? G_("template parameter pack %qD "
22319 "cannot have a default argument")
22320 : G_("parameter pack %qD cannot have "
22321 "a default argument"),
22322 id_declarator
->u
.id
.unqualified_name
);
22324 error_at (declarator_token_start
->location
,
22326 ? G_("template parameter pack cannot have "
22327 "a default argument")
22328 : G_("parameter pack cannot have a "
22329 "default argument"));
22331 default_argument
= NULL_TREE
;
22335 default_argument
= NULL_TREE
;
22337 if (default_argument
)
22338 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22340 /* Generate a location for the parameter, ranging from the start of the
22341 initial token to the end of the final token (using input_location for
22342 the latter, set up by cp_lexer_set_source_position_from_token when
22345 If we have a identifier, then use it for the caret location, e.g.
22347 extern int callee (int one, int (*two)(int, int), float three);
22348 ~~~~~~^~~~~~~~~~~~~~
22350 otherwise, reuse the start location for the caret location e.g.:
22352 extern int callee (int one, int (*)(int, int), float three);
22356 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22357 ? declarator
->id_loc
22358 : decl_spec_token_start
->location
);
22359 location_t param_loc
= make_location (caret_loc
,
22360 decl_spec_token_start
->location
,
22363 return make_parameter_declarator (&decl_specifiers
,
22367 template_parameter_pack_p
);
22370 /* Parse a default argument and return it.
22372 TEMPLATE_PARM_P is true if this is a default argument for a
22373 non-type template parameter. */
22375 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22377 tree default_argument
= NULL_TREE
;
22378 bool saved_greater_than_is_operator_p
;
22379 bool saved_local_variables_forbidden_p
;
22380 bool non_constant_p
, is_direct_init
;
22382 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22384 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22385 parser
->greater_than_is_operator_p
= !template_parm_p
;
22386 /* Local variable names (and the `this' keyword) may not
22387 appear in a default argument. */
22388 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22389 parser
->local_variables_forbidden_p
= true;
22390 /* Parse the assignment-expression. */
22391 if (template_parm_p
)
22392 push_deferring_access_checks (dk_no_deferred
);
22393 tree saved_class_ptr
= NULL_TREE
;
22394 tree saved_class_ref
= NULL_TREE
;
22395 /* The "this" pointer is not valid in a default argument. */
22398 saved_class_ptr
= current_class_ptr
;
22399 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22400 saved_class_ref
= current_class_ref
;
22401 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22404 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22405 /* Restore the "this" pointer. */
22408 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22409 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22411 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22412 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22413 if (template_parm_p
)
22414 pop_deferring_access_checks ();
22415 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22416 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22418 return default_argument
;
22421 /* Parse a function-body.
22424 compound_statement */
22427 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22429 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22430 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22434 /* Parse a ctor-initializer-opt followed by a function-body. Return
22435 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22436 is true we are parsing a function-try-block. */
22439 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22440 bool in_function_try_block
)
22443 const bool check_body_p
=
22444 DECL_CONSTRUCTOR_P (current_function_decl
)
22445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22448 /* Begin the function body. */
22449 body
= begin_function_body ();
22450 /* Parse the optional ctor-initializer. */
22451 cp_parser_ctor_initializer_opt (parser
);
22453 /* If we're parsing a constexpr constructor definition, we need
22454 to check that the constructor body is indeed empty. However,
22455 before we get to cp_parser_function_body lot of junk has been
22456 generated, so we can't just check that we have an empty block.
22457 Rather we take a snapshot of the outermost block, and check whether
22458 cp_parser_function_body changed its state. */
22461 list
= cur_stmt_list
;
22462 if (STATEMENT_LIST_TAIL (list
))
22463 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22465 /* Parse the function-body. */
22466 cp_parser_function_body (parser
, in_function_try_block
);
22468 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22469 /* Finish the function body. */
22470 finish_function_body (body
);
22473 /* Parse an initializer.
22476 = initializer-clause
22477 ( expression-list )
22479 Returns an expression representing the initializer. If no
22480 initializer is present, NULL_TREE is returned.
22482 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22483 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22484 set to TRUE if there is no initializer present. If there is an
22485 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22486 is set to true; otherwise it is set to false. */
22489 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22490 bool* non_constant_p
, bool subexpression_p
)
22495 /* Peek at the next token. */
22496 token
= cp_lexer_peek_token (parser
->lexer
);
22498 /* Let our caller know whether or not this initializer was
22500 *is_direct_init
= (token
->type
!= CPP_EQ
);
22501 /* Assume that the initializer is constant. */
22502 *non_constant_p
= false;
22504 if (token
->type
== CPP_EQ
)
22506 /* Consume the `='. */
22507 cp_lexer_consume_token (parser
->lexer
);
22508 /* Parse the initializer-clause. */
22509 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22511 else if (token
->type
== CPP_OPEN_PAREN
)
22513 vec
<tree
, va_gc
> *vec
;
22514 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22516 /*allow_expansion_p=*/true,
22519 return error_mark_node
;
22520 init
= build_tree_list_vec (vec
);
22521 release_tree_vector (vec
);
22523 else if (token
->type
== CPP_OPEN_BRACE
)
22525 cp_lexer_set_source_position (parser
->lexer
);
22526 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22527 init
= cp_parser_braced_list (parser
, non_constant_p
);
22528 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22532 /* Anything else is an error. */
22533 cp_parser_error (parser
, "expected initializer");
22534 init
= error_mark_node
;
22537 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22538 init
= error_mark_node
;
22543 /* Parse an initializer-clause.
22545 initializer-clause:
22546 assignment-expression
22549 Returns an expression representing the initializer.
22551 If the `assignment-expression' production is used the value
22552 returned is simply a representation for the expression.
22554 Otherwise, calls cp_parser_braced_list. */
22557 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22559 cp_expr initializer
;
22561 /* Assume the expression is constant. */
22562 *non_constant_p
= false;
22564 /* If it is not a `{', then we are looking at an
22565 assignment-expression. */
22566 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22569 = cp_parser_constant_expression (parser
,
22570 /*allow_non_constant_p=*/true,
22574 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22576 return initializer
;
22579 /* Parse a brace-enclosed initializer list.
22582 { initializer-list , [opt] }
22583 { designated-initializer-list , [opt] }
22586 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22587 the elements of the initializer-list (or NULL, if the last
22588 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22589 NULL_TREE. There is no way to detect whether or not the optional
22590 trailing `,' was provided. NON_CONSTANT_P is as for
22591 cp_parser_initializer. */
22594 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22597 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22599 /* Consume the `{' token. */
22600 matching_braces braces
;
22601 braces
.require_open (parser
);
22602 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22603 initializer
= make_node (CONSTRUCTOR
);
22604 /* If it's not a `}', then there is a non-trivial initializer. */
22605 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22607 /* Parse the initializer list. */
22608 CONSTRUCTOR_ELTS (initializer
)
22609 = cp_parser_initializer_list (parser
, non_constant_p
);
22610 /* A trailing `,' token is allowed. */
22611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22612 cp_lexer_consume_token (parser
->lexer
);
22615 *non_constant_p
= false;
22616 /* Now, there should be a trailing `}'. */
22617 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22618 braces
.require_close (parser
);
22619 TREE_TYPE (initializer
) = init_list_type_node
;
22621 cp_expr
result (initializer
);
22622 /* Build a location of the form:
22625 with caret==start at the open brace, finish at the close brace. */
22626 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22627 result
.set_location (combined_loc
);
22631 /* Consume tokens up to, and including, the next non-nested closing `]'.
22632 Returns true iff we found a closing `]'. */
22635 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22637 unsigned square_depth
= 0;
22641 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22643 switch (token
->type
)
22645 case CPP_PRAGMA_EOL
:
22646 if (!parser
->lexer
->in_pragma
)
22650 /* If we've run out of tokens, then there is no closing `]'. */
22653 case CPP_OPEN_SQUARE
:
22657 case CPP_CLOSE_SQUARE
:
22658 if (!square_depth
--)
22660 cp_lexer_consume_token (parser
->lexer
);
22669 /* Consume the token. */
22670 cp_lexer_consume_token (parser
->lexer
);
22674 /* Return true if we are looking at an array-designator, false otherwise. */
22677 cp_parser_array_designator_p (cp_parser
*parser
)
22679 /* Consume the `['. */
22680 cp_lexer_consume_token (parser
->lexer
);
22682 cp_lexer_save_tokens (parser
->lexer
);
22684 /* Skip tokens until the next token is a closing square bracket.
22685 If we find the closing `]', and the next token is a `=', then
22686 we are looking at an array designator. */
22687 bool array_designator_p
22688 = (cp_parser_skip_to_closing_square_bracket (parser
)
22689 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22691 /* Roll back the tokens we skipped. */
22692 cp_lexer_rollback_tokens (parser
->lexer
);
22694 return array_designator_p
;
22697 /* Parse an initializer-list.
22700 initializer-clause ... [opt]
22701 initializer-list , initializer-clause ... [opt]
22705 designated-initializer-list:
22706 designated-initializer-clause
22707 designated-initializer-list , designated-initializer-clause
22709 designated-initializer-clause:
22710 designator brace-or-equal-initializer
22718 designation initializer-clause ...[opt]
22719 initializer-list , designation initializer-clause ...[opt]
22724 [ constant-expression ] =
22726 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22727 for the initializer. If the INDEX of the elt is non-NULL, it is the
22728 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22729 as for cp_parser_initializer. */
22731 static vec
<constructor_elt
, va_gc
> *
22732 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22734 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22735 bool first_p
= true;
22736 tree first_designator
= NULL_TREE
;
22738 /* Assume all of the expressions are constant. */
22739 *non_constant_p
= false;
22741 /* Parse the rest of the list. */
22747 bool clause_non_constant_p
;
22748 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22750 /* Handle the C++2A syntax, '. id ='. */
22751 if ((cxx_dialect
>= cxx2a
22752 || cp_parser_allow_gnu_extensions_p (parser
))
22753 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22754 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22755 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22756 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22757 == CPP_OPEN_BRACE
)))
22759 if (cxx_dialect
< cxx2a
)
22760 pedwarn (loc
, OPT_Wpedantic
,
22761 "C++ designated initializers only available with "
22762 "-std=c++2a or -std=gnu++2a");
22763 /* Consume the `.'. */
22764 cp_lexer_consume_token (parser
->lexer
);
22765 /* Consume the identifier. */
22766 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22768 /* Consume the `='. */
22769 cp_lexer_consume_token (parser
->lexer
);
22771 /* Also, if the next token is an identifier and the following one is a
22772 colon, we are looking at the GNU designated-initializer
22774 else if (cp_parser_allow_gnu_extensions_p (parser
)
22775 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22776 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22779 /* Warn the user that they are using an extension. */
22780 pedwarn (loc
, OPT_Wpedantic
,
22781 "ISO C++ does not allow GNU designated initializers");
22782 /* Consume the identifier. */
22783 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22784 /* Consume the `:'. */
22785 cp_lexer_consume_token (parser
->lexer
);
22787 /* Also handle C99 array designators, '[ const ] ='. */
22788 else if (cp_parser_allow_gnu_extensions_p (parser
)
22789 && !c_dialect_objc ()
22790 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22792 /* In C++11, [ could start a lambda-introducer. */
22793 bool non_const
= false;
22795 cp_parser_parse_tentatively (parser
);
22797 if (!cp_parser_array_designator_p (parser
))
22799 cp_parser_simulate_error (parser
);
22800 designator
= NULL_TREE
;
22804 designator
= cp_parser_constant_expression (parser
, true,
22806 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22807 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22810 if (!cp_parser_parse_definitely (parser
))
22811 designator
= NULL_TREE
;
22813 && (!require_potential_rvalue_constant_expression
22815 designator
= NULL_TREE
;
22817 /* Warn the user that they are using an extension. */
22818 pedwarn (loc
, OPT_Wpedantic
,
22819 "ISO C++ does not allow C99 designated initializers");
22822 designator
= NULL_TREE
;
22826 first_designator
= designator
;
22829 else if (cxx_dialect
>= cxx2a
22830 && first_designator
!= error_mark_node
22831 && (!first_designator
!= !designator
))
22833 error_at (loc
, "either all initializer clauses should be designated "
22834 "or none of them should be");
22835 first_designator
= error_mark_node
;
22837 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22838 first_designator
= designator
;
22840 /* Parse the initializer. */
22841 initializer
= cp_parser_initializer_clause (parser
,
22842 &clause_non_constant_p
);
22843 /* If any clause is non-constant, so is the entire initializer. */
22844 if (clause_non_constant_p
)
22845 *non_constant_p
= true;
22847 /* If we have an ellipsis, this is an initializer pack
22849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22851 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22853 /* Consume the `...'. */
22854 cp_lexer_consume_token (parser
->lexer
);
22856 if (designator
&& cxx_dialect
>= cxx2a
)
22858 "%<...%> not allowed in designated initializer list");
22860 /* Turn the initializer into an initializer expansion. */
22861 initializer
= make_pack_expansion (initializer
);
22864 /* Add it to the vector. */
22865 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22867 /* If the next token is not a comma, we have reached the end of
22869 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22872 /* Peek at the next token. */
22873 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22874 /* If the next token is a `}', then we're still done. An
22875 initializer-clause can have a trailing `,' after the
22876 initializer-list and before the closing `}'. */
22877 if (token
->type
== CPP_CLOSE_BRACE
)
22880 /* Consume the `,' token. */
22881 cp_lexer_consume_token (parser
->lexer
);
22884 /* The same identifier shall not appear in multiple designators
22885 of a designated-initializer-list. */
22886 if (first_designator
)
22889 tree designator
, val
;
22890 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22891 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22893 if (IDENTIFIER_MARKED (designator
))
22895 error_at (cp_expr_loc_or_loc (val
, input_location
),
22896 "%<.%s%> designator used multiple times in "
22897 "the same initializer list",
22898 IDENTIFIER_POINTER (designator
));
22899 (*v
)[i
].index
= error_mark_node
;
22902 IDENTIFIER_MARKED (designator
) = 1;
22904 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22905 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22906 IDENTIFIER_MARKED (designator
) = 0;
22912 /* Classes [gram.class] */
22914 /* Parse a class-name.
22920 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22921 to indicate that names looked up in dependent types should be
22922 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22923 keyword has been used to indicate that the name that appears next
22924 is a template. TAG_TYPE indicates the explicit tag given before
22925 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22926 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22927 is the class being defined in a class-head. If ENUM_OK is TRUE,
22928 enum-names are also accepted.
22930 Returns the TYPE_DECL representing the class. */
22933 cp_parser_class_name (cp_parser
*parser
,
22934 bool typename_keyword_p
,
22935 bool template_keyword_p
,
22936 enum tag_types tag_type
,
22937 bool check_dependency_p
,
22939 bool is_declaration
,
22946 tree identifier
= NULL_TREE
;
22948 /* All class-names start with an identifier. */
22949 token
= cp_lexer_peek_token (parser
->lexer
);
22950 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22952 cp_parser_error (parser
, "expected class-name");
22953 return error_mark_node
;
22956 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22957 to a template-id, so we save it here. */
22958 scope
= parser
->scope
;
22959 if (scope
== error_mark_node
)
22960 return error_mark_node
;
22962 /* Any name names a type if we're following the `typename' keyword
22963 in a qualified name where the enclosing scope is type-dependent. */
22964 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22965 && dependent_type_p (scope
));
22966 /* Handle the common case (an identifier, but not a template-id)
22968 if (token
->type
== CPP_NAME
22969 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22971 cp_token
*identifier_token
;
22974 /* Look for the identifier. */
22975 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22976 ambiguous_p
= identifier_token
->error_reported
;
22977 identifier
= cp_parser_identifier (parser
);
22978 /* If the next token isn't an identifier, we are certainly not
22979 looking at a class-name. */
22980 if (identifier
== error_mark_node
)
22981 decl
= error_mark_node
;
22982 /* If we know this is a type-name, there's no need to look it
22984 else if (typename_p
)
22988 tree ambiguous_decls
;
22989 /* If we already know that this lookup is ambiguous, then
22990 we've already issued an error message; there's no reason
22994 cp_parser_simulate_error (parser
);
22995 return error_mark_node
;
22997 /* If the next token is a `::', then the name must be a type
23000 [basic.lookup.qual]
23002 During the lookup for a name preceding the :: scope
23003 resolution operator, object, function, and enumerator
23004 names are ignored. */
23005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23006 tag_type
= scope_type
;
23007 /* Look up the name. */
23008 decl
= cp_parser_lookup_name (parser
, identifier
,
23010 /*is_template=*/false,
23011 /*is_namespace=*/false,
23012 check_dependency_p
,
23014 identifier_token
->location
);
23015 if (ambiguous_decls
)
23017 if (cp_parser_parsing_tentatively (parser
))
23018 cp_parser_simulate_error (parser
);
23019 return error_mark_node
;
23025 /* Try a template-id. */
23026 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23027 check_dependency_p
,
23030 if (decl
== error_mark_node
)
23031 return error_mark_node
;
23034 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23036 /* If this is a typename, create a TYPENAME_TYPE. */
23037 if (typename_p
&& decl
!= error_mark_node
)
23039 decl
= make_typename_type (scope
, decl
, typename_type
,
23040 /*complain=*/tf_error
);
23041 if (decl
!= error_mark_node
)
23042 decl
= TYPE_NAME (decl
);
23045 decl
= strip_using_decl (decl
);
23047 /* Check to see that it is really the name of a class. */
23048 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23049 && identifier_p (TREE_OPERAND (decl
, 0))
23050 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23051 /* Situations like this:
23053 template <typename T> struct A {
23054 typename T::template X<int>::I i;
23057 are problematic. Is `T::template X<int>' a class-name? The
23058 standard does not seem to be definitive, but there is no other
23059 valid interpretation of the following `::'. Therefore, those
23060 names are considered class-names. */
23062 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23063 if (decl
!= error_mark_node
)
23064 decl
= TYPE_NAME (decl
);
23066 else if (TREE_CODE (decl
) != TYPE_DECL
23067 || TREE_TYPE (decl
) == error_mark_node
23068 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23069 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23070 /* In Objective-C 2.0, a classname followed by '.' starts a
23071 dot-syntax expression, and it's not a type-name. */
23072 || (c_dialect_objc ()
23073 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23074 && objc_is_class_name (decl
)))
23075 decl
= error_mark_node
;
23077 if (decl
== error_mark_node
)
23078 cp_parser_error (parser
, "expected class-name");
23079 else if (identifier
&& !parser
->scope
)
23080 maybe_note_name_used_in_class (identifier
, decl
);
23085 /* Parse a class-specifier.
23088 class-head { member-specification [opt] }
23090 Returns the TREE_TYPE representing the class. */
23093 cp_parser_class_specifier_1 (cp_parser
* parser
)
23096 tree attributes
= NULL_TREE
;
23097 bool nested_name_specifier_p
;
23098 unsigned saved_num_template_parameter_lists
;
23099 bool saved_in_function_body
;
23100 unsigned char in_statement
;
23101 bool in_switch_statement_p
;
23102 bool saved_in_unbraced_linkage_specification_p
;
23103 tree old_scope
= NULL_TREE
;
23104 tree scope
= NULL_TREE
;
23105 cp_token
*closing_brace
;
23107 push_deferring_access_checks (dk_no_deferred
);
23109 /* Parse the class-head. */
23110 type
= cp_parser_class_head (parser
,
23111 &nested_name_specifier_p
);
23112 /* If the class-head was a semantic disaster, skip the entire body
23116 cp_parser_skip_to_end_of_block_or_statement (parser
);
23117 pop_deferring_access_checks ();
23118 return error_mark_node
;
23121 /* Look for the `{'. */
23122 matching_braces braces
;
23123 if (!braces
.require_open (parser
))
23125 pop_deferring_access_checks ();
23126 return error_mark_node
;
23129 cp_ensure_no_omp_declare_simd (parser
);
23130 cp_ensure_no_oacc_routine (parser
);
23132 /* Issue an error message if type-definitions are forbidden here. */
23133 cp_parser_check_type_definition (parser
);
23134 /* Remember that we are defining one more class. */
23135 ++parser
->num_classes_being_defined
;
23136 /* Inside the class, surrounding template-parameter-lists do not
23138 saved_num_template_parameter_lists
23139 = parser
->num_template_parameter_lists
;
23140 parser
->num_template_parameter_lists
= 0;
23141 /* We are not in a function body. */
23142 saved_in_function_body
= parser
->in_function_body
;
23143 parser
->in_function_body
= false;
23144 /* Or in a loop. */
23145 in_statement
= parser
->in_statement
;
23146 parser
->in_statement
= 0;
23147 /* Or in a switch. */
23148 in_switch_statement_p
= parser
->in_switch_statement_p
;
23149 parser
->in_switch_statement_p
= false;
23150 /* We are not immediately inside an extern "lang" block. */
23151 saved_in_unbraced_linkage_specification_p
23152 = parser
->in_unbraced_linkage_specification_p
;
23153 parser
->in_unbraced_linkage_specification_p
= false;
23155 // Associate constraints with the type.
23157 type
= associate_classtype_constraints (type
);
23159 /* Start the class. */
23160 if (nested_name_specifier_p
)
23162 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23163 old_scope
= push_inner_scope (scope
);
23165 type
= begin_class_definition (type
);
23167 if (type
== error_mark_node
)
23168 /* If the type is erroneous, skip the entire body of the class. */
23169 cp_parser_skip_to_closing_brace (parser
);
23171 /* Parse the member-specification. */
23172 cp_parser_member_specification_opt (parser
);
23174 /* Look for the trailing `}'. */
23175 closing_brace
= braces
.require_close (parser
);
23176 /* Look for trailing attributes to apply to this class. */
23177 if (cp_parser_allow_gnu_extensions_p (parser
))
23178 attributes
= cp_parser_gnu_attributes_opt (parser
);
23179 if (type
!= error_mark_node
)
23180 type
= finish_struct (type
, attributes
);
23181 if (nested_name_specifier_p
)
23182 pop_inner_scope (old_scope
, scope
);
23184 /* We've finished a type definition. Check for the common syntax
23185 error of forgetting a semicolon after the definition. We need to
23186 be careful, as we can't just check for not-a-semicolon and be done
23187 with it; the user might have typed:
23189 class X { } c = ...;
23190 class X { } *p = ...;
23192 and so forth. Instead, enumerate all the possible tokens that
23193 might follow this production; if we don't see one of them, then
23194 complain and silently insert the semicolon. */
23196 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23197 bool want_semicolon
= true;
23199 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23200 /* Don't try to parse c++11 attributes here. As per the
23201 grammar, that should be a task for
23202 cp_parser_decl_specifier_seq. */
23203 want_semicolon
= false;
23205 switch (token
->type
)
23208 case CPP_SEMICOLON
:
23211 case CPP_OPEN_PAREN
:
23212 case CPP_CLOSE_PAREN
:
23214 want_semicolon
= false;
23217 /* While it's legal for type qualifiers and storage class
23218 specifiers to follow type definitions in the grammar, only
23219 compiler testsuites contain code like that. Assume that if
23220 we see such code, then what we're really seeing is a case
23224 const <type> var = ...;
23229 static <type> func (...) ...
23231 i.e. the qualifier or specifier applies to the next
23232 declaration. To do so, however, we need to look ahead one
23233 more token to see if *that* token is a type specifier.
23235 This code could be improved to handle:
23238 static const <type> var = ...; */
23240 if (keyword_is_decl_specifier (token
->keyword
))
23242 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23244 /* Handling user-defined types here would be nice, but very
23247 = (lookahead
->type
== CPP_KEYWORD
23248 && keyword_begins_type_specifier (lookahead
->keyword
));
23255 /* If we don't have a type, then something is very wrong and we
23256 shouldn't try to do anything clever. Likewise for not seeing the
23258 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23260 /* Locate the closing brace. */
23261 cp_token_position prev
23262 = cp_lexer_previous_token_position (parser
->lexer
);
23263 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23264 location_t loc
= prev_token
->location
;
23266 /* We want to suggest insertion of a ';' immediately *after* the
23267 closing brace, so, if we can, offset the location by 1 column. */
23268 location_t next_loc
= loc
;
23269 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23270 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23272 rich_location
richloc (line_table
, next_loc
);
23274 /* If we successfully offset the location, suggest the fix-it. */
23275 if (next_loc
!= loc
)
23276 richloc
.add_fixit_insert_before (next_loc
, ";");
23278 if (CLASSTYPE_DECLARED_CLASS (type
))
23279 error_at (&richloc
,
23280 "expected %<;%> after class definition");
23281 else if (TREE_CODE (type
) == RECORD_TYPE
)
23282 error_at (&richloc
,
23283 "expected %<;%> after struct definition");
23284 else if (TREE_CODE (type
) == UNION_TYPE
)
23285 error_at (&richloc
,
23286 "expected %<;%> after union definition");
23288 gcc_unreachable ();
23290 /* Unget one token and smash it to look as though we encountered
23291 a semicolon in the input stream. */
23292 cp_lexer_set_token_position (parser
->lexer
, prev
);
23293 token
= cp_lexer_peek_token (parser
->lexer
);
23294 token
->type
= CPP_SEMICOLON
;
23295 token
->keyword
= RID_MAX
;
23299 /* If this class is not itself within the scope of another class,
23300 then we need to parse the bodies of all of the queued function
23301 definitions. Note that the queued functions defined in a class
23302 are not always processed immediately following the
23303 class-specifier for that class. Consider:
23306 struct B { void f() { sizeof (A); } };
23309 If `f' were processed before the processing of `A' were
23310 completed, there would be no way to compute the size of `A'.
23311 Note that the nesting we are interested in here is lexical --
23312 not the semantic nesting given by TYPE_CONTEXT. In particular,
23315 struct A { struct B; };
23316 struct A::B { void f() { } };
23318 there is no need to delay the parsing of `A::B::f'. */
23319 if (--parser
->num_classes_being_defined
== 0)
23322 tree class_type
= NULL_TREE
;
23323 tree pushed_scope
= NULL_TREE
;
23325 cp_default_arg_entry
*e
;
23326 tree save_ccp
, save_ccr
;
23328 if (any_erroneous_template_args_p (type
))
23330 /* Skip default arguments, NSDMIs, etc, in order to improve
23331 error recovery (c++/71169, c++/71832). */
23332 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23333 vec_safe_truncate (unparsed_nsdmis
, 0);
23334 vec_safe_truncate (unparsed_classes
, 0);
23335 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23338 /* In a first pass, parse default arguments to the functions.
23339 Then, in a second pass, parse the bodies of the functions.
23340 This two-phased approach handles cases like:
23348 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23351 /* If there are default arguments that have not yet been processed,
23352 take care of them now. */
23353 if (class_type
!= e
->class_type
)
23356 pop_scope (pushed_scope
);
23357 class_type
= e
->class_type
;
23358 pushed_scope
= push_scope (class_type
);
23360 /* Make sure that any template parameters are in scope. */
23361 maybe_begin_member_template_processing (decl
);
23362 /* Parse the default argument expressions. */
23363 cp_parser_late_parsing_default_args (parser
, decl
);
23364 /* Remove any template parameters from the symbol table. */
23365 maybe_end_member_template_processing ();
23367 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23368 /* Now parse any NSDMIs. */
23369 save_ccp
= current_class_ptr
;
23370 save_ccr
= current_class_ref
;
23371 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23373 if (class_type
!= DECL_CONTEXT (decl
))
23376 pop_scope (pushed_scope
);
23377 class_type
= DECL_CONTEXT (decl
);
23378 pushed_scope
= push_scope (class_type
);
23380 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23381 cp_parser_late_parsing_nsdmi (parser
, decl
);
23383 vec_safe_truncate (unparsed_nsdmis
, 0);
23384 current_class_ptr
= save_ccp
;
23385 current_class_ref
= save_ccr
;
23387 pop_scope (pushed_scope
);
23389 /* Now do some post-NSDMI bookkeeping. */
23390 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23391 after_nsdmi_defaulted_late_checks (class_type
);
23392 vec_safe_truncate (unparsed_classes
, 0);
23393 after_nsdmi_defaulted_late_checks (type
);
23395 /* Now parse the body of the functions. */
23398 /* OpenMP UDRs need to be parsed before all other functions. */
23399 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23400 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23401 cp_parser_late_parsing_for_member (parser
, decl
);
23402 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23403 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23404 cp_parser_late_parsing_for_member (parser
, decl
);
23407 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23408 cp_parser_late_parsing_for_member (parser
, decl
);
23409 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23412 vec_safe_push (unparsed_classes
, type
);
23414 /* Put back any saved access checks. */
23415 pop_deferring_access_checks ();
23417 /* Restore saved state. */
23418 parser
->in_switch_statement_p
= in_switch_statement_p
;
23419 parser
->in_statement
= in_statement
;
23420 parser
->in_function_body
= saved_in_function_body
;
23421 parser
->num_template_parameter_lists
23422 = saved_num_template_parameter_lists
;
23423 parser
->in_unbraced_linkage_specification_p
23424 = saved_in_unbraced_linkage_specification_p
;
23430 cp_parser_class_specifier (cp_parser
* parser
)
23433 timevar_push (TV_PARSE_STRUCT
);
23434 ret
= cp_parser_class_specifier_1 (parser
);
23435 timevar_pop (TV_PARSE_STRUCT
);
23439 /* Parse a class-head.
23442 class-key identifier [opt] base-clause [opt]
23443 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23444 class-key nested-name-specifier [opt] template-id
23447 class-virt-specifier:
23451 class-key attributes identifier [opt] base-clause [opt]
23452 class-key attributes nested-name-specifier identifier base-clause [opt]
23453 class-key attributes nested-name-specifier [opt] template-id
23456 Upon return BASES is initialized to the list of base classes (or
23457 NULL, if there are none) in the same form returned by
23458 cp_parser_base_clause.
23460 Returns the TYPE of the indicated class. Sets
23461 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23462 involving a nested-name-specifier was used, and FALSE otherwise.
23464 Returns error_mark_node if this is not a class-head.
23466 Returns NULL_TREE if the class-head is syntactically valid, but
23467 semantically invalid in a way that means we should skip the entire
23468 body of the class. */
23471 cp_parser_class_head (cp_parser
* parser
,
23472 bool* nested_name_specifier_p
)
23474 tree nested_name_specifier
;
23475 enum tag_types class_key
;
23476 tree id
= NULL_TREE
;
23477 tree type
= NULL_TREE
;
23480 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23481 bool template_id_p
= false;
23482 bool qualified_p
= false;
23483 bool invalid_nested_name_p
= false;
23484 bool invalid_explicit_specialization_p
= false;
23485 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23486 tree pushed_scope
= NULL_TREE
;
23487 unsigned num_templates
;
23488 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23489 /* Assume no nested-name-specifier will be present. */
23490 *nested_name_specifier_p
= false;
23491 /* Assume no template parameter lists will be used in defining the
23494 parser
->colon_corrects_to_scope_p
= false;
23496 /* Look for the class-key. */
23497 class_key
= cp_parser_class_key (parser
);
23498 if (class_key
== none_type
)
23499 return error_mark_node
;
23501 location_t class_head_start_location
= input_location
;
23503 /* Parse the attributes. */
23504 attributes
= cp_parser_attributes_opt (parser
);
23506 /* If the next token is `::', that is invalid -- but sometimes
23507 people do try to write:
23511 Handle this gracefully by accepting the extra qualifier, and then
23512 issuing an error about it later if this really is a
23513 class-head. If it turns out just to be an elaborated type
23514 specifier, remain silent. */
23515 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23516 qualified_p
= true;
23518 push_deferring_access_checks (dk_no_check
);
23520 /* Determine the name of the class. Begin by looking for an
23521 optional nested-name-specifier. */
23522 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23523 nested_name_specifier
23524 = cp_parser_nested_name_specifier_opt (parser
,
23525 /*typename_keyword_p=*/false,
23526 /*check_dependency_p=*/false,
23528 /*is_declaration=*/false);
23529 /* If there was a nested-name-specifier, then there *must* be an
23532 cp_token
*bad_template_keyword
= NULL
;
23534 if (nested_name_specifier
)
23536 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23537 /* Although the grammar says `identifier', it really means
23538 `class-name' or `template-name'. You are only allowed to
23539 define a class that has already been declared with this
23542 The proposed resolution for Core Issue 180 says that wherever
23543 you see `class T::X' you should treat `X' as a type-name.
23545 It is OK to define an inaccessible class; for example:
23547 class A { class B; };
23550 We do not know if we will see a class-name, or a
23551 template-name. We look for a class-name first, in case the
23552 class-name is a template-id; if we looked for the
23553 template-name first we would stop after the template-name. */
23554 cp_parser_parse_tentatively (parser
);
23555 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23556 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23557 type
= cp_parser_class_name (parser
,
23558 /*typename_keyword_p=*/false,
23559 /*template_keyword_p=*/false,
23561 /*check_dependency_p=*/false,
23562 /*class_head_p=*/true,
23563 /*is_declaration=*/false);
23564 /* If that didn't work, ignore the nested-name-specifier. */
23565 if (!cp_parser_parse_definitely (parser
))
23567 invalid_nested_name_p
= true;
23568 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23569 id
= cp_parser_identifier (parser
);
23570 if (id
== error_mark_node
)
23573 /* If we could not find a corresponding TYPE, treat this
23574 declaration like an unqualified declaration. */
23575 if (type
== error_mark_node
)
23576 nested_name_specifier
= NULL_TREE
;
23577 /* Otherwise, count the number of templates used in TYPE and its
23578 containing scopes. */
23580 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23582 /* Otherwise, the identifier is optional. */
23585 /* We don't know whether what comes next is a template-id,
23586 an identifier, or nothing at all. */
23587 cp_parser_parse_tentatively (parser
);
23588 /* Check for a template-id. */
23589 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23590 id
= cp_parser_template_id (parser
,
23591 /*template_keyword_p=*/false,
23592 /*check_dependency_p=*/true,
23594 /*is_declaration=*/true);
23595 /* If that didn't work, it could still be an identifier. */
23596 if (!cp_parser_parse_definitely (parser
))
23598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23600 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23601 id
= cp_parser_identifier (parser
);
23608 template_id_p
= true;
23613 pop_deferring_access_checks ();
23617 cp_parser_check_for_invalid_template_id (parser
, id
,
23619 type_start_token
->location
);
23621 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23623 /* If it's not a `:' or a `{' then we can't really be looking at a
23624 class-head, since a class-head only appears as part of a
23625 class-specifier. We have to detect this situation before calling
23626 xref_tag, since that has irreversible side-effects. */
23627 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23629 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23630 type
= error_mark_node
;
23634 /* At this point, we're going ahead with the class-specifier, even
23635 if some other problem occurs. */
23636 cp_parser_commit_to_tentative_parse (parser
);
23637 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23639 cp_parser_error (parser
,
23640 "cannot specify %<override%> for a class");
23641 type
= error_mark_node
;
23644 /* Issue the error about the overly-qualified name now. */
23647 cp_parser_error (parser
,
23648 "global qualification of class name is invalid");
23649 type
= error_mark_node
;
23652 else if (invalid_nested_name_p
)
23654 cp_parser_error (parser
,
23655 "qualified name does not name a class");
23656 type
= error_mark_node
;
23659 else if (nested_name_specifier
)
23663 if (bad_template_keyword
)
23664 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23665 keyword template shall not appear at the top level. */
23666 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23667 "keyword %<template%> not allowed in class-head-name");
23669 /* Reject typedef-names in class heads. */
23670 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23672 error_at (type_start_token
->location
,
23673 "invalid class name in declaration of %qD",
23679 /* Figure out in what scope the declaration is being placed. */
23680 scope
= current_scope ();
23681 /* If that scope does not contain the scope in which the
23682 class was originally declared, the program is invalid. */
23683 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23685 if (at_namespace_scope_p ())
23686 error_at (type_start_token
->location
,
23687 "declaration of %qD in namespace %qD which does not "
23689 type
, scope
, nested_name_specifier
);
23691 error_at (type_start_token
->location
,
23692 "declaration of %qD in %qD which does not enclose %qD",
23693 type
, scope
, nested_name_specifier
);
23699 A declarator-id shall not be qualified except for the
23700 definition of a ... nested class outside of its class
23701 ... [or] the definition or explicit instantiation of a
23702 class member of a namespace outside of its namespace. */
23703 if (scope
== nested_name_specifier
)
23705 permerror (nested_name_specifier_token_start
->location
,
23706 "extra qualification not allowed");
23707 nested_name_specifier
= NULL_TREE
;
23711 /* An explicit-specialization must be preceded by "template <>". If
23712 it is not, try to recover gracefully. */
23713 if (at_namespace_scope_p ()
23714 && parser
->num_template_parameter_lists
== 0
23715 && !processing_template_parmlist
23718 /* Build a location of this form:
23719 struct typename <ARGS>
23720 ^~~~~~~~~~~~~~~~~~~~~~
23721 with caret==start at the start token, and
23722 finishing at the end of the type. */
23723 location_t reported_loc
23724 = make_location (class_head_start_location
,
23725 class_head_start_location
,
23726 get_finish (type_start_token
->location
));
23727 rich_location
richloc (line_table
, reported_loc
);
23728 richloc
.add_fixit_insert_before (class_head_start_location
,
23730 error_at (&richloc
,
23731 "an explicit specialization must be preceded by"
23732 " %<template <>%>");
23733 invalid_explicit_specialization_p
= true;
23734 /* Take the same action that would have been taken by
23735 cp_parser_explicit_specialization. */
23736 ++parser
->num_template_parameter_lists
;
23737 begin_specialization ();
23739 /* There must be no "return" statements between this point and the
23740 end of this function; set "type "to the correct return value and
23741 use "goto done;" to return. */
23742 /* Make sure that the right number of template parameters were
23744 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23746 type_start_token
->location
,
23747 /*declarator=*/NULL
))
23749 /* If something went wrong, there is no point in even trying to
23750 process the class-definition. */
23755 /* Look up the type. */
23758 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23759 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23760 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23762 error_at (type_start_token
->location
,
23763 "function template %qD redeclared as a class template", id
);
23764 type
= error_mark_node
;
23768 type
= TREE_TYPE (id
);
23769 type
= maybe_process_partial_specialization (type
);
23771 /* Check the scope while we still know whether or not we had a
23772 nested-name-specifier. */
23773 if (type
!= error_mark_node
)
23774 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23776 if (nested_name_specifier
)
23777 pushed_scope
= push_scope (nested_name_specifier
);
23779 else if (nested_name_specifier
)
23785 template <typename T> struct S { struct T };
23786 template <typename T> struct S<T>::T { };
23788 we will get a TYPENAME_TYPE when processing the definition of
23789 `S::T'. We need to resolve it to the actual type before we
23790 try to define it. */
23791 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23793 class_type
= resolve_typename_type (TREE_TYPE (type
),
23794 /*only_current_p=*/false);
23795 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23796 type
= TYPE_NAME (class_type
);
23799 cp_parser_error (parser
, "could not resolve typename type");
23800 type
= error_mark_node
;
23804 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23805 == error_mark_node
)
23811 class_type
= current_class_type
;
23812 /* Enter the scope indicated by the nested-name-specifier. */
23813 pushed_scope
= push_scope (nested_name_specifier
);
23814 /* Get the canonical version of this type. */
23815 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23816 /* Call push_template_decl if it seems like we should be defining a
23817 template either from the template headers or the type we're
23818 defining, so that we diagnose both extra and missing headers. */
23819 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23820 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23821 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23823 type
= push_template_decl (type
);
23824 if (type
== error_mark_node
)
23831 type
= TREE_TYPE (type
);
23832 *nested_name_specifier_p
= true;
23834 else /* The name is not a nested name. */
23836 /* If the class was unnamed, create a dummy name. */
23838 id
= make_anon_name ();
23839 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23840 ? ts_within_enclosing_non_class
23842 type
= xref_tag (class_key
, id
, tag_scope
,
23843 parser
->num_template_parameter_lists
);
23846 /* Indicate whether this class was declared as a `class' or as a
23848 if (TREE_CODE (type
) == RECORD_TYPE
)
23849 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23850 cp_parser_check_class_key (class_key
, type
);
23852 /* If this type was already complete, and we see another definition,
23853 that's an error. */
23854 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23856 error_at (type_start_token
->location
, "redefinition of %q#T",
23858 inform (location_of (type
), "previous definition of %q#T",
23863 else if (type
== error_mark_node
)
23868 /* Apply attributes now, before any use of the class as a template
23869 argument in its base list. */
23870 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23871 fixup_attribute_variants (type
);
23874 /* We will have entered the scope containing the class; the names of
23875 base classes should be looked up in that context. For example:
23877 struct A { struct B {}; struct C; };
23878 struct A::C : B {};
23882 /* Get the list of base-classes, if there is one. */
23883 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23885 /* PR59482: enter the class scope so that base-specifiers are looked
23889 bases
= cp_parser_base_clause (parser
);
23890 /* PR59482: get out of the previously pushed class scope so that the
23891 subsequent pops pop the right thing. */
23898 /* If we're really defining a class, process the base classes.
23899 If they're invalid, fail. */
23900 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23901 xref_basetypes (type
, bases
);
23904 /* Leave the scope given by the nested-name-specifier. We will
23905 enter the class scope itself while processing the members. */
23907 pop_scope (pushed_scope
);
23909 if (invalid_explicit_specialization_p
)
23911 end_specialization ();
23912 --parser
->num_template_parameter_lists
;
23916 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23917 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23918 CLASSTYPE_FINAL (type
) = 1;
23920 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23924 /* Parse a class-key.
23931 Returns the kind of class-key specified, or none_type to indicate
23934 static enum tag_types
23935 cp_parser_class_key (cp_parser
* parser
)
23938 enum tag_types tag_type
;
23940 /* Look for the class-key. */
23941 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23945 /* Check to see if the TOKEN is a class-key. */
23946 tag_type
= cp_parser_token_is_class_key (token
);
23948 cp_parser_error (parser
, "expected class-key");
23952 /* Parse a type-parameter-key.
23954 type-parameter-key:
23960 cp_parser_type_parameter_key (cp_parser
* parser
)
23962 /* Look for the type-parameter-key. */
23963 enum tag_types tag_type
= none_type
;
23964 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23965 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23967 cp_lexer_consume_token (parser
->lexer
);
23968 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23969 /* typename is not allowed in a template template parameter
23970 by the standard until C++17. */
23971 pedwarn (token
->location
, OPT_Wpedantic
,
23972 "ISO C++ forbids typename key in template template parameter;"
23973 " use -std=c++17 or -std=gnu++17");
23976 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23981 /* Parse an (optional) member-specification.
23983 member-specification:
23984 member-declaration member-specification [opt]
23985 access-specifier : member-specification [opt] */
23988 cp_parser_member_specification_opt (cp_parser
* parser
)
23995 /* Peek at the next token. */
23996 token
= cp_lexer_peek_token (parser
->lexer
);
23997 /* If it's a `}', or EOF then we've seen all the members. */
23998 if (token
->type
== CPP_CLOSE_BRACE
23999 || token
->type
== CPP_EOF
24000 || token
->type
== CPP_PRAGMA_EOL
)
24003 /* See if this token is a keyword. */
24004 keyword
= token
->keyword
;
24008 case RID_PROTECTED
:
24010 /* Consume the access-specifier. */
24011 cp_lexer_consume_token (parser
->lexer
);
24012 /* Remember which access-specifier is active. */
24013 current_access_specifier
= token
->u
.value
;
24014 /* Look for the `:'. */
24015 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24019 /* Accept #pragmas at class scope. */
24020 if (token
->type
== CPP_PRAGMA
)
24022 cp_parser_pragma (parser
, pragma_member
, NULL
);
24026 /* Otherwise, the next construction must be a
24027 member-declaration. */
24028 cp_parser_member_declaration (parser
);
24033 /* Parse a member-declaration.
24035 member-declaration:
24036 decl-specifier-seq [opt] member-declarator-list [opt] ;
24037 function-definition ; [opt]
24038 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24040 template-declaration
24043 member-declarator-list:
24045 member-declarator-list , member-declarator
24048 declarator pure-specifier [opt]
24049 declarator constant-initializer [opt]
24050 identifier [opt] : constant-expression
24054 member-declaration:
24055 __extension__ member-declaration
24058 declarator attributes [opt] pure-specifier [opt]
24059 declarator attributes [opt] constant-initializer [opt]
24060 identifier [opt] attributes [opt] : constant-expression
24064 member-declaration:
24065 static_assert-declaration */
24068 cp_parser_member_declaration (cp_parser
* parser
)
24070 cp_decl_specifier_seq decl_specifiers
;
24071 tree prefix_attributes
;
24073 int declares_class_or_enum
;
24075 cp_token
*token
= NULL
;
24076 cp_token
*decl_spec_token_start
= NULL
;
24077 cp_token
*initializer_token_start
= NULL
;
24078 int saved_pedantic
;
24079 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24081 /* Check for the `__extension__' keyword. */
24082 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24085 cp_parser_member_declaration (parser
);
24086 /* Restore the old value of the PEDANTIC flag. */
24087 pedantic
= saved_pedantic
;
24092 /* Check for a template-declaration. */
24093 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24095 /* An explicit specialization here is an error condition, and we
24096 expect the specialization handler to detect and report this. */
24097 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24098 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24099 cp_parser_explicit_specialization (parser
);
24101 cp_parser_template_declaration (parser
, /*member_p=*/true);
24105 /* Check for a template introduction. */
24106 else if (cp_parser_template_declaration_after_export (parser
, true))
24109 /* Check for a using-declaration. */
24110 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24112 if (cxx_dialect
< cxx11
)
24114 /* Parse the using-declaration. */
24115 cp_parser_using_declaration (parser
,
24116 /*access_declaration_p=*/false);
24122 bool alias_decl_expected
;
24123 cp_parser_parse_tentatively (parser
);
24124 decl
= cp_parser_alias_declaration (parser
);
24125 /* Note that if we actually see the '=' token after the
24126 identifier, cp_parser_alias_declaration commits the
24127 tentative parse. In that case, we really expect an
24128 alias-declaration. Otherwise, we expect a using
24130 alias_decl_expected
=
24131 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24132 cp_parser_parse_definitely (parser
);
24134 if (alias_decl_expected
)
24135 finish_member_declaration (decl
);
24137 cp_parser_using_declaration (parser
,
24138 /*access_declaration_p=*/false);
24143 /* Check for @defs. */
24144 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24147 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24148 ivar
= ivar_chains
;
24152 ivar
= TREE_CHAIN (member
);
24153 TREE_CHAIN (member
) = NULL_TREE
;
24154 finish_member_declaration (member
);
24159 /* If the next token is `static_assert' we have a static assertion. */
24160 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24162 cp_parser_static_assert (parser
, /*member_p=*/true);
24166 parser
->colon_corrects_to_scope_p
= false;
24168 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24171 /* Parse the decl-specifier-seq. */
24172 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24173 cp_parser_decl_specifier_seq (parser
,
24174 (CP_PARSER_FLAGS_OPTIONAL
24175 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24177 &declares_class_or_enum
);
24178 /* Check for an invalid type-name. */
24179 if (!decl_specifiers
.any_type_specifiers_p
24180 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24182 /* If there is no declarator, then the decl-specifier-seq should
24184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24186 /* If there was no decl-specifier-seq, and the next token is a
24187 `;', then we have something like:
24193 Each member-declaration shall declare at least one member
24194 name of the class. */
24195 if (!decl_specifiers
.any_specifiers_p
)
24197 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24198 if (!in_system_header_at (token
->location
))
24200 gcc_rich_location
richloc (token
->location
);
24201 richloc
.add_fixit_remove ();
24202 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24209 /* See if this declaration is a friend. */
24210 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24211 /* If there were decl-specifiers, check to see if there was
24212 a class-declaration. */
24213 type
= check_tag_decl (&decl_specifiers
,
24214 /*explicit_type_instantiation_p=*/false);
24215 /* Nested classes have already been added to the class, but
24216 a `friend' needs to be explicitly registered. */
24219 /* If the `friend' keyword was present, the friend must
24220 be introduced with a class-key. */
24221 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24222 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24223 "in C++03 a class-key must be used "
24224 "when declaring a friend");
24227 template <typename T> struct A {
24228 friend struct A<T>::B;
24231 A<T>::B will be represented by a TYPENAME_TYPE, and
24232 therefore not recognized by check_tag_decl. */
24235 type
= decl_specifiers
.type
;
24236 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24237 type
= TREE_TYPE (type
);
24239 if (!type
|| !TYPE_P (type
))
24240 error_at (decl_spec_token_start
->location
,
24241 "friend declaration does not name a class or "
24244 make_friend_class (current_class_type
, type
,
24245 /*complain=*/true);
24247 /* If there is no TYPE, an error message will already have
24249 else if (!type
|| type
== error_mark_node
)
24251 /* An anonymous aggregate has to be handled specially; such
24252 a declaration really declares a data member (with a
24253 particular type), as opposed to a nested class. */
24254 else if (ANON_AGGR_TYPE_P (type
))
24257 if (decl_specifiers
.storage_class
!= sc_none
)
24258 error_at (decl_spec_token_start
->location
,
24259 "a storage class on an anonymous aggregate "
24260 "in class scope is not allowed");
24262 /* Remove constructors and such from TYPE, now that we
24263 know it is an anonymous aggregate. */
24264 fixup_anonymous_aggr (type
);
24265 /* And make the corresponding data member. */
24266 decl
= build_decl (decl_spec_token_start
->location
,
24267 FIELD_DECL
, NULL_TREE
, type
);
24268 /* Add it to the class. */
24269 finish_member_declaration (decl
);
24272 cp_parser_check_access_in_redeclaration
24274 decl_spec_token_start
->location
);
24279 bool assume_semicolon
= false;
24281 /* Clear attributes from the decl_specifiers but keep them
24282 around as prefix attributes that apply them to the entity
24284 prefix_attributes
= decl_specifiers
.attributes
;
24285 decl_specifiers
.attributes
= NULL_TREE
;
24287 /* See if these declarations will be friends. */
24288 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24290 /* Keep going until we hit the `;' at the end of the
24292 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24294 tree attributes
= NULL_TREE
;
24295 tree first_attribute
;
24297 bool named_bitfld
= false;
24299 /* Peek at the next token. */
24300 token
= cp_lexer_peek_token (parser
->lexer
);
24302 /* The following code wants to know early if it is a bit-field
24303 or some other declaration. Attributes can appear before
24304 the `:' token. Skip over them without consuming any tokens
24305 to peek if they are followed by `:'. */
24306 if (cp_next_tokens_can_be_attribute_p (parser
)
24307 || (token
->type
== CPP_NAME
24308 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24309 && (named_bitfld
= true)))
24312 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24313 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24316 /* Check for a bitfield declaration. */
24317 if (token
->type
== CPP_COLON
24318 || (token
->type
== CPP_NAME
24319 && token
== cp_lexer_peek_token (parser
->lexer
)
24320 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24321 && (named_bitfld
= true)))
24325 tree late_attributes
= NULL_TREE
;
24326 location_t id_location
24327 = cp_lexer_peek_token (parser
->lexer
)->location
;
24330 identifier
= cp_parser_identifier (parser
);
24332 identifier
= NULL_TREE
;
24334 /* Look for attributes that apply to the bitfield. */
24335 attributes
= cp_parser_attributes_opt (parser
);
24337 /* Consume the `:' token. */
24338 cp_lexer_consume_token (parser
->lexer
);
24340 /* Get the width of the bitfield. */
24341 width
= cp_parser_constant_expression (parser
, false, NULL
,
24342 cxx_dialect
>= cxx11
);
24344 /* In C++2A and as extension for C++11 and above we allow
24345 default member initializers for bit-fields. */
24346 initializer
= NULL_TREE
;
24347 if (cxx_dialect
>= cxx11
24348 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24349 || cp_lexer_next_token_is (parser
->lexer
,
24353 = cp_lexer_peek_token (parser
->lexer
)->location
;
24354 if (cxx_dialect
< cxx2a
24355 && !in_system_header_at (loc
)
24356 && identifier
!= NULL_TREE
)
24358 "default member initializers for bit-fields "
24359 "only available with -std=c++2a or "
24362 initializer
= cp_parser_save_nsdmi (parser
);
24363 if (identifier
== NULL_TREE
)
24365 error_at (loc
, "default member initializer for "
24366 "unnamed bit-field");
24367 initializer
= NULL_TREE
;
24372 /* Look for attributes that apply to the bitfield after
24373 the `:' token and width. This is where GCC used to
24374 parse attributes in the past, pedwarn if there is
24375 a std attribute. */
24376 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24377 pedwarn (input_location
, OPT_Wpedantic
,
24378 "ISO C++ allows bit-field attributes only "
24379 "before the %<:%> token");
24381 late_attributes
= cp_parser_attributes_opt (parser
);
24384 attributes
= attr_chainon (attributes
, late_attributes
);
24386 /* Remember which attributes are prefix attributes and
24388 first_attribute
= attributes
;
24389 /* Combine the attributes. */
24390 attributes
= attr_chainon (prefix_attributes
, attributes
);
24392 /* Create the bitfield declaration. */
24393 decl
= grokbitfield (identifier
24394 ? make_id_declarator (NULL_TREE
,
24400 width
, initializer
,
24405 cp_declarator
*declarator
;
24406 tree asm_specification
;
24407 int ctor_dtor_or_conv_p
;
24409 /* Parse the declarator. */
24411 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24412 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24413 &ctor_dtor_or_conv_p
,
24414 /*parenthesized_p=*/NULL
,
24418 /* If something went wrong parsing the declarator, make sure
24419 that we at least consume some tokens. */
24420 if (declarator
== cp_error_declarator
)
24422 /* Skip to the end of the statement. */
24423 cp_parser_skip_to_end_of_statement (parser
);
24424 /* If the next token is not a semicolon, that is
24425 probably because we just skipped over the body of
24426 a function. So, we consume a semicolon if
24427 present, but do not issue an error message if it
24429 if (cp_lexer_next_token_is (parser
->lexer
,
24431 cp_lexer_consume_token (parser
->lexer
);
24435 if (declares_class_or_enum
& 2)
24436 cp_parser_check_for_definition_in_return_type
24437 (declarator
, decl_specifiers
.type
,
24438 decl_specifiers
.locations
[ds_type_spec
]);
24440 /* Look for an asm-specification. */
24441 asm_specification
= cp_parser_asm_specification_opt (parser
);
24442 /* Look for attributes that apply to the declaration. */
24443 attributes
= cp_parser_attributes_opt (parser
);
24444 /* Remember which attributes are prefix attributes and
24446 first_attribute
= attributes
;
24447 /* Combine the attributes. */
24448 attributes
= attr_chainon (prefix_attributes
, attributes
);
24450 /* If it's an `=', then we have a constant-initializer or a
24451 pure-specifier. It is not correct to parse the
24452 initializer before registering the member declaration
24453 since the member declaration should be in scope while
24454 its initializer is processed. However, the rest of the
24455 front end does not yet provide an interface that allows
24456 us to handle this correctly. */
24457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24461 A pure-specifier shall be used only in the declaration of
24462 a virtual function.
24464 A member-declarator can contain a constant-initializer
24465 only if it declares a static member of integral or
24468 Therefore, if the DECLARATOR is for a function, we look
24469 for a pure-specifier; otherwise, we look for a
24470 constant-initializer. When we call `grokfield', it will
24471 perform more stringent semantics checks. */
24472 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24473 if (function_declarator_p (declarator
)
24474 || (decl_specifiers
.type
24475 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24476 && declarator
->kind
== cdk_id
24477 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24478 == FUNCTION_TYPE
)))
24479 initializer
= cp_parser_pure_specifier (parser
);
24480 else if (decl_specifiers
.storage_class
!= sc_static
)
24481 initializer
= cp_parser_save_nsdmi (parser
);
24482 else if (cxx_dialect
>= cxx11
)
24485 /* Don't require a constant rvalue in C++11, since we
24486 might want a reference constant. We'll enforce
24487 constancy later. */
24488 cp_lexer_consume_token (parser
->lexer
);
24489 /* Parse the initializer. */
24490 initializer
= cp_parser_initializer_clause (parser
,
24494 /* Parse the initializer. */
24495 initializer
= cp_parser_constant_initializer (parser
);
24497 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24498 && !function_declarator_p (declarator
))
24501 if (decl_specifiers
.storage_class
!= sc_static
)
24502 initializer
= cp_parser_save_nsdmi (parser
);
24504 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24506 /* Otherwise, there is no initializer. */
24508 initializer
= NULL_TREE
;
24510 /* See if we are probably looking at a function
24511 definition. We are certainly not looking at a
24512 member-declarator. Calling `grokfield' has
24513 side-effects, so we must not do it unless we are sure
24514 that we are looking at a member-declarator. */
24515 if (cp_parser_token_starts_function_definition_p
24516 (cp_lexer_peek_token (parser
->lexer
)))
24518 /* The grammar does not allow a pure-specifier to be
24519 used when a member function is defined. (It is
24520 possible that this fact is an oversight in the
24521 standard, since a pure function may be defined
24522 outside of the class-specifier. */
24523 if (initializer
&& initializer_token_start
)
24524 error_at (initializer_token_start
->location
,
24525 "pure-specifier on function-definition");
24526 decl
= cp_parser_save_member_function_body (parser
,
24530 if (parser
->fully_implicit_function_template_p
)
24531 decl
= finish_fully_implicit_template (parser
, decl
);
24532 /* If the member was not a friend, declare it here. */
24534 finish_member_declaration (decl
);
24535 /* Peek at the next token. */
24536 token
= cp_lexer_peek_token (parser
->lexer
);
24537 /* If the next token is a semicolon, consume it. */
24538 if (token
->type
== CPP_SEMICOLON
)
24540 location_t semicolon_loc
24541 = cp_lexer_consume_token (parser
->lexer
)->location
;
24542 gcc_rich_location
richloc (semicolon_loc
);
24543 richloc
.add_fixit_remove ();
24544 warning_at (&richloc
, OPT_Wextra_semi
,
24545 "extra %<;%> after in-class "
24546 "function definition");
24551 if (declarator
->kind
== cdk_function
)
24552 declarator
->id_loc
= token
->location
;
24553 /* Create the declaration. */
24554 decl
= grokfield (declarator
, &decl_specifiers
,
24555 initializer
, /*init_const_expr_p=*/true,
24556 asm_specification
, attributes
);
24557 if (parser
->fully_implicit_function_template_p
)
24560 finish_fully_implicit_template (parser
, 0);
24562 decl
= finish_fully_implicit_template (parser
, decl
);
24566 cp_finalize_omp_declare_simd (parser
, decl
);
24567 cp_finalize_oacc_routine (parser
, decl
, false);
24569 /* Reset PREFIX_ATTRIBUTES. */
24570 if (attributes
!= error_mark_node
)
24572 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24573 attributes
= TREE_CHAIN (attributes
);
24575 TREE_CHAIN (attributes
) = NULL_TREE
;
24578 /* If there is any qualification still in effect, clear it
24579 now; we will be starting fresh with the next declarator. */
24580 parser
->scope
= NULL_TREE
;
24581 parser
->qualifying_scope
= NULL_TREE
;
24582 parser
->object_scope
= NULL_TREE
;
24583 /* If it's a `,', then there are more declarators. */
24584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24586 cp_lexer_consume_token (parser
->lexer
);
24587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24589 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24590 gcc_rich_location
richloc (token
->location
);
24591 richloc
.add_fixit_remove ();
24592 error_at (&richloc
, "stray %<,%> at end of "
24593 "member declaration");
24596 /* If the next token isn't a `;', then we have a parse error. */
24597 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24600 /* The next token might be a ways away from where the
24601 actual semicolon is missing. Find the previous token
24602 and use that for our error position. */
24603 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24604 gcc_rich_location
richloc (token
->location
);
24605 richloc
.add_fixit_insert_after (";");
24606 error_at (&richloc
, "expected %<;%> at end of "
24607 "member declaration");
24609 /* Assume that the user meant to provide a semicolon. If
24610 we were to cp_parser_skip_to_end_of_statement, we might
24611 skip to a semicolon inside a member function definition
24612 and issue nonsensical error messages. */
24613 assume_semicolon
= true;
24618 /* Add DECL to the list of members. */
24620 /* Explicitly include, eg, NSDMIs, for better error
24621 recovery (c++/58650). */
24622 || !DECL_DECLARES_FUNCTION_P (decl
))
24623 finish_member_declaration (decl
);
24625 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24626 cp_parser_save_default_args (parser
, decl
);
24627 else if (TREE_CODE (decl
) == FIELD_DECL
24628 && DECL_INITIAL (decl
))
24629 /* Add DECL to the queue of NSDMI to be parsed later. */
24630 vec_safe_push (unparsed_nsdmis
, decl
);
24633 if (assume_semicolon
)
24638 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24640 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24643 /* Parse a pure-specifier.
24648 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24649 Otherwise, ERROR_MARK_NODE is returned. */
24652 cp_parser_pure_specifier (cp_parser
* parser
)
24656 /* Look for the `=' token. */
24657 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24658 return error_mark_node
;
24659 /* Look for the `0' token. */
24660 token
= cp_lexer_peek_token (parser
->lexer
);
24662 if (token
->type
== CPP_EOF
24663 || token
->type
== CPP_PRAGMA_EOL
)
24664 return error_mark_node
;
24666 cp_lexer_consume_token (parser
->lexer
);
24668 /* Accept = default or = delete in c++0x mode. */
24669 if (token
->keyword
== RID_DEFAULT
24670 || token
->keyword
== RID_DELETE
)
24672 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24673 return token
->u
.value
;
24676 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24677 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24679 cp_parser_error (parser
,
24680 "invalid pure specifier (only %<= 0%> is allowed)");
24681 cp_parser_skip_to_end_of_statement (parser
);
24682 return error_mark_node
;
24684 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24686 error_at (token
->location
, "templates may not be %<virtual%>");
24687 return error_mark_node
;
24690 return integer_zero_node
;
24693 /* Parse a constant-initializer.
24695 constant-initializer:
24696 = constant-expression
24698 Returns a representation of the constant-expression. */
24701 cp_parser_constant_initializer (cp_parser
* parser
)
24703 /* Look for the `=' token. */
24704 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24705 return error_mark_node
;
24707 /* It is invalid to write:
24709 struct S { static const int i = { 7 }; };
24712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24714 cp_parser_error (parser
,
24715 "a brace-enclosed initializer is not allowed here");
24716 /* Consume the opening brace. */
24717 matching_braces braces
;
24718 braces
.consume_open (parser
);
24719 /* Skip the initializer. */
24720 cp_parser_skip_to_closing_brace (parser
);
24721 /* Look for the trailing `}'. */
24722 braces
.require_close (parser
);
24724 return error_mark_node
;
24727 return cp_parser_constant_expression (parser
);
24730 /* Derived classes [gram.class.derived] */
24732 /* Parse a base-clause.
24735 : base-specifier-list
24737 base-specifier-list:
24738 base-specifier ... [opt]
24739 base-specifier-list , base-specifier ... [opt]
24741 Returns a TREE_LIST representing the base-classes, in the order in
24742 which they were declared. The representation of each node is as
24743 described by cp_parser_base_specifier.
24745 In the case that no bases are specified, this function will return
24746 NULL_TREE, not ERROR_MARK_NODE. */
24749 cp_parser_base_clause (cp_parser
* parser
)
24751 tree bases
= NULL_TREE
;
24753 /* Look for the `:' that begins the list. */
24754 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24756 /* Scan the base-specifier-list. */
24761 bool pack_expansion_p
= false;
24763 /* Look for the base-specifier. */
24764 base
= cp_parser_base_specifier (parser
);
24765 /* Look for the (optional) ellipsis. */
24766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24768 /* Consume the `...'. */
24769 cp_lexer_consume_token (parser
->lexer
);
24771 pack_expansion_p
= true;
24774 /* Add BASE to the front of the list. */
24775 if (base
&& base
!= error_mark_node
)
24777 if (pack_expansion_p
)
24778 /* Make this a pack expansion type. */
24779 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24781 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24783 TREE_CHAIN (base
) = bases
;
24787 /* Peek at the next token. */
24788 token
= cp_lexer_peek_token (parser
->lexer
);
24789 /* If it's not a comma, then the list is complete. */
24790 if (token
->type
!= CPP_COMMA
)
24792 /* Consume the `,'. */
24793 cp_lexer_consume_token (parser
->lexer
);
24796 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24797 base class had a qualified name. However, the next name that
24798 appears is certainly not qualified. */
24799 parser
->scope
= NULL_TREE
;
24800 parser
->qualifying_scope
= NULL_TREE
;
24801 parser
->object_scope
= NULL_TREE
;
24803 return nreverse (bases
);
24806 /* Parse a base-specifier.
24809 :: [opt] nested-name-specifier [opt] class-name
24810 virtual access-specifier [opt] :: [opt] nested-name-specifier
24812 access-specifier virtual [opt] :: [opt] nested-name-specifier
24815 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24816 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24817 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24818 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24821 cp_parser_base_specifier (cp_parser
* parser
)
24825 bool virtual_p
= false;
24826 bool duplicate_virtual_error_issued_p
= false;
24827 bool duplicate_access_error_issued_p
= false;
24828 bool class_scope_p
, template_p
;
24829 tree access
= access_default_node
;
24832 /* Process the optional `virtual' and `access-specifier'. */
24835 /* Peek at the next token. */
24836 token
= cp_lexer_peek_token (parser
->lexer
);
24837 /* Process `virtual'. */
24838 switch (token
->keyword
)
24841 /* If `virtual' appears more than once, issue an error. */
24842 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24844 cp_parser_error (parser
,
24845 "%<virtual%> specified more than once in base-specifier");
24846 duplicate_virtual_error_issued_p
= true;
24851 /* Consume the `virtual' token. */
24852 cp_lexer_consume_token (parser
->lexer
);
24857 case RID_PROTECTED
:
24859 /* If more than one access specifier appears, issue an
24861 if (access
!= access_default_node
24862 && !duplicate_access_error_issued_p
)
24864 cp_parser_error (parser
,
24865 "more than one access specifier in base-specifier");
24866 duplicate_access_error_issued_p
= true;
24869 access
= ridpointers
[(int) token
->keyword
];
24871 /* Consume the access-specifier. */
24872 cp_lexer_consume_token (parser
->lexer
);
24881 /* It is not uncommon to see programs mechanically, erroneously, use
24882 the 'typename' keyword to denote (dependent) qualified types
24883 as base classes. */
24884 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24886 token
= cp_lexer_peek_token (parser
->lexer
);
24887 if (!processing_template_decl
)
24888 error_at (token
->location
,
24889 "keyword %<typename%> not allowed outside of templates");
24891 error_at (token
->location
,
24892 "keyword %<typename%> not allowed in this context "
24893 "(the base class is implicitly a type)");
24894 cp_lexer_consume_token (parser
->lexer
);
24897 /* Look for the optional `::' operator. */
24898 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24899 /* Look for the nested-name-specifier. The simplest way to
24904 The keyword `typename' is not permitted in a base-specifier or
24905 mem-initializer; in these contexts a qualified name that
24906 depends on a template-parameter is implicitly assumed to be a
24909 is to pretend that we have seen the `typename' keyword at this
24911 cp_parser_nested_name_specifier_opt (parser
,
24912 /*typename_keyword_p=*/true,
24913 /*check_dependency_p=*/true,
24915 /*is_declaration=*/true);
24916 /* If the base class is given by a qualified name, assume that names
24917 we see are type names or templates, as appropriate. */
24918 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24919 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24922 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24923 /* DR 950 allows decltype as a base-specifier. */
24924 type
= cp_parser_decltype (parser
);
24927 /* Otherwise, look for the class-name. */
24928 type
= cp_parser_class_name (parser
,
24932 /*check_dependency_p=*/true,
24933 /*class_head_p=*/false,
24934 /*is_declaration=*/true);
24935 type
= TREE_TYPE (type
);
24938 if (type
== error_mark_node
)
24939 return error_mark_node
;
24941 return finish_base_specifier (type
, access
, virtual_p
);
24944 /* Exception handling [gram.exception] */
24946 /* Parse an (optional) noexcept-specification.
24948 noexcept-specification:
24949 noexcept ( constant-expression ) [opt]
24951 If no noexcept-specification is present, returns NULL_TREE.
24952 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24953 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24954 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24955 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24956 in which case a boolean condition is returned instead. */
24959 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24960 bool require_constexpr
,
24961 bool* consumed_expr
,
24965 const char *saved_message
;
24967 /* Peek at the next token. */
24968 token
= cp_lexer_peek_token (parser
->lexer
);
24970 /* Is it a noexcept-specification? */
24971 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24974 cp_lexer_consume_token (parser
->lexer
);
24976 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24978 matching_parens parens
;
24979 parens
.consume_open (parser
);
24981 tree save_ccp
= current_class_ptr
;
24982 tree save_ccr
= current_class_ref
;
24984 if (current_class_type
)
24985 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
24987 if (require_constexpr
)
24989 /* Types may not be defined in an exception-specification. */
24990 saved_message
= parser
->type_definition_forbidden_message
;
24991 parser
->type_definition_forbidden_message
24992 = G_("types may not be defined in an exception-specification");
24994 expr
= cp_parser_constant_expression (parser
);
24996 /* Restore the saved message. */
24997 parser
->type_definition_forbidden_message
= saved_message
;
25001 expr
= cp_parser_expression (parser
);
25002 *consumed_expr
= true;
25005 parens
.require_close (parser
);
25007 current_class_ptr
= save_ccp
;
25008 current_class_ref
= save_ccr
;
25012 expr
= boolean_true_node
;
25013 if (!require_constexpr
)
25014 *consumed_expr
= false;
25017 /* We cannot build a noexcept-spec right away because this will check
25018 that expr is a constexpr. */
25020 return build_noexcept_spec (expr
, tf_warning_or_error
);
25028 /* Parse an (optional) exception-specification.
25030 exception-specification:
25031 throw ( type-id-list [opt] )
25033 Returns a TREE_LIST representing the exception-specification. The
25034 TREE_VALUE of each node is a type. */
25037 cp_parser_exception_specification_opt (cp_parser
* parser
)
25041 const char *saved_message
;
25043 /* Peek at the next token. */
25044 token
= cp_lexer_peek_token (parser
->lexer
);
25046 /* Is it a noexcept-specification? */
25047 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25049 if (type_id_list
!= NULL_TREE
)
25050 return type_id_list
;
25052 /* If it's not `throw', then there's no exception-specification. */
25053 if (!cp_parser_is_keyword (token
, RID_THROW
))
25056 location_t loc
= token
->location
;
25058 /* Consume the `throw'. */
25059 cp_lexer_consume_token (parser
->lexer
);
25061 /* Look for the `('. */
25062 matching_parens parens
;
25063 parens
.require_open (parser
);
25065 /* Peek at the next token. */
25066 token
= cp_lexer_peek_token (parser
->lexer
);
25067 /* If it's not a `)', then there is a type-id-list. */
25068 if (token
->type
!= CPP_CLOSE_PAREN
)
25070 /* Types may not be defined in an exception-specification. */
25071 saved_message
= parser
->type_definition_forbidden_message
;
25072 parser
->type_definition_forbidden_message
25073 = G_("types may not be defined in an exception-specification");
25074 /* Parse the type-id-list. */
25075 type_id_list
= cp_parser_type_id_list (parser
);
25076 /* Restore the saved message. */
25077 parser
->type_definition_forbidden_message
= saved_message
;
25079 if (cxx_dialect
>= cxx17
)
25081 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25083 type_id_list
= NULL_TREE
;
25085 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25086 warning_at (loc
, OPT_Wdeprecated
,
25087 "dynamic exception specifications are deprecated in "
25090 /* In C++17, throw() is equivalent to noexcept (true). throw()
25091 is deprecated in C++11 and above as well, but is still widely used,
25092 so don't warn about it yet. */
25093 else if (cxx_dialect
>= cxx17
)
25094 type_id_list
= noexcept_true_spec
;
25096 type_id_list
= empty_except_spec
;
25098 /* Look for the `)'. */
25099 parens
.require_close (parser
);
25101 return type_id_list
;
25104 /* Parse an (optional) type-id-list.
25108 type-id-list , type-id ... [opt]
25110 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25111 in the order that the types were presented. */
25114 cp_parser_type_id_list (cp_parser
* parser
)
25116 tree types
= NULL_TREE
;
25123 token
= cp_lexer_peek_token (parser
->lexer
);
25125 /* Get the next type-id. */
25126 type
= cp_parser_type_id (parser
);
25127 /* Check for invalid 'auto'. */
25128 if (flag_concepts
&& type_uses_auto (type
))
25130 error_at (token
->location
,
25131 "invalid use of %<auto%> in exception-specification");
25132 type
= error_mark_node
;
25134 /* Parse the optional ellipsis. */
25135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25137 /* Consume the `...'. */
25138 cp_lexer_consume_token (parser
->lexer
);
25140 /* Turn the type into a pack expansion expression. */
25141 type
= make_pack_expansion (type
);
25143 /* Add it to the list. */
25144 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25145 /* Peek at the next token. */
25146 token
= cp_lexer_peek_token (parser
->lexer
);
25147 /* If it is not a `,', we are done. */
25148 if (token
->type
!= CPP_COMMA
)
25150 /* Consume the `,'. */
25151 cp_lexer_consume_token (parser
->lexer
);
25154 return nreverse (types
);
25157 /* Parse a try-block.
25160 try compound-statement handler-seq */
25163 cp_parser_try_block (cp_parser
* parser
)
25167 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25168 if (parser
->in_function_body
25169 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
25170 error ("%<try%> in %<constexpr%> function");
25172 try_block
= begin_try_block ();
25173 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25174 finish_try_block (try_block
);
25175 cp_parser_handler_seq (parser
);
25176 finish_handler_sequence (try_block
);
25181 /* Parse a function-try-block.
25183 function-try-block:
25184 try ctor-initializer [opt] function-body handler-seq */
25187 cp_parser_function_try_block (cp_parser
* parser
)
25189 tree compound_stmt
;
25192 /* Look for the `try' keyword. */
25193 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25195 /* Let the rest of the front end know where we are. */
25196 try_block
= begin_function_try_block (&compound_stmt
);
25197 /* Parse the function-body. */
25198 cp_parser_ctor_initializer_opt_and_function_body
25199 (parser
, /*in_function_try_block=*/true);
25200 /* We're done with the `try' part. */
25201 finish_function_try_block (try_block
);
25202 /* Parse the handlers. */
25203 cp_parser_handler_seq (parser
);
25204 /* We're done with the handlers. */
25205 finish_function_handler_sequence (try_block
, compound_stmt
);
25208 /* Parse a handler-seq.
25211 handler handler-seq [opt] */
25214 cp_parser_handler_seq (cp_parser
* parser
)
25220 /* Parse the handler. */
25221 cp_parser_handler (parser
);
25222 /* Peek at the next token. */
25223 token
= cp_lexer_peek_token (parser
->lexer
);
25224 /* If it's not `catch' then there are no more handlers. */
25225 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25230 /* Parse a handler.
25233 catch ( exception-declaration ) compound-statement */
25236 cp_parser_handler (cp_parser
* parser
)
25241 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25242 handler
= begin_handler ();
25243 matching_parens parens
;
25244 parens
.require_open (parser
);
25245 declaration
= cp_parser_exception_declaration (parser
);
25246 finish_handler_parms (declaration
, handler
);
25247 parens
.require_close (parser
);
25248 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25249 finish_handler (handler
);
25252 /* Parse an exception-declaration.
25254 exception-declaration:
25255 type-specifier-seq declarator
25256 type-specifier-seq abstract-declarator
25260 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25261 ellipsis variant is used. */
25264 cp_parser_exception_declaration (cp_parser
* parser
)
25266 cp_decl_specifier_seq type_specifiers
;
25267 cp_declarator
*declarator
;
25268 const char *saved_message
;
25270 /* If it's an ellipsis, it's easy to handle. */
25271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25273 /* Consume the `...' token. */
25274 cp_lexer_consume_token (parser
->lexer
);
25278 /* Types may not be defined in exception-declarations. */
25279 saved_message
= parser
->type_definition_forbidden_message
;
25280 parser
->type_definition_forbidden_message
25281 = G_("types may not be defined in exception-declarations");
25283 /* Parse the type-specifier-seq. */
25284 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25285 /*is_declaration=*/true,
25286 /*is_trailing_return=*/false,
25288 /* If it's a `)', then there is no declarator. */
25289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25292 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25293 CP_PARSER_FLAGS_NONE
,
25294 /*ctor_dtor_or_conv_p=*/NULL
,
25295 /*parenthesized_p=*/NULL
,
25296 /*member_p=*/false,
25297 /*friend_p=*/false);
25299 /* Restore the saved message. */
25300 parser
->type_definition_forbidden_message
= saved_message
;
25302 if (!type_specifiers
.any_specifiers_p
)
25303 return error_mark_node
;
25305 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25308 /* Parse a throw-expression.
25311 throw assignment-expression [opt]
25313 Returns a THROW_EXPR representing the throw-expression. */
25316 cp_parser_throw_expression (cp_parser
* parser
)
25321 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25322 token
= cp_lexer_peek_token (parser
->lexer
);
25323 /* Figure out whether or not there is an assignment-expression
25324 following the "throw" keyword. */
25325 if (token
->type
== CPP_COMMA
25326 || token
->type
== CPP_SEMICOLON
25327 || token
->type
== CPP_CLOSE_PAREN
25328 || token
->type
== CPP_CLOSE_SQUARE
25329 || token
->type
== CPP_CLOSE_BRACE
25330 || token
->type
== CPP_COLON
)
25331 expression
= NULL_TREE
;
25333 expression
= cp_parser_assignment_expression (parser
);
25335 return build_throw (expression
);
25338 /* GNU Extensions */
25340 /* Parse an (optional) asm-specification.
25343 asm ( string-literal )
25345 If the asm-specification is present, returns a STRING_CST
25346 corresponding to the string-literal. Otherwise, returns
25350 cp_parser_asm_specification_opt (cp_parser
* parser
)
25353 tree asm_specification
;
25355 /* Peek at the next token. */
25356 token
= cp_lexer_peek_token (parser
->lexer
);
25357 /* If the next token isn't the `asm' keyword, then there's no
25358 asm-specification. */
25359 if (!cp_parser_is_keyword (token
, RID_ASM
))
25362 /* Consume the `asm' token. */
25363 cp_lexer_consume_token (parser
->lexer
);
25364 /* Look for the `('. */
25365 matching_parens parens
;
25366 parens
.require_open (parser
);
25368 /* Look for the string-literal. */
25369 asm_specification
= cp_parser_string_literal (parser
, false, false);
25371 /* Look for the `)'. */
25372 parens
.require_close (parser
);
25374 return asm_specification
;
25377 /* Parse an asm-operand-list.
25381 asm-operand-list , asm-operand
25384 string-literal ( expression )
25385 [ string-literal ] string-literal ( expression )
25387 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25388 each node is the expression. The TREE_PURPOSE is itself a
25389 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25390 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25391 is a STRING_CST for the string literal before the parenthesis. Returns
25392 ERROR_MARK_NODE if any of the operands are invalid. */
25395 cp_parser_asm_operand_list (cp_parser
* parser
)
25397 tree asm_operands
= NULL_TREE
;
25398 bool invalid_operands
= false;
25402 tree string_literal
;
25406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25408 /* Consume the `[' token. */
25409 cp_lexer_consume_token (parser
->lexer
);
25410 /* Read the operand name. */
25411 name
= cp_parser_identifier (parser
);
25412 if (name
!= error_mark_node
)
25413 name
= build_string (IDENTIFIER_LENGTH (name
),
25414 IDENTIFIER_POINTER (name
));
25415 /* Look for the closing `]'. */
25416 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25420 /* Look for the string-literal. */
25421 string_literal
= cp_parser_string_literal (parser
, false, false);
25423 /* Look for the `('. */
25424 matching_parens parens
;
25425 parens
.require_open (parser
);
25426 /* Parse the expression. */
25427 expression
= cp_parser_expression (parser
);
25428 /* Look for the `)'. */
25429 parens
.require_close (parser
);
25431 if (name
== error_mark_node
25432 || string_literal
== error_mark_node
25433 || expression
== error_mark_node
)
25434 invalid_operands
= true;
25436 /* Add this operand to the list. */
25437 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25440 /* If the next token is not a `,', there are no more
25442 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25444 /* Consume the `,'. */
25445 cp_lexer_consume_token (parser
->lexer
);
25448 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25451 /* Parse an asm-clobber-list.
25455 asm-clobber-list , string-literal
25457 Returns a TREE_LIST, indicating the clobbers in the order that they
25458 appeared. The TREE_VALUE of each node is a STRING_CST. */
25461 cp_parser_asm_clobber_list (cp_parser
* parser
)
25463 tree clobbers
= NULL_TREE
;
25467 tree string_literal
;
25469 /* Look for the string literal. */
25470 string_literal
= cp_parser_string_literal (parser
, false, false);
25471 /* Add it to the list. */
25472 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25473 /* If the next token is not a `,', then the list is
25475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25477 /* Consume the `,' token. */
25478 cp_lexer_consume_token (parser
->lexer
);
25484 /* Parse an asm-label-list.
25488 asm-label-list , identifier
25490 Returns a TREE_LIST, indicating the labels in the order that they
25491 appeared. The TREE_VALUE of each node is a label. */
25494 cp_parser_asm_label_list (cp_parser
* parser
)
25496 tree labels
= NULL_TREE
;
25500 tree identifier
, label
, name
;
25502 /* Look for the identifier. */
25503 identifier
= cp_parser_identifier (parser
);
25504 if (!error_operand_p (identifier
))
25506 label
= lookup_label (identifier
);
25507 if (TREE_CODE (label
) == LABEL_DECL
)
25509 TREE_USED (label
) = 1;
25510 check_goto (label
);
25511 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25512 IDENTIFIER_POINTER (identifier
));
25513 labels
= tree_cons (name
, label
, labels
);
25516 /* If the next token is not a `,', then the list is
25518 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25520 /* Consume the `,' token. */
25521 cp_lexer_consume_token (parser
->lexer
);
25524 return nreverse (labels
);
25527 /* Return TRUE iff the next tokens in the stream are possibly the
25528 beginning of a GNU extension attribute. */
25531 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25533 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25536 /* Return TRUE iff the next tokens in the stream are possibly the
25537 beginning of a standard C++-11 attribute specifier. */
25540 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25542 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25545 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25546 beginning of a standard C++-11 attribute specifier. */
25549 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25551 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25553 return (cxx_dialect
>= cxx11
25554 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25555 || (token
->type
== CPP_OPEN_SQUARE
25556 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25557 && token
->type
== CPP_OPEN_SQUARE
)));
25560 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25561 beginning of a GNU extension attribute. */
25564 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25566 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25568 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25571 /* Return true iff the next tokens can be the beginning of either a
25572 GNU attribute list, or a standard C++11 attribute sequence. */
25575 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25577 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25578 || cp_next_tokens_can_be_std_attribute_p (parser
));
25581 /* Return true iff the next Nth tokens can be the beginning of either
25582 a GNU attribute list, or a standard C++11 attribute sequence. */
25585 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25587 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25588 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25591 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25592 of GNU attributes, or return NULL. */
25595 cp_parser_attributes_opt (cp_parser
*parser
)
25597 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25598 return cp_parser_gnu_attributes_opt (parser
);
25599 return cp_parser_std_attribute_spec_seq (parser
);
25602 /* Parse an (optional) series of attributes.
25605 attributes attribute
25608 __attribute__ (( attribute-list [opt] ))
25610 The return value is as for cp_parser_gnu_attribute_list. */
25613 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25615 tree attributes
= NULL_TREE
;
25617 temp_override
<bool> cleanup
25618 (parser
->auto_is_implicit_function_template_parm_p
, false);
25623 tree attribute_list
;
25626 /* Peek at the next token. */
25627 token
= cp_lexer_peek_token (parser
->lexer
);
25628 /* If it's not `__attribute__', then we're done. */
25629 if (token
->keyword
!= RID_ATTRIBUTE
)
25632 /* Consume the `__attribute__' keyword. */
25633 cp_lexer_consume_token (parser
->lexer
);
25634 /* Look for the two `(' tokens. */
25635 matching_parens outer_parens
;
25636 outer_parens
.require_open (parser
);
25637 matching_parens inner_parens
;
25638 inner_parens
.require_open (parser
);
25640 /* Peek at the next token. */
25641 token
= cp_lexer_peek_token (parser
->lexer
);
25642 if (token
->type
!= CPP_CLOSE_PAREN
)
25643 /* Parse the attribute-list. */
25644 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25646 /* If the next token is a `)', then there is no attribute
25648 attribute_list
= NULL
;
25650 /* Look for the two `)' tokens. */
25651 if (!inner_parens
.require_close (parser
))
25653 if (!outer_parens
.require_close (parser
))
25656 cp_parser_skip_to_end_of_statement (parser
);
25658 /* Add these new attributes to the list. */
25659 attributes
= attr_chainon (attributes
, attribute_list
);
25665 /* Parse a GNU attribute-list.
25669 attribute-list , attribute
25673 identifier ( identifier )
25674 identifier ( identifier , expression-list )
25675 identifier ( expression-list )
25677 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25678 to an attribute. The TREE_PURPOSE of each node is the identifier
25679 indicating which attribute is in use. The TREE_VALUE represents
25680 the arguments, if any. */
25683 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25685 tree attribute_list
= NULL_TREE
;
25686 bool save_translate_strings_p
= parser
->translate_strings_p
;
25688 /* Don't create wrapper nodes within attributes: the
25689 handlers don't know how to handle them. */
25690 auto_suppress_location_wrappers sentinel
;
25692 parser
->translate_strings_p
= false;
25699 /* Look for the identifier. We also allow keywords here; for
25700 example `__attribute__ ((const))' is legal. */
25701 token
= cp_lexer_peek_token (parser
->lexer
);
25702 if (token
->type
== CPP_NAME
25703 || token
->type
== CPP_KEYWORD
)
25705 tree arguments
= NULL_TREE
;
25707 /* Consume the token, but save it since we need it for the
25708 SIMD enabled function parsing. */
25709 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25711 /* Save away the identifier that indicates which attribute
25713 identifier
= (token
->type
== CPP_KEYWORD
)
25714 /* For keywords, use the canonical spelling, not the
25715 parsed identifier. */
25716 ? ridpointers
[(int) token
->keyword
]
25717 : id_token
->u
.value
;
25719 identifier
= canonicalize_attr_name (identifier
);
25720 attribute
= build_tree_list (identifier
, NULL_TREE
);
25722 /* Peek at the next token. */
25723 token
= cp_lexer_peek_token (parser
->lexer
);
25724 /* If it's an `(', then parse the attribute arguments. */
25725 if (token
->type
== CPP_OPEN_PAREN
)
25727 vec
<tree
, va_gc
> *vec
;
25728 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25729 ? id_attr
: normal_attr
);
25730 vec
= cp_parser_parenthesized_expression_list
25731 (parser
, attr_flag
, /*cast_p=*/false,
25732 /*allow_expansion_p=*/false,
25733 /*non_constant_p=*/NULL
);
25735 arguments
= error_mark_node
;
25738 arguments
= build_tree_list_vec (vec
);
25739 release_tree_vector (vec
);
25741 /* Save the arguments away. */
25742 TREE_VALUE (attribute
) = arguments
;
25745 if (arguments
!= error_mark_node
)
25747 /* Add this attribute to the list. */
25748 TREE_CHAIN (attribute
) = attribute_list
;
25749 attribute_list
= attribute
;
25752 token
= cp_lexer_peek_token (parser
->lexer
);
25754 /* Unless EXACTLY_ONE is set look for more attributes.
25755 If the next token isn't a `,', we're done. */
25756 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25759 /* Consume the comma and keep going. */
25760 cp_lexer_consume_token (parser
->lexer
);
25762 parser
->translate_strings_p
= save_translate_strings_p
;
25764 /* We built up the list in reverse order. */
25765 return nreverse (attribute_list
);
25768 /* Parse a standard C++11 attribute.
25770 The returned representation is a TREE_LIST which TREE_PURPOSE is
25771 the scoped name of the attribute, and the TREE_VALUE is its
25774 Note that the scoped name of the attribute is itself a TREE_LIST
25775 which TREE_PURPOSE is the namespace of the attribute, and
25776 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25777 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25778 and which TREE_PURPOSE is directly the attribute name.
25780 Clients of the attribute code should use get_attribute_namespace
25781 and get_attribute_name to get the actual namespace and name of
25782 attributes, regardless of their being GNU or C++11 attributes.
25785 attribute-token attribute-argument-clause [opt]
25789 attribute-scoped-token
25791 attribute-scoped-token:
25792 attribute-namespace :: identifier
25794 attribute-namespace:
25797 attribute-argument-clause:
25798 ( balanced-token-seq )
25800 balanced-token-seq:
25801 balanced-token [opt]
25802 balanced-token-seq balanced-token
25805 ( balanced-token-seq )
25806 [ balanced-token-seq ]
25807 { balanced-token-seq }. */
25810 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25812 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25815 temp_override
<bool> cleanup
25816 (parser
->auto_is_implicit_function_template_parm_p
, false);
25818 /* First, parse name of the attribute, a.k.a attribute-token. */
25820 token
= cp_lexer_peek_token (parser
->lexer
);
25821 if (token
->type
== CPP_NAME
)
25822 attr_id
= token
->u
.value
;
25823 else if (token
->type
== CPP_KEYWORD
)
25824 attr_id
= ridpointers
[(int) token
->keyword
];
25825 else if (token
->flags
& NAMED_OP
)
25826 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25828 if (attr_id
== NULL_TREE
)
25831 cp_lexer_consume_token (parser
->lexer
);
25833 token
= cp_lexer_peek_token (parser
->lexer
);
25834 if (token
->type
== CPP_SCOPE
)
25836 /* We are seeing a scoped attribute token. */
25838 cp_lexer_consume_token (parser
->lexer
);
25840 error_at (token
->location
, "attribute using prefix used together "
25841 "with scoped attribute token");
25844 token
= cp_lexer_consume_token (parser
->lexer
);
25845 if (token
->type
== CPP_NAME
)
25846 attr_id
= token
->u
.value
;
25847 else if (token
->type
== CPP_KEYWORD
)
25848 attr_id
= ridpointers
[(int) token
->keyword
];
25849 else if (token
->flags
& NAMED_OP
)
25850 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25853 error_at (token
->location
,
25854 "expected an identifier for the attribute name");
25855 return error_mark_node
;
25858 attr_ns
= canonicalize_attr_name (attr_ns
);
25859 attr_id
= canonicalize_attr_name (attr_id
);
25860 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25862 token
= cp_lexer_peek_token (parser
->lexer
);
25866 attr_ns
= canonicalize_attr_name (attr_ns
);
25867 attr_id
= canonicalize_attr_name (attr_id
);
25868 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25873 attr_id
= canonicalize_attr_name (attr_id
);
25874 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25876 /* C++11 noreturn attribute is equivalent to GNU's. */
25877 if (is_attribute_p ("noreturn", attr_id
))
25878 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25879 /* C++14 deprecated attribute is equivalent to GNU's. */
25880 else if (is_attribute_p ("deprecated", attr_id
))
25881 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25882 /* C++17 fallthrough attribute is equivalent to GNU's. */
25883 else if (is_attribute_p ("fallthrough", attr_id
))
25884 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25885 /* Transactional Memory TS optimize_for_synchronized attribute is
25886 equivalent to GNU transaction_callable. */
25887 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25888 TREE_PURPOSE (attribute
)
25889 = get_identifier ("transaction_callable");
25890 /* Transactional Memory attributes are GNU attributes. */
25891 else if (tm_attr_to_mask (attr_id
))
25892 TREE_PURPOSE (attribute
) = attr_id
;
25895 /* Now parse the optional argument clause of the attribute. */
25897 if (token
->type
!= CPP_OPEN_PAREN
)
25901 vec
<tree
, va_gc
> *vec
;
25902 int attr_flag
= normal_attr
;
25904 if (attr_ns
== gnu_identifier
25905 && attribute_takes_identifier_p (attr_id
))
25906 /* A GNU attribute that takes an identifier in parameter. */
25907 attr_flag
= id_attr
;
25909 vec
= cp_parser_parenthesized_expression_list
25910 (parser
, attr_flag
, /*cast_p=*/false,
25911 /*allow_expansion_p=*/true,
25912 /*non_constant_p=*/NULL
);
25914 arguments
= error_mark_node
;
25917 arguments
= build_tree_list_vec (vec
);
25918 release_tree_vector (vec
);
25921 if (arguments
== error_mark_node
)
25922 attribute
= error_mark_node
;
25924 TREE_VALUE (attribute
) = arguments
;
25930 /* Check that the attribute ATTRIBUTE appears at most once in the
25931 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25932 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25933 isn't implemented yet in GCC. */
25936 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25940 tree name
= get_attribute_name (attribute
);
25941 if (is_attribute_p ("noreturn", name
)
25942 && lookup_attribute ("noreturn", attributes
))
25943 error ("attribute %<noreturn%> can appear at most once "
25944 "in an attribute-list");
25945 else if (is_attribute_p ("deprecated", name
)
25946 && lookup_attribute ("deprecated", attributes
))
25947 error ("attribute %<deprecated%> can appear at most once "
25948 "in an attribute-list");
25952 /* Parse a list of standard C++-11 attributes.
25956 attribute-list , attribute[opt]
25958 attribute-list , attribute ...
25962 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25964 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25965 cp_token
*token
= NULL
;
25969 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25970 if (attribute
== error_mark_node
)
25972 if (attribute
!= NULL_TREE
)
25974 cp_parser_check_std_attribute (attributes
, attribute
);
25975 TREE_CHAIN (attribute
) = attributes
;
25976 attributes
= attribute
;
25978 token
= cp_lexer_peek_token (parser
->lexer
);
25979 if (token
->type
== CPP_ELLIPSIS
)
25981 cp_lexer_consume_token (parser
->lexer
);
25982 if (attribute
== NULL_TREE
)
25983 error_at (token
->location
,
25984 "expected attribute before %<...%>");
25987 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25988 if (pack
== error_mark_node
)
25989 return error_mark_node
;
25990 TREE_VALUE (attribute
) = pack
;
25992 token
= cp_lexer_peek_token (parser
->lexer
);
25994 if (token
->type
!= CPP_COMMA
)
25996 cp_lexer_consume_token (parser
->lexer
);
25998 attributes
= nreverse (attributes
);
26002 /* Parse a standard C++-11 attribute specifier.
26004 attribute-specifier:
26005 [ [ attribute-using-prefix [opt] attribute-list ] ]
26006 alignment-specifier
26008 attribute-using-prefix:
26009 using attribute-namespace :
26011 alignment-specifier:
26012 alignas ( type-id ... [opt] )
26013 alignas ( alignment-expression ... [opt] ). */
26016 cp_parser_std_attribute_spec (cp_parser
*parser
)
26018 tree attributes
= NULL_TREE
;
26019 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26021 if (token
->type
== CPP_OPEN_SQUARE
26022 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26024 tree attr_ns
= NULL_TREE
;
26026 cp_lexer_consume_token (parser
->lexer
);
26027 cp_lexer_consume_token (parser
->lexer
);
26029 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26031 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26032 if (token
->type
== CPP_NAME
)
26033 attr_ns
= token
->u
.value
;
26034 else if (token
->type
== CPP_KEYWORD
)
26035 attr_ns
= ridpointers
[(int) token
->keyword
];
26036 else if (token
->flags
& NAMED_OP
)
26037 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26040 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26042 if (cxx_dialect
< cxx17
26043 && !in_system_header_at (input_location
))
26044 pedwarn (input_location
, 0,
26045 "attribute using prefix only available "
26046 "with -std=c++17 or -std=gnu++17");
26048 cp_lexer_consume_token (parser
->lexer
);
26049 cp_lexer_consume_token (parser
->lexer
);
26050 cp_lexer_consume_token (parser
->lexer
);
26053 attr_ns
= NULL_TREE
;
26056 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26058 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26059 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26060 cp_parser_skip_to_end_of_statement (parser
);
26062 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26063 when we are sure that we have actually parsed them. */
26064 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26070 /* Look for an alignment-specifier. */
26072 token
= cp_lexer_peek_token (parser
->lexer
);
26074 if (token
->type
!= CPP_KEYWORD
26075 || token
->keyword
!= RID_ALIGNAS
)
26078 cp_lexer_consume_token (parser
->lexer
);
26079 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26081 matching_parens parens
;
26082 if (!parens
.require_open (parser
))
26083 return error_mark_node
;
26085 cp_parser_parse_tentatively (parser
);
26086 alignas_expr
= cp_parser_type_id (parser
);
26088 if (!cp_parser_parse_definitely (parser
))
26090 alignas_expr
= cp_parser_assignment_expression (parser
);
26091 if (alignas_expr
== error_mark_node
)
26092 cp_parser_skip_to_end_of_statement (parser
);
26093 if (alignas_expr
== NULL_TREE
26094 || alignas_expr
== error_mark_node
)
26095 return alignas_expr
;
26098 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26099 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26101 /* Handle alignas (pack...). */
26102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26104 cp_lexer_consume_token (parser
->lexer
);
26105 alignas_expr
= make_pack_expansion (alignas_expr
);
26108 /* Something went wrong, so don't build the attribute. */
26109 if (alignas_expr
== error_mark_node
)
26110 return error_mark_node
;
26112 if (!parens
.require_close (parser
))
26113 return error_mark_node
;
26115 /* Build the C++-11 representation of an 'aligned'
26118 = build_tree_list (build_tree_list (gnu_identifier
,
26119 aligned_identifier
), alignas_expr
);
26125 /* Parse a standard C++-11 attribute-specifier-seq.
26127 attribute-specifier-seq:
26128 attribute-specifier-seq [opt] attribute-specifier
26132 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26134 tree attr_specs
= NULL_TREE
;
26135 tree attr_last
= NULL_TREE
;
26137 /* Don't create wrapper nodes within attributes: the
26138 handlers don't know how to handle them. */
26139 auto_suppress_location_wrappers sentinel
;
26143 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26144 if (attr_spec
== NULL_TREE
)
26146 if (attr_spec
== error_mark_node
)
26147 return error_mark_node
;
26150 TREE_CHAIN (attr_last
) = attr_spec
;
26152 attr_specs
= attr_last
= attr_spec
;
26153 attr_last
= tree_last (attr_last
);
26159 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26160 return index of the first token after balanced-token, or N on failure. */
26163 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26166 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26168 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26170 case CPP_PRAGMA_EOL
:
26171 if (!parser
->lexer
->in_pragma
)
26175 /* Ran out of tokens. */
26177 case CPP_OPEN_PAREN
:
26180 case CPP_OPEN_BRACE
:
26183 case CPP_OPEN_SQUARE
:
26186 case CPP_CLOSE_PAREN
:
26189 case CPP_CLOSE_BRACE
:
26192 case CPP_CLOSE_SQUARE
:
26198 while (nparens
|| nbraces
|| nsquares
);
26202 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26203 return index of the first token after the GNU attribute tokens, or N on
26207 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26211 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26212 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26213 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26216 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26219 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26226 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26227 next token), return index of the first token after the standard C++11
26228 attribute tokens, or N on failure. */
26231 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26235 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26236 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26238 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26241 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26245 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26246 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26248 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26259 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26260 as the next token), return index of the first token after the attribute
26261 tokens, or N on failure. */
26264 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26266 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26267 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26268 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26271 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26272 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26273 current value of the PEDANTIC flag, regardless of whether or not
26274 the `__extension__' keyword is present. The caller is responsible
26275 for restoring the value of the PEDANTIC flag. */
26278 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26280 /* Save the old value of the PEDANTIC flag. */
26281 *saved_pedantic
= pedantic
;
26283 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26285 /* Consume the `__extension__' token. */
26286 cp_lexer_consume_token (parser
->lexer
);
26287 /* We're not being pedantic while the `__extension__' keyword is
26297 /* Parse a label declaration.
26300 __label__ label-declarator-seq ;
26302 label-declarator-seq:
26303 identifier , label-declarator-seq
26307 cp_parser_label_declaration (cp_parser
* parser
)
26309 /* Look for the `__label__' keyword. */
26310 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26316 /* Look for an identifier. */
26317 identifier
= cp_parser_identifier (parser
);
26318 /* If we failed, stop. */
26319 if (identifier
== error_mark_node
)
26321 /* Declare it as a label. */
26322 finish_label_decl (identifier
);
26323 /* If the next token is a `;', stop. */
26324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26326 /* Look for the `,' separating the label declarations. */
26327 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26330 /* Look for the final `;'. */
26331 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26334 // -------------------------------------------------------------------------- //
26337 // Parse a requires clause.
26339 // requires-clause:
26340 // 'requires' logical-or-expression
26342 // The required logical-or-expression must be a constant expression. Note
26343 // that we don't check that the expression is constepxr here. We defer until
26344 // we analyze constraints and then, we only check atomic constraints.
26346 cp_parser_requires_clause (cp_parser
*parser
)
26348 // Parse the requires clause so that it is not automatically folded.
26349 ++processing_template_decl
;
26350 tree expr
= cp_parser_binary_expression (parser
, false, false,
26351 PREC_NOT_OPERATOR
, NULL
);
26352 if (check_for_bare_parameter_packs (expr
))
26353 expr
= error_mark_node
;
26354 --processing_template_decl
;
26358 // Optionally parse a requires clause:
26360 cp_parser_requires_clause_opt (cp_parser
*parser
)
26362 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26363 if (tok
->keyword
!= RID_REQUIRES
)
26365 if (!flag_concepts
&& tok
->type
== CPP_NAME
26366 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26368 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26369 "%<requires%> only available with -fconcepts");
26370 /* Parse and discard the requires-clause. */
26371 cp_lexer_consume_token (parser
->lexer
);
26372 cp_parser_requires_clause (parser
);
26376 cp_lexer_consume_token (parser
->lexer
);
26377 return cp_parser_requires_clause (parser
);
26381 /*---------------------------------------------------------------------------
26382 Requires expressions
26383 ---------------------------------------------------------------------------*/
26385 /* Parse a requires expression
26387 requirement-expression:
26388 'requires' requirement-parameter-list [opt] requirement-body */
26390 cp_parser_requires_expression (cp_parser
*parser
)
26392 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26393 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26395 /* A requires-expression shall appear only within a concept
26396 definition or a requires-clause.
26398 TODO: Implement this diagnostic correctly. */
26399 if (!processing_template_decl
)
26401 error_at (loc
, "a requires expression cannot appear outside a template");
26402 cp_parser_skip_to_end_of_statement (parser
);
26403 return error_mark_node
;
26408 /* Local parameters are delared as variables within the scope
26409 of the expression. They are not visible past the end of
26410 the expression. Expressions within the requires-expression
26411 are unevaluated. */
26412 struct scope_sentinel
26416 ++cp_unevaluated_operand
;
26417 begin_scope (sk_block
, NULL_TREE
);
26422 pop_bindings_and_leave_scope ();
26423 --cp_unevaluated_operand
;
26427 /* Parse the optional parameter list. */
26428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26430 parms
= cp_parser_requirement_parameter_list (parser
);
26431 if (parms
== error_mark_node
)
26432 return error_mark_node
;
26437 /* Parse the requirement body. */
26438 reqs
= cp_parser_requirement_body (parser
);
26439 if (reqs
== error_mark_node
)
26440 return error_mark_node
;
26443 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26445 grokparms (parms
, &parms
);
26446 return finish_requires_expr (parms
, reqs
);
26449 /* Parse a parameterized requirement.
26451 requirement-parameter-list:
26452 '(' parameter-declaration-clause ')' */
26454 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26456 matching_parens parens
;
26457 if (!parens
.require_open (parser
))
26458 return error_mark_node
;
26461 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26463 if (!parens
.require_close (parser
))
26464 return error_mark_node
;
26469 /* Parse the body of a requirement.
26472 '{' requirement-list '}' */
26474 cp_parser_requirement_body (cp_parser
*parser
)
26476 matching_braces braces
;
26477 if (!braces
.require_open (parser
))
26478 return error_mark_node
;
26480 tree reqs
= cp_parser_requirement_list (parser
);
26482 if (!braces
.require_close (parser
))
26483 return error_mark_node
;
26488 /* Parse a list of requirements.
26492 requirement-list ';' requirement[opt] */
26494 cp_parser_requirement_list (cp_parser
*parser
)
26496 tree result
= NULL_TREE
;
26499 tree req
= cp_parser_requirement (parser
);
26500 if (req
== error_mark_node
)
26501 return error_mark_node
;
26503 result
= tree_cons (NULL_TREE
, req
, result
);
26505 /* If we see a semi-colon, consume it. */
26506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26507 cp_lexer_consume_token (parser
->lexer
);
26509 /* Stop processing at the end of the list. */
26510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26514 /* Reverse the order of requirements so they are analyzed in
26515 declaration order. */
26516 return nreverse (result
);
26519 /* Parse a syntactic requirement or type requirement.
26523 compound-requirement
26525 nested-requirement */
26527 cp_parser_requirement (cp_parser
*parser
)
26529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26530 return cp_parser_compound_requirement (parser
);
26531 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26532 return cp_parser_type_requirement (parser
);
26533 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26534 return cp_parser_nested_requirement (parser
);
26536 return cp_parser_simple_requirement (parser
);
26539 /* Parse a simple requirement.
26541 simple-requirement:
26544 cp_parser_simple_requirement (cp_parser
*parser
)
26546 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26547 if (!expr
|| expr
== error_mark_node
)
26548 return error_mark_node
;
26550 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26551 return error_mark_node
;
26553 return finish_simple_requirement (expr
);
26556 /* Parse a type requirement
26559 nested-name-specifier [opt] required-type-name ';'
26561 required-type-name:
26563 'template' [opt] simple-template-id */
26565 cp_parser_type_requirement (cp_parser
*parser
)
26567 cp_lexer_consume_token (parser
->lexer
);
26569 // Save the scope before parsing name specifiers.
26570 tree saved_scope
= parser
->scope
;
26571 tree saved_object_scope
= parser
->object_scope
;
26572 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26573 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26574 cp_parser_nested_name_specifier_opt (parser
,
26575 /*typename_keyword_p=*/true,
26576 /*check_dependency_p=*/false,
26578 /*is_declaration=*/false);
26581 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26583 cp_lexer_consume_token (parser
->lexer
);
26584 type
= cp_parser_template_id (parser
,
26585 /*template_keyword_p=*/true,
26586 /*check_dependency=*/false,
26587 /*tag_type=*/none_type
,
26588 /*is_declaration=*/false);
26589 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26590 /*complain=*/tf_error
);
26593 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26595 if (TREE_CODE (type
) == TYPE_DECL
)
26596 type
= TREE_TYPE (type
);
26598 parser
->scope
= saved_scope
;
26599 parser
->object_scope
= saved_object_scope
;
26600 parser
->qualifying_scope
= saved_qualifying_scope
;
26602 if (type
== error_mark_node
)
26603 cp_parser_skip_to_end_of_statement (parser
);
26605 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26606 return error_mark_node
;
26607 if (type
== error_mark_node
)
26608 return error_mark_node
;
26610 return finish_type_requirement (type
);
26613 /* Parse a compound requirement
26615 compound-requirement:
26616 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26618 cp_parser_compound_requirement (cp_parser
*parser
)
26620 /* Parse an expression enclosed in '{ }'s. */
26621 matching_braces braces
;
26622 if (!braces
.require_open (parser
))
26623 return error_mark_node
;
26625 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26626 if (!expr
|| expr
== error_mark_node
)
26627 return error_mark_node
;
26629 if (!braces
.require_close (parser
))
26630 return error_mark_node
;
26632 /* Parse the optional noexcept. */
26633 bool noexcept_p
= false;
26634 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26636 cp_lexer_consume_token (parser
->lexer
);
26640 /* Parse the optional trailing return type. */
26641 tree type
= NULL_TREE
;
26642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26644 cp_lexer_consume_token (parser
->lexer
);
26645 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26646 parser
->in_result_type_constraint_p
= true;
26647 type
= cp_parser_trailing_type_id (parser
);
26648 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26649 if (type
== error_mark_node
)
26650 return error_mark_node
;
26653 return finish_compound_requirement (expr
, type
, noexcept_p
);
26656 /* Parse a nested requirement. This is the same as a requires clause.
26658 nested-requirement:
26661 cp_parser_nested_requirement (cp_parser
*parser
)
26663 cp_lexer_consume_token (parser
->lexer
);
26664 tree req
= cp_parser_requires_clause (parser
);
26665 if (req
== error_mark_node
)
26666 return error_mark_node
;
26667 return finish_nested_requirement (req
);
26670 /* Support Functions */
26672 /* Return the appropriate prefer_type argument for lookup_name_real based on
26673 tag_type and template_mem_access. */
26676 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26678 /* DR 141: When looking in the current enclosing context for a template-name
26679 after -> or ., only consider class templates. */
26680 if (template_mem_access
)
26684 case none_type
: return 0; // No preference.
26685 case scope_type
: return 1; // Type or namespace.
26686 default: return 2; // Type only.
26690 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26691 NAME should have one of the representations used for an
26692 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26693 is returned. If PARSER->SCOPE is a dependent type, then a
26694 SCOPE_REF is returned.
26696 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26697 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26698 was formed. Abstractly, such entities should not be passed to this
26699 function, because they do not need to be looked up, but it is
26700 simpler to check for this special case here, rather than at the
26703 In cases not explicitly covered above, this function returns a
26704 DECL, OVERLOAD, or baselink representing the result of the lookup.
26705 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26708 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26709 (e.g., "struct") that was used. In that case bindings that do not
26710 refer to types are ignored.
26712 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26715 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26718 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26721 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26722 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26723 NULL_TREE otherwise. */
26726 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26727 enum tag_types tag_type
,
26730 bool check_dependency
,
26731 tree
*ambiguous_decls
,
26732 location_t name_location
)
26735 tree object_type
= parser
->context
->object_type
;
26737 /* Assume that the lookup will be unambiguous. */
26738 if (ambiguous_decls
)
26739 *ambiguous_decls
= NULL_TREE
;
26741 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26742 no longer valid. Note that if we are parsing tentatively, and
26743 the parse fails, OBJECT_TYPE will be automatically restored. */
26744 parser
->context
->object_type
= NULL_TREE
;
26746 if (name
== error_mark_node
)
26747 return error_mark_node
;
26749 /* A template-id has already been resolved; there is no lookup to
26751 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26753 if (BASELINK_P (name
))
26755 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26756 == TEMPLATE_ID_EXPR
);
26760 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26761 it should already have been checked to make sure that the name
26762 used matches the type being destroyed. */
26763 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26767 /* Figure out to which type this destructor applies. */
26769 type
= parser
->scope
;
26770 else if (object_type
)
26771 type
= object_type
;
26773 type
= current_class_type
;
26774 /* If that's not a class type, there is no destructor. */
26775 if (!type
|| !CLASS_TYPE_P (type
))
26776 return error_mark_node
;
26778 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26779 lazily_declare_fn (sfk_destructor
, type
);
26781 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26784 return error_mark_node
;
26787 /* By this point, the NAME should be an ordinary identifier. If
26788 the id-expression was a qualified name, the qualifying scope is
26789 stored in PARSER->SCOPE at this point. */
26790 gcc_assert (identifier_p (name
));
26792 /* Perform the lookup. */
26797 if (parser
->scope
== error_mark_node
)
26798 return error_mark_node
;
26800 /* If the SCOPE is dependent, the lookup must be deferred until
26801 the template is instantiated -- unless we are explicitly
26802 looking up names in uninstantiated templates. Even then, we
26803 cannot look up the name if the scope is not a class type; it
26804 might, for example, be a template type parameter. */
26805 dependent_p
= (TYPE_P (parser
->scope
)
26806 && dependent_scope_p (parser
->scope
));
26807 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26809 /* Defer lookup. */
26810 decl
= error_mark_node
;
26813 tree pushed_scope
= NULL_TREE
;
26815 /* If PARSER->SCOPE is a dependent type, then it must be a
26816 class type, and we must not be checking dependencies;
26817 otherwise, we would have processed this lookup above. So
26818 that PARSER->SCOPE is not considered a dependent base by
26819 lookup_member, we must enter the scope here. */
26821 pushed_scope
= push_scope (parser
->scope
);
26823 /* If the PARSER->SCOPE is a template specialization, it
26824 may be instantiated during name lookup. In that case,
26825 errors may be issued. Even if we rollback the current
26826 tentative parse, those errors are valid. */
26827 decl
= lookup_qualified_name (parser
->scope
, name
,
26828 prefer_type_arg (tag_type
),
26829 /*complain=*/true);
26831 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26832 lookup result and the nested-name-specifier nominates a class C:
26833 * if the name specified after the nested-name-specifier, when
26834 looked up in C, is the injected-class-name of C (Clause 9), or
26835 * if the name specified after the nested-name-specifier is the
26836 same as the identifier or the simple-template-id's template-
26837 name in the last component of the nested-name-specifier,
26838 the name is instead considered to name the constructor of
26839 class C. [ Note: for example, the constructor is not an
26840 acceptable lookup result in an elaborated-type-specifier so
26841 the constructor would not be used in place of the
26842 injected-class-name. --end note ] Such a constructor name
26843 shall be used only in the declarator-id of a declaration that
26844 names a constructor or in a using-declaration. */
26845 if (tag_type
== none_type
26846 && DECL_SELF_REFERENCE_P (decl
)
26847 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26848 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26849 prefer_type_arg (tag_type
),
26850 /*complain=*/true);
26852 /* If we have a single function from a using decl, pull it out. */
26853 if (TREE_CODE (decl
) == OVERLOAD
26854 && !really_overloaded_fn (decl
))
26855 decl
= OVL_FUNCTION (decl
);
26858 pop_scope (pushed_scope
);
26861 /* If the scope is a dependent type and either we deferred lookup or
26862 we did lookup but didn't find the name, rememeber the name. */
26863 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26864 && dependent_type_p (parser
->scope
))
26870 /* The resolution to Core Issue 180 says that `struct
26871 A::B' should be considered a type-name, even if `A'
26873 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26874 /*complain=*/tf_error
);
26875 if (type
!= error_mark_node
)
26876 decl
= TYPE_NAME (type
);
26878 else if (is_template
26879 && (cp_parser_next_token_ends_template_argument_p (parser
)
26880 || cp_lexer_next_token_is (parser
->lexer
,
26882 decl
= make_unbound_class_template (parser
->scope
,
26884 /*complain=*/tf_error
);
26886 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26887 parser
->scope
, name
,
26890 parser
->qualifying_scope
= parser
->scope
;
26891 parser
->object_scope
= NULL_TREE
;
26893 else if (object_type
)
26895 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26896 OBJECT_TYPE is not a class. */
26897 if (CLASS_TYPE_P (object_type
))
26898 /* If the OBJECT_TYPE is a template specialization, it may
26899 be instantiated during name lookup. In that case, errors
26900 may be issued. Even if we rollback the current tentative
26901 parse, those errors are valid. */
26902 decl
= lookup_member (object_type
,
26905 prefer_type_arg (tag_type
),
26906 tf_warning_or_error
);
26911 /* Look it up in the enclosing context. DR 141: When looking for a
26912 template-name after -> or ., only consider class templates. */
26913 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26915 /*block_p=*/true, is_namespace
, 0);
26916 if (object_type
== unknown_type_node
)
26917 /* The object is type-dependent, so we can't look anything up; we used
26918 this to get the DR 141 behavior. */
26919 object_type
= NULL_TREE
;
26920 parser
->object_scope
= object_type
;
26921 parser
->qualifying_scope
= NULL_TREE
;
26925 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26927 /*block_p=*/true, is_namespace
, 0);
26928 parser
->qualifying_scope
= NULL_TREE
;
26929 parser
->object_scope
= NULL_TREE
;
26932 /* If the lookup failed, let our caller know. */
26933 if (!decl
|| decl
== error_mark_node
)
26934 return error_mark_node
;
26936 /* Pull out the template from an injected-class-name (or multiple). */
26938 decl
= maybe_get_template_decl_from_type_decl (decl
);
26940 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26941 if (TREE_CODE (decl
) == TREE_LIST
)
26943 if (ambiguous_decls
)
26944 *ambiguous_decls
= decl
;
26945 /* The error message we have to print is too complicated for
26946 cp_parser_error, so we incorporate its actions directly. */
26947 if (!cp_parser_simulate_error (parser
))
26949 error_at (name_location
, "reference to %qD is ambiguous",
26951 print_candidates (decl
);
26953 return error_mark_node
;
26956 gcc_assert (DECL_P (decl
)
26957 || TREE_CODE (decl
) == OVERLOAD
26958 || TREE_CODE (decl
) == SCOPE_REF
26959 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26960 || BASELINK_P (decl
));
26962 /* If we have resolved the name of a member declaration, check to
26963 see if the declaration is accessible. When the name resolves to
26964 set of overloaded functions, accessibility is checked when
26965 overload resolution is done.
26967 During an explicit instantiation, access is not checked at all,
26968 as per [temp.explicit]. */
26970 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26972 maybe_record_typedef_use (decl
);
26974 return cp_expr (decl
, name_location
);
26977 /* Like cp_parser_lookup_name, but for use in the typical case where
26978 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26979 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26982 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26984 return cp_parser_lookup_name (parser
, name
,
26986 /*is_template=*/false,
26987 /*is_namespace=*/false,
26988 /*check_dependency=*/true,
26989 /*ambiguous_decls=*/NULL
,
26993 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26994 the current context, return the TYPE_DECL. If TAG_NAME_P is
26995 true, the DECL indicates the class being defined in a class-head,
26996 or declared in an elaborated-type-specifier.
26998 Otherwise, return DECL. */
27001 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27003 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27004 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27007 template <typename T> struct B;
27010 template <typename T> struct A::B {};
27012 Similarly, in an elaborated-type-specifier:
27014 namespace N { struct X{}; }
27017 template <typename T> friend struct N::X;
27020 However, if the DECL refers to a class type, and we are in
27021 the scope of the class, then the name lookup automatically
27022 finds the TYPE_DECL created by build_self_reference rather
27023 than a TEMPLATE_DECL. For example, in:
27025 template <class T> struct S {
27029 there is no need to handle such case. */
27031 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27032 return DECL_TEMPLATE_RESULT (decl
);
27037 /* If too many, or too few, template-parameter lists apply to the
27038 declarator, issue an error message. Returns TRUE if all went well,
27039 and FALSE otherwise. */
27042 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27043 cp_declarator
*declarator
,
27044 location_t declarator_location
)
27046 switch (declarator
->kind
)
27050 unsigned num_templates
= 0;
27051 tree scope
= declarator
->u
.id
.qualifying_scope
;
27052 bool template_id_p
= false;
27055 num_templates
= num_template_headers_for_class (scope
);
27056 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27057 == TEMPLATE_ID_EXPR
)
27059 /* If the DECLARATOR has the form `X<y>' then it uses one
27060 additional level of template parameters. */
27062 template_id_p
= true;
27065 return cp_parser_check_template_parameters
27066 (parser
, num_templates
, template_id_p
, declarator_location
,
27073 case cdk_reference
:
27075 return (cp_parser_check_declarator_template_parameters
27076 (parser
, declarator
->declarator
, declarator_location
));
27083 gcc_unreachable ();
27088 /* NUM_TEMPLATES were used in the current declaration. If that is
27089 invalid, return FALSE and issue an error messages. Otherwise,
27090 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27091 declarator and we can print more accurate diagnostics. */
27094 cp_parser_check_template_parameters (cp_parser
* parser
,
27095 unsigned num_templates
,
27096 bool template_id_p
,
27097 location_t location
,
27098 cp_declarator
*declarator
)
27100 /* If there are the same number of template classes and parameter
27101 lists, that's OK. */
27102 if (parser
->num_template_parameter_lists
== num_templates
)
27104 /* If there are more, but only one more, and the name ends in an identifier,
27105 then we are declaring a primary template. That's OK too. */
27107 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27109 /* If there are more template classes than parameter lists, we have
27112 template <class T> void S<T>::R<T>::f (); */
27113 if (parser
->num_template_parameter_lists
< num_templates
)
27115 if (declarator
&& !current_function_decl
)
27116 error_at (location
, "specializing member %<%T::%E%> "
27117 "requires %<template<>%> syntax",
27118 declarator
->u
.id
.qualifying_scope
,
27119 declarator
->u
.id
.unqualified_name
);
27120 else if (declarator
)
27121 error_at (location
, "invalid declaration of %<%T::%E%>",
27122 declarator
->u
.id
.qualifying_scope
,
27123 declarator
->u
.id
.unqualified_name
);
27125 error_at (location
, "too few template-parameter-lists");
27128 /* Otherwise, there are too many template parameter lists. We have
27131 template <class T> template <class U> void S::f(); */
27132 error_at (location
, "too many template-parameter-lists");
27136 /* Parse an optional `::' token indicating that the following name is
27137 from the global namespace. If so, PARSER->SCOPE is set to the
27138 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27139 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27140 Returns the new value of PARSER->SCOPE, if the `::' token is
27141 present, and NULL_TREE otherwise. */
27144 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27148 /* Peek at the next token. */
27149 token
= cp_lexer_peek_token (parser
->lexer
);
27150 /* If we're looking at a `::' token then we're starting from the
27151 global namespace, not our current location. */
27152 if (token
->type
== CPP_SCOPE
)
27154 /* Consume the `::' token. */
27155 cp_lexer_consume_token (parser
->lexer
);
27156 /* Set the SCOPE so that we know where to start the lookup. */
27157 parser
->scope
= global_namespace
;
27158 parser
->qualifying_scope
= global_namespace
;
27159 parser
->object_scope
= NULL_TREE
;
27161 return parser
->scope
;
27163 else if (!current_scope_valid_p
)
27165 parser
->scope
= NULL_TREE
;
27166 parser
->qualifying_scope
= NULL_TREE
;
27167 parser
->object_scope
= NULL_TREE
;
27173 /* Returns TRUE if the upcoming token sequence is the start of a
27174 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27175 declarator is preceded by the `friend' specifier. */
27178 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
27180 bool constructor_p
;
27181 bool outside_class_specifier_p
;
27182 tree nested_name_specifier
;
27183 cp_token
*next_token
;
27185 /* The common case is that this is not a constructor declarator, so
27186 try to avoid doing lots of work if at all possible. It's not
27187 valid declare a constructor at function scope. */
27188 if (parser
->in_function_body
)
27190 /* And only certain tokens can begin a constructor declarator. */
27191 next_token
= cp_lexer_peek_token (parser
->lexer
);
27192 if (next_token
->type
!= CPP_NAME
27193 && next_token
->type
!= CPP_SCOPE
27194 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27195 && next_token
->type
!= CPP_TEMPLATE_ID
)
27198 /* Parse tentatively; we are going to roll back all of the tokens
27200 cp_parser_parse_tentatively (parser
);
27201 /* Assume that we are looking at a constructor declarator. */
27202 constructor_p
= true;
27204 /* Look for the optional `::' operator. */
27205 cp_parser_global_scope_opt (parser
,
27206 /*current_scope_valid_p=*/false);
27207 /* Look for the nested-name-specifier. */
27208 nested_name_specifier
27209 = (cp_parser_nested_name_specifier_opt (parser
,
27210 /*typename_keyword_p=*/false,
27211 /*check_dependency_p=*/false,
27213 /*is_declaration=*/false));
27215 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27216 if (nested_name_specifier
27217 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27219 tree s
= resolve_typename_type (nested_name_specifier
,
27220 /*only_current_p=*/false);
27221 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27222 nested_name_specifier
= s
;
27225 outside_class_specifier_p
= (!at_class_scope_p ()
27226 || !TYPE_BEING_DEFINED (current_class_type
)
27229 /* Outside of a class-specifier, there must be a
27230 nested-name-specifier. Except in C++17 mode, where we
27231 might be declaring a guiding declaration. */
27232 if (!nested_name_specifier
&& outside_class_specifier_p
27233 && cxx_dialect
< cxx17
)
27234 constructor_p
= false;
27235 else if (nested_name_specifier
== error_mark_node
)
27236 constructor_p
= false;
27238 /* If we have a class scope, this is easy; DR 147 says that S::S always
27239 names the constructor, and no other qualified name could. */
27240 if (constructor_p
&& nested_name_specifier
27241 && CLASS_TYPE_P (nested_name_specifier
))
27243 tree id
= cp_parser_unqualified_id (parser
,
27244 /*template_keyword_p=*/false,
27245 /*check_dependency_p=*/false,
27246 /*declarator_p=*/true,
27247 /*optional_p=*/false);
27248 if (is_overloaded_fn (id
))
27249 id
= DECL_NAME (get_first_fn (id
));
27250 if (!constructor_name_p (id
, nested_name_specifier
))
27251 constructor_p
= false;
27253 /* If we still think that this might be a constructor-declarator,
27254 look for a class-name. */
27255 else if (constructor_p
)
27259 template <typename T> struct S {
27263 we must recognize that the nested `S' names a class. */
27264 if (cxx_dialect
>= cxx17
)
27265 cp_parser_parse_tentatively (parser
);
27268 type_decl
= cp_parser_class_name (parser
,
27269 /*typename_keyword_p=*/false,
27270 /*template_keyword_p=*/false,
27272 /*check_dependency_p=*/false,
27273 /*class_head_p=*/false,
27274 /*is_declaration=*/false);
27276 if (cxx_dialect
>= cxx17
27277 && !cp_parser_parse_definitely (parser
))
27279 type_decl
= NULL_TREE
;
27280 tree tmpl
= cp_parser_template_name (parser
,
27281 /*template_keyword*/false,
27282 /*check_dependency_p*/false,
27283 /*is_declaration*/false,
27285 /*is_identifier*/NULL
);
27286 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27287 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27288 /* It's a deduction guide, return true. */;
27290 cp_parser_simulate_error (parser
);
27293 /* If there was no class-name, then this is not a constructor.
27294 Otherwise, if we are in a class-specifier and we aren't
27295 handling a friend declaration, check that its type matches
27296 current_class_type (c++/38313). Note: error_mark_node
27297 is left alone for error recovery purposes. */
27298 constructor_p
= (!cp_parser_error_occurred (parser
)
27299 && (outside_class_specifier_p
27300 || type_decl
== NULL_TREE
27301 || type_decl
== error_mark_node
27302 || same_type_p (current_class_type
,
27303 TREE_TYPE (type_decl
))));
27305 /* If we're still considering a constructor, we have to see a `(',
27306 to begin the parameter-declaration-clause, followed by either a
27307 `)', an `...', or a decl-specifier. We need to check for a
27308 type-specifier to avoid being fooled into thinking that:
27312 is a constructor. (It is actually a function named `f' that
27313 takes one parameter (of type `int') and returns a value of type
27316 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27317 constructor_p
= false;
27320 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27321 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27322 /* A parameter declaration begins with a decl-specifier,
27323 which is either the "attribute" keyword, a storage class
27324 specifier, or (usually) a type-specifier. */
27325 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27328 tree pushed_scope
= NULL_TREE
;
27329 unsigned saved_num_template_parameter_lists
;
27331 /* Names appearing in the type-specifier should be looked up
27332 in the scope of the class. */
27333 if (current_class_type
)
27335 else if (type_decl
)
27337 type
= TREE_TYPE (type_decl
);
27338 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27340 type
= resolve_typename_type (type
,
27341 /*only_current_p=*/false);
27342 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27344 cp_parser_abort_tentative_parse (parser
);
27348 pushed_scope
= push_scope (type
);
27351 /* Inside the constructor parameter list, surrounding
27352 template-parameter-lists do not apply. */
27353 saved_num_template_parameter_lists
27354 = parser
->num_template_parameter_lists
;
27355 parser
->num_template_parameter_lists
= 0;
27357 /* Look for the type-specifier. */
27358 cp_parser_type_specifier (parser
,
27359 CP_PARSER_FLAGS_NONE
,
27360 /*decl_specs=*/NULL
,
27361 /*is_declarator=*/true,
27362 /*declares_class_or_enum=*/NULL
,
27363 /*is_cv_qualifier=*/NULL
);
27365 parser
->num_template_parameter_lists
27366 = saved_num_template_parameter_lists
;
27368 /* Leave the scope of the class. */
27370 pop_scope (pushed_scope
);
27372 constructor_p
= !cp_parser_error_occurred (parser
);
27376 /* We did not really want to consume any tokens. */
27377 cp_parser_abort_tentative_parse (parser
);
27379 return constructor_p
;
27382 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27383 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27384 they must be performed once we are in the scope of the function.
27386 Returns the function defined. */
27389 cp_parser_function_definition_from_specifiers_and_declarator
27390 (cp_parser
* parser
,
27391 cp_decl_specifier_seq
*decl_specifiers
,
27393 const cp_declarator
*declarator
)
27398 /* Begin the function-definition. */
27399 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27401 /* The things we're about to see are not directly qualified by any
27402 template headers we've seen thus far. */
27403 reset_specialization ();
27405 /* If there were names looked up in the decl-specifier-seq that we
27406 did not check, check them now. We must wait until we are in the
27407 scope of the function to perform the checks, since the function
27408 might be a friend. */
27409 perform_deferred_access_checks (tf_warning_or_error
);
27413 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27414 parser
->omp_declare_simd
= NULL
;
27415 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27416 parser
->oacc_routine
= NULL
;
27421 /* Skip the entire function. */
27422 cp_parser_skip_to_end_of_block_or_statement (parser
);
27423 fn
= error_mark_node
;
27425 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27427 /* Seen already, skip it. An error message has already been output. */
27428 cp_parser_skip_to_end_of_block_or_statement (parser
);
27429 fn
= current_function_decl
;
27430 current_function_decl
= NULL_TREE
;
27431 /* If this is a function from a class, pop the nested class. */
27432 if (current_class_name
)
27433 pop_nested_class ();
27438 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27439 tv
= TV_PARSE_INLINE
;
27441 tv
= TV_PARSE_FUNC
;
27443 fn
= cp_parser_function_definition_after_declarator (parser
,
27444 /*inline_p=*/false);
27451 /* Parse the part of a function-definition that follows the
27452 declarator. INLINE_P is TRUE iff this function is an inline
27453 function defined within a class-specifier.
27455 Returns the function defined. */
27458 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27462 bool saved_in_unbraced_linkage_specification_p
;
27463 bool saved_in_function_body
;
27464 unsigned saved_num_template_parameter_lists
;
27466 bool fully_implicit_function_template_p
27467 = parser
->fully_implicit_function_template_p
;
27468 parser
->fully_implicit_function_template_p
= false;
27469 tree implicit_template_parms
27470 = parser
->implicit_template_parms
;
27471 parser
->implicit_template_parms
= 0;
27472 cp_binding_level
* implicit_template_scope
27473 = parser
->implicit_template_scope
;
27474 parser
->implicit_template_scope
= 0;
27476 saved_in_function_body
= parser
->in_function_body
;
27477 parser
->in_function_body
= true;
27478 /* If the next token is `return', then the code may be trying to
27479 make use of the "named return value" extension that G++ used to
27481 token
= cp_lexer_peek_token (parser
->lexer
);
27482 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27484 /* Consume the `return' keyword. */
27485 cp_lexer_consume_token (parser
->lexer
);
27486 /* Look for the identifier that indicates what value is to be
27488 cp_parser_identifier (parser
);
27489 /* Issue an error message. */
27490 error_at (token
->location
,
27491 "named return values are no longer supported");
27492 /* Skip tokens until we reach the start of the function body. */
27495 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27496 if (token
->type
== CPP_OPEN_BRACE
27497 || token
->type
== CPP_EOF
27498 || token
->type
== CPP_PRAGMA_EOL
)
27500 cp_lexer_consume_token (parser
->lexer
);
27503 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27504 anything declared inside `f'. */
27505 saved_in_unbraced_linkage_specification_p
27506 = parser
->in_unbraced_linkage_specification_p
;
27507 parser
->in_unbraced_linkage_specification_p
= false;
27508 /* Inside the function, surrounding template-parameter-lists do not
27510 saved_num_template_parameter_lists
27511 = parser
->num_template_parameter_lists
;
27512 parser
->num_template_parameter_lists
= 0;
27514 /* If the next token is `try', `__transaction_atomic', or
27515 `__transaction_relaxed`, then we are looking at either function-try-block
27516 or function-transaction-block. Note that all of these include the
27518 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27519 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27520 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27521 RID_TRANSACTION_RELAXED
))
27522 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27523 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27524 cp_parser_function_try_block (parser
);
27526 cp_parser_ctor_initializer_opt_and_function_body
27527 (parser
, /*in_function_try_block=*/false);
27529 /* Finish the function. */
27530 fn
= finish_function (inline_p
);
27531 /* Generate code for it, if necessary. */
27532 expand_or_defer_fn (fn
);
27533 /* Restore the saved values. */
27534 parser
->in_unbraced_linkage_specification_p
27535 = saved_in_unbraced_linkage_specification_p
;
27536 parser
->num_template_parameter_lists
27537 = saved_num_template_parameter_lists
;
27538 parser
->in_function_body
= saved_in_function_body
;
27540 parser
->fully_implicit_function_template_p
27541 = fully_implicit_function_template_p
;
27542 parser
->implicit_template_parms
27543 = implicit_template_parms
;
27544 parser
->implicit_template_scope
27545 = implicit_template_scope
;
27547 if (parser
->fully_implicit_function_template_p
)
27548 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27553 /* Parse a template-declaration body (following argument list). */
27556 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27557 tree parameter_list
,
27560 tree decl
= NULL_TREE
;
27561 bool friend_p
= false;
27563 /* We just processed one more parameter list. */
27564 ++parser
->num_template_parameter_lists
;
27566 /* Get the deferred access checks from the parameter list. These
27567 will be checked once we know what is being declared, as for a
27568 member template the checks must be performed in the scope of the
27569 class containing the member. */
27570 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27572 /* Tentatively parse for a new template parameter list, which can either be
27573 the template keyword or a template introduction. */
27574 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27576 else if (cxx_dialect
>= cxx11
27577 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27578 decl
= cp_parser_alias_declaration (parser
);
27581 /* There are no access checks when parsing a template, as we do not
27582 know if a specialization will be a friend. */
27583 push_deferring_access_checks (dk_no_check
);
27584 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27585 decl
= cp_parser_single_declaration (parser
,
27588 /*explicit_specialization_p=*/false,
27590 pop_deferring_access_checks ();
27592 /* If this is a member template declaration, let the front
27594 if (member_p
&& !friend_p
&& decl
)
27596 if (TREE_CODE (decl
) == TYPE_DECL
)
27597 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27599 decl
= finish_member_template_decl (decl
);
27601 else if (friend_p
&& decl
27602 && DECL_DECLARES_TYPE_P (decl
))
27603 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27604 /*complain=*/true);
27606 /* We are done with the current parameter list. */
27607 --parser
->num_template_parameter_lists
;
27609 pop_deferring_access_checks ();
27612 finish_template_decl (parameter_list
);
27614 /* Check the template arguments for a literal operator template. */
27616 && DECL_DECLARES_FUNCTION_P (decl
)
27617 && UDLIT_OPER_P (DECL_NAME (decl
)))
27620 if (parameter_list
== NULL_TREE
)
27624 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27625 if (num_parms
== 1)
27627 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27628 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27629 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27630 /* OK, C++20 string literal operator template. We don't need
27631 to warn in lower dialects here because we will have already
27632 warned about the template parameter. */;
27633 else if (TREE_TYPE (parm
) != char_type_node
27634 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27637 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27639 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27640 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27641 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27642 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27643 if (parm
== error_mark_node
27644 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27645 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27648 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27649 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27650 "ISO C++ did not adopt string literal operator templa"
27651 "tes taking an argument pack of characters");
27658 if (cxx_dialect
> cxx17
)
27659 error ("literal operator template %qD has invalid parameter list;"
27660 " Expected non-type template parameter pack <char...> "
27661 " or single non-type parameter of class type",
27664 error ("literal operator template %qD has invalid parameter list."
27665 " Expected non-type template parameter pack <char...>",
27670 /* Register member declarations. */
27671 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27672 finish_member_declaration (decl
);
27673 /* If DECL is a function template, we must return to parse it later.
27674 (Even though there is no definition, there might be default
27675 arguments that need handling.) */
27676 if (member_p
&& decl
27677 && DECL_DECLARES_FUNCTION_P (decl
))
27678 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27681 /* Parse a template introduction header for a template-declaration. Returns
27682 false if tentative parse fails. */
27685 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27687 cp_parser_parse_tentatively (parser
);
27689 tree saved_scope
= parser
->scope
;
27690 tree saved_object_scope
= parser
->object_scope
;
27691 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27693 /* Look for the optional `::' operator. */
27694 cp_parser_global_scope_opt (parser
,
27695 /*current_scope_valid_p=*/false);
27696 /* Look for the nested-name-specifier. */
27697 cp_parser_nested_name_specifier_opt (parser
,
27698 /*typename_keyword_p=*/false,
27699 /*check_dependency_p=*/true,
27701 /*is_declaration=*/false);
27703 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27704 tree concept_name
= cp_parser_identifier (parser
);
27706 /* Look up the concept for which we will be matching
27707 template parameters. */
27708 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27710 parser
->scope
= saved_scope
;
27711 parser
->object_scope
= saved_object_scope
;
27712 parser
->qualifying_scope
= saved_qualifying_scope
;
27714 if (concept_name
== error_mark_node
)
27715 cp_parser_simulate_error (parser
);
27717 /* Look for opening brace for introduction. */
27718 matching_braces braces
;
27719 braces
.require_open (parser
);
27721 if (!cp_parser_parse_definitely (parser
))
27724 push_deferring_access_checks (dk_deferred
);
27726 /* Build vector of placeholder parameters and grab
27727 matching identifiers. */
27728 tree introduction_list
= cp_parser_introduction_list (parser
);
27730 /* Look for closing brace for introduction. */
27731 if (!braces
.require_close (parser
))
27734 /* The introduction-list shall not be empty. */
27735 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27738 /* In cp_parser_introduction_list we have already issued an error. */
27742 if (tmpl_decl
== error_mark_node
)
27744 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27749 /* Build and associate the constraint. */
27750 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27751 if (parms
&& parms
!= error_mark_node
)
27753 cp_parser_template_declaration_after_parameters (parser
, parms
,
27758 error_at (token
->location
, "no matching concept for template-introduction");
27762 /* Parse a normal template-declaration following the template keyword. */
27765 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27767 tree parameter_list
;
27768 bool need_lang_pop
;
27769 location_t location
= input_location
;
27771 /* Look for the `<' token. */
27772 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27774 if (at_class_scope_p () && current_function_decl
)
27776 /* 14.5.2.2 [temp.mem]
27778 A local class shall not have member templates. */
27779 error_at (location
,
27780 "invalid declaration of member template in local class");
27781 cp_parser_skip_to_end_of_block_or_statement (parser
);
27786 A template ... shall not have C linkage. */
27787 if (current_lang_name
== lang_name_c
)
27789 error_at (location
, "template with C linkage");
27790 maybe_show_extern_c_location ();
27791 /* Give it C++ linkage to avoid confusing other parts of the
27793 push_lang_context (lang_name_cplusplus
);
27794 need_lang_pop
= true;
27797 need_lang_pop
= false;
27799 /* We cannot perform access checks on the template parameter
27800 declarations until we know what is being declared, just as we
27801 cannot check the decl-specifier list. */
27802 push_deferring_access_checks (dk_deferred
);
27804 /* If the next token is `>', then we have an invalid
27805 specialization. Rather than complain about an invalid template
27806 parameter, issue an error message here. */
27807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27809 cp_parser_error (parser
, "invalid explicit specialization");
27810 begin_specialization ();
27811 parameter_list
= NULL_TREE
;
27815 /* Parse the template parameters. */
27816 parameter_list
= cp_parser_template_parameter_list (parser
);
27819 /* Look for the `>'. */
27820 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27822 /* Manage template requirements */
27825 tree reqs
= get_shorthand_constraints (current_template_parms
);
27826 if (tree r
= cp_parser_requires_clause_opt (parser
))
27827 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27828 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27831 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27834 /* For the erroneous case of a template with C linkage, we pushed an
27835 implicit C++ linkage scope; exit that scope now. */
27837 pop_lang_context ();
27840 /* Parse a template-declaration, assuming that the `export' (and
27841 `extern') keywords, if present, has already been scanned. MEMBER_P
27842 is as for cp_parser_template_declaration. */
27845 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27847 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27849 cp_lexer_consume_token (parser
->lexer
);
27850 cp_parser_explicit_template_declaration (parser
, member_p
);
27853 else if (flag_concepts
)
27854 return cp_parser_template_introduction (parser
, member_p
);
27859 /* Perform the deferred access checks from a template-parameter-list.
27860 CHECKS is a TREE_LIST of access checks, as returned by
27861 get_deferred_access_checks. */
27864 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27866 ++processing_template_parmlist
;
27867 perform_access_checks (checks
, tf_warning_or_error
);
27868 --processing_template_parmlist
;
27871 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27872 `function-definition' sequence that follows a template header.
27873 If MEMBER_P is true, this declaration appears in a class scope.
27875 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27876 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27879 cp_parser_single_declaration (cp_parser
* parser
,
27880 vec
<deferred_access_check
, va_gc
> *checks
,
27882 bool explicit_specialization_p
,
27885 int declares_class_or_enum
;
27886 tree decl
= NULL_TREE
;
27887 cp_decl_specifier_seq decl_specifiers
;
27888 bool function_definition_p
= false;
27889 cp_token
*decl_spec_token_start
;
27891 /* This function is only used when processing a template
27893 gcc_assert (innermost_scope_kind () == sk_template_parms
27894 || innermost_scope_kind () == sk_template_spec
);
27896 /* Defer access checks until we know what is being declared. */
27897 push_deferring_access_checks (dk_deferred
);
27899 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27901 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27902 cp_parser_decl_specifier_seq (parser
,
27903 (CP_PARSER_FLAGS_OPTIONAL
27904 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
27906 &declares_class_or_enum
);
27908 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27910 /* There are no template typedefs. */
27911 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27913 error_at (decl_spec_token_start
->location
,
27914 "template declaration of %<typedef%>");
27915 decl
= error_mark_node
;
27918 /* Gather up the access checks that occurred the
27919 decl-specifier-seq. */
27920 stop_deferring_access_checks ();
27922 /* Check for the declaration of a template class. */
27923 if (declares_class_or_enum
)
27925 if (cp_parser_declares_only_class_p (parser
)
27926 || (declares_class_or_enum
& 2))
27928 // If this is a declaration, but not a definition, associate
27929 // any constraints with the type declaration. Constraints
27930 // are associated with definitions in cp_parser_class_specifier.
27931 if (declares_class_or_enum
== 1)
27932 associate_classtype_constraints (decl_specifiers
.type
);
27934 decl
= shadow_tag (&decl_specifiers
);
27939 friend template <typename T> struct A<T>::B;
27942 A<T>::B will be represented by a TYPENAME_TYPE, and
27943 therefore not recognized by shadow_tag. */
27944 if (friend_p
&& *friend_p
27946 && decl_specifiers
.type
27947 && TYPE_P (decl_specifiers
.type
))
27948 decl
= decl_specifiers
.type
;
27950 if (decl
&& decl
!= error_mark_node
)
27951 decl
= TYPE_NAME (decl
);
27953 decl
= error_mark_node
;
27955 /* Perform access checks for template parameters. */
27956 cp_parser_perform_template_parameter_access_checks (checks
);
27958 /* Give a helpful diagnostic for
27959 template <class T> struct A { } a;
27960 if we aren't already recovering from an error. */
27961 if (!cp_parser_declares_only_class_p (parser
)
27964 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27965 "a class template declaration must not declare "
27967 cp_parser_skip_to_end_of_block_or_statement (parser
);
27973 /* Complain about missing 'typename' or other invalid type names. */
27974 if (!decl_specifiers
.any_type_specifiers_p
27975 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27977 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27978 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27979 the rest of this declaration. */
27980 decl
= error_mark_node
;
27984 /* If it's not a template class, try for a template function. If
27985 the next token is a `;', then this declaration does not declare
27986 anything. But, if there were errors in the decl-specifiers, then
27987 the error might well have come from an attempted class-specifier.
27988 In that case, there's no need to warn about a missing declarator. */
27990 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27991 || decl_specifiers
.type
!= error_mark_node
))
27993 decl
= cp_parser_init_declarator (parser
,
27994 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
27997 /*function_definition_allowed_p=*/true,
27999 declares_class_or_enum
,
28000 &function_definition_p
,
28003 /* 7.1.1-1 [dcl.stc]
28005 A storage-class-specifier shall not be specified in an explicit
28006 specialization... */
28008 && explicit_specialization_p
28009 && decl_specifiers
.storage_class
!= sc_none
)
28011 error_at (decl_spec_token_start
->location
,
28012 "explicit template specialization cannot have a storage class");
28013 decl
= error_mark_node
;
28016 if (decl
&& VAR_P (decl
))
28017 check_template_variable (decl
);
28020 /* Look for a trailing `;' after the declaration. */
28021 if (!function_definition_p
28022 && (decl
== error_mark_node
28023 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28024 cp_parser_skip_to_end_of_block_or_statement (parser
);
28027 pop_deferring_access_checks ();
28029 /* Clear any current qualification; whatever comes next is the start
28030 of something new. */
28031 parser
->scope
= NULL_TREE
;
28032 parser
->qualifying_scope
= NULL_TREE
;
28033 parser
->object_scope
= NULL_TREE
;
28038 /* Parse a cast-expression that is not the operand of a unary "&". */
28041 cp_parser_simple_cast_expression (cp_parser
*parser
)
28043 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28044 /*cast_p=*/false, /*decltype*/false, NULL
);
28047 /* Parse a functional cast to TYPE. Returns an expression
28048 representing the cast. */
28051 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28053 vec
<tree
, va_gc
> *vec
;
28054 tree expression_list
;
28058 location_t start_loc
= input_location
;
28061 type
= error_mark_node
;
28063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28065 cp_lexer_set_source_position (parser
->lexer
);
28066 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28067 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28068 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28069 if (TREE_CODE (type
) == TYPE_DECL
)
28070 type
= TREE_TYPE (type
);
28072 cast
= finish_compound_literal (type
, expression_list
,
28073 tf_warning_or_error
, fcl_functional
);
28074 /* Create a location of the form:
28077 with caret == start at the start of the type name,
28078 finishing at the closing brace. */
28079 location_t finish_loc
28080 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28081 location_t combined_loc
= make_location (start_loc
, start_loc
,
28083 cast
.set_location (combined_loc
);
28088 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28090 /*allow_expansion_p=*/true,
28091 /*non_constant_p=*/NULL
);
28093 expression_list
= error_mark_node
;
28096 expression_list
= build_tree_list_vec (vec
);
28097 release_tree_vector (vec
);
28100 cast
= build_functional_cast (type
, expression_list
,
28101 tf_warning_or_error
);
28102 /* [expr.const]/1: In an integral constant expression "only type
28103 conversions to integral or enumeration type can be used". */
28104 if (TREE_CODE (type
) == TYPE_DECL
)
28105 type
= TREE_TYPE (type
);
28106 if (cast
!= error_mark_node
28107 && !cast_valid_in_integral_constant_expression_p (type
)
28108 && cp_parser_non_integral_constant_expression (parser
,
28110 return error_mark_node
;
28112 /* Create a location of the form:
28115 with caret == start at the start of the type name,
28116 finishing at the closing paren. */
28117 location_t finish_loc
28118 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28119 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28120 cast
.set_location (combined_loc
);
28124 /* Save the tokens that make up the body of a member function defined
28125 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28126 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28127 specifiers applied to the declaration. Returns the FUNCTION_DECL
28128 for the member function. */
28131 cp_parser_save_member_function_body (cp_parser
* parser
,
28132 cp_decl_specifier_seq
*decl_specifiers
,
28133 cp_declarator
*declarator
,
28139 bool function_try_block
= false;
28141 /* Create the FUNCTION_DECL. */
28142 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28143 cp_finalize_omp_declare_simd (parser
, fn
);
28144 cp_finalize_oacc_routine (parser
, fn
, true);
28145 /* If something went badly wrong, bail out now. */
28146 if (fn
== error_mark_node
)
28148 /* If there's a function-body, skip it. */
28149 if (cp_parser_token_starts_function_definition_p
28150 (cp_lexer_peek_token (parser
->lexer
)))
28151 cp_parser_skip_to_end_of_block_or_statement (parser
);
28152 return error_mark_node
;
28155 /* Remember it, if there default args to post process. */
28156 cp_parser_save_default_args (parser
, fn
);
28158 /* Save away the tokens that make up the body of the
28160 first
= parser
->lexer
->next_token
;
28162 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28163 cp_lexer_consume_token (parser
->lexer
);
28164 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28165 RID_TRANSACTION_ATOMIC
))
28167 cp_lexer_consume_token (parser
->lexer
);
28168 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28170 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28171 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28172 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28173 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28174 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28176 cp_lexer_consume_token (parser
->lexer
);
28177 cp_lexer_consume_token (parser
->lexer
);
28178 cp_lexer_consume_token (parser
->lexer
);
28179 cp_lexer_consume_token (parser
->lexer
);
28180 cp_lexer_consume_token (parser
->lexer
);
28183 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28184 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28186 cp_lexer_consume_token (parser
->lexer
);
28187 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28192 /* Handle function try blocks. */
28193 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28195 cp_lexer_consume_token (parser
->lexer
);
28196 function_try_block
= true;
28198 /* We can have braced-init-list mem-initializers before the fn body. */
28199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28201 cp_lexer_consume_token (parser
->lexer
);
28202 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28204 /* cache_group will stop after an un-nested { } pair, too. */
28205 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28208 /* variadic mem-inits have ... after the ')'. */
28209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28210 cp_lexer_consume_token (parser
->lexer
);
28213 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28214 /* Handle function try blocks. */
28215 if (function_try_block
)
28216 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28217 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28218 last
= parser
->lexer
->next_token
;
28220 /* Save away the inline definition; we will process it when the
28221 class is complete. */
28222 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28223 DECL_PENDING_INLINE_P (fn
) = 1;
28225 /* We need to know that this was defined in the class, so that
28226 friend templates are handled correctly. */
28227 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28229 /* Add FN to the queue of functions to be parsed later. */
28230 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28235 /* Save the tokens that make up the in-class initializer for a non-static
28236 data member. Returns a DEFAULT_ARG. */
28239 cp_parser_save_nsdmi (cp_parser
* parser
)
28241 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28244 /* Parse a template-argument-list, as well as the trailing ">" (but
28245 not the opening "<"). See cp_parser_template_argument_list for the
28249 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28253 tree saved_qualifying_scope
;
28254 tree saved_object_scope
;
28255 bool saved_greater_than_is_operator_p
;
28259 When parsing a template-id, the first non-nested `>' is taken as
28260 the end of the template-argument-list rather than a greater-than
28262 saved_greater_than_is_operator_p
28263 = parser
->greater_than_is_operator_p
;
28264 parser
->greater_than_is_operator_p
= false;
28265 /* Parsing the argument list may modify SCOPE, so we save it
28267 saved_scope
= parser
->scope
;
28268 saved_qualifying_scope
= parser
->qualifying_scope
;
28269 saved_object_scope
= parser
->object_scope
;
28270 /* We need to evaluate the template arguments, even though this
28271 template-id may be nested within a "sizeof". */
28273 /* Parse the template-argument-list itself. */
28274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28275 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28276 arguments
= NULL_TREE
;
28278 arguments
= cp_parser_template_argument_list (parser
);
28279 /* Look for the `>' that ends the template-argument-list. If we find
28280 a '>>' instead, it's probably just a typo. */
28281 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28283 if (cxx_dialect
!= cxx98
)
28285 /* In C++0x, a `>>' in a template argument list or cast
28286 expression is considered to be two separate `>'
28287 tokens. So, change the current token to a `>', but don't
28288 consume it: it will be consumed later when the outer
28289 template argument list (or cast expression) is parsed.
28290 Note that this replacement of `>' for `>>' is necessary
28291 even if we are parsing tentatively: in the tentative
28292 case, after calling
28293 cp_parser_enclosed_template_argument_list we will always
28294 throw away all of the template arguments and the first
28295 closing `>', either because the template argument list
28296 was erroneous or because we are replacing those tokens
28297 with a CPP_TEMPLATE_ID token. The second `>' (which will
28298 not have been thrown away) is needed either to close an
28299 outer template argument list or to complete a new-style
28301 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28302 token
->type
= CPP_GREATER
;
28304 else if (!saved_greater_than_is_operator_p
)
28306 /* If we're in a nested template argument list, the '>>' has
28307 to be a typo for '> >'. We emit the error message, but we
28308 continue parsing and we push a '>' as next token, so that
28309 the argument list will be parsed correctly. Note that the
28310 global source location is still on the token before the
28311 '>>', so we need to say explicitly where we want it. */
28312 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28313 gcc_rich_location
richloc (token
->location
);
28314 richloc
.add_fixit_replace ("> >");
28315 error_at (&richloc
, "%<>>%> should be %<> >%> "
28316 "within a nested template argument list");
28318 token
->type
= CPP_GREATER
;
28322 /* If this is not a nested template argument list, the '>>'
28323 is a typo for '>'. Emit an error message and continue.
28324 Same deal about the token location, but here we can get it
28325 right by consuming the '>>' before issuing the diagnostic. */
28326 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28327 error_at (token
->location
,
28328 "spurious %<>>%>, use %<>%> to terminate "
28329 "a template argument list");
28333 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28334 /* The `>' token might be a greater-than operator again now. */
28335 parser
->greater_than_is_operator_p
28336 = saved_greater_than_is_operator_p
;
28337 /* Restore the SAVED_SCOPE. */
28338 parser
->scope
= saved_scope
;
28339 parser
->qualifying_scope
= saved_qualifying_scope
;
28340 parser
->object_scope
= saved_object_scope
;
28345 /* MEMBER_FUNCTION is a member function, or a friend. If default
28346 arguments, or the body of the function have not yet been parsed,
28350 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28352 timevar_push (TV_PARSE_INMETH
);
28353 /* If this member is a template, get the underlying
28355 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28356 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28358 /* There should not be any class definitions in progress at this
28359 point; the bodies of members are only parsed outside of all class
28361 gcc_assert (parser
->num_classes_being_defined
== 0);
28362 /* While we're parsing the member functions we might encounter more
28363 classes. We want to handle them right away, but we don't want
28364 them getting mixed up with functions that are currently in the
28366 push_unparsed_function_queues (parser
);
28368 /* Make sure that any template parameters are in scope. */
28369 maybe_begin_member_template_processing (member_function
);
28371 /* If the body of the function has not yet been parsed, parse it
28373 if (DECL_PENDING_INLINE_P (member_function
))
28375 tree function_scope
;
28376 cp_token_cache
*tokens
;
28378 /* The function is no longer pending; we are processing it. */
28379 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28380 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28381 DECL_PENDING_INLINE_P (member_function
) = 0;
28383 /* If this is a local class, enter the scope of the containing
28385 function_scope
= current_function_decl
;
28386 if (function_scope
)
28387 push_function_context ();
28389 /* Push the body of the function onto the lexer stack. */
28390 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28392 /* Let the front end know that we going to be defining this
28394 start_preparsed_function (member_function
, NULL_TREE
,
28395 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28397 /* Don't do access checking if it is a templated function. */
28398 if (processing_template_decl
)
28399 push_deferring_access_checks (dk_no_check
);
28401 /* #pragma omp declare reduction needs special parsing. */
28402 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28404 parser
->lexer
->in_pragma
= true;
28405 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28406 finish_function (/*inline_p=*/true);
28407 cp_check_omp_declare_reduction (member_function
);
28410 /* Now, parse the body of the function. */
28411 cp_parser_function_definition_after_declarator (parser
,
28412 /*inline_p=*/true);
28414 if (processing_template_decl
)
28415 pop_deferring_access_checks ();
28417 /* Leave the scope of the containing function. */
28418 if (function_scope
)
28419 pop_function_context ();
28420 cp_parser_pop_lexer (parser
);
28423 /* Remove any template parameters from the symbol table. */
28424 maybe_end_member_template_processing ();
28426 /* Restore the queue. */
28427 pop_unparsed_function_queues (parser
);
28428 timevar_pop (TV_PARSE_INMETH
);
28431 /* If DECL contains any default args, remember it on the unparsed
28432 functions queue. */
28435 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28439 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28441 probe
= TREE_CHAIN (probe
))
28442 if (TREE_PURPOSE (probe
))
28444 cp_default_arg_entry entry
= {current_class_type
, decl
};
28445 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28450 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28451 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28452 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28453 from the parameter-type-list. */
28456 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28457 tree default_arg
, tree parmtype
)
28459 cp_token_cache
*tokens
;
28463 if (default_arg
== error_mark_node
)
28464 return error_mark_node
;
28466 /* Push the saved tokens for the default argument onto the parser's
28468 tokens
= DEFARG_TOKENS (default_arg
);
28469 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28471 start_lambda_scope (decl
);
28473 /* Parse the default argument. */
28474 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28475 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28478 finish_lambda_scope ();
28480 if (parsed_arg
== error_mark_node
)
28481 cp_parser_skip_to_end_of_statement (parser
);
28483 if (!processing_template_decl
)
28485 /* In a non-template class, check conversions now. In a template,
28486 we'll wait and instantiate these as needed. */
28487 if (TREE_CODE (decl
) == PARM_DECL
)
28488 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28489 tf_warning_or_error
);
28490 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28491 parsed_arg
= error_mark_node
;
28493 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28496 /* If the token stream has not been completely used up, then
28497 there was extra junk after the end of the default
28499 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28501 if (TREE_CODE (decl
) == PARM_DECL
)
28502 cp_parser_error (parser
, "expected %<,%>");
28504 cp_parser_error (parser
, "expected %<;%>");
28507 /* Revert to the main lexer. */
28508 cp_parser_pop_lexer (parser
);
28513 /* FIELD is a non-static data member with an initializer which we saved for
28514 later; parse it now. */
28517 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28521 maybe_begin_member_template_processing (field
);
28523 push_unparsed_function_queues (parser
);
28524 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28525 DECL_INITIAL (field
),
28527 pop_unparsed_function_queues (parser
);
28529 maybe_end_member_template_processing ();
28531 DECL_INITIAL (field
) = def
;
28534 /* FN is a FUNCTION_DECL which may contains a parameter with an
28535 unparsed DEFAULT_ARG. Parse the default args now. This function
28536 assumes that the current scope is the scope in which the default
28537 argument should be processed. */
28540 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28542 bool saved_local_variables_forbidden_p
;
28543 tree parm
, parmdecl
;
28545 /* While we're parsing the default args, we might (due to the
28546 statement expression extension) encounter more classes. We want
28547 to handle them right away, but we don't want them getting mixed
28548 up with default args that are currently in the queue. */
28549 push_unparsed_function_queues (parser
);
28551 /* Local variable names (and the `this' keyword) may not appear
28552 in a default argument. */
28553 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28554 parser
->local_variables_forbidden_p
= true;
28556 push_defarg_context (fn
);
28558 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28559 parmdecl
= DECL_ARGUMENTS (fn
);
28560 parm
&& parm
!= void_list_node
;
28561 parm
= TREE_CHAIN (parm
),
28562 parmdecl
= DECL_CHAIN (parmdecl
))
28564 tree default_arg
= TREE_PURPOSE (parm
);
28566 vec
<tree
, va_gc
> *insts
;
28573 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28574 /* This can happen for a friend declaration for a function
28575 already declared with default arguments. */
28579 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28581 TREE_VALUE (parm
));
28582 TREE_PURPOSE (parm
) = parsed_arg
;
28584 /* Update any instantiations we've already created. */
28585 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28586 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28587 TREE_PURPOSE (copy
) = parsed_arg
;
28590 pop_defarg_context ();
28592 /* Make sure no default arg is missing. */
28593 check_default_args (fn
);
28595 /* Restore the state of local_variables_forbidden_p. */
28596 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28598 /* Restore the queue. */
28599 pop_unparsed_function_queues (parser
);
28602 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28604 sizeof ... ( identifier )
28606 where the 'sizeof' token has already been consumed. */
28609 cp_parser_sizeof_pack (cp_parser
*parser
)
28611 /* Consume the `...'. */
28612 cp_lexer_consume_token (parser
->lexer
);
28613 maybe_warn_variadic_templates ();
28615 matching_parens parens
;
28616 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28618 parens
.consume_open (parser
);
28620 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28621 "%<sizeof...%> argument must be surrounded by parentheses");
28623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28624 tree name
= cp_parser_identifier (parser
);
28625 if (name
== error_mark_node
)
28626 return error_mark_node
;
28627 /* The name is not qualified. */
28628 parser
->scope
= NULL_TREE
;
28629 parser
->qualifying_scope
= NULL_TREE
;
28630 parser
->object_scope
= NULL_TREE
;
28631 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28632 if (expr
== error_mark_node
)
28633 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28635 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28636 expr
= TREE_TYPE (expr
);
28637 else if (TREE_CODE (expr
) == CONST_DECL
)
28638 expr
= DECL_INITIAL (expr
);
28639 expr
= make_pack_expansion (expr
);
28640 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28643 parens
.require_close (parser
);
28648 /* Parse the operand of `sizeof' (or a similar operator). Returns
28649 either a TYPE or an expression, depending on the form of the
28650 input. The KEYWORD indicates which kind of expression we have
28654 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28656 tree expr
= NULL_TREE
;
28657 const char *saved_message
;
28659 bool saved_integral_constant_expression_p
;
28660 bool saved_non_integral_constant_expression_p
;
28662 /* If it's a `...', then we are computing the length of a parameter
28664 if (keyword
== RID_SIZEOF
28665 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28666 return cp_parser_sizeof_pack (parser
);
28668 /* Types cannot be defined in a `sizeof' expression. Save away the
28670 saved_message
= parser
->type_definition_forbidden_message
;
28671 /* And create the new one. */
28672 tmp
= concat ("types may not be defined in %<",
28673 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28674 "%> expressions", NULL
);
28675 parser
->type_definition_forbidden_message
= tmp
;
28677 /* The restrictions on constant-expressions do not apply inside
28678 sizeof expressions. */
28679 saved_integral_constant_expression_p
28680 = parser
->integral_constant_expression_p
;
28681 saved_non_integral_constant_expression_p
28682 = parser
->non_integral_constant_expression_p
;
28683 parser
->integral_constant_expression_p
= false;
28685 /* Do not actually evaluate the expression. */
28686 ++cp_unevaluated_operand
;
28687 ++c_inhibit_evaluation_warnings
;
28688 /* If it's a `(', then we might be looking at the type-id
28690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28692 tree type
= NULL_TREE
;
28694 /* We can't be sure yet whether we're looking at a type-id or an
28696 cp_parser_parse_tentatively (parser
);
28698 matching_parens parens
;
28699 parens
.consume_open (parser
);
28701 /* Note: as a GNU Extension, compound literals are considered
28702 postfix-expressions as they are in C99, so they are valid
28703 arguments to sizeof. See comment in cp_parser_cast_expression
28705 if (cp_parser_compound_literal_p (parser
))
28706 cp_parser_simulate_error (parser
);
28709 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28710 parser
->in_type_id_in_expr_p
= true;
28711 /* Look for the type-id. */
28712 type
= cp_parser_type_id (parser
);
28713 /* Look for the closing `)'. */
28714 parens
.require_close (parser
);
28715 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28718 /* If all went well, then we're done. */
28719 if (cp_parser_parse_definitely (parser
))
28723 /* If the type-id production did not work out, then we must be
28724 looking at the unary-expression production. */
28726 expr
= cp_parser_unary_expression (parser
);
28728 /* Go back to evaluating expressions. */
28729 --cp_unevaluated_operand
;
28730 --c_inhibit_evaluation_warnings
;
28732 /* Free the message we created. */
28734 /* And restore the old one. */
28735 parser
->type_definition_forbidden_message
= saved_message
;
28736 parser
->integral_constant_expression_p
28737 = saved_integral_constant_expression_p
;
28738 parser
->non_integral_constant_expression_p
28739 = saved_non_integral_constant_expression_p
;
28744 /* If the current declaration has no declarator, return true. */
28747 cp_parser_declares_only_class_p (cp_parser
*parser
)
28749 /* If the next token is a `;' or a `,' then there is no
28751 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28752 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28755 /* Update the DECL_SPECS to reflect the storage class indicated by
28759 cp_parser_set_storage_class (cp_parser
*parser
,
28760 cp_decl_specifier_seq
*decl_specs
,
28764 cp_storage_class storage_class
;
28766 if (parser
->in_unbraced_linkage_specification_p
)
28768 error_at (token
->location
, "invalid use of %qD in linkage specification",
28769 ridpointers
[keyword
]);
28772 else if (decl_specs
->storage_class
!= sc_none
)
28774 decl_specs
->conflicting_specifiers_p
= true;
28778 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28779 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28780 && decl_specs
->gnu_thread_keyword_p
)
28782 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28783 "%<__thread%> before %qD", ridpointers
[keyword
]);
28789 storage_class
= sc_auto
;
28792 storage_class
= sc_register
;
28795 storage_class
= sc_static
;
28798 storage_class
= sc_extern
;
28801 storage_class
= sc_mutable
;
28804 gcc_unreachable ();
28806 decl_specs
->storage_class
= storage_class
;
28807 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28809 /* A storage class specifier cannot be applied alongside a typedef
28810 specifier. If there is a typedef specifier present then set
28811 conflicting_specifiers_p which will trigger an error later
28812 on in grokdeclarator. */
28813 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28814 decl_specs
->conflicting_specifiers_p
= true;
28817 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28818 is true, the type is a class or enum definition. */
28821 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28824 bool type_definition_p
)
28826 decl_specs
->any_specifiers_p
= true;
28828 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28829 (with, for example, in "typedef int wchar_t;") we remember that
28830 this is what happened. In system headers, we ignore these
28831 declarations so that G++ can work with system headers that are not
28833 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28834 && !type_definition_p
28835 && (type_spec
== boolean_type_node
28836 || type_spec
== char16_type_node
28837 || type_spec
== char32_type_node
28838 || type_spec
== wchar_type_node
)
28839 && (decl_specs
->type
28840 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28841 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28842 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28843 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28845 decl_specs
->redefined_builtin_type
= type_spec
;
28846 set_and_check_decl_spec_loc (decl_specs
,
28847 ds_redefined_builtin_type_spec
,
28849 if (!decl_specs
->type
)
28851 decl_specs
->type
= type_spec
;
28852 decl_specs
->type_definition_p
= false;
28853 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28856 else if (decl_specs
->type
)
28857 decl_specs
->multiple_types_p
= true;
28860 decl_specs
->type
= type_spec
;
28861 decl_specs
->type_definition_p
= type_definition_p
;
28862 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28863 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28867 /* True iff TOKEN is the GNU keyword __thread. */
28870 token_is__thread (cp_token
*token
)
28872 gcc_assert (token
->keyword
== RID_THREAD
);
28873 return id_equal (token
->u
.value
, "__thread");
28876 /* Set the location for a declarator specifier and check if it is
28879 DECL_SPECS is the sequence of declarator specifiers onto which to
28882 DS is the single declarator specifier to set which location is to
28883 be set onto the existing sequence of declarators.
28885 LOCATION is the location for the declarator specifier to
28889 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28890 cp_decl_spec ds
, cp_token
*token
)
28892 gcc_assert (ds
< ds_last
);
28894 if (decl_specs
== NULL
)
28897 location_t location
= token
->location
;
28899 if (decl_specs
->locations
[ds
] == 0)
28901 decl_specs
->locations
[ds
] = location
;
28902 if (ds
== ds_thread
)
28903 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28909 if (decl_specs
->locations
[ds_long_long
] != 0)
28910 error_at (location
,
28911 "%<long long long%> is too long for GCC");
28914 decl_specs
->locations
[ds_long_long
] = location
;
28915 pedwarn_cxx98 (location
,
28917 "ISO C++ 1998 does not support %<long long%>");
28920 else if (ds
== ds_thread
)
28922 bool gnu
= token_is__thread (token
);
28923 gcc_rich_location
richloc (location
);
28924 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28926 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28927 error_at (&richloc
,
28928 "both %<__thread%> and %<thread_local%> specified");
28932 richloc
.add_fixit_remove ();
28933 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28938 static const char *const decl_spec_names
[] = {
28955 gcc_rich_location
richloc (location
);
28956 richloc
.add_fixit_remove ();
28957 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28962 /* Return true iff the declarator specifier DS is present in the
28963 sequence of declarator specifiers DECL_SPECS. */
28966 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28969 gcc_assert (ds
< ds_last
);
28971 if (decl_specs
== NULL
)
28974 return decl_specs
->locations
[ds
] != 0;
28977 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28978 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28981 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28983 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28986 /* Issue an error message indicating that TOKEN_DESC was expected.
28987 If KEYWORD is true, it indicated this function is called by
28988 cp_parser_require_keword and the required token can only be
28989 a indicated keyword.
28991 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28992 within any error as the location of an "opening" token matching
28993 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28994 RT_CLOSE_PAREN). */
28997 cp_parser_required_error (cp_parser
*parser
,
28998 required_token token_desc
,
29000 location_t matching_location
)
29002 if (cp_parser_simulate_error (parser
))
29005 const char *gmsgid
= NULL
;
29006 switch (token_desc
)
29009 gmsgid
= G_("expected %<new%>");
29012 gmsgid
= G_("expected %<delete%>");
29015 gmsgid
= G_("expected %<return%>");
29018 gmsgid
= G_("expected %<while%>");
29021 gmsgid
= G_("expected %<extern%>");
29023 case RT_STATIC_ASSERT
:
29024 gmsgid
= G_("expected %<static_assert%>");
29027 gmsgid
= G_("expected %<decltype%>");
29030 gmsgid
= G_("expected %<operator%>");
29033 gmsgid
= G_("expected %<class%>");
29036 gmsgid
= G_("expected %<template%>");
29039 gmsgid
= G_("expected %<namespace%>");
29042 gmsgid
= G_("expected %<using%>");
29045 gmsgid
= G_("expected %<asm%>");
29048 gmsgid
= G_("expected %<try%>");
29051 gmsgid
= G_("expected %<catch%>");
29054 gmsgid
= G_("expected %<throw%>");
29057 gmsgid
= G_("expected %<__label__%>");
29060 gmsgid
= G_("expected %<@try%>");
29062 case RT_AT_SYNCHRONIZED
:
29063 gmsgid
= G_("expected %<@synchronized%>");
29066 gmsgid
= G_("expected %<@throw%>");
29068 case RT_TRANSACTION_ATOMIC
:
29069 gmsgid
= G_("expected %<__transaction_atomic%>");
29071 case RT_TRANSACTION_RELAXED
:
29072 gmsgid
= G_("expected %<__transaction_relaxed%>");
29078 if (!gmsgid
&& !keyword
)
29080 switch (token_desc
)
29083 gmsgid
= G_("expected %<;%>");
29085 case RT_OPEN_PAREN
:
29086 gmsgid
= G_("expected %<(%>");
29088 case RT_CLOSE_BRACE
:
29089 gmsgid
= G_("expected %<}%>");
29091 case RT_OPEN_BRACE
:
29092 gmsgid
= G_("expected %<{%>");
29094 case RT_CLOSE_SQUARE
:
29095 gmsgid
= G_("expected %<]%>");
29097 case RT_OPEN_SQUARE
:
29098 gmsgid
= G_("expected %<[%>");
29101 gmsgid
= G_("expected %<,%>");
29104 gmsgid
= G_("expected %<::%>");
29107 gmsgid
= G_("expected %<<%>");
29110 gmsgid
= G_("expected %<>%>");
29113 gmsgid
= G_("expected %<=%>");
29116 gmsgid
= G_("expected %<...%>");
29119 gmsgid
= G_("expected %<*%>");
29122 gmsgid
= G_("expected %<~%>");
29125 gmsgid
= G_("expected %<:%>");
29127 case RT_COLON_SCOPE
:
29128 gmsgid
= G_("expected %<:%> or %<::%>");
29130 case RT_CLOSE_PAREN
:
29131 gmsgid
= G_("expected %<)%>");
29133 case RT_COMMA_CLOSE_PAREN
:
29134 gmsgid
= G_("expected %<,%> or %<)%>");
29136 case RT_PRAGMA_EOL
:
29137 gmsgid
= G_("expected end of line");
29140 gmsgid
= G_("expected identifier");
29143 gmsgid
= G_("expected selection-statement");
29146 gmsgid
= G_("expected iteration-statement");
29149 gmsgid
= G_("expected jump-statement");
29152 gmsgid
= G_("expected class-key");
29154 case RT_CLASS_TYPENAME_TEMPLATE
:
29155 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29158 gcc_unreachable ();
29163 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29167 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29168 issue an error message indicating that TOKEN_DESC was expected.
29170 Returns the token consumed, if the token had the appropriate type.
29171 Otherwise, returns NULL.
29173 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29174 within any error as the location of an "opening" token matching
29175 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29176 RT_CLOSE_PAREN). */
29179 cp_parser_require (cp_parser
* parser
,
29180 enum cpp_ttype type
,
29181 required_token token_desc
,
29182 location_t matching_location
)
29184 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29185 return cp_lexer_consume_token (parser
->lexer
);
29188 /* Output the MESSAGE -- unless we're parsing tentatively. */
29189 if (!cp_parser_simulate_error (parser
))
29190 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29191 matching_location
);
29196 /* An error message is produced if the next token is not '>'.
29197 All further tokens are skipped until the desired token is
29198 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29201 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29203 /* Current level of '< ... >'. */
29204 unsigned level
= 0;
29205 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29206 unsigned nesting_depth
= 0;
29208 /* Are we ready, yet? If not, issue error message. */
29209 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29212 /* Skip tokens until the desired token is found. */
29215 /* Peek at the next token. */
29216 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29219 if (!nesting_depth
)
29224 if (cxx_dialect
== cxx98
)
29225 /* C++0x views the `>>' operator as two `>' tokens, but
29228 else if (!nesting_depth
&& level
-- == 0)
29230 /* We've hit a `>>' where the first `>' closes the
29231 template argument list, and the second `>' is
29232 spurious. Just consume the `>>' and stop; we've
29233 already produced at least one error. */
29234 cp_lexer_consume_token (parser
->lexer
);
29237 /* Fall through for C++0x, so we handle the second `>' in
29239 gcc_fallthrough ();
29242 if (!nesting_depth
&& level
-- == 0)
29244 /* We've reached the token we want, consume it and stop. */
29245 cp_lexer_consume_token (parser
->lexer
);
29250 case CPP_OPEN_PAREN
:
29251 case CPP_OPEN_SQUARE
:
29255 case CPP_CLOSE_PAREN
:
29256 case CPP_CLOSE_SQUARE
:
29257 if (nesting_depth
-- == 0)
29262 case CPP_PRAGMA_EOL
:
29263 case CPP_SEMICOLON
:
29264 case CPP_OPEN_BRACE
:
29265 case CPP_CLOSE_BRACE
:
29266 /* The '>' was probably forgotten, don't look further. */
29273 /* Consume this token. */
29274 cp_lexer_consume_token (parser
->lexer
);
29278 /* If the next token is the indicated keyword, consume it. Otherwise,
29279 issue an error message indicating that TOKEN_DESC was expected.
29281 Returns the token consumed, if the token had the appropriate type.
29282 Otherwise, returns NULL. */
29285 cp_parser_require_keyword (cp_parser
* parser
,
29287 required_token token_desc
)
29289 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29291 if (token
&& token
->keyword
!= keyword
)
29293 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29301 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29302 function-definition. */
29305 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29307 return (/* An ordinary function-body begins with an `{'. */
29308 token
->type
== CPP_OPEN_BRACE
29309 /* A ctor-initializer begins with a `:'. */
29310 || token
->type
== CPP_COLON
29311 /* A function-try-block begins with `try'. */
29312 || token
->keyword
== RID_TRY
29313 /* A function-transaction-block begins with `__transaction_atomic'
29314 or `__transaction_relaxed'. */
29315 || token
->keyword
== RID_TRANSACTION_ATOMIC
29316 || token
->keyword
== RID_TRANSACTION_RELAXED
29317 /* The named return value extension begins with `return'. */
29318 || token
->keyword
== RID_RETURN
);
29321 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29325 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29329 token
= cp_lexer_peek_token (parser
->lexer
);
29330 return (token
->type
== CPP_OPEN_BRACE
29331 || (token
->type
== CPP_COLON
29332 && !parser
->colon_doesnt_start_class_def_p
));
29335 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29336 C++0x) ending a template-argument. */
29339 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29343 token
= cp_lexer_peek_token (parser
->lexer
);
29344 return (token
->type
== CPP_COMMA
29345 || token
->type
== CPP_GREATER
29346 || token
->type
== CPP_ELLIPSIS
29347 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29350 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29351 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29354 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29359 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29360 if (token
->type
== CPP_LESS
)
29362 /* Check for the sequence `<::' in the original code. It would be lexed as
29363 `[:', where `[' is a digraph, and there is no whitespace before
29365 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29368 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29369 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29375 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29376 or none_type otherwise. */
29378 static enum tag_types
29379 cp_parser_token_is_class_key (cp_token
* token
)
29381 switch (token
->keyword
)
29386 return record_type
;
29395 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29396 or none_type otherwise or if the token is null. */
29398 static enum tag_types
29399 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29404 switch (token
->keyword
)
29409 return typename_type
;
29416 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29419 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29421 if (type
== error_mark_node
)
29423 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29425 if (permerror (input_location
, "%qs tag used in naming %q#T",
29426 class_key
== union_type
? "union"
29427 : class_key
== record_type
? "struct" : "class",
29429 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29430 "%q#T was previously declared here", type
);
29434 /* Issue an error message if DECL is redeclared with different
29435 access than its original declaration [class.access.spec/3].
29436 This applies to nested classes, nested class templates and
29437 enumerations [class.mem/1]. */
29440 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29443 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29444 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29447 if ((TREE_PRIVATE (decl
)
29448 != (current_access_specifier
== access_private_node
))
29449 || (TREE_PROTECTED (decl
)
29450 != (current_access_specifier
== access_protected_node
)))
29451 error_at (location
, "%qD redeclared with different access", decl
);
29454 /* Look for the `template' keyword, as a syntactic disambiguator.
29455 Return TRUE iff it is present, in which case it will be
29459 cp_parser_optional_template_keyword (cp_parser
*parser
)
29461 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29463 /* In C++98 the `template' keyword can only be used within templates;
29464 outside templates the parser can always figure out what is a
29465 template and what is not. In C++11, per the resolution of DR 468,
29466 `template' is allowed in cases where it is not strictly necessary. */
29467 if (!processing_template_decl
29468 && pedantic
&& cxx_dialect
== cxx98
)
29470 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29471 pedwarn (token
->location
, OPT_Wpedantic
,
29472 "in C++98 %<template%> (as a disambiguator) is only "
29473 "allowed within templates");
29474 /* If this part of the token stream is rescanned, the same
29475 error message would be generated. So, we purge the token
29476 from the stream. */
29477 cp_lexer_purge_token (parser
->lexer
);
29482 /* Consume the `template' keyword. */
29483 cp_lexer_consume_token (parser
->lexer
);
29490 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29491 set PARSER->SCOPE, and perform other related actions. */
29494 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29496 struct tree_check
*check_value
;
29498 /* Get the stored value. */
29499 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29500 /* Set the scope from the stored value. */
29501 parser
->scope
= saved_checks_value (check_value
);
29502 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29503 parser
->object_scope
= NULL_TREE
;
29506 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29507 encounter the end of a block before what we were looking for. */
29510 cp_parser_cache_group (cp_parser
*parser
,
29511 enum cpp_ttype end
,
29516 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29518 /* Abort a parenthesized expression if we encounter a semicolon. */
29519 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29520 && token
->type
== CPP_SEMICOLON
)
29522 /* If we've reached the end of the file, stop. */
29523 if (token
->type
== CPP_EOF
29524 || (end
!= CPP_PRAGMA_EOL
29525 && token
->type
== CPP_PRAGMA_EOL
))
29527 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29528 /* We've hit the end of an enclosing block, so there's been some
29529 kind of syntax error. */
29532 /* Consume the token. */
29533 cp_lexer_consume_token (parser
->lexer
);
29534 /* See if it starts a new group. */
29535 if (token
->type
== CPP_OPEN_BRACE
)
29537 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29538 /* In theory this should probably check end == '}', but
29539 cp_parser_save_member_function_body needs it to exit
29540 after either '}' or ')' when called with ')'. */
29544 else if (token
->type
== CPP_OPEN_PAREN
)
29546 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29547 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29550 else if (token
->type
== CPP_PRAGMA
)
29551 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29552 else if (token
->type
== end
)
29557 /* Like above, for caching a default argument or NSDMI. Both of these are
29558 terminated by a non-nested comma, but it can be unclear whether or not a
29559 comma is nested in a template argument list unless we do more parsing.
29560 In order to handle this ambiguity, when we encounter a ',' after a '<'
29561 we try to parse what follows as a parameter-declaration-list (in the
29562 case of a default argument) or a member-declarator (in the case of an
29563 NSDMI). If that succeeds, then we stop caching. */
29566 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29568 unsigned depth
= 0;
29569 int maybe_template_id
= 0;
29570 cp_token
*first_token
;
29572 tree default_argument
;
29574 /* Add tokens until we have processed the entire default
29575 argument. We add the range [first_token, token). */
29576 first_token
= cp_lexer_peek_token (parser
->lexer
);
29577 if (first_token
->type
== CPP_OPEN_BRACE
)
29579 /* For list-initialization, this is straightforward. */
29580 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29581 token
= cp_lexer_peek_token (parser
->lexer
);
29587 /* Peek at the next token. */
29588 token
= cp_lexer_peek_token (parser
->lexer
);
29589 /* What we do depends on what token we have. */
29590 switch (token
->type
)
29592 /* In valid code, a default argument must be
29593 immediately followed by a `,' `)', or `...'. */
29595 if (depth
== 0 && maybe_template_id
)
29597 /* If we've seen a '<', we might be in a
29598 template-argument-list. Until Core issue 325 is
29599 resolved, we don't know how this situation ought
29600 to be handled, so try to DTRT. We check whether
29601 what comes after the comma is a valid parameter
29602 declaration list. If it is, then the comma ends
29603 the default argument; otherwise the default
29604 argument continues. */
29605 bool error
= false;
29608 /* Set ITALP so cp_parser_parameter_declaration_list
29609 doesn't decide to commit to this parse. */
29610 bool saved_italp
= parser
->in_template_argument_list_p
;
29611 parser
->in_template_argument_list_p
= true;
29613 cp_parser_parse_tentatively (parser
);
29617 /* Parse declarators until we reach a non-comma or
29618 somthing that cannot be an initializer.
29619 Just checking whether we're looking at a single
29620 declarator is insufficient. Consider:
29621 int var = tuple<T,U>::x;
29622 The template parameter 'U' looks exactly like a
29626 int ctor_dtor_or_conv_p
;
29627 cp_lexer_consume_token (parser
->lexer
);
29628 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29629 CP_PARSER_FLAGS_NONE
,
29630 &ctor_dtor_or_conv_p
,
29631 /*parenthesized_p=*/NULL
,
29633 /*friend_p=*/false);
29634 peek
= cp_lexer_peek_token (parser
->lexer
);
29635 if (cp_parser_error_occurred (parser
))
29638 while (peek
->type
== CPP_COMMA
);
29639 /* If we met an '=' or ';' then the original comma
29640 was the end of the NSDMI. Otherwise assume
29641 we're still in the NSDMI. */
29642 error
= (peek
->type
!= CPP_EQ
29643 && peek
->type
!= CPP_SEMICOLON
);
29647 cp_lexer_consume_token (parser
->lexer
);
29648 begin_scope (sk_function_parms
, NULL_TREE
);
29649 tree t
= cp_parser_parameter_declaration_list
29650 (parser
, CP_PARSER_FLAGS_NONE
);
29651 if (t
== error_mark_node
)
29653 pop_bindings_and_leave_scope ();
29655 if (!cp_parser_error_occurred (parser
) && !error
)
29657 cp_parser_abort_tentative_parse (parser
);
29659 parser
->in_template_argument_list_p
= saved_italp
;
29663 case CPP_CLOSE_PAREN
:
29665 /* If we run into a non-nested `;', `}', or `]',
29666 then the code is invalid -- but the default
29667 argument is certainly over. */
29668 case CPP_SEMICOLON
:
29669 case CPP_CLOSE_BRACE
:
29670 case CPP_CLOSE_SQUARE
:
29672 /* Handle correctly int n = sizeof ... ( p ); */
29673 && token
->type
!= CPP_ELLIPSIS
)
29675 /* Update DEPTH, if necessary. */
29676 else if (token
->type
== CPP_CLOSE_PAREN
29677 || token
->type
== CPP_CLOSE_BRACE
29678 || token
->type
== CPP_CLOSE_SQUARE
)
29682 case CPP_OPEN_PAREN
:
29683 case CPP_OPEN_SQUARE
:
29684 case CPP_OPEN_BRACE
:
29690 /* This might be the comparison operator, or it might
29691 start a template argument list. */
29692 ++maybe_template_id
;
29696 if (cxx_dialect
== cxx98
)
29698 /* Fall through for C++0x, which treats the `>>'
29699 operator like two `>' tokens in certain
29701 gcc_fallthrough ();
29706 /* This might be an operator, or it might close a
29707 template argument list. But if a previous '<'
29708 started a template argument list, this will have
29709 closed it, so we can't be in one anymore. */
29710 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29711 if (maybe_template_id
< 0)
29712 maybe_template_id
= 0;
29716 /* If we run out of tokens, issue an error message. */
29718 case CPP_PRAGMA_EOL
:
29719 error_at (token
->location
, "file ends in default argument");
29720 return error_mark_node
;
29724 /* In these cases, we should look for template-ids.
29725 For example, if the default argument is
29726 `X<int, double>()', we need to do name lookup to
29727 figure out whether or not `X' is a template; if
29728 so, the `,' does not end the default argument.
29730 That is not yet done. */
29737 /* If we've reached the end, stop. */
29741 /* Add the token to the token block. */
29742 token
= cp_lexer_consume_token (parser
->lexer
);
29745 /* Create a DEFAULT_ARG to represent the unparsed default
29747 default_argument
= make_node (DEFAULT_ARG
);
29748 DEFARG_TOKENS (default_argument
)
29749 = cp_token_cache_new (first_token
, token
);
29750 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29752 return default_argument
;
29755 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29758 defarg_location (tree default_argument
)
29760 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29761 location_t start
= tokens
->first
->location
;
29762 location_t end
= tokens
->last
->location
;
29763 return make_location (start
, start
, end
);
29766 /* Begin parsing tentatively. We always save tokens while parsing
29767 tentatively so that if the tentative parsing fails we can restore the
29771 cp_parser_parse_tentatively (cp_parser
* parser
)
29773 /* Enter a new parsing context. */
29774 parser
->context
= cp_parser_context_new (parser
->context
);
29775 /* Begin saving tokens. */
29776 cp_lexer_save_tokens (parser
->lexer
);
29777 /* In order to avoid repetitive access control error messages,
29778 access checks are queued up until we are no longer parsing
29780 push_deferring_access_checks (dk_deferred
);
29783 /* Commit to the currently active tentative parse. */
29786 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29788 cp_parser_context
*context
;
29791 /* Mark all of the levels as committed. */
29792 lexer
= parser
->lexer
;
29793 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29795 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29797 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29798 while (!cp_lexer_saving_tokens (lexer
))
29799 lexer
= lexer
->next
;
29800 cp_lexer_commit_tokens (lexer
);
29804 /* Commit to the topmost currently active tentative parse.
29806 Note that this function shouldn't be called when there are
29807 irreversible side-effects while in a tentative state. For
29808 example, we shouldn't create a permanent entry in the symbol
29809 table, or issue an error message that might not apply if the
29810 tentative parse is aborted. */
29813 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29815 cp_parser_context
*context
= parser
->context
;
29816 cp_lexer
*lexer
= parser
->lexer
;
29820 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29822 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29824 while (!cp_lexer_saving_tokens (lexer
))
29825 lexer
= lexer
->next
;
29826 cp_lexer_commit_tokens (lexer
);
29830 /* Abort the currently active tentative parse. All consumed tokens
29831 will be rolled back, and no diagnostics will be issued. */
29834 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29836 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29837 || errorcount
> 0);
29838 cp_parser_simulate_error (parser
);
29839 /* Now, pretend that we want to see if the construct was
29840 successfully parsed. */
29841 cp_parser_parse_definitely (parser
);
29844 /* Stop parsing tentatively. If a parse error has occurred, restore the
29845 token stream. Otherwise, commit to the tokens we have consumed.
29846 Returns true if no error occurred; false otherwise. */
29849 cp_parser_parse_definitely (cp_parser
* parser
)
29851 bool error_occurred
;
29852 cp_parser_context
*context
;
29854 /* Remember whether or not an error occurred, since we are about to
29855 destroy that information. */
29856 error_occurred
= cp_parser_error_occurred (parser
);
29857 /* Remove the topmost context from the stack. */
29858 context
= parser
->context
;
29859 parser
->context
= context
->next
;
29860 /* If no parse errors occurred, commit to the tentative parse. */
29861 if (!error_occurred
)
29863 /* Commit to the tokens read tentatively, unless that was
29865 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29866 cp_lexer_commit_tokens (parser
->lexer
);
29868 pop_to_parent_deferring_access_checks ();
29870 /* Otherwise, if errors occurred, roll back our state so that things
29871 are just as they were before we began the tentative parse. */
29874 cp_lexer_rollback_tokens (parser
->lexer
);
29875 pop_deferring_access_checks ();
29877 /* Add the context to the front of the free list. */
29878 context
->next
= cp_parser_context_free_list
;
29879 cp_parser_context_free_list
= context
;
29881 return !error_occurred
;
29884 /* Returns true if we are parsing tentatively and are not committed to
29885 this tentative parse. */
29888 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29890 return (cp_parser_parsing_tentatively (parser
)
29891 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29894 /* Returns nonzero iff an error has occurred during the most recent
29895 tentative parse. */
29898 cp_parser_error_occurred (cp_parser
* parser
)
29900 return (cp_parser_parsing_tentatively (parser
)
29901 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29904 /* Returns nonzero if GNU extensions are allowed. */
29907 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29909 return parser
->allow_gnu_extensions_p
;
29912 /* Objective-C++ Productions */
29915 /* Parse an Objective-C expression, which feeds into a primary-expression
29919 objc-message-expression
29920 objc-string-literal
29921 objc-encode-expression
29922 objc-protocol-expression
29923 objc-selector-expression
29925 Returns a tree representation of the expression. */
29928 cp_parser_objc_expression (cp_parser
* parser
)
29930 /* Try to figure out what kind of declaration is present. */
29931 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29935 case CPP_OPEN_SQUARE
:
29936 return cp_parser_objc_message_expression (parser
);
29938 case CPP_OBJC_STRING
:
29939 kwd
= cp_lexer_consume_token (parser
->lexer
);
29940 return objc_build_string_object (kwd
->u
.value
);
29943 switch (kwd
->keyword
)
29945 case RID_AT_ENCODE
:
29946 return cp_parser_objc_encode_expression (parser
);
29948 case RID_AT_PROTOCOL
:
29949 return cp_parser_objc_protocol_expression (parser
);
29951 case RID_AT_SELECTOR
:
29952 return cp_parser_objc_selector_expression (parser
);
29959 error_at (kwd
->location
,
29960 "misplaced %<@%D%> Objective-C++ construct",
29962 cp_parser_skip_to_end_of_block_or_statement (parser
);
29965 return error_mark_node
;
29968 /* Parse an Objective-C message expression.
29970 objc-message-expression:
29971 [ objc-message-receiver objc-message-args ]
29973 Returns a representation of an Objective-C message. */
29976 cp_parser_objc_message_expression (cp_parser
* parser
)
29978 tree receiver
, messageargs
;
29980 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29981 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29982 receiver
= cp_parser_objc_message_receiver (parser
);
29983 messageargs
= cp_parser_objc_message_args (parser
);
29984 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29985 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29987 tree result
= objc_build_message_expr (receiver
, messageargs
);
29989 /* Construct a location e.g.
29992 ranging from the '[' to the ']', with the caret at the start. */
29993 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29994 protected_set_expr_location (result
, combined_loc
);
29999 /* Parse an objc-message-receiver.
30001 objc-message-receiver:
30003 simple-type-specifier
30005 Returns a representation of the type or expression. */
30008 cp_parser_objc_message_receiver (cp_parser
* parser
)
30012 /* An Objective-C message receiver may be either (1) a type
30013 or (2) an expression. */
30014 cp_parser_parse_tentatively (parser
);
30015 rcv
= cp_parser_expression (parser
);
30017 /* If that worked out, fine. */
30018 if (cp_parser_parse_definitely (parser
))
30021 cp_parser_parse_tentatively (parser
);
30022 rcv
= cp_parser_simple_type_specifier (parser
,
30023 /*decl_specs=*/NULL
,
30024 CP_PARSER_FLAGS_NONE
);
30026 if (cp_parser_parse_definitely (parser
))
30027 return objc_get_class_reference (rcv
);
30029 cp_parser_error (parser
, "objective-c++ message receiver expected");
30030 return error_mark_node
;
30033 /* Parse the arguments and selectors comprising an Objective-C message.
30038 objc-selector-args , objc-comma-args
30040 objc-selector-args:
30041 objc-selector [opt] : assignment-expression
30042 objc-selector-args objc-selector [opt] : assignment-expression
30045 assignment-expression
30046 objc-comma-args , assignment-expression
30048 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30049 selector arguments and TREE_VALUE containing a list of comma
30053 cp_parser_objc_message_args (cp_parser
* parser
)
30055 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30056 bool maybe_unary_selector_p
= true;
30057 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30059 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30061 tree selector
= NULL_TREE
, arg
;
30063 if (token
->type
!= CPP_COLON
)
30064 selector
= cp_parser_objc_selector (parser
);
30066 /* Detect if we have a unary selector. */
30067 if (maybe_unary_selector_p
30068 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30069 return build_tree_list (selector
, NULL_TREE
);
30071 maybe_unary_selector_p
= false;
30072 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30073 arg
= cp_parser_assignment_expression (parser
);
30076 = chainon (sel_args
,
30077 build_tree_list (selector
, arg
));
30079 token
= cp_lexer_peek_token (parser
->lexer
);
30082 /* Handle non-selector arguments, if any. */
30083 while (token
->type
== CPP_COMMA
)
30087 cp_lexer_consume_token (parser
->lexer
);
30088 arg
= cp_parser_assignment_expression (parser
);
30091 = chainon (addl_args
,
30092 build_tree_list (NULL_TREE
, arg
));
30094 token
= cp_lexer_peek_token (parser
->lexer
);
30097 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30099 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30100 return build_tree_list (error_mark_node
, error_mark_node
);
30103 return build_tree_list (sel_args
, addl_args
);
30106 /* Parse an Objective-C encode expression.
30108 objc-encode-expression:
30109 @encode objc-typename
30111 Returns an encoded representation of the type argument. */
30114 cp_parser_objc_encode_expression (cp_parser
* parser
)
30118 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30120 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30121 matching_parens parens
;
30122 parens
.require_open (parser
);
30123 token
= cp_lexer_peek_token (parser
->lexer
);
30124 type
= complete_type (cp_parser_type_id (parser
));
30125 parens
.require_close (parser
);
30129 error_at (token
->location
,
30130 "%<@encode%> must specify a type as an argument");
30131 return error_mark_node
;
30134 /* This happens if we find @encode(T) (where T is a template
30135 typename or something dependent on a template typename) when
30136 parsing a template. In that case, we can't compile it
30137 immediately, but we rather create an AT_ENCODE_EXPR which will
30138 need to be instantiated when the template is used.
30140 if (dependent_type_p (type
))
30142 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30143 TREE_READONLY (value
) = 1;
30148 /* Build a location of the form:
30151 with caret==start at the @ token, finishing at the close paren. */
30152 location_t combined_loc
30153 = make_location (start_loc
, start_loc
,
30154 cp_lexer_previous_token (parser
->lexer
)->location
);
30156 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30159 /* Parse an Objective-C @defs expression. */
30162 cp_parser_objc_defs_expression (cp_parser
*parser
)
30166 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30167 matching_parens parens
;
30168 parens
.require_open (parser
);
30169 name
= cp_parser_identifier (parser
);
30170 parens
.require_close (parser
);
30172 return objc_get_class_ivars (name
);
30175 /* Parse an Objective-C protocol expression.
30177 objc-protocol-expression:
30178 @protocol ( identifier )
30180 Returns a representation of the protocol expression. */
30183 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30186 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30188 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30189 matching_parens parens
;
30190 parens
.require_open (parser
);
30191 proto
= cp_parser_identifier (parser
);
30192 parens
.require_close (parser
);
30194 /* Build a location of the form:
30197 with caret==start at the @ token, finishing at the close paren. */
30198 location_t combined_loc
30199 = make_location (start_loc
, start_loc
,
30200 cp_lexer_previous_token (parser
->lexer
)->location
);
30201 tree result
= objc_build_protocol_expr (proto
);
30202 protected_set_expr_location (result
, combined_loc
);
30206 /* Parse an Objective-C selector expression.
30208 objc-selector-expression:
30209 @selector ( objc-method-signature )
30211 objc-method-signature:
30217 objc-selector-seq objc-selector :
30219 Returns a representation of the method selector. */
30222 cp_parser_objc_selector_expression (cp_parser
* parser
)
30224 tree sel_seq
= NULL_TREE
;
30225 bool maybe_unary_selector_p
= true;
30227 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30229 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30230 matching_parens parens
;
30231 parens
.require_open (parser
);
30232 token
= cp_lexer_peek_token (parser
->lexer
);
30234 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30235 || token
->type
== CPP_SCOPE
)
30237 tree selector
= NULL_TREE
;
30239 if (token
->type
!= CPP_COLON
30240 || token
->type
== CPP_SCOPE
)
30241 selector
= cp_parser_objc_selector (parser
);
30243 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30244 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30246 /* Detect if we have a unary selector. */
30247 if (maybe_unary_selector_p
)
30249 sel_seq
= selector
;
30250 goto finish_selector
;
30254 cp_parser_error (parser
, "expected %<:%>");
30257 maybe_unary_selector_p
= false;
30258 token
= cp_lexer_consume_token (parser
->lexer
);
30260 if (token
->type
== CPP_SCOPE
)
30263 = chainon (sel_seq
,
30264 build_tree_list (selector
, NULL_TREE
));
30266 = chainon (sel_seq
,
30267 build_tree_list (NULL_TREE
, NULL_TREE
));
30271 = chainon (sel_seq
,
30272 build_tree_list (selector
, NULL_TREE
));
30274 token
= cp_lexer_peek_token (parser
->lexer
);
30278 parens
.require_close (parser
);
30281 /* Build a location of the form:
30284 with caret==start at the @ token, finishing at the close paren. */
30285 location_t combined_loc
30286 = make_location (loc
, loc
,
30287 cp_lexer_previous_token (parser
->lexer
)->location
);
30288 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30289 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30290 protected_set_expr_location (result
, combined_loc
);
30294 /* Parse a list of identifiers.
30296 objc-identifier-list:
30298 objc-identifier-list , identifier
30300 Returns a TREE_LIST of identifier nodes. */
30303 cp_parser_objc_identifier_list (cp_parser
* parser
)
30309 identifier
= cp_parser_identifier (parser
);
30310 if (identifier
== error_mark_node
)
30311 return error_mark_node
;
30313 list
= build_tree_list (NULL_TREE
, identifier
);
30314 sep
= cp_lexer_peek_token (parser
->lexer
);
30316 while (sep
->type
== CPP_COMMA
)
30318 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30319 identifier
= cp_parser_identifier (parser
);
30320 if (identifier
== error_mark_node
)
30323 list
= chainon (list
, build_tree_list (NULL_TREE
,
30325 sep
= cp_lexer_peek_token (parser
->lexer
);
30331 /* Parse an Objective-C alias declaration.
30333 objc-alias-declaration:
30334 @compatibility_alias identifier identifier ;
30336 This function registers the alias mapping with the Objective-C front end.
30337 It returns nothing. */
30340 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30344 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30345 alias
= cp_parser_identifier (parser
);
30346 orig
= cp_parser_identifier (parser
);
30347 objc_declare_alias (alias
, orig
);
30348 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30351 /* Parse an Objective-C class forward-declaration.
30353 objc-class-declaration:
30354 @class objc-identifier-list ;
30356 The function registers the forward declarations with the Objective-C
30357 front end. It returns nothing. */
30360 cp_parser_objc_class_declaration (cp_parser
* parser
)
30362 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30367 id
= cp_parser_identifier (parser
);
30368 if (id
== error_mark_node
)
30371 objc_declare_class (id
);
30373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30374 cp_lexer_consume_token (parser
->lexer
);
30378 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30381 /* Parse a list of Objective-C protocol references.
30383 objc-protocol-refs-opt:
30384 objc-protocol-refs [opt]
30386 objc-protocol-refs:
30387 < objc-identifier-list >
30389 Returns a TREE_LIST of identifiers, if any. */
30392 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30394 tree protorefs
= NULL_TREE
;
30396 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30398 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30399 protorefs
= cp_parser_objc_identifier_list (parser
);
30400 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30406 /* Parse a Objective-C visibility specification. */
30409 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30411 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30413 switch (vis
->keyword
)
30415 case RID_AT_PRIVATE
:
30416 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30418 case RID_AT_PROTECTED
:
30419 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30421 case RID_AT_PUBLIC
:
30422 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30424 case RID_AT_PACKAGE
:
30425 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30431 /* Eat '@private'/'@protected'/'@public'. */
30432 cp_lexer_consume_token (parser
->lexer
);
30435 /* Parse an Objective-C method type. Return 'true' if it is a class
30436 (+) method, and 'false' if it is an instance (-) method. */
30439 cp_parser_objc_method_type (cp_parser
* parser
)
30441 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30447 /* Parse an Objective-C protocol qualifier. */
30450 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30452 tree quals
= NULL_TREE
, node
;
30453 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30455 node
= token
->u
.value
;
30457 while (node
&& identifier_p (node
)
30458 && (node
== ridpointers
[(int) RID_IN
]
30459 || node
== ridpointers
[(int) RID_OUT
]
30460 || node
== ridpointers
[(int) RID_INOUT
]
30461 || node
== ridpointers
[(int) RID_BYCOPY
]
30462 || node
== ridpointers
[(int) RID_BYREF
]
30463 || node
== ridpointers
[(int) RID_ONEWAY
]))
30465 quals
= tree_cons (NULL_TREE
, node
, quals
);
30466 cp_lexer_consume_token (parser
->lexer
);
30467 token
= cp_lexer_peek_token (parser
->lexer
);
30468 node
= token
->u
.value
;
30474 /* Parse an Objective-C typename. */
30477 cp_parser_objc_typename (cp_parser
* parser
)
30479 tree type_name
= NULL_TREE
;
30481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30483 tree proto_quals
, cp_type
= NULL_TREE
;
30485 matching_parens parens
;
30486 parens
.consume_open (parser
); /* Eat '('. */
30487 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30489 /* An ObjC type name may consist of just protocol qualifiers, in which
30490 case the type shall default to 'id'. */
30491 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30493 cp_type
= cp_parser_type_id (parser
);
30495 /* If the type could not be parsed, an error has already
30496 been produced. For error recovery, behave as if it had
30497 not been specified, which will use the default type
30499 if (cp_type
== error_mark_node
)
30501 cp_type
= NULL_TREE
;
30502 /* We need to skip to the closing parenthesis as
30503 cp_parser_type_id() does not seem to do it for
30505 cp_parser_skip_to_closing_parenthesis (parser
,
30506 /*recovering=*/true,
30507 /*or_comma=*/false,
30508 /*consume_paren=*/false);
30512 parens
.require_close (parser
);
30513 type_name
= build_tree_list (proto_quals
, cp_type
);
30519 /* Check to see if TYPE refers to an Objective-C selector name. */
30522 cp_parser_objc_selector_p (enum cpp_ttype type
)
30524 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30525 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30526 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30527 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30528 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30531 /* Parse an Objective-C selector. */
30534 cp_parser_objc_selector (cp_parser
* parser
)
30536 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30538 if (!cp_parser_objc_selector_p (token
->type
))
30540 error_at (token
->location
, "invalid Objective-C++ selector name");
30541 return error_mark_node
;
30544 /* C++ operator names are allowed to appear in ObjC selectors. */
30545 switch (token
->type
)
30547 case CPP_AND_AND
: return get_identifier ("and");
30548 case CPP_AND_EQ
: return get_identifier ("and_eq");
30549 case CPP_AND
: return get_identifier ("bitand");
30550 case CPP_OR
: return get_identifier ("bitor");
30551 case CPP_COMPL
: return get_identifier ("compl");
30552 case CPP_NOT
: return get_identifier ("not");
30553 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30554 case CPP_OR_OR
: return get_identifier ("or");
30555 case CPP_OR_EQ
: return get_identifier ("or_eq");
30556 case CPP_XOR
: return get_identifier ("xor");
30557 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30558 default: return token
->u
.value
;
30562 /* Parse an Objective-C params list. */
30565 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30567 tree params
= NULL_TREE
;
30568 bool maybe_unary_selector_p
= true;
30569 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30571 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30573 tree selector
= NULL_TREE
, type_name
, identifier
;
30574 tree parm_attr
= NULL_TREE
;
30576 if (token
->keyword
== RID_ATTRIBUTE
)
30579 if (token
->type
!= CPP_COLON
)
30580 selector
= cp_parser_objc_selector (parser
);
30582 /* Detect if we have a unary selector. */
30583 if (maybe_unary_selector_p
30584 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30586 params
= selector
; /* Might be followed by attributes. */
30590 maybe_unary_selector_p
= false;
30591 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30593 /* Something went quite wrong. There should be a colon
30594 here, but there is not. Stop parsing parameters. */
30597 type_name
= cp_parser_objc_typename (parser
);
30598 /* New ObjC allows attributes on parameters too. */
30599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30600 parm_attr
= cp_parser_attributes_opt (parser
);
30601 identifier
= cp_parser_identifier (parser
);
30605 objc_build_keyword_decl (selector
,
30610 token
= cp_lexer_peek_token (parser
->lexer
);
30613 if (params
== NULL_TREE
)
30615 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30616 return error_mark_node
;
30619 /* We allow tail attributes for the method. */
30620 if (token
->keyword
== RID_ATTRIBUTE
)
30622 *attributes
= cp_parser_attributes_opt (parser
);
30623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30624 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30626 cp_parser_error (parser
,
30627 "method attributes must be specified at the end");
30628 return error_mark_node
;
30631 if (params
== NULL_TREE
)
30633 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30634 return error_mark_node
;
30639 /* Parse the non-keyword Objective-C params. */
30642 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30645 tree params
= make_node (TREE_LIST
);
30646 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30647 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30649 while (token
->type
== CPP_COMMA
)
30651 cp_parameter_declarator
*parmdecl
;
30654 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30655 token
= cp_lexer_peek_token (parser
->lexer
);
30657 if (token
->type
== CPP_ELLIPSIS
)
30659 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30661 token
= cp_lexer_peek_token (parser
->lexer
);
30665 /* TODO: parse attributes for tail parameters. */
30666 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30668 parm
= grokdeclarator (parmdecl
->declarator
,
30669 &parmdecl
->decl_specifiers
,
30670 PARM
, /*initialized=*/0,
30671 /*attrlist=*/NULL
);
30673 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30674 token
= cp_lexer_peek_token (parser
->lexer
);
30677 /* We allow tail attributes for the method. */
30678 if (token
->keyword
== RID_ATTRIBUTE
)
30680 if (*attributes
== NULL_TREE
)
30682 *attributes
= cp_parser_attributes_opt (parser
);
30683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30684 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30688 /* We have an error, but parse the attributes, so that we can
30690 *attributes
= cp_parser_attributes_opt (parser
);
30692 cp_parser_error (parser
,
30693 "method attributes must be specified at the end");
30694 return error_mark_node
;
30700 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30703 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30705 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30707 /* If the next token is `extern' and the following token is a string
30708 literal, then we have a linkage specification. */
30709 if (token
->keyword
== RID_EXTERN
30710 && cp_parser_is_pure_string_literal
30711 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30712 cp_parser_linkage_specification (parser
);
30713 /* Handle #pragma, if any. */
30714 else if (token
->type
== CPP_PRAGMA
)
30715 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30716 /* Allow stray semicolons. */
30717 else if (token
->type
== CPP_SEMICOLON
)
30718 cp_lexer_consume_token (parser
->lexer
);
30719 /* Mark methods as optional or required, when building protocols. */
30720 else if (token
->keyword
== RID_AT_OPTIONAL
)
30722 cp_lexer_consume_token (parser
->lexer
);
30723 objc_set_method_opt (true);
30725 else if (token
->keyword
== RID_AT_REQUIRED
)
30727 cp_lexer_consume_token (parser
->lexer
);
30728 objc_set_method_opt (false);
30730 else if (token
->keyword
== RID_NAMESPACE
)
30731 cp_parser_namespace_definition (parser
);
30732 /* Other stray characters must generate errors. */
30733 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30735 cp_lexer_consume_token (parser
->lexer
);
30736 error ("stray %qs between Objective-C++ methods",
30737 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30739 /* Finally, try to parse a block-declaration, or a function-definition. */
30741 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30744 /* Parse a method signature. */
30747 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30749 tree rettype
, kwdparms
, optparms
;
30750 bool ellipsis
= false;
30751 bool is_class_method
;
30753 is_class_method
= cp_parser_objc_method_type (parser
);
30754 rettype
= cp_parser_objc_typename (parser
);
30755 *attributes
= NULL_TREE
;
30756 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30757 if (kwdparms
== error_mark_node
)
30758 return error_mark_node
;
30759 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30760 if (optparms
== error_mark_node
)
30761 return error_mark_node
;
30763 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30767 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30770 cp_lexer_save_tokens (parser
->lexer
);
30771 tattr
= cp_parser_attributes_opt (parser
);
30772 gcc_assert (tattr
) ;
30774 /* If the attributes are followed by a method introducer, this is not allowed.
30775 Dump the attributes and flag the situation. */
30776 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30777 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30780 /* Otherwise, the attributes introduce some interstitial code, possibly so
30781 rewind to allow that check. */
30782 cp_lexer_rollback_tokens (parser
->lexer
);
30786 /* Parse an Objective-C method prototype list. */
30789 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30791 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30793 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30795 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30797 tree attributes
, sig
;
30798 bool is_class_method
;
30799 if (token
->type
== CPP_PLUS
)
30800 is_class_method
= true;
30802 is_class_method
= false;
30803 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30804 if (sig
== error_mark_node
)
30806 cp_parser_skip_to_end_of_block_or_statement (parser
);
30807 token
= cp_lexer_peek_token (parser
->lexer
);
30810 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30811 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30813 else if (token
->keyword
== RID_AT_PROPERTY
)
30814 cp_parser_objc_at_property_declaration (parser
);
30815 else if (token
->keyword
== RID_ATTRIBUTE
30816 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30817 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30819 "prefix attributes are ignored for methods");
30821 /* Allow for interspersed non-ObjC++ code. */
30822 cp_parser_objc_interstitial_code (parser
);
30824 token
= cp_lexer_peek_token (parser
->lexer
);
30827 if (token
->type
!= CPP_EOF
)
30828 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30830 cp_parser_error (parser
, "expected %<@end%>");
30832 objc_finish_interface ();
30835 /* Parse an Objective-C method definition list. */
30838 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30840 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30842 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30846 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30849 tree sig
, attribute
;
30850 bool is_class_method
;
30851 if (token
->type
== CPP_PLUS
)
30852 is_class_method
= true;
30854 is_class_method
= false;
30855 push_deferring_access_checks (dk_deferred
);
30856 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30857 if (sig
== error_mark_node
)
30859 cp_parser_skip_to_end_of_block_or_statement (parser
);
30860 token
= cp_lexer_peek_token (parser
->lexer
);
30863 objc_start_method_definition (is_class_method
, sig
, attribute
,
30866 /* For historical reasons, we accept an optional semicolon. */
30867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30868 cp_lexer_consume_token (parser
->lexer
);
30870 ptk
= cp_lexer_peek_token (parser
->lexer
);
30871 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30872 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30874 perform_deferred_access_checks (tf_warning_or_error
);
30875 stop_deferring_access_checks ();
30876 meth
= cp_parser_function_definition_after_declarator (parser
,
30878 pop_deferring_access_checks ();
30879 objc_finish_method_definition (meth
);
30882 /* The following case will be removed once @synthesize is
30883 completely implemented. */
30884 else if (token
->keyword
== RID_AT_PROPERTY
)
30885 cp_parser_objc_at_property_declaration (parser
);
30886 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30887 cp_parser_objc_at_synthesize_declaration (parser
);
30888 else if (token
->keyword
== RID_AT_DYNAMIC
)
30889 cp_parser_objc_at_dynamic_declaration (parser
);
30890 else if (token
->keyword
== RID_ATTRIBUTE
30891 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30892 warning_at (token
->location
, OPT_Wattributes
,
30893 "prefix attributes are ignored for methods");
30895 /* Allow for interspersed non-ObjC++ code. */
30896 cp_parser_objc_interstitial_code (parser
);
30898 token
= cp_lexer_peek_token (parser
->lexer
);
30901 if (token
->type
!= CPP_EOF
)
30902 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30904 cp_parser_error (parser
, "expected %<@end%>");
30906 objc_finish_implementation ();
30909 /* Parse Objective-C ivars. */
30912 cp_parser_objc_class_ivars (cp_parser
* parser
)
30914 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30916 if (token
->type
!= CPP_OPEN_BRACE
)
30917 return; /* No ivars specified. */
30919 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30920 token
= cp_lexer_peek_token (parser
->lexer
);
30922 while (token
->type
!= CPP_CLOSE_BRACE
30923 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30925 cp_decl_specifier_seq declspecs
;
30926 int decl_class_or_enum_p
;
30927 tree prefix_attributes
;
30929 cp_parser_objc_visibility_spec (parser
);
30931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30934 cp_parser_decl_specifier_seq (parser
,
30935 CP_PARSER_FLAGS_OPTIONAL
,
30937 &decl_class_or_enum_p
);
30939 /* auto, register, static, extern, mutable. */
30940 if (declspecs
.storage_class
!= sc_none
)
30942 cp_parser_error (parser
, "invalid type for instance variable");
30943 declspecs
.storage_class
= sc_none
;
30946 /* thread_local. */
30947 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30949 cp_parser_error (parser
, "invalid type for instance variable");
30950 declspecs
.locations
[ds_thread
] = 0;
30954 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30956 cp_parser_error (parser
, "invalid type for instance variable");
30957 declspecs
.locations
[ds_typedef
] = 0;
30960 prefix_attributes
= declspecs
.attributes
;
30961 declspecs
.attributes
= NULL_TREE
;
30963 /* Keep going until we hit the `;' at the end of the
30965 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30967 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30968 cp_declarator
*declarator
= NULL
;
30969 int ctor_dtor_or_conv_p
;
30971 /* Check for a (possibly unnamed) bitfield declaration. */
30972 token
= cp_lexer_peek_token (parser
->lexer
);
30973 if (token
->type
== CPP_COLON
)
30976 if (token
->type
== CPP_NAME
30977 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30980 /* Get the name of the bitfield. */
30981 declarator
= make_id_declarator (NULL_TREE
,
30982 cp_parser_identifier (parser
),
30983 sfk_none
, token
->location
);
30986 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30987 /* Get the width of the bitfield. */
30989 = cp_parser_constant_expression (parser
);
30993 /* Parse the declarator. */
30995 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30996 CP_PARSER_FLAGS_NONE
,
30997 &ctor_dtor_or_conv_p
,
30998 /*parenthesized_p=*/NULL
,
30999 /*member_p=*/false,
31000 /*friend_p=*/false);
31003 /* Look for attributes that apply to the ivar. */
31004 attributes
= cp_parser_attributes_opt (parser
);
31005 /* Remember which attributes are prefix attributes and
31007 first_attribute
= attributes
;
31008 /* Combine the attributes. */
31009 attributes
= attr_chainon (prefix_attributes
, attributes
);
31012 /* Create the bitfield declaration. */
31013 decl
= grokbitfield (declarator
, &declspecs
,
31014 width
, NULL_TREE
, attributes
);
31016 decl
= grokfield (declarator
, &declspecs
,
31017 NULL_TREE
, /*init_const_expr_p=*/false,
31018 NULL_TREE
, attributes
);
31020 /* Add the instance variable. */
31021 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31022 objc_add_instance_variable (decl
);
31024 /* Reset PREFIX_ATTRIBUTES. */
31025 if (attributes
!= error_mark_node
)
31027 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31028 attributes
= TREE_CHAIN (attributes
);
31030 TREE_CHAIN (attributes
) = NULL_TREE
;
31033 token
= cp_lexer_peek_token (parser
->lexer
);
31035 if (token
->type
== CPP_COMMA
)
31037 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31043 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31044 token
= cp_lexer_peek_token (parser
->lexer
);
31047 if (token
->keyword
== RID_AT_END
)
31048 cp_parser_error (parser
, "expected %<}%>");
31050 /* Do not consume the RID_AT_END, so it will be read again as terminating
31051 the @interface of @implementation. */
31052 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31053 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31055 /* For historical reasons, we accept an optional semicolon. */
31056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31057 cp_lexer_consume_token (parser
->lexer
);
31060 /* Parse an Objective-C protocol declaration. */
31063 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31065 tree proto
, protorefs
;
31068 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31071 tok
= cp_lexer_peek_token (parser
->lexer
);
31072 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31073 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31077 /* See if we have a forward declaration or a definition. */
31078 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31080 /* Try a forward declaration first. */
31081 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31087 id
= cp_parser_identifier (parser
);
31088 if (id
== error_mark_node
)
31091 objc_declare_protocol (id
, attributes
);
31093 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31094 cp_lexer_consume_token (parser
->lexer
);
31098 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31101 /* Ok, we got a full-fledged definition (or at least should). */
31104 proto
= cp_parser_identifier (parser
);
31105 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31106 objc_start_protocol (proto
, protorefs
, attributes
);
31107 cp_parser_objc_method_prototype_list (parser
);
31111 /* Parse an Objective-C superclass or category. */
31114 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31117 tree
*categ
, bool *is_class_extension
)
31119 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31121 *super
= *categ
= NULL_TREE
;
31122 *is_class_extension
= false;
31123 if (next
->type
== CPP_COLON
)
31125 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31126 *super
= cp_parser_identifier (parser
);
31128 else if (next
->type
== CPP_OPEN_PAREN
)
31130 matching_parens parens
;
31131 parens
.consume_open (parser
); /* Eat '('. */
31133 /* If there is no category name, and this is an @interface, we
31134 have a class extension. */
31135 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31137 *categ
= NULL_TREE
;
31138 *is_class_extension
= true;
31141 *categ
= cp_parser_identifier (parser
);
31143 parens
.require_close (parser
);
31147 /* Parse an Objective-C class interface. */
31150 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31152 tree name
, super
, categ
, protos
;
31153 bool is_class_extension
;
31155 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31156 name
= cp_parser_identifier (parser
);
31157 if (name
== error_mark_node
)
31159 /* It's hard to recover because even if valid @interface stuff
31160 is to follow, we can't compile it (or validate it) if we
31161 don't even know which class it refers to. Let's assume this
31162 was a stray '@interface' token in the stream and skip it.
31166 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31167 &is_class_extension
);
31168 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31170 /* We have either a class or a category on our hands. */
31171 if (categ
|| is_class_extension
)
31172 objc_start_category_interface (name
, categ
, protos
, attributes
);
31175 objc_start_class_interface (name
, super
, protos
, attributes
);
31176 /* Handle instance variable declarations, if any. */
31177 cp_parser_objc_class_ivars (parser
);
31178 objc_continue_interface ();
31181 cp_parser_objc_method_prototype_list (parser
);
31184 /* Parse an Objective-C class implementation. */
31187 cp_parser_objc_class_implementation (cp_parser
* parser
)
31189 tree name
, super
, categ
;
31190 bool is_class_extension
;
31192 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31193 name
= cp_parser_identifier (parser
);
31194 if (name
== error_mark_node
)
31196 /* It's hard to recover because even if valid @implementation
31197 stuff is to follow, we can't compile it (or validate it) if
31198 we don't even know which class it refers to. Let's assume
31199 this was a stray '@implementation' token in the stream and
31204 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31205 &is_class_extension
);
31207 /* We have either a class or a category on our hands. */
31209 objc_start_category_implementation (name
, categ
);
31212 objc_start_class_implementation (name
, super
);
31213 /* Handle instance variable declarations, if any. */
31214 cp_parser_objc_class_ivars (parser
);
31215 objc_continue_implementation ();
31218 cp_parser_objc_method_definition_list (parser
);
31221 /* Consume the @end token and finish off the implementation. */
31224 cp_parser_objc_end_implementation (cp_parser
* parser
)
31226 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31227 objc_finish_implementation ();
31230 /* Parse an Objective-C declaration. */
31233 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31235 /* Try to figure out what kind of declaration is present. */
31236 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31239 switch (kwd
->keyword
)
31244 error_at (kwd
->location
, "attributes may not be specified before"
31245 " the %<@%D%> Objective-C++ keyword",
31249 case RID_AT_IMPLEMENTATION
:
31250 warning_at (kwd
->location
, OPT_Wattributes
,
31251 "prefix attributes are ignored before %<@%D%>",
31258 switch (kwd
->keyword
)
31261 cp_parser_objc_alias_declaration (parser
);
31264 cp_parser_objc_class_declaration (parser
);
31266 case RID_AT_PROTOCOL
:
31267 cp_parser_objc_protocol_declaration (parser
, attributes
);
31269 case RID_AT_INTERFACE
:
31270 cp_parser_objc_class_interface (parser
, attributes
);
31272 case RID_AT_IMPLEMENTATION
:
31273 cp_parser_objc_class_implementation (parser
);
31276 cp_parser_objc_end_implementation (parser
);
31279 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31281 cp_parser_skip_to_end_of_block_or_statement (parser
);
31285 /* Parse an Objective-C try-catch-finally statement.
31287 objc-try-catch-finally-stmt:
31288 @try compound-statement objc-catch-clause-seq [opt]
31289 objc-finally-clause [opt]
31291 objc-catch-clause-seq:
31292 objc-catch-clause objc-catch-clause-seq [opt]
31295 @catch ( objc-exception-declaration ) compound-statement
31297 objc-finally-clause:
31298 @finally compound-statement
31300 objc-exception-declaration:
31301 parameter-declaration
31304 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31308 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31309 for C. Keep them in sync. */
31312 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31314 location_t location
;
31317 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31318 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31319 objc_maybe_warn_exceptions (location
);
31320 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31321 node, lest it get absorbed into the surrounding block. */
31322 stmt
= push_stmt_list ();
31323 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31324 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31326 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31328 cp_parameter_declarator
*parm
;
31329 tree parameter_declaration
= error_mark_node
;
31330 bool seen_open_paren
= false;
31331 matching_parens parens
;
31333 cp_lexer_consume_token (parser
->lexer
);
31334 if (parens
.require_open (parser
))
31335 seen_open_paren
= true;
31336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31338 /* We have "@catch (...)" (where the '...' are literally
31339 what is in the code). Skip the '...'.
31340 parameter_declaration is set to NULL_TREE, and
31341 objc_being_catch_clauses() knows that that means
31343 cp_lexer_consume_token (parser
->lexer
);
31344 parameter_declaration
= NULL_TREE
;
31348 /* We have "@catch (NSException *exception)" or something
31349 like that. Parse the parameter declaration. */
31350 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31353 parameter_declaration
= error_mark_node
;
31355 parameter_declaration
= grokdeclarator (parm
->declarator
,
31356 &parm
->decl_specifiers
,
31357 PARM
, /*initialized=*/0,
31358 /*attrlist=*/NULL
);
31360 if (seen_open_paren
)
31361 parens
.require_close (parser
);
31364 /* If there was no open parenthesis, we are recovering from
31365 an error, and we are trying to figure out what mistake
31366 the user has made. */
31368 /* If there is an immediate closing parenthesis, the user
31369 probably forgot the opening one (ie, they typed "@catch
31370 NSException *e)". Parse the closing parenthesis and keep
31372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31373 cp_lexer_consume_token (parser
->lexer
);
31375 /* If these is no immediate closing parenthesis, the user
31376 probably doesn't know that parenthesis are required at
31377 all (ie, they typed "@catch NSException *e"). So, just
31378 forget about the closing parenthesis and keep going. */
31380 objc_begin_catch_clause (parameter_declaration
);
31381 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31382 objc_finish_catch_clause ();
31384 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31386 cp_lexer_consume_token (parser
->lexer
);
31387 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31388 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31389 node, lest it get absorbed into the surrounding block. */
31390 stmt
= push_stmt_list ();
31391 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31392 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31395 return objc_finish_try_stmt ();
31398 /* Parse an Objective-C synchronized statement.
31400 objc-synchronized-stmt:
31401 @synchronized ( expression ) compound-statement
31403 Returns NULL_TREE. */
31406 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31408 location_t location
;
31411 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31413 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31414 objc_maybe_warn_exceptions (location
);
31415 matching_parens parens
;
31416 parens
.require_open (parser
);
31417 lock
= cp_parser_expression (parser
);
31418 parens
.require_close (parser
);
31420 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31421 node, lest it get absorbed into the surrounding block. */
31422 stmt
= push_stmt_list ();
31423 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31425 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31428 /* Parse an Objective-C throw statement.
31431 @throw assignment-expression [opt] ;
31433 Returns a constructed '@throw' statement. */
31436 cp_parser_objc_throw_statement (cp_parser
*parser
)
31438 tree expr
= NULL_TREE
;
31439 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31441 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31443 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31444 expr
= cp_parser_expression (parser
);
31446 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31448 return objc_build_throw_stmt (loc
, expr
);
31451 /* Parse an Objective-C statement. */
31454 cp_parser_objc_statement (cp_parser
* parser
)
31456 /* Try to figure out what kind of declaration is present. */
31457 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31459 switch (kwd
->keyword
)
31462 return cp_parser_objc_try_catch_finally_statement (parser
);
31463 case RID_AT_SYNCHRONIZED
:
31464 return cp_parser_objc_synchronized_statement (parser
);
31466 return cp_parser_objc_throw_statement (parser
);
31468 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31470 cp_parser_skip_to_end_of_block_or_statement (parser
);
31473 return error_mark_node
;
31476 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31477 look ahead to see if an objc keyword follows the attributes. This
31478 is to detect the use of prefix attributes on ObjC @interface and
31482 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31484 cp_lexer_save_tokens (parser
->lexer
);
31485 *attrib
= cp_parser_attributes_opt (parser
);
31486 gcc_assert (*attrib
);
31487 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31489 cp_lexer_commit_tokens (parser
->lexer
);
31492 cp_lexer_rollback_tokens (parser
->lexer
);
31496 /* This routine is a minimal replacement for
31497 c_parser_struct_declaration () used when parsing the list of
31498 types/names or ObjC++ properties. For example, when parsing the
31501 @property (readonly) int a, b, c;
31503 this function is responsible for parsing "int a, int b, int c" and
31504 returning the declarations as CHAIN of DECLs.
31506 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31507 similar parsing. */
31509 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31511 tree decls
= NULL_TREE
;
31512 cp_decl_specifier_seq declspecs
;
31513 int decl_class_or_enum_p
;
31514 tree prefix_attributes
;
31516 cp_parser_decl_specifier_seq (parser
,
31517 CP_PARSER_FLAGS_NONE
,
31519 &decl_class_or_enum_p
);
31521 if (declspecs
.type
== error_mark_node
)
31522 return error_mark_node
;
31524 /* auto, register, static, extern, mutable. */
31525 if (declspecs
.storage_class
!= sc_none
)
31527 cp_parser_error (parser
, "invalid type for property");
31528 declspecs
.storage_class
= sc_none
;
31531 /* thread_local. */
31532 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31534 cp_parser_error (parser
, "invalid type for property");
31535 declspecs
.locations
[ds_thread
] = 0;
31539 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31541 cp_parser_error (parser
, "invalid type for property");
31542 declspecs
.locations
[ds_typedef
] = 0;
31545 prefix_attributes
= declspecs
.attributes
;
31546 declspecs
.attributes
= NULL_TREE
;
31548 /* Keep going until we hit the `;' at the end of the declaration. */
31549 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31551 tree attributes
, first_attribute
, decl
;
31552 cp_declarator
*declarator
;
31555 /* Parse the declarator. */
31556 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31557 CP_PARSER_FLAGS_NONE
,
31558 NULL
, NULL
, false, false);
31560 /* Look for attributes that apply to the ivar. */
31561 attributes
= cp_parser_attributes_opt (parser
);
31562 /* Remember which attributes are prefix attributes and
31564 first_attribute
= attributes
;
31565 /* Combine the attributes. */
31566 attributes
= attr_chainon (prefix_attributes
, attributes
);
31568 decl
= grokfield (declarator
, &declspecs
,
31569 NULL_TREE
, /*init_const_expr_p=*/false,
31570 NULL_TREE
, attributes
);
31572 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31573 return error_mark_node
;
31575 /* Reset PREFIX_ATTRIBUTES. */
31576 if (attributes
!= error_mark_node
)
31578 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31579 attributes
= TREE_CHAIN (attributes
);
31581 TREE_CHAIN (attributes
) = NULL_TREE
;
31584 DECL_CHAIN (decl
) = decls
;
31587 token
= cp_lexer_peek_token (parser
->lexer
);
31588 if (token
->type
== CPP_COMMA
)
31590 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31599 /* Parse an Objective-C @property declaration. The syntax is:
31601 objc-property-declaration:
31602 '@property' objc-property-attributes[opt] struct-declaration ;
31604 objc-property-attributes:
31605 '(' objc-property-attribute-list ')'
31607 objc-property-attribute-list:
31608 objc-property-attribute
31609 objc-property-attribute-list, objc-property-attribute
31611 objc-property-attribute
31612 'getter' = identifier
31613 'setter' = identifier
31622 @property NSString *name;
31623 @property (readonly) id object;
31624 @property (retain, nonatomic, getter=getTheName) id name;
31625 @property int a, b, c;
31627 PS: This function is identical to
31628 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31630 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31632 /* The following variables hold the attributes of the properties as
31633 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31634 seen. When we see an attribute, we set them to 'true' (if they
31635 are boolean properties) or to the identifier (if they have an
31636 argument, ie, for getter and setter). Note that here we only
31637 parse the list of attributes, check the syntax and accumulate the
31638 attributes that we find. objc_add_property_declaration() will
31639 then process the information. */
31640 bool property_assign
= false;
31641 bool property_copy
= false;
31642 tree property_getter_ident
= NULL_TREE
;
31643 bool property_nonatomic
= false;
31644 bool property_readonly
= false;
31645 bool property_readwrite
= false;
31646 bool property_retain
= false;
31647 tree property_setter_ident
= NULL_TREE
;
31649 /* 'properties' is the list of properties that we read. Usually a
31650 single one, but maybe more (eg, in "@property int a, b, c;" there
31655 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31657 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31659 /* Parse the optional attribute list... */
31660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31663 matching_parens parens
;
31664 parens
.consume_open (parser
);
31668 bool syntax_error
= false;
31669 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31672 if (token
->type
!= CPP_NAME
)
31674 cp_parser_error (parser
, "expected identifier");
31677 keyword
= C_RID_CODE (token
->u
.value
);
31678 cp_lexer_consume_token (parser
->lexer
);
31681 case RID_ASSIGN
: property_assign
= true; break;
31682 case RID_COPY
: property_copy
= true; break;
31683 case RID_NONATOMIC
: property_nonatomic
= true; break;
31684 case RID_READONLY
: property_readonly
= true; break;
31685 case RID_READWRITE
: property_readwrite
= true; break;
31686 case RID_RETAIN
: property_retain
= true; break;
31690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31692 if (keyword
== RID_GETTER
)
31693 cp_parser_error (parser
,
31694 "missing %<=%> (after %<getter%> attribute)");
31696 cp_parser_error (parser
,
31697 "missing %<=%> (after %<setter%> attribute)");
31698 syntax_error
= true;
31701 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31702 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31704 cp_parser_error (parser
, "expected identifier");
31705 syntax_error
= true;
31708 if (keyword
== RID_SETTER
)
31710 if (property_setter_ident
!= NULL_TREE
)
31712 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31713 cp_lexer_consume_token (parser
->lexer
);
31716 property_setter_ident
= cp_parser_objc_selector (parser
);
31717 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31718 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31720 cp_lexer_consume_token (parser
->lexer
);
31724 if (property_getter_ident
!= NULL_TREE
)
31726 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31727 cp_lexer_consume_token (parser
->lexer
);
31730 property_getter_ident
= cp_parser_objc_selector (parser
);
31734 cp_parser_error (parser
, "unknown property attribute");
31735 syntax_error
= true;
31742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31743 cp_lexer_consume_token (parser
->lexer
);
31748 /* FIXME: "@property (setter, assign);" will generate a spurious
31749 "error: expected ‘)’ before ‘,’ token". This is because
31750 cp_parser_require, unlike the C counterpart, will produce an
31751 error even if we are in error recovery. */
31752 if (!parens
.require_close (parser
))
31754 cp_parser_skip_to_closing_parenthesis (parser
,
31755 /*recovering=*/true,
31756 /*or_comma=*/false,
31757 /*consume_paren=*/true);
31761 /* ... and the property declaration(s). */
31762 properties
= cp_parser_objc_struct_declaration (parser
);
31764 if (properties
== error_mark_node
)
31766 cp_parser_skip_to_end_of_statement (parser
);
31767 /* If the next token is now a `;', consume it. */
31768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31769 cp_lexer_consume_token (parser
->lexer
);
31773 if (properties
== NULL_TREE
)
31774 cp_parser_error (parser
, "expected identifier");
31777 /* Comma-separated properties are chained together in
31778 reverse order; add them one by one. */
31779 properties
= nreverse (properties
);
31781 for (; properties
; properties
= TREE_CHAIN (properties
))
31782 objc_add_property_declaration (loc
, copy_node (properties
),
31783 property_readonly
, property_readwrite
,
31784 property_assign
, property_retain
,
31785 property_copy
, property_nonatomic
,
31786 property_getter_ident
, property_setter_ident
);
31789 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31792 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31794 objc-synthesize-declaration:
31795 @synthesize objc-synthesize-identifier-list ;
31797 objc-synthesize-identifier-list:
31798 objc-synthesize-identifier
31799 objc-synthesize-identifier-list, objc-synthesize-identifier
31801 objc-synthesize-identifier
31803 identifier = identifier
31806 @synthesize MyProperty;
31807 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31809 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31810 for C. Keep them in sync.
31813 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31815 tree list
= NULL_TREE
;
31817 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31819 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31822 tree property
, ivar
;
31823 property
= cp_parser_identifier (parser
);
31824 if (property
== error_mark_node
)
31826 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31829 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31831 cp_lexer_consume_token (parser
->lexer
);
31832 ivar
= cp_parser_identifier (parser
);
31833 if (ivar
== error_mark_node
)
31835 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31841 list
= chainon (list
, build_tree_list (ivar
, property
));
31842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31843 cp_lexer_consume_token (parser
->lexer
);
31847 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31848 objc_add_synthesize_declaration (loc
, list
);
31851 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31853 objc-dynamic-declaration:
31854 @dynamic identifier-list ;
31857 @dynamic MyProperty;
31858 @dynamic MyProperty, AnotherProperty;
31860 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31861 for C. Keep them in sync.
31864 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31866 tree list
= NULL_TREE
;
31868 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31870 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31874 property
= cp_parser_identifier (parser
);
31875 if (property
== error_mark_node
)
31877 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31880 list
= chainon (list
, build_tree_list (NULL
, property
));
31881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31882 cp_lexer_consume_token (parser
->lexer
);
31886 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31887 objc_add_dynamic_declaration (loc
, list
);
31891 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31893 /* Returns name of the next clause.
31894 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31895 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31896 returned and the token is consumed. */
31898 static pragma_omp_clause
31899 cp_parser_omp_clause_name (cp_parser
*parser
)
31901 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31903 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31904 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31905 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31906 result
= PRAGMA_OMP_CLAUSE_IF
;
31907 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31908 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31909 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31910 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31911 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31912 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31913 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31914 result
= PRAGMA_OMP_CLAUSE_FOR
;
31915 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31917 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31918 const char *p
= IDENTIFIER_POINTER (id
);
31923 if (!strcmp ("aligned", p
))
31924 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31925 else if (!strcmp ("async", p
))
31926 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31929 if (!strcmp ("collapse", p
))
31930 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31931 else if (!strcmp ("copy", p
))
31932 result
= PRAGMA_OACC_CLAUSE_COPY
;
31933 else if (!strcmp ("copyin", p
))
31934 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31935 else if (!strcmp ("copyout", p
))
31936 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31937 else if (!strcmp ("copyprivate", p
))
31938 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31939 else if (!strcmp ("create", p
))
31940 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31943 if (!strcmp ("defaultmap", p
))
31944 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31945 else if (!strcmp ("depend", p
))
31946 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31947 else if (!strcmp ("device", p
))
31948 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31949 else if (!strcmp ("deviceptr", p
))
31950 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31951 else if (!strcmp ("device_resident", p
))
31952 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31953 else if (!strcmp ("dist_schedule", p
))
31954 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31957 if (!strcmp ("final", p
))
31958 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31959 else if (!strcmp ("finalize", p
))
31960 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31961 else if (!strcmp ("firstprivate", p
))
31962 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31963 else if (!strcmp ("from", p
))
31964 result
= PRAGMA_OMP_CLAUSE_FROM
;
31967 if (!strcmp ("gang", p
))
31968 result
= PRAGMA_OACC_CLAUSE_GANG
;
31969 else if (!strcmp ("grainsize", p
))
31970 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31973 if (!strcmp ("hint", p
))
31974 result
= PRAGMA_OMP_CLAUSE_HINT
;
31975 else if (!strcmp ("host", p
))
31976 result
= PRAGMA_OACC_CLAUSE_HOST
;
31979 if (!strcmp ("if_present", p
))
31980 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31981 else if (!strcmp ("in_reduction", p
))
31982 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
31983 else if (!strcmp ("inbranch", p
))
31984 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31985 else if (!strcmp ("independent", p
))
31986 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31987 else if (!strcmp ("is_device_ptr", p
))
31988 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31991 if (!strcmp ("lastprivate", p
))
31992 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31993 else if (!strcmp ("linear", p
))
31994 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31995 else if (!strcmp ("link", p
))
31996 result
= PRAGMA_OMP_CLAUSE_LINK
;
31999 if (!strcmp ("map", p
))
32000 result
= PRAGMA_OMP_CLAUSE_MAP
;
32001 else if (!strcmp ("mergeable", p
))
32002 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32005 if (!strcmp ("nogroup", p
))
32006 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32007 else if (!strcmp ("nontemporal", p
))
32008 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32009 else if (!strcmp ("notinbranch", p
))
32010 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32011 else if (!strcmp ("nowait", p
))
32012 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32013 else if (!strcmp ("num_gangs", p
))
32014 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32015 else if (!strcmp ("num_tasks", p
))
32016 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32017 else if (!strcmp ("num_teams", p
))
32018 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32019 else if (!strcmp ("num_threads", p
))
32020 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32021 else if (!strcmp ("num_workers", p
))
32022 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32025 if (!strcmp ("ordered", p
))
32026 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32029 if (!strcmp ("parallel", p
))
32030 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32031 else if (!strcmp ("present", p
))
32032 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32033 else if (!strcmp ("present_or_copy", p
)
32034 || !strcmp ("pcopy", p
))
32035 result
= PRAGMA_OACC_CLAUSE_COPY
;
32036 else if (!strcmp ("present_or_copyin", p
)
32037 || !strcmp ("pcopyin", p
))
32038 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32039 else if (!strcmp ("present_or_copyout", p
)
32040 || !strcmp ("pcopyout", p
))
32041 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32042 else if (!strcmp ("present_or_create", p
)
32043 || !strcmp ("pcreate", p
))
32044 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32045 else if (!strcmp ("priority", p
))
32046 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32047 else if (!strcmp ("proc_bind", p
))
32048 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32051 if (!strcmp ("reduction", p
))
32052 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32055 if (!strcmp ("safelen", p
))
32056 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32057 else if (!strcmp ("schedule", p
))
32058 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32059 else if (!strcmp ("sections", p
))
32060 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32061 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32062 result
= PRAGMA_OACC_CLAUSE_HOST
;
32063 else if (!strcmp ("seq", p
))
32064 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32065 else if (!strcmp ("shared", p
))
32066 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32067 else if (!strcmp ("simd", p
))
32068 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32069 else if (!strcmp ("simdlen", p
))
32070 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32073 if (!strcmp ("task_reduction", p
))
32074 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32075 else if (!strcmp ("taskgroup", p
))
32076 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32077 else if (!strcmp ("thread_limit", p
))
32078 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32079 else if (!strcmp ("threads", p
))
32080 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32081 else if (!strcmp ("tile", p
))
32082 result
= PRAGMA_OACC_CLAUSE_TILE
;
32083 else if (!strcmp ("to", p
))
32084 result
= PRAGMA_OMP_CLAUSE_TO
;
32087 if (!strcmp ("uniform", p
))
32088 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32089 else if (!strcmp ("untied", p
))
32090 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32091 else if (!strcmp ("use_device", p
))
32092 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32093 else if (!strcmp ("use_device_ptr", p
))
32094 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32097 if (!strcmp ("vector", p
))
32098 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32099 else if (!strcmp ("vector_length", p
))
32100 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32103 if (!strcmp ("wait", p
))
32104 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32105 else if (!strcmp ("worker", p
))
32106 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32111 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32112 cp_lexer_consume_token (parser
->lexer
);
32117 /* Validate that a clause of the given type does not already exist. */
32120 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32121 const char *name
, location_t location
)
32125 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32126 if (OMP_CLAUSE_CODE (c
) == code
)
32128 error_at (location
, "too many %qs clauses", name
);
32136 variable-list , identifier
32138 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32139 colon). An opening parenthesis will have been consumed by the caller.
32141 If KIND is nonzero, create the appropriate node and install the decl
32142 in OMP_CLAUSE_DECL and add the node to the head of the list.
32144 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32145 return the list created.
32147 COLON can be NULL if only closing parenthesis should end the list,
32148 or pointer to bool which will receive false if the list is terminated
32149 by closing parenthesis or true if the list is terminated by colon. */
32152 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32153 tree list
, bool *colon
)
32156 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32159 parser
->colon_corrects_to_scope_p
= false;
32166 if (kind
== OMP_CLAUSE_DEPEND
)
32167 cp_parser_parse_tentatively (parser
);
32168 token
= cp_lexer_peek_token (parser
->lexer
);
32170 && current_class_ptr
32171 && cp_parser_is_keyword (token
, RID_THIS
))
32173 decl
= finish_this_expr ();
32174 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32175 || CONVERT_EXPR_P (decl
))
32176 decl
= TREE_OPERAND (decl
, 0);
32177 cp_lexer_consume_token (parser
->lexer
);
32181 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32182 /*check_dependency_p=*/true,
32183 /*template_p=*/NULL
,
32184 /*declarator_p=*/false,
32185 /*optional_p=*/false);
32186 if (name
== error_mark_node
)
32188 if (kind
== OMP_CLAUSE_DEPEND
32189 && cp_parser_simulate_error (parser
))
32190 goto depend_lvalue
;
32194 if (identifier_p (name
))
32195 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32198 if (decl
== error_mark_node
)
32200 if (kind
== OMP_CLAUSE_DEPEND
32201 && cp_parser_simulate_error (parser
))
32202 goto depend_lvalue
;
32203 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32207 if (decl
== error_mark_node
)
32209 else if (kind
!= 0)
32213 case OMP_CLAUSE__CACHE_
:
32214 /* The OpenACC cache directive explicitly only allows "array
32215 elements or subarrays". */
32216 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32218 error_at (token
->location
, "expected %<[%>");
32219 decl
= error_mark_node
;
32223 case OMP_CLAUSE_MAP
:
32224 case OMP_CLAUSE_FROM
:
32225 case OMP_CLAUSE_TO
:
32226 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32229 = cp_lexer_peek_token (parser
->lexer
)->location
;
32230 cp_id_kind idk
= CP_ID_KIND_NONE
;
32231 cp_lexer_consume_token (parser
->lexer
);
32232 decl
= convert_from_reference (decl
);
32234 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32239 case OMP_CLAUSE_DEPEND
:
32240 case OMP_CLAUSE_REDUCTION
:
32241 case OMP_CLAUSE_IN_REDUCTION
:
32242 case OMP_CLAUSE_TASK_REDUCTION
:
32243 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32245 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32247 parser
->colon_corrects_to_scope_p
= false;
32248 cp_lexer_consume_token (parser
->lexer
);
32249 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32250 low_bound
= cp_parser_expression (parser
);
32252 parser
->colon_corrects_to_scope_p
32253 = saved_colon_corrects_to_scope_p
;
32254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32255 length
= integer_one_node
;
32258 /* Look for `:'. */
32259 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32261 if (kind
== OMP_CLAUSE_DEPEND
32262 && cp_parser_simulate_error (parser
))
32263 goto depend_lvalue
;
32266 if (kind
== OMP_CLAUSE_DEPEND
)
32267 cp_parser_commit_to_tentative_parse (parser
);
32268 if (!cp_lexer_next_token_is (parser
->lexer
,
32270 length
= cp_parser_expression (parser
);
32272 /* Look for the closing `]'. */
32273 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32276 if (kind
== OMP_CLAUSE_DEPEND
32277 && cp_parser_simulate_error (parser
))
32278 goto depend_lvalue
;
32282 decl
= tree_cons (low_bound
, length
, decl
);
32289 if (kind
== OMP_CLAUSE_DEPEND
)
32291 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32292 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32293 && cp_parser_simulate_error (parser
))
32296 cp_parser_abort_tentative_parse (parser
);
32297 decl
= cp_parser_assignment_expression (parser
, NULL
,
32301 cp_parser_parse_definitely (parser
);
32304 tree u
= build_omp_clause (token
->location
, kind
);
32305 OMP_CLAUSE_DECL (u
) = decl
;
32306 OMP_CLAUSE_CHAIN (u
) = list
;
32310 list
= tree_cons (decl
, NULL_TREE
, list
);
32313 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32315 cp_lexer_consume_token (parser
->lexer
);
32319 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32321 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32324 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32328 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32332 /* Try to resync to an unnested comma. Copied from
32333 cp_parser_parenthesized_expression_list. */
32336 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32337 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32338 /*recovering=*/true,
32340 /*consume_paren=*/true);
32348 /* Similarly, but expect leading and trailing parenthesis. This is a very
32349 common case for omp clauses. */
32352 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32354 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32355 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32360 copy ( variable-list )
32361 copyin ( variable-list )
32362 copyout ( variable-list )
32363 create ( variable-list )
32364 delete ( variable-list )
32365 present ( variable-list ) */
32368 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32371 enum gomp_map_kind kind
;
32374 case PRAGMA_OACC_CLAUSE_COPY
:
32375 kind
= GOMP_MAP_TOFROM
;
32377 case PRAGMA_OACC_CLAUSE_COPYIN
:
32378 kind
= GOMP_MAP_TO
;
32380 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32381 kind
= GOMP_MAP_FROM
;
32383 case PRAGMA_OACC_CLAUSE_CREATE
:
32384 kind
= GOMP_MAP_ALLOC
;
32386 case PRAGMA_OACC_CLAUSE_DELETE
:
32387 kind
= GOMP_MAP_RELEASE
;
32389 case PRAGMA_OACC_CLAUSE_DEVICE
:
32390 kind
= GOMP_MAP_FORCE_TO
;
32392 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32393 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32395 case PRAGMA_OACC_CLAUSE_HOST
:
32396 kind
= GOMP_MAP_FORCE_FROM
;
32398 case PRAGMA_OACC_CLAUSE_LINK
:
32399 kind
= GOMP_MAP_LINK
;
32401 case PRAGMA_OACC_CLAUSE_PRESENT
:
32402 kind
= GOMP_MAP_FORCE_PRESENT
;
32405 gcc_unreachable ();
32408 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32410 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32411 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32417 deviceptr ( variable-list ) */
32420 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32422 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32425 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32426 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32427 variable-list must only allow for pointer variables. */
32428 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32429 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32431 tree v
= TREE_PURPOSE (t
);
32432 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32433 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32434 OMP_CLAUSE_DECL (u
) = v
;
32435 OMP_CLAUSE_CHAIN (u
) = list
;
32450 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32451 enum omp_clause_code code
,
32452 tree list
, location_t location
)
32454 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32455 tree c
= build_omp_clause (location
, code
);
32456 OMP_CLAUSE_CHAIN (c
) = list
;
32461 num_gangs ( expression )
32462 num_workers ( expression )
32463 vector_length ( expression ) */
32466 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32467 const char *str
, tree list
)
32469 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32471 matching_parens parens
;
32472 if (!parens
.require_open (parser
))
32475 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32477 if (t
== error_mark_node
32478 || !parens
.require_close (parser
))
32480 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32481 /*or_comma=*/false,
32482 /*consume_paren=*/true);
32486 check_no_duplicate_clause (list
, code
, str
, loc
);
32488 tree c
= build_omp_clause (loc
, code
);
32489 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32490 OMP_CLAUSE_CHAIN (c
) = list
;
32496 gang [( gang-arg-list )]
32497 worker [( [num:] int-expr )]
32498 vector [( [length:] int-expr )]
32500 where gang-arg is one of:
32505 and size-expr may be:
32512 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32513 const char *str
, tree list
)
32515 const char *id
= "num";
32516 cp_lexer
*lexer
= parser
->lexer
;
32517 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32518 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32520 if (kind
== OMP_CLAUSE_VECTOR
)
32523 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32525 matching_parens parens
;
32526 parens
.consume_open (parser
);
32530 cp_token
*next
= cp_lexer_peek_token (lexer
);
32533 /* Gang static argument. */
32534 if (kind
== OMP_CLAUSE_GANG
32535 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32537 cp_lexer_consume_token (lexer
);
32539 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32540 goto cleanup_error
;
32543 if (ops
[idx
] != NULL
)
32545 cp_parser_error (parser
, "too many %<static%> arguments");
32546 goto cleanup_error
;
32549 /* Check for the '*' argument. */
32550 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32551 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32552 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32555 cp_lexer_consume_token (lexer
);
32556 ops
[idx
] = integer_minus_one_node
;
32558 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32560 cp_lexer_consume_token (lexer
);
32566 /* Worker num: argument and vector length: arguments. */
32567 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32568 && id_equal (next
->u
.value
, id
)
32569 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32571 cp_lexer_consume_token (lexer
); /* id */
32572 cp_lexer_consume_token (lexer
); /* ':' */
32575 /* Now collect the actual argument. */
32576 if (ops
[idx
] != NULL_TREE
)
32578 cp_parser_error (parser
, "unexpected argument");
32579 goto cleanup_error
;
32582 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32584 if (expr
== error_mark_node
)
32585 goto cleanup_error
;
32587 mark_exp_read (expr
);
32590 if (kind
== OMP_CLAUSE_GANG
32591 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32593 cp_lexer_consume_token (lexer
);
32600 if (!parens
.require_close (parser
))
32601 goto cleanup_error
;
32604 check_no_duplicate_clause (list
, kind
, str
, loc
);
32606 c
= build_omp_clause (loc
, kind
);
32609 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32611 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32612 OMP_CLAUSE_CHAIN (c
) = list
;
32617 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32622 tile ( size-expr-list ) */
32625 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32627 tree c
, expr
= error_mark_node
;
32628 tree tile
= NULL_TREE
;
32630 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32631 so, but the spec authors never considered such a case and have
32632 differing opinions on what it might mean, including 'not
32634 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32635 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32638 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32643 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32647 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32648 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32650 cp_lexer_consume_token (parser
->lexer
);
32651 expr
= integer_zero_node
;
32654 expr
= cp_parser_constant_expression (parser
);
32656 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32658 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32660 /* Consume the trailing ')'. */
32661 cp_lexer_consume_token (parser
->lexer
);
32663 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32664 tile
= nreverse (tile
);
32665 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32666 OMP_CLAUSE_CHAIN (c
) = list
;
32671 Parse wait clause or directive parameters. */
32674 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32676 vec
<tree
, va_gc
> *args
;
32679 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32681 /*allow_expansion_p=*/true,
32682 /*non_constant_p=*/NULL
);
32684 if (args
== NULL
|| args
->length () == 0)
32688 cp_parser_error (parser
, "expected integer expression list");
32689 release_tree_vector (args
);
32694 args_tree
= build_tree_list_vec (args
);
32696 release_tree_vector (args
);
32698 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32700 tree targ
= TREE_VALUE (t
);
32702 if (targ
!= error_mark_node
)
32704 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32705 error ("%<wait%> expression must be integral");
32708 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32710 targ
= mark_rvalue_use (targ
);
32711 OMP_CLAUSE_DECL (c
) = targ
;
32712 OMP_CLAUSE_CHAIN (c
) = list
;
32722 wait ( int-expr-list ) */
32725 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32727 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32729 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32732 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32738 collapse ( constant-expression ) */
32741 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32747 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32748 matching_parens parens
;
32749 if (!parens
.require_open (parser
))
32752 num
= cp_parser_constant_expression (parser
);
32754 if (!parens
.require_close (parser
))
32755 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32756 /*or_comma=*/false,
32757 /*consume_paren=*/true);
32759 if (num
== error_mark_node
)
32761 num
= fold_non_dependent_expr (num
);
32762 if (!tree_fits_shwi_p (num
)
32763 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32764 || (n
= tree_to_shwi (num
)) <= 0
32767 error_at (loc
, "collapse argument needs positive constant integer expression");
32771 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32772 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32773 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32774 OMP_CLAUSE_CHAIN (c
) = list
;
32775 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32781 default ( none | shared )
32784 default ( none | present ) */
32787 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32788 location_t location
, bool is_oacc
)
32790 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32793 matching_parens parens
;
32794 if (!parens
.require_open (parser
))
32796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32798 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32799 const char *p
= IDENTIFIER_POINTER (id
);
32804 if (strcmp ("none", p
) != 0)
32806 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32810 if (strcmp ("present", p
) != 0 || !is_oacc
)
32812 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32816 if (strcmp ("shared", p
) != 0 || is_oacc
)
32818 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32825 cp_lexer_consume_token (parser
->lexer
);
32831 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32833 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32836 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32837 || !parens
.require_close (parser
))
32838 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32839 /*or_comma=*/false,
32840 /*consume_paren=*/true);
32842 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32845 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32846 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32847 OMP_CLAUSE_CHAIN (c
) = list
;
32848 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32854 final ( expression ) */
32857 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32861 matching_parens parens
;
32862 if (!parens
.require_open (parser
))
32865 t
= cp_parser_assignment_expression (parser
);
32867 if (t
== error_mark_node
32868 || !parens
.require_close (parser
))
32869 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32870 /*or_comma=*/false,
32871 /*consume_paren=*/true);
32873 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32875 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32876 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32877 OMP_CLAUSE_CHAIN (c
) = list
;
32886 if ( directive-name-modifier : expression )
32888 directive-name-modifier:
32889 parallel | task | taskloop | target data | target | target update
32890 | target enter data | target exit data
32893 directive-name-modifier:
32894 ... | simd | cancel */
32897 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32901 enum tree_code if_modifier
= ERROR_MARK
;
32903 matching_parens parens
;
32904 if (!parens
.require_open (parser
))
32907 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32909 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32910 const char *p
= IDENTIFIER_POINTER (id
);
32913 if (strcmp ("cancel", p
) == 0)
32914 if_modifier
= VOID_CST
;
32915 else if (strcmp ("parallel", p
) == 0)
32916 if_modifier
= OMP_PARALLEL
;
32917 else if (strcmp ("simd", p
) == 0)
32918 if_modifier
= OMP_SIMD
;
32919 else if (strcmp ("task", p
) == 0)
32920 if_modifier
= OMP_TASK
;
32921 else if (strcmp ("taskloop", p
) == 0)
32922 if_modifier
= OMP_TASKLOOP
;
32923 else if (strcmp ("target", p
) == 0)
32925 if_modifier
= OMP_TARGET
;
32926 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32928 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32929 p
= IDENTIFIER_POINTER (id
);
32930 if (strcmp ("data", p
) == 0)
32931 if_modifier
= OMP_TARGET_DATA
;
32932 else if (strcmp ("update", p
) == 0)
32933 if_modifier
= OMP_TARGET_UPDATE
;
32934 else if (strcmp ("enter", p
) == 0)
32935 if_modifier
= OMP_TARGET_ENTER_DATA
;
32936 else if (strcmp ("exit", p
) == 0)
32937 if_modifier
= OMP_TARGET_EXIT_DATA
;
32938 if (if_modifier
!= OMP_TARGET
)
32943 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32944 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32946 if_modifier
= ERROR_MARK
;
32948 if (if_modifier
== OMP_TARGET_ENTER_DATA
32949 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32951 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32953 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32954 p
= IDENTIFIER_POINTER (id
);
32955 if (strcmp ("data", p
) == 0)
32961 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32962 error_at (loc
, "expected %<data%>");
32963 if_modifier
= ERROR_MARK
;
32968 if (if_modifier
!= ERROR_MARK
)
32970 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32973 cp_lexer_consume_token (parser
->lexer
);
32980 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32981 error_at (loc
, "expected %<:%>");
32983 if_modifier
= ERROR_MARK
;
32988 t
= cp_parser_assignment_expression (parser
);
32990 if (t
== error_mark_node
32991 || !parens
.require_close (parser
))
32992 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32993 /*or_comma=*/false,
32994 /*consume_paren=*/true);
32996 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32997 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32999 if (if_modifier
!= ERROR_MARK
33000 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33002 const char *p
= NULL
;
33003 switch (if_modifier
)
33005 case VOID_CST
: p
= "cancel"; break;
33006 case OMP_PARALLEL
: p
= "parallel"; break;
33007 case OMP_SIMD
: p
= "simd"; break;
33008 case OMP_TASK
: p
= "task"; break;
33009 case OMP_TASKLOOP
: p
= "taskloop"; break;
33010 case OMP_TARGET_DATA
: p
= "target data"; break;
33011 case OMP_TARGET
: p
= "target"; break;
33012 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33013 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33014 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33015 default: gcc_unreachable ();
33017 error_at (location
, "too many %<if%> clauses with %qs modifier",
33021 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33024 error_at (location
, "too many %<if%> clauses");
33026 error_at (location
, "too many %<if%> clauses without modifier");
33029 else if (if_modifier
== ERROR_MARK
33030 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33032 error_at (location
, "if any %<if%> clause has modifier, then all "
33033 "%<if%> clauses have to use modifier");
33038 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33039 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33040 OMP_CLAUSE_IF_EXPR (c
) = t
;
33041 OMP_CLAUSE_CHAIN (c
) = list
;
33050 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33051 tree list
, location_t location
)
33055 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33058 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33059 OMP_CLAUSE_CHAIN (c
) = list
;
33067 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33068 tree list
, location_t location
)
33072 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33074 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33075 OMP_CLAUSE_CHAIN (c
) = list
;
33080 num_threads ( expression ) */
33083 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33084 location_t location
)
33088 matching_parens parens
;
33089 if (!parens
.require_open (parser
))
33092 t
= cp_parser_assignment_expression (parser
);
33094 if (t
== error_mark_node
33095 || !parens
.require_close (parser
))
33096 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33097 /*or_comma=*/false,
33098 /*consume_paren=*/true);
33100 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33101 "num_threads", location
);
33103 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33104 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33105 OMP_CLAUSE_CHAIN (c
) = list
;
33111 num_tasks ( expression ) */
33114 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33115 location_t location
)
33119 matching_parens parens
;
33120 if (!parens
.require_open (parser
))
33123 t
= cp_parser_assignment_expression (parser
);
33125 if (t
== error_mark_node
33126 || !parens
.require_close (parser
))
33127 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33128 /*or_comma=*/false,
33129 /*consume_paren=*/true);
33131 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33132 "num_tasks", location
);
33134 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33135 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33136 OMP_CLAUSE_CHAIN (c
) = list
;
33142 grainsize ( expression ) */
33145 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33146 location_t location
)
33150 matching_parens parens
;
33151 if (!parens
.require_open (parser
))
33154 t
= cp_parser_assignment_expression (parser
);
33156 if (t
== error_mark_node
33157 || !parens
.require_close (parser
))
33158 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33159 /*or_comma=*/false,
33160 /*consume_paren=*/true);
33162 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33163 "grainsize", location
);
33165 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33166 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33167 OMP_CLAUSE_CHAIN (c
) = list
;
33173 priority ( expression ) */
33176 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33177 location_t location
)
33181 matching_parens parens
;
33182 if (!parens
.require_open (parser
))
33185 t
= cp_parser_assignment_expression (parser
);
33187 if (t
== error_mark_node
33188 || !parens
.require_close (parser
))
33189 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33190 /*or_comma=*/false,
33191 /*consume_paren=*/true);
33193 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33194 "priority", location
);
33196 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33197 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33198 OMP_CLAUSE_CHAIN (c
) = list
;
33204 hint ( expression ) */
33207 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33211 matching_parens parens
;
33212 if (!parens
.require_open (parser
))
33215 t
= cp_parser_assignment_expression (parser
);
33217 if (t
== error_mark_node
33218 || !parens
.require_close (parser
))
33219 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33220 /*or_comma=*/false,
33221 /*consume_paren=*/true);
33223 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33225 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33226 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33227 OMP_CLAUSE_CHAIN (c
) = list
;
33233 defaultmap ( tofrom : scalar )
33236 defaultmap ( implicit-behavior [ : variable-category ] ) */
33239 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33240 location_t location
)
33244 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33245 enum omp_clause_defaultmap_kind category
33246 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33248 matching_parens parens
;
33249 if (!parens
.require_open (parser
))
33252 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33254 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33257 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33258 "%<tofrom%>, %<firstprivate%>, %<none%> "
33264 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33265 p
= IDENTIFIER_POINTER (id
);
33271 if (strcmp ("alloc", p
) == 0)
33272 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33274 goto invalid_behavior
;
33278 if (strcmp ("default", p
) == 0)
33279 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33281 goto invalid_behavior
;
33285 if (strcmp ("firstprivate", p
) == 0)
33286 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33287 else if (strcmp ("from", p
) == 0)
33288 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33290 goto invalid_behavior
;
33294 if (strcmp ("none", p
) == 0)
33295 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33297 goto invalid_behavior
;
33301 if (strcmp ("tofrom", p
) == 0)
33302 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33303 else if (strcmp ("to", p
) == 0)
33304 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33306 goto invalid_behavior
;
33310 goto invalid_behavior
;
33312 cp_lexer_consume_token (parser
->lexer
);
33314 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33316 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33319 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33322 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33326 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33327 p
= IDENTIFIER_POINTER (id
);
33332 if (strcmp ("aggregate", p
) == 0)
33333 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33335 goto invalid_category
;
33339 if (strcmp ("pointer", p
) == 0)
33340 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33342 goto invalid_category
;
33346 if (strcmp ("scalar", p
) == 0)
33347 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33349 goto invalid_category
;
33353 goto invalid_category
;
33356 cp_lexer_consume_token (parser
->lexer
);
33358 if (!parens
.require_close (parser
))
33361 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33362 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33363 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33364 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33365 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33366 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33368 enum omp_clause_defaultmap_kind cat
= category
;
33369 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33370 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33371 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33375 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33378 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33381 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33384 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33388 gcc_unreachable ();
33391 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33394 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33399 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33400 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33401 OMP_CLAUSE_CHAIN (c
) = list
;
33405 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33406 /*or_comma=*/false,
33407 /*consume_paren=*/true);
33415 ordered ( constant-expression ) */
33418 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33419 tree list
, location_t location
)
33421 tree c
, num
= NULL_TREE
;
33424 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33425 "ordered", location
);
33427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33429 matching_parens parens
;
33430 parens
.consume_open (parser
);
33432 num
= cp_parser_constant_expression (parser
);
33434 if (!parens
.require_close (parser
))
33435 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33436 /*or_comma=*/false,
33437 /*consume_paren=*/true);
33439 if (num
== error_mark_node
)
33441 num
= fold_non_dependent_expr (num
);
33442 if (!tree_fits_shwi_p (num
)
33443 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33444 || (n
= tree_to_shwi (num
)) <= 0
33447 error_at (location
,
33448 "ordered argument needs positive constant integer "
33454 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33455 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33456 OMP_CLAUSE_CHAIN (c
) = list
;
33461 reduction ( reduction-operator : variable-list )
33463 reduction-operator:
33464 One of: + * - & ^ | && ||
33468 reduction-operator:
33469 One of: + * - & ^ | && || min max
33473 reduction-operator:
33474 One of: + * - & ^ | && ||
33478 reduction ( reduction-modifier, reduction-operator : variable-list )
33479 in_reduction ( reduction-operator : variable-list )
33480 task_reduction ( reduction-operator : variable-list ) */
33483 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33484 bool is_omp
, tree list
)
33486 enum tree_code code
= ERROR_MARK
;
33487 tree nlist
, c
, id
= NULL_TREE
;
33489 bool inscan
= false;
33491 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33494 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33496 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33497 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33499 cp_lexer_consume_token (parser
->lexer
);
33500 cp_lexer_consume_token (parser
->lexer
);
33502 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33503 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33505 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33506 const char *p
= IDENTIFIER_POINTER (id
);
33507 if (strcmp (p
, "task") == 0)
33509 else if (strcmp (p
, "inscan") == 0)
33512 sorry ("%<inscan%> modifier on %<reduction%> clause "
33513 "not supported yet");
33515 if (task
|| inscan
)
33517 cp_lexer_consume_token (parser
->lexer
);
33518 cp_lexer_consume_token (parser
->lexer
);
33523 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33525 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33526 case CPP_MULT
: code
= MULT_EXPR
; break;
33527 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33528 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33529 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33530 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33531 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33532 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33536 if (code
!= ERROR_MARK
)
33537 cp_lexer_consume_token (parser
->lexer
);
33540 bool saved_colon_corrects_to_scope_p
;
33541 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33542 parser
->colon_corrects_to_scope_p
= false;
33543 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33544 /*check_dependency_p=*/true,
33545 /*template_p=*/NULL
,
33546 /*declarator_p=*/false,
33547 /*optional_p=*/false);
33548 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33549 if (identifier_p (id
))
33551 const char *p
= IDENTIFIER_POINTER (id
);
33553 if (strcmp (p
, "min") == 0)
33555 else if (strcmp (p
, "max") == 0)
33557 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33559 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33561 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33563 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33564 code
= BIT_AND_EXPR
;
33565 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33566 code
= BIT_IOR_EXPR
;
33567 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33568 code
= BIT_XOR_EXPR
;
33569 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33570 code
= TRUTH_ANDIF_EXPR
;
33571 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33572 code
= TRUTH_ORIF_EXPR
;
33573 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33574 tree scope
= parser
->scope
;
33576 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33577 parser
->scope
= NULL_TREE
;
33578 parser
->qualifying_scope
= NULL_TREE
;
33579 parser
->object_scope
= NULL_TREE
;
33583 error ("invalid reduction-identifier");
33585 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33586 /*or_comma=*/false,
33587 /*consume_paren=*/true);
33592 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33595 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33597 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33599 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33601 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33603 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33604 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33611 schedule ( schedule-kind )
33612 schedule ( schedule-kind , expression )
33615 static | dynamic | guided | runtime | auto
33618 schedule ( schedule-modifier : schedule-kind )
33619 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33627 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33630 int modifiers
= 0, nmodifiers
= 0;
33632 matching_parens parens
;
33633 if (!parens
.require_open (parser
))
33636 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33638 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33640 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33641 const char *p
= IDENTIFIER_POINTER (id
);
33642 if (strcmp ("simd", p
) == 0)
33643 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33644 else if (strcmp ("monotonic", p
) == 0)
33645 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33646 else if (strcmp ("nonmonotonic", p
) == 0)
33647 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33650 cp_lexer_consume_token (parser
->lexer
);
33651 if (nmodifiers
++ == 0
33652 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33653 cp_lexer_consume_token (parser
->lexer
);
33656 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33663 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33664 const char *p
= IDENTIFIER_POINTER (id
);
33669 if (strcmp ("dynamic", p
) != 0)
33671 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33675 if (strcmp ("guided", p
) != 0)
33677 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33681 if (strcmp ("runtime", p
) != 0)
33683 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33690 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33691 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33692 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33693 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33696 cp_lexer_consume_token (parser
->lexer
);
33698 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33699 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33700 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33701 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33703 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33711 cp_lexer_consume_token (parser
->lexer
);
33713 token
= cp_lexer_peek_token (parser
->lexer
);
33714 t
= cp_parser_assignment_expression (parser
);
33716 if (t
== error_mark_node
)
33718 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33719 error_at (token
->location
, "schedule %<runtime%> does not take "
33720 "a %<chunk_size%> parameter");
33721 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33722 error_at (token
->location
, "schedule %<auto%> does not take "
33723 "a %<chunk_size%> parameter");
33725 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33727 if (!parens
.require_close (parser
))
33730 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33733 OMP_CLAUSE_SCHEDULE_KIND (c
)
33734 = (enum omp_clause_schedule_kind
)
33735 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33737 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33738 OMP_CLAUSE_CHAIN (c
) = list
;
33742 cp_parser_error (parser
, "invalid schedule kind");
33744 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33745 /*or_comma=*/false,
33746 /*consume_paren=*/true);
33754 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33755 tree list
, location_t location
)
33759 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33761 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33762 OMP_CLAUSE_CHAIN (c
) = list
;
33771 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33772 tree list
, location_t location
)
33774 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33775 tree c
= build_omp_clause (location
, code
);
33776 OMP_CLAUSE_CHAIN (c
) = list
;
33787 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33788 enum omp_clause_code code
,
33789 tree list
, location_t location
)
33791 tree c
= build_omp_clause (location
, code
);
33792 OMP_CLAUSE_CHAIN (c
) = list
;
33800 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33801 tree list
, location_t location
)
33803 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33804 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33805 OMP_CLAUSE_CHAIN (c
) = list
;
33814 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33815 enum omp_clause_code code
,
33816 tree list
, location_t location
)
33818 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33819 tree c
= build_omp_clause (location
, code
);
33820 OMP_CLAUSE_CHAIN (c
) = list
;
33825 num_teams ( expression ) */
33828 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33829 location_t location
)
33833 matching_parens parens
;
33834 if (!parens
.require_open (parser
))
33837 t
= cp_parser_assignment_expression (parser
);
33839 if (t
== error_mark_node
33840 || !parens
.require_close (parser
))
33841 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33842 /*or_comma=*/false,
33843 /*consume_paren=*/true);
33845 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33846 "num_teams", location
);
33848 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33849 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33850 OMP_CLAUSE_CHAIN (c
) = list
;
33856 thread_limit ( expression ) */
33859 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33860 location_t location
)
33864 matching_parens parens
;
33865 if (!parens
.require_open (parser
))
33868 t
= cp_parser_assignment_expression (parser
);
33870 if (t
== error_mark_node
33871 || !parens
.require_close (parser
))
33872 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33873 /*or_comma=*/false,
33874 /*consume_paren=*/true);
33876 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33877 "thread_limit", location
);
33879 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33880 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33881 OMP_CLAUSE_CHAIN (c
) = list
;
33887 aligned ( variable-list )
33888 aligned ( variable-list : constant-expression ) */
33891 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33893 tree nlist
, c
, alignment
= NULL_TREE
;
33896 matching_parens parens
;
33897 if (!parens
.require_open (parser
))
33900 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33905 alignment
= cp_parser_constant_expression (parser
);
33907 if (!parens
.require_close (parser
))
33908 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33909 /*or_comma=*/false,
33910 /*consume_paren=*/true);
33912 if (alignment
== error_mark_node
)
33913 alignment
= NULL_TREE
;
33916 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33917 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33923 lastprivate ( variable-list )
33926 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33929 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
33931 bool conditional
= false;
33933 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33937 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
33939 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33940 const char *p
= IDENTIFIER_POINTER (id
);
33942 if (strcmp ("conditional", p
) == 0)
33944 conditional
= true;
33945 cp_lexer_consume_token (parser
->lexer
);
33946 cp_lexer_consume_token (parser
->lexer
);
33950 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
33954 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33955 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
33960 linear ( variable-list )
33961 linear ( variable-list : expression )
33964 linear ( modifier ( variable-list ) )
33965 linear ( modifier ( variable-list ) : expression ) */
33968 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33971 tree nlist
, c
, step
= integer_one_node
;
33973 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33975 matching_parens parens
;
33976 if (!parens
.require_open (parser
))
33979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33981 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33982 const char *p
= IDENTIFIER_POINTER (id
);
33984 if (strcmp ("ref", p
) == 0)
33985 kind
= OMP_CLAUSE_LINEAR_REF
;
33986 else if (strcmp ("val", p
) == 0)
33987 kind
= OMP_CLAUSE_LINEAR_VAL
;
33988 else if (strcmp ("uval", p
) == 0)
33989 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33990 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33991 cp_lexer_consume_token (parser
->lexer
);
33993 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33996 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33997 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34001 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34002 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34004 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34005 else if (!parens
.require_close (parser
))
34006 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34007 /*or_comma=*/false,
34008 /*consume_paren=*/true);
34015 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34016 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34018 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34019 cp_parser_parse_tentatively (parser
);
34020 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34021 /*check_dependency_p=*/true,
34022 /*template_p=*/NULL
,
34023 /*declarator_p=*/false,
34024 /*optional_p=*/false);
34025 if (step
!= error_mark_node
)
34026 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34027 if (step
== error_mark_node
)
34030 cp_parser_abort_tentative_parse (parser
);
34032 else if (!cp_parser_parse_definitely (parser
))
34036 step
= cp_parser_assignment_expression (parser
);
34038 if (!parens
.require_close (parser
))
34039 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34040 /*or_comma=*/false,
34041 /*consume_paren=*/true);
34043 if (step
== error_mark_node
)
34047 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34049 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34050 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34057 safelen ( constant-expression ) */
34060 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34061 location_t location
)
34065 matching_parens parens
;
34066 if (!parens
.require_open (parser
))
34069 t
= cp_parser_constant_expression (parser
);
34071 if (t
== error_mark_node
34072 || !parens
.require_close (parser
))
34073 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34074 /*or_comma=*/false,
34075 /*consume_paren=*/true);
34077 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34079 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34080 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34081 OMP_CLAUSE_CHAIN (c
) = list
;
34087 simdlen ( constant-expression ) */
34090 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34091 location_t location
)
34095 matching_parens parens
;
34096 if (!parens
.require_open (parser
))
34099 t
= cp_parser_constant_expression (parser
);
34101 if (t
== error_mark_node
34102 || !parens
.require_close (parser
))
34103 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34104 /*or_comma=*/false,
34105 /*consume_paren=*/true);
34107 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34109 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34110 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34111 OMP_CLAUSE_CHAIN (c
) = list
;
34118 identifier [+/- integer]
34119 vec , identifier [+/- integer]
34123 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34128 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34130 cp_parser_error (parser
, "expected identifier");
34134 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34136 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34137 tree t
, identifier
= cp_parser_identifier (parser
);
34138 tree addend
= NULL
;
34140 if (identifier
== error_mark_node
)
34141 t
= error_mark_node
;
34144 t
= cp_parser_lookup_name_simple
34145 (parser
, identifier
,
34146 cp_lexer_peek_token (parser
->lexer
)->location
);
34147 if (t
== error_mark_node
)
34148 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34155 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34157 addend
= integer_zero_node
;
34158 goto add_to_vector
;
34160 cp_lexer_consume_token (parser
->lexer
);
34162 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34164 cp_parser_error (parser
, "expected integer");
34168 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34169 if (TREE_CODE (addend
) != INTEGER_CST
)
34171 cp_parser_error (parser
, "expected integer");
34174 cp_lexer_consume_token (parser
->lexer
);
34177 if (t
!= error_mark_node
)
34179 vec
= tree_cons (addend
, t
, vec
);
34181 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34184 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34187 cp_lexer_consume_token (parser
->lexer
);
34190 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34192 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34193 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34194 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34195 OMP_CLAUSE_CHAIN (u
) = list
;
34202 iterators ( iterators-definition )
34204 iterators-definition:
34206 iterator-specifier , iterators-definition
34208 iterator-specifier:
34209 identifier = range-specification
34210 iterator-type identifier = range-specification
34212 range-specification:
34214 begin : end : step */
34217 cp_parser_omp_iterators (cp_parser
*parser
)
34219 tree ret
= NULL_TREE
, *last
= &ret
;
34220 cp_lexer_consume_token (parser
->lexer
);
34222 matching_parens parens
;
34223 if (!parens
.require_open (parser
))
34224 return error_mark_node
;
34226 bool saved_colon_corrects_to_scope_p
34227 = parser
->colon_corrects_to_scope_p
;
34228 bool saved_colon_doesnt_start_class_def_p
34229 = parser
->colon_doesnt_start_class_def_p
;
34234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34235 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34236 iter_type
= integer_type_node
;
34239 const char *saved_message
34240 = parser
->type_definition_forbidden_message
;
34241 parser
->type_definition_forbidden_message
34242 = G_("types may not be defined in iterator type");
34244 iter_type
= cp_parser_type_id (parser
);
34246 parser
->type_definition_forbidden_message
= saved_message
;
34249 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34250 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34252 cp_parser_error (parser
, "expected identifier");
34256 tree id
= cp_parser_identifier (parser
);
34257 if (id
== error_mark_node
)
34260 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34263 parser
->colon_corrects_to_scope_p
= false;
34264 parser
->colon_doesnt_start_class_def_p
= true;
34265 tree begin
= cp_parser_assignment_expression (parser
);
34267 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34270 tree end
= cp_parser_assignment_expression (parser
);
34272 tree step
= integer_one_node
;
34273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34275 cp_lexer_consume_token (parser
->lexer
);
34276 step
= cp_parser_assignment_expression (parser
);
34279 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34280 DECL_ARTIFICIAL (iter_var
) = 1;
34281 DECL_CONTEXT (iter_var
) = current_function_decl
;
34282 pushdecl (iter_var
);
34284 *last
= make_tree_vec (6);
34285 TREE_VEC_ELT (*last
, 0) = iter_var
;
34286 TREE_VEC_ELT (*last
, 1) = begin
;
34287 TREE_VEC_ELT (*last
, 2) = end
;
34288 TREE_VEC_ELT (*last
, 3) = step
;
34289 last
= &TREE_CHAIN (*last
);
34291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34293 cp_lexer_consume_token (parser
->lexer
);
34300 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34301 parser
->colon_doesnt_start_class_def_p
34302 = saved_colon_doesnt_start_class_def_p
;
34304 if (!parens
.require_close (parser
))
34305 cp_parser_skip_to_closing_parenthesis (parser
,
34306 /*recovering=*/true,
34307 /*or_comma=*/false,
34308 /*consume_paren=*/true);
34310 return ret
? ret
: error_mark_node
;
34314 depend ( depend-kind : variable-list )
34322 depend ( sink : vec )
34325 depend ( depend-modifier , depend-kind: variable-list )
34328 in | out | inout | mutexinoutset | depobj
34331 iterator ( iterators-definition ) */
34334 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34336 tree nlist
, c
, iterators
= NULL_TREE
;
34337 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34339 matching_parens parens
;
34340 if (!parens
.require_open (parser
))
34345 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34348 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34349 const char *p
= IDENTIFIER_POINTER (id
);
34351 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34353 begin_scope (sk_omp
, NULL
);
34354 iterators
= cp_parser_omp_iterators (parser
);
34355 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34358 if (strcmp ("in", p
) == 0)
34359 kind
= OMP_CLAUSE_DEPEND_IN
;
34360 else if (strcmp ("inout", p
) == 0)
34361 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34362 else if (strcmp ("mutexinoutset", p
) == 0)
34363 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34364 else if (strcmp ("out", p
) == 0)
34365 kind
= OMP_CLAUSE_DEPEND_OUT
;
34366 else if (strcmp ("depobj", p
) == 0)
34367 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34368 else if (strcmp ("sink", p
) == 0)
34369 kind
= OMP_CLAUSE_DEPEND_SINK
;
34370 else if (strcmp ("source", p
) == 0)
34371 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34378 cp_lexer_consume_token (parser
->lexer
);
34381 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34383 poplevel (0, 1, 0);
34384 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34385 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34386 iterators
= NULL_TREE
;
34389 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34391 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34392 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34393 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34394 OMP_CLAUSE_CHAIN (c
) = list
;
34395 if (!parens
.require_close (parser
))
34396 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34397 /*or_comma=*/false,
34398 /*consume_paren=*/true);
34402 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34405 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34406 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34409 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34414 tree block
= poplevel (1, 1, 0);
34415 if (iterators
== error_mark_node
)
34416 iterators
= NULL_TREE
;
34418 TREE_VEC_ELT (iterators
, 5) = block
;
34421 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34423 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34425 OMP_CLAUSE_DECL (c
)
34426 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34432 cp_parser_error (parser
, "invalid depend kind");
34435 poplevel (0, 1, 0);
34436 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34437 /*or_comma=*/false,
34438 /*consume_paren=*/true);
34443 map ( map-kind : variable-list )
34444 map ( variable-list )
34447 alloc | to | from | tofrom
34451 alloc | to | from | tofrom | release | delete
34453 map ( always [,] map-kind: variable-list ) */
34456 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34459 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34460 bool always
= false;
34462 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34467 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34468 const char *p
= IDENTIFIER_POINTER (id
);
34470 if (strcmp ("always", p
) == 0)
34473 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34475 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34476 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34478 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34482 cp_lexer_consume_token (parser
->lexer
);
34484 cp_lexer_consume_token (parser
->lexer
);
34489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34490 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34492 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34493 const char *p
= IDENTIFIER_POINTER (id
);
34495 if (strcmp ("alloc", p
) == 0)
34496 kind
= GOMP_MAP_ALLOC
;
34497 else if (strcmp ("to", p
) == 0)
34498 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34499 else if (strcmp ("from", p
) == 0)
34500 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34501 else if (strcmp ("tofrom", p
) == 0)
34502 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34503 else if (strcmp ("release", p
) == 0)
34504 kind
= GOMP_MAP_RELEASE
;
34507 cp_parser_error (parser
, "invalid map kind");
34508 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34509 /*or_comma=*/false,
34510 /*consume_paren=*/true);
34513 cp_lexer_consume_token (parser
->lexer
);
34514 cp_lexer_consume_token (parser
->lexer
);
34516 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34517 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34519 kind
= GOMP_MAP_DELETE
;
34520 cp_lexer_consume_token (parser
->lexer
);
34521 cp_lexer_consume_token (parser
->lexer
);
34524 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34527 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34528 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34534 device ( expression ) */
34537 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34538 location_t location
)
34542 matching_parens parens
;
34543 if (!parens
.require_open (parser
))
34546 t
= cp_parser_assignment_expression (parser
);
34548 if (t
== error_mark_node
34549 || !parens
.require_close (parser
))
34550 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34551 /*or_comma=*/false,
34552 /*consume_paren=*/true);
34554 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34555 "device", location
);
34557 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34558 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34559 OMP_CLAUSE_CHAIN (c
) = list
;
34565 dist_schedule ( static )
34566 dist_schedule ( static , expression ) */
34569 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34570 location_t location
)
34574 matching_parens parens
;
34575 if (!parens
.require_open (parser
))
34578 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34580 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34582 cp_lexer_consume_token (parser
->lexer
);
34584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34586 cp_lexer_consume_token (parser
->lexer
);
34588 t
= cp_parser_assignment_expression (parser
);
34590 if (t
== error_mark_node
)
34592 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34594 if (!parens
.require_close (parser
))
34597 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34600 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34602 OMP_CLAUSE_CHAIN (c
) = list
;
34606 cp_parser_error (parser
, "invalid dist_schedule kind");
34608 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34609 /*or_comma=*/false,
34610 /*consume_paren=*/true);
34615 proc_bind ( proc-bind-kind )
34618 master | close | spread */
34621 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34622 location_t location
)
34625 enum omp_clause_proc_bind_kind kind
;
34627 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34632 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34633 const char *p
= IDENTIFIER_POINTER (id
);
34635 if (strcmp ("master", p
) == 0)
34636 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34637 else if (strcmp ("close", p
) == 0)
34638 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34639 else if (strcmp ("spread", p
) == 0)
34640 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34647 cp_lexer_consume_token (parser
->lexer
);
34648 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34651 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34652 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34654 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34655 OMP_CLAUSE_CHAIN (c
) = list
;
34659 cp_parser_error (parser
, "invalid depend kind");
34661 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34662 /*or_comma=*/false,
34663 /*consume_paren=*/true);
34668 async [( int-expr )] */
34671 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34674 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34676 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34678 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34680 matching_parens parens
;
34681 parens
.consume_open (parser
);
34683 t
= cp_parser_expression (parser
);
34684 if (t
== error_mark_node
34685 || !parens
.require_close (parser
))
34686 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34687 /*or_comma=*/false,
34688 /*consume_paren=*/true);
34691 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34693 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34694 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34695 OMP_CLAUSE_CHAIN (c
) = list
;
34701 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34702 is a bitmask in MASK. Return the list of clauses found. */
34705 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34706 const char *where
, cp_token
*pragma_tok
,
34707 bool finish_p
= true)
34709 tree clauses
= NULL
;
34712 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34715 pragma_omp_clause c_kind
;
34716 omp_clause_code code
;
34717 const char *c_name
;
34718 tree prev
= clauses
;
34720 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34721 cp_lexer_consume_token (parser
->lexer
);
34723 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34724 c_kind
= cp_parser_omp_clause_name (parser
);
34728 case PRAGMA_OACC_CLAUSE_ASYNC
:
34729 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34732 case PRAGMA_OACC_CLAUSE_AUTO
:
34733 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34737 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34738 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34739 c_name
= "collapse";
34741 case PRAGMA_OACC_CLAUSE_COPY
:
34742 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34745 case PRAGMA_OACC_CLAUSE_COPYIN
:
34746 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34749 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34750 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34751 c_name
= "copyout";
34753 case PRAGMA_OACC_CLAUSE_CREATE
:
34754 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34757 case PRAGMA_OACC_CLAUSE_DELETE
:
34758 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34761 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34762 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34763 c_name
= "default";
34765 case PRAGMA_OACC_CLAUSE_DEVICE
:
34766 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34769 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34770 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34771 c_name
= "deviceptr";
34773 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34774 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34775 c_name
= "device_resident";
34777 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34778 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34780 c_name
= "finalize";
34782 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34783 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34785 c_name
= "firstprivate";
34787 case PRAGMA_OACC_CLAUSE_GANG
:
34789 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34792 case PRAGMA_OACC_CLAUSE_HOST
:
34793 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34796 case PRAGMA_OACC_CLAUSE_IF
:
34797 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34800 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34801 clauses
= cp_parser_oacc_simple_clause (parser
,
34802 OMP_CLAUSE_IF_PRESENT
,
34804 c_name
= "if_present";
34806 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34807 clauses
= cp_parser_oacc_simple_clause (parser
,
34808 OMP_CLAUSE_INDEPENDENT
,
34810 c_name
= "independent";
34812 case PRAGMA_OACC_CLAUSE_LINK
:
34813 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34816 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34817 code
= OMP_CLAUSE_NUM_GANGS
;
34818 c_name
= "num_gangs";
34819 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34822 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34823 c_name
= "num_workers";
34824 code
= OMP_CLAUSE_NUM_WORKERS
;
34825 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34828 case PRAGMA_OACC_CLAUSE_PRESENT
:
34829 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34830 c_name
= "present";
34832 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34833 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34835 c_name
= "private";
34837 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34839 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34841 c_name
= "reduction";
34843 case PRAGMA_OACC_CLAUSE_SEQ
:
34844 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34848 case PRAGMA_OACC_CLAUSE_TILE
:
34849 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34852 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34853 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34855 c_name
= "use_device";
34857 case PRAGMA_OACC_CLAUSE_VECTOR
:
34859 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34862 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34863 c_name
= "vector_length";
34864 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34865 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34868 case PRAGMA_OACC_CLAUSE_WAIT
:
34869 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34872 case PRAGMA_OACC_CLAUSE_WORKER
:
34874 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34878 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34884 if (((mask
>> c_kind
) & 1) == 0)
34886 /* Remove the invalid clause(s) from the list to avoid
34887 confusing the rest of the compiler. */
34889 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34894 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34897 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34902 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34903 is a bitmask in MASK. Return the list of clauses found; the result
34904 of clause default goes in *pdefault. */
34907 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34908 const char *where
, cp_token
*pragma_tok
,
34909 bool finish_p
= true)
34911 tree clauses
= NULL
;
34913 cp_token
*token
= NULL
;
34915 /* Don't create location wrapper nodes within OpenMP clauses. */
34916 auto_suppress_location_wrappers sentinel
;
34918 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34920 pragma_omp_clause c_kind
;
34921 const char *c_name
;
34922 tree prev
= clauses
;
34924 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34925 cp_lexer_consume_token (parser
->lexer
);
34927 token
= cp_lexer_peek_token (parser
->lexer
);
34928 c_kind
= cp_parser_omp_clause_name (parser
);
34932 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
34933 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
34935 c_name
= "collapse";
34937 case PRAGMA_OMP_CLAUSE_COPYIN
:
34938 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
34941 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
34942 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
34944 c_name
= "copyprivate";
34946 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34947 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
34948 token
->location
, false);
34949 c_name
= "default";
34951 case PRAGMA_OMP_CLAUSE_FINAL
:
34952 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
34955 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
34956 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34958 c_name
= "firstprivate";
34960 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34961 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34963 c_name
= "grainsize";
34965 case PRAGMA_OMP_CLAUSE_HINT
:
34966 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34970 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34971 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34973 c_name
= "defaultmap";
34975 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34976 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34978 c_name
= "use_device_ptr";
34980 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34981 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34983 c_name
= "is_device_ptr";
34985 case PRAGMA_OMP_CLAUSE_IF
:
34986 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34990 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
34992 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
34994 c_name
= "in_reduction";
34996 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34997 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
34998 c_name
= "lastprivate";
35000 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35001 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35003 c_name
= "mergeable";
35005 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35006 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35009 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35010 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35012 c_name
= "num_tasks";
35014 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35015 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35017 c_name
= "num_threads";
35019 case PRAGMA_OMP_CLAUSE_ORDERED
:
35020 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35022 c_name
= "ordered";
35024 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35025 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35027 c_name
= "priority";
35029 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35030 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35032 c_name
= "private";
35034 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35036 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35038 c_name
= "reduction";
35040 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35041 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35043 c_name
= "schedule";
35045 case PRAGMA_OMP_CLAUSE_SHARED
:
35046 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35050 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35052 = cp_parser_omp_clause_reduction (parser
,
35053 OMP_CLAUSE_TASK_REDUCTION
,
35055 c_name
= "task_reduction";
35057 case PRAGMA_OMP_CLAUSE_UNTIED
:
35058 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35062 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35063 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35064 clauses
, token
->location
);
35065 c_name
= "inbranch";
35067 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35068 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35070 c_name
= "nontemporal";
35072 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35073 clauses
= cp_parser_omp_clause_branch (parser
,
35074 OMP_CLAUSE_NOTINBRANCH
,
35075 clauses
, token
->location
);
35076 c_name
= "notinbranch";
35078 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35079 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35080 clauses
, token
->location
);
35081 c_name
= "parallel";
35085 error_at (token
->location
, "%qs must be the first clause of %qs",
35090 case PRAGMA_OMP_CLAUSE_FOR
:
35091 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35092 clauses
, token
->location
);
35095 goto clause_not_first
;
35097 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35098 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35099 clauses
, token
->location
);
35100 c_name
= "sections";
35102 goto clause_not_first
;
35104 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35105 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35106 clauses
, token
->location
);
35107 c_name
= "taskgroup";
35109 goto clause_not_first
;
35111 case PRAGMA_OMP_CLAUSE_LINK
:
35112 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35115 case PRAGMA_OMP_CLAUSE_TO
:
35116 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35117 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35120 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35123 case PRAGMA_OMP_CLAUSE_FROM
:
35124 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35127 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35128 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35130 c_name
= "uniform";
35132 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35133 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35135 c_name
= "num_teams";
35137 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35138 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35140 c_name
= "thread_limit";
35142 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35143 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35144 c_name
= "aligned";
35146 case PRAGMA_OMP_CLAUSE_LINEAR
:
35148 bool declare_simd
= false;
35149 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35150 declare_simd
= true;
35151 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35155 case PRAGMA_OMP_CLAUSE_DEPEND
:
35156 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35160 case PRAGMA_OMP_CLAUSE_MAP
:
35161 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35164 case PRAGMA_OMP_CLAUSE_DEVICE
:
35165 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35169 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35170 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35172 c_name
= "dist_schedule";
35174 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35175 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35177 c_name
= "proc_bind";
35179 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35180 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35182 c_name
= "safelen";
35184 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35185 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35187 c_name
= "simdlen";
35189 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35190 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35192 c_name
= "nogroup";
35194 case PRAGMA_OMP_CLAUSE_THREADS
:
35196 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35197 clauses
, token
->location
);
35198 c_name
= "threads";
35200 case PRAGMA_OMP_CLAUSE_SIMD
:
35202 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35203 clauses
, token
->location
);
35207 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35213 if (((mask
>> c_kind
) & 1) == 0)
35215 /* Remove the invalid clause(s) from the list to avoid
35216 confusing the rest of the compiler. */
35218 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35222 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35225 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35226 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35228 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35237 In practice, we're also interested in adding the statement to an
35238 outer node. So it is convenient if we work around the fact that
35239 cp_parser_statement calls add_stmt. */
35242 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35244 unsigned save
= parser
->in_statement
;
35246 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35247 This preserves the "not within loop or switch" style error messages
35248 for nonsense cases like
35254 if (parser
->in_statement
)
35255 parser
->in_statement
= IN_OMP_BLOCK
;
35261 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35263 parser
->in_statement
= save
;
35267 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35269 tree stmt
= begin_omp_structured_block ();
35270 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35272 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35274 cp_parser_end_omp_structured_block (parser
, save
);
35275 return finish_omp_structured_block (stmt
);
35279 # pragma omp atomic new-line
35283 x binop= expr | x++ | ++x | x-- | --x
35285 +, *, -, /, &, ^, |, <<, >>
35287 where x is an lvalue expression with scalar type.
35290 # pragma omp atomic new-line
35293 # pragma omp atomic read new-line
35296 # pragma omp atomic write new-line
35299 # pragma omp atomic update new-line
35302 # pragma omp atomic capture new-line
35305 # pragma omp atomic capture new-line
35313 expression-stmt | x = x binop expr
35315 v = expression-stmt
35317 { v = x; update-stmt; } | { update-stmt; v = x; }
35321 expression-stmt | x = x binop expr | x = expr binop x
35325 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35327 where x and v are lvalue expressions with scalar type. */
35330 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35332 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35333 tree rhs1
= NULL_TREE
, orig_lhs
;
35334 location_t loc
= pragma_tok
->location
;
35335 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35336 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35337 bool structured_block
= false;
35339 tree clauses
= NULL_TREE
;
35341 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35343 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35344 cp_lexer_consume_token (parser
->lexer
);
35348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35350 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35351 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35352 const char *p
= IDENTIFIER_POINTER (id
);
35353 enum tree_code new_code
= ERROR_MARK
;
35354 enum omp_memory_order new_memory_order
35355 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35357 if (!strcmp (p
, "read"))
35358 new_code
= OMP_ATOMIC_READ
;
35359 else if (!strcmp (p
, "write"))
35360 new_code
= NOP_EXPR
;
35361 else if (!strcmp (p
, "update"))
35362 new_code
= OMP_ATOMIC
;
35363 else if (!strcmp (p
, "capture"))
35364 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35365 else if (!strcmp (p
, "seq_cst"))
35366 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35367 else if (!strcmp (p
, "acq_rel"))
35368 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35369 else if (!strcmp (p
, "release"))
35370 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35371 else if (!strcmp (p
, "acquire"))
35372 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35373 else if (!strcmp (p
, "relaxed"))
35374 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35375 else if (!strcmp (p
, "hint"))
35377 cp_lexer_consume_token (parser
->lexer
);
35378 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35384 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35385 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35386 "%<release%>, %<relaxed%> or %<hint%> clause");
35390 if (new_code
!= ERROR_MARK
)
35392 if (code
!= ERROR_MARK
)
35393 error_at (cloc
, "too many atomic clauses");
35397 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35399 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35400 error_at (cloc
, "too many memory order clauses");
35402 memory_order
= new_memory_order
;
35404 cp_lexer_consume_token (parser
->lexer
);
35410 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35412 if (code
== ERROR_MARK
)
35414 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35417 = (enum omp_requires
) (omp_requires_mask
35418 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35419 switch ((enum omp_memory_order
)
35420 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35422 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35423 case OMP_MEMORY_ORDER_RELAXED
:
35424 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35426 case OMP_MEMORY_ORDER_SEQ_CST
:
35427 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35429 case OMP_MEMORY_ORDER_ACQ_REL
:
35432 case OMP_ATOMIC_READ
:
35433 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35435 case NOP_EXPR
: /* atomic write */
35437 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35440 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35445 gcc_unreachable ();
35451 case OMP_ATOMIC_READ
:
35452 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35453 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35455 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35456 "%<acq_rel%> or %<release%> clauses");
35457 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35460 case NOP_EXPR
: /* atomic write */
35461 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35462 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35464 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35465 "%<acq_rel%> or %<acquire%> clauses");
35466 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35470 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35471 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35473 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35474 "%<acq_rel%> or %<acquire%> clauses");
35475 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35484 case OMP_ATOMIC_READ
:
35485 case NOP_EXPR
: /* atomic write */
35486 v
= cp_parser_unary_expression (parser
);
35487 if (v
== error_mark_node
)
35489 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35491 if (code
== NOP_EXPR
)
35492 lhs
= cp_parser_expression (parser
);
35494 lhs
= cp_parser_unary_expression (parser
);
35495 if (lhs
== error_mark_node
)
35497 if (code
== NOP_EXPR
)
35499 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35507 case OMP_ATOMIC_CAPTURE_NEW
:
35508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35510 cp_lexer_consume_token (parser
->lexer
);
35511 structured_block
= true;
35515 v
= cp_parser_unary_expression (parser
);
35516 if (v
== error_mark_node
)
35518 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35526 lhs
= cp_parser_unary_expression (parser
);
35528 switch (TREE_CODE (lhs
))
35533 case POSTINCREMENT_EXPR
:
35534 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35535 code
= OMP_ATOMIC_CAPTURE_OLD
;
35537 case PREINCREMENT_EXPR
:
35538 lhs
= TREE_OPERAND (lhs
, 0);
35539 opcode
= PLUS_EXPR
;
35540 rhs
= integer_one_node
;
35543 case POSTDECREMENT_EXPR
:
35544 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35545 code
= OMP_ATOMIC_CAPTURE_OLD
;
35547 case PREDECREMENT_EXPR
:
35548 lhs
= TREE_OPERAND (lhs
, 0);
35549 opcode
= MINUS_EXPR
;
35550 rhs
= integer_one_node
;
35553 case COMPOUND_EXPR
:
35554 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35555 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35556 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35557 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35558 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35559 (TREE_OPERAND (lhs
, 1), 0), 0)))
35561 /* Undo effects of boolean_increment for post {in,de}crement. */
35562 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35565 if (TREE_CODE (lhs
) == MODIFY_EXPR
35566 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35568 /* Undo effects of boolean_increment. */
35569 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35571 /* This is pre or post increment. */
35572 rhs
= TREE_OPERAND (lhs
, 1);
35573 lhs
= TREE_OPERAND (lhs
, 0);
35575 if (code
== OMP_ATOMIC_CAPTURE_NEW
35576 && !structured_block
35577 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35578 code
= OMP_ATOMIC_CAPTURE_OLD
;
35584 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35587 opcode
= MULT_EXPR
;
35590 opcode
= TRUNC_DIV_EXPR
;
35593 opcode
= PLUS_EXPR
;
35596 opcode
= MINUS_EXPR
;
35598 case CPP_LSHIFT_EQ
:
35599 opcode
= LSHIFT_EXPR
;
35601 case CPP_RSHIFT_EQ
:
35602 opcode
= RSHIFT_EXPR
;
35605 opcode
= BIT_AND_EXPR
;
35608 opcode
= BIT_IOR_EXPR
;
35611 opcode
= BIT_XOR_EXPR
;
35614 enum cp_parser_prec oprec
;
35616 cp_lexer_consume_token (parser
->lexer
);
35617 cp_parser_parse_tentatively (parser
);
35618 rhs1
= cp_parser_simple_cast_expression (parser
);
35619 if (rhs1
== error_mark_node
)
35621 cp_parser_abort_tentative_parse (parser
);
35622 cp_parser_simple_cast_expression (parser
);
35625 token
= cp_lexer_peek_token (parser
->lexer
);
35626 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35628 cp_parser_abort_tentative_parse (parser
);
35629 cp_parser_parse_tentatively (parser
);
35630 rhs
= cp_parser_binary_expression (parser
, false, true,
35631 PREC_NOT_OPERATOR
, NULL
);
35632 if (rhs
== error_mark_node
)
35634 cp_parser_abort_tentative_parse (parser
);
35635 cp_parser_binary_expression (parser
, false, true,
35636 PREC_NOT_OPERATOR
, NULL
);
35639 switch (TREE_CODE (rhs
))
35642 case TRUNC_DIV_EXPR
:
35651 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35653 if (cp_parser_parse_definitely (parser
))
35655 opcode
= TREE_CODE (rhs
);
35656 rhs1
= TREE_OPERAND (rhs
, 0);
35657 rhs
= TREE_OPERAND (rhs
, 1);
35667 cp_parser_abort_tentative_parse (parser
);
35668 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35670 rhs
= cp_parser_expression (parser
);
35671 if (rhs
== error_mark_node
)
35677 cp_parser_error (parser
,
35678 "invalid form of %<#pragma omp atomic%>");
35681 if (!cp_parser_parse_definitely (parser
))
35683 switch (token
->type
)
35685 case CPP_SEMICOLON
:
35686 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35688 code
= OMP_ATOMIC_CAPTURE_OLD
;
35693 cp_lexer_consume_token (parser
->lexer
);
35696 else if (structured_block
)
35703 cp_parser_error (parser
,
35704 "invalid form of %<#pragma omp atomic%>");
35707 opcode
= MULT_EXPR
;
35710 opcode
= TRUNC_DIV_EXPR
;
35713 opcode
= PLUS_EXPR
;
35716 opcode
= MINUS_EXPR
;
35719 opcode
= LSHIFT_EXPR
;
35722 opcode
= RSHIFT_EXPR
;
35725 opcode
= BIT_AND_EXPR
;
35728 opcode
= BIT_IOR_EXPR
;
35731 opcode
= BIT_XOR_EXPR
;
35734 cp_parser_error (parser
,
35735 "invalid operator for %<#pragma omp atomic%>");
35738 oprec
= TOKEN_PRECEDENCE (token
);
35739 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35740 if (commutative_tree_code (opcode
))
35741 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35742 cp_lexer_consume_token (parser
->lexer
);
35743 rhs
= cp_parser_binary_expression (parser
, false, false,
35745 if (rhs
== error_mark_node
)
35750 cp_parser_error (parser
,
35751 "invalid operator for %<#pragma omp atomic%>");
35754 cp_lexer_consume_token (parser
->lexer
);
35756 rhs
= cp_parser_expression (parser
);
35757 if (rhs
== error_mark_node
)
35762 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35764 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35766 v
= cp_parser_unary_expression (parser
);
35767 if (v
== error_mark_node
)
35769 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35771 lhs1
= cp_parser_unary_expression (parser
);
35772 if (lhs1
== error_mark_node
)
35775 if (structured_block
)
35777 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35778 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35781 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35782 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35783 rhs1
, clauses
, memory_order
);
35784 if (!structured_block
)
35785 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35789 cp_parser_skip_to_end_of_block_or_statement (parser
);
35790 if (structured_block
)
35792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35793 cp_lexer_consume_token (parser
->lexer
);
35794 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35796 cp_parser_skip_to_end_of_block_or_statement (parser
);
35797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35798 cp_lexer_consume_token (parser
->lexer
);
35805 # pragma omp barrier new-line */
35808 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35810 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35811 finish_omp_barrier ();
35815 # pragma omp critical [(name)] new-line
35819 # pragma omp critical [(name) [hint(expression)]] new-line
35820 structured-block */
35822 #define OMP_CRITICAL_CLAUSE_MASK \
35823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35826 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35828 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35832 matching_parens parens
;
35833 parens
.consume_open (parser
);
35835 name
= cp_parser_identifier (parser
);
35837 if (name
== error_mark_node
35838 || !parens
.require_close (parser
))
35839 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35840 /*or_comma=*/false,
35841 /*consume_paren=*/true);
35842 if (name
== error_mark_node
)
35845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35846 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35847 cp_lexer_consume_token (parser
->lexer
);
35849 clauses
= cp_parser_omp_all_clauses (parser
,
35850 OMP_CRITICAL_CLAUSE_MASK
,
35851 "#pragma omp critical", pragma_tok
);
35854 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35856 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35857 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35861 # pragma omp depobj ( depobj ) depobj-clause new-line
35864 depend (dependence-type : locator)
35866 update (dependence-type)
35875 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
35877 location_t loc
= pragma_tok
->location
;
35878 matching_parens parens
;
35879 if (!parens
.require_open (parser
))
35881 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35885 tree depobj
= cp_parser_assignment_expression (parser
);
35887 if (!parens
.require_close (parser
))
35888 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35889 /*or_comma=*/false,
35890 /*consume_paren=*/true);
35892 tree clause
= NULL_TREE
;
35893 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35894 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35897 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35898 const char *p
= IDENTIFIER_POINTER (id
);
35900 cp_lexer_consume_token (parser
->lexer
);
35901 if (!strcmp ("depend", p
))
35903 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
35905 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
35907 clause
= error_mark_node
;
35909 else if (!strcmp ("destroy", p
))
35910 kind
= OMP_CLAUSE_DEPEND_LAST
;
35911 else if (!strcmp ("update", p
))
35913 matching_parens c_parens
;
35914 if (c_parens
.require_open (parser
))
35917 = cp_lexer_peek_token (parser
->lexer
)->location
;
35918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35920 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35921 const char *p2
= IDENTIFIER_POINTER (id2
);
35923 cp_lexer_consume_token (parser
->lexer
);
35924 if (!strcmp ("in", p2
))
35925 kind
= OMP_CLAUSE_DEPEND_IN
;
35926 else if (!strcmp ("out", p2
))
35927 kind
= OMP_CLAUSE_DEPEND_OUT
;
35928 else if (!strcmp ("inout", p2
))
35929 kind
= OMP_CLAUSE_DEPEND_INOUT
;
35930 else if (!strcmp ("mutexinoutset", p2
))
35931 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
35933 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35935 clause
= error_mark_node
;
35936 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
35937 "%<mutexinoutset%>");
35939 if (!c_parens
.require_close (parser
))
35940 cp_parser_skip_to_closing_parenthesis (parser
,
35941 /*recovering=*/true,
35942 /*or_comma=*/false,
35943 /*consume_paren=*/true);
35946 clause
= error_mark_node
;
35949 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35951 clause
= error_mark_node
;
35952 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
35954 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35956 finish_omp_depobj (loc
, depobj
, kind
, clause
);
35961 # pragma omp flush flush-vars[opt] new-line
35967 # pragma omp flush memory-order-clause new-line */
35970 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
35972 enum memmodel mo
= MEMMODEL_LAST
;
35973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35976 const char *p
= IDENTIFIER_POINTER (id
);
35977 if (!strcmp (p
, "acq_rel"))
35978 mo
= MEMMODEL_ACQ_REL
;
35979 else if (!strcmp (p
, "release"))
35980 mo
= MEMMODEL_RELEASE
;
35981 else if (!strcmp (p
, "acquire"))
35982 mo
= MEMMODEL_ACQUIRE
;
35984 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35985 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35986 cp_lexer_consume_token (parser
->lexer
);
35988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35990 if (mo
!= MEMMODEL_LAST
)
35991 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35992 "%<flush%> list specified together with memory order "
35994 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35996 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35998 finish_omp_flush (mo
);
36001 /* Helper function, to parse omp for increment expression. */
36004 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36006 tree cond
= cp_parser_binary_expression (parser
, false, true,
36007 PREC_NOT_OPERATOR
, NULL
);
36008 if (cond
== error_mark_node
36009 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36011 cp_parser_skip_to_end_of_statement (parser
);
36012 return error_mark_node
;
36015 switch (TREE_CODE (cond
))
36023 if (code
!= OACC_LOOP
)
36025 gcc_fallthrough ();
36027 return error_mark_node
;
36030 /* If decl is an iterator, preserve LHS and RHS of the relational
36031 expr until finish_omp_for. */
36033 && (type_dependent_expression_p (decl
)
36034 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36037 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36039 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36040 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36041 /*overload=*/NULL
, tf_warning_or_error
);
36044 /* Helper function, to parse omp for increment expression. */
36047 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36049 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36055 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36057 op
= (token
->type
== CPP_PLUS_PLUS
36058 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36059 cp_lexer_consume_token (parser
->lexer
);
36060 lhs
= cp_parser_simple_cast_expression (parser
);
36062 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36063 return error_mark_node
;
36064 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36067 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36069 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36070 return error_mark_node
;
36072 token
= cp_lexer_peek_token (parser
->lexer
);
36073 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36075 op
= (token
->type
== CPP_PLUS_PLUS
36076 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36077 cp_lexer_consume_token (parser
->lexer
);
36078 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36081 op
= cp_parser_assignment_operator_opt (parser
);
36082 if (op
== ERROR_MARK
)
36083 return error_mark_node
;
36085 if (op
!= NOP_EXPR
)
36087 rhs
= cp_parser_assignment_expression (parser
);
36088 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36089 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36092 lhs
= cp_parser_binary_expression (parser
, false, false,
36093 PREC_ADDITIVE_EXPRESSION
, NULL
);
36094 token
= cp_lexer_peek_token (parser
->lexer
);
36095 decl_first
= (lhs
== decl
36096 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36099 if (token
->type
!= CPP_PLUS
36100 && token
->type
!= CPP_MINUS
)
36101 return error_mark_node
;
36105 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36106 cp_lexer_consume_token (parser
->lexer
);
36107 rhs
= cp_parser_binary_expression (parser
, false, false,
36108 PREC_ADDITIVE_EXPRESSION
, NULL
);
36109 token
= cp_lexer_peek_token (parser
->lexer
);
36110 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36112 if (lhs
== NULL_TREE
)
36114 if (op
== PLUS_EXPR
)
36117 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36118 tf_warning_or_error
);
36121 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36122 ERROR_MARK
, NULL
, tf_warning_or_error
);
36125 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36130 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36131 || op
== MINUS_EXPR
)
36132 return error_mark_node
;
36133 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36136 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36138 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36141 /* Parse the initialization statement of an OpenMP for loop.
36143 Return true if the resulting construct should have an
36144 OMP_CLAUSE_PRIVATE added to it. */
36147 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36148 tree
&this_pre_body
,
36149 vec
<tree
, va_gc
> *&for_block
,
36155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36158 tree add_private_clause
= NULL_TREE
;
36160 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36164 integer-type var = lb
36165 random-access-iterator-type var = lb
36166 pointer-type var = lb
36168 cp_decl_specifier_seq type_specifiers
;
36170 /* First, try to parse as an initialized declaration. See
36171 cp_parser_condition, from whence the bulk of this is copied. */
36173 cp_parser_parse_tentatively (parser
);
36174 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36175 /*is_declaration=*/true,
36176 /*is_trailing_return=*/false,
36178 if (cp_parser_parse_definitely (parser
))
36180 /* If parsing a type specifier seq succeeded, then this
36181 MUST be a initialized declaration. */
36182 tree asm_specification
, attributes
;
36183 cp_declarator
*declarator
;
36185 declarator
= cp_parser_declarator (parser
,
36186 CP_PARSER_DECLARATOR_NAMED
,
36187 CP_PARSER_FLAGS_NONE
,
36188 /*ctor_dtor_or_conv_p=*/NULL
,
36189 /*parenthesized_p=*/NULL
,
36190 /*member_p=*/false,
36191 /*friend_p=*/false);
36192 attributes
= cp_parser_attributes_opt (parser
);
36193 asm_specification
= cp_parser_asm_specification_opt (parser
);
36195 if (declarator
== cp_error_declarator
)
36196 cp_parser_skip_to_end_of_statement (parser
);
36200 tree pushed_scope
, auto_node
;
36202 decl
= start_decl (declarator
, &type_specifiers
,
36203 SD_INITIALIZED
, attributes
,
36204 /*prefix_attributes=*/NULL_TREE
,
36207 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36208 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36210 if (cp_lexer_next_token_is (parser
->lexer
,
36212 error ("parenthesized initialization is not allowed in "
36213 "OpenMP %<for%> loop");
36215 /* Trigger an error. */
36216 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36218 init
= error_mark_node
;
36219 cp_parser_skip_to_end_of_statement (parser
);
36221 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36222 || type_dependent_expression_p (decl
)
36225 bool is_direct_init
, is_non_constant_init
;
36227 init
= cp_parser_initializer (parser
,
36229 &is_non_constant_init
);
36234 = do_auto_deduction (TREE_TYPE (decl
), init
,
36237 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36238 && !type_dependent_expression_p (decl
))
36242 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36244 LOOKUP_ONLYCONVERTING
);
36246 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36248 vec_safe_push (for_block
, this_pre_body
);
36253 init
= pop_stmt_list (this_pre_body
);
36254 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36256 tree_stmt_iterator i
= tsi_start (init
);
36257 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36258 while (!tsi_end_p (i
))
36260 tree t
= tsi_stmt (i
);
36261 if (TREE_CODE (t
) == DECL_EXPR
36262 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36265 vec_safe_push (for_block
, t
);
36270 if (tsi_one_before_end_p (i
))
36272 tree t
= tsi_stmt (i
);
36274 free_stmt_list (init
);
36279 this_pre_body
= NULL_TREE
;
36284 cp_lexer_consume_token (parser
->lexer
);
36285 init
= cp_parser_assignment_expression (parser
);
36288 if (TYPE_REF_P (TREE_TYPE (decl
)))
36289 init
= error_mark_node
;
36291 cp_finish_decl (decl
, NULL_TREE
,
36292 /*init_const_expr_p=*/false,
36294 LOOKUP_ONLYCONVERTING
);
36298 pop_scope (pushed_scope
);
36304 /* If parsing a type specifier sequence failed, then
36305 this MUST be a simple expression. */
36306 cp_parser_parse_tentatively (parser
);
36307 decl
= cp_parser_primary_expression (parser
, false, false,
36309 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36310 if (!cp_parser_error_occurred (parser
)
36312 && (TREE_CODE (decl
) == COMPONENT_REF
36313 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36315 cp_parser_abort_tentative_parse (parser
);
36316 cp_parser_parse_tentatively (parser
);
36317 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36318 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36319 /*check_dependency_p=*/true,
36320 /*template_p=*/NULL
,
36321 /*declarator_p=*/false,
36322 /*optional_p=*/false);
36323 if (name
!= error_mark_node
36324 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36326 decl
= cp_parser_lookup_name_simple (parser
, name
,
36328 if (TREE_CODE (decl
) == FIELD_DECL
)
36329 add_private_clause
= omp_privatize_field (decl
, false);
36331 cp_parser_abort_tentative_parse (parser
);
36332 cp_parser_parse_tentatively (parser
);
36333 decl
= cp_parser_primary_expression (parser
, false, false,
36336 if (!cp_parser_error_occurred (parser
)
36339 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36343 cp_parser_parse_definitely (parser
);
36344 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36345 rhs
= cp_parser_assignment_expression (parser
);
36347 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36350 tf_warning_or_error
));
36351 if (!add_private_clause
)
36352 add_private_clause
= decl
;
36357 cp_parser_abort_tentative_parse (parser
);
36358 init
= cp_parser_expression (parser
);
36361 if (TREE_CODE (init
) == MODIFY_EXPR
36362 || TREE_CODE (init
) == MODOP_EXPR
)
36363 real_decl
= TREE_OPERAND (init
, 0);
36367 return add_private_clause
;
36370 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36373 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36374 tree
&decl
, tree
&orig_decl
, tree
&init
,
36375 tree
&orig_init
, tree
&cond
, tree
&incr
)
36377 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36378 tree iter_type
, begin_expr
, end_expr
;
36380 if (processing_template_decl
)
36382 if (check_for_bare_parameter_packs (init
))
36383 init
= error_mark_node
;
36384 if (!type_dependent_expression_p (init
)
36385 /* do_auto_deduction doesn't mess with template init-lists. */
36386 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36389 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36391 tree v
= DECL_VALUE_EXPR (decl
);
36392 if (TREE_CODE (v
) == ARRAY_REF
36393 && VAR_P (TREE_OPERAND (v
, 0))
36394 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36395 d
= TREE_OPERAND (v
, 0);
36397 do_range_for_auto_deduction (d
, init
);
36399 cond
= global_namespace
;
36403 this_pre_body
= pop_stmt_list (this_pre_body
);
36407 init
= mark_lvalue_use (init
);
36409 if (decl
== error_mark_node
|| init
== error_mark_node
)
36410 /* If an error happened previously do nothing or else a lot of
36411 unhelpful errors would be issued. */
36412 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36418 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36419 /* Can't bind a reference to an array of runtime bound. */
36423 range_temp
= build_range_temp (init
);
36424 DECL_NAME (range_temp
) = NULL_TREE
;
36425 pushdecl (range_temp
);
36426 cp_finish_decl (range_temp
, init
,
36427 /*is_constant_init*/false, NULL_TREE
,
36428 LOOKUP_ONLYCONVERTING
);
36429 range_temp_decl
= range_temp
;
36430 range_temp
= convert_from_reference (range_temp
);
36432 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36433 &begin_expr
, &end_expr
);
36436 tree end_iter_type
= iter_type
;
36437 if (cxx_dialect
>= cxx17
)
36438 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36439 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36440 TREE_USED (end
) = 1;
36441 DECL_ARTIFICIAL (end
) = 1;
36443 cp_finish_decl (end
, end_expr
,
36444 /*is_constant_init*/false, NULL_TREE
,
36445 LOOKUP_ONLYCONVERTING
);
36447 /* The new for initialization statement. */
36448 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36449 TREE_USED (begin
) = 1;
36450 DECL_ARTIFICIAL (begin
) = 1;
36453 if (CLASS_TYPE_P (iter_type
))
36458 begin_expr
= NULL_TREE
;
36460 cp_finish_decl (begin
, begin_expr
,
36461 /*is_constant_init*/false, NULL_TREE
,
36462 LOOKUP_ONLYCONVERTING
);
36464 /* The new for condition. */
36465 if (CLASS_TYPE_P (iter_type
))
36466 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36468 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36471 NULL
, tf_warning_or_error
);
36473 /* The new increment expression. */
36474 if (CLASS_TYPE_P (iter_type
))
36475 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36477 incr
= finish_unary_op_expr (input_location
,
36478 PREINCREMENT_EXPR
, begin
,
36479 tf_warning_or_error
);
36485 vec_safe_push (for_block
, this_pre_body
);
36486 this_pre_body
= NULL_TREE
;
36489 tree decomp_first_name
= NULL_TREE
;
36490 unsigned decomp_cnt
= 0;
36491 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36493 tree v
= DECL_VALUE_EXPR (orig_decl
);
36494 if (TREE_CODE (v
) == ARRAY_REF
36495 && VAR_P (TREE_OPERAND (v
, 0))
36496 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36498 tree d
= orig_decl
;
36499 orig_decl
= TREE_OPERAND (v
, 0);
36500 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36501 decomp_first_name
= d
;
36505 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36508 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36510 if (!error_operand_p (t
))
36511 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36515 tree v
= make_tree_vec (decomp_cnt
+ 3);
36516 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36517 TREE_VEC_ELT (v
, 1) = end
;
36518 TREE_VEC_ELT (v
, 2) = orig_decl
;
36519 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36521 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36522 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36524 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36527 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36528 inside of the collapsed body. */
36531 cp_finish_omp_range_for (tree orig
, tree begin
)
36533 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36534 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36535 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36536 tree decomp_first_name
= NULL_TREE
;
36537 unsigned int decomp_cnt
= 0;
36539 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36541 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36542 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36543 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36546 /* The declaration is initialized with *__begin inside the loop body. */
36547 cp_finish_decl (decl
,
36548 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36549 tf_warning_or_error
),
36550 /*is_constant_init*/false, NULL_TREE
,
36551 LOOKUP_ONLYCONVERTING
);
36552 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36553 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36556 /* Parse the restricted form of the for statement allowed by OpenMP. */
36559 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36560 tree
*cclauses
, bool *if_p
)
36562 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36564 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36565 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36566 location_t loc_first
;
36567 bool collapse_err
= false;
36568 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36569 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36570 auto_vec
<tree
, 4> orig_inits
;
36571 bool tiling
= false;
36573 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36574 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36575 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36576 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36579 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36581 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36582 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36585 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36588 if (ordered
&& ordered
< collapse
)
36590 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36591 "%<ordered%> clause parameter is less than %<collapse%>");
36592 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36593 = build_int_cst (NULL_TREE
, collapse
);
36594 ordered
= collapse
;
36598 for (tree
*pc
= &clauses
; *pc
; )
36599 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36601 error_at (OMP_CLAUSE_LOCATION (*pc
),
36602 "%<linear%> clause may not be specified together "
36603 "with %<ordered%> clause with a parameter");
36604 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36607 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36610 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36611 count
= ordered
? ordered
: collapse
;
36613 declv
= make_tree_vec (count
);
36614 initv
= make_tree_vec (count
);
36615 condv
= make_tree_vec (count
);
36616 incrv
= make_tree_vec (count
);
36617 orig_declv
= NULL_TREE
;
36619 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36621 for (i
= 0; i
< count
; i
++)
36623 int bracecount
= 0;
36624 tree add_private_clause
= NULL_TREE
;
36627 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36630 cp_parser_error (parser
, "for statement expected");
36633 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36635 /* Don't create location wrapper nodes within an OpenMP "for"
36637 auto_suppress_location_wrappers sentinel
;
36639 matching_parens parens
;
36640 if (!parens
.require_open (parser
))
36643 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36644 this_pre_body
= push_stmt_list ();
36646 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36648 /* Save tokens so that we can put them back. */
36649 cp_lexer_save_tokens (parser
->lexer
);
36651 /* Look for ':' that is not nested in () or {}. */
36653 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36654 /*recovering=*/false,
36659 /* Roll back the tokens we skipped. */
36660 cp_lexer_rollback_tokens (parser
->lexer
);
36664 bool saved_colon_corrects_to_scope_p
36665 = parser
->colon_corrects_to_scope_p
;
36667 /* A colon is used in range-based for. */
36668 parser
->colon_corrects_to_scope_p
= false;
36670 /* Parse the declaration. */
36671 cp_parser_simple_declaration (parser
,
36672 /*function_definition_allowed_p=*/
36674 parser
->colon_corrects_to_scope_p
36675 = saved_colon_corrects_to_scope_p
;
36677 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36679 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36682 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36683 orig_decl
, init
, orig_init
,
36690 pre_body
= push_stmt_list ();
36692 add_stmt (this_pre_body
);
36693 pre_body
= pop_stmt_list (pre_body
);
36696 pre_body
= this_pre_body
;
36700 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36701 "%<ordered%> clause with parameter on "
36702 "range-based %<for%> loop");
36704 goto parse_close_paren
;
36709 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36710 init
, orig_init
, decl
, real_decl
);
36712 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36715 this_pre_body
= pop_stmt_list (this_pre_body
);
36719 pre_body
= push_stmt_list ();
36721 add_stmt (this_pre_body
);
36722 pre_body
= pop_stmt_list (pre_body
);
36725 pre_body
= this_pre_body
;
36730 if (cclauses
!= NULL
36731 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36732 && real_decl
!= NULL_TREE
)
36735 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36736 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36737 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36739 error_at (loc
, "iteration variable %qD"
36740 " should not be firstprivate", real_decl
);
36741 *c
= OMP_CLAUSE_CHAIN (*c
);
36743 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36744 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36746 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36748 *c
= OMP_CLAUSE_CHAIN (*c
);
36749 if (code
== OMP_SIMD
)
36751 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36752 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36756 OMP_CLAUSE_CHAIN (l
) = clauses
;
36759 add_private_clause
= NULL_TREE
;
36763 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36764 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36765 add_private_clause
= NULL_TREE
;
36766 c
= &OMP_CLAUSE_CHAIN (*c
);
36770 if (add_private_clause
)
36773 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36775 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36776 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36777 && OMP_CLAUSE_DECL (c
) == decl
)
36779 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36780 && OMP_CLAUSE_DECL (c
) == decl
)
36781 error_at (loc
, "iteration variable %qD "
36782 "should not be firstprivate",
36784 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36785 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36786 && OMP_CLAUSE_DECL (c
) == decl
)
36787 error_at (loc
, "iteration variable %qD should not be reduction",
36792 if (code
!= OMP_SIMD
)
36793 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36794 else if (collapse
== 1)
36795 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36797 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36798 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36799 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36802 OMP_CLAUSE_CHAIN (c
) = clauses
;
36804 /* For linear, signal that we need to fill up
36805 the so far unknown linear step. */
36806 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36807 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36813 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36814 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36815 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36818 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36820 /* If decl is an iterator, preserve the operator on decl
36821 until finish_omp_for. */
36823 && ((processing_template_decl
36824 && (TREE_TYPE (real_decl
) == NULL_TREE
36825 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36826 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36827 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36829 incr
= cp_parser_expression (parser
);
36830 if (!EXPR_HAS_LOCATION (incr
))
36831 protected_set_expr_location (incr
, input_location
);
36835 if (!parens
.require_close (parser
))
36836 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36837 /*or_comma=*/false,
36838 /*consume_paren=*/true);
36840 TREE_VEC_ELT (declv
, i
) = decl
;
36841 TREE_VEC_ELT (initv
, i
) = init
;
36842 TREE_VEC_ELT (condv
, i
) = cond
;
36843 TREE_VEC_ELT (incrv
, i
) = incr
;
36846 orig_inits
.safe_grow_cleared (i
+ 1);
36847 orig_inits
[i
] = orig_init
;
36852 orig_declv
= copy_node (declv
);
36853 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36855 else if (orig_declv
)
36856 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36858 if (i
== count
- 1)
36861 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36862 in between the collapsed for loops to be still considered perfectly
36863 nested. Hopefully the final version clarifies this.
36864 For now handle (multiple) {'s and empty statements. */
36865 cp_parser_parse_tentatively (parser
);
36868 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36870 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36872 cp_lexer_consume_token (parser
->lexer
);
36875 else if (bracecount
36876 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36877 cp_lexer_consume_token (parser
->lexer
);
36880 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36881 error_at (loc
, "not enough for loops to collapse");
36882 collapse_err
= true;
36883 cp_parser_abort_tentative_parse (parser
);
36891 cp_parser_parse_definitely (parser
);
36892 nbraces
+= bracecount
;
36899 /* Note that we saved the original contents of this flag when we entered
36900 the structured block, and so we don't need to re-save it here. */
36901 parser
->in_statement
= IN_OMP_FOR
;
36903 /* Note that the grammar doesn't call for a structured block here,
36904 though the loop as a whole is a structured block. */
36907 body
= begin_omp_structured_block ();
36908 for (i
= 0; i
< count
; i
++)
36909 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
36910 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
36911 TREE_VEC_ELT (declv
, i
));
36914 body
= push_stmt_list ();
36915 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36917 body
= finish_omp_structured_block (body
);
36919 body
= pop_stmt_list (body
);
36921 if (declv
== NULL_TREE
)
36924 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
36925 incrv
, body
, pre_body
, &orig_inits
, clauses
);
36929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36931 cp_lexer_consume_token (parser
->lexer
);
36934 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36935 cp_lexer_consume_token (parser
->lexer
);
36940 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36941 "collapsed loops not perfectly nested");
36943 collapse_err
= true;
36944 cp_parser_statement_seq_opt (parser
, NULL
);
36945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
36950 while (!for_block
->is_empty ())
36952 tree t
= for_block
->pop ();
36953 if (TREE_CODE (t
) == STATEMENT_LIST
)
36954 add_stmt (pop_stmt_list (t
));
36958 release_tree_vector (for_block
);
36963 /* Helper function for OpenMP parsing, split clauses and call
36964 finish_omp_clauses on each of the set of clauses afterwards. */
36967 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
36968 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
36971 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
36972 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
36974 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
36978 #pragma omp simd simd-clause[optseq] new-line
36981 #define OMP_SIMD_CLAUSE_MASK \
36982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36994 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36995 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36998 tree clauses
, sb
, ret
;
37000 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37002 strcat (p_name
, " simd");
37003 mask
|= OMP_SIMD_CLAUSE_MASK
;
37005 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37009 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37010 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37011 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37012 OMP_CLAUSE_ORDERED
);
37013 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37015 error_at (OMP_CLAUSE_LOCATION (c
),
37016 "%<ordered%> clause with parameter may not be specified "
37017 "on %qs construct", p_name
);
37018 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37022 keep_next_level (true);
37023 sb
= begin_omp_structured_block ();
37024 save
= cp_parser_begin_omp_structured_block (parser
);
37026 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37028 cp_parser_end_omp_structured_block (parser
, save
);
37029 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37035 #pragma omp for for-clause[optseq] new-line
37039 #pragma omp for simd for-simd-clause[optseq] new-line
37042 #define OMP_FOR_CLAUSE_MASK \
37043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37054 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37055 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37058 tree clauses
, sb
, ret
;
37060 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37062 strcat (p_name
, " for");
37063 mask
|= OMP_FOR_CLAUSE_MASK
;
37064 /* parallel for{, simd} disallows nowait clause, but for
37065 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37066 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37067 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37068 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37069 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37070 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37074 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37075 const char *p
= IDENTIFIER_POINTER (id
);
37077 if (strcmp (p
, "simd") == 0)
37079 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37080 if (cclauses
== NULL
)
37081 cclauses
= cclauses_buf
;
37083 cp_lexer_consume_token (parser
->lexer
);
37084 if (!flag_openmp
) /* flag_openmp_simd */
37085 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37087 sb
= begin_omp_structured_block ();
37088 save
= cp_parser_begin_omp_structured_block (parser
);
37089 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37091 cp_parser_end_omp_structured_block (parser
, save
);
37092 tree body
= finish_omp_structured_block (sb
);
37095 ret
= make_node (OMP_FOR
);
37096 TREE_TYPE (ret
) = void_type_node
;
37097 OMP_FOR_BODY (ret
) = body
;
37098 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37099 SET_EXPR_LOCATION (ret
, loc
);
37104 if (!flag_openmp
) /* flag_openmp_simd */
37106 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37110 /* Composite distribute parallel for disallows linear clause. */
37111 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37112 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37114 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37118 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37119 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37122 keep_next_level (true);
37123 sb
= begin_omp_structured_block ();
37124 save
= cp_parser_begin_omp_structured_block (parser
);
37126 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37128 cp_parser_end_omp_structured_block (parser
, save
);
37129 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37134 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37135 omp_clause_mask
, tree
*, bool *);
37138 # pragma omp master new-line
37139 structured-block */
37142 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37143 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37146 tree clauses
, sb
, ret
;
37148 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37150 strcat (p_name
, " master");
37152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37154 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37155 const char *p
= IDENTIFIER_POINTER (id
);
37157 if (strcmp (p
, "taskloop") == 0)
37159 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37160 if (cclauses
== NULL
)
37161 cclauses
= cclauses_buf
;
37163 cp_lexer_consume_token (parser
->lexer
);
37164 if (!flag_openmp
) /* flag_openmp_simd */
37165 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37167 sb
= begin_omp_structured_block ();
37168 save
= cp_parser_begin_omp_structured_block (parser
);
37169 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37171 cp_parser_end_omp_structured_block (parser
, save
);
37172 tree body
= finish_omp_structured_block (sb
);
37175 return c_finish_omp_master (loc
, body
);
37178 if (!flag_openmp
) /* flag_openmp_simd */
37180 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37186 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37188 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37191 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37193 return c_finish_omp_master (loc
,
37194 cp_parser_omp_structured_block (parser
, if_p
));
37198 # pragma omp ordered new-line
37202 # pragma omp ordered ordered-clauses new-line
37203 structured-block */
37205 #define OMP_ORDERED_CLAUSE_MASK \
37206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37209 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37210 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37213 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37214 enum pragma_context context
, bool *if_p
)
37216 location_t loc
= pragma_tok
->location
;
37218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37221 const char *p
= IDENTIFIER_POINTER (id
);
37223 if (strcmp (p
, "depend") == 0)
37225 if (!flag_openmp
) /* flag_openmp_simd */
37227 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37230 if (context
== pragma_stmt
)
37232 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37233 "%<depend%> clause may only be used in compound "
37235 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37239 = cp_parser_omp_all_clauses (parser
,
37240 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37241 "#pragma omp ordered", pragma_tok
);
37242 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37248 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37249 "#pragma omp ordered", pragma_tok
);
37251 if (!flag_openmp
/* flag_openmp_simd */
37252 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37255 c_finish_omp_ordered (loc
, clauses
,
37256 cp_parser_omp_structured_block (parser
, if_p
));
37263 { section-sequence }
37266 section-directive[opt] structured-block
37267 section-sequence section-directive structured-block */
37270 cp_parser_omp_sections_scope (cp_parser
*parser
)
37272 tree stmt
, substmt
;
37273 bool error_suppress
= false;
37276 matching_braces braces
;
37277 if (!braces
.require_open (parser
))
37280 stmt
= push_stmt_list ();
37282 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37283 != PRAGMA_OMP_SECTION
)
37285 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37286 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37287 add_stmt (substmt
);
37292 tok
= cp_lexer_peek_token (parser
->lexer
);
37293 if (tok
->type
== CPP_CLOSE_BRACE
)
37295 if (tok
->type
== CPP_EOF
)
37298 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37300 cp_lexer_consume_token (parser
->lexer
);
37301 cp_parser_require_pragma_eol (parser
, tok
);
37302 error_suppress
= false;
37304 else if (!error_suppress
)
37306 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37307 error_suppress
= true;
37310 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37311 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37312 add_stmt (substmt
);
37314 braces
.require_close (parser
);
37316 substmt
= pop_stmt_list (stmt
);
37318 stmt
= make_node (OMP_SECTIONS
);
37319 TREE_TYPE (stmt
) = void_type_node
;
37320 OMP_SECTIONS_BODY (stmt
) = substmt
;
37327 # pragma omp sections sections-clause[optseq] newline
37330 #define OMP_SECTIONS_CLAUSE_MASK \
37331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37338 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37339 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37344 strcat (p_name
, " sections");
37345 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37347 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37349 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37353 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37354 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37357 ret
= cp_parser_omp_sections_scope (parser
);
37359 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37365 # pragma omp parallel parallel-clause[optseq] new-line
37367 # pragma omp parallel for parallel-for-clause[optseq] new-line
37369 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37373 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37374 structured-block */
37376 #define OMP_PARALLEL_CLAUSE_MASK \
37377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37388 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37389 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37392 tree stmt
, clauses
, block
;
37394 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37396 strcat (p_name
, " parallel");
37397 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37398 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37399 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37400 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37401 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37403 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37405 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37406 if (cclauses
== NULL
)
37407 cclauses
= cclauses_buf
;
37409 cp_lexer_consume_token (parser
->lexer
);
37410 if (!flag_openmp
) /* flag_openmp_simd */
37411 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37413 block
= begin_omp_parallel ();
37414 save
= cp_parser_begin_omp_structured_block (parser
);
37415 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37417 cp_parser_end_omp_structured_block (parser
, save
);
37418 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37420 if (ret
== NULL_TREE
)
37422 OMP_PARALLEL_COMBINED (stmt
) = 1;
37425 /* When combined with distribute, parallel has to be followed by for.
37426 #pragma omp target parallel is allowed though. */
37428 && (mask
& (OMP_CLAUSE_MASK_1
37429 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37431 error_at (loc
, "expected %<for%> after %qs", p_name
);
37432 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37435 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37437 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37438 const char *p
= IDENTIFIER_POINTER (id
);
37439 if (strcmp (p
, "master") == 0)
37441 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37442 cclauses
= cclauses_buf
;
37444 cp_lexer_consume_token (parser
->lexer
);
37445 block
= begin_omp_parallel ();
37446 save
= cp_parser_begin_omp_structured_block (parser
);
37447 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37449 cp_parser_end_omp_structured_block (parser
, save
);
37450 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37452 OMP_PARALLEL_COMBINED (stmt
) = 1;
37453 if (ret
== NULL_TREE
)
37457 else if (!flag_openmp
) /* flag_openmp_simd */
37459 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37462 else if (strcmp (p
, "sections") == 0)
37464 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37465 cclauses
= cclauses_buf
;
37467 cp_lexer_consume_token (parser
->lexer
);
37468 block
= begin_omp_parallel ();
37469 save
= cp_parser_begin_omp_structured_block (parser
);
37470 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37471 cp_parser_end_omp_structured_block (parser
, save
);
37472 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37474 OMP_PARALLEL_COMBINED (stmt
) = 1;
37478 else if (!flag_openmp
) /* flag_openmp_simd */
37480 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37484 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37488 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37489 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37492 block
= begin_omp_parallel ();
37493 save
= cp_parser_begin_omp_structured_block (parser
);
37494 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37495 cp_parser_end_omp_structured_block (parser
, save
);
37496 stmt
= finish_omp_parallel (clauses
, block
);
37501 # pragma omp single single-clause[optseq] new-line
37502 structured-block */
37504 #define OMP_SINGLE_CLAUSE_MASK \
37505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37511 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37513 tree stmt
= make_node (OMP_SINGLE
);
37514 TREE_TYPE (stmt
) = void_type_node
;
37515 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37517 OMP_SINGLE_CLAUSES (stmt
)
37518 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37519 "#pragma omp single", pragma_tok
);
37520 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37522 return add_stmt (stmt
);
37526 # pragma omp task task-clause[optseq] new-line
37527 structured-block */
37529 #define OMP_TASK_CLAUSE_MASK \
37530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37543 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37545 tree clauses
, block
;
37548 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37549 "#pragma omp task", pragma_tok
);
37550 block
= begin_omp_task ();
37551 save
= cp_parser_begin_omp_structured_block (parser
);
37552 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37553 cp_parser_end_omp_structured_block (parser
, save
);
37554 return finish_omp_task (clauses
, block
);
37558 # pragma omp taskwait new-line
37561 # pragma omp taskwait taskwait-clause[opt] new-line */
37563 #define OMP_TASKWAIT_CLAUSE_MASK \
37564 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37567 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37570 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37571 "#pragma omp taskwait", pragma_tok
);
37575 tree stmt
= make_node (OMP_TASK
);
37576 TREE_TYPE (stmt
) = void_node
;
37577 OMP_TASK_CLAUSES (stmt
) = clauses
;
37578 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37579 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37583 finish_omp_taskwait ();
37587 # pragma omp taskyield new-line */
37590 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37592 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37593 finish_omp_taskyield ();
37597 # pragma omp taskgroup new-line
37601 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37603 #define OMP_TASKGROUP_CLAUSE_MASK \
37604 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37607 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37610 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37611 "#pragma omp taskgroup", pragma_tok
);
37612 return c_finish_omp_taskgroup (input_location
,
37613 cp_parser_omp_structured_block (parser
,
37620 # pragma omp threadprivate (variable-list) */
37623 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37627 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37628 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37630 finish_omp_threadprivate (vars
);
37634 # pragma omp cancel cancel-clause[optseq] new-line */
37636 #define OMP_CANCEL_CLAUSE_MASK \
37637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37644 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37646 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37647 "#pragma omp cancel", pragma_tok
);
37648 finish_omp_cancel (clauses
);
37652 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37654 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37661 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37662 enum pragma_context context
)
37665 bool point_seen
= false;
37667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37669 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37670 const char *p
= IDENTIFIER_POINTER (id
);
37672 if (strcmp (p
, "point") == 0)
37674 cp_lexer_consume_token (parser
->lexer
);
37680 cp_parser_error (parser
, "expected %<point%>");
37681 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37685 if (context
!= pragma_compound
)
37687 if (context
== pragma_stmt
)
37688 error_at (pragma_tok
->location
,
37689 "%<#pragma %s%> may only be used in compound statements",
37690 "omp cancellation point");
37692 cp_parser_error (parser
, "expected declaration specifiers");
37693 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37697 clauses
= cp_parser_omp_all_clauses (parser
,
37698 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37699 "#pragma omp cancellation point",
37701 finish_omp_cancellation_point (clauses
);
37705 #pragma omp distribute distribute-clause[optseq] new-line
37708 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37709 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37716 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37717 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37720 tree clauses
, sb
, ret
;
37722 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37724 strcat (p_name
, " distribute");
37725 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37729 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37730 const char *p
= IDENTIFIER_POINTER (id
);
37732 bool parallel
= false;
37734 if (strcmp (p
, "simd") == 0)
37737 parallel
= strcmp (p
, "parallel") == 0;
37738 if (parallel
|| simd
)
37740 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37741 if (cclauses
== NULL
)
37742 cclauses
= cclauses_buf
;
37743 cp_lexer_consume_token (parser
->lexer
);
37744 if (!flag_openmp
) /* flag_openmp_simd */
37747 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37750 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37753 sb
= begin_omp_structured_block ();
37754 save
= cp_parser_begin_omp_structured_block (parser
);
37756 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37759 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37761 cp_parser_end_omp_structured_block (parser
, save
);
37762 tree body
= finish_omp_structured_block (sb
);
37765 ret
= make_node (OMP_DISTRIBUTE
);
37766 TREE_TYPE (ret
) = void_type_node
;
37767 OMP_FOR_BODY (ret
) = body
;
37768 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37769 SET_EXPR_LOCATION (ret
, loc
);
37774 if (!flag_openmp
) /* flag_openmp_simd */
37776 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37780 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37784 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37785 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37788 keep_next_level (true);
37789 sb
= begin_omp_structured_block ();
37790 save
= cp_parser_begin_omp_structured_block (parser
);
37792 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37794 cp_parser_end_omp_structured_block (parser
, save
);
37795 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37801 # pragma omp teams teams-clause[optseq] new-line
37802 structured-block */
37804 #define OMP_TEAMS_CLAUSE_MASK \
37805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37814 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37815 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37818 tree clauses
, sb
, ret
;
37820 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37822 strcat (p_name
, " teams");
37823 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37827 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37828 const char *p
= IDENTIFIER_POINTER (id
);
37829 if (strcmp (p
, "distribute") == 0)
37831 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37832 if (cclauses
== NULL
)
37833 cclauses
= cclauses_buf
;
37835 cp_lexer_consume_token (parser
->lexer
);
37836 if (!flag_openmp
) /* flag_openmp_simd */
37837 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37839 keep_next_level (true);
37840 sb
= begin_omp_structured_block ();
37841 save
= cp_parser_begin_omp_structured_block (parser
);
37842 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37844 cp_parser_end_omp_structured_block (parser
, save
);
37845 tree body
= finish_omp_structured_block (sb
);
37848 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37849 ret
= make_node (OMP_TEAMS
);
37850 TREE_TYPE (ret
) = void_type_node
;
37851 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37852 OMP_TEAMS_BODY (ret
) = body
;
37853 OMP_TEAMS_COMBINED (ret
) = 1;
37854 SET_EXPR_LOCATION (ret
, loc
);
37855 return add_stmt (ret
);
37858 if (!flag_openmp
) /* flag_openmp_simd */
37860 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37864 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37868 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
37869 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37872 tree stmt
= make_node (OMP_TEAMS
);
37873 TREE_TYPE (stmt
) = void_type_node
;
37874 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
37875 keep_next_level (true);
37876 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37877 SET_EXPR_LOCATION (stmt
, loc
);
37879 return add_stmt (stmt
);
37883 # pragma omp target data target-data-clause[optseq] new-line
37884 structured-block */
37886 #define OMP_TARGET_DATA_CLAUSE_MASK \
37887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37893 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37896 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
37897 "#pragma omp target data", pragma_tok
);
37899 for (tree
*pc
= &clauses
; *pc
;)
37901 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37902 switch (OMP_CLAUSE_MAP_KIND (*pc
))
37905 case GOMP_MAP_ALWAYS_TO
:
37906 case GOMP_MAP_FROM
:
37907 case GOMP_MAP_ALWAYS_FROM
:
37908 case GOMP_MAP_TOFROM
:
37909 case GOMP_MAP_ALWAYS_TOFROM
:
37910 case GOMP_MAP_ALLOC
:
37913 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37914 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
37915 case GOMP_MAP_ALWAYS_POINTER
:
37919 error_at (OMP_CLAUSE_LOCATION (*pc
),
37920 "%<#pragma omp target data%> with map-type other "
37921 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37922 "on %<map%> clause");
37923 *pc
= OMP_CLAUSE_CHAIN (*pc
);
37926 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
37928 pc
= &OMP_CLAUSE_CHAIN (*pc
);
37934 error_at (pragma_tok
->location
,
37935 "%<#pragma omp target data%> must contain at least "
37936 "one %<map%> or %<use_device_ptr%> clause");
37940 tree stmt
= make_node (OMP_TARGET_DATA
);
37941 TREE_TYPE (stmt
) = void_type_node
;
37942 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
37944 keep_next_level (true);
37945 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37947 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37948 return add_stmt (stmt
);
37952 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37953 structured-block */
37955 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37963 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37964 enum pragma_context context
)
37966 bool data_seen
= false;
37967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37969 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37970 const char *p
= IDENTIFIER_POINTER (id
);
37972 if (strcmp (p
, "data") == 0)
37974 cp_lexer_consume_token (parser
->lexer
);
37980 cp_parser_error (parser
, "expected %<data%>");
37981 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37985 if (context
== pragma_stmt
)
37987 error_at (pragma_tok
->location
,
37988 "%<#pragma %s%> may only be used in compound statements",
37989 "omp target enter data");
37990 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37995 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
37996 "#pragma omp target enter data", pragma_tok
);
37998 for (tree
*pc
= &clauses
; *pc
;)
38000 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38001 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38004 case GOMP_MAP_ALWAYS_TO
:
38005 case GOMP_MAP_ALLOC
:
38008 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38009 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38010 case GOMP_MAP_ALWAYS_POINTER
:
38014 error_at (OMP_CLAUSE_LOCATION (*pc
),
38015 "%<#pragma omp target enter data%> with map-type other "
38016 "than %<to%> or %<alloc%> on %<map%> clause");
38017 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38020 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38026 error_at (pragma_tok
->location
,
38027 "%<#pragma omp target enter data%> must contain at least "
38028 "one %<map%> clause");
38032 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38033 TREE_TYPE (stmt
) = void_type_node
;
38034 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38035 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38036 return add_stmt (stmt
);
38040 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38041 structured-block */
38043 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38051 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38052 enum pragma_context context
)
38054 bool data_seen
= false;
38055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38057 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38058 const char *p
= IDENTIFIER_POINTER (id
);
38060 if (strcmp (p
, "data") == 0)
38062 cp_lexer_consume_token (parser
->lexer
);
38068 cp_parser_error (parser
, "expected %<data%>");
38069 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38073 if (context
== pragma_stmt
)
38075 error_at (pragma_tok
->location
,
38076 "%<#pragma %s%> may only be used in compound statements",
38077 "omp target exit data");
38078 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38083 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38084 "#pragma omp target exit data", pragma_tok
);
38086 for (tree
*pc
= &clauses
; *pc
;)
38088 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38089 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38091 case GOMP_MAP_FROM
:
38092 case GOMP_MAP_ALWAYS_FROM
:
38093 case GOMP_MAP_RELEASE
:
38094 case GOMP_MAP_DELETE
:
38097 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38098 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38099 case GOMP_MAP_ALWAYS_POINTER
:
38103 error_at (OMP_CLAUSE_LOCATION (*pc
),
38104 "%<#pragma omp target exit data%> with map-type other "
38105 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38107 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38110 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38116 error_at (pragma_tok
->location
,
38117 "%<#pragma omp target exit data%> must contain at least "
38118 "one %<map%> clause");
38122 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38123 TREE_TYPE (stmt
) = void_type_node
;
38124 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38125 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38126 return add_stmt (stmt
);
38130 # pragma omp target update target-update-clause[optseq] new-line */
38132 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38141 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38142 enum pragma_context context
)
38144 if (context
== pragma_stmt
)
38146 error_at (pragma_tok
->location
,
38147 "%<#pragma %s%> may only be used in compound statements",
38148 "omp target update");
38149 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38154 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38155 "#pragma omp target update", pragma_tok
);
38156 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38157 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38159 error_at (pragma_tok
->location
,
38160 "%<#pragma omp target update%> must contain at least one "
38161 "%<from%> or %<to%> clauses");
38165 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38166 TREE_TYPE (stmt
) = void_type_node
;
38167 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38168 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38174 # pragma omp target target-clause[optseq] new-line
38175 structured-block */
38177 #define OMP_TARGET_CLAUSE_MASK \
38178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38189 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38190 enum pragma_context context
, bool *if_p
)
38192 tree
*pc
= NULL
, stmt
;
38196 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38200 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38201 const char *p
= IDENTIFIER_POINTER (id
);
38202 enum tree_code ccode
= ERROR_MARK
;
38204 if (strcmp (p
, "teams") == 0)
38206 else if (strcmp (p
, "parallel") == 0)
38207 ccode
= OMP_PARALLEL
;
38208 else if (strcmp (p
, "simd") == 0)
38210 if (ccode
!= ERROR_MARK
)
38212 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38213 char p_name
[sizeof ("#pragma omp target teams distribute "
38214 "parallel for simd")];
38216 cp_lexer_consume_token (parser
->lexer
);
38217 strcpy (p_name
, "#pragma omp target");
38218 if (!flag_openmp
) /* flag_openmp_simd */
38224 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38225 OMP_TARGET_CLAUSE_MASK
,
38229 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38230 OMP_TARGET_CLAUSE_MASK
,
38234 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38235 OMP_TARGET_CLAUSE_MASK
,
38239 gcc_unreachable ();
38241 return stmt
!= NULL_TREE
;
38243 keep_next_level (true);
38244 tree sb
= begin_omp_structured_block (), ret
;
38245 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38249 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38250 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38254 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38255 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38259 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38260 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38264 gcc_unreachable ();
38266 cp_parser_end_omp_structured_block (parser
, save
);
38267 tree body
= finish_omp_structured_block (sb
);
38268 if (ret
== NULL_TREE
)
38270 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38272 /* For combined target teams, ensure the num_teams and
38273 thread_limit clause expressions are evaluated on the host,
38274 before entering the target construct. */
38276 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38277 c
; c
= OMP_CLAUSE_CHAIN (c
))
38278 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38279 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38280 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38282 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38283 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38284 if (expr
== error_mark_node
)
38286 tree tmp
= TARGET_EXPR_SLOT (expr
);
38288 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38289 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38290 OMP_CLAUSE_FIRSTPRIVATE
);
38291 OMP_CLAUSE_DECL (tc
) = tmp
;
38292 OMP_CLAUSE_CHAIN (tc
)
38293 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38294 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38297 tree stmt
= make_node (OMP_TARGET
);
38298 TREE_TYPE (stmt
) = void_type_node
;
38299 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38300 OMP_TARGET_BODY (stmt
) = body
;
38301 OMP_TARGET_COMBINED (stmt
) = 1;
38302 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38304 pc
= &OMP_TARGET_CLAUSES (stmt
);
38305 goto check_clauses
;
38307 else if (!flag_openmp
) /* flag_openmp_simd */
38309 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38312 else if (strcmp (p
, "data") == 0)
38314 cp_lexer_consume_token (parser
->lexer
);
38315 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38318 else if (strcmp (p
, "enter") == 0)
38320 cp_lexer_consume_token (parser
->lexer
);
38321 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38324 else if (strcmp (p
, "exit") == 0)
38326 cp_lexer_consume_token (parser
->lexer
);
38327 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38330 else if (strcmp (p
, "update") == 0)
38332 cp_lexer_consume_token (parser
->lexer
);
38333 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38336 if (!flag_openmp
) /* flag_openmp_simd */
38338 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38342 stmt
= make_node (OMP_TARGET
);
38343 TREE_TYPE (stmt
) = void_type_node
;
38345 OMP_TARGET_CLAUSES (stmt
)
38346 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38347 "#pragma omp target", pragma_tok
);
38348 pc
= &OMP_TARGET_CLAUSES (stmt
);
38349 keep_next_level (true);
38350 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38352 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38358 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38359 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38362 case GOMP_MAP_ALWAYS_TO
:
38363 case GOMP_MAP_FROM
:
38364 case GOMP_MAP_ALWAYS_FROM
:
38365 case GOMP_MAP_TOFROM
:
38366 case GOMP_MAP_ALWAYS_TOFROM
:
38367 case GOMP_MAP_ALLOC
:
38368 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38369 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38370 case GOMP_MAP_ALWAYS_POINTER
:
38373 error_at (OMP_CLAUSE_LOCATION (*pc
),
38374 "%<#pragma omp target%> with map-type other "
38375 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38376 "on %<map%> clause");
38377 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38380 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38386 # pragma acc cache (variable-list) new-line
38390 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38392 tree stmt
, clauses
;
38394 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38395 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38397 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38399 stmt
= make_node (OACC_CACHE
);
38400 TREE_TYPE (stmt
) = void_type_node
;
38401 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38402 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38409 # pragma acc data oacc-data-clause[optseq] new-line
38410 structured-block */
38412 #define OACC_DATA_CLAUSE_MASK \
38413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38422 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38424 tree stmt
, clauses
, block
;
38427 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38428 "#pragma acc data", pragma_tok
);
38430 block
= begin_omp_parallel ();
38431 save
= cp_parser_begin_omp_structured_block (parser
);
38432 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38433 cp_parser_end_omp_structured_block (parser
, save
);
38434 stmt
= finish_oacc_data (clauses
, block
);
38439 # pragma acc host_data <clauses> new-line
38440 structured-block */
38442 #define OACC_HOST_DATA_CLAUSE_MASK \
38443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38446 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38448 tree stmt
, clauses
, block
;
38451 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38452 "#pragma acc host_data", pragma_tok
);
38454 block
= begin_omp_parallel ();
38455 save
= cp_parser_begin_omp_structured_block (parser
);
38456 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38457 cp_parser_end_omp_structured_block (parser
, save
);
38458 stmt
= finish_oacc_host_data (clauses
, block
);
38463 # pragma acc declare oacc-data-clause[optseq] new-line
38466 #define OACC_DECLARE_CLAUSE_MASK \
38467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38477 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38479 tree clauses
, stmt
;
38480 bool error
= false;
38482 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38483 "#pragma acc declare", pragma_tok
, true);
38486 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38488 error_at (pragma_tok
->location
,
38489 "no valid clauses specified in %<#pragma acc declare%>");
38493 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38495 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38496 tree decl
= OMP_CLAUSE_DECL (t
);
38497 if (!DECL_P (decl
))
38499 error_at (loc
, "array section in %<#pragma acc declare%>");
38503 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38504 switch (OMP_CLAUSE_MAP_KIND (t
))
38506 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38507 case GOMP_MAP_ALLOC
:
38509 case GOMP_MAP_FORCE_DEVICEPTR
:
38510 case GOMP_MAP_DEVICE_RESIDENT
:
38513 case GOMP_MAP_LINK
:
38514 if (!global_bindings_p ()
38515 && (TREE_STATIC (decl
)
38516 || !DECL_EXTERNAL (decl
)))
38519 "%qD must be a global variable in "
38520 "%<#pragma acc declare link%>",
38528 if (global_bindings_p ())
38530 error_at (loc
, "invalid OpenACC clause at file scope");
38534 if (DECL_EXTERNAL (decl
))
38537 "invalid use of %<extern%> variable %qD "
38538 "in %<#pragma acc declare%>", decl
);
38542 else if (TREE_PUBLIC (decl
))
38545 "invalid use of %<global%> variable %qD "
38546 "in %<#pragma acc declare%>", decl
);
38553 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38554 || lookup_attribute ("omp declare target link",
38555 DECL_ATTRIBUTES (decl
)))
38557 error_at (loc
, "variable %qD used more than once with "
38558 "%<#pragma acc declare%>", decl
);
38567 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38568 id
= get_identifier ("omp declare target link");
38570 id
= get_identifier ("omp declare target");
38572 DECL_ATTRIBUTES (decl
)
38573 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38574 if (global_bindings_p ())
38576 symtab_node
*node
= symtab_node::get (decl
);
38579 node
->offloadable
= 1;
38580 if (ENABLE_OFFLOADING
)
38582 g
->have_offload
= true;
38583 if (is_a
<varpool_node
*> (node
))
38584 vec_safe_push (offload_vars
, decl
);
38591 if (error
|| global_bindings_p ())
38594 stmt
= make_node (OACC_DECLARE
);
38595 TREE_TYPE (stmt
) = void_type_node
;
38596 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38597 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38605 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38609 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38611 LOC is the location of the #pragma token.
38614 #define OACC_ENTER_DATA_CLAUSE_MASK \
38615 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38621 #define OACC_EXIT_DATA_CLAUSE_MASK \
38622 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38630 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38633 location_t loc
= pragma_tok
->location
;
38634 tree stmt
, clauses
;
38635 const char *p
= "";
38637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38638 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38640 if (strcmp (p
, "data") != 0)
38642 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38643 enter
? "enter" : "exit");
38644 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38648 cp_lexer_consume_token (parser
->lexer
);
38651 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38652 "#pragma acc enter data", pragma_tok
);
38654 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38655 "#pragma acc exit data", pragma_tok
);
38657 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38659 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38660 enter
? "enter" : "exit");
38664 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38665 TREE_TYPE (stmt
) = void_type_node
;
38666 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38667 SET_EXPR_LOCATION (stmt
, loc
);
38673 # pragma acc loop oacc-loop-clause[optseq] new-line
38674 structured-block */
38676 #define OACC_LOOP_CLAUSE_MASK \
38677 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38689 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38690 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38692 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38694 strcat (p_name
, " loop");
38695 mask
|= OACC_LOOP_CLAUSE_MASK
;
38697 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38701 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38703 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38705 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38708 tree block
= begin_omp_structured_block ();
38709 int save
= cp_parser_begin_omp_structured_block (parser
);
38710 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38711 cp_parser_end_omp_structured_block (parser
, save
);
38712 add_stmt (finish_omp_structured_block (block
));
38718 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38723 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38727 #define OACC_KERNELS_CLAUSE_MASK \
38728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38742 #define OACC_PARALLEL_CLAUSE_MASK \
38743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38761 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38762 char *p_name
, bool *if_p
)
38764 omp_clause_mask mask
;
38765 enum tree_code code
;
38766 switch (cp_parser_pragma_kind (pragma_tok
))
38768 case PRAGMA_OACC_KERNELS
:
38769 strcat (p_name
, " kernels");
38770 mask
= OACC_KERNELS_CLAUSE_MASK
;
38771 code
= OACC_KERNELS
;
38773 case PRAGMA_OACC_PARALLEL
:
38774 strcat (p_name
, " parallel");
38775 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38776 code
= OACC_PARALLEL
;
38779 gcc_unreachable ();
38782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38785 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38786 if (strcmp (p
, "loop") == 0)
38788 cp_lexer_consume_token (parser
->lexer
);
38789 tree block
= begin_omp_parallel ();
38791 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
38793 protected_set_expr_location (stmt
, pragma_tok
->location
);
38794 return finish_omp_construct (code
, block
, clauses
);
38798 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38800 tree block
= begin_omp_parallel ();
38801 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38802 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38803 cp_parser_end_omp_structured_block (parser
, save
);
38804 return finish_omp_construct (code
, block
, clauses
);
38808 # pragma acc update oacc-update-clause[optseq] new-line
38811 #define OACC_UPDATE_CLAUSE_MASK \
38812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38820 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38822 tree stmt
, clauses
;
38824 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38825 "#pragma acc update", pragma_tok
);
38827 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38829 error_at (pragma_tok
->location
,
38830 "%<#pragma acc update%> must contain at least one "
38831 "%<device%> or %<host%> or %<self%> clause");
38835 stmt
= make_node (OACC_UPDATE
);
38836 TREE_TYPE (stmt
) = void_type_node
;
38837 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38838 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38844 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38846 LOC is the location of the #pragma token.
38849 #define OACC_WAIT_CLAUSE_MASK \
38850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38853 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38855 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38856 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38858 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38859 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
38861 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
38862 "#pragma acc wait", pragma_tok
);
38864 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
38865 stmt
= finish_expr_stmt (stmt
);
38871 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38873 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38882 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38883 enum pragma_context context
)
38885 bool first_p
= parser
->omp_declare_simd
== NULL
;
38886 cp_omp_declare_simd_data data
;
38889 data
.error_seen
= false;
38890 data
.fndecl_seen
= false;
38891 data
.tokens
= vNULL
;
38892 data
.clauses
= NULL_TREE
;
38893 /* It is safe to take the address of a local variable; it will only be
38894 used while this scope is live. */
38895 parser
->omp_declare_simd
= &data
;
38898 /* Store away all pragma tokens. */
38899 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38900 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38901 cp_lexer_consume_token (parser
->lexer
);
38902 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38903 parser
->omp_declare_simd
->error_seen
= true;
38904 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38905 struct cp_token_cache
*cp
38906 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38907 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
38911 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38912 cp_parser_pragma (parser
, context
, NULL
);
38915 case pragma_external
:
38916 cp_parser_declaration (parser
);
38918 case pragma_member
:
38919 cp_parser_member_declaration (parser
);
38921 case pragma_objc_icode
:
38922 cp_parser_block_declaration (parser
, /*statement_p=*/false);
38925 cp_parser_declaration_statement (parser
);
38928 if (parser
->omp_declare_simd
38929 && !parser
->omp_declare_simd
->error_seen
38930 && !parser
->omp_declare_simd
->fndecl_seen
)
38931 error_at (pragma_tok
->location
,
38932 "%<#pragma omp declare simd%> not immediately followed by "
38933 "function declaration or definition");
38934 data
.tokens
.release ();
38935 parser
->omp_declare_simd
= NULL
;
38939 /* Finalize #pragma omp declare simd clauses after direct declarator has
38940 been parsed, and put that into "omp declare simd" attribute. */
38943 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
38945 struct cp_token_cache
*ce
;
38946 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
38949 if (!data
->error_seen
&& data
->fndecl_seen
)
38951 error ("%<#pragma omp declare simd%> not immediately followed by "
38952 "a single function declaration or definition");
38953 data
->error_seen
= true;
38955 if (data
->error_seen
)
38958 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
38962 cp_parser_push_lexer_for_tokens (parser
, ce
);
38963 parser
->lexer
->in_pragma
= true;
38964 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38965 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38966 cp_lexer_consume_token (parser
->lexer
);
38967 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
38968 "#pragma omp declare simd", pragma_tok
);
38969 cp_parser_pop_lexer (parser
);
38971 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
38972 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
38973 TREE_CHAIN (c
) = attrs
;
38974 if (processing_template_decl
)
38975 ATTR_IS_DEPENDENT (c
) = 1;
38979 data
->fndecl_seen
= true;
38985 # pragma omp declare target new-line
38986 declarations and definitions
38987 # pragma omp end declare target new-line
38990 # pragma omp declare target ( extended-list ) new-line
38992 # pragma omp declare target declare-target-clauses[seq] new-line */
38994 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38999 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39001 tree clauses
= NULL_TREE
;
39002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39004 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39005 "#pragma omp declare target", pragma_tok
);
39006 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39008 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39010 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39011 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39015 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39016 scope_chain
->omp_declare_target_attribute
++;
39019 if (scope_chain
->omp_declare_target_attribute
)
39020 error_at (pragma_tok
->location
,
39021 "%<#pragma omp declare target%> with clauses in between "
39022 "%<#pragma omp declare target%> without clauses and "
39023 "%<#pragma omp end declare target%>");
39024 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39026 tree t
= OMP_CLAUSE_DECL (c
), id
;
39027 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39028 tree at2
= lookup_attribute ("omp declare target link",
39029 DECL_ATTRIBUTES (t
));
39030 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39032 id
= get_identifier ("omp declare target link");
39033 std::swap (at1
, at2
);
39036 id
= get_identifier ("omp declare target");
39039 error_at (OMP_CLAUSE_LOCATION (c
),
39040 "%qD specified both in declare target %<link%> and %<to%>"
39046 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39047 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39050 symtab_node
*node
= symtab_node::get (t
);
39053 node
->offloadable
= 1;
39054 if (ENABLE_OFFLOADING
)
39056 g
->have_offload
= true;
39057 if (is_a
<varpool_node
*> (node
))
39058 vec_safe_push (offload_vars
, t
);
39066 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39068 const char *p
= "";
39069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39071 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39072 p
= IDENTIFIER_POINTER (id
);
39074 if (strcmp (p
, "declare") == 0)
39076 cp_lexer_consume_token (parser
->lexer
);
39078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39080 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39081 p
= IDENTIFIER_POINTER (id
);
39083 if (strcmp (p
, "target") == 0)
39084 cp_lexer_consume_token (parser
->lexer
);
39087 cp_parser_error (parser
, "expected %<target%>");
39088 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39094 cp_parser_error (parser
, "expected %<declare%>");
39095 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39098 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39099 if (!scope_chain
->omp_declare_target_attribute
)
39100 error_at (pragma_tok
->location
,
39101 "%<#pragma omp end declare target%> without corresponding "
39102 "%<#pragma omp declare target%>");
39104 scope_chain
->omp_declare_target_attribute
--;
39107 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39108 expression and optional initializer clause of
39109 #pragma omp declare reduction. We store the expression(s) as
39110 either 3, 6 or 7 special statements inside of the artificial function's
39111 body. The first two statements are DECL_EXPRs for the artificial
39112 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39113 expression that uses those variables.
39114 If there was any INITIALIZER clause, this is followed by further statements,
39115 the fourth and fifth statements are DECL_EXPRs for the artificial
39116 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39117 constructor variant (first token after open paren is not omp_priv),
39118 then the sixth statement is a statement with the function call expression
39119 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39120 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39121 to initialize the OMP_PRIV artificial variable and there is seventh
39122 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39125 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39127 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39128 gcc_assert (TYPE_REF_P (type
));
39129 type
= TREE_TYPE (type
);
39130 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39131 DECL_ARTIFICIAL (omp_out
) = 1;
39132 pushdecl (omp_out
);
39133 add_decl_expr (omp_out
);
39134 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39135 DECL_ARTIFICIAL (omp_in
) = 1;
39137 add_decl_expr (omp_in
);
39139 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39141 keep_next_level (true);
39142 tree block
= begin_omp_structured_block ();
39143 combiner
= cp_parser_expression (parser
);
39144 finish_expr_stmt (combiner
);
39145 block
= finish_omp_structured_block (block
);
39148 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39151 const char *p
= "";
39152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39154 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39155 p
= IDENTIFIER_POINTER (id
);
39158 if (strcmp (p
, "initializer") == 0)
39160 cp_lexer_consume_token (parser
->lexer
);
39161 matching_parens parens
;
39162 if (!parens
.require_open (parser
))
39166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39168 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39169 p
= IDENTIFIER_POINTER (id
);
39172 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39173 DECL_ARTIFICIAL (omp_priv
) = 1;
39174 pushdecl (omp_priv
);
39175 add_decl_expr (omp_priv
);
39176 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39177 DECL_ARTIFICIAL (omp_orig
) = 1;
39178 pushdecl (omp_orig
);
39179 add_decl_expr (omp_orig
);
39181 keep_next_level (true);
39182 block
= begin_omp_structured_block ();
39185 if (strcmp (p
, "omp_priv") == 0)
39187 bool is_direct_init
, is_non_constant_init
;
39189 cp_lexer_consume_token (parser
->lexer
);
39190 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39192 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39193 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39195 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39196 == CPP_CLOSE_PAREN
))
39198 finish_omp_structured_block (block
);
39199 error ("invalid initializer clause");
39202 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39203 &is_non_constant_init
);
39204 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39205 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39209 cp_parser_parse_tentatively (parser
);
39210 /* Don't create location wrapper nodes here. */
39211 auto_suppress_location_wrappers sentinel
;
39212 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39213 /*check_dependency_p=*/true,
39214 /*template_p=*/NULL
,
39215 /*declarator_p=*/false,
39216 /*optional_p=*/false);
39217 vec
<tree
, va_gc
> *args
;
39218 if (fn_name
== error_mark_node
39219 || cp_parser_error_occurred (parser
)
39220 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39221 || ((args
= cp_parser_parenthesized_expression_list
39222 (parser
, non_attr
, /*cast_p=*/false,
39223 /*allow_expansion_p=*/true,
39224 /*non_constant_p=*/NULL
)),
39225 cp_parser_error_occurred (parser
)))
39227 finish_omp_structured_block (block
);
39228 cp_parser_abort_tentative_parse (parser
);
39229 cp_parser_error (parser
, "expected id-expression (arguments)");
39234 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39235 if (arg
== omp_priv
39236 || (TREE_CODE (arg
) == ADDR_EXPR
39237 && TREE_OPERAND (arg
, 0) == omp_priv
))
39239 cp_parser_abort_tentative_parse (parser
);
39240 if (arg
== NULL_TREE
)
39241 error ("one of the initializer call arguments should be %<omp_priv%>"
39242 " or %<&omp_priv%>");
39243 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39245 finish_expr_stmt (initializer
);
39248 block
= finish_omp_structured_block (block
);
39249 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39253 add_decl_expr (omp_orig
);
39255 if (!parens
.require_close (parser
))
39259 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39260 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39267 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39268 initializer-clause[opt] new-line
39270 initializer-clause:
39271 initializer (omp_priv initializer)
39272 initializer (function-name (argument-list)) */
39275 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39276 enum pragma_context
)
39278 auto_vec
<tree
> types
;
39279 enum tree_code reduc_code
= ERROR_MARK
;
39280 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39282 cp_token
*first_token
;
39283 cp_token_cache
*cp
;
39287 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39288 p
= obstack_alloc (&declarator_obstack
, 0);
39290 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39293 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39296 reduc_code
= PLUS_EXPR
;
39299 reduc_code
= MULT_EXPR
;
39302 reduc_code
= MINUS_EXPR
;
39305 reduc_code
= BIT_AND_EXPR
;
39308 reduc_code
= BIT_XOR_EXPR
;
39311 reduc_code
= BIT_IOR_EXPR
;
39314 reduc_code
= TRUTH_ANDIF_EXPR
;
39317 reduc_code
= TRUTH_ORIF_EXPR
;
39320 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39323 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39324 "%<|%>, %<&&%>, %<||%> or identifier");
39328 if (reduc_code
!= ERROR_MARK
)
39329 cp_lexer_consume_token (parser
->lexer
);
39331 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39332 if (reduc_id
== error_mark_node
)
39335 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39338 /* Types may not be defined in declare reduction type list. */
39339 const char *saved_message
;
39340 saved_message
= parser
->type_definition_forbidden_message
;
39341 parser
->type_definition_forbidden_message
39342 = G_("types may not be defined in declare reduction type list");
39343 bool saved_colon_corrects_to_scope_p
;
39344 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39345 parser
->colon_corrects_to_scope_p
= false;
39346 bool saved_colon_doesnt_start_class_def_p
;
39347 saved_colon_doesnt_start_class_def_p
39348 = parser
->colon_doesnt_start_class_def_p
;
39349 parser
->colon_doesnt_start_class_def_p
= true;
39353 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39354 type
= cp_parser_type_id (parser
);
39355 if (type
== error_mark_node
)
39357 else if (ARITHMETIC_TYPE_P (type
)
39358 && (orig_reduc_id
== NULL_TREE
39359 || (TREE_CODE (type
) != COMPLEX_TYPE
39360 && (id_equal (orig_reduc_id
, "min")
39361 || id_equal (orig_reduc_id
, "max")))))
39362 error_at (loc
, "predeclared arithmetic type %qT in "
39363 "%<#pragma omp declare reduction%>", type
);
39364 else if (TREE_CODE (type
) == FUNCTION_TYPE
39365 || TREE_CODE (type
) == METHOD_TYPE
39366 || TREE_CODE (type
) == ARRAY_TYPE
)
39367 error_at (loc
, "function or array type %qT in "
39368 "%<#pragma omp declare reduction%>", type
);
39369 else if (TYPE_REF_P (type
))
39370 error_at (loc
, "reference type %qT in "
39371 "%<#pragma omp declare reduction%>", type
);
39372 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39373 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39374 "%<#pragma omp declare reduction%>", type
);
39376 types
.safe_push (type
);
39378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39379 cp_lexer_consume_token (parser
->lexer
);
39384 /* Restore the saved message. */
39385 parser
->type_definition_forbidden_message
= saved_message
;
39386 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39387 parser
->colon_doesnt_start_class_def_p
39388 = saved_colon_doesnt_start_class_def_p
;
39390 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39391 || types
.is_empty ())
39394 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39398 first_token
= cp_lexer_peek_token (parser
->lexer
);
39401 FOR_EACH_VEC_ELT (types
, i
, type
)
39404 = build_function_type_list (void_type_node
,
39405 cp_build_reference_type (type
, false),
39407 tree this_reduc_id
= reduc_id
;
39408 if (!dependent_type_p (type
))
39409 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39410 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39411 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39412 DECL_ARTIFICIAL (fndecl
) = 1;
39413 DECL_EXTERNAL (fndecl
) = 1;
39414 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39415 DECL_IGNORED_P (fndecl
) = 1;
39416 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39417 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39418 DECL_ATTRIBUTES (fndecl
)
39419 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39420 DECL_ATTRIBUTES (fndecl
));
39421 if (processing_template_decl
)
39422 fndecl
= push_template_decl (fndecl
);
39423 bool block_scope
= false;
39424 tree block
= NULL_TREE
;
39425 if (current_function_decl
)
39427 block_scope
= true;
39428 DECL_CONTEXT (fndecl
) = global_namespace
;
39429 if (!processing_template_decl
)
39432 else if (current_class_type
)
39436 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39437 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39438 cp_lexer_consume_token (parser
->lexer
);
39439 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39441 cp
= cp_token_cache_new (first_token
,
39442 cp_lexer_peek_nth_token (parser
->lexer
,
39445 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39446 finish_member_declaration (fndecl
);
39447 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39448 DECL_PENDING_INLINE_P (fndecl
) = 1;
39449 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39454 DECL_CONTEXT (fndecl
) = current_namespace
;
39458 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39460 block
= begin_omp_structured_block ();
39463 cp_parser_push_lexer_for_tokens (parser
, cp
);
39464 parser
->lexer
->in_pragma
= true;
39466 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39469 finish_function (/*inline_p=*/false);
39471 DECL_CONTEXT (fndecl
) = current_function_decl
;
39473 cp_parser_pop_lexer (parser
);
39477 cp_parser_pop_lexer (parser
);
39479 finish_function (/*inline_p=*/false);
39482 DECL_CONTEXT (fndecl
) = current_function_decl
;
39483 block
= finish_omp_structured_block (block
);
39484 if (TREE_CODE (block
) == BIND_EXPR
)
39485 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39486 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39487 DECL_SAVED_TREE (fndecl
) = block
;
39488 if (processing_template_decl
)
39489 add_decl_expr (fndecl
);
39491 cp_check_omp_declare_reduction (fndecl
);
39492 if (cp
== NULL
&& types
.length () > 1)
39493 cp
= cp_token_cache_new (first_token
,
39494 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39495 if (errs
!= errorcount
)
39499 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39502 /* Free any declarators allocated. */
39503 obstack_free (&declarator_obstack
, p
);
39507 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39508 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39509 initializer-clause[opt] new-line
39510 #pragma omp declare target new-line */
39513 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39514 enum pragma_context context
)
39516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39518 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39519 const char *p
= IDENTIFIER_POINTER (id
);
39521 if (strcmp (p
, "simd") == 0)
39523 cp_lexer_consume_token (parser
->lexer
);
39524 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39528 cp_ensure_no_omp_declare_simd (parser
);
39529 if (strcmp (p
, "reduction") == 0)
39531 cp_lexer_consume_token (parser
->lexer
);
39532 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39536 if (!flag_openmp
) /* flag_openmp_simd */
39538 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39541 if (strcmp (p
, "target") == 0)
39543 cp_lexer_consume_token (parser
->lexer
);
39544 cp_parser_omp_declare_target (parser
, pragma_tok
);
39548 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39550 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39555 #pragma omp requires clauses[optseq] new-line */
39558 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39561 enum omp_requires new_req
= (enum omp_requires
) 0;
39563 location_t loc
= pragma_tok
->location
;
39564 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39566 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39567 cp_lexer_consume_token (parser
->lexer
);
39571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39573 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39574 const char *p
= IDENTIFIER_POINTER (id
);
39575 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39576 enum omp_requires this_req
= (enum omp_requires
) 0;
39578 if (!strcmp (p
, "unified_address"))
39579 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39580 else if (!strcmp (p
, "unified_shared_memory"))
39581 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39582 else if (!strcmp (p
, "dynamic_allocators"))
39583 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39584 else if (!strcmp (p
, "reverse_offload"))
39585 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39586 else if (!strcmp (p
, "atomic_default_mem_order"))
39588 cp_lexer_consume_token (parser
->lexer
);
39590 matching_parens parens
;
39591 if (parens
.require_open (parser
))
39593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39595 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39596 p
= IDENTIFIER_POINTER (id
);
39598 if (!strcmp (p
, "seq_cst"))
39600 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39601 else if (!strcmp (p
, "relaxed"))
39603 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39604 else if (!strcmp (p
, "acq_rel"))
39606 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39610 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39611 "expected %<seq_cst%>, %<relaxed%> or "
39613 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39615 cp_lexer_consume_token (parser
->lexer
);
39618 cp_lexer_consume_token (parser
->lexer
);
39620 if (!parens
.require_close (parser
))
39621 cp_parser_skip_to_closing_parenthesis (parser
,
39622 /*recovering=*/true,
39623 /*or_comma=*/false,
39629 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39637 error_at (cloc
, "expected %<unified_address%>, "
39638 "%<unified_shared_memory%>, "
39639 "%<dynamic_allocators%>, "
39640 "%<reverse_offload%> "
39641 "or %<atomic_default_mem_order%> clause");
39642 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39646 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39647 "supported yet", p
);
39649 cp_lexer_consume_token (parser
->lexer
);
39652 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39654 if ((this_req
& new_req
) != 0)
39655 error_at (cloc
, "too many %qs clauses", p
);
39656 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39657 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39658 error_at (cloc
, "%qs clause used lexically after first "
39659 "target construct or offloading API", p
);
39661 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39663 error_at (cloc
, "too many %qs clauses",
39664 "atomic_default_mem_order");
39665 this_req
= (enum omp_requires
) 0;
39667 else if ((omp_requires_mask
39668 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39670 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39671 " clause in a single compilation unit");
39673 = (enum omp_requires
)
39675 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39677 else if ((omp_requires_mask
39678 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39679 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39680 "lexically after first %<atomic%> construct "
39681 "without memory order clause");
39682 new_req
= (enum omp_requires
) (new_req
| this_req
);
39684 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39690 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39693 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39699 #pragma omp taskloop taskloop-clause[optseq] new-line
39702 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39705 #define OMP_TASKLOOP_CLAUSE_MASK \
39706 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39724 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39725 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39728 tree clauses
, sb
, ret
;
39730 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39732 strcat (p_name
, " taskloop");
39733 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39734 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39736 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39737 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39741 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39742 const char *p
= IDENTIFIER_POINTER (id
);
39744 if (strcmp (p
, "simd") == 0)
39746 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39747 if (cclauses
== NULL
)
39748 cclauses
= cclauses_buf
;
39750 cp_lexer_consume_token (parser
->lexer
);
39751 if (!flag_openmp
) /* flag_openmp_simd */
39752 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39754 sb
= begin_omp_structured_block ();
39755 save
= cp_parser_begin_omp_structured_block (parser
);
39756 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39758 cp_parser_end_omp_structured_block (parser
, save
);
39759 tree body
= finish_omp_structured_block (sb
);
39762 ret
= make_node (OMP_TASKLOOP
);
39763 TREE_TYPE (ret
) = void_type_node
;
39764 OMP_FOR_BODY (ret
) = body
;
39765 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39766 SET_EXPR_LOCATION (ret
, loc
);
39771 if (!flag_openmp
) /* flag_openmp_simd */
39773 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39777 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39781 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39782 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39785 keep_next_level (true);
39786 sb
= begin_omp_structured_block ();
39787 save
= cp_parser_begin_omp_structured_block (parser
);
39789 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39792 cp_parser_end_omp_structured_block (parser
, save
);
39793 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39800 # pragma acc routine oacc-routine-clause[optseq] new-line
39801 function-definition
39803 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39806 #define OACC_ROUTINE_CLAUSE_MASK \
39807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39813 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39814 component, which must resolve to a declared namespace-scope
39815 function. The clauses are either processed directly (for a named
39816 function), or defered until the immediatley following declaration
39820 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39821 enum pragma_context context
)
39823 gcc_checking_assert (context
== pragma_external
);
39824 /* The checking for "another pragma following this one" in the "no optional
39825 '( name )'" case makes sure that we dont re-enter. */
39826 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39828 cp_oacc_routine_data data
;
39829 data
.error_seen
= false;
39830 data
.fndecl_seen
= false;
39831 data
.tokens
= vNULL
;
39832 data
.clauses
= NULL_TREE
;
39833 data
.loc
= pragma_tok
->location
;
39834 /* It is safe to take the address of a local variable; it will only be
39835 used while this scope is live. */
39836 parser
->oacc_routine
= &data
;
39838 /* Look for optional '( name )'. */
39839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39841 matching_parens parens
;
39842 parens
.consume_open (parser
); /* '(' */
39844 /* We parse the name as an id-expression. If it resolves to
39845 anything other than a non-overloaded function at namespace
39846 scope, it's an error. */
39847 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39848 tree name
= cp_parser_id_expression (parser
,
39849 /*template_keyword_p=*/false,
39850 /*check_dependency_p=*/false,
39851 /*template_p=*/NULL
,
39852 /*declarator_p=*/false,
39853 /*optional_p=*/false);
39854 tree decl
= (identifier_p (name
)
39855 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39857 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39858 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39860 if (decl
== error_mark_node
39861 || !parens
.require_close (parser
))
39863 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39864 parser
->oacc_routine
= NULL
;
39869 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39870 "#pragma acc routine",
39871 cp_lexer_peek_token (parser
->lexer
));
39873 if (decl
&& is_overloaded_fn (decl
)
39874 && (TREE_CODE (decl
) != FUNCTION_DECL
39875 || DECL_FUNCTION_TEMPLATE_P (decl
)))
39877 error_at (name_loc
,
39878 "%<#pragma acc routine%> names a set of overloads");
39879 parser
->oacc_routine
= NULL
;
39883 /* Perhaps we should use the same rule as declarations in different
39885 if (!DECL_NAMESPACE_SCOPE_P (decl
))
39887 error_at (name_loc
,
39888 "%qD does not refer to a namespace scope function", decl
);
39889 parser
->oacc_routine
= NULL
;
39893 if (TREE_CODE (decl
) != FUNCTION_DECL
)
39895 error_at (name_loc
, "%qD does not refer to a function", decl
);
39896 parser
->oacc_routine
= NULL
;
39900 cp_finalize_oacc_routine (parser
, decl
, false);
39901 parser
->oacc_routine
= NULL
;
39903 else /* No optional '( name )'. */
39905 /* Store away all pragma tokens. */
39906 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39907 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39908 cp_lexer_consume_token (parser
->lexer
);
39909 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39910 parser
->oacc_routine
->error_seen
= true;
39911 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39912 struct cp_token_cache
*cp
39913 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39914 parser
->oacc_routine
->tokens
.safe_push (cp
);
39916 /* Emit a helpful diagnostic if there's another pragma following this
39918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39920 cp_ensure_no_oacc_routine (parser
);
39921 data
.tokens
.release ();
39922 /* ..., and then just keep going. */
39926 /* We only have to consider the pragma_external case here. */
39927 cp_parser_declaration (parser
);
39928 if (parser
->oacc_routine
39929 && !parser
->oacc_routine
->fndecl_seen
)
39930 cp_ensure_no_oacc_routine (parser
);
39932 parser
->oacc_routine
= NULL
;
39933 data
.tokens
.release ();
39937 /* Finalize #pragma acc routine clauses after direct declarator has
39941 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
39943 struct cp_token_cache
*ce
;
39944 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
39946 if (!data
->error_seen
&& data
->fndecl_seen
)
39948 error_at (data
->loc
,
39949 "%<#pragma acc routine%> not immediately followed by "
39950 "a single function declaration or definition");
39951 data
->error_seen
= true;
39953 if (data
->error_seen
)
39956 gcc_checking_assert (data
->tokens
.length () == 1);
39957 ce
= data
->tokens
[0];
39959 cp_parser_push_lexer_for_tokens (parser
, ce
);
39960 parser
->lexer
->in_pragma
= true;
39961 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39963 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39964 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
39965 parser
->oacc_routine
->clauses
39966 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39967 "#pragma acc routine", pragma_tok
);
39968 cp_parser_pop_lexer (parser
);
39969 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39975 /* Apply any saved OpenACC routine clauses to a just-parsed
39979 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
39981 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
39983 /* Keep going if we're in error reporting mode. */
39984 if (parser
->oacc_routine
->error_seen
39985 || fndecl
== error_mark_node
)
39988 if (parser
->oacc_routine
->fndecl_seen
)
39990 error_at (parser
->oacc_routine
->loc
,
39991 "%<#pragma acc routine%> not immediately followed by"
39992 " a single function declaration or definition");
39993 parser
->oacc_routine
= NULL
;
39996 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
39998 cp_ensure_no_oacc_routine (parser
);
40002 if (oacc_get_fn_attrib (fndecl
))
40004 error_at (parser
->oacc_routine
->loc
,
40005 "%<#pragma acc routine%> already applied to %qD", fndecl
);
40006 parser
->oacc_routine
= NULL
;
40010 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40012 error_at (parser
->oacc_routine
->loc
,
40014 ? G_("%<#pragma acc routine%> must be applied before use")
40015 : G_("%<#pragma acc routine%> must be applied before "
40017 parser
->oacc_routine
= NULL
;
40021 /* Process the routine's dimension clauses. */
40022 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40023 oacc_replace_fn_attrib (fndecl
, dims
);
40025 /* Add an "omp declare target" attribute. */
40026 DECL_ATTRIBUTES (fndecl
)
40027 = tree_cons (get_identifier ("omp declare target"),
40028 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
40030 /* Don't unset parser->oacc_routine here: we may still need it to
40031 diagnose wrong usage. But, remember that we've used this "#pragma acc
40033 parser
->oacc_routine
->fndecl_seen
= true;
40037 /* Main entry point to OpenMP statement pragmas. */
40040 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40043 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40044 omp_clause_mask
mask (0);
40046 switch (cp_parser_pragma_kind (pragma_tok
))
40048 case PRAGMA_OACC_ATOMIC
:
40049 cp_parser_omp_atomic (parser
, pragma_tok
);
40051 case PRAGMA_OACC_CACHE
:
40052 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40054 case PRAGMA_OACC_DATA
:
40055 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40057 case PRAGMA_OACC_ENTER_DATA
:
40058 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40060 case PRAGMA_OACC_EXIT_DATA
:
40061 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40063 case PRAGMA_OACC_HOST_DATA
:
40064 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40066 case PRAGMA_OACC_KERNELS
:
40067 case PRAGMA_OACC_PARALLEL
:
40068 strcpy (p_name
, "#pragma acc");
40069 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40072 case PRAGMA_OACC_LOOP
:
40073 strcpy (p_name
, "#pragma acc");
40074 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40077 case PRAGMA_OACC_UPDATE
:
40078 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40080 case PRAGMA_OACC_WAIT
:
40081 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40083 case PRAGMA_OMP_ATOMIC
:
40084 cp_parser_omp_atomic (parser
, pragma_tok
);
40086 case PRAGMA_OMP_CRITICAL
:
40087 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40089 case PRAGMA_OMP_DISTRIBUTE
:
40090 strcpy (p_name
, "#pragma omp");
40091 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40094 case PRAGMA_OMP_FOR
:
40095 strcpy (p_name
, "#pragma omp");
40096 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40099 case PRAGMA_OMP_MASTER
:
40100 strcpy (p_name
, "#pragma omp");
40101 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40104 case PRAGMA_OMP_PARALLEL
:
40105 strcpy (p_name
, "#pragma omp");
40106 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40109 case PRAGMA_OMP_SECTIONS
:
40110 strcpy (p_name
, "#pragma omp");
40111 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40113 case PRAGMA_OMP_SIMD
:
40114 strcpy (p_name
, "#pragma omp");
40115 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40118 case PRAGMA_OMP_SINGLE
:
40119 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40121 case PRAGMA_OMP_TASK
:
40122 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40124 case PRAGMA_OMP_TASKGROUP
:
40125 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40127 case PRAGMA_OMP_TASKLOOP
:
40128 strcpy (p_name
, "#pragma omp");
40129 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40132 case PRAGMA_OMP_TEAMS
:
40133 strcpy (p_name
, "#pragma omp");
40134 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40138 gcc_unreachable ();
40141 protected_set_expr_location (stmt
, pragma_tok
->location
);
40144 /* Transactional Memory parsing routines. */
40146 /* Parse a transaction attribute.
40152 We use this instead of cp_parser_attributes_opt for transactions to avoid
40153 the pedwarn in C++98 mode. */
40156 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40159 tree attr_name
, attr
= NULL
;
40161 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40162 return cp_parser_attributes_opt (parser
);
40164 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40166 cp_lexer_consume_token (parser
->lexer
);
40167 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40170 token
= cp_lexer_peek_token (parser
->lexer
);
40171 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40173 token
= cp_lexer_consume_token (parser
->lexer
);
40175 attr_name
= (token
->type
== CPP_KEYWORD
40176 /* For keywords, use the canonical spelling,
40177 not the parsed identifier. */
40178 ? ridpointers
[(int) token
->keyword
]
40180 attr
= build_tree_list (attr_name
, NULL_TREE
);
40183 cp_parser_error (parser
, "expected identifier");
40185 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40187 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40191 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40193 transaction-statement:
40194 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40196 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40200 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40202 unsigned char old_in
= parser
->in_transaction
;
40203 unsigned char this_in
= 1, new_in
;
40204 enum rid keyword
= token
->keyword
;
40205 tree stmt
, attrs
, noex
;
40207 cp_lexer_consume_token (parser
->lexer
);
40209 if (keyword
== RID_TRANSACTION_RELAXED
40210 || keyword
== RID_SYNCHRONIZED
)
40211 this_in
|= TM_STMT_ATTR_RELAXED
;
40214 attrs
= cp_parser_txn_attribute_opt (parser
);
40216 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40219 /* Parse a noexcept specification. */
40220 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40221 noex
= boolean_true_node
;
40222 else if (keyword
== RID_ATOMIC_CANCEL
)
40224 /* cancel-and-throw is unimplemented. */
40225 sorry ("atomic_cancel");
40229 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40231 /* Keep track if we're in the lexical scope of an outer transaction. */
40232 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40234 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40236 parser
->in_transaction
= new_in
;
40237 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40238 parser
->in_transaction
= old_in
;
40240 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40245 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40247 transaction-expression:
40248 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40249 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40253 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40255 unsigned char old_in
= parser
->in_transaction
;
40256 unsigned char this_in
= 1;
40260 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40262 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40263 || keyword
== RID_TRANSACTION_RELAXED
);
40267 keyword
== RID_TRANSACTION_RELAXED
40268 ? G_("%<__transaction_relaxed%> without transactional memory "
40270 : G_("%<__transaction_atomic%> without transactional memory "
40271 "support enabled"));
40273 token
= cp_parser_require_keyword (parser
, keyword
,
40274 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40275 : RT_TRANSACTION_RELAXED
));
40276 gcc_assert (token
!= NULL
);
40278 if (keyword
== RID_TRANSACTION_RELAXED
)
40279 this_in
|= TM_STMT_ATTR_RELAXED
;
40281 /* Set this early. This might mean that we allow transaction_cancel in
40282 an expression that we find out later actually has to be a constexpr.
40283 However, we expect that cxx_constant_value will be able to deal with
40284 this; also, if the noexcept has no constexpr, then what we parse next
40285 really is a transaction's body. */
40286 parser
->in_transaction
= this_in
;
40288 /* Parse a noexcept specification. */
40289 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40292 if (!noex
|| !noex_expr
40293 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40295 matching_parens parens
;
40296 parens
.require_open (parser
);
40298 expr
= cp_parser_expression (parser
);
40299 expr
= finish_parenthesized_expr (expr
);
40301 parens
.require_close (parser
);
40305 /* The only expression that is available got parsed for the noexcept
40306 already. noexcept is true then. */
40308 noex
= boolean_true_node
;
40311 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40312 parser
->in_transaction
= old_in
;
40314 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40315 return error_mark_node
;
40317 return (flag_tm
? expr
: error_mark_node
);
40320 /* Parse a function-transaction-block.
40322 function-transaction-block:
40323 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40325 __transaction_atomic txn-attribute[opt] function-try-block
40326 __transaction_relaxed ctor-initializer[opt] function-body
40327 __transaction_relaxed function-try-block
40331 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40333 unsigned char old_in
= parser
->in_transaction
;
40334 unsigned char new_in
= 1;
40335 tree compound_stmt
, stmt
, attrs
;
40338 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40339 || keyword
== RID_TRANSACTION_RELAXED
);
40340 token
= cp_parser_require_keyword (parser
, keyword
,
40341 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40342 : RT_TRANSACTION_RELAXED
));
40343 gcc_assert (token
!= NULL
);
40345 if (keyword
== RID_TRANSACTION_RELAXED
)
40346 new_in
|= TM_STMT_ATTR_RELAXED
;
40349 attrs
= cp_parser_txn_attribute_opt (parser
);
40351 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40354 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40356 parser
->in_transaction
= new_in
;
40358 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40359 cp_parser_function_try_block (parser
);
40361 cp_parser_ctor_initializer_opt_and_function_body
40362 (parser
, /*in_function_try_block=*/false);
40364 parser
->in_transaction
= old_in
;
40366 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40369 /* Parse a __transaction_cancel statement.
40372 __transaction_cancel txn-attribute[opt] ;
40373 __transaction_cancel txn-attribute[opt] throw-expression ;
40375 ??? Cancel and throw is not yet implemented. */
40378 cp_parser_transaction_cancel (cp_parser
*parser
)
40381 bool is_outer
= false;
40384 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40385 RT_TRANSACTION_CANCEL
);
40386 gcc_assert (token
!= NULL
);
40388 attrs
= cp_parser_txn_attribute_opt (parser
);
40390 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40392 /* ??? Parse cancel-and-throw here. */
40394 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40398 error_at (token
->location
, "%<__transaction_cancel%> without "
40399 "transactional memory support enabled");
40400 return error_mark_node
;
40402 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40404 error_at (token
->location
, "%<__transaction_cancel%> within a "
40405 "%<__transaction_relaxed%>");
40406 return error_mark_node
;
40410 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40411 && !is_tm_may_cancel_outer (current_function_decl
))
40413 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40414 "within outer %<__transaction_atomic%>");
40415 error_at (token
->location
,
40416 " or a %<transaction_may_cancel_outer%> function");
40417 return error_mark_node
;
40420 else if (parser
->in_transaction
== 0)
40422 error_at (token
->location
, "%<__transaction_cancel%> not within "
40423 "%<__transaction_atomic%>");
40424 return error_mark_node
;
40427 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40435 static GTY (()) cp_parser
*the_parser
;
40438 /* Special handling for the first token or line in the file. The first
40439 thing in the file might be #pragma GCC pch_preprocess, which loads a
40440 PCH file, which is a GC collection point. So we need to handle this
40441 first pragma without benefit of an existing lexer structure.
40443 Always returns one token to the caller in *FIRST_TOKEN. This is
40444 either the true first token of the file, or the first token after
40445 the initial pragma. */
40448 cp_parser_initial_pragma (cp_token
*first_token
)
40452 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40453 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40456 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40457 if (first_token
->type
== CPP_STRING
)
40459 name
= first_token
->u
.value
;
40461 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40462 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40463 error_at (first_token
->location
,
40464 "junk at end of %<#pragma GCC pch_preprocess%>");
40467 error_at (first_token
->location
, "expected string literal");
40469 /* Skip to the end of the pragma. */
40470 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40471 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40473 /* Now actually load the PCH file. */
40475 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40477 /* Read one more token to return to our caller. We have to do this
40478 after reading the PCH file in, since its pointers have to be
40480 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40483 /* Parse a pragma GCC ivdep. */
40486 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40488 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40492 /* Parse a pragma GCC unroll. */
40494 static unsigned short
40495 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40497 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40498 tree expr
= cp_parser_constant_expression (parser
);
40499 unsigned short unroll
;
40500 expr
= maybe_constant_value (expr
);
40501 HOST_WIDE_INT lunroll
= 0;
40502 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40503 || TREE_CODE (expr
) != INTEGER_CST
40504 || (lunroll
= tree_to_shwi (expr
)) < 0
40505 || lunroll
>= USHRT_MAX
)
40507 error_at (location
, "%<#pragma GCC unroll%> requires an"
40508 " assignment-expression that evaluates to a non-negative"
40509 " integral constant less than %u", USHRT_MAX
);
40514 unroll
= (unsigned short)lunroll
;
40518 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40522 /* Normal parsing of a pragma token. Here we can (and must) use the
40526 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40528 cp_token
*pragma_tok
;
40533 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40534 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40535 parser
->lexer
->in_pragma
= true;
40537 id
= cp_parser_pragma_kind (pragma_tok
);
40538 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40539 cp_ensure_no_omp_declare_simd (parser
);
40542 case PRAGMA_GCC_PCH_PREPROCESS
:
40543 error_at (pragma_tok
->location
,
40544 "%<#pragma GCC pch_preprocess%> must be first");
40547 case PRAGMA_OMP_BARRIER
:
40550 case pragma_compound
:
40551 cp_parser_omp_barrier (parser
, pragma_tok
);
40554 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40555 "used in compound statements", "omp barrier");
40562 case PRAGMA_OMP_DEPOBJ
:
40565 case pragma_compound
:
40566 cp_parser_omp_depobj (parser
, pragma_tok
);
40569 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40570 "used in compound statements", "omp depobj");
40577 case PRAGMA_OMP_FLUSH
:
40580 case pragma_compound
:
40581 cp_parser_omp_flush (parser
, pragma_tok
);
40584 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40585 "used in compound statements", "omp flush");
40592 case PRAGMA_OMP_TASKWAIT
:
40595 case pragma_compound
:
40596 cp_parser_omp_taskwait (parser
, pragma_tok
);
40599 error_at (pragma_tok
->location
,
40600 "%<#pragma %s%> may only be used in compound statements",
40608 case PRAGMA_OMP_TASKYIELD
:
40611 case pragma_compound
:
40612 cp_parser_omp_taskyield (parser
, pragma_tok
);
40615 error_at (pragma_tok
->location
,
40616 "%<#pragma %s%> may only be used in compound statements",
40624 case PRAGMA_OMP_CANCEL
:
40627 case pragma_compound
:
40628 cp_parser_omp_cancel (parser
, pragma_tok
);
40631 error_at (pragma_tok
->location
,
40632 "%<#pragma %s%> may only be used in compound statements",
40640 case PRAGMA_OMP_CANCELLATION_POINT
:
40641 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40644 case PRAGMA_OMP_THREADPRIVATE
:
40645 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40648 case PRAGMA_OMP_DECLARE
:
40649 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40651 case PRAGMA_OACC_DECLARE
:
40652 cp_parser_oacc_declare (parser
, pragma_tok
);
40655 case PRAGMA_OACC_ENTER_DATA
:
40656 if (context
== pragma_stmt
)
40658 error_at (pragma_tok
->location
,
40659 "%<#pragma %s%> may only be used in compound statements",
40663 else if (context
!= pragma_compound
)
40665 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40668 case PRAGMA_OACC_EXIT_DATA
:
40669 if (context
== pragma_stmt
)
40671 error_at (pragma_tok
->location
,
40672 "%<#pragma %s%> may only be used in compound statements",
40676 else if (context
!= pragma_compound
)
40678 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40681 case PRAGMA_OACC_ROUTINE
:
40682 if (context
!= pragma_external
)
40684 error_at (pragma_tok
->location
,
40685 "%<#pragma acc routine%> must be at file scope");
40688 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40691 case PRAGMA_OACC_UPDATE
:
40692 if (context
== pragma_stmt
)
40694 error_at (pragma_tok
->location
,
40695 "%<#pragma %s%> may only be used in compound statements",
40699 else if (context
!= pragma_compound
)
40701 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40704 case PRAGMA_OACC_WAIT
:
40705 if (context
== pragma_stmt
)
40707 error_at (pragma_tok
->location
,
40708 "%<#pragma %s%> may only be used in compound statements",
40712 else if (context
!= pragma_compound
)
40714 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40717 case PRAGMA_OACC_ATOMIC
:
40718 case PRAGMA_OACC_CACHE
:
40719 case PRAGMA_OACC_DATA
:
40720 case PRAGMA_OACC_HOST_DATA
:
40721 case PRAGMA_OACC_KERNELS
:
40722 case PRAGMA_OACC_PARALLEL
:
40723 case PRAGMA_OACC_LOOP
:
40724 case PRAGMA_OMP_ATOMIC
:
40725 case PRAGMA_OMP_CRITICAL
:
40726 case PRAGMA_OMP_DISTRIBUTE
:
40727 case PRAGMA_OMP_FOR
:
40728 case PRAGMA_OMP_MASTER
:
40729 case PRAGMA_OMP_PARALLEL
:
40730 case PRAGMA_OMP_SECTIONS
:
40731 case PRAGMA_OMP_SIMD
:
40732 case PRAGMA_OMP_SINGLE
:
40733 case PRAGMA_OMP_TASK
:
40734 case PRAGMA_OMP_TASKGROUP
:
40735 case PRAGMA_OMP_TASKLOOP
:
40736 case PRAGMA_OMP_TEAMS
:
40737 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40739 stmt
= push_omp_privatization_clauses (false);
40740 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40741 pop_omp_privatization_clauses (stmt
);
40744 case PRAGMA_OMP_REQUIRES
:
40745 return cp_parser_omp_requires (parser
, pragma_tok
);
40747 case PRAGMA_OMP_ORDERED
:
40748 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40750 stmt
= push_omp_privatization_clauses (false);
40751 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40752 pop_omp_privatization_clauses (stmt
);
40755 case PRAGMA_OMP_TARGET
:
40756 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40758 stmt
= push_omp_privatization_clauses (false);
40759 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40760 pop_omp_privatization_clauses (stmt
);
40763 case PRAGMA_OMP_END_DECLARE_TARGET
:
40764 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40767 case PRAGMA_OMP_SECTION
:
40768 error_at (pragma_tok
->location
,
40769 "%<#pragma omp section%> may only be used in "
40770 "%<#pragma omp sections%> construct");
40775 if (context
== pragma_external
)
40777 error_at (pragma_tok
->location
,
40778 "%<#pragma GCC ivdep%> must be inside a function");
40781 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40782 unsigned short unroll
;
40783 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40784 if (tok
->type
== CPP_PRAGMA
40785 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40787 tok
= cp_lexer_consume_token (parser
->lexer
);
40788 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40789 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40793 if (tok
->type
!= CPP_KEYWORD
40794 || (tok
->keyword
!= RID_FOR
40795 && tok
->keyword
!= RID_WHILE
40796 && tok
->keyword
!= RID_DO
))
40798 cp_parser_error (parser
, "for, while or do statement expected");
40801 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40805 case PRAGMA_UNROLL
:
40807 if (context
== pragma_external
)
40809 error_at (pragma_tok
->location
,
40810 "%<#pragma GCC unroll%> must be inside a function");
40813 const unsigned short unroll
40814 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40816 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40817 if (tok
->type
== CPP_PRAGMA
40818 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40820 tok
= cp_lexer_consume_token (parser
->lexer
);
40821 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40822 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40826 if (tok
->type
!= CPP_KEYWORD
40827 || (tok
->keyword
!= RID_FOR
40828 && tok
->keyword
!= RID_WHILE
40829 && tok
->keyword
!= RID_DO
))
40831 cp_parser_error (parser
, "for, while or do statement expected");
40834 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40839 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40840 c_invoke_pragma_handler (id
);
40844 cp_parser_error (parser
, "expected declaration specifiers");
40848 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40852 /* The interface the pragma parsers have to the lexer. */
40855 pragma_lex (tree
*value
, location_t
*loc
)
40857 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40858 enum cpp_ttype ret
= tok
->type
;
40860 *value
= tok
->u
.value
;
40862 *loc
= tok
->location
;
40864 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
40866 else if (ret
== CPP_STRING
)
40867 *value
= cp_parser_string_literal (the_parser
, false, false);
40870 if (ret
== CPP_KEYWORD
)
40872 cp_lexer_consume_token (the_parser
->lexer
);
40879 /* External interface. */
40881 /* Parse one entire translation unit. */
40884 c_parse_file (void)
40886 static bool already_called
= false;
40888 if (already_called
)
40889 fatal_error (input_location
,
40890 "inter-module optimizations not implemented for C++");
40891 already_called
= true;
40893 the_parser
= cp_parser_new ();
40894 push_deferring_access_checks (flag_access_control
40895 ? dk_no_deferred
: dk_no_check
);
40896 cp_parser_translation_unit (the_parser
);
40899 finish_translation_unit ();
40902 /* Create an identifier for a generic parameter type (a synthesized
40903 template parameter implied by `auto' or a concept identifier). */
40905 static GTY(()) int generic_parm_count
;
40907 make_generic_type_name ()
40910 sprintf (buf
, "auto:%d", ++generic_parm_count
);
40911 return get_identifier (buf
);
40914 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40915 (creating a new template parameter list if necessary). Returns the newly
40916 created template type parm. */
40919 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
40921 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
40923 /* Before committing to modifying any scope, if we're in an
40924 implicit template scope, and we're trying to synthesize a
40925 constrained parameter, try to find a previous parameter with
40926 the same name. This is the same-type rule for abbreviated
40927 function templates.
40929 NOTE: We can generate implicit parameters when tentatively
40930 parsing a nested name specifier, only to reject that parse
40931 later. However, matching the same template-id as part of a
40932 direct-declarator should generate an identical template
40933 parameter, so this rule will merge them. */
40934 if (parser
->implicit_template_scope
&& constr
)
40936 tree t
= parser
->implicit_template_parms
;
40939 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
40941 tree d
= TREE_VALUE (t
);
40942 if (TREE_CODE (d
) == PARM_DECL
)
40943 /* Return the TEMPLATE_PARM_INDEX. */
40944 d
= DECL_INITIAL (d
);
40947 t
= TREE_CHAIN (t
);
40951 /* We are either continuing a function template that already contains implicit
40952 template parameters, creating a new fully-implicit function template, or
40953 extending an existing explicit function template with implicit template
40956 cp_binding_level
*const entry_scope
= current_binding_level
;
40958 bool become_template
= false;
40959 cp_binding_level
*parent_scope
= 0;
40961 if (parser
->implicit_template_scope
)
40963 gcc_assert (parser
->implicit_template_parms
);
40965 current_binding_level
= parser
->implicit_template_scope
;
40969 /* Roll back to the existing template parameter scope (in the case of
40970 extending an explicit function template) or introduce a new template
40971 parameter scope ahead of the function parameter scope (or class scope
40972 in the case of out-of-line member definitions). The function scope is
40973 added back after template parameter synthesis below. */
40975 cp_binding_level
*scope
= entry_scope
;
40977 while (scope
->kind
== sk_function_parms
)
40979 parent_scope
= scope
;
40980 scope
= scope
->level_chain
;
40982 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
40984 /* If not defining a class, then any class scope is a scope level in
40985 an out-of-line member definition. In this case simply wind back
40986 beyond the first such scope to inject the template parameter list.
40987 Otherwise wind back to the class being defined. The latter can
40988 occur in class member friend declarations such as:
40994 friend void A::foo (auto);
40997 The template parameter list synthesized for the friend declaration
40998 must be injected in the scope of 'B'. This can also occur in
40999 erroneous cases such as:
41005 void B::foo (auto) {}
41008 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41009 but, nevertheless, the template parameter list synthesized for the
41010 declarator should be injected into the scope of 'A' as if the
41011 ill-formed template was specified explicitly. */
41013 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41015 parent_scope
= scope
;
41016 scope
= scope
->level_chain
;
41020 current_binding_level
= scope
;
41022 if (scope
->kind
!= sk_template_parms
41023 || !function_being_declared_is_template_p (parser
))
41025 /* Introduce a new template parameter list for implicit template
41028 become_template
= true;
41030 parser
->implicit_template_scope
41031 = begin_scope (sk_template_parms
, NULL
);
41033 ++processing_template_decl
;
41035 parser
->fully_implicit_function_template_p
= true;
41036 ++parser
->num_template_parameter_lists
;
41040 /* Synthesize implicit template parameters at the end of the explicit
41041 template parameter list. */
41043 gcc_assert (current_template_parms
);
41045 parser
->implicit_template_scope
= scope
;
41047 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41048 parser
->implicit_template_parms
41049 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41053 /* Synthesize a new template parameter and track the current template
41054 parameter chain with implicit_template_parms. */
41056 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41057 tree synth_id
= make_generic_type_name ();
41058 tree synth_tmpl_parm
;
41059 bool non_type
= false;
41061 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41063 = finish_template_type_parm (class_type_node
, synth_id
);
41064 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41066 = finish_constrained_template_template_parm (proto
, synth_id
);
41069 synth_tmpl_parm
= copy_decl (proto
);
41070 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41074 // Attach the constraint to the parm before processing.
41075 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41076 TREE_TYPE (node
) = constr
;
41078 = process_template_parm (parser
->implicit_template_parms
,
41081 /*non_type=*/non_type
,
41082 /*param_pack=*/false);
41084 // Chain the new parameter to the list of implicit parameters.
41085 if (parser
->implicit_template_parms
)
41086 parser
->implicit_template_parms
41087 = TREE_CHAIN (parser
->implicit_template_parms
);
41089 parser
->implicit_template_parms
= new_parm
;
41091 tree new_decl
= get_local_decls ();
41093 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41094 new_decl
= DECL_INITIAL (new_decl
);
41096 /* If creating a fully implicit function template, start the new implicit
41097 template parameter list with this synthesized type, otherwise grow the
41098 current template parameter list. */
41100 if (become_template
)
41102 parent_scope
->level_chain
= current_binding_level
;
41104 tree new_parms
= make_tree_vec (1);
41105 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41106 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41107 new_parms
, current_template_parms
);
41111 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41112 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41113 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41114 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41117 // If the new parameter was constrained, we need to add that to the
41118 // constraints in the template parameter list.
41119 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41121 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41122 reqs
= conjoin_constraints (reqs
, req
);
41123 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41126 current_binding_level
= entry_scope
;
41131 /* Finish the declaration of a fully implicit function template. Such a
41132 template has no explicit template parameter list so has not been through the
41133 normal template head and tail processing. synthesize_implicit_template_parm
41134 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41135 provided if the declaration is a class member such that its template
41136 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41137 form is returned. Otherwise NULL_TREE is returned. */
41140 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41142 gcc_assert (parser
->fully_implicit_function_template_p
);
41144 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41145 && DECL_VIRTUAL_P (member_decl_opt
))
41147 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41148 "implicit templates may not be %<virtual%>");
41149 DECL_VIRTUAL_P (member_decl_opt
) = false;
41152 if (member_decl_opt
)
41153 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41154 end_template_decl ();
41156 parser
->fully_implicit_function_template_p
= false;
41157 parser
->implicit_template_parms
= 0;
41158 parser
->implicit_template_scope
= 0;
41159 --parser
->num_template_parameter_lists
;
41161 return member_decl_opt
;
41164 /* Like finish_fully_implicit_template, but to be used in error
41165 recovery, rearranging scopes so that we restore the state we had
41166 before synthesize_implicit_template_parm inserted the implement
41167 template parms scope. */
41170 abort_fully_implicit_template (cp_parser
*parser
)
41172 cp_binding_level
*return_to_scope
= current_binding_level
;
41174 if (parser
->implicit_template_scope
41175 && return_to_scope
!= parser
->implicit_template_scope
)
41177 cp_binding_level
*child
= return_to_scope
;
41178 for (cp_binding_level
*scope
= child
->level_chain
;
41179 scope
!= parser
->implicit_template_scope
;
41180 scope
= child
->level_chain
)
41182 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41183 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41184 current_binding_level
= parser
->implicit_template_scope
;
41187 return_to_scope
= return_to_scope
->level_chain
;
41189 finish_fully_implicit_template (parser
, NULL
);
41191 gcc_assert (current_binding_level
== return_to_scope
);
41194 /* Helper function for diagnostics that have complained about things
41195 being used with 'extern "C"' linkage.
41197 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41200 maybe_show_extern_c_location (void)
41202 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41203 inform (the_parser
->innermost_linkage_specification_location
,
41204 "%<extern \"C\"%> linkage started here");
41207 #include "gt-cp-parser.h"