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 "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "String expressions should be translated "
560 "to execution character set",
561 parser
->translate_strings_p
);
562 cp_debug_print_flag (file
, "Parsing function body outside of a "
563 "local class", parser
->in_function_body
);
564 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
565 parser
->colon_corrects_to_scope_p
);
566 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
567 parser
->colon_doesnt_start_class_def_p
);
568 if (parser
->type_definition_forbidden_message
)
569 fprintf (file
, "Error message for forbidden type definitions: %s\n",
570 parser
->type_definition_forbidden_message
);
571 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
572 fprintf (file
, "Number of class definitions in progress: %u\n",
573 parser
->num_classes_being_defined
);
574 fprintf (file
, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser
->num_template_parameter_lists
);
576 cp_debug_parser_tokens (file
, parser
, window_size
);
577 token
= parser
->lexer
->next_token
;
578 fprintf (file
, "Next token to parse:\n");
579 fprintf (file
, "\tToken: ");
580 cp_lexer_print_token (file
, token
);
581 eloc
= expand_location (token
->location
);
582 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
583 fprintf (file
, "\tLine: %d\n", eloc
.line
);
584 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
588 debug (cp_parser
&ref
)
590 cp_debug_parser (stderr
, &ref
);
594 debug (cp_parser
*ptr
)
599 fprintf (stderr
, "<nil>\n");
602 /* Allocate memory for a new lexer object and return it. */
605 cp_lexer_alloc (void)
609 c_common_no_more_pch ();
611 /* Allocate the memory. */
612 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
614 /* Initially we are not debugging. */
615 lexer
->debugging_p
= false;
617 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
619 /* Create the buffer. */
620 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
630 cp_lexer_new_main (void)
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token
);
640 lexer
= cp_lexer_alloc ();
642 /* Put the first token in the buffer. */
643 lexer
->buffer
->quick_push (token
);
645 /* Get the remaining tokens from the preprocessor. */
646 while (token
.type
!= CPP_EOF
)
648 cp_lexer_get_preprocessor_token (lexer
, &token
);
649 vec_safe_push (lexer
->buffer
, token
);
652 lexer
->last_token
= lexer
->buffer
->address ()
653 + lexer
->buffer
->length ()
655 lexer
->next_token
= lexer
->buffer
->length ()
656 ? lexer
->buffer
->address ()
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
663 gcc_assert (!lexer
->next_token
->purged_p
);
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
671 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
673 cp_token
*first
= cache
->first
;
674 cp_token
*last
= cache
->last
;
675 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
677 /* We do not own the buffer. */
678 lexer
->buffer
= NULL
;
679 lexer
->next_token
= first
== last
? &eof_token
: first
;
680 lexer
->last_token
= last
;
682 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
684 /* Initially we are not debugging. */
685 lexer
->debugging_p
= false;
687 gcc_assert (!lexer
->next_token
->purged_p
);
691 /* Frees all resources associated with LEXER. */
694 cp_lexer_destroy (cp_lexer
*lexer
)
696 vec_free (lexer
->buffer
);
697 lexer
->saved_tokens
.release ();
701 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
702 be used. The point of this flag is to help the compiler to fold away calls
703 to cp_lexer_debugging_p within this source file at compile time, when the
704 lexer is not being debugged. */
706 #define LEXER_DEBUGGING_ENABLED_P false
708 /* Returns nonzero if debugging information should be output. */
711 cp_lexer_debugging_p (cp_lexer
*lexer
)
713 if (!LEXER_DEBUGGING_ENABLED_P
)
716 return lexer
->debugging_p
;
720 static inline cp_token_position
721 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
723 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
725 return lexer
->next_token
- previous_p
;
728 static inline cp_token
*
729 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
735 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
737 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
740 static inline cp_token_position
741 cp_lexer_previous_token_position (cp_lexer
*lexer
)
743 if (lexer
->next_token
== &eof_token
)
744 return lexer
->last_token
- 1;
746 return cp_lexer_token_position (lexer
, true);
749 static inline cp_token
*
750 cp_lexer_previous_token (cp_lexer
*lexer
)
752 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
754 /* Skip past purged tokens. */
757 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
761 return cp_lexer_token_at (lexer
, tp
);
764 /* nonzero if we are presently saving tokens. */
767 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
769 return lexer
->saved_tokens
.length () != 0;
772 /* Store the next token from the preprocessor in *TOKEN. Return true
773 if we reach EOF. If LEXER is NULL, assume we are handling an
774 initial #pragma pch_preprocess, and thus want the lexer to return
775 processed strings. */
778 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
780 static int is_extern_c
= 0;
782 /* Get a new token from the preprocessor. */
784 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
785 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
786 token
->keyword
= RID_MAX
;
787 token
->purged_p
= false;
788 token
->error_reported
= false;
790 /* On some systems, some header files are surrounded by an
791 implicit extern "C" block. Set a flag in the token if it
792 comes from such a header. */
793 is_extern_c
+= pending_lang_change
;
794 pending_lang_change
= 0;
795 token
->implicit_extern_c
= is_extern_c
> 0;
797 /* Check to see if this token is a keyword. */
798 if (token
->type
== CPP_NAME
)
800 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
802 /* Mark this token as a keyword. */
803 token
->type
= CPP_KEYWORD
;
804 /* Record which keyword. */
805 token
->keyword
= C_RID_CODE (token
->u
.value
);
809 if (warn_cxx11_compat
810 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
811 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
813 /* Warn about the C++0x keyword (but still treat it as
815 warning (OPT_Wc__11_compat
,
816 "identifier %qE is a keyword in C++11",
819 /* Clear out the C_RID_CODE so we don't warn about this
820 particular identifier-turned-keyword again. */
821 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
824 token
->keyword
= RID_MAX
;
827 else if (token
->type
== CPP_AT_NAME
)
829 /* This only happens in Objective-C++; it must be a keyword. */
830 token
->type
= CPP_KEYWORD
;
831 switch (C_RID_CODE (token
->u
.value
))
833 /* Replace 'class' with '@class', 'private' with '@private',
834 etc. This prevents confusion with the C++ keyword
835 'class', and makes the tokens consistent with other
836 Objective-C 'AT' keywords. For example '@class' is
837 reported as RID_AT_CLASS which is consistent with
838 '@synchronized', which is reported as
841 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
842 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
843 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
844 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
845 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
846 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
847 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
848 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
849 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
854 /* Update the globals input_location and the input file stack from TOKEN. */
856 cp_lexer_set_source_position_from_token (cp_token
*token
)
858 if (token
->type
!= CPP_EOF
)
860 input_location
= token
->location
;
864 /* Update the globals input_location and the input file stack from LEXER. */
866 cp_lexer_set_source_position (cp_lexer
*lexer
)
868 cp_token
*token
= cp_lexer_peek_token (lexer
);
869 cp_lexer_set_source_position_from_token (token
);
872 /* Return a pointer to the next token in the token stream, but do not
875 static inline cp_token
*
876 cp_lexer_peek_token (cp_lexer
*lexer
)
878 if (cp_lexer_debugging_p (lexer
))
880 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
881 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
882 putc ('\n', cp_lexer_debug_stream
);
884 return lexer
->next_token
;
887 /* Return true if the next token has the indicated TYPE. */
890 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
892 return cp_lexer_peek_token (lexer
)->type
== type
;
895 /* Return true if the next token does not have the indicated TYPE. */
898 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
900 return !cp_lexer_next_token_is (lexer
, type
);
903 /* Return true if the next token is the indicated KEYWORD. */
906 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
908 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
912 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
914 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
918 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
920 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
923 /* Return true if KEYWORD can start a decl-specifier. */
926 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
930 /* auto specifier: storage-class-specifier in C++,
931 simple-type-specifier in C++0x. */
933 /* Storage classes. */
939 /* Elaborated type specifiers. */
945 /* Simple type specifiers. */
959 /* GNU extensions. */
962 /* C++0x extensions. */
964 case RID_UNDERLYING_TYPE
:
969 if (keyword
>= RID_FIRST_INT_N
970 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
971 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
977 /* Return true if the next token is a keyword for a decl-specifier. */
980 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
984 token
= cp_lexer_peek_token (lexer
);
985 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
988 /* Returns TRUE iff the token T begins a decltype type. */
991 token_is_decltype (cp_token
*t
)
993 return (t
->keyword
== RID_DECLTYPE
994 || t
->type
== CPP_DECLTYPE
);
997 /* Returns TRUE iff the next token begins a decltype type. */
1000 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1002 cp_token
*t
= cp_lexer_peek_token (lexer
);
1003 return token_is_decltype (t
);
1006 /* Called when processing a token with tree_check_value; perform or defer the
1007 associated checks and return the value. */
1010 saved_checks_value (struct tree_check
*check_value
)
1012 /* Perform any access checks that were deferred. */
1013 vec
<deferred_access_check
, va_gc
> *checks
;
1014 deferred_access_check
*chk
;
1015 checks
= check_value
->checks
;
1019 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1020 perform_or_defer_access_check (chk
->binfo
,
1022 chk
->diag_decl
, tf_warning_or_error
);
1024 /* Return the stored value. */
1025 return check_value
->value
;
1028 /* Return a pointer to the Nth token in the token stream. If N is 1,
1029 then this is precisely equivalent to cp_lexer_peek_token (except
1030 that it is not inline). One would like to disallow that case, but
1031 there is one case (cp_parser_nth_token_starts_template_id) where
1032 the caller passes a variable for N and it might be 1. */
1035 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1039 /* N is 1-based, not zero-based. */
1042 if (cp_lexer_debugging_p (lexer
))
1043 fprintf (cp_lexer_debug_stream
,
1044 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1047 token
= lexer
->next_token
;
1048 gcc_assert (!n
|| token
!= &eof_token
);
1052 if (token
== lexer
->last_token
)
1058 if (!token
->purged_p
)
1062 if (cp_lexer_debugging_p (lexer
))
1064 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1065 putc ('\n', cp_lexer_debug_stream
);
1071 /* Return the next token, and advance the lexer's next_token pointer
1072 to point to the next non-purged token. */
1075 cp_lexer_consume_token (cp_lexer
* lexer
)
1077 cp_token
*token
= lexer
->next_token
;
1079 gcc_assert (token
!= &eof_token
);
1080 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1084 lexer
->next_token
++;
1085 if (lexer
->next_token
== lexer
->last_token
)
1087 lexer
->next_token
= &eof_token
;
1092 while (lexer
->next_token
->purged_p
);
1094 cp_lexer_set_source_position_from_token (token
);
1096 /* Provide debugging output. */
1097 if (cp_lexer_debugging_p (lexer
))
1099 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1100 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1101 putc ('\n', cp_lexer_debug_stream
);
1107 /* Permanently remove the next token from the token stream, and
1108 advance the next_token pointer to refer to the next non-purged
1112 cp_lexer_purge_token (cp_lexer
*lexer
)
1114 cp_token
*tok
= lexer
->next_token
;
1116 gcc_assert (tok
!= &eof_token
);
1117 tok
->purged_p
= true;
1118 tok
->location
= UNKNOWN_LOCATION
;
1119 tok
->u
.value
= NULL_TREE
;
1120 tok
->keyword
= RID_MAX
;
1125 if (tok
== lexer
->last_token
)
1131 while (tok
->purged_p
);
1132 lexer
->next_token
= tok
;
1135 /* Permanently remove all tokens after TOK, up to, but not
1136 including, the token that will be returned next by
1137 cp_lexer_peek_token. */
1140 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1142 cp_token
*peek
= lexer
->next_token
;
1144 if (peek
== &eof_token
)
1145 peek
= lexer
->last_token
;
1147 gcc_assert (tok
< peek
);
1149 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1151 tok
->purged_p
= true;
1152 tok
->location
= UNKNOWN_LOCATION
;
1153 tok
->u
.value
= NULL_TREE
;
1154 tok
->keyword
= RID_MAX
;
1158 /* Begin saving tokens. All tokens consumed after this point will be
1162 cp_lexer_save_tokens (cp_lexer
* lexer
)
1164 /* Provide debugging output. */
1165 if (cp_lexer_debugging_p (lexer
))
1166 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1168 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1171 /* Commit to the portion of the token stream most recently saved. */
1174 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1176 /* Provide debugging output. */
1177 if (cp_lexer_debugging_p (lexer
))
1178 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1180 lexer
->saved_tokens
.pop ();
1183 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1184 to the token stream. Stop saving tokens. */
1187 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1189 /* Provide debugging output. */
1190 if (cp_lexer_debugging_p (lexer
))
1191 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1193 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1196 /* RAII wrapper around the above functions, with sanity checking. Creating
1197 a variable saves tokens, which are committed when the variable is
1198 destroyed unless they are explicitly rolled back by calling the rollback
1201 struct saved_token_sentinel
1206 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1208 len
= lexer
->saved_tokens
.length ();
1209 cp_lexer_save_tokens (lexer
);
1213 cp_lexer_rollback_tokens (lexer
);
1216 ~saved_token_sentinel()
1219 cp_lexer_commit_tokens (lexer
);
1220 gcc_assert (lexer
->saved_tokens
.length () == len
);
1224 /* Print a representation of the TOKEN on the STREAM. */
1227 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1229 /* We don't use cpp_type2name here because the parser defines
1230 a few tokens of its own. */
1231 static const char *const token_names
[] = {
1232 /* cpplib-defined token types */
1233 #define OP(e, s) #e,
1234 #define TK(e, s) #e,
1238 /* C++ parser token types - see "Manifest constants", above. */
1241 "NESTED_NAME_SPECIFIER",
1244 /* For some tokens, print the associated data. */
1245 switch (token
->type
)
1248 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1249 For example, `struct' is mapped to an INTEGER_CST. */
1250 if (!identifier_p (token
->u
.value
))
1254 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1261 case CPP_UTF8STRING
:
1262 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1266 print_generic_expr (stream
, token
->u
.value
);
1270 /* If we have a name for the token, print it out. Otherwise, we
1271 simply give the numeric code. */
1272 if (token
->type
< ARRAY_SIZE(token_names
))
1273 fputs (token_names
[token
->type
], stream
);
1275 fprintf (stream
, "[%d]", token
->type
);
1281 debug (cp_token
&ref
)
1283 cp_lexer_print_token (stderr
, &ref
);
1284 fprintf (stderr
, "\n");
1288 debug (cp_token
*ptr
)
1293 fprintf (stderr
, "<nil>\n");
1297 /* Start emitting debugging information. */
1300 cp_lexer_start_debugging (cp_lexer
* lexer
)
1302 if (!LEXER_DEBUGGING_ENABLED_P
)
1303 fatal_error (input_location
,
1304 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1306 lexer
->debugging_p
= true;
1307 cp_lexer_debug_stream
= stderr
;
1310 /* Stop emitting debugging information. */
1313 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1315 if (!LEXER_DEBUGGING_ENABLED_P
)
1316 fatal_error (input_location
,
1317 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1319 lexer
->debugging_p
= false;
1320 cp_lexer_debug_stream
= NULL
;
1323 /* Create a new cp_token_cache, representing a range of tokens. */
1325 static cp_token_cache
*
1326 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1328 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1329 cache
->first
= first
;
1334 /* Diagnose if #pragma omp declare simd isn't followed immediately
1335 by function declaration or definition. */
1338 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1340 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1342 error ("%<#pragma omp declare simd%> not immediately followed by "
1343 "function declaration or definition");
1344 parser
->omp_declare_simd
= NULL
;
1348 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1349 and put that into "omp declare simd" attribute. */
1352 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1354 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1356 if (fndecl
== error_mark_node
)
1358 parser
->omp_declare_simd
= NULL
;
1361 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1363 cp_ensure_no_omp_declare_simd (parser
);
1369 /* Diagnose if #pragma acc routine isn't followed immediately by function
1370 declaration or definition. */
1373 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1375 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1377 error_at (parser
->oacc_routine
->loc
,
1378 "%<#pragma acc routine%> not immediately followed by "
1379 "function declaration or definition");
1380 parser
->oacc_routine
= NULL
;
1384 /* Decl-specifiers. */
1386 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1389 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1391 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1396 /* Nothing other than the parser should be creating declarators;
1397 declarators are a semi-syntactic representation of C++ entities.
1398 Other parts of the front end that need to create entities (like
1399 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1401 static cp_declarator
*make_call_declarator
1402 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1403 static cp_declarator
*make_array_declarator
1404 (cp_declarator
*, tree
);
1405 static cp_declarator
*make_pointer_declarator
1406 (cp_cv_quals
, cp_declarator
*, tree
);
1407 static cp_declarator
*make_reference_declarator
1408 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1409 static cp_declarator
*make_ptrmem_declarator
1410 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1412 /* An erroneous declarator. */
1413 static cp_declarator
*cp_error_declarator
;
1415 /* The obstack on which declarators and related data structures are
1417 static struct obstack declarator_obstack
;
1419 /* Alloc BYTES from the declarator memory pool. */
1421 static inline void *
1422 alloc_declarator (size_t bytes
)
1424 return obstack_alloc (&declarator_obstack
, bytes
);
1427 /* Allocate a declarator of the indicated KIND. Clear fields that are
1428 common to all declarators. */
1430 static cp_declarator
*
1431 make_declarator (cp_declarator_kind kind
)
1433 cp_declarator
*declarator
;
1435 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1436 declarator
->kind
= kind
;
1437 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1438 declarator
->attributes
= NULL_TREE
;
1439 declarator
->std_attributes
= NULL_TREE
;
1440 declarator
->declarator
= NULL
;
1441 declarator
->parameter_pack_p
= false;
1442 declarator
->id_loc
= UNKNOWN_LOCATION
;
1447 /* Make a declarator for a generalized identifier. If
1448 QUALIFYING_SCOPE is non-NULL, the identifier is
1449 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1450 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1453 static cp_declarator
*
1454 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1455 special_function_kind sfk
)
1457 cp_declarator
*declarator
;
1459 /* It is valid to write:
1461 class C { void f(); };
1465 The standard is not clear about whether `typedef const C D' is
1466 legal; as of 2002-09-15 the committee is considering that
1467 question. EDG 3.0 allows that syntax. Therefore, we do as
1469 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1470 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1472 gcc_assert (identifier_p (unqualified_name
)
1473 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1474 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1476 declarator
= make_declarator (cdk_id
);
1477 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1478 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1479 declarator
->u
.id
.sfk
= sfk
;
1484 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1485 of modifiers such as const or volatile to apply to the pointer
1486 type, represented as identifiers. ATTRIBUTES represent the attributes that
1487 appertain to the pointer or reference. */
1490 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1493 cp_declarator
*declarator
;
1495 declarator
= make_declarator (cdk_pointer
);
1496 declarator
->declarator
= target
;
1497 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1498 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1501 declarator
->id_loc
= target
->id_loc
;
1502 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1503 target
->parameter_pack_p
= false;
1506 declarator
->parameter_pack_p
= false;
1508 declarator
->std_attributes
= attributes
;
1513 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1514 represent the attributes that appertain to the pointer or
1518 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1519 bool rvalue_ref
, tree attributes
)
1521 cp_declarator
*declarator
;
1523 declarator
= make_declarator (cdk_reference
);
1524 declarator
->declarator
= target
;
1525 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1526 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1529 declarator
->id_loc
= target
->id_loc
;
1530 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1531 target
->parameter_pack_p
= false;
1534 declarator
->parameter_pack_p
= false;
1536 declarator
->std_attributes
= attributes
;
1541 /* Like make_pointer_declarator -- but for a pointer to a non-static
1542 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1543 appertain to the pointer or reference. */
1546 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1547 cp_declarator
*pointee
,
1550 cp_declarator
*declarator
;
1552 declarator
= make_declarator (cdk_ptrmem
);
1553 declarator
->declarator
= pointee
;
1554 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1555 declarator
->u
.pointer
.class_type
= class_type
;
1559 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1560 pointee
->parameter_pack_p
= false;
1563 declarator
->parameter_pack_p
= false;
1565 declarator
->std_attributes
= attributes
;
1570 /* Make a declarator for the function given by TARGET, with the
1571 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1572 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1573 indicates what exceptions can be thrown. */
1576 make_call_declarator (cp_declarator
*target
,
1578 cp_cv_quals cv_qualifiers
,
1579 cp_virt_specifiers virt_specifiers
,
1580 cp_ref_qualifier ref_qualifier
,
1582 tree exception_specification
,
1583 tree late_return_type
,
1584 tree requires_clause
)
1586 cp_declarator
*declarator
;
1588 declarator
= make_declarator (cdk_function
);
1589 declarator
->declarator
= target
;
1590 declarator
->u
.function
.parameters
= parms
;
1591 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1592 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1593 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1594 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1595 declarator
->u
.function
.exception_specification
= exception_specification
;
1596 declarator
->u
.function
.late_return_type
= late_return_type
;
1597 declarator
->u
.function
.requires_clause
= requires_clause
;
1600 declarator
->id_loc
= target
->id_loc
;
1601 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1602 target
->parameter_pack_p
= false;
1605 declarator
->parameter_pack_p
= false;
1610 /* Make a declarator for an array of BOUNDS elements, each of which is
1611 defined by ELEMENT. */
1614 make_array_declarator (cp_declarator
*element
, tree bounds
)
1616 cp_declarator
*declarator
;
1618 declarator
= make_declarator (cdk_array
);
1619 declarator
->declarator
= element
;
1620 declarator
->u
.array
.bounds
= bounds
;
1623 declarator
->id_loc
= element
->id_loc
;
1624 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1625 element
->parameter_pack_p
= false;
1628 declarator
->parameter_pack_p
= false;
1633 /* Determine whether the declarator we've seen so far can be a
1634 parameter pack, when followed by an ellipsis. */
1636 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1638 if (declarator
&& declarator
->parameter_pack_p
)
1639 /* We already saw an ellipsis. */
1642 /* Search for a declarator name, or any other declarator that goes
1643 after the point where the ellipsis could appear in a parameter
1644 pack. If we find any of these, then this declarator can not be
1645 made into a parameter pack. */
1647 while (declarator
&& !found
)
1649 switch ((int)declarator
->kind
)
1661 declarator
= declarator
->declarator
;
1669 cp_parameter_declarator
*no_parameters
;
1671 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1672 DECLARATOR and DEFAULT_ARGUMENT. */
1674 cp_parameter_declarator
*
1675 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1676 cp_declarator
*declarator
,
1677 tree default_argument
,
1679 bool template_parameter_pack_p
= false)
1681 cp_parameter_declarator
*parameter
;
1683 parameter
= ((cp_parameter_declarator
*)
1684 alloc_declarator (sizeof (cp_parameter_declarator
)));
1685 parameter
->next
= NULL
;
1686 if (decl_specifiers
)
1687 parameter
->decl_specifiers
= *decl_specifiers
;
1689 clear_decl_specs (¶meter
->decl_specifiers
);
1690 parameter
->declarator
= declarator
;
1691 parameter
->default_argument
= default_argument
;
1692 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1693 parameter
->loc
= loc
;
1698 /* Returns true iff DECLARATOR is a declaration for a function. */
1701 function_declarator_p (const cp_declarator
*declarator
)
1705 if (declarator
->kind
== cdk_function
1706 && declarator
->declarator
->kind
== cdk_id
)
1708 if (declarator
->kind
== cdk_id
1709 || declarator
->kind
== cdk_decomp
1710 || declarator
->kind
== cdk_error
)
1712 declarator
= declarator
->declarator
;
1722 A cp_parser parses the token stream as specified by the C++
1723 grammar. Its job is purely parsing, not semantic analysis. For
1724 example, the parser breaks the token stream into declarators,
1725 expressions, statements, and other similar syntactic constructs.
1726 It does not check that the types of the expressions on either side
1727 of an assignment-statement are compatible, or that a function is
1728 not declared with a parameter of type `void'.
1730 The parser invokes routines elsewhere in the compiler to perform
1731 semantic analysis and to build up the abstract syntax tree for the
1734 The parser (and the template instantiation code, which is, in a
1735 way, a close relative of parsing) are the only parts of the
1736 compiler that should be calling push_scope and pop_scope, or
1737 related functions. The parser (and template instantiation code)
1738 keeps track of what scope is presently active; everything else
1739 should simply honor that. (The code that generates static
1740 initializers may also need to set the scope, in order to check
1741 access control correctly when emitting the initializers.)
1746 The parser is of the standard recursive-descent variety. Upcoming
1747 tokens in the token stream are examined in order to determine which
1748 production to use when parsing a non-terminal. Some C++ constructs
1749 require arbitrary look ahead to disambiguate. For example, it is
1750 impossible, in the general case, to tell whether a statement is an
1751 expression or declaration without scanning the entire statement.
1752 Therefore, the parser is capable of "parsing tentatively." When the
1753 parser is not sure what construct comes next, it enters this mode.
1754 Then, while we attempt to parse the construct, the parser queues up
1755 error messages, rather than issuing them immediately, and saves the
1756 tokens it consumes. If the construct is parsed successfully, the
1757 parser "commits", i.e., it issues any queued error messages and
1758 the tokens that were being preserved are permanently discarded.
1759 If, however, the construct is not parsed successfully, the parser
1760 rolls back its state completely so that it can resume parsing using
1761 a different alternative.
1766 The performance of the parser could probably be improved substantially.
1767 We could often eliminate the need to parse tentatively by looking ahead
1768 a little bit. In some places, this approach might not entirely eliminate
1769 the need to parse tentatively, but it might still speed up the average
1772 /* Flags that are passed to some parsing functions. These values can
1773 be bitwise-ored together. */
1778 CP_PARSER_FLAGS_NONE
= 0x0,
1779 /* The construct is optional. If it is not present, then no error
1780 should be issued. */
1781 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1782 /* When parsing a type-specifier, treat user-defined type-names
1783 as non-type identifiers. */
1784 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1785 /* When parsing a type-specifier, do not try to parse a class-specifier
1786 or enum-specifier. */
1787 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1788 /* When parsing a decl-specifier-seq, only allow type-specifier or
1790 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1791 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1792 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1795 /* This type is used for parameters and variables which hold
1796 combinations of the above flags. */
1797 typedef int cp_parser_flags
;
1799 /* The different kinds of declarators we want to parse. */
1801 enum cp_parser_declarator_kind
1803 /* We want an abstract declarator. */
1804 CP_PARSER_DECLARATOR_ABSTRACT
,
1805 /* We want a named declarator. */
1806 CP_PARSER_DECLARATOR_NAMED
,
1807 /* We don't mind, but the name must be an unqualified-id. */
1808 CP_PARSER_DECLARATOR_EITHER
1811 /* The precedence values used to parse binary expressions. The minimum value
1812 of PREC must be 1, because zero is reserved to quickly discriminate
1813 binary operators from other tokens. */
1818 PREC_LOGICAL_OR_EXPRESSION
,
1819 PREC_LOGICAL_AND_EXPRESSION
,
1820 PREC_INCLUSIVE_OR_EXPRESSION
,
1821 PREC_EXCLUSIVE_OR_EXPRESSION
,
1822 PREC_AND_EXPRESSION
,
1823 PREC_EQUALITY_EXPRESSION
,
1824 PREC_RELATIONAL_EXPRESSION
,
1825 PREC_SHIFT_EXPRESSION
,
1826 PREC_ADDITIVE_EXPRESSION
,
1827 PREC_MULTIPLICATIVE_EXPRESSION
,
1829 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1832 /* A mapping from a token type to a corresponding tree node type, with a
1833 precedence value. */
1835 struct cp_parser_binary_operations_map_node
1837 /* The token type. */
1838 enum cpp_ttype token_type
;
1839 /* The corresponding tree code. */
1840 enum tree_code tree_type
;
1841 /* The precedence of this operator. */
1842 enum cp_parser_prec prec
;
1845 struct cp_parser_expression_stack_entry
1847 /* Left hand side of the binary operation we are currently
1850 /* Original tree code for left hand side, if it was a binary
1851 expression itself (used for -Wparentheses). */
1852 enum tree_code lhs_type
;
1853 /* Tree code for the binary operation we are parsing. */
1854 enum tree_code tree_type
;
1855 /* Precedence of the binary operation we are parsing. */
1856 enum cp_parser_prec prec
;
1857 /* Location of the binary operation we are parsing. */
1861 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1862 entries because precedence levels on the stack are monotonically
1864 typedef struct cp_parser_expression_stack_entry
1865 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1869 /* Constructors and destructors. */
1871 static cp_parser_context
*cp_parser_context_new
1872 (cp_parser_context
*);
1874 /* Class variables. */
1876 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1878 /* The operator-precedence table used by cp_parser_binary_expression.
1879 Transformed into an associative array (binops_by_token) by
1882 static const cp_parser_binary_operations_map_node binops
[] = {
1883 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1884 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1886 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1887 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1888 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1890 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1891 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1893 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1894 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1896 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1897 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1898 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1899 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1901 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1902 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1904 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1906 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1908 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1910 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1912 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1915 /* The same as binops, but initialized by cp_parser_new so that
1916 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1918 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1920 /* Constructors and destructors. */
1922 /* Construct a new context. The context below this one on the stack
1923 is given by NEXT. */
1925 static cp_parser_context
*
1926 cp_parser_context_new (cp_parser_context
* next
)
1928 cp_parser_context
*context
;
1930 /* Allocate the storage. */
1931 if (cp_parser_context_free_list
!= NULL
)
1933 /* Pull the first entry from the free list. */
1934 context
= cp_parser_context_free_list
;
1935 cp_parser_context_free_list
= context
->next
;
1936 memset (context
, 0, sizeof (*context
));
1939 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1941 /* No errors have occurred yet in this context. */
1942 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1943 /* If this is not the bottommost context, copy information that we
1944 need from the previous context. */
1947 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1948 expression, then we are parsing one in this context, too. */
1949 context
->object_type
= next
->object_type
;
1950 /* Thread the stack. */
1951 context
->next
= next
;
1957 /* Managing the unparsed function queues. */
1959 #define unparsed_funs_with_default_args \
1960 parser->unparsed_queues->last ().funs_with_default_args
1961 #define unparsed_funs_with_definitions \
1962 parser->unparsed_queues->last ().funs_with_definitions
1963 #define unparsed_nsdmis \
1964 parser->unparsed_queues->last ().nsdmis
1965 #define unparsed_classes \
1966 parser->unparsed_queues->last ().classes
1969 push_unparsed_function_queues (cp_parser
*parser
)
1971 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1972 vec_safe_push (parser
->unparsed_queues
, e
);
1976 pop_unparsed_function_queues (cp_parser
*parser
)
1978 release_tree_vector (unparsed_funs_with_definitions
);
1979 parser
->unparsed_queues
->pop ();
1984 /* Constructors and destructors. */
1986 static cp_parser
*cp_parser_new
1989 /* Routines to parse various constructs.
1991 Those that return `tree' will return the error_mark_node (rather
1992 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1993 Sometimes, they will return an ordinary node if error-recovery was
1994 attempted, even though a parse error occurred. So, to check
1995 whether or not a parse error occurred, you should always use
1996 cp_parser_error_occurred. If the construct is optional (indicated
1997 either by an `_opt' in the name of the function that does the
1998 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1999 the construct is not present. */
2001 /* Lexical conventions [gram.lex] */
2003 static cp_expr cp_parser_identifier
2005 static cp_expr cp_parser_string_literal
2006 (cp_parser
*, bool, bool, bool);
2007 static cp_expr cp_parser_userdef_char_literal
2009 static tree cp_parser_userdef_string_literal
2011 static cp_expr cp_parser_userdef_numeric_literal
2014 /* Basic concepts [gram.basic] */
2016 static void cp_parser_translation_unit (cp_parser
*);
2018 /* Expressions [gram.expr] */
2020 static cp_expr cp_parser_primary_expression
2021 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2022 static cp_expr cp_parser_id_expression
2023 (cp_parser
*, bool, bool, bool *, bool, bool);
2024 static cp_expr cp_parser_unqualified_id
2025 (cp_parser
*, bool, bool, bool, bool);
2026 static tree cp_parser_nested_name_specifier_opt
2027 (cp_parser
*, bool, bool, bool, bool, bool = false);
2028 static tree cp_parser_nested_name_specifier
2029 (cp_parser
*, bool, bool, bool, bool);
2030 static tree cp_parser_qualifying_entity
2031 (cp_parser
*, bool, bool, bool, bool, bool);
2032 static cp_expr cp_parser_postfix_expression
2033 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2034 static tree cp_parser_postfix_open_square_expression
2035 (cp_parser
*, tree
, bool, bool);
2036 static tree cp_parser_postfix_dot_deref_expression
2037 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2038 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2039 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2041 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2042 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2043 static void cp_parser_pseudo_destructor_name
2044 (cp_parser
*, tree
, tree
*, tree
*);
2045 static cp_expr cp_parser_unary_expression
2046 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2047 static enum tree_code cp_parser_unary_operator
2049 static tree cp_parser_new_expression
2051 static vec
<tree
, va_gc
> *cp_parser_new_placement
2053 static tree cp_parser_new_type_id
2054 (cp_parser
*, tree
*);
2055 static cp_declarator
*cp_parser_new_declarator_opt
2057 static cp_declarator
*cp_parser_direct_new_declarator
2059 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2061 static tree cp_parser_delete_expression
2063 static cp_expr cp_parser_cast_expression
2064 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2065 static cp_expr cp_parser_binary_expression
2066 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2067 static tree cp_parser_question_colon_clause
2068 (cp_parser
*, cp_expr
);
2069 static cp_expr cp_parser_assignment_expression
2070 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2071 static enum tree_code cp_parser_assignment_operator_opt
2073 static cp_expr cp_parser_expression
2074 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2075 static cp_expr cp_parser_constant_expression
2076 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2077 static cp_expr cp_parser_builtin_offsetof
2079 static cp_expr cp_parser_lambda_expression
2081 static void cp_parser_lambda_introducer
2082 (cp_parser
*, tree
);
2083 static bool cp_parser_lambda_declarator_opt
2084 (cp_parser
*, tree
);
2085 static void cp_parser_lambda_body
2086 (cp_parser
*, tree
);
2088 /* Statements [gram.stmt.stmt] */
2090 static void cp_parser_statement
2091 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2092 static void cp_parser_label_for_labeled_statement
2093 (cp_parser
*, tree
);
2094 static tree cp_parser_expression_statement
2095 (cp_parser
*, tree
);
2096 static tree cp_parser_compound_statement
2097 (cp_parser
*, tree
, int, bool);
2098 static void cp_parser_statement_seq_opt
2099 (cp_parser
*, tree
);
2100 static tree cp_parser_selection_statement
2101 (cp_parser
*, bool *, vec
<tree
> *);
2102 static tree cp_parser_condition
2104 static tree cp_parser_iteration_statement
2105 (cp_parser
*, bool *, bool, unsigned short);
2106 static bool cp_parser_init_statement
2107 (cp_parser
*, tree
*decl
);
2108 static tree cp_parser_for
2109 (cp_parser
*, bool, unsigned short);
2110 static tree cp_parser_c_for
2111 (cp_parser
*, tree
, tree
, bool, unsigned short);
2112 static tree cp_parser_range_for
2113 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2114 static void do_range_for_auto_deduction
2116 static tree cp_parser_perform_range_for_lookup
2117 (tree
, tree
*, tree
*);
2118 static tree cp_parser_range_for_member_function
2120 static tree cp_parser_jump_statement
2122 static void cp_parser_declaration_statement
2125 static tree cp_parser_implicitly_scoped_statement
2126 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2127 static void cp_parser_already_scoped_statement
2128 (cp_parser
*, bool *, const token_indent_info
&);
2130 /* Declarations [gram.dcl.dcl] */
2132 static void cp_parser_declaration_seq_opt
2134 static void cp_parser_declaration
2136 static void cp_parser_toplevel_declaration
2138 static void cp_parser_block_declaration
2139 (cp_parser
*, bool);
2140 static void cp_parser_simple_declaration
2141 (cp_parser
*, bool, tree
*);
2142 static void cp_parser_decl_specifier_seq
2143 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2146 static tree cp_parser_function_specifier_opt
2147 (cp_parser
*, cp_decl_specifier_seq
*);
2148 static tree cp_parser_type_specifier
2149 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2151 static tree cp_parser_simple_type_specifier
2152 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2153 static tree cp_parser_type_name
2154 (cp_parser
*, bool);
2155 static tree cp_parser_type_name
2157 static tree cp_parser_nonclass_name
2158 (cp_parser
* parser
);
2159 static tree cp_parser_elaborated_type_specifier
2160 (cp_parser
*, bool, bool);
2161 static tree cp_parser_enum_specifier
2163 static void cp_parser_enumerator_list
2164 (cp_parser
*, tree
);
2165 static void cp_parser_enumerator_definition
2166 (cp_parser
*, tree
);
2167 static tree cp_parser_namespace_name
2169 static void cp_parser_namespace_definition
2171 static void cp_parser_namespace_body
2173 static tree cp_parser_qualified_namespace_specifier
2175 static void cp_parser_namespace_alias_definition
2177 static bool cp_parser_using_declaration
2178 (cp_parser
*, bool);
2179 static void cp_parser_using_directive
2181 static tree cp_parser_alias_declaration
2183 static void cp_parser_asm_definition
2185 static void cp_parser_linkage_specification
2187 static void cp_parser_static_assert
2188 (cp_parser
*, bool);
2189 static tree cp_parser_decltype
2191 static tree cp_parser_decomposition_declaration
2192 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2194 /* Declarators [gram.dcl.decl] */
2196 static tree cp_parser_init_declarator
2197 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2198 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2199 static cp_declarator
*cp_parser_declarator
2200 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2201 static cp_declarator
*cp_parser_direct_declarator
2202 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2203 static enum tree_code cp_parser_ptr_operator
2204 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2205 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2207 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2209 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2211 static tree cp_parser_tx_qualifier_opt
2213 static tree cp_parser_late_return_type_opt
2214 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2215 static tree cp_parser_declarator_id
2216 (cp_parser
*, bool);
2217 static tree cp_parser_type_id
2218 (cp_parser
*, location_t
* = NULL
);
2219 static tree cp_parser_template_type_arg
2221 static tree
cp_parser_trailing_type_id (cp_parser
*);
2222 static tree cp_parser_type_id_1
2223 (cp_parser
*, bool, bool, location_t
*);
2224 static void cp_parser_type_specifier_seq
2225 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2226 static tree cp_parser_parameter_declaration_clause
2228 static tree cp_parser_parameter_declaration_list
2230 static cp_parameter_declarator
*cp_parser_parameter_declaration
2231 (cp_parser
*, bool, bool *);
2232 static tree cp_parser_default_argument
2233 (cp_parser
*, bool);
2234 static void cp_parser_function_body
2235 (cp_parser
*, bool);
2236 static tree cp_parser_initializer
2237 (cp_parser
*, bool *, bool *, bool = false);
2238 static cp_expr cp_parser_initializer_clause
2239 (cp_parser
*, bool *);
2240 static cp_expr cp_parser_braced_list
2241 (cp_parser
*, bool*);
2242 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2243 (cp_parser
*, bool *);
2245 static void cp_parser_ctor_initializer_opt_and_function_body
2246 (cp_parser
*, bool);
2248 static tree cp_parser_late_parsing_omp_declare_simd
2249 (cp_parser
*, tree
);
2251 static tree cp_parser_late_parsing_oacc_routine
2252 (cp_parser
*, tree
);
2254 static tree synthesize_implicit_template_parm
2255 (cp_parser
*, tree
);
2256 static tree finish_fully_implicit_template
2257 (cp_parser
*, tree
);
2258 static void abort_fully_implicit_template
2261 /* Classes [gram.class] */
2263 static tree cp_parser_class_name
2264 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2267 static tree cp_parser_class_head
2268 (cp_parser
*, bool *);
2269 static enum tag_types cp_parser_class_key
2271 static void cp_parser_type_parameter_key
2272 (cp_parser
* parser
);
2273 static void cp_parser_member_specification_opt
2275 static void cp_parser_member_declaration
2277 static tree cp_parser_pure_specifier
2279 static tree cp_parser_constant_initializer
2282 /* Derived classes [gram.class.derived] */
2284 static tree cp_parser_base_clause
2286 static tree cp_parser_base_specifier
2289 /* Special member functions [gram.special] */
2291 static tree cp_parser_conversion_function_id
2293 static tree cp_parser_conversion_type_id
2295 static cp_declarator
*cp_parser_conversion_declarator_opt
2297 static void cp_parser_ctor_initializer_opt
2299 static void cp_parser_mem_initializer_list
2301 static tree cp_parser_mem_initializer
2303 static tree cp_parser_mem_initializer_id
2306 /* Overloading [gram.over] */
2308 static cp_expr cp_parser_operator_function_id
2310 static cp_expr cp_parser_operator
2313 /* Templates [gram.temp] */
2315 static void cp_parser_template_declaration
2316 (cp_parser
*, bool);
2317 static tree cp_parser_template_parameter_list
2319 static tree cp_parser_template_parameter
2320 (cp_parser
*, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser
*, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser
*, bool, bool, enum tag_types
, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2327 static tree cp_parser_template_argument_list
2329 static tree cp_parser_template_argument
2331 static void cp_parser_explicit_instantiation
2333 static void cp_parser_explicit_specialization
2336 /* Exception handling [gram.exception] */
2338 static tree cp_parser_try_block
2340 static void cp_parser_function_try_block
2342 static void cp_parser_handler_seq
2344 static void cp_parser_handler
2346 static tree cp_parser_exception_declaration
2348 static tree cp_parser_throw_expression
2350 static tree cp_parser_exception_specification_opt
2352 static tree cp_parser_type_id_list
2355 /* GNU Extensions */
2357 static tree cp_parser_asm_specification_opt
2359 static tree cp_parser_asm_operand_list
2361 static tree cp_parser_asm_clobber_list
2363 static tree cp_parser_asm_label_list
2365 static bool cp_next_tokens_can_be_attribute_p
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2369 static bool cp_next_tokens_can_be_std_attribute_p
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser
*, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser
*, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser
*, size_t);
2377 static tree cp_parser_attributes_opt
2379 static tree cp_parser_gnu_attributes_opt
2381 static tree cp_parser_gnu_attribute_list
2383 static tree cp_parser_std_attribute
2384 (cp_parser
*, tree
);
2385 static tree cp_parser_std_attribute_spec
2387 static tree cp_parser_std_attribute_spec_seq
2389 static size_t cp_parser_skip_attributes_opt
2390 (cp_parser
*, size_t);
2391 static bool cp_parser_extension_opt
2392 (cp_parser
*, int *);
2393 static void cp_parser_label_declaration
2396 /* Concept Extensions */
2398 static tree cp_parser_requires_clause
2400 static tree cp_parser_requires_clause_opt
2402 static tree cp_parser_requires_expression
2404 static tree cp_parser_requirement_parameter_list
2406 static tree cp_parser_requirement_body
2408 static tree cp_parser_requirement_list
2410 static tree cp_parser_requirement
2412 static tree cp_parser_simple_requirement
2414 static tree cp_parser_compound_requirement
2416 static tree cp_parser_type_requirement
2418 static tree cp_parser_nested_requirement
2421 /* Transactional Memory Extensions */
2423 static tree cp_parser_transaction
2424 (cp_parser
*, cp_token
*);
2425 static tree cp_parser_transaction_expression
2426 (cp_parser
*, enum rid
);
2427 static void cp_parser_function_transaction
2428 (cp_parser
*, enum rid
);
2429 static tree cp_parser_transaction_cancel
2432 enum pragma_context
{
2439 static bool cp_parser_pragma
2440 (cp_parser
*, enum pragma_context
, bool *);
2442 /* Objective-C++ Productions */
2444 static tree cp_parser_objc_message_receiver
2446 static tree cp_parser_objc_message_args
2448 static tree cp_parser_objc_message_expression
2450 static cp_expr cp_parser_objc_encode_expression
2452 static tree cp_parser_objc_defs_expression
2454 static tree cp_parser_objc_protocol_expression
2456 static tree cp_parser_objc_selector_expression
2458 static cp_expr cp_parser_objc_expression
2460 static bool cp_parser_objc_selector_p
2462 static tree cp_parser_objc_selector
2464 static tree cp_parser_objc_protocol_refs_opt
2466 static void cp_parser_objc_declaration
2467 (cp_parser
*, tree
);
2468 static tree cp_parser_objc_statement
2470 static bool cp_parser_objc_valid_prefix_attributes
2471 (cp_parser
*, tree
*);
2472 static void cp_parser_objc_at_property_declaration
2474 static void cp_parser_objc_at_synthesize_declaration
2476 static void cp_parser_objc_at_dynamic_declaration
2478 static tree cp_parser_objc_struct_declaration
2481 /* Utility Routines */
2483 static cp_expr cp_parser_lookup_name
2484 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2485 static tree cp_parser_lookup_name_simple
2486 (cp_parser
*, tree
, location_t
);
2487 static tree cp_parser_maybe_treat_template_as_class
2489 static bool cp_parser_check_declarator_template_parameters
2490 (cp_parser
*, cp_declarator
*, location_t
);
2491 static bool cp_parser_check_template_parameters
2492 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2493 static cp_expr cp_parser_simple_cast_expression
2495 static tree cp_parser_global_scope_opt
2496 (cp_parser
*, bool);
2497 static bool cp_parser_constructor_declarator_p
2498 (cp_parser
*, bool);
2499 static tree cp_parser_function_definition_from_specifiers_and_declarator
2500 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2501 static tree cp_parser_function_definition_after_declarator
2502 (cp_parser
*, bool);
2503 static bool cp_parser_template_declaration_after_export
2504 (cp_parser
*, bool);
2505 static void cp_parser_perform_template_parameter_access_checks
2506 (vec
<deferred_access_check
, va_gc
> *);
2507 static tree cp_parser_single_declaration
2508 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2509 static cp_expr cp_parser_functional_cast
2510 (cp_parser
*, tree
);
2511 static tree cp_parser_save_member_function_body
2512 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2513 static tree cp_parser_save_nsdmi
2515 static tree cp_parser_enclosed_template_argument_list
2517 static void cp_parser_save_default_args
2518 (cp_parser
*, tree
);
2519 static void cp_parser_late_parsing_for_member
2520 (cp_parser
*, tree
);
2521 static tree cp_parser_late_parse_one_default_arg
2522 (cp_parser
*, tree
, tree
, tree
);
2523 static void cp_parser_late_parsing_nsdmi
2524 (cp_parser
*, tree
);
2525 static void cp_parser_late_parsing_default_args
2526 (cp_parser
*, tree
);
2527 static tree cp_parser_sizeof_operand
2528 (cp_parser
*, enum rid
);
2529 static cp_expr cp_parser_trait_expr
2530 (cp_parser
*, enum rid
);
2531 static bool cp_parser_declares_only_class_p
2533 static void cp_parser_set_storage_class
2534 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2535 static void cp_parser_set_decl_spec_type
2536 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2537 static void set_and_check_decl_spec_loc
2538 (cp_decl_specifier_seq
*decl_specs
,
2539 cp_decl_spec ds
, cp_token
*);
2540 static bool cp_parser_friend_p
2541 (const cp_decl_specifier_seq
*);
2542 static void cp_parser_required_error
2543 (cp_parser
*, required_token
, bool, location_t
);
2544 static cp_token
*cp_parser_require
2545 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2546 static cp_token
*cp_parser_require_keyword
2547 (cp_parser
*, enum rid
, required_token
);
2548 static bool cp_parser_token_starts_function_definition_p
2550 static bool cp_parser_next_token_starts_class_definition_p
2552 static bool cp_parser_next_token_ends_template_argument_p
2554 static bool cp_parser_nth_token_starts_template_argument_list_p
2555 (cp_parser
*, size_t);
2556 static enum tag_types cp_parser_token_is_class_key
2558 static enum tag_types cp_parser_token_is_type_parameter_key
2560 static void cp_parser_check_class_key
2561 (enum tag_types
, tree type
);
2562 static void cp_parser_check_access_in_redeclaration
2563 (tree type
, location_t location
);
2564 static bool cp_parser_optional_template_keyword
2566 static void cp_parser_pre_parsed_nested_name_specifier
2568 static bool cp_parser_cache_group
2569 (cp_parser
*, enum cpp_ttype
, unsigned);
2570 static tree cp_parser_cache_defarg
2571 (cp_parser
*parser
, bool nsdmi
);
2572 static void cp_parser_parse_tentatively
2574 static void cp_parser_commit_to_tentative_parse
2576 static void cp_parser_commit_to_topmost_tentative_parse
2578 static void cp_parser_abort_tentative_parse
2580 static bool cp_parser_parse_definitely
2582 static inline bool cp_parser_parsing_tentatively
2584 static bool cp_parser_uncommitted_to_tentative_parse_p
2586 static void cp_parser_error
2587 (cp_parser
*, const char *);
2588 static void cp_parser_name_lookup_error
2589 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2590 static bool cp_parser_simulate_error
2592 static bool cp_parser_check_type_definition
2594 static void cp_parser_check_for_definition_in_return_type
2595 (cp_declarator
*, tree
, location_t type_location
);
2596 static void cp_parser_check_for_invalid_template_id
2597 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2598 static bool cp_parser_non_integral_constant_expression
2599 (cp_parser
*, non_integral_constant
);
2600 static void cp_parser_diagnose_invalid_type_name
2601 (cp_parser
*, tree
, location_t
);
2602 static bool cp_parser_parse_and_diagnose_invalid_type_name
2604 static int cp_parser_skip_to_closing_parenthesis
2605 (cp_parser
*, bool, bool, bool);
2606 static void cp_parser_skip_to_end_of_statement
2608 static void cp_parser_consume_semicolon_at_end_of_statement
2610 static void cp_parser_skip_to_end_of_block_or_statement
2612 static bool cp_parser_skip_to_closing_brace
2614 static void cp_parser_skip_to_end_of_template_parameter_list
2616 static void cp_parser_skip_to_pragma_eol
2617 (cp_parser
*, cp_token
*);
2618 static bool cp_parser_error_occurred
2620 static bool cp_parser_allow_gnu_extensions_p
2622 static bool cp_parser_is_pure_string_literal
2624 static bool cp_parser_is_string_literal
2626 static bool cp_parser_is_keyword
2627 (cp_token
*, enum rid
);
2628 static tree cp_parser_make_typename_type
2629 (cp_parser
*, tree
, location_t location
);
2630 static cp_declarator
* cp_parser_make_indirect_declarator
2631 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2632 static bool cp_parser_compound_literal_p
2634 static bool cp_parser_array_designator_p
2636 static bool cp_parser_init_statement_p
2638 static bool cp_parser_skip_to_closing_square_bracket
2641 /* Concept-related syntactic transformations */
2643 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2644 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2646 // -------------------------------------------------------------------------- //
2647 // Unevaluated Operand Guard
2649 // Implementation of an RAII helper for unevaluated operand parsing.
2650 cp_unevaluated::cp_unevaluated ()
2652 ++cp_unevaluated_operand
;
2653 ++c_inhibit_evaluation_warnings
;
2656 cp_unevaluated::~cp_unevaluated ()
2658 --c_inhibit_evaluation_warnings
;
2659 --cp_unevaluated_operand
;
2662 // -------------------------------------------------------------------------- //
2663 // Tentative Parsing
2665 /* Returns nonzero if we are parsing tentatively. */
2668 cp_parser_parsing_tentatively (cp_parser
* parser
)
2670 return parser
->context
->next
!= NULL
;
2673 /* Returns nonzero if TOKEN is a string literal. */
2676 cp_parser_is_pure_string_literal (cp_token
* token
)
2678 return (token
->type
== CPP_STRING
||
2679 token
->type
== CPP_STRING16
||
2680 token
->type
== CPP_STRING32
||
2681 token
->type
== CPP_WSTRING
||
2682 token
->type
== CPP_UTF8STRING
);
2685 /* Returns nonzero if TOKEN is a string literal
2686 of a user-defined string literal. */
2689 cp_parser_is_string_literal (cp_token
* token
)
2691 return (cp_parser_is_pure_string_literal (token
) ||
2692 token
->type
== CPP_STRING_USERDEF
||
2693 token
->type
== CPP_STRING16_USERDEF
||
2694 token
->type
== CPP_STRING32_USERDEF
||
2695 token
->type
== CPP_WSTRING_USERDEF
||
2696 token
->type
== CPP_UTF8STRING_USERDEF
);
2699 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2702 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2704 return token
->keyword
== keyword
;
2707 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2710 static enum pragma_kind
2711 cp_parser_pragma_kind (cp_token
*token
)
2713 if (token
->type
!= CPP_PRAGMA
)
2715 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2716 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2719 /* Helper function for cp_parser_error.
2720 Having peeked a token of kind TOK1_KIND that might signify
2721 a conflict marker, peek successor tokens to determine
2722 if we actually do have a conflict marker.
2723 Specifically, we consider a run of 7 '<', '=' or '>' characters
2724 at the start of a line as a conflict marker.
2725 These come through the lexer as three pairs and a single,
2726 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2727 If it returns true, *OUT_LOC is written to with the location/range
2731 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2732 location_t
*out_loc
)
2734 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2735 if (token2
->type
!= tok1_kind
)
2737 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2738 if (token3
->type
!= tok1_kind
)
2740 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2741 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2744 /* It must be at the start of the line. */
2745 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2746 if (LOCATION_COLUMN (start_loc
) != 1)
2749 /* We have a conflict marker. Construct a location of the form:
2752 with start == caret, finishing at the end of the marker. */
2753 location_t finish_loc
= get_finish (token4
->location
);
2754 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2759 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2763 get_matching_symbol (required_token token_desc
)
2770 case RT_CLOSE_BRACE
:
2772 case RT_CLOSE_PAREN
:
2777 /* Attempt to convert TOKEN_DESC from a required_token to an
2778 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2780 static enum cpp_ttype
2781 get_required_cpp_ttype (required_token token_desc
)
2786 return CPP_SEMICOLON
;
2788 return CPP_OPEN_PAREN
;
2789 case RT_CLOSE_BRACE
:
2790 return CPP_CLOSE_BRACE
;
2792 return CPP_OPEN_BRACE
;
2793 case RT_CLOSE_SQUARE
:
2794 return CPP_CLOSE_SQUARE
;
2795 case RT_OPEN_SQUARE
:
2796 return CPP_OPEN_SQUARE
;
2801 case RT_CLOSE_PAREN
:
2802 return CPP_CLOSE_PAREN
;
2805 /* Use CPP_EOF as a "no completions possible" code. */
2811 /* Subroutine of cp_parser_error and cp_parser_required_error.
2813 Issue a diagnostic of the form
2814 FILE:LINE: MESSAGE before TOKEN
2815 where TOKEN is the next token in the input stream. MESSAGE
2816 (specified by the caller) is usually of the form "expected
2819 This bypasses the check for tentative passing, and potentially
2820 adds material needed by cp_parser_required_error.
2822 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2823 suggesting insertion of the missing token.
2825 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2826 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2830 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2831 required_token missing_token_desc
,
2832 location_t matching_location
)
2834 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2835 /* This diagnostic makes more sense if it is tagged to the line
2836 of the token we just peeked at. */
2837 cp_lexer_set_source_position_from_token (token
);
2839 if (token
->type
== CPP_PRAGMA
)
2841 error_at (token
->location
,
2842 "%<#pragma%> is not allowed here");
2843 cp_parser_skip_to_pragma_eol (parser
, token
);
2847 /* If this is actually a conflict marker, report it as such. */
2848 if (token
->type
== CPP_LSHIFT
2849 || token
->type
== CPP_RSHIFT
2850 || token
->type
== CPP_EQ_EQ
)
2853 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2855 error_at (loc
, "version control conflict marker in file");
2856 expanded_location token_exploc
= expand_location (token
->location
);
2857 /* Consume tokens until the end of the source line. */
2860 cp_lexer_consume_token (parser
->lexer
);
2861 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2864 expanded_location next_exploc
= expand_location (next
->location
);
2865 if (next_exploc
.file
!= token_exploc
.file
)
2867 if (next_exploc
.line
!= token_exploc
.line
)
2874 gcc_rich_location
richloc (input_location
);
2876 bool added_matching_location
= false;
2878 if (missing_token_desc
!= RT_NONE
)
2880 /* Potentially supply a fix-it hint, suggesting to add the
2881 missing token immediately after the *previous* token.
2882 This may move the primary location within richloc. */
2883 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2884 location_t prev_token_loc
2885 = cp_lexer_previous_token (parser
->lexer
)->location
;
2886 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2888 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2889 Attempt to consolidate diagnostics by printing it as a
2890 secondary range within the main diagnostic. */
2891 if (matching_location
!= UNKNOWN_LOCATION
)
2892 added_matching_location
2893 = richloc
.add_location_if_nearby (matching_location
);
2896 /* Actually emit the error. */
2897 c_parse_error (gmsgid
,
2898 /* Because c_parser_error does not understand
2899 CPP_KEYWORD, keywords are treated like
2901 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2902 token
->u
.value
, token
->flags
, &richloc
);
2904 if (missing_token_desc
!= RT_NONE
)
2906 /* If we weren't able to consolidate matching_location, then
2907 print it as a secondary diagnostic. */
2908 if (matching_location
!= UNKNOWN_LOCATION
2909 && !added_matching_location
)
2910 inform (matching_location
, "to match this %qs",
2911 get_matching_symbol (missing_token_desc
));
2915 /* If not parsing tentatively, issue a diagnostic of the form
2916 FILE:LINE: MESSAGE before TOKEN
2917 where TOKEN is the next token in the input stream. MESSAGE
2918 (specified by the caller) is usually of the form "expected
2922 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2924 if (!cp_parser_simulate_error (parser
))
2925 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2928 /* Issue an error about name-lookup failing. NAME is the
2929 IDENTIFIER_NODE DECL is the result of
2930 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2931 the thing that we hoped to find. */
2934 cp_parser_name_lookup_error (cp_parser
* parser
,
2937 name_lookup_error desired
,
2938 location_t location
)
2940 /* If name lookup completely failed, tell the user that NAME was not
2942 if (decl
== error_mark_node
)
2944 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2945 error_at (location
, "%<%E::%E%> has not been declared",
2946 parser
->scope
, name
);
2947 else if (parser
->scope
== global_namespace
)
2948 error_at (location
, "%<::%E%> has not been declared", name
);
2949 else if (parser
->object_scope
2950 && !CLASS_TYPE_P (parser
->object_scope
))
2951 error_at (location
, "request for member %qE in non-class type %qT",
2952 name
, parser
->object_scope
);
2953 else if (parser
->object_scope
)
2954 error_at (location
, "%<%T::%E%> has not been declared",
2955 parser
->object_scope
, name
);
2957 error_at (location
, "%qE has not been declared", name
);
2959 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2964 error_at (location
, "%<%E::%E%> is not a type",
2965 parser
->scope
, name
);
2968 error_at (location
, "%<%E::%E%> is not a class or namespace",
2969 parser
->scope
, name
);
2973 "%<%E::%E%> is not a class, namespace, or enumeration",
2974 parser
->scope
, name
);
2981 else if (parser
->scope
== global_namespace
)
2986 error_at (location
, "%<::%E%> is not a type", name
);
2989 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2993 "%<::%E%> is not a class, namespace, or enumeration",
3005 error_at (location
, "%qE is not a type", name
);
3008 error_at (location
, "%qE is not a class or namespace", name
);
3012 "%qE is not a class, namespace, or enumeration", name
);
3020 /* If we are parsing tentatively, remember that an error has occurred
3021 during this tentative parse. Returns true if the error was
3022 simulated; false if a message should be issued by the caller. */
3025 cp_parser_simulate_error (cp_parser
* parser
)
3027 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3029 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3035 /* This function is called when a type is defined. If type
3036 definitions are forbidden at this point, an error message is
3040 cp_parser_check_type_definition (cp_parser
* parser
)
3042 /* If types are forbidden here, issue a message. */
3043 if (parser
->type_definition_forbidden_message
)
3045 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3046 in the message need to be interpreted. */
3047 error (parser
->type_definition_forbidden_message
);
3053 /* This function is called when the DECLARATOR is processed. The TYPE
3054 was a type defined in the decl-specifiers. If it is invalid to
3055 define a type in the decl-specifiers for DECLARATOR, an error is
3056 issued. TYPE_LOCATION is the location of TYPE and is used
3057 for error reporting. */
3060 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3061 tree type
, location_t type_location
)
3063 /* [dcl.fct] forbids type definitions in return types.
3064 Unfortunately, it's not easy to know whether or not we are
3065 processing a return type until after the fact. */
3067 && (declarator
->kind
== cdk_pointer
3068 || declarator
->kind
== cdk_reference
3069 || declarator
->kind
== cdk_ptrmem
))
3070 declarator
= declarator
->declarator
;
3072 && declarator
->kind
== cdk_function
)
3074 error_at (type_location
,
3075 "new types may not be defined in a return type");
3076 inform (type_location
,
3077 "(perhaps a semicolon is missing after the definition of %qT)",
3082 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3083 "<" in any valid C++ program. If the next token is indeed "<",
3084 issue a message warning the user about what appears to be an
3085 invalid attempt to form a template-id. LOCATION is the location
3086 of the type-specifier (TYPE) */
3089 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3091 enum tag_types tag_type
,
3092 location_t location
)
3094 cp_token_position start
= 0;
3096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3098 if (TREE_CODE (type
) == TYPE_DECL
)
3099 type
= TREE_TYPE (type
);
3100 if (TYPE_P (type
) && !template_placeholder_p (type
))
3101 error_at (location
, "%qT is not a template", type
);
3102 else if (identifier_p (type
))
3104 if (tag_type
!= none_type
)
3105 error_at (location
, "%qE is not a class template", type
);
3107 error_at (location
, "%qE is not a template", type
);
3110 error_at (location
, "invalid template-id");
3111 /* Remember the location of the invalid "<". */
3112 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3113 start
= cp_lexer_token_position (parser
->lexer
, true);
3114 /* Consume the "<". */
3115 cp_lexer_consume_token (parser
->lexer
);
3116 /* Parse the template arguments. */
3117 cp_parser_enclosed_template_argument_list (parser
);
3118 /* Permanently remove the invalid template arguments so that
3119 this error message is not issued again. */
3121 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3125 /* If parsing an integral constant-expression, issue an error message
3126 about the fact that THING appeared and return true. Otherwise,
3127 return false. In either case, set
3128 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3131 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3132 non_integral_constant thing
)
3134 parser
->non_integral_constant_expression_p
= true;
3135 if (parser
->integral_constant_expression_p
)
3137 if (!parser
->allow_non_integral_constant_expression_p
)
3139 const char *msg
= NULL
;
3143 pedwarn (input_location
, OPT_Wpedantic
,
3144 "ISO C++ forbids using a floating-point literal "
3145 "in a constant-expression");
3148 error ("a cast to a type other than an integral or "
3149 "enumeration type cannot appear in a "
3150 "constant-expression");
3153 error ("%<typeid%> operator "
3154 "cannot appear in a constant-expression");
3157 error ("non-constant compound literals "
3158 "cannot appear in a constant-expression");
3161 error ("a function call "
3162 "cannot appear in a constant-expression");
3165 error ("an increment "
3166 "cannot appear in a constant-expression");
3169 error ("an decrement "
3170 "cannot appear in a constant-expression");
3173 error ("an array reference "
3174 "cannot appear in a constant-expression");
3176 case NIC_ADDR_LABEL
:
3177 error ("the address of a label "
3178 "cannot appear in a constant-expression");
3180 case NIC_OVERLOADED
:
3181 error ("calls to overloaded operators "
3182 "cannot appear in a constant-expression");
3184 case NIC_ASSIGNMENT
:
3185 error ("an assignment cannot appear in a constant-expression");
3188 error ("a comma operator "
3189 "cannot appear in a constant-expression");
3191 case NIC_CONSTRUCTOR
:
3192 error ("a call to a constructor "
3193 "cannot appear in a constant-expression");
3195 case NIC_TRANSACTION
:
3196 error ("a transaction expression "
3197 "cannot appear in a constant-expression");
3203 msg
= "__FUNCTION__";
3205 case NIC_PRETTY_FUNC
:
3206 msg
= "__PRETTY_FUNCTION__";
3226 case NIC_PREINCREMENT
:
3229 case NIC_PREDECREMENT
:
3242 error ("%qs cannot appear in a constant-expression", msg
);
3249 /* Emit a diagnostic for an invalid type name. This function commits
3250 to the current active tentative parse, if any. (Otherwise, the
3251 problematic construct might be encountered again later, resulting
3252 in duplicate error messages.) LOCATION is the location of ID. */
3255 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3256 location_t location
)
3258 tree decl
, ambiguous_decls
;
3259 cp_parser_commit_to_tentative_parse (parser
);
3260 /* Try to lookup the identifier. */
3261 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3262 /*is_template=*/false,
3263 /*is_namespace=*/false,
3264 /*check_dependency=*/true,
3265 &ambiguous_decls
, location
);
3266 if (ambiguous_decls
)
3267 /* If the lookup was ambiguous, an error will already have
3270 /* If the lookup found a template-name, it means that the user forgot
3271 to specify an argument list. Emit a useful error message. */
3272 if (DECL_TYPE_TEMPLATE_P (decl
))
3274 auto_diagnostic_group d
;
3276 "invalid use of template-name %qE without an argument list",
3278 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3279 inform (location
, "class template argument deduction is only available "
3280 "with -std=c++17 or -std=gnu++17");
3281 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3283 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3284 error_at (location
, "invalid use of destructor %qD as a type", id
);
3285 else if (TREE_CODE (decl
) == TYPE_DECL
)
3286 /* Something like 'unsigned A a;' */
3287 error_at (location
, "invalid combination of multiple type-specifiers");
3288 else if (!parser
->scope
)
3290 /* Issue an error message. */
3291 auto_diagnostic_group d
;
3293 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3294 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3297 gcc_rich_location
richloc (location
);
3298 richloc
.add_fixit_replace (hint
.suggestion ());
3300 "%qE does not name a type; did you mean %qs?",
3301 id
, hint
.suggestion ());
3304 error_at (location
, "%qE does not name a type", id
);
3305 /* If we're in a template class, it's possible that the user was
3306 referring to a type from a base class. For example:
3308 template <typename T> struct A { typedef T X; };
3309 template <typename T> struct B : public A<T> { X x; };
3311 The user should have said "typename A<T>::X". */
3312 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3313 inform (location
, "C++11 %<constexpr%> only available with "
3314 "-std=c++11 or -std=gnu++11");
3315 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3316 inform (location
, "C++11 %<noexcept%> only available with "
3317 "-std=c++11 or -std=gnu++11");
3318 else if (cxx_dialect
< cxx11
3319 && TREE_CODE (id
) == IDENTIFIER_NODE
3320 && id_equal (id
, "thread_local"))
3321 inform (location
, "C++11 %<thread_local%> only available with "
3322 "-std=c++11 or -std=gnu++11");
3323 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3324 inform (location
, "%<concept%> only available with -fconcepts");
3325 else if (processing_template_decl
&& current_class_type
3326 && TYPE_BINFO (current_class_type
))
3330 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3334 tree base_type
= BINFO_TYPE (b
);
3335 if (CLASS_TYPE_P (base_type
)
3336 && dependent_type_p (base_type
))
3339 /* Go from a particular instantiation of the
3340 template (which will have an empty TYPE_FIELDs),
3341 to the main version. */
3342 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3343 for (field
= TYPE_FIELDS (base_type
);
3345 field
= DECL_CHAIN (field
))
3346 if (TREE_CODE (field
) == TYPE_DECL
3347 && DECL_NAME (field
) == id
)
3350 "(perhaps %<typename %T::%E%> was intended)",
3351 BINFO_TYPE (b
), id
);
3360 /* Here we diagnose qualified-ids where the scope is actually correct,
3361 but the identifier does not resolve to a valid type name. */
3362 else if (parser
->scope
!= error_mark_node
)
3364 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3366 auto_diagnostic_group d
;
3367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a template type",
3371 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3372 error_at (location_of (id
),
3373 "%qE in namespace %qE does not name a template type",
3374 TREE_OPERAND (id
, 0), parser
->scope
);
3376 error_at (location_of (id
),
3377 "%qE in namespace %qE does not name a type",
3380 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3381 else if (decl
== error_mark_node
)
3382 suggest_alternative_in_explicit_scope (location
, id
,
3385 else if (CLASS_TYPE_P (parser
->scope
)
3386 && constructor_name_p (id
, parser
->scope
))
3389 auto_diagnostic_group d
;
3390 error_at (location
, "%<%T::%E%> names the constructor, not"
3391 " the type", parser
->scope
, id
);
3392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3393 error_at (location
, "and %qT has no template constructors",
3396 else if (TYPE_P (parser
->scope
)
3397 && dependent_scope_p (parser
->scope
))
3399 gcc_rich_location
richloc (location
);
3400 richloc
.add_fixit_insert_before ("typename ");
3401 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3403 "need %<typename%> before %<%T::%D::%E%> because "
3404 "%<%T::%D%> is a dependent scope",
3405 TYPE_CONTEXT (parser
->scope
),
3406 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3408 TYPE_CONTEXT (parser
->scope
),
3409 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3411 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3412 "%qT is a dependent scope",
3413 parser
->scope
, id
, parser
->scope
);
3415 else if (TYPE_P (parser
->scope
))
3417 auto_diagnostic_group d
;
3418 if (!COMPLETE_TYPE_P (parser
->scope
))
3419 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3421 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3422 error_at (location_of (id
),
3423 "%qE in %q#T does not name a template type",
3425 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3426 error_at (location_of (id
),
3427 "%qE in %q#T does not name a template type",
3428 TREE_OPERAND (id
, 0), parser
->scope
);
3430 error_at (location_of (id
),
3431 "%qE in %q#T does not name a type",
3434 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3441 /* Check for a common situation where a type-name should be present,
3442 but is not, and issue a sensible error message. Returns true if an
3443 invalid type-name was detected.
3445 The situation handled by this function are variable declarations of the
3446 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3447 Usually, `ID' should name a type, but if we got here it means that it
3448 does not. We try to emit the best possible error message depending on
3449 how exactly the id-expression looks like. */
3452 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3455 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3457 /* Avoid duplicate error about ambiguous lookup. */
3458 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3460 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3461 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3465 cp_parser_parse_tentatively (parser
);
3466 id
= cp_parser_id_expression (parser
,
3467 /*template_keyword_p=*/false,
3468 /*check_dependency_p=*/true,
3469 /*template_p=*/NULL
,
3470 /*declarator_p=*/false,
3471 /*optional_p=*/false);
3472 /* If the next token is a (, this is a function with no explicit return
3473 type, i.e. constructor, destructor or conversion op. */
3474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3475 || TREE_CODE (id
) == TYPE_DECL
)
3477 cp_parser_abort_tentative_parse (parser
);
3480 if (!cp_parser_parse_definitely (parser
))
3483 /* Emit a diagnostic for the invalid type. */
3484 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3486 /* If we aren't in the middle of a declarator (i.e. in a
3487 parameter-declaration-clause), skip to the end of the declaration;
3488 there's no point in trying to process it. */
3489 if (!parser
->in_declarator_p
)
3490 cp_parser_skip_to_end_of_block_or_statement (parser
);
3494 /* Consume tokens up to, and including, the next non-nested closing `)'.
3495 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3496 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3497 found an unnested token of that type. */
3500 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3505 unsigned paren_depth
= 0;
3506 unsigned brace_depth
= 0;
3507 unsigned square_depth
= 0;
3508 unsigned condop_depth
= 0;
3510 if (recovering
&& or_ttype
== CPP_EOF
3511 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3516 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3518 /* Have we found what we're looking for before the closing paren? */
3519 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3520 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3523 switch (token
->type
)
3526 case CPP_PRAGMA_EOL
:
3527 /* If we've run out of tokens, then there is no closing `)'. */
3530 /* This is good for lambda expression capture-lists. */
3531 case CPP_OPEN_SQUARE
:
3534 case CPP_CLOSE_SQUARE
:
3535 if (!square_depth
--)
3540 /* This matches the processing in skip_to_end_of_statement. */
3545 case CPP_OPEN_BRACE
:
3548 case CPP_CLOSE_BRACE
:
3553 case CPP_OPEN_PAREN
:
3558 case CPP_CLOSE_PAREN
:
3559 if (!brace_depth
&& !paren_depth
--)
3562 cp_lexer_consume_token (parser
->lexer
);
3568 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3573 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3581 /* Consume the token. */
3582 cp_lexer_consume_token (parser
->lexer
);
3586 /* Consume tokens up to, and including, the next non-nested closing `)'.
3587 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3588 are doing error recovery. Returns -1 if OR_COMMA is true and we
3589 found an unnested token of that type. */
3592 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3597 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3598 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3599 ttype
, consume_paren
);
3602 /* Consume tokens until we reach the end of the current statement.
3603 Normally, that will be just before consuming a `;'. However, if a
3604 non-nested `}' comes first, then we stop before consuming that. */
3607 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3609 unsigned nesting_depth
= 0;
3611 /* Unwind generic function template scope if necessary. */
3612 if (parser
->fully_implicit_function_template_p
)
3613 abort_fully_implicit_template (parser
);
3617 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3619 switch (token
->type
)
3622 case CPP_PRAGMA_EOL
:
3623 /* If we've run out of tokens, stop. */
3627 /* If the next token is a `;', we have reached the end of the
3633 case CPP_CLOSE_BRACE
:
3634 /* If this is a non-nested '}', stop before consuming it.
3635 That way, when confronted with something like:
3639 we stop before consuming the closing '}', even though we
3640 have not yet reached a `;'. */
3641 if (nesting_depth
== 0)
3644 /* If it is the closing '}' for a block that we have
3645 scanned, stop -- but only after consuming the token.
3651 we will stop after the body of the erroneously declared
3652 function, but before consuming the following `typedef'
3654 if (--nesting_depth
== 0)
3656 cp_lexer_consume_token (parser
->lexer
);
3661 case CPP_OPEN_BRACE
:
3669 /* Consume the token. */
3670 cp_lexer_consume_token (parser
->lexer
);
3674 /* This function is called at the end of a statement or declaration.
3675 If the next token is a semicolon, it is consumed; otherwise, error
3676 recovery is attempted. */
3679 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3681 /* Look for the trailing `;'. */
3682 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3684 /* If there is additional (erroneous) input, skip to the end of
3686 cp_parser_skip_to_end_of_statement (parser
);
3687 /* If the next token is now a `;', consume it. */
3688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3689 cp_lexer_consume_token (parser
->lexer
);
3693 /* Skip tokens until we have consumed an entire block, or until we
3694 have consumed a non-nested `;'. */
3697 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3699 int nesting_depth
= 0;
3701 /* Unwind generic function template scope if necessary. */
3702 if (parser
->fully_implicit_function_template_p
)
3703 abort_fully_implicit_template (parser
);
3705 while (nesting_depth
>= 0)
3707 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3709 switch (token
->type
)
3712 case CPP_PRAGMA_EOL
:
3713 /* If we've run out of tokens, stop. */
3717 /* Stop if this is an unnested ';'. */
3722 case CPP_CLOSE_BRACE
:
3723 /* Stop if this is an unnested '}', or closes the outermost
3726 if (nesting_depth
< 0)
3732 case CPP_OPEN_BRACE
:
3741 /* Consume the token. */
3742 cp_lexer_consume_token (parser
->lexer
);
3746 /* Skip tokens until a non-nested closing curly brace is the next
3747 token, or there are no more tokens. Return true in the first case,
3751 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3753 unsigned nesting_depth
= 0;
3757 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3759 switch (token
->type
)
3762 case CPP_PRAGMA_EOL
:
3763 /* If we've run out of tokens, stop. */
3766 case CPP_CLOSE_BRACE
:
3767 /* If the next token is a non-nested `}', then we have reached
3768 the end of the current block. */
3769 if (nesting_depth
-- == 0)
3773 case CPP_OPEN_BRACE
:
3774 /* If it the next token is a `{', then we are entering a new
3775 block. Consume the entire block. */
3783 /* Consume the token. */
3784 cp_lexer_consume_token (parser
->lexer
);
3788 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3789 parameter is the PRAGMA token, allowing us to purge the entire pragma
3793 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3797 parser
->lexer
->in_pragma
= false;
3800 token
= cp_lexer_consume_token (parser
->lexer
);
3801 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3803 /* Ensure that the pragma is not parsed again. */
3804 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3807 /* Require pragma end of line, resyncing with it as necessary. The
3808 arguments are as for cp_parser_skip_to_pragma_eol. */
3811 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3813 parser
->lexer
->in_pragma
= false;
3814 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3815 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3818 /* This is a simple wrapper around make_typename_type. When the id is
3819 an unresolved identifier node, we can provide a superior diagnostic
3820 using cp_parser_diagnose_invalid_type_name. */
3823 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3824 location_t id_location
)
3827 if (identifier_p (id
))
3829 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3830 /*complain=*/tf_none
);
3831 if (result
== error_mark_node
)
3832 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3835 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3838 /* This is a wrapper around the
3839 make_{pointer,ptrmem,reference}_declarator functions that decides
3840 which one to call based on the CODE and CLASS_TYPE arguments. The
3841 CODE argument should be one of the values returned by
3842 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3843 appertain to the pointer or reference. */
3845 static cp_declarator
*
3846 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3847 cp_cv_quals cv_qualifiers
,
3848 cp_declarator
*target
,
3851 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3852 return cp_error_declarator
;
3854 if (code
== INDIRECT_REF
)
3855 if (class_type
== NULL_TREE
)
3856 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3858 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3859 target
, attributes
);
3860 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3861 return make_reference_declarator (cv_qualifiers
, target
,
3863 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3864 return make_reference_declarator (cv_qualifiers
, target
,
3869 /* Create a new C++ parser. */
3872 cp_parser_new (void)
3878 /* cp_lexer_new_main is called before doing GC allocation because
3879 cp_lexer_new_main might load a PCH file. */
3880 lexer
= cp_lexer_new_main ();
3882 /* Initialize the binops_by_token so that we can get the tree
3883 directly from the token. */
3884 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3885 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3887 parser
= ggc_cleared_alloc
<cp_parser
> ();
3888 parser
->lexer
= lexer
;
3889 parser
->context
= cp_parser_context_new (NULL
);
3891 /* For now, we always accept GNU extensions. */
3892 parser
->allow_gnu_extensions_p
= 1;
3894 /* The `>' token is a greater-than operator, not the end of a
3896 parser
->greater_than_is_operator_p
= true;
3898 parser
->default_arg_ok_p
= true;
3900 /* We are not parsing a constant-expression. */
3901 parser
->integral_constant_expression_p
= false;
3902 parser
->allow_non_integral_constant_expression_p
= false;
3903 parser
->non_integral_constant_expression_p
= false;
3905 /* Local variable names are not forbidden. */
3906 parser
->local_variables_forbidden_p
= false;
3908 /* We are not processing an `extern "C"' declaration. */
3909 parser
->in_unbraced_linkage_specification_p
= false;
3911 /* We are not processing a declarator. */
3912 parser
->in_declarator_p
= false;
3914 /* We are not processing a template-argument-list. */
3915 parser
->in_template_argument_list_p
= false;
3917 /* We are not in an iteration statement. */
3918 parser
->in_statement
= 0;
3920 /* We are not in a switch statement. */
3921 parser
->in_switch_statement_p
= false;
3923 /* We are not parsing a type-id inside an expression. */
3924 parser
->in_type_id_in_expr_p
= false;
3926 /* String literals should be translated to the execution character set. */
3927 parser
->translate_strings_p
= true;
3929 /* We are not parsing a function body. */
3930 parser
->in_function_body
= false;
3932 /* We can correct until told otherwise. */
3933 parser
->colon_corrects_to_scope_p
= true;
3935 /* The unparsed function queue is empty. */
3936 push_unparsed_function_queues (parser
);
3938 /* There are no classes being defined. */
3939 parser
->num_classes_being_defined
= 0;
3941 /* No template parameters apply. */
3942 parser
->num_template_parameter_lists
= 0;
3944 /* Special parsing data structures. */
3945 parser
->omp_declare_simd
= NULL
;
3946 parser
->oacc_routine
= NULL
;
3948 /* Not declaring an implicit function template. */
3949 parser
->auto_is_implicit_function_template_parm_p
= false;
3950 parser
->fully_implicit_function_template_p
= false;
3951 parser
->implicit_template_parms
= 0;
3952 parser
->implicit_template_scope
= 0;
3954 /* Allow constrained-type-specifiers. */
3955 parser
->prevent_constrained_type_specifiers
= 0;
3957 /* We haven't yet seen an 'extern "C"'. */
3958 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3963 /* Create a cp_lexer structure which will emit the tokens in CACHE
3964 and push it onto the parser's lexer stack. This is used for delayed
3965 parsing of in-class method bodies and default arguments, and should
3966 not be confused with tentative parsing. */
3968 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3970 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3971 lexer
->next
= parser
->lexer
;
3972 parser
->lexer
= lexer
;
3974 /* Move the current source position to that of the first token in the
3976 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3979 /* Pop the top lexer off the parser stack. This is never used for the
3980 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3982 cp_parser_pop_lexer (cp_parser
*parser
)
3984 cp_lexer
*lexer
= parser
->lexer
;
3985 parser
->lexer
= lexer
->next
;
3986 cp_lexer_destroy (lexer
);
3988 /* Put the current source position back where it was before this
3989 lexer was pushed. */
3990 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3993 /* Lexical conventions [gram.lex] */
3995 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3999 cp_parser_identifier (cp_parser
* parser
)
4003 /* Look for the identifier. */
4004 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4005 /* Return the value. */
4007 return cp_expr (token
->u
.value
, token
->location
);
4009 return error_mark_node
;
4012 /* Parse a sequence of adjacent string constants. Returns a
4013 TREE_STRING representing the combined, nul-terminated string
4014 constant. If TRANSLATE is true, translate the string to the
4015 execution character set. If WIDE_OK is true, a wide string is
4018 C++98 [lex.string] says that if a narrow string literal token is
4019 adjacent to a wide string literal token, the behavior is undefined.
4020 However, C99 6.4.5p4 says that this results in a wide string literal.
4021 We follow C99 here, for consistency with the C front end.
4023 This code is largely lifted from lex_string() in c-lex.c.
4025 FUTURE: ObjC++ will need to handle @-strings here. */
4027 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4028 bool lookup_udlit
= true)
4032 struct obstack str_ob
;
4033 struct obstack loc_ob
;
4034 cpp_string str
, istr
, *strs
;
4036 enum cpp_ttype type
, curr_type
;
4037 int have_suffix_p
= 0;
4039 tree suffix_id
= NULL_TREE
;
4040 bool curr_tok_is_userdef_p
= false;
4042 tok
= cp_lexer_peek_token (parser
->lexer
);
4043 if (!cp_parser_is_string_literal (tok
))
4045 cp_parser_error (parser
, "expected string-literal");
4046 return error_mark_node
;
4049 location_t loc
= tok
->location
;
4051 if (cpp_userdef_string_p (tok
->type
))
4053 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4054 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4055 curr_tok_is_userdef_p
= true;
4059 string_tree
= tok
->u
.value
;
4060 curr_type
= tok
->type
;
4064 /* Try to avoid the overhead of creating and destroying an obstack
4065 for the common case of just one string. */
4066 if (!cp_parser_is_string_literal
4067 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4069 cp_lexer_consume_token (parser
->lexer
);
4071 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4072 str
.len
= TREE_STRING_LENGTH (string_tree
);
4075 if (curr_tok_is_userdef_p
)
4077 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4079 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4082 curr_type
= tok
->type
;
4088 location_t last_tok_loc
= tok
->location
;
4089 gcc_obstack_init (&str_ob
);
4090 gcc_obstack_init (&loc_ob
);
4095 cp_lexer_consume_token (parser
->lexer
);
4097 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4098 str
.len
= TREE_STRING_LENGTH (string_tree
);
4100 if (curr_tok_is_userdef_p
)
4102 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4103 if (have_suffix_p
== 0)
4105 suffix_id
= curr_suffix_id
;
4108 else if (have_suffix_p
== 1
4109 && curr_suffix_id
!= suffix_id
)
4111 error ("inconsistent user-defined literal suffixes"
4112 " %qD and %qD in string literal",
4113 suffix_id
, curr_suffix_id
);
4116 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4119 curr_type
= tok
->type
;
4121 if (type
!= curr_type
)
4123 if (type
== CPP_STRING
)
4125 else if (curr_type
!= CPP_STRING
)
4127 rich_location
rich_loc (line_table
, tok
->location
);
4128 rich_loc
.add_range (last_tok_loc
);
4129 error_at (&rich_loc
,
4130 "unsupported non-standard concatenation "
4131 "of string literals");
4135 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4136 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4138 last_tok_loc
= tok
->location
;
4140 tok
= cp_lexer_peek_token (parser
->lexer
);
4141 if (cpp_userdef_string_p (tok
->type
))
4143 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4144 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4145 curr_tok_is_userdef_p
= true;
4149 string_tree
= tok
->u
.value
;
4150 curr_type
= tok
->type
;
4151 curr_tok_is_userdef_p
= false;
4154 while (cp_parser_is_string_literal (tok
));
4156 /* A string literal built by concatenation has its caret=start at
4157 the start of the initial string, and its finish at the finish of
4158 the final string literal. */
4159 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4161 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4164 if (type
!= CPP_STRING
&& !wide_ok
)
4166 cp_parser_error (parser
, "a wide string is invalid in this context");
4170 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4171 (parse_in
, strs
, count
, &istr
, type
))
4173 value
= build_string (istr
.len
, (const char *)istr
.text
);
4174 free (CONST_CAST (unsigned char *, istr
.text
));
4177 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4178 gcc_assert (g_string_concat_db
);
4179 g_string_concat_db
->record_string_concatenation (count
, locs
);
4186 case CPP_UTF8STRING
:
4187 TREE_TYPE (value
) = char_array_type_node
;
4190 TREE_TYPE (value
) = char16_array_type_node
;
4193 TREE_TYPE (value
) = char32_array_type_node
;
4196 TREE_TYPE (value
) = wchar_array_type_node
;
4200 value
= fix_string_type (value
);
4204 tree literal
= build_userdef_literal (suffix_id
, value
,
4205 OT_NONE
, NULL_TREE
);
4207 value
= cp_parser_userdef_string_literal (literal
);
4213 /* cpp_interpret_string has issued an error. */
4214 value
= error_mark_node
;
4218 obstack_free (&str_ob
, 0);
4219 obstack_free (&loc_ob
, 0);
4222 return cp_expr (value
, loc
);
4225 /* Look up a literal operator with the name and the exact arguments. */
4228 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4231 decl
= lookup_name (name
);
4232 if (!decl
|| !is_overloaded_fn (decl
))
4233 return error_mark_node
;
4235 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4240 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4241 if (parmtypes
!= NULL_TREE
)
4243 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4244 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4246 tree tparm
= TREE_VALUE (parmtypes
);
4247 tree targ
= TREE_TYPE ((*args
)[ix
]);
4248 bool ptr
= TYPE_PTR_P (tparm
);
4249 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4250 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4252 || !same_type_p (TREE_TYPE (tparm
),
4257 && ix
== vec_safe_length (args
)
4258 /* May be this should be sufficient_parms_p instead,
4259 depending on how exactly should user-defined literals
4260 work in presence of default arguments on the literal
4261 operator parameters. */
4262 && parmtypes
== void_list_node
)
4267 return error_mark_node
;
4270 /* Parse a user-defined char constant. Returns a call to a user-defined
4271 literal operator taking the character as an argument. */
4274 cp_parser_userdef_char_literal (cp_parser
*parser
)
4276 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4277 tree literal
= token
->u
.value
;
4278 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4279 tree value
= USERDEF_LITERAL_VALUE (literal
);
4280 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4283 /* Build up a call to the user-defined operator */
4284 /* Lookup the name we got back from the id-expression. */
4285 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4286 vec_safe_push (args
, value
);
4287 decl
= lookup_literal_operator (name
, args
);
4288 if (!decl
|| decl
== error_mark_node
)
4290 error ("unable to find character literal operator %qD with %qT argument",
4291 name
, TREE_TYPE (value
));
4292 release_tree_vector (args
);
4293 return error_mark_node
;
4295 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4296 release_tree_vector (args
);
4300 /* A subroutine of cp_parser_userdef_numeric_literal to
4301 create a char... template parameter pack from a string node. */
4304 make_char_string_pack (tree value
)
4307 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4308 const char *str
= TREE_STRING_POINTER (value
);
4309 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4310 tree argvec
= make_tree_vec (1);
4312 /* Fill in CHARVEC with all of the parameters. */
4313 charvec
= make_tree_vec (len
);
4314 for (i
= 0; i
< len
; ++i
)
4316 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4317 cpp_string in
= { 3, s
};
4318 cpp_string out
= { 0, 0 };
4319 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4321 gcc_assert (out
.len
== 2);
4322 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4326 /* Build the argument packs. */
4327 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4329 TREE_VEC_ELT (argvec
, 0) = argpack
;
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4338 make_string_pack (tree value
)
4341 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4342 const unsigned char *str
4343 = (const unsigned char *) TREE_STRING_POINTER (value
);
4344 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4345 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4346 tree argvec
= make_tree_vec (2);
4348 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4349 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4351 /* First template parm is character type. */
4352 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4354 /* Fill in CHARVEC with all of the parameters. */
4355 charvec
= make_tree_vec (len
);
4356 for (int i
= 0; i
< len
; ++i
)
4357 TREE_VEC_ELT (charvec
, i
)
4358 = double_int_to_tree (str_char_type_node
,
4359 double_int::from_buffer (str
+ i
* sz
, sz
));
4361 /* Build the argument packs. */
4362 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4364 TREE_VEC_ELT (argvec
, 1) = argpack
;
4369 /* Parse a user-defined numeric constant. returns a call to a user-defined
4370 literal operator. */
4373 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4375 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4376 tree literal
= token
->u
.value
;
4377 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4378 tree value
= USERDEF_LITERAL_VALUE (literal
);
4379 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4380 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4381 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4383 vec
<tree
, va_gc
> *args
;
4385 /* Look for a literal operator taking the exact type of numeric argument
4386 as the literal value. */
4387 args
= make_tree_vector ();
4388 vec_safe_push (args
, value
);
4389 decl
= lookup_literal_operator (name
, args
);
4390 if (decl
&& decl
!= error_mark_node
)
4392 result
= finish_call_expr (decl
, &args
, false, true,
4393 tf_warning_or_error
);
4395 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4397 warning_at (token
->location
, OPT_Woverflow
,
4398 "integer literal exceeds range of %qT type",
4399 long_long_unsigned_type_node
);
4404 warning_at (token
->location
, OPT_Woverflow
,
4405 "floating literal exceeds range of %qT type",
4406 long_double_type_node
);
4407 else if (overflow
< 0)
4408 warning_at (token
->location
, OPT_Woverflow
,
4409 "floating literal truncated to zero");
4412 release_tree_vector (args
);
4415 release_tree_vector (args
);
4417 /* If the numeric argument didn't work, look for a raw literal
4418 operator taking a const char* argument consisting of the number
4419 in string format. */
4420 args
= make_tree_vector ();
4421 vec_safe_push (args
, num_string
);
4422 decl
= lookup_literal_operator (name
, args
);
4423 if (decl
&& decl
!= error_mark_node
)
4425 result
= finish_call_expr (decl
, &args
, false, true,
4426 tf_warning_or_error
);
4427 release_tree_vector (args
);
4430 release_tree_vector (args
);
4432 /* If the raw literal didn't work, look for a non-type template
4433 function with parameter pack char.... Call the function with
4434 template parameter characters representing the number. */
4435 args
= make_tree_vector ();
4436 decl
= lookup_literal_operator (name
, args
);
4437 if (decl
&& decl
!= error_mark_node
)
4439 tree tmpl_args
= make_char_string_pack (num_string
);
4440 if (tmpl_args
== NULL_TREE
)
4442 error ("failed to translate literal to execution character set %qT",
4444 return error_mark_node
;
4446 decl
= lookup_template_function (decl
, tmpl_args
);
4447 result
= finish_call_expr (decl
, &args
, false, true,
4448 tf_warning_or_error
);
4449 release_tree_vector (args
);
4453 release_tree_vector (args
);
4455 /* In C++14 the standard library defines complex number suffixes that
4456 conflict with GNU extensions. Prefer them if <complex> is #included. */
4457 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4458 bool i14
= (cxx_dialect
> cxx11
4459 && (id_equal (suffix_id
, "i")
4460 || id_equal (suffix_id
, "if")
4461 || id_equal (suffix_id
, "il")));
4462 diagnostic_t kind
= DK_ERROR
;
4467 tree cxlit
= lookup_qualified_name (std_node
,
4468 get_identifier ("complex_literals"),
4470 if (cxlit
== error_mark_node
)
4472 /* No <complex>, so pedwarn and use GNU semantics. */
4474 opt
= OPT_Wpedantic
;
4479 = emit_diagnostic (kind
, input_location
, opt
,
4480 "unable to find numeric literal operator %qD", name
);
4483 /* Don't inform either. */;
4486 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4487 "(from <complex>) to enable the C++14 user-defined literal "
4490 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4491 "GNU built-in suffix");
4494 inform (token
->location
, "use -fext-numeric-literals "
4495 "to enable more built-in suffixes");
4497 if (kind
== DK_ERROR
)
4498 value
= error_mark_node
;
4501 /* Use the built-in semantics. */
4503 if (id_equal (suffix_id
, "i"))
4505 if (TREE_CODE (value
) == INTEGER_CST
)
4506 type
= integer_type_node
;
4508 type
= double_type_node
;
4510 else if (id_equal (suffix_id
, "if"))
4511 type
= float_type_node
;
4512 else /* if (id_equal (suffix_id, "il")) */
4513 type
= long_double_type_node
;
4515 value
= build_complex (build_complex_type (type
),
4516 fold_convert (type
, integer_zero_node
),
4517 fold_convert (type
, value
));
4520 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4521 /* Avoid repeated diagnostics. */
4522 token
->u
.value
= value
;
4526 /* Parse a user-defined string constant. Returns a call to a user-defined
4527 literal operator taking a character pointer and the length of the string
4531 cp_parser_userdef_string_literal (tree literal
)
4533 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4534 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4535 tree value
= USERDEF_LITERAL_VALUE (literal
);
4536 int len
= TREE_STRING_LENGTH (value
)
4537 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4539 vec
<tree
, va_gc
> *args
;
4541 /* Build up a call to the user-defined operator. */
4542 /* Lookup the name we got back from the id-expression. */
4543 args
= make_tree_vector ();
4544 vec_safe_push (args
, value
);
4545 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4546 decl
= lookup_literal_operator (name
, args
);
4548 if (decl
&& decl
!= error_mark_node
)
4550 result
= finish_call_expr (decl
, &args
, false, true,
4551 tf_warning_or_error
);
4552 release_tree_vector (args
);
4555 release_tree_vector (args
);
4557 /* Look for a template function with typename parameter CharT
4558 and parameter pack CharT... Call the function with
4559 template parameter characters representing the string. */
4560 args
= make_tree_vector ();
4561 decl
= lookup_literal_operator (name
, args
);
4562 if (decl
&& decl
!= error_mark_node
)
4564 tree tmpl_args
= make_string_pack (value
);
4565 decl
= lookup_template_function (decl
, tmpl_args
);
4566 result
= finish_call_expr (decl
, &args
, false, true,
4567 tf_warning_or_error
);
4568 release_tree_vector (args
);
4571 release_tree_vector (args
);
4573 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4574 name
, TREE_TYPE (value
), size_type_node
);
4575 return error_mark_node
;
4579 /* Basic concepts [gram.basic] */
4581 /* Parse a translation-unit.
4584 declaration-seq [opt] */
4587 cp_parser_translation_unit (cp_parser
* parser
)
4589 gcc_checking_assert (!cp_error_declarator
);
4591 /* Create the declarator obstack. */
4592 gcc_obstack_init (&declarator_obstack
);
4593 /* Create the error declarator. */
4594 cp_error_declarator
= make_declarator (cdk_error
);
4595 /* Create the empty parameter list. */
4596 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4598 /* Remember where the base of the declarator obstack lies. */
4599 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4601 bool implicit_extern_c
= false;
4605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4607 /* If we're entering or exiting a region that's implicitly
4608 extern "C", modify the lang context appropriately. */
4609 if (implicit_extern_c
4610 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4612 implicit_extern_c
= !implicit_extern_c
;
4613 if (implicit_extern_c
)
4614 push_lang_context (lang_name_c
);
4616 pop_lang_context ();
4619 if (token
->type
== CPP_EOF
)
4622 if (token
->type
== CPP_CLOSE_BRACE
)
4624 cp_parser_error (parser
, "expected declaration");
4625 cp_lexer_consume_token (parser
->lexer
);
4626 /* If the next token is now a `;', consume it. */
4627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4628 cp_lexer_consume_token (parser
->lexer
);
4631 cp_parser_toplevel_declaration (parser
);
4634 /* Get rid of the token array; we don't need it any more. */
4635 cp_lexer_destroy (parser
->lexer
);
4636 parser
->lexer
= NULL
;
4638 /* The EOF should have reset this. */
4639 gcc_checking_assert (!implicit_extern_c
);
4641 /* Make sure the declarator obstack was fully cleaned up. */
4642 gcc_assert (obstack_next_free (&declarator_obstack
)
4643 == declarator_obstack_base
);
4646 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4647 decltype context. */
4649 static inline tsubst_flags_t
4650 complain_flags (bool decltype_p
)
4652 tsubst_flags_t complain
= tf_warning_or_error
;
4654 complain
|= tf_decltype
;
4658 /* We're about to parse a collection of statements. If we're currently
4659 parsing tentatively, set up a firewall so that any nested
4660 cp_parser_commit_to_tentative_parse won't affect the current context. */
4662 static cp_token_position
4663 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4665 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4668 cp_parser_parse_tentatively (parser
);
4669 cp_parser_commit_to_topmost_tentative_parse (parser
);
4670 return cp_lexer_token_position (parser
->lexer
, false);
4673 /* We've finished parsing the collection of statements. Wrap up the
4674 firewall and replace the relevant tokens with the parsed form. */
4677 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4683 /* Finish the firewall level. */
4684 cp_parser_parse_definitely (parser
);
4685 /* And remember the result of the parse for when we try again. */
4686 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4687 token
->type
= CPP_PREPARSED_EXPR
;
4688 token
->u
.value
= expr
;
4689 token
->keyword
= RID_MAX
;
4690 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4693 /* Like the above functions, but let the user modify the tokens. Used by
4694 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4695 later parses, so it makes sense to localize the effects of
4696 cp_parser_commit_to_tentative_parse. */
4698 struct tentative_firewall
4703 tentative_firewall (cp_parser
*p
): parser(p
)
4705 /* If we're currently parsing tentatively, start a committed level as a
4706 firewall and then an inner tentative parse. */
4707 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4709 cp_parser_parse_tentatively (parser
);
4710 cp_parser_commit_to_topmost_tentative_parse (parser
);
4711 cp_parser_parse_tentatively (parser
);
4715 ~tentative_firewall()
4719 /* Finish the inner tentative parse and the firewall, propagating any
4720 uncommitted error state to the outer tentative parse. */
4721 bool err
= cp_parser_error_occurred (parser
);
4722 cp_parser_parse_definitely (parser
);
4723 cp_parser_parse_definitely (parser
);
4725 cp_parser_simulate_error (parser
);
4730 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4731 This class is for tracking such a matching pair of symbols.
4732 In particular, it tracks the location of the first token,
4733 so that if the second token is missing, we can highlight the
4734 location of the first token when notifying the user about the
4737 template <typename traits_t
>
4741 /* token_pair's ctor. */
4742 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4744 /* If the next token is the opening symbol for this pair, consume it and
4746 Otherwise, issue an error and return false.
4747 In either case, record the location of the opening token. */
4749 bool require_open (cp_parser
*parser
)
4751 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4752 return cp_parser_require (parser
, traits_t::open_token_type
,
4753 traits_t::required_token_open
);
4756 /* Consume the next token from PARSER, recording its location as
4757 that of the opening token within the pair. */
4759 cp_token
* consume_open (cp_parser
*parser
)
4761 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4762 gcc_assert (tok
->type
== traits_t::open_token_type
);
4763 m_open_loc
= tok
->location
;
4767 /* If the next token is the closing symbol for this pair, consume it
4769 Otherwise, issue an error, highlighting the location of the
4770 corresponding opening token, and return NULL. */
4772 cp_token
*require_close (cp_parser
*parser
) const
4774 return cp_parser_require (parser
, traits_t::close_token_type
,
4775 traits_t::required_token_close
,
4780 location_t m_open_loc
;
4783 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4785 struct matching_paren_traits
4787 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4788 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4789 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4790 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4793 /* "matching_parens" is a token_pair<T> class for tracking matching
4794 pairs of parentheses. */
4796 typedef token_pair
<matching_paren_traits
> matching_parens
;
4798 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4800 struct matching_brace_traits
4802 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4803 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4804 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4805 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4808 /* "matching_braces" is a token_pair<T> class for tracking matching
4811 typedef token_pair
<matching_brace_traits
> matching_braces
;
4814 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4815 enclosing parentheses. */
4818 cp_parser_statement_expr (cp_parser
*parser
)
4820 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4822 /* Consume the '('. */
4823 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4824 matching_parens parens
;
4825 parens
.consume_open (parser
);
4826 /* Start the statement-expression. */
4827 tree expr
= begin_stmt_expr ();
4828 /* Parse the compound-statement. */
4829 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4831 expr
= finish_stmt_expr (expr
, false);
4832 /* Consume the ')'. */
4833 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4834 if (!parens
.require_close (parser
))
4835 cp_parser_skip_to_end_of_statement (parser
);
4837 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4838 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4839 return cp_expr (expr
, combined_loc
);
4842 /* Expressions [gram.expr] */
4844 /* Parse a fold-operator.
4847 - * / % ^ & | = < > << >>
4848 = -= *= /= %= ^= &= |= <<= >>=
4849 == != <= >= && || , .* ->*
4851 This returns the tree code corresponding to the matched operator
4852 as an int. When the current token matches a compound assignment
4853 opertor, the resulting tree code is the negative value of the
4854 non-assignment operator. */
4857 cp_parser_fold_operator (cp_token
*token
)
4859 switch (token
->type
)
4861 case CPP_PLUS
: return PLUS_EXPR
;
4862 case CPP_MINUS
: return MINUS_EXPR
;
4863 case CPP_MULT
: return MULT_EXPR
;
4864 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4865 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4866 case CPP_XOR
: return BIT_XOR_EXPR
;
4867 case CPP_AND
: return BIT_AND_EXPR
;
4868 case CPP_OR
: return BIT_IOR_EXPR
;
4869 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4870 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4872 case CPP_EQ
: return -NOP_EXPR
;
4873 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4874 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4875 case CPP_MULT_EQ
: return -MULT_EXPR
;
4876 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4877 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4878 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4879 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4880 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4881 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4882 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4884 case CPP_EQ_EQ
: return EQ_EXPR
;
4885 case CPP_NOT_EQ
: return NE_EXPR
;
4886 case CPP_LESS
: return LT_EXPR
;
4887 case CPP_GREATER
: return GT_EXPR
;
4888 case CPP_LESS_EQ
: return LE_EXPR
;
4889 case CPP_GREATER_EQ
: return GE_EXPR
;
4891 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4892 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4894 case CPP_COMMA
: return COMPOUND_EXPR
;
4896 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4897 case CPP_DEREF_STAR
: return MEMBER_REF
;
4899 default: return ERROR_MARK
;
4903 /* Returns true if CODE indicates a binary expression, which is not allowed in
4904 the LHS of a fold-expression. More codes will need to be added to use this
4905 function in other contexts. */
4908 is_binary_op (tree_code code
)
4913 case POINTER_PLUS_EXPR
:
4916 case TRUNC_DIV_EXPR
:
4917 case TRUNC_MOD_EXPR
:
4933 case TRUTH_ANDIF_EXPR
:
4934 case TRUTH_ORIF_EXPR
:
4947 /* If the next token is a suitable fold operator, consume it and return as
4948 the function above. */
4951 cp_parser_fold_operator (cp_parser
*parser
)
4953 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4954 int code
= cp_parser_fold_operator (token
);
4955 if (code
!= ERROR_MARK
)
4956 cp_lexer_consume_token (parser
->lexer
);
4960 /* Parse a fold-expression.
4963 ( ... folding-operator cast-expression)
4964 ( cast-expression folding-operator ... )
4965 ( cast-expression folding operator ... folding-operator cast-expression)
4967 Note that the '(' and ')' are matched in primary expression. */
4970 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4977 cp_lexer_consume_token (parser
->lexer
);
4978 int op
= cp_parser_fold_operator (parser
);
4979 if (op
== ERROR_MARK
)
4981 cp_parser_error (parser
, "expected binary operator");
4982 return error_mark_node
;
4985 tree expr
= cp_parser_cast_expression (parser
, false, false,
4987 if (expr
== error_mark_node
)
4988 return error_mark_node
;
4989 return finish_left_unary_fold_expr (expr
, op
);
4992 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4993 int op
= cp_parser_fold_operator (parser
);
4994 if (op
== ERROR_MARK
)
4996 cp_parser_error (parser
, "expected binary operator");
4997 return error_mark_node
;
5000 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5002 cp_parser_error (parser
, "expected ...");
5003 return error_mark_node
;
5005 cp_lexer_consume_token (parser
->lexer
);
5007 /* The operands of a fold-expression are cast-expressions, so binary or
5008 conditional expressions are not allowed. We check this here to avoid
5009 tentative parsing. */
5010 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5011 /* OK, the expression was parenthesized. */;
5012 else if (is_binary_op (TREE_CODE (expr1
)))
5013 error_at (location_of (expr1
),
5014 "binary expression in operand of fold-expression");
5015 else if (TREE_CODE (expr1
) == COND_EXPR
5016 || (REFERENCE_REF_P (expr1
)
5017 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5018 error_at (location_of (expr1
),
5019 "conditional expression in operand of fold-expression");
5022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5023 return finish_right_unary_fold_expr (expr1
, op
);
5025 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5027 cp_parser_error (parser
, "mismatched operator in fold-expression");
5028 return error_mark_node
;
5030 cp_lexer_consume_token (parser
->lexer
);
5032 // Binary left or right fold.
5033 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5034 if (expr2
== error_mark_node
)
5035 return error_mark_node
;
5036 return finish_binary_fold_expr (expr1
, expr2
, op
);
5039 /* Parse a primary-expression.
5046 lambda-expression (C++11)
5051 ( compound-statement )
5052 __builtin_va_arg ( assignment-expression , type-id )
5053 __builtin_offsetof ( type-id , offsetof-expression )
5056 __has_nothrow_assign ( type-id )
5057 __has_nothrow_constructor ( type-id )
5058 __has_nothrow_copy ( type-id )
5059 __has_trivial_assign ( type-id )
5060 __has_trivial_constructor ( type-id )
5061 __has_trivial_copy ( type-id )
5062 __has_trivial_destructor ( type-id )
5063 __has_virtual_destructor ( type-id )
5064 __is_abstract ( type-id )
5065 __is_base_of ( type-id , type-id )
5066 __is_class ( type-id )
5067 __is_empty ( type-id )
5068 __is_enum ( type-id )
5069 __is_final ( type-id )
5070 __is_literal_type ( type-id )
5071 __is_pod ( type-id )
5072 __is_polymorphic ( type-id )
5073 __is_std_layout ( type-id )
5074 __is_trivial ( type-id )
5075 __is_union ( type-id )
5077 Objective-C++ Extension:
5085 ADDRESS_P is true iff this expression was immediately preceded by
5086 "&" and therefore might denote a pointer-to-member. CAST_P is true
5087 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5088 true iff this expression is a template argument.
5090 Returns a representation of the expression. Upon return, *IDK
5091 indicates what kind of id-expression (if any) was present. */
5094 cp_parser_primary_expression (cp_parser
*parser
,
5097 bool template_arg_p
,
5101 cp_token
*token
= NULL
;
5103 /* Assume the primary expression is not an id-expression. */
5104 *idk
= CP_ID_KIND_NONE
;
5106 /* Peek at the next token. */
5107 token
= cp_lexer_peek_token (parser
->lexer
);
5108 switch ((int) token
->type
)
5117 user-defined-literal */
5124 case CPP_PREPARSED_EXPR
:
5125 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5126 return cp_parser_userdef_numeric_literal (parser
);
5127 token
= cp_lexer_consume_token (parser
->lexer
);
5128 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5130 error_at (token
->location
,
5131 "fixed-point types not supported in C++");
5132 return error_mark_node
;
5134 /* Floating-point literals are only allowed in an integral
5135 constant expression if they are cast to an integral or
5136 enumeration type. */
5137 if (TREE_CODE (token
->u
.value
) == REAL_CST
5138 && parser
->integral_constant_expression_p
5141 /* CAST_P will be set even in invalid code like "int(2.7 +
5142 ...)". Therefore, we have to check that the next token
5143 is sure to end the cast. */
5146 cp_token
*next_token
;
5148 next_token
= cp_lexer_peek_token (parser
->lexer
);
5149 if (/* The comma at the end of an
5150 enumerator-definition. */
5151 next_token
->type
!= CPP_COMMA
5152 /* The curly brace at the end of an enum-specifier. */
5153 && next_token
->type
!= CPP_CLOSE_BRACE
5154 /* The end of a statement. */
5155 && next_token
->type
!= CPP_SEMICOLON
5156 /* The end of the cast-expression. */
5157 && next_token
->type
!= CPP_CLOSE_PAREN
5158 /* The end of an array bound. */
5159 && next_token
->type
!= CPP_CLOSE_SQUARE
5160 /* The closing ">" in a template-argument-list. */
5161 && (next_token
->type
!= CPP_GREATER
5162 || parser
->greater_than_is_operator_p
)
5163 /* C++0x only: A ">>" treated like two ">" tokens,
5164 in a template-argument-list. */
5165 && (next_token
->type
!= CPP_RSHIFT
5166 || (cxx_dialect
== cxx98
)
5167 || parser
->greater_than_is_operator_p
))
5171 /* If we are within a cast, then the constraint that the
5172 cast is to an integral or enumeration type will be
5173 checked at that point. If we are not within a cast, then
5174 this code is invalid. */
5176 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5178 return cp_expr (token
->u
.value
, token
->location
);
5180 case CPP_CHAR_USERDEF
:
5181 case CPP_CHAR16_USERDEF
:
5182 case CPP_CHAR32_USERDEF
:
5183 case CPP_WCHAR_USERDEF
:
5184 case CPP_UTF8CHAR_USERDEF
:
5185 return cp_parser_userdef_char_literal (parser
);
5191 case CPP_UTF8STRING
:
5192 case CPP_STRING_USERDEF
:
5193 case CPP_STRING16_USERDEF
:
5194 case CPP_STRING32_USERDEF
:
5195 case CPP_WSTRING_USERDEF
:
5196 case CPP_UTF8STRING_USERDEF
:
5197 /* ??? Should wide strings be allowed when parser->translate_strings_p
5198 is false (i.e. in attributes)? If not, we can kill the third
5199 argument to cp_parser_string_literal. */
5200 return cp_parser_string_literal (parser
,
5201 parser
->translate_strings_p
,
5204 case CPP_OPEN_PAREN
:
5205 /* If we see `( { ' then we are looking at the beginning of
5206 a GNU statement-expression. */
5207 if (cp_parser_allow_gnu_extensions_p (parser
)
5208 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5210 /* Statement-expressions are not allowed by the standard. */
5211 pedwarn (token
->location
, OPT_Wpedantic
,
5212 "ISO C++ forbids braced-groups within expressions");
5214 /* And they're not allowed outside of a function-body; you
5215 cannot, for example, write:
5217 int i = ({ int j = 3; j + 1; });
5219 at class or namespace scope. */
5220 if (!parser
->in_function_body
5221 || parser
->in_template_argument_list_p
)
5223 error_at (token
->location
,
5224 "statement-expressions are not allowed outside "
5225 "functions nor in template-argument lists");
5226 cp_parser_skip_to_end_of_block_or_statement (parser
);
5227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5228 cp_lexer_consume_token (parser
->lexer
);
5229 return error_mark_node
;
5232 return cp_parser_statement_expr (parser
);
5234 /* Otherwise it's a normal parenthesized expression. */
5237 bool saved_greater_than_is_operator_p
;
5239 location_t open_paren_loc
= token
->location
;
5241 /* Consume the `('. */
5242 matching_parens parens
;
5243 parens
.consume_open (parser
);
5244 /* Within a parenthesized expression, a `>' token is always
5245 the greater-than operator. */
5246 saved_greater_than_is_operator_p
5247 = parser
->greater_than_is_operator_p
;
5248 parser
->greater_than_is_operator_p
= true;
5250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5251 /* Left fold expression. */
5254 /* Parse the parenthesized expression. */
5255 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5257 token
= cp_lexer_peek_token (parser
->lexer
);
5258 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5260 expr
= cp_parser_fold_expression (parser
, expr
);
5261 if (expr
!= error_mark_node
5262 && cxx_dialect
< cxx17
5263 && !in_system_header_at (input_location
))
5264 pedwarn (input_location
, 0, "fold-expressions only available "
5265 "with -std=c++17 or -std=gnu++17");
5268 /* Let the front end know that this expression was
5269 enclosed in parentheses. This matters in case, for
5270 example, the expression is of the form `A::B', since
5271 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5273 expr
= finish_parenthesized_expr (expr
);
5275 /* DR 705: Wrapping an unqualified name in parentheses
5276 suppresses arg-dependent lookup. We want to pass back
5277 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5278 (c++/37862), but none of the others. */
5279 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5280 *idk
= CP_ID_KIND_NONE
;
5282 /* The `>' token might be the end of a template-id or
5283 template-parameter-list now. */
5284 parser
->greater_than_is_operator_p
5285 = saved_greater_than_is_operator_p
;
5287 /* Consume the `)'. */
5288 token
= cp_lexer_peek_token (parser
->lexer
);
5289 location_t close_paren_loc
= token
->location
;
5290 expr
.set_range (open_paren_loc
, close_paren_loc
);
5291 if (!parens
.require_close (parser
)
5292 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5293 cp_parser_skip_to_end_of_statement (parser
);
5298 case CPP_OPEN_SQUARE
:
5300 if (c_dialect_objc ())
5302 /* We might have an Objective-C++ message. */
5303 cp_parser_parse_tentatively (parser
);
5304 tree msg
= cp_parser_objc_message_expression (parser
);
5305 /* If that works out, we're done ... */
5306 if (cp_parser_parse_definitely (parser
))
5308 /* ... else, fall though to see if it's a lambda. */
5310 cp_expr lam
= cp_parser_lambda_expression (parser
);
5311 /* Don't warn about a failed tentative parse. */
5312 if (cp_parser_error_occurred (parser
))
5313 return error_mark_node
;
5314 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5318 case CPP_OBJC_STRING
:
5319 if (c_dialect_objc ())
5320 /* We have an Objective-C++ string literal. */
5321 return cp_parser_objc_expression (parser
);
5322 cp_parser_error (parser
, "expected primary-expression");
5323 return error_mark_node
;
5326 switch (token
->keyword
)
5328 /* These two are the boolean literals. */
5330 cp_lexer_consume_token (parser
->lexer
);
5331 return cp_expr (boolean_true_node
, token
->location
);
5333 cp_lexer_consume_token (parser
->lexer
);
5334 return cp_expr (boolean_false_node
, token
->location
);
5336 /* The `__null' literal. */
5338 cp_lexer_consume_token (parser
->lexer
);
5339 return cp_expr (null_node
, token
->location
);
5341 /* The `nullptr' literal. */
5343 cp_lexer_consume_token (parser
->lexer
);
5344 return cp_expr (nullptr_node
, token
->location
);
5346 /* Recognize the `this' keyword. */
5348 cp_lexer_consume_token (parser
->lexer
);
5349 if (parser
->local_variables_forbidden_p
)
5351 error_at (token
->location
,
5352 "%<this%> may not be used in this context");
5353 return error_mark_node
;
5355 /* Pointers cannot appear in constant-expressions. */
5356 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5357 return error_mark_node
;
5358 return cp_expr (finish_this_expr (), token
->location
);
5360 /* The `operator' keyword can be the beginning of an
5365 case RID_FUNCTION_NAME
:
5366 case RID_PRETTY_FUNCTION_NAME
:
5367 case RID_C99_FUNCTION_NAME
:
5369 non_integral_constant name
;
5371 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5372 __func__ are the names of variables -- but they are
5373 treated specially. Therefore, they are handled here,
5374 rather than relying on the generic id-expression logic
5375 below. Grammatically, these names are id-expressions.
5377 Consume the token. */
5378 token
= cp_lexer_consume_token (parser
->lexer
);
5380 switch (token
->keyword
)
5382 case RID_FUNCTION_NAME
:
5383 name
= NIC_FUNC_NAME
;
5385 case RID_PRETTY_FUNCTION_NAME
:
5386 name
= NIC_PRETTY_FUNC
;
5388 case RID_C99_FUNCTION_NAME
:
5389 name
= NIC_C99_FUNC
;
5395 if (cp_parser_non_integral_constant_expression (parser
, name
))
5396 return error_mark_node
;
5398 /* Look up the name. */
5399 return finish_fname (token
->u
.value
);
5406 source_location type_location
;
5407 location_t start_loc
5408 = cp_lexer_peek_token (parser
->lexer
)->location
;
5409 /* The `__builtin_va_arg' construct is used to handle
5410 `va_arg'. Consume the `__builtin_va_arg' token. */
5411 cp_lexer_consume_token (parser
->lexer
);
5412 /* Look for the opening `('. */
5413 matching_parens parens
;
5414 parens
.require_open (parser
);
5415 /* Now, parse the assignment-expression. */
5416 expression
= cp_parser_assignment_expression (parser
);
5417 /* Look for the `,'. */
5418 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5419 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5420 /* Parse the type-id. */
5422 type_id_in_expr_sentinel
s (parser
);
5423 type
= cp_parser_type_id (parser
);
5425 /* Look for the closing `)'. */
5426 location_t finish_loc
5427 = cp_lexer_peek_token (parser
->lexer
)->location
;
5428 parens
.require_close (parser
);
5429 /* Using `va_arg' in a constant-expression is not
5431 if (cp_parser_non_integral_constant_expression (parser
,
5433 return error_mark_node
;
5434 /* Construct a location of the form:
5435 __builtin_va_arg (v, int)
5436 ~~~~~~~~~~~~~~~~~~~~~^~~~
5437 with the caret at the type, ranging from the start of the
5438 "__builtin_va_arg" token to the close paren. */
5439 location_t combined_loc
5440 = make_location (type_location
, start_loc
, finish_loc
);
5441 return build_x_va_arg (combined_loc
, expression
, type
);
5445 return cp_parser_builtin_offsetof (parser
);
5447 case RID_HAS_NOTHROW_ASSIGN
:
5448 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5449 case RID_HAS_NOTHROW_COPY
:
5450 case RID_HAS_TRIVIAL_ASSIGN
:
5451 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5452 case RID_HAS_TRIVIAL_COPY
:
5453 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5454 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5455 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5456 case RID_IS_ABSTRACT
:
5457 case RID_IS_AGGREGATE
:
5458 case RID_IS_BASE_OF
:
5463 case RID_IS_LITERAL_TYPE
:
5465 case RID_IS_POLYMORPHIC
:
5466 case RID_IS_SAME_AS
:
5467 case RID_IS_STD_LAYOUT
:
5468 case RID_IS_TRIVIAL
:
5469 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5470 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5471 case RID_IS_TRIVIALLY_COPYABLE
:
5473 case RID_IS_ASSIGNABLE
:
5474 case RID_IS_CONSTRUCTIBLE
:
5475 return cp_parser_trait_expr (parser
, token
->keyword
);
5479 return cp_parser_requires_expression (parser
);
5481 /* Objective-C++ expressions. */
5483 case RID_AT_PROTOCOL
:
5484 case RID_AT_SELECTOR
:
5485 return cp_parser_objc_expression (parser
);
5488 if (parser
->in_function_body
5489 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5492 error_at (token
->location
,
5493 "a template declaration cannot appear at block scope");
5494 cp_parser_skip_to_end_of_block_or_statement (parser
);
5495 return error_mark_node
;
5499 cp_parser_error (parser
, "expected primary-expression");
5500 return error_mark_node
;
5503 /* An id-expression can start with either an identifier, a
5504 `::' as the beginning of a qualified-id, or the "operator"
5508 case CPP_TEMPLATE_ID
:
5509 case CPP_NESTED_NAME_SPECIFIER
:
5512 cp_expr id_expression
;
5514 const char *error_msg
;
5517 cp_token
*id_expr_token
;
5519 /* Parse the id-expression. */
5521 = cp_parser_id_expression (parser
,
5522 /*template_keyword_p=*/false,
5523 /*check_dependency_p=*/true,
5525 /*declarator_p=*/false,
5526 /*optional_p=*/false);
5527 if (id_expression
== error_mark_node
)
5528 return error_mark_node
;
5529 id_expr_token
= token
;
5530 token
= cp_lexer_peek_token (parser
->lexer
);
5531 done
= (token
->type
!= CPP_OPEN_SQUARE
5532 && token
->type
!= CPP_OPEN_PAREN
5533 && token
->type
!= CPP_DOT
5534 && token
->type
!= CPP_DEREF
5535 && token
->type
!= CPP_PLUS_PLUS
5536 && token
->type
!= CPP_MINUS_MINUS
);
5537 /* If we have a template-id, then no further lookup is
5538 required. If the template-id was for a template-class, we
5539 will sometimes have a TYPE_DECL at this point. */
5540 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5541 || TREE_CODE (id_expression
) == TYPE_DECL
)
5542 decl
= id_expression
;
5543 /* Look up the name. */
5546 tree ambiguous_decls
;
5548 /* If we already know that this lookup is ambiguous, then
5549 we've already issued an error message; there's no reason
5551 if (id_expr_token
->type
== CPP_NAME
5552 && id_expr_token
->error_reported
)
5554 cp_parser_simulate_error (parser
);
5555 return error_mark_node
;
5558 decl
= cp_parser_lookup_name (parser
, id_expression
,
5561 /*is_namespace=*/false,
5562 /*check_dependency=*/true,
5564 id_expr_token
->location
);
5565 /* If the lookup was ambiguous, an error will already have
5567 if (ambiguous_decls
)
5568 return error_mark_node
;
5570 /* In Objective-C++, we may have an Objective-C 2.0
5571 dot-syntax for classes here. */
5572 if (c_dialect_objc ()
5573 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5574 && TREE_CODE (decl
) == TYPE_DECL
5575 && objc_is_class_name (decl
))
5578 cp_lexer_consume_token (parser
->lexer
);
5579 component
= cp_parser_identifier (parser
);
5580 if (component
== error_mark_node
)
5581 return error_mark_node
;
5583 tree result
= objc_build_class_component_ref (id_expression
,
5585 /* Build a location of the form:
5588 with caret at the start of the component name (at
5589 input_location), ranging from the start of the id_expression
5590 to the end of the component name. */
5591 location_t combined_loc
5592 = make_location (input_location
, id_expression
.get_start (),
5593 get_finish (input_location
));
5594 protected_set_expr_location (result
, combined_loc
);
5598 /* In Objective-C++, an instance variable (ivar) may be preferred
5599 to whatever cp_parser_lookup_name() found.
5600 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5601 rest of c-family, we have to do a little extra work to preserve
5602 any location information in cp_expr "decl". Given that
5603 objc_lookup_ivar is implemented in "c-family" and "objc", we
5604 have a trip through the pure "tree" type, rather than cp_expr.
5605 Naively copying it back to "decl" would implicitly give the
5606 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5607 store an EXPR_LOCATION. Hence we only update "decl" (and
5608 hence its location_t) if we get back a different tree node. */
5609 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5611 if (decl_tree
!= decl
.get_value ())
5612 decl
= cp_expr (decl_tree
);
5614 /* If name lookup gives us a SCOPE_REF, then the
5615 qualifying scope was dependent. */
5616 if (TREE_CODE (decl
) == SCOPE_REF
)
5618 /* At this point, we do not know if DECL is a valid
5619 integral constant expression. We assume that it is
5620 in fact such an expression, so that code like:
5622 template <int N> struct A {
5626 is accepted. At template-instantiation time, we
5627 will check that B<N>::i is actually a constant. */
5630 /* Check to see if DECL is a local variable in a context
5631 where that is forbidden. */
5632 if (parser
->local_variables_forbidden_p
5633 && local_variable_p (decl
))
5635 error_at (id_expr_token
->location
,
5636 "local variable %qD may not appear in this context",
5638 return error_mark_node
;
5642 if (processing_template_decl
)
5643 if (tree fns
= maybe_get_fns (decl
))
5644 /* It's too difficult to mark ths in all the places where
5645 we know for sure we need to keep the lookup, so do it
5646 now. The cost is extra GC to recycle the lookups
5647 resolved at parse time. */
5650 decl
= (finish_id_expression
5651 (id_expression
, decl
, parser
->scope
,
5653 parser
->integral_constant_expression_p
,
5654 parser
->allow_non_integral_constant_expression_p
,
5655 &parser
->non_integral_constant_expression_p
,
5656 template_p
, done
, address_p
,
5659 id_expression
.get_location ()));
5661 cp_parser_error (parser
, error_msg
);
5662 decl
.set_location (id_expr_token
->location
);
5666 /* Anything else is an error. */
5668 cp_parser_error (parser
, "expected primary-expression");
5669 return error_mark_node
;
5673 static inline cp_expr
5674 cp_parser_primary_expression (cp_parser
*parser
,
5677 bool template_arg_p
,
5680 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5681 /*decltype*/false, idk
);
5684 /* Parse an id-expression.
5691 :: [opt] nested-name-specifier template [opt] unqualified-id
5693 :: operator-function-id
5696 Return a representation of the unqualified portion of the
5697 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5698 a `::' or nested-name-specifier.
5700 Often, if the id-expression was a qualified-id, the caller will
5701 want to make a SCOPE_REF to represent the qualified-id. This
5702 function does not do this in order to avoid wastefully creating
5703 SCOPE_REFs when they are not required.
5705 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5708 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5709 uninstantiated templates.
5711 If *TEMPLATE_P is non-NULL, it is set to true iff the
5712 `template' keyword is used to explicitly indicate that the entity
5713 named is a template.
5715 If DECLARATOR_P is true, the id-expression is appearing as part of
5716 a declarator, rather than as part of an expression. */
5719 cp_parser_id_expression (cp_parser
*parser
,
5720 bool template_keyword_p
,
5721 bool check_dependency_p
,
5726 bool global_scope_p
;
5727 bool nested_name_specifier_p
;
5729 /* Assume the `template' keyword was not used. */
5731 *template_p
= template_keyword_p
;
5733 /* Look for the optional `::' operator. */
5735 = (!template_keyword_p
5736 && (cp_parser_global_scope_opt (parser
,
5737 /*current_scope_valid_p=*/false)
5740 /* Look for the optional nested-name-specifier. */
5741 nested_name_specifier_p
5742 = (cp_parser_nested_name_specifier_opt (parser
,
5743 /*typename_keyword_p=*/false,
5750 /* If there is a nested-name-specifier, then we are looking at
5751 the first qualified-id production. */
5752 if (nested_name_specifier_p
)
5755 tree saved_object_scope
;
5756 tree saved_qualifying_scope
;
5757 cp_expr unqualified_id
;
5760 /* See if the next token is the `template' keyword. */
5762 template_p
= &is_template
;
5763 *template_p
= cp_parser_optional_template_keyword (parser
);
5764 /* Name lookup we do during the processing of the
5765 unqualified-id might obliterate SCOPE. */
5766 saved_scope
= parser
->scope
;
5767 saved_object_scope
= parser
->object_scope
;
5768 saved_qualifying_scope
= parser
->qualifying_scope
;
5769 /* Process the final unqualified-id. */
5770 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5773 /*optional_p=*/false);
5774 /* Restore the SAVED_SCOPE for our caller. */
5775 parser
->scope
= saved_scope
;
5776 parser
->object_scope
= saved_object_scope
;
5777 parser
->qualifying_scope
= saved_qualifying_scope
;
5779 return unqualified_id
;
5781 /* Otherwise, if we are in global scope, then we are looking at one
5782 of the other qualified-id productions. */
5783 else if (global_scope_p
)
5788 /* Peek at the next token. */
5789 token
= cp_lexer_peek_token (parser
->lexer
);
5791 /* If it's an identifier, and the next token is not a "<", then
5792 we can avoid the template-id case. This is an optimization
5793 for this common case. */
5794 if (token
->type
== CPP_NAME
5795 && !cp_parser_nth_token_starts_template_argument_list_p
5797 return cp_parser_identifier (parser
);
5799 cp_parser_parse_tentatively (parser
);
5800 /* Try a template-id. */
5801 id
= cp_parser_template_id (parser
,
5802 /*template_keyword_p=*/false,
5803 /*check_dependency_p=*/true,
5806 /* If that worked, we're done. */
5807 if (cp_parser_parse_definitely (parser
))
5810 /* Peek at the next token. (Changes in the token buffer may
5811 have invalidated the pointer obtained above.) */
5812 token
= cp_lexer_peek_token (parser
->lexer
);
5814 switch (token
->type
)
5817 return cp_parser_identifier (parser
);
5820 if (token
->keyword
== RID_OPERATOR
)
5821 return cp_parser_operator_function_id (parser
);
5825 cp_parser_error (parser
, "expected id-expression");
5826 return error_mark_node
;
5830 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5831 /*check_dependency_p=*/true,
5836 /* Parse an unqualified-id.
5840 operator-function-id
5841 conversion-function-id
5845 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5846 keyword, in a construct like `A::template ...'.
5848 Returns a representation of unqualified-id. For the `identifier'
5849 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5850 production a BIT_NOT_EXPR is returned; the operand of the
5851 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5852 other productions, see the documentation accompanying the
5853 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5854 names are looked up in uninstantiated templates. If DECLARATOR_P
5855 is true, the unqualified-id is appearing as part of a declarator,
5856 rather than as part of an expression. */
5859 cp_parser_unqualified_id (cp_parser
* parser
,
5860 bool template_keyword_p
,
5861 bool check_dependency_p
,
5867 /* Peek at the next token. */
5868 token
= cp_lexer_peek_token (parser
->lexer
);
5870 switch ((int) token
->type
)
5876 /* We don't know yet whether or not this will be a
5878 cp_parser_parse_tentatively (parser
);
5879 /* Try a template-id. */
5880 id
= cp_parser_template_id (parser
, template_keyword_p
,
5884 /* If it worked, we're done. */
5885 if (cp_parser_parse_definitely (parser
))
5887 /* Otherwise, it's an ordinary identifier. */
5888 return cp_parser_identifier (parser
);
5891 case CPP_TEMPLATE_ID
:
5892 return cp_parser_template_id (parser
, template_keyword_p
,
5900 tree qualifying_scope
;
5905 /* Consume the `~' token. */
5906 cp_lexer_consume_token (parser
->lexer
);
5907 /* Parse the class-name. The standard, as written, seems to
5910 template <typename T> struct S { ~S (); };
5911 template <typename T> S<T>::~S() {}
5913 is invalid, since `~' must be followed by a class-name, but
5914 `S<T>' is dependent, and so not known to be a class.
5915 That's not right; we need to look in uninstantiated
5916 templates. A further complication arises from:
5918 template <typename T> void f(T t) {
5922 Here, it is not possible to look up `T' in the scope of `T'
5923 itself. We must look in both the current scope, and the
5924 scope of the containing complete expression.
5926 Yet another issue is:
5935 The standard does not seem to say that the `S' in `~S'
5936 should refer to the type `S' and not the data member
5939 /* DR 244 says that we look up the name after the "~" in the
5940 same scope as we looked up the qualifying name. That idea
5941 isn't fully worked out; it's more complicated than that. */
5942 scope
= parser
->scope
;
5943 object_scope
= parser
->object_scope
;
5944 qualifying_scope
= parser
->qualifying_scope
;
5946 /* Check for invalid scopes. */
5947 if (scope
== error_mark_node
)
5949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5950 cp_lexer_consume_token (parser
->lexer
);
5951 return error_mark_node
;
5953 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5955 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5956 error_at (token
->location
,
5957 "scope %qT before %<~%> is not a class-name",
5959 cp_parser_simulate_error (parser
);
5960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5961 cp_lexer_consume_token (parser
->lexer
);
5962 return error_mark_node
;
5964 gcc_assert (!scope
|| TYPE_P (scope
));
5966 /* If the name is of the form "X::~X" it's OK even if X is a
5968 token
= cp_lexer_peek_token (parser
->lexer
);
5970 && token
->type
== CPP_NAME
5971 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5973 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5974 || (CLASS_TYPE_P (scope
)
5975 && constructor_name_p (token
->u
.value
, scope
))))
5977 cp_lexer_consume_token (parser
->lexer
);
5978 return build_nt (BIT_NOT_EXPR
, scope
);
5981 /* ~auto means the destructor of whatever the object is. */
5982 if (cp_parser_is_keyword (token
, RID_AUTO
))
5984 if (cxx_dialect
< cxx14
)
5985 pedwarn (input_location
, 0,
5986 "%<~auto%> only available with "
5987 "-std=c++14 or -std=gnu++14");
5988 cp_lexer_consume_token (parser
->lexer
);
5989 return build_nt (BIT_NOT_EXPR
, make_auto ());
5992 /* If there was an explicit qualification (S::~T), first look
5993 in the scope given by the qualification (i.e., S).
5995 Note: in the calls to cp_parser_class_name below we pass
5996 typename_type so that lookup finds the injected-class-name
5997 rather than the constructor. */
5999 type_decl
= NULL_TREE
;
6002 cp_parser_parse_tentatively (parser
);
6003 type_decl
= cp_parser_class_name (parser
,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6010 if (cp_parser_parse_definitely (parser
))
6013 /* In "N::S::~S", look in "N" as well. */
6014 if (!done
&& scope
&& qualifying_scope
)
6016 cp_parser_parse_tentatively (parser
);
6017 parser
->scope
= qualifying_scope
;
6018 parser
->object_scope
= NULL_TREE
;
6019 parser
->qualifying_scope
= NULL_TREE
;
6021 = cp_parser_class_name (parser
,
6022 /*typename_keyword_p=*/false,
6023 /*template_keyword_p=*/false,
6025 /*check_dependency=*/false,
6026 /*class_head_p=*/false,
6028 if (cp_parser_parse_definitely (parser
))
6031 /* In "p->S::~T", look in the scope given by "*p" as well. */
6032 else if (!done
&& object_scope
)
6034 cp_parser_parse_tentatively (parser
);
6035 parser
->scope
= object_scope
;
6036 parser
->object_scope
= NULL_TREE
;
6037 parser
->qualifying_scope
= NULL_TREE
;
6039 = cp_parser_class_name (parser
,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6046 if (cp_parser_parse_definitely (parser
))
6049 /* Look in the surrounding context. */
6052 parser
->scope
= NULL_TREE
;
6053 parser
->object_scope
= NULL_TREE
;
6054 parser
->qualifying_scope
= NULL_TREE
;
6055 if (processing_template_decl
)
6056 cp_parser_parse_tentatively (parser
);
6058 = cp_parser_class_name (parser
,
6059 /*typename_keyword_p=*/false,
6060 /*template_keyword_p=*/false,
6062 /*check_dependency=*/false,
6063 /*class_head_p=*/false,
6065 if (processing_template_decl
6066 && ! cp_parser_parse_definitely (parser
))
6068 /* We couldn't find a type with this name. If we're parsing
6069 tentatively, fail and try something else. */
6070 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6072 cp_parser_simulate_error (parser
);
6073 return error_mark_node
;
6075 /* Otherwise, accept it and check for a match at instantiation
6077 type_decl
= cp_parser_identifier (parser
);
6078 if (type_decl
!= error_mark_node
)
6079 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6083 /* If an error occurred, assume that the name of the
6084 destructor is the same as the name of the qualifying
6085 class. That allows us to keep parsing after running
6086 into ill-formed destructor names. */
6087 if (type_decl
== error_mark_node
&& scope
)
6088 return build_nt (BIT_NOT_EXPR
, scope
);
6089 else if (type_decl
== error_mark_node
)
6090 return error_mark_node
;
6092 /* Check that destructor name and scope match. */
6093 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6095 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6096 error_at (token
->location
,
6097 "declaration of %<~%T%> as member of %qT",
6099 cp_parser_simulate_error (parser
);
6100 return error_mark_node
;
6105 A typedef-name that names a class shall not be used as the
6106 identifier in the declarator for a destructor declaration. */
6108 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6109 && !DECL_SELF_REFERENCE_P (type_decl
)
6110 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6111 error_at (token
->location
,
6112 "typedef-name %qD used as destructor declarator",
6115 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6119 if (token
->keyword
== RID_OPERATOR
)
6123 /* This could be a template-id, so we try that first. */
6124 cp_parser_parse_tentatively (parser
);
6125 /* Try a template-id. */
6126 id
= cp_parser_template_id (parser
, template_keyword_p
,
6127 /*check_dependency_p=*/true,
6130 /* If that worked, we're done. */
6131 if (cp_parser_parse_definitely (parser
))
6133 /* We still don't know whether we're looking at an
6134 operator-function-id or a conversion-function-id. */
6135 cp_parser_parse_tentatively (parser
);
6136 /* Try an operator-function-id. */
6137 id
= cp_parser_operator_function_id (parser
);
6138 /* If that didn't work, try a conversion-function-id. */
6139 if (!cp_parser_parse_definitely (parser
))
6140 id
= cp_parser_conversion_function_id (parser
);
6149 cp_parser_error (parser
, "expected unqualified-id");
6150 return error_mark_node
;
6154 /* Parse an (optional) nested-name-specifier.
6156 nested-name-specifier: [C++98]
6157 class-or-namespace-name :: nested-name-specifier [opt]
6158 class-or-namespace-name :: template nested-name-specifier [opt]
6160 nested-name-specifier: [C++0x]
6163 nested-name-specifier identifier ::
6164 nested-name-specifier template [opt] simple-template-id ::
6166 PARSER->SCOPE should be set appropriately before this function is
6167 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6168 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6171 Sets PARSER->SCOPE to the class (TYPE) or namespace
6172 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6173 it unchanged if there is no nested-name-specifier. Returns the new
6174 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6176 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6177 part of a declaration and/or decl-specifier. */
6180 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6181 bool typename_keyword_p
,
6182 bool check_dependency_p
,
6184 bool is_declaration
,
6185 bool template_keyword_p
/* = false */)
6187 bool success
= false;
6188 cp_token_position start
= 0;
6191 /* Remember where the nested-name-specifier starts. */
6192 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6194 start
= cp_lexer_token_position (parser
->lexer
, false);
6195 push_deferring_access_checks (dk_deferred
);
6202 tree saved_qualifying_scope
;
6204 /* Spot cases that cannot be the beginning of a
6205 nested-name-specifier. */
6206 token
= cp_lexer_peek_token (parser
->lexer
);
6208 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6209 the already parsed nested-name-specifier. */
6210 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6212 /* Grab the nested-name-specifier and continue the loop. */
6213 cp_parser_pre_parsed_nested_name_specifier (parser
);
6214 /* If we originally encountered this nested-name-specifier
6215 with IS_DECLARATION set to false, we will not have
6216 resolved TYPENAME_TYPEs, so we must do so here. */
6218 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6220 new_scope
= resolve_typename_type (parser
->scope
,
6221 /*only_current_p=*/false);
6222 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6223 parser
->scope
= new_scope
;
6229 /* Spot cases that cannot be the beginning of a
6230 nested-name-specifier. On the second and subsequent times
6231 through the loop, we look for the `template' keyword. */
6232 if (success
&& token
->keyword
== RID_TEMPLATE
)
6234 /* A template-id can start a nested-name-specifier. */
6235 else if (token
->type
== CPP_TEMPLATE_ID
)
6237 /* DR 743: decltype can be used in a nested-name-specifier. */
6238 else if (token_is_decltype (token
))
6242 /* If the next token is not an identifier, then it is
6243 definitely not a type-name or namespace-name. */
6244 if (token
->type
!= CPP_NAME
)
6246 /* If the following token is neither a `<' (to begin a
6247 template-id), nor a `::', then we are not looking at a
6248 nested-name-specifier. */
6249 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6251 if (token
->type
== CPP_COLON
6252 && parser
->colon_corrects_to_scope_p
6253 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6255 gcc_rich_location
richloc (token
->location
);
6256 richloc
.add_fixit_replace ("::");
6258 "found %<:%> in nested-name-specifier, "
6260 token
->type
= CPP_SCOPE
;
6263 if (token
->type
!= CPP_SCOPE
6264 && !cp_parser_nth_token_starts_template_argument_list_p
6269 /* The nested-name-specifier is optional, so we parse
6271 cp_parser_parse_tentatively (parser
);
6273 /* Look for the optional `template' keyword, if this isn't the
6274 first time through the loop. */
6276 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6278 /* Save the old scope since the name lookup we are about to do
6279 might destroy it. */
6280 old_scope
= parser
->scope
;
6281 saved_qualifying_scope
= parser
->qualifying_scope
;
6282 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6283 look up names in "X<T>::I" in order to determine that "Y" is
6284 a template. So, if we have a typename at this point, we make
6285 an effort to look through it. */
6287 && !typename_keyword_p
6289 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6290 parser
->scope
= resolve_typename_type (parser
->scope
,
6291 /*only_current_p=*/false);
6292 /* Parse the qualifying entity. */
6294 = cp_parser_qualifying_entity (parser
,
6300 /* Look for the `::' token. */
6301 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6303 /* If we found what we wanted, we keep going; otherwise, we're
6305 if (!cp_parser_parse_definitely (parser
))
6307 bool error_p
= false;
6309 /* Restore the OLD_SCOPE since it was valid before the
6310 failed attempt at finding the last
6311 class-or-namespace-name. */
6312 parser
->scope
= old_scope
;
6313 parser
->qualifying_scope
= saved_qualifying_scope
;
6315 /* If the next token is a decltype, and the one after that is a
6316 `::', then the decltype has failed to resolve to a class or
6317 enumeration type. Give this error even when parsing
6318 tentatively since it can't possibly be valid--and we're going
6319 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6320 won't get another chance.*/
6321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6322 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6325 token
= cp_lexer_consume_token (parser
->lexer
);
6326 error_at (token
->location
, "decltype evaluates to %qT, "
6327 "which is not a class or enumeration type",
6328 token
->u
.tree_check_value
->value
);
6329 parser
->scope
= error_mark_node
;
6333 cp_lexer_consume_token (parser
->lexer
);
6336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6337 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6339 /* If we have a non-type template-id followed by ::, it can't
6340 possibly be valid. */
6341 token
= cp_lexer_peek_token (parser
->lexer
);
6342 tree tid
= token
->u
.tree_check_value
->value
;
6343 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6344 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6346 tree tmpl
= NULL_TREE
;
6347 if (is_overloaded_fn (tid
))
6349 tree fns
= get_fns (tid
);
6350 if (OVL_SINGLE_P (fns
))
6351 tmpl
= OVL_FIRST (fns
);
6352 error_at (token
->location
, "function template-id %qD "
6353 "in nested-name-specifier", tid
);
6357 /* Variable template. */
6358 tmpl
= TREE_OPERAND (tid
, 0);
6359 gcc_assert (variable_template_p (tmpl
));
6360 error_at (token
->location
, "variable template-id %qD "
6361 "in nested-name-specifier", tid
);
6364 inform (DECL_SOURCE_LOCATION (tmpl
),
6365 "%qD declared here", tmpl
);
6367 parser
->scope
= error_mark_node
;
6371 cp_lexer_consume_token (parser
->lexer
);
6372 cp_lexer_consume_token (parser
->lexer
);
6376 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6378 /* If the next token is an identifier, and the one after
6379 that is a `::', then any valid interpretation would have
6380 found a class-or-namespace-name. */
6381 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6382 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6384 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6387 token
= cp_lexer_consume_token (parser
->lexer
);
6390 if (!token
->error_reported
)
6393 tree ambiguous_decls
;
6395 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6397 /*is_template=*/false,
6398 /*is_namespace=*/false,
6399 /*check_dependency=*/true,
6402 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6403 error_at (token
->location
,
6404 "%qD used without template arguments",
6406 else if (ambiguous_decls
)
6408 // cp_parser_lookup_name has the same diagnostic,
6409 // thus make sure to emit it at most once.
6410 if (cp_parser_uncommitted_to_tentative_parse_p
6413 error_at (token
->location
,
6414 "reference to %qD is ambiguous",
6416 print_candidates (ambiguous_decls
);
6418 decl
= error_mark_node
;
6422 if (cxx_dialect
!= cxx98
)
6423 cp_parser_name_lookup_error
6424 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6427 cp_parser_name_lookup_error
6428 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6432 parser
->scope
= error_mark_node
;
6434 /* Treat this as a successful nested-name-specifier
6439 If the name found is not a class-name (clause
6440 _class_) or namespace-name (_namespace.def_), the
6441 program is ill-formed. */
6444 cp_lexer_consume_token (parser
->lexer
);
6448 /* We've found one valid nested-name-specifier. */
6450 /* Name lookup always gives us a DECL. */
6451 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6452 new_scope
= TREE_TYPE (new_scope
);
6453 /* Uses of "template" must be followed by actual templates. */
6454 if (template_keyword_p
6455 && !(CLASS_TYPE_P (new_scope
)
6456 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6457 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6458 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6459 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6460 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6461 == TEMPLATE_ID_EXPR
)))
6462 permerror (input_location
, TYPE_P (new_scope
)
6463 ? G_("%qT is not a template")
6464 : G_("%qD is not a template"),
6466 /* If it is a class scope, try to complete it; we are about to
6467 be looking up names inside the class. */
6468 if (TYPE_P (new_scope
)
6469 /* Since checking types for dependency can be expensive,
6470 avoid doing it if the type is already complete. */
6471 && !COMPLETE_TYPE_P (new_scope
)
6472 /* Do not try to complete dependent types. */
6473 && !dependent_type_p (new_scope
))
6475 new_scope
= complete_type (new_scope
);
6476 /* If it is a typedef to current class, use the current
6477 class instead, as the typedef won't have any names inside
6479 if (!COMPLETE_TYPE_P (new_scope
)
6480 && currently_open_class (new_scope
))
6481 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6483 /* Make sure we look in the right scope the next time through
6485 parser
->scope
= new_scope
;
6488 /* If parsing tentatively, replace the sequence of tokens that makes
6489 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6490 token. That way, should we re-parse the token stream, we will
6491 not have to repeat the effort required to do the parse, nor will
6492 we issue duplicate error messages. */
6493 if (success
&& start
)
6497 token
= cp_lexer_token_at (parser
->lexer
, start
);
6498 /* Reset the contents of the START token. */
6499 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6500 /* Retrieve any deferred checks. Do not pop this access checks yet
6501 so the memory will not be reclaimed during token replacing below. */
6502 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6503 token
->u
.tree_check_value
->value
= parser
->scope
;
6504 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6505 token
->u
.tree_check_value
->qualifying_scope
=
6506 parser
->qualifying_scope
;
6507 token
->keyword
= RID_MAX
;
6509 /* Purge all subsequent tokens. */
6510 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6514 pop_to_parent_deferring_access_checks ();
6516 return success
? parser
->scope
: NULL_TREE
;
6519 /* Parse a nested-name-specifier. See
6520 cp_parser_nested_name_specifier_opt for details. This function
6521 behaves identically, except that it will an issue an error if no
6522 nested-name-specifier is present. */
6525 cp_parser_nested_name_specifier (cp_parser
*parser
,
6526 bool typename_keyword_p
,
6527 bool check_dependency_p
,
6529 bool is_declaration
)
6533 /* Look for the nested-name-specifier. */
6534 scope
= cp_parser_nested_name_specifier_opt (parser
,
6539 /* If it was not present, issue an error message. */
6542 cp_parser_error (parser
, "expected nested-name-specifier");
6543 parser
->scope
= NULL_TREE
;
6549 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6550 this is either a class-name or a namespace-name (which corresponds
6551 to the class-or-namespace-name production in the grammar). For
6552 C++0x, it can also be a type-name that refers to an enumeration
6553 type or a simple-template-id.
6555 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6556 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6557 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6558 TYPE_P is TRUE iff the next name should be taken as a class-name,
6559 even the same name is declared to be another entity in the same
6562 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6563 specified by the class-or-namespace-name. If neither is found the
6564 ERROR_MARK_NODE is returned. */
6567 cp_parser_qualifying_entity (cp_parser
*parser
,
6568 bool typename_keyword_p
,
6569 bool template_keyword_p
,
6570 bool check_dependency_p
,
6572 bool is_declaration
)
6575 tree saved_qualifying_scope
;
6576 tree saved_object_scope
;
6579 bool successful_parse_p
;
6581 /* DR 743: decltype can appear in a nested-name-specifier. */
6582 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6584 scope
= cp_parser_decltype (parser
);
6585 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6586 && !MAYBE_CLASS_TYPE_P (scope
))
6588 cp_parser_simulate_error (parser
);
6589 return error_mark_node
;
6591 if (TYPE_NAME (scope
))
6592 scope
= TYPE_NAME (scope
);
6596 /* Before we try to parse the class-name, we must save away the
6597 current PARSER->SCOPE since cp_parser_class_name will destroy
6599 saved_scope
= parser
->scope
;
6600 saved_qualifying_scope
= parser
->qualifying_scope
;
6601 saved_object_scope
= parser
->object_scope
;
6602 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6603 there is no need to look for a namespace-name. */
6604 only_class_p
= template_keyword_p
6605 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6607 cp_parser_parse_tentatively (parser
);
6608 scope
= cp_parser_class_name (parser
,
6611 type_p
? class_type
: none_type
,
6613 /*class_head_p=*/false,
6615 /*enum_ok=*/cxx_dialect
> cxx98
);
6616 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6617 /* If that didn't work, try for a namespace-name. */
6618 if (!only_class_p
&& !successful_parse_p
)
6620 /* Restore the saved scope. */
6621 parser
->scope
= saved_scope
;
6622 parser
->qualifying_scope
= saved_qualifying_scope
;
6623 parser
->object_scope
= saved_object_scope
;
6624 /* If we are not looking at an identifier followed by the scope
6625 resolution operator, then this is not part of a
6626 nested-name-specifier. (Note that this function is only used
6627 to parse the components of a nested-name-specifier.) */
6628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6629 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6630 return error_mark_node
;
6631 scope
= cp_parser_namespace_name (parser
);
6637 /* Return true if we are looking at a compound-literal, false otherwise. */
6640 cp_parser_compound_literal_p (cp_parser
*parser
)
6642 cp_lexer_save_tokens (parser
->lexer
);
6644 /* Skip tokens until the next token is a closing parenthesis.
6645 If we find the closing `)', and the next token is a `{', then
6646 we are looking at a compound-literal. */
6647 bool compound_literal_p
6648 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6649 /*consume_paren=*/true)
6650 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6652 /* Roll back the tokens we skipped. */
6653 cp_lexer_rollback_tokens (parser
->lexer
);
6655 return compound_literal_p
;
6658 /* Return true if EXPR is the integer constant zero or a complex constant
6659 of zero, without any folding, but ignoring location wrappers. */
6662 literal_integer_zerop (const_tree expr
)
6664 return (location_wrapper_p (expr
)
6665 && integer_zerop (TREE_OPERAND (expr
, 0)));
6668 /* Parse a postfix-expression.
6672 postfix-expression [ expression ]
6673 postfix-expression ( expression-list [opt] )
6674 simple-type-specifier ( expression-list [opt] )
6675 typename :: [opt] nested-name-specifier identifier
6676 ( expression-list [opt] )
6677 typename :: [opt] nested-name-specifier template [opt] template-id
6678 ( expression-list [opt] )
6679 postfix-expression . template [opt] id-expression
6680 postfix-expression -> template [opt] id-expression
6681 postfix-expression . pseudo-destructor-name
6682 postfix-expression -> pseudo-destructor-name
6683 postfix-expression ++
6684 postfix-expression --
6685 dynamic_cast < type-id > ( expression )
6686 static_cast < type-id > ( expression )
6687 reinterpret_cast < type-id > ( expression )
6688 const_cast < type-id > ( expression )
6689 typeid ( expression )
6695 ( type-id ) { initializer-list , [opt] }
6697 This extension is a GNU version of the C99 compound-literal
6698 construct. (The C99 grammar uses `type-name' instead of `type-id',
6699 but they are essentially the same concept.)
6701 If ADDRESS_P is true, the postfix expression is the operand of the
6702 `&' operator. CAST_P is true if this expression is the target of a
6705 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6706 class member access expressions [expr.ref].
6708 Returns a representation of the expression. */
6711 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6712 bool member_access_only_p
, bool decltype_p
,
6713 cp_id_kind
* pidk_return
)
6718 cp_id_kind idk
= CP_ID_KIND_NONE
;
6719 cp_expr postfix_expression
= NULL_TREE
;
6720 bool is_member_access
= false;
6722 /* Peek at the next token. */
6723 token
= cp_lexer_peek_token (parser
->lexer
);
6724 loc
= token
->location
;
6725 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6727 /* Some of the productions are determined by keywords. */
6728 keyword
= token
->keyword
;
6738 const char *saved_message
;
6739 bool saved_in_type_id_in_expr_p
;
6741 /* All of these can be handled in the same way from the point
6742 of view of parsing. Begin by consuming the token
6743 identifying the cast. */
6744 cp_lexer_consume_token (parser
->lexer
);
6746 /* New types cannot be defined in the cast. */
6747 saved_message
= parser
->type_definition_forbidden_message
;
6748 parser
->type_definition_forbidden_message
6749 = G_("types may not be defined in casts");
6751 /* Look for the opening `<'. */
6752 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6753 /* Parse the type to which we are casting. */
6754 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6755 parser
->in_type_id_in_expr_p
= true;
6756 type
= cp_parser_type_id (parser
);
6757 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6758 /* Look for the closing `>'. */
6759 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6760 /* Restore the old message. */
6761 parser
->type_definition_forbidden_message
= saved_message
;
6763 bool saved_greater_than_is_operator_p
6764 = parser
->greater_than_is_operator_p
;
6765 parser
->greater_than_is_operator_p
= true;
6767 /* And the expression which is being cast. */
6768 matching_parens parens
;
6769 parens
.require_open (parser
);
6770 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6771 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6773 location_t end_loc
= close_paren
?
6774 close_paren
->location
: UNKNOWN_LOCATION
;
6776 parser
->greater_than_is_operator_p
6777 = saved_greater_than_is_operator_p
;
6779 /* Only type conversions to integral or enumeration types
6780 can be used in constant-expressions. */
6781 if (!cast_valid_in_integral_constant_expression_p (type
)
6782 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6784 postfix_expression
= error_mark_node
;
6792 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6796 = build_static_cast (type
, expression
, tf_warning_or_error
);
6800 = build_reinterpret_cast (type
, expression
,
6801 tf_warning_or_error
);
6805 = build_const_cast (type
, expression
, tf_warning_or_error
);
6811 /* Construct a location e.g. :
6812 reinterpret_cast <int *> (expr)
6813 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6814 ranging from the start of the "*_cast" token to the final closing
6815 paren, with the caret at the start. */
6816 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6817 postfix_expression
.set_location (cp_cast_loc
);
6824 const char *saved_message
;
6825 bool saved_in_type_id_in_expr_p
;
6827 /* Consume the `typeid' token. */
6828 cp_lexer_consume_token (parser
->lexer
);
6829 /* Look for the `(' token. */
6830 matching_parens parens
;
6831 parens
.require_open (parser
);
6832 /* Types cannot be defined in a `typeid' expression. */
6833 saved_message
= parser
->type_definition_forbidden_message
;
6834 parser
->type_definition_forbidden_message
6835 = G_("types may not be defined in a %<typeid%> expression");
6836 /* We can't be sure yet whether we're looking at a type-id or an
6838 cp_parser_parse_tentatively (parser
);
6839 /* Try a type-id first. */
6840 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6841 parser
->in_type_id_in_expr_p
= true;
6842 type
= cp_parser_type_id (parser
);
6843 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6844 /* Look for the `)' token. Otherwise, we can't be sure that
6845 we're not looking at an expression: consider `typeid (int
6846 (3))', for example. */
6847 cp_token
*close_paren
= parens
.require_close (parser
);
6848 /* If all went well, simply lookup the type-id. */
6849 if (cp_parser_parse_definitely (parser
))
6850 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6851 /* Otherwise, fall back to the expression variant. */
6856 /* Look for an expression. */
6857 expression
= cp_parser_expression (parser
, & idk
);
6858 /* Compute its typeid. */
6859 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6860 /* Look for the `)' token. */
6861 close_paren
= parens
.require_close (parser
);
6863 /* Restore the saved message. */
6864 parser
->type_definition_forbidden_message
= saved_message
;
6865 /* `typeid' may not appear in an integral constant expression. */
6866 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6867 postfix_expression
= error_mark_node
;
6869 /* Construct a location e.g. :
6872 ranging from the start of the "typeid" token to the final closing
6873 paren, with the caret at the start. */
6876 location_t typeid_loc
6877 = make_location (start_loc
, start_loc
, close_paren
->location
);
6878 postfix_expression
.set_location (typeid_loc
);
6879 postfix_expression
.maybe_add_location_wrapper ();
6887 /* The syntax permitted here is the same permitted for an
6888 elaborated-type-specifier. */
6889 ++parser
->prevent_constrained_type_specifiers
;
6890 type
= cp_parser_elaborated_type_specifier (parser
,
6891 /*is_friend=*/false,
6892 /*is_declaration=*/false);
6893 --parser
->prevent_constrained_type_specifiers
;
6894 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6899 case RID_BUILTIN_SHUFFLE
:
6900 case RID_BUILTIN_LAUNDER
:
6902 vec
<tree
, va_gc
> *vec
;
6906 cp_lexer_consume_token (parser
->lexer
);
6907 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6908 /*cast_p=*/false, /*allow_expansion_p=*/true,
6909 /*non_constant_p=*/NULL
);
6912 postfix_expression
= error_mark_node
;
6916 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6922 if (vec
->length () == 1)
6924 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6927 error_at (loc
, "wrong number of arguments to "
6928 "%<__builtin_addressof%>");
6929 postfix_expression
= error_mark_node
;
6933 case RID_BUILTIN_LAUNDER
:
6934 if (vec
->length () == 1)
6935 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6936 tf_warning_or_error
);
6939 error_at (loc
, "wrong number of arguments to "
6940 "%<__builtin_launder%>");
6941 postfix_expression
= error_mark_node
;
6945 case RID_BUILTIN_SHUFFLE
:
6946 if (vec
->length () == 2)
6948 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6949 (*vec
)[1], tf_warning_or_error
);
6950 else if (vec
->length () == 3)
6952 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6953 (*vec
)[2], tf_warning_or_error
);
6956 error_at (loc
, "wrong number of arguments to "
6957 "%<__builtin_shuffle%>");
6958 postfix_expression
= error_mark_node
;
6972 /* If the next thing is a simple-type-specifier, we may be
6973 looking at a functional cast. We could also be looking at
6974 an id-expression. So, we try the functional cast, and if
6975 that doesn't work we fall back to the primary-expression. */
6976 cp_parser_parse_tentatively (parser
);
6977 /* Look for the simple-type-specifier. */
6978 ++parser
->prevent_constrained_type_specifiers
;
6979 type
= cp_parser_simple_type_specifier (parser
,
6980 /*decl_specs=*/NULL
,
6981 CP_PARSER_FLAGS_NONE
);
6982 --parser
->prevent_constrained_type_specifiers
;
6983 /* Parse the cast itself. */
6984 if (!cp_parser_error_occurred (parser
))
6986 = cp_parser_functional_cast (parser
, type
);
6987 /* If that worked, we're done. */
6988 if (cp_parser_parse_definitely (parser
))
6991 /* If the functional-cast didn't work out, try a
6992 compound-literal. */
6993 if (cp_parser_allow_gnu_extensions_p (parser
)
6994 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6996 cp_expr initializer
= NULL_TREE
;
6998 cp_parser_parse_tentatively (parser
);
7000 matching_parens parens
;
7001 parens
.consume_open (parser
);
7003 /* Avoid calling cp_parser_type_id pointlessly, see comment
7004 in cp_parser_cast_expression about c++/29234. */
7005 if (!cp_parser_compound_literal_p (parser
))
7006 cp_parser_simulate_error (parser
);
7009 /* Parse the type. */
7010 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7011 parser
->in_type_id_in_expr_p
= true;
7012 type
= cp_parser_type_id (parser
);
7013 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7014 parens
.require_close (parser
);
7017 /* If things aren't going well, there's no need to
7019 if (!cp_parser_error_occurred (parser
))
7021 bool non_constant_p
;
7022 /* Parse the brace-enclosed initializer list. */
7023 initializer
= cp_parser_braced_list (parser
,
7026 /* If that worked, we're definitely looking at a
7027 compound-literal expression. */
7028 if (cp_parser_parse_definitely (parser
))
7030 /* Warn the user that a compound literal is not
7031 allowed in standard C++. */
7032 pedwarn (input_location
, OPT_Wpedantic
,
7033 "ISO C++ forbids compound-literals");
7034 /* For simplicity, we disallow compound literals in
7035 constant-expressions. We could
7036 allow compound literals of integer type, whose
7037 initializer was a constant, in constant
7038 expressions. Permitting that usage, as a further
7039 extension, would not change the meaning of any
7040 currently accepted programs. (Of course, as
7041 compound literals are not part of ISO C++, the
7042 standard has nothing to say.) */
7043 if (cp_parser_non_integral_constant_expression (parser
,
7046 postfix_expression
= error_mark_node
;
7049 /* Form the representation of the compound-literal. */
7051 = finish_compound_literal (type
, initializer
,
7052 tf_warning_or_error
, fcl_c99
);
7053 postfix_expression
.set_location (initializer
.get_location ());
7058 /* It must be a primary-expression. */
7060 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7061 /*template_arg_p=*/false,
7068 /* Note that we don't need to worry about calling build_cplus_new on a
7069 class-valued CALL_EXPR in decltype when it isn't the end of the
7070 postfix-expression; unary_complex_lvalue will take care of that for
7073 /* Keep looping until the postfix-expression is complete. */
7076 if (idk
== CP_ID_KIND_UNQUALIFIED
7077 && identifier_p (postfix_expression
)
7078 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7079 /* It is not a Koenig lookup function call. */
7081 = unqualified_name_lookup_error (postfix_expression
);
7083 /* Peek at the next token. */
7084 token
= cp_lexer_peek_token (parser
->lexer
);
7086 switch (token
->type
)
7088 case CPP_OPEN_SQUARE
:
7089 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7091 cp_parser_error (parser
,
7092 "two consecutive %<[%> shall "
7093 "only introduce an attribute");
7094 return error_mark_node
;
7097 = cp_parser_postfix_open_square_expression (parser
,
7101 postfix_expression
.set_range (start_loc
,
7102 postfix_expression
.get_location ());
7104 idk
= CP_ID_KIND_NONE
;
7105 is_member_access
= false;
7108 case CPP_OPEN_PAREN
:
7109 /* postfix-expression ( expression-list [opt] ) */
7112 bool is_builtin_constant_p
;
7113 bool saved_integral_constant_expression_p
= false;
7114 bool saved_non_integral_constant_expression_p
= false;
7115 tsubst_flags_t complain
= complain_flags (decltype_p
);
7116 vec
<tree
, va_gc
> *args
;
7117 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7119 is_member_access
= false;
7121 is_builtin_constant_p
7122 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7123 if (is_builtin_constant_p
)
7125 /* The whole point of __builtin_constant_p is to allow
7126 non-constant expressions to appear as arguments. */
7127 saved_integral_constant_expression_p
7128 = parser
->integral_constant_expression_p
;
7129 saved_non_integral_constant_expression_p
7130 = parser
->non_integral_constant_expression_p
;
7131 parser
->integral_constant_expression_p
= false;
7133 args
= (cp_parser_parenthesized_expression_list
7135 /*cast_p=*/false, /*allow_expansion_p=*/true,
7136 /*non_constant_p=*/NULL
,
7137 /*close_paren_loc=*/&close_paren_loc
,
7138 /*wrap_locations_p=*/true));
7139 if (is_builtin_constant_p
)
7141 parser
->integral_constant_expression_p
7142 = saved_integral_constant_expression_p
;
7143 parser
->non_integral_constant_expression_p
7144 = saved_non_integral_constant_expression_p
;
7149 postfix_expression
= error_mark_node
;
7153 /* Function calls are not permitted in
7154 constant-expressions. */
7155 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7156 && cp_parser_non_integral_constant_expression (parser
,
7159 postfix_expression
= error_mark_node
;
7160 release_tree_vector (args
);
7165 if (idk
== CP_ID_KIND_UNQUALIFIED
7166 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7168 if (identifier_p (postfix_expression
))
7170 if (!args
->is_empty ())
7173 if (!any_type_dependent_arguments_p (args
))
7175 = perform_koenig_lookup (postfix_expression
, args
,
7180 = unqualified_fn_lookup_error (postfix_expression
);
7182 /* We do not perform argument-dependent lookup if
7183 normal lookup finds a non-function, in accordance
7184 with the expected resolution of DR 218. */
7185 else if (!args
->is_empty ()
7186 && is_overloaded_fn (postfix_expression
))
7188 tree fn
= get_first_fn (postfix_expression
);
7189 fn
= STRIP_TEMPLATE (fn
);
7191 /* Do not do argument dependent lookup if regular
7192 lookup finds a member function or a block-scope
7193 function declaration. [basic.lookup.argdep]/3 */
7194 if (!DECL_FUNCTION_MEMBER_P (fn
)
7195 && !DECL_LOCAL_FUNCTION_P (fn
))
7198 if (!any_type_dependent_arguments_p (args
))
7200 = perform_koenig_lookup (postfix_expression
, args
,
7206 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7208 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7209 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7211 if (processing_template_decl
7212 && (type_dependent_object_expression_p (instance
)
7213 || (!BASELINK_P (fn
)
7214 && TREE_CODE (fn
) != FIELD_DECL
)
7215 || type_dependent_expression_p (fn
)
7216 || any_type_dependent_arguments_p (args
)))
7218 maybe_generic_this_capture (instance
, fn
);
7220 = build_min_nt_call_vec (postfix_expression
, args
);
7221 release_tree_vector (args
);
7225 if (BASELINK_P (fn
))
7228 = (build_new_method_call
7229 (instance
, fn
, &args
, NULL_TREE
,
7230 (idk
== CP_ID_KIND_QUALIFIED
7231 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7238 = finish_call_expr (postfix_expression
, &args
,
7239 /*disallow_virtual=*/false,
7243 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7244 || TREE_CODE (postfix_expression
) == MEMBER_REF
7245 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7246 postfix_expression
= (build_offset_ref_call_from_tree
7247 (postfix_expression
, &args
,
7249 else if (idk
== CP_ID_KIND_QUALIFIED
)
7250 /* A call to a static class member, or a namespace-scope
7253 = finish_call_expr (postfix_expression
, &args
,
7254 /*disallow_virtual=*/true,
7258 /* All other function calls. */
7260 = finish_call_expr (postfix_expression
, &args
,
7261 /*disallow_virtual=*/false,
7265 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7267 location_t combined_loc
= make_location (token
->location
,
7270 postfix_expression
.set_location (combined_loc
);
7273 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7274 idk
= CP_ID_KIND_NONE
;
7276 release_tree_vector (args
);
7282 /* postfix-expression . template [opt] id-expression
7283 postfix-expression . pseudo-destructor-name
7284 postfix-expression -> template [opt] id-expression
7285 postfix-expression -> pseudo-destructor-name */
7287 /* Consume the `.' or `->' operator. */
7288 cp_lexer_consume_token (parser
->lexer
);
7291 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7295 is_member_access
= true;
7299 /* postfix-expression ++ */
7300 /* Consume the `++' token. */
7301 cp_lexer_consume_token (parser
->lexer
);
7302 /* Generate a representation for the complete expression. */
7304 = finish_increment_expr (postfix_expression
,
7305 POSTINCREMENT_EXPR
);
7306 /* Increments may not appear in constant-expressions. */
7307 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7308 postfix_expression
= error_mark_node
;
7309 idk
= CP_ID_KIND_NONE
;
7310 is_member_access
= false;
7313 case CPP_MINUS_MINUS
:
7314 /* postfix-expression -- */
7315 /* Consume the `--' token. */
7316 cp_lexer_consume_token (parser
->lexer
);
7317 /* Generate a representation for the complete expression. */
7319 = finish_increment_expr (postfix_expression
,
7320 POSTDECREMENT_EXPR
);
7321 /* Decrements may not appear in constant-expressions. */
7322 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7323 postfix_expression
= error_mark_node
;
7324 idk
= CP_ID_KIND_NONE
;
7325 is_member_access
= false;
7329 if (pidk_return
!= NULL
)
7330 * pidk_return
= idk
;
7331 if (member_access_only_p
)
7332 return is_member_access
7333 ? postfix_expression
7334 : cp_expr (error_mark_node
);
7336 return postfix_expression
;
7340 /* We should never get here. */
7342 return error_mark_node
;
7345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7346 by cp_parser_builtin_offsetof. We're looking for
7348 postfix-expression [ expression ]
7349 postfix-expression [ braced-init-list ] (C++11)
7351 FOR_OFFSETOF is set if we're being called in that context, which
7352 changes how we deal with integer constant expressions. */
7355 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7356 tree postfix_expression
,
7360 tree index
= NULL_TREE
;
7361 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7362 bool saved_greater_than_is_operator_p
;
7364 /* Consume the `[' token. */
7365 cp_lexer_consume_token (parser
->lexer
);
7367 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7368 parser
->greater_than_is_operator_p
= true;
7370 /* Parse the index expression. */
7371 /* ??? For offsetof, there is a question of what to allow here. If
7372 offsetof is not being used in an integral constant expression context,
7373 then we *could* get the right answer by computing the value at runtime.
7374 If we are in an integral constant expression context, then we might
7375 could accept any constant expression; hard to say without analysis.
7376 Rather than open the barn door too wide right away, allow only integer
7377 constant expressions here. */
7379 index
= cp_parser_constant_expression (parser
);
7382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7384 bool expr_nonconst_p
;
7385 cp_lexer_set_source_position (parser
->lexer
);
7386 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7387 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7390 index
= cp_parser_expression (parser
);
7393 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7395 /* Look for the closing `]'. */
7396 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7398 /* Build the ARRAY_REF. */
7399 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7402 /* When not doing offsetof, array references are not permitted in
7403 constant-expressions. */
7405 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7406 postfix_expression
= error_mark_node
;
7408 return postfix_expression
;
7411 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7412 dereference of incomplete type, returns true if error_mark_node should
7413 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7414 and *DEPENDENT_P. */
7417 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7420 /* In a template, be permissive by treating an object expression
7421 of incomplete type as dependent (after a pedwarn). */
7422 diagnostic_t kind
= (processing_template_decl
7423 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7425 switch (TREE_CODE (*postfix_expression
))
7428 case REINTERPRET_CAST_EXPR
:
7429 case CONST_CAST_EXPR
:
7430 case STATIC_CAST_EXPR
:
7431 case DYNAMIC_CAST_EXPR
:
7432 case IMPLICIT_CONV_EXPR
:
7433 case VIEW_CONVERT_EXPR
:
7434 case NON_LVALUE_EXPR
:
7438 /* Don't emit any diagnostic for OVERLOADs. */
7442 /* Avoid clobbering e.g. DECLs. */
7443 if (!EXPR_P (*postfix_expression
))
7448 if (kind
== DK_IGNORED
)
7451 location_t exploc
= location_of (*postfix_expression
);
7452 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7453 if (!MAYBE_CLASS_TYPE_P (*scope
))
7455 if (kind
== DK_ERROR
)
7456 *scope
= *postfix_expression
= error_mark_node
;
7457 else if (processing_template_decl
)
7459 *dependent_p
= true;
7460 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7465 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7466 by cp_parser_builtin_offsetof. We're looking for
7468 postfix-expression . template [opt] id-expression
7469 postfix-expression . pseudo-destructor-name
7470 postfix-expression -> template [opt] id-expression
7471 postfix-expression -> pseudo-destructor-name
7473 FOR_OFFSETOF is set if we're being called in that context. That sorta
7474 limits what of the above we'll actually accept, but nevermind.
7475 TOKEN_TYPE is the "." or "->" token, which will already have been
7476 removed from the stream. */
7479 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7480 enum cpp_ttype token_type
,
7481 cp_expr postfix_expression
,
7482 bool for_offsetof
, cp_id_kind
*idk
,
7483 location_t location
)
7487 bool pseudo_destructor_p
;
7488 tree scope
= NULL_TREE
;
7489 location_t start_loc
= postfix_expression
.get_start ();
7491 /* If this is a `->' operator, dereference the pointer. */
7492 if (token_type
== CPP_DEREF
)
7493 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7494 tf_warning_or_error
);
7495 /* Check to see whether or not the expression is type-dependent and
7496 not the current instantiation. */
7497 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7498 /* The identifier following the `->' or `.' is not qualified. */
7499 parser
->scope
= NULL_TREE
;
7500 parser
->qualifying_scope
= NULL_TREE
;
7501 parser
->object_scope
= NULL_TREE
;
7502 *idk
= CP_ID_KIND_NONE
;
7504 /* Enter the scope corresponding to the type of the object
7505 given by the POSTFIX_EXPRESSION. */
7508 scope
= TREE_TYPE (postfix_expression
);
7509 /* According to the standard, no expression should ever have
7510 reference type. Unfortunately, we do not currently match
7511 the standard in this respect in that our internal representation
7512 of an expression may have reference type even when the standard
7513 says it does not. Therefore, we have to manually obtain the
7514 underlying type here. */
7515 scope
= non_reference (scope
);
7516 /* The type of the POSTFIX_EXPRESSION must be complete. */
7517 /* Unlike the object expression in other contexts, *this is not
7518 required to be of complete type for purposes of class member
7519 access (5.2.5) outside the member function body. */
7520 if (postfix_expression
!= current_class_ref
7521 && scope
!= error_mark_node
7522 && !currently_open_class (scope
))
7524 scope
= complete_type (scope
);
7525 if (!COMPLETE_TYPE_P (scope
)
7526 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7528 return error_mark_node
;
7533 /* Let the name lookup machinery know that we are processing a
7534 class member access expression. */
7535 parser
->context
->object_type
= scope
;
7536 /* If something went wrong, we want to be able to discern that case,
7537 as opposed to the case where there was no SCOPE due to the type
7538 of expression being dependent. */
7540 scope
= error_mark_node
;
7541 /* If the SCOPE was erroneous, make the various semantic analysis
7542 functions exit quickly -- and without issuing additional error
7544 if (scope
== error_mark_node
)
7545 postfix_expression
= error_mark_node
;
7550 /* Tell cp_parser_lookup_name that there was an object, even though it's
7552 parser
->context
->object_type
= unknown_type_node
;
7554 /* Assume this expression is not a pseudo-destructor access. */
7555 pseudo_destructor_p
= false;
7557 /* If the SCOPE is a scalar type, then, if this is a valid program,
7558 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7559 is type dependent, it can be pseudo-destructor-name or something else.
7560 Try to parse it as pseudo-destructor-name first. */
7561 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7566 cp_parser_parse_tentatively (parser
);
7567 /* Parse the pseudo-destructor-name. */
7569 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7572 && (cp_parser_error_occurred (parser
)
7573 || !SCALAR_TYPE_P (type
)))
7574 cp_parser_abort_tentative_parse (parser
);
7575 else if (cp_parser_parse_definitely (parser
))
7577 pseudo_destructor_p
= true;
7579 = finish_pseudo_destructor_expr (postfix_expression
,
7584 if (!pseudo_destructor_p
)
7586 /* If the SCOPE is not a scalar type, we are looking at an
7587 ordinary class member access expression, rather than a
7588 pseudo-destructor-name. */
7590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7591 /* Parse the id-expression. */
7592 name
= (cp_parser_id_expression
7594 cp_parser_optional_template_keyword (parser
),
7595 /*check_dependency_p=*/true,
7597 /*declarator_p=*/false,
7598 /*optional_p=*/false));
7599 /* In general, build a SCOPE_REF if the member name is qualified.
7600 However, if the name was not dependent and has already been
7601 resolved; there is no need to build the SCOPE_REF. For example;
7603 struct X { void f(); };
7604 template <typename T> void f(T* t) { t->X::f(); }
7606 Even though "t" is dependent, "X::f" is not and has been resolved
7607 to a BASELINK; there is no need to include scope information. */
7609 /* But we do need to remember that there was an explicit scope for
7610 virtual function calls. */
7612 *idk
= CP_ID_KIND_QUALIFIED
;
7614 /* If the name is a template-id that names a type, we will get a
7615 TYPE_DECL here. That is invalid code. */
7616 if (TREE_CODE (name
) == TYPE_DECL
)
7618 error_at (token
->location
, "invalid use of %qD", name
);
7619 postfix_expression
= error_mark_node
;
7623 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7625 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7627 error_at (token
->location
, "%<%D::%D%> is not a class member",
7628 parser
->scope
, name
);
7629 postfix_expression
= error_mark_node
;
7632 name
= build_qualified_name (/*type=*/NULL_TREE
,
7636 parser
->scope
= NULL_TREE
;
7637 parser
->qualifying_scope
= NULL_TREE
;
7638 parser
->object_scope
= NULL_TREE
;
7640 if (parser
->scope
&& name
&& BASELINK_P (name
))
7641 adjust_result_of_qualified_name_lookup
7642 (name
, parser
->scope
, scope
);
7644 = finish_class_member_access_expr (postfix_expression
, name
,
7646 tf_warning_or_error
);
7647 /* Build a location e.g.:
7650 where the caret is at the deref token, ranging from
7651 the start of postfix_expression to the end of the access expr. */
7653 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7654 location_t combined_loc
7655 = make_location (input_location
, start_loc
, end_loc
);
7656 protected_set_expr_location (postfix_expression
, combined_loc
);
7660 /* We no longer need to look up names in the scope of the object on
7661 the left-hand side of the `.' or `->' operator. */
7662 parser
->context
->object_type
= NULL_TREE
;
7664 /* Outside of offsetof, these operators may not appear in
7665 constant-expressions. */
7667 && (cp_parser_non_integral_constant_expression
7668 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7669 postfix_expression
= error_mark_node
;
7671 return postfix_expression
;
7674 /* Parse a parenthesized expression-list.
7677 assignment-expression
7678 expression-list, assignment-expression
7683 identifier, expression-list
7685 CAST_P is true if this expression is the target of a cast.
7687 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7690 WRAP_LOCATIONS_P is true if expressions within this list for which
7691 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7692 their source locations.
7694 Returns a vector of trees. Each element is a representation of an
7695 assignment-expression. NULL is returned if the ( and or ) are
7696 missing. An empty, but allocated, vector is returned on no
7697 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7698 if we are parsing an attribute list for an attribute that wants a
7699 plain identifier argument, normal_attr for an attribute that wants
7700 an expression, or non_attr if we aren't parsing an attribute list. If
7701 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7702 not all of the expressions in the list were constant.
7703 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7704 will be written to with the location of the closing parenthesis. If
7705 an error occurs, it may or may not be written to. */
7707 static vec
<tree
, va_gc
> *
7708 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7709 int is_attribute_list
,
7711 bool allow_expansion_p
,
7712 bool *non_constant_p
,
7713 location_t
*close_paren_loc
,
7714 bool wrap_locations_p
)
7716 vec
<tree
, va_gc
> *expression_list
;
7717 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7718 tree identifier
= NULL_TREE
;
7719 bool saved_greater_than_is_operator_p
;
7721 /* Assume all the expressions will be constant. */
7723 *non_constant_p
= false;
7725 matching_parens parens
;
7726 if (!parens
.require_open (parser
))
7729 expression_list
= make_tree_vector ();
7731 /* Within a parenthesized expression, a `>' token is always
7732 the greater-than operator. */
7733 saved_greater_than_is_operator_p
7734 = parser
->greater_than_is_operator_p
;
7735 parser
->greater_than_is_operator_p
= true;
7737 cp_expr
expr (NULL_TREE
);
7739 /* Consume expressions until there are no more. */
7740 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7743 /* At the beginning of attribute lists, check to see if the
7744 next token is an identifier. */
7745 if (is_attribute_list
== id_attr
7746 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7750 /* Consume the identifier. */
7751 token
= cp_lexer_consume_token (parser
->lexer
);
7752 /* Save the identifier. */
7753 identifier
= token
->u
.value
;
7757 bool expr_non_constant_p
;
7759 /* Parse the next assignment-expression. */
7760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7762 /* A braced-init-list. */
7763 cp_lexer_set_source_position (parser
->lexer
);
7764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7765 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7766 if (non_constant_p
&& expr_non_constant_p
)
7767 *non_constant_p
= true;
7769 else if (non_constant_p
)
7771 expr
= (cp_parser_constant_expression
7772 (parser
, /*allow_non_constant_p=*/true,
7773 &expr_non_constant_p
));
7774 if (expr_non_constant_p
)
7775 *non_constant_p
= true;
7778 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7782 expr
= instantiate_non_dependent_expr (expr
);
7784 /* If we have an ellipsis, then this is an expression
7786 if (allow_expansion_p
7787 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7789 /* Consume the `...'. */
7790 cp_lexer_consume_token (parser
->lexer
);
7792 /* Build the argument pack. */
7793 expr
= make_pack_expansion (expr
);
7796 if (wrap_locations_p
)
7797 expr
.maybe_add_location_wrapper ();
7799 /* Add it to the list. We add error_mark_node
7800 expressions to the list, so that we can still tell if
7801 the correct form for a parenthesized expression-list
7802 is found. That gives better errors. */
7803 vec_safe_push (expression_list
, expr
.get_value ());
7805 if (expr
== error_mark_node
)
7809 /* After the first item, attribute lists look the same as
7810 expression lists. */
7811 is_attribute_list
= non_attr
;
7814 /* If the next token isn't a `,', then we are done. */
7815 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7818 /* Otherwise, consume the `,' and keep going. */
7819 cp_lexer_consume_token (parser
->lexer
);
7822 if (close_paren_loc
)
7823 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7825 if (!parens
.require_close (parser
))
7830 /* We try and resync to an unnested comma, as that will give the
7831 user better diagnostics. */
7832 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7833 /*recovering=*/true,
7835 /*consume_paren=*/true);
7840 parser
->greater_than_is_operator_p
7841 = saved_greater_than_is_operator_p
;
7846 parser
->greater_than_is_operator_p
7847 = saved_greater_than_is_operator_p
;
7850 vec_safe_insert (expression_list
, 0, identifier
);
7852 return expression_list
;
7855 /* Parse a pseudo-destructor-name.
7857 pseudo-destructor-name:
7858 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7859 :: [opt] nested-name-specifier template template-id :: ~ type-name
7860 :: [opt] nested-name-specifier [opt] ~ type-name
7862 If either of the first two productions is used, sets *SCOPE to the
7863 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7864 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7865 or ERROR_MARK_NODE if the parse fails. */
7868 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7873 bool nested_name_specifier_p
;
7876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7877 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7878 && !type_dependent_expression_p (object
))
7880 if (cxx_dialect
< cxx14
)
7881 pedwarn (input_location
, 0,
7882 "%<~auto%> only available with "
7883 "-std=c++14 or -std=gnu++14");
7884 cp_lexer_consume_token (parser
->lexer
);
7885 cp_lexer_consume_token (parser
->lexer
);
7887 *type
= TREE_TYPE (object
);
7891 /* Assume that things will not work out. */
7892 *type
= error_mark_node
;
7894 /* Look for the optional `::' operator. */
7895 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7896 /* Look for the optional nested-name-specifier. */
7897 nested_name_specifier_p
7898 = (cp_parser_nested_name_specifier_opt (parser
,
7899 /*typename_keyword_p=*/false,
7900 /*check_dependency_p=*/true,
7902 /*is_declaration=*/false)
7904 /* Now, if we saw a nested-name-specifier, we might be doing the
7905 second production. */
7906 if (nested_name_specifier_p
7907 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7909 /* Consume the `template' keyword. */
7910 cp_lexer_consume_token (parser
->lexer
);
7911 /* Parse the template-id. */
7912 cp_parser_template_id (parser
,
7913 /*template_keyword_p=*/true,
7914 /*check_dependency_p=*/false,
7916 /*is_declaration=*/true);
7917 /* Look for the `::' token. */
7918 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7920 /* If the next token is not a `~', then there might be some
7921 additional qualification. */
7922 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7924 /* At this point, we're looking for "type-name :: ~". The type-name
7925 must not be a class-name, since this is a pseudo-destructor. So,
7926 it must be either an enum-name, or a typedef-name -- both of which
7927 are just identifiers. So, we peek ahead to check that the "::"
7928 and "~" tokens are present; if they are not, then we can avoid
7929 calling type_name. */
7930 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7931 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7932 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7934 cp_parser_error (parser
, "non-scalar type");
7938 /* Look for the type-name. */
7939 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7940 if (*scope
== error_mark_node
)
7943 /* Look for the `::' token. */
7944 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7949 /* Look for the `~'. */
7950 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7952 /* Once we see the ~, this has to be a pseudo-destructor. */
7953 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7954 cp_parser_commit_to_topmost_tentative_parse (parser
);
7956 /* Look for the type-name again. We are not responsible for
7957 checking that it matches the first type-name. */
7958 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7961 /* Parse a unary-expression.
7967 unary-operator cast-expression
7968 sizeof unary-expression
7970 alignof ( type-id ) [C++0x]
7977 __extension__ cast-expression
7978 __alignof__ unary-expression
7979 __alignof__ ( type-id )
7980 alignof unary-expression [C++0x]
7981 __real__ cast-expression
7982 __imag__ cast-expression
7984 sizeof ( type-id ) { initializer-list , [opt] }
7985 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7986 __alignof__ ( type-id ) { initializer-list , [opt] }
7988 ADDRESS_P is true iff the unary-expression is appearing as the
7989 operand of the `&' operator. CAST_P is true if this expression is
7990 the target of a cast.
7992 Returns a representation of the expression. */
7995 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7996 bool address_p
, bool cast_p
, bool decltype_p
)
7999 enum tree_code unary_operator
;
8001 /* Peek at the next token. */
8002 token
= cp_lexer_peek_token (parser
->lexer
);
8003 /* Some keywords give away the kind of expression. */
8004 if (token
->type
== CPP_KEYWORD
)
8006 enum rid keyword
= token
->keyword
;
8015 location_t start_loc
= token
->location
;
8017 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8018 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8020 /* Consume the token. */
8021 cp_lexer_consume_token (parser
->lexer
);
8022 /* Parse the operand. */
8023 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8025 if (TYPE_P (operand
))
8026 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8030 /* ISO C++ defines alignof only with types, not with
8031 expressions. So pedwarn if alignof is used with a non-
8032 type expression. However, __alignof__ is ok. */
8034 pedwarn (token
->location
, OPT_Wpedantic
,
8035 "ISO C++ does not allow %<alignof%> "
8038 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8040 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8041 SIZEOF_EXPR with the original operand. */
8042 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8044 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8046 if (!processing_template_decl
&& TYPE_P (operand
))
8048 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8049 build1 (NOP_EXPR
, operand
,
8051 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8054 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8055 TREE_SIDE_EFFECTS (ret
) = 0;
8056 TREE_READONLY (ret
) = 1;
8060 /* Construct a location e.g. :
8063 with start == caret at the start of the "alignof"/"sizeof"
8064 token, with the endpoint at the final closing paren. */
8065 location_t finish_loc
8066 = cp_lexer_previous_token (parser
->lexer
)->location
;
8067 location_t compound_loc
8068 = make_location (start_loc
, start_loc
, finish_loc
);
8070 cp_expr
ret_expr (ret
);
8071 ret_expr
.set_location (compound_loc
);
8072 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8077 return cp_parser_new_expression (parser
);
8080 return cp_parser_delete_expression (parser
);
8084 /* The saved value of the PEDANTIC flag. */
8088 /* Save away the PEDANTIC flag. */
8089 cp_parser_extension_opt (parser
, &saved_pedantic
);
8090 /* Parse the cast-expression. */
8091 expr
= cp_parser_simple_cast_expression (parser
);
8092 /* Restore the PEDANTIC flag. */
8093 pedantic
= saved_pedantic
;
8103 /* Consume the `__real__' or `__imag__' token. */
8104 cp_lexer_consume_token (parser
->lexer
);
8105 /* Parse the cast-expression. */
8106 expression
= cp_parser_simple_cast_expression (parser
);
8107 /* Create the complete representation. */
8108 return build_x_unary_op (token
->location
,
8109 (keyword
== RID_REALPART
8110 ? REALPART_EXPR
: IMAGPART_EXPR
),
8112 tf_warning_or_error
);
8116 case RID_TRANSACTION_ATOMIC
:
8117 case RID_TRANSACTION_RELAXED
:
8118 return cp_parser_transaction_expression (parser
, keyword
);
8123 const char *saved_message
;
8124 bool saved_integral_constant_expression_p
;
8125 bool saved_non_integral_constant_expression_p
;
8126 bool saved_greater_than_is_operator_p
;
8128 location_t start_loc
= token
->location
;
8130 cp_lexer_consume_token (parser
->lexer
);
8131 matching_parens parens
;
8132 parens
.require_open (parser
);
8134 saved_message
= parser
->type_definition_forbidden_message
;
8135 parser
->type_definition_forbidden_message
8136 = G_("types may not be defined in %<noexcept%> expressions");
8138 saved_integral_constant_expression_p
8139 = parser
->integral_constant_expression_p
;
8140 saved_non_integral_constant_expression_p
8141 = parser
->non_integral_constant_expression_p
;
8142 parser
->integral_constant_expression_p
= false;
8144 saved_greater_than_is_operator_p
8145 = parser
->greater_than_is_operator_p
;
8146 parser
->greater_than_is_operator_p
= true;
8148 ++cp_unevaluated_operand
;
8149 ++c_inhibit_evaluation_warnings
;
8150 ++cp_noexcept_operand
;
8151 expr
= cp_parser_expression (parser
);
8152 --cp_noexcept_operand
;
8153 --c_inhibit_evaluation_warnings
;
8154 --cp_unevaluated_operand
;
8156 parser
->greater_than_is_operator_p
8157 = saved_greater_than_is_operator_p
;
8159 parser
->integral_constant_expression_p
8160 = saved_integral_constant_expression_p
;
8161 parser
->non_integral_constant_expression_p
8162 = saved_non_integral_constant_expression_p
;
8164 parser
->type_definition_forbidden_message
= saved_message
;
8166 location_t finish_loc
8167 = cp_lexer_peek_token (parser
->lexer
)->location
;
8168 parens
.require_close (parser
);
8170 /* Construct a location of the form:
8173 with start == caret, finishing at the close-paren. */
8174 location_t noexcept_loc
8175 = make_location (start_loc
, start_loc
, finish_loc
);
8177 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8186 /* Look for the `:: new' and `:: delete', which also signal the
8187 beginning of a new-expression, or delete-expression,
8188 respectively. If the next token is `::', then it might be one of
8190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8194 /* See if the token after the `::' is one of the keywords in
8195 which we're interested. */
8196 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8197 /* If it's `new', we have a new-expression. */
8198 if (keyword
== RID_NEW
)
8199 return cp_parser_new_expression (parser
);
8200 /* Similarly, for `delete'. */
8201 else if (keyword
== RID_DELETE
)
8202 return cp_parser_delete_expression (parser
);
8205 /* Look for a unary operator. */
8206 unary_operator
= cp_parser_unary_operator (token
);
8207 /* The `++' and `--' operators can be handled similarly, even though
8208 they are not technically unary-operators in the grammar. */
8209 if (unary_operator
== ERROR_MARK
)
8211 if (token
->type
== CPP_PLUS_PLUS
)
8212 unary_operator
= PREINCREMENT_EXPR
;
8213 else if (token
->type
== CPP_MINUS_MINUS
)
8214 unary_operator
= PREDECREMENT_EXPR
;
8215 /* Handle the GNU address-of-label extension. */
8216 else if (cp_parser_allow_gnu_extensions_p (parser
)
8217 && token
->type
== CPP_AND_AND
)
8221 location_t start_loc
= token
->location
;
8223 /* Consume the '&&' token. */
8224 cp_lexer_consume_token (parser
->lexer
);
8225 /* Look for the identifier. */
8226 location_t finish_loc
8227 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8228 identifier
= cp_parser_identifier (parser
);
8229 /* Construct a location of the form:
8232 with caret==start at the "&&", finish at the end of the label. */
8233 location_t combined_loc
8234 = make_location (start_loc
, start_loc
, finish_loc
);
8235 /* Create an expression representing the address. */
8236 expression
= finish_label_address_expr (identifier
, combined_loc
);
8237 if (cp_parser_non_integral_constant_expression (parser
,
8239 expression
= error_mark_node
;
8243 if (unary_operator
!= ERROR_MARK
)
8245 cp_expr cast_expression
;
8246 cp_expr expression
= error_mark_node
;
8247 non_integral_constant non_constant_p
= NIC_NONE
;
8248 location_t loc
= token
->location
;
8249 tsubst_flags_t complain
= complain_flags (decltype_p
);
8251 /* Consume the operator token. */
8252 token
= cp_lexer_consume_token (parser
->lexer
);
8253 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8255 /* Parse the cast-expression. */
8257 = cp_parser_cast_expression (parser
,
8258 unary_operator
== ADDR_EXPR
,
8264 OP_TOKEN CAST_EXPRESSION
8265 ^~~~~~~~~~~~~~~~~~~~~~~~~
8266 with start==caret at the operator token, and
8267 extending to the end of the cast_expression. */
8268 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8270 /* Now, build an appropriate representation. */
8271 switch (unary_operator
)
8274 non_constant_p
= NIC_STAR
;
8275 expression
= build_x_indirect_ref (loc
, cast_expression
,
8278 /* TODO: build_x_indirect_ref does not always honor the
8279 location, so ensure it is set. */
8280 expression
.set_location (loc
);
8284 non_constant_p
= NIC_ADDR
;
8287 expression
= build_x_unary_op (loc
, unary_operator
,
8290 /* TODO: build_x_unary_op does not always honor the location,
8291 so ensure it is set. */
8292 expression
.set_location (loc
);
8295 case PREINCREMENT_EXPR
:
8296 case PREDECREMENT_EXPR
:
8297 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8298 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8301 /* Immediately fold negation of a constant, unless the constant is 0
8302 (since -0 == 0) or it would overflow. */
8303 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8304 && CONSTANT_CLASS_P (cast_expression
)
8305 && !integer_zerop (cast_expression
)
8306 && !TREE_OVERFLOW (cast_expression
))
8308 tree folded
= fold_build1 (unary_operator
,
8309 TREE_TYPE (cast_expression
),
8311 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8313 expression
= cp_expr (folded
, loc
);
8318 case UNARY_PLUS_EXPR
:
8319 case TRUTH_NOT_EXPR
:
8320 expression
= finish_unary_op_expr (loc
, unary_operator
,
8321 cast_expression
, complain
);
8328 if (non_constant_p
!= NIC_NONE
8329 && cp_parser_non_integral_constant_expression (parser
,
8331 expression
= error_mark_node
;
8336 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8337 /*member_access_only_p=*/false,
8342 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8343 unary-operator, the corresponding tree code is returned. */
8345 static enum tree_code
8346 cp_parser_unary_operator (cp_token
* token
)
8348 switch (token
->type
)
8351 return INDIRECT_REF
;
8357 return UNARY_PLUS_EXPR
;
8363 return TRUTH_NOT_EXPR
;
8366 return BIT_NOT_EXPR
;
8373 /* Parse a new-expression.
8376 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8377 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8379 Returns a representation of the expression. */
8382 cp_parser_new_expression (cp_parser
* parser
)
8384 bool global_scope_p
;
8385 vec
<tree
, va_gc
> *placement
;
8387 vec
<tree
, va_gc
> *initializer
;
8388 tree nelts
= NULL_TREE
;
8391 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8393 /* Look for the optional `::' operator. */
8395 = (cp_parser_global_scope_opt (parser
,
8396 /*current_scope_valid_p=*/false)
8398 /* Look for the `new' operator. */
8399 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8400 /* There's no easy way to tell a new-placement from the
8401 `( type-id )' construct. */
8402 cp_parser_parse_tentatively (parser
);
8403 /* Look for a new-placement. */
8404 placement
= cp_parser_new_placement (parser
);
8405 /* If that didn't work out, there's no new-placement. */
8406 if (!cp_parser_parse_definitely (parser
))
8408 if (placement
!= NULL
)
8409 release_tree_vector (placement
);
8413 /* If the next token is a `(', then we have a parenthesized
8415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8418 const char *saved_message
= parser
->type_definition_forbidden_message
;
8420 /* Consume the `('. */
8421 matching_parens parens
;
8422 parens
.consume_open (parser
);
8424 /* Parse the type-id. */
8425 parser
->type_definition_forbidden_message
8426 = G_("types may not be defined in a new-expression");
8428 type_id_in_expr_sentinel
s (parser
);
8429 type
= cp_parser_type_id (parser
);
8431 parser
->type_definition_forbidden_message
= saved_message
;
8433 /* Look for the closing `)'. */
8434 parens
.require_close (parser
);
8435 token
= cp_lexer_peek_token (parser
->lexer
);
8436 /* There should not be a direct-new-declarator in this production,
8437 but GCC used to allowed this, so we check and emit a sensible error
8438 message for this case. */
8439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8441 error_at (token
->location
,
8442 "array bound forbidden after parenthesized type-id");
8443 inform (token
->location
,
8444 "try removing the parentheses around the type-id");
8445 cp_parser_direct_new_declarator (parser
);
8448 /* Otherwise, there must be a new-type-id. */
8450 type
= cp_parser_new_type_id (parser
, &nelts
);
8452 /* If the next token is a `(' or '{', then we have a new-initializer. */
8453 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8454 if (token
->type
== CPP_OPEN_PAREN
8455 || token
->type
== CPP_OPEN_BRACE
)
8456 initializer
= cp_parser_new_initializer (parser
);
8460 /* A new-expression may not appear in an integral constant
8462 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8463 ret
= error_mark_node
;
8464 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8465 of a new-type-id or type-id of a new-expression, the new-expression shall
8466 contain a new-initializer of the form ( assignment-expression )".
8467 Additionally, consistently with the spirit of DR 1467, we want to accept
8468 'new auto { 2 }' too. */
8469 else if ((ret
= type_uses_auto (type
))
8470 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8471 && (vec_safe_length (initializer
) != 1
8472 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8473 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8475 error_at (token
->location
,
8476 "initialization of new-expression for type %<auto%> "
8477 "requires exactly one element");
8478 ret
= error_mark_node
;
8482 /* Construct a location e.g.:
8485 with caret == start at the start of the "new" token, and the end
8486 at the end of the final token we consumed. */
8487 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8488 location_t end_loc
= get_finish (end_tok
->location
);
8489 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8491 /* Create a representation of the new-expression. */
8492 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8493 tf_warning_or_error
);
8494 protected_set_expr_location (ret
, combined_loc
);
8497 if (placement
!= NULL
)
8498 release_tree_vector (placement
);
8499 if (initializer
!= NULL
)
8500 release_tree_vector (initializer
);
8505 /* Parse a new-placement.
8510 Returns the same representation as for an expression-list. */
8512 static vec
<tree
, va_gc
> *
8513 cp_parser_new_placement (cp_parser
* parser
)
8515 vec
<tree
, va_gc
> *expression_list
;
8517 /* Parse the expression-list. */
8518 expression_list
= (cp_parser_parenthesized_expression_list
8519 (parser
, non_attr
, /*cast_p=*/false,
8520 /*allow_expansion_p=*/true,
8521 /*non_constant_p=*/NULL
));
8523 if (expression_list
&& expression_list
->is_empty ())
8524 error ("expected expression-list or type-id");
8526 return expression_list
;
8529 /* Parse a new-type-id.
8532 type-specifier-seq new-declarator [opt]
8534 Returns the TYPE allocated. If the new-type-id indicates an array
8535 type, *NELTS is set to the number of elements in the last array
8536 bound; the TYPE will not include the last array bound. */
8539 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8541 cp_decl_specifier_seq type_specifier_seq
;
8542 cp_declarator
*new_declarator
;
8543 cp_declarator
*declarator
;
8544 cp_declarator
*outer_declarator
;
8545 const char *saved_message
;
8547 /* The type-specifier sequence must not contain type definitions.
8548 (It cannot contain declarations of new types either, but if they
8549 are not definitions we will catch that because they are not
8551 saved_message
= parser
->type_definition_forbidden_message
;
8552 parser
->type_definition_forbidden_message
8553 = G_("types may not be defined in a new-type-id");
8554 /* Parse the type-specifier-seq. */
8555 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8556 /*is_trailing_return=*/false,
8557 &type_specifier_seq
);
8558 /* Restore the old message. */
8559 parser
->type_definition_forbidden_message
= saved_message
;
8561 if (type_specifier_seq
.type
== error_mark_node
)
8562 return error_mark_node
;
8564 /* Parse the new-declarator. */
8565 new_declarator
= cp_parser_new_declarator_opt (parser
);
8567 /* Determine the number of elements in the last array dimension, if
8570 /* Skip down to the last array dimension. */
8571 declarator
= new_declarator
;
8572 outer_declarator
= NULL
;
8573 while (declarator
&& (declarator
->kind
== cdk_pointer
8574 || declarator
->kind
== cdk_ptrmem
))
8576 outer_declarator
= declarator
;
8577 declarator
= declarator
->declarator
;
8580 && declarator
->kind
== cdk_array
8581 && declarator
->declarator
8582 && declarator
->declarator
->kind
== cdk_array
)
8584 outer_declarator
= declarator
;
8585 declarator
= declarator
->declarator
;
8588 if (declarator
&& declarator
->kind
== cdk_array
)
8590 *nelts
= declarator
->u
.array
.bounds
;
8591 if (*nelts
== error_mark_node
)
8592 *nelts
= integer_one_node
;
8594 if (outer_declarator
)
8595 outer_declarator
->declarator
= declarator
->declarator
;
8597 new_declarator
= NULL
;
8600 return groktypename (&type_specifier_seq
, new_declarator
, false);
8603 /* Parse an (optional) new-declarator.
8606 ptr-operator new-declarator [opt]
8607 direct-new-declarator
8609 Returns the declarator. */
8611 static cp_declarator
*
8612 cp_parser_new_declarator_opt (cp_parser
* parser
)
8614 enum tree_code code
;
8615 tree type
, std_attributes
= NULL_TREE
;
8616 cp_cv_quals cv_quals
;
8618 /* We don't know if there's a ptr-operator next, or not. */
8619 cp_parser_parse_tentatively (parser
);
8620 /* Look for a ptr-operator. */
8621 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8622 /* If that worked, look for more new-declarators. */
8623 if (cp_parser_parse_definitely (parser
))
8625 cp_declarator
*declarator
;
8627 /* Parse another optional declarator. */
8628 declarator
= cp_parser_new_declarator_opt (parser
);
8630 declarator
= cp_parser_make_indirect_declarator
8631 (code
, type
, cv_quals
, declarator
, std_attributes
);
8636 /* If the next token is a `[', there is a direct-new-declarator. */
8637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8638 return cp_parser_direct_new_declarator (parser
);
8643 /* Parse a direct-new-declarator.
8645 direct-new-declarator:
8647 direct-new-declarator [constant-expression]
8651 static cp_declarator
*
8652 cp_parser_direct_new_declarator (cp_parser
* parser
)
8654 cp_declarator
*declarator
= NULL
;
8661 /* Look for the opening `['. */
8662 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8664 token
= cp_lexer_peek_token (parser
->lexer
);
8665 expression
= cp_parser_expression (parser
);
8666 /* The standard requires that the expression have integral
8667 type. DR 74 adds enumeration types. We believe that the
8668 real intent is that these expressions be handled like the
8669 expression in a `switch' condition, which also allows
8670 classes with a single conversion to integral or
8671 enumeration type. */
8672 if (!processing_template_decl
)
8675 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8680 error_at (token
->location
,
8681 "expression in new-declarator must have integral "
8682 "or enumeration type");
8683 expression
= error_mark_node
;
8687 /* Look for the closing `]'. */
8688 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8690 /* Add this bound to the declarator. */
8691 declarator
= make_array_declarator (declarator
, expression
);
8693 /* If the next token is not a `[', then there are no more
8695 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8702 /* Parse a new-initializer.
8705 ( expression-list [opt] )
8708 Returns a representation of the expression-list. */
8710 static vec
<tree
, va_gc
> *
8711 cp_parser_new_initializer (cp_parser
* parser
)
8713 vec
<tree
, va_gc
> *expression_list
;
8715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8718 bool expr_non_constant_p
;
8719 cp_lexer_set_source_position (parser
->lexer
);
8720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8721 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8722 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8723 expression_list
= make_tree_vector_single (t
);
8726 expression_list
= (cp_parser_parenthesized_expression_list
8727 (parser
, non_attr
, /*cast_p=*/false,
8728 /*allow_expansion_p=*/true,
8729 /*non_constant_p=*/NULL
));
8731 return expression_list
;
8734 /* Parse a delete-expression.
8737 :: [opt] delete cast-expression
8738 :: [opt] delete [ ] cast-expression
8740 Returns a representation of the expression. */
8743 cp_parser_delete_expression (cp_parser
* parser
)
8745 bool global_scope_p
;
8749 /* Look for the optional `::' operator. */
8751 = (cp_parser_global_scope_opt (parser
,
8752 /*current_scope_valid_p=*/false)
8754 /* Look for the `delete' keyword. */
8755 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8756 /* See if the array syntax is in use. */
8757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8759 /* Consume the `[' token. */
8760 cp_lexer_consume_token (parser
->lexer
);
8761 /* Look for the `]' token. */
8762 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8763 /* Remember that this is the `[]' construct. */
8769 /* Parse the cast-expression. */
8770 expression
= cp_parser_simple_cast_expression (parser
);
8772 /* A delete-expression may not appear in an integral constant
8774 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8775 return error_mark_node
;
8777 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8778 tf_warning_or_error
);
8781 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8782 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8786 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8788 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8789 switch (token
->type
)
8795 case CPP_CLOSE_SQUARE
:
8796 case CPP_CLOSE_PAREN
:
8797 case CPP_CLOSE_BRACE
:
8798 case CPP_OPEN_BRACE
:
8802 case CPP_DEREF_STAR
:
8810 case CPP_GREATER_EQ
:
8831 case CPP_OPEN_PAREN
:
8832 /* In ((type ()) () the last () isn't a valid cast-expression,
8833 so the whole must be parsed as postfix-expression. */
8834 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8837 case CPP_OPEN_SQUARE
:
8838 /* '[' may start a primary-expression in obj-c++ and in C++11,
8839 as a lambda-expression, eg, '(void)[]{}'. */
8840 if (cxx_dialect
>= cxx11
)
8842 return c_dialect_objc ();
8845 case CPP_MINUS_MINUS
:
8846 /* '++' and '--' may or may not start a cast-expression:
8848 struct T { void operator++(int); };
8849 void f() { (T())++; }
8862 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8863 in the order: const_cast, static_cast, reinterpret_cast.
8865 Don't suggest dynamic_cast.
8867 Return the first legal cast kind found, or NULL otherwise. */
8870 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8874 /* Reuse the parser logic by attempting to build the various kinds of
8875 cast, with "complain" disabled.
8876 Identify the first such cast that is valid. */
8878 /* Don't attempt to run such logic within template processing. */
8879 if (processing_template_decl
)
8882 /* First try const_cast. */
8883 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8884 if (trial
!= error_mark_node
)
8885 return "const_cast";
8887 /* If that fails, try static_cast. */
8888 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8889 if (trial
!= error_mark_node
)
8890 return "static_cast";
8892 /* Finally, try reinterpret_cast. */
8893 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8894 if (trial
!= error_mark_node
)
8895 return "reinterpret_cast";
8897 /* No such cast possible. */
8901 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8902 suggesting how to convert a C-style cast of the form:
8906 to a C++-style cast.
8908 The primary range of RICHLOC is asssumed to be that of the original
8909 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8910 of the parens in the C-style cast. */
8913 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8914 location_t close_paren_loc
, tree orig_expr
,
8917 /* This function is non-trivial, so bail out now if the warning isn't
8918 going to be emitted. */
8919 if (!warn_old_style_cast
)
8922 /* Try to find a legal C++ cast, trying them in order:
8923 const_cast, static_cast, reinterpret_cast. */
8924 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8925 if (!cast_suggestion
)
8928 /* Replace the open paren with "CAST_SUGGESTION<". */
8930 pp_printf (&pp
, "%s<", cast_suggestion
);
8931 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8933 /* Replace the close paren with "> (". */
8934 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8936 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8937 rich_loc
->add_fixit_insert_after (")");
8941 /* Parse a cast-expression.
8945 ( type-id ) cast-expression
8947 ADDRESS_P is true iff the unary-expression is appearing as the
8948 operand of the `&' operator. CAST_P is true if this expression is
8949 the target of a cast.
8951 Returns a representation of the expression. */
8954 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8955 bool decltype_p
, cp_id_kind
* pidk
)
8957 /* If it's a `(', then we might be looking at a cast. */
8958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8960 tree type
= NULL_TREE
;
8961 cp_expr
expr (NULL_TREE
);
8962 int cast_expression
= 0;
8963 const char *saved_message
;
8965 /* There's no way to know yet whether or not this is a cast.
8966 For example, `(int (3))' is a unary-expression, while `(int)
8967 3' is a cast. So, we resort to parsing tentatively. */
8968 cp_parser_parse_tentatively (parser
);
8969 /* Types may not be defined in a cast. */
8970 saved_message
= parser
->type_definition_forbidden_message
;
8971 parser
->type_definition_forbidden_message
8972 = G_("types may not be defined in casts");
8973 /* Consume the `('. */
8974 matching_parens parens
;
8975 cp_token
*open_paren
= parens
.consume_open (parser
);
8976 location_t open_paren_loc
= open_paren
->location
;
8977 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8979 /* A very tricky bit is that `(struct S) { 3 }' is a
8980 compound-literal (which we permit in C++ as an extension).
8981 But, that construct is not a cast-expression -- it is a
8982 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8983 is legal; if the compound-literal were a cast-expression,
8984 you'd need an extra set of parentheses.) But, if we parse
8985 the type-id, and it happens to be a class-specifier, then we
8986 will commit to the parse at that point, because we cannot
8987 undo the action that is done when creating a new class. So,
8988 then we cannot back up and do a postfix-expression.
8990 Another tricky case is the following (c++/29234):
8992 struct S { void operator () (); };
8999 As a type-id we parse the parenthesized S()() as a function
9000 returning a function, groktypename complains and we cannot
9001 back up in this case either.
9003 Therefore, we scan ahead to the closing `)', and check to see
9004 if the tokens after the `)' can start a cast-expression. Otherwise
9005 we are dealing with an unary-expression, a postfix-expression
9008 Yet another tricky case, in C++11, is the following (c++/54891):
9012 The issue is that usually, besides the case of lambda-expressions,
9013 the parenthesized type-id cannot be followed by '[', and, eg, we
9014 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9015 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9016 we don't commit, we try a cast-expression, then an unary-expression.
9018 Save tokens so that we can put them back. */
9019 cp_lexer_save_tokens (parser
->lexer
);
9021 /* We may be looking at a cast-expression. */
9022 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9023 /*consume_paren=*/true))
9025 = cp_parser_tokens_start_cast_expression (parser
);
9027 /* Roll back the tokens we skipped. */
9028 cp_lexer_rollback_tokens (parser
->lexer
);
9029 /* If we aren't looking at a cast-expression, simulate an error so
9030 that the call to cp_parser_error_occurred below returns true. */
9031 if (!cast_expression
)
9032 cp_parser_simulate_error (parser
);
9035 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9036 parser
->in_type_id_in_expr_p
= true;
9037 /* Look for the type-id. */
9038 type
= cp_parser_type_id (parser
);
9039 /* Look for the closing `)'. */
9040 cp_token
*close_paren
= parens
.require_close (parser
);
9042 close_paren_loc
= close_paren
->location
;
9043 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9046 /* Restore the saved message. */
9047 parser
->type_definition_forbidden_message
= saved_message
;
9049 /* At this point this can only be either a cast or a
9050 parenthesized ctor such as `(T ())' that looks like a cast to
9051 function returning T. */
9052 if (!cp_parser_error_occurred (parser
))
9054 /* Only commit if the cast-expression doesn't start with
9055 '++', '--', or '[' in C++11. */
9056 if (cast_expression
> 0)
9057 cp_parser_commit_to_topmost_tentative_parse (parser
);
9059 expr
= cp_parser_cast_expression (parser
,
9060 /*address_p=*/false,
9062 /*decltype_p=*/false,
9065 if (cp_parser_parse_definitely (parser
))
9067 /* Warn about old-style casts, if so requested. */
9068 if (warn_old_style_cast
9069 && !in_system_header_at (input_location
)
9070 && !VOID_TYPE_P (type
)
9071 && current_lang_name
!= lang_name_c
)
9073 gcc_rich_location
rich_loc (input_location
);
9074 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9076 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9077 "use of old-style cast to %q#T", type
);
9080 /* Only type conversions to integral or enumeration types
9081 can be used in constant-expressions. */
9082 if (!cast_valid_in_integral_constant_expression_p (type
)
9083 && cp_parser_non_integral_constant_expression (parser
,
9085 return error_mark_node
;
9087 /* Perform the cast. */
9091 with start==caret at the open paren, extending to the
9093 location_t cast_loc
= make_location (open_paren_loc
,
9095 expr
.get_finish ());
9096 expr
= build_c_cast (cast_loc
, type
, expr
);
9101 cp_parser_abort_tentative_parse (parser
);
9104 /* If we get here, then it's not a cast, so it must be a
9105 unary-expression. */
9106 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9107 cast_p
, decltype_p
);
9110 /* Parse a binary expression of the general form:
9114 pm-expression .* cast-expression
9115 pm-expression ->* cast-expression
9117 multiplicative-expression:
9119 multiplicative-expression * pm-expression
9120 multiplicative-expression / pm-expression
9121 multiplicative-expression % pm-expression
9123 additive-expression:
9124 multiplicative-expression
9125 additive-expression + multiplicative-expression
9126 additive-expression - multiplicative-expression
9130 shift-expression << additive-expression
9131 shift-expression >> additive-expression
9133 relational-expression:
9135 relational-expression < shift-expression
9136 relational-expression > shift-expression
9137 relational-expression <= shift-expression
9138 relational-expression >= shift-expression
9142 relational-expression:
9143 relational-expression <? shift-expression
9144 relational-expression >? shift-expression
9146 equality-expression:
9147 relational-expression
9148 equality-expression == relational-expression
9149 equality-expression != relational-expression
9153 and-expression & equality-expression
9155 exclusive-or-expression:
9157 exclusive-or-expression ^ and-expression
9159 inclusive-or-expression:
9160 exclusive-or-expression
9161 inclusive-or-expression | exclusive-or-expression
9163 logical-and-expression:
9164 inclusive-or-expression
9165 logical-and-expression && inclusive-or-expression
9167 logical-or-expression:
9168 logical-and-expression
9169 logical-or-expression || logical-and-expression
9171 All these are implemented with a single function like:
9174 simple-cast-expression
9175 binary-expression <token> binary-expression
9177 CAST_P is true if this expression is the target of a cast.
9179 The binops_by_token map is used to get the tree codes for each <token> type.
9180 binary-expressions are associated according to a precedence table. */
9182 #define TOKEN_PRECEDENCE(token) \
9183 (((token->type == CPP_GREATER \
9184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9185 && !parser->greater_than_is_operator_p) \
9186 ? PREC_NOT_OPERATOR \
9187 : binops_by_token[token->type].prec)
9190 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9191 bool no_toplevel_fold_p
,
9193 enum cp_parser_prec prec
,
9196 cp_parser_expression_stack stack
;
9197 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9198 cp_parser_expression_stack_entry current
;
9201 enum tree_code rhs_type
;
9202 enum cp_parser_prec new_prec
, lookahead_prec
;
9205 /* Parse the first expression. */
9206 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9207 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9208 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9209 cast_p
, decltype_p
, pidk
);
9210 current
.prec
= prec
;
9212 if (cp_parser_error_occurred (parser
))
9213 return error_mark_node
;
9217 /* Get an operator token. */
9218 token
= cp_lexer_peek_token (parser
->lexer
);
9220 if (warn_cxx11_compat
9221 && token
->type
== CPP_RSHIFT
9222 && !parser
->greater_than_is_operator_p
)
9224 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9225 "%<>>%> operator is treated"
9226 " as two right angle brackets in C++11"))
9227 inform (token
->location
,
9228 "suggest parentheses around %<>>%> expression");
9231 new_prec
= TOKEN_PRECEDENCE (token
);
9232 if (new_prec
!= PREC_NOT_OPERATOR
9233 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9234 /* This is a fold-expression; handle it later. */
9235 new_prec
= PREC_NOT_OPERATOR
;
9237 /* Popping an entry off the stack means we completed a subexpression:
9238 - either we found a token which is not an operator (`>' where it is not
9239 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9240 will happen repeatedly;
9241 - or, we found an operator which has lower priority. This is the case
9242 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9244 if (new_prec
<= current
.prec
)
9253 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9254 current
.loc
= token
->location
;
9256 /* We used the operator token. */
9257 cp_lexer_consume_token (parser
->lexer
);
9259 /* For "false && x" or "true || x", x will never be executed;
9260 disable warnings while evaluating it. */
9261 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9262 c_inhibit_evaluation_warnings
+=
9263 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9264 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9265 c_inhibit_evaluation_warnings
+=
9266 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9268 /* Extract another operand. It may be the RHS of this expression
9269 or the LHS of a new, higher priority expression. */
9270 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9271 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9272 rhs
= cp_parser_simple_cast_expression (parser
);
9274 /* Get another operator token. Look up its precedence to avoid
9275 building a useless (immediately popped) stack entry for common
9276 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9277 token
= cp_lexer_peek_token (parser
->lexer
);
9278 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9279 if (lookahead_prec
!= PREC_NOT_OPERATOR
9280 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9281 lookahead_prec
= PREC_NOT_OPERATOR
;
9282 if (lookahead_prec
> new_prec
)
9284 /* ... and prepare to parse the RHS of the new, higher priority
9285 expression. Since precedence levels on the stack are
9286 monotonically increasing, we do not have to care about
9291 current
.lhs_type
= rhs_type
;
9292 current
.prec
= new_prec
;
9293 new_prec
= lookahead_prec
;
9297 lookahead_prec
= new_prec
;
9298 /* If the stack is not empty, we have parsed into LHS the right side
9299 (`4' in the example above) of an expression we had suspended.
9300 We can use the information on the stack to recover the LHS (`3')
9301 from the stack together with the tree code (`MULT_EXPR'), and
9302 the precedence of the higher level subexpression
9303 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9304 which will be used to actually build the additive expression. */
9306 rhs_type
= current
.lhs_type
;
9311 /* Undo the disabling of warnings done above. */
9312 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9313 c_inhibit_evaluation_warnings
-=
9314 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9315 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9316 c_inhibit_evaluation_warnings
-=
9317 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9319 if (warn_logical_not_paren
9320 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9321 && current
.lhs_type
== TRUTH_NOT_EXPR
9322 /* Avoid warning for !!x == y. */
9323 && (TREE_CODE (current
.lhs
) != NE_EXPR
9324 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9325 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9326 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9327 /* Avoid warning for !b == y where b is boolean. */
9328 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9329 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9331 /* Avoid warning for !!b == y where b is boolean. */
9332 && (!DECL_P (current
.lhs
)
9333 || TREE_TYPE (current
.lhs
) == NULL_TREE
9334 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9335 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9336 current
.lhs
, maybe_constant_value (rhs
));
9340 location_t combined_loc
= make_location (current
.loc
,
9341 current
.lhs
.get_start (),
9344 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9345 ERROR_MARK for everything that is not a binary expression.
9346 This makes warn_about_parentheses miss some warnings that
9347 involve unary operators. For unary expressions we should
9348 pass the correct tree_code unless the unary expression was
9349 surrounded by parentheses.
9351 if (no_toplevel_fold_p
9352 && lookahead_prec
<= current
.prec
9355 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9356 current
.lhs
= error_mark_node
;
9360 = build_min (current
.tree_type
,
9361 TREE_CODE_CLASS (current
.tree_type
)
9363 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9364 current
.lhs
.get_value (), rhs
.get_value ());
9365 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9370 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9371 current
.lhs
, current
.lhs_type
,
9372 rhs
, rhs_type
, &overload
,
9373 complain_flags (decltype_p
));
9374 /* TODO: build_x_binary_op doesn't always honor the location. */
9375 current
.lhs
.set_location (combined_loc
);
9377 current
.lhs_type
= current
.tree_type
;
9379 /* If the binary operator required the use of an overloaded operator,
9380 then this expression cannot be an integral constant-expression.
9381 An overloaded operator can be used even if both operands are
9382 otherwise permissible in an integral constant-expression if at
9383 least one of the operands is of enumeration type. */
9386 && cp_parser_non_integral_constant_expression (parser
,
9388 return error_mark_node
;
9395 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9396 bool no_toplevel_fold_p
,
9397 enum cp_parser_prec prec
,
9400 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9401 /*decltype*/false, prec
, pidk
);
9404 /* Parse the `? expression : assignment-expression' part of a
9405 conditional-expression. The LOGICAL_OR_EXPR is the
9406 logical-or-expression that started the conditional-expression.
9407 Returns a representation of the entire conditional-expression.
9409 This routine is used by cp_parser_assignment_expression.
9411 ? expression : assignment-expression
9415 ? : assignment-expression */
9418 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9420 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9421 cp_expr assignment_expr
;
9422 struct cp_token
*token
;
9423 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9425 /* Consume the `?' token. */
9426 cp_lexer_consume_token (parser
->lexer
);
9427 token
= cp_lexer_peek_token (parser
->lexer
);
9428 if (cp_parser_allow_gnu_extensions_p (parser
)
9429 && token
->type
== CPP_COLON
)
9431 pedwarn (token
->location
, OPT_Wpedantic
,
9432 "ISO C++ does not allow ?: with omitted middle operand");
9433 /* Implicit true clause. */
9435 c_inhibit_evaluation_warnings
+=
9436 folded_logical_or_expr
== truthvalue_true_node
;
9437 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9441 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9442 parser
->colon_corrects_to_scope_p
= false;
9443 /* Parse the expression. */
9444 c_inhibit_evaluation_warnings
+=
9445 folded_logical_or_expr
== truthvalue_false_node
;
9446 expr
= cp_parser_expression (parser
);
9447 c_inhibit_evaluation_warnings
+=
9448 ((folded_logical_or_expr
== truthvalue_true_node
)
9449 - (folded_logical_or_expr
== truthvalue_false_node
));
9450 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9453 /* The next token should be a `:'. */
9454 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9455 /* Parse the assignment-expression. */
9456 assignment_expr
= cp_parser_assignment_expression (parser
);
9457 c_inhibit_evaluation_warnings
-=
9458 folded_logical_or_expr
== truthvalue_true_node
;
9461 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9462 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9463 with the caret at the "?", ranging from the start of
9464 the logical_or_expr to the end of the assignment_expr. */
9465 loc
= make_location (loc
,
9466 logical_or_expr
.get_start (),
9467 assignment_expr
.get_finish ());
9469 /* Build the conditional-expression. */
9470 return build_x_conditional_expr (loc
, logical_or_expr
,
9473 tf_warning_or_error
);
9476 /* Parse an assignment-expression.
9478 assignment-expression:
9479 conditional-expression
9480 logical-or-expression assignment-operator assignment_expression
9483 CAST_P is true if this expression is the target of a cast.
9484 DECLTYPE_P is true if this expression is the operand of decltype.
9486 Returns a representation for the expression. */
9489 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9490 bool cast_p
, bool decltype_p
)
9494 /* If the next token is the `throw' keyword, then we're looking at
9495 a throw-expression. */
9496 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9497 expr
= cp_parser_throw_expression (parser
);
9498 /* Otherwise, it must be that we are looking at a
9499 logical-or-expression. */
9502 /* Parse the binary expressions (logical-or-expression). */
9503 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9505 PREC_NOT_OPERATOR
, pidk
);
9506 /* If the next token is a `?' then we're actually looking at a
9507 conditional-expression. */
9508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9509 return cp_parser_question_colon_clause (parser
, expr
);
9512 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9514 /* If it's an assignment-operator, we're using the second
9516 enum tree_code assignment_operator
9517 = cp_parser_assignment_operator_opt (parser
);
9518 if (assignment_operator
!= ERROR_MARK
)
9520 bool non_constant_p
;
9522 /* Parse the right-hand side of the assignment. */
9523 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9526 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9527 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9529 /* An assignment may not appear in a
9530 constant-expression. */
9531 if (cp_parser_non_integral_constant_expression (parser
,
9533 return error_mark_node
;
9534 /* Build the assignment expression. Its default
9538 is the location of the '=' token as the
9539 caret, ranging from the start of the lhs to the
9541 loc
= make_location (loc
,
9544 expr
= build_x_modify_expr (loc
, expr
,
9545 assignment_operator
,
9547 complain_flags (decltype_p
));
9548 /* TODO: build_x_modify_expr doesn't honor the location,
9549 so we must set it here. */
9550 expr
.set_location (loc
);
9558 /* Parse an (optional) assignment-operator.
9560 assignment-operator: one of
9561 = *= /= %= += -= >>= <<= &= ^= |=
9565 assignment-operator: one of
9568 If the next token is an assignment operator, the corresponding tree
9569 code is returned, and the token is consumed. For example, for
9570 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9571 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9572 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9573 operator, ERROR_MARK is returned. */
9575 static enum tree_code
9576 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9581 /* Peek at the next token. */
9582 token
= cp_lexer_peek_token (parser
->lexer
);
9584 switch (token
->type
)
9595 op
= TRUNC_DIV_EXPR
;
9599 op
= TRUNC_MOD_EXPR
;
9631 /* Nothing else is an assignment operator. */
9635 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9636 if (op
!= ERROR_MARK
9637 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9640 /* If it was an assignment operator, consume it. */
9641 if (op
!= ERROR_MARK
)
9642 cp_lexer_consume_token (parser
->lexer
);
9647 /* Parse an expression.
9650 assignment-expression
9651 expression , assignment-expression
9653 CAST_P is true if this expression is the target of a cast.
9654 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9655 except possibly parenthesized or on the RHS of a comma (N3276).
9657 Returns a representation of the expression. */
9660 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9661 bool cast_p
, bool decltype_p
)
9663 cp_expr expression
= NULL_TREE
;
9664 location_t loc
= UNKNOWN_LOCATION
;
9668 cp_expr assignment_expression
;
9670 /* Parse the next assignment-expression. */
9671 assignment_expression
9672 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9674 /* We don't create a temporary for a call that is the immediate operand
9675 of decltype or on the RHS of a comma. But when we see a comma, we
9676 need to create a temporary for a call on the LHS. */
9677 if (decltype_p
&& !processing_template_decl
9678 && TREE_CODE (assignment_expression
) == CALL_EXPR
9679 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9680 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9681 assignment_expression
9682 = build_cplus_new (TREE_TYPE (assignment_expression
),
9683 assignment_expression
, tf_warning_or_error
);
9685 /* If this is the first assignment-expression, we can just
9688 expression
= assignment_expression
;
9691 /* Create a location with caret at the comma, ranging
9692 from the start of the LHS to the end of the RHS. */
9693 loc
= make_location (loc
,
9694 expression
.get_start (),
9695 assignment_expression
.get_finish ());
9696 expression
= build_x_compound_expr (loc
, expression
,
9697 assignment_expression
,
9698 complain_flags (decltype_p
));
9699 expression
.set_location (loc
);
9701 /* If the next token is not a comma, or we're in a fold-expression, then
9702 we are done with the expression. */
9703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9704 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9706 /* Consume the `,'. */
9707 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9708 cp_lexer_consume_token (parser
->lexer
);
9709 /* A comma operator cannot appear in a constant-expression. */
9710 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9711 expression
= error_mark_node
;
9717 /* Parse a constant-expression.
9719 constant-expression:
9720 conditional-expression
9722 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9723 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9724 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9725 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9726 only parse a conditional-expression, otherwise parse an
9727 assignment-expression. See below for rationale. */
9730 cp_parser_constant_expression (cp_parser
* parser
,
9731 bool allow_non_constant_p
,
9732 bool *non_constant_p
,
9735 bool saved_integral_constant_expression_p
;
9736 bool saved_allow_non_integral_constant_expression_p
;
9737 bool saved_non_integral_constant_expression_p
;
9740 /* It might seem that we could simply parse the
9741 conditional-expression, and then check to see if it were
9742 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9743 one that the compiler can figure out is constant, possibly after
9744 doing some simplifications or optimizations. The standard has a
9745 precise definition of constant-expression, and we must honor
9746 that, even though it is somewhat more restrictive.
9752 is not a legal declaration, because `(2, 3)' is not a
9753 constant-expression. The `,' operator is forbidden in a
9754 constant-expression. However, GCC's constant-folding machinery
9755 will fold this operation to an INTEGER_CST for `3'. */
9757 /* Save the old settings. */
9758 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9759 saved_allow_non_integral_constant_expression_p
9760 = parser
->allow_non_integral_constant_expression_p
;
9761 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9762 /* We are now parsing a constant-expression. */
9763 parser
->integral_constant_expression_p
= true;
9764 parser
->allow_non_integral_constant_expression_p
9765 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9766 parser
->non_integral_constant_expression_p
= false;
9767 /* Although the grammar says "conditional-expression", when not STRICT_P,
9768 we parse an "assignment-expression", which also permits
9769 "throw-expression" and the use of assignment operators. In the case
9770 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9771 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9772 actually essential that we look for an assignment-expression.
9773 For example, cp_parser_initializer_clauses uses this function to
9774 determine whether a particular assignment-expression is in fact
9778 /* Parse the binary expressions (logical-or-expression). */
9779 expression
= cp_parser_binary_expression (parser
, false, false, false,
9780 PREC_NOT_OPERATOR
, NULL
);
9781 /* If the next token is a `?' then we're actually looking at
9782 a conditional-expression; otherwise we're done. */
9783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9784 expression
= cp_parser_question_colon_clause (parser
, expression
);
9787 expression
= cp_parser_assignment_expression (parser
);
9788 /* Restore the old settings. */
9789 parser
->integral_constant_expression_p
9790 = saved_integral_constant_expression_p
;
9791 parser
->allow_non_integral_constant_expression_p
9792 = saved_allow_non_integral_constant_expression_p
;
9793 if (cxx_dialect
>= cxx11
)
9795 /* Require an rvalue constant expression here; that's what our
9796 callers expect. Reference constant expressions are handled
9797 separately in e.g. cp_parser_template_argument. */
9798 tree decay
= expression
;
9799 if (TREE_TYPE (expression
)
9800 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9801 decay
= build_address (expression
);
9802 bool is_const
= potential_rvalue_constant_expression (decay
);
9803 parser
->non_integral_constant_expression_p
= !is_const
;
9804 if (!is_const
&& !allow_non_constant_p
)
9805 require_potential_rvalue_constant_expression (decay
);
9807 if (allow_non_constant_p
)
9808 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9809 parser
->non_integral_constant_expression_p
9810 = saved_non_integral_constant_expression_p
;
9815 /* Parse __builtin_offsetof.
9817 offsetof-expression:
9818 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9820 offsetof-member-designator:
9822 | offsetof-member-designator "." id-expression
9823 | offsetof-member-designator "[" expression "]"
9824 | offsetof-member-designator "->" id-expression */
9827 cp_parser_builtin_offsetof (cp_parser
*parser
)
9829 int save_ice_p
, save_non_ice_p
;
9834 location_t finish_loc
;
9836 /* We're about to accept non-integral-constant things, but will
9837 definitely yield an integral constant expression. Save and
9838 restore these values around our local parsing. */
9839 save_ice_p
= parser
->integral_constant_expression_p
;
9840 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9842 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9844 /* Consume the "__builtin_offsetof" token. */
9845 cp_lexer_consume_token (parser
->lexer
);
9846 /* Consume the opening `('. */
9847 matching_parens parens
;
9848 parens
.require_open (parser
);
9849 /* Parse the type-id. */
9850 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9852 const char *saved_message
= parser
->type_definition_forbidden_message
;
9853 parser
->type_definition_forbidden_message
9854 = G_("types may not be defined within __builtin_offsetof");
9855 type
= cp_parser_type_id (parser
);
9856 parser
->type_definition_forbidden_message
= saved_message
;
9858 /* Look for the `,'. */
9859 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9860 token
= cp_lexer_peek_token (parser
->lexer
);
9862 /* Build the (type *)null that begins the traditional offsetof macro. */
9864 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9865 tf_warning_or_error
);
9867 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9868 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9869 true, &dummy
, token
->location
);
9872 token
= cp_lexer_peek_token (parser
->lexer
);
9873 switch (token
->type
)
9875 case CPP_OPEN_SQUARE
:
9876 /* offsetof-member-designator "[" expression "]" */
9877 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9882 /* offsetof-member-designator "->" identifier */
9883 expr
= grok_array_decl (token
->location
, expr
,
9884 integer_zero_node
, false);
9888 /* offsetof-member-designator "." identifier */
9889 cp_lexer_consume_token (parser
->lexer
);
9890 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9895 case CPP_CLOSE_PAREN
:
9896 /* Consume the ")" token. */
9897 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9898 cp_lexer_consume_token (parser
->lexer
);
9902 /* Error. We know the following require will fail, but
9903 that gives the proper error message. */
9904 parens
.require_close (parser
);
9905 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9906 expr
= error_mark_node
;
9912 /* Make a location of the form:
9913 __builtin_offsetof (struct s, f)
9914 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9915 with caret at the type-id, ranging from the start of the
9916 "_builtin_offsetof" token to the close paren. */
9917 loc
= make_location (loc
, start_loc
, finish_loc
);
9918 /* The result will be an INTEGER_CST, so we need to explicitly
9919 preserve the location. */
9920 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9923 parser
->integral_constant_expression_p
= save_ice_p
;
9924 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9926 expr
= expr
.maybe_add_location_wrapper ();
9930 /* Parse a trait expression.
9932 Returns a representation of the expression, the underlying type
9933 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9936 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9939 tree type1
, type2
= NULL_TREE
;
9940 bool binary
= false;
9941 bool variadic
= false;
9945 case RID_HAS_NOTHROW_ASSIGN
:
9946 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9948 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9949 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9951 case RID_HAS_NOTHROW_COPY
:
9952 kind
= CPTK_HAS_NOTHROW_COPY
;
9954 case RID_HAS_TRIVIAL_ASSIGN
:
9955 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9957 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9958 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9960 case RID_HAS_TRIVIAL_COPY
:
9961 kind
= CPTK_HAS_TRIVIAL_COPY
;
9963 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9964 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9966 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9967 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9969 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9970 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9972 case RID_IS_ABSTRACT
:
9973 kind
= CPTK_IS_ABSTRACT
;
9975 case RID_IS_AGGREGATE
:
9976 kind
= CPTK_IS_AGGREGATE
;
9978 case RID_IS_BASE_OF
:
9979 kind
= CPTK_IS_BASE_OF
;
9983 kind
= CPTK_IS_CLASS
;
9986 kind
= CPTK_IS_EMPTY
;
9989 kind
= CPTK_IS_ENUM
;
9992 kind
= CPTK_IS_FINAL
;
9994 case RID_IS_LITERAL_TYPE
:
9995 kind
= CPTK_IS_LITERAL_TYPE
;
10000 case RID_IS_POLYMORPHIC
:
10001 kind
= CPTK_IS_POLYMORPHIC
;
10003 case RID_IS_SAME_AS
:
10004 kind
= CPTK_IS_SAME_AS
;
10007 case RID_IS_STD_LAYOUT
:
10008 kind
= CPTK_IS_STD_LAYOUT
;
10010 case RID_IS_TRIVIAL
:
10011 kind
= CPTK_IS_TRIVIAL
;
10013 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10014 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10017 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10018 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10021 case RID_IS_TRIVIALLY_COPYABLE
:
10022 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10025 kind
= CPTK_IS_UNION
;
10027 case RID_UNDERLYING_TYPE
:
10028 kind
= CPTK_UNDERLYING_TYPE
;
10033 case RID_DIRECT_BASES
:
10034 kind
= CPTK_DIRECT_BASES
;
10036 case RID_IS_ASSIGNABLE
:
10037 kind
= CPTK_IS_ASSIGNABLE
;
10040 case RID_IS_CONSTRUCTIBLE
:
10041 kind
= CPTK_IS_CONSTRUCTIBLE
;
10045 gcc_unreachable ();
10048 /* Get location of initial token. */
10049 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10051 /* Consume the token. */
10052 cp_lexer_consume_token (parser
->lexer
);
10054 matching_parens parens
;
10055 parens
.require_open (parser
);
10058 type_id_in_expr_sentinel
s (parser
);
10059 type1
= cp_parser_type_id (parser
);
10062 if (type1
== error_mark_node
)
10063 return error_mark_node
;
10067 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10070 type_id_in_expr_sentinel
s (parser
);
10071 type2
= cp_parser_type_id (parser
);
10074 if (type2
== error_mark_node
)
10075 return error_mark_node
;
10079 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10081 cp_lexer_consume_token (parser
->lexer
);
10082 tree elt
= cp_parser_type_id (parser
);
10083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10085 cp_lexer_consume_token (parser
->lexer
);
10086 elt
= make_pack_expansion (elt
);
10088 if (elt
== error_mark_node
)
10089 return error_mark_node
;
10090 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10094 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10095 parens
.require_close (parser
);
10097 /* Construct a location of the form:
10098 __is_trivially_copyable(_Tp)
10099 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10100 with start == caret, finishing at the close-paren. */
10101 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10103 /* Complete the trait expression, which may mean either processing
10104 the trait expr now or saving it for template instantiation. */
10107 case CPTK_UNDERLYING_TYPE
:
10108 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10110 return cp_expr (finish_bases (type1
, false), trait_loc
);
10111 case CPTK_DIRECT_BASES
:
10112 return cp_expr (finish_bases (type1
, true), trait_loc
);
10114 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10118 /* Parse a lambda expression.
10121 lambda-introducer lambda-declarator [opt] compound-statement
10123 Returns a representation of the expression. */
10126 cp_parser_lambda_expression (cp_parser
* parser
)
10128 tree lambda_expr
= build_lambda_expr ();
10131 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10132 cp_token_position start
= 0;
10134 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10136 if (cp_unevaluated_operand
)
10138 if (!token
->error_reported
)
10140 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10141 "lambda-expression in unevaluated context");
10142 token
->error_reported
= true;
10146 else if (parser
->in_template_argument_list_p
)
10148 if (!token
->error_reported
)
10150 error_at (token
->location
, "lambda-expression in template-argument");
10151 token
->error_reported
= true;
10156 /* We may be in the middle of deferred access check. Disable
10158 push_deferring_access_checks (dk_no_deferred
);
10160 cp_parser_lambda_introducer (parser
, lambda_expr
);
10162 type
= begin_lambda_type (lambda_expr
);
10163 if (type
== error_mark_node
)
10164 return error_mark_node
;
10166 record_lambda_scope (lambda_expr
);
10168 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10169 determine_visibility (TYPE_NAME (type
));
10171 /* Now that we've started the type, add the capture fields for any
10172 explicit captures. */
10173 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10176 /* Inside the class, surrounding template-parameter-lists do not apply. */
10177 unsigned int saved_num_template_parameter_lists
10178 = parser
->num_template_parameter_lists
;
10179 unsigned char in_statement
= parser
->in_statement
;
10180 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10181 bool fully_implicit_function_template_p
10182 = parser
->fully_implicit_function_template_p
;
10183 tree implicit_template_parms
= parser
->implicit_template_parms
;
10184 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10185 bool auto_is_implicit_function_template_parm_p
10186 = parser
->auto_is_implicit_function_template_parm_p
;
10188 parser
->num_template_parameter_lists
= 0;
10189 parser
->in_statement
= 0;
10190 parser
->in_switch_statement_p
= false;
10191 parser
->fully_implicit_function_template_p
= false;
10192 parser
->implicit_template_parms
= 0;
10193 parser
->implicit_template_scope
= 0;
10194 parser
->auto_is_implicit_function_template_parm_p
= false;
10196 /* By virtue of defining a local class, a lambda expression has access to
10197 the private variables of enclosing classes. */
10199 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10201 if (ok
&& cp_parser_error_occurred (parser
))
10206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10207 && cp_parser_start_tentative_firewall (parser
))
10209 cp_parser_lambda_body (parser
, lambda_expr
);
10211 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10213 if (cp_parser_skip_to_closing_brace (parser
))
10214 cp_lexer_consume_token (parser
->lexer
);
10217 /* The capture list was built up in reverse order; fix that now. */
10218 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10219 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10222 maybe_add_lambda_conv_op (type
);
10224 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10226 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10227 parser
->in_statement
= in_statement
;
10228 parser
->in_switch_statement_p
= in_switch_statement_p
;
10229 parser
->fully_implicit_function_template_p
10230 = fully_implicit_function_template_p
;
10231 parser
->implicit_template_parms
= implicit_template_parms
;
10232 parser
->implicit_template_scope
= implicit_template_scope
;
10233 parser
->auto_is_implicit_function_template_parm_p
10234 = auto_is_implicit_function_template_parm_p
;
10237 /* This field is only used during parsing of the lambda. */
10238 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10240 /* This lambda shouldn't have any proxies left at this point. */
10241 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10242 /* And now that we're done, push proxies for an enclosing lambda. */
10243 insert_pending_capture_proxies ();
10245 /* Update the lambda expression to a range. */
10246 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10247 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10249 end_tok
->location
);
10252 lambda_expr
= build_lambda_object (lambda_expr
);
10254 lambda_expr
= error_mark_node
;
10256 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10258 pop_deferring_access_checks ();
10260 return lambda_expr
;
10263 /* Parse the beginning of a lambda expression.
10266 [ lambda-capture [opt] ]
10268 LAMBDA_EXPR is the current representation of the lambda expression. */
10271 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10273 /* Need commas after the first capture. */
10276 /* Eat the leading `['. */
10277 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10279 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10281 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10282 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10283 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10284 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10286 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10288 cp_lexer_consume_token (parser
->lexer
);
10291 if (!(at_function_scope_p () || parsing_nsdmi ()))
10292 error ("non-local lambda expression cannot have a capture-default");
10295 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10297 cp_token
* capture_token
;
10299 tree capture_init_expr
;
10300 cp_id_kind idk
= CP_ID_KIND_NONE
;
10301 bool explicit_init_p
= false;
10303 enum capture_kind_type
10308 enum capture_kind_type capture_kind
= BY_COPY
;
10310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10312 error ("expected end of capture-list");
10319 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10321 /* Possibly capture `this'. */
10322 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10324 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10325 if (cxx_dialect
< cxx2a
10326 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10327 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10328 "with by-copy capture default");
10329 cp_lexer_consume_token (parser
->lexer
);
10330 add_capture (lambda_expr
,
10331 /*id=*/this_identifier
,
10332 /*initializer=*/finish_this_expr (),
10333 /*by_reference_p=*/true,
10338 /* Possibly capture `*this'. */
10339 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10340 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10343 if (cxx_dialect
< cxx17
)
10344 pedwarn (loc
, 0, "%<*this%> capture only available with "
10345 "-std=c++17 or -std=gnu++17");
10346 cp_lexer_consume_token (parser
->lexer
);
10347 cp_lexer_consume_token (parser
->lexer
);
10348 add_capture (lambda_expr
,
10349 /*id=*/this_identifier
,
10350 /*initializer=*/finish_this_expr (),
10351 /*by_reference_p=*/false,
10356 /* Remember whether we want to capture as a reference or not. */
10357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10359 capture_kind
= BY_REFERENCE
;
10360 cp_lexer_consume_token (parser
->lexer
);
10363 /* Get the identifier. */
10364 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10365 capture_id
= cp_parser_identifier (parser
);
10367 if (capture_id
== error_mark_node
)
10368 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10369 delimiters, but I modified this to stop on unnested ']' as well. It
10370 was already changed to stop on unnested '}', so the
10371 "closing_parenthesis" name is no more misleading with my change. */
10373 cp_parser_skip_to_closing_parenthesis (parser
,
10374 /*recovering=*/true,
10376 /*consume_paren=*/true);
10380 /* Find the initializer for this capture. */
10381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10382 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10383 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10385 bool direct
, non_constant
;
10386 /* An explicit initializer exists. */
10387 if (cxx_dialect
< cxx14
)
10388 pedwarn (input_location
, 0,
10389 "lambda capture initializers "
10390 "only available with -std=c++14 or -std=gnu++14");
10391 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10392 &non_constant
, true);
10393 explicit_init_p
= true;
10394 if (capture_init_expr
== NULL_TREE
)
10396 error ("empty initializer for lambda init-capture");
10397 capture_init_expr
= error_mark_node
;
10402 const char* error_msg
;
10404 /* Turn the identifier into an id-expression. */
10406 = cp_parser_lookup_name_simple (parser
, capture_id
,
10407 capture_token
->location
);
10409 if (capture_init_expr
== error_mark_node
)
10411 unqualified_name_lookup_error (capture_id
);
10414 else if (!VAR_P (capture_init_expr
)
10415 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10417 error_at (capture_token
->location
,
10418 "capture of non-variable %qE",
10419 capture_init_expr
);
10420 if (DECL_P (capture_init_expr
))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10422 "%q#D declared here", capture_init_expr
);
10425 if (VAR_P (capture_init_expr
)
10426 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10428 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10429 "%qD with non-automatic storage duration",
10430 capture_init_expr
))
10431 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10432 "%q#D declared here", capture_init_expr
);
10437 = finish_id_expression
10442 /*integral_constant_expression_p=*/false,
10443 /*allow_non_integral_constant_expression_p=*/false,
10444 /*non_integral_constant_expression_p=*/NULL
,
10445 /*template_p=*/false,
10447 /*address_p=*/false,
10448 /*template_arg_p=*/false,
10450 capture_token
->location
);
10452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10454 cp_lexer_consume_token (parser
->lexer
);
10455 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10460 && !explicit_init_p
)
10462 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10463 && capture_kind
== BY_COPY
)
10464 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10465 "of %qD redundant with by-copy capture default",
10467 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10468 && capture_kind
== BY_REFERENCE
)
10469 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10470 "capture of %qD redundant with by-reference capture "
10471 "default", capture_id
);
10474 add_capture (lambda_expr
,
10477 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10480 /* If there is any qualification still in effect, clear it
10481 now; we will be starting fresh with the next capture. */
10482 parser
->scope
= NULL_TREE
;
10483 parser
->qualifying_scope
= NULL_TREE
;
10484 parser
->object_scope
= NULL_TREE
;
10487 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10490 /* Parse the (optional) middle of a lambda expression.
10493 < template-parameter-list [opt] >
10494 ( parameter-declaration-clause [opt] )
10495 attribute-specifier [opt]
10496 decl-specifier-seq [opt]
10497 exception-specification [opt]
10498 lambda-return-type-clause [opt]
10500 LAMBDA_EXPR is the current representation of the lambda expression. */
10503 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10505 /* 5.1.1.4 of the standard says:
10506 If a lambda-expression does not include a lambda-declarator, it is as if
10507 the lambda-declarator were ().
10508 This means an empty parameter list, no attributes, and no exception
10510 tree param_list
= void_list_node
;
10511 tree attributes
= NULL_TREE
;
10512 tree exception_spec
= NULL_TREE
;
10513 tree template_param_list
= NULL_TREE
;
10514 tree tx_qual
= NULL_TREE
;
10515 tree return_type
= NULL_TREE
;
10516 cp_decl_specifier_seq lambda_specs
;
10517 clear_decl_specs (&lambda_specs
);
10519 /* The template-parameter-list is optional, but must begin with
10520 an opening angle if present. */
10521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10523 if (cxx_dialect
< cxx14
)
10524 pedwarn (parser
->lexer
->next_token
->location
, 0,
10525 "lambda templates are only available with "
10526 "-std=c++14 or -std=gnu++14");
10527 else if (cxx_dialect
< cxx2a
)
10528 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10529 "lambda templates are only available with "
10530 "-std=c++2a or -std=gnu++2a");
10532 cp_lexer_consume_token (parser
->lexer
);
10534 template_param_list
= cp_parser_template_parameter_list (parser
);
10536 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10538 /* We just processed one more parameter list. */
10539 ++parser
->num_template_parameter_lists
;
10542 /* The parameter-declaration-clause is optional (unless
10543 template-parameter-list was given), but must begin with an
10544 opening parenthesis if present. */
10545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10547 matching_parens parens
;
10548 parens
.consume_open (parser
);
10550 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10552 /* Parse parameters. */
10553 param_list
= cp_parser_parameter_declaration_clause (parser
);
10555 /* Default arguments shall not be specified in the
10556 parameter-declaration-clause of a lambda-declarator. */
10557 if (cxx_dialect
< cxx14
)
10558 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10559 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10560 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10561 "default argument specified for lambda parameter");
10563 parens
.require_close (parser
);
10565 attributes
= cp_parser_attributes_opt (parser
);
10567 /* In the decl-specifier-seq of the lambda-declarator, each
10568 decl-specifier shall either be mutable or constexpr. */
10569 int declares_class_or_enum
;
10570 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10571 cp_parser_decl_specifier_seq (parser
,
10572 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10573 &lambda_specs
, &declares_class_or_enum
);
10574 if (lambda_specs
.storage_class
== sc_mutable
)
10576 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10577 if (lambda_specs
.conflicting_specifiers_p
)
10578 error_at (lambda_specs
.locations
[ds_storage_class
],
10579 "duplicate %<mutable%>");
10582 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10584 /* Parse optional exception specification. */
10585 exception_spec
= cp_parser_exception_specification_opt (parser
);
10587 /* Parse optional trailing return type. */
10588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10590 cp_lexer_consume_token (parser
->lexer
);
10591 return_type
= cp_parser_trailing_type_id (parser
);
10594 /* The function parameters must be in scope all the way until after the
10595 trailing-return-type in case of decltype. */
10596 pop_bindings_and_leave_scope ();
10598 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10599 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10601 /* Create the function call operator.
10603 Messing with declarators like this is no uglier than building up the
10604 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10607 cp_decl_specifier_seq return_type_specs
;
10608 cp_declarator
* declarator
;
10613 clear_decl_specs (&return_type_specs
);
10614 return_type_specs
.type
= make_auto ();
10616 if (lambda_specs
.locations
[ds_constexpr
])
10618 if (cxx_dialect
>= cxx17
)
10619 return_type_specs
.locations
[ds_constexpr
]
10620 = lambda_specs
.locations
[ds_constexpr
];
10622 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10623 "lambda only available with -std=c++17 or -std=gnu++17");
10626 p
= obstack_alloc (&declarator_obstack
, 0);
10628 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10630 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10631 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10632 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10633 VIRT_SPEC_UNSPECIFIED
,
10637 /*late_return_type=*/NULL_TREE
,
10638 /*requires_clause*/NULL_TREE
);
10639 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10641 declarator
->u
.function
.late_return_type
= return_type
;
10643 fco
= grokmethod (&return_type_specs
,
10646 if (fco
!= error_mark_node
)
10648 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10649 DECL_ARTIFICIAL (fco
) = 1;
10650 /* Give the object parameter a different name. */
10651 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10652 DECL_LAMBDA_FUNCTION (fco
) = 1;
10654 if (template_param_list
)
10656 fco
= finish_member_template_decl (fco
);
10657 finish_template_decl (template_param_list
);
10658 --parser
->num_template_parameter_lists
;
10660 else if (parser
->fully_implicit_function_template_p
)
10661 fco
= finish_fully_implicit_template (parser
, fco
);
10663 finish_member_declaration (fco
);
10665 obstack_free (&declarator_obstack
, p
);
10667 return (fco
!= error_mark_node
);
10671 /* Parse the body of a lambda expression, which is simply
10675 but which requires special handling.
10676 LAMBDA_EXPR is the current representation of the lambda expression. */
10679 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10681 bool nested
= (current_function_decl
!= NULL_TREE
);
10682 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10683 bool in_function_body
= parser
->in_function_body
;
10686 push_function_context ();
10688 /* Still increment function_depth so that we don't GC in the
10689 middle of an expression. */
10692 vec
<tree
> omp_privatization_save
;
10693 save_omp_privatization_clauses (omp_privatization_save
);
10694 /* Clear this in case we're in the middle of a default argument. */
10695 parser
->local_variables_forbidden_p
= false;
10696 parser
->in_function_body
= true;
10699 local_specialization_stack
s (lss_copy
);
10700 tree fco
= lambda_function (lambda_expr
);
10701 tree body
= start_lambda_function (fco
, lambda_expr
);
10702 matching_braces braces
;
10704 if (braces
.require_open (parser
))
10706 tree compound_stmt
= begin_compound_stmt (0);
10708 /* Originally C++11 required us to peek for 'return expr'; and
10709 process it specially here to deduce the return type. N3638
10710 removed the need for that. */
10712 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10713 cp_parser_label_declaration (parser
);
10714 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10715 braces
.require_close (parser
);
10717 finish_compound_stmt (compound_stmt
);
10720 finish_lambda_function (body
);
10723 restore_omp_privatization_clauses (omp_privatization_save
);
10724 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10725 parser
->in_function_body
= in_function_body
;
10727 pop_function_context();
10732 /* Statements [gram.stmt.stmt] */
10734 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10737 add_debug_begin_stmt (location_t loc
)
10739 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10741 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10742 /* A concept is never expanded normally. */
10745 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10746 SET_EXPR_LOCATION (stmt
, loc
);
10750 /* Parse a statement.
10754 expression-statement
10756 selection-statement
10757 iteration-statement
10759 declaration-statement
10766 attribute-specifier-seq (opt) expression-statement
10767 attribute-specifier-seq (opt) compound-statement
10768 attribute-specifier-seq (opt) selection-statement
10769 attribute-specifier-seq (opt) iteration-statement
10770 attribute-specifier-seq (opt) jump-statement
10771 declaration-statement
10772 attribute-specifier-seq (opt) try-block
10775 expression-statement
10783 IN_COMPOUND is true when the statement is nested inside a
10784 cp_parser_compound_statement; this matters for certain pragmas.
10786 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10787 is a (possibly labeled) if statement which is not enclosed in braces
10788 and has an else clause. This is used to implement -Wparentheses.
10790 CHAIN is a vector of if-else-if conditions. */
10793 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10794 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10795 location_t
*loc_after_labels
)
10797 tree statement
, std_attrs
= NULL_TREE
;
10799 location_t statement_location
, attrs_location
;
10804 /* There is no statement yet. */
10805 statement
= NULL_TREE
;
10807 saved_token_sentinel
saved_tokens (parser
->lexer
);
10808 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10809 if (c_dialect_objc ())
10810 /* In obj-c++, seeing '[[' might be the either the beginning of
10811 c++11 attributes, or a nested objc-message-expression. So
10812 let's parse the c++11 attributes tentatively. */
10813 cp_parser_parse_tentatively (parser
);
10814 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10815 if (c_dialect_objc ())
10817 if (!cp_parser_parse_definitely (parser
))
10818 std_attrs
= NULL_TREE
;
10821 /* Peek at the next token. */
10822 token
= cp_lexer_peek_token (parser
->lexer
);
10823 /* Remember the location of the first token in the statement. */
10824 statement_location
= token
->location
;
10825 add_debug_begin_stmt (statement_location
);
10826 /* If this is a keyword, then that will often determine what kind of
10827 statement we have. */
10828 if (token
->type
== CPP_KEYWORD
)
10830 enum rid keyword
= token
->keyword
;
10836 /* Looks like a labeled-statement with a case label.
10837 Parse the label, and then use tail recursion to parse
10839 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10840 in_compound
= false;
10845 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10851 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10858 statement
= cp_parser_jump_statement (parser
);
10861 /* Objective-C++ exception-handling constructs. */
10864 case RID_AT_FINALLY
:
10865 case RID_AT_SYNCHRONIZED
:
10867 statement
= cp_parser_objc_statement (parser
);
10871 statement
= cp_parser_try_block (parser
);
10874 case RID_NAMESPACE
:
10875 /* This must be a namespace alias definition. */
10876 cp_parser_declaration_statement (parser
);
10879 case RID_TRANSACTION_ATOMIC
:
10880 case RID_TRANSACTION_RELAXED
:
10881 case RID_SYNCHRONIZED
:
10882 case RID_ATOMIC_NOEXCEPT
:
10883 case RID_ATOMIC_CANCEL
:
10884 statement
= cp_parser_transaction (parser
, token
);
10886 case RID_TRANSACTION_CANCEL
:
10887 statement
= cp_parser_transaction_cancel (parser
);
10891 /* It might be a keyword like `int' that can start a
10892 declaration-statement. */
10896 else if (token
->type
== CPP_NAME
)
10898 /* If the next token is a `:', then we are looking at a
10899 labeled-statement. */
10900 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10901 if (token
->type
== CPP_COLON
)
10903 /* Looks like a labeled-statement with an ordinary label.
10904 Parse the label, and then use tail recursion to parse
10907 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10908 in_compound
= false;
10912 /* Anything that starts with a `{' must be a compound-statement. */
10913 else if (token
->type
== CPP_OPEN_BRACE
)
10914 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10915 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10916 a statement all its own. */
10917 else if (token
->type
== CPP_PRAGMA
)
10919 /* Only certain OpenMP pragmas are attached to statements, and thus
10920 are considered statements themselves. All others are not. In
10921 the context of a compound, accept the pragma as a "statement" and
10922 return so that we can check for a close brace. Otherwise we
10923 require a real statement and must go back and read one. */
10925 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10926 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10930 else if (token
->type
== CPP_EOF
)
10932 cp_parser_error (parser
, "expected statement");
10936 /* Everything else must be a declaration-statement or an
10937 expression-statement. Try for the declaration-statement
10938 first, unless we are looking at a `;', in which case we know that
10939 we have an expression-statement. */
10942 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10944 if (std_attrs
!= NULL_TREE
)
10946 /* Attributes should be parsed as part of the the
10947 declaration, so let's un-parse them. */
10948 saved_tokens
.rollback();
10949 std_attrs
= NULL_TREE
;
10952 cp_parser_parse_tentatively (parser
);
10953 /* Try to parse the declaration-statement. */
10954 cp_parser_declaration_statement (parser
);
10955 /* If that worked, we're done. */
10956 if (cp_parser_parse_definitely (parser
))
10959 /* All preceding labels have been parsed at this point. */
10960 if (loc_after_labels
!= NULL
)
10961 *loc_after_labels
= statement_location
;
10963 /* Look for an expression-statement instead. */
10964 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10966 /* Handle [[fallthrough]];. */
10967 if (attribute_fallthrough_p (std_attrs
))
10969 /* The next token after the fallthrough attribute is ';'. */
10970 if (statement
== NULL_TREE
)
10972 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10973 statement
= build_call_expr_internal_loc (statement_location
,
10975 void_type_node
, 0);
10976 finish_expr_stmt (statement
);
10979 warning_at (statement_location
, OPT_Wattributes
,
10980 "%<fallthrough%> attribute not followed by %<;%>");
10981 std_attrs
= NULL_TREE
;
10985 /* Set the line number for the statement. */
10986 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10987 SET_EXPR_LOCATION (statement
, statement_location
);
10989 /* Allow "[[fallthrough]];", but warn otherwise. */
10990 if (std_attrs
!= NULL_TREE
)
10991 warning_at (attrs_location
,
10993 "attributes at the beginning of statement are ignored");
10996 /* Append ATTR to attribute list ATTRS. */
10999 attr_chainon (tree attrs
, tree attr
)
11001 if (attrs
== error_mark_node
)
11002 return error_mark_node
;
11003 if (attr
== error_mark_node
)
11004 return error_mark_node
;
11005 return chainon (attrs
, attr
);
11008 /* Parse the label for a labeled-statement, i.e.
11011 case constant-expression :
11015 case constant-expression ... constant-expression : statement
11017 When a label is parsed without errors, the label is added to the
11018 parse tree by the finish_* functions, so this function doesn't
11019 have to return the label. */
11022 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11025 tree label
= NULL_TREE
;
11026 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11028 /* The next token should be an identifier. */
11029 token
= cp_lexer_peek_token (parser
->lexer
);
11030 if (token
->type
!= CPP_NAME
11031 && token
->type
!= CPP_KEYWORD
)
11033 cp_parser_error (parser
, "expected labeled-statement");
11037 /* Remember whether this case or a user-defined label is allowed to fall
11039 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11041 parser
->colon_corrects_to_scope_p
= false;
11042 switch (token
->keyword
)
11046 tree expr
, expr_hi
;
11047 cp_token
*ellipsis
;
11049 /* Consume the `case' token. */
11050 cp_lexer_consume_token (parser
->lexer
);
11051 /* Parse the constant-expression. */
11052 expr
= cp_parser_constant_expression (parser
);
11053 if (check_for_bare_parameter_packs (expr
))
11054 expr
= error_mark_node
;
11056 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11057 if (ellipsis
->type
== CPP_ELLIPSIS
)
11059 /* Consume the `...' token. */
11060 cp_lexer_consume_token (parser
->lexer
);
11061 expr_hi
= cp_parser_constant_expression (parser
);
11062 if (check_for_bare_parameter_packs (expr_hi
))
11063 expr_hi
= error_mark_node
;
11065 /* We don't need to emit warnings here, as the common code
11066 will do this for us. */
11069 expr_hi
= NULL_TREE
;
11071 if (parser
->in_switch_statement_p
)
11073 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11074 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11075 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11078 error_at (token
->location
,
11079 "case label %qE not within a switch statement",
11085 /* Consume the `default' token. */
11086 cp_lexer_consume_token (parser
->lexer
);
11088 if (parser
->in_switch_statement_p
)
11090 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11091 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11092 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11095 error_at (token
->location
, "case label not within a switch statement");
11099 /* Anything else must be an ordinary label. */
11100 label
= finish_label_stmt (cp_parser_identifier (parser
));
11101 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11102 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11106 /* Require the `:' token. */
11107 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11109 /* An ordinary label may optionally be followed by attributes.
11110 However, this is only permitted if the attributes are then
11111 followed by a semicolon. This is because, for backward
11112 compatibility, when parsing
11113 lab: __attribute__ ((unused)) int i;
11114 we want the attribute to attach to "i", not "lab". */
11115 if (label
!= NULL_TREE
11116 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11119 cp_parser_parse_tentatively (parser
);
11120 attrs
= cp_parser_gnu_attributes_opt (parser
);
11121 if (attrs
== NULL_TREE
11122 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11123 cp_parser_abort_tentative_parse (parser
);
11124 else if (!cp_parser_parse_definitely (parser
))
11127 attributes
= attr_chainon (attributes
, attrs
);
11130 if (attributes
!= NULL_TREE
)
11131 cplus_decl_attributes (&label
, attributes
, 0);
11133 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11136 /* Parse an expression-statement.
11138 expression-statement:
11141 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11142 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11143 indicates whether this expression-statement is part of an
11144 expression statement. */
11147 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11149 tree statement
= NULL_TREE
;
11150 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11151 location_t loc
= token
->location
;
11153 /* There might be attribute fallthrough. */
11154 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11156 /* If the next token is a ';', then there is no expression
11158 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11160 statement
= cp_parser_expression (parser
);
11161 if (statement
== error_mark_node
11162 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11164 cp_parser_skip_to_end_of_block_or_statement (parser
);
11165 return error_mark_node
;
11169 /* Handle [[fallthrough]];. */
11170 if (attribute_fallthrough_p (attr
))
11172 /* The next token after the fallthrough attribute is ';'. */
11173 if (statement
== NULL_TREE
)
11174 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11175 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11176 void_type_node
, 0);
11178 warning_at (loc
, OPT_Wattributes
,
11179 "%<fallthrough%> attribute not followed by %<;%>");
11183 /* Allow "[[fallthrough]];", but warn otherwise. */
11184 if (attr
!= NULL_TREE
)
11185 warning_at (loc
, OPT_Wattributes
,
11186 "attributes at the beginning of statement are ignored");
11188 /* Give a helpful message for "A<T>::type t;" and the like. */
11189 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11190 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11192 if (TREE_CODE (statement
) == SCOPE_REF
)
11193 error_at (token
->location
, "need %<typename%> before %qE because "
11194 "%qT is a dependent scope",
11195 statement
, TREE_OPERAND (statement
, 0));
11196 else if (is_overloaded_fn (statement
)
11197 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11200 tree fn
= get_first_fn (statement
);
11201 error_at (token
->location
,
11202 "%<%T::%D%> names the constructor, not the type",
11203 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11207 /* Consume the final `;'. */
11208 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11210 if (in_statement_expr
11211 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11212 /* This is the final expression statement of a statement
11214 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11215 else if (statement
)
11216 statement
= finish_expr_stmt (statement
);
11221 /* Parse a compound-statement.
11223 compound-statement:
11224 { statement-seq [opt] }
11228 compound-statement:
11229 { label-declaration-seq [opt] statement-seq [opt] }
11231 label-declaration-seq:
11233 label-declaration-seq label-declaration
11235 Returns a tree representing the statement. */
11238 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11239 int bcs_flags
, bool function_body
)
11241 tree compound_stmt
;
11242 matching_braces braces
;
11244 /* Consume the `{'. */
11245 if (!braces
.require_open (parser
))
11246 return error_mark_node
;
11247 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11248 && !function_body
&& cxx_dialect
< cxx14
)
11249 pedwarn (input_location
, OPT_Wpedantic
,
11250 "compound-statement in %<constexpr%> function");
11251 /* Begin the compound-statement. */
11252 compound_stmt
= begin_compound_stmt (bcs_flags
);
11253 /* If the next keyword is `__label__' we have a label declaration. */
11254 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11255 cp_parser_label_declaration (parser
);
11256 /* Parse an (optional) statement-seq. */
11257 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11258 /* Finish the compound-statement. */
11259 finish_compound_stmt (compound_stmt
);
11260 /* Consume the `}'. */
11261 braces
.require_close (parser
);
11263 return compound_stmt
;
11266 /* Parse an (optional) statement-seq.
11270 statement-seq [opt] statement */
11273 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11275 /* Scan statements until there aren't any more. */
11278 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11280 /* If we are looking at a `}', then we have run out of
11281 statements; the same is true if we have reached the end
11282 of file, or have stumbled upon a stray '@end'. */
11283 if (token
->type
== CPP_CLOSE_BRACE
11284 || token
->type
== CPP_EOF
11285 || token
->type
== CPP_PRAGMA_EOL
11286 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11289 /* If we are in a compound statement and find 'else' then
11290 something went wrong. */
11291 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11293 if (parser
->in_statement
& IN_IF_STMT
)
11297 token
= cp_lexer_consume_token (parser
->lexer
);
11298 error_at (token
->location
, "%<else%> without a previous %<if%>");
11302 /* Parse the statement. */
11303 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11307 /* Return true if this is the C++20 version of range-based-for with
11311 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11315 /* Save tokens so that we can put them back. */
11316 cp_lexer_save_tokens (parser
->lexer
);
11318 /* There has to be an unnested ; followed by an unnested :. */
11319 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11320 /*recovering=*/false,
11322 /*consume_paren=*/false) != -1)
11325 /* We found the semicolon, eat it now. */
11326 cp_lexer_consume_token (parser
->lexer
);
11328 /* Now look for ':' that is not nested in () or {}. */
11329 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11330 /*recovering=*/false,
11332 /*consume_paren=*/false) == -1);
11335 /* Roll back the tokens we skipped. */
11336 cp_lexer_rollback_tokens (parser
->lexer
);
11341 /* Return true if we're looking at (init; cond), false otherwise. */
11344 cp_parser_init_statement_p (cp_parser
*parser
)
11346 /* Save tokens so that we can put them back. */
11347 cp_lexer_save_tokens (parser
->lexer
);
11349 /* Look for ';' that is not nested in () or {}. */
11350 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11351 /*recovering=*/false,
11353 /*consume_paren=*/false);
11355 /* Roll back the tokens we skipped. */
11356 cp_lexer_rollback_tokens (parser
->lexer
);
11361 /* Parse a selection-statement.
11363 selection-statement:
11364 if ( init-statement [opt] condition ) statement
11365 if ( init-statement [opt] condition ) statement else statement
11366 switch ( init-statement [opt] condition ) statement
11368 Returns the new IF_STMT or SWITCH_STMT.
11370 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11371 is a (possibly labeled) if statement which is not enclosed in
11372 braces and has an else clause. This is used to implement
11375 CHAIN is a vector of if-else-if conditions. This is used to implement
11376 -Wduplicated-cond. */
11379 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11384 token_indent_info guard_tinfo
;
11389 /* Peek at the next token. */
11390 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11391 guard_tinfo
= get_token_indent_info (token
);
11393 /* See what kind of keyword it is. */
11394 keyword
= token
->keyword
;
11404 if (keyword
== RID_IF
11405 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11409 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11410 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11411 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11412 "with -std=c++17 or -std=gnu++17");
11415 /* Look for the `('. */
11416 matching_parens parens
;
11417 if (!parens
.require_open (parser
))
11419 cp_parser_skip_to_end_of_statement (parser
);
11420 return error_mark_node
;
11423 /* Begin the selection-statement. */
11424 if (keyword
== RID_IF
)
11426 statement
= begin_if_stmt ();
11427 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11430 statement
= begin_switch_stmt ();
11432 /* Parse the optional init-statement. */
11433 if (cp_parser_init_statement_p (parser
))
11436 if (cxx_dialect
< cxx17
)
11437 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11438 "init-statement in selection statements only available "
11439 "with -std=c++17 or -std=gnu++17");
11440 cp_parser_init_statement (parser
, &decl
);
11443 /* Parse the condition. */
11444 condition
= cp_parser_condition (parser
);
11445 /* Look for the `)'. */
11446 if (!parens
.require_close (parser
))
11447 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11448 /*consume_paren=*/true);
11450 if (keyword
== RID_IF
)
11453 unsigned char in_statement
;
11455 /* Add the condition. */
11456 condition
= finish_if_stmt_cond (condition
, statement
);
11458 if (warn_duplicated_cond
)
11459 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11462 /* Parse the then-clause. */
11463 in_statement
= parser
->in_statement
;
11464 parser
->in_statement
|= IN_IF_STMT
;
11466 /* Outside a template, the non-selected branch of a constexpr
11467 if is a 'discarded statement', i.e. unevaluated. */
11468 bool was_discarded
= in_discarded_stmt
;
11469 bool discard_then
= (cx
&& !processing_template_decl
11470 && integer_zerop (condition
));
11473 in_discarded_stmt
= true;
11474 ++c_inhibit_evaluation_warnings
;
11477 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11480 parser
->in_statement
= in_statement
;
11482 finish_then_clause (statement
);
11486 THEN_CLAUSE (statement
) = NULL_TREE
;
11487 in_discarded_stmt
= was_discarded
;
11488 --c_inhibit_evaluation_warnings
;
11491 /* If the next token is `else', parse the else-clause. */
11492 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11495 bool discard_else
= (cx
&& !processing_template_decl
11496 && integer_nonzerop (condition
));
11499 in_discarded_stmt
= true;
11500 ++c_inhibit_evaluation_warnings
;
11504 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11505 /* Consume the `else' keyword. */
11506 cp_lexer_consume_token (parser
->lexer
);
11507 if (warn_duplicated_cond
)
11509 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11513 /* We've got "if (COND) else if (COND2)". Start
11514 the condition chain and add COND as the first
11516 chain
= new vec
<tree
> ();
11517 if (!CONSTANT_CLASS_P (condition
)
11518 && !TREE_SIDE_EFFECTS (condition
))
11520 /* Wrap it in a NOP_EXPR so that we can set the
11521 location of the condition. */
11522 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11524 SET_EXPR_LOCATION (e
, token
->location
);
11525 chain
->safe_push (e
);
11528 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11531 /* This is if-else without subsequent if. Zap the
11532 condition chain; we would have already warned at
11538 begin_else_clause (statement
);
11539 /* Parse the else-clause. */
11540 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11541 guard_tinfo
, chain
);
11543 finish_else_clause (statement
);
11545 /* If we are currently parsing a then-clause, then
11546 IF_P will not be NULL. We set it to true to
11547 indicate that this if statement has an else clause.
11548 This may trigger the Wparentheses warning below
11549 when we get back up to the parent if statement. */
11555 ELSE_CLAUSE (statement
) = NULL_TREE
;
11556 in_discarded_stmt
= was_discarded
;
11557 --c_inhibit_evaluation_warnings
;
11562 /* This if statement does not have an else clause. If
11563 NESTED_IF is true, then the then-clause has an if
11564 statement which does have an else clause. We warn
11565 about the potential ambiguity. */
11567 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11568 "suggest explicit braces to avoid ambiguous"
11570 if (warn_duplicated_cond
)
11572 /* We don't need the condition chain anymore. */
11578 /* Now we're all done with the if-statement. */
11579 finish_if_stmt (statement
);
11583 bool in_switch_statement_p
;
11584 unsigned char in_statement
;
11586 /* Add the condition. */
11587 finish_switch_cond (condition
, statement
);
11589 /* Parse the body of the switch-statement. */
11590 in_switch_statement_p
= parser
->in_switch_statement_p
;
11591 in_statement
= parser
->in_statement
;
11592 parser
->in_switch_statement_p
= true;
11593 parser
->in_statement
|= IN_SWITCH_STMT
;
11594 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11596 parser
->in_switch_statement_p
= in_switch_statement_p
;
11597 parser
->in_statement
= in_statement
;
11599 /* Now we're all done with the switch-statement. */
11600 finish_switch_stmt (statement
);
11608 cp_parser_error (parser
, "expected selection-statement");
11609 return error_mark_node
;
11613 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11614 If we have seen at least one decl-specifier, and the next token
11615 is not a parenthesis, then we must be looking at a declaration.
11616 (After "int (" we might be looking at a functional cast.) */
11619 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11620 bool any_specifiers_p
)
11622 if (any_specifiers_p
11623 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11624 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11625 && !cp_parser_error_occurred (parser
))
11626 cp_parser_commit_to_tentative_parse (parser
);
11629 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11630 The declarator shall not specify a function or an array. Returns
11631 TRUE if the declarator is valid, FALSE otherwise. */
11634 cp_parser_check_condition_declarator (cp_parser
* parser
,
11635 cp_declarator
*declarator
,
11638 if (declarator
== cp_error_declarator
11639 || function_declarator_p (declarator
)
11640 || declarator
->kind
== cdk_array
)
11642 if (declarator
== cp_error_declarator
)
11643 /* Already complained. */;
11644 else if (declarator
->kind
== cdk_array
)
11645 error_at (loc
, "condition declares an array");
11647 error_at (loc
, "condition declares a function");
11648 if (parser
->fully_implicit_function_template_p
)
11649 abort_fully_implicit_template (parser
);
11650 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11651 /*or_comma=*/false,
11652 /*consume_paren=*/false);
11659 /* Parse a condition.
11663 type-specifier-seq declarator = initializer-clause
11664 type-specifier-seq declarator braced-init-list
11669 type-specifier-seq declarator asm-specification [opt]
11670 attributes [opt] = assignment-expression
11672 Returns the expression that should be tested. */
11675 cp_parser_condition (cp_parser
* parser
)
11677 cp_decl_specifier_seq type_specifiers
;
11678 const char *saved_message
;
11679 int declares_class_or_enum
;
11681 /* Try the declaration first. */
11682 cp_parser_parse_tentatively (parser
);
11683 /* New types are not allowed in the type-specifier-seq for a
11685 saved_message
= parser
->type_definition_forbidden_message
;
11686 parser
->type_definition_forbidden_message
11687 = G_("types may not be defined in conditions");
11688 /* Parse the type-specifier-seq. */
11689 cp_parser_decl_specifier_seq (parser
,
11690 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11692 &declares_class_or_enum
);
11693 /* Restore the saved message. */
11694 parser
->type_definition_forbidden_message
= saved_message
;
11696 cp_parser_maybe_commit_to_declaration (parser
,
11697 type_specifiers
.any_specifiers_p
);
11699 /* If all is well, we might be looking at a declaration. */
11700 if (!cp_parser_error_occurred (parser
))
11703 tree asm_specification
;
11705 cp_declarator
*declarator
;
11706 tree initializer
= NULL_TREE
;
11707 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11709 /* Parse the declarator. */
11710 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11711 /*ctor_dtor_or_conv_p=*/NULL
,
11712 /*parenthesized_p=*/NULL
,
11713 /*member_p=*/false,
11714 /*friend_p=*/false);
11715 /* Parse the attributes. */
11716 attributes
= cp_parser_attributes_opt (parser
);
11717 /* Parse the asm-specification. */
11718 asm_specification
= cp_parser_asm_specification_opt (parser
);
11719 /* If the next token is not an `=' or '{', then we might still be
11720 looking at an expression. For example:
11724 looks like a decl-specifier-seq and a declarator -- but then
11725 there is no `=', so this is an expression. */
11726 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11727 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11728 cp_parser_simulate_error (parser
);
11730 /* If we did see an `=' or '{', then we are looking at a declaration
11732 if (cp_parser_parse_definitely (parser
))
11735 bool non_constant_p
= false;
11736 int flags
= LOOKUP_ONLYCONVERTING
;
11738 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11739 return error_mark_node
;
11741 /* Create the declaration. */
11742 decl
= start_decl (declarator
, &type_specifiers
,
11743 /*initialized_p=*/true,
11744 attributes
, /*prefix_attributes=*/NULL_TREE
,
11747 /* Parse the initializer. */
11748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11750 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11751 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11754 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11756 /* Consume the `='. */
11757 cp_lexer_consume_token (parser
->lexer
);
11758 initializer
= cp_parser_initializer_clause (parser
,
11763 cp_parser_error (parser
, "expected initializer");
11764 initializer
= error_mark_node
;
11766 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11769 /* Process the initializer. */
11770 cp_finish_decl (decl
,
11771 initializer
, !non_constant_p
,
11776 pop_scope (pushed_scope
);
11778 return convert_from_reference (decl
);
11781 /* If we didn't even get past the declarator successfully, we are
11782 definitely not looking at a declaration. */
11784 cp_parser_abort_tentative_parse (parser
);
11786 /* Otherwise, we are looking at an expression. */
11787 return cp_parser_expression (parser
);
11790 /* Parses a for-statement or range-for-statement until the closing ')',
11794 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11796 tree init
, scope
, decl
;
11799 /* Begin the for-statement. */
11800 scope
= begin_for_scope (&init
);
11802 /* Parse the initialization. */
11803 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11806 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11808 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11812 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11813 unsigned short unroll
)
11815 /* Normal for loop */
11816 tree condition
= NULL_TREE
;
11817 tree expression
= NULL_TREE
;
11820 stmt
= begin_for_stmt (scope
, init
);
11821 /* The init-statement has already been parsed in
11822 cp_parser_init_statement, so no work is needed here. */
11823 finish_init_stmt (stmt
);
11825 /* If there's a condition, process it. */
11826 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11827 condition
= cp_parser_condition (parser
);
11830 cp_parser_error (parser
, "missing loop condition in loop with "
11831 "%<GCC ivdep%> pragma");
11832 condition
= error_mark_node
;
11836 cp_parser_error (parser
, "missing loop condition in loop with "
11837 "%<GCC unroll%> pragma");
11838 condition
= error_mark_node
;
11840 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11841 /* Look for the `;'. */
11842 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11844 /* If there's an expression, process it. */
11845 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11846 expression
= cp_parser_expression (parser
);
11847 finish_for_expr (expression
, stmt
);
11852 /* Tries to parse a range-based for-statement:
11855 decl-specifier-seq declarator : expression
11857 The decl-specifier-seq declarator and the `:' are already parsed by
11858 cp_parser_init_statement. If processing_template_decl it returns a
11859 newly created RANGE_FOR_STMT; if not, it is converted to a
11860 regular FOR_STMT. */
11863 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11864 bool ivdep
, unsigned short unroll
)
11866 tree stmt
, range_expr
;
11867 auto_vec
<cxx_binding
*, 16> bindings
;
11868 auto_vec
<tree
, 16> names
;
11869 tree decomp_first_name
= NULL_TREE
;
11870 unsigned int decomp_cnt
= 0;
11872 /* Get the range declaration momentarily out of the way so that
11873 the range expression doesn't clash with it. */
11874 if (range_decl
!= error_mark_node
)
11876 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11878 tree v
= DECL_VALUE_EXPR (range_decl
);
11879 /* For decomposition declaration get all of the corresponding
11880 declarations out of the way. */
11881 if (TREE_CODE (v
) == ARRAY_REF
11882 && VAR_P (TREE_OPERAND (v
, 0))
11883 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11885 tree d
= range_decl
;
11886 range_decl
= TREE_OPERAND (v
, 0);
11887 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11888 decomp_first_name
= d
;
11889 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11891 tree name
= DECL_NAME (d
);
11892 names
.safe_push (name
);
11893 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11894 IDENTIFIER_BINDING (name
)
11895 = IDENTIFIER_BINDING (name
)->previous
;
11899 if (names
.is_empty ())
11901 tree name
= DECL_NAME (range_decl
);
11902 names
.safe_push (name
);
11903 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11904 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11910 bool expr_non_constant_p
;
11911 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11914 range_expr
= cp_parser_expression (parser
);
11916 /* Put the range declaration(s) back into scope. */
11917 for (unsigned int i
= 0; i
< names
.length (); i
++)
11919 cxx_binding
*binding
= bindings
[i
];
11920 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11921 IDENTIFIER_BINDING (names
[i
]) = binding
;
11924 /* If in template, STMT is converted to a normal for-statement
11925 at instantiation. If not, it is done just ahead. */
11926 if (processing_template_decl
)
11928 if (check_for_bare_parameter_packs (range_expr
))
11929 range_expr
= error_mark_node
;
11930 stmt
= begin_range_for_stmt (scope
, init
);
11932 RANGE_FOR_IVDEP (stmt
) = 1;
11934 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11935 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11936 if (!type_dependent_expression_p (range_expr
)
11937 /* do_auto_deduction doesn't mess with template init-lists. */
11938 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11939 do_range_for_auto_deduction (range_decl
, range_expr
);
11943 stmt
= begin_for_stmt (scope
, init
);
11944 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11945 decomp_first_name
, decomp_cnt
, ivdep
,
11951 /* Subroutine of cp_convert_range_for: given the initializer expression,
11952 builds up the range temporary. */
11955 build_range_temp (tree range_expr
)
11957 tree range_type
, range_temp
;
11959 /* Find out the type deduced by the declaration
11960 `auto &&__range = range_expr'. */
11961 range_type
= cp_build_reference_type (make_auto (), true);
11962 range_type
= do_auto_deduction (range_type
, range_expr
,
11963 type_uses_auto (range_type
));
11965 /* Create the __range variable. */
11966 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
11968 TREE_USED (range_temp
) = 1;
11969 DECL_ARTIFICIAL (range_temp
) = 1;
11974 /* Used by cp_parser_range_for in template context: we aren't going to
11975 do a full conversion yet, but we still need to resolve auto in the
11976 type of the for-range-declaration if present. This is basically
11977 a shortcut version of cp_convert_range_for. */
11980 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11982 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11985 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11986 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11987 iter_type
= (cp_parser_perform_range_for_lookup
11988 (range_temp
, &begin_dummy
, &end_dummy
));
11991 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11993 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11995 tf_warning_or_error
);
11996 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11997 iter_decl
, auto_node
);
12002 /* Converts a range-based for-statement into a normal
12003 for-statement, as per the definition.
12005 for (RANGE_DECL : RANGE_EXPR)
12008 should be equivalent to:
12011 auto &&__range = RANGE_EXPR;
12012 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12016 RANGE_DECL = *__begin;
12021 If RANGE_EXPR is an array:
12022 BEGIN_EXPR = __range
12023 END_EXPR = __range + ARRAY_SIZE(__range)
12024 Else if RANGE_EXPR has a member 'begin' or 'end':
12025 BEGIN_EXPR = __range.begin()
12026 END_EXPR = __range.end()
12028 BEGIN_EXPR = begin(__range)
12029 END_EXPR = end(__range);
12031 If __range has a member 'begin' but not 'end', or vice versa, we must
12032 still use the second alternative (it will surely fail, however).
12033 When calling begin()/end() in the third alternative we must use
12034 argument dependent lookup, but always considering 'std' as an associated
12038 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12039 tree decomp_first_name
, unsigned int decomp_cnt
,
12040 bool ivdep
, unsigned short unroll
)
12043 tree iter_type
, begin_expr
, end_expr
;
12044 tree condition
, expression
;
12046 range_expr
= mark_lvalue_use (range_expr
);
12048 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12049 /* If an error happened previously do nothing or else a lot of
12050 unhelpful errors would be issued. */
12051 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12056 if (VAR_P (range_expr
)
12057 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12058 /* Can't bind a reference to an array of runtime bound. */
12059 range_temp
= range_expr
;
12062 range_temp
= build_range_temp (range_expr
);
12063 pushdecl (range_temp
);
12064 cp_finish_decl (range_temp
, range_expr
,
12065 /*is_constant_init*/false, NULL_TREE
,
12066 LOOKUP_ONLYCONVERTING
);
12067 range_temp
= convert_from_reference (range_temp
);
12069 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12070 &begin_expr
, &end_expr
);
12073 /* The new for initialization statement. */
12074 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12076 TREE_USED (begin
) = 1;
12077 DECL_ARTIFICIAL (begin
) = 1;
12079 cp_finish_decl (begin
, begin_expr
,
12080 /*is_constant_init*/false, NULL_TREE
,
12081 LOOKUP_ONLYCONVERTING
);
12083 if (cxx_dialect
>= cxx17
)
12084 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12085 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12086 TREE_USED (end
) = 1;
12087 DECL_ARTIFICIAL (end
) = 1;
12089 cp_finish_decl (end
, end_expr
,
12090 /*is_constant_init*/false, NULL_TREE
,
12091 LOOKUP_ONLYCONVERTING
);
12093 finish_init_stmt (statement
);
12095 /* The new for condition. */
12096 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12099 NULL
, tf_warning_or_error
);
12100 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12102 /* The new increment expression. */
12103 expression
= finish_unary_op_expr (input_location
,
12104 PREINCREMENT_EXPR
, begin
,
12105 tf_warning_or_error
);
12106 finish_for_expr (expression
, statement
);
12108 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12109 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12111 /* The declaration is initialized with *__begin inside the loop body. */
12112 cp_finish_decl (range_decl
,
12113 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12114 tf_warning_or_error
),
12115 /*is_constant_init*/false, NULL_TREE
,
12116 LOOKUP_ONLYCONVERTING
);
12117 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12118 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12123 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12124 We need to solve both at the same time because the method used
12125 depends on the existence of members begin or end.
12126 Returns the type deduced for the iterator expression. */
12129 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12131 if (error_operand_p (range
))
12133 *begin
= *end
= error_mark_node
;
12134 return error_mark_node
;
12137 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12139 error ("range-based %<for%> expression of type %qT "
12140 "has incomplete type", TREE_TYPE (range
));
12141 *begin
= *end
= error_mark_node
;
12142 return error_mark_node
;
12144 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12146 /* If RANGE is an array, we will use pointer arithmetic. */
12147 *begin
= decay_conversion (range
, tf_warning_or_error
);
12148 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12150 array_type_nelts_top (TREE_TYPE (range
)),
12152 return TREE_TYPE (*begin
);
12156 /* If it is not an array, we must do a bit of magic. */
12157 tree id_begin
, id_end
;
12158 tree member_begin
, member_end
;
12160 *begin
= *end
= error_mark_node
;
12162 id_begin
= get_identifier ("begin");
12163 id_end
= get_identifier ("end");
12164 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12165 /*protect=*/2, /*want_type=*/false,
12166 tf_warning_or_error
);
12167 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12168 /*protect=*/2, /*want_type=*/false,
12169 tf_warning_or_error
);
12171 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12173 /* Use the member functions. */
12174 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12175 *end
= cp_parser_range_for_member_function (range
, id_end
);
12179 /* Use global functions with ADL. */
12180 vec
<tree
, va_gc
> *vec
;
12181 vec
= make_tree_vector ();
12183 vec_safe_push (vec
, range
);
12185 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12186 tf_warning_or_error
);
12187 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12188 tf_warning_or_error
);
12189 member_end
= perform_koenig_lookup (id_end
, vec
,
12190 tf_warning_or_error
);
12191 *end
= finish_call_expr (member_end
, &vec
, false, true,
12192 tf_warning_or_error
);
12194 release_tree_vector (vec
);
12197 /* Last common checks. */
12198 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12200 /* If one of the expressions is an error do no more checks. */
12201 *begin
= *end
= error_mark_node
;
12202 return error_mark_node
;
12204 else if (type_dependent_expression_p (*begin
)
12205 || type_dependent_expression_p (*end
))
12206 /* Can happen, when, eg, in a template context, Koenig lookup
12207 can't resolve begin/end (c++/58503). */
12211 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12212 /* The unqualified type of the __begin and __end temporaries should
12213 be the same, as required by the multiple auto declaration. */
12214 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12216 if (cxx_dialect
>= cxx17
12217 && (build_x_binary_op (input_location
, NE_EXPR
,
12218 *begin
, ERROR_MARK
,
12221 != error_mark_node
))
12222 /* P0184R0 allows __begin and __end to have different types,
12223 but make sure they are comparable so we can give a better
12226 error ("inconsistent begin/end types in range-based %<for%> "
12227 "statement: %qT and %qT",
12228 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12235 /* Helper function for cp_parser_perform_range_for_lookup.
12236 Builds a tree for RANGE.IDENTIFIER(). */
12239 cp_parser_range_for_member_function (tree range
, tree identifier
)
12242 vec
<tree
, va_gc
> *vec
;
12244 member
= finish_class_member_access_expr (range
, identifier
,
12245 false, tf_warning_or_error
);
12246 if (member
== error_mark_node
)
12247 return error_mark_node
;
12249 vec
= make_tree_vector ();
12250 res
= finish_call_expr (member
, &vec
,
12251 /*disallow_virtual=*/false,
12252 /*koenig_p=*/false,
12253 tf_warning_or_error
);
12254 release_tree_vector (vec
);
12258 /* Parse an iteration-statement.
12260 iteration-statement:
12261 while ( condition ) statement
12262 do statement while ( expression ) ;
12263 for ( init-statement condition [opt] ; expression [opt] )
12266 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12269 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12270 unsigned short unroll
)
12275 unsigned char in_statement
;
12276 token_indent_info guard_tinfo
;
12278 /* Peek at the next token. */
12279 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12281 return error_mark_node
;
12283 guard_tinfo
= get_token_indent_info (token
);
12285 /* Remember whether or not we are already within an iteration
12287 in_statement
= parser
->in_statement
;
12289 /* See what kind of keyword it is. */
12290 keyword
= token
->keyword
;
12297 /* Begin the while-statement. */
12298 statement
= begin_while_stmt ();
12299 /* Look for the `('. */
12300 matching_parens parens
;
12301 parens
.require_open (parser
);
12302 /* Parse the condition. */
12303 condition
= cp_parser_condition (parser
);
12304 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12305 /* Look for the `)'. */
12306 parens
.require_close (parser
);
12307 /* Parse the dependent statement. */
12308 parser
->in_statement
= IN_ITERATION_STMT
;
12309 bool prev
= note_iteration_stmt_body_start ();
12310 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12311 note_iteration_stmt_body_end (prev
);
12312 parser
->in_statement
= in_statement
;
12313 /* We're done with the while-statement. */
12314 finish_while_stmt (statement
);
12322 /* Begin the do-statement. */
12323 statement
= begin_do_stmt ();
12324 /* Parse the body of the do-statement. */
12325 parser
->in_statement
= IN_ITERATION_STMT
;
12326 bool prev
= note_iteration_stmt_body_start ();
12327 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12328 note_iteration_stmt_body_end (prev
);
12329 parser
->in_statement
= in_statement
;
12330 finish_do_body (statement
);
12331 /* Look for the `while' keyword. */
12332 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12333 /* Look for the `('. */
12334 matching_parens parens
;
12335 parens
.require_open (parser
);
12336 /* Parse the expression. */
12337 expression
= cp_parser_expression (parser
);
12338 /* We're done with the do-statement. */
12339 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12340 /* Look for the `)'. */
12341 parens
.require_close (parser
);
12342 /* Look for the `;'. */
12343 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12349 /* Look for the `('. */
12350 matching_parens parens
;
12351 parens
.require_open (parser
);
12353 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12355 /* Look for the `)'. */
12356 parens
.require_close (parser
);
12358 /* Parse the body of the for-statement. */
12359 parser
->in_statement
= IN_ITERATION_STMT
;
12360 bool prev
= note_iteration_stmt_body_start ();
12361 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12362 note_iteration_stmt_body_end (prev
);
12363 parser
->in_statement
= in_statement
;
12365 /* We're done with the for-statement. */
12366 finish_for_stmt (statement
);
12371 cp_parser_error (parser
, "expected iteration-statement");
12372 statement
= error_mark_node
;
12379 /* Parse a init-statement or the declarator of a range-based-for.
12380 Returns true if a range-based-for declaration is seen.
12383 expression-statement
12384 simple-declaration */
12387 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12389 /* If the next token is a `;', then we have an empty
12390 expression-statement. Grammatically, this is also a
12391 simple-declaration, but an invalid one, because it does not
12392 declare anything. Therefore, if we did not handle this case
12393 specially, we would issue an error message about an invalid
12395 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12397 bool is_range_for
= false;
12398 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12400 /* Try to parse the init-statement. */
12401 if (cp_parser_range_based_for_with_init_p (parser
))
12404 cp_parser_parse_tentatively (parser
);
12405 /* Parse the declaration. */
12406 cp_parser_simple_declaration (parser
,
12407 /*function_definition_allowed_p=*/false,
12409 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12410 if (!cp_parser_parse_definitely (parser
))
12411 /* That didn't work, try to parse it as an expression-statement. */
12412 cp_parser_expression_statement (parser
, NULL_TREE
);
12414 if (cxx_dialect
< cxx2a
)
12416 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12417 "range-based %<for%> loops with initializer only "
12418 "available with -std=c++2a or -std=gnu++2a");
12419 *decl
= error_mark_node
;
12423 /* A colon is used in range-based for. */
12424 parser
->colon_corrects_to_scope_p
= false;
12426 /* We're going to speculatively look for a declaration, falling back
12427 to an expression, if necessary. */
12428 cp_parser_parse_tentatively (parser
);
12429 /* Parse the declaration. */
12430 cp_parser_simple_declaration (parser
,
12431 /*function_definition_allowed_p=*/false,
12433 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12436 /* It is a range-for, consume the ':'. */
12437 cp_lexer_consume_token (parser
->lexer
);
12438 is_range_for
= true;
12439 if (cxx_dialect
< cxx11
)
12440 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12441 "range-based %<for%> loops only available with "
12442 "-std=c++11 or -std=gnu++11");
12445 /* The ';' is not consumed yet because we told
12446 cp_parser_simple_declaration not to. */
12447 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12449 if (cp_parser_parse_definitely (parser
))
12450 return is_range_for
;
12451 /* If the tentative parse failed, then we shall need to look for an
12452 expression-statement. */
12454 /* If we are here, it is an expression-statement. */
12455 cp_parser_expression_statement (parser
, NULL_TREE
);
12459 /* Parse a jump-statement.
12464 return expression [opt] ;
12465 return braced-init-list ;
12471 goto * expression ;
12473 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12476 cp_parser_jump_statement (cp_parser
* parser
)
12478 tree statement
= error_mark_node
;
12481 unsigned char in_statement
;
12483 /* Peek at the next token. */
12484 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12486 return error_mark_node
;
12488 /* See what kind of keyword it is. */
12489 keyword
= token
->keyword
;
12493 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12494 switch (in_statement
)
12497 error_at (token
->location
, "break statement not within loop or switch");
12500 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12501 || in_statement
== IN_ITERATION_STMT
);
12502 statement
= finish_break_stmt ();
12503 if (in_statement
== IN_ITERATION_STMT
)
12504 break_maybe_infinite_loop ();
12507 error_at (token
->location
, "invalid exit from OpenMP structured block");
12510 error_at (token
->location
, "break statement used with OpenMP for loop");
12513 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12517 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12520 error_at (token
->location
, "continue statement not within a loop");
12522 /* Fall through. */
12523 case IN_ITERATION_STMT
:
12525 statement
= finish_continue_stmt ();
12528 error_at (token
->location
, "invalid exit from OpenMP structured block");
12531 gcc_unreachable ();
12533 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12539 bool expr_non_constant_p
;
12541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12543 cp_lexer_set_source_position (parser
->lexer
);
12544 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12545 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12547 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12548 expr
= cp_parser_expression (parser
);
12550 /* If the next token is a `;', then there is no
12553 /* Build the return-statement. */
12554 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12555 /* Don't deduce from a discarded return statement. */;
12557 statement
= finish_return_stmt (expr
);
12558 /* Look for the final `;'. */
12559 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12564 if (parser
->in_function_body
12565 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12567 error ("%<goto%> in %<constexpr%> function");
12568 cp_function_chain
->invalid_constexpr
= true;
12571 /* Create the goto-statement. */
12572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12574 /* Issue a warning about this use of a GNU extension. */
12575 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12576 /* Consume the '*' token. */
12577 cp_lexer_consume_token (parser
->lexer
);
12578 /* Parse the dependent expression. */
12579 finish_goto_stmt (cp_parser_expression (parser
));
12582 finish_goto_stmt (cp_parser_identifier (parser
));
12583 /* Look for the final `;'. */
12584 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12588 cp_parser_error (parser
, "expected jump-statement");
12595 /* Parse a declaration-statement.
12597 declaration-statement:
12598 block-declaration */
12601 cp_parser_declaration_statement (cp_parser
* parser
)
12605 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12606 p
= obstack_alloc (&declarator_obstack
, 0);
12608 /* Parse the block-declaration. */
12609 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12611 /* Free any declarators allocated. */
12612 obstack_free (&declarator_obstack
, p
);
12615 /* Some dependent statements (like `if (cond) statement'), are
12616 implicitly in their own scope. In other words, if the statement is
12617 a single statement (as opposed to a compound-statement), it is
12618 none-the-less treated as if it were enclosed in braces. Any
12619 declarations appearing in the dependent statement are out of scope
12620 after control passes that point. This function parses a statement,
12621 but ensures that is in its own scope, even if it is not a
12622 compound-statement.
12624 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12625 is a (possibly labeled) if statement which is not enclosed in
12626 braces and has an else clause. This is used to implement
12629 CHAIN is a vector of if-else-if conditions. This is used to implement
12632 Returns the new statement. */
12635 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12636 const token_indent_info
&guard_tinfo
,
12640 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12641 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12642 token_indent_info body_tinfo
12643 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12648 /* Mark if () ; with a special NOP_EXPR. */
12649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12651 cp_lexer_consume_token (parser
->lexer
);
12652 statement
= add_stmt (build_empty_stmt (body_loc
));
12654 if (guard_tinfo
.keyword
== RID_IF
12655 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12656 warning_at (body_loc
, OPT_Wempty_body
,
12657 "suggest braces around empty body in an %<if%> statement");
12658 else if (guard_tinfo
.keyword
== RID_ELSE
)
12659 warning_at (body_loc
, OPT_Wempty_body
,
12660 "suggest braces around empty body in an %<else%> statement");
12662 /* if a compound is opened, we simply parse the statement directly. */
12663 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12664 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12665 /* If the token is not a `{', then we must take special action. */
12668 /* Create a compound-statement. */
12669 statement
= begin_compound_stmt (0);
12670 /* Parse the dependent-statement. */
12671 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12672 &body_loc_after_labels
);
12673 /* Finish the dummy compound-statement. */
12674 finish_compound_stmt (statement
);
12677 token_indent_info next_tinfo
12678 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12679 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12681 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12682 && next_tinfo
.type
!= CPP_SEMICOLON
)
12683 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12684 guard_tinfo
.location
, guard_tinfo
.keyword
);
12686 /* Return the statement. */
12690 /* For some dependent statements (like `while (cond) statement'), we
12691 have already created a scope. Therefore, even if the dependent
12692 statement is a compound-statement, we do not want to create another
12696 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12697 const token_indent_info
&guard_tinfo
)
12699 /* If the token is a `{', then we must take special action. */
12700 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12702 token_indent_info body_tinfo
12703 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12704 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12706 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12707 &loc_after_labels
);
12708 token_indent_info next_tinfo
12709 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12710 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12712 if (loc_after_labels
!= UNKNOWN_LOCATION
12713 && next_tinfo
.type
!= CPP_SEMICOLON
)
12714 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12715 guard_tinfo
.location
,
12716 guard_tinfo
.keyword
);
12720 /* Avoid calling cp_parser_compound_statement, so that we
12721 don't create a new scope. Do everything else by hand. */
12722 matching_braces braces
;
12723 braces
.require_open (parser
);
12724 /* If the next keyword is `__label__' we have a label declaration. */
12725 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12726 cp_parser_label_declaration (parser
);
12727 /* Parse an (optional) statement-seq. */
12728 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12729 braces
.require_close (parser
);
12733 /* Declarations [gram.dcl.dcl] */
12735 /* Parse an optional declaration-sequence.
12739 declaration-seq declaration */
12742 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12746 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12748 if (token
->type
== CPP_CLOSE_BRACE
12749 || token
->type
== CPP_EOF
)
12752 cp_parser_toplevel_declaration (parser
);
12756 /* Parse a declaration.
12760 function-definition
12761 template-declaration
12762 explicit-instantiation
12763 explicit-specialization
12764 linkage-specification
12765 namespace-definition
12773 __extension__ declaration */
12776 cp_parser_declaration (cp_parser
* parser
)
12780 int saved_pedantic
;
12782 tree attributes
= NULL_TREE
;
12784 /* Check for the `__extension__' keyword. */
12785 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12787 /* Parse the qualified declaration. */
12788 cp_parser_declaration (parser
);
12789 /* Restore the PEDANTIC flag. */
12790 pedantic
= saved_pedantic
;
12795 /* Try to figure out what kind of declaration is present. */
12796 token1
= *cp_lexer_peek_token (parser
->lexer
);
12798 if (token1
.type
!= CPP_EOF
)
12799 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12802 token2
.type
= CPP_EOF
;
12803 token2
.keyword
= RID_MAX
;
12806 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12807 p
= obstack_alloc (&declarator_obstack
, 0);
12809 /* If the next token is `extern' and the following token is a string
12810 literal, then we have a linkage specification. */
12811 if (token1
.keyword
== RID_EXTERN
12812 && cp_parser_is_pure_string_literal (&token2
))
12813 cp_parser_linkage_specification (parser
);
12814 /* If the next token is `template', then we have either a template
12815 declaration, an explicit instantiation, or an explicit
12817 else if (token1
.keyword
== RID_TEMPLATE
)
12819 /* `template <>' indicates a template specialization. */
12820 if (token2
.type
== CPP_LESS
12821 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12822 cp_parser_explicit_specialization (parser
);
12823 /* `template <' indicates a template declaration. */
12824 else if (token2
.type
== CPP_LESS
)
12825 cp_parser_template_declaration (parser
, /*member_p=*/false);
12826 /* Anything else must be an explicit instantiation. */
12828 cp_parser_explicit_instantiation (parser
);
12830 /* If the next token is `export', then we have a template
12832 else if (token1
.keyword
== RID_EXPORT
)
12833 cp_parser_template_declaration (parser
, /*member_p=*/false);
12834 /* If the next token is `extern', 'static' or 'inline' and the one
12835 after that is `template', we have a GNU extended explicit
12836 instantiation directive. */
12837 else if (cp_parser_allow_gnu_extensions_p (parser
)
12838 && (token1
.keyword
== RID_EXTERN
12839 || token1
.keyword
== RID_STATIC
12840 || token1
.keyword
== RID_INLINE
)
12841 && token2
.keyword
== RID_TEMPLATE
)
12842 cp_parser_explicit_instantiation (parser
);
12843 /* If the next token is `namespace', check for a named or unnamed
12844 namespace definition. */
12845 else if (token1
.keyword
== RID_NAMESPACE
12846 && (/* A named namespace definition. */
12847 (token2
.type
== CPP_NAME
12848 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12850 || (token2
.type
== CPP_OPEN_SQUARE
12851 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12852 == CPP_OPEN_SQUARE
)
12853 /* An unnamed namespace definition. */
12854 || token2
.type
== CPP_OPEN_BRACE
12855 || token2
.keyword
== RID_ATTRIBUTE
))
12856 cp_parser_namespace_definition (parser
);
12857 /* An inline (associated) namespace definition. */
12858 else if (token1
.keyword
== RID_INLINE
12859 && token2
.keyword
== RID_NAMESPACE
)
12860 cp_parser_namespace_definition (parser
);
12861 /* Objective-C++ declaration/definition. */
12862 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12863 cp_parser_objc_declaration (parser
, NULL_TREE
);
12864 else if (c_dialect_objc ()
12865 && token1
.keyword
== RID_ATTRIBUTE
12866 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12867 cp_parser_objc_declaration (parser
, attributes
);
12868 /* At this point we may have a template declared by a concept
12870 else if (flag_concepts
12871 && cp_parser_template_declaration_after_export (parser
,
12872 /*member_p=*/false))
12875 /* Try to parse a block-declaration, or a function-definition. */
12876 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12878 /* Free any declarators allocated. */
12879 obstack_free (&declarator_obstack
, p
);
12882 /* Parse a namespace-scope declaration. */
12885 cp_parser_toplevel_declaration (cp_parser
* parser
)
12887 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12889 if (token
->type
== CPP_PRAGMA
)
12890 /* A top-level declaration can consist solely of a #pragma. A
12891 nested declaration cannot, so this is done here and not in
12892 cp_parser_declaration. (A #pragma at block scope is
12893 handled in cp_parser_statement.) */
12894 cp_parser_pragma (parser
, pragma_external
, NULL
);
12895 else if (token
->type
== CPP_SEMICOLON
)
12897 /* A declaration consisting of a single semicolon is
12898 invalid. Allow it unless we're being pedantic. */
12899 cp_lexer_consume_token (parser
->lexer
);
12900 if (!in_system_header_at (input_location
))
12901 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12904 /* Parse the declaration itself. */
12905 cp_parser_declaration (parser
);
12908 /* Parse a block-declaration.
12913 namespace-alias-definition
12920 __extension__ block-declaration
12925 static_assert-declaration
12927 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12928 part of a declaration-statement. */
12931 cp_parser_block_declaration (cp_parser
*parser
,
12935 int saved_pedantic
;
12937 /* Check for the `__extension__' keyword. */
12938 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12940 /* Parse the qualified declaration. */
12941 cp_parser_block_declaration (parser
, statement_p
);
12942 /* Restore the PEDANTIC flag. */
12943 pedantic
= saved_pedantic
;
12948 /* Peek at the next token to figure out which kind of declaration is
12950 token1
= cp_lexer_peek_token (parser
->lexer
);
12952 /* If the next keyword is `asm', we have an asm-definition. */
12953 if (token1
->keyword
== RID_ASM
)
12956 cp_parser_commit_to_tentative_parse (parser
);
12957 cp_parser_asm_definition (parser
);
12959 /* If the next keyword is `namespace', we have a
12960 namespace-alias-definition. */
12961 else if (token1
->keyword
== RID_NAMESPACE
)
12962 cp_parser_namespace_alias_definition (parser
);
12963 /* If the next keyword is `using', we have a
12964 using-declaration, a using-directive, or an alias-declaration. */
12965 else if (token1
->keyword
== RID_USING
)
12970 cp_parser_commit_to_tentative_parse (parser
);
12971 /* If the token after `using' is `namespace', then we have a
12972 using-directive. */
12973 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12974 if (token2
->keyword
== RID_NAMESPACE
)
12975 cp_parser_using_directive (parser
);
12976 /* If the second token after 'using' is '=', then we have an
12977 alias-declaration. */
12978 else if (cxx_dialect
>= cxx11
12979 && token2
->type
== CPP_NAME
12980 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12981 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12982 cp_parser_alias_declaration (parser
);
12983 /* Otherwise, it's a using-declaration. */
12985 cp_parser_using_declaration (parser
,
12986 /*access_declaration_p=*/false);
12988 /* If the next keyword is `__label__' we have a misplaced label
12990 else if (token1
->keyword
== RID_LABEL
)
12992 cp_lexer_consume_token (parser
->lexer
);
12993 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12994 cp_parser_skip_to_end_of_statement (parser
);
12995 /* If the next token is now a `;', consume it. */
12996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12997 cp_lexer_consume_token (parser
->lexer
);
12999 /* If the next token is `static_assert' we have a static assertion. */
13000 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13001 cp_parser_static_assert (parser
, /*member_p=*/false);
13002 /* Anything else must be a simple-declaration. */
13004 cp_parser_simple_declaration (parser
, !statement_p
,
13005 /*maybe_range_for_decl*/NULL
);
13008 /* Parse a simple-declaration.
13010 simple-declaration:
13011 decl-specifier-seq [opt] init-declarator-list [opt] ;
13012 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13013 brace-or-equal-initializer ;
13015 init-declarator-list:
13017 init-declarator-list , init-declarator
13019 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13020 function-definition as a simple-declaration.
13022 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13023 parsed declaration if it is an uninitialized single declarator not followed
13024 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13025 if present, will not be consumed. */
13028 cp_parser_simple_declaration (cp_parser
* parser
,
13029 bool function_definition_allowed_p
,
13030 tree
*maybe_range_for_decl
)
13032 cp_decl_specifier_seq decl_specifiers
;
13033 int declares_class_or_enum
;
13034 bool saw_declarator
;
13035 location_t comma_loc
= UNKNOWN_LOCATION
;
13036 location_t init_loc
= UNKNOWN_LOCATION
;
13038 if (maybe_range_for_decl
)
13039 *maybe_range_for_decl
= NULL_TREE
;
13041 /* Defer access checks until we know what is being declared; the
13042 checks for names appearing in the decl-specifier-seq should be
13043 done as if we were in the scope of the thing being declared. */
13044 push_deferring_access_checks (dk_deferred
);
13046 /* Parse the decl-specifier-seq. We have to keep track of whether
13047 or not the decl-specifier-seq declares a named class or
13048 enumeration type, since that is the only case in which the
13049 init-declarator-list is allowed to be empty.
13053 In a simple-declaration, the optional init-declarator-list can be
13054 omitted only when declaring a class or enumeration, that is when
13055 the decl-specifier-seq contains either a class-specifier, an
13056 elaborated-type-specifier, or an enum-specifier. */
13057 cp_parser_decl_specifier_seq (parser
,
13058 CP_PARSER_FLAGS_OPTIONAL
,
13060 &declares_class_or_enum
);
13061 /* We no longer need to defer access checks. */
13062 stop_deferring_access_checks ();
13064 /* In a block scope, a valid declaration must always have a
13065 decl-specifier-seq. By not trying to parse declarators, we can
13066 resolve the declaration/expression ambiguity more quickly. */
13067 if (!function_definition_allowed_p
13068 && !decl_specifiers
.any_specifiers_p
)
13070 cp_parser_error (parser
, "expected declaration");
13074 /* If the next two tokens are both identifiers, the code is
13075 erroneous. The usual cause of this situation is code like:
13079 where "T" should name a type -- but does not. */
13080 if (!decl_specifiers
.any_type_specifiers_p
13081 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13083 /* If parsing tentatively, we should commit; we really are
13084 looking at a declaration. */
13085 cp_parser_commit_to_tentative_parse (parser
);
13090 cp_parser_maybe_commit_to_declaration (parser
,
13091 decl_specifiers
.any_specifiers_p
);
13093 /* Look for C++17 decomposition declaration. */
13094 for (size_t n
= 1; ; n
++)
13095 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13096 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13098 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13099 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13100 && decl_specifiers
.any_specifiers_p
)
13103 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13104 maybe_range_for_decl
,
13107 /* The next token should be either a `,' or a `;'. */
13108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13109 /* If it's a `;', we are done. */
13110 if (token
->type
== CPP_SEMICOLON
)
13112 else if (maybe_range_for_decl
)
13114 if (*maybe_range_for_decl
== NULL_TREE
)
13115 *maybe_range_for_decl
= error_mark_node
;
13118 /* Anything else is an error. */
13121 /* If we have already issued an error message we don't need
13122 to issue another one. */
13123 if ((decl
!= error_mark_node
13124 && DECL_INITIAL (decl
) != error_mark_node
)
13125 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13126 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13127 /* Skip tokens until we reach the end of the statement. */
13128 cp_parser_skip_to_end_of_statement (parser
);
13129 /* If the next token is now a `;', consume it. */
13130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13131 cp_lexer_consume_token (parser
->lexer
);
13139 bool auto_specifier_p
;
13140 /* NULL_TREE if both variable and function declaration are allowed,
13141 error_mark_node if function declaration are not allowed and
13142 a FUNCTION_DECL that should be diagnosed if it is followed by
13143 variable declarations. */
13144 tree auto_function_declaration
;
13146 last_type
= NULL_TREE
;
13148 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13149 auto_function_declaration
= NULL_TREE
;
13151 /* Keep going until we hit the `;' at the end of the simple
13153 saw_declarator
= false;
13154 while (cp_lexer_next_token_is_not (parser
->lexer
,
13158 bool function_definition_p
;
13160 tree auto_result
= NULL_TREE
;
13162 if (saw_declarator
)
13164 /* If we are processing next declarator, comma is expected */
13165 token
= cp_lexer_peek_token (parser
->lexer
);
13166 gcc_assert (token
->type
== CPP_COMMA
);
13167 cp_lexer_consume_token (parser
->lexer
);
13168 if (maybe_range_for_decl
)
13170 *maybe_range_for_decl
= error_mark_node
;
13171 if (comma_loc
== UNKNOWN_LOCATION
)
13172 comma_loc
= token
->location
;
13176 saw_declarator
= true;
13178 /* Parse the init-declarator. */
13179 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13181 function_definition_allowed_p
,
13182 /*member_p=*/false,
13183 declares_class_or_enum
,
13184 &function_definition_p
,
13185 maybe_range_for_decl
,
13188 /* If an error occurred while parsing tentatively, exit quickly.
13189 (That usually happens when in the body of a function; each
13190 statement is treated as a declaration-statement until proven
13192 if (cp_parser_error_occurred (parser
))
13195 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13197 /* If the init-declarator-list contains more than one
13198 init-declarator, they shall all form declarations of
13200 if (auto_function_declaration
== NULL_TREE
)
13201 auto_function_declaration
13202 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13203 else if (TREE_CODE (decl
) == FUNCTION_DECL
13204 || auto_function_declaration
!= error_mark_node
)
13206 error_at (decl_specifiers
.locations
[ds_type_spec
],
13207 "non-variable %qD in declaration with more than one "
13208 "declarator with placeholder type",
13209 TREE_CODE (decl
) == FUNCTION_DECL
13210 ? decl
: auto_function_declaration
);
13211 auto_function_declaration
= error_mark_node
;
13216 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13219 && last_type
!= error_mark_node
13220 && !same_type_p (auto_result
, last_type
))
13222 /* If the list of declarators contains more than one declarator,
13223 the type of each declared variable is determined as described
13224 above. If the type deduced for the template parameter U is not
13225 the same in each deduction, the program is ill-formed. */
13226 error_at (decl_specifiers
.locations
[ds_type_spec
],
13227 "inconsistent deduction for %qT: %qT and then %qT",
13228 decl_specifiers
.type
, last_type
, auto_result
);
13229 last_type
= error_mark_node
;
13232 last_type
= auto_result
;
13235 /* Handle function definitions specially. */
13236 if (function_definition_p
)
13238 /* If the next token is a `,', then we are probably
13239 processing something like:
13243 which is erroneous. */
13244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13246 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13247 error_at (token
->location
,
13249 " declarations and function-definitions is forbidden");
13251 /* Otherwise, we're done with the list of declarators. */
13254 pop_deferring_access_checks ();
13258 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13259 *maybe_range_for_decl
= decl
;
13260 /* The next token should be either a `,' or a `;'. */
13261 token
= cp_lexer_peek_token (parser
->lexer
);
13262 /* If it's a `,', there are more declarators to come. */
13263 if (token
->type
== CPP_COMMA
)
13264 /* will be consumed next time around */;
13265 /* If it's a `;', we are done. */
13266 else if (token
->type
== CPP_SEMICOLON
)
13268 else if (maybe_range_for_decl
)
13270 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13271 permerror (decl_specifiers
.locations
[ds_type_spec
],
13272 "types may not be defined in a for-range-declaration");
13275 /* Anything else is an error. */
13278 /* If we have already issued an error message we don't need
13279 to issue another one. */
13280 if ((decl
!= error_mark_node
13281 && DECL_INITIAL (decl
) != error_mark_node
)
13282 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13283 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13284 /* Skip tokens until we reach the end of the statement. */
13285 cp_parser_skip_to_end_of_statement (parser
);
13286 /* If the next token is now a `;', consume it. */
13287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13288 cp_lexer_consume_token (parser
->lexer
);
13291 /* After the first time around, a function-definition is not
13292 allowed -- even if it was OK at first. For example:
13297 function_definition_allowed_p
= false;
13300 /* Issue an error message if no declarators are present, and the
13301 decl-specifier-seq does not itself declare a class or
13302 enumeration: [dcl.dcl]/3. */
13303 if (!saw_declarator
)
13305 if (cp_parser_declares_only_class_p (parser
))
13307 if (!declares_class_or_enum
13308 && decl_specifiers
.type
13309 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13310 /* Ensure an error is issued anyway when finish_decltype_type,
13311 called via cp_parser_decl_specifier_seq, returns a class or
13312 an enumeration (c++/51786). */
13313 decl_specifiers
.type
= NULL_TREE
;
13314 shadow_tag (&decl_specifiers
);
13316 /* Perform any deferred access checks. */
13317 perform_deferred_access_checks (tf_warning_or_error
);
13320 /* Consume the `;'. */
13322 if (!maybe_range_for_decl
)
13323 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13324 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13326 if (init_loc
!= UNKNOWN_LOCATION
)
13327 error_at (init_loc
, "initializer in range-based %<for%> loop");
13328 if (comma_loc
!= UNKNOWN_LOCATION
)
13329 error_at (comma_loc
,
13330 "multiple declarations in range-based %<for%> loop");
13334 pop_deferring_access_checks ();
13337 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13338 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13342 cp_parser_decomposition_declaration (cp_parser
*parser
,
13343 cp_decl_specifier_seq
*decl_specifiers
,
13344 tree
*maybe_range_for_decl
,
13345 location_t
*init_loc
)
13347 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13348 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13349 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13351 /* Parse the identifier-list. */
13352 auto_vec
<cp_expr
, 10> v
;
13353 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13356 cp_expr e
= cp_parser_identifier (parser
);
13357 if (e
.get_value () == error_mark_node
)
13360 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13362 cp_lexer_consume_token (parser
->lexer
);
13365 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13366 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13368 end_loc
= UNKNOWN_LOCATION
;
13369 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13372 cp_lexer_consume_token (parser
->lexer
);
13375 cp_parser_skip_to_end_of_statement (parser
);
13376 return error_mark_node
;
13380 if (cxx_dialect
< cxx17
)
13381 pedwarn (loc
, 0, "structured bindings only available with "
13382 "-std=c++17 or -std=gnu++17");
13385 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13386 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13387 declarator
->id_loc
= loc
;
13388 if (ref_qual
!= REF_QUAL_NONE
)
13389 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13390 ref_qual
== REF_QUAL_RVALUE
,
13392 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13393 NULL_TREE
, decl_specifiers
->attributes
,
13395 tree orig_decl
= decl
;
13399 cp_decl_specifier_seq decl_specs
;
13400 clear_decl_specs (&decl_specs
);
13401 decl_specs
.type
= make_auto ();
13403 FOR_EACH_VEC_ELT (v
, i
, e
)
13406 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13408 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13409 declarator
->id_loc
= e
.get_location ();
13410 tree elt_pushed_scope
;
13411 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13412 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13413 if (decl2
== error_mark_node
)
13414 decl
= error_mark_node
;
13415 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13417 /* Ensure we've diagnosed redeclaration if we aren't creating
13419 gcc_assert (errorcount
);
13420 decl
= error_mark_node
;
13424 if (elt_pushed_scope
)
13425 pop_scope (elt_pushed_scope
);
13430 error_at (loc
, "empty structured binding declaration");
13431 decl
= error_mark_node
;
13434 if (maybe_range_for_decl
== NULL
13435 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13437 bool non_constant_p
= false, is_direct_init
= false;
13438 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13439 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13441 if (initializer
== NULL_TREE
13442 || (TREE_CODE (initializer
) == TREE_LIST
13443 && TREE_CHAIN (initializer
))
13445 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13446 && CONSTRUCTOR_NELTS (initializer
) != 1))
13448 error_at (loc
, "invalid initializer for structured binding "
13450 initializer
= error_mark_node
;
13453 if (decl
!= error_mark_node
)
13455 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13456 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13457 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13458 cp_finish_decomp (decl
, prev
, v
.length ());
13461 else if (decl
!= error_mark_node
)
13463 *maybe_range_for_decl
= prev
;
13464 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13465 the underlying DECL. */
13466 cp_finish_decomp (decl
, prev
, v
.length ());
13470 pop_scope (pushed_scope
);
13472 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13474 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13475 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13481 /* Parse a decl-specifier-seq.
13483 decl-specifier-seq:
13484 decl-specifier-seq [opt] decl-specifier
13485 decl-specifier attribute-specifier-seq [opt] (C++11)
13488 storage-class-specifier
13499 Concepts Extension:
13504 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13506 The parser flags FLAGS is used to control type-specifier parsing.
13508 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13511 1: one of the decl-specifiers is an elaborated-type-specifier
13512 (i.e., a type declaration)
13513 2: one of the decl-specifiers is an enum-specifier or a
13514 class-specifier (i.e., a type definition)
13519 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13520 cp_parser_flags flags
,
13521 cp_decl_specifier_seq
*decl_specs
,
13522 int* declares_class_or_enum
)
13524 bool constructor_possible_p
= !parser
->in_declarator_p
;
13525 bool found_decl_spec
= false;
13526 cp_token
*start_token
= NULL
;
13529 /* Clear DECL_SPECS. */
13530 clear_decl_specs (decl_specs
);
13532 /* Assume no class or enumeration type is declared. */
13533 *declares_class_or_enum
= 0;
13535 /* Keep reading specifiers until there are no more to read. */
13538 bool constructor_p
;
13542 /* Peek at the next token. */
13543 token
= cp_lexer_peek_token (parser
->lexer
);
13545 /* Save the first token of the decl spec list for error
13548 start_token
= token
;
13549 /* Handle attributes. */
13550 if (cp_next_tokens_can_be_attribute_p (parser
))
13552 /* Parse the attributes. */
13553 tree attrs
= cp_parser_attributes_opt (parser
);
13555 /* In a sequence of declaration specifiers, c++11 attributes
13556 appertain to the type that precede them. In that case
13559 The attribute-specifier-seq affects the type only for
13560 the declaration it appears in, not other declarations
13561 involving the same type.
13563 But for now let's force the user to position the
13564 attribute either at the beginning of the declaration or
13565 after the declarator-id, which would clearly mean that it
13566 applies to the declarator. */
13567 if (cxx11_attribute_p (attrs
))
13569 if (!found_decl_spec
)
13570 /* The c++11 attribute is at the beginning of the
13571 declaration. It appertains to the entity being
13575 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13577 /* This is an attribute following a
13578 class-specifier. */
13579 if (decl_specs
->type_definition_p
)
13580 warn_misplaced_attr_for_class_type (token
->location
,
13586 decl_specs
->std_attributes
13587 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13588 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13589 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13595 decl_specs
->attributes
13596 = attr_chainon (decl_specs
->attributes
, attrs
);
13597 if (decl_specs
->locations
[ds_attribute
] == 0)
13598 decl_specs
->locations
[ds_attribute
] = token
->location
;
13601 /* Assume we will find a decl-specifier keyword. */
13602 found_decl_spec
= true;
13603 /* If the next token is an appropriate keyword, we can simply
13604 add it to the list. */
13605 switch (token
->keyword
)
13611 if (!at_class_scope_p ())
13613 gcc_rich_location
richloc (token
->location
);
13614 richloc
.add_fixit_remove ();
13615 error_at (&richloc
, "%<friend%> used outside of class");
13616 cp_lexer_purge_token (parser
->lexer
);
13621 /* Consume the token. */
13622 cp_lexer_consume_token (parser
->lexer
);
13626 case RID_CONSTEXPR
:
13628 cp_lexer_consume_token (parser
->lexer
);
13633 cp_lexer_consume_token (parser
->lexer
);
13636 /* function-specifier:
13643 cp_parser_function_specifier_opt (parser
, decl_specs
);
13650 /* Consume the token. */
13651 cp_lexer_consume_token (parser
->lexer
);
13652 /* A constructor declarator cannot appear in a typedef. */
13653 constructor_possible_p
= false;
13654 /* The "typedef" keyword can only occur in a declaration; we
13655 may as well commit at this point. */
13656 cp_parser_commit_to_tentative_parse (parser
);
13658 if (decl_specs
->storage_class
!= sc_none
)
13659 decl_specs
->conflicting_specifiers_p
= true;
13662 /* storage-class-specifier:
13672 if (cxx_dialect
== cxx98
)
13674 /* Consume the token. */
13675 cp_lexer_consume_token (parser
->lexer
);
13677 /* Complain about `auto' as a storage specifier, if
13678 we're complaining about C++0x compatibility. */
13679 gcc_rich_location
richloc (token
->location
);
13680 richloc
.add_fixit_remove ();
13681 warning_at (&richloc
, OPT_Wc__11_compat
,
13682 "%<auto%> changes meaning in C++11; "
13683 "please remove it");
13685 /* Set the storage class anyway. */
13686 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13690 /* C++0x auto type-specifier. */
13691 found_decl_spec
= false;
13698 /* Consume the token. */
13699 cp_lexer_consume_token (parser
->lexer
);
13700 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13704 /* Consume the token. */
13706 cp_lexer_consume_token (parser
->lexer
);
13710 /* We did not yet find a decl-specifier yet. */
13711 found_decl_spec
= false;
13715 if (found_decl_spec
13716 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13717 && token
->keyword
!= RID_CONSTEXPR
)
13718 error ("decl-specifier invalid in condition");
13720 if (found_decl_spec
13721 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13722 && token
->keyword
!= RID_MUTABLE
13723 && token
->keyword
!= RID_CONSTEXPR
)
13724 error_at (token
->location
, "%qD invalid in lambda",
13725 ridpointers
[token
->keyword
]);
13728 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13730 /* Constructors are a special case. The `S' in `S()' is not a
13731 decl-specifier; it is the beginning of the declarator. */
13733 = (!found_decl_spec
13734 && constructor_possible_p
13735 && (cp_parser_constructor_declarator_p
13736 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13738 /* If we don't have a DECL_SPEC yet, then we must be looking at
13739 a type-specifier. */
13740 if (!found_decl_spec
&& !constructor_p
)
13742 int decl_spec_declares_class_or_enum
;
13743 bool is_cv_qualifier
;
13747 = cp_parser_type_specifier (parser
, flags
,
13749 /*is_declaration=*/true,
13750 &decl_spec_declares_class_or_enum
,
13752 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13754 /* If this type-specifier referenced a user-defined type
13755 (a typedef, class-name, etc.), then we can't allow any
13756 more such type-specifiers henceforth.
13760 The longest sequence of decl-specifiers that could
13761 possibly be a type name is taken as the
13762 decl-specifier-seq of a declaration. The sequence shall
13763 be self-consistent as described below.
13767 As a general rule, at most one type-specifier is allowed
13768 in the complete decl-specifier-seq of a declaration. The
13769 only exceptions are the following:
13771 -- const or volatile can be combined with any other
13774 -- signed or unsigned can be combined with char, long,
13782 void g (const int Pc);
13784 Here, Pc is *not* part of the decl-specifier seq; it's
13785 the declarator. Therefore, once we see a type-specifier
13786 (other than a cv-qualifier), we forbid any additional
13787 user-defined types. We *do* still allow things like `int
13788 int' to be considered a decl-specifier-seq, and issue the
13789 error message later. */
13790 if (type_spec
&& !is_cv_qualifier
)
13791 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13792 /* A constructor declarator cannot follow a type-specifier. */
13795 constructor_possible_p
= false;
13796 found_decl_spec
= true;
13797 if (!is_cv_qualifier
)
13798 decl_specs
->any_type_specifiers_p
= true;
13800 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
13801 error_at (token
->location
, "type-specifier invalid in lambda");
13805 /* If we still do not have a DECL_SPEC, then there are no more
13806 decl-specifiers. */
13807 if (!found_decl_spec
)
13810 decl_specs
->any_specifiers_p
= true;
13811 /* After we see one decl-specifier, further decl-specifiers are
13812 always optional. */
13813 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13816 /* Don't allow a friend specifier with a class definition. */
13817 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13818 && (*declares_class_or_enum
& 2))
13819 error_at (decl_specs
->locations
[ds_friend
],
13820 "class definition may not be declared a friend");
13823 /* Parse an (optional) storage-class-specifier.
13825 storage-class-specifier:
13834 storage-class-specifier:
13837 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13840 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13842 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13845 if (cxx_dialect
!= cxx98
)
13847 /* Fall through for C++98. */
13848 gcc_fallthrough ();
13855 /* Consume the token. */
13856 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13863 /* Parse an (optional) function-specifier.
13865 function-specifier:
13870 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13871 Updates DECL_SPECS, if it is non-NULL. */
13874 cp_parser_function_specifier_opt (cp_parser
* parser
,
13875 cp_decl_specifier_seq
*decl_specs
)
13877 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13878 switch (token
->keyword
)
13881 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13885 /* 14.5.2.3 [temp.mem]
13887 A member function template shall not be virtual. */
13888 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13889 && current_class_type
)
13890 error_at (token
->location
, "templates may not be %<virtual%>");
13892 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13896 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13903 /* Consume the token. */
13904 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13907 /* Parse a linkage-specification.
13909 linkage-specification:
13910 extern string-literal { declaration-seq [opt] }
13911 extern string-literal declaration */
13914 cp_parser_linkage_specification (cp_parser
* parser
)
13918 /* Look for the `extern' keyword. */
13919 cp_token
*extern_token
13920 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13922 /* Look for the string-literal. */
13923 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13924 linkage
= cp_parser_string_literal (parser
, false, false);
13926 /* Transform the literal into an identifier. If the literal is a
13927 wide-character string, or contains embedded NULs, then we can't
13928 handle it as the user wants. */
13929 if (strlen (TREE_STRING_POINTER (linkage
))
13930 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13932 cp_parser_error (parser
, "invalid linkage-specification");
13933 /* Assume C++ linkage. */
13934 linkage
= lang_name_cplusplus
;
13937 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13939 /* We're now using the new linkage. */
13940 push_lang_context (linkage
);
13942 /* Preserve the location of the the innermost linkage specification,
13943 tracking the locations of nested specifications via a local. */
13944 location_t saved_location
13945 = parser
->innermost_linkage_specification_location
;
13946 /* Construct a location ranging from the start of the "extern" to
13947 the end of the string-literal, with the caret at the start, e.g.:
13951 parser
->innermost_linkage_specification_location
13952 = make_location (extern_token
->location
,
13953 extern_token
->location
,
13954 get_finish (string_token
->location
));
13956 /* If the next token is a `{', then we're using the first
13958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13960 cp_ensure_no_omp_declare_simd (parser
);
13961 cp_ensure_no_oacc_routine (parser
);
13963 /* Consume the `{' token. */
13964 matching_braces braces
;
13965 braces
.consume_open (parser
)->location
;
13966 /* Parse the declarations. */
13967 cp_parser_declaration_seq_opt (parser
);
13968 /* Look for the closing `}'. */
13969 braces
.require_close (parser
);
13971 /* Otherwise, there's just one declaration. */
13974 bool saved_in_unbraced_linkage_specification_p
;
13976 saved_in_unbraced_linkage_specification_p
13977 = parser
->in_unbraced_linkage_specification_p
;
13978 parser
->in_unbraced_linkage_specification_p
= true;
13979 cp_parser_declaration (parser
);
13980 parser
->in_unbraced_linkage_specification_p
13981 = saved_in_unbraced_linkage_specification_p
;
13984 /* We're done with the linkage-specification. */
13985 pop_lang_context ();
13987 /* Restore location of parent linkage specification, if any. */
13988 parser
->innermost_linkage_specification_location
= saved_location
;
13991 /* Parse a static_assert-declaration.
13993 static_assert-declaration:
13994 static_assert ( constant-expression , string-literal ) ;
13995 static_assert ( constant-expression ) ; (C++17)
13997 If MEMBER_P, this static_assert is a class member. */
14000 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14003 location_t token_loc
;
14007 /* Peek at the `static_assert' token so we can keep track of exactly
14008 where the static assertion started. */
14009 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14011 /* Look for the `static_assert' keyword. */
14012 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14016 /* We know we are in a static assertion; commit to any tentative
14018 if (cp_parser_parsing_tentatively (parser
))
14019 cp_parser_commit_to_tentative_parse (parser
);
14021 /* Parse the `(' starting the static assertion condition. */
14022 matching_parens parens
;
14023 parens
.require_open (parser
);
14025 /* Parse the constant-expression. Allow a non-constant expression
14026 here in order to give better diagnostics in finish_static_assert. */
14028 cp_parser_constant_expression (parser
,
14029 /*allow_non_constant_p=*/true,
14030 /*non_constant_p=*/&dummy
);
14032 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14034 if (cxx_dialect
< cxx17
)
14035 pedwarn (input_location
, OPT_Wpedantic
,
14036 "static_assert without a message "
14037 "only available with -std=c++17 or -std=gnu++17");
14039 cp_lexer_consume_token (parser
->lexer
);
14040 message
= build_string (1, "");
14041 TREE_TYPE (message
) = char_array_type_node
;
14042 fix_string_type (message
);
14046 /* Parse the separating `,'. */
14047 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14049 /* Parse the string-literal message. */
14050 message
= cp_parser_string_literal (parser
,
14051 /*translate=*/false,
14054 /* A `)' completes the static assertion. */
14055 if (!parens
.require_close (parser
))
14056 cp_parser_skip_to_closing_parenthesis (parser
,
14057 /*recovering=*/true,
14058 /*or_comma=*/false,
14059 /*consume_paren=*/true);
14062 /* A semicolon terminates the declaration. */
14063 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14065 /* Get the location for the static assertion. Use that of the
14066 condition if available, otherwise, use that of the "static_assert"
14068 location_t assert_loc
= condition
.get_location ();
14069 if (assert_loc
== UNKNOWN_LOCATION
)
14070 assert_loc
= token_loc
;
14072 /* Complete the static assertion, which may mean either processing
14073 the static assert now or saving it for template instantiation. */
14074 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14077 /* Parse the expression in decltype ( expression ). */
14080 cp_parser_decltype_expr (cp_parser
*parser
,
14081 bool &id_expression_or_member_access_p
)
14083 cp_token
*id_expr_start_token
;
14086 /* Since we're going to preserve any side-effects from this parse, set up a
14087 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14088 in the expression. */
14089 tentative_firewall
firewall (parser
);
14091 /* First, try parsing an id-expression. */
14092 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14093 cp_parser_parse_tentatively (parser
);
14094 expr
= cp_parser_id_expression (parser
,
14095 /*template_keyword_p=*/false,
14096 /*check_dependency_p=*/true,
14097 /*template_p=*/NULL
,
14098 /*declarator_p=*/false,
14099 /*optional_p=*/false);
14101 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14103 bool non_integral_constant_expression_p
= false;
14104 tree id_expression
= expr
;
14106 const char *error_msg
;
14108 if (identifier_p (expr
))
14109 /* Lookup the name we got back from the id-expression. */
14110 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14111 id_expr_start_token
->location
);
14113 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14114 /* A template without args is not a complete id-expression. */
14115 expr
= error_mark_node
;
14118 && expr
!= error_mark_node
14119 && TREE_CODE (expr
) != TYPE_DECL
14120 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14121 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14122 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14124 /* Complete lookup of the id-expression. */
14125 expr
= (finish_id_expression
14126 (id_expression
, expr
, parser
->scope
, &idk
,
14127 /*integral_constant_expression_p=*/false,
14128 /*allow_non_integral_constant_expression_p=*/true,
14129 &non_integral_constant_expression_p
,
14130 /*template_p=*/false,
14132 /*address_p=*/false,
14133 /*template_arg_p=*/false,
14135 id_expr_start_token
->location
));
14137 if (expr
== error_mark_node
)
14138 /* We found an id-expression, but it was something that we
14139 should not have found. This is an error, not something
14140 we can recover from, so note that we found an
14141 id-expression and we'll recover as gracefully as
14143 id_expression_or_member_access_p
= true;
14147 && expr
!= error_mark_node
14148 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14149 /* We have an id-expression. */
14150 id_expression_or_member_access_p
= true;
14153 if (!id_expression_or_member_access_p
)
14155 /* Abort the id-expression parse. */
14156 cp_parser_abort_tentative_parse (parser
);
14158 /* Parsing tentatively, again. */
14159 cp_parser_parse_tentatively (parser
);
14161 /* Parse a class member access. */
14162 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14163 /*cast_p=*/false, /*decltype*/true,
14164 /*member_access_only_p=*/true, NULL
);
14167 && expr
!= error_mark_node
14168 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14169 /* We have an id-expression. */
14170 id_expression_or_member_access_p
= true;
14173 if (id_expression_or_member_access_p
)
14174 /* We have parsed the complete id-expression or member access. */
14175 cp_parser_parse_definitely (parser
);
14178 /* Abort our attempt to parse an id-expression or member access
14180 cp_parser_abort_tentative_parse (parser
);
14182 /* Commit to the tentative_firewall so we get syntax errors. */
14183 cp_parser_commit_to_tentative_parse (parser
);
14185 /* Parse a full expression. */
14186 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14187 /*decltype_p=*/true);
14193 /* Parse a `decltype' type. Returns the type.
14195 simple-type-specifier:
14196 decltype ( expression )
14198 decltype ( auto ) */
14201 cp_parser_decltype (cp_parser
*parser
)
14203 bool id_expression_or_member_access_p
= false;
14204 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14206 if (start_token
->type
== CPP_DECLTYPE
)
14208 /* Already parsed. */
14209 cp_lexer_consume_token (parser
->lexer
);
14210 return saved_checks_value (start_token
->u
.tree_check_value
);
14213 /* Look for the `decltype' token. */
14214 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14215 return error_mark_node
;
14217 /* Parse the opening `('. */
14218 matching_parens parens
;
14219 if (!parens
.require_open (parser
))
14220 return error_mark_node
;
14222 push_deferring_access_checks (dk_deferred
);
14224 tree expr
= NULL_TREE
;
14226 if (cxx_dialect
>= cxx14
14227 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14228 /* decltype (auto) */
14229 cp_lexer_consume_token (parser
->lexer
);
14232 /* decltype (expression) */
14234 /* Types cannot be defined in a `decltype' expression. Save away the
14235 old message and set the new one. */
14236 const char *saved_message
= parser
->type_definition_forbidden_message
;
14237 parser
->type_definition_forbidden_message
14238 = G_("types may not be defined in %<decltype%> expressions");
14240 /* The restrictions on constant-expressions do not apply inside
14241 decltype expressions. */
14242 bool saved_integral_constant_expression_p
14243 = parser
->integral_constant_expression_p
;
14244 bool saved_non_integral_constant_expression_p
14245 = parser
->non_integral_constant_expression_p
;
14246 parser
->integral_constant_expression_p
= false;
14248 /* Within a parenthesized expression, a `>' token is always
14249 the greater-than operator. */
14250 bool saved_greater_than_is_operator_p
14251 = parser
->greater_than_is_operator_p
;
14252 parser
->greater_than_is_operator_p
= true;
14254 /* Do not actually evaluate the expression. */
14255 ++cp_unevaluated_operand
;
14257 /* Do not warn about problems with the expression. */
14258 ++c_inhibit_evaluation_warnings
;
14260 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14262 /* Go back to evaluating expressions. */
14263 --cp_unevaluated_operand
;
14264 --c_inhibit_evaluation_warnings
;
14266 /* The `>' token might be the end of a template-id or
14267 template-parameter-list now. */
14268 parser
->greater_than_is_operator_p
14269 = saved_greater_than_is_operator_p
;
14271 /* Restore the old message and the integral constant expression
14273 parser
->type_definition_forbidden_message
= saved_message
;
14274 parser
->integral_constant_expression_p
14275 = saved_integral_constant_expression_p
;
14276 parser
->non_integral_constant_expression_p
14277 = saved_non_integral_constant_expression_p
;
14280 /* Parse to the closing `)'. */
14281 if (!parens
.require_close (parser
))
14283 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14284 /*consume_paren=*/true);
14285 pop_deferring_access_checks ();
14286 return error_mark_node
;
14292 expr
= make_decltype_auto ();
14293 AUTO_IS_DECLTYPE (expr
) = true;
14296 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14297 tf_warning_or_error
);
14299 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14301 start_token
->type
= CPP_DECLTYPE
;
14302 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14303 start_token
->u
.tree_check_value
->value
= expr
;
14304 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14305 start_token
->keyword
= RID_MAX
;
14306 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14308 pop_to_parent_deferring_access_checks ();
14313 /* Special member functions [gram.special] */
14315 /* Parse a conversion-function-id.
14317 conversion-function-id:
14318 operator conversion-type-id
14320 Returns an IDENTIFIER_NODE representing the operator. */
14323 cp_parser_conversion_function_id (cp_parser
* parser
)
14327 tree saved_qualifying_scope
;
14328 tree saved_object_scope
;
14329 tree pushed_scope
= NULL_TREE
;
14331 /* Look for the `operator' token. */
14332 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14333 return error_mark_node
;
14334 /* When we parse the conversion-type-id, the current scope will be
14335 reset. However, we need that information in able to look up the
14336 conversion function later, so we save it here. */
14337 saved_scope
= parser
->scope
;
14338 saved_qualifying_scope
= parser
->qualifying_scope
;
14339 saved_object_scope
= parser
->object_scope
;
14340 /* We must enter the scope of the class so that the names of
14341 entities declared within the class are available in the
14342 conversion-type-id. For example, consider:
14349 S::operator I() { ... }
14351 In order to see that `I' is a type-name in the definition, we
14352 must be in the scope of `S'. */
14354 pushed_scope
= push_scope (saved_scope
);
14355 /* Parse the conversion-type-id. */
14356 type
= cp_parser_conversion_type_id (parser
);
14357 /* Leave the scope of the class, if any. */
14359 pop_scope (pushed_scope
);
14360 /* Restore the saved scope. */
14361 parser
->scope
= saved_scope
;
14362 parser
->qualifying_scope
= saved_qualifying_scope
;
14363 parser
->object_scope
= saved_object_scope
;
14364 /* If the TYPE is invalid, indicate failure. */
14365 if (type
== error_mark_node
)
14366 return error_mark_node
;
14367 return make_conv_op_name (type
);
14370 /* Parse a conversion-type-id:
14372 conversion-type-id:
14373 type-specifier-seq conversion-declarator [opt]
14375 Returns the TYPE specified. */
14378 cp_parser_conversion_type_id (cp_parser
* parser
)
14381 cp_decl_specifier_seq type_specifiers
;
14382 cp_declarator
*declarator
;
14383 tree type_specified
;
14384 const char *saved_message
;
14386 /* Parse the attributes. */
14387 attributes
= cp_parser_attributes_opt (parser
);
14389 saved_message
= parser
->type_definition_forbidden_message
;
14390 parser
->type_definition_forbidden_message
14391 = G_("types may not be defined in a conversion-type-id");
14393 /* Parse the type-specifiers. */
14394 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14395 /*is_trailing_return=*/false,
14398 parser
->type_definition_forbidden_message
= saved_message
;
14400 /* If that didn't work, stop. */
14401 if (type_specifiers
.type
== error_mark_node
)
14402 return error_mark_node
;
14403 /* Parse the conversion-declarator. */
14404 declarator
= cp_parser_conversion_declarator_opt (parser
);
14406 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14407 /*initialized=*/0, &attributes
);
14409 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14411 /* Don't give this error when parsing tentatively. This happens to
14412 work because we always parse this definitively once. */
14413 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14414 && type_uses_auto (type_specified
))
14416 if (cxx_dialect
< cxx14
)
14418 error ("invalid use of %<auto%> in conversion operator");
14419 return error_mark_node
;
14421 else if (template_parm_scope_p ())
14422 warning (0, "use of %<auto%> in member template "
14423 "conversion operator can never be deduced");
14426 return type_specified
;
14429 /* Parse an (optional) conversion-declarator.
14431 conversion-declarator:
14432 ptr-operator conversion-declarator [opt]
14436 static cp_declarator
*
14437 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14439 enum tree_code code
;
14440 tree class_type
, std_attributes
= NULL_TREE
;
14441 cp_cv_quals cv_quals
;
14443 /* We don't know if there's a ptr-operator next, or not. */
14444 cp_parser_parse_tentatively (parser
);
14445 /* Try the ptr-operator. */
14446 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14448 /* If it worked, look for more conversion-declarators. */
14449 if (cp_parser_parse_definitely (parser
))
14451 cp_declarator
*declarator
;
14453 /* Parse another optional declarator. */
14454 declarator
= cp_parser_conversion_declarator_opt (parser
);
14456 declarator
= cp_parser_make_indirect_declarator
14457 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14465 /* Parse an (optional) ctor-initializer.
14468 : mem-initializer-list */
14471 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14473 /* If the next token is not a `:', then there is no
14474 ctor-initializer. */
14475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14477 /* Do default initialization of any bases and members. */
14478 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14479 finish_mem_initializers (NULL_TREE
);
14483 /* Consume the `:' token. */
14484 cp_lexer_consume_token (parser
->lexer
);
14485 /* And the mem-initializer-list. */
14486 cp_parser_mem_initializer_list (parser
);
14489 /* Parse a mem-initializer-list.
14491 mem-initializer-list:
14492 mem-initializer ... [opt]
14493 mem-initializer ... [opt] , mem-initializer-list */
14496 cp_parser_mem_initializer_list (cp_parser
* parser
)
14498 tree mem_initializer_list
= NULL_TREE
;
14499 tree target_ctor
= error_mark_node
;
14500 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14502 /* Let the semantic analysis code know that we are starting the
14503 mem-initializer-list. */
14504 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14505 error_at (token
->location
,
14506 "only constructors take member initializers");
14508 /* Loop through the list. */
14511 tree mem_initializer
;
14513 token
= cp_lexer_peek_token (parser
->lexer
);
14514 /* Parse the mem-initializer. */
14515 mem_initializer
= cp_parser_mem_initializer (parser
);
14516 /* If the next token is a `...', we're expanding member initializers. */
14517 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14519 || (mem_initializer
!= error_mark_node
14520 && check_for_bare_parameter_packs (TREE_PURPOSE
14521 (mem_initializer
))))
14523 /* Consume the `...'. */
14525 cp_lexer_consume_token (parser
->lexer
);
14527 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14528 can be expanded but members cannot. */
14529 if (mem_initializer
!= error_mark_node
14530 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14532 error_at (token
->location
,
14533 "cannot expand initializer for member %qD",
14534 TREE_PURPOSE (mem_initializer
));
14535 mem_initializer
= error_mark_node
;
14538 /* Construct the pack expansion type. */
14539 if (mem_initializer
!= error_mark_node
)
14540 mem_initializer
= make_pack_expansion (mem_initializer
);
14542 if (target_ctor
!= error_mark_node
14543 && mem_initializer
!= error_mark_node
)
14545 error ("mem-initializer for %qD follows constructor delegation",
14546 TREE_PURPOSE (mem_initializer
));
14547 mem_initializer
= error_mark_node
;
14549 /* Look for a target constructor. */
14550 if (mem_initializer
!= error_mark_node
14551 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14552 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14554 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14555 if (mem_initializer_list
)
14557 error ("constructor delegation follows mem-initializer for %qD",
14558 TREE_PURPOSE (mem_initializer_list
));
14559 mem_initializer
= error_mark_node
;
14561 target_ctor
= mem_initializer
;
14563 /* Add it to the list, unless it was erroneous. */
14564 if (mem_initializer
!= error_mark_node
)
14566 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14567 mem_initializer_list
= mem_initializer
;
14569 /* If the next token is not a `,', we're done. */
14570 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14572 /* Consume the `,' token. */
14573 cp_lexer_consume_token (parser
->lexer
);
14576 /* Perform semantic analysis. */
14577 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14578 finish_mem_initializers (mem_initializer_list
);
14581 /* Parse a mem-initializer.
14584 mem-initializer-id ( expression-list [opt] )
14585 mem-initializer-id braced-init-list
14590 ( expression-list [opt] )
14592 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14593 class) or FIELD_DECL (for a non-static data member) to initialize;
14594 the TREE_VALUE is the expression-list. An empty initialization
14595 list is represented by void_list_node. */
14598 cp_parser_mem_initializer (cp_parser
* parser
)
14600 tree mem_initializer_id
;
14601 tree expression_list
;
14603 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14605 /* Find out what is being initialized. */
14606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14608 permerror (token
->location
,
14609 "anachronistic old-style base class initializer");
14610 mem_initializer_id
= NULL_TREE
;
14614 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14615 if (mem_initializer_id
== error_mark_node
)
14616 return mem_initializer_id
;
14618 member
= expand_member_init (mem_initializer_id
);
14619 if (member
&& !DECL_P (member
))
14620 in_base_initializer
= 1;
14622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14624 bool expr_non_constant_p
;
14625 cp_lexer_set_source_position (parser
->lexer
);
14626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14627 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14628 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14629 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14633 vec
<tree
, va_gc
> *vec
;
14634 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14636 /*allow_expansion_p=*/true,
14637 /*non_constant_p=*/NULL
);
14639 return error_mark_node
;
14640 expression_list
= build_tree_list_vec (vec
);
14641 release_tree_vector (vec
);
14644 if (expression_list
== error_mark_node
)
14645 return error_mark_node
;
14646 if (!expression_list
)
14647 expression_list
= void_type_node
;
14649 in_base_initializer
= 0;
14651 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14654 /* Parse a mem-initializer-id.
14656 mem-initializer-id:
14657 :: [opt] nested-name-specifier [opt] class-name
14658 decltype-specifier (C++11)
14661 Returns a TYPE indicating the class to be initialized for the first
14662 production (and the second in C++11). Returns an IDENTIFIER_NODE
14663 indicating the data member to be initialized for the last production. */
14666 cp_parser_mem_initializer_id (cp_parser
* parser
)
14668 bool global_scope_p
;
14669 bool nested_name_specifier_p
;
14670 bool template_p
= false;
14673 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14675 /* `typename' is not allowed in this context ([temp.res]). */
14676 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14678 error_at (token
->location
,
14679 "keyword %<typename%> not allowed in this context (a qualified "
14680 "member initializer is implicitly a type)");
14681 cp_lexer_consume_token (parser
->lexer
);
14683 /* Look for the optional `::' operator. */
14685 = (cp_parser_global_scope_opt (parser
,
14686 /*current_scope_valid_p=*/false)
14688 /* Look for the optional nested-name-specifier. The simplest way to
14693 The keyword `typename' is not permitted in a base-specifier or
14694 mem-initializer; in these contexts a qualified name that
14695 depends on a template-parameter is implicitly assumed to be a
14698 is to assume that we have seen the `typename' keyword at this
14700 nested_name_specifier_p
14701 = (cp_parser_nested_name_specifier_opt (parser
,
14702 /*typename_keyword_p=*/true,
14703 /*check_dependency_p=*/true,
14705 /*is_declaration=*/true)
14707 if (nested_name_specifier_p
)
14708 template_p
= cp_parser_optional_template_keyword (parser
);
14709 /* If there is a `::' operator or a nested-name-specifier, then we
14710 are definitely looking for a class-name. */
14711 if (global_scope_p
|| nested_name_specifier_p
)
14712 return cp_parser_class_name (parser
,
14713 /*typename_keyword_p=*/true,
14714 /*template_keyword_p=*/template_p
,
14716 /*check_dependency_p=*/true,
14717 /*class_head_p=*/false,
14718 /*is_declaration=*/true);
14719 /* Otherwise, we could also be looking for an ordinary identifier. */
14720 cp_parser_parse_tentatively (parser
);
14721 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14722 /* Try a decltype-specifier. */
14723 id
= cp_parser_decltype (parser
);
14725 /* Otherwise, try a class-name. */
14726 id
= cp_parser_class_name (parser
,
14727 /*typename_keyword_p=*/true,
14728 /*template_keyword_p=*/false,
14730 /*check_dependency_p=*/true,
14731 /*class_head_p=*/false,
14732 /*is_declaration=*/true);
14733 /* If we found one, we're done. */
14734 if (cp_parser_parse_definitely (parser
))
14736 /* Otherwise, look for an ordinary identifier. */
14737 return cp_parser_identifier (parser
);
14740 /* Overloading [gram.over] */
14742 /* Parse an operator-function-id.
14744 operator-function-id:
14747 Returns an IDENTIFIER_NODE for the operator which is a
14748 human-readable spelling of the identifier, e.g., `operator +'. */
14751 cp_parser_operator_function_id (cp_parser
* parser
)
14753 /* Look for the `operator' keyword. */
14754 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14755 return error_mark_node
;
14756 /* And then the name of the operator itself. */
14757 return cp_parser_operator (parser
);
14760 /* Return an identifier node for a user-defined literal operator.
14761 The suffix identifier is chained to the operator name identifier. */
14764 cp_literal_operator_id (const char* name
)
14767 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14768 + strlen (name
) + 10);
14769 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14770 identifier
= get_identifier (buffer
);
14775 /* Parse an operator.
14778 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14779 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14780 || ++ -- , ->* -> () []
14787 Returns an IDENTIFIER_NODE for the operator which is a
14788 human-readable spelling of the identifier, e.g., `operator +'. */
14791 cp_parser_operator (cp_parser
* parser
)
14793 tree id
= NULL_TREE
;
14797 /* Peek at the next token. */
14798 token
= cp_lexer_peek_token (parser
->lexer
);
14800 location_t start_loc
= token
->location
;
14802 /* Figure out which operator we have. */
14803 enum tree_code op
= ERROR_MARK
;
14804 bool assop
= false;
14805 bool consumed
= false;
14806 switch (token
->type
)
14810 /* The keyword should be either `new' or `delete'. */
14811 if (token
->keyword
== RID_NEW
)
14813 else if (token
->keyword
== RID_DELETE
)
14818 /* Consume the `new' or `delete' token. */
14819 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14821 /* Peek at the next token. */
14822 token
= cp_lexer_peek_token (parser
->lexer
);
14823 /* If it's a `[' token then this is the array variant of the
14825 if (token
->type
== CPP_OPEN_SQUARE
)
14827 /* Consume the `[' token. */
14828 cp_lexer_consume_token (parser
->lexer
);
14829 /* Look for the `]' token. */
14830 if (cp_token
*close_token
14831 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14832 end_loc
= close_token
->location
;
14833 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14835 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14853 op
= TRUNC_DIV_EXPR
;
14857 op
= TRUNC_MOD_EXPR
;
14877 op
= TRUTH_NOT_EXPR
;
14910 op
= TRUNC_DIV_EXPR
;
14915 op
= TRUNC_MOD_EXPR
;
14941 case CPP_LSHIFT_EQ
:
14946 case CPP_RSHIFT_EQ
:
14963 case CPP_GREATER_EQ
:
14968 op
= TRUTH_ANDIF_EXPR
;
14972 op
= TRUTH_ORIF_EXPR
;
14975 case CPP_PLUS_PLUS
:
14976 op
= POSTINCREMENT_EXPR
;
14979 case CPP_MINUS_MINUS
:
14980 op
= PREDECREMENT_EXPR
;
14984 op
= COMPOUND_EXPR
;
14987 case CPP_DEREF_STAR
:
14992 op
= COMPONENT_REF
;
14995 case CPP_OPEN_PAREN
:
14997 /* Consume the `('. */
14998 matching_parens parens
;
14999 parens
.consume_open (parser
);
15000 /* Look for the matching `)'. */
15001 parens
.require_close (parser
);
15007 case CPP_OPEN_SQUARE
:
15008 /* Consume the `['. */
15009 cp_lexer_consume_token (parser
->lexer
);
15010 /* Look for the matching `]'. */
15011 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15016 case CPP_UTF8STRING
:
15017 case CPP_UTF8STRING_USERDEF
:
15024 case CPP_STRING_USERDEF
:
15025 case CPP_WSTRING_USERDEF
:
15026 case CPP_STRING16_USERDEF
:
15027 case CPP_STRING32_USERDEF
:
15029 tree str
, string_tree
;
15032 if (cxx_dialect
== cxx98
)
15033 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15035 /* Consume the string. */
15036 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15037 /*wide_ok=*/true, /*lookup_udlit=*/false);
15038 if (str
== error_mark_node
)
15039 return error_mark_node
;
15040 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15042 string_tree
= USERDEF_LITERAL_VALUE (str
);
15043 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15048 /* Look for the suffix identifier. */
15049 token
= cp_lexer_peek_token (parser
->lexer
);
15050 if (token
->type
== CPP_NAME
)
15051 id
= cp_parser_identifier (parser
);
15052 else if (token
->type
== CPP_KEYWORD
)
15054 error ("unexpected keyword;"
15055 " remove space between quotes and suffix identifier");
15056 return error_mark_node
;
15060 error ("expected suffix identifier");
15061 return error_mark_node
;
15064 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15065 (TREE_TYPE (TREE_TYPE (string_tree
))));
15066 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15069 error ("expected empty string after %<operator%> keyword");
15070 return error_mark_node
;
15072 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15075 error ("invalid encoding prefix in literal operator");
15076 return error_mark_node
;
15078 if (id
!= error_mark_node
)
15080 const char *name
= IDENTIFIER_POINTER (id
);
15081 id
= cp_literal_operator_id (name
);
15087 /* Anything else is an error. */
15091 /* If we have selected an identifier, we need to consume the
15093 if (op
!= ERROR_MARK
)
15095 id
= ovl_op_identifier (assop
, op
);
15097 cp_lexer_consume_token (parser
->lexer
);
15099 /* Otherwise, no valid operator name was present. */
15102 cp_parser_error (parser
, "expected operator");
15103 id
= error_mark_node
;
15106 return cp_expr (id
, start_loc
);
15109 /* Parse a template-declaration.
15111 template-declaration:
15112 export [opt] template < template-parameter-list > declaration
15114 If MEMBER_P is TRUE, this template-declaration occurs within a
15117 The grammar rule given by the standard isn't correct. What
15118 is really meant is:
15120 template-declaration:
15121 export [opt] template-parameter-list-seq
15122 decl-specifier-seq [opt] init-declarator [opt] ;
15123 export [opt] template-parameter-list-seq
15124 function-definition
15126 template-parameter-list-seq:
15127 template-parameter-list-seq [opt]
15128 template < template-parameter-list >
15130 Concept Extensions:
15132 template-parameter-list-seq:
15133 template < template-parameter-list > requires-clause [opt]
15136 requires logical-or-expression */
15139 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15141 /* Check for `export'. */
15142 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15144 /* Consume the `export' token. */
15145 cp_lexer_consume_token (parser
->lexer
);
15146 /* Warn that we do not support `export'. */
15147 warning (0, "keyword %<export%> not implemented, and will be ignored");
15150 cp_parser_template_declaration_after_export (parser
, member_p
);
15153 /* Parse a template-parameter-list.
15155 template-parameter-list:
15157 template-parameter-list , template-parameter
15159 Returns a TREE_LIST. Each node represents a template parameter.
15160 The nodes are connected via their TREE_CHAINs. */
15163 cp_parser_template_parameter_list (cp_parser
* parser
)
15165 tree parameter_list
= NULL_TREE
;
15167 begin_template_parm_list ();
15169 /* The loop below parses the template parms. We first need to know
15170 the total number of template parms to be able to compute proper
15171 canonical types of each dependent type. So after the loop, when
15172 we know the total number of template parms,
15173 end_template_parm_list computes the proper canonical types and
15174 fixes up the dependent types accordingly. */
15179 bool is_parameter_pack
;
15180 location_t parm_loc
;
15182 /* Parse the template-parameter. */
15183 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15184 parameter
= cp_parser_template_parameter (parser
,
15186 &is_parameter_pack
);
15187 /* Add it to the list. */
15188 if (parameter
!= error_mark_node
)
15189 parameter_list
= process_template_parm (parameter_list
,
15193 is_parameter_pack
);
15196 tree err_parm
= build_tree_list (parameter
, parameter
);
15197 parameter_list
= chainon (parameter_list
, err_parm
);
15200 /* If the next token is not a `,', we're done. */
15201 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15203 /* Otherwise, consume the `,' token. */
15204 cp_lexer_consume_token (parser
->lexer
);
15207 return end_template_parm_list (parameter_list
);
15210 /* Parse a introduction-list.
15213 introduced-parameter
15214 introduction-list , introduced-parameter
15216 introduced-parameter:
15217 ...[opt] identifier
15219 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15220 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15221 WILDCARD_DECL will also have DECL_NAME set and token location in
15222 DECL_SOURCE_LOCATION. */
15225 cp_parser_introduction_list (cp_parser
*parser
)
15227 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15231 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15233 cp_lexer_consume_token (parser
->lexer
);
15235 tree identifier
= cp_parser_identifier (parser
);
15236 if (identifier
== error_mark_node
)
15239 /* Build placeholder. */
15240 tree parm
= build_nt (WILDCARD_DECL
);
15241 DECL_SOURCE_LOCATION (parm
)
15242 = cp_lexer_peek_token (parser
->lexer
)->location
;
15243 DECL_NAME (parm
) = identifier
;
15244 WILDCARD_PACK_P (parm
) = is_pack
;
15245 vec_safe_push (introduction_vec
, parm
);
15247 /* If the next token is not a `,', we're done. */
15248 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15250 /* Otherwise, consume the `,' token. */
15251 cp_lexer_consume_token (parser
->lexer
);
15254 /* Convert the vec into a TREE_VEC. */
15255 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15258 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15259 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15261 release_tree_vector (introduction_vec
);
15262 return introduction_list
;
15265 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15266 is an abstract declarator. */
15268 static inline cp_declarator
*
15269 get_id_declarator (cp_declarator
*declarator
)
15271 cp_declarator
*d
= declarator
;
15272 while (d
&& d
->kind
!= cdk_id
)
15277 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15278 is an abstract declarator. */
15281 get_unqualified_id (cp_declarator
*declarator
)
15283 declarator
= get_id_declarator (declarator
);
15285 return declarator
->u
.id
.unqualified_name
;
15290 /* Returns true if DECL represents a constrained-parameter. */
15293 is_constrained_parameter (tree decl
)
15296 && TREE_CODE (decl
) == TYPE_DECL
15297 && CONSTRAINED_PARM_CONCEPT (decl
)
15298 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15301 /* Returns true if PARM declares a constrained-parameter. */
15304 is_constrained_parameter (cp_parameter_declarator
*parm
)
15306 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15309 /* Check that the type parameter is only a declarator-id, and that its
15310 type is not cv-qualified. */
15313 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15314 cp_parameter_declarator
*parm
)
15316 if (!parm
->declarator
)
15319 if (parm
->declarator
->kind
!= cdk_id
)
15321 cp_parser_error (parser
, "invalid constrained type parameter");
15325 /* Don't allow cv-qualified type parameters. */
15326 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15327 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15329 cp_parser_error (parser
, "cv-qualified type parameter");
15336 /* Finish parsing/processing a template type parameter and checking
15337 various restrictions. */
15340 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15342 cp_parameter_declarator
* parmdecl
)
15344 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15345 return finish_template_type_parm (class_type_node
, id
);
15347 return error_mark_node
;
15351 finish_constrained_template_template_parm (tree proto
, tree id
)
15353 /* FIXME: This should probably be copied, and we may need to adjust
15354 the template parameter depths. */
15355 tree saved_parms
= current_template_parms
;
15356 begin_template_parm_list ();
15357 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15358 end_template_parm_list ();
15360 tree parm
= finish_template_template_parm (class_type_node
, id
);
15361 current_template_parms
= saved_parms
;
15366 /* Finish parsing/processing a template template parameter by borrowing
15367 the template parameter list from the prototype parameter. */
15370 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15373 cp_parameter_declarator
*parmdecl
)
15375 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15376 return error_mark_node
;
15377 return finish_constrained_template_template_parm (proto
, id
);
15380 /* Create a new non-type template parameter from the given PARM
15384 constrained_non_type_template_parm (bool *is_non_type
,
15385 cp_parameter_declarator
*parm
)
15387 *is_non_type
= true;
15388 cp_declarator
*decl
= parm
->declarator
;
15389 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15390 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15391 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15394 /* Build a constrained template parameter based on the PARMDECL
15395 declarator. The type of PARMDECL is the constrained type, which
15396 refers to the prototype template parameter that ultimately
15397 specifies the type of the declared parameter. */
15400 finish_constrained_parameter (cp_parser
*parser
,
15401 cp_parameter_declarator
*parmdecl
,
15403 bool *is_parameter_pack
)
15405 tree decl
= parmdecl
->decl_specifiers
.type
;
15406 tree id
= get_unqualified_id (parmdecl
->declarator
);
15407 tree def
= parmdecl
->default_argument
;
15408 tree proto
= DECL_INITIAL (decl
);
15410 /* A template parameter constrained by a variadic concept shall also
15411 be declared as a template parameter pack. */
15412 bool is_variadic
= template_parameter_pack_p (proto
);
15413 if (is_variadic
&& !*is_parameter_pack
)
15414 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15416 /* Build the parameter. Return an error if the declarator was invalid. */
15418 if (TREE_CODE (proto
) == TYPE_DECL
)
15419 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15420 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15421 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15424 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15425 if (parm
== error_mark_node
)
15426 return error_mark_node
;
15428 /* Finish the parameter decl and create a node attaching the
15429 default argument and constraint. */
15430 parm
= build_tree_list (def
, parm
);
15431 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15436 /* Returns true if the parsed type actually represents the declaration
15437 of a type template-parameter. */
15440 declares_constrained_type_template_parameter (tree type
)
15442 return (is_constrained_parameter (type
)
15443 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15447 /* Returns true if the parsed type actually represents the declaration of
15448 a template template-parameter. */
15451 declares_constrained_template_template_parameter (tree type
)
15453 return (is_constrained_parameter (type
)
15454 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15457 /* Parse a default argument for a type template-parameter.
15458 Note that diagnostics are handled in cp_parser_template_parameter. */
15461 cp_parser_default_type_template_argument (cp_parser
*parser
)
15463 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15465 /* Consume the `=' token. */
15466 cp_lexer_consume_token (parser
->lexer
);
15468 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15470 /* Parse the default-argument. */
15471 push_deferring_access_checks (dk_no_deferred
);
15472 tree default_argument
= cp_parser_type_id (parser
);
15473 pop_deferring_access_checks ();
15475 if (flag_concepts
&& type_uses_auto (default_argument
))
15477 error_at (token
->location
,
15478 "invalid use of %<auto%> in default template argument");
15479 return error_mark_node
;
15482 return default_argument
;
15485 /* Parse a default argument for a template template-parameter. */
15488 cp_parser_default_template_template_argument (cp_parser
*parser
)
15490 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15494 /* Consume the `='. */
15495 cp_lexer_consume_token (parser
->lexer
);
15496 /* Parse the id-expression. */
15497 push_deferring_access_checks (dk_no_deferred
);
15498 /* save token before parsing the id-expression, for error
15500 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15501 tree default_argument
15502 = cp_parser_id_expression (parser
,
15503 /*template_keyword_p=*/false,
15504 /*check_dependency_p=*/true,
15505 /*template_p=*/&is_template
,
15506 /*declarator_p=*/false,
15507 /*optional_p=*/false);
15508 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15509 /* If the id-expression was a template-id that refers to
15510 a template-class, we already have the declaration here,
15511 so no further lookup is needed. */
15514 /* Look up the name. */
15516 = cp_parser_lookup_name (parser
, default_argument
,
15518 /*is_template=*/is_template
,
15519 /*is_namespace=*/false,
15520 /*check_dependency=*/true,
15521 /*ambiguous_decls=*/NULL
,
15523 /* See if the default argument is valid. */
15524 default_argument
= check_template_template_default_arg (default_argument
);
15525 pop_deferring_access_checks ();
15526 return default_argument
;
15529 /* Parse a template-parameter.
15531 template-parameter:
15533 parameter-declaration
15535 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15536 the parameter. The TREE_PURPOSE is the default value, if any.
15537 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15538 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15539 set to true iff this parameter is a parameter pack. */
15542 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15543 bool *is_parameter_pack
)
15546 cp_parameter_declarator
*parameter_declarator
;
15549 /* Assume it is a type parameter or a template parameter. */
15550 *is_non_type
= false;
15551 /* Assume it not a parameter pack. */
15552 *is_parameter_pack
= false;
15553 /* Peek at the next token. */
15554 token
= cp_lexer_peek_token (parser
->lexer
);
15555 /* If it is `template', we have a type-parameter. */
15556 if (token
->keyword
== RID_TEMPLATE
)
15557 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15558 /* If it is `class' or `typename' we do not know yet whether it is a
15559 type parameter or a non-type parameter. Consider:
15561 template <typename T, typename T::X X> ...
15565 template <class C, class D*> ...
15567 Here, the first parameter is a type parameter, and the second is
15568 a non-type parameter. We can tell by looking at the token after
15569 the identifier -- if it is a `,', `=', or `>' then we have a type
15571 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15573 /* Peek at the token after `class' or `typename'. */
15574 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15575 /* If it's an ellipsis, we have a template type parameter
15577 if (token
->type
== CPP_ELLIPSIS
)
15578 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15579 /* If it's an identifier, skip it. */
15580 if (token
->type
== CPP_NAME
)
15581 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15582 /* Now, see if the token looks like the end of a template
15584 if (token
->type
== CPP_COMMA
15585 || token
->type
== CPP_EQ
15586 || token
->type
== CPP_GREATER
)
15587 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15590 /* Otherwise, it is a non-type parameter or a constrained parameter.
15594 When parsing a default template-argument for a non-type
15595 template-parameter, the first non-nested `>' is taken as the end
15596 of the template parameter-list rather than a greater-than
15598 parameter_declarator
15599 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15600 /*parenthesized_p=*/NULL
);
15602 if (!parameter_declarator
)
15603 return error_mark_node
;
15605 /* If the parameter declaration is marked as a parameter pack, set
15606 *IS_PARAMETER_PACK to notify the caller. */
15607 if (parameter_declarator
->template_parameter_pack_p
)
15608 *is_parameter_pack
= true;
15610 if (parameter_declarator
->default_argument
)
15612 /* Can happen in some cases of erroneous input (c++/34892). */
15613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15614 /* Consume the `...' for better error recovery. */
15615 cp_lexer_consume_token (parser
->lexer
);
15618 // The parameter may have been constrained.
15619 if (is_constrained_parameter (parameter_declarator
))
15620 return finish_constrained_parameter (parser
,
15621 parameter_declarator
,
15623 is_parameter_pack
);
15625 // Now we're sure that the parameter is a non-type parameter.
15626 *is_non_type
= true;
15628 parm
= grokdeclarator (parameter_declarator
->declarator
,
15629 ¶meter_declarator
->decl_specifiers
,
15630 TPARM
, /*initialized=*/0,
15631 /*attrlist=*/NULL
);
15632 if (parm
== error_mark_node
)
15633 return error_mark_node
;
15635 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15638 /* Parse a type-parameter.
15641 class identifier [opt]
15642 class identifier [opt] = type-id
15643 typename identifier [opt]
15644 typename identifier [opt] = type-id
15645 template < template-parameter-list > class identifier [opt]
15646 template < template-parameter-list > class identifier [opt]
15649 GNU Extension (variadic templates):
15652 class ... identifier [opt]
15653 typename ... identifier [opt]
15655 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15656 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15657 the declaration of the parameter.
15659 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15662 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15667 /* Look for a keyword to tell us what kind of parameter this is. */
15668 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15670 return error_mark_node
;
15672 switch (token
->keyword
)
15678 tree default_argument
;
15680 /* If the next token is an ellipsis, we have a template
15682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15684 /* Consume the `...' token. */
15685 cp_lexer_consume_token (parser
->lexer
);
15686 maybe_warn_variadic_templates ();
15688 *is_parameter_pack
= true;
15691 /* If the next token is an identifier, then it names the
15693 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15694 identifier
= cp_parser_identifier (parser
);
15696 identifier
= NULL_TREE
;
15698 /* Create the parameter. */
15699 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15701 /* If the next token is an `=', we have a default argument. */
15702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15705 = cp_parser_default_type_template_argument (parser
);
15707 /* Template parameter packs cannot have default
15709 if (*is_parameter_pack
)
15712 error_at (token
->location
,
15713 "template parameter pack %qD cannot have a "
15714 "default argument", identifier
);
15716 error_at (token
->location
,
15717 "template parameter packs cannot have "
15718 "default arguments");
15719 default_argument
= NULL_TREE
;
15721 else if (check_for_bare_parameter_packs (default_argument
))
15722 default_argument
= error_mark_node
;
15725 default_argument
= NULL_TREE
;
15727 /* Create the combined representation of the parameter and the
15728 default argument. */
15729 parameter
= build_tree_list (default_argument
, parameter
);
15736 tree default_argument
;
15738 /* Look for the `<'. */
15739 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15740 /* Parse the template-parameter-list. */
15741 cp_parser_template_parameter_list (parser
);
15742 /* Look for the `>'. */
15743 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15745 // If template requirements are present, parse them.
15748 tree reqs
= get_shorthand_constraints (current_template_parms
);
15749 if (tree r
= cp_parser_requires_clause_opt (parser
))
15750 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15751 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15754 /* Look for the `class' or 'typename' keywords. */
15755 cp_parser_type_parameter_key (parser
);
15756 /* If the next token is an ellipsis, we have a template
15758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15760 /* Consume the `...' token. */
15761 cp_lexer_consume_token (parser
->lexer
);
15762 maybe_warn_variadic_templates ();
15764 *is_parameter_pack
= true;
15766 /* If the next token is an `=', then there is a
15767 default-argument. If the next token is a `>', we are at
15768 the end of the parameter-list. If the next token is a `,',
15769 then we are at the end of this parameter. */
15770 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15771 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15772 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15774 identifier
= cp_parser_identifier (parser
);
15775 /* Treat invalid names as if the parameter were nameless. */
15776 if (identifier
== error_mark_node
)
15777 identifier
= NULL_TREE
;
15780 identifier
= NULL_TREE
;
15782 /* Create the template parameter. */
15783 parameter
= finish_template_template_parm (class_type_node
,
15786 /* If the next token is an `=', then there is a
15787 default-argument. */
15788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15791 = cp_parser_default_template_template_argument (parser
);
15793 /* Template parameter packs cannot have default
15795 if (*is_parameter_pack
)
15798 error_at (token
->location
,
15799 "template parameter pack %qD cannot "
15800 "have a default argument",
15803 error_at (token
->location
, "template parameter packs cannot "
15804 "have default arguments");
15805 default_argument
= NULL_TREE
;
15809 default_argument
= NULL_TREE
;
15811 /* Create the combined representation of the parameter and the
15812 default argument. */
15813 parameter
= build_tree_list (default_argument
, parameter
);
15818 gcc_unreachable ();
15825 /* Parse a template-id.
15828 template-name < template-argument-list [opt] >
15830 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15831 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15832 returned. Otherwise, if the template-name names a function, or set
15833 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15834 names a class, returns a TYPE_DECL for the specialization.
15836 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15837 uninstantiated templates. */
15840 cp_parser_template_id (cp_parser
*parser
,
15841 bool template_keyword_p
,
15842 bool check_dependency_p
,
15843 enum tag_types tag_type
,
15844 bool is_declaration
)
15849 cp_token_position start_of_id
= 0;
15850 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15851 bool is_identifier
;
15853 /* If the next token corresponds to a template-id, there is no need
15855 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15856 if (token
->type
== CPP_TEMPLATE_ID
)
15858 cp_lexer_consume_token (parser
->lexer
);
15859 return saved_checks_value (token
->u
.tree_check_value
);
15862 /* Avoid performing name lookup if there is no possibility of
15863 finding a template-id. */
15864 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15865 || (token
->type
== CPP_NAME
15866 && !cp_parser_nth_token_starts_template_argument_list_p
15869 cp_parser_error (parser
, "expected template-id");
15870 return error_mark_node
;
15873 /* Remember where the template-id starts. */
15874 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15875 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15877 push_deferring_access_checks (dk_deferred
);
15879 /* Parse the template-name. */
15880 is_identifier
= false;
15881 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15882 check_dependency_p
,
15886 if (templ
== error_mark_node
|| is_identifier
)
15888 pop_deferring_access_checks ();
15892 /* Since we're going to preserve any side-effects from this parse, set up a
15893 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15894 in the template arguments. */
15895 tentative_firewall
firewall (parser
);
15897 /* If we find the sequence `[:' after a template-name, it's probably
15898 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15899 parse correctly the argument list. */
15900 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15901 == CPP_OPEN_SQUARE
)
15902 && next_token
->flags
& DIGRAPH
15903 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15905 && !(next_token_2
->flags
& PREV_WHITE
))
15907 cp_parser_parse_tentatively (parser
);
15908 /* Change `:' into `::'. */
15909 next_token_2
->type
= CPP_SCOPE
;
15910 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15912 cp_lexer_consume_token (parser
->lexer
);
15914 /* Parse the arguments. */
15915 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15916 if (!cp_parser_parse_definitely (parser
))
15918 /* If we couldn't parse an argument list, then we revert our changes
15919 and return simply an error. Maybe this is not a template-id
15921 next_token_2
->type
= CPP_COLON
;
15922 cp_parser_error (parser
, "expected %<<%>");
15923 pop_deferring_access_checks ();
15924 return error_mark_node
;
15926 /* Otherwise, emit an error about the invalid digraph, but continue
15927 parsing because we got our argument list. */
15928 if (permerror (next_token
->location
,
15929 "%<<::%> cannot begin a template-argument list"))
15931 static bool hint
= false;
15932 inform (next_token
->location
,
15933 "%<<:%> is an alternate spelling for %<[%>."
15934 " Insert whitespace between %<<%> and %<::%>");
15935 if (!hint
&& !flag_permissive
)
15937 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15938 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15939 "accept your code)");
15946 /* Look for the `<' that starts the template-argument-list. */
15947 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15949 pop_deferring_access_checks ();
15950 return error_mark_node
;
15952 /* Parse the arguments. */
15953 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15956 /* Set the location to be of the form:
15957 template-name < template-argument-list [opt] >
15958 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15959 with caret == start at the start of the template-name,
15960 ranging until the closing '>'. */
15961 location_t finish_loc
15962 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15963 location_t combined_loc
15964 = make_location (token
->location
, token
->location
, finish_loc
);
15966 /* Check for concepts autos where they don't belong. We could
15967 identify types in some cases of idnetifier TEMPL, looking ahead
15968 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15969 types. We reject them in functions, but if what we have is an
15970 identifier, even with none_type we can't conclude it's NOT a
15971 type, we have to wait for template substitution. */
15972 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15973 template_id
= error_mark_node
;
15974 /* Build a representation of the specialization. */
15975 else if (identifier_p (templ
))
15976 template_id
= build_min_nt_loc (combined_loc
,
15979 else if (DECL_TYPE_TEMPLATE_P (templ
)
15980 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15982 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15983 template (rather than some instantiation thereof) only if
15984 is not nested within some other construct. For example, in
15985 "template <typename T> void f(T) { A<T>::", A<T> is just an
15986 instantiation of A. */
15987 bool entering_scope
15988 = (template_parm_scope_p ()
15989 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
15991 = finish_template_type (templ
, arguments
, entering_scope
);
15993 /* A template-like identifier may be a partial concept id. */
15994 else if (flag_concepts
15995 && (template_id
= (cp_parser_maybe_partial_concept_id
15996 (parser
, templ
, arguments
))))
15997 return template_id
;
15998 else if (variable_template_p (templ
))
16000 template_id
= lookup_template_variable (templ
, arguments
);
16001 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16002 SET_EXPR_LOCATION (template_id
, combined_loc
);
16006 /* If it's not a class-template or a template-template, it should be
16007 a function-template. */
16008 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16009 || TREE_CODE (templ
) == OVERLOAD
16010 || BASELINK_P (templ
)));
16012 template_id
= lookup_template_function (templ
, arguments
);
16013 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16014 SET_EXPR_LOCATION (template_id
, combined_loc
);
16017 /* If parsing tentatively, replace the sequence of tokens that makes
16018 up the template-id with a CPP_TEMPLATE_ID token. That way,
16019 should we re-parse the token stream, we will not have to repeat
16020 the effort required to do the parse, nor will we issue duplicate
16021 error messages about problems during instantiation of the
16024 /* Don't do this if we had a parse error in a declarator; re-parsing
16025 might succeed if a name changes meaning (60361). */
16026 && !(cp_parser_error_occurred (parser
)
16027 && cp_parser_parsing_tentatively (parser
)
16028 && parser
->in_declarator_p
))
16030 /* Reset the contents of the START_OF_ID token. */
16031 token
->type
= CPP_TEMPLATE_ID
;
16032 token
->location
= combined_loc
;
16034 /* Retrieve any deferred checks. Do not pop this access checks yet
16035 so the memory will not be reclaimed during token replacing below. */
16036 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16037 token
->u
.tree_check_value
->value
= template_id
;
16038 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16039 token
->keyword
= RID_MAX
;
16041 /* Purge all subsequent tokens. */
16042 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16044 /* ??? Can we actually assume that, if template_id ==
16045 error_mark_node, we will have issued a diagnostic to the
16046 user, as opposed to simply marking the tentative parse as
16048 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16049 error_at (token
->location
, "parse error in template argument list");
16052 pop_to_parent_deferring_access_checks ();
16053 return template_id
;
16056 /* Parse a template-name.
16061 The standard should actually say:
16065 operator-function-id
16067 A defect report has been filed about this issue.
16069 A conversion-function-id cannot be a template name because they cannot
16070 be part of a template-id. In fact, looking at this code:
16072 a.operator K<int>()
16074 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16075 It is impossible to call a templated conversion-function-id with an
16076 explicit argument list, since the only allowed template parameter is
16077 the type to which it is converting.
16079 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16080 `template' keyword, in a construction like:
16084 In that case `f' is taken to be a template-name, even though there
16085 is no way of knowing for sure.
16087 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16088 name refers to a set of overloaded functions, at least one of which
16089 is a template, or an IDENTIFIER_NODE with the name of the template,
16090 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16091 names are looked up inside uninstantiated templates. */
16094 cp_parser_template_name (cp_parser
* parser
,
16095 bool template_keyword_p
,
16096 bool check_dependency_p
,
16097 bool is_declaration
,
16098 enum tag_types tag_type
,
16099 bool *is_identifier
)
16103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16105 /* If the next token is `operator', then we have either an
16106 operator-function-id or a conversion-function-id. */
16107 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16109 /* We don't know whether we're looking at an
16110 operator-function-id or a conversion-function-id. */
16111 cp_parser_parse_tentatively (parser
);
16112 /* Try an operator-function-id. */
16113 identifier
= cp_parser_operator_function_id (parser
);
16114 /* If that didn't work, try a conversion-function-id. */
16115 if (!cp_parser_parse_definitely (parser
))
16117 cp_parser_error (parser
, "expected template-name");
16118 return error_mark_node
;
16121 /* Look for the identifier. */
16123 identifier
= cp_parser_identifier (parser
);
16125 /* If we didn't find an identifier, we don't have a template-id. */
16126 if (identifier
== error_mark_node
)
16127 return error_mark_node
;
16129 /* If the name immediately followed the `template' keyword, then it
16130 is a template-name. However, if the next token is not `<', then
16131 we do not treat it as a template-name, since it is not being used
16132 as part of a template-id. This enables us to handle constructs
16135 template <typename T> struct S { S(); };
16136 template <typename T> S<T>::S();
16138 correctly. We would treat `S' as a template -- if it were `S<T>'
16139 -- but we do not if there is no `<'. */
16141 if (processing_template_decl
16142 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16144 /* In a declaration, in a dependent context, we pretend that the
16145 "template" keyword was present in order to improve error
16146 recovery. For example, given:
16148 template <typename T> void f(T::X<int>);
16150 we want to treat "X<int>" as a template-id. */
16152 && !template_keyword_p
16153 && parser
->scope
&& TYPE_P (parser
->scope
)
16154 && check_dependency_p
16155 && dependent_scope_p (parser
->scope
)
16156 /* Do not do this for dtors (or ctors), since they never
16157 need the template keyword before their name. */
16158 && !constructor_name_p (identifier
, parser
->scope
))
16160 cp_token_position start
= 0;
16162 /* Explain what went wrong. */
16163 error_at (token
->location
, "non-template %qD used as template",
16165 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16166 parser
->scope
, identifier
);
16167 /* If parsing tentatively, find the location of the "<" token. */
16168 if (cp_parser_simulate_error (parser
))
16169 start
= cp_lexer_token_position (parser
->lexer
, true);
16170 /* Parse the template arguments so that we can issue error
16171 messages about them. */
16172 cp_lexer_consume_token (parser
->lexer
);
16173 cp_parser_enclosed_template_argument_list (parser
);
16174 /* Skip tokens until we find a good place from which to
16175 continue parsing. */
16176 cp_parser_skip_to_closing_parenthesis (parser
,
16177 /*recovering=*/true,
16179 /*consume_paren=*/false);
16180 /* If parsing tentatively, permanently remove the
16181 template argument list. That will prevent duplicate
16182 error messages from being issued about the missing
16183 "template" keyword. */
16185 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16187 *is_identifier
= true;
16188 parser
->context
->object_type
= NULL_TREE
;
16192 /* If the "template" keyword is present, then there is generally
16193 no point in doing name-lookup, so we just return IDENTIFIER.
16194 But, if the qualifying scope is non-dependent then we can
16195 (and must) do name-lookup normally. */
16196 if (template_keyword_p
)
16198 tree scope
= (parser
->scope
? parser
->scope
16199 : parser
->context
->object_type
);
16200 if (scope
&& TYPE_P (scope
)
16201 && (!CLASS_TYPE_P (scope
)
16202 || (check_dependency_p
&& dependent_type_p (scope
))))
16204 /* We're optimizing away the call to cp_parser_lookup_name, but
16205 we still need to do this. */
16206 parser
->context
->object_type
= NULL_TREE
;
16212 /* Look up the name. */
16213 decl
= cp_parser_lookup_name (parser
, identifier
,
16215 /*is_template=*/true,
16216 /*is_namespace=*/false,
16217 check_dependency_p
,
16218 /*ambiguous_decls=*/NULL
,
16221 decl
= strip_using_decl (decl
);
16223 /* If DECL is a template, then the name was a template-name. */
16224 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16226 if (TREE_DEPRECATED (decl
)
16227 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16228 warn_deprecated_use (decl
, NULL_TREE
);
16232 /* The standard does not explicitly indicate whether a name that
16233 names a set of overloaded declarations, some of which are
16234 templates, is a template-name. However, such a name should
16235 be a template-name; otherwise, there is no way to form a
16236 template-id for the overloaded templates. */
16237 bool found
= false;
16239 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16240 !found
&& iter
; ++iter
)
16241 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16246 /* The name does not name a template. */
16247 cp_parser_error (parser
, "expected template-name");
16248 return error_mark_node
;
16252 /* If DECL is dependent, and refers to a function, then just return
16253 its name; we will look it up again during template instantiation. */
16254 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16256 tree scope
= ovl_scope (decl
);
16257 if (TYPE_P (scope
) && dependent_type_p (scope
))
16264 /* Parse a template-argument-list.
16266 template-argument-list:
16267 template-argument ... [opt]
16268 template-argument-list , template-argument ... [opt]
16270 Returns a TREE_VEC containing the arguments. */
16273 cp_parser_template_argument_list (cp_parser
* parser
)
16275 tree fixed_args
[10];
16276 unsigned n_args
= 0;
16277 unsigned alloced
= 10;
16278 tree
*arg_ary
= fixed_args
;
16280 bool saved_in_template_argument_list_p
;
16282 bool saved_non_ice_p
;
16284 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16285 parser
->in_template_argument_list_p
= true;
16286 /* Even if the template-id appears in an integral
16287 constant-expression, the contents of the argument list do
16289 saved_ice_p
= parser
->integral_constant_expression_p
;
16290 parser
->integral_constant_expression_p
= false;
16291 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16292 parser
->non_integral_constant_expression_p
= false;
16294 /* Parse the arguments. */
16300 /* Consume the comma. */
16301 cp_lexer_consume_token (parser
->lexer
);
16303 /* Parse the template-argument. */
16304 argument
= cp_parser_template_argument (parser
);
16306 /* If the next token is an ellipsis, we're expanding a template
16308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16310 if (argument
== error_mark_node
)
16312 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16313 error_at (token
->location
,
16314 "expected parameter pack before %<...%>");
16316 /* Consume the `...' token. */
16317 cp_lexer_consume_token (parser
->lexer
);
16319 /* Make the argument into a TYPE_PACK_EXPANSION or
16320 EXPR_PACK_EXPANSION. */
16321 argument
= make_pack_expansion (argument
);
16324 if (n_args
== alloced
)
16328 if (arg_ary
== fixed_args
)
16330 arg_ary
= XNEWVEC (tree
, alloced
);
16331 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16334 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16336 arg_ary
[n_args
++] = argument
;
16338 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16340 vec
= make_tree_vec (n_args
);
16343 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16345 if (arg_ary
!= fixed_args
)
16347 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16348 parser
->integral_constant_expression_p
= saved_ice_p
;
16349 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16351 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16355 /* Parse a template-argument.
16358 assignment-expression
16362 The representation is that of an assignment-expression, type-id, or
16363 id-expression -- except that the qualified id-expression is
16364 evaluated, so that the value returned is either a DECL or an
16367 Although the standard says "assignment-expression", it forbids
16368 throw-expressions or assignments in the template argument.
16369 Therefore, we use "conditional-expression" instead. */
16372 cp_parser_template_argument (cp_parser
* parser
)
16377 bool maybe_type_id
= false;
16378 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16379 location_t loc
= 0;
16382 /* There's really no way to know what we're looking at, so we just
16383 try each alternative in order.
16387 In a template-argument, an ambiguity between a type-id and an
16388 expression is resolved to a type-id, regardless of the form of
16389 the corresponding template-parameter.
16391 Therefore, we try a type-id first. */
16392 cp_parser_parse_tentatively (parser
);
16393 argument
= cp_parser_template_type_arg (parser
);
16394 /* If there was no error parsing the type-id but the next token is a
16395 '>>', our behavior depends on which dialect of C++ we're
16396 parsing. In C++98, we probably found a typo for '> >'. But there
16397 are type-id which are also valid expressions. For instance:
16399 struct X { int operator >> (int); };
16400 template <int V> struct Foo {};
16403 Here 'X()' is a valid type-id of a function type, but the user just
16404 wanted to write the expression "X() >> 5". Thus, we remember that we
16405 found a valid type-id, but we still try to parse the argument as an
16406 expression to see what happens.
16408 In C++0x, the '>>' will be considered two separate '>'
16410 if (!cp_parser_error_occurred (parser
)
16411 && cxx_dialect
== cxx98
16412 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16414 maybe_type_id
= true;
16415 cp_parser_abort_tentative_parse (parser
);
16419 /* If the next token isn't a `,' or a `>', then this argument wasn't
16420 really finished. This means that the argument is not a valid
16422 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16423 cp_parser_error (parser
, "expected template-argument");
16424 /* If that worked, we're done. */
16425 if (cp_parser_parse_definitely (parser
))
16428 /* We're still not sure what the argument will be. */
16429 cp_parser_parse_tentatively (parser
);
16430 /* Try a template. */
16431 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16432 argument
= cp_parser_id_expression (parser
,
16433 /*template_keyword_p=*/false,
16434 /*check_dependency_p=*/true,
16436 /*declarator_p=*/false,
16437 /*optional_p=*/false);
16438 /* If the next token isn't a `,' or a `>', then this argument wasn't
16439 really finished. */
16440 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16441 cp_parser_error (parser
, "expected template-argument");
16442 if (!cp_parser_error_occurred (parser
))
16444 /* Figure out what is being referred to. If the id-expression
16445 was for a class template specialization, then we will have a
16446 TYPE_DECL at this point. There is no need to do name lookup
16447 at this point in that case. */
16448 if (TREE_CODE (argument
) != TYPE_DECL
)
16449 argument
= cp_parser_lookup_name (parser
, argument
,
16451 /*is_template=*/template_p
,
16452 /*is_namespace=*/false,
16453 /*check_dependency=*/true,
16454 /*ambiguous_decls=*/NULL
,
16455 argument_start_token
->location
);
16456 /* Handle a constrained-type-specifier for a non-type template
16458 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16460 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16461 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16462 cp_parser_error (parser
, "expected template-name");
16464 if (cp_parser_parse_definitely (parser
))
16466 if (TREE_DEPRECATED (argument
))
16467 warn_deprecated_use (argument
, NULL_TREE
);
16470 /* It must be a non-type argument. In C++17 any constant-expression is
16472 if (cxx_dialect
> cxx14
)
16475 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16477 -- an integral constant-expression of integral or enumeration
16480 -- the name of a non-type template-parameter; or
16482 -- the name of an object or function with external linkage...
16484 -- the address of an object or function with external linkage...
16486 -- a pointer to member... */
16487 /* Look for a non-type template parameter. */
16488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16490 cp_parser_parse_tentatively (parser
);
16491 argument
= cp_parser_primary_expression (parser
,
16492 /*address_p=*/false,
16494 /*template_arg_p=*/true,
16496 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16497 || !cp_parser_next_token_ends_template_argument_p (parser
))
16498 cp_parser_simulate_error (parser
);
16499 if (cp_parser_parse_definitely (parser
))
16503 /* If the next token is "&", the argument must be the address of an
16504 object or function with external linkage. */
16505 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16508 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16509 cp_lexer_consume_token (parser
->lexer
);
16511 /* See if we might have an id-expression. */
16512 token
= cp_lexer_peek_token (parser
->lexer
);
16513 if (token
->type
== CPP_NAME
16514 || token
->keyword
== RID_OPERATOR
16515 || token
->type
== CPP_SCOPE
16516 || token
->type
== CPP_TEMPLATE_ID
16517 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16519 cp_parser_parse_tentatively (parser
);
16520 argument
= cp_parser_primary_expression (parser
,
16523 /*template_arg_p=*/true,
16525 if (cp_parser_error_occurred (parser
)
16526 || !cp_parser_next_token_ends_template_argument_p (parser
))
16527 cp_parser_abort_tentative_parse (parser
);
16532 if (INDIRECT_REF_P (argument
))
16534 /* Strip the dereference temporarily. */
16535 gcc_assert (REFERENCE_REF_P (argument
));
16536 argument
= TREE_OPERAND (argument
, 0);
16539 /* If we're in a template, we represent a qualified-id referring
16540 to a static data member as a SCOPE_REF even if the scope isn't
16541 dependent so that we can check access control later. */
16543 if (TREE_CODE (probe
) == SCOPE_REF
)
16544 probe
= TREE_OPERAND (probe
, 1);
16547 /* A variable without external linkage might still be a
16548 valid constant-expression, so no error is issued here
16549 if the external-linkage check fails. */
16550 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16551 cp_parser_simulate_error (parser
);
16553 else if (is_overloaded_fn (argument
))
16554 /* All overloaded functions are allowed; if the external
16555 linkage test does not pass, an error will be issued
16559 && (TREE_CODE (argument
) == OFFSET_REF
16560 || TREE_CODE (argument
) == SCOPE_REF
))
16561 /* A pointer-to-member. */
16563 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16566 cp_parser_simulate_error (parser
);
16568 if (cp_parser_parse_definitely (parser
))
16571 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16572 tf_warning_or_error
);
16574 argument
= convert_from_reference (argument
);
16579 /* If the argument started with "&", there are no other valid
16580 alternatives at this point. */
16583 cp_parser_error (parser
, "invalid non-type template argument");
16584 return error_mark_node
;
16588 /* If the argument wasn't successfully parsed as a type-id followed
16589 by '>>', the argument can only be a constant expression now.
16590 Otherwise, we try parsing the constant-expression tentatively,
16591 because the argument could really be a type-id. */
16593 cp_parser_parse_tentatively (parser
);
16595 if (cxx_dialect
<= cxx14
)
16596 argument
= cp_parser_constant_expression (parser
);
16599 /* With C++17 generalized non-type template arguments we need to handle
16600 lvalue constant expressions, too. */
16601 argument
= cp_parser_assignment_expression (parser
);
16602 require_potential_constant_expression (argument
);
16605 if (!maybe_type_id
)
16607 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16608 cp_parser_error (parser
, "expected template-argument");
16609 if (cp_parser_parse_definitely (parser
))
16611 /* We did our best to parse the argument as a non type-id, but that
16612 was the only alternative that matched (albeit with a '>' after
16613 it). We can assume it's just a typo from the user, and a
16614 diagnostic will then be issued. */
16615 return cp_parser_template_type_arg (parser
);
16618 /* Parse an explicit-instantiation.
16620 explicit-instantiation:
16621 template declaration
16623 Although the standard says `declaration', what it really means is:
16625 explicit-instantiation:
16626 template decl-specifier-seq [opt] declarator [opt] ;
16628 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16629 supposed to be allowed. A defect report has been filed about this
16634 explicit-instantiation:
16635 storage-class-specifier template
16636 decl-specifier-seq [opt] declarator [opt] ;
16637 function-specifier template
16638 decl-specifier-seq [opt] declarator [opt] ; */
16641 cp_parser_explicit_instantiation (cp_parser
* parser
)
16643 int declares_class_or_enum
;
16644 cp_decl_specifier_seq decl_specifiers
;
16645 tree extension_specifier
= NULL_TREE
;
16647 timevar_push (TV_TEMPLATE_INST
);
16649 /* Look for an (optional) storage-class-specifier or
16650 function-specifier. */
16651 if (cp_parser_allow_gnu_extensions_p (parser
))
16653 extension_specifier
16654 = cp_parser_storage_class_specifier_opt (parser
);
16655 if (!extension_specifier
)
16656 extension_specifier
16657 = cp_parser_function_specifier_opt (parser
,
16658 /*decl_specs=*/NULL
);
16661 /* Look for the `template' keyword. */
16662 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16663 /* Let the front end know that we are processing an explicit
16665 begin_explicit_instantiation ();
16666 /* [temp.explicit] says that we are supposed to ignore access
16667 control while processing explicit instantiation directives. */
16668 push_deferring_access_checks (dk_no_check
);
16669 /* Parse a decl-specifier-seq. */
16670 cp_parser_decl_specifier_seq (parser
,
16671 CP_PARSER_FLAGS_OPTIONAL
,
16673 &declares_class_or_enum
);
16674 /* If there was exactly one decl-specifier, and it declared a class,
16675 and there's no declarator, then we have an explicit type
16677 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16681 type
= check_tag_decl (&decl_specifiers
,
16682 /*explicit_type_instantiation_p=*/true);
16683 /* Turn access control back on for names used during
16684 template instantiation. */
16685 pop_deferring_access_checks ();
16687 do_type_instantiation (type
, extension_specifier
,
16688 /*complain=*/tf_error
);
16692 cp_declarator
*declarator
;
16695 /* Parse the declarator. */
16697 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16698 /*ctor_dtor_or_conv_p=*/NULL
,
16699 /*parenthesized_p=*/NULL
,
16700 /*member_p=*/false,
16701 /*friend_p=*/false);
16702 if (declares_class_or_enum
& 2)
16703 cp_parser_check_for_definition_in_return_type (declarator
,
16704 decl_specifiers
.type
,
16705 decl_specifiers
.locations
[ds_type_spec
]);
16706 if (declarator
!= cp_error_declarator
)
16708 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16709 permerror (decl_specifiers
.locations
[ds_inline
],
16710 "explicit instantiation shall not use"
16711 " %<inline%> specifier");
16712 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16713 permerror (decl_specifiers
.locations
[ds_constexpr
],
16714 "explicit instantiation shall not use"
16715 " %<constexpr%> specifier");
16717 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16718 NORMAL
, 0, &decl_specifiers
.attributes
);
16719 /* Turn access control back on for names used during
16720 template instantiation. */
16721 pop_deferring_access_checks ();
16722 /* Do the explicit instantiation. */
16723 do_decl_instantiation (decl
, extension_specifier
);
16727 pop_deferring_access_checks ();
16728 /* Skip the body of the explicit instantiation. */
16729 cp_parser_skip_to_end_of_statement (parser
);
16732 /* We're done with the instantiation. */
16733 end_explicit_instantiation ();
16735 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16737 timevar_pop (TV_TEMPLATE_INST
);
16740 /* Parse an explicit-specialization.
16742 explicit-specialization:
16743 template < > declaration
16745 Although the standard says `declaration', what it really means is:
16747 explicit-specialization:
16748 template <> decl-specifier [opt] init-declarator [opt] ;
16749 template <> function-definition
16750 template <> explicit-specialization
16751 template <> template-declaration */
16754 cp_parser_explicit_specialization (cp_parser
* parser
)
16756 bool need_lang_pop
;
16757 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16759 /* Look for the `template' keyword. */
16760 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16761 /* Look for the `<'. */
16762 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16763 /* Look for the `>'. */
16764 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16765 /* We have processed another parameter list. */
16766 ++parser
->num_template_parameter_lists
;
16769 A template ... explicit specialization ... shall not have C
16771 if (current_lang_name
== lang_name_c
)
16773 error_at (token
->location
, "template specialization with C linkage");
16774 maybe_show_extern_c_location ();
16775 /* Give it C++ linkage to avoid confusing other parts of the
16777 push_lang_context (lang_name_cplusplus
);
16778 need_lang_pop
= true;
16781 need_lang_pop
= false;
16782 /* Let the front end know that we are beginning a specialization. */
16783 if (!begin_specialization ())
16785 end_specialization ();
16789 /* If the next keyword is `template', we need to figure out whether
16790 or not we're looking a template-declaration. */
16791 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16793 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16794 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16795 cp_parser_template_declaration_after_export (parser
,
16796 /*member_p=*/false);
16798 cp_parser_explicit_specialization (parser
);
16801 /* Parse the dependent declaration. */
16802 cp_parser_single_declaration (parser
,
16804 /*member_p=*/false,
16805 /*explicit_specialization_p=*/true,
16806 /*friend_p=*/NULL
);
16807 /* We're done with the specialization. */
16808 end_specialization ();
16809 /* For the erroneous case of a template with C linkage, we pushed an
16810 implicit C++ linkage scope; exit that scope now. */
16812 pop_lang_context ();
16813 /* We're done with this parameter list. */
16814 --parser
->num_template_parameter_lists
;
16817 /* Parse a type-specifier.
16820 simple-type-specifier
16823 elaborated-type-specifier
16831 Returns a representation of the type-specifier. For a
16832 class-specifier, enum-specifier, or elaborated-type-specifier, a
16833 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16835 The parser flags FLAGS is used to control type-specifier parsing.
16837 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16838 in a decl-specifier-seq.
16840 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16841 class-specifier, enum-specifier, or elaborated-type-specifier, then
16842 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16843 if a type is declared; 2 if it is defined. Otherwise, it is set to
16846 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16847 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16848 is set to FALSE. */
16851 cp_parser_type_specifier (cp_parser
* parser
,
16852 cp_parser_flags flags
,
16853 cp_decl_specifier_seq
*decl_specs
,
16854 bool is_declaration
,
16855 int* declares_class_or_enum
,
16856 bool* is_cv_qualifier
)
16858 tree type_spec
= NULL_TREE
;
16861 cp_decl_spec ds
= ds_last
;
16863 /* Assume this type-specifier does not declare a new type. */
16864 if (declares_class_or_enum
)
16865 *declares_class_or_enum
= 0;
16866 /* And that it does not specify a cv-qualifier. */
16867 if (is_cv_qualifier
)
16868 *is_cv_qualifier
= false;
16869 /* Peek at the next token. */
16870 token
= cp_lexer_peek_token (parser
->lexer
);
16872 /* If we're looking at a keyword, we can use that to guide the
16873 production we choose. */
16874 keyword
= token
->keyword
;
16878 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16879 goto elaborated_type_specifier
;
16881 /* Look for the enum-specifier. */
16882 type_spec
= cp_parser_enum_specifier (parser
);
16883 /* If that worked, we're done. */
16886 if (declares_class_or_enum
)
16887 *declares_class_or_enum
= 2;
16889 cp_parser_set_decl_spec_type (decl_specs
,
16892 /*type_definition_p=*/true);
16896 goto elaborated_type_specifier
;
16898 /* Any of these indicate either a class-specifier, or an
16899 elaborated-type-specifier. */
16903 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16904 goto elaborated_type_specifier
;
16906 /* Parse tentatively so that we can back up if we don't find a
16907 class-specifier. */
16908 cp_parser_parse_tentatively (parser
);
16909 /* Look for the class-specifier. */
16910 type_spec
= cp_parser_class_specifier (parser
);
16911 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16912 /* If that worked, we're done. */
16913 if (cp_parser_parse_definitely (parser
))
16915 if (declares_class_or_enum
)
16916 *declares_class_or_enum
= 2;
16918 cp_parser_set_decl_spec_type (decl_specs
,
16921 /*type_definition_p=*/true);
16925 /* Fall through. */
16926 elaborated_type_specifier
:
16927 /* We're declaring (not defining) a class or enum. */
16928 if (declares_class_or_enum
)
16929 *declares_class_or_enum
= 1;
16931 /* Fall through. */
16933 /* Look for an elaborated-type-specifier. */
16935 = (cp_parser_elaborated_type_specifier
16937 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16940 cp_parser_set_decl_spec_type (decl_specs
,
16943 /*type_definition_p=*/false);
16948 if (is_cv_qualifier
)
16949 *is_cv_qualifier
= true;
16954 if (is_cv_qualifier
)
16955 *is_cv_qualifier
= true;
16960 if (is_cv_qualifier
)
16961 *is_cv_qualifier
= true;
16965 /* The `__complex__' keyword is a GNU extension. */
16973 /* Handle simple keywords. */
16978 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16979 decl_specs
->any_specifiers_p
= true;
16981 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16984 /* If we do not already have a type-specifier, assume we are looking
16985 at a simple-type-specifier. */
16986 type_spec
= cp_parser_simple_type_specifier (parser
,
16990 /* If we didn't find a type-specifier, and a type-specifier was not
16991 optional in this context, issue an error message. */
16992 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16994 cp_parser_error (parser
, "expected type specifier");
16995 return error_mark_node
;
17001 /* Parse a simple-type-specifier.
17003 simple-type-specifier:
17004 :: [opt] nested-name-specifier [opt] type-name
17005 :: [opt] nested-name-specifier template template-id
17020 simple-type-specifier:
17022 decltype ( expression )
17025 __underlying_type ( type-id )
17029 nested-name-specifier(opt) template-name
17033 simple-type-specifier:
17035 __typeof__ unary-expression
17036 __typeof__ ( type-id )
17037 __typeof__ ( type-id ) { initializer-list , [opt] }
17039 Concepts Extension:
17041 simple-type-specifier:
17042 constrained-type-specifier
17044 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17045 appropriately updated. */
17048 cp_parser_simple_type_specifier (cp_parser
* parser
,
17049 cp_decl_specifier_seq
*decl_specs
,
17050 cp_parser_flags flags
)
17052 tree type
= NULL_TREE
;
17056 /* Peek at the next token. */
17057 token
= cp_lexer_peek_token (parser
->lexer
);
17059 /* If we're looking at a keyword, things are easy. */
17060 switch (token
->keyword
)
17064 decl_specs
->explicit_char_p
= true;
17065 type
= char_type_node
;
17068 type
= char16_type_node
;
17071 type
= char32_type_node
;
17074 type
= wchar_type_node
;
17077 type
= boolean_type_node
;
17080 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17081 type
= short_integer_type_node
;
17085 decl_specs
->explicit_int_p
= true;
17086 type
= integer_type_node
;
17092 idx
= token
->keyword
- RID_INT_N_0
;
17093 if (! int_n_enabled_p
[idx
])
17097 decl_specs
->explicit_intN_p
= true;
17098 decl_specs
->int_n_idx
= idx
;
17100 type
= int_n_trees
[idx
].signed_type
;
17104 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17105 type
= long_integer_type_node
;
17108 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17109 type
= integer_type_node
;
17112 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17113 type
= unsigned_type_node
;
17116 type
= float_type_node
;
17119 type
= double_type_node
;
17122 type
= void_type_node
;
17126 maybe_warn_cpp0x (CPP0X_AUTO
);
17127 if (parser
->auto_is_implicit_function_template_parm_p
)
17129 /* The 'auto' might be the placeholder return type for a function decl
17130 with trailing return type. */
17131 bool have_trailing_return_fn_decl
= false;
17133 cp_parser_parse_tentatively (parser
);
17134 cp_lexer_consume_token (parser
->lexer
);
17135 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17136 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17137 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17138 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17142 cp_lexer_consume_token (parser
->lexer
);
17143 cp_parser_skip_to_closing_parenthesis (parser
,
17144 /*recovering*/false,
17146 /*consume_paren*/true);
17150 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17152 have_trailing_return_fn_decl
= true;
17156 cp_lexer_consume_token (parser
->lexer
);
17158 cp_parser_abort_tentative_parse (parser
);
17160 if (have_trailing_return_fn_decl
)
17162 type
= make_auto ();
17166 if (cxx_dialect
>= cxx14
)
17168 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17169 type
= TREE_TYPE (type
);
17172 type
= error_mark_node
;
17174 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17176 if (cxx_dialect
< cxx14
)
17177 error_at (token
->location
,
17178 "use of %<auto%> in lambda parameter declaration "
17179 "only available with "
17180 "-std=c++14 or -std=gnu++14");
17182 else if (cxx_dialect
< cxx14
)
17183 error_at (token
->location
,
17184 "use of %<auto%> in parameter declaration "
17185 "only available with "
17186 "-std=c++14 or -std=gnu++14");
17187 else if (!flag_concepts
)
17188 pedwarn (token
->location
, 0,
17189 "use of %<auto%> in parameter declaration "
17190 "only available with -fconcepts");
17193 type
= make_auto ();
17197 /* Since DR 743, decltype can either be a simple-type-specifier by
17198 itself or begin a nested-name-specifier. Parsing it will replace
17199 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17200 handling below decide what to do. */
17201 cp_parser_decltype (parser
);
17202 cp_lexer_set_token_position (parser
->lexer
, token
);
17206 /* Consume the `typeof' token. */
17207 cp_lexer_consume_token (parser
->lexer
);
17208 /* Parse the operand to `typeof'. */
17209 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17210 /* If it is not already a TYPE, take its type. */
17211 if (!TYPE_P (type
))
17212 type
= finish_typeof (type
);
17215 cp_parser_set_decl_spec_type (decl_specs
, type
,
17217 /*type_definition_p=*/false);
17221 case RID_UNDERLYING_TYPE
:
17222 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17224 cp_parser_set_decl_spec_type (decl_specs
, type
,
17226 /*type_definition_p=*/false);
17231 case RID_DIRECT_BASES
:
17232 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17234 cp_parser_set_decl_spec_type (decl_specs
, type
,
17236 /*type_definition_p=*/false);
17242 /* If token is an already-parsed decltype not followed by ::,
17243 it's a simple-type-specifier. */
17244 if (token
->type
== CPP_DECLTYPE
17245 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17247 type
= saved_checks_value (token
->u
.tree_check_value
);
17250 cp_parser_set_decl_spec_type (decl_specs
, type
,
17252 /*type_definition_p=*/false);
17253 /* Remember that we are handling a decltype in order to
17254 implement the resolution of DR 1510 when the argument
17255 isn't instantiation dependent. */
17256 decl_specs
->decltype_p
= true;
17258 cp_lexer_consume_token (parser
->lexer
);
17262 /* If the type-specifier was for a built-in type, we're done. */
17265 /* Record the type. */
17267 && (token
->keyword
!= RID_SIGNED
17268 && token
->keyword
!= RID_UNSIGNED
17269 && token
->keyword
!= RID_SHORT
17270 && token
->keyword
!= RID_LONG
))
17271 cp_parser_set_decl_spec_type (decl_specs
,
17274 /*type_definition_p=*/false);
17276 decl_specs
->any_specifiers_p
= true;
17278 /* Consume the token. */
17279 cp_lexer_consume_token (parser
->lexer
);
17281 if (type
== error_mark_node
)
17282 return error_mark_node
;
17284 /* There is no valid C++ program where a non-template type is
17285 followed by a "<". That usually indicates that the user thought
17286 that the type was a template. */
17287 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17290 return TYPE_NAME (type
);
17293 /* The type-specifier must be a user-defined type. */
17294 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17299 /* Don't gobble tokens or issue error messages if this is an
17300 optional type-specifier. */
17301 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17302 cp_parser_parse_tentatively (parser
);
17304 token
= cp_lexer_peek_token (parser
->lexer
);
17306 /* Look for the optional `::' operator. */
17308 = (cp_parser_global_scope_opt (parser
,
17309 /*current_scope_valid_p=*/false)
17311 /* Look for the nested-name specifier. */
17313 = (cp_parser_nested_name_specifier_opt (parser
,
17314 /*typename_keyword_p=*/false,
17315 /*check_dependency_p=*/true,
17317 /*is_declaration=*/false)
17319 /* If we have seen a nested-name-specifier, and the next token
17320 is `template', then we are using the template-id production. */
17322 && cp_parser_optional_template_keyword (parser
))
17324 /* Look for the template-id. */
17325 type
= cp_parser_template_id (parser
,
17326 /*template_keyword_p=*/true,
17327 /*check_dependency_p=*/true,
17329 /*is_declaration=*/false);
17330 /* If the template-id did not name a type, we are out of
17332 if (TREE_CODE (type
) != TYPE_DECL
)
17334 cp_parser_error (parser
, "expected template-id for type");
17338 /* Otherwise, look for a type-name. */
17340 type
= cp_parser_type_name (parser
);
17341 /* Keep track of all name-lookups performed in class scopes. */
17345 && TREE_CODE (type
) == TYPE_DECL
17346 && identifier_p (DECL_NAME (type
)))
17347 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17348 /* If it didn't work out, we don't have a TYPE. */
17349 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17350 && !cp_parser_parse_definitely (parser
))
17352 if (!type
&& cxx_dialect
>= cxx17
)
17354 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17355 cp_parser_parse_tentatively (parser
);
17357 cp_parser_global_scope_opt (parser
,
17358 /*current_scope_valid_p=*/false);
17359 cp_parser_nested_name_specifier_opt (parser
,
17360 /*typename_keyword_p=*/false,
17361 /*check_dependency_p=*/true,
17363 /*is_declaration=*/false);
17364 tree name
= cp_parser_identifier (parser
);
17365 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17366 && parser
->scope
!= error_mark_node
)
17368 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17370 /*is_template=*/false,
17371 /*is_namespace=*/false,
17372 /*check_dependency=*/true,
17373 /*ambiguous_decls=*/NULL
,
17375 if (tmpl
&& tmpl
!= error_mark_node
17376 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17377 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17378 type
= make_template_placeholder (tmpl
);
17381 type
= error_mark_node
;
17382 if (!cp_parser_simulate_error (parser
))
17383 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17384 NLE_TYPE
, token
->location
);
17388 type
= error_mark_node
;
17390 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17391 && !cp_parser_parse_definitely (parser
))
17394 if (type
&& decl_specs
)
17395 cp_parser_set_decl_spec_type (decl_specs
, type
,
17397 /*type_definition_p=*/false);
17400 /* If we didn't get a type-name, issue an error message. */
17401 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17403 cp_parser_error (parser
, "expected type-name");
17404 return error_mark_node
;
17407 if (type
&& type
!= error_mark_node
)
17409 /* See if TYPE is an Objective-C type, and if so, parse and
17410 accept any protocol references following it. Do this before
17411 the cp_parser_check_for_invalid_template_id() call, because
17412 Objective-C types can be followed by '<...>' which would
17413 enclose protocol names rather than template arguments, and so
17414 everything is fine. */
17415 if (c_dialect_objc () && !parser
->scope
17416 && (objc_is_id (type
) || objc_is_class_name (type
)))
17418 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17419 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17421 /* Clobber the "unqualified" type previously entered into
17422 DECL_SPECS with the new, improved protocol-qualified version. */
17424 decl_specs
->type
= qual_type
;
17429 /* There is no valid C++ program where a non-template type is
17430 followed by a "<". That usually indicates that the user
17431 thought that the type was a template. */
17432 cp_parser_check_for_invalid_template_id (parser
, type
,
17440 /* Parse a type-name.
17446 simple-template-id [in c++0x]
17463 Returns a TYPE_DECL for the type. */
17466 cp_parser_type_name (cp_parser
* parser
)
17468 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17473 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17477 /* We can't know yet whether it is a class-name or not. */
17478 cp_parser_parse_tentatively (parser
);
17479 /* Try a class-name. */
17480 type_decl
= cp_parser_class_name (parser
,
17481 typename_keyword_p
,
17482 /*template_keyword_p=*/false,
17484 /*check_dependency_p=*/true,
17485 /*class_head_p=*/false,
17486 /*is_declaration=*/false);
17487 /* If it's not a class-name, keep looking. */
17488 if (!cp_parser_parse_definitely (parser
))
17490 if (cxx_dialect
< cxx11
)
17491 /* It must be a typedef-name or an enum-name. */
17492 return cp_parser_nonclass_name (parser
);
17494 cp_parser_parse_tentatively (parser
);
17495 /* It is either a simple-template-id representing an
17496 instantiation of an alias template... */
17497 type_decl
= cp_parser_template_id (parser
,
17498 /*template_keyword_p=*/false,
17499 /*check_dependency_p=*/true,
17501 /*is_declaration=*/false);
17502 /* Note that this must be an instantiation of an alias template
17503 because [temp.names]/6 says:
17505 A template-id that names an alias template specialization
17508 Whereas [temp.names]/7 says:
17510 A simple-template-id that names a class template
17511 specialization is a class-name.
17513 With concepts, this could also be a partial-concept-id that
17514 declares a non-type template parameter. */
17515 if (type_decl
!= NULL_TREE
17516 && TREE_CODE (type_decl
) == TYPE_DECL
17517 && TYPE_DECL_ALIAS_P (type_decl
))
17518 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17519 else if (is_constrained_parameter (type_decl
))
17520 /* Don't do anything. */ ;
17522 cp_parser_simulate_error (parser
);
17524 if (!cp_parser_parse_definitely (parser
))
17525 /* ... Or a typedef-name or an enum-name. */
17526 return cp_parser_nonclass_name (parser
);
17532 /* Check if DECL and ARGS can form a constrained-type-specifier.
17533 If ARGS is non-null, we try to form a concept check of the
17534 form DECL<?, ARGS> where ? is a wildcard that matches any
17535 kind of template argument. If ARGS is NULL, then we try to
17536 form a concept check of the form DECL<?>. */
17539 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17540 tree decl
, tree args
)
17542 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17544 /* If we a constrained-type-specifier cannot be deduced. */
17545 if (parser
->prevent_constrained_type_specifiers
)
17548 /* A constrained type specifier can only be found in an
17549 overload set or as a reference to a template declaration.
17551 FIXME: This might be masking a bug. It's possible that
17552 that the deduction below is causing template specializations
17553 to be formed with the wildcard as an argument. */
17554 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17557 /* Try to build a call expression that evaluates the
17558 concept. This can fail if the overload set refers
17559 only to non-templates. */
17560 tree placeholder
= build_nt (WILDCARD_DECL
);
17561 tree check
= build_concept_check (decl
, placeholder
, args
);
17562 if (check
== error_mark_node
)
17565 /* Deduce the checked constraint and the prototype parameter.
17567 FIXME: In certain cases, failure to deduce should be a
17568 diagnosable error. */
17571 if (!deduce_constrained_parameter (check
, conc
, proto
))
17574 /* In template parameter scope, this results in a constrained
17575 parameter. Return a descriptor of that parm. */
17576 if (processing_template_parmlist
)
17577 return build_constrained_parameter (conc
, proto
, args
);
17579 /* In a parameter-declaration-clause, constrained-type
17580 specifiers result in invented template parameters. */
17581 if (parser
->auto_is_implicit_function_template_parm_p
)
17583 tree x
= build_constrained_parameter (conc
, proto
, args
);
17584 return synthesize_implicit_template_parm (parser
, x
);
17588 /* Otherwise, we're in a context where the constrained
17589 type name is deduced and the constraint applies
17590 after deduction. */
17591 return make_constrained_auto (conc
, args
);
17597 /* If DECL refers to a concept, return a TYPE_DECL representing
17598 the result of using the constrained type specifier in the
17599 current context. DECL refers to a concept if
17601 - it is an overload set containing a function concept taking a single
17604 - it is a variable concept taking a single type argument. */
17607 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17610 && (TREE_CODE (decl
) == OVERLOAD
17611 || BASELINK_P (decl
)
17612 || variable_concept_p (decl
)))
17613 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17618 /* Check if DECL and ARGS form a partial-concept-id. If so,
17619 assign ID to the resulting constrained placeholder.
17621 Returns true if the partial-concept-id designates a placeholder
17622 and false otherwise. Note that *id is set to NULL_TREE in
17626 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17628 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17631 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17643 Returns a TYPE_DECL for the type. */
17646 cp_parser_nonclass_name (cp_parser
* parser
)
17651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17652 identifier
= cp_parser_identifier (parser
);
17653 if (identifier
== error_mark_node
)
17654 return error_mark_node
;
17656 /* Look up the type-name. */
17657 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17659 type_decl
= strip_using_decl (type_decl
);
17661 /* If we found an overload set, then it may refer to a concept-name. */
17662 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17665 if (TREE_CODE (type_decl
) != TYPE_DECL
17666 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17668 /* See if this is an Objective-C type. */
17669 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17670 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17672 type_decl
= TYPE_NAME (type
);
17675 /* Issue an error if we did not find a type-name. */
17676 if (TREE_CODE (type_decl
) != TYPE_DECL
17677 /* In Objective-C, we have the complication that class names are
17678 normally type names and start declarations (eg, the
17679 "NSObject" in "NSObject *object;"), but can be used in an
17680 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17681 is an expression. So, a classname followed by a dot is not a
17682 valid type-name. */
17683 || (objc_is_class_name (TREE_TYPE (type_decl
))
17684 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17686 if (!cp_parser_simulate_error (parser
))
17687 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17688 NLE_TYPE
, token
->location
);
17689 return error_mark_node
;
17691 /* Remember that the name was used in the definition of the
17692 current class so that we can check later to see if the
17693 meaning would have been different after the class was
17694 entirely defined. */
17695 else if (type_decl
!= error_mark_node
17697 maybe_note_name_used_in_class (identifier
, type_decl
);
17702 /* Parse an elaborated-type-specifier. Note that the grammar given
17703 here incorporates the resolution to DR68.
17705 elaborated-type-specifier:
17706 class-key :: [opt] nested-name-specifier [opt] identifier
17707 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17708 enum-key :: [opt] nested-name-specifier [opt] identifier
17709 typename :: [opt] nested-name-specifier identifier
17710 typename :: [opt] nested-name-specifier template [opt]
17715 elaborated-type-specifier:
17716 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17717 class-key attributes :: [opt] nested-name-specifier [opt]
17718 template [opt] template-id
17719 enum attributes :: [opt] nested-name-specifier [opt] identifier
17721 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17722 declared `friend'. If IS_DECLARATION is TRUE, then this
17723 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17724 something is being declared.
17726 Returns the TYPE specified. */
17729 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17731 bool is_declaration
)
17733 enum tag_types tag_type
;
17735 tree type
= NULL_TREE
;
17736 tree attributes
= NULL_TREE
;
17738 cp_token
*token
= NULL
;
17740 /* See if we're looking at the `enum' keyword. */
17741 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17743 /* Consume the `enum' token. */
17744 cp_lexer_consume_token (parser
->lexer
);
17745 /* Remember that it's an enumeration type. */
17746 tag_type
= enum_type
;
17747 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17748 enums) is used here. */
17749 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17750 if (cp_parser_is_keyword (token
, RID_CLASS
)
17751 || cp_parser_is_keyword (token
, RID_STRUCT
))
17753 gcc_rich_location
richloc (token
->location
);
17754 richloc
.add_range (input_location
);
17755 richloc
.add_fixit_remove ();
17756 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17757 "a scoped enum must not use the %qD keyword",
17759 /* Consume the `struct' or `class' and parse it anyway. */
17760 cp_lexer_consume_token (parser
->lexer
);
17762 /* Parse the attributes. */
17763 attributes
= cp_parser_attributes_opt (parser
);
17765 /* Or, it might be `typename'. */
17766 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17769 /* Consume the `typename' token. */
17770 cp_lexer_consume_token (parser
->lexer
);
17771 /* Remember that it's a `typename' type. */
17772 tag_type
= typename_type
;
17774 /* Otherwise it must be a class-key. */
17777 tag_type
= cp_parser_class_key (parser
);
17778 if (tag_type
== none_type
)
17779 return error_mark_node
;
17780 /* Parse the attributes. */
17781 attributes
= cp_parser_attributes_opt (parser
);
17784 /* Look for the `::' operator. */
17785 globalscope
= cp_parser_global_scope_opt (parser
,
17786 /*current_scope_valid_p=*/false);
17787 /* Look for the nested-name-specifier. */
17788 tree nested_name_specifier
;
17789 if (tag_type
== typename_type
&& !globalscope
)
17791 nested_name_specifier
17792 = cp_parser_nested_name_specifier (parser
,
17793 /*typename_keyword_p=*/true,
17794 /*check_dependency_p=*/true,
17797 if (!nested_name_specifier
)
17798 return error_mark_node
;
17801 /* Even though `typename' is not present, the proposed resolution
17802 to Core Issue 180 says that in `class A<T>::B', `B' should be
17803 considered a type-name, even if `A<T>' is dependent. */
17804 nested_name_specifier
17805 = cp_parser_nested_name_specifier_opt (parser
,
17806 /*typename_keyword_p=*/true,
17807 /*check_dependency_p=*/true,
17810 /* For everything but enumeration types, consider a template-id.
17811 For an enumeration type, consider only a plain identifier. */
17812 if (tag_type
!= enum_type
)
17814 bool template_p
= false;
17817 /* Allow the `template' keyword. */
17818 template_p
= cp_parser_optional_template_keyword (parser
);
17819 /* If we didn't see `template', we don't know if there's a
17820 template-id or not. */
17822 cp_parser_parse_tentatively (parser
);
17823 /* Parse the template-id. */
17824 token
= cp_lexer_peek_token (parser
->lexer
);
17825 decl
= cp_parser_template_id (parser
, template_p
,
17826 /*check_dependency_p=*/true,
17829 /* If we didn't find a template-id, look for an ordinary
17831 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17833 /* We can get here when cp_parser_template_id, called by
17834 cp_parser_class_name with tag_type == none_type, succeeds
17835 and caches a BASELINK. Then, when called again here,
17836 instead of failing and returning an error_mark_node
17837 returns it (see template/typename17.C in C++11).
17838 ??? Could we diagnose this earlier? */
17839 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17841 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17842 type
= error_mark_node
;
17844 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17845 in effect, then we must assume that, upon instantiation, the
17846 template will correspond to a class. */
17847 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17848 && tag_type
== typename_type
)
17849 type
= make_typename_type (parser
->scope
, decl
,
17851 /*complain=*/tf_error
);
17852 /* If the `typename' keyword is in effect and DECL is not a type
17853 decl, then type is non existent. */
17854 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17856 else if (TREE_CODE (decl
) == TYPE_DECL
)
17858 type
= check_elaborated_type_specifier (tag_type
, decl
,
17859 /*allow_template_p=*/true);
17861 /* If the next token is a semicolon, this must be a specialization,
17862 instantiation, or friend declaration. Check the scope while we
17863 still know whether or not we had a nested-name-specifier. */
17864 if (type
!= error_mark_node
17865 && !nested_name_specifier
&& !is_friend
17866 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17867 check_unqualified_spec_or_inst (type
, token
->location
);
17869 else if (decl
== error_mark_node
)
17870 type
= error_mark_node
;
17875 token
= cp_lexer_peek_token (parser
->lexer
);
17876 identifier
= cp_parser_identifier (parser
);
17878 if (identifier
== error_mark_node
)
17880 parser
->scope
= NULL_TREE
;
17881 return error_mark_node
;
17884 /* For a `typename', we needn't call xref_tag. */
17885 if (tag_type
== typename_type
17886 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17887 return cp_parser_make_typename_type (parser
, identifier
,
17890 /* Template parameter lists apply only if we are not within a
17891 function parameter list. */
17892 bool template_parm_lists_apply
17893 = parser
->num_template_parameter_lists
;
17894 if (template_parm_lists_apply
)
17895 for (cp_binding_level
*s
= current_binding_level
;
17896 s
&& s
->kind
!= sk_template_parms
;
17897 s
= s
->level_chain
)
17898 if (s
->kind
== sk_function_parms
)
17899 template_parm_lists_apply
= false;
17901 /* Look up a qualified name in the usual way. */
17905 tree ambiguous_decls
;
17907 decl
= cp_parser_lookup_name (parser
, identifier
,
17909 /*is_template=*/false,
17910 /*is_namespace=*/false,
17911 /*check_dependency=*/true,
17915 /* If the lookup was ambiguous, an error will already have been
17917 if (ambiguous_decls
)
17918 return error_mark_node
;
17920 /* If we are parsing friend declaration, DECL may be a
17921 TEMPLATE_DECL tree node here. However, we need to check
17922 whether this TEMPLATE_DECL results in valid code. Consider
17923 the following example:
17926 template <class T> class C {};
17929 template <class T> friend class N::C; // #1, valid code
17931 template <class T> class Y {
17932 friend class N::C; // #2, invalid code
17935 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17936 name lookup of `N::C'. We see that friend declaration must
17937 be template for the code to be valid. Note that
17938 processing_template_decl does not work here since it is
17939 always 1 for the above two cases. */
17941 decl
= (cp_parser_maybe_treat_template_as_class
17942 (decl
, /*tag_name_p=*/is_friend
17943 && template_parm_lists_apply
));
17945 if (TREE_CODE (decl
) != TYPE_DECL
)
17947 cp_parser_diagnose_invalid_type_name (parser
,
17950 return error_mark_node
;
17953 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17955 bool allow_template
= (template_parm_lists_apply
17956 || DECL_SELF_REFERENCE_P (decl
));
17957 type
= check_elaborated_type_specifier (tag_type
, decl
,
17960 if (type
== error_mark_node
)
17961 return error_mark_node
;
17964 /* Forward declarations of nested types, such as
17969 are invalid unless all components preceding the final '::'
17970 are complete. If all enclosing types are complete, these
17971 declarations become merely pointless.
17973 Invalid forward declarations of nested types are errors
17974 caught elsewhere in parsing. Those that are pointless arrive
17977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17978 && !is_friend
&& !processing_explicit_instantiation
)
17979 warning (0, "declaration %qD does not declare anything", decl
);
17981 type
= TREE_TYPE (decl
);
17985 /* An elaborated-type-specifier sometimes introduces a new type and
17986 sometimes names an existing type. Normally, the rule is that it
17987 introduces a new type only if there is not an existing type of
17988 the same name already in scope. For example, given:
17991 void f() { struct S s; }
17993 the `struct S' in the body of `f' is the same `struct S' as in
17994 the global scope; the existing definition is used. However, if
17995 there were no global declaration, this would introduce a new
17996 local class named `S'.
17998 An exception to this rule applies to the following code:
18000 namespace N { struct S; }
18002 Here, the elaborated-type-specifier names a new type
18003 unconditionally; even if there is already an `S' in the
18004 containing scope this declaration names a new type.
18005 This exception only applies if the elaborated-type-specifier
18006 forms the complete declaration:
18010 A declaration consisting solely of `class-key identifier ;' is
18011 either a redeclaration of the name in the current scope or a
18012 forward declaration of the identifier as a class name. It
18013 introduces the name into the current scope.
18015 We are in this situation precisely when the next token is a `;'.
18017 An exception to the exception is that a `friend' declaration does
18018 *not* name a new type; i.e., given:
18020 struct S { friend struct T; };
18022 `T' is not a new type in the scope of `S'.
18024 Also, `new struct S' or `sizeof (struct S)' never results in the
18025 definition of a new type; a new type can only be declared in a
18026 declaration context. */
18032 /* Friends have special name lookup rules. */
18033 ts
= ts_within_enclosing_non_class
;
18034 else if (is_declaration
18035 && cp_lexer_next_token_is (parser
->lexer
,
18037 /* This is a `class-key identifier ;' */
18043 (template_parm_lists_apply
18044 && (cp_parser_next_token_starts_class_definition_p (parser
)
18045 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18046 /* An unqualified name was used to reference this type, so
18047 there were no qualifying templates. */
18048 if (template_parm_lists_apply
18049 && !cp_parser_check_template_parameters (parser
,
18050 /*num_templates=*/0,
18051 /*template_id*/false,
18053 /*declarator=*/NULL
))
18054 return error_mark_node
;
18055 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18059 if (type
== error_mark_node
)
18060 return error_mark_node
;
18062 /* Allow attributes on forward declarations of classes. */
18065 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18066 warning (OPT_Wattributes
,
18067 "attributes ignored on uninstantiated type");
18068 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18069 && ! processing_explicit_instantiation
)
18070 warning (OPT_Wattributes
,
18071 "attributes ignored on template instantiation");
18072 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18073 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18075 warning (OPT_Wattributes
,
18076 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18079 if (tag_type
!= enum_type
)
18081 /* Indicate whether this class was declared as a `class' or as a
18083 if (CLASS_TYPE_P (type
))
18084 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18085 cp_parser_check_class_key (tag_type
, type
);
18088 /* A "<" cannot follow an elaborated type specifier. If that
18089 happens, the user was probably trying to form a template-id. */
18090 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18096 /* Parse an enum-specifier.
18099 enum-head { enumerator-list [opt] }
18100 enum-head { enumerator-list , } [C++0x]
18103 enum-key identifier [opt] enum-base [opt]
18104 enum-key nested-name-specifier identifier enum-base [opt]
18109 enum struct [C++0x]
18112 : type-specifier-seq
18114 opaque-enum-specifier:
18115 enum-key identifier enum-base [opt] ;
18118 enum-key attributes[opt] identifier [opt] enum-base [opt]
18119 { enumerator-list [opt] }attributes[opt]
18120 enum-key attributes[opt] identifier [opt] enum-base [opt]
18121 { enumerator-list, }attributes[opt] [C++0x]
18123 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18124 if the token stream isn't an enum-specifier after all. */
18127 cp_parser_enum_specifier (cp_parser
* parser
)
18130 tree type
= NULL_TREE
;
18132 tree nested_name_specifier
= NULL_TREE
;
18134 bool scoped_enum_p
= false;
18135 bool has_underlying_type
= false;
18136 bool nested_being_defined
= false;
18137 bool new_value_list
= false;
18138 bool is_new_type
= false;
18139 bool is_unnamed
= false;
18140 tree underlying_type
= NULL_TREE
;
18141 cp_token
*type_start_token
= NULL
;
18142 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18144 parser
->colon_corrects_to_scope_p
= false;
18146 /* Parse tentatively so that we can back up if we don't find a
18148 cp_parser_parse_tentatively (parser
);
18150 /* Caller guarantees that the current token is 'enum', an identifier
18151 possibly follows, and the token after that is an opening brace.
18152 If we don't have an identifier, fabricate an anonymous name for
18153 the enumeration being defined. */
18154 cp_lexer_consume_token (parser
->lexer
);
18156 /* Parse the "class" or "struct", which indicates a scoped
18157 enumeration type in C++0x. */
18158 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18159 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18161 if (cxx_dialect
< cxx11
)
18162 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18164 /* Consume the `struct' or `class' token. */
18165 cp_lexer_consume_token (parser
->lexer
);
18167 scoped_enum_p
= true;
18170 attributes
= cp_parser_attributes_opt (parser
);
18172 /* Clear the qualification. */
18173 parser
->scope
= NULL_TREE
;
18174 parser
->qualifying_scope
= NULL_TREE
;
18175 parser
->object_scope
= NULL_TREE
;
18177 /* Figure out in what scope the declaration is being placed. */
18178 prev_scope
= current_scope ();
18180 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18182 push_deferring_access_checks (dk_no_check
);
18183 nested_name_specifier
18184 = cp_parser_nested_name_specifier_opt (parser
,
18185 /*typename_keyword_p=*/true,
18186 /*check_dependency_p=*/false,
18188 /*is_declaration=*/false);
18190 if (nested_name_specifier
)
18194 identifier
= cp_parser_identifier (parser
);
18195 name
= cp_parser_lookup_name (parser
, identifier
,
18197 /*is_template=*/false,
18198 /*is_namespace=*/false,
18199 /*check_dependency=*/true,
18200 /*ambiguous_decls=*/NULL
,
18202 if (name
&& name
!= error_mark_node
)
18204 type
= TREE_TYPE (name
);
18205 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18207 /* Are template enums allowed in ISO? */
18208 if (template_parm_scope_p ())
18209 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18210 "%qD is an enumeration template", name
);
18211 /* ignore a typename reference, for it will be solved by name
18216 else if (nested_name_specifier
== error_mark_node
)
18217 /* We already issued an error. */;
18220 error_at (type_start_token
->location
,
18221 "%qD does not name an enumeration in %qT",
18222 identifier
, nested_name_specifier
);
18223 nested_name_specifier
= error_mark_node
;
18228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18229 identifier
= cp_parser_identifier (parser
);
18232 identifier
= make_anon_name ();
18235 error_at (type_start_token
->location
,
18236 "unnamed scoped enum is not allowed");
18239 pop_deferring_access_checks ();
18241 /* Check for the `:' that denotes a specified underlying type in C++0x.
18242 Note that a ':' could also indicate a bitfield width, however. */
18243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18245 cp_decl_specifier_seq type_specifiers
;
18247 /* Consume the `:'. */
18248 cp_lexer_consume_token (parser
->lexer
);
18250 /* Parse the type-specifier-seq. */
18251 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18252 /*is_trailing_return=*/false,
18255 /* At this point this is surely not elaborated type specifier. */
18256 if (!cp_parser_parse_definitely (parser
))
18259 if (cxx_dialect
< cxx11
)
18260 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18262 has_underlying_type
= true;
18264 /* If that didn't work, stop. */
18265 if (type_specifiers
.type
!= error_mark_node
)
18267 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18268 /*initialized=*/0, NULL
);
18269 if (underlying_type
== error_mark_node
18270 || check_for_bare_parameter_packs (underlying_type
))
18271 underlying_type
= NULL_TREE
;
18275 /* Look for the `{' but don't consume it yet. */
18276 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18278 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18280 cp_parser_error (parser
, "expected %<{%>");
18281 if (has_underlying_type
)
18287 /* An opaque-enum-specifier must have a ';' here. */
18288 if ((scoped_enum_p
|| underlying_type
)
18289 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18291 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18292 if (has_underlying_type
)
18300 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18303 if (nested_name_specifier
)
18305 if (CLASS_TYPE_P (nested_name_specifier
))
18307 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18308 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18309 push_scope (nested_name_specifier
);
18311 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18313 push_nested_namespace (nested_name_specifier
);
18317 /* Issue an error message if type-definitions are forbidden here. */
18318 if (!cp_parser_check_type_definition (parser
))
18319 type
= error_mark_node
;
18321 /* Create the new type. We do this before consuming the opening
18322 brace so the enum will be recorded as being on the line of its
18323 tag (or the 'enum' keyword, if there is no tag). */
18324 type
= start_enum (identifier
, type
, underlying_type
,
18325 attributes
, scoped_enum_p
, &is_new_type
);
18327 /* If the next token is not '{' it is an opaque-enum-specifier or an
18328 elaborated-type-specifier. */
18329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18331 timevar_push (TV_PARSE_ENUM
);
18332 if (nested_name_specifier
18333 && nested_name_specifier
!= error_mark_node
)
18335 /* The following catches invalid code such as:
18336 enum class S<int>::E { A, B, C }; */
18337 if (!processing_specialization
18338 && CLASS_TYPE_P (nested_name_specifier
)
18339 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18340 error_at (type_start_token
->location
, "cannot add an enumerator "
18341 "list to a template instantiation");
18343 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18345 error_at (type_start_token
->location
,
18346 "%<%T::%E%> has not been declared",
18347 TYPE_CONTEXT (nested_name_specifier
),
18348 nested_name_specifier
);
18349 type
= error_mark_node
;
18351 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18352 && !CLASS_TYPE_P (nested_name_specifier
))
18354 error_at (type_start_token
->location
, "nested name specifier "
18355 "%qT for enum declaration does not name a class "
18356 "or namespace", nested_name_specifier
);
18357 type
= error_mark_node
;
18359 /* If that scope does not contain the scope in which the
18360 class was originally declared, the program is invalid. */
18361 else if (prev_scope
&& !is_ancestor (prev_scope
,
18362 nested_name_specifier
))
18364 if (at_namespace_scope_p ())
18365 error_at (type_start_token
->location
,
18366 "declaration of %qD in namespace %qD which does not "
18368 type
, prev_scope
, nested_name_specifier
);
18370 error_at (type_start_token
->location
,
18371 "declaration of %qD in %qD which does not "
18373 type
, prev_scope
, nested_name_specifier
);
18374 type
= error_mark_node
;
18376 /* If that scope is the scope where the declaration is being placed
18377 the program is invalid. */
18378 else if (CLASS_TYPE_P (nested_name_specifier
)
18379 && CLASS_TYPE_P (prev_scope
)
18380 && same_type_p (nested_name_specifier
, prev_scope
))
18382 permerror (type_start_token
->location
,
18383 "extra qualification not allowed");
18384 nested_name_specifier
= NULL_TREE
;
18389 begin_scope (sk_scoped_enum
, type
);
18391 /* Consume the opening brace. */
18392 matching_braces braces
;
18393 braces
.consume_open (parser
);
18395 if (type
== error_mark_node
)
18396 ; /* Nothing to add */
18397 else if (OPAQUE_ENUM_P (type
)
18398 || (cxx_dialect
> cxx98
&& processing_specialization
))
18400 new_value_list
= true;
18401 SET_OPAQUE_ENUM_P (type
, false);
18402 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18406 error_at (type_start_token
->location
,
18407 "multiple definition of %q#T", type
);
18408 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18409 "previous definition here");
18410 type
= error_mark_node
;
18413 if (type
== error_mark_node
)
18414 cp_parser_skip_to_end_of_block_or_statement (parser
);
18415 /* If the next token is not '}', then there are some enumerators. */
18416 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18418 if (is_unnamed
&& !scoped_enum_p
)
18419 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18420 "ISO C++ forbids empty unnamed enum");
18423 cp_parser_enumerator_list (parser
, type
);
18425 /* Consume the final '}'. */
18426 braces
.require_close (parser
);
18430 timevar_pop (TV_PARSE_ENUM
);
18434 /* If a ';' follows, then it is an opaque-enum-specifier
18435 and additional restrictions apply. */
18436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18439 error_at (type_start_token
->location
,
18440 "opaque-enum-specifier without name");
18441 else if (nested_name_specifier
)
18442 error_at (type_start_token
->location
,
18443 "opaque-enum-specifier must use a simple identifier");
18447 /* Look for trailing attributes to apply to this enumeration, and
18448 apply them if appropriate. */
18449 if (cp_parser_allow_gnu_extensions_p (parser
))
18451 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18452 cplus_decl_attributes (&type
,
18454 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18457 /* Finish up the enumeration. */
18458 if (type
!= error_mark_node
)
18460 if (new_value_list
)
18461 finish_enum_value_list (type
);
18463 finish_enum (type
);
18466 if (nested_name_specifier
)
18468 if (CLASS_TYPE_P (nested_name_specifier
))
18470 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18471 pop_scope (nested_name_specifier
);
18473 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18475 pop_nested_namespace (nested_name_specifier
);
18479 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18483 /* Parse an enumerator-list. The enumerators all have the indicated
18487 enumerator-definition
18488 enumerator-list , enumerator-definition */
18491 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18495 /* Parse an enumerator-definition. */
18496 cp_parser_enumerator_definition (parser
, type
);
18498 /* If the next token is not a ',', we've reached the end of
18500 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18502 /* Otherwise, consume the `,' and keep going. */
18503 cp_lexer_consume_token (parser
->lexer
);
18504 /* If the next token is a `}', there is a trailing comma. */
18505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18507 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18508 pedwarn (input_location
, OPT_Wpedantic
,
18509 "comma at end of enumerator list");
18515 /* Parse an enumerator-definition. The enumerator has the indicated
18518 enumerator-definition:
18520 enumerator = constant-expression
18527 enumerator-definition:
18528 enumerator attributes [opt]
18529 enumerator attributes [opt] = constant-expression */
18532 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18538 /* Save the input location because we are interested in the location
18539 of the identifier and not the location of the explicit value. */
18540 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18542 /* Look for the identifier. */
18543 identifier
= cp_parser_identifier (parser
);
18544 if (identifier
== error_mark_node
)
18547 /* Parse any specified attributes. */
18548 tree attrs
= cp_parser_attributes_opt (parser
);
18550 /* If the next token is an '=', then there is an explicit value. */
18551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18553 /* Consume the `=' token. */
18554 cp_lexer_consume_token (parser
->lexer
);
18555 /* Parse the value. */
18556 value
= cp_parser_constant_expression (parser
);
18561 /* If we are processing a template, make sure the initializer of the
18562 enumerator doesn't contain any bare template parameter pack. */
18563 if (check_for_bare_parameter_packs (value
))
18564 value
= error_mark_node
;
18566 /* Create the enumerator. */
18567 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18570 /* Parse a namespace-name.
18573 original-namespace-name
18576 Returns the NAMESPACE_DECL for the namespace. */
18579 cp_parser_namespace_name (cp_parser
* parser
)
18582 tree namespace_decl
;
18584 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18586 /* Get the name of the namespace. */
18587 identifier
= cp_parser_identifier (parser
);
18588 if (identifier
== error_mark_node
)
18589 return error_mark_node
;
18591 /* Look up the identifier in the currently active scope. Look only
18592 for namespaces, due to:
18594 [basic.lookup.udir]
18596 When looking up a namespace-name in a using-directive or alias
18597 definition, only namespace names are considered.
18601 [basic.lookup.qual]
18603 During the lookup of a name preceding the :: scope resolution
18604 operator, object, function, and enumerator names are ignored.
18606 (Note that cp_parser_qualifying_entity only calls this
18607 function if the token after the name is the scope resolution
18609 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18611 /*is_template=*/false,
18612 /*is_namespace=*/true,
18613 /*check_dependency=*/true,
18614 /*ambiguous_decls=*/NULL
,
18616 /* If it's not a namespace, issue an error. */
18617 if (namespace_decl
== error_mark_node
18618 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18620 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18622 auto_diagnostic_group d
;
18623 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18624 if (namespace_decl
== error_mark_node
18625 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18626 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18629 cp_parser_error (parser
, "expected namespace-name");
18630 namespace_decl
= error_mark_node
;
18633 return namespace_decl
;
18636 /* Parse a namespace-definition.
18638 namespace-definition:
18639 named-namespace-definition
18640 unnamed-namespace-definition
18642 named-namespace-definition:
18643 original-namespace-definition
18644 extension-namespace-definition
18646 original-namespace-definition:
18647 namespace identifier { namespace-body }
18649 extension-namespace-definition:
18650 namespace original-namespace-name { namespace-body }
18652 unnamed-namespace-definition:
18653 namespace { namespace-body } */
18656 cp_parser_namespace_definition (cp_parser
* parser
)
18659 int nested_definition_count
= 0;
18661 cp_ensure_no_omp_declare_simd (parser
);
18662 cp_ensure_no_oacc_routine (parser
);
18664 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18668 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18669 cp_lexer_consume_token (parser
->lexer
);
18672 /* Look for the `namespace' keyword. */
18674 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18676 /* Parse any specified attributes before the identifier. */
18677 tree attribs
= cp_parser_attributes_opt (parser
);
18681 identifier
= NULL_TREE
;
18683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18685 identifier
= cp_parser_identifier (parser
);
18687 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18688 pedwarn (input_location
, OPT_Wpedantic
,
18689 "standard attributes on namespaces must precede "
18690 "the namespace name");
18692 /* Parse any attributes specified after the identifier. */
18693 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18699 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18700 pedwarn (input_location
, OPT_Wpedantic
,
18701 "nested namespace definitions only available with "
18702 "-std=c++17 or -std=gnu++17");
18704 /* Nested namespace names can create new namespaces (unlike
18705 other qualified-ids). */
18706 if (int count
= identifier
? push_namespace (identifier
) : 0)
18707 nested_definition_count
+= count
;
18709 cp_parser_error (parser
, "nested namespace name required");
18710 cp_lexer_consume_token (parser
->lexer
);
18713 if (nested_definition_count
&& !identifier
)
18714 cp_parser_error (parser
, "namespace name required");
18716 if (nested_definition_count
&& attribs
)
18717 error_at (token
->location
,
18718 "a nested namespace definition cannot have attributes");
18719 if (nested_definition_count
&& is_inline
)
18720 error_at (token
->location
,
18721 "a nested namespace definition cannot be inline");
18723 /* Start the namespace. */
18724 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18726 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18728 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18730 /* Look for the `{' to validate starting the namespace. */
18731 matching_braces braces
;
18732 if (braces
.require_open (parser
))
18734 /* Parse the body of the namespace. */
18735 cp_parser_namespace_body (parser
);
18737 /* Look for the final `}'. */
18738 braces
.require_close (parser
);
18741 if (has_visibility
)
18742 pop_visibility (1);
18744 /* Pop the nested namespace definitions. */
18745 while (nested_definition_count
--)
18749 /* Parse a namespace-body.
18752 declaration-seq [opt] */
18755 cp_parser_namespace_body (cp_parser
* parser
)
18757 cp_parser_declaration_seq_opt (parser
);
18760 /* Parse a namespace-alias-definition.
18762 namespace-alias-definition:
18763 namespace identifier = qualified-namespace-specifier ; */
18766 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18769 tree namespace_specifier
;
18771 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18773 /* Look for the `namespace' keyword. */
18774 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18775 /* Look for the identifier. */
18776 identifier
= cp_parser_identifier (parser
);
18777 if (identifier
== error_mark_node
)
18779 /* Look for the `=' token. */
18780 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18781 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18783 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18784 /* Skip the definition. */
18785 cp_lexer_consume_token (parser
->lexer
);
18786 if (cp_parser_skip_to_closing_brace (parser
))
18787 cp_lexer_consume_token (parser
->lexer
);
18790 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18791 /* Look for the qualified-namespace-specifier. */
18792 namespace_specifier
18793 = cp_parser_qualified_namespace_specifier (parser
);
18794 /* Look for the `;' token. */
18795 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18797 /* Register the alias in the symbol table. */
18798 do_namespace_alias (identifier
, namespace_specifier
);
18801 /* Parse a qualified-namespace-specifier.
18803 qualified-namespace-specifier:
18804 :: [opt] nested-name-specifier [opt] namespace-name
18806 Returns a NAMESPACE_DECL corresponding to the specified
18810 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18812 /* Look for the optional `::'. */
18813 cp_parser_global_scope_opt (parser
,
18814 /*current_scope_valid_p=*/false);
18816 /* Look for the optional nested-name-specifier. */
18817 cp_parser_nested_name_specifier_opt (parser
,
18818 /*typename_keyword_p=*/false,
18819 /*check_dependency_p=*/true,
18821 /*is_declaration=*/true);
18823 return cp_parser_namespace_name (parser
);
18826 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18827 access declaration.
18830 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18831 using :: unqualified-id ;
18833 access-declaration:
18839 cp_parser_using_declaration (cp_parser
* parser
,
18840 bool access_declaration_p
)
18843 bool typename_p
= false;
18844 bool global_scope_p
;
18848 int oldcount
= errorcount
;
18849 cp_token
*diag_token
= NULL
;
18851 if (access_declaration_p
)
18853 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18854 cp_parser_parse_tentatively (parser
);
18858 /* Look for the `using' keyword. */
18859 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18862 /* Peek at the next token. */
18863 token
= cp_lexer_peek_token (parser
->lexer
);
18864 /* See if it's `typename'. */
18865 if (token
->keyword
== RID_TYPENAME
)
18867 /* Remember that we've seen it. */
18869 /* Consume the `typename' token. */
18870 cp_lexer_consume_token (parser
->lexer
);
18874 /* Look for the optional global scope qualification. */
18876 = (cp_parser_global_scope_opt (parser
,
18877 /*current_scope_valid_p=*/false)
18880 /* If we saw `typename', or didn't see `::', then there must be a
18881 nested-name-specifier present. */
18882 if (typename_p
|| !global_scope_p
)
18884 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18885 /*check_dependency_p=*/true,
18887 /*is_declaration=*/true);
18888 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18890 cp_parser_skip_to_end_of_block_or_statement (parser
);
18894 /* Otherwise, we could be in either of the two productions. In that
18895 case, treat the nested-name-specifier as optional. */
18897 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18898 /*typename_keyword_p=*/false,
18899 /*check_dependency_p=*/true,
18901 /*is_declaration=*/true);
18903 qscope
= global_namespace
;
18904 else if (UNSCOPED_ENUM_P (qscope
))
18905 qscope
= CP_TYPE_CONTEXT (qscope
);
18907 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18908 /* Something has already gone wrong; there's no need to parse
18909 further. Since an error has occurred, the return value of
18910 cp_parser_parse_definitely will be false, as required. */
18911 return cp_parser_parse_definitely (parser
);
18913 token
= cp_lexer_peek_token (parser
->lexer
);
18914 /* Parse the unqualified-id. */
18915 identifier
= cp_parser_unqualified_id (parser
,
18916 /*template_keyword_p=*/false,
18917 /*check_dependency_p=*/true,
18918 /*declarator_p=*/true,
18919 /*optional_p=*/false);
18921 if (access_declaration_p
)
18923 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18924 cp_parser_simulate_error (parser
);
18925 if (!cp_parser_parse_definitely (parser
))
18928 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18930 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18931 if (cxx_dialect
< cxx17
18932 && !in_system_header_at (ell
->location
))
18933 pedwarn (ell
->location
, 0,
18934 "pack expansion in using-declaration only available "
18935 "with -std=c++17 or -std=gnu++17");
18936 qscope
= make_pack_expansion (qscope
);
18939 /* The function we call to handle a using-declaration is different
18940 depending on what scope we are in. */
18941 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18943 else if (!identifier_p (identifier
)
18944 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18945 /* [namespace.udecl]
18947 A using declaration shall not name a template-id. */
18948 error_at (token
->location
,
18949 "a template-id may not appear in a using-declaration");
18952 if (at_class_scope_p ())
18954 /* Create the USING_DECL. */
18955 decl
= do_class_using_decl (qscope
, identifier
);
18957 if (decl
&& typename_p
)
18958 USING_DECL_TYPENAME_P (decl
) = 1;
18960 if (check_for_bare_parameter_packs (decl
))
18962 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18966 /* Add it to the list of members in this class. */
18967 finish_member_declaration (decl
);
18971 decl
= cp_parser_lookup_name_simple (parser
,
18974 if (decl
== error_mark_node
)
18975 cp_parser_name_lookup_error (parser
, identifier
,
18978 else if (check_for_bare_parameter_packs (decl
))
18980 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18983 else if (!at_namespace_scope_p ())
18984 finish_local_using_decl (decl
, qscope
, identifier
);
18986 finish_namespace_using_decl (decl
, qscope
, identifier
);
18990 if (!access_declaration_p
18991 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18993 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18994 if (cxx_dialect
< cxx17
)
18995 pedwarn (comma
->location
, 0,
18996 "comma-separated list in using-declaration only available "
18997 "with -std=c++17 or -std=gnu++17");
19001 /* Look for the final `;'. */
19002 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19004 if (access_declaration_p
&& errorcount
== oldcount
)
19005 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19006 "access declarations are deprecated "
19007 "in favour of using-declarations; "
19008 "suggestion: add the %<using%> keyword");
19013 /* Parse an alias-declaration.
19016 using identifier attribute-specifier-seq [opt] = type-id */
19019 cp_parser_alias_declaration (cp_parser
* parser
)
19021 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19022 location_t id_location
, type_location
;
19023 cp_declarator
*declarator
;
19024 cp_decl_specifier_seq decl_specs
;
19026 const char *saved_message
= NULL
;
19028 /* Look for the `using' keyword. */
19029 cp_token
*using_token
19030 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19031 if (using_token
== NULL
)
19032 return error_mark_node
;
19034 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19035 id
= cp_parser_identifier (parser
);
19036 if (id
== error_mark_node
)
19037 return error_mark_node
;
19039 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19040 attributes
= cp_parser_attributes_opt (parser
);
19041 if (attributes
== error_mark_node
)
19042 return error_mark_node
;
19044 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19046 if (cp_parser_error_occurred (parser
))
19047 return error_mark_node
;
19049 cp_parser_commit_to_tentative_parse (parser
);
19051 /* Now we are going to parse the type-id of the declaration. */
19056 "A type-specifier-seq shall not define a class or enumeration
19057 unless it appears in the type-id of an alias-declaration (7.1.3) that
19058 is not the declaration of a template-declaration."
19060 In other words, if we currently are in an alias template, the
19061 type-id should not define a type.
19063 So let's set parser->type_definition_forbidden_message in that
19064 case; cp_parser_check_type_definition (called by
19065 cp_parser_class_specifier) will then emit an error if a type is
19066 defined in the type-id. */
19067 if (parser
->num_template_parameter_lists
)
19069 saved_message
= parser
->type_definition_forbidden_message
;
19070 parser
->type_definition_forbidden_message
=
19071 G_("types may not be defined in alias template declarations");
19074 type
= cp_parser_type_id (parser
, &type_location
);
19076 /* Restore the error message if need be. */
19077 if (parser
->num_template_parameter_lists
)
19078 parser
->type_definition_forbidden_message
= saved_message
;
19080 if (type
== error_mark_node
19081 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19083 cp_parser_skip_to_end_of_block_or_statement (parser
);
19084 return error_mark_node
;
19087 /* A typedef-name can also be introduced by an alias-declaration. The
19088 identifier following the using keyword becomes a typedef-name. It has
19089 the same semantics as if it were introduced by the typedef
19090 specifier. In particular, it does not define a new type and it shall
19091 not appear in the type-id. */
19093 clear_decl_specs (&decl_specs
);
19094 decl_specs
.type
= type
;
19095 if (attributes
!= NULL_TREE
)
19097 decl_specs
.attributes
= attributes
;
19098 set_and_check_decl_spec_loc (&decl_specs
,
19102 set_and_check_decl_spec_loc (&decl_specs
,
19105 set_and_check_decl_spec_loc (&decl_specs
,
19108 decl_specs
.locations
[ds_type_spec
] = type_location
;
19110 if (parser
->num_template_parameter_lists
19111 && !cp_parser_check_template_parameters (parser
,
19112 /*num_templates=*/0,
19113 /*template_id*/false,
19115 /*declarator=*/NULL
))
19116 return error_mark_node
;
19118 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19119 declarator
->id_loc
= id_location
;
19121 member_p
= at_class_scope_p ();
19123 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19124 NULL_TREE
, attributes
);
19126 decl
= start_decl (declarator
, &decl_specs
, 0,
19127 attributes
, NULL_TREE
, &pushed_scope
);
19128 if (decl
== error_mark_node
)
19131 // Attach constraints to the alias declaration.
19132 if (flag_concepts
&& current_template_parms
)
19134 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19135 tree constr
= build_constraints (reqs
, NULL_TREE
);
19136 set_constraints (decl
, constr
);
19139 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19142 pop_scope (pushed_scope
);
19144 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19145 added into the symbol table; otherwise, return the TYPE_DECL. */
19146 if (DECL_LANG_SPECIFIC (decl
)
19147 && DECL_TEMPLATE_INFO (decl
)
19148 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19150 decl
= DECL_TI_TEMPLATE (decl
);
19152 check_member_template (decl
);
19158 /* Parse a using-directive.
19161 using namespace :: [opt] nested-name-specifier [opt]
19162 namespace-name ; */
19165 cp_parser_using_directive (cp_parser
* parser
)
19167 tree namespace_decl
;
19170 /* Look for the `using' keyword. */
19171 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19172 /* And the `namespace' keyword. */
19173 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19174 /* Look for the optional `::' operator. */
19175 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19176 /* And the optional nested-name-specifier. */
19177 cp_parser_nested_name_specifier_opt (parser
,
19178 /*typename_keyword_p=*/false,
19179 /*check_dependency_p=*/true,
19181 /*is_declaration=*/true);
19182 /* Get the namespace being used. */
19183 namespace_decl
= cp_parser_namespace_name (parser
);
19184 /* And any specified attributes. */
19185 attribs
= cp_parser_attributes_opt (parser
);
19187 /* Update the symbol table. */
19188 if (namespace_bindings_p ())
19189 finish_namespace_using_directive (namespace_decl
, attribs
);
19191 finish_local_using_directive (namespace_decl
, attribs
);
19193 /* Look for the final `;'. */
19194 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19197 /* Parse an asm-definition.
19200 asm ( string-literal ) ;
19205 asm volatile [opt] ( string-literal ) ;
19206 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19207 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19208 : asm-operand-list [opt] ) ;
19209 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19210 : asm-operand-list [opt]
19211 : asm-clobber-list [opt] ) ;
19212 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19213 : asm-clobber-list [opt]
19214 : asm-goto-list ) ; */
19217 cp_parser_asm_definition (cp_parser
* parser
)
19220 tree outputs
= NULL_TREE
;
19221 tree inputs
= NULL_TREE
;
19222 tree clobbers
= NULL_TREE
;
19223 tree labels
= NULL_TREE
;
19225 bool volatile_p
= false;
19226 bool extended_p
= false;
19227 bool invalid_inputs_p
= false;
19228 bool invalid_outputs_p
= false;
19229 bool goto_p
= false;
19230 required_token missing
= RT_NONE
;
19232 /* Look for the `asm' keyword. */
19233 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19235 if (parser
->in_function_body
19236 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19238 error ("%<asm%> in %<constexpr%> function");
19239 cp_function_chain
->invalid_constexpr
= true;
19242 /* See if the next token is `volatile'. */
19243 if (cp_parser_allow_gnu_extensions_p (parser
)
19244 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19246 /* Remember that we saw the `volatile' keyword. */
19248 /* Consume the token. */
19249 cp_lexer_consume_token (parser
->lexer
);
19251 if (cp_parser_allow_gnu_extensions_p (parser
)
19252 && parser
->in_function_body
19253 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19255 /* Remember that we saw the `goto' keyword. */
19257 /* Consume the token. */
19258 cp_lexer_consume_token (parser
->lexer
);
19260 /* Look for the opening `('. */
19261 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19263 /* Look for the string. */
19264 string
= cp_parser_string_literal (parser
, false, false);
19265 if (string
== error_mark_node
)
19267 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19268 /*consume_paren=*/true);
19272 /* If we're allowing GNU extensions, check for the extended assembly
19273 syntax. Unfortunately, the `:' tokens need not be separated by
19274 a space in C, and so, for compatibility, we tolerate that here
19275 too. Doing that means that we have to treat the `::' operator as
19277 if (cp_parser_allow_gnu_extensions_p (parser
)
19278 && parser
->in_function_body
19279 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19280 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19282 bool inputs_p
= false;
19283 bool clobbers_p
= false;
19284 bool labels_p
= false;
19286 /* The extended syntax was used. */
19289 /* Look for outputs. */
19290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19292 /* Consume the `:'. */
19293 cp_lexer_consume_token (parser
->lexer
);
19294 /* Parse the output-operands. */
19295 if (cp_lexer_next_token_is_not (parser
->lexer
,
19297 && cp_lexer_next_token_is_not (parser
->lexer
,
19299 && cp_lexer_next_token_is_not (parser
->lexer
,
19303 outputs
= cp_parser_asm_operand_list (parser
);
19304 if (outputs
== error_mark_node
)
19305 invalid_outputs_p
= true;
19308 /* If the next token is `::', there are no outputs, and the
19309 next token is the beginning of the inputs. */
19310 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19311 /* The inputs are coming next. */
19314 /* Look for inputs. */
19316 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19318 /* Consume the `:' or `::'. */
19319 cp_lexer_consume_token (parser
->lexer
);
19320 /* Parse the output-operands. */
19321 if (cp_lexer_next_token_is_not (parser
->lexer
,
19323 && cp_lexer_next_token_is_not (parser
->lexer
,
19325 && cp_lexer_next_token_is_not (parser
->lexer
,
19328 inputs
= cp_parser_asm_operand_list (parser
);
19329 if (inputs
== error_mark_node
)
19330 invalid_inputs_p
= true;
19333 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19334 /* The clobbers are coming next. */
19337 /* Look for clobbers. */
19339 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19342 /* Consume the `:' or `::'. */
19343 cp_lexer_consume_token (parser
->lexer
);
19344 /* Parse the clobbers. */
19345 if (cp_lexer_next_token_is_not (parser
->lexer
,
19347 && cp_lexer_next_token_is_not (parser
->lexer
,
19349 clobbers
= cp_parser_asm_clobber_list (parser
);
19352 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19353 /* The labels are coming next. */
19356 /* Look for labels. */
19358 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19361 /* Consume the `:' or `::'. */
19362 cp_lexer_consume_token (parser
->lexer
);
19363 /* Parse the labels. */
19364 labels
= cp_parser_asm_label_list (parser
);
19367 if (goto_p
&& !labels_p
)
19368 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19371 missing
= RT_COLON_SCOPE
;
19373 /* Look for the closing `)'. */
19374 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19375 missing
? missing
: RT_CLOSE_PAREN
))
19376 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19377 /*consume_paren=*/true);
19378 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19380 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19382 /* Create the ASM_EXPR. */
19383 if (parser
->in_function_body
)
19385 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19386 inputs
, clobbers
, labels
);
19387 /* If the extended syntax was not used, mark the ASM_EXPR. */
19390 tree temp
= asm_stmt
;
19391 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19392 temp
= TREE_OPERAND (temp
, 0);
19394 ASM_INPUT_P (temp
) = 1;
19398 symtab
->finalize_toplevel_asm (string
);
19402 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19403 type that comes from the decl-specifier-seq. */
19406 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19408 for (cp_declarator
*d
= declarator
; d
;)
19418 if (TYPE_PTRMEMFUNC_P (type
))
19419 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19420 type
= TREE_TYPE (type
);
19428 /* Declarators [gram.dcl.decl] */
19430 /* Parse an init-declarator.
19433 declarator initializer [opt]
19438 declarator asm-specification [opt] attributes [opt] initializer [opt]
19440 function-definition:
19441 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19443 decl-specifier-seq [opt] declarator function-try-block
19447 function-definition:
19448 __extension__ function-definition
19452 function-definition:
19453 decl-specifier-seq [opt] declarator function-transaction-block
19455 The DECL_SPECIFIERS apply to this declarator. Returns a
19456 representation of the entity declared. If MEMBER_P is TRUE, then
19457 this declarator appears in a class scope. The new DECL created by
19458 this declarator is returned.
19460 The CHECKS are access checks that should be performed once we know
19461 what entity is being declared (and, therefore, what classes have
19464 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19465 for a function-definition here as well. If the declarator is a
19466 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19467 be TRUE upon return. By that point, the function-definition will
19468 have been completely parsed.
19470 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19473 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19474 parsed declaration if it is an uninitialized single declarator not followed
19475 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19476 if present, will not be consumed. If returned, this declarator will be
19477 created with SD_INITIALIZED but will not call cp_finish_decl.
19479 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19480 and there is an initializer, the pointed location_t is set to the
19481 location of the '=' or `(', or '{' in C++11 token introducing the
19485 cp_parser_init_declarator (cp_parser
* parser
,
19486 cp_decl_specifier_seq
*decl_specifiers
,
19487 vec
<deferred_access_check
, va_gc
> *checks
,
19488 bool function_definition_allowed_p
,
19490 int declares_class_or_enum
,
19491 bool* function_definition_p
,
19492 tree
* maybe_range_for_decl
,
19493 location_t
* init_loc
,
19496 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19497 *attributes_start_token
= NULL
;
19498 cp_declarator
*declarator
;
19499 tree prefix_attributes
;
19500 tree attributes
= NULL
;
19501 tree asm_specification
;
19503 tree decl
= NULL_TREE
;
19505 int is_initialized
;
19506 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19507 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19509 enum cpp_ttype initialization_kind
;
19510 bool is_direct_init
= false;
19511 bool is_non_constant_init
;
19512 int ctor_dtor_or_conv_p
;
19513 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19514 tree pushed_scope
= NULL_TREE
;
19515 bool range_for_decl_p
= false;
19516 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19517 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19519 /* Gather the attributes that were provided with the
19520 decl-specifiers. */
19521 prefix_attributes
= decl_specifiers
->attributes
;
19523 /* Assume that this is not the declarator for a function
19525 if (function_definition_p
)
19526 *function_definition_p
= false;
19528 /* Default arguments are only permitted for function parameters. */
19529 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19530 parser
->default_arg_ok_p
= false;
19532 /* Defer access checks while parsing the declarator; we cannot know
19533 what names are accessible until we know what is being
19535 resume_deferring_access_checks ();
19537 token
= cp_lexer_peek_token (parser
->lexer
);
19539 /* Parse the declarator. */
19541 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19542 &ctor_dtor_or_conv_p
,
19543 /*parenthesized_p=*/NULL
,
19544 member_p
, friend_p
);
19545 /* Gather up the deferred checks. */
19546 stop_deferring_access_checks ();
19548 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19550 /* If the DECLARATOR was erroneous, there's no need to go
19552 if (declarator
== cp_error_declarator
)
19553 return error_mark_node
;
19555 /* Check that the number of template-parameter-lists is OK. */
19556 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19558 return error_mark_node
;
19560 if (declares_class_or_enum
& 2)
19561 cp_parser_check_for_definition_in_return_type (declarator
,
19562 decl_specifiers
->type
,
19563 decl_specifiers
->locations
[ds_type_spec
]);
19565 /* Figure out what scope the entity declared by the DECLARATOR is
19566 located in. `grokdeclarator' sometimes changes the scope, so
19567 we compute it now. */
19568 scope
= get_scope_of_declarator (declarator
);
19570 /* Perform any lookups in the declared type which were thought to be
19571 dependent, but are not in the scope of the declarator. */
19572 decl_specifiers
->type
19573 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19575 /* If we're allowing GNU extensions, look for an
19576 asm-specification. */
19577 if (cp_parser_allow_gnu_extensions_p (parser
))
19579 /* Look for an asm-specification. */
19580 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19581 asm_specification
= cp_parser_asm_specification_opt (parser
);
19584 asm_specification
= NULL_TREE
;
19586 /* Look for attributes. */
19587 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19588 attributes
= cp_parser_attributes_opt (parser
);
19590 /* Peek at the next token. */
19591 token
= cp_lexer_peek_token (parser
->lexer
);
19593 bool bogus_implicit_tmpl
= false;
19595 if (function_declarator_p (declarator
))
19597 /* Handle C++17 deduction guides. */
19598 if (!decl_specifiers
->type
19599 && ctor_dtor_or_conv_p
<= 0
19600 && cxx_dialect
>= cxx17
)
19602 cp_declarator
*id
= get_id_declarator (declarator
);
19603 tree name
= id
->u
.id
.unqualified_name
;
19604 parser
->scope
= id
->u
.id
.qualifying_scope
;
19605 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19607 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19608 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19610 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19611 id
->u
.id
.sfk
= sfk_deduction_guide
;
19612 ctor_dtor_or_conv_p
= 1;
19616 /* Check to see if the token indicates the start of a
19617 function-definition. */
19618 if (cp_parser_token_starts_function_definition_p (token
))
19620 if (!function_definition_allowed_p
)
19622 /* If a function-definition should not appear here, issue an
19624 cp_parser_error (parser
,
19625 "a function-definition is not allowed here");
19626 return error_mark_node
;
19629 location_t func_brace_location
19630 = cp_lexer_peek_token (parser
->lexer
)->location
;
19632 /* Neither attributes nor an asm-specification are allowed
19633 on a function-definition. */
19634 if (asm_specification
)
19635 error_at (asm_spec_start_token
->location
,
19636 "an asm-specification is not allowed "
19637 "on a function-definition");
19639 error_at (attributes_start_token
->location
,
19640 "attributes are not allowed "
19641 "on a function-definition");
19642 /* This is a function-definition. */
19643 *function_definition_p
= true;
19645 /* Parse the function definition. */
19647 decl
= cp_parser_save_member_function_body (parser
,
19650 prefix_attributes
);
19653 (cp_parser_function_definition_from_specifiers_and_declarator
19654 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19656 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19658 /* This is where the prologue starts... */
19659 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19660 = func_brace_location
;
19666 else if (parser
->fully_implicit_function_template_p
)
19668 /* A non-template declaration involving a function parameter list
19669 containing an implicit template parameter will be made into a
19670 template. If the resulting declaration is not going to be an
19671 actual function then finish the template scope here to prevent it.
19672 An error message will be issued once we have a decl to talk about.
19674 FIXME probably we should do type deduction rather than create an
19675 implicit template, but the standard currently doesn't allow it. */
19676 bogus_implicit_tmpl
= true;
19677 finish_fully_implicit_template (parser
, NULL_TREE
);
19682 Only in function declarations for constructors, destructors, type
19683 conversions, and deduction guides can the decl-specifier-seq be omitted.
19685 We explicitly postpone this check past the point where we handle
19686 function-definitions because we tolerate function-definitions
19687 that are missing their return types in some modes. */
19688 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19690 cp_parser_error (parser
,
19691 "expected constructor, destructor, or type conversion");
19692 return error_mark_node
;
19695 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19696 if (token
->type
== CPP_EQ
19697 || token
->type
== CPP_OPEN_PAREN
19698 || token
->type
== CPP_OPEN_BRACE
)
19700 is_initialized
= SD_INITIALIZED
;
19701 initialization_kind
= token
->type
;
19702 if (maybe_range_for_decl
)
19703 *maybe_range_for_decl
= error_mark_node
;
19704 tmp_init_loc
= token
->location
;
19705 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19706 *init_loc
= tmp_init_loc
;
19708 if (token
->type
== CPP_EQ
19709 && function_declarator_p (declarator
))
19711 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19712 if (t2
->keyword
== RID_DEFAULT
)
19713 is_initialized
= SD_DEFAULTED
;
19714 else if (t2
->keyword
== RID_DELETE
)
19715 is_initialized
= SD_DELETED
;
19720 /* If the init-declarator isn't initialized and isn't followed by a
19721 `,' or `;', it's not a valid init-declarator. */
19722 if (token
->type
!= CPP_COMMA
19723 && token
->type
!= CPP_SEMICOLON
)
19725 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19726 range_for_decl_p
= true;
19729 if (!maybe_range_for_decl
)
19730 cp_parser_error (parser
, "expected initializer");
19731 return error_mark_node
;
19734 is_initialized
= SD_UNINITIALIZED
;
19735 initialization_kind
= CPP_EOF
;
19738 /* Because start_decl has side-effects, we should only call it if we
19739 know we're going ahead. By this point, we know that we cannot
19740 possibly be looking at any other construct. */
19741 cp_parser_commit_to_tentative_parse (parser
);
19743 /* Enter the newly declared entry in the symbol table. If we're
19744 processing a declaration in a class-specifier, we wait until
19745 after processing the initializer. */
19748 if (parser
->in_unbraced_linkage_specification_p
)
19749 decl_specifiers
->storage_class
= sc_extern
;
19750 decl
= start_decl (declarator
, decl_specifiers
,
19751 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19752 attributes
, prefix_attributes
, &pushed_scope
);
19753 cp_finalize_omp_declare_simd (parser
, decl
);
19754 cp_finalize_oacc_routine (parser
, decl
, false);
19755 /* Adjust location of decl if declarator->id_loc is more appropriate:
19756 set, and decl wasn't merged with another decl, in which case its
19757 location would be different from input_location, and more accurate. */
19759 && declarator
->id_loc
!= UNKNOWN_LOCATION
19760 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19761 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19764 /* Enter the SCOPE. That way unqualified names appearing in the
19765 initializer will be looked up in SCOPE. */
19766 pushed_scope
= push_scope (scope
);
19768 /* Perform deferred access control checks, now that we know in which
19769 SCOPE the declared entity resides. */
19770 if (!member_p
&& decl
)
19772 tree saved_current_function_decl
= NULL_TREE
;
19774 /* If the entity being declared is a function, pretend that we
19775 are in its scope. If it is a `friend', it may have access to
19776 things that would not otherwise be accessible. */
19777 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19779 saved_current_function_decl
= current_function_decl
;
19780 current_function_decl
= decl
;
19783 /* Perform access checks for template parameters. */
19784 cp_parser_perform_template_parameter_access_checks (checks
);
19786 /* Perform the access control checks for the declarator and the
19787 decl-specifiers. */
19788 perform_deferred_access_checks (tf_warning_or_error
);
19790 /* Restore the saved value. */
19791 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19792 current_function_decl
= saved_current_function_decl
;
19795 /* Parse the initializer. */
19796 initializer
= NULL_TREE
;
19797 is_direct_init
= false;
19798 is_non_constant_init
= true;
19799 if (is_initialized
)
19801 if (function_declarator_p (declarator
))
19803 if (initialization_kind
== CPP_EQ
)
19804 initializer
= cp_parser_pure_specifier (parser
);
19807 /* If the declaration was erroneous, we don't really
19808 know what the user intended, so just silently
19809 consume the initializer. */
19810 if (decl
!= error_mark_node
)
19811 error_at (tmp_init_loc
, "initializer provided for function");
19812 cp_parser_skip_to_closing_parenthesis (parser
,
19813 /*recovering=*/true,
19814 /*or_comma=*/false,
19815 /*consume_paren=*/true);
19820 /* We want to record the extra mangling scope for in-class
19821 initializers of class members and initializers of static data
19822 member templates. The former involves deferring
19823 parsing of the initializer until end of class as with default
19824 arguments. So right here we only handle the latter. */
19825 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19826 start_lambda_scope (decl
);
19827 initializer
= cp_parser_initializer (parser
,
19829 &is_non_constant_init
);
19830 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19831 finish_lambda_scope ();
19832 if (initializer
== error_mark_node
)
19833 cp_parser_skip_to_end_of_statement (parser
);
19837 /* The old parser allows attributes to appear after a parenthesized
19838 initializer. Mark Mitchell proposed removing this functionality
19839 on the GCC mailing lists on 2002-08-13. This parser accepts the
19840 attributes -- but ignores them. Made a permerror in GCC 8. */
19841 if (cp_parser_allow_gnu_extensions_p (parser
)
19842 && initialization_kind
== CPP_OPEN_PAREN
19843 && cp_parser_attributes_opt (parser
)
19844 && permerror (input_location
,
19845 "attributes after parenthesized initializer ignored"))
19848 if (flag_permissive
&& !hint
)
19851 inform (input_location
,
19852 "this flexibility is deprecated and will be removed");
19856 /* And now complain about a non-function implicit template. */
19857 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19858 error_at (DECL_SOURCE_LOCATION (decl
),
19859 "non-function %qD declared as implicit template", decl
);
19861 /* For an in-class declaration, use `grokfield' to create the
19867 pop_scope (pushed_scope
);
19868 pushed_scope
= NULL_TREE
;
19870 decl
= grokfield (declarator
, decl_specifiers
,
19871 initializer
, !is_non_constant_init
,
19872 /*asmspec=*/NULL_TREE
,
19873 attr_chainon (attributes
, prefix_attributes
));
19874 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19875 cp_parser_save_default_args (parser
, decl
);
19876 cp_finalize_omp_declare_simd (parser
, decl
);
19877 cp_finalize_oacc_routine (parser
, decl
, false);
19880 /* Finish processing the declaration. But, skip member
19882 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19884 cp_finish_decl (decl
,
19885 initializer
, !is_non_constant_init
,
19887 /* If the initializer is in parentheses, then this is
19888 a direct-initialization, which means that an
19889 `explicit' constructor is OK. Otherwise, an
19890 `explicit' constructor cannot be used. */
19891 ((is_direct_init
|| !is_initialized
)
19892 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19894 else if ((cxx_dialect
!= cxx98
) && friend_p
19895 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19896 /* Core issue #226 (C++0x only): A default template-argument
19897 shall not be specified in a friend class template
19899 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19900 /*is_partial=*/false, /*is_friend_decl=*/1);
19902 if (!friend_p
&& pushed_scope
)
19903 pop_scope (pushed_scope
);
19905 if (function_declarator_p (declarator
)
19906 && parser
->fully_implicit_function_template_p
)
19909 decl
= finish_fully_implicit_template (parser
, decl
);
19911 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19914 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19915 && type_uses_auto (decl_specifiers
->type
))
19916 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19921 /* Parse a declarator.
19925 ptr-operator declarator
19927 abstract-declarator:
19928 ptr-operator abstract-declarator [opt]
19929 direct-abstract-declarator
19934 attributes [opt] direct-declarator
19935 attributes [opt] ptr-operator declarator
19937 abstract-declarator:
19938 attributes [opt] ptr-operator abstract-declarator [opt]
19939 attributes [opt] direct-abstract-declarator
19941 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19942 detect constructors, destructors, deduction guides, or conversion operators.
19943 It is set to -1 if the declarator is a name, and +1 if it is a
19944 function. Otherwise it is set to zero. Usually you just want to
19945 test for >0, but internally the negative value is used.
19947 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19948 a decl-specifier-seq unless it declares a constructor, destructor,
19949 or conversion. It might seem that we could check this condition in
19950 semantic analysis, rather than parsing, but that makes it difficult
19951 to handle something like `f()'. We want to notice that there are
19952 no decl-specifiers, and therefore realize that this is an
19953 expression, not a declaration.)
19955 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19956 the declarator is a direct-declarator of the form "(...)".
19958 MEMBER_P is true iff this declarator is a member-declarator.
19960 FRIEND_P is true iff this declarator is a friend. */
19962 static cp_declarator
*
19963 cp_parser_declarator (cp_parser
* parser
,
19964 cp_parser_declarator_kind dcl_kind
,
19965 int* ctor_dtor_or_conv_p
,
19966 bool* parenthesized_p
,
19967 bool member_p
, bool friend_p
)
19969 cp_declarator
*declarator
;
19970 enum tree_code code
;
19971 cp_cv_quals cv_quals
;
19973 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19975 /* Assume this is not a constructor, destructor, or type-conversion
19977 if (ctor_dtor_or_conv_p
)
19978 *ctor_dtor_or_conv_p
= 0;
19980 if (cp_parser_allow_gnu_extensions_p (parser
))
19981 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19983 /* Check for the ptr-operator production. */
19984 cp_parser_parse_tentatively (parser
);
19985 /* Parse the ptr-operator. */
19986 code
= cp_parser_ptr_operator (parser
,
19991 /* If that worked, then we have a ptr-operator. */
19992 if (cp_parser_parse_definitely (parser
))
19994 /* If a ptr-operator was found, then this declarator was not
19996 if (parenthesized_p
)
19997 *parenthesized_p
= true;
19998 /* The dependent declarator is optional if we are parsing an
19999 abstract-declarator. */
20000 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20001 cp_parser_parse_tentatively (parser
);
20003 /* Parse the dependent declarator. */
20004 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20005 /*ctor_dtor_or_conv_p=*/NULL
,
20006 /*parenthesized_p=*/NULL
,
20007 /*member_p=*/false,
20010 /* If we are parsing an abstract-declarator, we must handle the
20011 case where the dependent declarator is absent. */
20012 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20013 && !cp_parser_parse_definitely (parser
))
20016 declarator
= cp_parser_make_indirect_declarator
20017 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20019 /* Everything else is a direct-declarator. */
20022 if (parenthesized_p
)
20023 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20025 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20026 ctor_dtor_or_conv_p
,
20027 member_p
, friend_p
);
20030 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20031 declarator
->attributes
= gnu_attributes
;
20035 /* Parse a direct-declarator or direct-abstract-declarator.
20039 direct-declarator ( parameter-declaration-clause )
20040 cv-qualifier-seq [opt]
20041 ref-qualifier [opt]
20042 exception-specification [opt]
20043 direct-declarator [ constant-expression [opt] ]
20046 direct-abstract-declarator:
20047 direct-abstract-declarator [opt]
20048 ( parameter-declaration-clause )
20049 cv-qualifier-seq [opt]
20050 ref-qualifier [opt]
20051 exception-specification [opt]
20052 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20053 ( abstract-declarator )
20055 Returns a representation of the declarator. DCL_KIND is
20056 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20057 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20058 we are parsing a direct-declarator. It is
20059 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20060 of ambiguity we prefer an abstract declarator, as per
20061 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20062 as for cp_parser_declarator. */
20064 static cp_declarator
*
20065 cp_parser_direct_declarator (cp_parser
* parser
,
20066 cp_parser_declarator_kind dcl_kind
,
20067 int* ctor_dtor_or_conv_p
,
20068 bool member_p
, bool friend_p
)
20071 cp_declarator
*declarator
= NULL
;
20072 tree scope
= NULL_TREE
;
20073 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20074 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20076 tree pushed_scope
= NULL_TREE
;
20077 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20081 /* Peek at the next token. */
20082 token
= cp_lexer_peek_token (parser
->lexer
);
20083 if (token
->type
== CPP_OPEN_PAREN
)
20085 /* This is either a parameter-declaration-clause, or a
20086 parenthesized declarator. When we know we are parsing a
20087 named declarator, it must be a parenthesized declarator
20088 if FIRST is true. For instance, `(int)' is a
20089 parameter-declaration-clause, with an omitted
20090 direct-abstract-declarator. But `((*))', is a
20091 parenthesized abstract declarator. Finally, when T is a
20092 template parameter `(T)' is a
20093 parameter-declaration-clause, and not a parenthesized
20096 We first try and parse a parameter-declaration-clause,
20097 and then try a nested declarator (if FIRST is true).
20099 It is not an error for it not to be a
20100 parameter-declaration-clause, even when FIRST is
20106 The first is the declaration of a function while the
20107 second is the definition of a variable, including its
20110 Having seen only the parenthesis, we cannot know which of
20111 these two alternatives should be selected. Even more
20112 complex are examples like:
20117 The former is a function-declaration; the latter is a
20118 variable initialization.
20120 Thus again, we try a parameter-declaration-clause, and if
20121 that fails, we back out and return. */
20123 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20126 bool is_declarator
= false;
20130 /* In a member-declarator, the only valid interpretation
20131 of a parenthesis is the start of a
20132 parameter-declaration-clause. (It is invalid to
20133 initialize a static data member with a parenthesized
20134 initializer; only the "=" form of initialization is
20137 cp_parser_parse_tentatively (parser
);
20139 /* Consume the `('. */
20140 matching_parens parens
;
20141 parens
.consume_open (parser
);
20144 /* If this is going to be an abstract declarator, we're
20145 in a declarator and we can't have default args. */
20146 parser
->default_arg_ok_p
= false;
20147 parser
->in_declarator_p
= true;
20150 begin_scope (sk_function_parms
, NULL_TREE
);
20152 /* Parse the parameter-declaration-clause. */
20153 params
= cp_parser_parameter_declaration_clause (parser
);
20155 /* Consume the `)'. */
20156 parens
.require_close (parser
);
20158 /* If all went well, parse the cv-qualifier-seq,
20159 ref-qualifier and the exception-specification. */
20160 if (member_p
|| cp_parser_parse_definitely (parser
))
20162 cp_cv_quals cv_quals
;
20163 cp_virt_specifiers virt_specifiers
;
20164 cp_ref_qualifier ref_qual
;
20165 tree exception_specification
;
20168 bool memfn
= (member_p
|| (pushed_scope
20169 && CLASS_TYPE_P (pushed_scope
)));
20171 is_declarator
= true;
20173 if (ctor_dtor_or_conv_p
)
20174 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20177 /* Parse the cv-qualifier-seq. */
20178 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20179 /* Parse the ref-qualifier. */
20180 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20181 /* Parse the tx-qualifier. */
20182 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20183 /* And the exception-specification. */
20184 exception_specification
20185 = cp_parser_exception_specification_opt (parser
);
20187 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20189 /* In here, we handle cases where attribute is used after
20190 the function declaration. For example:
20191 void func (int x) __attribute__((vector(..))); */
20192 tree gnu_attrs
= NULL_TREE
;
20193 tree requires_clause
= NULL_TREE
;
20194 late_return
= (cp_parser_late_return_type_opt
20195 (parser
, declarator
, requires_clause
,
20196 memfn
? cv_quals
: -1));
20198 /* Parse the virt-specifier-seq. */
20199 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20201 /* Create the function-declarator. */
20202 declarator
= make_call_declarator (declarator
,
20208 exception_specification
,
20211 declarator
->std_attributes
= attrs
;
20212 declarator
->attributes
= gnu_attrs
;
20213 /* Any subsequent parameter lists are to do with
20214 return type, so are not those of the declared
20216 parser
->default_arg_ok_p
= false;
20219 /* Remove the function parms from scope. */
20220 pop_bindings_and_leave_scope ();
20223 /* Repeat the main loop. */
20227 /* If this is the first, we can try a parenthesized
20231 bool saved_in_type_id_in_expr_p
;
20233 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20234 parser
->in_declarator_p
= saved_in_declarator_p
;
20236 open_paren
= token
;
20237 /* Consume the `('. */
20238 matching_parens parens
;
20239 parens
.consume_open (parser
);
20240 /* Parse the nested declarator. */
20241 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20242 parser
->in_type_id_in_expr_p
= true;
20244 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20245 /*parenthesized_p=*/NULL
,
20246 member_p
, friend_p
);
20247 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20249 /* Expect a `)'. */
20250 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20251 if (!parens
.require_close (parser
))
20252 declarator
= cp_error_declarator
;
20253 if (declarator
== cp_error_declarator
)
20256 goto handle_declarator
;
20258 /* Otherwise, we must be done. */
20262 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20263 && token
->type
== CPP_OPEN_SQUARE
20264 && !cp_next_tokens_can_be_attribute_p (parser
))
20266 /* Parse an array-declarator. */
20267 tree bounds
, attrs
;
20269 if (ctor_dtor_or_conv_p
)
20270 *ctor_dtor_or_conv_p
= 0;
20274 parser
->default_arg_ok_p
= false;
20275 parser
->in_declarator_p
= true;
20276 /* Consume the `['. */
20277 cp_lexer_consume_token (parser
->lexer
);
20278 /* Peek at the next token. */
20279 token
= cp_lexer_peek_token (parser
->lexer
);
20280 /* If the next token is `]', then there is no
20281 constant-expression. */
20282 if (token
->type
!= CPP_CLOSE_SQUARE
)
20284 bool non_constant_p
;
20286 = cp_parser_constant_expression (parser
,
20287 /*allow_non_constant=*/true,
20289 if (!non_constant_p
)
20291 else if (error_operand_p (bounds
))
20292 /* Already gave an error. */;
20293 else if (!parser
->in_function_body
20294 || current_binding_level
->kind
== sk_function_parms
)
20296 /* Normally, the array bound must be an integral constant
20297 expression. However, as an extension, we allow VLAs
20298 in function scopes as long as they aren't part of a
20299 parameter declaration. */
20300 cp_parser_error (parser
,
20301 "array bound is not an integer constant");
20302 bounds
= error_mark_node
;
20304 else if (processing_template_decl
20305 && !type_dependent_expression_p (bounds
))
20307 /* Remember this wasn't a constant-expression. */
20308 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20309 TREE_SIDE_EFFECTS (bounds
) = 1;
20313 bounds
= NULL_TREE
;
20314 /* Look for the closing `]'. */
20315 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20317 declarator
= cp_error_declarator
;
20321 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20322 declarator
= make_array_declarator (declarator
, bounds
);
20323 declarator
->std_attributes
= attrs
;
20325 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20328 tree qualifying_scope
;
20329 tree unqualified_name
;
20331 special_function_kind sfk
;
20333 bool pack_expansion_p
= false;
20334 cp_token
*declarator_id_start_token
;
20336 /* Parse a declarator-id */
20337 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20340 cp_parser_parse_tentatively (parser
);
20342 /* If we see an ellipsis, we should be looking at a
20344 if (token
->type
== CPP_ELLIPSIS
)
20346 /* Consume the `...' */
20347 cp_lexer_consume_token (parser
->lexer
);
20349 pack_expansion_p
= true;
20353 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20355 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20356 qualifying_scope
= parser
->scope
;
20361 if (!unqualified_name
&& pack_expansion_p
)
20363 /* Check whether an error occurred. */
20364 okay
= !cp_parser_error_occurred (parser
);
20366 /* We already consumed the ellipsis to mark a
20367 parameter pack, but we have no way to report it,
20368 so abort the tentative parse. We will be exiting
20369 immediately anyway. */
20370 cp_parser_abort_tentative_parse (parser
);
20373 okay
= cp_parser_parse_definitely (parser
);
20376 unqualified_name
= error_mark_node
;
20377 else if (unqualified_name
20378 && (qualifying_scope
20379 || (!identifier_p (unqualified_name
))))
20381 cp_parser_error (parser
, "expected unqualified-id");
20382 unqualified_name
= error_mark_node
;
20386 if (!unqualified_name
)
20388 if (unqualified_name
== error_mark_node
)
20390 declarator
= cp_error_declarator
;
20391 pack_expansion_p
= false;
20392 declarator
->parameter_pack_p
= false;
20396 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20398 if (qualifying_scope
&& at_namespace_scope_p ()
20399 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20401 /* In the declaration of a member of a template class
20402 outside of the class itself, the SCOPE will sometimes
20403 be a TYPENAME_TYPE. For example, given:
20405 template <typename T>
20406 int S<T>::R::i = 3;
20408 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20409 this context, we must resolve S<T>::R to an ordinary
20410 type, rather than a typename type.
20412 The reason we normally avoid resolving TYPENAME_TYPEs
20413 is that a specialization of `S' might render
20414 `S<T>::R' not a type. However, if `S' is
20415 specialized, then this `i' will not be used, so there
20416 is no harm in resolving the types here. */
20419 /* Resolve the TYPENAME_TYPE. */
20420 type
= resolve_typename_type (qualifying_scope
,
20421 /*only_current_p=*/false);
20422 /* If that failed, the declarator is invalid. */
20423 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20425 if (typedef_variant_p (type
))
20426 error_at (declarator_id_start_token
->location
,
20427 "cannot define member of dependent typedef "
20430 error_at (declarator_id_start_token
->location
,
20431 "%<%T::%E%> is not a type",
20432 TYPE_CONTEXT (qualifying_scope
),
20433 TYPE_IDENTIFIER (qualifying_scope
));
20435 qualifying_scope
= type
;
20440 if (unqualified_name
)
20444 if (qualifying_scope
20445 && CLASS_TYPE_P (qualifying_scope
))
20446 class_type
= qualifying_scope
;
20448 class_type
= current_class_type
;
20450 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20452 tree name_type
= TREE_TYPE (unqualified_name
);
20454 if (!class_type
|| !same_type_p (name_type
, class_type
))
20456 /* We do not attempt to print the declarator
20457 here because we do not have enough
20458 information about its original syntactic
20460 cp_parser_error (parser
, "invalid declarator");
20461 declarator
= cp_error_declarator
;
20464 else if (qualifying_scope
20465 && CLASSTYPE_USE_TEMPLATE (name_type
))
20467 error_at (declarator_id_start_token
->location
,
20468 "invalid use of constructor as a template");
20469 inform (declarator_id_start_token
->location
,
20470 "use %<%T::%D%> instead of %<%T::%D%> to "
20471 "name the constructor in a qualified name",
20473 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20474 class_type
, name_type
);
20475 declarator
= cp_error_declarator
;
20478 unqualified_name
= constructor_name (class_type
);
20483 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20484 sfk
= sfk_destructor
;
20485 else if (identifier_p (unqualified_name
)
20486 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20487 sfk
= sfk_conversion
;
20488 else if (/* There's no way to declare a constructor
20489 for an unnamed type, even if the type
20490 got a name for linkage purposes. */
20491 !TYPE_WAS_UNNAMED (class_type
)
20492 /* Handle correctly (c++/19200):
20506 friend void N::S();
20508 && (!friend_p
|| class_type
== qualifying_scope
)
20509 && constructor_name_p (unqualified_name
,
20511 sfk
= sfk_constructor
;
20512 else if (is_overloaded_fn (unqualified_name
)
20513 && DECL_CONSTRUCTOR_P (get_first_fn
20514 (unqualified_name
)))
20515 sfk
= sfk_constructor
;
20517 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20518 *ctor_dtor_or_conv_p
= -1;
20521 declarator
= make_id_declarator (qualifying_scope
,
20524 declarator
->std_attributes
= attrs
;
20525 declarator
->id_loc
= token
->location
;
20526 declarator
->parameter_pack_p
= pack_expansion_p
;
20528 if (pack_expansion_p
)
20529 maybe_warn_variadic_templates ();
20532 handle_declarator
:;
20533 scope
= get_scope_of_declarator (declarator
);
20536 /* Any names that appear after the declarator-id for a
20537 member are looked up in the containing scope. */
20538 if (at_function_scope_p ())
20540 /* But declarations with qualified-ids can't appear in a
20542 cp_parser_error (parser
, "qualified-id in declaration");
20543 declarator
= cp_error_declarator
;
20546 pushed_scope
= push_scope (scope
);
20548 parser
->in_declarator_p
= true;
20549 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20550 || (declarator
&& declarator
->kind
== cdk_id
))
20551 /* Default args are only allowed on function
20553 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20555 parser
->default_arg_ok_p
= false;
20564 /* For an abstract declarator, we might wind up with nothing at this
20565 point. That's an error; the declarator is not optional. */
20567 cp_parser_error (parser
, "expected declarator");
20568 else if (open_paren
)
20570 /* Record overly parenthesized declarator so we can give a
20571 diagnostic about confusing decl/expr disambiguation. */
20572 if (declarator
->kind
== cdk_array
)
20574 /* If the open and close parens are on different lines, this
20575 is probably a formatting thing, so ignore. */
20576 expanded_location open
= expand_location (open_paren
->location
);
20577 expanded_location close
= expand_location (close_paren
->location
);
20578 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20582 declarator
->parenthesized
= open_paren
->location
;
20585 /* If we entered a scope, we must exit it now. */
20587 pop_scope (pushed_scope
);
20589 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20590 parser
->in_declarator_p
= saved_in_declarator_p
;
20595 /* Parse a ptr-operator.
20598 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20599 * cv-qualifier-seq [opt]
20601 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20602 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20607 & cv-qualifier-seq [opt]
20609 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20610 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20611 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20612 filled in with the TYPE containing the member. *CV_QUALS is
20613 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20614 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20615 Note that the tree codes returned by this function have nothing
20616 to do with the types of trees that will be eventually be created
20617 to represent the pointer or reference type being parsed. They are
20618 just constants with suggestive names. */
20619 static enum tree_code
20620 cp_parser_ptr_operator (cp_parser
* parser
,
20622 cp_cv_quals
*cv_quals
,
20625 enum tree_code code
= ERROR_MARK
;
20627 tree attrs
= NULL_TREE
;
20629 /* Assume that it's not a pointer-to-member. */
20631 /* And that there are no cv-qualifiers. */
20632 *cv_quals
= TYPE_UNQUALIFIED
;
20634 /* Peek at the next token. */
20635 token
= cp_lexer_peek_token (parser
->lexer
);
20637 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20638 if (token
->type
== CPP_MULT
)
20639 code
= INDIRECT_REF
;
20640 else if (token
->type
== CPP_AND
)
20642 else if ((cxx_dialect
!= cxx98
) &&
20643 token
->type
== CPP_AND_AND
) /* C++0x only */
20644 code
= NON_LVALUE_EXPR
;
20646 if (code
!= ERROR_MARK
)
20648 /* Consume the `*', `&' or `&&'. */
20649 cp_lexer_consume_token (parser
->lexer
);
20651 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20652 `&', if we are allowing GNU extensions. (The only qualifier
20653 that can legally appear after `&' is `restrict', but that is
20654 enforced during semantic analysis. */
20655 if (code
== INDIRECT_REF
20656 || cp_parser_allow_gnu_extensions_p (parser
))
20657 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20659 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20660 if (attributes
!= NULL
)
20661 *attributes
= attrs
;
20665 /* Try the pointer-to-member case. */
20666 cp_parser_parse_tentatively (parser
);
20667 /* Look for the optional `::' operator. */
20668 cp_parser_global_scope_opt (parser
,
20669 /*current_scope_valid_p=*/false);
20670 /* Look for the nested-name specifier. */
20671 token
= cp_lexer_peek_token (parser
->lexer
);
20672 cp_parser_nested_name_specifier (parser
,
20673 /*typename_keyword_p=*/false,
20674 /*check_dependency_p=*/true,
20676 /*is_declaration=*/false);
20677 /* If we found it, and the next token is a `*', then we are
20678 indeed looking at a pointer-to-member operator. */
20679 if (!cp_parser_error_occurred (parser
)
20680 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20682 /* Indicate that the `*' operator was used. */
20683 code
= INDIRECT_REF
;
20685 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20686 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20687 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20688 error_at (token
->location
, "cannot form pointer to member of "
20689 "non-class %q#T", parser
->scope
);
20692 /* The type of which the member is a member is given by the
20694 *type
= parser
->scope
;
20695 /* The next name will not be qualified. */
20696 parser
->scope
= NULL_TREE
;
20697 parser
->qualifying_scope
= NULL_TREE
;
20698 parser
->object_scope
= NULL_TREE
;
20699 /* Look for optional c++11 attributes. */
20700 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20701 if (attributes
!= NULL
)
20702 *attributes
= attrs
;
20703 /* Look for the optional cv-qualifier-seq. */
20704 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20707 /* If that didn't work we don't have a ptr-operator. */
20708 if (!cp_parser_parse_definitely (parser
))
20709 cp_parser_error (parser
, "expected ptr-operator");
20715 /* Parse an (optional) cv-qualifier-seq.
20718 cv-qualifier cv-qualifier-seq [opt]
20729 Returns a bitmask representing the cv-qualifiers. */
20732 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20734 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20739 cp_cv_quals cv_qualifier
;
20741 /* Peek at the next token. */
20742 token
= cp_lexer_peek_token (parser
->lexer
);
20743 /* See if it's a cv-qualifier. */
20744 switch (token
->keyword
)
20747 cv_qualifier
= TYPE_QUAL_CONST
;
20751 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20755 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20759 cv_qualifier
= TYPE_UNQUALIFIED
;
20766 if (cv_quals
& cv_qualifier
)
20768 gcc_rich_location
richloc (token
->location
);
20769 richloc
.add_fixit_remove ();
20770 error_at (&richloc
, "duplicate cv-qualifier");
20771 cp_lexer_purge_token (parser
->lexer
);
20775 cp_lexer_consume_token (parser
->lexer
);
20776 cv_quals
|= cv_qualifier
;
20783 /* Parse an (optional) ref-qualifier
20789 Returns cp_ref_qualifier representing ref-qualifier. */
20791 static cp_ref_qualifier
20792 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20794 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20796 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20797 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20802 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20803 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20805 switch (token
->type
)
20808 curr_ref_qual
= REF_QUAL_LVALUE
;
20812 curr_ref_qual
= REF_QUAL_RVALUE
;
20816 curr_ref_qual
= REF_QUAL_NONE
;
20820 if (!curr_ref_qual
)
20824 error_at (token
->location
, "multiple ref-qualifiers");
20825 cp_lexer_purge_token (parser
->lexer
);
20829 ref_qual
= curr_ref_qual
;
20830 cp_lexer_consume_token (parser
->lexer
);
20837 /* Parse an optional tx-qualifier.
20841 transaction_safe_dynamic */
20844 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20847 if (token
->type
== CPP_NAME
)
20849 tree name
= token
->u
.value
;
20850 const char *p
= IDENTIFIER_POINTER (name
);
20851 const int len
= strlen ("transaction_safe");
20852 if (!strncmp (p
, "transaction_safe", len
))
20856 || !strcmp (p
, "_dynamic"))
20858 cp_lexer_consume_token (parser
->lexer
);
20861 error ("%qE requires %<-fgnu-tm%>", name
);
20872 /* Parse an (optional) virt-specifier-seq.
20874 virt-specifier-seq:
20875 virt-specifier virt-specifier-seq [opt]
20881 Returns a bitmask representing the virt-specifiers. */
20883 static cp_virt_specifiers
20884 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20886 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20891 cp_virt_specifiers virt_specifier
;
20893 /* Peek at the next token. */
20894 token
= cp_lexer_peek_token (parser
->lexer
);
20895 /* See if it's a virt-specifier-qualifier. */
20896 if (token
->type
!= CPP_NAME
)
20898 if (id_equal (token
->u
.value
, "override"))
20900 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20901 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20903 else if (id_equal (token
->u
.value
, "final"))
20905 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20906 virt_specifier
= VIRT_SPEC_FINAL
;
20908 else if (id_equal (token
->u
.value
, "__final"))
20910 virt_specifier
= VIRT_SPEC_FINAL
;
20915 if (virt_specifiers
& virt_specifier
)
20917 gcc_rich_location
richloc (token
->location
);
20918 richloc
.add_fixit_remove ();
20919 error_at (&richloc
, "duplicate virt-specifier");
20920 cp_lexer_purge_token (parser
->lexer
);
20924 cp_lexer_consume_token (parser
->lexer
);
20925 virt_specifiers
|= virt_specifier
;
20928 return virt_specifiers
;
20931 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20932 is in scope even though it isn't real. */
20935 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20939 if (current_class_ptr
)
20941 /* We don't clear this between NSDMIs. Is it already what we want? */
20942 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20943 if (DECL_P (current_class_ptr
)
20944 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20945 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20946 && cp_type_quals (type
) == quals
)
20950 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20951 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20952 current_class_ptr
= NULL_TREE
;
20954 = cp_build_fold_indirect_ref (this_parm
);
20955 current_class_ptr
= this_parm
;
20958 /* Return true iff our current scope is a non-static data member
20962 parsing_nsdmi (void)
20964 /* We recognize NSDMI context by the context-less 'this' pointer set up
20965 by the function above. */
20966 if (current_class_ptr
20967 && TREE_CODE (current_class_ptr
) == PARM_DECL
20968 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20973 /* Parse a late-specified return type, if any. This is not a separate
20974 non-terminal, but part of a function declarator, which looks like
20976 -> trailing-type-specifier-seq abstract-declarator(opt)
20978 Returns the type indicated by the type-id.
20980 In addition to this, parse any queued up #pragma omp declare simd
20981 clauses, and #pragma acc routine clauses.
20983 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20987 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20988 tree
& requires_clause
, cp_cv_quals quals
)
20991 tree type
= NULL_TREE
;
20992 bool declare_simd_p
= (parser
->omp_declare_simd
20994 && declarator
->kind
== cdk_id
);
20996 bool oacc_routine_p
= (parser
->oacc_routine
20998 && declarator
->kind
== cdk_id
);
21000 /* Peek at the next token. */
21001 token
= cp_lexer_peek_token (parser
->lexer
);
21002 /* A late-specified return type is indicated by an initial '->'. */
21003 if (token
->type
!= CPP_DEREF
21004 && token
->keyword
!= RID_REQUIRES
21005 && !(token
->type
== CPP_NAME
21006 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21007 && !(declare_simd_p
|| oacc_routine_p
))
21010 tree save_ccp
= current_class_ptr
;
21011 tree save_ccr
= current_class_ref
;
21014 /* DR 1207: 'this' is in scope in the trailing return type. */
21015 inject_this_parameter (current_class_type
, quals
);
21018 if (token
->type
== CPP_DEREF
)
21020 /* Consume the ->. */
21021 cp_lexer_consume_token (parser
->lexer
);
21023 type
= cp_parser_trailing_type_id (parser
);
21026 /* Function declarations may be followed by a trailing
21027 requires-clause. */
21028 requires_clause
= cp_parser_requires_clause_opt (parser
);
21030 if (declare_simd_p
)
21031 declarator
->attributes
21032 = cp_parser_late_parsing_omp_declare_simd (parser
,
21033 declarator
->attributes
);
21034 if (oacc_routine_p
)
21035 declarator
->attributes
21036 = cp_parser_late_parsing_oacc_routine (parser
,
21037 declarator
->attributes
);
21041 current_class_ptr
= save_ccp
;
21042 current_class_ref
= save_ccr
;
21048 /* Parse a declarator-id.
21052 :: [opt] nested-name-specifier [opt] type-name
21054 In the `id-expression' case, the value returned is as for
21055 cp_parser_id_expression if the id-expression was an unqualified-id.
21056 If the id-expression was a qualified-id, then a SCOPE_REF is
21057 returned. The first operand is the scope (either a NAMESPACE_DECL
21058 or TREE_TYPE), but the second is still just a representation of an
21062 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21065 /* The expression must be an id-expression. Assume that qualified
21066 names are the names of types so that:
21069 int S<T>::R::i = 3;
21071 will work; we must treat `S<T>::R' as the name of a type.
21072 Similarly, assume that qualified names are templates, where
21076 int S<T>::R<T>::i = 3;
21079 id
= cp_parser_id_expression (parser
,
21080 /*template_keyword_p=*/false,
21081 /*check_dependency_p=*/false,
21082 /*template_p=*/NULL
,
21083 /*declarator_p=*/true,
21085 if (id
&& BASELINK_P (id
))
21086 id
= BASELINK_FUNCTIONS (id
);
21090 /* Parse a type-id.
21093 type-specifier-seq abstract-declarator [opt]
21095 Returns the TYPE specified. */
21098 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21099 bool is_trailing_return
, location_t
* type_location
)
21101 cp_decl_specifier_seq type_specifier_seq
;
21102 cp_declarator
*abstract_declarator
;
21104 /* Parse the type-specifier-seq. */
21105 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21106 is_trailing_return
,
21107 &type_specifier_seq
);
21109 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21111 if (is_template_arg
&& type_specifier_seq
.type
21112 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21113 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21114 /* A bare template name as a template argument is a template template
21115 argument, not a placeholder, so fail parsing it as a type argument. */
21117 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21118 cp_parser_simulate_error (parser
);
21119 return error_mark_node
;
21121 if (type_specifier_seq
.type
== error_mark_node
)
21122 return error_mark_node
;
21124 /* There might or might not be an abstract declarator. */
21125 cp_parser_parse_tentatively (parser
);
21126 /* Look for the declarator. */
21127 abstract_declarator
21128 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21129 /*parenthesized_p=*/NULL
,
21130 /*member_p=*/false,
21131 /*friend_p=*/false);
21132 /* Check to see if there really was a declarator. */
21133 if (!cp_parser_parse_definitely (parser
))
21134 abstract_declarator
= NULL
;
21136 if (type_specifier_seq
.type
21137 /* The concepts TS allows 'auto' as a type-id. */
21138 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21139 /* None of the valid uses of 'auto' in C++14 involve the type-id
21140 nonterminal, but it is valid in a trailing-return-type. */
21141 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21142 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21144 /* A type-id with type 'auto' is only ok if the abstract declarator
21145 is a function declarator with a late-specified return type.
21147 A type-id with 'auto' is also valid in a trailing-return-type
21148 in a compound-requirement. */
21149 if (abstract_declarator
21150 && abstract_declarator
->kind
== cdk_function
21151 && abstract_declarator
->u
.function
.late_return_type
)
21153 else if (parser
->in_result_type_constraint_p
)
21157 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21158 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21160 error_at (loc
, "missing template arguments after %qT",
21162 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21166 error_at (loc
, "invalid use of %qT", auto_node
);
21167 return error_mark_node
;
21171 return groktypename (&type_specifier_seq
, abstract_declarator
,
21176 cp_parser_type_id (cp_parser
*parser
, location_t
* type_location
)
21178 return cp_parser_type_id_1 (parser
, false, false, type_location
);
21182 cp_parser_template_type_arg (cp_parser
*parser
)
21185 const char *saved_message
= parser
->type_definition_forbidden_message
;
21186 parser
->type_definition_forbidden_message
21187 = G_("types may not be defined in template arguments");
21188 r
= cp_parser_type_id_1 (parser
, true, false, NULL
);
21189 parser
->type_definition_forbidden_message
= saved_message
;
21190 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21192 error ("invalid use of %<auto%> in template argument");
21193 r
= error_mark_node
;
21199 cp_parser_trailing_type_id (cp_parser
*parser
)
21201 return cp_parser_type_id_1 (parser
, false, true, NULL
);
21204 /* Parse a type-specifier-seq.
21206 type-specifier-seq:
21207 type-specifier type-specifier-seq [opt]
21211 type-specifier-seq:
21212 attributes type-specifier-seq [opt]
21214 If IS_DECLARATION is true, we are at the start of a "condition" or
21215 exception-declaration, so we might be followed by a declarator-id.
21217 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21218 i.e. we've just seen "->".
21220 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21223 cp_parser_type_specifier_seq (cp_parser
* parser
,
21224 bool is_declaration
,
21225 bool is_trailing_return
,
21226 cp_decl_specifier_seq
*type_specifier_seq
)
21228 bool seen_type_specifier
= false;
21229 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21230 cp_token
*start_token
= NULL
;
21232 /* Clear the TYPE_SPECIFIER_SEQ. */
21233 clear_decl_specs (type_specifier_seq
);
21235 /* In the context of a trailing return type, enum E { } is an
21236 elaborated-type-specifier followed by a function-body, not an
21238 if (is_trailing_return
)
21239 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21241 /* Parse the type-specifiers and attributes. */
21244 tree type_specifier
;
21245 bool is_cv_qualifier
;
21247 /* Check for attributes first. */
21248 if (cp_next_tokens_can_be_attribute_p (parser
))
21250 type_specifier_seq
->attributes
21251 = attr_chainon (type_specifier_seq
->attributes
,
21252 cp_parser_attributes_opt (parser
));
21256 /* record the token of the beginning of the type specifier seq,
21257 for error reporting purposes*/
21259 start_token
= cp_lexer_peek_token (parser
->lexer
);
21261 /* Look for the type-specifier. */
21262 type_specifier
= cp_parser_type_specifier (parser
,
21264 type_specifier_seq
,
21265 /*is_declaration=*/false,
21268 if (!type_specifier
)
21270 /* If the first type-specifier could not be found, this is not a
21271 type-specifier-seq at all. */
21272 if (!seen_type_specifier
)
21274 /* Set in_declarator_p to avoid skipping to the semicolon. */
21275 int in_decl
= parser
->in_declarator_p
;
21276 parser
->in_declarator_p
= true;
21278 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21279 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21280 cp_parser_error (parser
, "expected type-specifier");
21282 parser
->in_declarator_p
= in_decl
;
21284 type_specifier_seq
->type
= error_mark_node
;
21287 /* If subsequent type-specifiers could not be found, the
21288 type-specifier-seq is complete. */
21292 seen_type_specifier
= true;
21293 /* The standard says that a condition can be:
21295 type-specifier-seq declarator = assignment-expression
21302 we should treat the "S" as a declarator, not as a
21303 type-specifier. The standard doesn't say that explicitly for
21304 type-specifier-seq, but it does say that for
21305 decl-specifier-seq in an ordinary declaration. Perhaps it
21306 would be clearer just to allow a decl-specifier-seq here, and
21307 then add a semantic restriction that if any decl-specifiers
21308 that are not type-specifiers appear, the program is invalid. */
21309 if (is_declaration
&& !is_cv_qualifier
)
21310 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21314 /* Return whether the function currently being declared has an associated
21315 template parameter list. */
21318 function_being_declared_is_template_p (cp_parser
* parser
)
21320 if (!current_template_parms
|| processing_template_parmlist
)
21323 if (parser
->implicit_template_scope
)
21326 if (at_class_scope_p ()
21327 && TYPE_BEING_DEFINED (current_class_type
))
21328 return parser
->num_template_parameter_lists
!= 0;
21330 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21331 (current_class_type
));
21334 /* Parse a parameter-declaration-clause.
21336 parameter-declaration-clause:
21337 parameter-declaration-list [opt] ... [opt]
21338 parameter-declaration-list , ...
21340 Returns a representation for the parameter declarations. A return
21341 value of NULL indicates a parameter-declaration-clause consisting
21342 only of an ellipsis. */
21345 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21351 temp_override
<bool> cleanup
21352 (parser
->auto_is_implicit_function_template_parm_p
);
21354 if (!processing_specialization
21355 && !processing_template_parmlist
21356 && !processing_explicit_instantiation
21357 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21358 actual function or a random abstract declarator. */
21359 && parser
->default_arg_ok_p
)
21360 if (!current_function_decl
21361 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21362 parser
->auto_is_implicit_function_template_parm_p
= true;
21364 /* Peek at the next token. */
21365 token
= cp_lexer_peek_token (parser
->lexer
);
21366 /* Check for trivial parameter-declaration-clauses. */
21367 if (token
->type
== CPP_ELLIPSIS
)
21369 /* Consume the `...' token. */
21370 cp_lexer_consume_token (parser
->lexer
);
21373 else if (token
->type
== CPP_CLOSE_PAREN
)
21374 /* There are no parameters. */
21375 return void_list_node
;
21376 /* Check for `(void)', too, which is a special case. */
21377 else if (token
->keyword
== RID_VOID
21378 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21379 == CPP_CLOSE_PAREN
))
21381 /* Consume the `void' token. */
21382 cp_lexer_consume_token (parser
->lexer
);
21383 /* There are no parameters. */
21384 return void_list_node
;
21387 /* Parse the parameter-declaration-list. */
21388 parameters
= cp_parser_parameter_declaration_list (parser
);
21389 /* If a parse error occurred while parsing the
21390 parameter-declaration-list, then the entire
21391 parameter-declaration-clause is erroneous. */
21392 if (parameters
== error_mark_node
)
21395 /* Peek at the next token. */
21396 token
= cp_lexer_peek_token (parser
->lexer
);
21397 /* If it's a `,', the clause should terminate with an ellipsis. */
21398 if (token
->type
== CPP_COMMA
)
21400 /* Consume the `,'. */
21401 cp_lexer_consume_token (parser
->lexer
);
21402 /* Expect an ellipsis. */
21404 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21406 /* It might also be `...' if the optional trailing `,' was
21408 else if (token
->type
== CPP_ELLIPSIS
)
21410 /* Consume the `...' token. */
21411 cp_lexer_consume_token (parser
->lexer
);
21412 /* And remember that we saw it. */
21416 ellipsis_p
= false;
21418 /* Finish the parameter list. */
21420 parameters
= chainon (parameters
, void_list_node
);
21425 /* Parse a parameter-declaration-list.
21427 parameter-declaration-list:
21428 parameter-declaration
21429 parameter-declaration-list , parameter-declaration
21431 Returns a representation of the parameter-declaration-list, as for
21432 cp_parser_parameter_declaration_clause. However, the
21433 `void_list_node' is never appended to the list. */
21436 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21438 tree parameters
= NULL_TREE
;
21439 tree
*tail
= ¶meters
;
21440 bool saved_in_unbraced_linkage_specification_p
;
21443 /* The special considerations that apply to a function within an
21444 unbraced linkage specifications do not apply to the parameters
21445 to the function. */
21446 saved_in_unbraced_linkage_specification_p
21447 = parser
->in_unbraced_linkage_specification_p
;
21448 parser
->in_unbraced_linkage_specification_p
= false;
21450 /* Look for more parameters. */
21453 cp_parameter_declarator
*parameter
;
21454 tree decl
= error_mark_node
;
21455 bool parenthesized_p
= false;
21457 /* Parse the parameter. */
21459 = cp_parser_parameter_declaration (parser
,
21460 /*template_parm_p=*/false,
21463 /* We don't know yet if the enclosing context is deprecated, so wait
21464 and warn in grokparms if appropriate. */
21465 deprecated_state
= DEPRECATED_SUPPRESS
;
21469 decl
= grokdeclarator (parameter
->declarator
,
21470 ¶meter
->decl_specifiers
,
21472 parameter
->default_argument
!= NULL_TREE
,
21473 ¶meter
->decl_specifiers
.attributes
);
21474 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21475 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21478 deprecated_state
= DEPRECATED_NORMAL
;
21480 /* If a parse error occurred parsing the parameter declaration,
21481 then the entire parameter-declaration-list is erroneous. */
21482 if (decl
== error_mark_node
)
21484 parameters
= error_mark_node
;
21488 if (parameter
->decl_specifiers
.attributes
)
21489 cplus_decl_attributes (&decl
,
21490 parameter
->decl_specifiers
.attributes
,
21492 if (DECL_NAME (decl
))
21493 decl
= pushdecl (decl
);
21495 if (decl
!= error_mark_node
)
21497 retrofit_lang_decl (decl
);
21498 DECL_PARM_INDEX (decl
) = ++index
;
21499 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21502 /* Add the new parameter to the list. */
21503 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21504 tail
= &TREE_CHAIN (*tail
);
21506 /* Peek at the next token. */
21507 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21508 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21509 /* These are for Objective-C++ */
21510 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21511 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21512 /* The parameter-declaration-list is complete. */
21514 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21518 /* Peek at the next token. */
21519 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21520 /* If it's an ellipsis, then the list is complete. */
21521 if (token
->type
== CPP_ELLIPSIS
)
21523 /* Otherwise, there must be more parameters. Consume the
21525 cp_lexer_consume_token (parser
->lexer
);
21526 /* When parsing something like:
21528 int i(float f, double d)
21530 we can tell after seeing the declaration for "f" that we
21531 are not looking at an initialization of a variable "i",
21532 but rather at the declaration of a function "i".
21534 Due to the fact that the parsing of template arguments
21535 (as specified to a template-id) requires backtracking we
21536 cannot use this technique when inside a template argument
21538 if (!parser
->in_template_argument_list_p
21539 && !parser
->in_type_id_in_expr_p
21540 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21541 /* However, a parameter-declaration of the form
21542 "float(f)" (which is a valid declaration of a
21543 parameter "f") can also be interpreted as an
21544 expression (the conversion of "f" to "float"). */
21545 && !parenthesized_p
)
21546 cp_parser_commit_to_tentative_parse (parser
);
21550 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21551 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21552 cp_parser_skip_to_closing_parenthesis (parser
,
21553 /*recovering=*/true,
21554 /*or_comma=*/false,
21555 /*consume_paren=*/false);
21560 parser
->in_unbraced_linkage_specification_p
21561 = saved_in_unbraced_linkage_specification_p
;
21563 /* Reset implicit_template_scope if we are about to leave the function
21564 parameter list that introduced it. Note that for out-of-line member
21565 definitions, there will be one or more class scopes before we get to
21566 the template parameter scope. */
21568 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21569 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21571 while (maybe_its
->kind
== sk_class
)
21572 maybe_its
= maybe_its
->level_chain
;
21573 if (maybe_its
== its
)
21575 parser
->implicit_template_parms
= 0;
21576 parser
->implicit_template_scope
= 0;
21583 /* Parse a parameter declaration.
21585 parameter-declaration:
21586 decl-specifier-seq ... [opt] declarator
21587 decl-specifier-seq declarator = assignment-expression
21588 decl-specifier-seq ... [opt] abstract-declarator [opt]
21589 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21591 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21592 declares a template parameter. (In that case, a non-nested `>'
21593 token encountered during the parsing of the assignment-expression
21594 is not interpreted as a greater-than operator.)
21596 Returns a representation of the parameter, or NULL if an error
21597 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21598 true iff the declarator is of the form "(p)". */
21600 static cp_parameter_declarator
*
21601 cp_parser_parameter_declaration (cp_parser
*parser
,
21602 bool template_parm_p
,
21603 bool *parenthesized_p
)
21605 int declares_class_or_enum
;
21606 cp_decl_specifier_seq decl_specifiers
;
21607 cp_declarator
*declarator
;
21608 tree default_argument
;
21609 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21610 const char *saved_message
;
21611 bool template_parameter_pack_p
= false;
21613 /* In a template parameter, `>' is not an operator.
21617 When parsing a default template-argument for a non-type
21618 template-parameter, the first non-nested `>' is taken as the end
21619 of the template parameter-list rather than a greater-than
21622 /* Type definitions may not appear in parameter types. */
21623 saved_message
= parser
->type_definition_forbidden_message
;
21624 parser
->type_definition_forbidden_message
21625 = G_("types may not be defined in parameter types");
21627 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21628 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21629 (current_template_parms
)) : 0);
21631 /* Parse the declaration-specifiers. */
21632 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21633 cp_parser_decl_specifier_seq (parser
,
21634 CP_PARSER_FLAGS_NONE
,
21636 &declares_class_or_enum
);
21638 /* Complain about missing 'typename' or other invalid type names. */
21639 if (!decl_specifiers
.any_type_specifiers_p
21640 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21641 decl_specifiers
.type
= error_mark_node
;
21643 /* If an error occurred, there's no reason to attempt to parse the
21644 rest of the declaration. */
21645 if (cp_parser_error_occurred (parser
))
21647 parser
->type_definition_forbidden_message
= saved_message
;
21651 /* Peek at the next token. */
21652 token
= cp_lexer_peek_token (parser
->lexer
);
21654 /* If the next token is a `)', `,', `=', `>', or `...', then there
21655 is no declarator. However, when variadic templates are enabled,
21656 there may be a declarator following `...'. */
21657 if (token
->type
== CPP_CLOSE_PAREN
21658 || token
->type
== CPP_COMMA
21659 || token
->type
== CPP_EQ
21660 || token
->type
== CPP_GREATER
)
21663 if (parenthesized_p
)
21664 *parenthesized_p
= false;
21666 /* Otherwise, there should be a declarator. */
21669 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21670 parser
->default_arg_ok_p
= false;
21672 /* After seeing a decl-specifier-seq, if the next token is not a
21673 "(", there is no possibility that the code is a valid
21674 expression. Therefore, if parsing tentatively, we commit at
21676 if (!parser
->in_template_argument_list_p
21677 /* In an expression context, having seen:
21681 we cannot be sure whether we are looking at a
21682 function-type (taking a "char" as a parameter) or a cast
21683 of some object of type "char" to "int". */
21684 && !parser
->in_type_id_in_expr_p
21685 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21687 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21688 cp_parser_commit_to_tentative_parse (parser
);
21689 /* Parse the declarator. */
21690 declarator_token_start
= token
;
21691 declarator
= cp_parser_declarator (parser
,
21692 CP_PARSER_DECLARATOR_EITHER
,
21693 /*ctor_dtor_or_conv_p=*/NULL
,
21695 /*member_p=*/false,
21696 /*friend_p=*/false);
21697 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21698 /* After the declarator, allow more attributes. */
21699 decl_specifiers
.attributes
21700 = attr_chainon (decl_specifiers
.attributes
,
21701 cp_parser_attributes_opt (parser
));
21703 /* If the declarator is a template parameter pack, remember that and
21704 clear the flag in the declarator itself so we don't get errors
21705 from grokdeclarator. */
21706 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21708 declarator
->parameter_pack_p
= false;
21709 template_parameter_pack_p
= true;
21713 /* If the next token is an ellipsis, and we have not seen a declarator
21714 name, and if either the type of the declarator contains parameter
21715 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21716 for, eg, abbreviated integral type names), then we actually have a
21717 parameter pack expansion expression. Otherwise, leave the ellipsis
21718 for a C-style variadic function. */
21719 token
= cp_lexer_peek_token (parser
->lexer
);
21721 /* If a function parameter pack was specified and an implicit template
21722 parameter was introduced during cp_parser_parameter_declaration,
21723 change any implicit parameters introduced into packs. */
21724 if (parser
->implicit_template_parms
21725 && ((token
->type
== CPP_ELLIPSIS
21726 && declarator_can_be_parameter_pack (declarator
))
21727 || (declarator
&& declarator
->parameter_pack_p
)))
21729 int latest_template_parm_idx
= TREE_VEC_LENGTH
21730 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21732 if (latest_template_parm_idx
!= template_parm_idx
)
21733 decl_specifiers
.type
= convert_generic_types_to_packs
21734 (decl_specifiers
.type
,
21735 template_parm_idx
, latest_template_parm_idx
);
21738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21740 tree type
= decl_specifiers
.type
;
21742 if (type
&& DECL_P (type
))
21743 type
= TREE_TYPE (type
);
21746 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21747 && (template_parm_p
|| uses_parameter_packs (type
)))
21748 || (!type
&& template_parm_p
))
21749 && declarator_can_be_parameter_pack (declarator
))
21751 /* Consume the `...'. */
21752 cp_lexer_consume_token (parser
->lexer
);
21753 maybe_warn_variadic_templates ();
21755 /* Build a pack expansion type */
21756 if (template_parm_p
)
21757 template_parameter_pack_p
= true;
21758 else if (declarator
)
21759 declarator
->parameter_pack_p
= true;
21761 decl_specifiers
.type
= make_pack_expansion (type
);
21765 /* The restriction on defining new types applies only to the type
21766 of the parameter, not to the default argument. */
21767 parser
->type_definition_forbidden_message
= saved_message
;
21769 /* If the next token is `=', then process a default argument. */
21770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21772 tree type
= decl_specifiers
.type
;
21773 token
= cp_lexer_peek_token (parser
->lexer
);
21774 /* If we are defining a class, then the tokens that make up the
21775 default argument must be saved and processed later. */
21776 if (!template_parm_p
&& at_class_scope_p ()
21777 && TYPE_BEING_DEFINED (current_class_type
)
21778 && !LAMBDA_TYPE_P (current_class_type
))
21779 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21781 // A constrained-type-specifier may declare a type template-parameter.
21782 else if (declares_constrained_type_template_parameter (type
))
21784 = cp_parser_default_type_template_argument (parser
);
21786 // A constrained-type-specifier may declare a template-template-parameter.
21787 else if (declares_constrained_template_template_parameter (type
))
21789 = cp_parser_default_template_template_argument (parser
);
21791 /* Outside of a class definition, we can just parse the
21792 assignment-expression. */
21795 = cp_parser_default_argument (parser
, template_parm_p
);
21797 if (!parser
->default_arg_ok_p
)
21799 permerror (token
->location
,
21800 "default arguments are only "
21801 "permitted for function parameters");
21803 else if ((declarator
&& declarator
->parameter_pack_p
)
21804 || template_parameter_pack_p
21805 || (decl_specifiers
.type
21806 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21808 /* Find the name of the parameter pack. */
21809 cp_declarator
*id_declarator
= declarator
;
21810 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21811 id_declarator
= id_declarator
->declarator
;
21813 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21814 error_at (declarator_token_start
->location
,
21816 ? G_("template parameter pack %qD "
21817 "cannot have a default argument")
21818 : G_("parameter pack %qD cannot have "
21819 "a default argument"),
21820 id_declarator
->u
.id
.unqualified_name
);
21822 error_at (declarator_token_start
->location
,
21824 ? G_("template parameter pack cannot have "
21825 "a default argument")
21826 : G_("parameter pack cannot have a "
21827 "default argument"));
21829 default_argument
= NULL_TREE
;
21833 default_argument
= NULL_TREE
;
21835 /* Generate a location for the parameter, ranging from the start of the
21836 initial token to the end of the final token (using input_location for
21837 the latter, set up by cp_lexer_set_source_position_from_token when
21840 If we have a identifier, then use it for the caret location, e.g.
21842 extern int callee (int one, int (*two)(int, int), float three);
21843 ~~~~~~^~~~~~~~~~~~~~
21845 otherwise, reuse the start location for the caret location e.g.:
21847 extern int callee (int one, int (*)(int, int), float three);
21851 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21852 ? declarator
->id_loc
21853 : decl_spec_token_start
->location
);
21854 location_t param_loc
= make_location (caret_loc
,
21855 decl_spec_token_start
->location
,
21858 return make_parameter_declarator (&decl_specifiers
,
21862 template_parameter_pack_p
);
21865 /* Parse a default argument and return it.
21867 TEMPLATE_PARM_P is true if this is a default argument for a
21868 non-type template parameter. */
21870 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21872 tree default_argument
= NULL_TREE
;
21873 bool saved_greater_than_is_operator_p
;
21874 bool saved_local_variables_forbidden_p
;
21875 bool non_constant_p
, is_direct_init
;
21877 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21879 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21880 parser
->greater_than_is_operator_p
= !template_parm_p
;
21881 /* Local variable names (and the `this' keyword) may not
21882 appear in a default argument. */
21883 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21884 parser
->local_variables_forbidden_p
= true;
21885 /* Parse the assignment-expression. */
21886 if (template_parm_p
)
21887 push_deferring_access_checks (dk_no_deferred
);
21888 tree saved_class_ptr
= NULL_TREE
;
21889 tree saved_class_ref
= NULL_TREE
;
21890 /* The "this" pointer is not valid in a default argument. */
21893 saved_class_ptr
= current_class_ptr
;
21894 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21895 saved_class_ref
= current_class_ref
;
21896 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21899 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21900 /* Restore the "this" pointer. */
21903 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21904 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21906 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21907 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21908 if (template_parm_p
)
21909 pop_deferring_access_checks ();
21910 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21911 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21913 return default_argument
;
21916 /* Parse a function-body.
21919 compound_statement */
21922 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21924 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21925 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21929 /* Parse a ctor-initializer-opt followed by a function-body. Return
21930 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21931 is true we are parsing a function-try-block. */
21934 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21935 bool in_function_try_block
)
21938 const bool check_body_p
=
21939 DECL_CONSTRUCTOR_P (current_function_decl
)
21940 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21943 /* Begin the function body. */
21944 body
= begin_function_body ();
21945 /* Parse the optional ctor-initializer. */
21946 cp_parser_ctor_initializer_opt (parser
);
21948 /* If we're parsing a constexpr constructor definition, we need
21949 to check that the constructor body is indeed empty. However,
21950 before we get to cp_parser_function_body lot of junk has been
21951 generated, so we can't just check that we have an empty block.
21952 Rather we take a snapshot of the outermost block, and check whether
21953 cp_parser_function_body changed its state. */
21956 list
= cur_stmt_list
;
21957 if (STATEMENT_LIST_TAIL (list
))
21958 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21960 /* Parse the function-body. */
21961 cp_parser_function_body (parser
, in_function_try_block
);
21963 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21964 /* Finish the function body. */
21965 finish_function_body (body
);
21968 /* Parse an initializer.
21971 = initializer-clause
21972 ( expression-list )
21974 Returns an expression representing the initializer. If no
21975 initializer is present, NULL_TREE is returned.
21977 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21978 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21979 set to TRUE if there is no initializer present. If there is an
21980 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21981 is set to true; otherwise it is set to false. */
21984 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21985 bool* non_constant_p
, bool subexpression_p
)
21990 /* Peek at the next token. */
21991 token
= cp_lexer_peek_token (parser
->lexer
);
21993 /* Let our caller know whether or not this initializer was
21995 *is_direct_init
= (token
->type
!= CPP_EQ
);
21996 /* Assume that the initializer is constant. */
21997 *non_constant_p
= false;
21999 if (token
->type
== CPP_EQ
)
22001 /* Consume the `='. */
22002 cp_lexer_consume_token (parser
->lexer
);
22003 /* Parse the initializer-clause. */
22004 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22006 else if (token
->type
== CPP_OPEN_PAREN
)
22008 vec
<tree
, va_gc
> *vec
;
22009 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22011 /*allow_expansion_p=*/true,
22014 return error_mark_node
;
22015 init
= build_tree_list_vec (vec
);
22016 release_tree_vector (vec
);
22018 else if (token
->type
== CPP_OPEN_BRACE
)
22020 cp_lexer_set_source_position (parser
->lexer
);
22021 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22022 init
= cp_parser_braced_list (parser
, non_constant_p
);
22023 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22027 /* Anything else is an error. */
22028 cp_parser_error (parser
, "expected initializer");
22029 init
= error_mark_node
;
22032 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22033 init
= error_mark_node
;
22038 /* Parse an initializer-clause.
22040 initializer-clause:
22041 assignment-expression
22044 Returns an expression representing the initializer.
22046 If the `assignment-expression' production is used the value
22047 returned is simply a representation for the expression.
22049 Otherwise, calls cp_parser_braced_list. */
22052 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22054 cp_expr initializer
;
22056 /* Assume the expression is constant. */
22057 *non_constant_p
= false;
22059 /* If it is not a `{', then we are looking at an
22060 assignment-expression. */
22061 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22064 = cp_parser_constant_expression (parser
,
22065 /*allow_non_constant_p=*/true,
22069 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22071 return initializer
;
22074 /* Parse a brace-enclosed initializer list.
22077 { initializer-list , [opt] }
22078 { designated-initializer-list , [opt] }
22081 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22082 the elements of the initializer-list (or NULL, if the last
22083 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22084 NULL_TREE. There is no way to detect whether or not the optional
22085 trailing `,' was provided. NON_CONSTANT_P is as for
22086 cp_parser_initializer. */
22089 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22092 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22094 /* Consume the `{' token. */
22095 matching_braces braces
;
22096 braces
.require_open (parser
);
22097 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22098 initializer
= make_node (CONSTRUCTOR
);
22099 /* If it's not a `}', then there is a non-trivial initializer. */
22100 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22102 /* Parse the initializer list. */
22103 CONSTRUCTOR_ELTS (initializer
)
22104 = cp_parser_initializer_list (parser
, non_constant_p
);
22105 /* A trailing `,' token is allowed. */
22106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22107 cp_lexer_consume_token (parser
->lexer
);
22110 *non_constant_p
= false;
22111 /* Now, there should be a trailing `}'. */
22112 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22113 braces
.require_close (parser
);
22114 TREE_TYPE (initializer
) = init_list_type_node
;
22116 cp_expr
result (initializer
);
22117 /* Build a location of the form:
22120 with caret==start at the open brace, finish at the close brace. */
22121 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22122 result
.set_location (combined_loc
);
22126 /* Consume tokens up to, and including, the next non-nested closing `]'.
22127 Returns true iff we found a closing `]'. */
22130 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22132 unsigned square_depth
= 0;
22136 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22138 switch (token
->type
)
22141 case CPP_PRAGMA_EOL
:
22142 /* If we've run out of tokens, then there is no closing `]'. */
22145 case CPP_OPEN_SQUARE
:
22149 case CPP_CLOSE_SQUARE
:
22150 if (!square_depth
--)
22152 cp_lexer_consume_token (parser
->lexer
);
22161 /* Consume the token. */
22162 cp_lexer_consume_token (parser
->lexer
);
22166 /* Return true if we are looking at an array-designator, false otherwise. */
22169 cp_parser_array_designator_p (cp_parser
*parser
)
22171 /* Consume the `['. */
22172 cp_lexer_consume_token (parser
->lexer
);
22174 cp_lexer_save_tokens (parser
->lexer
);
22176 /* Skip tokens until the next token is a closing square bracket.
22177 If we find the closing `]', and the next token is a `=', then
22178 we are looking at an array designator. */
22179 bool array_designator_p
22180 = (cp_parser_skip_to_closing_square_bracket (parser
)
22181 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22183 /* Roll back the tokens we skipped. */
22184 cp_lexer_rollback_tokens (parser
->lexer
);
22186 return array_designator_p
;
22189 /* Parse an initializer-list.
22192 initializer-clause ... [opt]
22193 initializer-list , initializer-clause ... [opt]
22197 designated-initializer-list:
22198 designated-initializer-clause
22199 designated-initializer-list , designated-initializer-clause
22201 designated-initializer-clause:
22202 designator brace-or-equal-initializer
22210 designation initializer-clause ...[opt]
22211 initializer-list , designation initializer-clause ...[opt]
22216 [ constant-expression ] =
22218 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22219 for the initializer. If the INDEX of the elt is non-NULL, it is the
22220 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22221 as for cp_parser_initializer. */
22223 static vec
<constructor_elt
, va_gc
> *
22224 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22226 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22227 bool first_p
= true;
22228 tree first_designator
= NULL_TREE
;
22230 /* Assume all of the expressions are constant. */
22231 *non_constant_p
= false;
22233 /* Parse the rest of the list. */
22239 bool clause_non_constant_p
;
22240 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22242 /* Handle the C++2A syntax, '. id ='. */
22243 if ((cxx_dialect
>= cxx2a
22244 || cp_parser_allow_gnu_extensions_p (parser
))
22245 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22246 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22247 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22248 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22249 == CPP_OPEN_BRACE
)))
22251 if (cxx_dialect
< cxx2a
)
22252 pedwarn (loc
, OPT_Wpedantic
,
22253 "C++ designated initializers only available with "
22254 "-std=c++2a or -std=gnu++2a");
22255 /* Consume the `.'. */
22256 cp_lexer_consume_token (parser
->lexer
);
22257 /* Consume the identifier. */
22258 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22260 /* Consume the `='. */
22261 cp_lexer_consume_token (parser
->lexer
);
22263 /* Also, if the next token is an identifier and the following one is a
22264 colon, we are looking at the GNU designated-initializer
22266 else if (cp_parser_allow_gnu_extensions_p (parser
)
22267 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22268 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22271 /* Warn the user that they are using an extension. */
22272 pedwarn (loc
, OPT_Wpedantic
,
22273 "ISO C++ does not allow GNU designated initializers");
22274 /* Consume the identifier. */
22275 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22276 /* Consume the `:'. */
22277 cp_lexer_consume_token (parser
->lexer
);
22279 /* Also handle C99 array designators, '[ const ] ='. */
22280 else if (cp_parser_allow_gnu_extensions_p (parser
)
22281 && !c_dialect_objc ()
22282 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22284 /* In C++11, [ could start a lambda-introducer. */
22285 bool non_const
= false;
22287 cp_parser_parse_tentatively (parser
);
22289 if (!cp_parser_array_designator_p (parser
))
22291 cp_parser_simulate_error (parser
);
22292 designator
= NULL_TREE
;
22296 designator
= cp_parser_constant_expression (parser
, true,
22298 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22299 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22302 if (!cp_parser_parse_definitely (parser
))
22303 designator
= NULL_TREE
;
22305 && (!require_potential_rvalue_constant_expression
22307 designator
= NULL_TREE
;
22309 /* Warn the user that they are using an extension. */
22310 pedwarn (loc
, OPT_Wpedantic
,
22311 "ISO C++ does not allow C99 designated initializers");
22314 designator
= NULL_TREE
;
22318 first_designator
= designator
;
22321 else if (cxx_dialect
>= cxx2a
22322 && first_designator
!= error_mark_node
22323 && (!first_designator
!= !designator
))
22325 error_at (loc
, "either all initializer clauses should be designated "
22326 "or none of them should be");
22327 first_designator
= error_mark_node
;
22329 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22330 first_designator
= designator
;
22332 /* Parse the initializer. */
22333 initializer
= cp_parser_initializer_clause (parser
,
22334 &clause_non_constant_p
);
22335 /* If any clause is non-constant, so is the entire initializer. */
22336 if (clause_non_constant_p
)
22337 *non_constant_p
= true;
22339 /* If we have an ellipsis, this is an initializer pack
22341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22343 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22345 /* Consume the `...'. */
22346 cp_lexer_consume_token (parser
->lexer
);
22348 if (designator
&& cxx_dialect
>= cxx2a
)
22350 "%<...%> not allowed in designated initializer list");
22352 /* Turn the initializer into an initializer expansion. */
22353 initializer
= make_pack_expansion (initializer
);
22356 /* Add it to the vector. */
22357 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22359 /* If the next token is not a comma, we have reached the end of
22361 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22364 /* Peek at the next token. */
22365 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22366 /* If the next token is a `}', then we're still done. An
22367 initializer-clause can have a trailing `,' after the
22368 initializer-list and before the closing `}'. */
22369 if (token
->type
== CPP_CLOSE_BRACE
)
22372 /* Consume the `,' token. */
22373 cp_lexer_consume_token (parser
->lexer
);
22376 /* The same identifier shall not appear in multiple designators
22377 of a designated-initializer-list. */
22378 if (first_designator
)
22381 tree designator
, val
;
22382 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22383 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22385 if (IDENTIFIER_MARKED (designator
))
22387 error_at (cp_expr_loc_or_loc (val
, input_location
),
22388 "%<.%s%> designator used multiple times in "
22389 "the same initializer list",
22390 IDENTIFIER_POINTER (designator
));
22391 (*v
)[i
].index
= error_mark_node
;
22394 IDENTIFIER_MARKED (designator
) = 1;
22396 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22397 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22398 IDENTIFIER_MARKED (designator
) = 0;
22404 /* Classes [gram.class] */
22406 /* Parse a class-name.
22412 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22413 to indicate that names looked up in dependent types should be
22414 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22415 keyword has been used to indicate that the name that appears next
22416 is a template. TAG_TYPE indicates the explicit tag given before
22417 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22418 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22419 is the class being defined in a class-head. If ENUM_OK is TRUE,
22420 enum-names are also accepted.
22422 Returns the TYPE_DECL representing the class. */
22425 cp_parser_class_name (cp_parser
*parser
,
22426 bool typename_keyword_p
,
22427 bool template_keyword_p
,
22428 enum tag_types tag_type
,
22429 bool check_dependency_p
,
22431 bool is_declaration
,
22438 tree identifier
= NULL_TREE
;
22440 /* All class-names start with an identifier. */
22441 token
= cp_lexer_peek_token (parser
->lexer
);
22442 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22444 cp_parser_error (parser
, "expected class-name");
22445 return error_mark_node
;
22448 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22449 to a template-id, so we save it here. */
22450 scope
= parser
->scope
;
22451 if (scope
== error_mark_node
)
22452 return error_mark_node
;
22454 /* Any name names a type if we're following the `typename' keyword
22455 in a qualified name where the enclosing scope is type-dependent. */
22456 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22457 && dependent_type_p (scope
));
22458 /* Handle the common case (an identifier, but not a template-id)
22460 if (token
->type
== CPP_NAME
22461 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22463 cp_token
*identifier_token
;
22466 /* Look for the identifier. */
22467 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22468 ambiguous_p
= identifier_token
->error_reported
;
22469 identifier
= cp_parser_identifier (parser
);
22470 /* If the next token isn't an identifier, we are certainly not
22471 looking at a class-name. */
22472 if (identifier
== error_mark_node
)
22473 decl
= error_mark_node
;
22474 /* If we know this is a type-name, there's no need to look it
22476 else if (typename_p
)
22480 tree ambiguous_decls
;
22481 /* If we already know that this lookup is ambiguous, then
22482 we've already issued an error message; there's no reason
22486 cp_parser_simulate_error (parser
);
22487 return error_mark_node
;
22489 /* If the next token is a `::', then the name must be a type
22492 [basic.lookup.qual]
22494 During the lookup for a name preceding the :: scope
22495 resolution operator, object, function, and enumerator
22496 names are ignored. */
22497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22498 tag_type
= scope_type
;
22499 /* Look up the name. */
22500 decl
= cp_parser_lookup_name (parser
, identifier
,
22502 /*is_template=*/false,
22503 /*is_namespace=*/false,
22504 check_dependency_p
,
22506 identifier_token
->location
);
22507 if (ambiguous_decls
)
22509 if (cp_parser_parsing_tentatively (parser
))
22510 cp_parser_simulate_error (parser
);
22511 return error_mark_node
;
22517 /* Try a template-id. */
22518 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22519 check_dependency_p
,
22522 if (decl
== error_mark_node
)
22523 return error_mark_node
;
22526 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22528 /* If this is a typename, create a TYPENAME_TYPE. */
22529 if (typename_p
&& decl
!= error_mark_node
)
22531 decl
= make_typename_type (scope
, decl
, typename_type
,
22532 /*complain=*/tf_error
);
22533 if (decl
!= error_mark_node
)
22534 decl
= TYPE_NAME (decl
);
22537 decl
= strip_using_decl (decl
);
22539 /* Check to see that it is really the name of a class. */
22540 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22541 && identifier_p (TREE_OPERAND (decl
, 0))
22542 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22543 /* Situations like this:
22545 template <typename T> struct A {
22546 typename T::template X<int>::I i;
22549 are problematic. Is `T::template X<int>' a class-name? The
22550 standard does not seem to be definitive, but there is no other
22551 valid interpretation of the following `::'. Therefore, those
22552 names are considered class-names. */
22554 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22555 if (decl
!= error_mark_node
)
22556 decl
= TYPE_NAME (decl
);
22558 else if (TREE_CODE (decl
) != TYPE_DECL
22559 || TREE_TYPE (decl
) == error_mark_node
22560 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22561 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22562 /* In Objective-C 2.0, a classname followed by '.' starts a
22563 dot-syntax expression, and it's not a type-name. */
22564 || (c_dialect_objc ()
22565 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22566 && objc_is_class_name (decl
)))
22567 decl
= error_mark_node
;
22569 if (decl
== error_mark_node
)
22570 cp_parser_error (parser
, "expected class-name");
22571 else if (identifier
&& !parser
->scope
)
22572 maybe_note_name_used_in_class (identifier
, decl
);
22577 /* Parse a class-specifier.
22580 class-head { member-specification [opt] }
22582 Returns the TREE_TYPE representing the class. */
22585 cp_parser_class_specifier_1 (cp_parser
* parser
)
22588 tree attributes
= NULL_TREE
;
22589 bool nested_name_specifier_p
;
22590 unsigned saved_num_template_parameter_lists
;
22591 bool saved_in_function_body
;
22592 unsigned char in_statement
;
22593 bool in_switch_statement_p
;
22594 bool saved_in_unbraced_linkage_specification_p
;
22595 tree old_scope
= NULL_TREE
;
22596 tree scope
= NULL_TREE
;
22597 cp_token
*closing_brace
;
22599 push_deferring_access_checks (dk_no_deferred
);
22601 /* Parse the class-head. */
22602 type
= cp_parser_class_head (parser
,
22603 &nested_name_specifier_p
);
22604 /* If the class-head was a semantic disaster, skip the entire body
22608 cp_parser_skip_to_end_of_block_or_statement (parser
);
22609 pop_deferring_access_checks ();
22610 return error_mark_node
;
22613 /* Look for the `{'. */
22614 matching_braces braces
;
22615 if (!braces
.require_open (parser
))
22617 pop_deferring_access_checks ();
22618 return error_mark_node
;
22621 cp_ensure_no_omp_declare_simd (parser
);
22622 cp_ensure_no_oacc_routine (parser
);
22624 /* Issue an error message if type-definitions are forbidden here. */
22625 cp_parser_check_type_definition (parser
);
22626 /* Remember that we are defining one more class. */
22627 ++parser
->num_classes_being_defined
;
22628 /* Inside the class, surrounding template-parameter-lists do not
22630 saved_num_template_parameter_lists
22631 = parser
->num_template_parameter_lists
;
22632 parser
->num_template_parameter_lists
= 0;
22633 /* We are not in a function body. */
22634 saved_in_function_body
= parser
->in_function_body
;
22635 parser
->in_function_body
= false;
22636 /* Or in a loop. */
22637 in_statement
= parser
->in_statement
;
22638 parser
->in_statement
= 0;
22639 /* Or in a switch. */
22640 in_switch_statement_p
= parser
->in_switch_statement_p
;
22641 parser
->in_switch_statement_p
= false;
22642 /* We are not immediately inside an extern "lang" block. */
22643 saved_in_unbraced_linkage_specification_p
22644 = parser
->in_unbraced_linkage_specification_p
;
22645 parser
->in_unbraced_linkage_specification_p
= false;
22647 // Associate constraints with the type.
22649 type
= associate_classtype_constraints (type
);
22651 /* Start the class. */
22652 if (nested_name_specifier_p
)
22654 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22655 old_scope
= push_inner_scope (scope
);
22657 type
= begin_class_definition (type
);
22659 if (type
== error_mark_node
)
22660 /* If the type is erroneous, skip the entire body of the class. */
22661 cp_parser_skip_to_closing_brace (parser
);
22663 /* Parse the member-specification. */
22664 cp_parser_member_specification_opt (parser
);
22666 /* Look for the trailing `}'. */
22667 closing_brace
= braces
.require_close (parser
);
22668 /* Look for trailing attributes to apply to this class. */
22669 if (cp_parser_allow_gnu_extensions_p (parser
))
22670 attributes
= cp_parser_gnu_attributes_opt (parser
);
22671 if (type
!= error_mark_node
)
22672 type
= finish_struct (type
, attributes
);
22673 if (nested_name_specifier_p
)
22674 pop_inner_scope (old_scope
, scope
);
22676 /* We've finished a type definition. Check for the common syntax
22677 error of forgetting a semicolon after the definition. We need to
22678 be careful, as we can't just check for not-a-semicolon and be done
22679 with it; the user might have typed:
22681 class X { } c = ...;
22682 class X { } *p = ...;
22684 and so forth. Instead, enumerate all the possible tokens that
22685 might follow this production; if we don't see one of them, then
22686 complain and silently insert the semicolon. */
22688 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22689 bool want_semicolon
= true;
22691 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22692 /* Don't try to parse c++11 attributes here. As per the
22693 grammar, that should be a task for
22694 cp_parser_decl_specifier_seq. */
22695 want_semicolon
= false;
22697 switch (token
->type
)
22700 case CPP_SEMICOLON
:
22703 case CPP_OPEN_PAREN
:
22704 case CPP_CLOSE_PAREN
:
22706 want_semicolon
= false;
22709 /* While it's legal for type qualifiers and storage class
22710 specifiers to follow type definitions in the grammar, only
22711 compiler testsuites contain code like that. Assume that if
22712 we see such code, then what we're really seeing is a case
22716 const <type> var = ...;
22721 static <type> func (...) ...
22723 i.e. the qualifier or specifier applies to the next
22724 declaration. To do so, however, we need to look ahead one
22725 more token to see if *that* token is a type specifier.
22727 This code could be improved to handle:
22730 static const <type> var = ...; */
22732 if (keyword_is_decl_specifier (token
->keyword
))
22734 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22736 /* Handling user-defined types here would be nice, but very
22739 = (lookahead
->type
== CPP_KEYWORD
22740 && keyword_begins_type_specifier (lookahead
->keyword
));
22747 /* If we don't have a type, then something is very wrong and we
22748 shouldn't try to do anything clever. Likewise for not seeing the
22750 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22752 /* Locate the closing brace. */
22753 cp_token_position prev
22754 = cp_lexer_previous_token_position (parser
->lexer
);
22755 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22756 location_t loc
= prev_token
->location
;
22758 /* We want to suggest insertion of a ';' immediately *after* the
22759 closing brace, so, if we can, offset the location by 1 column. */
22760 location_t next_loc
= loc
;
22761 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22762 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22764 rich_location
richloc (line_table
, next_loc
);
22766 /* If we successfully offset the location, suggest the fix-it. */
22767 if (next_loc
!= loc
)
22768 richloc
.add_fixit_insert_before (next_loc
, ";");
22770 if (CLASSTYPE_DECLARED_CLASS (type
))
22771 error_at (&richloc
,
22772 "expected %<;%> after class definition");
22773 else if (TREE_CODE (type
) == RECORD_TYPE
)
22774 error_at (&richloc
,
22775 "expected %<;%> after struct definition");
22776 else if (TREE_CODE (type
) == UNION_TYPE
)
22777 error_at (&richloc
,
22778 "expected %<;%> after union definition");
22780 gcc_unreachable ();
22782 /* Unget one token and smash it to look as though we encountered
22783 a semicolon in the input stream. */
22784 cp_lexer_set_token_position (parser
->lexer
, prev
);
22785 token
= cp_lexer_peek_token (parser
->lexer
);
22786 token
->type
= CPP_SEMICOLON
;
22787 token
->keyword
= RID_MAX
;
22791 /* If this class is not itself within the scope of another class,
22792 then we need to parse the bodies of all of the queued function
22793 definitions. Note that the queued functions defined in a class
22794 are not always processed immediately following the
22795 class-specifier for that class. Consider:
22798 struct B { void f() { sizeof (A); } };
22801 If `f' were processed before the processing of `A' were
22802 completed, there would be no way to compute the size of `A'.
22803 Note that the nesting we are interested in here is lexical --
22804 not the semantic nesting given by TYPE_CONTEXT. In particular,
22807 struct A { struct B; };
22808 struct A::B { void f() { } };
22810 there is no need to delay the parsing of `A::B::f'. */
22811 if (--parser
->num_classes_being_defined
== 0)
22814 tree class_type
= NULL_TREE
;
22815 tree pushed_scope
= NULL_TREE
;
22817 cp_default_arg_entry
*e
;
22818 tree save_ccp
, save_ccr
;
22820 if (any_erroneous_template_args_p (type
))
22822 /* Skip default arguments, NSDMIs, etc, in order to improve
22823 error recovery (c++/71169, c++/71832). */
22824 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22825 vec_safe_truncate (unparsed_nsdmis
, 0);
22826 vec_safe_truncate (unparsed_classes
, 0);
22827 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22830 /* In a first pass, parse default arguments to the functions.
22831 Then, in a second pass, parse the bodies of the functions.
22832 This two-phased approach handles cases like:
22840 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22843 /* If there are default arguments that have not yet been processed,
22844 take care of them now. */
22845 if (class_type
!= e
->class_type
)
22848 pop_scope (pushed_scope
);
22849 class_type
= e
->class_type
;
22850 pushed_scope
= push_scope (class_type
);
22852 /* Make sure that any template parameters are in scope. */
22853 maybe_begin_member_template_processing (decl
);
22854 /* Parse the default argument expressions. */
22855 cp_parser_late_parsing_default_args (parser
, decl
);
22856 /* Remove any template parameters from the symbol table. */
22857 maybe_end_member_template_processing ();
22859 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22860 /* Now parse any NSDMIs. */
22861 save_ccp
= current_class_ptr
;
22862 save_ccr
= current_class_ref
;
22863 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22865 if (class_type
!= DECL_CONTEXT (decl
))
22868 pop_scope (pushed_scope
);
22869 class_type
= DECL_CONTEXT (decl
);
22870 pushed_scope
= push_scope (class_type
);
22872 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22873 cp_parser_late_parsing_nsdmi (parser
, decl
);
22875 vec_safe_truncate (unparsed_nsdmis
, 0);
22876 current_class_ptr
= save_ccp
;
22877 current_class_ref
= save_ccr
;
22879 pop_scope (pushed_scope
);
22881 /* Now do some post-NSDMI bookkeeping. */
22882 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22883 after_nsdmi_defaulted_late_checks (class_type
);
22884 vec_safe_truncate (unparsed_classes
, 0);
22885 after_nsdmi_defaulted_late_checks (type
);
22887 /* Now parse the body of the functions. */
22890 /* OpenMP UDRs need to be parsed before all other functions. */
22891 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22892 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22893 cp_parser_late_parsing_for_member (parser
, decl
);
22894 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22895 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22896 cp_parser_late_parsing_for_member (parser
, decl
);
22899 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22900 cp_parser_late_parsing_for_member (parser
, decl
);
22901 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22904 vec_safe_push (unparsed_classes
, type
);
22906 /* Put back any saved access checks. */
22907 pop_deferring_access_checks ();
22909 /* Restore saved state. */
22910 parser
->in_switch_statement_p
= in_switch_statement_p
;
22911 parser
->in_statement
= in_statement
;
22912 parser
->in_function_body
= saved_in_function_body
;
22913 parser
->num_template_parameter_lists
22914 = saved_num_template_parameter_lists
;
22915 parser
->in_unbraced_linkage_specification_p
22916 = saved_in_unbraced_linkage_specification_p
;
22922 cp_parser_class_specifier (cp_parser
* parser
)
22925 timevar_push (TV_PARSE_STRUCT
);
22926 ret
= cp_parser_class_specifier_1 (parser
);
22927 timevar_pop (TV_PARSE_STRUCT
);
22931 /* Parse a class-head.
22934 class-key identifier [opt] base-clause [opt]
22935 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22936 class-key nested-name-specifier [opt] template-id
22939 class-virt-specifier:
22943 class-key attributes identifier [opt] base-clause [opt]
22944 class-key attributes nested-name-specifier identifier base-clause [opt]
22945 class-key attributes nested-name-specifier [opt] template-id
22948 Upon return BASES is initialized to the list of base classes (or
22949 NULL, if there are none) in the same form returned by
22950 cp_parser_base_clause.
22952 Returns the TYPE of the indicated class. Sets
22953 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22954 involving a nested-name-specifier was used, and FALSE otherwise.
22956 Returns error_mark_node if this is not a class-head.
22958 Returns NULL_TREE if the class-head is syntactically valid, but
22959 semantically invalid in a way that means we should skip the entire
22960 body of the class. */
22963 cp_parser_class_head (cp_parser
* parser
,
22964 bool* nested_name_specifier_p
)
22966 tree nested_name_specifier
;
22967 enum tag_types class_key
;
22968 tree id
= NULL_TREE
;
22969 tree type
= NULL_TREE
;
22972 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22973 bool template_id_p
= false;
22974 bool qualified_p
= false;
22975 bool invalid_nested_name_p
= false;
22976 bool invalid_explicit_specialization_p
= false;
22977 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22978 tree pushed_scope
= NULL_TREE
;
22979 unsigned num_templates
;
22980 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22981 /* Assume no nested-name-specifier will be present. */
22982 *nested_name_specifier_p
= false;
22983 /* Assume no template parameter lists will be used in defining the
22986 parser
->colon_corrects_to_scope_p
= false;
22988 /* Look for the class-key. */
22989 class_key
= cp_parser_class_key (parser
);
22990 if (class_key
== none_type
)
22991 return error_mark_node
;
22993 location_t class_head_start_location
= input_location
;
22995 /* Parse the attributes. */
22996 attributes
= cp_parser_attributes_opt (parser
);
22998 /* If the next token is `::', that is invalid -- but sometimes
22999 people do try to write:
23003 Handle this gracefully by accepting the extra qualifier, and then
23004 issuing an error about it later if this really is a
23005 class-head. If it turns out just to be an elaborated type
23006 specifier, remain silent. */
23007 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23008 qualified_p
= true;
23010 push_deferring_access_checks (dk_no_check
);
23012 /* Determine the name of the class. Begin by looking for an
23013 optional nested-name-specifier. */
23014 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23015 nested_name_specifier
23016 = cp_parser_nested_name_specifier_opt (parser
,
23017 /*typename_keyword_p=*/false,
23018 /*check_dependency_p=*/false,
23020 /*is_declaration=*/false);
23021 /* If there was a nested-name-specifier, then there *must* be an
23024 cp_token
*bad_template_keyword
= NULL
;
23026 if (nested_name_specifier
)
23028 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23029 /* Although the grammar says `identifier', it really means
23030 `class-name' or `template-name'. You are only allowed to
23031 define a class that has already been declared with this
23034 The proposed resolution for Core Issue 180 says that wherever
23035 you see `class T::X' you should treat `X' as a type-name.
23037 It is OK to define an inaccessible class; for example:
23039 class A { class B; };
23042 We do not know if we will see a class-name, or a
23043 template-name. We look for a class-name first, in case the
23044 class-name is a template-id; if we looked for the
23045 template-name first we would stop after the template-name. */
23046 cp_parser_parse_tentatively (parser
);
23047 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23048 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23049 type
= cp_parser_class_name (parser
,
23050 /*typename_keyword_p=*/false,
23051 /*template_keyword_p=*/false,
23053 /*check_dependency_p=*/false,
23054 /*class_head_p=*/true,
23055 /*is_declaration=*/false);
23056 /* If that didn't work, ignore the nested-name-specifier. */
23057 if (!cp_parser_parse_definitely (parser
))
23059 invalid_nested_name_p
= true;
23060 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23061 id
= cp_parser_identifier (parser
);
23062 if (id
== error_mark_node
)
23065 /* If we could not find a corresponding TYPE, treat this
23066 declaration like an unqualified declaration. */
23067 if (type
== error_mark_node
)
23068 nested_name_specifier
= NULL_TREE
;
23069 /* Otherwise, count the number of templates used in TYPE and its
23070 containing scopes. */
23072 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23074 /* Otherwise, the identifier is optional. */
23077 /* We don't know whether what comes next is a template-id,
23078 an identifier, or nothing at all. */
23079 cp_parser_parse_tentatively (parser
);
23080 /* Check for a template-id. */
23081 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23082 id
= cp_parser_template_id (parser
,
23083 /*template_keyword_p=*/false,
23084 /*check_dependency_p=*/true,
23086 /*is_declaration=*/true);
23087 /* If that didn't work, it could still be an identifier. */
23088 if (!cp_parser_parse_definitely (parser
))
23090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23092 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23093 id
= cp_parser_identifier (parser
);
23100 template_id_p
= true;
23105 pop_deferring_access_checks ();
23109 cp_parser_check_for_invalid_template_id (parser
, id
,
23111 type_start_token
->location
);
23113 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23115 /* If it's not a `:' or a `{' then we can't really be looking at a
23116 class-head, since a class-head only appears as part of a
23117 class-specifier. We have to detect this situation before calling
23118 xref_tag, since that has irreversible side-effects. */
23119 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23121 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23122 type
= error_mark_node
;
23126 /* At this point, we're going ahead with the class-specifier, even
23127 if some other problem occurs. */
23128 cp_parser_commit_to_tentative_parse (parser
);
23129 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23131 cp_parser_error (parser
,
23132 "cannot specify %<override%> for a class");
23133 type
= error_mark_node
;
23136 /* Issue the error about the overly-qualified name now. */
23139 cp_parser_error (parser
,
23140 "global qualification of class name is invalid");
23141 type
= error_mark_node
;
23144 else if (invalid_nested_name_p
)
23146 cp_parser_error (parser
,
23147 "qualified name does not name a class");
23148 type
= error_mark_node
;
23151 else if (nested_name_specifier
)
23155 if (bad_template_keyword
)
23156 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23157 keyword template shall not appear at the top level. */
23158 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23159 "keyword %<template%> not allowed in class-head-name");
23161 /* Reject typedef-names in class heads. */
23162 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23164 error_at (type_start_token
->location
,
23165 "invalid class name in declaration of %qD",
23171 /* Figure out in what scope the declaration is being placed. */
23172 scope
= current_scope ();
23173 /* If that scope does not contain the scope in which the
23174 class was originally declared, the program is invalid. */
23175 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23177 if (at_namespace_scope_p ())
23178 error_at (type_start_token
->location
,
23179 "declaration of %qD in namespace %qD which does not "
23181 type
, scope
, nested_name_specifier
);
23183 error_at (type_start_token
->location
,
23184 "declaration of %qD in %qD which does not enclose %qD",
23185 type
, scope
, nested_name_specifier
);
23191 A declarator-id shall not be qualified except for the
23192 definition of a ... nested class outside of its class
23193 ... [or] the definition or explicit instantiation of a
23194 class member of a namespace outside of its namespace. */
23195 if (scope
== nested_name_specifier
)
23197 permerror (nested_name_specifier_token_start
->location
,
23198 "extra qualification not allowed");
23199 nested_name_specifier
= NULL_TREE
;
23203 /* An explicit-specialization must be preceded by "template <>". If
23204 it is not, try to recover gracefully. */
23205 if (at_namespace_scope_p ()
23206 && parser
->num_template_parameter_lists
== 0
23207 && !processing_template_parmlist
23210 /* Build a location of this form:
23211 struct typename <ARGS>
23212 ^~~~~~~~~~~~~~~~~~~~~~
23213 with caret==start at the start token, and
23214 finishing at the end of the type. */
23215 location_t reported_loc
23216 = make_location (class_head_start_location
,
23217 class_head_start_location
,
23218 get_finish (type_start_token
->location
));
23219 rich_location
richloc (line_table
, reported_loc
);
23220 richloc
.add_fixit_insert_before (class_head_start_location
,
23222 error_at (&richloc
,
23223 "an explicit specialization must be preceded by"
23224 " %<template <>%>");
23225 invalid_explicit_specialization_p
= true;
23226 /* Take the same action that would have been taken by
23227 cp_parser_explicit_specialization. */
23228 ++parser
->num_template_parameter_lists
;
23229 begin_specialization ();
23231 /* There must be no "return" statements between this point and the
23232 end of this function; set "type "to the correct return value and
23233 use "goto done;" to return. */
23234 /* Make sure that the right number of template parameters were
23236 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23238 type_start_token
->location
,
23239 /*declarator=*/NULL
))
23241 /* If something went wrong, there is no point in even trying to
23242 process the class-definition. */
23247 /* Look up the type. */
23250 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23251 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23252 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23254 error_at (type_start_token
->location
,
23255 "function template %qD redeclared as a class template", id
);
23256 type
= error_mark_node
;
23260 type
= TREE_TYPE (id
);
23261 type
= maybe_process_partial_specialization (type
);
23263 /* Check the scope while we still know whether or not we had a
23264 nested-name-specifier. */
23265 if (type
!= error_mark_node
)
23266 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23268 if (nested_name_specifier
)
23269 pushed_scope
= push_scope (nested_name_specifier
);
23271 else if (nested_name_specifier
)
23277 template <typename T> struct S { struct T };
23278 template <typename T> struct S<T>::T { };
23280 we will get a TYPENAME_TYPE when processing the definition of
23281 `S::T'. We need to resolve it to the actual type before we
23282 try to define it. */
23283 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23285 class_type
= resolve_typename_type (TREE_TYPE (type
),
23286 /*only_current_p=*/false);
23287 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23288 type
= TYPE_NAME (class_type
);
23291 cp_parser_error (parser
, "could not resolve typename type");
23292 type
= error_mark_node
;
23296 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23297 == error_mark_node
)
23303 class_type
= current_class_type
;
23304 /* Enter the scope indicated by the nested-name-specifier. */
23305 pushed_scope
= push_scope (nested_name_specifier
);
23306 /* Get the canonical version of this type. */
23307 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23308 /* Call push_template_decl if it seems like we should be defining a
23309 template either from the template headers or the type we're
23310 defining, so that we diagnose both extra and missing headers. */
23311 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23312 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23313 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23315 type
= push_template_decl (type
);
23316 if (type
== error_mark_node
)
23323 type
= TREE_TYPE (type
);
23324 *nested_name_specifier_p
= true;
23326 else /* The name is not a nested name. */
23328 /* If the class was unnamed, create a dummy name. */
23330 id
= make_anon_name ();
23331 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23332 ? ts_within_enclosing_non_class
23334 type
= xref_tag (class_key
, id
, tag_scope
,
23335 parser
->num_template_parameter_lists
);
23338 /* Indicate whether this class was declared as a `class' or as a
23340 if (TREE_CODE (type
) == RECORD_TYPE
)
23341 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23342 cp_parser_check_class_key (class_key
, type
);
23344 /* If this type was already complete, and we see another definition,
23345 that's an error. */
23346 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23348 error_at (type_start_token
->location
, "redefinition of %q#T",
23350 inform (location_of (type
), "previous definition of %q#T",
23355 else if (type
== error_mark_node
)
23360 /* Apply attributes now, before any use of the class as a template
23361 argument in its base list. */
23362 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23363 fixup_attribute_variants (type
);
23366 /* We will have entered the scope containing the class; the names of
23367 base classes should be looked up in that context. For example:
23369 struct A { struct B {}; struct C; };
23370 struct A::C : B {};
23374 /* Get the list of base-classes, if there is one. */
23375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23377 /* PR59482: enter the class scope so that base-specifiers are looked
23381 bases
= cp_parser_base_clause (parser
);
23382 /* PR59482: get out of the previously pushed class scope so that the
23383 subsequent pops pop the right thing. */
23390 /* If we're really defining a class, process the base classes.
23391 If they're invalid, fail. */
23392 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23393 xref_basetypes (type
, bases
);
23396 /* Leave the scope given by the nested-name-specifier. We will
23397 enter the class scope itself while processing the members. */
23399 pop_scope (pushed_scope
);
23401 if (invalid_explicit_specialization_p
)
23403 end_specialization ();
23404 --parser
->num_template_parameter_lists
;
23408 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23409 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23410 CLASSTYPE_FINAL (type
) = 1;
23412 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23416 /* Parse a class-key.
23423 Returns the kind of class-key specified, or none_type to indicate
23426 static enum tag_types
23427 cp_parser_class_key (cp_parser
* parser
)
23430 enum tag_types tag_type
;
23432 /* Look for the class-key. */
23433 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23437 /* Check to see if the TOKEN is a class-key. */
23438 tag_type
= cp_parser_token_is_class_key (token
);
23440 cp_parser_error (parser
, "expected class-key");
23444 /* Parse a type-parameter-key.
23446 type-parameter-key:
23452 cp_parser_type_parameter_key (cp_parser
* parser
)
23454 /* Look for the type-parameter-key. */
23455 enum tag_types tag_type
= none_type
;
23456 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23457 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23459 cp_lexer_consume_token (parser
->lexer
);
23460 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23461 /* typename is not allowed in a template template parameter
23462 by the standard until C++17. */
23463 pedwarn (token
->location
, OPT_Wpedantic
,
23464 "ISO C++ forbids typename key in template template parameter;"
23465 " use -std=c++17 or -std=gnu++17");
23468 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23473 /* Parse an (optional) member-specification.
23475 member-specification:
23476 member-declaration member-specification [opt]
23477 access-specifier : member-specification [opt] */
23480 cp_parser_member_specification_opt (cp_parser
* parser
)
23487 /* Peek at the next token. */
23488 token
= cp_lexer_peek_token (parser
->lexer
);
23489 /* If it's a `}', or EOF then we've seen all the members. */
23490 if (token
->type
== CPP_CLOSE_BRACE
23491 || token
->type
== CPP_EOF
23492 || token
->type
== CPP_PRAGMA_EOL
)
23495 /* See if this token is a keyword. */
23496 keyword
= token
->keyword
;
23500 case RID_PROTECTED
:
23502 /* Consume the access-specifier. */
23503 cp_lexer_consume_token (parser
->lexer
);
23504 /* Remember which access-specifier is active. */
23505 current_access_specifier
= token
->u
.value
;
23506 /* Look for the `:'. */
23507 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23511 /* Accept #pragmas at class scope. */
23512 if (token
->type
== CPP_PRAGMA
)
23514 cp_parser_pragma (parser
, pragma_member
, NULL
);
23518 /* Otherwise, the next construction must be a
23519 member-declaration. */
23520 cp_parser_member_declaration (parser
);
23525 /* Parse a member-declaration.
23527 member-declaration:
23528 decl-specifier-seq [opt] member-declarator-list [opt] ;
23529 function-definition ; [opt]
23530 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23532 template-declaration
23535 member-declarator-list:
23537 member-declarator-list , member-declarator
23540 declarator pure-specifier [opt]
23541 declarator constant-initializer [opt]
23542 identifier [opt] : constant-expression
23546 member-declaration:
23547 __extension__ member-declaration
23550 declarator attributes [opt] pure-specifier [opt]
23551 declarator attributes [opt] constant-initializer [opt]
23552 identifier [opt] attributes [opt] : constant-expression
23556 member-declaration:
23557 static_assert-declaration */
23560 cp_parser_member_declaration (cp_parser
* parser
)
23562 cp_decl_specifier_seq decl_specifiers
;
23563 tree prefix_attributes
;
23565 int declares_class_or_enum
;
23567 cp_token
*token
= NULL
;
23568 cp_token
*decl_spec_token_start
= NULL
;
23569 cp_token
*initializer_token_start
= NULL
;
23570 int saved_pedantic
;
23571 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23573 /* Check for the `__extension__' keyword. */
23574 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23577 cp_parser_member_declaration (parser
);
23578 /* Restore the old value of the PEDANTIC flag. */
23579 pedantic
= saved_pedantic
;
23584 /* Check for a template-declaration. */
23585 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23587 /* An explicit specialization here is an error condition, and we
23588 expect the specialization handler to detect and report this. */
23589 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23590 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23591 cp_parser_explicit_specialization (parser
);
23593 cp_parser_template_declaration (parser
, /*member_p=*/true);
23597 /* Check for a template introduction. */
23598 else if (cp_parser_template_declaration_after_export (parser
, true))
23601 /* Check for a using-declaration. */
23602 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23604 if (cxx_dialect
< cxx11
)
23606 /* Parse the using-declaration. */
23607 cp_parser_using_declaration (parser
,
23608 /*access_declaration_p=*/false);
23614 bool alias_decl_expected
;
23615 cp_parser_parse_tentatively (parser
);
23616 decl
= cp_parser_alias_declaration (parser
);
23617 /* Note that if we actually see the '=' token after the
23618 identifier, cp_parser_alias_declaration commits the
23619 tentative parse. In that case, we really expect an
23620 alias-declaration. Otherwise, we expect a using
23622 alias_decl_expected
=
23623 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23624 cp_parser_parse_definitely (parser
);
23626 if (alias_decl_expected
)
23627 finish_member_declaration (decl
);
23629 cp_parser_using_declaration (parser
,
23630 /*access_declaration_p=*/false);
23635 /* Check for @defs. */
23636 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23639 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23640 ivar
= ivar_chains
;
23644 ivar
= TREE_CHAIN (member
);
23645 TREE_CHAIN (member
) = NULL_TREE
;
23646 finish_member_declaration (member
);
23651 /* If the next token is `static_assert' we have a static assertion. */
23652 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23654 cp_parser_static_assert (parser
, /*member_p=*/true);
23658 parser
->colon_corrects_to_scope_p
= false;
23660 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23663 /* Parse the decl-specifier-seq. */
23664 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23665 cp_parser_decl_specifier_seq (parser
,
23666 CP_PARSER_FLAGS_OPTIONAL
,
23668 &declares_class_or_enum
);
23669 /* Check for an invalid type-name. */
23670 if (!decl_specifiers
.any_type_specifiers_p
23671 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23673 /* If there is no declarator, then the decl-specifier-seq should
23675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23677 /* If there was no decl-specifier-seq, and the next token is a
23678 `;', then we have something like:
23684 Each member-declaration shall declare at least one member
23685 name of the class. */
23686 if (!decl_specifiers
.any_specifiers_p
)
23688 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23689 if (!in_system_header_at (token
->location
))
23691 gcc_rich_location
richloc (token
->location
);
23692 richloc
.add_fixit_remove ();
23693 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23700 /* See if this declaration is a friend. */
23701 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23702 /* If there were decl-specifiers, check to see if there was
23703 a class-declaration. */
23704 type
= check_tag_decl (&decl_specifiers
,
23705 /*explicit_type_instantiation_p=*/false);
23706 /* Nested classes have already been added to the class, but
23707 a `friend' needs to be explicitly registered. */
23710 /* If the `friend' keyword was present, the friend must
23711 be introduced with a class-key. */
23712 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23713 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23714 "in C++03 a class-key must be used "
23715 "when declaring a friend");
23718 template <typename T> struct A {
23719 friend struct A<T>::B;
23722 A<T>::B will be represented by a TYPENAME_TYPE, and
23723 therefore not recognized by check_tag_decl. */
23726 type
= decl_specifiers
.type
;
23727 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23728 type
= TREE_TYPE (type
);
23730 if (!type
|| !TYPE_P (type
))
23731 error_at (decl_spec_token_start
->location
,
23732 "friend declaration does not name a class or "
23735 make_friend_class (current_class_type
, type
,
23736 /*complain=*/true);
23738 /* If there is no TYPE, an error message will already have
23740 else if (!type
|| type
== error_mark_node
)
23742 /* An anonymous aggregate has to be handled specially; such
23743 a declaration really declares a data member (with a
23744 particular type), as opposed to a nested class. */
23745 else if (ANON_AGGR_TYPE_P (type
))
23748 if (decl_specifiers
.storage_class
!= sc_none
)
23749 error_at (decl_spec_token_start
->location
,
23750 "a storage class on an anonymous aggregate "
23751 "in class scope is not allowed");
23753 /* Remove constructors and such from TYPE, now that we
23754 know it is an anonymous aggregate. */
23755 fixup_anonymous_aggr (type
);
23756 /* And make the corresponding data member. */
23757 decl
= build_decl (decl_spec_token_start
->location
,
23758 FIELD_DECL
, NULL_TREE
, type
);
23759 /* Add it to the class. */
23760 finish_member_declaration (decl
);
23763 cp_parser_check_access_in_redeclaration
23765 decl_spec_token_start
->location
);
23770 bool assume_semicolon
= false;
23772 /* Clear attributes from the decl_specifiers but keep them
23773 around as prefix attributes that apply them to the entity
23775 prefix_attributes
= decl_specifiers
.attributes
;
23776 decl_specifiers
.attributes
= NULL_TREE
;
23778 /* See if these declarations will be friends. */
23779 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23781 /* Keep going until we hit the `;' at the end of the
23783 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23785 tree attributes
= NULL_TREE
;
23786 tree first_attribute
;
23788 bool named_bitfld
= false;
23790 /* Peek at the next token. */
23791 token
= cp_lexer_peek_token (parser
->lexer
);
23793 /* The following code wants to know early if it is a bit-field
23794 or some other declaration. Attributes can appear before
23795 the `:' token. Skip over them without consuming any tokens
23796 to peek if they are followed by `:'. */
23797 if (cp_next_tokens_can_be_attribute_p (parser
)
23798 || (token
->type
== CPP_NAME
23799 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23800 && (named_bitfld
= true)))
23803 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23804 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23807 /* Check for a bitfield declaration. */
23808 if (token
->type
== CPP_COLON
23809 || (token
->type
== CPP_NAME
23810 && token
== cp_lexer_peek_token (parser
->lexer
)
23811 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23812 && (named_bitfld
= true)))
23816 tree late_attributes
= NULL_TREE
;
23819 identifier
= cp_parser_identifier (parser
);
23821 identifier
= NULL_TREE
;
23823 /* Look for attributes that apply to the bitfield. */
23824 attributes
= cp_parser_attributes_opt (parser
);
23826 /* Consume the `:' token. */
23827 cp_lexer_consume_token (parser
->lexer
);
23829 /* Get the width of the bitfield. */
23830 width
= cp_parser_constant_expression (parser
, false, NULL
,
23831 cxx_dialect
>= cxx11
);
23833 /* In C++2A and as extension for C++11 and above we allow
23834 default member initializers for bit-fields. */
23835 initializer
= NULL_TREE
;
23836 if (cxx_dialect
>= cxx11
23837 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23838 || cp_lexer_next_token_is (parser
->lexer
,
23842 = cp_lexer_peek_token (parser
->lexer
)->location
;
23843 if (cxx_dialect
< cxx2a
23844 && !in_system_header_at (loc
)
23845 && identifier
!= NULL_TREE
)
23847 "default member initializers for bit-fields "
23848 "only available with -std=c++2a or "
23851 initializer
= cp_parser_save_nsdmi (parser
);
23852 if (identifier
== NULL_TREE
)
23854 error_at (loc
, "default member initializer for "
23855 "unnamed bit-field");
23856 initializer
= NULL_TREE
;
23861 /* Look for attributes that apply to the bitfield after
23862 the `:' token and width. This is where GCC used to
23863 parse attributes in the past, pedwarn if there is
23864 a std attribute. */
23865 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23866 pedwarn (input_location
, OPT_Wpedantic
,
23867 "ISO C++ allows bit-field attributes only "
23868 "before the %<:%> token");
23870 late_attributes
= cp_parser_attributes_opt (parser
);
23873 attributes
= attr_chainon (attributes
, late_attributes
);
23875 /* Remember which attributes are prefix attributes and
23877 first_attribute
= attributes
;
23878 /* Combine the attributes. */
23879 attributes
= attr_chainon (prefix_attributes
, attributes
);
23881 /* Create the bitfield declaration. */
23882 decl
= grokbitfield (identifier
23883 ? make_id_declarator (NULL_TREE
,
23888 width
, initializer
,
23893 cp_declarator
*declarator
;
23894 tree asm_specification
;
23895 int ctor_dtor_or_conv_p
;
23897 /* Parse the declarator. */
23899 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23900 &ctor_dtor_or_conv_p
,
23901 /*parenthesized_p=*/NULL
,
23905 /* If something went wrong parsing the declarator, make sure
23906 that we at least consume some tokens. */
23907 if (declarator
== cp_error_declarator
)
23909 /* Skip to the end of the statement. */
23910 cp_parser_skip_to_end_of_statement (parser
);
23911 /* If the next token is not a semicolon, that is
23912 probably because we just skipped over the body of
23913 a function. So, we consume a semicolon if
23914 present, but do not issue an error message if it
23916 if (cp_lexer_next_token_is (parser
->lexer
,
23918 cp_lexer_consume_token (parser
->lexer
);
23922 if (declares_class_or_enum
& 2)
23923 cp_parser_check_for_definition_in_return_type
23924 (declarator
, decl_specifiers
.type
,
23925 decl_specifiers
.locations
[ds_type_spec
]);
23927 /* Look for an asm-specification. */
23928 asm_specification
= cp_parser_asm_specification_opt (parser
);
23929 /* Look for attributes that apply to the declaration. */
23930 attributes
= cp_parser_attributes_opt (parser
);
23931 /* Remember which attributes are prefix attributes and
23933 first_attribute
= attributes
;
23934 /* Combine the attributes. */
23935 attributes
= attr_chainon (prefix_attributes
, attributes
);
23937 /* If it's an `=', then we have a constant-initializer or a
23938 pure-specifier. It is not correct to parse the
23939 initializer before registering the member declaration
23940 since the member declaration should be in scope while
23941 its initializer is processed. However, the rest of the
23942 front end does not yet provide an interface that allows
23943 us to handle this correctly. */
23944 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23948 A pure-specifier shall be used only in the declaration of
23949 a virtual function.
23951 A member-declarator can contain a constant-initializer
23952 only if it declares a static member of integral or
23955 Therefore, if the DECLARATOR is for a function, we look
23956 for a pure-specifier; otherwise, we look for a
23957 constant-initializer. When we call `grokfield', it will
23958 perform more stringent semantics checks. */
23959 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23960 if (function_declarator_p (declarator
)
23961 || (decl_specifiers
.type
23962 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23963 && declarator
->kind
== cdk_id
23964 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23965 == FUNCTION_TYPE
)))
23966 initializer
= cp_parser_pure_specifier (parser
);
23967 else if (decl_specifiers
.storage_class
!= sc_static
)
23968 initializer
= cp_parser_save_nsdmi (parser
);
23969 else if (cxx_dialect
>= cxx11
)
23972 /* Don't require a constant rvalue in C++11, since we
23973 might want a reference constant. We'll enforce
23974 constancy later. */
23975 cp_lexer_consume_token (parser
->lexer
);
23976 /* Parse the initializer. */
23977 initializer
= cp_parser_initializer_clause (parser
,
23981 /* Parse the initializer. */
23982 initializer
= cp_parser_constant_initializer (parser
);
23984 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23985 && !function_declarator_p (declarator
))
23988 if (decl_specifiers
.storage_class
!= sc_static
)
23989 initializer
= cp_parser_save_nsdmi (parser
);
23991 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23993 /* Otherwise, there is no initializer. */
23995 initializer
= NULL_TREE
;
23997 /* See if we are probably looking at a function
23998 definition. We are certainly not looking at a
23999 member-declarator. Calling `grokfield' has
24000 side-effects, so we must not do it unless we are sure
24001 that we are looking at a member-declarator. */
24002 if (cp_parser_token_starts_function_definition_p
24003 (cp_lexer_peek_token (parser
->lexer
)))
24005 /* The grammar does not allow a pure-specifier to be
24006 used when a member function is defined. (It is
24007 possible that this fact is an oversight in the
24008 standard, since a pure function may be defined
24009 outside of the class-specifier. */
24010 if (initializer
&& initializer_token_start
)
24011 error_at (initializer_token_start
->location
,
24012 "pure-specifier on function-definition");
24013 decl
= cp_parser_save_member_function_body (parser
,
24017 if (parser
->fully_implicit_function_template_p
)
24018 decl
= finish_fully_implicit_template (parser
, decl
);
24019 /* If the member was not a friend, declare it here. */
24021 finish_member_declaration (decl
);
24022 /* Peek at the next token. */
24023 token
= cp_lexer_peek_token (parser
->lexer
);
24024 /* If the next token is a semicolon, consume it. */
24025 if (token
->type
== CPP_SEMICOLON
)
24027 location_t semicolon_loc
24028 = cp_lexer_consume_token (parser
->lexer
)->location
;
24029 gcc_rich_location
richloc (semicolon_loc
);
24030 richloc
.add_fixit_remove ();
24031 warning_at (&richloc
, OPT_Wextra_semi
,
24032 "extra %<;%> after in-class "
24033 "function definition");
24038 if (declarator
->kind
== cdk_function
)
24039 declarator
->id_loc
= token
->location
;
24040 /* Create the declaration. */
24041 decl
= grokfield (declarator
, &decl_specifiers
,
24042 initializer
, /*init_const_expr_p=*/true,
24043 asm_specification
, attributes
);
24044 if (parser
->fully_implicit_function_template_p
)
24047 finish_fully_implicit_template (parser
, 0);
24049 decl
= finish_fully_implicit_template (parser
, decl
);
24053 cp_finalize_omp_declare_simd (parser
, decl
);
24054 cp_finalize_oacc_routine (parser
, decl
, false);
24056 /* Reset PREFIX_ATTRIBUTES. */
24057 if (attributes
!= error_mark_node
)
24059 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24060 attributes
= TREE_CHAIN (attributes
);
24062 TREE_CHAIN (attributes
) = NULL_TREE
;
24065 /* If there is any qualification still in effect, clear it
24066 now; we will be starting fresh with the next declarator. */
24067 parser
->scope
= NULL_TREE
;
24068 parser
->qualifying_scope
= NULL_TREE
;
24069 parser
->object_scope
= NULL_TREE
;
24070 /* If it's a `,', then there are more declarators. */
24071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24073 cp_lexer_consume_token (parser
->lexer
);
24074 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24076 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24077 gcc_rich_location
richloc (token
->location
);
24078 richloc
.add_fixit_remove ();
24079 error_at (&richloc
, "stray %<,%> at end of "
24080 "member declaration");
24083 /* If the next token isn't a `;', then we have a parse error. */
24084 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24087 /* The next token might be a ways away from where the
24088 actual semicolon is missing. Find the previous token
24089 and use that for our error position. */
24090 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24091 gcc_rich_location
richloc (token
->location
);
24092 richloc
.add_fixit_insert_after (";");
24093 error_at (&richloc
, "expected %<;%> at end of "
24094 "member declaration");
24096 /* Assume that the user meant to provide a semicolon. If
24097 we were to cp_parser_skip_to_end_of_statement, we might
24098 skip to a semicolon inside a member function definition
24099 and issue nonsensical error messages. */
24100 assume_semicolon
= true;
24105 /* Add DECL to the list of members. */
24107 /* Explicitly include, eg, NSDMIs, for better error
24108 recovery (c++/58650). */
24109 || !DECL_DECLARES_FUNCTION_P (decl
))
24110 finish_member_declaration (decl
);
24112 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24113 cp_parser_save_default_args (parser
, decl
);
24114 else if (TREE_CODE (decl
) == FIELD_DECL
24115 && DECL_INITIAL (decl
))
24116 /* Add DECL to the queue of NSDMI to be parsed later. */
24117 vec_safe_push (unparsed_nsdmis
, decl
);
24120 if (assume_semicolon
)
24125 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24127 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24130 /* Parse a pure-specifier.
24135 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24136 Otherwise, ERROR_MARK_NODE is returned. */
24139 cp_parser_pure_specifier (cp_parser
* parser
)
24143 /* Look for the `=' token. */
24144 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24145 return error_mark_node
;
24146 /* Look for the `0' token. */
24147 token
= cp_lexer_peek_token (parser
->lexer
);
24149 if (token
->type
== CPP_EOF
24150 || token
->type
== CPP_PRAGMA_EOL
)
24151 return error_mark_node
;
24153 cp_lexer_consume_token (parser
->lexer
);
24155 /* Accept = default or = delete in c++0x mode. */
24156 if (token
->keyword
== RID_DEFAULT
24157 || token
->keyword
== RID_DELETE
)
24159 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24160 return token
->u
.value
;
24163 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24164 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24166 cp_parser_error (parser
,
24167 "invalid pure specifier (only %<= 0%> is allowed)");
24168 cp_parser_skip_to_end_of_statement (parser
);
24169 return error_mark_node
;
24171 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24173 error_at (token
->location
, "templates may not be %<virtual%>");
24174 return error_mark_node
;
24177 return integer_zero_node
;
24180 /* Parse a constant-initializer.
24182 constant-initializer:
24183 = constant-expression
24185 Returns a representation of the constant-expression. */
24188 cp_parser_constant_initializer (cp_parser
* parser
)
24190 /* Look for the `=' token. */
24191 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24192 return error_mark_node
;
24194 /* It is invalid to write:
24196 struct S { static const int i = { 7 }; };
24199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24201 cp_parser_error (parser
,
24202 "a brace-enclosed initializer is not allowed here");
24203 /* Consume the opening brace. */
24204 matching_braces braces
;
24205 braces
.consume_open (parser
);
24206 /* Skip the initializer. */
24207 cp_parser_skip_to_closing_brace (parser
);
24208 /* Look for the trailing `}'. */
24209 braces
.require_close (parser
);
24211 return error_mark_node
;
24214 return cp_parser_constant_expression (parser
);
24217 /* Derived classes [gram.class.derived] */
24219 /* Parse a base-clause.
24222 : base-specifier-list
24224 base-specifier-list:
24225 base-specifier ... [opt]
24226 base-specifier-list , base-specifier ... [opt]
24228 Returns a TREE_LIST representing the base-classes, in the order in
24229 which they were declared. The representation of each node is as
24230 described by cp_parser_base_specifier.
24232 In the case that no bases are specified, this function will return
24233 NULL_TREE, not ERROR_MARK_NODE. */
24236 cp_parser_base_clause (cp_parser
* parser
)
24238 tree bases
= NULL_TREE
;
24240 /* Look for the `:' that begins the list. */
24241 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24243 /* Scan the base-specifier-list. */
24248 bool pack_expansion_p
= false;
24250 /* Look for the base-specifier. */
24251 base
= cp_parser_base_specifier (parser
);
24252 /* Look for the (optional) ellipsis. */
24253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24255 /* Consume the `...'. */
24256 cp_lexer_consume_token (parser
->lexer
);
24258 pack_expansion_p
= true;
24261 /* Add BASE to the front of the list. */
24262 if (base
&& base
!= error_mark_node
)
24264 if (pack_expansion_p
)
24265 /* Make this a pack expansion type. */
24266 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24268 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24270 TREE_CHAIN (base
) = bases
;
24274 /* Peek at the next token. */
24275 token
= cp_lexer_peek_token (parser
->lexer
);
24276 /* If it's not a comma, then the list is complete. */
24277 if (token
->type
!= CPP_COMMA
)
24279 /* Consume the `,'. */
24280 cp_lexer_consume_token (parser
->lexer
);
24283 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24284 base class had a qualified name. However, the next name that
24285 appears is certainly not qualified. */
24286 parser
->scope
= NULL_TREE
;
24287 parser
->qualifying_scope
= NULL_TREE
;
24288 parser
->object_scope
= NULL_TREE
;
24290 return nreverse (bases
);
24293 /* Parse a base-specifier.
24296 :: [opt] nested-name-specifier [opt] class-name
24297 virtual access-specifier [opt] :: [opt] nested-name-specifier
24299 access-specifier virtual [opt] :: [opt] nested-name-specifier
24302 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24303 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24304 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24305 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24308 cp_parser_base_specifier (cp_parser
* parser
)
24312 bool virtual_p
= false;
24313 bool duplicate_virtual_error_issued_p
= false;
24314 bool duplicate_access_error_issued_p
= false;
24315 bool class_scope_p
, template_p
;
24316 tree access
= access_default_node
;
24319 /* Process the optional `virtual' and `access-specifier'. */
24322 /* Peek at the next token. */
24323 token
= cp_lexer_peek_token (parser
->lexer
);
24324 /* Process `virtual'. */
24325 switch (token
->keyword
)
24328 /* If `virtual' appears more than once, issue an error. */
24329 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24331 cp_parser_error (parser
,
24332 "%<virtual%> specified more than once in base-specifier");
24333 duplicate_virtual_error_issued_p
= true;
24338 /* Consume the `virtual' token. */
24339 cp_lexer_consume_token (parser
->lexer
);
24344 case RID_PROTECTED
:
24346 /* If more than one access specifier appears, issue an
24348 if (access
!= access_default_node
24349 && !duplicate_access_error_issued_p
)
24351 cp_parser_error (parser
,
24352 "more than one access specifier in base-specifier");
24353 duplicate_access_error_issued_p
= true;
24356 access
= ridpointers
[(int) token
->keyword
];
24358 /* Consume the access-specifier. */
24359 cp_lexer_consume_token (parser
->lexer
);
24368 /* It is not uncommon to see programs mechanically, erroneously, use
24369 the 'typename' keyword to denote (dependent) qualified types
24370 as base classes. */
24371 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24373 token
= cp_lexer_peek_token (parser
->lexer
);
24374 if (!processing_template_decl
)
24375 error_at (token
->location
,
24376 "keyword %<typename%> not allowed outside of templates");
24378 error_at (token
->location
,
24379 "keyword %<typename%> not allowed in this context "
24380 "(the base class is implicitly a type)");
24381 cp_lexer_consume_token (parser
->lexer
);
24384 /* Look for the optional `::' operator. */
24385 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24386 /* Look for the nested-name-specifier. The simplest way to
24391 The keyword `typename' is not permitted in a base-specifier or
24392 mem-initializer; in these contexts a qualified name that
24393 depends on a template-parameter is implicitly assumed to be a
24396 is to pretend that we have seen the `typename' keyword at this
24398 cp_parser_nested_name_specifier_opt (parser
,
24399 /*typename_keyword_p=*/true,
24400 /*check_dependency_p=*/true,
24402 /*is_declaration=*/true);
24403 /* If the base class is given by a qualified name, assume that names
24404 we see are type names or templates, as appropriate. */
24405 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24406 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24409 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24410 /* DR 950 allows decltype as a base-specifier. */
24411 type
= cp_parser_decltype (parser
);
24414 /* Otherwise, look for the class-name. */
24415 type
= cp_parser_class_name (parser
,
24419 /*check_dependency_p=*/true,
24420 /*class_head_p=*/false,
24421 /*is_declaration=*/true);
24422 type
= TREE_TYPE (type
);
24425 if (type
== error_mark_node
)
24426 return error_mark_node
;
24428 return finish_base_specifier (type
, access
, virtual_p
);
24431 /* Exception handling [gram.exception] */
24433 /* Parse an (optional) noexcept-specification.
24435 noexcept-specification:
24436 noexcept ( constant-expression ) [opt]
24438 If no noexcept-specification is present, returns NULL_TREE.
24439 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24440 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24441 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24442 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24443 in which case a boolean condition is returned instead. */
24446 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24447 bool require_constexpr
,
24448 bool* consumed_expr
,
24452 const char *saved_message
;
24454 /* Peek at the next token. */
24455 token
= cp_lexer_peek_token (parser
->lexer
);
24457 /* Is it a noexcept-specification? */
24458 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24461 cp_lexer_consume_token (parser
->lexer
);
24463 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24465 matching_parens parens
;
24466 parens
.consume_open (parser
);
24468 if (require_constexpr
)
24470 /* Types may not be defined in an exception-specification. */
24471 saved_message
= parser
->type_definition_forbidden_message
;
24472 parser
->type_definition_forbidden_message
24473 = G_("types may not be defined in an exception-specification");
24475 expr
= cp_parser_constant_expression (parser
);
24477 /* Restore the saved message. */
24478 parser
->type_definition_forbidden_message
= saved_message
;
24482 expr
= cp_parser_expression (parser
);
24483 *consumed_expr
= true;
24486 parens
.require_close (parser
);
24490 expr
= boolean_true_node
;
24491 if (!require_constexpr
)
24492 *consumed_expr
= false;
24495 /* We cannot build a noexcept-spec right away because this will check
24496 that expr is a constexpr. */
24498 return build_noexcept_spec (expr
, tf_warning_or_error
);
24506 /* Parse an (optional) exception-specification.
24508 exception-specification:
24509 throw ( type-id-list [opt] )
24511 Returns a TREE_LIST representing the exception-specification. The
24512 TREE_VALUE of each node is a type. */
24515 cp_parser_exception_specification_opt (cp_parser
* parser
)
24519 const char *saved_message
;
24521 /* Peek at the next token. */
24522 token
= cp_lexer_peek_token (parser
->lexer
);
24524 /* Is it a noexcept-specification? */
24525 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24527 if (type_id_list
!= NULL_TREE
)
24528 return type_id_list
;
24530 /* If it's not `throw', then there's no exception-specification. */
24531 if (!cp_parser_is_keyword (token
, RID_THROW
))
24534 location_t loc
= token
->location
;
24536 /* Consume the `throw'. */
24537 cp_lexer_consume_token (parser
->lexer
);
24539 /* Look for the `('. */
24540 matching_parens parens
;
24541 parens
.require_open (parser
);
24543 /* Peek at the next token. */
24544 token
= cp_lexer_peek_token (parser
->lexer
);
24545 /* If it's not a `)', then there is a type-id-list. */
24546 if (token
->type
!= CPP_CLOSE_PAREN
)
24548 /* Types may not be defined in an exception-specification. */
24549 saved_message
= parser
->type_definition_forbidden_message
;
24550 parser
->type_definition_forbidden_message
24551 = G_("types may not be defined in an exception-specification");
24552 /* Parse the type-id-list. */
24553 type_id_list
= cp_parser_type_id_list (parser
);
24554 /* Restore the saved message. */
24555 parser
->type_definition_forbidden_message
= saved_message
;
24557 if (cxx_dialect
>= cxx17
)
24559 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24561 type_id_list
= NULL_TREE
;
24563 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24564 warning_at (loc
, OPT_Wdeprecated
,
24565 "dynamic exception specifications are deprecated in "
24568 /* In C++17, throw() is equivalent to noexcept (true). throw()
24569 is deprecated in C++11 and above as well, but is still widely used,
24570 so don't warn about it yet. */
24571 else if (cxx_dialect
>= cxx17
)
24572 type_id_list
= noexcept_true_spec
;
24574 type_id_list
= empty_except_spec
;
24576 /* Look for the `)'. */
24577 parens
.require_close (parser
);
24579 return type_id_list
;
24582 /* Parse an (optional) type-id-list.
24586 type-id-list , type-id ... [opt]
24588 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24589 in the order that the types were presented. */
24592 cp_parser_type_id_list (cp_parser
* parser
)
24594 tree types
= NULL_TREE
;
24601 token
= cp_lexer_peek_token (parser
->lexer
);
24603 /* Get the next type-id. */
24604 type
= cp_parser_type_id (parser
);
24605 /* Check for invalid 'auto'. */
24606 if (flag_concepts
&& type_uses_auto (type
))
24608 error_at (token
->location
,
24609 "invalid use of %<auto%> in exception-specification");
24610 type
= error_mark_node
;
24612 /* Parse the optional ellipsis. */
24613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24615 /* Consume the `...'. */
24616 cp_lexer_consume_token (parser
->lexer
);
24618 /* Turn the type into a pack expansion expression. */
24619 type
= make_pack_expansion (type
);
24621 /* Add it to the list. */
24622 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24623 /* Peek at the next token. */
24624 token
= cp_lexer_peek_token (parser
->lexer
);
24625 /* If it is not a `,', we are done. */
24626 if (token
->type
!= CPP_COMMA
)
24628 /* Consume the `,'. */
24629 cp_lexer_consume_token (parser
->lexer
);
24632 return nreverse (types
);
24635 /* Parse a try-block.
24638 try compound-statement handler-seq */
24641 cp_parser_try_block (cp_parser
* parser
)
24645 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24646 if (parser
->in_function_body
24647 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24648 error ("%<try%> in %<constexpr%> function");
24650 try_block
= begin_try_block ();
24651 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24652 finish_try_block (try_block
);
24653 cp_parser_handler_seq (parser
);
24654 finish_handler_sequence (try_block
);
24659 /* Parse a function-try-block.
24661 function-try-block:
24662 try ctor-initializer [opt] function-body handler-seq */
24665 cp_parser_function_try_block (cp_parser
* parser
)
24667 tree compound_stmt
;
24670 /* Look for the `try' keyword. */
24671 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24673 /* Let the rest of the front end know where we are. */
24674 try_block
= begin_function_try_block (&compound_stmt
);
24675 /* Parse the function-body. */
24676 cp_parser_ctor_initializer_opt_and_function_body
24677 (parser
, /*in_function_try_block=*/true);
24678 /* We're done with the `try' part. */
24679 finish_function_try_block (try_block
);
24680 /* Parse the handlers. */
24681 cp_parser_handler_seq (parser
);
24682 /* We're done with the handlers. */
24683 finish_function_handler_sequence (try_block
, compound_stmt
);
24686 /* Parse a handler-seq.
24689 handler handler-seq [opt] */
24692 cp_parser_handler_seq (cp_parser
* parser
)
24698 /* Parse the handler. */
24699 cp_parser_handler (parser
);
24700 /* Peek at the next token. */
24701 token
= cp_lexer_peek_token (parser
->lexer
);
24702 /* If it's not `catch' then there are no more handlers. */
24703 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24708 /* Parse a handler.
24711 catch ( exception-declaration ) compound-statement */
24714 cp_parser_handler (cp_parser
* parser
)
24719 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24720 handler
= begin_handler ();
24721 matching_parens parens
;
24722 parens
.require_open (parser
);
24723 declaration
= cp_parser_exception_declaration (parser
);
24724 finish_handler_parms (declaration
, handler
);
24725 parens
.require_close (parser
);
24726 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24727 finish_handler (handler
);
24730 /* Parse an exception-declaration.
24732 exception-declaration:
24733 type-specifier-seq declarator
24734 type-specifier-seq abstract-declarator
24738 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24739 ellipsis variant is used. */
24742 cp_parser_exception_declaration (cp_parser
* parser
)
24744 cp_decl_specifier_seq type_specifiers
;
24745 cp_declarator
*declarator
;
24746 const char *saved_message
;
24748 /* If it's an ellipsis, it's easy to handle. */
24749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24751 /* Consume the `...' token. */
24752 cp_lexer_consume_token (parser
->lexer
);
24756 /* Types may not be defined in exception-declarations. */
24757 saved_message
= parser
->type_definition_forbidden_message
;
24758 parser
->type_definition_forbidden_message
24759 = G_("types may not be defined in exception-declarations");
24761 /* Parse the type-specifier-seq. */
24762 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24763 /*is_trailing_return=*/false,
24765 /* If it's a `)', then there is no declarator. */
24766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24769 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24770 /*ctor_dtor_or_conv_p=*/NULL
,
24771 /*parenthesized_p=*/NULL
,
24772 /*member_p=*/false,
24773 /*friend_p=*/false);
24775 /* Restore the saved message. */
24776 parser
->type_definition_forbidden_message
= saved_message
;
24778 if (!type_specifiers
.any_specifiers_p
)
24779 return error_mark_node
;
24781 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24784 /* Parse a throw-expression.
24787 throw assignment-expression [opt]
24789 Returns a THROW_EXPR representing the throw-expression. */
24792 cp_parser_throw_expression (cp_parser
* parser
)
24797 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24798 token
= cp_lexer_peek_token (parser
->lexer
);
24799 /* Figure out whether or not there is an assignment-expression
24800 following the "throw" keyword. */
24801 if (token
->type
== CPP_COMMA
24802 || token
->type
== CPP_SEMICOLON
24803 || token
->type
== CPP_CLOSE_PAREN
24804 || token
->type
== CPP_CLOSE_SQUARE
24805 || token
->type
== CPP_CLOSE_BRACE
24806 || token
->type
== CPP_COLON
)
24807 expression
= NULL_TREE
;
24809 expression
= cp_parser_assignment_expression (parser
);
24811 return build_throw (expression
);
24814 /* GNU Extensions */
24816 /* Parse an (optional) asm-specification.
24819 asm ( string-literal )
24821 If the asm-specification is present, returns a STRING_CST
24822 corresponding to the string-literal. Otherwise, returns
24826 cp_parser_asm_specification_opt (cp_parser
* parser
)
24829 tree asm_specification
;
24831 /* Peek at the next token. */
24832 token
= cp_lexer_peek_token (parser
->lexer
);
24833 /* If the next token isn't the `asm' keyword, then there's no
24834 asm-specification. */
24835 if (!cp_parser_is_keyword (token
, RID_ASM
))
24838 /* Consume the `asm' token. */
24839 cp_lexer_consume_token (parser
->lexer
);
24840 /* Look for the `('. */
24841 matching_parens parens
;
24842 parens
.require_open (parser
);
24844 /* Look for the string-literal. */
24845 asm_specification
= cp_parser_string_literal (parser
, false, false);
24847 /* Look for the `)'. */
24848 parens
.require_close (parser
);
24850 return asm_specification
;
24853 /* Parse an asm-operand-list.
24857 asm-operand-list , asm-operand
24860 string-literal ( expression )
24861 [ string-literal ] string-literal ( expression )
24863 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24864 each node is the expression. The TREE_PURPOSE is itself a
24865 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24866 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24867 is a STRING_CST for the string literal before the parenthesis. Returns
24868 ERROR_MARK_NODE if any of the operands are invalid. */
24871 cp_parser_asm_operand_list (cp_parser
* parser
)
24873 tree asm_operands
= NULL_TREE
;
24874 bool invalid_operands
= false;
24878 tree string_literal
;
24882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24884 /* Consume the `[' token. */
24885 cp_lexer_consume_token (parser
->lexer
);
24886 /* Read the operand name. */
24887 name
= cp_parser_identifier (parser
);
24888 if (name
!= error_mark_node
)
24889 name
= build_string (IDENTIFIER_LENGTH (name
),
24890 IDENTIFIER_POINTER (name
));
24891 /* Look for the closing `]'. */
24892 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24896 /* Look for the string-literal. */
24897 string_literal
= cp_parser_string_literal (parser
, false, false);
24899 /* Look for the `('. */
24900 matching_parens parens
;
24901 parens
.require_open (parser
);
24902 /* Parse the expression. */
24903 expression
= cp_parser_expression (parser
);
24904 /* Look for the `)'. */
24905 parens
.require_close (parser
);
24907 if (name
== error_mark_node
24908 || string_literal
== error_mark_node
24909 || expression
== error_mark_node
)
24910 invalid_operands
= true;
24912 /* Add this operand to the list. */
24913 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24916 /* If the next token is not a `,', there are no more
24918 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24920 /* Consume the `,'. */
24921 cp_lexer_consume_token (parser
->lexer
);
24924 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24927 /* Parse an asm-clobber-list.
24931 asm-clobber-list , string-literal
24933 Returns a TREE_LIST, indicating the clobbers in the order that they
24934 appeared. The TREE_VALUE of each node is a STRING_CST. */
24937 cp_parser_asm_clobber_list (cp_parser
* parser
)
24939 tree clobbers
= NULL_TREE
;
24943 tree string_literal
;
24945 /* Look for the string literal. */
24946 string_literal
= cp_parser_string_literal (parser
, false, false);
24947 /* Add it to the list. */
24948 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24949 /* If the next token is not a `,', then the list is
24951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24953 /* Consume the `,' token. */
24954 cp_lexer_consume_token (parser
->lexer
);
24960 /* Parse an asm-label-list.
24964 asm-label-list , identifier
24966 Returns a TREE_LIST, indicating the labels in the order that they
24967 appeared. The TREE_VALUE of each node is a label. */
24970 cp_parser_asm_label_list (cp_parser
* parser
)
24972 tree labels
= NULL_TREE
;
24976 tree identifier
, label
, name
;
24978 /* Look for the identifier. */
24979 identifier
= cp_parser_identifier (parser
);
24980 if (!error_operand_p (identifier
))
24982 label
= lookup_label (identifier
);
24983 if (TREE_CODE (label
) == LABEL_DECL
)
24985 TREE_USED (label
) = 1;
24986 check_goto (label
);
24987 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24988 IDENTIFIER_POINTER (identifier
));
24989 labels
= tree_cons (name
, label
, labels
);
24992 /* If the next token is not a `,', then the list is
24994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24996 /* Consume the `,' token. */
24997 cp_lexer_consume_token (parser
->lexer
);
25000 return nreverse (labels
);
25003 /* Return TRUE iff the next tokens in the stream are possibly the
25004 beginning of a GNU extension attribute. */
25007 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25009 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25012 /* Return TRUE iff the next tokens in the stream are possibly the
25013 beginning of a standard C++-11 attribute specifier. */
25016 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25018 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25021 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25022 beginning of a standard C++-11 attribute specifier. */
25025 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25027 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25029 return (cxx_dialect
>= cxx11
25030 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25031 || (token
->type
== CPP_OPEN_SQUARE
25032 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25033 && token
->type
== CPP_OPEN_SQUARE
)));
25036 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25037 beginning of a GNU extension attribute. */
25040 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25042 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25044 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25047 /* Return true iff the next tokens can be the beginning of either a
25048 GNU attribute list, or a standard C++11 attribute sequence. */
25051 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25053 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25054 || cp_next_tokens_can_be_std_attribute_p (parser
));
25057 /* Return true iff the next Nth tokens can be the beginning of either
25058 a GNU attribute list, or a standard C++11 attribute sequence. */
25061 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25063 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25064 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25067 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25068 of GNU attributes, or return NULL. */
25071 cp_parser_attributes_opt (cp_parser
*parser
)
25073 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25074 return cp_parser_gnu_attributes_opt (parser
);
25075 return cp_parser_std_attribute_spec_seq (parser
);
25078 /* Parse an (optional) series of attributes.
25081 attributes attribute
25084 __attribute__ (( attribute-list [opt] ))
25086 The return value is as for cp_parser_gnu_attribute_list. */
25089 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25091 tree attributes
= NULL_TREE
;
25093 temp_override
<bool> cleanup
25094 (parser
->auto_is_implicit_function_template_parm_p
, false);
25099 tree attribute_list
;
25102 /* Peek at the next token. */
25103 token
= cp_lexer_peek_token (parser
->lexer
);
25104 /* If it's not `__attribute__', then we're done. */
25105 if (token
->keyword
!= RID_ATTRIBUTE
)
25108 /* Consume the `__attribute__' keyword. */
25109 cp_lexer_consume_token (parser
->lexer
);
25110 /* Look for the two `(' tokens. */
25111 matching_parens outer_parens
;
25112 outer_parens
.require_open (parser
);
25113 matching_parens inner_parens
;
25114 inner_parens
.require_open (parser
);
25116 /* Peek at the next token. */
25117 token
= cp_lexer_peek_token (parser
->lexer
);
25118 if (token
->type
!= CPP_CLOSE_PAREN
)
25119 /* Parse the attribute-list. */
25120 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25122 /* If the next token is a `)', then there is no attribute
25124 attribute_list
= NULL
;
25126 /* Look for the two `)' tokens. */
25127 if (!inner_parens
.require_close (parser
))
25129 if (!outer_parens
.require_close (parser
))
25132 cp_parser_skip_to_end_of_statement (parser
);
25134 /* Add these new attributes to the list. */
25135 attributes
= attr_chainon (attributes
, attribute_list
);
25141 /* Parse a GNU attribute-list.
25145 attribute-list , attribute
25149 identifier ( identifier )
25150 identifier ( identifier , expression-list )
25151 identifier ( expression-list )
25153 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25154 to an attribute. The TREE_PURPOSE of each node is the identifier
25155 indicating which attribute is in use. The TREE_VALUE represents
25156 the arguments, if any. */
25159 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25161 tree attribute_list
= NULL_TREE
;
25162 bool save_translate_strings_p
= parser
->translate_strings_p
;
25164 parser
->translate_strings_p
= false;
25171 /* Look for the identifier. We also allow keywords here; for
25172 example `__attribute__ ((const))' is legal. */
25173 token
= cp_lexer_peek_token (parser
->lexer
);
25174 if (token
->type
== CPP_NAME
25175 || token
->type
== CPP_KEYWORD
)
25177 tree arguments
= NULL_TREE
;
25179 /* Consume the token, but save it since we need it for the
25180 SIMD enabled function parsing. */
25181 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25183 /* Save away the identifier that indicates which attribute
25185 identifier
= (token
->type
== CPP_KEYWORD
)
25186 /* For keywords, use the canonical spelling, not the
25187 parsed identifier. */
25188 ? ridpointers
[(int) token
->keyword
]
25189 : id_token
->u
.value
;
25191 identifier
= canonicalize_attr_name (identifier
);
25192 attribute
= build_tree_list (identifier
, NULL_TREE
);
25194 /* Peek at the next token. */
25195 token
= cp_lexer_peek_token (parser
->lexer
);
25196 /* If it's an `(', then parse the attribute arguments. */
25197 if (token
->type
== CPP_OPEN_PAREN
)
25199 vec
<tree
, va_gc
> *vec
;
25200 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25201 ? id_attr
: normal_attr
);
25202 vec
= cp_parser_parenthesized_expression_list
25203 (parser
, attr_flag
, /*cast_p=*/false,
25204 /*allow_expansion_p=*/false,
25205 /*non_constant_p=*/NULL
);
25207 arguments
= error_mark_node
;
25210 arguments
= build_tree_list_vec (vec
);
25211 release_tree_vector (vec
);
25213 /* Save the arguments away. */
25214 TREE_VALUE (attribute
) = arguments
;
25217 if (arguments
!= error_mark_node
)
25219 /* Add this attribute to the list. */
25220 TREE_CHAIN (attribute
) = attribute_list
;
25221 attribute_list
= attribute
;
25224 token
= cp_lexer_peek_token (parser
->lexer
);
25226 /* Now, look for more attributes. If the next token isn't a
25227 `,', we're done. */
25228 if (token
->type
!= CPP_COMMA
)
25231 /* Consume the comma and keep going. */
25232 cp_lexer_consume_token (parser
->lexer
);
25234 parser
->translate_strings_p
= save_translate_strings_p
;
25236 /* We built up the list in reverse order. */
25237 return nreverse (attribute_list
);
25240 /* Parse a standard C++11 attribute.
25242 The returned representation is a TREE_LIST which TREE_PURPOSE is
25243 the scoped name of the attribute, and the TREE_VALUE is its
25246 Note that the scoped name of the attribute is itself a TREE_LIST
25247 which TREE_PURPOSE is the namespace of the attribute, and
25248 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25249 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25250 and which TREE_PURPOSE is directly the attribute name.
25252 Clients of the attribute code should use get_attribute_namespace
25253 and get_attribute_name to get the actual namespace and name of
25254 attributes, regardless of their being GNU or C++11 attributes.
25257 attribute-token attribute-argument-clause [opt]
25261 attribute-scoped-token
25263 attribute-scoped-token:
25264 attribute-namespace :: identifier
25266 attribute-namespace:
25269 attribute-argument-clause:
25270 ( balanced-token-seq )
25272 balanced-token-seq:
25273 balanced-token [opt]
25274 balanced-token-seq balanced-token
25277 ( balanced-token-seq )
25278 [ balanced-token-seq ]
25279 { balanced-token-seq }. */
25282 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25284 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25287 temp_override
<bool> cleanup
25288 (parser
->auto_is_implicit_function_template_parm_p
, false);
25290 /* First, parse name of the attribute, a.k.a attribute-token. */
25292 token
= cp_lexer_peek_token (parser
->lexer
);
25293 if (token
->type
== CPP_NAME
)
25294 attr_id
= token
->u
.value
;
25295 else if (token
->type
== CPP_KEYWORD
)
25296 attr_id
= ridpointers
[(int) token
->keyword
];
25297 else if (token
->flags
& NAMED_OP
)
25298 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25300 if (attr_id
== NULL_TREE
)
25303 cp_lexer_consume_token (parser
->lexer
);
25305 token
= cp_lexer_peek_token (parser
->lexer
);
25306 if (token
->type
== CPP_SCOPE
)
25308 /* We are seeing a scoped attribute token. */
25310 cp_lexer_consume_token (parser
->lexer
);
25312 error_at (token
->location
, "attribute using prefix used together "
25313 "with scoped attribute token");
25316 token
= cp_lexer_consume_token (parser
->lexer
);
25317 if (token
->type
== CPP_NAME
)
25318 attr_id
= token
->u
.value
;
25319 else if (token
->type
== CPP_KEYWORD
)
25320 attr_id
= ridpointers
[(int) token
->keyword
];
25321 else if (token
->flags
& NAMED_OP
)
25322 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25325 error_at (token
->location
,
25326 "expected an identifier for the attribute name");
25327 return error_mark_node
;
25330 attr_ns
= canonicalize_attr_name (attr_ns
);
25331 attr_id
= canonicalize_attr_name (attr_id
);
25332 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25334 token
= cp_lexer_peek_token (parser
->lexer
);
25338 attr_ns
= canonicalize_attr_name (attr_ns
);
25339 attr_id
= canonicalize_attr_name (attr_id
);
25340 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25345 attr_id
= canonicalize_attr_name (attr_id
);
25346 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25348 /* C++11 noreturn attribute is equivalent to GNU's. */
25349 if (is_attribute_p ("noreturn", attr_id
))
25350 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25351 /* C++14 deprecated attribute is equivalent to GNU's. */
25352 else if (is_attribute_p ("deprecated", attr_id
))
25353 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25354 /* C++17 fallthrough attribute is equivalent to GNU's. */
25355 else if (is_attribute_p ("fallthrough", attr_id
))
25356 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25357 /* Transactional Memory TS optimize_for_synchronized attribute is
25358 equivalent to GNU transaction_callable. */
25359 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25360 TREE_PURPOSE (attribute
)
25361 = get_identifier ("transaction_callable");
25362 /* Transactional Memory attributes are GNU attributes. */
25363 else if (tm_attr_to_mask (attr_id
))
25364 TREE_PURPOSE (attribute
) = attr_id
;
25367 /* Now parse the optional argument clause of the attribute. */
25369 if (token
->type
!= CPP_OPEN_PAREN
)
25373 vec
<tree
, va_gc
> *vec
;
25374 int attr_flag
= normal_attr
;
25376 if (attr_ns
== gnu_identifier
25377 && attribute_takes_identifier_p (attr_id
))
25378 /* A GNU attribute that takes an identifier in parameter. */
25379 attr_flag
= id_attr
;
25381 vec
= cp_parser_parenthesized_expression_list
25382 (parser
, attr_flag
, /*cast_p=*/false,
25383 /*allow_expansion_p=*/true,
25384 /*non_constant_p=*/NULL
);
25386 arguments
= error_mark_node
;
25389 arguments
= build_tree_list_vec (vec
);
25390 release_tree_vector (vec
);
25393 if (arguments
== error_mark_node
)
25394 attribute
= error_mark_node
;
25396 TREE_VALUE (attribute
) = arguments
;
25402 /* Check that the attribute ATTRIBUTE appears at most once in the
25403 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25404 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25405 isn't implemented yet in GCC. */
25408 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25412 tree name
= get_attribute_name (attribute
);
25413 if (is_attribute_p ("noreturn", name
)
25414 && lookup_attribute ("noreturn", attributes
))
25415 error ("attribute %<noreturn%> can appear at most once "
25416 "in an attribute-list");
25417 else if (is_attribute_p ("deprecated", name
)
25418 && lookup_attribute ("deprecated", attributes
))
25419 error ("attribute %<deprecated%> can appear at most once "
25420 "in an attribute-list");
25424 /* Parse a list of standard C++-11 attributes.
25428 attribute-list , attribute[opt]
25430 attribute-list , attribute ...
25434 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25436 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25437 cp_token
*token
= NULL
;
25441 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25442 if (attribute
== error_mark_node
)
25444 if (attribute
!= NULL_TREE
)
25446 cp_parser_check_std_attribute (attributes
, attribute
);
25447 TREE_CHAIN (attribute
) = attributes
;
25448 attributes
= attribute
;
25450 token
= cp_lexer_peek_token (parser
->lexer
);
25451 if (token
->type
== CPP_ELLIPSIS
)
25453 cp_lexer_consume_token (parser
->lexer
);
25454 if (attribute
== NULL_TREE
)
25455 error_at (token
->location
,
25456 "expected attribute before %<...%>");
25459 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25460 if (pack
== error_mark_node
)
25461 return error_mark_node
;
25462 TREE_VALUE (attribute
) = pack
;
25464 token
= cp_lexer_peek_token (parser
->lexer
);
25466 if (token
->type
!= CPP_COMMA
)
25468 cp_lexer_consume_token (parser
->lexer
);
25470 attributes
= nreverse (attributes
);
25474 /* Parse a standard C++-11 attribute specifier.
25476 attribute-specifier:
25477 [ [ attribute-using-prefix [opt] attribute-list ] ]
25478 alignment-specifier
25480 attribute-using-prefix:
25481 using attribute-namespace :
25483 alignment-specifier:
25484 alignas ( type-id ... [opt] )
25485 alignas ( alignment-expression ... [opt] ). */
25488 cp_parser_std_attribute_spec (cp_parser
*parser
)
25490 tree attributes
= NULL_TREE
;
25491 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25493 if (token
->type
== CPP_OPEN_SQUARE
25494 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25496 tree attr_ns
= NULL_TREE
;
25498 cp_lexer_consume_token (parser
->lexer
);
25499 cp_lexer_consume_token (parser
->lexer
);
25501 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25503 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25504 if (token
->type
== CPP_NAME
)
25505 attr_ns
= token
->u
.value
;
25506 else if (token
->type
== CPP_KEYWORD
)
25507 attr_ns
= ridpointers
[(int) token
->keyword
];
25508 else if (token
->flags
& NAMED_OP
)
25509 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25512 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25514 if (cxx_dialect
< cxx17
25515 && !in_system_header_at (input_location
))
25516 pedwarn (input_location
, 0,
25517 "attribute using prefix only available "
25518 "with -std=c++17 or -std=gnu++17");
25520 cp_lexer_consume_token (parser
->lexer
);
25521 cp_lexer_consume_token (parser
->lexer
);
25522 cp_lexer_consume_token (parser
->lexer
);
25525 attr_ns
= NULL_TREE
;
25528 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25530 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25531 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25532 cp_parser_skip_to_end_of_statement (parser
);
25534 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25535 when we are sure that we have actually parsed them. */
25536 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25542 /* Look for an alignment-specifier. */
25544 token
= cp_lexer_peek_token (parser
->lexer
);
25546 if (token
->type
!= CPP_KEYWORD
25547 || token
->keyword
!= RID_ALIGNAS
)
25550 cp_lexer_consume_token (parser
->lexer
);
25551 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25553 matching_parens parens
;
25554 if (!parens
.require_open (parser
))
25555 return error_mark_node
;
25557 cp_parser_parse_tentatively (parser
);
25558 alignas_expr
= cp_parser_type_id (parser
);
25560 if (!cp_parser_parse_definitely (parser
))
25562 alignas_expr
= cp_parser_assignment_expression (parser
);
25563 if (alignas_expr
== error_mark_node
)
25564 cp_parser_skip_to_end_of_statement (parser
);
25565 if (alignas_expr
== NULL_TREE
25566 || alignas_expr
== error_mark_node
)
25567 return alignas_expr
;
25570 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25571 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25573 /* Handle alignas (pack...). */
25574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25576 cp_lexer_consume_token (parser
->lexer
);
25577 alignas_expr
= make_pack_expansion (alignas_expr
);
25580 /* Something went wrong, so don't build the attribute. */
25581 if (alignas_expr
== error_mark_node
)
25582 return error_mark_node
;
25584 if (!parens
.require_close (parser
))
25585 return error_mark_node
;
25587 /* Build the C++-11 representation of an 'aligned'
25590 = build_tree_list (build_tree_list (gnu_identifier
,
25591 aligned_identifier
), alignas_expr
);
25597 /* Parse a standard C++-11 attribute-specifier-seq.
25599 attribute-specifier-seq:
25600 attribute-specifier-seq [opt] attribute-specifier
25604 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25606 tree attr_specs
= NULL_TREE
;
25607 tree attr_last
= NULL_TREE
;
25611 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25612 if (attr_spec
== NULL_TREE
)
25614 if (attr_spec
== error_mark_node
)
25615 return error_mark_node
;
25618 TREE_CHAIN (attr_last
) = attr_spec
;
25620 attr_specs
= attr_last
= attr_spec
;
25621 attr_last
= tree_last (attr_last
);
25627 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25628 return index of the first token after balanced-token, or N on failure. */
25631 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25634 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25636 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25639 case CPP_PRAGMA_EOL
:
25640 /* Ran out of tokens. */
25642 case CPP_OPEN_PAREN
:
25645 case CPP_OPEN_BRACE
:
25648 case CPP_OPEN_SQUARE
:
25651 case CPP_CLOSE_PAREN
:
25654 case CPP_CLOSE_BRACE
:
25657 case CPP_CLOSE_SQUARE
:
25663 while (nparens
|| nbraces
|| nsquares
);
25667 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25668 return index of the first token after the GNU attribute tokens, or N on
25672 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25676 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25677 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25678 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25681 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25684 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25691 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25692 next token), return index of the first token after the standard C++11
25693 attribute tokens, or N on failure. */
25696 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25700 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25701 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25703 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25706 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25710 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25711 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25713 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25724 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25725 as the next token), return index of the first token after the attribute
25726 tokens, or N on failure. */
25729 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25731 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25732 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25733 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25736 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25737 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25738 current value of the PEDANTIC flag, regardless of whether or not
25739 the `__extension__' keyword is present. The caller is responsible
25740 for restoring the value of the PEDANTIC flag. */
25743 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25745 /* Save the old value of the PEDANTIC flag. */
25746 *saved_pedantic
= pedantic
;
25748 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25750 /* Consume the `__extension__' token. */
25751 cp_lexer_consume_token (parser
->lexer
);
25752 /* We're not being pedantic while the `__extension__' keyword is
25762 /* Parse a label declaration.
25765 __label__ label-declarator-seq ;
25767 label-declarator-seq:
25768 identifier , label-declarator-seq
25772 cp_parser_label_declaration (cp_parser
* parser
)
25774 /* Look for the `__label__' keyword. */
25775 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25781 /* Look for an identifier. */
25782 identifier
= cp_parser_identifier (parser
);
25783 /* If we failed, stop. */
25784 if (identifier
== error_mark_node
)
25786 /* Declare it as a label. */
25787 finish_label_decl (identifier
);
25788 /* If the next token is a `;', stop. */
25789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25791 /* Look for the `,' separating the label declarations. */
25792 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25795 /* Look for the final `;'. */
25796 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25799 // -------------------------------------------------------------------------- //
25802 // Parse a requires clause.
25804 // requires-clause:
25805 // 'requires' logical-or-expression
25807 // The required logical-or-expression must be a constant expression. Note
25808 // that we don't check that the expression is constepxr here. We defer until
25809 // we analyze constraints and then, we only check atomic constraints.
25811 cp_parser_requires_clause (cp_parser
*parser
)
25813 // Parse the requires clause so that it is not automatically folded.
25814 ++processing_template_decl
;
25815 tree expr
= cp_parser_binary_expression (parser
, false, false,
25816 PREC_NOT_OPERATOR
, NULL
);
25817 if (check_for_bare_parameter_packs (expr
))
25818 expr
= error_mark_node
;
25819 --processing_template_decl
;
25823 // Optionally parse a requires clause:
25825 cp_parser_requires_clause_opt (cp_parser
*parser
)
25827 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25828 if (tok
->keyword
!= RID_REQUIRES
)
25830 if (!flag_concepts
&& tok
->type
== CPP_NAME
25831 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25833 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25834 "%<requires%> only available with -fconcepts");
25835 /* Parse and discard the requires-clause. */
25836 cp_lexer_consume_token (parser
->lexer
);
25837 cp_parser_requires_clause (parser
);
25841 cp_lexer_consume_token (parser
->lexer
);
25842 return cp_parser_requires_clause (parser
);
25846 /*---------------------------------------------------------------------------
25847 Requires expressions
25848 ---------------------------------------------------------------------------*/
25850 /* Parse a requires expression
25852 requirement-expression:
25853 'requires' requirement-parameter-list [opt] requirement-body */
25855 cp_parser_requires_expression (cp_parser
*parser
)
25857 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25858 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25860 /* A requires-expression shall appear only within a concept
25861 definition or a requires-clause.
25863 TODO: Implement this diagnostic correctly. */
25864 if (!processing_template_decl
)
25866 error_at (loc
, "a requires expression cannot appear outside a template");
25867 cp_parser_skip_to_end_of_statement (parser
);
25868 return error_mark_node
;
25873 /* Local parameters are delared as variables within the scope
25874 of the expression. They are not visible past the end of
25875 the expression. Expressions within the requires-expression
25876 are unevaluated. */
25877 struct scope_sentinel
25881 ++cp_unevaluated_operand
;
25882 begin_scope (sk_block
, NULL_TREE
);
25887 pop_bindings_and_leave_scope ();
25888 --cp_unevaluated_operand
;
25892 /* Parse the optional parameter list. */
25893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25895 parms
= cp_parser_requirement_parameter_list (parser
);
25896 if (parms
== error_mark_node
)
25897 return error_mark_node
;
25902 /* Parse the requirement body. */
25903 reqs
= cp_parser_requirement_body (parser
);
25904 if (reqs
== error_mark_node
)
25905 return error_mark_node
;
25908 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25910 grokparms (parms
, &parms
);
25911 return finish_requires_expr (parms
, reqs
);
25914 /* Parse a parameterized requirement.
25916 requirement-parameter-list:
25917 '(' parameter-declaration-clause ')' */
25919 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25921 matching_parens parens
;
25922 if (!parens
.require_open (parser
))
25923 return error_mark_node
;
25925 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25927 if (!parens
.require_close (parser
))
25928 return error_mark_node
;
25933 /* Parse the body of a requirement.
25936 '{' requirement-list '}' */
25938 cp_parser_requirement_body (cp_parser
*parser
)
25940 matching_braces braces
;
25941 if (!braces
.require_open (parser
))
25942 return error_mark_node
;
25944 tree reqs
= cp_parser_requirement_list (parser
);
25946 if (!braces
.require_close (parser
))
25947 return error_mark_node
;
25952 /* Parse a list of requirements.
25956 requirement-list ';' requirement[opt] */
25958 cp_parser_requirement_list (cp_parser
*parser
)
25960 tree result
= NULL_TREE
;
25963 tree req
= cp_parser_requirement (parser
);
25964 if (req
== error_mark_node
)
25965 return error_mark_node
;
25967 result
= tree_cons (NULL_TREE
, req
, result
);
25969 /* If we see a semi-colon, consume it. */
25970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25971 cp_lexer_consume_token (parser
->lexer
);
25973 /* Stop processing at the end of the list. */
25974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25978 /* Reverse the order of requirements so they are analyzed in
25979 declaration order. */
25980 return nreverse (result
);
25983 /* Parse a syntactic requirement or type requirement.
25987 compound-requirement
25989 nested-requirement */
25991 cp_parser_requirement (cp_parser
*parser
)
25993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25994 return cp_parser_compound_requirement (parser
);
25995 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25996 return cp_parser_type_requirement (parser
);
25997 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25998 return cp_parser_nested_requirement (parser
);
26000 return cp_parser_simple_requirement (parser
);
26003 /* Parse a simple requirement.
26005 simple-requirement:
26008 cp_parser_simple_requirement (cp_parser
*parser
)
26010 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26011 if (!expr
|| expr
== error_mark_node
)
26012 return error_mark_node
;
26014 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26015 return error_mark_node
;
26017 return finish_simple_requirement (expr
);
26020 /* Parse a type requirement
26023 nested-name-specifier [opt] required-type-name ';'
26025 required-type-name:
26027 'template' [opt] simple-template-id */
26029 cp_parser_type_requirement (cp_parser
*parser
)
26031 cp_lexer_consume_token (parser
->lexer
);
26033 // Save the scope before parsing name specifiers.
26034 tree saved_scope
= parser
->scope
;
26035 tree saved_object_scope
= parser
->object_scope
;
26036 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26037 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26038 cp_parser_nested_name_specifier_opt (parser
,
26039 /*typename_keyword_p=*/true,
26040 /*check_dependency_p=*/false,
26042 /*is_declaration=*/false);
26045 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26047 cp_lexer_consume_token (parser
->lexer
);
26048 type
= cp_parser_template_id (parser
,
26049 /*template_keyword_p=*/true,
26050 /*check_dependency=*/false,
26051 /*tag_type=*/none_type
,
26052 /*is_declaration=*/false);
26053 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26054 /*complain=*/tf_error
);
26057 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26059 if (TREE_CODE (type
) == TYPE_DECL
)
26060 type
= TREE_TYPE (type
);
26062 parser
->scope
= saved_scope
;
26063 parser
->object_scope
= saved_object_scope
;
26064 parser
->qualifying_scope
= saved_qualifying_scope
;
26066 if (type
== error_mark_node
)
26067 cp_parser_skip_to_end_of_statement (parser
);
26069 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26070 return error_mark_node
;
26071 if (type
== error_mark_node
)
26072 return error_mark_node
;
26074 return finish_type_requirement (type
);
26077 /* Parse a compound requirement
26079 compound-requirement:
26080 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26082 cp_parser_compound_requirement (cp_parser
*parser
)
26084 /* Parse an expression enclosed in '{ }'s. */
26085 matching_braces braces
;
26086 if (!braces
.require_open (parser
))
26087 return error_mark_node
;
26089 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26090 if (!expr
|| expr
== error_mark_node
)
26091 return error_mark_node
;
26093 if (!braces
.require_close (parser
))
26094 return error_mark_node
;
26096 /* Parse the optional noexcept. */
26097 bool noexcept_p
= false;
26098 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26100 cp_lexer_consume_token (parser
->lexer
);
26104 /* Parse the optional trailing return type. */
26105 tree type
= NULL_TREE
;
26106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26108 cp_lexer_consume_token (parser
->lexer
);
26109 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26110 parser
->in_result_type_constraint_p
= true;
26111 type
= cp_parser_trailing_type_id (parser
);
26112 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26113 if (type
== error_mark_node
)
26114 return error_mark_node
;
26117 return finish_compound_requirement (expr
, type
, noexcept_p
);
26120 /* Parse a nested requirement. This is the same as a requires clause.
26122 nested-requirement:
26125 cp_parser_nested_requirement (cp_parser
*parser
)
26127 cp_lexer_consume_token (parser
->lexer
);
26128 tree req
= cp_parser_requires_clause (parser
);
26129 if (req
== error_mark_node
)
26130 return error_mark_node
;
26131 return finish_nested_requirement (req
);
26134 /* Support Functions */
26136 /* Return the appropriate prefer_type argument for lookup_name_real based on
26137 tag_type and template_mem_access. */
26140 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26142 /* DR 141: When looking in the current enclosing context for a template-name
26143 after -> or ., only consider class templates. */
26144 if (template_mem_access
)
26148 case none_type
: return 0; // No preference.
26149 case scope_type
: return 1; // Type or namespace.
26150 default: return 2; // Type only.
26154 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26155 NAME should have one of the representations used for an
26156 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26157 is returned. If PARSER->SCOPE is a dependent type, then a
26158 SCOPE_REF is returned.
26160 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26161 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26162 was formed. Abstractly, such entities should not be passed to this
26163 function, because they do not need to be looked up, but it is
26164 simpler to check for this special case here, rather than at the
26167 In cases not explicitly covered above, this function returns a
26168 DECL, OVERLOAD, or baselink representing the result of the lookup.
26169 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26172 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26173 (e.g., "struct") that was used. In that case bindings that do not
26174 refer to types are ignored.
26176 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26179 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26182 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26185 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26186 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26187 NULL_TREE otherwise. */
26190 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26191 enum tag_types tag_type
,
26194 bool check_dependency
,
26195 tree
*ambiguous_decls
,
26196 location_t name_location
)
26199 tree object_type
= parser
->context
->object_type
;
26201 /* Assume that the lookup will be unambiguous. */
26202 if (ambiguous_decls
)
26203 *ambiguous_decls
= NULL_TREE
;
26205 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26206 no longer valid. Note that if we are parsing tentatively, and
26207 the parse fails, OBJECT_TYPE will be automatically restored. */
26208 parser
->context
->object_type
= NULL_TREE
;
26210 if (name
== error_mark_node
)
26211 return error_mark_node
;
26213 /* A template-id has already been resolved; there is no lookup to
26215 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26217 if (BASELINK_P (name
))
26219 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26220 == TEMPLATE_ID_EXPR
);
26224 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26225 it should already have been checked to make sure that the name
26226 used matches the type being destroyed. */
26227 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26231 /* Figure out to which type this destructor applies. */
26233 type
= parser
->scope
;
26234 else if (object_type
)
26235 type
= object_type
;
26237 type
= current_class_type
;
26238 /* If that's not a class type, there is no destructor. */
26239 if (!type
|| !CLASS_TYPE_P (type
))
26240 return error_mark_node
;
26242 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26243 lazily_declare_fn (sfk_destructor
, type
);
26245 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26248 return error_mark_node
;
26251 /* By this point, the NAME should be an ordinary identifier. If
26252 the id-expression was a qualified name, the qualifying scope is
26253 stored in PARSER->SCOPE at this point. */
26254 gcc_assert (identifier_p (name
));
26256 /* Perform the lookup. */
26261 if (parser
->scope
== error_mark_node
)
26262 return error_mark_node
;
26264 /* If the SCOPE is dependent, the lookup must be deferred until
26265 the template is instantiated -- unless we are explicitly
26266 looking up names in uninstantiated templates. Even then, we
26267 cannot look up the name if the scope is not a class type; it
26268 might, for example, be a template type parameter. */
26269 dependent_p
= (TYPE_P (parser
->scope
)
26270 && dependent_scope_p (parser
->scope
));
26271 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26273 /* Defer lookup. */
26274 decl
= error_mark_node
;
26277 tree pushed_scope
= NULL_TREE
;
26279 /* If PARSER->SCOPE is a dependent type, then it must be a
26280 class type, and we must not be checking dependencies;
26281 otherwise, we would have processed this lookup above. So
26282 that PARSER->SCOPE is not considered a dependent base by
26283 lookup_member, we must enter the scope here. */
26285 pushed_scope
= push_scope (parser
->scope
);
26287 /* If the PARSER->SCOPE is a template specialization, it
26288 may be instantiated during name lookup. In that case,
26289 errors may be issued. Even if we rollback the current
26290 tentative parse, those errors are valid. */
26291 decl
= lookup_qualified_name (parser
->scope
, name
,
26292 prefer_type_arg (tag_type
),
26293 /*complain=*/true);
26295 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26296 lookup result and the nested-name-specifier nominates a class C:
26297 * if the name specified after the nested-name-specifier, when
26298 looked up in C, is the injected-class-name of C (Clause 9), or
26299 * if the name specified after the nested-name-specifier is the
26300 same as the identifier or the simple-template-id's template-
26301 name in the last component of the nested-name-specifier,
26302 the name is instead considered to name the constructor of
26303 class C. [ Note: for example, the constructor is not an
26304 acceptable lookup result in an elaborated-type-specifier so
26305 the constructor would not be used in place of the
26306 injected-class-name. --end note ] Such a constructor name
26307 shall be used only in the declarator-id of a declaration that
26308 names a constructor or in a using-declaration. */
26309 if (tag_type
== none_type
26310 && DECL_SELF_REFERENCE_P (decl
)
26311 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26312 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26313 prefer_type_arg (tag_type
),
26314 /*complain=*/true);
26316 /* If we have a single function from a using decl, pull it out. */
26317 if (TREE_CODE (decl
) == OVERLOAD
26318 && !really_overloaded_fn (decl
))
26319 decl
= OVL_FUNCTION (decl
);
26322 pop_scope (pushed_scope
);
26325 /* If the scope is a dependent type and either we deferred lookup or
26326 we did lookup but didn't find the name, rememeber the name. */
26327 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26328 && dependent_type_p (parser
->scope
))
26334 /* The resolution to Core Issue 180 says that `struct
26335 A::B' should be considered a type-name, even if `A'
26337 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26338 /*complain=*/tf_error
);
26339 if (type
!= error_mark_node
)
26340 decl
= TYPE_NAME (type
);
26342 else if (is_template
26343 && (cp_parser_next_token_ends_template_argument_p (parser
)
26344 || cp_lexer_next_token_is (parser
->lexer
,
26346 decl
= make_unbound_class_template (parser
->scope
,
26348 /*complain=*/tf_error
);
26350 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26351 parser
->scope
, name
,
26354 parser
->qualifying_scope
= parser
->scope
;
26355 parser
->object_scope
= NULL_TREE
;
26357 else if (object_type
)
26359 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26360 OBJECT_TYPE is not a class. */
26361 if (CLASS_TYPE_P (object_type
))
26362 /* If the OBJECT_TYPE is a template specialization, it may
26363 be instantiated during name lookup. In that case, errors
26364 may be issued. Even if we rollback the current tentative
26365 parse, those errors are valid. */
26366 decl
= lookup_member (object_type
,
26369 prefer_type_arg (tag_type
),
26370 tf_warning_or_error
);
26375 /* Look it up in the enclosing context. DR 141: When looking for a
26376 template-name after -> or ., only consider class templates. */
26377 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26379 /*block_p=*/true, is_namespace
, 0);
26380 if (object_type
== unknown_type_node
)
26381 /* The object is type-dependent, so we can't look anything up; we used
26382 this to get the DR 141 behavior. */
26383 object_type
= NULL_TREE
;
26384 parser
->object_scope
= object_type
;
26385 parser
->qualifying_scope
= NULL_TREE
;
26389 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26391 /*block_p=*/true, is_namespace
, 0);
26392 parser
->qualifying_scope
= NULL_TREE
;
26393 parser
->object_scope
= NULL_TREE
;
26396 /* If the lookup failed, let our caller know. */
26397 if (!decl
|| decl
== error_mark_node
)
26398 return error_mark_node
;
26400 /* Pull out the template from an injected-class-name (or multiple). */
26402 decl
= maybe_get_template_decl_from_type_decl (decl
);
26404 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26405 if (TREE_CODE (decl
) == TREE_LIST
)
26407 if (ambiguous_decls
)
26408 *ambiguous_decls
= decl
;
26409 /* The error message we have to print is too complicated for
26410 cp_parser_error, so we incorporate its actions directly. */
26411 if (!cp_parser_simulate_error (parser
))
26413 error_at (name_location
, "reference to %qD is ambiguous",
26415 print_candidates (decl
);
26417 return error_mark_node
;
26420 gcc_assert (DECL_P (decl
)
26421 || TREE_CODE (decl
) == OVERLOAD
26422 || TREE_CODE (decl
) == SCOPE_REF
26423 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26424 || BASELINK_P (decl
));
26426 /* If we have resolved the name of a member declaration, check to
26427 see if the declaration is accessible. When the name resolves to
26428 set of overloaded functions, accessibility is checked when
26429 overload resolution is done.
26431 During an explicit instantiation, access is not checked at all,
26432 as per [temp.explicit]. */
26434 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26436 maybe_record_typedef_use (decl
);
26438 return cp_expr (decl
, name_location
);
26441 /* Like cp_parser_lookup_name, but for use in the typical case where
26442 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26443 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26446 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26448 return cp_parser_lookup_name (parser
, name
,
26450 /*is_template=*/false,
26451 /*is_namespace=*/false,
26452 /*check_dependency=*/true,
26453 /*ambiguous_decls=*/NULL
,
26457 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26458 the current context, return the TYPE_DECL. If TAG_NAME_P is
26459 true, the DECL indicates the class being defined in a class-head,
26460 or declared in an elaborated-type-specifier.
26462 Otherwise, return DECL. */
26465 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26467 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26468 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26471 template <typename T> struct B;
26474 template <typename T> struct A::B {};
26476 Similarly, in an elaborated-type-specifier:
26478 namespace N { struct X{}; }
26481 template <typename T> friend struct N::X;
26484 However, if the DECL refers to a class type, and we are in
26485 the scope of the class, then the name lookup automatically
26486 finds the TYPE_DECL created by build_self_reference rather
26487 than a TEMPLATE_DECL. For example, in:
26489 template <class T> struct S {
26493 there is no need to handle such case. */
26495 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26496 return DECL_TEMPLATE_RESULT (decl
);
26501 /* If too many, or too few, template-parameter lists apply to the
26502 declarator, issue an error message. Returns TRUE if all went well,
26503 and FALSE otherwise. */
26506 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26507 cp_declarator
*declarator
,
26508 location_t declarator_location
)
26510 switch (declarator
->kind
)
26514 unsigned num_templates
= 0;
26515 tree scope
= declarator
->u
.id
.qualifying_scope
;
26516 bool template_id_p
= false;
26519 num_templates
= num_template_headers_for_class (scope
);
26520 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26521 == TEMPLATE_ID_EXPR
)
26523 /* If the DECLARATOR has the form `X<y>' then it uses one
26524 additional level of template parameters. */
26526 template_id_p
= true;
26529 return cp_parser_check_template_parameters
26530 (parser
, num_templates
, template_id_p
, declarator_location
,
26537 case cdk_reference
:
26539 return (cp_parser_check_declarator_template_parameters
26540 (parser
, declarator
->declarator
, declarator_location
));
26547 gcc_unreachable ();
26552 /* NUM_TEMPLATES were used in the current declaration. If that is
26553 invalid, return FALSE and issue an error messages. Otherwise,
26554 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26555 declarator and we can print more accurate diagnostics. */
26558 cp_parser_check_template_parameters (cp_parser
* parser
,
26559 unsigned num_templates
,
26560 bool template_id_p
,
26561 location_t location
,
26562 cp_declarator
*declarator
)
26564 /* If there are the same number of template classes and parameter
26565 lists, that's OK. */
26566 if (parser
->num_template_parameter_lists
== num_templates
)
26568 /* If there are more, but only one more, and the name ends in an identifier,
26569 then we are declaring a primary template. That's OK too. */
26571 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26573 /* If there are more template classes than parameter lists, we have
26576 template <class T> void S<T>::R<T>::f (); */
26577 if (parser
->num_template_parameter_lists
< num_templates
)
26579 if (declarator
&& !current_function_decl
)
26580 error_at (location
, "specializing member %<%T::%E%> "
26581 "requires %<template<>%> syntax",
26582 declarator
->u
.id
.qualifying_scope
,
26583 declarator
->u
.id
.unqualified_name
);
26584 else if (declarator
)
26585 error_at (location
, "invalid declaration of %<%T::%E%>",
26586 declarator
->u
.id
.qualifying_scope
,
26587 declarator
->u
.id
.unqualified_name
);
26589 error_at (location
, "too few template-parameter-lists");
26592 /* Otherwise, there are too many template parameter lists. We have
26595 template <class T> template <class U> void S::f(); */
26596 error_at (location
, "too many template-parameter-lists");
26600 /* Parse an optional `::' token indicating that the following name is
26601 from the global namespace. If so, PARSER->SCOPE is set to the
26602 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26603 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26604 Returns the new value of PARSER->SCOPE, if the `::' token is
26605 present, and NULL_TREE otherwise. */
26608 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26612 /* Peek at the next token. */
26613 token
= cp_lexer_peek_token (parser
->lexer
);
26614 /* If we're looking at a `::' token then we're starting from the
26615 global namespace, not our current location. */
26616 if (token
->type
== CPP_SCOPE
)
26618 /* Consume the `::' token. */
26619 cp_lexer_consume_token (parser
->lexer
);
26620 /* Set the SCOPE so that we know where to start the lookup. */
26621 parser
->scope
= global_namespace
;
26622 parser
->qualifying_scope
= global_namespace
;
26623 parser
->object_scope
= NULL_TREE
;
26625 return parser
->scope
;
26627 else if (!current_scope_valid_p
)
26629 parser
->scope
= NULL_TREE
;
26630 parser
->qualifying_scope
= NULL_TREE
;
26631 parser
->object_scope
= NULL_TREE
;
26637 /* Returns TRUE if the upcoming token sequence is the start of a
26638 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26639 declarator is preceded by the `friend' specifier. */
26642 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26644 bool constructor_p
;
26645 bool outside_class_specifier_p
;
26646 tree nested_name_specifier
;
26647 cp_token
*next_token
;
26649 /* The common case is that this is not a constructor declarator, so
26650 try to avoid doing lots of work if at all possible. It's not
26651 valid declare a constructor at function scope. */
26652 if (parser
->in_function_body
)
26654 /* And only certain tokens can begin a constructor declarator. */
26655 next_token
= cp_lexer_peek_token (parser
->lexer
);
26656 if (next_token
->type
!= CPP_NAME
26657 && next_token
->type
!= CPP_SCOPE
26658 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26659 && next_token
->type
!= CPP_TEMPLATE_ID
)
26662 /* Parse tentatively; we are going to roll back all of the tokens
26664 cp_parser_parse_tentatively (parser
);
26665 /* Assume that we are looking at a constructor declarator. */
26666 constructor_p
= true;
26668 /* Look for the optional `::' operator. */
26669 cp_parser_global_scope_opt (parser
,
26670 /*current_scope_valid_p=*/false);
26671 /* Look for the nested-name-specifier. */
26672 nested_name_specifier
26673 = (cp_parser_nested_name_specifier_opt (parser
,
26674 /*typename_keyword_p=*/false,
26675 /*check_dependency_p=*/false,
26677 /*is_declaration=*/false));
26679 outside_class_specifier_p
= (!at_class_scope_p ()
26680 || !TYPE_BEING_DEFINED (current_class_type
)
26683 /* Outside of a class-specifier, there must be a
26684 nested-name-specifier. Except in C++17 mode, where we
26685 might be declaring a guiding declaration. */
26686 if (!nested_name_specifier
&& outside_class_specifier_p
26687 && cxx_dialect
< cxx17
)
26688 constructor_p
= false;
26689 else if (nested_name_specifier
== error_mark_node
)
26690 constructor_p
= false;
26692 /* If we have a class scope, this is easy; DR 147 says that S::S always
26693 names the constructor, and no other qualified name could. */
26694 if (constructor_p
&& nested_name_specifier
26695 && CLASS_TYPE_P (nested_name_specifier
))
26697 tree id
= cp_parser_unqualified_id (parser
,
26698 /*template_keyword_p=*/false,
26699 /*check_dependency_p=*/false,
26700 /*declarator_p=*/true,
26701 /*optional_p=*/false);
26702 if (is_overloaded_fn (id
))
26703 id
= DECL_NAME (get_first_fn (id
));
26704 if (!constructor_name_p (id
, nested_name_specifier
))
26705 constructor_p
= false;
26707 /* If we still think that this might be a constructor-declarator,
26708 look for a class-name. */
26709 else if (constructor_p
)
26713 template <typename T> struct S {
26717 we must recognize that the nested `S' names a class. */
26718 if (cxx_dialect
>= cxx17
)
26719 cp_parser_parse_tentatively (parser
);
26722 type_decl
= cp_parser_class_name (parser
,
26723 /*typename_keyword_p=*/false,
26724 /*template_keyword_p=*/false,
26726 /*check_dependency_p=*/false,
26727 /*class_head_p=*/false,
26728 /*is_declaration=*/false);
26730 if (cxx_dialect
>= cxx17
26731 && !cp_parser_parse_definitely (parser
))
26733 type_decl
= NULL_TREE
;
26734 tree tmpl
= cp_parser_template_name (parser
,
26735 /*template_keyword*/false,
26736 /*check_dependency_p*/false,
26737 /*is_declaration*/false,
26739 /*is_identifier*/NULL
);
26740 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26741 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26742 /* It's a deduction guide, return true. */;
26744 cp_parser_simulate_error (parser
);
26747 /* If there was no class-name, then this is not a constructor.
26748 Otherwise, if we are in a class-specifier and we aren't
26749 handling a friend declaration, check that its type matches
26750 current_class_type (c++/38313). Note: error_mark_node
26751 is left alone for error recovery purposes. */
26752 constructor_p
= (!cp_parser_error_occurred (parser
)
26753 && (outside_class_specifier_p
26754 || type_decl
== NULL_TREE
26755 || type_decl
== error_mark_node
26756 || same_type_p (current_class_type
,
26757 TREE_TYPE (type_decl
))));
26759 /* If we're still considering a constructor, we have to see a `(',
26760 to begin the parameter-declaration-clause, followed by either a
26761 `)', an `...', or a decl-specifier. We need to check for a
26762 type-specifier to avoid being fooled into thinking that:
26766 is a constructor. (It is actually a function named `f' that
26767 takes one parameter (of type `int') and returns a value of type
26770 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26771 constructor_p
= false;
26774 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26775 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26776 /* A parameter declaration begins with a decl-specifier,
26777 which is either the "attribute" keyword, a storage class
26778 specifier, or (usually) a type-specifier. */
26779 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26782 tree pushed_scope
= NULL_TREE
;
26783 unsigned saved_num_template_parameter_lists
;
26785 /* Names appearing in the type-specifier should be looked up
26786 in the scope of the class. */
26787 if (current_class_type
)
26789 else if (type_decl
)
26791 type
= TREE_TYPE (type_decl
);
26792 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26794 type
= resolve_typename_type (type
,
26795 /*only_current_p=*/false);
26796 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26798 cp_parser_abort_tentative_parse (parser
);
26802 pushed_scope
= push_scope (type
);
26805 /* Inside the constructor parameter list, surrounding
26806 template-parameter-lists do not apply. */
26807 saved_num_template_parameter_lists
26808 = parser
->num_template_parameter_lists
;
26809 parser
->num_template_parameter_lists
= 0;
26811 /* Look for the type-specifier. */
26812 cp_parser_type_specifier (parser
,
26813 CP_PARSER_FLAGS_NONE
,
26814 /*decl_specs=*/NULL
,
26815 /*is_declarator=*/true,
26816 /*declares_class_or_enum=*/NULL
,
26817 /*is_cv_qualifier=*/NULL
);
26819 parser
->num_template_parameter_lists
26820 = saved_num_template_parameter_lists
;
26822 /* Leave the scope of the class. */
26824 pop_scope (pushed_scope
);
26826 constructor_p
= !cp_parser_error_occurred (parser
);
26830 /* We did not really want to consume any tokens. */
26831 cp_parser_abort_tentative_parse (parser
);
26833 return constructor_p
;
26836 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26837 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26838 they must be performed once we are in the scope of the function.
26840 Returns the function defined. */
26843 cp_parser_function_definition_from_specifiers_and_declarator
26844 (cp_parser
* parser
,
26845 cp_decl_specifier_seq
*decl_specifiers
,
26847 const cp_declarator
*declarator
)
26852 /* Begin the function-definition. */
26853 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26855 /* The things we're about to see are not directly qualified by any
26856 template headers we've seen thus far. */
26857 reset_specialization ();
26859 /* If there were names looked up in the decl-specifier-seq that we
26860 did not check, check them now. We must wait until we are in the
26861 scope of the function to perform the checks, since the function
26862 might be a friend. */
26863 perform_deferred_access_checks (tf_warning_or_error
);
26867 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26868 parser
->omp_declare_simd
= NULL
;
26869 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26870 parser
->oacc_routine
= NULL
;
26875 /* Skip the entire function. */
26876 cp_parser_skip_to_end_of_block_or_statement (parser
);
26877 fn
= error_mark_node
;
26879 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26881 /* Seen already, skip it. An error message has already been output. */
26882 cp_parser_skip_to_end_of_block_or_statement (parser
);
26883 fn
= current_function_decl
;
26884 current_function_decl
= NULL_TREE
;
26885 /* If this is a function from a class, pop the nested class. */
26886 if (current_class_name
)
26887 pop_nested_class ();
26892 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26893 tv
= TV_PARSE_INLINE
;
26895 tv
= TV_PARSE_FUNC
;
26897 fn
= cp_parser_function_definition_after_declarator (parser
,
26898 /*inline_p=*/false);
26905 /* Parse the part of a function-definition that follows the
26906 declarator. INLINE_P is TRUE iff this function is an inline
26907 function defined within a class-specifier.
26909 Returns the function defined. */
26912 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26916 bool saved_in_unbraced_linkage_specification_p
;
26917 bool saved_in_function_body
;
26918 unsigned saved_num_template_parameter_lists
;
26920 bool fully_implicit_function_template_p
26921 = parser
->fully_implicit_function_template_p
;
26922 parser
->fully_implicit_function_template_p
= false;
26923 tree implicit_template_parms
26924 = parser
->implicit_template_parms
;
26925 parser
->implicit_template_parms
= 0;
26926 cp_binding_level
* implicit_template_scope
26927 = parser
->implicit_template_scope
;
26928 parser
->implicit_template_scope
= 0;
26930 saved_in_function_body
= parser
->in_function_body
;
26931 parser
->in_function_body
= true;
26932 /* If the next token is `return', then the code may be trying to
26933 make use of the "named return value" extension that G++ used to
26935 token
= cp_lexer_peek_token (parser
->lexer
);
26936 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26938 /* Consume the `return' keyword. */
26939 cp_lexer_consume_token (parser
->lexer
);
26940 /* Look for the identifier that indicates what value is to be
26942 cp_parser_identifier (parser
);
26943 /* Issue an error message. */
26944 error_at (token
->location
,
26945 "named return values are no longer supported");
26946 /* Skip tokens until we reach the start of the function body. */
26949 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26950 if (token
->type
== CPP_OPEN_BRACE
26951 || token
->type
== CPP_EOF
26952 || token
->type
== CPP_PRAGMA_EOL
)
26954 cp_lexer_consume_token (parser
->lexer
);
26957 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26958 anything declared inside `f'. */
26959 saved_in_unbraced_linkage_specification_p
26960 = parser
->in_unbraced_linkage_specification_p
;
26961 parser
->in_unbraced_linkage_specification_p
= false;
26962 /* Inside the function, surrounding template-parameter-lists do not
26964 saved_num_template_parameter_lists
26965 = parser
->num_template_parameter_lists
;
26966 parser
->num_template_parameter_lists
= 0;
26968 /* If the next token is `try', `__transaction_atomic', or
26969 `__transaction_relaxed`, then we are looking at either function-try-block
26970 or function-transaction-block. Note that all of these include the
26972 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26973 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26974 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26975 RID_TRANSACTION_RELAXED
))
26976 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26977 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26978 cp_parser_function_try_block (parser
);
26980 cp_parser_ctor_initializer_opt_and_function_body
26981 (parser
, /*in_function_try_block=*/false);
26983 /* Finish the function. */
26984 fn
= finish_function (inline_p
);
26985 /* Generate code for it, if necessary. */
26986 expand_or_defer_fn (fn
);
26987 /* Restore the saved values. */
26988 parser
->in_unbraced_linkage_specification_p
26989 = saved_in_unbraced_linkage_specification_p
;
26990 parser
->num_template_parameter_lists
26991 = saved_num_template_parameter_lists
;
26992 parser
->in_function_body
= saved_in_function_body
;
26994 parser
->fully_implicit_function_template_p
26995 = fully_implicit_function_template_p
;
26996 parser
->implicit_template_parms
26997 = implicit_template_parms
;
26998 parser
->implicit_template_scope
26999 = implicit_template_scope
;
27001 if (parser
->fully_implicit_function_template_p
)
27002 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27007 /* Parse a template-declaration body (following argument list). */
27010 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27011 tree parameter_list
,
27014 tree decl
= NULL_TREE
;
27015 bool friend_p
= false;
27017 /* We just processed one more parameter list. */
27018 ++parser
->num_template_parameter_lists
;
27020 /* Get the deferred access checks from the parameter list. These
27021 will be checked once we know what is being declared, as for a
27022 member template the checks must be performed in the scope of the
27023 class containing the member. */
27024 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27026 /* Tentatively parse for a new template parameter list, which can either be
27027 the template keyword or a template introduction. */
27028 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27030 else if (cxx_dialect
>= cxx11
27031 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27032 decl
= cp_parser_alias_declaration (parser
);
27035 /* There are no access checks when parsing a template, as we do not
27036 know if a specialization will be a friend. */
27037 push_deferring_access_checks (dk_no_check
);
27038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27039 decl
= cp_parser_single_declaration (parser
,
27042 /*explicit_specialization_p=*/false,
27044 pop_deferring_access_checks ();
27046 /* If this is a member template declaration, let the front
27048 if (member_p
&& !friend_p
&& decl
)
27050 if (TREE_CODE (decl
) == TYPE_DECL
)
27051 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27053 decl
= finish_member_template_decl (decl
);
27055 else if (friend_p
&& decl
27056 && DECL_DECLARES_TYPE_P (decl
))
27057 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27058 /*complain=*/true);
27060 /* We are done with the current parameter list. */
27061 --parser
->num_template_parameter_lists
;
27063 pop_deferring_access_checks ();
27066 finish_template_decl (parameter_list
);
27068 /* Check the template arguments for a literal operator template. */
27070 && DECL_DECLARES_FUNCTION_P (decl
)
27071 && UDLIT_OPER_P (DECL_NAME (decl
)))
27074 if (parameter_list
== NULL_TREE
)
27078 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27079 if (num_parms
== 1)
27081 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27082 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27083 if (TREE_TYPE (parm
) != char_type_node
27084 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27087 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27089 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27090 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27091 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27092 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27093 if (parm
== error_mark_node
27094 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27095 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27103 if (cxx_dialect
>= cxx14
)
27104 error ("literal operator template %qD has invalid parameter list."
27105 " Expected non-type template argument pack <char...>"
27106 " or <typename CharT, CharT...>",
27109 error ("literal operator template %qD has invalid parameter list."
27110 " Expected non-type template argument pack <char...>",
27115 /* Register member declarations. */
27116 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27117 finish_member_declaration (decl
);
27118 /* If DECL is a function template, we must return to parse it later.
27119 (Even though there is no definition, there might be default
27120 arguments that need handling.) */
27121 if (member_p
&& decl
27122 && DECL_DECLARES_FUNCTION_P (decl
))
27123 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27126 /* Parse a template introduction header for a template-declaration. Returns
27127 false if tentative parse fails. */
27130 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27132 cp_parser_parse_tentatively (parser
);
27134 tree saved_scope
= parser
->scope
;
27135 tree saved_object_scope
= parser
->object_scope
;
27136 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27138 /* Look for the optional `::' operator. */
27139 cp_parser_global_scope_opt (parser
,
27140 /*current_scope_valid_p=*/false);
27141 /* Look for the nested-name-specifier. */
27142 cp_parser_nested_name_specifier_opt (parser
,
27143 /*typename_keyword_p=*/false,
27144 /*check_dependency_p=*/true,
27146 /*is_declaration=*/false);
27148 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27149 tree concept_name
= cp_parser_identifier (parser
);
27151 /* Look up the concept for which we will be matching
27152 template parameters. */
27153 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27155 parser
->scope
= saved_scope
;
27156 parser
->object_scope
= saved_object_scope
;
27157 parser
->qualifying_scope
= saved_qualifying_scope
;
27159 if (concept_name
== error_mark_node
)
27160 cp_parser_simulate_error (parser
);
27162 /* Look for opening brace for introduction. */
27163 matching_braces braces
;
27164 braces
.require_open (parser
);
27166 if (!cp_parser_parse_definitely (parser
))
27169 push_deferring_access_checks (dk_deferred
);
27171 /* Build vector of placeholder parameters and grab
27172 matching identifiers. */
27173 tree introduction_list
= cp_parser_introduction_list (parser
);
27175 /* Look for closing brace for introduction. */
27176 if (!braces
.require_close (parser
))
27179 /* The introduction-list shall not be empty. */
27180 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27183 /* In cp_parser_introduction_list we have already issued an error. */
27187 if (tmpl_decl
== error_mark_node
)
27189 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27194 /* Build and associate the constraint. */
27195 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27196 if (parms
&& parms
!= error_mark_node
)
27198 cp_parser_template_declaration_after_parameters (parser
, parms
,
27203 error_at (token
->location
, "no matching concept for template-introduction");
27207 /* Parse a normal template-declaration following the template keyword. */
27210 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27212 tree parameter_list
;
27213 bool need_lang_pop
;
27214 location_t location
= input_location
;
27216 /* Look for the `<' token. */
27217 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27219 if (at_class_scope_p () && current_function_decl
)
27221 /* 14.5.2.2 [temp.mem]
27223 A local class shall not have member templates. */
27224 error_at (location
,
27225 "invalid declaration of member template in local class");
27226 cp_parser_skip_to_end_of_block_or_statement (parser
);
27231 A template ... shall not have C linkage. */
27232 if (current_lang_name
== lang_name_c
)
27234 error_at (location
, "template with C linkage");
27235 maybe_show_extern_c_location ();
27236 /* Give it C++ linkage to avoid confusing other parts of the
27238 push_lang_context (lang_name_cplusplus
);
27239 need_lang_pop
= true;
27242 need_lang_pop
= false;
27244 /* We cannot perform access checks on the template parameter
27245 declarations until we know what is being declared, just as we
27246 cannot check the decl-specifier list. */
27247 push_deferring_access_checks (dk_deferred
);
27249 /* If the next token is `>', then we have an invalid
27250 specialization. Rather than complain about an invalid template
27251 parameter, issue an error message here. */
27252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27254 cp_parser_error (parser
, "invalid explicit specialization");
27255 begin_specialization ();
27256 parameter_list
= NULL_TREE
;
27260 /* Parse the template parameters. */
27261 parameter_list
= cp_parser_template_parameter_list (parser
);
27264 /* Look for the `>'. */
27265 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27267 /* Manage template requirements */
27270 tree reqs
= get_shorthand_constraints (current_template_parms
);
27271 if (tree r
= cp_parser_requires_clause_opt (parser
))
27272 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27273 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27276 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27279 /* For the erroneous case of a template with C linkage, we pushed an
27280 implicit C++ linkage scope; exit that scope now. */
27282 pop_lang_context ();
27285 /* Parse a template-declaration, assuming that the `export' (and
27286 `extern') keywords, if present, has already been scanned. MEMBER_P
27287 is as for cp_parser_template_declaration. */
27290 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27292 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27294 cp_lexer_consume_token (parser
->lexer
);
27295 cp_parser_explicit_template_declaration (parser
, member_p
);
27298 else if (flag_concepts
)
27299 return cp_parser_template_introduction (parser
, member_p
);
27304 /* Perform the deferred access checks from a template-parameter-list.
27305 CHECKS is a TREE_LIST of access checks, as returned by
27306 get_deferred_access_checks. */
27309 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27311 ++processing_template_parmlist
;
27312 perform_access_checks (checks
, tf_warning_or_error
);
27313 --processing_template_parmlist
;
27316 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27317 `function-definition' sequence that follows a template header.
27318 If MEMBER_P is true, this declaration appears in a class scope.
27320 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27321 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27324 cp_parser_single_declaration (cp_parser
* parser
,
27325 vec
<deferred_access_check
, va_gc
> *checks
,
27327 bool explicit_specialization_p
,
27330 int declares_class_or_enum
;
27331 tree decl
= NULL_TREE
;
27332 cp_decl_specifier_seq decl_specifiers
;
27333 bool function_definition_p
= false;
27334 cp_token
*decl_spec_token_start
;
27336 /* This function is only used when processing a template
27338 gcc_assert (innermost_scope_kind () == sk_template_parms
27339 || innermost_scope_kind () == sk_template_spec
);
27341 /* Defer access checks until we know what is being declared. */
27342 push_deferring_access_checks (dk_deferred
);
27344 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27346 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27347 cp_parser_decl_specifier_seq (parser
,
27348 CP_PARSER_FLAGS_OPTIONAL
,
27350 &declares_class_or_enum
);
27352 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27354 /* There are no template typedefs. */
27355 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27357 error_at (decl_spec_token_start
->location
,
27358 "template declaration of %<typedef%>");
27359 decl
= error_mark_node
;
27362 /* Gather up the access checks that occurred the
27363 decl-specifier-seq. */
27364 stop_deferring_access_checks ();
27366 /* Check for the declaration of a template class. */
27367 if (declares_class_or_enum
)
27369 if (cp_parser_declares_only_class_p (parser
)
27370 || (declares_class_or_enum
& 2))
27372 // If this is a declaration, but not a definition, associate
27373 // any constraints with the type declaration. Constraints
27374 // are associated with definitions in cp_parser_class_specifier.
27375 if (declares_class_or_enum
== 1)
27376 associate_classtype_constraints (decl_specifiers
.type
);
27378 decl
= shadow_tag (&decl_specifiers
);
27383 friend template <typename T> struct A<T>::B;
27386 A<T>::B will be represented by a TYPENAME_TYPE, and
27387 therefore not recognized by shadow_tag. */
27388 if (friend_p
&& *friend_p
27390 && decl_specifiers
.type
27391 && TYPE_P (decl_specifiers
.type
))
27392 decl
= decl_specifiers
.type
;
27394 if (decl
&& decl
!= error_mark_node
)
27395 decl
= TYPE_NAME (decl
);
27397 decl
= error_mark_node
;
27399 /* Perform access checks for template parameters. */
27400 cp_parser_perform_template_parameter_access_checks (checks
);
27402 /* Give a helpful diagnostic for
27403 template <class T> struct A { } a;
27404 if we aren't already recovering from an error. */
27405 if (!cp_parser_declares_only_class_p (parser
)
27408 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27409 "a class template declaration must not declare "
27411 cp_parser_skip_to_end_of_block_or_statement (parser
);
27417 /* Complain about missing 'typename' or other invalid type names. */
27418 if (!decl_specifiers
.any_type_specifiers_p
27419 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27421 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27422 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27423 the rest of this declaration. */
27424 decl
= error_mark_node
;
27428 /* If it's not a template class, try for a template function. If
27429 the next token is a `;', then this declaration does not declare
27430 anything. But, if there were errors in the decl-specifiers, then
27431 the error might well have come from an attempted class-specifier.
27432 In that case, there's no need to warn about a missing declarator. */
27434 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27435 || decl_specifiers
.type
!= error_mark_node
))
27437 decl
= cp_parser_init_declarator (parser
,
27440 /*function_definition_allowed_p=*/true,
27442 declares_class_or_enum
,
27443 &function_definition_p
,
27446 /* 7.1.1-1 [dcl.stc]
27448 A storage-class-specifier shall not be specified in an explicit
27449 specialization... */
27451 && explicit_specialization_p
27452 && decl_specifiers
.storage_class
!= sc_none
)
27454 error_at (decl_spec_token_start
->location
,
27455 "explicit template specialization cannot have a storage class");
27456 decl
= error_mark_node
;
27459 if (decl
&& VAR_P (decl
))
27460 check_template_variable (decl
);
27463 /* Look for a trailing `;' after the declaration. */
27464 if (!function_definition_p
27465 && (decl
== error_mark_node
27466 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27467 cp_parser_skip_to_end_of_block_or_statement (parser
);
27470 pop_deferring_access_checks ();
27472 /* Clear any current qualification; whatever comes next is the start
27473 of something new. */
27474 parser
->scope
= NULL_TREE
;
27475 parser
->qualifying_scope
= NULL_TREE
;
27476 parser
->object_scope
= NULL_TREE
;
27481 /* Parse a cast-expression that is not the operand of a unary "&". */
27484 cp_parser_simple_cast_expression (cp_parser
*parser
)
27486 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27487 /*cast_p=*/false, /*decltype*/false, NULL
);
27490 /* Parse a functional cast to TYPE. Returns an expression
27491 representing the cast. */
27494 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27496 vec
<tree
, va_gc
> *vec
;
27497 tree expression_list
;
27501 location_t start_loc
= input_location
;
27504 type
= error_mark_node
;
27506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27508 cp_lexer_set_source_position (parser
->lexer
);
27509 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27510 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27511 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27512 if (TREE_CODE (type
) == TYPE_DECL
)
27513 type
= TREE_TYPE (type
);
27515 cast
= finish_compound_literal (type
, expression_list
,
27516 tf_warning_or_error
, fcl_functional
);
27517 /* Create a location of the form:
27520 with caret == start at the start of the type name,
27521 finishing at the closing brace. */
27522 location_t finish_loc
27523 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27524 location_t combined_loc
= make_location (start_loc
, start_loc
,
27526 cast
.set_location (combined_loc
);
27531 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27533 /*allow_expansion_p=*/true,
27534 /*non_constant_p=*/NULL
);
27536 expression_list
= error_mark_node
;
27539 expression_list
= build_tree_list_vec (vec
);
27540 release_tree_vector (vec
);
27543 cast
= build_functional_cast (type
, expression_list
,
27544 tf_warning_or_error
);
27545 /* [expr.const]/1: In an integral constant expression "only type
27546 conversions to integral or enumeration type can be used". */
27547 if (TREE_CODE (type
) == TYPE_DECL
)
27548 type
= TREE_TYPE (type
);
27549 if (cast
!= error_mark_node
27550 && !cast_valid_in_integral_constant_expression_p (type
)
27551 && cp_parser_non_integral_constant_expression (parser
,
27553 return error_mark_node
;
27555 /* Create a location of the form:
27558 with caret == start at the start of the type name,
27559 finishing at the closing paren. */
27560 location_t finish_loc
27561 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27562 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27563 cast
.set_location (combined_loc
);
27567 /* Save the tokens that make up the body of a member function defined
27568 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27569 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27570 specifiers applied to the declaration. Returns the FUNCTION_DECL
27571 for the member function. */
27574 cp_parser_save_member_function_body (cp_parser
* parser
,
27575 cp_decl_specifier_seq
*decl_specifiers
,
27576 cp_declarator
*declarator
,
27582 bool function_try_block
= false;
27584 /* Create the FUNCTION_DECL. */
27585 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27586 cp_finalize_omp_declare_simd (parser
, fn
);
27587 cp_finalize_oacc_routine (parser
, fn
, true);
27588 /* If something went badly wrong, bail out now. */
27589 if (fn
== error_mark_node
)
27591 /* If there's a function-body, skip it. */
27592 if (cp_parser_token_starts_function_definition_p
27593 (cp_lexer_peek_token (parser
->lexer
)))
27594 cp_parser_skip_to_end_of_block_or_statement (parser
);
27595 return error_mark_node
;
27598 /* Remember it, if there default args to post process. */
27599 cp_parser_save_default_args (parser
, fn
);
27601 /* Save away the tokens that make up the body of the
27603 first
= parser
->lexer
->next_token
;
27605 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27606 cp_lexer_consume_token (parser
->lexer
);
27607 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27608 RID_TRANSACTION_ATOMIC
))
27610 cp_lexer_consume_token (parser
->lexer
);
27611 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27613 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27614 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27615 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27616 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27617 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27619 cp_lexer_consume_token (parser
->lexer
);
27620 cp_lexer_consume_token (parser
->lexer
);
27621 cp_lexer_consume_token (parser
->lexer
);
27622 cp_lexer_consume_token (parser
->lexer
);
27623 cp_lexer_consume_token (parser
->lexer
);
27626 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27627 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27629 cp_lexer_consume_token (parser
->lexer
);
27630 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27635 /* Handle function try blocks. */
27636 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27638 cp_lexer_consume_token (parser
->lexer
);
27639 function_try_block
= true;
27641 /* We can have braced-init-list mem-initializers before the fn body. */
27642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27644 cp_lexer_consume_token (parser
->lexer
);
27645 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27647 /* cache_group will stop after an un-nested { } pair, too. */
27648 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27651 /* variadic mem-inits have ... after the ')'. */
27652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27653 cp_lexer_consume_token (parser
->lexer
);
27656 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27657 /* Handle function try blocks. */
27658 if (function_try_block
)
27659 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27660 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27661 last
= parser
->lexer
->next_token
;
27663 /* Save away the inline definition; we will process it when the
27664 class is complete. */
27665 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27666 DECL_PENDING_INLINE_P (fn
) = 1;
27668 /* We need to know that this was defined in the class, so that
27669 friend templates are handled correctly. */
27670 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27672 /* Add FN to the queue of functions to be parsed later. */
27673 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27678 /* Save the tokens that make up the in-class initializer for a non-static
27679 data member. Returns a DEFAULT_ARG. */
27682 cp_parser_save_nsdmi (cp_parser
* parser
)
27684 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27687 /* Parse a template-argument-list, as well as the trailing ">" (but
27688 not the opening "<"). See cp_parser_template_argument_list for the
27692 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27696 tree saved_qualifying_scope
;
27697 tree saved_object_scope
;
27698 bool saved_greater_than_is_operator_p
;
27699 int saved_unevaluated_operand
;
27700 int saved_inhibit_evaluation_warnings
;
27704 When parsing a template-id, the first non-nested `>' is taken as
27705 the end of the template-argument-list rather than a greater-than
27707 saved_greater_than_is_operator_p
27708 = parser
->greater_than_is_operator_p
;
27709 parser
->greater_than_is_operator_p
= false;
27710 /* Parsing the argument list may modify SCOPE, so we save it
27712 saved_scope
= parser
->scope
;
27713 saved_qualifying_scope
= parser
->qualifying_scope
;
27714 saved_object_scope
= parser
->object_scope
;
27715 /* We need to evaluate the template arguments, even though this
27716 template-id may be nested within a "sizeof". */
27717 saved_unevaluated_operand
= cp_unevaluated_operand
;
27718 cp_unevaluated_operand
= 0;
27719 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27720 c_inhibit_evaluation_warnings
= 0;
27721 /* Parse the template-argument-list itself. */
27722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27723 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27724 arguments
= NULL_TREE
;
27726 arguments
= cp_parser_template_argument_list (parser
);
27727 /* Look for the `>' that ends the template-argument-list. If we find
27728 a '>>' instead, it's probably just a typo. */
27729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27731 if (cxx_dialect
!= cxx98
)
27733 /* In C++0x, a `>>' in a template argument list or cast
27734 expression is considered to be two separate `>'
27735 tokens. So, change the current token to a `>', but don't
27736 consume it: it will be consumed later when the outer
27737 template argument list (or cast expression) is parsed.
27738 Note that this replacement of `>' for `>>' is necessary
27739 even if we are parsing tentatively: in the tentative
27740 case, after calling
27741 cp_parser_enclosed_template_argument_list we will always
27742 throw away all of the template arguments and the first
27743 closing `>', either because the template argument list
27744 was erroneous or because we are replacing those tokens
27745 with a CPP_TEMPLATE_ID token. The second `>' (which will
27746 not have been thrown away) is needed either to close an
27747 outer template argument list or to complete a new-style
27749 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27750 token
->type
= CPP_GREATER
;
27752 else if (!saved_greater_than_is_operator_p
)
27754 /* If we're in a nested template argument list, the '>>' has
27755 to be a typo for '> >'. We emit the error message, but we
27756 continue parsing and we push a '>' as next token, so that
27757 the argument list will be parsed correctly. Note that the
27758 global source location is still on the token before the
27759 '>>', so we need to say explicitly where we want it. */
27760 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27761 gcc_rich_location
richloc (token
->location
);
27762 richloc
.add_fixit_replace ("> >");
27763 error_at (&richloc
, "%<>>%> should be %<> >%> "
27764 "within a nested template argument list");
27766 token
->type
= CPP_GREATER
;
27770 /* If this is not a nested template argument list, the '>>'
27771 is a typo for '>'. Emit an error message and continue.
27772 Same deal about the token location, but here we can get it
27773 right by consuming the '>>' before issuing the diagnostic. */
27774 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27775 error_at (token
->location
,
27776 "spurious %<>>%>, use %<>%> to terminate "
27777 "a template argument list");
27781 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27782 /* The `>' token might be a greater-than operator again now. */
27783 parser
->greater_than_is_operator_p
27784 = saved_greater_than_is_operator_p
;
27785 /* Restore the SAVED_SCOPE. */
27786 parser
->scope
= saved_scope
;
27787 parser
->qualifying_scope
= saved_qualifying_scope
;
27788 parser
->object_scope
= saved_object_scope
;
27789 cp_unevaluated_operand
= saved_unevaluated_operand
;
27790 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27795 /* MEMBER_FUNCTION is a member function, or a friend. If default
27796 arguments, or the body of the function have not yet been parsed,
27800 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27802 timevar_push (TV_PARSE_INMETH
);
27803 /* If this member is a template, get the underlying
27805 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27806 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27808 /* There should not be any class definitions in progress at this
27809 point; the bodies of members are only parsed outside of all class
27811 gcc_assert (parser
->num_classes_being_defined
== 0);
27812 /* While we're parsing the member functions we might encounter more
27813 classes. We want to handle them right away, but we don't want
27814 them getting mixed up with functions that are currently in the
27816 push_unparsed_function_queues (parser
);
27818 /* Make sure that any template parameters are in scope. */
27819 maybe_begin_member_template_processing (member_function
);
27821 /* If the body of the function has not yet been parsed, parse it
27823 if (DECL_PENDING_INLINE_P (member_function
))
27825 tree function_scope
;
27826 cp_token_cache
*tokens
;
27828 /* The function is no longer pending; we are processing it. */
27829 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27830 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27831 DECL_PENDING_INLINE_P (member_function
) = 0;
27833 /* If this is a local class, enter the scope of the containing
27835 function_scope
= current_function_decl
;
27836 if (function_scope
)
27837 push_function_context ();
27839 /* Push the body of the function onto the lexer stack. */
27840 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27842 /* Let the front end know that we going to be defining this
27844 start_preparsed_function (member_function
, NULL_TREE
,
27845 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27847 /* Don't do access checking if it is a templated function. */
27848 if (processing_template_decl
)
27849 push_deferring_access_checks (dk_no_check
);
27851 /* #pragma omp declare reduction needs special parsing. */
27852 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27854 parser
->lexer
->in_pragma
= true;
27855 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27856 finish_function (/*inline_p=*/true);
27857 cp_check_omp_declare_reduction (member_function
);
27860 /* Now, parse the body of the function. */
27861 cp_parser_function_definition_after_declarator (parser
,
27862 /*inline_p=*/true);
27864 if (processing_template_decl
)
27865 pop_deferring_access_checks ();
27867 /* Leave the scope of the containing function. */
27868 if (function_scope
)
27869 pop_function_context ();
27870 cp_parser_pop_lexer (parser
);
27873 /* Remove any template parameters from the symbol table. */
27874 maybe_end_member_template_processing ();
27876 /* Restore the queue. */
27877 pop_unparsed_function_queues (parser
);
27878 timevar_pop (TV_PARSE_INMETH
);
27881 /* If DECL contains any default args, remember it on the unparsed
27882 functions queue. */
27885 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27889 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27891 probe
= TREE_CHAIN (probe
))
27892 if (TREE_PURPOSE (probe
))
27894 cp_default_arg_entry entry
= {current_class_type
, decl
};
27895 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27900 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27901 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27902 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27903 from the parameter-type-list. */
27906 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27907 tree default_arg
, tree parmtype
)
27909 cp_token_cache
*tokens
;
27913 if (default_arg
== error_mark_node
)
27914 return error_mark_node
;
27916 /* Push the saved tokens for the default argument onto the parser's
27918 tokens
= DEFARG_TOKENS (default_arg
);
27919 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27921 start_lambda_scope (decl
);
27923 /* Parse the default argument. */
27924 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27925 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27928 finish_lambda_scope ();
27930 if (parsed_arg
== error_mark_node
)
27931 cp_parser_skip_to_end_of_statement (parser
);
27933 if (!processing_template_decl
)
27935 /* In a non-template class, check conversions now. In a template,
27936 we'll wait and instantiate these as needed. */
27937 if (TREE_CODE (decl
) == PARM_DECL
)
27938 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27939 tf_warning_or_error
);
27940 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27941 parsed_arg
= error_mark_node
;
27943 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27946 /* If the token stream has not been completely used up, then
27947 there was extra junk after the end of the default
27949 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27951 if (TREE_CODE (decl
) == PARM_DECL
)
27952 cp_parser_error (parser
, "expected %<,%>");
27954 cp_parser_error (parser
, "expected %<;%>");
27957 /* Revert to the main lexer. */
27958 cp_parser_pop_lexer (parser
);
27963 /* FIELD is a non-static data member with an initializer which we saved for
27964 later; parse it now. */
27967 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27971 maybe_begin_member_template_processing (field
);
27973 push_unparsed_function_queues (parser
);
27974 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27975 DECL_INITIAL (field
),
27977 pop_unparsed_function_queues (parser
);
27979 maybe_end_member_template_processing ();
27981 DECL_INITIAL (field
) = def
;
27984 /* FN is a FUNCTION_DECL which may contains a parameter with an
27985 unparsed DEFAULT_ARG. Parse the default args now. This function
27986 assumes that the current scope is the scope in which the default
27987 argument should be processed. */
27990 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27992 bool saved_local_variables_forbidden_p
;
27993 tree parm
, parmdecl
;
27995 /* While we're parsing the default args, we might (due to the
27996 statement expression extension) encounter more classes. We want
27997 to handle them right away, but we don't want them getting mixed
27998 up with default args that are currently in the queue. */
27999 push_unparsed_function_queues (parser
);
28001 /* Local variable names (and the `this' keyword) may not appear
28002 in a default argument. */
28003 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28004 parser
->local_variables_forbidden_p
= true;
28006 push_defarg_context (fn
);
28008 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28009 parmdecl
= DECL_ARGUMENTS (fn
);
28010 parm
&& parm
!= void_list_node
;
28011 parm
= TREE_CHAIN (parm
),
28012 parmdecl
= DECL_CHAIN (parmdecl
))
28014 tree default_arg
= TREE_PURPOSE (parm
);
28016 vec
<tree
, va_gc
> *insts
;
28023 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28024 /* This can happen for a friend declaration for a function
28025 already declared with default arguments. */
28029 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28031 TREE_VALUE (parm
));
28032 TREE_PURPOSE (parm
) = parsed_arg
;
28034 /* Update any instantiations we've already created. */
28035 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28036 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28037 TREE_PURPOSE (copy
) = parsed_arg
;
28040 pop_defarg_context ();
28042 /* Make sure no default arg is missing. */
28043 check_default_args (fn
);
28045 /* Restore the state of local_variables_forbidden_p. */
28046 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28048 /* Restore the queue. */
28049 pop_unparsed_function_queues (parser
);
28052 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28054 sizeof ... ( identifier )
28056 where the 'sizeof' token has already been consumed. */
28059 cp_parser_sizeof_pack (cp_parser
*parser
)
28061 /* Consume the `...'. */
28062 cp_lexer_consume_token (parser
->lexer
);
28063 maybe_warn_variadic_templates ();
28065 matching_parens parens
;
28066 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28068 parens
.consume_open (parser
);
28070 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28071 "%<sizeof...%> argument must be surrounded by parentheses");
28073 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28074 tree name
= cp_parser_identifier (parser
);
28075 if (name
== error_mark_node
)
28076 return error_mark_node
;
28077 /* The name is not qualified. */
28078 parser
->scope
= NULL_TREE
;
28079 parser
->qualifying_scope
= NULL_TREE
;
28080 parser
->object_scope
= NULL_TREE
;
28081 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28082 if (expr
== error_mark_node
)
28083 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28085 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28086 expr
= TREE_TYPE (expr
);
28087 else if (TREE_CODE (expr
) == CONST_DECL
)
28088 expr
= DECL_INITIAL (expr
);
28089 expr
= make_pack_expansion (expr
);
28090 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28093 parens
.require_close (parser
);
28098 /* Parse the operand of `sizeof' (or a similar operator). Returns
28099 either a TYPE or an expression, depending on the form of the
28100 input. The KEYWORD indicates which kind of expression we have
28104 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28106 tree expr
= NULL_TREE
;
28107 const char *saved_message
;
28109 bool saved_integral_constant_expression_p
;
28110 bool saved_non_integral_constant_expression_p
;
28112 /* If it's a `...', then we are computing the length of a parameter
28114 if (keyword
== RID_SIZEOF
28115 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28116 return cp_parser_sizeof_pack (parser
);
28118 /* Types cannot be defined in a `sizeof' expression. Save away the
28120 saved_message
= parser
->type_definition_forbidden_message
;
28121 /* And create the new one. */
28122 tmp
= concat ("types may not be defined in %<",
28123 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28124 "%> expressions", NULL
);
28125 parser
->type_definition_forbidden_message
= tmp
;
28127 /* The restrictions on constant-expressions do not apply inside
28128 sizeof expressions. */
28129 saved_integral_constant_expression_p
28130 = parser
->integral_constant_expression_p
;
28131 saved_non_integral_constant_expression_p
28132 = parser
->non_integral_constant_expression_p
;
28133 parser
->integral_constant_expression_p
= false;
28135 /* Do not actually evaluate the expression. */
28136 ++cp_unevaluated_operand
;
28137 ++c_inhibit_evaluation_warnings
;
28138 /* If it's a `(', then we might be looking at the type-id
28140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28142 tree type
= NULL_TREE
;
28144 /* We can't be sure yet whether we're looking at a type-id or an
28146 cp_parser_parse_tentatively (parser
);
28148 matching_parens parens
;
28149 parens
.consume_open (parser
);
28151 /* Note: as a GNU Extension, compound literals are considered
28152 postfix-expressions as they are in C99, so they are valid
28153 arguments to sizeof. See comment in cp_parser_cast_expression
28155 if (cp_parser_compound_literal_p (parser
))
28156 cp_parser_simulate_error (parser
);
28159 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28160 parser
->in_type_id_in_expr_p
= true;
28161 /* Look for the type-id. */
28162 type
= cp_parser_type_id (parser
);
28163 /* Look for the closing `)'. */
28164 parens
.require_close (parser
);
28165 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28168 /* If all went well, then we're done. */
28169 if (cp_parser_parse_definitely (parser
))
28171 cp_decl_specifier_seq decl_specs
;
28173 /* Build a trivial decl-specifier-seq. */
28174 clear_decl_specs (&decl_specs
);
28175 decl_specs
.type
= type
;
28177 /* Call grokdeclarator to figure out what type this is. */
28178 expr
= grokdeclarator (NULL
,
28182 /*attrlist=*/NULL
);
28186 /* If the type-id production did not work out, then we must be
28187 looking at the unary-expression production. */
28189 expr
= cp_parser_unary_expression (parser
);
28191 /* Go back to evaluating expressions. */
28192 --cp_unevaluated_operand
;
28193 --c_inhibit_evaluation_warnings
;
28195 /* Free the message we created. */
28197 /* And restore the old one. */
28198 parser
->type_definition_forbidden_message
= saved_message
;
28199 parser
->integral_constant_expression_p
28200 = saved_integral_constant_expression_p
;
28201 parser
->non_integral_constant_expression_p
28202 = saved_non_integral_constant_expression_p
;
28207 /* If the current declaration has no declarator, return true. */
28210 cp_parser_declares_only_class_p (cp_parser
*parser
)
28212 /* If the next token is a `;' or a `,' then there is no
28214 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28215 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28218 /* Update the DECL_SPECS to reflect the storage class indicated by
28222 cp_parser_set_storage_class (cp_parser
*parser
,
28223 cp_decl_specifier_seq
*decl_specs
,
28227 cp_storage_class storage_class
;
28229 if (parser
->in_unbraced_linkage_specification_p
)
28231 error_at (token
->location
, "invalid use of %qD in linkage specification",
28232 ridpointers
[keyword
]);
28235 else if (decl_specs
->storage_class
!= sc_none
)
28237 decl_specs
->conflicting_specifiers_p
= true;
28241 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28242 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28243 && decl_specs
->gnu_thread_keyword_p
)
28245 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28246 "%<__thread%> before %qD", ridpointers
[keyword
]);
28252 storage_class
= sc_auto
;
28255 storage_class
= sc_register
;
28258 storage_class
= sc_static
;
28261 storage_class
= sc_extern
;
28264 storage_class
= sc_mutable
;
28267 gcc_unreachable ();
28269 decl_specs
->storage_class
= storage_class
;
28270 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28272 /* A storage class specifier cannot be applied alongside a typedef
28273 specifier. If there is a typedef specifier present then set
28274 conflicting_specifiers_p which will trigger an error later
28275 on in grokdeclarator. */
28276 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28277 decl_specs
->conflicting_specifiers_p
= true;
28280 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28281 is true, the type is a class or enum definition. */
28284 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28287 bool type_definition_p
)
28289 decl_specs
->any_specifiers_p
= true;
28291 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28292 (with, for example, in "typedef int wchar_t;") we remember that
28293 this is what happened. In system headers, we ignore these
28294 declarations so that G++ can work with system headers that are not
28296 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28297 && !type_definition_p
28298 && (type_spec
== boolean_type_node
28299 || type_spec
== char16_type_node
28300 || type_spec
== char32_type_node
28301 || type_spec
== wchar_type_node
)
28302 && (decl_specs
->type
28303 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28304 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28305 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28306 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28308 decl_specs
->redefined_builtin_type
= type_spec
;
28309 set_and_check_decl_spec_loc (decl_specs
,
28310 ds_redefined_builtin_type_spec
,
28312 if (!decl_specs
->type
)
28314 decl_specs
->type
= type_spec
;
28315 decl_specs
->type_definition_p
= false;
28316 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28319 else if (decl_specs
->type
)
28320 decl_specs
->multiple_types_p
= true;
28323 decl_specs
->type
= type_spec
;
28324 decl_specs
->type_definition_p
= type_definition_p
;
28325 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28326 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28330 /* True iff TOKEN is the GNU keyword __thread. */
28333 token_is__thread (cp_token
*token
)
28335 gcc_assert (token
->keyword
== RID_THREAD
);
28336 return id_equal (token
->u
.value
, "__thread");
28339 /* Set the location for a declarator specifier and check if it is
28342 DECL_SPECS is the sequence of declarator specifiers onto which to
28345 DS is the single declarator specifier to set which location is to
28346 be set onto the existing sequence of declarators.
28348 LOCATION is the location for the declarator specifier to
28352 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28353 cp_decl_spec ds
, cp_token
*token
)
28355 gcc_assert (ds
< ds_last
);
28357 if (decl_specs
== NULL
)
28360 source_location location
= token
->location
;
28362 if (decl_specs
->locations
[ds
] == 0)
28364 decl_specs
->locations
[ds
] = location
;
28365 if (ds
== ds_thread
)
28366 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28372 if (decl_specs
->locations
[ds_long_long
] != 0)
28373 error_at (location
,
28374 "%<long long long%> is too long for GCC");
28377 decl_specs
->locations
[ds_long_long
] = location
;
28378 pedwarn_cxx98 (location
,
28380 "ISO C++ 1998 does not support %<long long%>");
28383 else if (ds
== ds_thread
)
28385 bool gnu
= token_is__thread (token
);
28386 gcc_rich_location
richloc (location
);
28387 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28389 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28390 error_at (&richloc
,
28391 "both %<__thread%> and %<thread_local%> specified");
28395 richloc
.add_fixit_remove ();
28396 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28401 static const char *const decl_spec_names
[] = {
28418 gcc_rich_location
richloc (location
);
28419 richloc
.add_fixit_remove ();
28420 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28425 /* Return true iff the declarator specifier DS is present in the
28426 sequence of declarator specifiers DECL_SPECS. */
28429 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28432 gcc_assert (ds
< ds_last
);
28434 if (decl_specs
== NULL
)
28437 return decl_specs
->locations
[ds
] != 0;
28440 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28441 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28444 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28446 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28449 /* Issue an error message indicating that TOKEN_DESC was expected.
28450 If KEYWORD is true, it indicated this function is called by
28451 cp_parser_require_keword and the required token can only be
28452 a indicated keyword.
28454 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28455 within any error as the location of an "opening" token matching
28456 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28457 RT_CLOSE_PAREN). */
28460 cp_parser_required_error (cp_parser
*parser
,
28461 required_token token_desc
,
28463 location_t matching_location
)
28465 if (cp_parser_simulate_error (parser
))
28468 const char *gmsgid
= NULL
;
28469 switch (token_desc
)
28472 gmsgid
= G_("expected %<new%>");
28475 gmsgid
= G_("expected %<delete%>");
28478 gmsgid
= G_("expected %<return%>");
28481 gmsgid
= G_("expected %<while%>");
28484 gmsgid
= G_("expected %<extern%>");
28486 case RT_STATIC_ASSERT
:
28487 gmsgid
= G_("expected %<static_assert%>");
28490 gmsgid
= G_("expected %<decltype%>");
28493 gmsgid
= G_("expected %<operator%>");
28496 gmsgid
= G_("expected %<class%>");
28499 gmsgid
= G_("expected %<template%>");
28502 gmsgid
= G_("expected %<namespace%>");
28505 gmsgid
= G_("expected %<using%>");
28508 gmsgid
= G_("expected %<asm%>");
28511 gmsgid
= G_("expected %<try%>");
28514 gmsgid
= G_("expected %<catch%>");
28517 gmsgid
= G_("expected %<throw%>");
28520 gmsgid
= G_("expected %<__label__%>");
28523 gmsgid
= G_("expected %<@try%>");
28525 case RT_AT_SYNCHRONIZED
:
28526 gmsgid
= G_("expected %<@synchronized%>");
28529 gmsgid
= G_("expected %<@throw%>");
28531 case RT_TRANSACTION_ATOMIC
:
28532 gmsgid
= G_("expected %<__transaction_atomic%>");
28534 case RT_TRANSACTION_RELAXED
:
28535 gmsgid
= G_("expected %<__transaction_relaxed%>");
28541 if (!gmsgid
&& !keyword
)
28543 switch (token_desc
)
28546 gmsgid
= G_("expected %<;%>");
28548 case RT_OPEN_PAREN
:
28549 gmsgid
= G_("expected %<(%>");
28551 case RT_CLOSE_BRACE
:
28552 gmsgid
= G_("expected %<}%>");
28554 case RT_OPEN_BRACE
:
28555 gmsgid
= G_("expected %<{%>");
28557 case RT_CLOSE_SQUARE
:
28558 gmsgid
= G_("expected %<]%>");
28560 case RT_OPEN_SQUARE
:
28561 gmsgid
= G_("expected %<[%>");
28564 gmsgid
= G_("expected %<,%>");
28567 gmsgid
= G_("expected %<::%>");
28570 gmsgid
= G_("expected %<<%>");
28573 gmsgid
= G_("expected %<>%>");
28576 gmsgid
= G_("expected %<=%>");
28579 gmsgid
= G_("expected %<...%>");
28582 gmsgid
= G_("expected %<*%>");
28585 gmsgid
= G_("expected %<~%>");
28588 gmsgid
= G_("expected %<:%>");
28590 case RT_COLON_SCOPE
:
28591 gmsgid
= G_("expected %<:%> or %<::%>");
28593 case RT_CLOSE_PAREN
:
28594 gmsgid
= G_("expected %<)%>");
28596 case RT_COMMA_CLOSE_PAREN
:
28597 gmsgid
= G_("expected %<,%> or %<)%>");
28599 case RT_PRAGMA_EOL
:
28600 gmsgid
= G_("expected end of line");
28603 gmsgid
= G_("expected identifier");
28606 gmsgid
= G_("expected selection-statement");
28609 gmsgid
= G_("expected iteration-statement");
28612 gmsgid
= G_("expected jump-statement");
28615 gmsgid
= G_("expected class-key");
28617 case RT_CLASS_TYPENAME_TEMPLATE
:
28618 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28621 gcc_unreachable ();
28626 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28630 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28631 issue an error message indicating that TOKEN_DESC was expected.
28633 Returns the token consumed, if the token had the appropriate type.
28634 Otherwise, returns NULL.
28636 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28637 within any error as the location of an "opening" token matching
28638 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28639 RT_CLOSE_PAREN). */
28642 cp_parser_require (cp_parser
* parser
,
28643 enum cpp_ttype type
,
28644 required_token token_desc
,
28645 location_t matching_location
)
28647 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28648 return cp_lexer_consume_token (parser
->lexer
);
28651 /* Output the MESSAGE -- unless we're parsing tentatively. */
28652 if (!cp_parser_simulate_error (parser
))
28653 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28654 matching_location
);
28659 /* An error message is produced if the next token is not '>'.
28660 All further tokens are skipped until the desired token is
28661 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28664 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28666 /* Current level of '< ... >'. */
28667 unsigned level
= 0;
28668 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28669 unsigned nesting_depth
= 0;
28671 /* Are we ready, yet? If not, issue error message. */
28672 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28675 /* Skip tokens until the desired token is found. */
28678 /* Peek at the next token. */
28679 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28682 if (!nesting_depth
)
28687 if (cxx_dialect
== cxx98
)
28688 /* C++0x views the `>>' operator as two `>' tokens, but
28691 else if (!nesting_depth
&& level
-- == 0)
28693 /* We've hit a `>>' where the first `>' closes the
28694 template argument list, and the second `>' is
28695 spurious. Just consume the `>>' and stop; we've
28696 already produced at least one error. */
28697 cp_lexer_consume_token (parser
->lexer
);
28700 /* Fall through for C++0x, so we handle the second `>' in
28702 gcc_fallthrough ();
28705 if (!nesting_depth
&& level
-- == 0)
28707 /* We've reached the token we want, consume it and stop. */
28708 cp_lexer_consume_token (parser
->lexer
);
28713 case CPP_OPEN_PAREN
:
28714 case CPP_OPEN_SQUARE
:
28718 case CPP_CLOSE_PAREN
:
28719 case CPP_CLOSE_SQUARE
:
28720 if (nesting_depth
-- == 0)
28725 case CPP_PRAGMA_EOL
:
28726 case CPP_SEMICOLON
:
28727 case CPP_OPEN_BRACE
:
28728 case CPP_CLOSE_BRACE
:
28729 /* The '>' was probably forgotten, don't look further. */
28736 /* Consume this token. */
28737 cp_lexer_consume_token (parser
->lexer
);
28741 /* If the next token is the indicated keyword, consume it. Otherwise,
28742 issue an error message indicating that TOKEN_DESC was expected.
28744 Returns the token consumed, if the token had the appropriate type.
28745 Otherwise, returns NULL. */
28748 cp_parser_require_keyword (cp_parser
* parser
,
28750 required_token token_desc
)
28752 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28754 if (token
&& token
->keyword
!= keyword
)
28756 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28764 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28765 function-definition. */
28768 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28770 return (/* An ordinary function-body begins with an `{'. */
28771 token
->type
== CPP_OPEN_BRACE
28772 /* A ctor-initializer begins with a `:'. */
28773 || token
->type
== CPP_COLON
28774 /* A function-try-block begins with `try'. */
28775 || token
->keyword
== RID_TRY
28776 /* A function-transaction-block begins with `__transaction_atomic'
28777 or `__transaction_relaxed'. */
28778 || token
->keyword
== RID_TRANSACTION_ATOMIC
28779 || token
->keyword
== RID_TRANSACTION_RELAXED
28780 /* The named return value extension begins with `return'. */
28781 || token
->keyword
== RID_RETURN
);
28784 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28788 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28792 token
= cp_lexer_peek_token (parser
->lexer
);
28793 return (token
->type
== CPP_OPEN_BRACE
28794 || (token
->type
== CPP_COLON
28795 && !parser
->colon_doesnt_start_class_def_p
));
28798 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28799 C++0x) ending a template-argument. */
28802 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28806 token
= cp_lexer_peek_token (parser
->lexer
);
28807 return (token
->type
== CPP_COMMA
28808 || token
->type
== CPP_GREATER
28809 || token
->type
== CPP_ELLIPSIS
28810 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28813 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28814 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28817 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28822 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28823 if (token
->type
== CPP_LESS
)
28825 /* Check for the sequence `<::' in the original code. It would be lexed as
28826 `[:', where `[' is a digraph, and there is no whitespace before
28828 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28831 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28832 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28838 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28839 or none_type otherwise. */
28841 static enum tag_types
28842 cp_parser_token_is_class_key (cp_token
* token
)
28844 switch (token
->keyword
)
28849 return record_type
;
28858 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28859 or none_type otherwise or if the token is null. */
28861 static enum tag_types
28862 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28867 switch (token
->keyword
)
28872 return typename_type
;
28879 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28882 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28884 if (type
== error_mark_node
)
28886 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28888 if (permerror (input_location
, "%qs tag used in naming %q#T",
28889 class_key
== union_type
? "union"
28890 : class_key
== record_type
? "struct" : "class",
28892 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28893 "%q#T was previously declared here", type
);
28897 /* Issue an error message if DECL is redeclared with different
28898 access than its original declaration [class.access.spec/3].
28899 This applies to nested classes, nested class templates and
28900 enumerations [class.mem/1]. */
28903 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28906 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28907 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28910 if ((TREE_PRIVATE (decl
)
28911 != (current_access_specifier
== access_private_node
))
28912 || (TREE_PROTECTED (decl
)
28913 != (current_access_specifier
== access_protected_node
)))
28914 error_at (location
, "%qD redeclared with different access", decl
);
28917 /* Look for the `template' keyword, as a syntactic disambiguator.
28918 Return TRUE iff it is present, in which case it will be
28922 cp_parser_optional_template_keyword (cp_parser
*parser
)
28924 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28926 /* In C++98 the `template' keyword can only be used within templates;
28927 outside templates the parser can always figure out what is a
28928 template and what is not. In C++11, per the resolution of DR 468,
28929 `template' is allowed in cases where it is not strictly necessary. */
28930 if (!processing_template_decl
28931 && pedantic
&& cxx_dialect
== cxx98
)
28933 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28934 pedwarn (token
->location
, OPT_Wpedantic
,
28935 "in C++98 %<template%> (as a disambiguator) is only "
28936 "allowed within templates");
28937 /* If this part of the token stream is rescanned, the same
28938 error message would be generated. So, we purge the token
28939 from the stream. */
28940 cp_lexer_purge_token (parser
->lexer
);
28945 /* Consume the `template' keyword. */
28946 cp_lexer_consume_token (parser
->lexer
);
28953 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28954 set PARSER->SCOPE, and perform other related actions. */
28957 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28959 struct tree_check
*check_value
;
28961 /* Get the stored value. */
28962 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28963 /* Set the scope from the stored value. */
28964 parser
->scope
= saved_checks_value (check_value
);
28965 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28966 parser
->object_scope
= NULL_TREE
;
28969 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28970 encounter the end of a block before what we were looking for. */
28973 cp_parser_cache_group (cp_parser
*parser
,
28974 enum cpp_ttype end
,
28979 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28981 /* Abort a parenthesized expression if we encounter a semicolon. */
28982 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28983 && token
->type
== CPP_SEMICOLON
)
28985 /* If we've reached the end of the file, stop. */
28986 if (token
->type
== CPP_EOF
28987 || (end
!= CPP_PRAGMA_EOL
28988 && token
->type
== CPP_PRAGMA_EOL
))
28990 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28991 /* We've hit the end of an enclosing block, so there's been some
28992 kind of syntax error. */
28995 /* Consume the token. */
28996 cp_lexer_consume_token (parser
->lexer
);
28997 /* See if it starts a new group. */
28998 if (token
->type
== CPP_OPEN_BRACE
)
29000 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29001 /* In theory this should probably check end == '}', but
29002 cp_parser_save_member_function_body needs it to exit
29003 after either '}' or ')' when called with ')'. */
29007 else if (token
->type
== CPP_OPEN_PAREN
)
29009 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29010 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29013 else if (token
->type
== CPP_PRAGMA
)
29014 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29015 else if (token
->type
== end
)
29020 /* Like above, for caching a default argument or NSDMI. Both of these are
29021 terminated by a non-nested comma, but it can be unclear whether or not a
29022 comma is nested in a template argument list unless we do more parsing.
29023 In order to handle this ambiguity, when we encounter a ',' after a '<'
29024 we try to parse what follows as a parameter-declaration-list (in the
29025 case of a default argument) or a member-declarator (in the case of an
29026 NSDMI). If that succeeds, then we stop caching. */
29029 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29031 unsigned depth
= 0;
29032 int maybe_template_id
= 0;
29033 cp_token
*first_token
;
29035 tree default_argument
;
29037 /* Add tokens until we have processed the entire default
29038 argument. We add the range [first_token, token). */
29039 first_token
= cp_lexer_peek_token (parser
->lexer
);
29040 if (first_token
->type
== CPP_OPEN_BRACE
)
29042 /* For list-initialization, this is straightforward. */
29043 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29044 token
= cp_lexer_peek_token (parser
->lexer
);
29050 /* Peek at the next token. */
29051 token
= cp_lexer_peek_token (parser
->lexer
);
29052 /* What we do depends on what token we have. */
29053 switch (token
->type
)
29055 /* In valid code, a default argument must be
29056 immediately followed by a `,' `)', or `...'. */
29058 if (depth
== 0 && maybe_template_id
)
29060 /* If we've seen a '<', we might be in a
29061 template-argument-list. Until Core issue 325 is
29062 resolved, we don't know how this situation ought
29063 to be handled, so try to DTRT. We check whether
29064 what comes after the comma is a valid parameter
29065 declaration list. If it is, then the comma ends
29066 the default argument; otherwise the default
29067 argument continues. */
29068 bool error
= false;
29071 /* Set ITALP so cp_parser_parameter_declaration_list
29072 doesn't decide to commit to this parse. */
29073 bool saved_italp
= parser
->in_template_argument_list_p
;
29074 parser
->in_template_argument_list_p
= true;
29076 cp_parser_parse_tentatively (parser
);
29080 /* Parse declarators until we reach a non-comma or
29081 somthing that cannot be an initializer.
29082 Just checking whether we're looking at a single
29083 declarator is insufficient. Consider:
29084 int var = tuple<T,U>::x;
29085 The template parameter 'U' looks exactly like a
29089 int ctor_dtor_or_conv_p
;
29090 cp_lexer_consume_token (parser
->lexer
);
29091 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29092 &ctor_dtor_or_conv_p
,
29093 /*parenthesized_p=*/NULL
,
29095 /*friend_p=*/false);
29096 peek
= cp_lexer_peek_token (parser
->lexer
);
29097 if (cp_parser_error_occurred (parser
))
29100 while (peek
->type
== CPP_COMMA
);
29101 /* If we met an '=' or ';' then the original comma
29102 was the end of the NSDMI. Otherwise assume
29103 we're still in the NSDMI. */
29104 error
= (peek
->type
!= CPP_EQ
29105 && peek
->type
!= CPP_SEMICOLON
);
29109 cp_lexer_consume_token (parser
->lexer
);
29110 begin_scope (sk_function_parms
, NULL_TREE
);
29111 if (cp_parser_parameter_declaration_list (parser
)
29112 == error_mark_node
)
29114 pop_bindings_and_leave_scope ();
29116 if (!cp_parser_error_occurred (parser
) && !error
)
29118 cp_parser_abort_tentative_parse (parser
);
29120 parser
->in_template_argument_list_p
= saved_italp
;
29124 case CPP_CLOSE_PAREN
:
29126 /* If we run into a non-nested `;', `}', or `]',
29127 then the code is invalid -- but the default
29128 argument is certainly over. */
29129 case CPP_SEMICOLON
:
29130 case CPP_CLOSE_BRACE
:
29131 case CPP_CLOSE_SQUARE
:
29133 /* Handle correctly int n = sizeof ... ( p ); */
29134 && token
->type
!= CPP_ELLIPSIS
)
29136 /* Update DEPTH, if necessary. */
29137 else if (token
->type
== CPP_CLOSE_PAREN
29138 || token
->type
== CPP_CLOSE_BRACE
29139 || token
->type
== CPP_CLOSE_SQUARE
)
29143 case CPP_OPEN_PAREN
:
29144 case CPP_OPEN_SQUARE
:
29145 case CPP_OPEN_BRACE
:
29151 /* This might be the comparison operator, or it might
29152 start a template argument list. */
29153 ++maybe_template_id
;
29157 if (cxx_dialect
== cxx98
)
29159 /* Fall through for C++0x, which treats the `>>'
29160 operator like two `>' tokens in certain
29162 gcc_fallthrough ();
29167 /* This might be an operator, or it might close a
29168 template argument list. But if a previous '<'
29169 started a template argument list, this will have
29170 closed it, so we can't be in one anymore. */
29171 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29172 if (maybe_template_id
< 0)
29173 maybe_template_id
= 0;
29177 /* If we run out of tokens, issue an error message. */
29179 case CPP_PRAGMA_EOL
:
29180 error_at (token
->location
, "file ends in default argument");
29181 return error_mark_node
;
29185 /* In these cases, we should look for template-ids.
29186 For example, if the default argument is
29187 `X<int, double>()', we need to do name lookup to
29188 figure out whether or not `X' is a template; if
29189 so, the `,' does not end the default argument.
29191 That is not yet done. */
29198 /* If we've reached the end, stop. */
29202 /* Add the token to the token block. */
29203 token
= cp_lexer_consume_token (parser
->lexer
);
29206 /* Create a DEFAULT_ARG to represent the unparsed default
29208 default_argument
= make_node (DEFAULT_ARG
);
29209 DEFARG_TOKENS (default_argument
)
29210 = cp_token_cache_new (first_token
, token
);
29211 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29213 return default_argument
;
29216 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29219 defarg_location (tree default_argument
)
29221 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29222 location_t start
= tokens
->first
->location
;
29223 location_t end
= tokens
->last
->location
;
29224 return make_location (start
, start
, end
);
29227 /* Begin parsing tentatively. We always save tokens while parsing
29228 tentatively so that if the tentative parsing fails we can restore the
29232 cp_parser_parse_tentatively (cp_parser
* parser
)
29234 /* Enter a new parsing context. */
29235 parser
->context
= cp_parser_context_new (parser
->context
);
29236 /* Begin saving tokens. */
29237 cp_lexer_save_tokens (parser
->lexer
);
29238 /* In order to avoid repetitive access control error messages,
29239 access checks are queued up until we are no longer parsing
29241 push_deferring_access_checks (dk_deferred
);
29244 /* Commit to the currently active tentative parse. */
29247 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29249 cp_parser_context
*context
;
29252 /* Mark all of the levels as committed. */
29253 lexer
= parser
->lexer
;
29254 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29256 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29258 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29259 while (!cp_lexer_saving_tokens (lexer
))
29260 lexer
= lexer
->next
;
29261 cp_lexer_commit_tokens (lexer
);
29265 /* Commit to the topmost currently active tentative parse.
29267 Note that this function shouldn't be called when there are
29268 irreversible side-effects while in a tentative state. For
29269 example, we shouldn't create a permanent entry in the symbol
29270 table, or issue an error message that might not apply if the
29271 tentative parse is aborted. */
29274 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29276 cp_parser_context
*context
= parser
->context
;
29277 cp_lexer
*lexer
= parser
->lexer
;
29281 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29283 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29285 while (!cp_lexer_saving_tokens (lexer
))
29286 lexer
= lexer
->next
;
29287 cp_lexer_commit_tokens (lexer
);
29291 /* Abort the currently active tentative parse. All consumed tokens
29292 will be rolled back, and no diagnostics will be issued. */
29295 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29297 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29298 || errorcount
> 0);
29299 cp_parser_simulate_error (parser
);
29300 /* Now, pretend that we want to see if the construct was
29301 successfully parsed. */
29302 cp_parser_parse_definitely (parser
);
29305 /* Stop parsing tentatively. If a parse error has occurred, restore the
29306 token stream. Otherwise, commit to the tokens we have consumed.
29307 Returns true if no error occurred; false otherwise. */
29310 cp_parser_parse_definitely (cp_parser
* parser
)
29312 bool error_occurred
;
29313 cp_parser_context
*context
;
29315 /* Remember whether or not an error occurred, since we are about to
29316 destroy that information. */
29317 error_occurred
= cp_parser_error_occurred (parser
);
29318 /* Remove the topmost context from the stack. */
29319 context
= parser
->context
;
29320 parser
->context
= context
->next
;
29321 /* If no parse errors occurred, commit to the tentative parse. */
29322 if (!error_occurred
)
29324 /* Commit to the tokens read tentatively, unless that was
29326 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29327 cp_lexer_commit_tokens (parser
->lexer
);
29329 pop_to_parent_deferring_access_checks ();
29331 /* Otherwise, if errors occurred, roll back our state so that things
29332 are just as they were before we began the tentative parse. */
29335 cp_lexer_rollback_tokens (parser
->lexer
);
29336 pop_deferring_access_checks ();
29338 /* Add the context to the front of the free list. */
29339 context
->next
= cp_parser_context_free_list
;
29340 cp_parser_context_free_list
= context
;
29342 return !error_occurred
;
29345 /* Returns true if we are parsing tentatively and are not committed to
29346 this tentative parse. */
29349 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29351 return (cp_parser_parsing_tentatively (parser
)
29352 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29355 /* Returns nonzero iff an error has occurred during the most recent
29356 tentative parse. */
29359 cp_parser_error_occurred (cp_parser
* parser
)
29361 return (cp_parser_parsing_tentatively (parser
)
29362 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29365 /* Returns nonzero if GNU extensions are allowed. */
29368 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29370 return parser
->allow_gnu_extensions_p
;
29373 /* Objective-C++ Productions */
29376 /* Parse an Objective-C expression, which feeds into a primary-expression
29380 objc-message-expression
29381 objc-string-literal
29382 objc-encode-expression
29383 objc-protocol-expression
29384 objc-selector-expression
29386 Returns a tree representation of the expression. */
29389 cp_parser_objc_expression (cp_parser
* parser
)
29391 /* Try to figure out what kind of declaration is present. */
29392 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29396 case CPP_OPEN_SQUARE
:
29397 return cp_parser_objc_message_expression (parser
);
29399 case CPP_OBJC_STRING
:
29400 kwd
= cp_lexer_consume_token (parser
->lexer
);
29401 return objc_build_string_object (kwd
->u
.value
);
29404 switch (kwd
->keyword
)
29406 case RID_AT_ENCODE
:
29407 return cp_parser_objc_encode_expression (parser
);
29409 case RID_AT_PROTOCOL
:
29410 return cp_parser_objc_protocol_expression (parser
);
29412 case RID_AT_SELECTOR
:
29413 return cp_parser_objc_selector_expression (parser
);
29420 error_at (kwd
->location
,
29421 "misplaced %<@%D%> Objective-C++ construct",
29423 cp_parser_skip_to_end_of_block_or_statement (parser
);
29426 return error_mark_node
;
29429 /* Parse an Objective-C message expression.
29431 objc-message-expression:
29432 [ objc-message-receiver objc-message-args ]
29434 Returns a representation of an Objective-C message. */
29437 cp_parser_objc_message_expression (cp_parser
* parser
)
29439 tree receiver
, messageargs
;
29441 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29442 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29443 receiver
= cp_parser_objc_message_receiver (parser
);
29444 messageargs
= cp_parser_objc_message_args (parser
);
29445 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29446 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29448 tree result
= objc_build_message_expr (receiver
, messageargs
);
29450 /* Construct a location e.g.
29453 ranging from the '[' to the ']', with the caret at the start. */
29454 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29455 protected_set_expr_location (result
, combined_loc
);
29460 /* Parse an objc-message-receiver.
29462 objc-message-receiver:
29464 simple-type-specifier
29466 Returns a representation of the type or expression. */
29469 cp_parser_objc_message_receiver (cp_parser
* parser
)
29473 /* An Objective-C message receiver may be either (1) a type
29474 or (2) an expression. */
29475 cp_parser_parse_tentatively (parser
);
29476 rcv
= cp_parser_expression (parser
);
29478 /* If that worked out, fine. */
29479 if (cp_parser_parse_definitely (parser
))
29482 cp_parser_parse_tentatively (parser
);
29483 rcv
= cp_parser_simple_type_specifier (parser
,
29484 /*decl_specs=*/NULL
,
29485 CP_PARSER_FLAGS_NONE
);
29487 if (cp_parser_parse_definitely (parser
))
29488 return objc_get_class_reference (rcv
);
29490 cp_parser_error (parser
, "objective-c++ message receiver expected");
29491 return error_mark_node
;
29494 /* Parse the arguments and selectors comprising an Objective-C message.
29499 objc-selector-args , objc-comma-args
29501 objc-selector-args:
29502 objc-selector [opt] : assignment-expression
29503 objc-selector-args objc-selector [opt] : assignment-expression
29506 assignment-expression
29507 objc-comma-args , assignment-expression
29509 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29510 selector arguments and TREE_VALUE containing a list of comma
29514 cp_parser_objc_message_args (cp_parser
* parser
)
29516 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29517 bool maybe_unary_selector_p
= true;
29518 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29520 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29522 tree selector
= NULL_TREE
, arg
;
29524 if (token
->type
!= CPP_COLON
)
29525 selector
= cp_parser_objc_selector (parser
);
29527 /* Detect if we have a unary selector. */
29528 if (maybe_unary_selector_p
29529 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29530 return build_tree_list (selector
, NULL_TREE
);
29532 maybe_unary_selector_p
= false;
29533 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29534 arg
= cp_parser_assignment_expression (parser
);
29537 = chainon (sel_args
,
29538 build_tree_list (selector
, arg
));
29540 token
= cp_lexer_peek_token (parser
->lexer
);
29543 /* Handle non-selector arguments, if any. */
29544 while (token
->type
== CPP_COMMA
)
29548 cp_lexer_consume_token (parser
->lexer
);
29549 arg
= cp_parser_assignment_expression (parser
);
29552 = chainon (addl_args
,
29553 build_tree_list (NULL_TREE
, arg
));
29555 token
= cp_lexer_peek_token (parser
->lexer
);
29558 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29560 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29561 return build_tree_list (error_mark_node
, error_mark_node
);
29564 return build_tree_list (sel_args
, addl_args
);
29567 /* Parse an Objective-C encode expression.
29569 objc-encode-expression:
29570 @encode objc-typename
29572 Returns an encoded representation of the type argument. */
29575 cp_parser_objc_encode_expression (cp_parser
* parser
)
29579 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29581 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29582 matching_parens parens
;
29583 parens
.require_open (parser
);
29584 token
= cp_lexer_peek_token (parser
->lexer
);
29585 type
= complete_type (cp_parser_type_id (parser
));
29586 parens
.require_close (parser
);
29590 error_at (token
->location
,
29591 "%<@encode%> must specify a type as an argument");
29592 return error_mark_node
;
29595 /* This happens if we find @encode(T) (where T is a template
29596 typename or something dependent on a template typename) when
29597 parsing a template. In that case, we can't compile it
29598 immediately, but we rather create an AT_ENCODE_EXPR which will
29599 need to be instantiated when the template is used.
29601 if (dependent_type_p (type
))
29603 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29604 TREE_READONLY (value
) = 1;
29609 /* Build a location of the form:
29612 with caret==start at the @ token, finishing at the close paren. */
29613 location_t combined_loc
29614 = make_location (start_loc
, start_loc
,
29615 cp_lexer_previous_token (parser
->lexer
)->location
);
29617 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29620 /* Parse an Objective-C @defs expression. */
29623 cp_parser_objc_defs_expression (cp_parser
*parser
)
29627 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29628 matching_parens parens
;
29629 parens
.require_open (parser
);
29630 name
= cp_parser_identifier (parser
);
29631 parens
.require_close (parser
);
29633 return objc_get_class_ivars (name
);
29636 /* Parse an Objective-C protocol expression.
29638 objc-protocol-expression:
29639 @protocol ( identifier )
29641 Returns a representation of the protocol expression. */
29644 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29647 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29649 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29650 matching_parens parens
;
29651 parens
.require_open (parser
);
29652 proto
= cp_parser_identifier (parser
);
29653 parens
.require_close (parser
);
29655 /* Build a location of the form:
29658 with caret==start at the @ token, finishing at the close paren. */
29659 location_t combined_loc
29660 = make_location (start_loc
, start_loc
,
29661 cp_lexer_previous_token (parser
->lexer
)->location
);
29662 tree result
= objc_build_protocol_expr (proto
);
29663 protected_set_expr_location (result
, combined_loc
);
29667 /* Parse an Objective-C selector expression.
29669 objc-selector-expression:
29670 @selector ( objc-method-signature )
29672 objc-method-signature:
29678 objc-selector-seq objc-selector :
29680 Returns a representation of the method selector. */
29683 cp_parser_objc_selector_expression (cp_parser
* parser
)
29685 tree sel_seq
= NULL_TREE
;
29686 bool maybe_unary_selector_p
= true;
29688 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29690 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29691 matching_parens parens
;
29692 parens
.require_open (parser
);
29693 token
= cp_lexer_peek_token (parser
->lexer
);
29695 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29696 || token
->type
== CPP_SCOPE
)
29698 tree selector
= NULL_TREE
;
29700 if (token
->type
!= CPP_COLON
29701 || token
->type
== CPP_SCOPE
)
29702 selector
= cp_parser_objc_selector (parser
);
29704 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29705 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29707 /* Detect if we have a unary selector. */
29708 if (maybe_unary_selector_p
)
29710 sel_seq
= selector
;
29711 goto finish_selector
;
29715 cp_parser_error (parser
, "expected %<:%>");
29718 maybe_unary_selector_p
= false;
29719 token
= cp_lexer_consume_token (parser
->lexer
);
29721 if (token
->type
== CPP_SCOPE
)
29724 = chainon (sel_seq
,
29725 build_tree_list (selector
, NULL_TREE
));
29727 = chainon (sel_seq
,
29728 build_tree_list (NULL_TREE
, NULL_TREE
));
29732 = chainon (sel_seq
,
29733 build_tree_list (selector
, NULL_TREE
));
29735 token
= cp_lexer_peek_token (parser
->lexer
);
29739 parens
.require_close (parser
);
29742 /* Build a location of the form:
29745 with caret==start at the @ token, finishing at the close paren. */
29746 location_t combined_loc
29747 = make_location (loc
, loc
,
29748 cp_lexer_previous_token (parser
->lexer
)->location
);
29749 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29750 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29751 protected_set_expr_location (result
, combined_loc
);
29755 /* Parse a list of identifiers.
29757 objc-identifier-list:
29759 objc-identifier-list , identifier
29761 Returns a TREE_LIST of identifier nodes. */
29764 cp_parser_objc_identifier_list (cp_parser
* parser
)
29770 identifier
= cp_parser_identifier (parser
);
29771 if (identifier
== error_mark_node
)
29772 return error_mark_node
;
29774 list
= build_tree_list (NULL_TREE
, identifier
);
29775 sep
= cp_lexer_peek_token (parser
->lexer
);
29777 while (sep
->type
== CPP_COMMA
)
29779 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29780 identifier
= cp_parser_identifier (parser
);
29781 if (identifier
== error_mark_node
)
29784 list
= chainon (list
, build_tree_list (NULL_TREE
,
29786 sep
= cp_lexer_peek_token (parser
->lexer
);
29792 /* Parse an Objective-C alias declaration.
29794 objc-alias-declaration:
29795 @compatibility_alias identifier identifier ;
29797 This function registers the alias mapping with the Objective-C front end.
29798 It returns nothing. */
29801 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29805 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29806 alias
= cp_parser_identifier (parser
);
29807 orig
= cp_parser_identifier (parser
);
29808 objc_declare_alias (alias
, orig
);
29809 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29812 /* Parse an Objective-C class forward-declaration.
29814 objc-class-declaration:
29815 @class objc-identifier-list ;
29817 The function registers the forward declarations with the Objective-C
29818 front end. It returns nothing. */
29821 cp_parser_objc_class_declaration (cp_parser
* parser
)
29823 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29828 id
= cp_parser_identifier (parser
);
29829 if (id
== error_mark_node
)
29832 objc_declare_class (id
);
29834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29835 cp_lexer_consume_token (parser
->lexer
);
29839 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29842 /* Parse a list of Objective-C protocol references.
29844 objc-protocol-refs-opt:
29845 objc-protocol-refs [opt]
29847 objc-protocol-refs:
29848 < objc-identifier-list >
29850 Returns a TREE_LIST of identifiers, if any. */
29853 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29855 tree protorefs
= NULL_TREE
;
29857 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29859 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29860 protorefs
= cp_parser_objc_identifier_list (parser
);
29861 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29867 /* Parse a Objective-C visibility specification. */
29870 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29872 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29874 switch (vis
->keyword
)
29876 case RID_AT_PRIVATE
:
29877 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29879 case RID_AT_PROTECTED
:
29880 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29882 case RID_AT_PUBLIC
:
29883 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29885 case RID_AT_PACKAGE
:
29886 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29892 /* Eat '@private'/'@protected'/'@public'. */
29893 cp_lexer_consume_token (parser
->lexer
);
29896 /* Parse an Objective-C method type. Return 'true' if it is a class
29897 (+) method, and 'false' if it is an instance (-) method. */
29900 cp_parser_objc_method_type (cp_parser
* parser
)
29902 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29908 /* Parse an Objective-C protocol qualifier. */
29911 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29913 tree quals
= NULL_TREE
, node
;
29914 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29916 node
= token
->u
.value
;
29918 while (node
&& identifier_p (node
)
29919 && (node
== ridpointers
[(int) RID_IN
]
29920 || node
== ridpointers
[(int) RID_OUT
]
29921 || node
== ridpointers
[(int) RID_INOUT
]
29922 || node
== ridpointers
[(int) RID_BYCOPY
]
29923 || node
== ridpointers
[(int) RID_BYREF
]
29924 || node
== ridpointers
[(int) RID_ONEWAY
]))
29926 quals
= tree_cons (NULL_TREE
, node
, quals
);
29927 cp_lexer_consume_token (parser
->lexer
);
29928 token
= cp_lexer_peek_token (parser
->lexer
);
29929 node
= token
->u
.value
;
29935 /* Parse an Objective-C typename. */
29938 cp_parser_objc_typename (cp_parser
* parser
)
29940 tree type_name
= NULL_TREE
;
29942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29944 tree proto_quals
, cp_type
= NULL_TREE
;
29946 matching_parens parens
;
29947 parens
.consume_open (parser
); /* Eat '('. */
29948 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29950 /* An ObjC type name may consist of just protocol qualifiers, in which
29951 case the type shall default to 'id'. */
29952 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29954 cp_type
= cp_parser_type_id (parser
);
29956 /* If the type could not be parsed, an error has already
29957 been produced. For error recovery, behave as if it had
29958 not been specified, which will use the default type
29960 if (cp_type
== error_mark_node
)
29962 cp_type
= NULL_TREE
;
29963 /* We need to skip to the closing parenthesis as
29964 cp_parser_type_id() does not seem to do it for
29966 cp_parser_skip_to_closing_parenthesis (parser
,
29967 /*recovering=*/true,
29968 /*or_comma=*/false,
29969 /*consume_paren=*/false);
29973 parens
.require_close (parser
);
29974 type_name
= build_tree_list (proto_quals
, cp_type
);
29980 /* Check to see if TYPE refers to an Objective-C selector name. */
29983 cp_parser_objc_selector_p (enum cpp_ttype type
)
29985 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29986 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29987 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29988 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29989 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29992 /* Parse an Objective-C selector. */
29995 cp_parser_objc_selector (cp_parser
* parser
)
29997 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29999 if (!cp_parser_objc_selector_p (token
->type
))
30001 error_at (token
->location
, "invalid Objective-C++ selector name");
30002 return error_mark_node
;
30005 /* C++ operator names are allowed to appear in ObjC selectors. */
30006 switch (token
->type
)
30008 case CPP_AND_AND
: return get_identifier ("and");
30009 case CPP_AND_EQ
: return get_identifier ("and_eq");
30010 case CPP_AND
: return get_identifier ("bitand");
30011 case CPP_OR
: return get_identifier ("bitor");
30012 case CPP_COMPL
: return get_identifier ("compl");
30013 case CPP_NOT
: return get_identifier ("not");
30014 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30015 case CPP_OR_OR
: return get_identifier ("or");
30016 case CPP_OR_EQ
: return get_identifier ("or_eq");
30017 case CPP_XOR
: return get_identifier ("xor");
30018 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30019 default: return token
->u
.value
;
30023 /* Parse an Objective-C params list. */
30026 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30028 tree params
= NULL_TREE
;
30029 bool maybe_unary_selector_p
= true;
30030 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30032 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30034 tree selector
= NULL_TREE
, type_name
, identifier
;
30035 tree parm_attr
= NULL_TREE
;
30037 if (token
->keyword
== RID_ATTRIBUTE
)
30040 if (token
->type
!= CPP_COLON
)
30041 selector
= cp_parser_objc_selector (parser
);
30043 /* Detect if we have a unary selector. */
30044 if (maybe_unary_selector_p
30045 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30047 params
= selector
; /* Might be followed by attributes. */
30051 maybe_unary_selector_p
= false;
30052 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30054 /* Something went quite wrong. There should be a colon
30055 here, but there is not. Stop parsing parameters. */
30058 type_name
= cp_parser_objc_typename (parser
);
30059 /* New ObjC allows attributes on parameters too. */
30060 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30061 parm_attr
= cp_parser_attributes_opt (parser
);
30062 identifier
= cp_parser_identifier (parser
);
30066 objc_build_keyword_decl (selector
,
30071 token
= cp_lexer_peek_token (parser
->lexer
);
30074 if (params
== NULL_TREE
)
30076 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30077 return error_mark_node
;
30080 /* We allow tail attributes for the method. */
30081 if (token
->keyword
== RID_ATTRIBUTE
)
30083 *attributes
= cp_parser_attributes_opt (parser
);
30084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30085 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30087 cp_parser_error (parser
,
30088 "method attributes must be specified at the end");
30089 return error_mark_node
;
30092 if (params
== NULL_TREE
)
30094 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30095 return error_mark_node
;
30100 /* Parse the non-keyword Objective-C params. */
30103 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30106 tree params
= make_node (TREE_LIST
);
30107 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30108 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30110 while (token
->type
== CPP_COMMA
)
30112 cp_parameter_declarator
*parmdecl
;
30115 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30116 token
= cp_lexer_peek_token (parser
->lexer
);
30118 if (token
->type
== CPP_ELLIPSIS
)
30120 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30122 token
= cp_lexer_peek_token (parser
->lexer
);
30126 /* TODO: parse attributes for tail parameters. */
30127 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30128 parm
= grokdeclarator (parmdecl
->declarator
,
30129 &parmdecl
->decl_specifiers
,
30130 PARM
, /*initialized=*/0,
30131 /*attrlist=*/NULL
);
30133 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30134 token
= cp_lexer_peek_token (parser
->lexer
);
30137 /* We allow tail attributes for the method. */
30138 if (token
->keyword
== RID_ATTRIBUTE
)
30140 if (*attributes
== NULL_TREE
)
30142 *attributes
= cp_parser_attributes_opt (parser
);
30143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30144 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30148 /* We have an error, but parse the attributes, so that we can
30150 *attributes
= cp_parser_attributes_opt (parser
);
30152 cp_parser_error (parser
,
30153 "method attributes must be specified at the end");
30154 return error_mark_node
;
30160 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30163 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30165 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30167 /* If the next token is `extern' and the following token is a string
30168 literal, then we have a linkage specification. */
30169 if (token
->keyword
== RID_EXTERN
30170 && cp_parser_is_pure_string_literal
30171 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30172 cp_parser_linkage_specification (parser
);
30173 /* Handle #pragma, if any. */
30174 else if (token
->type
== CPP_PRAGMA
)
30175 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30176 /* Allow stray semicolons. */
30177 else if (token
->type
== CPP_SEMICOLON
)
30178 cp_lexer_consume_token (parser
->lexer
);
30179 /* Mark methods as optional or required, when building protocols. */
30180 else if (token
->keyword
== RID_AT_OPTIONAL
)
30182 cp_lexer_consume_token (parser
->lexer
);
30183 objc_set_method_opt (true);
30185 else if (token
->keyword
== RID_AT_REQUIRED
)
30187 cp_lexer_consume_token (parser
->lexer
);
30188 objc_set_method_opt (false);
30190 else if (token
->keyword
== RID_NAMESPACE
)
30191 cp_parser_namespace_definition (parser
);
30192 /* Other stray characters must generate errors. */
30193 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30195 cp_lexer_consume_token (parser
->lexer
);
30196 error ("stray %qs between Objective-C++ methods",
30197 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30199 /* Finally, try to parse a block-declaration, or a function-definition. */
30201 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30204 /* Parse a method signature. */
30207 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30209 tree rettype
, kwdparms
, optparms
;
30210 bool ellipsis
= false;
30211 bool is_class_method
;
30213 is_class_method
= cp_parser_objc_method_type (parser
);
30214 rettype
= cp_parser_objc_typename (parser
);
30215 *attributes
= NULL_TREE
;
30216 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30217 if (kwdparms
== error_mark_node
)
30218 return error_mark_node
;
30219 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30220 if (optparms
== error_mark_node
)
30221 return error_mark_node
;
30223 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30227 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30230 cp_lexer_save_tokens (parser
->lexer
);
30231 tattr
= cp_parser_attributes_opt (parser
);
30232 gcc_assert (tattr
) ;
30234 /* If the attributes are followed by a method introducer, this is not allowed.
30235 Dump the attributes and flag the situation. */
30236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30237 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30240 /* Otherwise, the attributes introduce some interstitial code, possibly so
30241 rewind to allow that check. */
30242 cp_lexer_rollback_tokens (parser
->lexer
);
30246 /* Parse an Objective-C method prototype list. */
30249 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30251 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30253 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30255 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30257 tree attributes
, sig
;
30258 bool is_class_method
;
30259 if (token
->type
== CPP_PLUS
)
30260 is_class_method
= true;
30262 is_class_method
= false;
30263 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30264 if (sig
== error_mark_node
)
30266 cp_parser_skip_to_end_of_block_or_statement (parser
);
30267 token
= cp_lexer_peek_token (parser
->lexer
);
30270 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30271 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30273 else if (token
->keyword
== RID_AT_PROPERTY
)
30274 cp_parser_objc_at_property_declaration (parser
);
30275 else if (token
->keyword
== RID_ATTRIBUTE
30276 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30277 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30279 "prefix attributes are ignored for methods");
30281 /* Allow for interspersed non-ObjC++ code. */
30282 cp_parser_objc_interstitial_code (parser
);
30284 token
= cp_lexer_peek_token (parser
->lexer
);
30287 if (token
->type
!= CPP_EOF
)
30288 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30290 cp_parser_error (parser
, "expected %<@end%>");
30292 objc_finish_interface ();
30295 /* Parse an Objective-C method definition list. */
30298 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30300 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30302 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30306 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30309 tree sig
, attribute
;
30310 bool is_class_method
;
30311 if (token
->type
== CPP_PLUS
)
30312 is_class_method
= true;
30314 is_class_method
= false;
30315 push_deferring_access_checks (dk_deferred
);
30316 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30317 if (sig
== error_mark_node
)
30319 cp_parser_skip_to_end_of_block_or_statement (parser
);
30320 token
= cp_lexer_peek_token (parser
->lexer
);
30323 objc_start_method_definition (is_class_method
, sig
, attribute
,
30326 /* For historical reasons, we accept an optional semicolon. */
30327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30328 cp_lexer_consume_token (parser
->lexer
);
30330 ptk
= cp_lexer_peek_token (parser
->lexer
);
30331 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30332 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30334 perform_deferred_access_checks (tf_warning_or_error
);
30335 stop_deferring_access_checks ();
30336 meth
= cp_parser_function_definition_after_declarator (parser
,
30338 pop_deferring_access_checks ();
30339 objc_finish_method_definition (meth
);
30342 /* The following case will be removed once @synthesize is
30343 completely implemented. */
30344 else if (token
->keyword
== RID_AT_PROPERTY
)
30345 cp_parser_objc_at_property_declaration (parser
);
30346 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30347 cp_parser_objc_at_synthesize_declaration (parser
);
30348 else if (token
->keyword
== RID_AT_DYNAMIC
)
30349 cp_parser_objc_at_dynamic_declaration (parser
);
30350 else if (token
->keyword
== RID_ATTRIBUTE
30351 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30352 warning_at (token
->location
, OPT_Wattributes
,
30353 "prefix attributes are ignored for methods");
30355 /* Allow for interspersed non-ObjC++ code. */
30356 cp_parser_objc_interstitial_code (parser
);
30358 token
= cp_lexer_peek_token (parser
->lexer
);
30361 if (token
->type
!= CPP_EOF
)
30362 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30364 cp_parser_error (parser
, "expected %<@end%>");
30366 objc_finish_implementation ();
30369 /* Parse Objective-C ivars. */
30372 cp_parser_objc_class_ivars (cp_parser
* parser
)
30374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30376 if (token
->type
!= CPP_OPEN_BRACE
)
30377 return; /* No ivars specified. */
30379 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30380 token
= cp_lexer_peek_token (parser
->lexer
);
30382 while (token
->type
!= CPP_CLOSE_BRACE
30383 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30385 cp_decl_specifier_seq declspecs
;
30386 int decl_class_or_enum_p
;
30387 tree prefix_attributes
;
30389 cp_parser_objc_visibility_spec (parser
);
30391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30394 cp_parser_decl_specifier_seq (parser
,
30395 CP_PARSER_FLAGS_OPTIONAL
,
30397 &decl_class_or_enum_p
);
30399 /* auto, register, static, extern, mutable. */
30400 if (declspecs
.storage_class
!= sc_none
)
30402 cp_parser_error (parser
, "invalid type for instance variable");
30403 declspecs
.storage_class
= sc_none
;
30406 /* thread_local. */
30407 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30409 cp_parser_error (parser
, "invalid type for instance variable");
30410 declspecs
.locations
[ds_thread
] = 0;
30414 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30416 cp_parser_error (parser
, "invalid type for instance variable");
30417 declspecs
.locations
[ds_typedef
] = 0;
30420 prefix_attributes
= declspecs
.attributes
;
30421 declspecs
.attributes
= NULL_TREE
;
30423 /* Keep going until we hit the `;' at the end of the
30425 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30427 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30428 cp_declarator
*declarator
= NULL
;
30429 int ctor_dtor_or_conv_p
;
30431 /* Check for a (possibly unnamed) bitfield declaration. */
30432 token
= cp_lexer_peek_token (parser
->lexer
);
30433 if (token
->type
== CPP_COLON
)
30436 if (token
->type
== CPP_NAME
30437 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30440 /* Get the name of the bitfield. */
30441 declarator
= make_id_declarator (NULL_TREE
,
30442 cp_parser_identifier (parser
),
30446 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30447 /* Get the width of the bitfield. */
30449 = cp_parser_constant_expression (parser
);
30453 /* Parse the declarator. */
30455 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30456 &ctor_dtor_or_conv_p
,
30457 /*parenthesized_p=*/NULL
,
30458 /*member_p=*/false,
30459 /*friend_p=*/false);
30462 /* Look for attributes that apply to the ivar. */
30463 attributes
= cp_parser_attributes_opt (parser
);
30464 /* Remember which attributes are prefix attributes and
30466 first_attribute
= attributes
;
30467 /* Combine the attributes. */
30468 attributes
= attr_chainon (prefix_attributes
, attributes
);
30471 /* Create the bitfield declaration. */
30472 decl
= grokbitfield (declarator
, &declspecs
,
30473 width
, NULL_TREE
, attributes
);
30475 decl
= grokfield (declarator
, &declspecs
,
30476 NULL_TREE
, /*init_const_expr_p=*/false,
30477 NULL_TREE
, attributes
);
30479 /* Add the instance variable. */
30480 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30481 objc_add_instance_variable (decl
);
30483 /* Reset PREFIX_ATTRIBUTES. */
30484 if (attributes
!= error_mark_node
)
30486 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30487 attributes
= TREE_CHAIN (attributes
);
30489 TREE_CHAIN (attributes
) = NULL_TREE
;
30492 token
= cp_lexer_peek_token (parser
->lexer
);
30494 if (token
->type
== CPP_COMMA
)
30496 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30502 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30503 token
= cp_lexer_peek_token (parser
->lexer
);
30506 if (token
->keyword
== RID_AT_END
)
30507 cp_parser_error (parser
, "expected %<}%>");
30509 /* Do not consume the RID_AT_END, so it will be read again as terminating
30510 the @interface of @implementation. */
30511 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30512 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30514 /* For historical reasons, we accept an optional semicolon. */
30515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30516 cp_lexer_consume_token (parser
->lexer
);
30519 /* Parse an Objective-C protocol declaration. */
30522 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30524 tree proto
, protorefs
;
30527 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30528 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30530 tok
= cp_lexer_peek_token (parser
->lexer
);
30531 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30532 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30536 /* See if we have a forward declaration or a definition. */
30537 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30539 /* Try a forward declaration first. */
30540 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30546 id
= cp_parser_identifier (parser
);
30547 if (id
== error_mark_node
)
30550 objc_declare_protocol (id
, attributes
);
30552 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30553 cp_lexer_consume_token (parser
->lexer
);
30557 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30560 /* Ok, we got a full-fledged definition (or at least should). */
30563 proto
= cp_parser_identifier (parser
);
30564 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30565 objc_start_protocol (proto
, protorefs
, attributes
);
30566 cp_parser_objc_method_prototype_list (parser
);
30570 /* Parse an Objective-C superclass or category. */
30573 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30576 tree
*categ
, bool *is_class_extension
)
30578 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30580 *super
= *categ
= NULL_TREE
;
30581 *is_class_extension
= false;
30582 if (next
->type
== CPP_COLON
)
30584 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30585 *super
= cp_parser_identifier (parser
);
30587 else if (next
->type
== CPP_OPEN_PAREN
)
30589 matching_parens parens
;
30590 parens
.consume_open (parser
); /* Eat '('. */
30592 /* If there is no category name, and this is an @interface, we
30593 have a class extension. */
30594 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30596 *categ
= NULL_TREE
;
30597 *is_class_extension
= true;
30600 *categ
= cp_parser_identifier (parser
);
30602 parens
.require_close (parser
);
30606 /* Parse an Objective-C class interface. */
30609 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30611 tree name
, super
, categ
, protos
;
30612 bool is_class_extension
;
30614 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30615 name
= cp_parser_identifier (parser
);
30616 if (name
== error_mark_node
)
30618 /* It's hard to recover because even if valid @interface stuff
30619 is to follow, we can't compile it (or validate it) if we
30620 don't even know which class it refers to. Let's assume this
30621 was a stray '@interface' token in the stream and skip it.
30625 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30626 &is_class_extension
);
30627 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30629 /* We have either a class or a category on our hands. */
30630 if (categ
|| is_class_extension
)
30631 objc_start_category_interface (name
, categ
, protos
, attributes
);
30634 objc_start_class_interface (name
, super
, protos
, attributes
);
30635 /* Handle instance variable declarations, if any. */
30636 cp_parser_objc_class_ivars (parser
);
30637 objc_continue_interface ();
30640 cp_parser_objc_method_prototype_list (parser
);
30643 /* Parse an Objective-C class implementation. */
30646 cp_parser_objc_class_implementation (cp_parser
* parser
)
30648 tree name
, super
, categ
;
30649 bool is_class_extension
;
30651 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30652 name
= cp_parser_identifier (parser
);
30653 if (name
== error_mark_node
)
30655 /* It's hard to recover because even if valid @implementation
30656 stuff is to follow, we can't compile it (or validate it) if
30657 we don't even know which class it refers to. Let's assume
30658 this was a stray '@implementation' token in the stream and
30663 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30664 &is_class_extension
);
30666 /* We have either a class or a category on our hands. */
30668 objc_start_category_implementation (name
, categ
);
30671 objc_start_class_implementation (name
, super
);
30672 /* Handle instance variable declarations, if any. */
30673 cp_parser_objc_class_ivars (parser
);
30674 objc_continue_implementation ();
30677 cp_parser_objc_method_definition_list (parser
);
30680 /* Consume the @end token and finish off the implementation. */
30683 cp_parser_objc_end_implementation (cp_parser
* parser
)
30685 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30686 objc_finish_implementation ();
30689 /* Parse an Objective-C declaration. */
30692 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30694 /* Try to figure out what kind of declaration is present. */
30695 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30698 switch (kwd
->keyword
)
30703 error_at (kwd
->location
, "attributes may not be specified before"
30704 " the %<@%D%> Objective-C++ keyword",
30708 case RID_AT_IMPLEMENTATION
:
30709 warning_at (kwd
->location
, OPT_Wattributes
,
30710 "prefix attributes are ignored before %<@%D%>",
30717 switch (kwd
->keyword
)
30720 cp_parser_objc_alias_declaration (parser
);
30723 cp_parser_objc_class_declaration (parser
);
30725 case RID_AT_PROTOCOL
:
30726 cp_parser_objc_protocol_declaration (parser
, attributes
);
30728 case RID_AT_INTERFACE
:
30729 cp_parser_objc_class_interface (parser
, attributes
);
30731 case RID_AT_IMPLEMENTATION
:
30732 cp_parser_objc_class_implementation (parser
);
30735 cp_parser_objc_end_implementation (parser
);
30738 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30740 cp_parser_skip_to_end_of_block_or_statement (parser
);
30744 /* Parse an Objective-C try-catch-finally statement.
30746 objc-try-catch-finally-stmt:
30747 @try compound-statement objc-catch-clause-seq [opt]
30748 objc-finally-clause [opt]
30750 objc-catch-clause-seq:
30751 objc-catch-clause objc-catch-clause-seq [opt]
30754 @catch ( objc-exception-declaration ) compound-statement
30756 objc-finally-clause:
30757 @finally compound-statement
30759 objc-exception-declaration:
30760 parameter-declaration
30763 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30767 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30768 for C. Keep them in sync. */
30771 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30773 location_t location
;
30776 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30777 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30778 objc_maybe_warn_exceptions (location
);
30779 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30780 node, lest it get absorbed into the surrounding block. */
30781 stmt
= push_stmt_list ();
30782 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30783 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30785 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30787 cp_parameter_declarator
*parm
;
30788 tree parameter_declaration
= error_mark_node
;
30789 bool seen_open_paren
= false;
30790 matching_parens parens
;
30792 cp_lexer_consume_token (parser
->lexer
);
30793 if (parens
.require_open (parser
))
30794 seen_open_paren
= true;
30795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30797 /* We have "@catch (...)" (where the '...' are literally
30798 what is in the code). Skip the '...'.
30799 parameter_declaration is set to NULL_TREE, and
30800 objc_being_catch_clauses() knows that that means
30802 cp_lexer_consume_token (parser
->lexer
);
30803 parameter_declaration
= NULL_TREE
;
30807 /* We have "@catch (NSException *exception)" or something
30808 like that. Parse the parameter declaration. */
30809 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30811 parameter_declaration
= error_mark_node
;
30813 parameter_declaration
= grokdeclarator (parm
->declarator
,
30814 &parm
->decl_specifiers
,
30815 PARM
, /*initialized=*/0,
30816 /*attrlist=*/NULL
);
30818 if (seen_open_paren
)
30819 parens
.require_close (parser
);
30822 /* If there was no open parenthesis, we are recovering from
30823 an error, and we are trying to figure out what mistake
30824 the user has made. */
30826 /* If there is an immediate closing parenthesis, the user
30827 probably forgot the opening one (ie, they typed "@catch
30828 NSException *e)". Parse the closing parenthesis and keep
30830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30831 cp_lexer_consume_token (parser
->lexer
);
30833 /* If these is no immediate closing parenthesis, the user
30834 probably doesn't know that parenthesis are required at
30835 all (ie, they typed "@catch NSException *e"). So, just
30836 forget about the closing parenthesis and keep going. */
30838 objc_begin_catch_clause (parameter_declaration
);
30839 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30840 objc_finish_catch_clause ();
30842 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30844 cp_lexer_consume_token (parser
->lexer
);
30845 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30846 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30847 node, lest it get absorbed into the surrounding block. */
30848 stmt
= push_stmt_list ();
30849 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30850 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30853 return objc_finish_try_stmt ();
30856 /* Parse an Objective-C synchronized statement.
30858 objc-synchronized-stmt:
30859 @synchronized ( expression ) compound-statement
30861 Returns NULL_TREE. */
30864 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30866 location_t location
;
30869 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30871 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30872 objc_maybe_warn_exceptions (location
);
30873 matching_parens parens
;
30874 parens
.require_open (parser
);
30875 lock
= cp_parser_expression (parser
);
30876 parens
.require_close (parser
);
30878 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30879 node, lest it get absorbed into the surrounding block. */
30880 stmt
= push_stmt_list ();
30881 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30883 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30886 /* Parse an Objective-C throw statement.
30889 @throw assignment-expression [opt] ;
30891 Returns a constructed '@throw' statement. */
30894 cp_parser_objc_throw_statement (cp_parser
*parser
)
30896 tree expr
= NULL_TREE
;
30897 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30899 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30901 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30902 expr
= cp_parser_expression (parser
);
30904 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30906 return objc_build_throw_stmt (loc
, expr
);
30909 /* Parse an Objective-C statement. */
30912 cp_parser_objc_statement (cp_parser
* parser
)
30914 /* Try to figure out what kind of declaration is present. */
30915 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30917 switch (kwd
->keyword
)
30920 return cp_parser_objc_try_catch_finally_statement (parser
);
30921 case RID_AT_SYNCHRONIZED
:
30922 return cp_parser_objc_synchronized_statement (parser
);
30924 return cp_parser_objc_throw_statement (parser
);
30926 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30928 cp_parser_skip_to_end_of_block_or_statement (parser
);
30931 return error_mark_node
;
30934 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30935 look ahead to see if an objc keyword follows the attributes. This
30936 is to detect the use of prefix attributes on ObjC @interface and
30940 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30942 cp_lexer_save_tokens (parser
->lexer
);
30943 *attrib
= cp_parser_attributes_opt (parser
);
30944 gcc_assert (*attrib
);
30945 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30947 cp_lexer_commit_tokens (parser
->lexer
);
30950 cp_lexer_rollback_tokens (parser
->lexer
);
30954 /* This routine is a minimal replacement for
30955 c_parser_struct_declaration () used when parsing the list of
30956 types/names or ObjC++ properties. For example, when parsing the
30959 @property (readonly) int a, b, c;
30961 this function is responsible for parsing "int a, int b, int c" and
30962 returning the declarations as CHAIN of DECLs.
30964 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30965 similar parsing. */
30967 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30969 tree decls
= NULL_TREE
;
30970 cp_decl_specifier_seq declspecs
;
30971 int decl_class_or_enum_p
;
30972 tree prefix_attributes
;
30974 cp_parser_decl_specifier_seq (parser
,
30975 CP_PARSER_FLAGS_NONE
,
30977 &decl_class_or_enum_p
);
30979 if (declspecs
.type
== error_mark_node
)
30980 return error_mark_node
;
30982 /* auto, register, static, extern, mutable. */
30983 if (declspecs
.storage_class
!= sc_none
)
30985 cp_parser_error (parser
, "invalid type for property");
30986 declspecs
.storage_class
= sc_none
;
30989 /* thread_local. */
30990 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30992 cp_parser_error (parser
, "invalid type for property");
30993 declspecs
.locations
[ds_thread
] = 0;
30997 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30999 cp_parser_error (parser
, "invalid type for property");
31000 declspecs
.locations
[ds_typedef
] = 0;
31003 prefix_attributes
= declspecs
.attributes
;
31004 declspecs
.attributes
= NULL_TREE
;
31006 /* Keep going until we hit the `;' at the end of the declaration. */
31007 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31009 tree attributes
, first_attribute
, decl
;
31010 cp_declarator
*declarator
;
31013 /* Parse the declarator. */
31014 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31015 NULL
, NULL
, false, false);
31017 /* Look for attributes that apply to the ivar. */
31018 attributes
= cp_parser_attributes_opt (parser
);
31019 /* Remember which attributes are prefix attributes and
31021 first_attribute
= attributes
;
31022 /* Combine the attributes. */
31023 attributes
= attr_chainon (prefix_attributes
, attributes
);
31025 decl
= grokfield (declarator
, &declspecs
,
31026 NULL_TREE
, /*init_const_expr_p=*/false,
31027 NULL_TREE
, attributes
);
31029 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31030 return error_mark_node
;
31032 /* Reset PREFIX_ATTRIBUTES. */
31033 if (attributes
!= error_mark_node
)
31035 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31036 attributes
= TREE_CHAIN (attributes
);
31038 TREE_CHAIN (attributes
) = NULL_TREE
;
31041 DECL_CHAIN (decl
) = decls
;
31044 token
= cp_lexer_peek_token (parser
->lexer
);
31045 if (token
->type
== CPP_COMMA
)
31047 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31056 /* Parse an Objective-C @property declaration. The syntax is:
31058 objc-property-declaration:
31059 '@property' objc-property-attributes[opt] struct-declaration ;
31061 objc-property-attributes:
31062 '(' objc-property-attribute-list ')'
31064 objc-property-attribute-list:
31065 objc-property-attribute
31066 objc-property-attribute-list, objc-property-attribute
31068 objc-property-attribute
31069 'getter' = identifier
31070 'setter' = identifier
31079 @property NSString *name;
31080 @property (readonly) id object;
31081 @property (retain, nonatomic, getter=getTheName) id name;
31082 @property int a, b, c;
31084 PS: This function is identical to
31085 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31087 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31089 /* The following variables hold the attributes of the properties as
31090 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31091 seen. When we see an attribute, we set them to 'true' (if they
31092 are boolean properties) or to the identifier (if they have an
31093 argument, ie, for getter and setter). Note that here we only
31094 parse the list of attributes, check the syntax and accumulate the
31095 attributes that we find. objc_add_property_declaration() will
31096 then process the information. */
31097 bool property_assign
= false;
31098 bool property_copy
= false;
31099 tree property_getter_ident
= NULL_TREE
;
31100 bool property_nonatomic
= false;
31101 bool property_readonly
= false;
31102 bool property_readwrite
= false;
31103 bool property_retain
= false;
31104 tree property_setter_ident
= NULL_TREE
;
31106 /* 'properties' is the list of properties that we read. Usually a
31107 single one, but maybe more (eg, in "@property int a, b, c;" there
31112 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31114 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31116 /* Parse the optional attribute list... */
31117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31120 matching_parens parens
;
31121 parens
.consume_open (parser
);
31125 bool syntax_error
= false;
31126 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31129 if (token
->type
!= CPP_NAME
)
31131 cp_parser_error (parser
, "expected identifier");
31134 keyword
= C_RID_CODE (token
->u
.value
);
31135 cp_lexer_consume_token (parser
->lexer
);
31138 case RID_ASSIGN
: property_assign
= true; break;
31139 case RID_COPY
: property_copy
= true; break;
31140 case RID_NONATOMIC
: property_nonatomic
= true; break;
31141 case RID_READONLY
: property_readonly
= true; break;
31142 case RID_READWRITE
: property_readwrite
= true; break;
31143 case RID_RETAIN
: property_retain
= true; break;
31147 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31149 if (keyword
== RID_GETTER
)
31150 cp_parser_error (parser
,
31151 "missing %<=%> (after %<getter%> attribute)");
31153 cp_parser_error (parser
,
31154 "missing %<=%> (after %<setter%> attribute)");
31155 syntax_error
= true;
31158 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31159 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31161 cp_parser_error (parser
, "expected identifier");
31162 syntax_error
= true;
31165 if (keyword
== RID_SETTER
)
31167 if (property_setter_ident
!= NULL_TREE
)
31169 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31170 cp_lexer_consume_token (parser
->lexer
);
31173 property_setter_ident
= cp_parser_objc_selector (parser
);
31174 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31175 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31177 cp_lexer_consume_token (parser
->lexer
);
31181 if (property_getter_ident
!= NULL_TREE
)
31183 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31184 cp_lexer_consume_token (parser
->lexer
);
31187 property_getter_ident
= cp_parser_objc_selector (parser
);
31191 cp_parser_error (parser
, "unknown property attribute");
31192 syntax_error
= true;
31199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31200 cp_lexer_consume_token (parser
->lexer
);
31205 /* FIXME: "@property (setter, assign);" will generate a spurious
31206 "error: expected ‘)’ before ‘,’ token". This is because
31207 cp_parser_require, unlike the C counterpart, will produce an
31208 error even if we are in error recovery. */
31209 if (!parens
.require_close (parser
))
31211 cp_parser_skip_to_closing_parenthesis (parser
,
31212 /*recovering=*/true,
31213 /*or_comma=*/false,
31214 /*consume_paren=*/true);
31218 /* ... and the property declaration(s). */
31219 properties
= cp_parser_objc_struct_declaration (parser
);
31221 if (properties
== error_mark_node
)
31223 cp_parser_skip_to_end_of_statement (parser
);
31224 /* If the next token is now a `;', consume it. */
31225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31226 cp_lexer_consume_token (parser
->lexer
);
31230 if (properties
== NULL_TREE
)
31231 cp_parser_error (parser
, "expected identifier");
31234 /* Comma-separated properties are chained together in
31235 reverse order; add them one by one. */
31236 properties
= nreverse (properties
);
31238 for (; properties
; properties
= TREE_CHAIN (properties
))
31239 objc_add_property_declaration (loc
, copy_node (properties
),
31240 property_readonly
, property_readwrite
,
31241 property_assign
, property_retain
,
31242 property_copy
, property_nonatomic
,
31243 property_getter_ident
, property_setter_ident
);
31246 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31249 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31251 objc-synthesize-declaration:
31252 @synthesize objc-synthesize-identifier-list ;
31254 objc-synthesize-identifier-list:
31255 objc-synthesize-identifier
31256 objc-synthesize-identifier-list, objc-synthesize-identifier
31258 objc-synthesize-identifier
31260 identifier = identifier
31263 @synthesize MyProperty;
31264 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31266 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31267 for C. Keep them in sync.
31270 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31272 tree list
= NULL_TREE
;
31274 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31276 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31279 tree property
, ivar
;
31280 property
= cp_parser_identifier (parser
);
31281 if (property
== error_mark_node
)
31283 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31288 cp_lexer_consume_token (parser
->lexer
);
31289 ivar
= cp_parser_identifier (parser
);
31290 if (ivar
== error_mark_node
)
31292 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31298 list
= chainon (list
, build_tree_list (ivar
, property
));
31299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31300 cp_lexer_consume_token (parser
->lexer
);
31304 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31305 objc_add_synthesize_declaration (loc
, list
);
31308 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31310 objc-dynamic-declaration:
31311 @dynamic identifier-list ;
31314 @dynamic MyProperty;
31315 @dynamic MyProperty, AnotherProperty;
31317 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31318 for C. Keep them in sync.
31321 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31323 tree list
= NULL_TREE
;
31325 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31327 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31331 property
= cp_parser_identifier (parser
);
31332 if (property
== error_mark_node
)
31334 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31337 list
= chainon (list
, build_tree_list (NULL
, property
));
31338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31339 cp_lexer_consume_token (parser
->lexer
);
31343 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31344 objc_add_dynamic_declaration (loc
, list
);
31348 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31350 /* Returns name of the next clause.
31351 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31352 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31353 returned and the token is consumed. */
31355 static pragma_omp_clause
31356 cp_parser_omp_clause_name (cp_parser
*parser
)
31358 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31360 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31361 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31362 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31363 result
= PRAGMA_OMP_CLAUSE_IF
;
31364 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31365 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31366 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31367 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31368 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31369 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31370 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31371 result
= PRAGMA_OMP_CLAUSE_FOR
;
31372 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31374 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31375 const char *p
= IDENTIFIER_POINTER (id
);
31380 if (!strcmp ("aligned", p
))
31381 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31382 else if (!strcmp ("async", p
))
31383 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31386 if (!strcmp ("collapse", p
))
31387 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31388 else if (!strcmp ("copy", p
))
31389 result
= PRAGMA_OACC_CLAUSE_COPY
;
31390 else if (!strcmp ("copyin", p
))
31391 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31392 else if (!strcmp ("copyout", p
))
31393 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31394 else if (!strcmp ("copyprivate", p
))
31395 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31396 else if (!strcmp ("create", p
))
31397 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31400 if (!strcmp ("defaultmap", p
))
31401 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31402 else if (!strcmp ("depend", p
))
31403 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31404 else if (!strcmp ("device", p
))
31405 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31406 else if (!strcmp ("deviceptr", p
))
31407 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31408 else if (!strcmp ("device_resident", p
))
31409 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31410 else if (!strcmp ("dist_schedule", p
))
31411 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31414 if (!strcmp ("final", p
))
31415 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31416 else if (!strcmp ("finalize", p
))
31417 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31418 else if (!strcmp ("firstprivate", p
))
31419 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31420 else if (!strcmp ("from", p
))
31421 result
= PRAGMA_OMP_CLAUSE_FROM
;
31424 if (!strcmp ("gang", p
))
31425 result
= PRAGMA_OACC_CLAUSE_GANG
;
31426 else if (!strcmp ("grainsize", p
))
31427 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31430 if (!strcmp ("hint", p
))
31431 result
= PRAGMA_OMP_CLAUSE_HINT
;
31432 else if (!strcmp ("host", p
))
31433 result
= PRAGMA_OACC_CLAUSE_HOST
;
31436 if (!strcmp ("if_present", p
))
31437 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31438 else if (!strcmp ("inbranch", p
))
31439 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31440 else if (!strcmp ("independent", p
))
31441 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31442 else if (!strcmp ("is_device_ptr", p
))
31443 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31446 if (!strcmp ("lastprivate", p
))
31447 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31448 else if (!strcmp ("linear", p
))
31449 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31450 else if (!strcmp ("link", p
))
31451 result
= PRAGMA_OMP_CLAUSE_LINK
;
31454 if (!strcmp ("map", p
))
31455 result
= PRAGMA_OMP_CLAUSE_MAP
;
31456 else if (!strcmp ("mergeable", p
))
31457 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31460 if (!strcmp ("nogroup", p
))
31461 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31462 else if (!strcmp ("notinbranch", p
))
31463 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31464 else if (!strcmp ("nowait", p
))
31465 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31466 else if (!strcmp ("num_gangs", p
))
31467 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31468 else if (!strcmp ("num_tasks", p
))
31469 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31470 else if (!strcmp ("num_teams", p
))
31471 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31472 else if (!strcmp ("num_threads", p
))
31473 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31474 else if (!strcmp ("num_workers", p
))
31475 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31478 if (!strcmp ("ordered", p
))
31479 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31482 if (!strcmp ("parallel", p
))
31483 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31484 else if (!strcmp ("present", p
))
31485 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31486 else if (!strcmp ("present_or_copy", p
)
31487 || !strcmp ("pcopy", p
))
31488 result
= PRAGMA_OACC_CLAUSE_COPY
;
31489 else if (!strcmp ("present_or_copyin", p
)
31490 || !strcmp ("pcopyin", p
))
31491 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31492 else if (!strcmp ("present_or_copyout", p
)
31493 || !strcmp ("pcopyout", p
))
31494 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31495 else if (!strcmp ("present_or_create", p
)
31496 || !strcmp ("pcreate", p
))
31497 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31498 else if (!strcmp ("priority", p
))
31499 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31500 else if (!strcmp ("proc_bind", p
))
31501 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31504 if (!strcmp ("reduction", p
))
31505 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31508 if (!strcmp ("safelen", p
))
31509 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31510 else if (!strcmp ("schedule", p
))
31511 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31512 else if (!strcmp ("sections", p
))
31513 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31514 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31515 result
= PRAGMA_OACC_CLAUSE_HOST
;
31516 else if (!strcmp ("seq", p
))
31517 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31518 else if (!strcmp ("shared", p
))
31519 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31520 else if (!strcmp ("simd", p
))
31521 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31522 else if (!strcmp ("simdlen", p
))
31523 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31526 if (!strcmp ("taskgroup", p
))
31527 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31528 else if (!strcmp ("thread_limit", p
))
31529 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31530 else if (!strcmp ("threads", p
))
31531 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31532 else if (!strcmp ("tile", p
))
31533 result
= PRAGMA_OACC_CLAUSE_TILE
;
31534 else if (!strcmp ("to", p
))
31535 result
= PRAGMA_OMP_CLAUSE_TO
;
31538 if (!strcmp ("uniform", p
))
31539 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31540 else if (!strcmp ("untied", p
))
31541 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31542 else if (!strcmp ("use_device", p
))
31543 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31544 else if (!strcmp ("use_device_ptr", p
))
31545 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31548 if (!strcmp ("vector", p
))
31549 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31550 else if (!strcmp ("vector_length", p
))
31551 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31554 if (!strcmp ("wait", p
))
31555 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31556 else if (!strcmp ("worker", p
))
31557 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31562 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31563 cp_lexer_consume_token (parser
->lexer
);
31568 /* Validate that a clause of the given type does not already exist. */
31571 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31572 const char *name
, location_t location
)
31576 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31577 if (OMP_CLAUSE_CODE (c
) == code
)
31579 error_at (location
, "too many %qs clauses", name
);
31587 variable-list , identifier
31589 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31590 colon). An opening parenthesis will have been consumed by the caller.
31592 If KIND is nonzero, create the appropriate node and install the decl
31593 in OMP_CLAUSE_DECL and add the node to the head of the list.
31595 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31596 return the list created.
31598 COLON can be NULL if only closing parenthesis should end the list,
31599 or pointer to bool which will receive false if the list is terminated
31600 by closing parenthesis or true if the list is terminated by colon. */
31603 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31604 tree list
, bool *colon
)
31607 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31610 parser
->colon_corrects_to_scope_p
= false;
31617 token
= cp_lexer_peek_token (parser
->lexer
);
31619 && current_class_ptr
31620 && cp_parser_is_keyword (token
, RID_THIS
))
31622 decl
= finish_this_expr ();
31623 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31624 || CONVERT_EXPR_P (decl
))
31625 decl
= TREE_OPERAND (decl
, 0);
31626 cp_lexer_consume_token (parser
->lexer
);
31630 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31631 /*check_dependency_p=*/true,
31632 /*template_p=*/NULL
,
31633 /*declarator_p=*/false,
31634 /*optional_p=*/false);
31635 if (name
== error_mark_node
)
31638 if (identifier_p (name
))
31639 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31642 if (decl
== error_mark_node
)
31643 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31646 if (decl
== error_mark_node
)
31648 else if (kind
!= 0)
31652 case OMP_CLAUSE__CACHE_
:
31653 /* The OpenACC cache directive explicitly only allows "array
31654 elements or subarrays". */
31655 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31657 error_at (token
->location
, "expected %<[%>");
31658 decl
= error_mark_node
;
31662 case OMP_CLAUSE_MAP
:
31663 case OMP_CLAUSE_FROM
:
31664 case OMP_CLAUSE_TO
:
31665 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31668 = cp_lexer_peek_token (parser
->lexer
)->location
;
31669 cp_id_kind idk
= CP_ID_KIND_NONE
;
31670 cp_lexer_consume_token (parser
->lexer
);
31671 decl
= convert_from_reference (decl
);
31673 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31678 case OMP_CLAUSE_DEPEND
:
31679 case OMP_CLAUSE_REDUCTION
:
31680 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31682 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31684 parser
->colon_corrects_to_scope_p
= false;
31685 cp_lexer_consume_token (parser
->lexer
);
31686 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31687 low_bound
= cp_parser_expression (parser
);
31689 parser
->colon_corrects_to_scope_p
31690 = saved_colon_corrects_to_scope_p
;
31691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31692 length
= integer_one_node
;
31695 /* Look for `:'. */
31696 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31698 if (!cp_lexer_next_token_is (parser
->lexer
,
31700 length
= cp_parser_expression (parser
);
31702 /* Look for the closing `]'. */
31703 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31707 decl
= tree_cons (low_bound
, length
, decl
);
31714 tree u
= build_omp_clause (token
->location
, kind
);
31715 OMP_CLAUSE_DECL (u
) = decl
;
31716 OMP_CLAUSE_CHAIN (u
) = list
;
31720 list
= tree_cons (decl
, NULL_TREE
, list
);
31723 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31725 cp_lexer_consume_token (parser
->lexer
);
31729 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31731 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31734 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31738 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31742 /* Try to resync to an unnested comma. Copied from
31743 cp_parser_parenthesized_expression_list. */
31746 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31747 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31748 /*recovering=*/true,
31750 /*consume_paren=*/true);
31758 /* Similarly, but expect leading and trailing parenthesis. This is a very
31759 common case for omp clauses. */
31762 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31764 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31765 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31770 copy ( variable-list )
31771 copyin ( variable-list )
31772 copyout ( variable-list )
31773 create ( variable-list )
31774 delete ( variable-list )
31775 present ( variable-list ) */
31778 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31781 enum gomp_map_kind kind
;
31784 case PRAGMA_OACC_CLAUSE_COPY
:
31785 kind
= GOMP_MAP_TOFROM
;
31787 case PRAGMA_OACC_CLAUSE_COPYIN
:
31788 kind
= GOMP_MAP_TO
;
31790 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31791 kind
= GOMP_MAP_FROM
;
31793 case PRAGMA_OACC_CLAUSE_CREATE
:
31794 kind
= GOMP_MAP_ALLOC
;
31796 case PRAGMA_OACC_CLAUSE_DELETE
:
31797 kind
= GOMP_MAP_RELEASE
;
31799 case PRAGMA_OACC_CLAUSE_DEVICE
:
31800 kind
= GOMP_MAP_FORCE_TO
;
31802 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31803 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31805 case PRAGMA_OACC_CLAUSE_HOST
:
31806 kind
= GOMP_MAP_FORCE_FROM
;
31808 case PRAGMA_OACC_CLAUSE_LINK
:
31809 kind
= GOMP_MAP_LINK
;
31811 case PRAGMA_OACC_CLAUSE_PRESENT
:
31812 kind
= GOMP_MAP_FORCE_PRESENT
;
31815 gcc_unreachable ();
31818 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31820 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31821 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31827 deviceptr ( variable-list ) */
31830 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31832 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31835 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31836 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31837 variable-list must only allow for pointer variables. */
31838 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31839 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31841 tree v
= TREE_PURPOSE (t
);
31842 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31843 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31844 OMP_CLAUSE_DECL (u
) = v
;
31845 OMP_CLAUSE_CHAIN (u
) = list
;
31860 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31861 enum omp_clause_code code
,
31862 tree list
, location_t location
)
31864 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31865 tree c
= build_omp_clause (location
, code
);
31866 OMP_CLAUSE_CHAIN (c
) = list
;
31871 num_gangs ( expression )
31872 num_workers ( expression )
31873 vector_length ( expression ) */
31876 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31877 const char *str
, tree list
)
31879 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31881 matching_parens parens
;
31882 if (!parens
.require_open (parser
))
31885 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31887 if (t
== error_mark_node
31888 || !parens
.require_close (parser
))
31890 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31891 /*or_comma=*/false,
31892 /*consume_paren=*/true);
31896 check_no_duplicate_clause (list
, code
, str
, loc
);
31898 tree c
= build_omp_clause (loc
, code
);
31899 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31900 OMP_CLAUSE_CHAIN (c
) = list
;
31906 gang [( gang-arg-list )]
31907 worker [( [num:] int-expr )]
31908 vector [( [length:] int-expr )]
31910 where gang-arg is one of:
31915 and size-expr may be:
31922 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31923 const char *str
, tree list
)
31925 const char *id
= "num";
31926 cp_lexer
*lexer
= parser
->lexer
;
31927 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31928 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31930 if (kind
== OMP_CLAUSE_VECTOR
)
31933 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31935 matching_parens parens
;
31936 parens
.consume_open (parser
);
31940 cp_token
*next
= cp_lexer_peek_token (lexer
);
31943 /* Gang static argument. */
31944 if (kind
== OMP_CLAUSE_GANG
31945 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31947 cp_lexer_consume_token (lexer
);
31949 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31950 goto cleanup_error
;
31953 if (ops
[idx
] != NULL
)
31955 cp_parser_error (parser
, "too many %<static%> arguments");
31956 goto cleanup_error
;
31959 /* Check for the '*' argument. */
31960 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31961 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31962 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31965 cp_lexer_consume_token (lexer
);
31966 ops
[idx
] = integer_minus_one_node
;
31968 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31970 cp_lexer_consume_token (lexer
);
31976 /* Worker num: argument and vector length: arguments. */
31977 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31978 && id_equal (next
->u
.value
, id
)
31979 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31981 cp_lexer_consume_token (lexer
); /* id */
31982 cp_lexer_consume_token (lexer
); /* ':' */
31985 /* Now collect the actual argument. */
31986 if (ops
[idx
] != NULL_TREE
)
31988 cp_parser_error (parser
, "unexpected argument");
31989 goto cleanup_error
;
31992 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31994 if (expr
== error_mark_node
)
31995 goto cleanup_error
;
31997 mark_exp_read (expr
);
32000 if (kind
== OMP_CLAUSE_GANG
32001 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32003 cp_lexer_consume_token (lexer
);
32010 if (!parens
.require_close (parser
))
32011 goto cleanup_error
;
32014 check_no_duplicate_clause (list
, kind
, str
, loc
);
32016 c
= build_omp_clause (loc
, kind
);
32019 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32021 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32022 OMP_CLAUSE_CHAIN (c
) = list
;
32027 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32032 tile ( size-expr-list ) */
32035 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32037 tree c
, expr
= error_mark_node
;
32038 tree tile
= NULL_TREE
;
32040 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32041 so, but the spec authors never considered such a case and have
32042 differing opinions on what it might mean, including 'not
32044 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32045 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32048 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32053 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32057 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32058 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32060 cp_lexer_consume_token (parser
->lexer
);
32061 expr
= integer_zero_node
;
32064 expr
= cp_parser_constant_expression (parser
);
32066 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32068 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32070 /* Consume the trailing ')'. */
32071 cp_lexer_consume_token (parser
->lexer
);
32073 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32074 tile
= nreverse (tile
);
32075 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32076 OMP_CLAUSE_CHAIN (c
) = list
;
32081 Parse wait clause or directive parameters. */
32084 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32086 vec
<tree
, va_gc
> *args
;
32089 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32091 /*allow_expansion_p=*/true,
32092 /*non_constant_p=*/NULL
);
32094 if (args
== NULL
|| args
->length () == 0)
32096 cp_parser_error (parser
, "expected integer expression before ')'");
32098 release_tree_vector (args
);
32102 args_tree
= build_tree_list_vec (args
);
32104 release_tree_vector (args
);
32106 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32108 tree targ
= TREE_VALUE (t
);
32110 if (targ
!= error_mark_node
)
32112 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32113 error ("%<wait%> expression must be integral");
32116 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32118 targ
= mark_rvalue_use (targ
);
32119 OMP_CLAUSE_DECL (c
) = targ
;
32120 OMP_CLAUSE_CHAIN (c
) = list
;
32130 wait ( int-expr-list ) */
32133 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32135 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32137 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32140 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32146 collapse ( constant-expression ) */
32149 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32155 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32156 matching_parens parens
;
32157 if (!parens
.require_open (parser
))
32160 num
= cp_parser_constant_expression (parser
);
32162 if (!parens
.require_close (parser
))
32163 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32164 /*or_comma=*/false,
32165 /*consume_paren=*/true);
32167 if (num
== error_mark_node
)
32169 num
= fold_non_dependent_expr (num
);
32170 if (!tree_fits_shwi_p (num
)
32171 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32172 || (n
= tree_to_shwi (num
)) <= 0
32175 error_at (loc
, "collapse argument needs positive constant integer expression");
32179 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32180 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32181 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32182 OMP_CLAUSE_CHAIN (c
) = list
;
32183 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32189 default ( none | shared )
32192 default ( none | present ) */
32195 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32196 location_t location
, bool is_oacc
)
32198 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32201 matching_parens parens
;
32202 if (!parens
.require_open (parser
))
32204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32206 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32207 const char *p
= IDENTIFIER_POINTER (id
);
32212 if (strcmp ("none", p
) != 0)
32214 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32218 if (strcmp ("present", p
) != 0 || !is_oacc
)
32220 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32224 if (strcmp ("shared", p
) != 0 || is_oacc
)
32226 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32233 cp_lexer_consume_token (parser
->lexer
);
32239 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32241 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32244 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32245 || !parens
.require_close (parser
))
32246 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32247 /*or_comma=*/false,
32248 /*consume_paren=*/true);
32250 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32253 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32254 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32255 OMP_CLAUSE_CHAIN (c
) = list
;
32256 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32262 final ( expression ) */
32265 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32269 matching_parens parens
;
32270 if (!parens
.require_open (parser
))
32273 t
= cp_parser_condition (parser
);
32275 if (t
== error_mark_node
32276 || !parens
.require_close (parser
))
32277 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32278 /*or_comma=*/false,
32279 /*consume_paren=*/true);
32281 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32283 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32284 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32285 OMP_CLAUSE_CHAIN (c
) = list
;
32294 if ( directive-name-modifier : expression )
32296 directive-name-modifier:
32297 parallel | task | taskloop | target data | target | target update
32298 | target enter data | target exit data */
32301 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32305 enum tree_code if_modifier
= ERROR_MARK
;
32307 matching_parens parens
;
32308 if (!parens
.require_open (parser
))
32311 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32313 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32314 const char *p
= IDENTIFIER_POINTER (id
);
32317 if (strcmp ("parallel", p
) == 0)
32318 if_modifier
= OMP_PARALLEL
;
32319 else if (strcmp ("task", p
) == 0)
32320 if_modifier
= OMP_TASK
;
32321 else if (strcmp ("taskloop", p
) == 0)
32322 if_modifier
= OMP_TASKLOOP
;
32323 else if (strcmp ("target", p
) == 0)
32325 if_modifier
= OMP_TARGET
;
32326 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32328 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32329 p
= IDENTIFIER_POINTER (id
);
32330 if (strcmp ("data", p
) == 0)
32331 if_modifier
= OMP_TARGET_DATA
;
32332 else if (strcmp ("update", p
) == 0)
32333 if_modifier
= OMP_TARGET_UPDATE
;
32334 else if (strcmp ("enter", p
) == 0)
32335 if_modifier
= OMP_TARGET_ENTER_DATA
;
32336 else if (strcmp ("exit", p
) == 0)
32337 if_modifier
= OMP_TARGET_EXIT_DATA
;
32338 if (if_modifier
!= OMP_TARGET
)
32343 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32344 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32346 if_modifier
= ERROR_MARK
;
32348 if (if_modifier
== OMP_TARGET_ENTER_DATA
32349 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32351 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32353 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32354 p
= IDENTIFIER_POINTER (id
);
32355 if (strcmp ("data", p
) == 0)
32361 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32362 error_at (loc
, "expected %<data%>");
32363 if_modifier
= ERROR_MARK
;
32368 if (if_modifier
!= ERROR_MARK
)
32370 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32373 cp_lexer_consume_token (parser
->lexer
);
32380 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32381 error_at (loc
, "expected %<:%>");
32383 if_modifier
= ERROR_MARK
;
32388 t
= cp_parser_condition (parser
);
32390 if (t
== error_mark_node
32391 || !parens
.require_close (parser
))
32392 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32393 /*or_comma=*/false,
32394 /*consume_paren=*/true);
32396 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32397 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32399 if (if_modifier
!= ERROR_MARK
32400 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32402 const char *p
= NULL
;
32403 switch (if_modifier
)
32405 case OMP_PARALLEL
: p
= "parallel"; break;
32406 case OMP_TASK
: p
= "task"; break;
32407 case OMP_TASKLOOP
: p
= "taskloop"; break;
32408 case OMP_TARGET_DATA
: p
= "target data"; break;
32409 case OMP_TARGET
: p
= "target"; break;
32410 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32411 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32412 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32413 default: gcc_unreachable ();
32415 error_at (location
, "too many %<if%> clauses with %qs modifier",
32419 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32422 error_at (location
, "too many %<if%> clauses");
32424 error_at (location
, "too many %<if%> clauses without modifier");
32427 else if (if_modifier
== ERROR_MARK
32428 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32430 error_at (location
, "if any %<if%> clause has modifier, then all "
32431 "%<if%> clauses have to use modifier");
32436 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32437 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32438 OMP_CLAUSE_IF_EXPR (c
) = t
;
32439 OMP_CLAUSE_CHAIN (c
) = list
;
32448 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32449 tree list
, location_t location
)
32453 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32456 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32457 OMP_CLAUSE_CHAIN (c
) = list
;
32465 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32466 tree list
, location_t location
)
32470 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32472 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32473 OMP_CLAUSE_CHAIN (c
) = list
;
32478 num_threads ( expression ) */
32481 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32482 location_t location
)
32486 matching_parens parens
;
32487 if (!parens
.require_open (parser
))
32490 t
= cp_parser_expression (parser
);
32492 if (t
== error_mark_node
32493 || !parens
.require_close (parser
))
32494 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32495 /*or_comma=*/false,
32496 /*consume_paren=*/true);
32498 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32499 "num_threads", location
);
32501 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32502 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32503 OMP_CLAUSE_CHAIN (c
) = list
;
32509 num_tasks ( expression ) */
32512 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32513 location_t location
)
32517 matching_parens parens
;
32518 if (!parens
.require_open (parser
))
32521 t
= cp_parser_expression (parser
);
32523 if (t
== error_mark_node
32524 || !parens
.require_close (parser
))
32525 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32526 /*or_comma=*/false,
32527 /*consume_paren=*/true);
32529 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32530 "num_tasks", location
);
32532 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32533 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32534 OMP_CLAUSE_CHAIN (c
) = list
;
32540 grainsize ( expression ) */
32543 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32544 location_t location
)
32548 matching_parens parens
;
32549 if (!parens
.require_open (parser
))
32552 t
= cp_parser_expression (parser
);
32554 if (t
== error_mark_node
32555 || !parens
.require_close (parser
))
32556 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32557 /*or_comma=*/false,
32558 /*consume_paren=*/true);
32560 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32561 "grainsize", location
);
32563 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32564 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32565 OMP_CLAUSE_CHAIN (c
) = list
;
32571 priority ( expression ) */
32574 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32575 location_t location
)
32579 matching_parens parens
;
32580 if (!parens
.require_open (parser
))
32583 t
= cp_parser_expression (parser
);
32585 if (t
== error_mark_node
32586 || !parens
.require_close (parser
))
32587 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32588 /*or_comma=*/false,
32589 /*consume_paren=*/true);
32591 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32592 "priority", location
);
32594 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32595 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32596 OMP_CLAUSE_CHAIN (c
) = list
;
32602 hint ( expression ) */
32605 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32606 location_t location
)
32610 matching_parens parens
;
32611 if (!parens
.require_open (parser
))
32614 t
= cp_parser_expression (parser
);
32616 if (t
== error_mark_node
32617 || !parens
.require_close (parser
))
32618 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32619 /*or_comma=*/false,
32620 /*consume_paren=*/true);
32622 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32624 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32625 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32626 OMP_CLAUSE_CHAIN (c
) = list
;
32632 defaultmap ( tofrom : scalar ) */
32635 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32636 location_t location
)
32641 matching_parens parens
;
32642 if (!parens
.require_open (parser
))
32645 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32647 cp_parser_error (parser
, "expected %<tofrom%>");
32650 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32651 p
= IDENTIFIER_POINTER (id
);
32652 if (strcmp (p
, "tofrom") != 0)
32654 cp_parser_error (parser
, "expected %<tofrom%>");
32657 cp_lexer_consume_token (parser
->lexer
);
32658 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32661 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32663 cp_parser_error (parser
, "expected %<scalar%>");
32666 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32667 p
= IDENTIFIER_POINTER (id
);
32668 if (strcmp (p
, "scalar") != 0)
32670 cp_parser_error (parser
, "expected %<scalar%>");
32673 cp_lexer_consume_token (parser
->lexer
);
32674 if (!parens
.require_close (parser
))
32677 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32680 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32681 OMP_CLAUSE_CHAIN (c
) = list
;
32685 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32686 /*or_comma=*/false,
32687 /*consume_paren=*/true);
32695 ordered ( constant-expression ) */
32698 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32699 tree list
, location_t location
)
32701 tree c
, num
= NULL_TREE
;
32704 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32705 "ordered", location
);
32707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32709 matching_parens parens
;
32710 parens
.consume_open (parser
);
32712 num
= cp_parser_constant_expression (parser
);
32714 if (!parens
.require_close (parser
))
32715 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32716 /*or_comma=*/false,
32717 /*consume_paren=*/true);
32719 if (num
== error_mark_node
)
32721 num
= fold_non_dependent_expr (num
);
32722 if (!tree_fits_shwi_p (num
)
32723 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32724 || (n
= tree_to_shwi (num
)) <= 0
32727 error_at (location
,
32728 "ordered argument needs positive constant integer "
32734 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32735 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32736 OMP_CLAUSE_CHAIN (c
) = list
;
32741 reduction ( reduction-operator : variable-list )
32743 reduction-operator:
32744 One of: + * - & ^ | && ||
32748 reduction-operator:
32749 One of: + * - & ^ | && || min max
32753 reduction-operator:
32754 One of: + * - & ^ | && ||
32758 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32760 enum tree_code code
= ERROR_MARK
;
32761 tree nlist
, c
, id
= NULL_TREE
;
32763 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32766 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32768 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32769 case CPP_MULT
: code
= MULT_EXPR
; break;
32770 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32771 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32772 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32773 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32774 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32775 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32779 if (code
!= ERROR_MARK
)
32780 cp_lexer_consume_token (parser
->lexer
);
32783 bool saved_colon_corrects_to_scope_p
;
32784 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32785 parser
->colon_corrects_to_scope_p
= false;
32786 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32787 /*check_dependency_p=*/true,
32788 /*template_p=*/NULL
,
32789 /*declarator_p=*/false,
32790 /*optional_p=*/false);
32791 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32792 if (identifier_p (id
))
32794 const char *p
= IDENTIFIER_POINTER (id
);
32796 if (strcmp (p
, "min") == 0)
32798 else if (strcmp (p
, "max") == 0)
32800 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32802 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32804 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32806 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32807 code
= BIT_AND_EXPR
;
32808 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32809 code
= BIT_IOR_EXPR
;
32810 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32811 code
= BIT_XOR_EXPR
;
32812 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32813 code
= TRUTH_ANDIF_EXPR
;
32814 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32815 code
= TRUTH_ORIF_EXPR
;
32816 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32817 tree scope
= parser
->scope
;
32819 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32820 parser
->scope
= NULL_TREE
;
32821 parser
->qualifying_scope
= NULL_TREE
;
32822 parser
->object_scope
= NULL_TREE
;
32826 error ("invalid reduction-identifier");
32828 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32829 /*or_comma=*/false,
32830 /*consume_paren=*/true);
32835 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32838 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32840 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32842 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32843 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32850 schedule ( schedule-kind )
32851 schedule ( schedule-kind , expression )
32854 static | dynamic | guided | runtime | auto
32857 schedule ( schedule-modifier : schedule-kind )
32858 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32866 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32869 int modifiers
= 0, nmodifiers
= 0;
32871 matching_parens parens
;
32872 if (!parens
.require_open (parser
))
32875 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32877 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32879 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32880 const char *p
= IDENTIFIER_POINTER (id
);
32881 if (strcmp ("simd", p
) == 0)
32882 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32883 else if (strcmp ("monotonic", p
) == 0)
32884 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32885 else if (strcmp ("nonmonotonic", p
) == 0)
32886 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32889 cp_lexer_consume_token (parser
->lexer
);
32890 if (nmodifiers
++ == 0
32891 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32892 cp_lexer_consume_token (parser
->lexer
);
32895 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32902 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32903 const char *p
= IDENTIFIER_POINTER (id
);
32908 if (strcmp ("dynamic", p
) != 0)
32910 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32914 if (strcmp ("guided", p
) != 0)
32916 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32920 if (strcmp ("runtime", p
) != 0)
32922 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32929 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32930 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32931 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32932 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32935 cp_lexer_consume_token (parser
->lexer
);
32937 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32938 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32939 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32940 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32942 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32950 cp_lexer_consume_token (parser
->lexer
);
32952 token
= cp_lexer_peek_token (parser
->lexer
);
32953 t
= cp_parser_assignment_expression (parser
);
32955 if (t
== error_mark_node
)
32957 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32958 error_at (token
->location
, "schedule %<runtime%> does not take "
32959 "a %<chunk_size%> parameter");
32960 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32961 error_at (token
->location
, "schedule %<auto%> does not take "
32962 "a %<chunk_size%> parameter");
32964 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32966 if (!parens
.require_close (parser
))
32969 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32972 OMP_CLAUSE_SCHEDULE_KIND (c
)
32973 = (enum omp_clause_schedule_kind
)
32974 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32976 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32977 OMP_CLAUSE_CHAIN (c
) = list
;
32981 cp_parser_error (parser
, "invalid schedule kind");
32983 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32984 /*or_comma=*/false,
32985 /*consume_paren=*/true);
32993 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32994 tree list
, location_t location
)
32998 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33000 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33001 OMP_CLAUSE_CHAIN (c
) = list
;
33010 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33011 tree list
, location_t location
)
33013 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33014 tree c
= build_omp_clause (location
, code
);
33015 OMP_CLAUSE_CHAIN (c
) = list
;
33026 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33027 enum omp_clause_code code
,
33028 tree list
, location_t location
)
33030 tree c
= build_omp_clause (location
, code
);
33031 OMP_CLAUSE_CHAIN (c
) = list
;
33039 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33040 tree list
, location_t location
)
33042 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33043 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33044 OMP_CLAUSE_CHAIN (c
) = list
;
33053 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33054 enum omp_clause_code code
,
33055 tree list
, location_t location
)
33057 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33058 tree c
= build_omp_clause (location
, code
);
33059 OMP_CLAUSE_CHAIN (c
) = list
;
33064 num_teams ( expression ) */
33067 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33068 location_t location
)
33072 matching_parens parens
;
33073 if (!parens
.require_open (parser
))
33076 t
= cp_parser_expression (parser
);
33078 if (t
== error_mark_node
33079 || !parens
.require_close (parser
))
33080 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33081 /*or_comma=*/false,
33082 /*consume_paren=*/true);
33084 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33085 "num_teams", location
);
33087 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33088 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33089 OMP_CLAUSE_CHAIN (c
) = list
;
33095 thread_limit ( expression ) */
33098 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33099 location_t location
)
33103 matching_parens parens
;
33104 if (!parens
.require_open (parser
))
33107 t
= cp_parser_expression (parser
);
33109 if (t
== error_mark_node
33110 || !parens
.require_close (parser
))
33111 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33112 /*or_comma=*/false,
33113 /*consume_paren=*/true);
33115 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33116 "thread_limit", location
);
33118 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33119 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33120 OMP_CLAUSE_CHAIN (c
) = list
;
33126 aligned ( variable-list )
33127 aligned ( variable-list : constant-expression ) */
33130 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33132 tree nlist
, c
, alignment
= NULL_TREE
;
33135 matching_parens parens
;
33136 if (!parens
.require_open (parser
))
33139 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33144 alignment
= cp_parser_constant_expression (parser
);
33146 if (!parens
.require_close (parser
))
33147 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33148 /*or_comma=*/false,
33149 /*consume_paren=*/true);
33151 if (alignment
== error_mark_node
)
33152 alignment
= NULL_TREE
;
33155 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33156 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33162 linear ( variable-list )
33163 linear ( variable-list : expression )
33166 linear ( modifier ( variable-list ) )
33167 linear ( modifier ( variable-list ) : expression ) */
33170 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33173 tree nlist
, c
, step
= integer_one_node
;
33175 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33177 matching_parens parens
;
33178 if (!parens
.require_open (parser
))
33181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33183 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33184 const char *p
= IDENTIFIER_POINTER (id
);
33186 if (strcmp ("ref", p
) == 0)
33187 kind
= OMP_CLAUSE_LINEAR_REF
;
33188 else if (strcmp ("val", p
) == 0)
33189 kind
= OMP_CLAUSE_LINEAR_VAL
;
33190 else if (strcmp ("uval", p
) == 0)
33191 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33192 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33193 cp_lexer_consume_token (parser
->lexer
);
33195 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33198 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33199 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33203 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33204 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33206 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33207 else if (!parens
.require_close (parser
))
33208 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33209 /*or_comma=*/false,
33210 /*consume_paren=*/true);
33217 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33218 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33220 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33221 cp_parser_parse_tentatively (parser
);
33222 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33223 /*check_dependency_p=*/true,
33224 /*template_p=*/NULL
,
33225 /*declarator_p=*/false,
33226 /*optional_p=*/false);
33227 if (step
!= error_mark_node
)
33228 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33229 if (step
== error_mark_node
)
33232 cp_parser_abort_tentative_parse (parser
);
33234 else if (!cp_parser_parse_definitely (parser
))
33238 step
= cp_parser_expression (parser
);
33240 if (!parens
.require_close (parser
))
33241 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33242 /*or_comma=*/false,
33243 /*consume_paren=*/true);
33245 if (step
== error_mark_node
)
33249 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33251 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33252 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33259 safelen ( constant-expression ) */
33262 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33263 location_t location
)
33267 matching_parens parens
;
33268 if (!parens
.require_open (parser
))
33271 t
= cp_parser_constant_expression (parser
);
33273 if (t
== error_mark_node
33274 || !parens
.require_close (parser
))
33275 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33276 /*or_comma=*/false,
33277 /*consume_paren=*/true);
33279 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33281 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33282 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33283 OMP_CLAUSE_CHAIN (c
) = list
;
33289 simdlen ( constant-expression ) */
33292 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33293 location_t location
)
33297 matching_parens parens
;
33298 if (!parens
.require_open (parser
))
33301 t
= cp_parser_constant_expression (parser
);
33303 if (t
== error_mark_node
33304 || !parens
.require_close (parser
))
33305 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33306 /*or_comma=*/false,
33307 /*consume_paren=*/true);
33309 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33311 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33312 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33313 OMP_CLAUSE_CHAIN (c
) = list
;
33320 identifier [+/- integer]
33321 vec , identifier [+/- integer]
33325 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33330 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33332 cp_parser_error (parser
, "expected identifier");
33336 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33338 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33339 tree t
, identifier
= cp_parser_identifier (parser
);
33340 tree addend
= NULL
;
33342 if (identifier
== error_mark_node
)
33343 t
= error_mark_node
;
33346 t
= cp_parser_lookup_name_simple
33347 (parser
, identifier
,
33348 cp_lexer_peek_token (parser
->lexer
)->location
);
33349 if (t
== error_mark_node
)
33350 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33357 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33359 addend
= integer_zero_node
;
33360 goto add_to_vector
;
33362 cp_lexer_consume_token (parser
->lexer
);
33364 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33366 cp_parser_error (parser
, "expected integer");
33370 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33371 if (TREE_CODE (addend
) != INTEGER_CST
)
33373 cp_parser_error (parser
, "expected integer");
33376 cp_lexer_consume_token (parser
->lexer
);
33379 if (t
!= error_mark_node
)
33381 vec
= tree_cons (addend
, t
, vec
);
33383 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33386 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33389 cp_lexer_consume_token (parser
->lexer
);
33392 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33394 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33395 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33396 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33397 OMP_CLAUSE_CHAIN (u
) = list
;
33404 depend ( depend-kind : variable-list )
33412 depend ( sink : vec ) */
33415 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33418 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33420 matching_parens parens
;
33421 if (!parens
.require_open (parser
))
33424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33426 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33427 const char *p
= IDENTIFIER_POINTER (id
);
33429 if (strcmp ("in", p
) == 0)
33430 kind
= OMP_CLAUSE_DEPEND_IN
;
33431 else if (strcmp ("inout", p
) == 0)
33432 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33433 else if (strcmp ("out", p
) == 0)
33434 kind
= OMP_CLAUSE_DEPEND_OUT
;
33435 else if (strcmp ("source", p
) == 0)
33436 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33437 else if (strcmp ("sink", p
) == 0)
33438 kind
= OMP_CLAUSE_DEPEND_SINK
;
33445 cp_lexer_consume_token (parser
->lexer
);
33447 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33449 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33450 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33451 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33452 OMP_CLAUSE_CHAIN (c
) = list
;
33453 if (!parens
.require_close (parser
))
33454 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33455 /*or_comma=*/false,
33456 /*consume_paren=*/true);
33460 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33463 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33464 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33467 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33470 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33471 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33476 cp_parser_error (parser
, "invalid depend kind");
33478 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33479 /*or_comma=*/false,
33480 /*consume_paren=*/true);
33485 map ( map-kind : variable-list )
33486 map ( variable-list )
33489 alloc | to | from | tofrom
33493 alloc | to | from | tofrom | release | delete
33495 map ( always [,] map-kind: variable-list ) */
33498 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33501 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33502 bool always
= false;
33504 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33507 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33509 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33510 const char *p
= IDENTIFIER_POINTER (id
);
33512 if (strcmp ("always", p
) == 0)
33515 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33517 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33518 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33520 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33524 cp_lexer_consume_token (parser
->lexer
);
33526 cp_lexer_consume_token (parser
->lexer
);
33531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33532 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33534 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33535 const char *p
= IDENTIFIER_POINTER (id
);
33537 if (strcmp ("alloc", p
) == 0)
33538 kind
= GOMP_MAP_ALLOC
;
33539 else if (strcmp ("to", p
) == 0)
33540 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33541 else if (strcmp ("from", p
) == 0)
33542 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33543 else if (strcmp ("tofrom", p
) == 0)
33544 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33545 else if (strcmp ("release", p
) == 0)
33546 kind
= GOMP_MAP_RELEASE
;
33549 cp_parser_error (parser
, "invalid map kind");
33550 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33551 /*or_comma=*/false,
33552 /*consume_paren=*/true);
33555 cp_lexer_consume_token (parser
->lexer
);
33556 cp_lexer_consume_token (parser
->lexer
);
33558 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33559 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33561 kind
= GOMP_MAP_DELETE
;
33562 cp_lexer_consume_token (parser
->lexer
);
33563 cp_lexer_consume_token (parser
->lexer
);
33566 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33569 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33570 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33576 device ( expression ) */
33579 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33580 location_t location
)
33584 matching_parens parens
;
33585 if (!parens
.require_open (parser
))
33588 t
= cp_parser_expression (parser
);
33590 if (t
== error_mark_node
33591 || !parens
.require_close (parser
))
33592 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33593 /*or_comma=*/false,
33594 /*consume_paren=*/true);
33596 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33597 "device", location
);
33599 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33600 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33601 OMP_CLAUSE_CHAIN (c
) = list
;
33607 dist_schedule ( static )
33608 dist_schedule ( static , expression ) */
33611 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33612 location_t location
)
33616 matching_parens parens
;
33617 if (!parens
.require_open (parser
))
33620 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33622 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33624 cp_lexer_consume_token (parser
->lexer
);
33626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33628 cp_lexer_consume_token (parser
->lexer
);
33630 t
= cp_parser_assignment_expression (parser
);
33632 if (t
== error_mark_node
)
33634 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33636 if (!parens
.require_close (parser
))
33639 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33642 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33644 OMP_CLAUSE_CHAIN (c
) = list
;
33648 cp_parser_error (parser
, "invalid dist_schedule kind");
33650 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33651 /*or_comma=*/false,
33652 /*consume_paren=*/true);
33657 proc_bind ( proc-bind-kind )
33660 master | close | spread */
33663 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33664 location_t location
)
33667 enum omp_clause_proc_bind_kind kind
;
33669 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33672 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33674 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33675 const char *p
= IDENTIFIER_POINTER (id
);
33677 if (strcmp ("master", p
) == 0)
33678 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33679 else if (strcmp ("close", p
) == 0)
33680 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33681 else if (strcmp ("spread", p
) == 0)
33682 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33689 cp_lexer_consume_token (parser
->lexer
);
33690 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33693 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33694 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33696 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33697 OMP_CLAUSE_CHAIN (c
) = list
;
33701 cp_parser_error (parser
, "invalid depend kind");
33703 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33704 /*or_comma=*/false,
33705 /*consume_paren=*/true);
33710 async [( int-expr )] */
33713 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33716 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33718 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33720 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33722 matching_parens parens
;
33723 parens
.consume_open (parser
);
33725 t
= cp_parser_expression (parser
);
33726 if (t
== error_mark_node
33727 || !parens
.require_close (parser
))
33728 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33729 /*or_comma=*/false,
33730 /*consume_paren=*/true);
33733 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33735 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33736 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33737 OMP_CLAUSE_CHAIN (c
) = list
;
33743 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33744 is a bitmask in MASK. Return the list of clauses found. */
33747 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33748 const char *where
, cp_token
*pragma_tok
,
33749 bool finish_p
= true)
33751 tree clauses
= NULL
;
33754 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33757 pragma_omp_clause c_kind
;
33758 omp_clause_code code
;
33759 const char *c_name
;
33760 tree prev
= clauses
;
33762 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33763 cp_lexer_consume_token (parser
->lexer
);
33765 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33766 c_kind
= cp_parser_omp_clause_name (parser
);
33770 case PRAGMA_OACC_CLAUSE_ASYNC
:
33771 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33774 case PRAGMA_OACC_CLAUSE_AUTO
:
33775 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33779 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33780 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33781 c_name
= "collapse";
33783 case PRAGMA_OACC_CLAUSE_COPY
:
33784 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33787 case PRAGMA_OACC_CLAUSE_COPYIN
:
33788 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33791 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33792 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33793 c_name
= "copyout";
33795 case PRAGMA_OACC_CLAUSE_CREATE
:
33796 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33799 case PRAGMA_OACC_CLAUSE_DELETE
:
33800 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33803 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33804 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33805 c_name
= "default";
33807 case PRAGMA_OACC_CLAUSE_DEVICE
:
33808 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33811 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33812 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33813 c_name
= "deviceptr";
33815 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33816 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33817 c_name
= "device_resident";
33819 case PRAGMA_OACC_CLAUSE_FINALIZE
:
33820 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
33822 c_name
= "finalize";
33824 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33825 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33827 c_name
= "firstprivate";
33829 case PRAGMA_OACC_CLAUSE_GANG
:
33831 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33834 case PRAGMA_OACC_CLAUSE_HOST
:
33835 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33838 case PRAGMA_OACC_CLAUSE_IF
:
33839 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33842 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
33843 clauses
= cp_parser_oacc_simple_clause (parser
,
33844 OMP_CLAUSE_IF_PRESENT
,
33846 c_name
= "if_present";
33848 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33849 clauses
= cp_parser_oacc_simple_clause (parser
,
33850 OMP_CLAUSE_INDEPENDENT
,
33852 c_name
= "independent";
33854 case PRAGMA_OACC_CLAUSE_LINK
:
33855 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33858 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33859 code
= OMP_CLAUSE_NUM_GANGS
;
33860 c_name
= "num_gangs";
33861 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33864 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33865 c_name
= "num_workers";
33866 code
= OMP_CLAUSE_NUM_WORKERS
;
33867 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33870 case PRAGMA_OACC_CLAUSE_PRESENT
:
33871 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33872 c_name
= "present";
33874 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33875 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33877 c_name
= "private";
33879 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33880 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33881 c_name
= "reduction";
33883 case PRAGMA_OACC_CLAUSE_SEQ
:
33884 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33888 case PRAGMA_OACC_CLAUSE_TILE
:
33889 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33892 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33893 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33895 c_name
= "use_device";
33897 case PRAGMA_OACC_CLAUSE_VECTOR
:
33899 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33902 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33903 c_name
= "vector_length";
33904 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33905 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33908 case PRAGMA_OACC_CLAUSE_WAIT
:
33909 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33912 case PRAGMA_OACC_CLAUSE_WORKER
:
33914 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33918 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33924 if (((mask
>> c_kind
) & 1) == 0)
33926 /* Remove the invalid clause(s) from the list to avoid
33927 confusing the rest of the compiler. */
33929 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33934 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33937 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33942 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33943 is a bitmask in MASK. Return the list of clauses found; the result
33944 of clause default goes in *pdefault. */
33947 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33948 const char *where
, cp_token
*pragma_tok
,
33949 bool finish_p
= true)
33951 tree clauses
= NULL
;
33953 cp_token
*token
= NULL
;
33955 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33957 pragma_omp_clause c_kind
;
33958 const char *c_name
;
33959 tree prev
= clauses
;
33961 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33962 cp_lexer_consume_token (parser
->lexer
);
33964 token
= cp_lexer_peek_token (parser
->lexer
);
33965 c_kind
= cp_parser_omp_clause_name (parser
);
33969 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33970 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33972 c_name
= "collapse";
33974 case PRAGMA_OMP_CLAUSE_COPYIN
:
33975 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33978 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33979 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33981 c_name
= "copyprivate";
33983 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33984 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33985 token
->location
, false);
33986 c_name
= "default";
33988 case PRAGMA_OMP_CLAUSE_FINAL
:
33989 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33992 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33993 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33995 c_name
= "firstprivate";
33997 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33998 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34000 c_name
= "grainsize";
34002 case PRAGMA_OMP_CLAUSE_HINT
:
34003 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34007 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34008 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34010 c_name
= "defaultmap";
34012 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34013 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34015 c_name
= "use_device_ptr";
34017 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34018 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34020 c_name
= "is_device_ptr";
34022 case PRAGMA_OMP_CLAUSE_IF
:
34023 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34027 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34028 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34030 c_name
= "lastprivate";
34032 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34033 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34035 c_name
= "mergeable";
34037 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34038 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34041 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34042 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34044 c_name
= "num_tasks";
34046 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34047 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34049 c_name
= "num_threads";
34051 case PRAGMA_OMP_CLAUSE_ORDERED
:
34052 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34054 c_name
= "ordered";
34056 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34057 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34059 c_name
= "priority";
34061 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34062 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34064 c_name
= "private";
34066 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34067 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34068 c_name
= "reduction";
34070 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34071 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34073 c_name
= "schedule";
34075 case PRAGMA_OMP_CLAUSE_SHARED
:
34076 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34080 case PRAGMA_OMP_CLAUSE_UNTIED
:
34081 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34085 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34086 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34087 clauses
, token
->location
);
34088 c_name
= "inbranch";
34090 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34091 clauses
= cp_parser_omp_clause_branch (parser
,
34092 OMP_CLAUSE_NOTINBRANCH
,
34093 clauses
, token
->location
);
34094 c_name
= "notinbranch";
34096 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34097 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34098 clauses
, token
->location
);
34099 c_name
= "parallel";
34103 error_at (token
->location
, "%qs must be the first clause of %qs",
34108 case PRAGMA_OMP_CLAUSE_FOR
:
34109 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34110 clauses
, token
->location
);
34113 goto clause_not_first
;
34115 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34116 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34117 clauses
, token
->location
);
34118 c_name
= "sections";
34120 goto clause_not_first
;
34122 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34123 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34124 clauses
, token
->location
);
34125 c_name
= "taskgroup";
34127 goto clause_not_first
;
34129 case PRAGMA_OMP_CLAUSE_LINK
:
34130 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34133 case PRAGMA_OMP_CLAUSE_TO
:
34134 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34135 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34138 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34141 case PRAGMA_OMP_CLAUSE_FROM
:
34142 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34145 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34146 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34148 c_name
= "uniform";
34150 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34151 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34153 c_name
= "num_teams";
34155 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34156 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34158 c_name
= "thread_limit";
34160 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34161 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34162 c_name
= "aligned";
34164 case PRAGMA_OMP_CLAUSE_LINEAR
:
34166 bool declare_simd
= false;
34167 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34168 declare_simd
= true;
34169 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34173 case PRAGMA_OMP_CLAUSE_DEPEND
:
34174 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34178 case PRAGMA_OMP_CLAUSE_MAP
:
34179 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34182 case PRAGMA_OMP_CLAUSE_DEVICE
:
34183 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34187 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34188 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34190 c_name
= "dist_schedule";
34192 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34193 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34195 c_name
= "proc_bind";
34197 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34198 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34200 c_name
= "safelen";
34202 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34203 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34205 c_name
= "simdlen";
34207 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34208 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34210 c_name
= "nogroup";
34212 case PRAGMA_OMP_CLAUSE_THREADS
:
34214 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34215 clauses
, token
->location
);
34216 c_name
= "threads";
34218 case PRAGMA_OMP_CLAUSE_SIMD
:
34220 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34221 clauses
, token
->location
);
34225 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34231 if (((mask
>> c_kind
) & 1) == 0)
34233 /* Remove the invalid clause(s) from the list to avoid
34234 confusing the rest of the compiler. */
34236 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34240 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34243 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34244 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34246 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34255 In practice, we're also interested in adding the statement to an
34256 outer node. So it is convenient if we work around the fact that
34257 cp_parser_statement calls add_stmt. */
34260 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34262 unsigned save
= parser
->in_statement
;
34264 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34265 This preserves the "not within loop or switch" style error messages
34266 for nonsense cases like
34272 if (parser
->in_statement
)
34273 parser
->in_statement
= IN_OMP_BLOCK
;
34279 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34281 parser
->in_statement
= save
;
34285 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34287 tree stmt
= begin_omp_structured_block ();
34288 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34290 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34292 cp_parser_end_omp_structured_block (parser
, save
);
34293 return finish_omp_structured_block (stmt
);
34297 # pragma omp atomic new-line
34301 x binop= expr | x++ | ++x | x-- | --x
34303 +, *, -, /, &, ^, |, <<, >>
34305 where x is an lvalue expression with scalar type.
34308 # pragma omp atomic new-line
34311 # pragma omp atomic read new-line
34314 # pragma omp atomic write new-line
34317 # pragma omp atomic update new-line
34320 # pragma omp atomic capture new-line
34323 # pragma omp atomic capture new-line
34331 expression-stmt | x = x binop expr
34333 v = expression-stmt
34335 { v = x; update-stmt; } | { update-stmt; v = x; }
34339 expression-stmt | x = x binop expr | x = expr binop x
34343 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34345 where x and v are lvalue expressions with scalar type. */
34348 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34350 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34351 tree rhs1
= NULL_TREE
, orig_lhs
;
34352 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34353 bool structured_block
= false;
34354 bool seq_cst
= false;
34356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34358 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34359 const char *p
= IDENTIFIER_POINTER (id
);
34361 if (!strcmp (p
, "seq_cst"))
34364 cp_lexer_consume_token (parser
->lexer
);
34365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34366 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34367 cp_lexer_consume_token (parser
->lexer
);
34370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34372 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34373 const char *p
= IDENTIFIER_POINTER (id
);
34375 if (!strcmp (p
, "read"))
34376 code
= OMP_ATOMIC_READ
;
34377 else if (!strcmp (p
, "write"))
34379 else if (!strcmp (p
, "update"))
34381 else if (!strcmp (p
, "capture"))
34382 code
= OMP_ATOMIC_CAPTURE_NEW
;
34386 cp_lexer_consume_token (parser
->lexer
);
34390 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34391 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34392 cp_lexer_consume_token (parser
->lexer
);
34394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34396 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34397 const char *p
= IDENTIFIER_POINTER (id
);
34399 if (!strcmp (p
, "seq_cst"))
34402 cp_lexer_consume_token (parser
->lexer
);
34406 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34410 case OMP_ATOMIC_READ
:
34411 case NOP_EXPR
: /* atomic write */
34412 v
= cp_parser_unary_expression (parser
);
34413 if (v
== error_mark_node
)
34415 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34417 if (code
== NOP_EXPR
)
34418 lhs
= cp_parser_expression (parser
);
34420 lhs
= cp_parser_unary_expression (parser
);
34421 if (lhs
== error_mark_node
)
34423 if (code
== NOP_EXPR
)
34425 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34433 case OMP_ATOMIC_CAPTURE_NEW
:
34434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34436 cp_lexer_consume_token (parser
->lexer
);
34437 structured_block
= true;
34441 v
= cp_parser_unary_expression (parser
);
34442 if (v
== error_mark_node
)
34444 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34452 lhs
= cp_parser_unary_expression (parser
);
34454 switch (TREE_CODE (lhs
))
34459 case POSTINCREMENT_EXPR
:
34460 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34461 code
= OMP_ATOMIC_CAPTURE_OLD
;
34463 case PREINCREMENT_EXPR
:
34464 lhs
= TREE_OPERAND (lhs
, 0);
34465 opcode
= PLUS_EXPR
;
34466 rhs
= integer_one_node
;
34469 case POSTDECREMENT_EXPR
:
34470 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34471 code
= OMP_ATOMIC_CAPTURE_OLD
;
34473 case PREDECREMENT_EXPR
:
34474 lhs
= TREE_OPERAND (lhs
, 0);
34475 opcode
= MINUS_EXPR
;
34476 rhs
= integer_one_node
;
34479 case COMPOUND_EXPR
:
34480 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34481 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34482 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34483 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34484 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34485 (TREE_OPERAND (lhs
, 1), 0), 0)))
34487 /* Undo effects of boolean_increment for post {in,de}crement. */
34488 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34491 if (TREE_CODE (lhs
) == MODIFY_EXPR
34492 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34494 /* Undo effects of boolean_increment. */
34495 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34497 /* This is pre or post increment. */
34498 rhs
= TREE_OPERAND (lhs
, 1);
34499 lhs
= TREE_OPERAND (lhs
, 0);
34501 if (code
== OMP_ATOMIC_CAPTURE_NEW
34502 && !structured_block
34503 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34504 code
= OMP_ATOMIC_CAPTURE_OLD
;
34510 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34513 opcode
= MULT_EXPR
;
34516 opcode
= TRUNC_DIV_EXPR
;
34519 opcode
= PLUS_EXPR
;
34522 opcode
= MINUS_EXPR
;
34524 case CPP_LSHIFT_EQ
:
34525 opcode
= LSHIFT_EXPR
;
34527 case CPP_RSHIFT_EQ
:
34528 opcode
= RSHIFT_EXPR
;
34531 opcode
= BIT_AND_EXPR
;
34534 opcode
= BIT_IOR_EXPR
;
34537 opcode
= BIT_XOR_EXPR
;
34540 enum cp_parser_prec oprec
;
34542 cp_lexer_consume_token (parser
->lexer
);
34543 cp_parser_parse_tentatively (parser
);
34544 rhs1
= cp_parser_simple_cast_expression (parser
);
34545 if (rhs1
== error_mark_node
)
34547 cp_parser_abort_tentative_parse (parser
);
34548 cp_parser_simple_cast_expression (parser
);
34551 token
= cp_lexer_peek_token (parser
->lexer
);
34552 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34554 cp_parser_abort_tentative_parse (parser
);
34555 cp_parser_parse_tentatively (parser
);
34556 rhs
= cp_parser_binary_expression (parser
, false, true,
34557 PREC_NOT_OPERATOR
, NULL
);
34558 if (rhs
== error_mark_node
)
34560 cp_parser_abort_tentative_parse (parser
);
34561 cp_parser_binary_expression (parser
, false, true,
34562 PREC_NOT_OPERATOR
, NULL
);
34565 switch (TREE_CODE (rhs
))
34568 case TRUNC_DIV_EXPR
:
34577 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34579 if (cp_parser_parse_definitely (parser
))
34581 opcode
= TREE_CODE (rhs
);
34582 rhs1
= TREE_OPERAND (rhs
, 0);
34583 rhs
= TREE_OPERAND (rhs
, 1);
34593 cp_parser_abort_tentative_parse (parser
);
34594 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34596 rhs
= cp_parser_expression (parser
);
34597 if (rhs
== error_mark_node
)
34603 cp_parser_error (parser
,
34604 "invalid form of %<#pragma omp atomic%>");
34607 if (!cp_parser_parse_definitely (parser
))
34609 switch (token
->type
)
34611 case CPP_SEMICOLON
:
34612 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34614 code
= OMP_ATOMIC_CAPTURE_OLD
;
34619 cp_lexer_consume_token (parser
->lexer
);
34622 else if (structured_block
)
34629 cp_parser_error (parser
,
34630 "invalid form of %<#pragma omp atomic%>");
34633 opcode
= MULT_EXPR
;
34636 opcode
= TRUNC_DIV_EXPR
;
34639 opcode
= PLUS_EXPR
;
34642 opcode
= MINUS_EXPR
;
34645 opcode
= LSHIFT_EXPR
;
34648 opcode
= RSHIFT_EXPR
;
34651 opcode
= BIT_AND_EXPR
;
34654 opcode
= BIT_IOR_EXPR
;
34657 opcode
= BIT_XOR_EXPR
;
34660 cp_parser_error (parser
,
34661 "invalid operator for %<#pragma omp atomic%>");
34664 oprec
= TOKEN_PRECEDENCE (token
);
34665 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34666 if (commutative_tree_code (opcode
))
34667 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34668 cp_lexer_consume_token (parser
->lexer
);
34669 rhs
= cp_parser_binary_expression (parser
, false, false,
34671 if (rhs
== error_mark_node
)
34676 cp_parser_error (parser
,
34677 "invalid operator for %<#pragma omp atomic%>");
34680 cp_lexer_consume_token (parser
->lexer
);
34682 rhs
= cp_parser_expression (parser
);
34683 if (rhs
== error_mark_node
)
34688 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34690 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34692 v
= cp_parser_unary_expression (parser
);
34693 if (v
== error_mark_node
)
34695 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34697 lhs1
= cp_parser_unary_expression (parser
);
34698 if (lhs1
== error_mark_node
)
34701 if (structured_block
)
34703 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34704 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34707 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34708 if (!structured_block
)
34709 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34713 cp_parser_skip_to_end_of_block_or_statement (parser
);
34714 if (structured_block
)
34716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34717 cp_lexer_consume_token (parser
->lexer
);
34718 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34720 cp_parser_skip_to_end_of_block_or_statement (parser
);
34721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34722 cp_lexer_consume_token (parser
->lexer
);
34729 # pragma omp barrier new-line */
34732 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34734 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34735 finish_omp_barrier ();
34739 # pragma omp critical [(name)] new-line
34743 # pragma omp critical [(name) [hint(expression)]] new-line
34744 structured-block */
34746 #define OMP_CRITICAL_CLAUSE_MASK \
34747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34750 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34752 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34756 matching_parens parens
;
34757 parens
.consume_open (parser
);
34759 name
= cp_parser_identifier (parser
);
34761 if (name
== error_mark_node
34762 || !parens
.require_close (parser
))
34763 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34764 /*or_comma=*/false,
34765 /*consume_paren=*/true);
34766 if (name
== error_mark_node
)
34769 clauses
= cp_parser_omp_all_clauses (parser
,
34770 OMP_CRITICAL_CLAUSE_MASK
,
34771 "#pragma omp critical", pragma_tok
);
34774 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34776 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34777 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34781 # pragma omp flush flush-vars[opt] new-line
34784 ( variable-list ) */
34787 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34790 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34791 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34793 finish_omp_flush ();
34796 /* Helper function, to parse omp for increment expression. */
34799 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34801 tree cond
= cp_parser_binary_expression (parser
, false, true,
34802 PREC_NOT_OPERATOR
, NULL
);
34803 if (cond
== error_mark_node
34804 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34806 cp_parser_skip_to_end_of_statement (parser
);
34807 return error_mark_node
;
34810 switch (TREE_CODE (cond
))
34818 /* Fall through: OpenMP disallows NE_EXPR. */
34819 gcc_fallthrough ();
34821 return error_mark_node
;
34824 /* If decl is an iterator, preserve LHS and RHS of the relational
34825 expr until finish_omp_for. */
34827 && (type_dependent_expression_p (decl
)
34828 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34831 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34833 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34834 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34835 /*overload=*/NULL
, tf_warning_or_error
);
34838 /* Helper function, to parse omp for increment expression. */
34841 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34849 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34851 op
= (token
->type
== CPP_PLUS_PLUS
34852 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34853 cp_lexer_consume_token (parser
->lexer
);
34854 lhs
= cp_parser_simple_cast_expression (parser
);
34856 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34857 return error_mark_node
;
34858 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34861 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34863 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34864 return error_mark_node
;
34866 token
= cp_lexer_peek_token (parser
->lexer
);
34867 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34869 op
= (token
->type
== CPP_PLUS_PLUS
34870 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34871 cp_lexer_consume_token (parser
->lexer
);
34872 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34875 op
= cp_parser_assignment_operator_opt (parser
);
34876 if (op
== ERROR_MARK
)
34877 return error_mark_node
;
34879 if (op
!= NOP_EXPR
)
34881 rhs
= cp_parser_assignment_expression (parser
);
34882 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34883 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34886 lhs
= cp_parser_binary_expression (parser
, false, false,
34887 PREC_ADDITIVE_EXPRESSION
, NULL
);
34888 token
= cp_lexer_peek_token (parser
->lexer
);
34889 decl_first
= (lhs
== decl
34890 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34893 if (token
->type
!= CPP_PLUS
34894 && token
->type
!= CPP_MINUS
)
34895 return error_mark_node
;
34899 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34900 cp_lexer_consume_token (parser
->lexer
);
34901 rhs
= cp_parser_binary_expression (parser
, false, false,
34902 PREC_ADDITIVE_EXPRESSION
, NULL
);
34903 token
= cp_lexer_peek_token (parser
->lexer
);
34904 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34906 if (lhs
== NULL_TREE
)
34908 if (op
== PLUS_EXPR
)
34911 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34912 tf_warning_or_error
);
34915 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34916 ERROR_MARK
, NULL
, tf_warning_or_error
);
34919 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34924 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34925 || op
== MINUS_EXPR
)
34926 return error_mark_node
;
34927 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34930 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34932 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34935 /* Parse the initialization statement of an OpenMP for loop.
34937 Return true if the resulting construct should have an
34938 OMP_CLAUSE_PRIVATE added to it. */
34941 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34942 tree
&this_pre_body
,
34943 vec
<tree
, va_gc
> *&for_block
,
34949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34952 tree add_private_clause
= NULL_TREE
;
34954 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34958 integer-type var = lb
34959 random-access-iterator-type var = lb
34960 pointer-type var = lb
34962 cp_decl_specifier_seq type_specifiers
;
34964 /* First, try to parse as an initialized declaration. See
34965 cp_parser_condition, from whence the bulk of this is copied. */
34967 cp_parser_parse_tentatively (parser
);
34968 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34969 /*is_trailing_return=*/false,
34971 if (cp_parser_parse_definitely (parser
))
34973 /* If parsing a type specifier seq succeeded, then this
34974 MUST be a initialized declaration. */
34975 tree asm_specification
, attributes
;
34976 cp_declarator
*declarator
;
34978 declarator
= cp_parser_declarator (parser
,
34979 CP_PARSER_DECLARATOR_NAMED
,
34980 /*ctor_dtor_or_conv_p=*/NULL
,
34981 /*parenthesized_p=*/NULL
,
34982 /*member_p=*/false,
34983 /*friend_p=*/false);
34984 attributes
= cp_parser_attributes_opt (parser
);
34985 asm_specification
= cp_parser_asm_specification_opt (parser
);
34987 if (declarator
== cp_error_declarator
)
34988 cp_parser_skip_to_end_of_statement (parser
);
34992 tree pushed_scope
, auto_node
;
34994 decl
= start_decl (declarator
, &type_specifiers
,
34995 SD_INITIALIZED
, attributes
,
34996 /*prefix_attributes=*/NULL_TREE
,
34999 auto_node
= type_uses_auto (TREE_TYPE (decl
));
35000 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
35002 if (cp_lexer_next_token_is (parser
->lexer
,
35004 error ("parenthesized initialization is not allowed in "
35005 "OpenMP %<for%> loop");
35007 /* Trigger an error. */
35008 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35010 init
= error_mark_node
;
35011 cp_parser_skip_to_end_of_statement (parser
);
35013 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35014 || type_dependent_expression_p (decl
)
35017 bool is_direct_init
, is_non_constant_init
;
35019 init
= cp_parser_initializer (parser
,
35021 &is_non_constant_init
);
35026 = do_auto_deduction (TREE_TYPE (decl
), init
,
35029 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35030 && !type_dependent_expression_p (decl
))
35034 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35036 LOOKUP_ONLYCONVERTING
);
35038 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35040 vec_safe_push (for_block
, this_pre_body
);
35045 init
= pop_stmt_list (this_pre_body
);
35046 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35048 tree_stmt_iterator i
= tsi_start (init
);
35049 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35050 while (!tsi_end_p (i
))
35052 tree t
= tsi_stmt (i
);
35053 if (TREE_CODE (t
) == DECL_EXPR
35054 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35057 vec_safe_push (for_block
, t
);
35062 if (tsi_one_before_end_p (i
))
35064 tree t
= tsi_stmt (i
);
35066 free_stmt_list (init
);
35071 this_pre_body
= NULL_TREE
;
35076 cp_lexer_consume_token (parser
->lexer
);
35077 init
= cp_parser_assignment_expression (parser
);
35080 if (TYPE_REF_P (TREE_TYPE (decl
)))
35081 init
= error_mark_node
;
35083 cp_finish_decl (decl
, NULL_TREE
,
35084 /*init_const_expr_p=*/false,
35086 LOOKUP_ONLYCONVERTING
);
35090 pop_scope (pushed_scope
);
35096 /* If parsing a type specifier sequence failed, then
35097 this MUST be a simple expression. */
35098 cp_parser_parse_tentatively (parser
);
35099 decl
= cp_parser_primary_expression (parser
, false, false,
35101 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35102 if (!cp_parser_error_occurred (parser
)
35104 && (TREE_CODE (decl
) == COMPONENT_REF
35105 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35107 cp_parser_abort_tentative_parse (parser
);
35108 cp_parser_parse_tentatively (parser
);
35109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35110 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35111 /*check_dependency_p=*/true,
35112 /*template_p=*/NULL
,
35113 /*declarator_p=*/false,
35114 /*optional_p=*/false);
35115 if (name
!= error_mark_node
35116 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35118 decl
= cp_parser_lookup_name_simple (parser
, name
,
35120 if (TREE_CODE (decl
) == FIELD_DECL
)
35121 add_private_clause
= omp_privatize_field (decl
, false);
35123 cp_parser_abort_tentative_parse (parser
);
35124 cp_parser_parse_tentatively (parser
);
35125 decl
= cp_parser_primary_expression (parser
, false, false,
35128 if (!cp_parser_error_occurred (parser
)
35131 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35135 cp_parser_parse_definitely (parser
);
35136 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35137 rhs
= cp_parser_assignment_expression (parser
);
35139 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35142 tf_warning_or_error
));
35143 if (!add_private_clause
)
35144 add_private_clause
= decl
;
35149 cp_parser_abort_tentative_parse (parser
);
35150 init
= cp_parser_expression (parser
);
35153 if (TREE_CODE (init
) == MODIFY_EXPR
35154 || TREE_CODE (init
) == MODOP_EXPR
)
35155 real_decl
= TREE_OPERAND (init
, 0);
35159 return add_private_clause
;
35162 /* Parse the restricted form of the for statement allowed by OpenMP. */
35165 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35166 tree
*cclauses
, bool *if_p
)
35168 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35169 tree real_decl
, initv
, condv
, incrv
, declv
;
35170 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35171 location_t loc_first
;
35172 bool collapse_err
= false;
35173 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35174 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35175 auto_vec
<tree
, 4> orig_inits
;
35176 bool tiling
= false;
35178 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35179 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35180 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35181 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35184 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35186 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35187 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35190 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35193 if (ordered
&& ordered
< collapse
)
35195 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35196 "%<ordered%> clause parameter is less than %<collapse%>");
35197 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35198 = build_int_cst (NULL_TREE
, collapse
);
35199 ordered
= collapse
;
35203 for (tree
*pc
= &clauses
; *pc
; )
35204 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35206 error_at (OMP_CLAUSE_LOCATION (*pc
),
35207 "%<linear%> clause may not be specified together "
35208 "with %<ordered%> clause with a parameter");
35209 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35212 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35215 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35216 count
= ordered
? ordered
: collapse
;
35218 declv
= make_tree_vec (count
);
35219 initv
= make_tree_vec (count
);
35220 condv
= make_tree_vec (count
);
35221 incrv
= make_tree_vec (count
);
35223 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35225 for (i
= 0; i
< count
; i
++)
35227 int bracecount
= 0;
35228 tree add_private_clause
= NULL_TREE
;
35231 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35234 cp_parser_error (parser
, "for statement expected");
35237 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35239 matching_parens parens
;
35240 if (!parens
.require_open (parser
))
35243 init
= orig_init
= decl
= real_decl
= NULL
;
35244 this_pre_body
= push_stmt_list ();
35247 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35248 init
, orig_init
, decl
, real_decl
);
35250 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35253 this_pre_body
= pop_stmt_list (this_pre_body
);
35257 pre_body
= push_stmt_list ();
35259 add_stmt (this_pre_body
);
35260 pre_body
= pop_stmt_list (pre_body
);
35263 pre_body
= this_pre_body
;
35268 if (cclauses
!= NULL
35269 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35270 && real_decl
!= NULL_TREE
)
35273 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35274 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35275 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35277 error_at (loc
, "iteration variable %qD"
35278 " should not be firstprivate", real_decl
);
35279 *c
= OMP_CLAUSE_CHAIN (*c
);
35281 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35282 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35284 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35286 *c
= OMP_CLAUSE_CHAIN (*c
);
35287 if (code
== OMP_SIMD
)
35289 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35290 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35294 OMP_CLAUSE_CHAIN (l
) = clauses
;
35297 add_private_clause
= NULL_TREE
;
35301 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35302 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35303 add_private_clause
= NULL_TREE
;
35304 c
= &OMP_CLAUSE_CHAIN (*c
);
35308 if (add_private_clause
)
35311 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35313 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35314 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35315 && OMP_CLAUSE_DECL (c
) == decl
)
35317 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35318 && OMP_CLAUSE_DECL (c
) == decl
)
35319 error_at (loc
, "iteration variable %qD "
35320 "should not be firstprivate",
35322 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35323 && OMP_CLAUSE_DECL (c
) == decl
)
35324 error_at (loc
, "iteration variable %qD should not be reduction",
35329 if (code
!= OMP_SIMD
)
35330 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35331 else if (collapse
== 1)
35332 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35334 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35335 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35336 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35339 OMP_CLAUSE_CHAIN (c
) = clauses
;
35341 /* For linear, signal that we need to fill up
35342 the so far unknown linear step. */
35343 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35344 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35350 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35351 cond
= cp_parser_omp_for_cond (parser
, decl
);
35352 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35357 /* If decl is an iterator, preserve the operator on decl
35358 until finish_omp_for. */
35360 && ((processing_template_decl
35361 && (TREE_TYPE (real_decl
) == NULL_TREE
35362 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35363 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35364 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35366 incr
= cp_parser_expression (parser
);
35367 if (!EXPR_HAS_LOCATION (incr
))
35368 protected_set_expr_location (incr
, input_location
);
35371 if (!parens
.require_close (parser
))
35372 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35373 /*or_comma=*/false,
35374 /*consume_paren=*/true);
35376 TREE_VEC_ELT (declv
, i
) = decl
;
35377 TREE_VEC_ELT (initv
, i
) = init
;
35378 TREE_VEC_ELT (condv
, i
) = cond
;
35379 TREE_VEC_ELT (incrv
, i
) = incr
;
35382 orig_inits
.safe_grow_cleared (i
+ 1);
35383 orig_inits
[i
] = orig_init
;
35386 if (i
== count
- 1)
35389 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35390 in between the collapsed for loops to be still considered perfectly
35391 nested. Hopefully the final version clarifies this.
35392 For now handle (multiple) {'s and empty statements. */
35393 cp_parser_parse_tentatively (parser
);
35396 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35398 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35400 cp_lexer_consume_token (parser
->lexer
);
35403 else if (bracecount
35404 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35405 cp_lexer_consume_token (parser
->lexer
);
35408 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35409 error_at (loc
, "not enough for loops to collapse");
35410 collapse_err
= true;
35411 cp_parser_abort_tentative_parse (parser
);
35419 cp_parser_parse_definitely (parser
);
35420 nbraces
+= bracecount
;
35427 /* Note that we saved the original contents of this flag when we entered
35428 the structured block, and so we don't need to re-save it here. */
35429 parser
->in_statement
= IN_OMP_FOR
;
35431 /* Note that the grammar doesn't call for a structured block here,
35432 though the loop as a whole is a structured block. */
35433 body
= push_stmt_list ();
35434 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35435 body
= pop_stmt_list (body
);
35437 if (declv
== NULL_TREE
)
35440 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35441 body
, pre_body
, &orig_inits
, clauses
);
35445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35447 cp_lexer_consume_token (parser
->lexer
);
35450 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35451 cp_lexer_consume_token (parser
->lexer
);
35456 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35457 "collapsed loops not perfectly nested");
35459 collapse_err
= true;
35460 cp_parser_statement_seq_opt (parser
, NULL
);
35461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35466 while (!for_block
->is_empty ())
35468 tree t
= for_block
->pop ();
35469 if (TREE_CODE (t
) == STATEMENT_LIST
)
35470 add_stmt (pop_stmt_list (t
));
35474 release_tree_vector (for_block
);
35479 /* Helper function for OpenMP parsing, split clauses and call
35480 finish_omp_clauses on each of the set of clauses afterwards. */
35483 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35484 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35487 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35488 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35490 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35494 #pragma omp simd simd-clause[optseq] new-line
35497 #define OMP_SIMD_CLAUSE_MASK \
35498 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35508 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35509 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35512 tree clauses
, sb
, ret
;
35514 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35516 strcat (p_name
, " simd");
35517 mask
|= OMP_SIMD_CLAUSE_MASK
;
35519 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35523 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35524 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35525 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35526 OMP_CLAUSE_ORDERED
);
35527 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35529 error_at (OMP_CLAUSE_LOCATION (c
),
35530 "%<ordered%> clause with parameter may not be specified "
35531 "on %qs construct", p_name
);
35532 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35536 sb
= begin_omp_structured_block ();
35537 save
= cp_parser_begin_omp_structured_block (parser
);
35539 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35541 cp_parser_end_omp_structured_block (parser
, save
);
35542 add_stmt (finish_omp_structured_block (sb
));
35548 #pragma omp for for-clause[optseq] new-line
35552 #pragma omp for simd for-simd-clause[optseq] new-line
35555 #define OMP_FOR_CLAUSE_MASK \
35556 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35567 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35568 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35571 tree clauses
, sb
, ret
;
35573 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35575 strcat (p_name
, " for");
35576 mask
|= OMP_FOR_CLAUSE_MASK
;
35577 /* parallel for{, simd} disallows nowait clause, but for
35578 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35579 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35580 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35581 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35582 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35583 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35587 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35588 const char *p
= IDENTIFIER_POINTER (id
);
35590 if (strcmp (p
, "simd") == 0)
35592 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35593 if (cclauses
== NULL
)
35594 cclauses
= cclauses_buf
;
35596 cp_lexer_consume_token (parser
->lexer
);
35597 if (!flag_openmp
) /* flag_openmp_simd */
35598 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35600 sb
= begin_omp_structured_block ();
35601 save
= cp_parser_begin_omp_structured_block (parser
);
35602 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35604 cp_parser_end_omp_structured_block (parser
, save
);
35605 tree body
= finish_omp_structured_block (sb
);
35608 ret
= make_node (OMP_FOR
);
35609 TREE_TYPE (ret
) = void_type_node
;
35610 OMP_FOR_BODY (ret
) = body
;
35611 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35612 SET_EXPR_LOCATION (ret
, loc
);
35617 if (!flag_openmp
) /* flag_openmp_simd */
35619 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35623 /* Composite distribute parallel for disallows linear clause. */
35624 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35625 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35627 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35631 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35632 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35635 sb
= begin_omp_structured_block ();
35636 save
= cp_parser_begin_omp_structured_block (parser
);
35638 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35640 cp_parser_end_omp_structured_block (parser
, save
);
35641 add_stmt (finish_omp_structured_block (sb
));
35647 # pragma omp master new-line
35648 structured-block */
35651 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35653 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35654 return c_finish_omp_master (input_location
,
35655 cp_parser_omp_structured_block (parser
, if_p
));
35659 # pragma omp ordered new-line
35663 # pragma omp ordered ordered-clauses new-line
35664 structured-block */
35666 #define OMP_ORDERED_CLAUSE_MASK \
35667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35670 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35671 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35674 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35675 enum pragma_context context
, bool *if_p
)
35677 location_t loc
= pragma_tok
->location
;
35679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35681 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35682 const char *p
= IDENTIFIER_POINTER (id
);
35684 if (strcmp (p
, "depend") == 0)
35686 if (!flag_openmp
) /* flag_openmp_simd */
35688 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35691 if (context
== pragma_stmt
)
35693 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35694 "%<depend%> clause may only be used in compound "
35696 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35700 = cp_parser_omp_all_clauses (parser
,
35701 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35702 "#pragma omp ordered", pragma_tok
);
35703 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35709 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35710 "#pragma omp ordered", pragma_tok
);
35712 if (!flag_openmp
/* flag_openmp_simd */
35713 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35716 c_finish_omp_ordered (loc
, clauses
,
35717 cp_parser_omp_structured_block (parser
, if_p
));
35724 { section-sequence }
35727 section-directive[opt] structured-block
35728 section-sequence section-directive structured-block */
35731 cp_parser_omp_sections_scope (cp_parser
*parser
)
35733 tree stmt
, substmt
;
35734 bool error_suppress
= false;
35737 matching_braces braces
;
35738 if (!braces
.require_open (parser
))
35741 stmt
= push_stmt_list ();
35743 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35744 != PRAGMA_OMP_SECTION
)
35746 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35747 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35748 add_stmt (substmt
);
35753 tok
= cp_lexer_peek_token (parser
->lexer
);
35754 if (tok
->type
== CPP_CLOSE_BRACE
)
35756 if (tok
->type
== CPP_EOF
)
35759 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35761 cp_lexer_consume_token (parser
->lexer
);
35762 cp_parser_require_pragma_eol (parser
, tok
);
35763 error_suppress
= false;
35765 else if (!error_suppress
)
35767 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35768 error_suppress
= true;
35771 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35772 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35773 add_stmt (substmt
);
35775 braces
.require_close (parser
);
35777 substmt
= pop_stmt_list (stmt
);
35779 stmt
= make_node (OMP_SECTIONS
);
35780 TREE_TYPE (stmt
) = void_type_node
;
35781 OMP_SECTIONS_BODY (stmt
) = substmt
;
35788 # pragma omp sections sections-clause[optseq] newline
35791 #define OMP_SECTIONS_CLAUSE_MASK \
35792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35799 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35800 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35803 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35805 strcat (p_name
, " sections");
35806 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35808 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35810 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35814 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35815 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35818 ret
= cp_parser_omp_sections_scope (parser
);
35820 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35826 # pragma omp parallel parallel-clause[optseq] new-line
35828 # pragma omp parallel for parallel-for-clause[optseq] new-line
35830 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35834 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35835 structured-block */
35837 #define OMP_PARALLEL_CLAUSE_MASK \
35838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35849 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35850 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35853 tree stmt
, clauses
, block
;
35855 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35857 strcat (p_name
, " parallel");
35858 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35859 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35860 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35861 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35862 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35864 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35866 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35867 if (cclauses
== NULL
)
35868 cclauses
= cclauses_buf
;
35870 cp_lexer_consume_token (parser
->lexer
);
35871 if (!flag_openmp
) /* flag_openmp_simd */
35872 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35874 block
= begin_omp_parallel ();
35875 save
= cp_parser_begin_omp_structured_block (parser
);
35876 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35878 cp_parser_end_omp_structured_block (parser
, save
);
35879 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35881 if (ret
== NULL_TREE
)
35883 OMP_PARALLEL_COMBINED (stmt
) = 1;
35886 /* When combined with distribute, parallel has to be followed by for.
35887 #pragma omp target parallel is allowed though. */
35889 && (mask
& (OMP_CLAUSE_MASK_1
35890 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35892 error_at (loc
, "expected %<for%> after %qs", p_name
);
35893 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35896 else if (!flag_openmp
) /* flag_openmp_simd */
35898 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35901 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35903 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35904 const char *p
= IDENTIFIER_POINTER (id
);
35905 if (strcmp (p
, "sections") == 0)
35907 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35908 cclauses
= cclauses_buf
;
35910 cp_lexer_consume_token (parser
->lexer
);
35911 block
= begin_omp_parallel ();
35912 save
= cp_parser_begin_omp_structured_block (parser
);
35913 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35914 cp_parser_end_omp_structured_block (parser
, save
);
35915 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35917 OMP_PARALLEL_COMBINED (stmt
) = 1;
35922 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35926 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35927 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35930 block
= begin_omp_parallel ();
35931 save
= cp_parser_begin_omp_structured_block (parser
);
35932 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35933 cp_parser_end_omp_structured_block (parser
, save
);
35934 stmt
= finish_omp_parallel (clauses
, block
);
35939 # pragma omp single single-clause[optseq] new-line
35940 structured-block */
35942 #define OMP_SINGLE_CLAUSE_MASK \
35943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35949 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35951 tree stmt
= make_node (OMP_SINGLE
);
35952 TREE_TYPE (stmt
) = void_type_node
;
35954 OMP_SINGLE_CLAUSES (stmt
)
35955 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35956 "#pragma omp single", pragma_tok
);
35957 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35959 return add_stmt (stmt
);
35963 # pragma omp task task-clause[optseq] new-line
35964 structured-block */
35966 #define OMP_TASK_CLAUSE_MASK \
35967 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35979 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35981 tree clauses
, block
;
35984 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35985 "#pragma omp task", pragma_tok
);
35986 block
= begin_omp_task ();
35987 save
= cp_parser_begin_omp_structured_block (parser
);
35988 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35989 cp_parser_end_omp_structured_block (parser
, save
);
35990 return finish_omp_task (clauses
, block
);
35994 # pragma omp taskwait new-line */
35997 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35999 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36000 finish_omp_taskwait ();
36004 # pragma omp taskyield new-line */
36007 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
36009 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36010 finish_omp_taskyield ();
36014 # pragma omp taskgroup new-line
36015 structured-block */
36018 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36020 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36021 return c_finish_omp_taskgroup (input_location
,
36022 cp_parser_omp_structured_block (parser
,
36028 # pragma omp threadprivate (variable-list) */
36031 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36035 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36036 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36038 finish_omp_threadprivate (vars
);
36042 # pragma omp cancel cancel-clause[optseq] new-line */
36044 #define OMP_CANCEL_CLAUSE_MASK \
36045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36052 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36054 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36055 "#pragma omp cancel", pragma_tok
);
36056 finish_omp_cancel (clauses
);
36060 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36062 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36063 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36069 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36070 enum pragma_context context
)
36073 bool point_seen
= false;
36075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36077 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36078 const char *p
= IDENTIFIER_POINTER (id
);
36080 if (strcmp (p
, "point") == 0)
36082 cp_lexer_consume_token (parser
->lexer
);
36088 cp_parser_error (parser
, "expected %<point%>");
36089 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36093 if (context
!= pragma_compound
)
36095 if (context
== pragma_stmt
)
36096 error_at (pragma_tok
->location
,
36097 "%<#pragma %s%> may only be used in compound statements",
36098 "omp cancellation point");
36100 cp_parser_error (parser
, "expected declaration specifiers");
36101 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36105 clauses
= cp_parser_omp_all_clauses (parser
,
36106 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36107 "#pragma omp cancellation point",
36109 finish_omp_cancellation_point (clauses
);
36113 #pragma omp distribute distribute-clause[optseq] new-line
36116 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36124 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36125 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36128 tree clauses
, sb
, ret
;
36130 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36132 strcat (p_name
, " distribute");
36133 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36137 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36138 const char *p
= IDENTIFIER_POINTER (id
);
36140 bool parallel
= false;
36142 if (strcmp (p
, "simd") == 0)
36145 parallel
= strcmp (p
, "parallel") == 0;
36146 if (parallel
|| simd
)
36148 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36149 if (cclauses
== NULL
)
36150 cclauses
= cclauses_buf
;
36151 cp_lexer_consume_token (parser
->lexer
);
36152 if (!flag_openmp
) /* flag_openmp_simd */
36155 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36158 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36161 sb
= begin_omp_structured_block ();
36162 save
= cp_parser_begin_omp_structured_block (parser
);
36164 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36167 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36169 cp_parser_end_omp_structured_block (parser
, save
);
36170 tree body
= finish_omp_structured_block (sb
);
36173 ret
= make_node (OMP_DISTRIBUTE
);
36174 TREE_TYPE (ret
) = void_type_node
;
36175 OMP_FOR_BODY (ret
) = body
;
36176 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36177 SET_EXPR_LOCATION (ret
, loc
);
36182 if (!flag_openmp
) /* flag_openmp_simd */
36184 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36188 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36192 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36193 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36196 sb
= begin_omp_structured_block ();
36197 save
= cp_parser_begin_omp_structured_block (parser
);
36199 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36201 cp_parser_end_omp_structured_block (parser
, save
);
36202 add_stmt (finish_omp_structured_block (sb
));
36208 # pragma omp teams teams-clause[optseq] new-line
36209 structured-block */
36211 #define OMP_TEAMS_CLAUSE_MASK \
36212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36221 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36222 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36225 tree clauses
, sb
, ret
;
36227 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36229 strcat (p_name
, " teams");
36230 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36234 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36235 const char *p
= IDENTIFIER_POINTER (id
);
36236 if (strcmp (p
, "distribute") == 0)
36238 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36239 if (cclauses
== NULL
)
36240 cclauses
= cclauses_buf
;
36242 cp_lexer_consume_token (parser
->lexer
);
36243 if (!flag_openmp
) /* flag_openmp_simd */
36244 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36246 sb
= begin_omp_structured_block ();
36247 save
= cp_parser_begin_omp_structured_block (parser
);
36248 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36250 cp_parser_end_omp_structured_block (parser
, save
);
36251 tree body
= finish_omp_structured_block (sb
);
36254 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36255 ret
= make_node (OMP_TEAMS
);
36256 TREE_TYPE (ret
) = void_type_node
;
36257 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36258 OMP_TEAMS_BODY (ret
) = body
;
36259 OMP_TEAMS_COMBINED (ret
) = 1;
36260 SET_EXPR_LOCATION (ret
, loc
);
36261 return add_stmt (ret
);
36264 if (!flag_openmp
) /* flag_openmp_simd */
36266 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36270 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36274 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36275 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36278 tree stmt
= make_node (OMP_TEAMS
);
36279 TREE_TYPE (stmt
) = void_type_node
;
36280 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36281 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36282 SET_EXPR_LOCATION (stmt
, loc
);
36284 return add_stmt (stmt
);
36288 # pragma omp target data target-data-clause[optseq] new-line
36289 structured-block */
36291 #define OMP_TARGET_DATA_CLAUSE_MASK \
36292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36298 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36301 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36302 "#pragma omp target data", pragma_tok
);
36304 for (tree
*pc
= &clauses
; *pc
;)
36306 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36307 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36310 case GOMP_MAP_ALWAYS_TO
:
36311 case GOMP_MAP_FROM
:
36312 case GOMP_MAP_ALWAYS_FROM
:
36313 case GOMP_MAP_TOFROM
:
36314 case GOMP_MAP_ALWAYS_TOFROM
:
36315 case GOMP_MAP_ALLOC
:
36318 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36319 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36320 case GOMP_MAP_ALWAYS_POINTER
:
36324 error_at (OMP_CLAUSE_LOCATION (*pc
),
36325 "%<#pragma omp target data%> with map-type other "
36326 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36327 "on %<map%> clause");
36328 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36331 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36337 error_at (pragma_tok
->location
,
36338 "%<#pragma omp target data%> must contain at least "
36339 "one %<map%> clause");
36343 tree stmt
= make_node (OMP_TARGET_DATA
);
36344 TREE_TYPE (stmt
) = void_type_node
;
36345 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36347 keep_next_level (true);
36348 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36350 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36351 return add_stmt (stmt
);
36355 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36356 structured-block */
36358 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36359 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36366 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36367 enum pragma_context context
)
36369 bool data_seen
= false;
36370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36372 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36373 const char *p
= IDENTIFIER_POINTER (id
);
36375 if (strcmp (p
, "data") == 0)
36377 cp_lexer_consume_token (parser
->lexer
);
36383 cp_parser_error (parser
, "expected %<data%>");
36384 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36388 if (context
== pragma_stmt
)
36390 error_at (pragma_tok
->location
,
36391 "%<#pragma %s%> may only be used in compound statements",
36392 "omp target enter data");
36393 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36398 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36399 "#pragma omp target enter data", pragma_tok
);
36401 for (tree
*pc
= &clauses
; *pc
;)
36403 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36404 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36407 case GOMP_MAP_ALWAYS_TO
:
36408 case GOMP_MAP_ALLOC
:
36411 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36412 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36413 case GOMP_MAP_ALWAYS_POINTER
:
36417 error_at (OMP_CLAUSE_LOCATION (*pc
),
36418 "%<#pragma omp target enter data%> with map-type other "
36419 "than %<to%> or %<alloc%> on %<map%> clause");
36420 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36423 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36429 error_at (pragma_tok
->location
,
36430 "%<#pragma omp target enter data%> must contain at least "
36431 "one %<map%> clause");
36435 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36436 TREE_TYPE (stmt
) = void_type_node
;
36437 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36438 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36439 return add_stmt (stmt
);
36443 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36444 structured-block */
36446 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36454 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36455 enum pragma_context context
)
36457 bool data_seen
= false;
36458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36460 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36461 const char *p
= IDENTIFIER_POINTER (id
);
36463 if (strcmp (p
, "data") == 0)
36465 cp_lexer_consume_token (parser
->lexer
);
36471 cp_parser_error (parser
, "expected %<data%>");
36472 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36476 if (context
== pragma_stmt
)
36478 error_at (pragma_tok
->location
,
36479 "%<#pragma %s%> may only be used in compound statements",
36480 "omp target exit data");
36481 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36486 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36487 "#pragma omp target exit data", pragma_tok
);
36489 for (tree
*pc
= &clauses
; *pc
;)
36491 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36492 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36494 case GOMP_MAP_FROM
:
36495 case GOMP_MAP_ALWAYS_FROM
:
36496 case GOMP_MAP_RELEASE
:
36497 case GOMP_MAP_DELETE
:
36500 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36501 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36502 case GOMP_MAP_ALWAYS_POINTER
:
36506 error_at (OMP_CLAUSE_LOCATION (*pc
),
36507 "%<#pragma omp target exit data%> with map-type other "
36508 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36510 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36513 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36519 error_at (pragma_tok
->location
,
36520 "%<#pragma omp target exit data%> must contain at least "
36521 "one %<map%> clause");
36525 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36526 TREE_TYPE (stmt
) = void_type_node
;
36527 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36528 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36529 return add_stmt (stmt
);
36533 # pragma omp target update target-update-clause[optseq] new-line */
36535 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36544 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36545 enum pragma_context context
)
36547 if (context
== pragma_stmt
)
36549 error_at (pragma_tok
->location
,
36550 "%<#pragma %s%> may only be used in compound statements",
36551 "omp target update");
36552 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36557 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36558 "#pragma omp target update", pragma_tok
);
36559 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36560 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36562 error_at (pragma_tok
->location
,
36563 "%<#pragma omp target update%> must contain at least one "
36564 "%<from%> or %<to%> clauses");
36568 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36569 TREE_TYPE (stmt
) = void_type_node
;
36570 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36571 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36577 # pragma omp target target-clause[optseq] new-line
36578 structured-block */
36580 #define OMP_TARGET_CLAUSE_MASK \
36581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36592 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36593 enum pragma_context context
, bool *if_p
)
36595 tree
*pc
= NULL
, stmt
;
36597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36599 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36600 const char *p
= IDENTIFIER_POINTER (id
);
36601 enum tree_code ccode
= ERROR_MARK
;
36603 if (strcmp (p
, "teams") == 0)
36605 else if (strcmp (p
, "parallel") == 0)
36606 ccode
= OMP_PARALLEL
;
36607 else if (strcmp (p
, "simd") == 0)
36609 if (ccode
!= ERROR_MARK
)
36611 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36612 char p_name
[sizeof ("#pragma omp target teams distribute "
36613 "parallel for simd")];
36615 cp_lexer_consume_token (parser
->lexer
);
36616 strcpy (p_name
, "#pragma omp target");
36617 if (!flag_openmp
) /* flag_openmp_simd */
36623 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36624 OMP_TARGET_CLAUSE_MASK
,
36628 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36629 OMP_TARGET_CLAUSE_MASK
,
36633 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36634 OMP_TARGET_CLAUSE_MASK
,
36638 gcc_unreachable ();
36640 return stmt
!= NULL_TREE
;
36642 keep_next_level (true);
36643 tree sb
= begin_omp_structured_block (), ret
;
36644 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36648 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36649 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36653 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36654 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36658 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36659 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36663 gcc_unreachable ();
36665 cp_parser_end_omp_structured_block (parser
, save
);
36666 tree body
= finish_omp_structured_block (sb
);
36667 if (ret
== NULL_TREE
)
36669 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36671 /* For combined target teams, ensure the num_teams and
36672 thread_limit clause expressions are evaluated on the host,
36673 before entering the target construct. */
36675 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36676 c
; c
= OMP_CLAUSE_CHAIN (c
))
36677 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36678 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36679 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36681 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36682 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36683 if (expr
== error_mark_node
)
36685 tree tmp
= TARGET_EXPR_SLOT (expr
);
36687 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36688 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36689 OMP_CLAUSE_FIRSTPRIVATE
);
36690 OMP_CLAUSE_DECL (tc
) = tmp
;
36691 OMP_CLAUSE_CHAIN (tc
)
36692 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36693 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36696 tree stmt
= make_node (OMP_TARGET
);
36697 TREE_TYPE (stmt
) = void_type_node
;
36698 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36699 OMP_TARGET_BODY (stmt
) = body
;
36700 OMP_TARGET_COMBINED (stmt
) = 1;
36701 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36703 pc
= &OMP_TARGET_CLAUSES (stmt
);
36704 goto check_clauses
;
36706 else if (!flag_openmp
) /* flag_openmp_simd */
36708 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36711 else if (strcmp (p
, "data") == 0)
36713 cp_lexer_consume_token (parser
->lexer
);
36714 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36717 else if (strcmp (p
, "enter") == 0)
36719 cp_lexer_consume_token (parser
->lexer
);
36720 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36723 else if (strcmp (p
, "exit") == 0)
36725 cp_lexer_consume_token (parser
->lexer
);
36726 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36729 else if (strcmp (p
, "update") == 0)
36731 cp_lexer_consume_token (parser
->lexer
);
36732 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36735 if (!flag_openmp
) /* flag_openmp_simd */
36737 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36741 stmt
= make_node (OMP_TARGET
);
36742 TREE_TYPE (stmt
) = void_type_node
;
36744 OMP_TARGET_CLAUSES (stmt
)
36745 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36746 "#pragma omp target", pragma_tok
);
36747 pc
= &OMP_TARGET_CLAUSES (stmt
);
36748 keep_next_level (true);
36749 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36751 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36757 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36758 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36761 case GOMP_MAP_ALWAYS_TO
:
36762 case GOMP_MAP_FROM
:
36763 case GOMP_MAP_ALWAYS_FROM
:
36764 case GOMP_MAP_TOFROM
:
36765 case GOMP_MAP_ALWAYS_TOFROM
:
36766 case GOMP_MAP_ALLOC
:
36767 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36768 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36769 case GOMP_MAP_ALWAYS_POINTER
:
36772 error_at (OMP_CLAUSE_LOCATION (*pc
),
36773 "%<#pragma omp target%> with map-type other "
36774 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36775 "on %<map%> clause");
36776 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36779 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36785 # pragma acc cache (variable-list) new-line
36789 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36791 tree stmt
, clauses
;
36793 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36794 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36796 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36798 stmt
= make_node (OACC_CACHE
);
36799 TREE_TYPE (stmt
) = void_type_node
;
36800 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36801 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36808 # pragma acc data oacc-data-clause[optseq] new-line
36809 structured-block */
36811 #define OACC_DATA_CLAUSE_MASK \
36812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36821 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36823 tree stmt
, clauses
, block
;
36826 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36827 "#pragma acc data", pragma_tok
);
36829 block
= begin_omp_parallel ();
36830 save
= cp_parser_begin_omp_structured_block (parser
);
36831 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36832 cp_parser_end_omp_structured_block (parser
, save
);
36833 stmt
= finish_oacc_data (clauses
, block
);
36838 # pragma acc host_data <clauses> new-line
36839 structured-block */
36841 #define OACC_HOST_DATA_CLAUSE_MASK \
36842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36845 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36847 tree stmt
, clauses
, block
;
36850 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36851 "#pragma acc host_data", pragma_tok
);
36853 block
= begin_omp_parallel ();
36854 save
= cp_parser_begin_omp_structured_block (parser
);
36855 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36856 cp_parser_end_omp_structured_block (parser
, save
);
36857 stmt
= finish_oacc_host_data (clauses
, block
);
36862 # pragma acc declare oacc-data-clause[optseq] new-line
36865 #define OACC_DECLARE_CLAUSE_MASK \
36866 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36876 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36878 tree clauses
, stmt
;
36879 bool error
= false;
36881 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36882 "#pragma acc declare", pragma_tok
, true);
36885 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36887 error_at (pragma_tok
->location
,
36888 "no valid clauses specified in %<#pragma acc declare%>");
36892 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36894 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36895 tree decl
= OMP_CLAUSE_DECL (t
);
36896 if (!DECL_P (decl
))
36898 error_at (loc
, "array section in %<#pragma acc declare%>");
36902 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36903 switch (OMP_CLAUSE_MAP_KIND (t
))
36905 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36906 case GOMP_MAP_ALLOC
:
36908 case GOMP_MAP_FORCE_DEVICEPTR
:
36909 case GOMP_MAP_DEVICE_RESIDENT
:
36912 case GOMP_MAP_LINK
:
36913 if (!global_bindings_p ()
36914 && (TREE_STATIC (decl
)
36915 || !DECL_EXTERNAL (decl
)))
36918 "%qD must be a global variable in "
36919 "%<#pragma acc declare link%>",
36927 if (global_bindings_p ())
36929 error_at (loc
, "invalid OpenACC clause at file scope");
36933 if (DECL_EXTERNAL (decl
))
36936 "invalid use of %<extern%> variable %qD "
36937 "in %<#pragma acc declare%>", decl
);
36941 else if (TREE_PUBLIC (decl
))
36944 "invalid use of %<global%> variable %qD "
36945 "in %<#pragma acc declare%>", decl
);
36952 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36953 || lookup_attribute ("omp declare target link",
36954 DECL_ATTRIBUTES (decl
)))
36956 error_at (loc
, "variable %qD used more than once with "
36957 "%<#pragma acc declare%>", decl
);
36966 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36967 id
= get_identifier ("omp declare target link");
36969 id
= get_identifier ("omp declare target");
36971 DECL_ATTRIBUTES (decl
)
36972 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36973 if (global_bindings_p ())
36975 symtab_node
*node
= symtab_node::get (decl
);
36978 node
->offloadable
= 1;
36979 if (ENABLE_OFFLOADING
)
36981 g
->have_offload
= true;
36982 if (is_a
<varpool_node
*> (node
))
36983 vec_safe_push (offload_vars
, decl
);
36990 if (error
|| global_bindings_p ())
36993 stmt
= make_node (OACC_DECLARE
);
36994 TREE_TYPE (stmt
) = void_type_node
;
36995 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36996 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37004 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37008 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37010 LOC is the location of the #pragma token.
37013 #define OACC_ENTER_DATA_CLAUSE_MASK \
37014 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37020 #define OACC_EXIT_DATA_CLAUSE_MASK \
37021 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37029 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37032 location_t loc
= pragma_tok
->location
;
37033 tree stmt
, clauses
;
37034 const char *p
= "";
37036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37037 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37039 if (strcmp (p
, "data") != 0)
37041 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37042 enter
? "enter" : "exit");
37043 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37047 cp_lexer_consume_token (parser
->lexer
);
37050 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37051 "#pragma acc enter data", pragma_tok
);
37053 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37054 "#pragma acc exit data", pragma_tok
);
37056 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37058 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37059 enter
? "enter" : "exit");
37063 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37064 TREE_TYPE (stmt
) = void_type_node
;
37065 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37066 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37072 # pragma acc loop oacc-loop-clause[optseq] new-line
37073 structured-block */
37075 #define OACC_LOOP_CLAUSE_MASK \
37076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37088 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37089 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37091 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37093 strcat (p_name
, " loop");
37094 mask
|= OACC_LOOP_CLAUSE_MASK
;
37096 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37100 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37102 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37104 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37107 tree block
= begin_omp_structured_block ();
37108 int save
= cp_parser_begin_omp_structured_block (parser
);
37109 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37110 cp_parser_end_omp_structured_block (parser
, save
);
37111 add_stmt (finish_omp_structured_block (block
));
37117 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37122 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37126 #define OACC_KERNELS_CLAUSE_MASK \
37127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37141 #define OACC_PARALLEL_CLAUSE_MASK \
37142 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37160 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37161 char *p_name
, bool *if_p
)
37163 omp_clause_mask mask
;
37164 enum tree_code code
;
37165 switch (cp_parser_pragma_kind (pragma_tok
))
37167 case PRAGMA_OACC_KERNELS
:
37168 strcat (p_name
, " kernels");
37169 mask
= OACC_KERNELS_CLAUSE_MASK
;
37170 code
= OACC_KERNELS
;
37172 case PRAGMA_OACC_PARALLEL
:
37173 strcat (p_name
, " parallel");
37174 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37175 code
= OACC_PARALLEL
;
37178 gcc_unreachable ();
37181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37184 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37185 if (strcmp (p
, "loop") == 0)
37187 cp_lexer_consume_token (parser
->lexer
);
37188 tree block
= begin_omp_parallel ();
37190 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37192 return finish_omp_construct (code
, block
, clauses
);
37196 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37198 tree block
= begin_omp_parallel ();
37199 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37200 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37201 cp_parser_end_omp_structured_block (parser
, save
);
37202 return finish_omp_construct (code
, block
, clauses
);
37206 # pragma acc update oacc-update-clause[optseq] new-line
37209 #define OACC_UPDATE_CLAUSE_MASK \
37210 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37218 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37220 tree stmt
, clauses
;
37222 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37223 "#pragma acc update", pragma_tok
);
37225 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37227 error_at (pragma_tok
->location
,
37228 "%<#pragma acc update%> must contain at least one "
37229 "%<device%> or %<host%> or %<self%> clause");
37233 stmt
= make_node (OACC_UPDATE
);
37234 TREE_TYPE (stmt
) = void_type_node
;
37235 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37236 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37242 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37244 LOC is the location of the #pragma token.
37247 #define OACC_WAIT_CLAUSE_MASK \
37248 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37251 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37253 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37254 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37256 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37257 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37259 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37260 "#pragma acc wait", pragma_tok
);
37262 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37263 stmt
= finish_expr_stmt (stmt
);
37269 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37271 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37272 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37280 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37281 enum pragma_context context
)
37283 bool first_p
= parser
->omp_declare_simd
== NULL
;
37284 cp_omp_declare_simd_data data
;
37287 data
.error_seen
= false;
37288 data
.fndecl_seen
= false;
37289 data
.tokens
= vNULL
;
37290 data
.clauses
= NULL_TREE
;
37291 /* It is safe to take the address of a local variable; it will only be
37292 used while this scope is live. */
37293 parser
->omp_declare_simd
= &data
;
37296 /* Store away all pragma tokens. */
37297 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37298 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37299 cp_lexer_consume_token (parser
->lexer
);
37300 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37301 parser
->omp_declare_simd
->error_seen
= true;
37302 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37303 struct cp_token_cache
*cp
37304 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37305 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37309 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37310 cp_parser_pragma (parser
, context
, NULL
);
37313 case pragma_external
:
37314 cp_parser_declaration (parser
);
37316 case pragma_member
:
37317 cp_parser_member_declaration (parser
);
37319 case pragma_objc_icode
:
37320 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37323 cp_parser_declaration_statement (parser
);
37326 if (parser
->omp_declare_simd
37327 && !parser
->omp_declare_simd
->error_seen
37328 && !parser
->omp_declare_simd
->fndecl_seen
)
37329 error_at (pragma_tok
->location
,
37330 "%<#pragma omp declare simd%> not immediately followed by "
37331 "function declaration or definition");
37332 data
.tokens
.release ();
37333 parser
->omp_declare_simd
= NULL
;
37337 /* Finalize #pragma omp declare simd clauses after direct declarator has
37338 been parsed, and put that into "omp declare simd" attribute. */
37341 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37343 struct cp_token_cache
*ce
;
37344 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37347 if (!data
->error_seen
&& data
->fndecl_seen
)
37349 error ("%<#pragma omp declare simd%> not immediately followed by "
37350 "a single function declaration or definition");
37351 data
->error_seen
= true;
37353 if (data
->error_seen
)
37356 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37360 cp_parser_push_lexer_for_tokens (parser
, ce
);
37361 parser
->lexer
->in_pragma
= true;
37362 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37363 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37364 cp_lexer_consume_token (parser
->lexer
);
37365 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37366 "#pragma omp declare simd", pragma_tok
);
37367 cp_parser_pop_lexer (parser
);
37369 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37370 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37371 TREE_CHAIN (c
) = attrs
;
37372 if (processing_template_decl
)
37373 ATTR_IS_DEPENDENT (c
) = 1;
37377 data
->fndecl_seen
= true;
37383 # pragma omp declare target new-line
37384 declarations and definitions
37385 # pragma omp end declare target new-line
37388 # pragma omp declare target ( extended-list ) new-line
37390 # pragma omp declare target declare-target-clauses[seq] new-line */
37392 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37397 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37399 tree clauses
= NULL_TREE
;
37400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37402 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37403 "#pragma omp declare target", pragma_tok
);
37404 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37406 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37408 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37409 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37413 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37414 scope_chain
->omp_declare_target_attribute
++;
37417 if (scope_chain
->omp_declare_target_attribute
)
37418 error_at (pragma_tok
->location
,
37419 "%<#pragma omp declare target%> with clauses in between "
37420 "%<#pragma omp declare target%> without clauses and "
37421 "%<#pragma omp end declare target%>");
37422 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37424 tree t
= OMP_CLAUSE_DECL (c
), id
;
37425 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37426 tree at2
= lookup_attribute ("omp declare target link",
37427 DECL_ATTRIBUTES (t
));
37428 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37430 id
= get_identifier ("omp declare target link");
37431 std::swap (at1
, at2
);
37434 id
= get_identifier ("omp declare target");
37437 error_at (OMP_CLAUSE_LOCATION (c
),
37438 "%qD specified both in declare target %<link%> and %<to%>"
37444 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37445 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37448 symtab_node
*node
= symtab_node::get (t
);
37451 node
->offloadable
= 1;
37452 if (ENABLE_OFFLOADING
)
37454 g
->have_offload
= true;
37455 if (is_a
<varpool_node
*> (node
))
37456 vec_safe_push (offload_vars
, t
);
37464 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37466 const char *p
= "";
37467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37469 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37470 p
= IDENTIFIER_POINTER (id
);
37472 if (strcmp (p
, "declare") == 0)
37474 cp_lexer_consume_token (parser
->lexer
);
37476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37478 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37479 p
= IDENTIFIER_POINTER (id
);
37481 if (strcmp (p
, "target") == 0)
37482 cp_lexer_consume_token (parser
->lexer
);
37485 cp_parser_error (parser
, "expected %<target%>");
37486 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37492 cp_parser_error (parser
, "expected %<declare%>");
37493 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37496 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37497 if (!scope_chain
->omp_declare_target_attribute
)
37498 error_at (pragma_tok
->location
,
37499 "%<#pragma omp end declare target%> without corresponding "
37500 "%<#pragma omp declare target%>");
37502 scope_chain
->omp_declare_target_attribute
--;
37505 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37506 expression and optional initializer clause of
37507 #pragma omp declare reduction. We store the expression(s) as
37508 either 3, 6 or 7 special statements inside of the artificial function's
37509 body. The first two statements are DECL_EXPRs for the artificial
37510 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37511 expression that uses those variables.
37512 If there was any INITIALIZER clause, this is followed by further statements,
37513 the fourth and fifth statements are DECL_EXPRs for the artificial
37514 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37515 constructor variant (first token after open paren is not omp_priv),
37516 then the sixth statement is a statement with the function call expression
37517 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37518 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37519 to initialize the OMP_PRIV artificial variable and there is seventh
37520 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37523 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37525 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37526 gcc_assert (TYPE_REF_P (type
));
37527 type
= TREE_TYPE (type
);
37528 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37529 DECL_ARTIFICIAL (omp_out
) = 1;
37530 pushdecl (omp_out
);
37531 add_decl_expr (omp_out
);
37532 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37533 DECL_ARTIFICIAL (omp_in
) = 1;
37535 add_decl_expr (omp_in
);
37537 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37539 keep_next_level (true);
37540 tree block
= begin_omp_structured_block ();
37541 combiner
= cp_parser_expression (parser
);
37542 finish_expr_stmt (combiner
);
37543 block
= finish_omp_structured_block (block
);
37546 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37549 const char *p
= "";
37550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37552 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37553 p
= IDENTIFIER_POINTER (id
);
37556 if (strcmp (p
, "initializer") == 0)
37558 cp_lexer_consume_token (parser
->lexer
);
37559 matching_parens parens
;
37560 if (!parens
.require_open (parser
))
37564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37566 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37567 p
= IDENTIFIER_POINTER (id
);
37570 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37571 DECL_ARTIFICIAL (omp_priv
) = 1;
37572 pushdecl (omp_priv
);
37573 add_decl_expr (omp_priv
);
37574 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37575 DECL_ARTIFICIAL (omp_orig
) = 1;
37576 pushdecl (omp_orig
);
37577 add_decl_expr (omp_orig
);
37579 keep_next_level (true);
37580 block
= begin_omp_structured_block ();
37583 if (strcmp (p
, "omp_priv") == 0)
37585 bool is_direct_init
, is_non_constant_init
;
37587 cp_lexer_consume_token (parser
->lexer
);
37588 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37590 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37591 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37593 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37594 == CPP_CLOSE_PAREN
))
37596 finish_omp_structured_block (block
);
37597 error ("invalid initializer clause");
37600 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37601 &is_non_constant_init
);
37602 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37603 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37607 cp_parser_parse_tentatively (parser
);
37608 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37609 /*check_dependency_p=*/true,
37610 /*template_p=*/NULL
,
37611 /*declarator_p=*/false,
37612 /*optional_p=*/false);
37613 vec
<tree
, va_gc
> *args
;
37614 if (fn_name
== error_mark_node
37615 || cp_parser_error_occurred (parser
)
37616 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37617 || ((args
= cp_parser_parenthesized_expression_list
37618 (parser
, non_attr
, /*cast_p=*/false,
37619 /*allow_expansion_p=*/true,
37620 /*non_constant_p=*/NULL
)),
37621 cp_parser_error_occurred (parser
)))
37623 finish_omp_structured_block (block
);
37624 cp_parser_abort_tentative_parse (parser
);
37625 cp_parser_error (parser
, "expected id-expression (arguments)");
37630 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37631 if (arg
== omp_priv
37632 || (TREE_CODE (arg
) == ADDR_EXPR
37633 && TREE_OPERAND (arg
, 0) == omp_priv
))
37635 cp_parser_abort_tentative_parse (parser
);
37636 if (arg
== NULL_TREE
)
37637 error ("one of the initializer call arguments should be %<omp_priv%>"
37638 " or %<&omp_priv%>");
37639 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37641 finish_expr_stmt (initializer
);
37644 block
= finish_omp_structured_block (block
);
37645 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37649 add_decl_expr (omp_orig
);
37651 if (!parens
.require_close (parser
))
37655 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37656 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37663 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37664 initializer-clause[opt] new-line
37666 initializer-clause:
37667 initializer (omp_priv initializer)
37668 initializer (function-name (argument-list)) */
37671 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37672 enum pragma_context
)
37674 auto_vec
<tree
> types
;
37675 enum tree_code reduc_code
= ERROR_MARK
;
37676 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37678 cp_token
*first_token
;
37679 cp_token_cache
*cp
;
37683 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37684 p
= obstack_alloc (&declarator_obstack
, 0);
37686 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37689 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37692 reduc_code
= PLUS_EXPR
;
37695 reduc_code
= MULT_EXPR
;
37698 reduc_code
= MINUS_EXPR
;
37701 reduc_code
= BIT_AND_EXPR
;
37704 reduc_code
= BIT_XOR_EXPR
;
37707 reduc_code
= BIT_IOR_EXPR
;
37710 reduc_code
= TRUTH_ANDIF_EXPR
;
37713 reduc_code
= TRUTH_ORIF_EXPR
;
37716 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37719 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37720 "%<|%>, %<&&%>, %<||%> or identifier");
37724 if (reduc_code
!= ERROR_MARK
)
37725 cp_lexer_consume_token (parser
->lexer
);
37727 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37728 if (reduc_id
== error_mark_node
)
37731 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37734 /* Types may not be defined in declare reduction type list. */
37735 const char *saved_message
;
37736 saved_message
= parser
->type_definition_forbidden_message
;
37737 parser
->type_definition_forbidden_message
37738 = G_("types may not be defined in declare reduction type list");
37739 bool saved_colon_corrects_to_scope_p
;
37740 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37741 parser
->colon_corrects_to_scope_p
= false;
37742 bool saved_colon_doesnt_start_class_def_p
;
37743 saved_colon_doesnt_start_class_def_p
37744 = parser
->colon_doesnt_start_class_def_p
;
37745 parser
->colon_doesnt_start_class_def_p
= true;
37749 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37750 type
= cp_parser_type_id (parser
);
37751 if (type
== error_mark_node
)
37753 else if (ARITHMETIC_TYPE_P (type
)
37754 && (orig_reduc_id
== NULL_TREE
37755 || (TREE_CODE (type
) != COMPLEX_TYPE
37756 && (id_equal (orig_reduc_id
, "min")
37757 || id_equal (orig_reduc_id
, "max")))))
37758 error_at (loc
, "predeclared arithmetic type %qT in "
37759 "%<#pragma omp declare reduction%>", type
);
37760 else if (TREE_CODE (type
) == FUNCTION_TYPE
37761 || TREE_CODE (type
) == METHOD_TYPE
37762 || TREE_CODE (type
) == ARRAY_TYPE
)
37763 error_at (loc
, "function or array type %qT in "
37764 "%<#pragma omp declare reduction%>", type
);
37765 else if (TYPE_REF_P (type
))
37766 error_at (loc
, "reference type %qT in "
37767 "%<#pragma omp declare reduction%>", type
);
37768 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37769 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37770 "%<#pragma omp declare reduction%>", type
);
37772 types
.safe_push (type
);
37774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37775 cp_lexer_consume_token (parser
->lexer
);
37780 /* Restore the saved message. */
37781 parser
->type_definition_forbidden_message
= saved_message
;
37782 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37783 parser
->colon_doesnt_start_class_def_p
37784 = saved_colon_doesnt_start_class_def_p
;
37786 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37787 || types
.is_empty ())
37790 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37794 first_token
= cp_lexer_peek_token (parser
->lexer
);
37797 FOR_EACH_VEC_ELT (types
, i
, type
)
37800 = build_function_type_list (void_type_node
,
37801 cp_build_reference_type (type
, false),
37803 tree this_reduc_id
= reduc_id
;
37804 if (!dependent_type_p (type
))
37805 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37806 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37807 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37808 DECL_ARTIFICIAL (fndecl
) = 1;
37809 DECL_EXTERNAL (fndecl
) = 1;
37810 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37811 DECL_IGNORED_P (fndecl
) = 1;
37812 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37813 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37814 DECL_ATTRIBUTES (fndecl
)
37815 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37816 DECL_ATTRIBUTES (fndecl
));
37817 if (processing_template_decl
)
37818 fndecl
= push_template_decl (fndecl
);
37819 bool block_scope
= false;
37820 tree block
= NULL_TREE
;
37821 if (current_function_decl
)
37823 block_scope
= true;
37824 DECL_CONTEXT (fndecl
) = global_namespace
;
37825 if (!processing_template_decl
)
37828 else if (current_class_type
)
37832 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37833 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37834 cp_lexer_consume_token (parser
->lexer
);
37835 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37837 cp
= cp_token_cache_new (first_token
,
37838 cp_lexer_peek_nth_token (parser
->lexer
,
37841 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37842 finish_member_declaration (fndecl
);
37843 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37844 DECL_PENDING_INLINE_P (fndecl
) = 1;
37845 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37850 DECL_CONTEXT (fndecl
) = current_namespace
;
37854 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37856 block
= begin_omp_structured_block ();
37859 cp_parser_push_lexer_for_tokens (parser
, cp
);
37860 parser
->lexer
->in_pragma
= true;
37862 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37865 finish_function (/*inline_p=*/false);
37867 DECL_CONTEXT (fndecl
) = current_function_decl
;
37869 cp_parser_pop_lexer (parser
);
37873 cp_parser_pop_lexer (parser
);
37875 finish_function (/*inline_p=*/false);
37878 DECL_CONTEXT (fndecl
) = current_function_decl
;
37879 block
= finish_omp_structured_block (block
);
37880 if (TREE_CODE (block
) == BIND_EXPR
)
37881 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37882 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37883 DECL_SAVED_TREE (fndecl
) = block
;
37884 if (processing_template_decl
)
37885 add_decl_expr (fndecl
);
37887 cp_check_omp_declare_reduction (fndecl
);
37888 if (cp
== NULL
&& types
.length () > 1)
37889 cp
= cp_token_cache_new (first_token
,
37890 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37891 if (errs
!= errorcount
)
37895 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37898 /* Free any declarators allocated. */
37899 obstack_free (&declarator_obstack
, p
);
37903 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37904 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37905 initializer-clause[opt] new-line
37906 #pragma omp declare target new-line */
37909 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37910 enum pragma_context context
)
37912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37914 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37915 const char *p
= IDENTIFIER_POINTER (id
);
37917 if (strcmp (p
, "simd") == 0)
37919 cp_lexer_consume_token (parser
->lexer
);
37920 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37924 cp_ensure_no_omp_declare_simd (parser
);
37925 if (strcmp (p
, "reduction") == 0)
37927 cp_lexer_consume_token (parser
->lexer
);
37928 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37932 if (!flag_openmp
) /* flag_openmp_simd */
37934 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37937 if (strcmp (p
, "target") == 0)
37939 cp_lexer_consume_token (parser
->lexer
);
37940 cp_parser_omp_declare_target (parser
, pragma_tok
);
37944 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37946 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37951 #pragma omp taskloop taskloop-clause[optseq] new-line
37954 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37957 #define OMP_TASKLOOP_CLAUSE_MASK \
37958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37974 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37975 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37978 tree clauses
, sb
, ret
;
37980 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37982 strcat (p_name
, " taskloop");
37983 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37987 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37988 const char *p
= IDENTIFIER_POINTER (id
);
37990 if (strcmp (p
, "simd") == 0)
37992 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37993 if (cclauses
== NULL
)
37994 cclauses
= cclauses_buf
;
37996 cp_lexer_consume_token (parser
->lexer
);
37997 if (!flag_openmp
) /* flag_openmp_simd */
37998 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38000 sb
= begin_omp_structured_block ();
38001 save
= cp_parser_begin_omp_structured_block (parser
);
38002 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38004 cp_parser_end_omp_structured_block (parser
, save
);
38005 tree body
= finish_omp_structured_block (sb
);
38008 ret
= make_node (OMP_TASKLOOP
);
38009 TREE_TYPE (ret
) = void_type_node
;
38010 OMP_FOR_BODY (ret
) = body
;
38011 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38012 SET_EXPR_LOCATION (ret
, loc
);
38017 if (!flag_openmp
) /* flag_openmp_simd */
38019 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38023 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38027 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38028 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38031 sb
= begin_omp_structured_block ();
38032 save
= cp_parser_begin_omp_structured_block (parser
);
38034 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38037 cp_parser_end_omp_structured_block (parser
, save
);
38038 add_stmt (finish_omp_structured_block (sb
));
38045 # pragma acc routine oacc-routine-clause[optseq] new-line
38046 function-definition
38048 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38051 #define OACC_ROUTINE_CLAUSE_MASK \
38052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38058 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38059 component, which must resolve to a declared namespace-scope
38060 function. The clauses are either processed directly (for a named
38061 function), or defered until the immediatley following declaration
38065 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38066 enum pragma_context context
)
38068 gcc_checking_assert (context
== pragma_external
);
38069 /* The checking for "another pragma following this one" in the "no optional
38070 '( name )'" case makes sure that we dont re-enter. */
38071 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38073 cp_oacc_routine_data data
;
38074 data
.error_seen
= false;
38075 data
.fndecl_seen
= false;
38076 data
.tokens
= vNULL
;
38077 data
.clauses
= NULL_TREE
;
38078 data
.loc
= pragma_tok
->location
;
38079 /* It is safe to take the address of a local variable; it will only be
38080 used while this scope is live. */
38081 parser
->oacc_routine
= &data
;
38083 /* Look for optional '( name )'. */
38084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38086 matching_parens parens
;
38087 parens
.consume_open (parser
); /* '(' */
38089 /* We parse the name as an id-expression. If it resolves to
38090 anything other than a non-overloaded function at namespace
38091 scope, it's an error. */
38092 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38093 tree name
= cp_parser_id_expression (parser
,
38094 /*template_keyword_p=*/false,
38095 /*check_dependency_p=*/false,
38096 /*template_p=*/NULL
,
38097 /*declarator_p=*/false,
38098 /*optional_p=*/false);
38099 tree decl
= (identifier_p (name
)
38100 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38102 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38103 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38105 if (decl
== error_mark_node
38106 || !parens
.require_close (parser
))
38108 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38109 parser
->oacc_routine
= NULL
;
38114 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38115 "#pragma acc routine",
38116 cp_lexer_peek_token (parser
->lexer
));
38118 if (decl
&& is_overloaded_fn (decl
)
38119 && (TREE_CODE (decl
) != FUNCTION_DECL
38120 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38122 error_at (name_loc
,
38123 "%<#pragma acc routine%> names a set of overloads");
38124 parser
->oacc_routine
= NULL
;
38128 /* Perhaps we should use the same rule as declarations in different
38130 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38132 error_at (name_loc
,
38133 "%qD does not refer to a namespace scope function", decl
);
38134 parser
->oacc_routine
= NULL
;
38138 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38140 error_at (name_loc
, "%qD does not refer to a function", decl
);
38141 parser
->oacc_routine
= NULL
;
38145 cp_finalize_oacc_routine (parser
, decl
, false);
38146 parser
->oacc_routine
= NULL
;
38148 else /* No optional '( name )'. */
38150 /* Store away all pragma tokens. */
38151 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38152 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38153 cp_lexer_consume_token (parser
->lexer
);
38154 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38155 parser
->oacc_routine
->error_seen
= true;
38156 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38157 struct cp_token_cache
*cp
38158 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38159 parser
->oacc_routine
->tokens
.safe_push (cp
);
38161 /* Emit a helpful diagnostic if there's another pragma following this
38163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38165 cp_ensure_no_oacc_routine (parser
);
38166 data
.tokens
.release ();
38167 /* ..., and then just keep going. */
38171 /* We only have to consider the pragma_external case here. */
38172 cp_parser_declaration (parser
);
38173 if (parser
->oacc_routine
38174 && !parser
->oacc_routine
->fndecl_seen
)
38175 cp_ensure_no_oacc_routine (parser
);
38177 parser
->oacc_routine
= NULL
;
38178 data
.tokens
.release ();
38182 /* Finalize #pragma acc routine clauses after direct declarator has
38186 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38188 struct cp_token_cache
*ce
;
38189 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38191 if (!data
->error_seen
&& data
->fndecl_seen
)
38193 error_at (data
->loc
,
38194 "%<#pragma acc routine%> not immediately followed by "
38195 "a single function declaration or definition");
38196 data
->error_seen
= true;
38198 if (data
->error_seen
)
38201 gcc_checking_assert (data
->tokens
.length () == 1);
38202 ce
= data
->tokens
[0];
38204 cp_parser_push_lexer_for_tokens (parser
, ce
);
38205 parser
->lexer
->in_pragma
= true;
38206 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38208 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38209 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38210 parser
->oacc_routine
->clauses
38211 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38212 "#pragma acc routine", pragma_tok
);
38213 cp_parser_pop_lexer (parser
);
38214 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38220 /* Apply any saved OpenACC routine clauses to a just-parsed
38224 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38226 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38228 /* Keep going if we're in error reporting mode. */
38229 if (parser
->oacc_routine
->error_seen
38230 || fndecl
== error_mark_node
)
38233 if (parser
->oacc_routine
->fndecl_seen
)
38235 error_at (parser
->oacc_routine
->loc
,
38236 "%<#pragma acc routine%> not immediately followed by"
38237 " a single function declaration or definition");
38238 parser
->oacc_routine
= NULL
;
38241 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38243 cp_ensure_no_oacc_routine (parser
);
38247 if (oacc_get_fn_attrib (fndecl
))
38249 error_at (parser
->oacc_routine
->loc
,
38250 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38251 parser
->oacc_routine
= NULL
;
38255 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38257 error_at (parser
->oacc_routine
->loc
,
38259 ? G_("%<#pragma acc routine%> must be applied before use")
38260 : G_("%<#pragma acc routine%> must be applied before "
38262 parser
->oacc_routine
= NULL
;
38266 /* Process the routine's dimension clauses. */
38267 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38268 oacc_replace_fn_attrib (fndecl
, dims
);
38270 /* Add an "omp declare target" attribute. */
38271 DECL_ATTRIBUTES (fndecl
)
38272 = tree_cons (get_identifier ("omp declare target"),
38273 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38275 /* Don't unset parser->oacc_routine here: we may still need it to
38276 diagnose wrong usage. But, remember that we've used this "#pragma acc
38278 parser
->oacc_routine
->fndecl_seen
= true;
38282 /* Main entry point to OpenMP statement pragmas. */
38285 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38288 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38289 omp_clause_mask
mask (0);
38291 switch (cp_parser_pragma_kind (pragma_tok
))
38293 case PRAGMA_OACC_ATOMIC
:
38294 cp_parser_omp_atomic (parser
, pragma_tok
);
38296 case PRAGMA_OACC_CACHE
:
38297 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38299 case PRAGMA_OACC_DATA
:
38300 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38302 case PRAGMA_OACC_ENTER_DATA
:
38303 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38305 case PRAGMA_OACC_EXIT_DATA
:
38306 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38308 case PRAGMA_OACC_HOST_DATA
:
38309 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38311 case PRAGMA_OACC_KERNELS
:
38312 case PRAGMA_OACC_PARALLEL
:
38313 strcpy (p_name
, "#pragma acc");
38314 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38317 case PRAGMA_OACC_LOOP
:
38318 strcpy (p_name
, "#pragma acc");
38319 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38322 case PRAGMA_OACC_UPDATE
:
38323 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38325 case PRAGMA_OACC_WAIT
:
38326 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38328 case PRAGMA_OMP_ATOMIC
:
38329 cp_parser_omp_atomic (parser
, pragma_tok
);
38331 case PRAGMA_OMP_CRITICAL
:
38332 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38334 case PRAGMA_OMP_DISTRIBUTE
:
38335 strcpy (p_name
, "#pragma omp");
38336 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38339 case PRAGMA_OMP_FOR
:
38340 strcpy (p_name
, "#pragma omp");
38341 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38344 case PRAGMA_OMP_MASTER
:
38345 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38347 case PRAGMA_OMP_PARALLEL
:
38348 strcpy (p_name
, "#pragma omp");
38349 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38352 case PRAGMA_OMP_SECTIONS
:
38353 strcpy (p_name
, "#pragma omp");
38354 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38356 case PRAGMA_OMP_SIMD
:
38357 strcpy (p_name
, "#pragma omp");
38358 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38361 case PRAGMA_OMP_SINGLE
:
38362 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38364 case PRAGMA_OMP_TASK
:
38365 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38367 case PRAGMA_OMP_TASKGROUP
:
38368 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38370 case PRAGMA_OMP_TASKLOOP
:
38371 strcpy (p_name
, "#pragma omp");
38372 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38375 case PRAGMA_OMP_TEAMS
:
38376 strcpy (p_name
, "#pragma omp");
38377 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38381 gcc_unreachable ();
38384 protected_set_expr_location (stmt
, pragma_tok
->location
);
38387 /* Transactional Memory parsing routines. */
38389 /* Parse a transaction attribute.
38395 We use this instead of cp_parser_attributes_opt for transactions to avoid
38396 the pedwarn in C++98 mode. */
38399 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38402 tree attr_name
, attr
= NULL
;
38404 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38405 return cp_parser_attributes_opt (parser
);
38407 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38409 cp_lexer_consume_token (parser
->lexer
);
38410 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38413 token
= cp_lexer_peek_token (parser
->lexer
);
38414 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38416 token
= cp_lexer_consume_token (parser
->lexer
);
38418 attr_name
= (token
->type
== CPP_KEYWORD
38419 /* For keywords, use the canonical spelling,
38420 not the parsed identifier. */
38421 ? ridpointers
[(int) token
->keyword
]
38423 attr
= build_tree_list (attr_name
, NULL_TREE
);
38426 cp_parser_error (parser
, "expected identifier");
38428 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38430 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38434 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38436 transaction-statement:
38437 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38439 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38443 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38445 unsigned char old_in
= parser
->in_transaction
;
38446 unsigned char this_in
= 1, new_in
;
38447 enum rid keyword
= token
->keyword
;
38448 tree stmt
, attrs
, noex
;
38450 cp_lexer_consume_token (parser
->lexer
);
38452 if (keyword
== RID_TRANSACTION_RELAXED
38453 || keyword
== RID_SYNCHRONIZED
)
38454 this_in
|= TM_STMT_ATTR_RELAXED
;
38457 attrs
= cp_parser_txn_attribute_opt (parser
);
38459 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38462 /* Parse a noexcept specification. */
38463 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38464 noex
= boolean_true_node
;
38465 else if (keyword
== RID_ATOMIC_CANCEL
)
38467 /* cancel-and-throw is unimplemented. */
38468 sorry ("atomic_cancel");
38472 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38474 /* Keep track if we're in the lexical scope of an outer transaction. */
38475 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38477 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38479 parser
->in_transaction
= new_in
;
38480 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38481 parser
->in_transaction
= old_in
;
38483 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38488 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38490 transaction-expression:
38491 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38492 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38496 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38498 unsigned char old_in
= parser
->in_transaction
;
38499 unsigned char this_in
= 1;
38503 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38505 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38506 || keyword
== RID_TRANSACTION_RELAXED
);
38510 keyword
== RID_TRANSACTION_RELAXED
38511 ? G_("%<__transaction_relaxed%> without transactional memory "
38513 : G_("%<__transaction_atomic%> without transactional memory "
38514 "support enabled"));
38516 token
= cp_parser_require_keyword (parser
, keyword
,
38517 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38518 : RT_TRANSACTION_RELAXED
));
38519 gcc_assert (token
!= NULL
);
38521 if (keyword
== RID_TRANSACTION_RELAXED
)
38522 this_in
|= TM_STMT_ATTR_RELAXED
;
38524 /* Set this early. This might mean that we allow transaction_cancel in
38525 an expression that we find out later actually has to be a constexpr.
38526 However, we expect that cxx_constant_value will be able to deal with
38527 this; also, if the noexcept has no constexpr, then what we parse next
38528 really is a transaction's body. */
38529 parser
->in_transaction
= this_in
;
38531 /* Parse a noexcept specification. */
38532 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38535 if (!noex
|| !noex_expr
38536 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38538 matching_parens parens
;
38539 parens
.require_open (parser
);
38541 expr
= cp_parser_expression (parser
);
38542 expr
= finish_parenthesized_expr (expr
);
38544 parens
.require_close (parser
);
38548 /* The only expression that is available got parsed for the noexcept
38549 already. noexcept is true then. */
38551 noex
= boolean_true_node
;
38554 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38555 parser
->in_transaction
= old_in
;
38557 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38558 return error_mark_node
;
38560 return (flag_tm
? expr
: error_mark_node
);
38563 /* Parse a function-transaction-block.
38565 function-transaction-block:
38566 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38568 __transaction_atomic txn-attribute[opt] function-try-block
38569 __transaction_relaxed ctor-initializer[opt] function-body
38570 __transaction_relaxed function-try-block
38574 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38576 unsigned char old_in
= parser
->in_transaction
;
38577 unsigned char new_in
= 1;
38578 tree compound_stmt
, stmt
, attrs
;
38581 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38582 || keyword
== RID_TRANSACTION_RELAXED
);
38583 token
= cp_parser_require_keyword (parser
, keyword
,
38584 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38585 : RT_TRANSACTION_RELAXED
));
38586 gcc_assert (token
!= NULL
);
38588 if (keyword
== RID_TRANSACTION_RELAXED
)
38589 new_in
|= TM_STMT_ATTR_RELAXED
;
38592 attrs
= cp_parser_txn_attribute_opt (parser
);
38594 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38597 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38599 parser
->in_transaction
= new_in
;
38601 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38602 cp_parser_function_try_block (parser
);
38604 cp_parser_ctor_initializer_opt_and_function_body
38605 (parser
, /*in_function_try_block=*/false);
38607 parser
->in_transaction
= old_in
;
38609 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38612 /* Parse a __transaction_cancel statement.
38615 __transaction_cancel txn-attribute[opt] ;
38616 __transaction_cancel txn-attribute[opt] throw-expression ;
38618 ??? Cancel and throw is not yet implemented. */
38621 cp_parser_transaction_cancel (cp_parser
*parser
)
38624 bool is_outer
= false;
38627 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38628 RT_TRANSACTION_CANCEL
);
38629 gcc_assert (token
!= NULL
);
38631 attrs
= cp_parser_txn_attribute_opt (parser
);
38633 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38635 /* ??? Parse cancel-and-throw here. */
38637 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38641 error_at (token
->location
, "%<__transaction_cancel%> without "
38642 "transactional memory support enabled");
38643 return error_mark_node
;
38645 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38647 error_at (token
->location
, "%<__transaction_cancel%> within a "
38648 "%<__transaction_relaxed%>");
38649 return error_mark_node
;
38653 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38654 && !is_tm_may_cancel_outer (current_function_decl
))
38656 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38657 "within outer %<__transaction_atomic%>");
38658 error_at (token
->location
,
38659 " or a %<transaction_may_cancel_outer%> function");
38660 return error_mark_node
;
38663 else if (parser
->in_transaction
== 0)
38665 error_at (token
->location
, "%<__transaction_cancel%> not within "
38666 "%<__transaction_atomic%>");
38667 return error_mark_node
;
38670 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38678 static GTY (()) cp_parser
*the_parser
;
38681 /* Special handling for the first token or line in the file. The first
38682 thing in the file might be #pragma GCC pch_preprocess, which loads a
38683 PCH file, which is a GC collection point. So we need to handle this
38684 first pragma without benefit of an existing lexer structure.
38686 Always returns one token to the caller in *FIRST_TOKEN. This is
38687 either the true first token of the file, or the first token after
38688 the initial pragma. */
38691 cp_parser_initial_pragma (cp_token
*first_token
)
38695 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38696 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38699 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38700 if (first_token
->type
== CPP_STRING
)
38702 name
= first_token
->u
.value
;
38704 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38705 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38706 error_at (first_token
->location
,
38707 "junk at end of %<#pragma GCC pch_preprocess%>");
38710 error_at (first_token
->location
, "expected string literal");
38712 /* Skip to the end of the pragma. */
38713 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38714 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38716 /* Now actually load the PCH file. */
38718 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38720 /* Read one more token to return to our caller. We have to do this
38721 after reading the PCH file in, since its pointers have to be
38723 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38726 /* Parse a pragma GCC ivdep. */
38729 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38731 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38735 /* Parse a pragma GCC unroll. */
38737 static unsigned short
38738 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38740 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38741 tree expr
= cp_parser_constant_expression (parser
);
38742 unsigned short unroll
;
38743 expr
= maybe_constant_value (expr
);
38744 HOST_WIDE_INT lunroll
= 0;
38745 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38746 || TREE_CODE (expr
) != INTEGER_CST
38747 || (lunroll
= tree_to_shwi (expr
)) < 0
38748 || lunroll
>= USHRT_MAX
)
38750 error_at (location
, "%<#pragma GCC unroll%> requires an"
38751 " assignment-expression that evaluates to a non-negative"
38752 " integral constant less than %u", USHRT_MAX
);
38757 unroll
= (unsigned short)lunroll
;
38761 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38765 /* Normal parsing of a pragma token. Here we can (and must) use the
38769 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38771 cp_token
*pragma_tok
;
38776 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38777 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38778 parser
->lexer
->in_pragma
= true;
38780 id
= cp_parser_pragma_kind (pragma_tok
);
38781 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38782 cp_ensure_no_omp_declare_simd (parser
);
38785 case PRAGMA_GCC_PCH_PREPROCESS
:
38786 error_at (pragma_tok
->location
,
38787 "%<#pragma GCC pch_preprocess%> must be first");
38790 case PRAGMA_OMP_BARRIER
:
38793 case pragma_compound
:
38794 cp_parser_omp_barrier (parser
, pragma_tok
);
38797 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38798 "used in compound statements", "omp barrier");
38805 case PRAGMA_OMP_FLUSH
:
38808 case pragma_compound
:
38809 cp_parser_omp_flush (parser
, pragma_tok
);
38812 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38813 "used in compound statements", "omp flush");
38820 case PRAGMA_OMP_TASKWAIT
:
38823 case pragma_compound
:
38824 cp_parser_omp_taskwait (parser
, pragma_tok
);
38827 error_at (pragma_tok
->location
,
38828 "%<#pragma %s%> may only be used in compound statements",
38836 case PRAGMA_OMP_TASKYIELD
:
38839 case pragma_compound
:
38840 cp_parser_omp_taskyield (parser
, pragma_tok
);
38843 error_at (pragma_tok
->location
,
38844 "%<#pragma %s%> may only be used in compound statements",
38852 case PRAGMA_OMP_CANCEL
:
38855 case pragma_compound
:
38856 cp_parser_omp_cancel (parser
, pragma_tok
);
38859 error_at (pragma_tok
->location
,
38860 "%<#pragma %s%> may only be used in compound statements",
38868 case PRAGMA_OMP_CANCELLATION_POINT
:
38869 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38872 case PRAGMA_OMP_THREADPRIVATE
:
38873 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38876 case PRAGMA_OMP_DECLARE
:
38877 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38879 case PRAGMA_OACC_DECLARE
:
38880 cp_parser_oacc_declare (parser
, pragma_tok
);
38883 case PRAGMA_OACC_ENTER_DATA
:
38884 if (context
== pragma_stmt
)
38886 error_at (pragma_tok
->location
,
38887 "%<#pragma %s%> may only be used in compound statements",
38891 else if (context
!= pragma_compound
)
38893 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38896 case PRAGMA_OACC_EXIT_DATA
:
38897 if (context
== pragma_stmt
)
38899 error_at (pragma_tok
->location
,
38900 "%<#pragma %s%> may only be used in compound statements",
38904 else if (context
!= pragma_compound
)
38906 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38909 case PRAGMA_OACC_ROUTINE
:
38910 if (context
!= pragma_external
)
38912 error_at (pragma_tok
->location
,
38913 "%<#pragma acc routine%> must be at file scope");
38916 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38919 case PRAGMA_OACC_UPDATE
:
38920 if (context
== pragma_stmt
)
38922 error_at (pragma_tok
->location
,
38923 "%<#pragma %s%> may only be used in compound statements",
38927 else if (context
!= pragma_compound
)
38929 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38932 case PRAGMA_OACC_WAIT
:
38933 if (context
== pragma_stmt
)
38935 error_at (pragma_tok
->location
,
38936 "%<#pragma %s%> may only be used in compound statements",
38940 else if (context
!= pragma_compound
)
38942 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38945 case PRAGMA_OACC_ATOMIC
:
38946 case PRAGMA_OACC_CACHE
:
38947 case PRAGMA_OACC_DATA
:
38948 case PRAGMA_OACC_HOST_DATA
:
38949 case PRAGMA_OACC_KERNELS
:
38950 case PRAGMA_OACC_PARALLEL
:
38951 case PRAGMA_OACC_LOOP
:
38952 case PRAGMA_OMP_ATOMIC
:
38953 case PRAGMA_OMP_CRITICAL
:
38954 case PRAGMA_OMP_DISTRIBUTE
:
38955 case PRAGMA_OMP_FOR
:
38956 case PRAGMA_OMP_MASTER
:
38957 case PRAGMA_OMP_PARALLEL
:
38958 case PRAGMA_OMP_SECTIONS
:
38959 case PRAGMA_OMP_SIMD
:
38960 case PRAGMA_OMP_SINGLE
:
38961 case PRAGMA_OMP_TASK
:
38962 case PRAGMA_OMP_TASKGROUP
:
38963 case PRAGMA_OMP_TASKLOOP
:
38964 case PRAGMA_OMP_TEAMS
:
38965 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38967 stmt
= push_omp_privatization_clauses (false);
38968 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38969 pop_omp_privatization_clauses (stmt
);
38972 case PRAGMA_OMP_ORDERED
:
38973 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38975 stmt
= push_omp_privatization_clauses (false);
38976 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38977 pop_omp_privatization_clauses (stmt
);
38980 case PRAGMA_OMP_TARGET
:
38981 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38983 stmt
= push_omp_privatization_clauses (false);
38984 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38985 pop_omp_privatization_clauses (stmt
);
38988 case PRAGMA_OMP_END_DECLARE_TARGET
:
38989 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38992 case PRAGMA_OMP_SECTION
:
38993 error_at (pragma_tok
->location
,
38994 "%<#pragma omp section%> may only be used in "
38995 "%<#pragma omp sections%> construct");
39000 if (context
== pragma_external
)
39002 error_at (pragma_tok
->location
,
39003 "%<#pragma GCC ivdep%> must be inside a function");
39006 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39007 unsigned short unroll
;
39008 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39009 if (tok
->type
== CPP_PRAGMA
39010 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39012 tok
= cp_lexer_consume_token (parser
->lexer
);
39013 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39014 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39018 if (tok
->type
!= CPP_KEYWORD
39019 || (tok
->keyword
!= RID_FOR
39020 && tok
->keyword
!= RID_WHILE
39021 && tok
->keyword
!= RID_DO
))
39023 cp_parser_error (parser
, "for, while or do statement expected");
39026 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39030 case PRAGMA_UNROLL
:
39032 if (context
== pragma_external
)
39034 error_at (pragma_tok
->location
,
39035 "%<#pragma GCC unroll%> must be inside a function");
39038 const unsigned short unroll
39039 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39041 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39042 if (tok
->type
== CPP_PRAGMA
39043 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39045 tok
= cp_lexer_consume_token (parser
->lexer
);
39046 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39047 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39051 if (tok
->type
!= CPP_KEYWORD
39052 || (tok
->keyword
!= RID_FOR
39053 && tok
->keyword
!= RID_WHILE
39054 && tok
->keyword
!= RID_DO
))
39056 cp_parser_error (parser
, "for, while or do statement expected");
39059 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39064 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39065 c_invoke_pragma_handler (id
);
39069 cp_parser_error (parser
, "expected declaration specifiers");
39073 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39077 /* The interface the pragma parsers have to the lexer. */
39080 pragma_lex (tree
*value
, location_t
*loc
)
39082 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39083 enum cpp_ttype ret
= tok
->type
;
39085 *value
= tok
->u
.value
;
39087 *loc
= tok
->location
;
39089 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39091 else if (ret
== CPP_STRING
)
39092 *value
= cp_parser_string_literal (the_parser
, false, false);
39095 if (ret
== CPP_KEYWORD
)
39097 cp_lexer_consume_token (the_parser
->lexer
);
39104 /* External interface. */
39106 /* Parse one entire translation unit. */
39109 c_parse_file (void)
39111 static bool already_called
= false;
39113 if (already_called
)
39114 fatal_error (input_location
,
39115 "inter-module optimizations not implemented for C++");
39116 already_called
= true;
39118 the_parser
= cp_parser_new ();
39119 push_deferring_access_checks (flag_access_control
39120 ? dk_no_deferred
: dk_no_check
);
39121 cp_parser_translation_unit (the_parser
);
39124 finish_translation_unit ();
39127 /* Create an identifier for a generic parameter type (a synthesized
39128 template parameter implied by `auto' or a concept identifier). */
39130 static GTY(()) int generic_parm_count
;
39132 make_generic_type_name ()
39135 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39136 return get_identifier (buf
);
39139 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39140 (creating a new template parameter list if necessary). Returns the newly
39141 created template type parm. */
39144 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39146 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39148 /* Before committing to modifying any scope, if we're in an
39149 implicit template scope, and we're trying to synthesize a
39150 constrained parameter, try to find a previous parameter with
39151 the same name. This is the same-type rule for abbreviated
39152 function templates.
39154 NOTE: We can generate implicit parameters when tentatively
39155 parsing a nested name specifier, only to reject that parse
39156 later. However, matching the same template-id as part of a
39157 direct-declarator should generate an identical template
39158 parameter, so this rule will merge them. */
39159 if (parser
->implicit_template_scope
&& constr
)
39161 tree t
= parser
->implicit_template_parms
;
39164 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39166 tree d
= TREE_VALUE (t
);
39167 if (TREE_CODE (d
) == PARM_DECL
)
39168 /* Return the TEMPLATE_PARM_INDEX. */
39169 d
= DECL_INITIAL (d
);
39172 t
= TREE_CHAIN (t
);
39176 /* We are either continuing a function template that already contains implicit
39177 template parameters, creating a new fully-implicit function template, or
39178 extending an existing explicit function template with implicit template
39181 cp_binding_level
*const entry_scope
= current_binding_level
;
39183 bool become_template
= false;
39184 cp_binding_level
*parent_scope
= 0;
39186 if (parser
->implicit_template_scope
)
39188 gcc_assert (parser
->implicit_template_parms
);
39190 current_binding_level
= parser
->implicit_template_scope
;
39194 /* Roll back to the existing template parameter scope (in the case of
39195 extending an explicit function template) or introduce a new template
39196 parameter scope ahead of the function parameter scope (or class scope
39197 in the case of out-of-line member definitions). The function scope is
39198 added back after template parameter synthesis below. */
39200 cp_binding_level
*scope
= entry_scope
;
39202 while (scope
->kind
== sk_function_parms
)
39204 parent_scope
= scope
;
39205 scope
= scope
->level_chain
;
39207 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39209 /* If not defining a class, then any class scope is a scope level in
39210 an out-of-line member definition. In this case simply wind back
39211 beyond the first such scope to inject the template parameter list.
39212 Otherwise wind back to the class being defined. The latter can
39213 occur in class member friend declarations such as:
39219 friend void A::foo (auto);
39222 The template parameter list synthesized for the friend declaration
39223 must be injected in the scope of 'B'. This can also occur in
39224 erroneous cases such as:
39230 void B::foo (auto) {}
39233 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39234 but, nevertheless, the template parameter list synthesized for the
39235 declarator should be injected into the scope of 'A' as if the
39236 ill-formed template was specified explicitly. */
39238 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39240 parent_scope
= scope
;
39241 scope
= scope
->level_chain
;
39245 current_binding_level
= scope
;
39247 if (scope
->kind
!= sk_template_parms
39248 || !function_being_declared_is_template_p (parser
))
39250 /* Introduce a new template parameter list for implicit template
39253 become_template
= true;
39255 parser
->implicit_template_scope
39256 = begin_scope (sk_template_parms
, NULL
);
39258 ++processing_template_decl
;
39260 parser
->fully_implicit_function_template_p
= true;
39261 ++parser
->num_template_parameter_lists
;
39265 /* Synthesize implicit template parameters at the end of the explicit
39266 template parameter list. */
39268 gcc_assert (current_template_parms
);
39270 parser
->implicit_template_scope
= scope
;
39272 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39273 parser
->implicit_template_parms
39274 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39278 /* Synthesize a new template parameter and track the current template
39279 parameter chain with implicit_template_parms. */
39281 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39282 tree synth_id
= make_generic_type_name ();
39283 tree synth_tmpl_parm
;
39284 bool non_type
= false;
39286 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39288 = finish_template_type_parm (class_type_node
, synth_id
);
39289 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39291 = finish_constrained_template_template_parm (proto
, synth_id
);
39294 synth_tmpl_parm
= copy_decl (proto
);
39295 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39299 // Attach the constraint to the parm before processing.
39300 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39301 TREE_TYPE (node
) = constr
;
39303 = process_template_parm (parser
->implicit_template_parms
,
39306 /*non_type=*/non_type
,
39307 /*param_pack=*/false);
39309 // Chain the new parameter to the list of implicit parameters.
39310 if (parser
->implicit_template_parms
)
39311 parser
->implicit_template_parms
39312 = TREE_CHAIN (parser
->implicit_template_parms
);
39314 parser
->implicit_template_parms
= new_parm
;
39316 tree new_decl
= get_local_decls ();
39318 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39319 new_decl
= DECL_INITIAL (new_decl
);
39321 /* If creating a fully implicit function template, start the new implicit
39322 template parameter list with this synthesized type, otherwise grow the
39323 current template parameter list. */
39325 if (become_template
)
39327 parent_scope
->level_chain
= current_binding_level
;
39329 tree new_parms
= make_tree_vec (1);
39330 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39331 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39332 new_parms
, current_template_parms
);
39336 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39337 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39338 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39339 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39342 // If the new parameter was constrained, we need to add that to the
39343 // constraints in the template parameter list.
39344 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39346 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39347 reqs
= conjoin_constraints (reqs
, req
);
39348 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39351 current_binding_level
= entry_scope
;
39356 /* Finish the declaration of a fully implicit function template. Such a
39357 template has no explicit template parameter list so has not been through the
39358 normal template head and tail processing. synthesize_implicit_template_parm
39359 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39360 provided if the declaration is a class member such that its template
39361 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39362 form is returned. Otherwise NULL_TREE is returned. */
39365 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39367 gcc_assert (parser
->fully_implicit_function_template_p
);
39369 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39370 && DECL_VIRTUAL_P (member_decl_opt
))
39372 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39373 "implicit templates may not be %<virtual%>");
39374 DECL_VIRTUAL_P (member_decl_opt
) = false;
39377 if (member_decl_opt
)
39378 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39379 end_template_decl ();
39381 parser
->fully_implicit_function_template_p
= false;
39382 parser
->implicit_template_parms
= 0;
39383 parser
->implicit_template_scope
= 0;
39384 --parser
->num_template_parameter_lists
;
39386 return member_decl_opt
;
39389 /* Like finish_fully_implicit_template, but to be used in error
39390 recovery, rearranging scopes so that we restore the state we had
39391 before synthesize_implicit_template_parm inserted the implement
39392 template parms scope. */
39395 abort_fully_implicit_template (cp_parser
*parser
)
39397 cp_binding_level
*return_to_scope
= current_binding_level
;
39399 if (parser
->implicit_template_scope
39400 && return_to_scope
!= parser
->implicit_template_scope
)
39402 cp_binding_level
*child
= return_to_scope
;
39403 for (cp_binding_level
*scope
= child
->level_chain
;
39404 scope
!= parser
->implicit_template_scope
;
39405 scope
= child
->level_chain
)
39407 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39408 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39409 current_binding_level
= parser
->implicit_template_scope
;
39412 return_to_scope
= return_to_scope
->level_chain
;
39414 finish_fully_implicit_template (parser
, NULL
);
39416 gcc_assert (current_binding_level
== return_to_scope
);
39419 /* Helper function for diagnostics that have complained about things
39420 being used with 'extern "C"' linkage.
39422 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39425 maybe_show_extern_c_location (void)
39427 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39428 inform (the_parser
->innermost_linkage_specification_location
,
39429 "%<extern \"C\"%> linkage started here");
39432 #include "gt-cp-parser.h"