2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
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
);
3295 if (const char *suggestion
= hint
.suggestion ())
3297 gcc_rich_location
richloc (location
);
3298 richloc
.add_fixit_replace (suggestion
);
3300 "%qE does not name a type; did you mean %qs?",
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
;
3368 if (decl
== error_mark_node
)
3369 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3371 const char *suggestion
= hint
.suggestion ();
3372 gcc_rich_location
richloc (location_of (id
));
3374 richloc
.add_fixit_replace (suggestion
);
3375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3379 "%qE in namespace %qE does not name a template"
3380 " type; did you mean %qs?",
3381 id
, parser
->scope
, suggestion
);
3384 "%qE in namespace %qE does not name a template type",
3387 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3391 "%qE in namespace %qE does not name a template"
3392 " type; did you mean %qs?",
3393 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3396 "%qE in namespace %qE does not name a template"
3398 TREE_OPERAND (id
, 0), parser
->scope
);
3404 "%qE in namespace %qE does not name a type"
3405 "; did you mean %qs?",
3406 id
, parser
->scope
, suggestion
);
3409 "%qE in namespace %qE does not name a type",
3413 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3415 else if (CLASS_TYPE_P (parser
->scope
)
3416 && constructor_name_p (id
, parser
->scope
))
3419 auto_diagnostic_group d
;
3420 error_at (location
, "%<%T::%E%> names the constructor, not"
3421 " the type", parser
->scope
, id
);
3422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3423 error_at (location
, "and %qT has no template constructors",
3426 else if (TYPE_P (parser
->scope
)
3427 && dependent_scope_p (parser
->scope
))
3429 gcc_rich_location
richloc (location
);
3430 richloc
.add_fixit_insert_before ("typename ");
3431 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3433 "need %<typename%> before %<%T::%D::%E%> because "
3434 "%<%T::%D%> is a dependent scope",
3435 TYPE_CONTEXT (parser
->scope
),
3436 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3438 TYPE_CONTEXT (parser
->scope
),
3439 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3441 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3442 "%qT is a dependent scope",
3443 parser
->scope
, id
, parser
->scope
);
3445 else if (TYPE_P (parser
->scope
))
3447 auto_diagnostic_group d
;
3448 if (!COMPLETE_TYPE_P (parser
->scope
))
3449 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3451 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3452 error_at (location_of (id
),
3453 "%qE in %q#T does not name a template type",
3455 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3456 error_at (location_of (id
),
3457 "%qE in %q#T does not name a template type",
3458 TREE_OPERAND (id
, 0), parser
->scope
);
3460 error_at (location_of (id
),
3461 "%qE in %q#T does not name a type",
3464 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3471 /* Check for a common situation where a type-name should be present,
3472 but is not, and issue a sensible error message. Returns true if an
3473 invalid type-name was detected.
3475 The situation handled by this function are variable declarations of the
3476 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3477 Usually, `ID' should name a type, but if we got here it means that it
3478 does not. We try to emit the best possible error message depending on
3479 how exactly the id-expression looks like. */
3482 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3485 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3487 /* Avoid duplicate error about ambiguous lookup. */
3488 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3490 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3491 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3495 cp_parser_parse_tentatively (parser
);
3496 id
= cp_parser_id_expression (parser
,
3497 /*template_keyword_p=*/false,
3498 /*check_dependency_p=*/true,
3499 /*template_p=*/NULL
,
3500 /*declarator_p=*/false,
3501 /*optional_p=*/false);
3502 /* If the next token is a (, this is a function with no explicit return
3503 type, i.e. constructor, destructor or conversion op. */
3504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3505 || TREE_CODE (id
) == TYPE_DECL
)
3507 cp_parser_abort_tentative_parse (parser
);
3510 if (!cp_parser_parse_definitely (parser
))
3513 /* Emit a diagnostic for the invalid type. */
3514 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3516 /* If we aren't in the middle of a declarator (i.e. in a
3517 parameter-declaration-clause), skip to the end of the declaration;
3518 there's no point in trying to process it. */
3519 if (!parser
->in_declarator_p
)
3520 cp_parser_skip_to_end_of_block_or_statement (parser
);
3524 /* Consume tokens up to, and including, the next non-nested closing `)'.
3525 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3526 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3527 found an unnested token of that type. */
3530 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3535 unsigned paren_depth
= 0;
3536 unsigned brace_depth
= 0;
3537 unsigned square_depth
= 0;
3538 unsigned condop_depth
= 0;
3540 if (recovering
&& or_ttype
== CPP_EOF
3541 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3546 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3548 /* Have we found what we're looking for before the closing paren? */
3549 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3550 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3553 switch (token
->type
)
3556 case CPP_PRAGMA_EOL
:
3557 /* If we've run out of tokens, then there is no closing `)'. */
3560 /* This is good for lambda expression capture-lists. */
3561 case CPP_OPEN_SQUARE
:
3564 case CPP_CLOSE_SQUARE
:
3565 if (!square_depth
--)
3570 /* This matches the processing in skip_to_end_of_statement. */
3575 case CPP_OPEN_BRACE
:
3578 case CPP_CLOSE_BRACE
:
3583 case CPP_OPEN_PAREN
:
3588 case CPP_CLOSE_PAREN
:
3589 if (!brace_depth
&& !paren_depth
--)
3592 cp_lexer_consume_token (parser
->lexer
);
3598 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3603 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3611 /* Consume the token. */
3612 cp_lexer_consume_token (parser
->lexer
);
3616 /* Consume tokens up to, and including, the next non-nested closing `)'.
3617 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3618 are doing error recovery. Returns -1 if OR_COMMA is true and we
3619 found an unnested token of that type. */
3622 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3627 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3628 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3629 ttype
, consume_paren
);
3632 /* Consume tokens until we reach the end of the current statement.
3633 Normally, that will be just before consuming a `;'. However, if a
3634 non-nested `}' comes first, then we stop before consuming that. */
3637 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3639 unsigned nesting_depth
= 0;
3641 /* Unwind generic function template scope if necessary. */
3642 if (parser
->fully_implicit_function_template_p
)
3643 abort_fully_implicit_template (parser
);
3647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3649 switch (token
->type
)
3652 case CPP_PRAGMA_EOL
:
3653 /* If we've run out of tokens, stop. */
3657 /* If the next token is a `;', we have reached the end of the
3663 case CPP_CLOSE_BRACE
:
3664 /* If this is a non-nested '}', stop before consuming it.
3665 That way, when confronted with something like:
3669 we stop before consuming the closing '}', even though we
3670 have not yet reached a `;'. */
3671 if (nesting_depth
== 0)
3674 /* If it is the closing '}' for a block that we have
3675 scanned, stop -- but only after consuming the token.
3681 we will stop after the body of the erroneously declared
3682 function, but before consuming the following `typedef'
3684 if (--nesting_depth
== 0)
3686 cp_lexer_consume_token (parser
->lexer
);
3691 case CPP_OPEN_BRACE
:
3699 /* Consume the token. */
3700 cp_lexer_consume_token (parser
->lexer
);
3704 /* This function is called at the end of a statement or declaration.
3705 If the next token is a semicolon, it is consumed; otherwise, error
3706 recovery is attempted. */
3709 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3711 /* Look for the trailing `;'. */
3712 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3714 /* If there is additional (erroneous) input, skip to the end of
3716 cp_parser_skip_to_end_of_statement (parser
);
3717 /* If the next token is now a `;', consume it. */
3718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3719 cp_lexer_consume_token (parser
->lexer
);
3723 /* Skip tokens until we have consumed an entire block, or until we
3724 have consumed a non-nested `;'. */
3727 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3729 int nesting_depth
= 0;
3731 /* Unwind generic function template scope if necessary. */
3732 if (parser
->fully_implicit_function_template_p
)
3733 abort_fully_implicit_template (parser
);
3735 while (nesting_depth
>= 0)
3737 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3739 switch (token
->type
)
3742 case CPP_PRAGMA_EOL
:
3743 /* If we've run out of tokens, stop. */
3747 /* Stop if this is an unnested ';'. */
3752 case CPP_CLOSE_BRACE
:
3753 /* Stop if this is an unnested '}', or closes the outermost
3756 if (nesting_depth
< 0)
3762 case CPP_OPEN_BRACE
:
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser
->lexer
);
3776 /* Skip tokens until a non-nested closing curly brace is the next
3777 token, or there are no more tokens. Return true in the first case,
3781 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3783 unsigned nesting_depth
= 0;
3787 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3789 switch (token
->type
)
3792 case CPP_PRAGMA_EOL
:
3793 /* If we've run out of tokens, stop. */
3796 case CPP_CLOSE_BRACE
:
3797 /* If the next token is a non-nested `}', then we have reached
3798 the end of the current block. */
3799 if (nesting_depth
-- == 0)
3803 case CPP_OPEN_BRACE
:
3804 /* If it the next token is a `{', then we are entering a new
3805 block. Consume the entire block. */
3813 /* Consume the token. */
3814 cp_lexer_consume_token (parser
->lexer
);
3818 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3819 parameter is the PRAGMA token, allowing us to purge the entire pragma
3823 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3827 parser
->lexer
->in_pragma
= false;
3830 token
= cp_lexer_consume_token (parser
->lexer
);
3831 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3833 /* Ensure that the pragma is not parsed again. */
3834 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3837 /* Require pragma end of line, resyncing with it as necessary. The
3838 arguments are as for cp_parser_skip_to_pragma_eol. */
3841 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3843 parser
->lexer
->in_pragma
= false;
3844 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3845 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3848 /* This is a simple wrapper around make_typename_type. When the id is
3849 an unresolved identifier node, we can provide a superior diagnostic
3850 using cp_parser_diagnose_invalid_type_name. */
3853 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3854 location_t id_location
)
3857 if (identifier_p (id
))
3859 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3860 /*complain=*/tf_none
);
3861 if (result
== error_mark_node
)
3862 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3865 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3868 /* This is a wrapper around the
3869 make_{pointer,ptrmem,reference}_declarator functions that decides
3870 which one to call based on the CODE and CLASS_TYPE arguments. The
3871 CODE argument should be one of the values returned by
3872 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3873 appertain to the pointer or reference. */
3875 static cp_declarator
*
3876 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3877 cp_cv_quals cv_qualifiers
,
3878 cp_declarator
*target
,
3881 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3882 return cp_error_declarator
;
3884 if (code
== INDIRECT_REF
)
3885 if (class_type
== NULL_TREE
)
3886 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3888 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3889 target
, attributes
);
3890 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3891 return make_reference_declarator (cv_qualifiers
, target
,
3893 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3894 return make_reference_declarator (cv_qualifiers
, target
,
3899 /* Create a new C++ parser. */
3902 cp_parser_new (void)
3908 /* cp_lexer_new_main is called before doing GC allocation because
3909 cp_lexer_new_main might load a PCH file. */
3910 lexer
= cp_lexer_new_main ();
3912 /* Initialize the binops_by_token so that we can get the tree
3913 directly from the token. */
3914 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3915 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3917 parser
= ggc_cleared_alloc
<cp_parser
> ();
3918 parser
->lexer
= lexer
;
3919 parser
->context
= cp_parser_context_new (NULL
);
3921 /* For now, we always accept GNU extensions. */
3922 parser
->allow_gnu_extensions_p
= 1;
3924 /* The `>' token is a greater-than operator, not the end of a
3926 parser
->greater_than_is_operator_p
= true;
3928 parser
->default_arg_ok_p
= true;
3930 /* We are not parsing a constant-expression. */
3931 parser
->integral_constant_expression_p
= false;
3932 parser
->allow_non_integral_constant_expression_p
= false;
3933 parser
->non_integral_constant_expression_p
= false;
3935 /* Local variable names are not forbidden. */
3936 parser
->local_variables_forbidden_p
= false;
3938 /* We are not processing an `extern "C"' declaration. */
3939 parser
->in_unbraced_linkage_specification_p
= false;
3941 /* We are not processing a declarator. */
3942 parser
->in_declarator_p
= false;
3944 /* We are not processing a template-argument-list. */
3945 parser
->in_template_argument_list_p
= false;
3947 /* We are not in an iteration statement. */
3948 parser
->in_statement
= 0;
3950 /* We are not in a switch statement. */
3951 parser
->in_switch_statement_p
= false;
3953 /* We are not parsing a type-id inside an expression. */
3954 parser
->in_type_id_in_expr_p
= false;
3956 /* String literals should be translated to the execution character set. */
3957 parser
->translate_strings_p
= true;
3959 /* We are not parsing a function body. */
3960 parser
->in_function_body
= false;
3962 /* We can correct until told otherwise. */
3963 parser
->colon_corrects_to_scope_p
= true;
3965 /* The unparsed function queue is empty. */
3966 push_unparsed_function_queues (parser
);
3968 /* There are no classes being defined. */
3969 parser
->num_classes_being_defined
= 0;
3971 /* No template parameters apply. */
3972 parser
->num_template_parameter_lists
= 0;
3974 /* Special parsing data structures. */
3975 parser
->omp_declare_simd
= NULL
;
3976 parser
->oacc_routine
= NULL
;
3978 /* Not declaring an implicit function template. */
3979 parser
->auto_is_implicit_function_template_parm_p
= false;
3980 parser
->fully_implicit_function_template_p
= false;
3981 parser
->implicit_template_parms
= 0;
3982 parser
->implicit_template_scope
= 0;
3984 /* Allow constrained-type-specifiers. */
3985 parser
->prevent_constrained_type_specifiers
= 0;
3987 /* We haven't yet seen an 'extern "C"'. */
3988 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3993 /* Create a cp_lexer structure which will emit the tokens in CACHE
3994 and push it onto the parser's lexer stack. This is used for delayed
3995 parsing of in-class method bodies and default arguments, and should
3996 not be confused with tentative parsing. */
3998 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4000 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4001 lexer
->next
= parser
->lexer
;
4002 parser
->lexer
= lexer
;
4004 /* Move the current source position to that of the first token in the
4006 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4009 /* Pop the top lexer off the parser stack. This is never used for the
4010 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4012 cp_parser_pop_lexer (cp_parser
*parser
)
4014 cp_lexer
*lexer
= parser
->lexer
;
4015 parser
->lexer
= lexer
->next
;
4016 cp_lexer_destroy (lexer
);
4018 /* Put the current source position back where it was before this
4019 lexer was pushed. */
4020 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4023 /* Lexical conventions [gram.lex] */
4025 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4029 cp_parser_identifier (cp_parser
* parser
)
4033 /* Look for the identifier. */
4034 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4035 /* Return the value. */
4037 return cp_expr (token
->u
.value
, token
->location
);
4039 return error_mark_node
;
4042 /* Parse a sequence of adjacent string constants. Returns a
4043 TREE_STRING representing the combined, nul-terminated string
4044 constant. If TRANSLATE is true, translate the string to the
4045 execution character set. If WIDE_OK is true, a wide string is
4048 C++98 [lex.string] says that if a narrow string literal token is
4049 adjacent to a wide string literal token, the behavior is undefined.
4050 However, C99 6.4.5p4 says that this results in a wide string literal.
4051 We follow C99 here, for consistency with the C front end.
4053 This code is largely lifted from lex_string() in c-lex.c.
4055 FUTURE: ObjC++ will need to handle @-strings here. */
4057 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4058 bool lookup_udlit
= true)
4062 struct obstack str_ob
;
4063 struct obstack loc_ob
;
4064 cpp_string str
, istr
, *strs
;
4066 enum cpp_ttype type
, curr_type
;
4067 int have_suffix_p
= 0;
4069 tree suffix_id
= NULL_TREE
;
4070 bool curr_tok_is_userdef_p
= false;
4072 tok
= cp_lexer_peek_token (parser
->lexer
);
4073 if (!cp_parser_is_string_literal (tok
))
4075 cp_parser_error (parser
, "expected string-literal");
4076 return error_mark_node
;
4079 location_t loc
= tok
->location
;
4081 if (cpp_userdef_string_p (tok
->type
))
4083 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4084 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4085 curr_tok_is_userdef_p
= true;
4089 string_tree
= tok
->u
.value
;
4090 curr_type
= tok
->type
;
4094 /* Try to avoid the overhead of creating and destroying an obstack
4095 for the common case of just one string. */
4096 if (!cp_parser_is_string_literal
4097 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4099 cp_lexer_consume_token (parser
->lexer
);
4101 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4102 str
.len
= TREE_STRING_LENGTH (string_tree
);
4105 if (curr_tok_is_userdef_p
)
4107 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4109 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4112 curr_type
= tok
->type
;
4118 location_t last_tok_loc
= tok
->location
;
4119 gcc_obstack_init (&str_ob
);
4120 gcc_obstack_init (&loc_ob
);
4125 cp_lexer_consume_token (parser
->lexer
);
4127 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4128 str
.len
= TREE_STRING_LENGTH (string_tree
);
4130 if (curr_tok_is_userdef_p
)
4132 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4133 if (have_suffix_p
== 0)
4135 suffix_id
= curr_suffix_id
;
4138 else if (have_suffix_p
== 1
4139 && curr_suffix_id
!= suffix_id
)
4141 error ("inconsistent user-defined literal suffixes"
4142 " %qD and %qD in string literal",
4143 suffix_id
, curr_suffix_id
);
4146 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4149 curr_type
= tok
->type
;
4151 if (type
!= curr_type
)
4153 if (type
== CPP_STRING
)
4155 else if (curr_type
!= CPP_STRING
)
4157 rich_location
rich_loc (line_table
, tok
->location
);
4158 rich_loc
.add_range (last_tok_loc
);
4159 error_at (&rich_loc
,
4160 "unsupported non-standard concatenation "
4161 "of string literals");
4165 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4166 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4168 last_tok_loc
= tok
->location
;
4170 tok
= cp_lexer_peek_token (parser
->lexer
);
4171 if (cpp_userdef_string_p (tok
->type
))
4173 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4174 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4175 curr_tok_is_userdef_p
= true;
4179 string_tree
= tok
->u
.value
;
4180 curr_type
= tok
->type
;
4181 curr_tok_is_userdef_p
= false;
4184 while (cp_parser_is_string_literal (tok
));
4186 /* A string literal built by concatenation has its caret=start at
4187 the start of the initial string, and its finish at the finish of
4188 the final string literal. */
4189 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4191 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4194 if (type
!= CPP_STRING
&& !wide_ok
)
4196 cp_parser_error (parser
, "a wide string is invalid in this context");
4200 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4201 (parse_in
, strs
, count
, &istr
, type
))
4203 value
= build_string (istr
.len
, (const char *)istr
.text
);
4204 free (CONST_CAST (unsigned char *, istr
.text
));
4207 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4208 gcc_assert (g_string_concat_db
);
4209 g_string_concat_db
->record_string_concatenation (count
, locs
);
4216 case CPP_UTF8STRING
:
4217 TREE_TYPE (value
) = char_array_type_node
;
4220 TREE_TYPE (value
) = char16_array_type_node
;
4223 TREE_TYPE (value
) = char32_array_type_node
;
4226 TREE_TYPE (value
) = wchar_array_type_node
;
4230 value
= fix_string_type (value
);
4234 tree literal
= build_userdef_literal (suffix_id
, value
,
4235 OT_NONE
, NULL_TREE
);
4237 value
= cp_parser_userdef_string_literal (literal
);
4243 /* cpp_interpret_string has issued an error. */
4244 value
= error_mark_node
;
4248 obstack_free (&str_ob
, 0);
4249 obstack_free (&loc_ob
, 0);
4252 return cp_expr (value
, loc
);
4255 /* Look up a literal operator with the name and the exact arguments. */
4258 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4261 decl
= lookup_name (name
);
4262 if (!decl
|| !is_overloaded_fn (decl
))
4263 return error_mark_node
;
4265 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4270 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4271 if (parmtypes
!= NULL_TREE
)
4273 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4274 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4276 tree tparm
= TREE_VALUE (parmtypes
);
4277 tree targ
= TREE_TYPE ((*args
)[ix
]);
4278 bool ptr
= TYPE_PTR_P (tparm
);
4279 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4280 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4282 || !same_type_p (TREE_TYPE (tparm
),
4287 && ix
== vec_safe_length (args
)
4288 /* May be this should be sufficient_parms_p instead,
4289 depending on how exactly should user-defined literals
4290 work in presence of default arguments on the literal
4291 operator parameters. */
4292 && parmtypes
== void_list_node
)
4297 return error_mark_node
;
4300 /* Parse a user-defined char constant. Returns a call to a user-defined
4301 literal operator taking the character as an argument. */
4304 cp_parser_userdef_char_literal (cp_parser
*parser
)
4306 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4307 tree literal
= token
->u
.value
;
4308 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4309 tree value
= USERDEF_LITERAL_VALUE (literal
);
4310 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4313 /* Build up a call to the user-defined operator */
4314 /* Lookup the name we got back from the id-expression. */
4315 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4316 vec_safe_push (args
, value
);
4317 decl
= lookup_literal_operator (name
, args
);
4318 if (!decl
|| decl
== error_mark_node
)
4320 error ("unable to find character literal operator %qD with %qT argument",
4321 name
, TREE_TYPE (value
));
4322 release_tree_vector (args
);
4323 return error_mark_node
;
4325 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4326 release_tree_vector (args
);
4330 /* A subroutine of cp_parser_userdef_numeric_literal to
4331 create a char... template parameter pack from a string node. */
4334 make_char_string_pack (tree value
)
4337 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4338 const char *str
= TREE_STRING_POINTER (value
);
4339 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4340 tree argvec
= make_tree_vec (1);
4342 /* Fill in CHARVEC with all of the parameters. */
4343 charvec
= make_tree_vec (len
);
4344 for (i
= 0; i
< len
; ++i
)
4346 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4347 cpp_string in
= { 3, s
};
4348 cpp_string out
= { 0, 0 };
4349 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4351 gcc_assert (out
.len
== 2);
4352 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4356 /* Build the argument packs. */
4357 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4359 TREE_VEC_ELT (argvec
, 0) = argpack
;
4364 /* A subroutine of cp_parser_userdef_numeric_literal to
4365 create a char... template parameter pack from a string node. */
4368 make_string_pack (tree value
)
4371 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4372 const unsigned char *str
4373 = (const unsigned char *) TREE_STRING_POINTER (value
);
4374 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4375 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4376 tree argvec
= make_tree_vec (2);
4378 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4379 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4381 /* First template parm is character type. */
4382 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4384 /* Fill in CHARVEC with all of the parameters. */
4385 charvec
= make_tree_vec (len
);
4386 for (int i
= 0; i
< len
; ++i
)
4387 TREE_VEC_ELT (charvec
, i
)
4388 = double_int_to_tree (str_char_type_node
,
4389 double_int::from_buffer (str
+ i
* sz
, sz
));
4391 /* Build the argument packs. */
4392 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4394 TREE_VEC_ELT (argvec
, 1) = argpack
;
4399 /* Parse a user-defined numeric constant. returns a call to a user-defined
4400 literal operator. */
4403 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4405 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4406 tree literal
= token
->u
.value
;
4407 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4408 tree value
= USERDEF_LITERAL_VALUE (literal
);
4409 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4410 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4411 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4413 vec
<tree
, va_gc
> *args
;
4415 /* Look for a literal operator taking the exact type of numeric argument
4416 as the literal value. */
4417 args
= make_tree_vector ();
4418 vec_safe_push (args
, value
);
4419 decl
= lookup_literal_operator (name
, args
);
4420 if (decl
&& decl
!= error_mark_node
)
4422 result
= finish_call_expr (decl
, &args
, false, true,
4423 tf_warning_or_error
);
4425 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4427 warning_at (token
->location
, OPT_Woverflow
,
4428 "integer literal exceeds range of %qT type",
4429 long_long_unsigned_type_node
);
4434 warning_at (token
->location
, OPT_Woverflow
,
4435 "floating literal exceeds range of %qT type",
4436 long_double_type_node
);
4437 else if (overflow
< 0)
4438 warning_at (token
->location
, OPT_Woverflow
,
4439 "floating literal truncated to zero");
4442 release_tree_vector (args
);
4445 release_tree_vector (args
);
4447 /* If the numeric argument didn't work, look for a raw literal
4448 operator taking a const char* argument consisting of the number
4449 in string format. */
4450 args
= make_tree_vector ();
4451 vec_safe_push (args
, num_string
);
4452 decl
= lookup_literal_operator (name
, args
);
4453 if (decl
&& decl
!= error_mark_node
)
4455 result
= finish_call_expr (decl
, &args
, false, true,
4456 tf_warning_or_error
);
4457 release_tree_vector (args
);
4460 release_tree_vector (args
);
4462 /* If the raw literal didn't work, look for a non-type template
4463 function with parameter pack char.... Call the function with
4464 template parameter characters representing the number. */
4465 args
= make_tree_vector ();
4466 decl
= lookup_literal_operator (name
, args
);
4467 if (decl
&& decl
!= error_mark_node
)
4469 tree tmpl_args
= make_char_string_pack (num_string
);
4470 if (tmpl_args
== NULL_TREE
)
4472 error ("failed to translate literal to execution character set %qT",
4474 return error_mark_node
;
4476 decl
= lookup_template_function (decl
, tmpl_args
);
4477 result
= finish_call_expr (decl
, &args
, false, true,
4478 tf_warning_or_error
);
4479 release_tree_vector (args
);
4483 release_tree_vector (args
);
4485 /* In C++14 the standard library defines complex number suffixes that
4486 conflict with GNU extensions. Prefer them if <complex> is #included. */
4487 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4488 bool i14
= (cxx_dialect
> cxx11
4489 && (id_equal (suffix_id
, "i")
4490 || id_equal (suffix_id
, "if")
4491 || id_equal (suffix_id
, "il")));
4492 diagnostic_t kind
= DK_ERROR
;
4497 tree cxlit
= lookup_qualified_name (std_node
,
4498 get_identifier ("complex_literals"),
4500 if (cxlit
== error_mark_node
)
4502 /* No <complex>, so pedwarn and use GNU semantics. */
4504 opt
= OPT_Wpedantic
;
4509 = emit_diagnostic (kind
, input_location
, opt
,
4510 "unable to find numeric literal operator %qD", name
);
4513 /* Don't inform either. */;
4516 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4517 "(from <complex>) to enable the C++14 user-defined literal "
4520 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4521 "GNU built-in suffix");
4524 inform (token
->location
, "use -fext-numeric-literals "
4525 "to enable more built-in suffixes");
4527 if (kind
== DK_ERROR
)
4528 value
= error_mark_node
;
4531 /* Use the built-in semantics. */
4533 if (id_equal (suffix_id
, "i"))
4535 if (TREE_CODE (value
) == INTEGER_CST
)
4536 type
= integer_type_node
;
4538 type
= double_type_node
;
4540 else if (id_equal (suffix_id
, "if"))
4541 type
= float_type_node
;
4542 else /* if (id_equal (suffix_id, "il")) */
4543 type
= long_double_type_node
;
4545 value
= build_complex (build_complex_type (type
),
4546 fold_convert (type
, integer_zero_node
),
4547 fold_convert (type
, value
));
4550 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4551 /* Avoid repeated diagnostics. */
4552 token
->u
.value
= value
;
4556 /* Parse a user-defined string constant. Returns a call to a user-defined
4557 literal operator taking a character pointer and the length of the string
4561 cp_parser_userdef_string_literal (tree literal
)
4563 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4564 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4565 tree value
= USERDEF_LITERAL_VALUE (literal
);
4566 int len
= TREE_STRING_LENGTH (value
)
4567 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4569 vec
<tree
, va_gc
> *args
;
4571 /* Build up a call to the user-defined operator. */
4572 /* Lookup the name we got back from the id-expression. */
4573 args
= make_tree_vector ();
4574 vec_safe_push (args
, value
);
4575 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4576 decl
= lookup_literal_operator (name
, args
);
4578 if (decl
&& decl
!= error_mark_node
)
4580 result
= finish_call_expr (decl
, &args
, false, true,
4581 tf_warning_or_error
);
4582 release_tree_vector (args
);
4585 release_tree_vector (args
);
4587 /* Look for a template function with typename parameter CharT
4588 and parameter pack CharT... Call the function with
4589 template parameter characters representing the string. */
4590 args
= make_tree_vector ();
4591 decl
= lookup_literal_operator (name
, args
);
4592 if (decl
&& decl
!= error_mark_node
)
4594 tree tmpl_args
= make_string_pack (value
);
4595 decl
= lookup_template_function (decl
, tmpl_args
);
4596 result
= finish_call_expr (decl
, &args
, false, true,
4597 tf_warning_or_error
);
4598 release_tree_vector (args
);
4601 release_tree_vector (args
);
4603 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4604 name
, TREE_TYPE (value
), size_type_node
);
4605 return error_mark_node
;
4609 /* Basic concepts [gram.basic] */
4611 /* Parse a translation-unit.
4614 declaration-seq [opt] */
4617 cp_parser_translation_unit (cp_parser
* parser
)
4619 gcc_checking_assert (!cp_error_declarator
);
4621 /* Create the declarator obstack. */
4622 gcc_obstack_init (&declarator_obstack
);
4623 /* Create the error declarator. */
4624 cp_error_declarator
= make_declarator (cdk_error
);
4625 /* Create the empty parameter list. */
4626 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4628 /* Remember where the base of the declarator obstack lies. */
4629 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4631 bool implicit_extern_c
= false;
4635 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4637 /* If we're entering or exiting a region that's implicitly
4638 extern "C", modify the lang context appropriately. */
4639 if (implicit_extern_c
4640 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4642 implicit_extern_c
= !implicit_extern_c
;
4643 if (implicit_extern_c
)
4644 push_lang_context (lang_name_c
);
4646 pop_lang_context ();
4649 if (token
->type
== CPP_EOF
)
4652 if (token
->type
== CPP_CLOSE_BRACE
)
4654 cp_parser_error (parser
, "expected declaration");
4655 cp_lexer_consume_token (parser
->lexer
);
4656 /* If the next token is now a `;', consume it. */
4657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4658 cp_lexer_consume_token (parser
->lexer
);
4661 cp_parser_toplevel_declaration (parser
);
4664 /* Get rid of the token array; we don't need it any more. */
4665 cp_lexer_destroy (parser
->lexer
);
4666 parser
->lexer
= NULL
;
4668 /* The EOF should have reset this. */
4669 gcc_checking_assert (!implicit_extern_c
);
4671 /* Make sure the declarator obstack was fully cleaned up. */
4672 gcc_assert (obstack_next_free (&declarator_obstack
)
4673 == declarator_obstack_base
);
4676 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4677 decltype context. */
4679 static inline tsubst_flags_t
4680 complain_flags (bool decltype_p
)
4682 tsubst_flags_t complain
= tf_warning_or_error
;
4684 complain
|= tf_decltype
;
4688 /* We're about to parse a collection of statements. If we're currently
4689 parsing tentatively, set up a firewall so that any nested
4690 cp_parser_commit_to_tentative_parse won't affect the current context. */
4692 static cp_token_position
4693 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4695 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4698 cp_parser_parse_tentatively (parser
);
4699 cp_parser_commit_to_topmost_tentative_parse (parser
);
4700 return cp_lexer_token_position (parser
->lexer
, false);
4703 /* We've finished parsing the collection of statements. Wrap up the
4704 firewall and replace the relevant tokens with the parsed form. */
4707 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4713 /* Finish the firewall level. */
4714 cp_parser_parse_definitely (parser
);
4715 /* And remember the result of the parse for when we try again. */
4716 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4717 token
->type
= CPP_PREPARSED_EXPR
;
4718 token
->u
.value
= expr
;
4719 token
->keyword
= RID_MAX
;
4720 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4723 /* Like the above functions, but let the user modify the tokens. Used by
4724 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4725 later parses, so it makes sense to localize the effects of
4726 cp_parser_commit_to_tentative_parse. */
4728 struct tentative_firewall
4733 tentative_firewall (cp_parser
*p
): parser(p
)
4735 /* If we're currently parsing tentatively, start a committed level as a
4736 firewall and then an inner tentative parse. */
4737 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4739 cp_parser_parse_tentatively (parser
);
4740 cp_parser_commit_to_topmost_tentative_parse (parser
);
4741 cp_parser_parse_tentatively (parser
);
4745 ~tentative_firewall()
4749 /* Finish the inner tentative parse and the firewall, propagating any
4750 uncommitted error state to the outer tentative parse. */
4751 bool err
= cp_parser_error_occurred (parser
);
4752 cp_parser_parse_definitely (parser
);
4753 cp_parser_parse_definitely (parser
);
4755 cp_parser_simulate_error (parser
);
4760 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4761 This class is for tracking such a matching pair of symbols.
4762 In particular, it tracks the location of the first token,
4763 so that if the second token is missing, we can highlight the
4764 location of the first token when notifying the user about the
4767 template <typename traits_t
>
4771 /* token_pair's ctor. */
4772 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4774 /* If the next token is the opening symbol for this pair, consume it and
4776 Otherwise, issue an error and return false.
4777 In either case, record the location of the opening token. */
4779 bool require_open (cp_parser
*parser
)
4781 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4782 return cp_parser_require (parser
, traits_t::open_token_type
,
4783 traits_t::required_token_open
);
4786 /* Consume the next token from PARSER, recording its location as
4787 that of the opening token within the pair. */
4789 cp_token
* consume_open (cp_parser
*parser
)
4791 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4792 gcc_assert (tok
->type
== traits_t::open_token_type
);
4793 m_open_loc
= tok
->location
;
4797 /* If the next token is the closing symbol for this pair, consume it
4799 Otherwise, issue an error, highlighting the location of the
4800 corresponding opening token, and return NULL. */
4802 cp_token
*require_close (cp_parser
*parser
) const
4804 return cp_parser_require (parser
, traits_t::close_token_type
,
4805 traits_t::required_token_close
,
4810 location_t m_open_loc
;
4813 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4815 struct matching_paren_traits
4817 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4818 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4819 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4820 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4823 /* "matching_parens" is a token_pair<T> class for tracking matching
4824 pairs of parentheses. */
4826 typedef token_pair
<matching_paren_traits
> matching_parens
;
4828 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4830 struct matching_brace_traits
4832 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4833 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4834 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4835 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4838 /* "matching_braces" is a token_pair<T> class for tracking matching
4841 typedef token_pair
<matching_brace_traits
> matching_braces
;
4844 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4845 enclosing parentheses. */
4848 cp_parser_statement_expr (cp_parser
*parser
)
4850 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4852 /* Consume the '('. */
4853 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4854 matching_parens parens
;
4855 parens
.consume_open (parser
);
4856 /* Start the statement-expression. */
4857 tree expr
= begin_stmt_expr ();
4858 /* Parse the compound-statement. */
4859 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4861 expr
= finish_stmt_expr (expr
, false);
4862 /* Consume the ')'. */
4863 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4864 if (!parens
.require_close (parser
))
4865 cp_parser_skip_to_end_of_statement (parser
);
4867 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4868 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4869 return cp_expr (expr
, combined_loc
);
4872 /* Expressions [gram.expr] */
4874 /* Parse a fold-operator.
4877 - * / % ^ & | = < > << >>
4878 = -= *= /= %= ^= &= |= <<= >>=
4879 == != <= >= && || , .* ->*
4881 This returns the tree code corresponding to the matched operator
4882 as an int. When the current token matches a compound assignment
4883 opertor, the resulting tree code is the negative value of the
4884 non-assignment operator. */
4887 cp_parser_fold_operator (cp_token
*token
)
4889 switch (token
->type
)
4891 case CPP_PLUS
: return PLUS_EXPR
;
4892 case CPP_MINUS
: return MINUS_EXPR
;
4893 case CPP_MULT
: return MULT_EXPR
;
4894 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4895 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4896 case CPP_XOR
: return BIT_XOR_EXPR
;
4897 case CPP_AND
: return BIT_AND_EXPR
;
4898 case CPP_OR
: return BIT_IOR_EXPR
;
4899 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4900 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4902 case CPP_EQ
: return -NOP_EXPR
;
4903 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4904 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4905 case CPP_MULT_EQ
: return -MULT_EXPR
;
4906 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4907 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4908 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4909 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4910 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4911 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4912 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4914 case CPP_EQ_EQ
: return EQ_EXPR
;
4915 case CPP_NOT_EQ
: return NE_EXPR
;
4916 case CPP_LESS
: return LT_EXPR
;
4917 case CPP_GREATER
: return GT_EXPR
;
4918 case CPP_LESS_EQ
: return LE_EXPR
;
4919 case CPP_GREATER_EQ
: return GE_EXPR
;
4921 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4922 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4924 case CPP_COMMA
: return COMPOUND_EXPR
;
4926 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4927 case CPP_DEREF_STAR
: return MEMBER_REF
;
4929 default: return ERROR_MARK
;
4933 /* Returns true if CODE indicates a binary expression, which is not allowed in
4934 the LHS of a fold-expression. More codes will need to be added to use this
4935 function in other contexts. */
4938 is_binary_op (tree_code code
)
4943 case POINTER_PLUS_EXPR
:
4946 case TRUNC_DIV_EXPR
:
4947 case TRUNC_MOD_EXPR
:
4963 case TRUTH_ANDIF_EXPR
:
4964 case TRUTH_ORIF_EXPR
:
4977 /* If the next token is a suitable fold operator, consume it and return as
4978 the function above. */
4981 cp_parser_fold_operator (cp_parser
*parser
)
4983 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4984 int code
= cp_parser_fold_operator (token
);
4985 if (code
!= ERROR_MARK
)
4986 cp_lexer_consume_token (parser
->lexer
);
4990 /* Parse a fold-expression.
4993 ( ... folding-operator cast-expression)
4994 ( cast-expression folding-operator ... )
4995 ( cast-expression folding operator ... folding-operator cast-expression)
4997 Note that the '(' and ')' are matched in primary expression. */
5000 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5007 cp_lexer_consume_token (parser
->lexer
);
5008 int op
= cp_parser_fold_operator (parser
);
5009 if (op
== ERROR_MARK
)
5011 cp_parser_error (parser
, "expected binary operator");
5012 return error_mark_node
;
5015 tree expr
= cp_parser_cast_expression (parser
, false, false,
5017 if (expr
== error_mark_node
)
5018 return error_mark_node
;
5019 return finish_left_unary_fold_expr (expr
, op
);
5022 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5023 int op
= cp_parser_fold_operator (parser
);
5024 if (op
== ERROR_MARK
)
5026 cp_parser_error (parser
, "expected binary operator");
5027 return error_mark_node
;
5030 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5032 cp_parser_error (parser
, "expected ...");
5033 return error_mark_node
;
5035 cp_lexer_consume_token (parser
->lexer
);
5037 /* The operands of a fold-expression are cast-expressions, so binary or
5038 conditional expressions are not allowed. We check this here to avoid
5039 tentative parsing. */
5040 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5041 /* OK, the expression was parenthesized. */;
5042 else if (is_binary_op (TREE_CODE (expr1
)))
5043 error_at (location_of (expr1
),
5044 "binary expression in operand of fold-expression");
5045 else if (TREE_CODE (expr1
) == COND_EXPR
5046 || (REFERENCE_REF_P (expr1
)
5047 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5048 error_at (location_of (expr1
),
5049 "conditional expression in operand of fold-expression");
5052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5053 return finish_right_unary_fold_expr (expr1
, op
);
5055 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5057 cp_parser_error (parser
, "mismatched operator in fold-expression");
5058 return error_mark_node
;
5060 cp_lexer_consume_token (parser
->lexer
);
5062 // Binary left or right fold.
5063 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5064 if (expr2
== error_mark_node
)
5065 return error_mark_node
;
5066 return finish_binary_fold_expr (expr1
, expr2
, op
);
5069 /* Parse a primary-expression.
5076 lambda-expression (C++11)
5081 ( compound-statement )
5082 __builtin_va_arg ( assignment-expression , type-id )
5083 __builtin_offsetof ( type-id , offsetof-expression )
5086 __has_nothrow_assign ( type-id )
5087 __has_nothrow_constructor ( type-id )
5088 __has_nothrow_copy ( type-id )
5089 __has_trivial_assign ( type-id )
5090 __has_trivial_constructor ( type-id )
5091 __has_trivial_copy ( type-id )
5092 __has_trivial_destructor ( type-id )
5093 __has_virtual_destructor ( type-id )
5094 __is_abstract ( type-id )
5095 __is_base_of ( type-id , type-id )
5096 __is_class ( type-id )
5097 __is_empty ( type-id )
5098 __is_enum ( type-id )
5099 __is_final ( type-id )
5100 __is_literal_type ( type-id )
5101 __is_pod ( type-id )
5102 __is_polymorphic ( type-id )
5103 __is_std_layout ( type-id )
5104 __is_trivial ( type-id )
5105 __is_union ( type-id )
5107 Objective-C++ Extension:
5115 ADDRESS_P is true iff this expression was immediately preceded by
5116 "&" and therefore might denote a pointer-to-member. CAST_P is true
5117 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5118 true iff this expression is a template argument.
5120 Returns a representation of the expression. Upon return, *IDK
5121 indicates what kind of id-expression (if any) was present. */
5124 cp_parser_primary_expression (cp_parser
*parser
,
5127 bool template_arg_p
,
5131 cp_token
*token
= NULL
;
5133 /* Assume the primary expression is not an id-expression. */
5134 *idk
= CP_ID_KIND_NONE
;
5136 /* Peek at the next token. */
5137 token
= cp_lexer_peek_token (parser
->lexer
);
5138 switch ((int) token
->type
)
5147 user-defined-literal */
5154 case CPP_PREPARSED_EXPR
:
5155 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5156 return cp_parser_userdef_numeric_literal (parser
);
5157 token
= cp_lexer_consume_token (parser
->lexer
);
5158 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5160 error_at (token
->location
,
5161 "fixed-point types not supported in C++");
5162 return error_mark_node
;
5164 /* Floating-point literals are only allowed in an integral
5165 constant expression if they are cast to an integral or
5166 enumeration type. */
5167 if (TREE_CODE (token
->u
.value
) == REAL_CST
5168 && parser
->integral_constant_expression_p
5171 /* CAST_P will be set even in invalid code like "int(2.7 +
5172 ...)". Therefore, we have to check that the next token
5173 is sure to end the cast. */
5176 cp_token
*next_token
;
5178 next_token
= cp_lexer_peek_token (parser
->lexer
);
5179 if (/* The comma at the end of an
5180 enumerator-definition. */
5181 next_token
->type
!= CPP_COMMA
5182 /* The curly brace at the end of an enum-specifier. */
5183 && next_token
->type
!= CPP_CLOSE_BRACE
5184 /* The end of a statement. */
5185 && next_token
->type
!= CPP_SEMICOLON
5186 /* The end of the cast-expression. */
5187 && next_token
->type
!= CPP_CLOSE_PAREN
5188 /* The end of an array bound. */
5189 && next_token
->type
!= CPP_CLOSE_SQUARE
5190 /* The closing ">" in a template-argument-list. */
5191 && (next_token
->type
!= CPP_GREATER
5192 || parser
->greater_than_is_operator_p
)
5193 /* C++0x only: A ">>" treated like two ">" tokens,
5194 in a template-argument-list. */
5195 && (next_token
->type
!= CPP_RSHIFT
5196 || (cxx_dialect
== cxx98
)
5197 || parser
->greater_than_is_operator_p
))
5201 /* If we are within a cast, then the constraint that the
5202 cast is to an integral or enumeration type will be
5203 checked at that point. If we are not within a cast, then
5204 this code is invalid. */
5206 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5208 return cp_expr (token
->u
.value
, token
->location
);
5210 case CPP_CHAR_USERDEF
:
5211 case CPP_CHAR16_USERDEF
:
5212 case CPP_CHAR32_USERDEF
:
5213 case CPP_WCHAR_USERDEF
:
5214 case CPP_UTF8CHAR_USERDEF
:
5215 return cp_parser_userdef_char_literal (parser
);
5221 case CPP_UTF8STRING
:
5222 case CPP_STRING_USERDEF
:
5223 case CPP_STRING16_USERDEF
:
5224 case CPP_STRING32_USERDEF
:
5225 case CPP_WSTRING_USERDEF
:
5226 case CPP_UTF8STRING_USERDEF
:
5227 /* ??? Should wide strings be allowed when parser->translate_strings_p
5228 is false (i.e. in attributes)? If not, we can kill the third
5229 argument to cp_parser_string_literal. */
5230 return cp_parser_string_literal (parser
,
5231 parser
->translate_strings_p
,
5234 case CPP_OPEN_PAREN
:
5235 /* If we see `( { ' then we are looking at the beginning of
5236 a GNU statement-expression. */
5237 if (cp_parser_allow_gnu_extensions_p (parser
)
5238 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5240 /* Statement-expressions are not allowed by the standard. */
5241 pedwarn (token
->location
, OPT_Wpedantic
,
5242 "ISO C++ forbids braced-groups within expressions");
5244 /* And they're not allowed outside of a function-body; you
5245 cannot, for example, write:
5247 int i = ({ int j = 3; j + 1; });
5249 at class or namespace scope. */
5250 if (!parser
->in_function_body
5251 || parser
->in_template_argument_list_p
)
5253 error_at (token
->location
,
5254 "statement-expressions are not allowed outside "
5255 "functions nor in template-argument lists");
5256 cp_parser_skip_to_end_of_block_or_statement (parser
);
5257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5258 cp_lexer_consume_token (parser
->lexer
);
5259 return error_mark_node
;
5262 return cp_parser_statement_expr (parser
);
5264 /* Otherwise it's a normal parenthesized expression. */
5267 bool saved_greater_than_is_operator_p
;
5269 location_t open_paren_loc
= token
->location
;
5271 /* Consume the `('. */
5272 matching_parens parens
;
5273 parens
.consume_open (parser
);
5274 /* Within a parenthesized expression, a `>' token is always
5275 the greater-than operator. */
5276 saved_greater_than_is_operator_p
5277 = parser
->greater_than_is_operator_p
;
5278 parser
->greater_than_is_operator_p
= true;
5280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5281 /* Left fold expression. */
5284 /* Parse the parenthesized expression. */
5285 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5287 token
= cp_lexer_peek_token (parser
->lexer
);
5288 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5290 expr
= cp_parser_fold_expression (parser
, expr
);
5291 if (expr
!= error_mark_node
5292 && cxx_dialect
< cxx17
5293 && !in_system_header_at (input_location
))
5294 pedwarn (input_location
, 0, "fold-expressions only available "
5295 "with -std=c++17 or -std=gnu++17");
5298 /* Let the front end know that this expression was
5299 enclosed in parentheses. This matters in case, for
5300 example, the expression is of the form `A::B', since
5301 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5303 expr
= finish_parenthesized_expr (expr
);
5305 /* DR 705: Wrapping an unqualified name in parentheses
5306 suppresses arg-dependent lookup. We want to pass back
5307 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5308 (c++/37862), but none of the others. */
5309 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5310 *idk
= CP_ID_KIND_NONE
;
5312 /* The `>' token might be the end of a template-id or
5313 template-parameter-list now. */
5314 parser
->greater_than_is_operator_p
5315 = saved_greater_than_is_operator_p
;
5317 /* Consume the `)'. */
5318 token
= cp_lexer_peek_token (parser
->lexer
);
5319 location_t close_paren_loc
= token
->location
;
5320 expr
.set_range (open_paren_loc
, close_paren_loc
);
5321 if (!parens
.require_close (parser
)
5322 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5323 cp_parser_skip_to_end_of_statement (parser
);
5328 case CPP_OPEN_SQUARE
:
5330 if (c_dialect_objc ())
5332 /* We might have an Objective-C++ message. */
5333 cp_parser_parse_tentatively (parser
);
5334 tree msg
= cp_parser_objc_message_expression (parser
);
5335 /* If that works out, we're done ... */
5336 if (cp_parser_parse_definitely (parser
))
5338 /* ... else, fall though to see if it's a lambda. */
5340 cp_expr lam
= cp_parser_lambda_expression (parser
);
5341 /* Don't warn about a failed tentative parse. */
5342 if (cp_parser_error_occurred (parser
))
5343 return error_mark_node
;
5344 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5348 case CPP_OBJC_STRING
:
5349 if (c_dialect_objc ())
5350 /* We have an Objective-C++ string literal. */
5351 return cp_parser_objc_expression (parser
);
5352 cp_parser_error (parser
, "expected primary-expression");
5353 return error_mark_node
;
5356 switch (token
->keyword
)
5358 /* These two are the boolean literals. */
5360 cp_lexer_consume_token (parser
->lexer
);
5361 return cp_expr (boolean_true_node
, token
->location
);
5363 cp_lexer_consume_token (parser
->lexer
);
5364 return cp_expr (boolean_false_node
, token
->location
);
5366 /* The `__null' literal. */
5368 cp_lexer_consume_token (parser
->lexer
);
5369 return cp_expr (null_node
, token
->location
);
5371 /* The `nullptr' literal. */
5373 cp_lexer_consume_token (parser
->lexer
);
5374 return cp_expr (nullptr_node
, token
->location
);
5376 /* Recognize the `this' keyword. */
5378 cp_lexer_consume_token (parser
->lexer
);
5379 if (parser
->local_variables_forbidden_p
)
5381 error_at (token
->location
,
5382 "%<this%> may not be used in this context");
5383 return error_mark_node
;
5385 /* Pointers cannot appear in constant-expressions. */
5386 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5387 return error_mark_node
;
5388 return cp_expr (finish_this_expr (), token
->location
);
5390 /* The `operator' keyword can be the beginning of an
5395 case RID_FUNCTION_NAME
:
5396 case RID_PRETTY_FUNCTION_NAME
:
5397 case RID_C99_FUNCTION_NAME
:
5399 non_integral_constant name
;
5401 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5402 __func__ are the names of variables -- but they are
5403 treated specially. Therefore, they are handled here,
5404 rather than relying on the generic id-expression logic
5405 below. Grammatically, these names are id-expressions.
5407 Consume the token. */
5408 token
= cp_lexer_consume_token (parser
->lexer
);
5410 switch (token
->keyword
)
5412 case RID_FUNCTION_NAME
:
5413 name
= NIC_FUNC_NAME
;
5415 case RID_PRETTY_FUNCTION_NAME
:
5416 name
= NIC_PRETTY_FUNC
;
5418 case RID_C99_FUNCTION_NAME
:
5419 name
= NIC_C99_FUNC
;
5425 if (cp_parser_non_integral_constant_expression (parser
, name
))
5426 return error_mark_node
;
5428 /* Look up the name. */
5429 return finish_fname (token
->u
.value
);
5436 source_location type_location
;
5437 location_t start_loc
5438 = cp_lexer_peek_token (parser
->lexer
)->location
;
5439 /* The `__builtin_va_arg' construct is used to handle
5440 `va_arg'. Consume the `__builtin_va_arg' token. */
5441 cp_lexer_consume_token (parser
->lexer
);
5442 /* Look for the opening `('. */
5443 matching_parens parens
;
5444 parens
.require_open (parser
);
5445 /* Now, parse the assignment-expression. */
5446 expression
= cp_parser_assignment_expression (parser
);
5447 /* Look for the `,'. */
5448 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5449 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5450 /* Parse the type-id. */
5452 type_id_in_expr_sentinel
s (parser
);
5453 type
= cp_parser_type_id (parser
);
5455 /* Look for the closing `)'. */
5456 location_t finish_loc
5457 = cp_lexer_peek_token (parser
->lexer
)->location
;
5458 parens
.require_close (parser
);
5459 /* Using `va_arg' in a constant-expression is not
5461 if (cp_parser_non_integral_constant_expression (parser
,
5463 return error_mark_node
;
5464 /* Construct a location of the form:
5465 __builtin_va_arg (v, int)
5466 ~~~~~~~~~~~~~~~~~~~~~^~~~
5467 with the caret at the type, ranging from the start of the
5468 "__builtin_va_arg" token to the close paren. */
5469 location_t combined_loc
5470 = make_location (type_location
, start_loc
, finish_loc
);
5471 return build_x_va_arg (combined_loc
, expression
, type
);
5475 return cp_parser_builtin_offsetof (parser
);
5477 case RID_HAS_NOTHROW_ASSIGN
:
5478 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5479 case RID_HAS_NOTHROW_COPY
:
5480 case RID_HAS_TRIVIAL_ASSIGN
:
5481 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5482 case RID_HAS_TRIVIAL_COPY
:
5483 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5484 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5485 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5486 case RID_IS_ABSTRACT
:
5487 case RID_IS_AGGREGATE
:
5488 case RID_IS_BASE_OF
:
5493 case RID_IS_LITERAL_TYPE
:
5495 case RID_IS_POLYMORPHIC
:
5496 case RID_IS_SAME_AS
:
5497 case RID_IS_STD_LAYOUT
:
5498 case RID_IS_TRIVIAL
:
5499 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5500 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5501 case RID_IS_TRIVIALLY_COPYABLE
:
5503 case RID_IS_ASSIGNABLE
:
5504 case RID_IS_CONSTRUCTIBLE
:
5505 return cp_parser_trait_expr (parser
, token
->keyword
);
5509 return cp_parser_requires_expression (parser
);
5511 /* Objective-C++ expressions. */
5513 case RID_AT_PROTOCOL
:
5514 case RID_AT_SELECTOR
:
5515 return cp_parser_objc_expression (parser
);
5518 if (parser
->in_function_body
5519 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5522 error_at (token
->location
,
5523 "a template declaration cannot appear at block scope");
5524 cp_parser_skip_to_end_of_block_or_statement (parser
);
5525 return error_mark_node
;
5529 cp_parser_error (parser
, "expected primary-expression");
5530 return error_mark_node
;
5533 /* An id-expression can start with either an identifier, a
5534 `::' as the beginning of a qualified-id, or the "operator"
5538 case CPP_TEMPLATE_ID
:
5539 case CPP_NESTED_NAME_SPECIFIER
:
5542 cp_expr id_expression
;
5544 const char *error_msg
;
5547 cp_token
*id_expr_token
;
5549 /* Parse the id-expression. */
5551 = cp_parser_id_expression (parser
,
5552 /*template_keyword_p=*/false,
5553 /*check_dependency_p=*/true,
5555 /*declarator_p=*/false,
5556 /*optional_p=*/false);
5557 if (id_expression
== error_mark_node
)
5558 return error_mark_node
;
5559 id_expr_token
= token
;
5560 token
= cp_lexer_peek_token (parser
->lexer
);
5561 done
= (token
->type
!= CPP_OPEN_SQUARE
5562 && token
->type
!= CPP_OPEN_PAREN
5563 && token
->type
!= CPP_DOT
5564 && token
->type
!= CPP_DEREF
5565 && token
->type
!= CPP_PLUS_PLUS
5566 && token
->type
!= CPP_MINUS_MINUS
);
5567 /* If we have a template-id, then no further lookup is
5568 required. If the template-id was for a template-class, we
5569 will sometimes have a TYPE_DECL at this point. */
5570 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5571 || TREE_CODE (id_expression
) == TYPE_DECL
)
5572 decl
= id_expression
;
5573 /* Look up the name. */
5576 tree ambiguous_decls
;
5578 /* If we already know that this lookup is ambiguous, then
5579 we've already issued an error message; there's no reason
5581 if (id_expr_token
->type
== CPP_NAME
5582 && id_expr_token
->error_reported
)
5584 cp_parser_simulate_error (parser
);
5585 return error_mark_node
;
5588 decl
= cp_parser_lookup_name (parser
, id_expression
,
5591 /*is_namespace=*/false,
5592 /*check_dependency=*/true,
5594 id_expr_token
->location
);
5595 /* If the lookup was ambiguous, an error will already have
5597 if (ambiguous_decls
)
5598 return error_mark_node
;
5600 /* In Objective-C++, we may have an Objective-C 2.0
5601 dot-syntax for classes here. */
5602 if (c_dialect_objc ()
5603 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5604 && TREE_CODE (decl
) == TYPE_DECL
5605 && objc_is_class_name (decl
))
5608 cp_lexer_consume_token (parser
->lexer
);
5609 component
= cp_parser_identifier (parser
);
5610 if (component
== error_mark_node
)
5611 return error_mark_node
;
5613 tree result
= objc_build_class_component_ref (id_expression
,
5615 /* Build a location of the form:
5618 with caret at the start of the component name (at
5619 input_location), ranging from the start of the id_expression
5620 to the end of the component name. */
5621 location_t combined_loc
5622 = make_location (input_location
, id_expression
.get_start (),
5623 get_finish (input_location
));
5624 protected_set_expr_location (result
, combined_loc
);
5628 /* In Objective-C++, an instance variable (ivar) may be preferred
5629 to whatever cp_parser_lookup_name() found.
5630 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5631 rest of c-family, we have to do a little extra work to preserve
5632 any location information in cp_expr "decl". Given that
5633 objc_lookup_ivar is implemented in "c-family" and "objc", we
5634 have a trip through the pure "tree" type, rather than cp_expr.
5635 Naively copying it back to "decl" would implicitly give the
5636 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5637 store an EXPR_LOCATION. Hence we only update "decl" (and
5638 hence its location_t) if we get back a different tree node. */
5639 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5641 if (decl_tree
!= decl
.get_value ())
5642 decl
= cp_expr (decl_tree
);
5644 /* If name lookup gives us a SCOPE_REF, then the
5645 qualifying scope was dependent. */
5646 if (TREE_CODE (decl
) == SCOPE_REF
)
5648 /* At this point, we do not know if DECL is a valid
5649 integral constant expression. We assume that it is
5650 in fact such an expression, so that code like:
5652 template <int N> struct A {
5656 is accepted. At template-instantiation time, we
5657 will check that B<N>::i is actually a constant. */
5660 /* Check to see if DECL is a local variable in a context
5661 where that is forbidden. */
5662 if (parser
->local_variables_forbidden_p
5663 && local_variable_p (decl
))
5665 error_at (id_expr_token
->location
,
5666 "local variable %qD may not appear in this context",
5668 return error_mark_node
;
5672 if (processing_template_decl
)
5673 if (tree fns
= maybe_get_fns (decl
))
5674 /* It's too difficult to mark ths in all the places where
5675 we know for sure we need to keep the lookup, so do it
5676 now. The cost is extra GC to recycle the lookups
5677 resolved at parse time. */
5680 decl
= (finish_id_expression
5681 (id_expression
, decl
, parser
->scope
,
5683 parser
->integral_constant_expression_p
,
5684 parser
->allow_non_integral_constant_expression_p
,
5685 &parser
->non_integral_constant_expression_p
,
5686 template_p
, done
, address_p
,
5689 id_expression
.get_location ()));
5691 cp_parser_error (parser
, error_msg
);
5692 decl
.set_location (id_expr_token
->location
);
5696 /* Anything else is an error. */
5698 cp_parser_error (parser
, "expected primary-expression");
5699 return error_mark_node
;
5703 static inline cp_expr
5704 cp_parser_primary_expression (cp_parser
*parser
,
5707 bool template_arg_p
,
5710 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5711 /*decltype*/false, idk
);
5714 /* Parse an id-expression.
5721 :: [opt] nested-name-specifier template [opt] unqualified-id
5723 :: operator-function-id
5726 Return a representation of the unqualified portion of the
5727 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5728 a `::' or nested-name-specifier.
5730 Often, if the id-expression was a qualified-id, the caller will
5731 want to make a SCOPE_REF to represent the qualified-id. This
5732 function does not do this in order to avoid wastefully creating
5733 SCOPE_REFs when they are not required.
5735 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5738 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5739 uninstantiated templates.
5741 If *TEMPLATE_P is non-NULL, it is set to true iff the
5742 `template' keyword is used to explicitly indicate that the entity
5743 named is a template.
5745 If DECLARATOR_P is true, the id-expression is appearing as part of
5746 a declarator, rather than as part of an expression. */
5749 cp_parser_id_expression (cp_parser
*parser
,
5750 bool template_keyword_p
,
5751 bool check_dependency_p
,
5756 bool global_scope_p
;
5757 bool nested_name_specifier_p
;
5759 /* Assume the `template' keyword was not used. */
5761 *template_p
= template_keyword_p
;
5763 /* Look for the optional `::' operator. */
5765 = (!template_keyword_p
5766 && (cp_parser_global_scope_opt (parser
,
5767 /*current_scope_valid_p=*/false)
5770 /* Look for the optional nested-name-specifier. */
5771 nested_name_specifier_p
5772 = (cp_parser_nested_name_specifier_opt (parser
,
5773 /*typename_keyword_p=*/false,
5780 /* If there is a nested-name-specifier, then we are looking at
5781 the first qualified-id production. */
5782 if (nested_name_specifier_p
)
5785 tree saved_object_scope
;
5786 tree saved_qualifying_scope
;
5787 cp_expr unqualified_id
;
5790 /* See if the next token is the `template' keyword. */
5792 template_p
= &is_template
;
5793 *template_p
= cp_parser_optional_template_keyword (parser
);
5794 /* Name lookup we do during the processing of the
5795 unqualified-id might obliterate SCOPE. */
5796 saved_scope
= parser
->scope
;
5797 saved_object_scope
= parser
->object_scope
;
5798 saved_qualifying_scope
= parser
->qualifying_scope
;
5799 /* Process the final unqualified-id. */
5800 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5803 /*optional_p=*/false);
5804 /* Restore the SAVED_SCOPE for our caller. */
5805 parser
->scope
= saved_scope
;
5806 parser
->object_scope
= saved_object_scope
;
5807 parser
->qualifying_scope
= saved_qualifying_scope
;
5809 return unqualified_id
;
5811 /* Otherwise, if we are in global scope, then we are looking at one
5812 of the other qualified-id productions. */
5813 else if (global_scope_p
)
5818 /* Peek at the next token. */
5819 token
= cp_lexer_peek_token (parser
->lexer
);
5821 /* If it's an identifier, and the next token is not a "<", then
5822 we can avoid the template-id case. This is an optimization
5823 for this common case. */
5824 if (token
->type
== CPP_NAME
5825 && !cp_parser_nth_token_starts_template_argument_list_p
5827 return cp_parser_identifier (parser
);
5829 cp_parser_parse_tentatively (parser
);
5830 /* Try a template-id. */
5831 id
= cp_parser_template_id (parser
,
5832 /*template_keyword_p=*/false,
5833 /*check_dependency_p=*/true,
5836 /* If that worked, we're done. */
5837 if (cp_parser_parse_definitely (parser
))
5840 /* Peek at the next token. (Changes in the token buffer may
5841 have invalidated the pointer obtained above.) */
5842 token
= cp_lexer_peek_token (parser
->lexer
);
5844 switch (token
->type
)
5847 return cp_parser_identifier (parser
);
5850 if (token
->keyword
== RID_OPERATOR
)
5851 return cp_parser_operator_function_id (parser
);
5855 cp_parser_error (parser
, "expected id-expression");
5856 return error_mark_node
;
5860 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5861 /*check_dependency_p=*/true,
5866 /* Parse an unqualified-id.
5870 operator-function-id
5871 conversion-function-id
5875 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5876 keyword, in a construct like `A::template ...'.
5878 Returns a representation of unqualified-id. For the `identifier'
5879 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5880 production a BIT_NOT_EXPR is returned; the operand of the
5881 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5882 other productions, see the documentation accompanying the
5883 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5884 names are looked up in uninstantiated templates. If DECLARATOR_P
5885 is true, the unqualified-id is appearing as part of a declarator,
5886 rather than as part of an expression. */
5889 cp_parser_unqualified_id (cp_parser
* parser
,
5890 bool template_keyword_p
,
5891 bool check_dependency_p
,
5897 /* Peek at the next token. */
5898 token
= cp_lexer_peek_token (parser
->lexer
);
5900 switch ((int) token
->type
)
5906 /* We don't know yet whether or not this will be a
5908 cp_parser_parse_tentatively (parser
);
5909 /* Try a template-id. */
5910 id
= cp_parser_template_id (parser
, template_keyword_p
,
5914 /* If it worked, we're done. */
5915 if (cp_parser_parse_definitely (parser
))
5917 /* Otherwise, it's an ordinary identifier. */
5918 return cp_parser_identifier (parser
);
5921 case CPP_TEMPLATE_ID
:
5922 return cp_parser_template_id (parser
, template_keyword_p
,
5930 tree qualifying_scope
;
5935 /* Consume the `~' token. */
5936 cp_lexer_consume_token (parser
->lexer
);
5937 /* Parse the class-name. The standard, as written, seems to
5940 template <typename T> struct S { ~S (); };
5941 template <typename T> S<T>::~S() {}
5943 is invalid, since `~' must be followed by a class-name, but
5944 `S<T>' is dependent, and so not known to be a class.
5945 That's not right; we need to look in uninstantiated
5946 templates. A further complication arises from:
5948 template <typename T> void f(T t) {
5952 Here, it is not possible to look up `T' in the scope of `T'
5953 itself. We must look in both the current scope, and the
5954 scope of the containing complete expression.
5956 Yet another issue is:
5965 The standard does not seem to say that the `S' in `~S'
5966 should refer to the type `S' and not the data member
5969 /* DR 244 says that we look up the name after the "~" in the
5970 same scope as we looked up the qualifying name. That idea
5971 isn't fully worked out; it's more complicated than that. */
5972 scope
= parser
->scope
;
5973 object_scope
= parser
->object_scope
;
5974 qualifying_scope
= parser
->qualifying_scope
;
5976 /* Check for invalid scopes. */
5977 if (scope
== error_mark_node
)
5979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5980 cp_lexer_consume_token (parser
->lexer
);
5981 return error_mark_node
;
5983 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5985 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5986 error_at (token
->location
,
5987 "scope %qT before %<~%> is not a class-name",
5989 cp_parser_simulate_error (parser
);
5990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5991 cp_lexer_consume_token (parser
->lexer
);
5992 return error_mark_node
;
5994 gcc_assert (!scope
|| TYPE_P (scope
));
5996 /* If the name is of the form "X::~X" it's OK even if X is a
5998 token
= cp_lexer_peek_token (parser
->lexer
);
6000 && token
->type
== CPP_NAME
6001 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6003 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6004 || (CLASS_TYPE_P (scope
)
6005 && constructor_name_p (token
->u
.value
, scope
))))
6007 cp_lexer_consume_token (parser
->lexer
);
6008 return build_nt (BIT_NOT_EXPR
, scope
);
6011 /* ~auto means the destructor of whatever the object is. */
6012 if (cp_parser_is_keyword (token
, RID_AUTO
))
6014 if (cxx_dialect
< cxx14
)
6015 pedwarn (input_location
, 0,
6016 "%<~auto%> only available with "
6017 "-std=c++14 or -std=gnu++14");
6018 cp_lexer_consume_token (parser
->lexer
);
6019 return build_nt (BIT_NOT_EXPR
, make_auto ());
6022 /* If there was an explicit qualification (S::~T), first look
6023 in the scope given by the qualification (i.e., S).
6025 Note: in the calls to cp_parser_class_name below we pass
6026 typename_type so that lookup finds the injected-class-name
6027 rather than the constructor. */
6029 type_decl
= NULL_TREE
;
6032 cp_parser_parse_tentatively (parser
);
6033 type_decl
= cp_parser_class_name (parser
,
6034 /*typename_keyword_p=*/false,
6035 /*template_keyword_p=*/false,
6037 /*check_dependency=*/false,
6038 /*class_head_p=*/false,
6040 if (cp_parser_parse_definitely (parser
))
6043 /* In "N::S::~S", look in "N" as well. */
6044 if (!done
&& scope
&& qualifying_scope
)
6046 cp_parser_parse_tentatively (parser
);
6047 parser
->scope
= qualifying_scope
;
6048 parser
->object_scope
= NULL_TREE
;
6049 parser
->qualifying_scope
= NULL_TREE
;
6051 = cp_parser_class_name (parser
,
6052 /*typename_keyword_p=*/false,
6053 /*template_keyword_p=*/false,
6055 /*check_dependency=*/false,
6056 /*class_head_p=*/false,
6058 if (cp_parser_parse_definitely (parser
))
6061 /* In "p->S::~T", look in the scope given by "*p" as well. */
6062 else if (!done
&& object_scope
)
6064 cp_parser_parse_tentatively (parser
);
6065 parser
->scope
= object_scope
;
6066 parser
->object_scope
= NULL_TREE
;
6067 parser
->qualifying_scope
= NULL_TREE
;
6069 = cp_parser_class_name (parser
,
6070 /*typename_keyword_p=*/false,
6071 /*template_keyword_p=*/false,
6073 /*check_dependency=*/false,
6074 /*class_head_p=*/false,
6076 if (cp_parser_parse_definitely (parser
))
6079 /* Look in the surrounding context. */
6082 parser
->scope
= NULL_TREE
;
6083 parser
->object_scope
= NULL_TREE
;
6084 parser
->qualifying_scope
= NULL_TREE
;
6085 if (processing_template_decl
)
6086 cp_parser_parse_tentatively (parser
);
6088 = cp_parser_class_name (parser
,
6089 /*typename_keyword_p=*/false,
6090 /*template_keyword_p=*/false,
6092 /*check_dependency=*/false,
6093 /*class_head_p=*/false,
6095 if (processing_template_decl
6096 && ! cp_parser_parse_definitely (parser
))
6098 /* We couldn't find a type with this name. If we're parsing
6099 tentatively, fail and try something else. */
6100 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6102 cp_parser_simulate_error (parser
);
6103 return error_mark_node
;
6105 /* Otherwise, accept it and check for a match at instantiation
6107 type_decl
= cp_parser_identifier (parser
);
6108 if (type_decl
!= error_mark_node
)
6109 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6113 /* If an error occurred, assume that the name of the
6114 destructor is the same as the name of the qualifying
6115 class. That allows us to keep parsing after running
6116 into ill-formed destructor names. */
6117 if (type_decl
== error_mark_node
&& scope
)
6118 return build_nt (BIT_NOT_EXPR
, scope
);
6119 else if (type_decl
== error_mark_node
)
6120 return error_mark_node
;
6122 /* Check that destructor name and scope match. */
6123 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6125 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6126 error_at (token
->location
,
6127 "declaration of %<~%T%> as member of %qT",
6129 cp_parser_simulate_error (parser
);
6130 return error_mark_node
;
6135 A typedef-name that names a class shall not be used as the
6136 identifier in the declarator for a destructor declaration. */
6138 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6139 && !DECL_SELF_REFERENCE_P (type_decl
)
6140 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6141 error_at (token
->location
,
6142 "typedef-name %qD used as destructor declarator",
6145 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6149 if (token
->keyword
== RID_OPERATOR
)
6153 /* This could be a template-id, so we try that first. */
6154 cp_parser_parse_tentatively (parser
);
6155 /* Try a template-id. */
6156 id
= cp_parser_template_id (parser
, template_keyword_p
,
6157 /*check_dependency_p=*/true,
6160 /* If that worked, we're done. */
6161 if (cp_parser_parse_definitely (parser
))
6163 /* We still don't know whether we're looking at an
6164 operator-function-id or a conversion-function-id. */
6165 cp_parser_parse_tentatively (parser
);
6166 /* Try an operator-function-id. */
6167 id
= cp_parser_operator_function_id (parser
);
6168 /* If that didn't work, try a conversion-function-id. */
6169 if (!cp_parser_parse_definitely (parser
))
6170 id
= cp_parser_conversion_function_id (parser
);
6179 cp_parser_error (parser
, "expected unqualified-id");
6180 return error_mark_node
;
6184 /* Parse an (optional) nested-name-specifier.
6186 nested-name-specifier: [C++98]
6187 class-or-namespace-name :: nested-name-specifier [opt]
6188 class-or-namespace-name :: template nested-name-specifier [opt]
6190 nested-name-specifier: [C++0x]
6193 nested-name-specifier identifier ::
6194 nested-name-specifier template [opt] simple-template-id ::
6196 PARSER->SCOPE should be set appropriately before this function is
6197 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6198 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6201 Sets PARSER->SCOPE to the class (TYPE) or namespace
6202 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6203 it unchanged if there is no nested-name-specifier. Returns the new
6204 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6206 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6207 part of a declaration and/or decl-specifier. */
6210 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6211 bool typename_keyword_p
,
6212 bool check_dependency_p
,
6214 bool is_declaration
,
6215 bool template_keyword_p
/* = false */)
6217 bool success
= false;
6218 cp_token_position start
= 0;
6221 /* Remember where the nested-name-specifier starts. */
6222 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6224 start
= cp_lexer_token_position (parser
->lexer
, false);
6225 push_deferring_access_checks (dk_deferred
);
6232 tree saved_qualifying_scope
;
6234 /* Spot cases that cannot be the beginning of a
6235 nested-name-specifier. */
6236 token
= cp_lexer_peek_token (parser
->lexer
);
6238 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6239 the already parsed nested-name-specifier. */
6240 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6242 /* Grab the nested-name-specifier and continue the loop. */
6243 cp_parser_pre_parsed_nested_name_specifier (parser
);
6244 /* If we originally encountered this nested-name-specifier
6245 with IS_DECLARATION set to false, we will not have
6246 resolved TYPENAME_TYPEs, so we must do so here. */
6248 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6250 new_scope
= resolve_typename_type (parser
->scope
,
6251 /*only_current_p=*/false);
6252 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6253 parser
->scope
= new_scope
;
6259 /* Spot cases that cannot be the beginning of a
6260 nested-name-specifier. On the second and subsequent times
6261 through the loop, we look for the `template' keyword. */
6262 if (success
&& token
->keyword
== RID_TEMPLATE
)
6264 /* A template-id can start a nested-name-specifier. */
6265 else if (token
->type
== CPP_TEMPLATE_ID
)
6267 /* DR 743: decltype can be used in a nested-name-specifier. */
6268 else if (token_is_decltype (token
))
6272 /* If the next token is not an identifier, then it is
6273 definitely not a type-name or namespace-name. */
6274 if (token
->type
!= CPP_NAME
)
6276 /* If the following token is neither a `<' (to begin a
6277 template-id), nor a `::', then we are not looking at a
6278 nested-name-specifier. */
6279 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6281 if (token
->type
== CPP_COLON
6282 && parser
->colon_corrects_to_scope_p
6283 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6285 gcc_rich_location
richloc (token
->location
);
6286 richloc
.add_fixit_replace ("::");
6288 "found %<:%> in nested-name-specifier, "
6290 token
->type
= CPP_SCOPE
;
6293 if (token
->type
!= CPP_SCOPE
6294 && !cp_parser_nth_token_starts_template_argument_list_p
6299 /* The nested-name-specifier is optional, so we parse
6301 cp_parser_parse_tentatively (parser
);
6303 /* Look for the optional `template' keyword, if this isn't the
6304 first time through the loop. */
6306 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6308 /* Save the old scope since the name lookup we are about to do
6309 might destroy it. */
6310 old_scope
= parser
->scope
;
6311 saved_qualifying_scope
= parser
->qualifying_scope
;
6312 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6313 look up names in "X<T>::I" in order to determine that "Y" is
6314 a template. So, if we have a typename at this point, we make
6315 an effort to look through it. */
6317 && !typename_keyword_p
6319 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6320 parser
->scope
= resolve_typename_type (parser
->scope
,
6321 /*only_current_p=*/false);
6322 /* Parse the qualifying entity. */
6324 = cp_parser_qualifying_entity (parser
,
6330 /* Look for the `::' token. */
6331 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6333 /* If we found what we wanted, we keep going; otherwise, we're
6335 if (!cp_parser_parse_definitely (parser
))
6337 bool error_p
= false;
6339 /* Restore the OLD_SCOPE since it was valid before the
6340 failed attempt at finding the last
6341 class-or-namespace-name. */
6342 parser
->scope
= old_scope
;
6343 parser
->qualifying_scope
= saved_qualifying_scope
;
6345 /* If the next token is a decltype, and the one after that is a
6346 `::', then the decltype has failed to resolve to a class or
6347 enumeration type. Give this error even when parsing
6348 tentatively since it can't possibly be valid--and we're going
6349 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6350 won't get another chance.*/
6351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6352 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6355 token
= cp_lexer_consume_token (parser
->lexer
);
6356 error_at (token
->location
, "decltype evaluates to %qT, "
6357 "which is not a class or enumeration type",
6358 token
->u
.tree_check_value
->value
);
6359 parser
->scope
= error_mark_node
;
6363 cp_lexer_consume_token (parser
->lexer
);
6366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6367 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6369 /* If we have a non-type template-id followed by ::, it can't
6370 possibly be valid. */
6371 token
= cp_lexer_peek_token (parser
->lexer
);
6372 tree tid
= token
->u
.tree_check_value
->value
;
6373 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6374 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6376 tree tmpl
= NULL_TREE
;
6377 if (is_overloaded_fn (tid
))
6379 tree fns
= get_fns (tid
);
6380 if (OVL_SINGLE_P (fns
))
6381 tmpl
= OVL_FIRST (fns
);
6382 error_at (token
->location
, "function template-id %qD "
6383 "in nested-name-specifier", tid
);
6387 /* Variable template. */
6388 tmpl
= TREE_OPERAND (tid
, 0);
6389 gcc_assert (variable_template_p (tmpl
));
6390 error_at (token
->location
, "variable template-id %qD "
6391 "in nested-name-specifier", tid
);
6394 inform (DECL_SOURCE_LOCATION (tmpl
),
6395 "%qD declared here", tmpl
);
6397 parser
->scope
= error_mark_node
;
6401 cp_lexer_consume_token (parser
->lexer
);
6402 cp_lexer_consume_token (parser
->lexer
);
6406 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6408 /* If the next token is an identifier, and the one after
6409 that is a `::', then any valid interpretation would have
6410 found a class-or-namespace-name. */
6411 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6412 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6414 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6417 token
= cp_lexer_consume_token (parser
->lexer
);
6420 if (!token
->error_reported
)
6423 tree ambiguous_decls
;
6425 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6427 /*is_template=*/false,
6428 /*is_namespace=*/false,
6429 /*check_dependency=*/true,
6432 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6433 error_at (token
->location
,
6434 "%qD used without template arguments",
6436 else if (ambiguous_decls
)
6438 // cp_parser_lookup_name has the same diagnostic,
6439 // thus make sure to emit it at most once.
6440 if (cp_parser_uncommitted_to_tentative_parse_p
6443 error_at (token
->location
,
6444 "reference to %qD is ambiguous",
6446 print_candidates (ambiguous_decls
);
6448 decl
= error_mark_node
;
6452 if (cxx_dialect
!= cxx98
)
6453 cp_parser_name_lookup_error
6454 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6457 cp_parser_name_lookup_error
6458 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6462 parser
->scope
= error_mark_node
;
6464 /* Treat this as a successful nested-name-specifier
6469 If the name found is not a class-name (clause
6470 _class_) or namespace-name (_namespace.def_), the
6471 program is ill-formed. */
6474 cp_lexer_consume_token (parser
->lexer
);
6478 /* We've found one valid nested-name-specifier. */
6480 /* Name lookup always gives us a DECL. */
6481 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6482 new_scope
= TREE_TYPE (new_scope
);
6483 /* Uses of "template" must be followed by actual templates. */
6484 if (template_keyword_p
6485 && !(CLASS_TYPE_P (new_scope
)
6486 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6487 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6488 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6489 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6490 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6491 == TEMPLATE_ID_EXPR
)))
6492 permerror (input_location
, TYPE_P (new_scope
)
6493 ? G_("%qT is not a template")
6494 : G_("%qD is not a template"),
6496 /* If it is a class scope, try to complete it; we are about to
6497 be looking up names inside the class. */
6498 if (TYPE_P (new_scope
)
6499 /* Since checking types for dependency can be expensive,
6500 avoid doing it if the type is already complete. */
6501 && !COMPLETE_TYPE_P (new_scope
)
6502 /* Do not try to complete dependent types. */
6503 && !dependent_type_p (new_scope
))
6505 new_scope
= complete_type (new_scope
);
6506 /* If it is a typedef to current class, use the current
6507 class instead, as the typedef won't have any names inside
6509 if (!COMPLETE_TYPE_P (new_scope
)
6510 && currently_open_class (new_scope
))
6511 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6513 /* Make sure we look in the right scope the next time through
6515 parser
->scope
= new_scope
;
6518 /* If parsing tentatively, replace the sequence of tokens that makes
6519 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6520 token. That way, should we re-parse the token stream, we will
6521 not have to repeat the effort required to do the parse, nor will
6522 we issue duplicate error messages. */
6523 if (success
&& start
)
6527 token
= cp_lexer_token_at (parser
->lexer
, start
);
6528 /* Reset the contents of the START token. */
6529 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6530 /* Retrieve any deferred checks. Do not pop this access checks yet
6531 so the memory will not be reclaimed during token replacing below. */
6532 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6533 token
->u
.tree_check_value
->value
= parser
->scope
;
6534 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6535 token
->u
.tree_check_value
->qualifying_scope
=
6536 parser
->qualifying_scope
;
6537 token
->keyword
= RID_MAX
;
6539 /* Purge all subsequent tokens. */
6540 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6544 pop_to_parent_deferring_access_checks ();
6546 return success
? parser
->scope
: NULL_TREE
;
6549 /* Parse a nested-name-specifier. See
6550 cp_parser_nested_name_specifier_opt for details. This function
6551 behaves identically, except that it will an issue an error if no
6552 nested-name-specifier is present. */
6555 cp_parser_nested_name_specifier (cp_parser
*parser
,
6556 bool typename_keyword_p
,
6557 bool check_dependency_p
,
6559 bool is_declaration
)
6563 /* Look for the nested-name-specifier. */
6564 scope
= cp_parser_nested_name_specifier_opt (parser
,
6569 /* If it was not present, issue an error message. */
6572 cp_parser_error (parser
, "expected nested-name-specifier");
6573 parser
->scope
= NULL_TREE
;
6579 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6580 this is either a class-name or a namespace-name (which corresponds
6581 to the class-or-namespace-name production in the grammar). For
6582 C++0x, it can also be a type-name that refers to an enumeration
6583 type or a simple-template-id.
6585 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6586 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6587 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6588 TYPE_P is TRUE iff the next name should be taken as a class-name,
6589 even the same name is declared to be another entity in the same
6592 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6593 specified by the class-or-namespace-name. If neither is found the
6594 ERROR_MARK_NODE is returned. */
6597 cp_parser_qualifying_entity (cp_parser
*parser
,
6598 bool typename_keyword_p
,
6599 bool template_keyword_p
,
6600 bool check_dependency_p
,
6602 bool is_declaration
)
6605 tree saved_qualifying_scope
;
6606 tree saved_object_scope
;
6609 bool successful_parse_p
;
6611 /* DR 743: decltype can appear in a nested-name-specifier. */
6612 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6614 scope
= cp_parser_decltype (parser
);
6615 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6616 && !MAYBE_CLASS_TYPE_P (scope
))
6618 cp_parser_simulate_error (parser
);
6619 return error_mark_node
;
6621 if (TYPE_NAME (scope
))
6622 scope
= TYPE_NAME (scope
);
6626 /* Before we try to parse the class-name, we must save away the
6627 current PARSER->SCOPE since cp_parser_class_name will destroy
6629 saved_scope
= parser
->scope
;
6630 saved_qualifying_scope
= parser
->qualifying_scope
;
6631 saved_object_scope
= parser
->object_scope
;
6632 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6633 there is no need to look for a namespace-name. */
6634 only_class_p
= template_keyword_p
6635 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6637 cp_parser_parse_tentatively (parser
);
6638 scope
= cp_parser_class_name (parser
,
6641 type_p
? class_type
: none_type
,
6643 /*class_head_p=*/false,
6645 /*enum_ok=*/cxx_dialect
> cxx98
);
6646 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6647 /* If that didn't work, try for a namespace-name. */
6648 if (!only_class_p
&& !successful_parse_p
)
6650 /* Restore the saved scope. */
6651 parser
->scope
= saved_scope
;
6652 parser
->qualifying_scope
= saved_qualifying_scope
;
6653 parser
->object_scope
= saved_object_scope
;
6654 /* If we are not looking at an identifier followed by the scope
6655 resolution operator, then this is not part of a
6656 nested-name-specifier. (Note that this function is only used
6657 to parse the components of a nested-name-specifier.) */
6658 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6659 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6660 return error_mark_node
;
6661 scope
= cp_parser_namespace_name (parser
);
6667 /* Return true if we are looking at a compound-literal, false otherwise. */
6670 cp_parser_compound_literal_p (cp_parser
*parser
)
6672 cp_lexer_save_tokens (parser
->lexer
);
6674 /* Skip tokens until the next token is a closing parenthesis.
6675 If we find the closing `)', and the next token is a `{', then
6676 we are looking at a compound-literal. */
6677 bool compound_literal_p
6678 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6679 /*consume_paren=*/true)
6680 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6682 /* Roll back the tokens we skipped. */
6683 cp_lexer_rollback_tokens (parser
->lexer
);
6685 return compound_literal_p
;
6688 /* Return true if EXPR is the integer constant zero or a complex constant
6689 of zero, without any folding, but ignoring location wrappers. */
6692 literal_integer_zerop (const_tree expr
)
6694 return (location_wrapper_p (expr
)
6695 && integer_zerop (TREE_OPERAND (expr
, 0)));
6698 /* Parse a postfix-expression.
6702 postfix-expression [ expression ]
6703 postfix-expression ( expression-list [opt] )
6704 simple-type-specifier ( expression-list [opt] )
6705 typename :: [opt] nested-name-specifier identifier
6706 ( expression-list [opt] )
6707 typename :: [opt] nested-name-specifier template [opt] template-id
6708 ( expression-list [opt] )
6709 postfix-expression . template [opt] id-expression
6710 postfix-expression -> template [opt] id-expression
6711 postfix-expression . pseudo-destructor-name
6712 postfix-expression -> pseudo-destructor-name
6713 postfix-expression ++
6714 postfix-expression --
6715 dynamic_cast < type-id > ( expression )
6716 static_cast < type-id > ( expression )
6717 reinterpret_cast < type-id > ( expression )
6718 const_cast < type-id > ( expression )
6719 typeid ( expression )
6725 ( type-id ) { initializer-list , [opt] }
6727 This extension is a GNU version of the C99 compound-literal
6728 construct. (The C99 grammar uses `type-name' instead of `type-id',
6729 but they are essentially the same concept.)
6731 If ADDRESS_P is true, the postfix expression is the operand of the
6732 `&' operator. CAST_P is true if this expression is the target of a
6735 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6736 class member access expressions [expr.ref].
6738 Returns a representation of the expression. */
6741 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6742 bool member_access_only_p
, bool decltype_p
,
6743 cp_id_kind
* pidk_return
)
6748 cp_id_kind idk
= CP_ID_KIND_NONE
;
6749 cp_expr postfix_expression
= NULL_TREE
;
6750 bool is_member_access
= false;
6752 /* Peek at the next token. */
6753 token
= cp_lexer_peek_token (parser
->lexer
);
6754 loc
= token
->location
;
6755 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6757 /* Some of the productions are determined by keywords. */
6758 keyword
= token
->keyword
;
6768 const char *saved_message
;
6769 bool saved_in_type_id_in_expr_p
;
6771 /* All of these can be handled in the same way from the point
6772 of view of parsing. Begin by consuming the token
6773 identifying the cast. */
6774 cp_lexer_consume_token (parser
->lexer
);
6776 /* New types cannot be defined in the cast. */
6777 saved_message
= parser
->type_definition_forbidden_message
;
6778 parser
->type_definition_forbidden_message
6779 = G_("types may not be defined in casts");
6781 /* Look for the opening `<'. */
6782 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6783 /* Parse the type to which we are casting. */
6784 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6785 parser
->in_type_id_in_expr_p
= true;
6786 type
= cp_parser_type_id (parser
);
6787 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6788 /* Look for the closing `>'. */
6789 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6790 /* Restore the old message. */
6791 parser
->type_definition_forbidden_message
= saved_message
;
6793 bool saved_greater_than_is_operator_p
6794 = parser
->greater_than_is_operator_p
;
6795 parser
->greater_than_is_operator_p
= true;
6797 /* And the expression which is being cast. */
6798 matching_parens parens
;
6799 parens
.require_open (parser
);
6800 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6801 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6803 location_t end_loc
= close_paren
?
6804 close_paren
->location
: UNKNOWN_LOCATION
;
6806 parser
->greater_than_is_operator_p
6807 = saved_greater_than_is_operator_p
;
6809 /* Only type conversions to integral or enumeration types
6810 can be used in constant-expressions. */
6811 if (!cast_valid_in_integral_constant_expression_p (type
)
6812 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6814 postfix_expression
= error_mark_node
;
6822 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6826 = build_static_cast (type
, expression
, tf_warning_or_error
);
6830 = build_reinterpret_cast (type
, expression
,
6831 tf_warning_or_error
);
6835 = build_const_cast (type
, expression
, tf_warning_or_error
);
6841 /* Construct a location e.g. :
6842 reinterpret_cast <int *> (expr)
6843 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6844 ranging from the start of the "*_cast" token to the final closing
6845 paren, with the caret at the start. */
6846 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6847 postfix_expression
.set_location (cp_cast_loc
);
6854 const char *saved_message
;
6855 bool saved_in_type_id_in_expr_p
;
6857 /* Consume the `typeid' token. */
6858 cp_lexer_consume_token (parser
->lexer
);
6859 /* Look for the `(' token. */
6860 matching_parens parens
;
6861 parens
.require_open (parser
);
6862 /* Types cannot be defined in a `typeid' expression. */
6863 saved_message
= parser
->type_definition_forbidden_message
;
6864 parser
->type_definition_forbidden_message
6865 = G_("types may not be defined in a %<typeid%> expression");
6866 /* We can't be sure yet whether we're looking at a type-id or an
6868 cp_parser_parse_tentatively (parser
);
6869 /* Try a type-id first. */
6870 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6871 parser
->in_type_id_in_expr_p
= true;
6872 type
= cp_parser_type_id (parser
);
6873 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6874 /* Look for the `)' token. Otherwise, we can't be sure that
6875 we're not looking at an expression: consider `typeid (int
6876 (3))', for example. */
6877 cp_token
*close_paren
= parens
.require_close (parser
);
6878 /* If all went well, simply lookup the type-id. */
6879 if (cp_parser_parse_definitely (parser
))
6880 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6881 /* Otherwise, fall back to the expression variant. */
6886 /* Look for an expression. */
6887 expression
= cp_parser_expression (parser
, & idk
);
6888 /* Compute its typeid. */
6889 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6890 /* Look for the `)' token. */
6891 close_paren
= parens
.require_close (parser
);
6893 /* Restore the saved message. */
6894 parser
->type_definition_forbidden_message
= saved_message
;
6895 /* `typeid' may not appear in an integral constant expression. */
6896 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6897 postfix_expression
= error_mark_node
;
6899 /* Construct a location e.g. :
6902 ranging from the start of the "typeid" token to the final closing
6903 paren, with the caret at the start. */
6906 location_t typeid_loc
6907 = make_location (start_loc
, start_loc
, close_paren
->location
);
6908 postfix_expression
.set_location (typeid_loc
);
6909 postfix_expression
.maybe_add_location_wrapper ();
6917 /* The syntax permitted here is the same permitted for an
6918 elaborated-type-specifier. */
6919 ++parser
->prevent_constrained_type_specifiers
;
6920 type
= cp_parser_elaborated_type_specifier (parser
,
6921 /*is_friend=*/false,
6922 /*is_declaration=*/false);
6923 --parser
->prevent_constrained_type_specifiers
;
6924 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6929 case RID_BUILTIN_SHUFFLE
:
6930 case RID_BUILTIN_LAUNDER
:
6932 vec
<tree
, va_gc
> *vec
;
6936 cp_lexer_consume_token (parser
->lexer
);
6937 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6938 /*cast_p=*/false, /*allow_expansion_p=*/true,
6939 /*non_constant_p=*/NULL
);
6942 postfix_expression
= error_mark_node
;
6946 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6952 if (vec
->length () == 1)
6954 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6957 error_at (loc
, "wrong number of arguments to "
6958 "%<__builtin_addressof%>");
6959 postfix_expression
= error_mark_node
;
6963 case RID_BUILTIN_LAUNDER
:
6964 if (vec
->length () == 1)
6965 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6966 tf_warning_or_error
);
6969 error_at (loc
, "wrong number of arguments to "
6970 "%<__builtin_launder%>");
6971 postfix_expression
= error_mark_node
;
6975 case RID_BUILTIN_SHUFFLE
:
6976 if (vec
->length () == 2)
6978 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6979 (*vec
)[1], tf_warning_or_error
);
6980 else if (vec
->length () == 3)
6982 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6983 (*vec
)[2], tf_warning_or_error
);
6986 error_at (loc
, "wrong number of arguments to "
6987 "%<__builtin_shuffle%>");
6988 postfix_expression
= error_mark_node
;
7002 /* If the next thing is a simple-type-specifier, we may be
7003 looking at a functional cast. We could also be looking at
7004 an id-expression. So, we try the functional cast, and if
7005 that doesn't work we fall back to the primary-expression. */
7006 cp_parser_parse_tentatively (parser
);
7007 /* Look for the simple-type-specifier. */
7008 ++parser
->prevent_constrained_type_specifiers
;
7009 type
= cp_parser_simple_type_specifier (parser
,
7010 /*decl_specs=*/NULL
,
7011 CP_PARSER_FLAGS_NONE
);
7012 --parser
->prevent_constrained_type_specifiers
;
7013 /* Parse the cast itself. */
7014 if (!cp_parser_error_occurred (parser
))
7016 = cp_parser_functional_cast (parser
, type
);
7017 /* If that worked, we're done. */
7018 if (cp_parser_parse_definitely (parser
))
7021 /* If the functional-cast didn't work out, try a
7022 compound-literal. */
7023 if (cp_parser_allow_gnu_extensions_p (parser
)
7024 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7026 cp_expr initializer
= NULL_TREE
;
7028 cp_parser_parse_tentatively (parser
);
7030 matching_parens parens
;
7031 parens
.consume_open (parser
);
7033 /* Avoid calling cp_parser_type_id pointlessly, see comment
7034 in cp_parser_cast_expression about c++/29234. */
7035 if (!cp_parser_compound_literal_p (parser
))
7036 cp_parser_simulate_error (parser
);
7039 /* Parse the type. */
7040 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7041 parser
->in_type_id_in_expr_p
= true;
7042 type
= cp_parser_type_id (parser
);
7043 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7044 parens
.require_close (parser
);
7047 /* If things aren't going well, there's no need to
7049 if (!cp_parser_error_occurred (parser
))
7051 bool non_constant_p
;
7052 /* Parse the brace-enclosed initializer list. */
7053 initializer
= cp_parser_braced_list (parser
,
7056 /* If that worked, we're definitely looking at a
7057 compound-literal expression. */
7058 if (cp_parser_parse_definitely (parser
))
7060 /* Warn the user that a compound literal is not
7061 allowed in standard C++. */
7062 pedwarn (input_location
, OPT_Wpedantic
,
7063 "ISO C++ forbids compound-literals");
7064 /* For simplicity, we disallow compound literals in
7065 constant-expressions. We could
7066 allow compound literals of integer type, whose
7067 initializer was a constant, in constant
7068 expressions. Permitting that usage, as a further
7069 extension, would not change the meaning of any
7070 currently accepted programs. (Of course, as
7071 compound literals are not part of ISO C++, the
7072 standard has nothing to say.) */
7073 if (cp_parser_non_integral_constant_expression (parser
,
7076 postfix_expression
= error_mark_node
;
7079 /* Form the representation of the compound-literal. */
7081 = finish_compound_literal (type
, initializer
,
7082 tf_warning_or_error
, fcl_c99
);
7083 postfix_expression
.set_location (initializer
.get_location ());
7088 /* It must be a primary-expression. */
7090 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7091 /*template_arg_p=*/false,
7098 /* Note that we don't need to worry about calling build_cplus_new on a
7099 class-valued CALL_EXPR in decltype when it isn't the end of the
7100 postfix-expression; unary_complex_lvalue will take care of that for
7103 /* Keep looping until the postfix-expression is complete. */
7106 if (idk
== CP_ID_KIND_UNQUALIFIED
7107 && identifier_p (postfix_expression
)
7108 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7109 /* It is not a Koenig lookup function call. */
7111 = unqualified_name_lookup_error (postfix_expression
);
7113 /* Peek at the next token. */
7114 token
= cp_lexer_peek_token (parser
->lexer
);
7116 switch (token
->type
)
7118 case CPP_OPEN_SQUARE
:
7119 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7121 cp_parser_error (parser
,
7122 "two consecutive %<[%> shall "
7123 "only introduce an attribute");
7124 return error_mark_node
;
7127 = cp_parser_postfix_open_square_expression (parser
,
7131 postfix_expression
.set_range (start_loc
,
7132 postfix_expression
.get_location ());
7134 idk
= CP_ID_KIND_NONE
;
7135 is_member_access
= false;
7138 case CPP_OPEN_PAREN
:
7139 /* postfix-expression ( expression-list [opt] ) */
7142 bool is_builtin_constant_p
;
7143 bool saved_integral_constant_expression_p
= false;
7144 bool saved_non_integral_constant_expression_p
= false;
7145 tsubst_flags_t complain
= complain_flags (decltype_p
);
7146 vec
<tree
, va_gc
> *args
;
7147 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7149 is_member_access
= false;
7151 is_builtin_constant_p
7152 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7153 if (is_builtin_constant_p
)
7155 /* The whole point of __builtin_constant_p is to allow
7156 non-constant expressions to appear as arguments. */
7157 saved_integral_constant_expression_p
7158 = parser
->integral_constant_expression_p
;
7159 saved_non_integral_constant_expression_p
7160 = parser
->non_integral_constant_expression_p
;
7161 parser
->integral_constant_expression_p
= false;
7163 args
= (cp_parser_parenthesized_expression_list
7165 /*cast_p=*/false, /*allow_expansion_p=*/true,
7166 /*non_constant_p=*/NULL
,
7167 /*close_paren_loc=*/&close_paren_loc
,
7168 /*wrap_locations_p=*/true));
7169 if (is_builtin_constant_p
)
7171 parser
->integral_constant_expression_p
7172 = saved_integral_constant_expression_p
;
7173 parser
->non_integral_constant_expression_p
7174 = saved_non_integral_constant_expression_p
;
7179 postfix_expression
= error_mark_node
;
7183 /* Function calls are not permitted in
7184 constant-expressions. */
7185 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7186 && cp_parser_non_integral_constant_expression (parser
,
7189 postfix_expression
= error_mark_node
;
7190 release_tree_vector (args
);
7195 if (idk
== CP_ID_KIND_UNQUALIFIED
7196 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7198 if (identifier_p (postfix_expression
)
7199 /* In C++2A, we may need to perform ADL for a template
7201 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7202 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7204 if (!args
->is_empty ())
7207 if (!any_type_dependent_arguments_p (args
))
7209 = perform_koenig_lookup (postfix_expression
, args
,
7214 = unqualified_fn_lookup_error (postfix_expression
);
7216 /* We do not perform argument-dependent lookup if
7217 normal lookup finds a non-function, in accordance
7218 with the expected resolution of DR 218. */
7219 else if (!args
->is_empty ()
7220 && is_overloaded_fn (postfix_expression
))
7222 tree fn
= get_first_fn (postfix_expression
);
7223 fn
= STRIP_TEMPLATE (fn
);
7225 /* Do not do argument dependent lookup if regular
7226 lookup finds a member function or a block-scope
7227 function declaration. [basic.lookup.argdep]/3 */
7228 if (!DECL_FUNCTION_MEMBER_P (fn
)
7229 && !DECL_LOCAL_FUNCTION_P (fn
))
7232 if (!any_type_dependent_arguments_p (args
))
7234 = perform_koenig_lookup (postfix_expression
, args
,
7240 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7242 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7243 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7245 if (processing_template_decl
7246 && (type_dependent_object_expression_p (instance
)
7247 || (!BASELINK_P (fn
)
7248 && TREE_CODE (fn
) != FIELD_DECL
)
7249 || type_dependent_expression_p (fn
)
7250 || any_type_dependent_arguments_p (args
)))
7252 maybe_generic_this_capture (instance
, fn
);
7254 = build_min_nt_call_vec (postfix_expression
, args
);
7255 release_tree_vector (args
);
7259 if (BASELINK_P (fn
))
7262 = (build_new_method_call
7263 (instance
, fn
, &args
, NULL_TREE
,
7264 (idk
== CP_ID_KIND_QUALIFIED
7265 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7272 = finish_call_expr (postfix_expression
, &args
,
7273 /*disallow_virtual=*/false,
7277 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7278 || TREE_CODE (postfix_expression
) == MEMBER_REF
7279 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7280 postfix_expression
= (build_offset_ref_call_from_tree
7281 (postfix_expression
, &args
,
7283 else if (idk
== CP_ID_KIND_QUALIFIED
)
7284 /* A call to a static class member, or a namespace-scope
7287 = finish_call_expr (postfix_expression
, &args
,
7288 /*disallow_virtual=*/true,
7292 /* All other function calls. */
7294 = finish_call_expr (postfix_expression
, &args
,
7295 /*disallow_virtual=*/false,
7299 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7301 location_t combined_loc
= make_location (token
->location
,
7304 postfix_expression
.set_location (combined_loc
);
7307 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7308 idk
= CP_ID_KIND_NONE
;
7310 release_tree_vector (args
);
7316 /* postfix-expression . template [opt] id-expression
7317 postfix-expression . pseudo-destructor-name
7318 postfix-expression -> template [opt] id-expression
7319 postfix-expression -> pseudo-destructor-name */
7321 /* Consume the `.' or `->' operator. */
7322 cp_lexer_consume_token (parser
->lexer
);
7325 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7329 is_member_access
= true;
7333 /* postfix-expression ++ */
7334 /* Consume the `++' token. */
7335 cp_lexer_consume_token (parser
->lexer
);
7336 /* Generate a representation for the complete expression. */
7338 = finish_increment_expr (postfix_expression
,
7339 POSTINCREMENT_EXPR
);
7340 /* Increments may not appear in constant-expressions. */
7341 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7342 postfix_expression
= error_mark_node
;
7343 idk
= CP_ID_KIND_NONE
;
7344 is_member_access
= false;
7347 case CPP_MINUS_MINUS
:
7348 /* postfix-expression -- */
7349 /* Consume the `--' token. */
7350 cp_lexer_consume_token (parser
->lexer
);
7351 /* Generate a representation for the complete expression. */
7353 = finish_increment_expr (postfix_expression
,
7354 POSTDECREMENT_EXPR
);
7355 /* Decrements may not appear in constant-expressions. */
7356 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7357 postfix_expression
= error_mark_node
;
7358 idk
= CP_ID_KIND_NONE
;
7359 is_member_access
= false;
7363 if (pidk_return
!= NULL
)
7364 * pidk_return
= idk
;
7365 if (member_access_only_p
)
7366 return is_member_access
7367 ? postfix_expression
7368 : cp_expr (error_mark_node
);
7370 return postfix_expression
;
7374 /* We should never get here. */
7376 return error_mark_node
;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression [ expression ]
7383 postfix-expression [ braced-init-list ] (C++11)
7385 FOR_OFFSETOF is set if we're being called in that context, which
7386 changes how we deal with integer constant expressions. */
7389 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7390 tree postfix_expression
,
7394 tree index
= NULL_TREE
;
7395 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7396 bool saved_greater_than_is_operator_p
;
7398 /* Consume the `[' token. */
7399 cp_lexer_consume_token (parser
->lexer
);
7401 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7402 parser
->greater_than_is_operator_p
= true;
7404 /* Parse the index expression. */
7405 /* ??? For offsetof, there is a question of what to allow here. If
7406 offsetof is not being used in an integral constant expression context,
7407 then we *could* get the right answer by computing the value at runtime.
7408 If we are in an integral constant expression context, then we might
7409 could accept any constant expression; hard to say without analysis.
7410 Rather than open the barn door too wide right away, allow only integer
7411 constant expressions here. */
7413 index
= cp_parser_constant_expression (parser
);
7416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7418 bool expr_nonconst_p
;
7419 cp_lexer_set_source_position (parser
->lexer
);
7420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7421 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7424 index
= cp_parser_expression (parser
);
7427 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7429 /* Look for the closing `]'. */
7430 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7432 /* Build the ARRAY_REF. */
7433 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7436 /* When not doing offsetof, array references are not permitted in
7437 constant-expressions. */
7439 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7440 postfix_expression
= error_mark_node
;
7442 return postfix_expression
;
7445 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7446 dereference of incomplete type, returns true if error_mark_node should
7447 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7448 and *DEPENDENT_P. */
7451 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7454 /* In a template, be permissive by treating an object expression
7455 of incomplete type as dependent (after a pedwarn). */
7456 diagnostic_t kind
= (processing_template_decl
7457 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7459 switch (TREE_CODE (*postfix_expression
))
7462 case REINTERPRET_CAST_EXPR
:
7463 case CONST_CAST_EXPR
:
7464 case STATIC_CAST_EXPR
:
7465 case DYNAMIC_CAST_EXPR
:
7466 case IMPLICIT_CONV_EXPR
:
7467 case VIEW_CONVERT_EXPR
:
7468 case NON_LVALUE_EXPR
:
7472 /* Don't emit any diagnostic for OVERLOADs. */
7476 /* Avoid clobbering e.g. DECLs. */
7477 if (!EXPR_P (*postfix_expression
))
7482 if (kind
== DK_IGNORED
)
7485 location_t exploc
= location_of (*postfix_expression
);
7486 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7487 if (!MAYBE_CLASS_TYPE_P (*scope
))
7489 if (kind
== DK_ERROR
)
7490 *scope
= *postfix_expression
= error_mark_node
;
7491 else if (processing_template_decl
)
7493 *dependent_p
= true;
7494 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7499 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7500 by cp_parser_builtin_offsetof. We're looking for
7502 postfix-expression . template [opt] id-expression
7503 postfix-expression . pseudo-destructor-name
7504 postfix-expression -> template [opt] id-expression
7505 postfix-expression -> pseudo-destructor-name
7507 FOR_OFFSETOF is set if we're being called in that context. That sorta
7508 limits what of the above we'll actually accept, but nevermind.
7509 TOKEN_TYPE is the "." or "->" token, which will already have been
7510 removed from the stream. */
7513 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7514 enum cpp_ttype token_type
,
7515 cp_expr postfix_expression
,
7516 bool for_offsetof
, cp_id_kind
*idk
,
7517 location_t location
)
7521 bool pseudo_destructor_p
;
7522 tree scope
= NULL_TREE
;
7523 location_t start_loc
= postfix_expression
.get_start ();
7525 /* If this is a `->' operator, dereference the pointer. */
7526 if (token_type
== CPP_DEREF
)
7527 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7528 tf_warning_or_error
);
7529 /* Check to see whether or not the expression is type-dependent and
7530 not the current instantiation. */
7531 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7532 /* The identifier following the `->' or `.' is not qualified. */
7533 parser
->scope
= NULL_TREE
;
7534 parser
->qualifying_scope
= NULL_TREE
;
7535 parser
->object_scope
= NULL_TREE
;
7536 *idk
= CP_ID_KIND_NONE
;
7538 /* Enter the scope corresponding to the type of the object
7539 given by the POSTFIX_EXPRESSION. */
7542 scope
= TREE_TYPE (postfix_expression
);
7543 /* According to the standard, no expression should ever have
7544 reference type. Unfortunately, we do not currently match
7545 the standard in this respect in that our internal representation
7546 of an expression may have reference type even when the standard
7547 says it does not. Therefore, we have to manually obtain the
7548 underlying type here. */
7549 scope
= non_reference (scope
);
7550 /* The type of the POSTFIX_EXPRESSION must be complete. */
7551 /* Unlike the object expression in other contexts, *this is not
7552 required to be of complete type for purposes of class member
7553 access (5.2.5) outside the member function body. */
7554 if (postfix_expression
!= current_class_ref
7555 && scope
!= error_mark_node
7556 && !currently_open_class (scope
))
7558 scope
= complete_type (scope
);
7559 if (!COMPLETE_TYPE_P (scope
)
7560 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7562 return error_mark_node
;
7567 /* Let the name lookup machinery know that we are processing a
7568 class member access expression. */
7569 parser
->context
->object_type
= scope
;
7570 /* If something went wrong, we want to be able to discern that case,
7571 as opposed to the case where there was no SCOPE due to the type
7572 of expression being dependent. */
7574 scope
= error_mark_node
;
7575 /* If the SCOPE was erroneous, make the various semantic analysis
7576 functions exit quickly -- and without issuing additional error
7578 if (scope
== error_mark_node
)
7579 postfix_expression
= error_mark_node
;
7584 /* Tell cp_parser_lookup_name that there was an object, even though it's
7586 parser
->context
->object_type
= unknown_type_node
;
7588 /* Assume this expression is not a pseudo-destructor access. */
7589 pseudo_destructor_p
= false;
7591 /* If the SCOPE is a scalar type, then, if this is a valid program,
7592 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7593 is type dependent, it can be pseudo-destructor-name or something else.
7594 Try to parse it as pseudo-destructor-name first. */
7595 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7600 cp_parser_parse_tentatively (parser
);
7601 /* Parse the pseudo-destructor-name. */
7603 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7606 && (cp_parser_error_occurred (parser
)
7607 || !SCALAR_TYPE_P (type
)))
7608 cp_parser_abort_tentative_parse (parser
);
7609 else if (cp_parser_parse_definitely (parser
))
7611 pseudo_destructor_p
= true;
7613 = finish_pseudo_destructor_expr (postfix_expression
,
7618 if (!pseudo_destructor_p
)
7620 /* If the SCOPE is not a scalar type, we are looking at an
7621 ordinary class member access expression, rather than a
7622 pseudo-destructor-name. */
7624 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7625 /* Parse the id-expression. */
7626 name
= (cp_parser_id_expression
7628 cp_parser_optional_template_keyword (parser
),
7629 /*check_dependency_p=*/true,
7631 /*declarator_p=*/false,
7632 /*optional_p=*/false));
7633 /* In general, build a SCOPE_REF if the member name is qualified.
7634 However, if the name was not dependent and has already been
7635 resolved; there is no need to build the SCOPE_REF. For example;
7637 struct X { void f(); };
7638 template <typename T> void f(T* t) { t->X::f(); }
7640 Even though "t" is dependent, "X::f" is not and has been resolved
7641 to a BASELINK; there is no need to include scope information. */
7643 /* But we do need to remember that there was an explicit scope for
7644 virtual function calls. */
7646 *idk
= CP_ID_KIND_QUALIFIED
;
7648 /* If the name is a template-id that names a type, we will get a
7649 TYPE_DECL here. That is invalid code. */
7650 if (TREE_CODE (name
) == TYPE_DECL
)
7652 error_at (token
->location
, "invalid use of %qD", name
);
7653 postfix_expression
= error_mark_node
;
7657 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7659 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7661 error_at (token
->location
, "%<%D::%D%> is not a class member",
7662 parser
->scope
, name
);
7663 postfix_expression
= error_mark_node
;
7666 name
= build_qualified_name (/*type=*/NULL_TREE
,
7670 parser
->scope
= NULL_TREE
;
7671 parser
->qualifying_scope
= NULL_TREE
;
7672 parser
->object_scope
= NULL_TREE
;
7674 if (parser
->scope
&& name
&& BASELINK_P (name
))
7675 adjust_result_of_qualified_name_lookup
7676 (name
, parser
->scope
, scope
);
7678 = finish_class_member_access_expr (postfix_expression
, name
,
7680 tf_warning_or_error
);
7681 /* Build a location e.g.:
7684 where the caret is at the deref token, ranging from
7685 the start of postfix_expression to the end of the access expr. */
7687 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7688 location_t combined_loc
7689 = make_location (input_location
, start_loc
, end_loc
);
7690 protected_set_expr_location (postfix_expression
, combined_loc
);
7694 /* We no longer need to look up names in the scope of the object on
7695 the left-hand side of the `.' or `->' operator. */
7696 parser
->context
->object_type
= NULL_TREE
;
7698 /* Outside of offsetof, these operators may not appear in
7699 constant-expressions. */
7701 && (cp_parser_non_integral_constant_expression
7702 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7703 postfix_expression
= error_mark_node
;
7705 return postfix_expression
;
7708 /* Parse a parenthesized expression-list.
7711 assignment-expression
7712 expression-list, assignment-expression
7717 identifier, expression-list
7719 CAST_P is true if this expression is the target of a cast.
7721 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7724 WRAP_LOCATIONS_P is true if expressions within this list for which
7725 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7726 their source locations.
7728 Returns a vector of trees. Each element is a representation of an
7729 assignment-expression. NULL is returned if the ( and or ) are
7730 missing. An empty, but allocated, vector is returned on no
7731 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7732 if we are parsing an attribute list for an attribute that wants a
7733 plain identifier argument, normal_attr for an attribute that wants
7734 an expression, or non_attr if we aren't parsing an attribute list. If
7735 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7736 not all of the expressions in the list were constant.
7737 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7738 will be written to with the location of the closing parenthesis. If
7739 an error occurs, it may or may not be written to. */
7741 static vec
<tree
, va_gc
> *
7742 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7743 int is_attribute_list
,
7745 bool allow_expansion_p
,
7746 bool *non_constant_p
,
7747 location_t
*close_paren_loc
,
7748 bool wrap_locations_p
)
7750 vec
<tree
, va_gc
> *expression_list
;
7751 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7752 tree identifier
= NULL_TREE
;
7753 bool saved_greater_than_is_operator_p
;
7755 /* Assume all the expressions will be constant. */
7757 *non_constant_p
= false;
7759 matching_parens parens
;
7760 if (!parens
.require_open (parser
))
7763 expression_list
= make_tree_vector ();
7765 /* Within a parenthesized expression, a `>' token is always
7766 the greater-than operator. */
7767 saved_greater_than_is_operator_p
7768 = parser
->greater_than_is_operator_p
;
7769 parser
->greater_than_is_operator_p
= true;
7771 cp_expr
expr (NULL_TREE
);
7773 /* Consume expressions until there are no more. */
7774 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7777 /* At the beginning of attribute lists, check to see if the
7778 next token is an identifier. */
7779 if (is_attribute_list
== id_attr
7780 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7784 /* Consume the identifier. */
7785 token
= cp_lexer_consume_token (parser
->lexer
);
7786 /* Save the identifier. */
7787 identifier
= token
->u
.value
;
7791 bool expr_non_constant_p
;
7793 /* Parse the next assignment-expression. */
7794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7796 /* A braced-init-list. */
7797 cp_lexer_set_source_position (parser
->lexer
);
7798 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7799 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7800 if (non_constant_p
&& expr_non_constant_p
)
7801 *non_constant_p
= true;
7803 else if (non_constant_p
)
7805 expr
= (cp_parser_constant_expression
7806 (parser
, /*allow_non_constant_p=*/true,
7807 &expr_non_constant_p
));
7808 if (expr_non_constant_p
)
7809 *non_constant_p
= true;
7812 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7816 expr
= instantiate_non_dependent_expr (expr
);
7818 /* If we have an ellipsis, then this is an expression
7820 if (allow_expansion_p
7821 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7823 /* Consume the `...'. */
7824 cp_lexer_consume_token (parser
->lexer
);
7826 /* Build the argument pack. */
7827 expr
= make_pack_expansion (expr
);
7830 if (wrap_locations_p
)
7831 expr
.maybe_add_location_wrapper ();
7833 /* Add it to the list. We add error_mark_node
7834 expressions to the list, so that we can still tell if
7835 the correct form for a parenthesized expression-list
7836 is found. That gives better errors. */
7837 vec_safe_push (expression_list
, expr
.get_value ());
7839 if (expr
== error_mark_node
)
7843 /* After the first item, attribute lists look the same as
7844 expression lists. */
7845 is_attribute_list
= non_attr
;
7848 /* If the next token isn't a `,', then we are done. */
7849 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7852 /* Otherwise, consume the `,' and keep going. */
7853 cp_lexer_consume_token (parser
->lexer
);
7856 if (close_paren_loc
)
7857 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7859 if (!parens
.require_close (parser
))
7864 /* We try and resync to an unnested comma, as that will give the
7865 user better diagnostics. */
7866 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7867 /*recovering=*/true,
7869 /*consume_paren=*/true);
7874 parser
->greater_than_is_operator_p
7875 = saved_greater_than_is_operator_p
;
7880 parser
->greater_than_is_operator_p
7881 = saved_greater_than_is_operator_p
;
7884 vec_safe_insert (expression_list
, 0, identifier
);
7886 return expression_list
;
7889 /* Parse a pseudo-destructor-name.
7891 pseudo-destructor-name:
7892 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7893 :: [opt] nested-name-specifier template template-id :: ~ type-name
7894 :: [opt] nested-name-specifier [opt] ~ type-name
7896 If either of the first two productions is used, sets *SCOPE to the
7897 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7898 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7899 or ERROR_MARK_NODE if the parse fails. */
7902 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7907 bool nested_name_specifier_p
;
7910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7911 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7912 && !type_dependent_expression_p (object
))
7914 if (cxx_dialect
< cxx14
)
7915 pedwarn (input_location
, 0,
7916 "%<~auto%> only available with "
7917 "-std=c++14 or -std=gnu++14");
7918 cp_lexer_consume_token (parser
->lexer
);
7919 cp_lexer_consume_token (parser
->lexer
);
7921 *type
= TREE_TYPE (object
);
7925 /* Assume that things will not work out. */
7926 *type
= error_mark_node
;
7928 /* Look for the optional `::' operator. */
7929 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7930 /* Look for the optional nested-name-specifier. */
7931 nested_name_specifier_p
7932 = (cp_parser_nested_name_specifier_opt (parser
,
7933 /*typename_keyword_p=*/false,
7934 /*check_dependency_p=*/true,
7936 /*is_declaration=*/false)
7938 /* Now, if we saw a nested-name-specifier, we might be doing the
7939 second production. */
7940 if (nested_name_specifier_p
7941 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7943 /* Consume the `template' keyword. */
7944 cp_lexer_consume_token (parser
->lexer
);
7945 /* Parse the template-id. */
7946 cp_parser_template_id (parser
,
7947 /*template_keyword_p=*/true,
7948 /*check_dependency_p=*/false,
7950 /*is_declaration=*/true);
7951 /* Look for the `::' token. */
7952 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7954 /* If the next token is not a `~', then there might be some
7955 additional qualification. */
7956 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7958 /* At this point, we're looking for "type-name :: ~". The type-name
7959 must not be a class-name, since this is a pseudo-destructor. So,
7960 it must be either an enum-name, or a typedef-name -- both of which
7961 are just identifiers. So, we peek ahead to check that the "::"
7962 and "~" tokens are present; if they are not, then we can avoid
7963 calling type_name. */
7964 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7965 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7966 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7968 cp_parser_error (parser
, "non-scalar type");
7972 /* Look for the type-name. */
7973 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7974 if (*scope
== error_mark_node
)
7977 /* Look for the `::' token. */
7978 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7983 /* Look for the `~'. */
7984 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7986 /* Once we see the ~, this has to be a pseudo-destructor. */
7987 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7988 cp_parser_commit_to_topmost_tentative_parse (parser
);
7990 /* Look for the type-name again. We are not responsible for
7991 checking that it matches the first type-name. */
7992 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7995 /* Parse a unary-expression.
8001 unary-operator cast-expression
8002 sizeof unary-expression
8004 alignof ( type-id ) [C++0x]
8011 __extension__ cast-expression
8012 __alignof__ unary-expression
8013 __alignof__ ( type-id )
8014 alignof unary-expression [C++0x]
8015 __real__ cast-expression
8016 __imag__ cast-expression
8018 sizeof ( type-id ) { initializer-list , [opt] }
8019 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8020 __alignof__ ( type-id ) { initializer-list , [opt] }
8022 ADDRESS_P is true iff the unary-expression is appearing as the
8023 operand of the `&' operator. CAST_P is true if this expression is
8024 the target of a cast.
8026 Returns a representation of the expression. */
8029 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8030 bool address_p
, bool cast_p
, bool decltype_p
)
8033 enum tree_code unary_operator
;
8035 /* Peek at the next token. */
8036 token
= cp_lexer_peek_token (parser
->lexer
);
8037 /* Some keywords give away the kind of expression. */
8038 if (token
->type
== CPP_KEYWORD
)
8040 enum rid keyword
= token
->keyword
;
8049 location_t start_loc
= token
->location
;
8051 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8052 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8054 /* Consume the token. */
8055 cp_lexer_consume_token (parser
->lexer
);
8056 /* Parse the operand. */
8057 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8059 if (TYPE_P (operand
))
8060 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8064 /* ISO C++ defines alignof only with types, not with
8065 expressions. So pedwarn if alignof is used with a non-
8066 type expression. However, __alignof__ is ok. */
8068 pedwarn (token
->location
, OPT_Wpedantic
,
8069 "ISO C++ does not allow %<alignof%> "
8072 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8074 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8075 SIZEOF_EXPR with the original operand. */
8076 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8078 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8080 if (!processing_template_decl
&& TYPE_P (operand
))
8082 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8083 build1 (NOP_EXPR
, operand
,
8085 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8088 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8089 TREE_SIDE_EFFECTS (ret
) = 0;
8090 TREE_READONLY (ret
) = 1;
8094 /* Construct a location e.g. :
8097 with start == caret at the start of the "alignof"/"sizeof"
8098 token, with the endpoint at the final closing paren. */
8099 location_t finish_loc
8100 = cp_lexer_previous_token (parser
->lexer
)->location
;
8101 location_t compound_loc
8102 = make_location (start_loc
, start_loc
, finish_loc
);
8104 cp_expr
ret_expr (ret
);
8105 ret_expr
.set_location (compound_loc
);
8106 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8111 return cp_parser_new_expression (parser
);
8114 return cp_parser_delete_expression (parser
);
8118 /* The saved value of the PEDANTIC flag. */
8122 /* Save away the PEDANTIC flag. */
8123 cp_parser_extension_opt (parser
, &saved_pedantic
);
8124 /* Parse the cast-expression. */
8125 expr
= cp_parser_simple_cast_expression (parser
);
8126 /* Restore the PEDANTIC flag. */
8127 pedantic
= saved_pedantic
;
8137 /* Consume the `__real__' or `__imag__' token. */
8138 cp_lexer_consume_token (parser
->lexer
);
8139 /* Parse the cast-expression. */
8140 expression
= cp_parser_simple_cast_expression (parser
);
8141 /* Create the complete representation. */
8142 return build_x_unary_op (token
->location
,
8143 (keyword
== RID_REALPART
8144 ? REALPART_EXPR
: IMAGPART_EXPR
),
8146 tf_warning_or_error
);
8150 case RID_TRANSACTION_ATOMIC
:
8151 case RID_TRANSACTION_RELAXED
:
8152 return cp_parser_transaction_expression (parser
, keyword
);
8157 const char *saved_message
;
8158 bool saved_integral_constant_expression_p
;
8159 bool saved_non_integral_constant_expression_p
;
8160 bool saved_greater_than_is_operator_p
;
8162 location_t start_loc
= token
->location
;
8164 cp_lexer_consume_token (parser
->lexer
);
8165 matching_parens parens
;
8166 parens
.require_open (parser
);
8168 saved_message
= parser
->type_definition_forbidden_message
;
8169 parser
->type_definition_forbidden_message
8170 = G_("types may not be defined in %<noexcept%> expressions");
8172 saved_integral_constant_expression_p
8173 = parser
->integral_constant_expression_p
;
8174 saved_non_integral_constant_expression_p
8175 = parser
->non_integral_constant_expression_p
;
8176 parser
->integral_constant_expression_p
= false;
8178 saved_greater_than_is_operator_p
8179 = parser
->greater_than_is_operator_p
;
8180 parser
->greater_than_is_operator_p
= true;
8182 ++cp_unevaluated_operand
;
8183 ++c_inhibit_evaluation_warnings
;
8184 ++cp_noexcept_operand
;
8185 expr
= cp_parser_expression (parser
);
8186 --cp_noexcept_operand
;
8187 --c_inhibit_evaluation_warnings
;
8188 --cp_unevaluated_operand
;
8190 parser
->greater_than_is_operator_p
8191 = saved_greater_than_is_operator_p
;
8193 parser
->integral_constant_expression_p
8194 = saved_integral_constant_expression_p
;
8195 parser
->non_integral_constant_expression_p
8196 = saved_non_integral_constant_expression_p
;
8198 parser
->type_definition_forbidden_message
= saved_message
;
8200 location_t finish_loc
8201 = cp_lexer_peek_token (parser
->lexer
)->location
;
8202 parens
.require_close (parser
);
8204 /* Construct a location of the form:
8207 with start == caret, finishing at the close-paren. */
8208 location_t noexcept_loc
8209 = make_location (start_loc
, start_loc
, finish_loc
);
8211 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8220 /* Look for the `:: new' and `:: delete', which also signal the
8221 beginning of a new-expression, or delete-expression,
8222 respectively. If the next token is `::', then it might be one of
8224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8228 /* See if the token after the `::' is one of the keywords in
8229 which we're interested. */
8230 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8231 /* If it's `new', we have a new-expression. */
8232 if (keyword
== RID_NEW
)
8233 return cp_parser_new_expression (parser
);
8234 /* Similarly, for `delete'. */
8235 else if (keyword
== RID_DELETE
)
8236 return cp_parser_delete_expression (parser
);
8239 /* Look for a unary operator. */
8240 unary_operator
= cp_parser_unary_operator (token
);
8241 /* The `++' and `--' operators can be handled similarly, even though
8242 they are not technically unary-operators in the grammar. */
8243 if (unary_operator
== ERROR_MARK
)
8245 if (token
->type
== CPP_PLUS_PLUS
)
8246 unary_operator
= PREINCREMENT_EXPR
;
8247 else if (token
->type
== CPP_MINUS_MINUS
)
8248 unary_operator
= PREDECREMENT_EXPR
;
8249 /* Handle the GNU address-of-label extension. */
8250 else if (cp_parser_allow_gnu_extensions_p (parser
)
8251 && token
->type
== CPP_AND_AND
)
8255 location_t start_loc
= token
->location
;
8257 /* Consume the '&&' token. */
8258 cp_lexer_consume_token (parser
->lexer
);
8259 /* Look for the identifier. */
8260 location_t finish_loc
8261 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8262 identifier
= cp_parser_identifier (parser
);
8263 /* Construct a location of the form:
8266 with caret==start at the "&&", finish at the end of the label. */
8267 location_t combined_loc
8268 = make_location (start_loc
, start_loc
, finish_loc
);
8269 /* Create an expression representing the address. */
8270 expression
= finish_label_address_expr (identifier
, combined_loc
);
8271 if (cp_parser_non_integral_constant_expression (parser
,
8273 expression
= error_mark_node
;
8277 if (unary_operator
!= ERROR_MARK
)
8279 cp_expr cast_expression
;
8280 cp_expr expression
= error_mark_node
;
8281 non_integral_constant non_constant_p
= NIC_NONE
;
8282 location_t loc
= token
->location
;
8283 tsubst_flags_t complain
= complain_flags (decltype_p
);
8285 /* Consume the operator token. */
8286 token
= cp_lexer_consume_token (parser
->lexer
);
8287 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8289 /* Parse the cast-expression. */
8291 = cp_parser_cast_expression (parser
,
8292 unary_operator
== ADDR_EXPR
,
8298 OP_TOKEN CAST_EXPRESSION
8299 ^~~~~~~~~~~~~~~~~~~~~~~~~
8300 with start==caret at the operator token, and
8301 extending to the end of the cast_expression. */
8302 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8304 /* Now, build an appropriate representation. */
8305 switch (unary_operator
)
8308 non_constant_p
= NIC_STAR
;
8309 expression
= build_x_indirect_ref (loc
, cast_expression
,
8312 /* TODO: build_x_indirect_ref does not always honor the
8313 location, so ensure it is set. */
8314 expression
.set_location (loc
);
8318 non_constant_p
= NIC_ADDR
;
8321 expression
= build_x_unary_op (loc
, unary_operator
,
8324 /* TODO: build_x_unary_op does not always honor the location,
8325 so ensure it is set. */
8326 expression
.set_location (loc
);
8329 case PREINCREMENT_EXPR
:
8330 case PREDECREMENT_EXPR
:
8331 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8332 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8335 /* Immediately fold negation of a constant, unless the constant is 0
8336 (since -0 == 0) or it would overflow. */
8337 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8338 && CONSTANT_CLASS_P (cast_expression
)
8339 && !integer_zerop (cast_expression
)
8340 && !TREE_OVERFLOW (cast_expression
))
8342 tree folded
= fold_build1 (unary_operator
,
8343 TREE_TYPE (cast_expression
),
8345 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8347 expression
= cp_expr (folded
, loc
);
8352 case UNARY_PLUS_EXPR
:
8353 case TRUTH_NOT_EXPR
:
8354 expression
= finish_unary_op_expr (loc
, unary_operator
,
8355 cast_expression
, complain
);
8362 if (non_constant_p
!= NIC_NONE
8363 && cp_parser_non_integral_constant_expression (parser
,
8365 expression
= error_mark_node
;
8370 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8371 /*member_access_only_p=*/false,
8376 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8377 unary-operator, the corresponding tree code is returned. */
8379 static enum tree_code
8380 cp_parser_unary_operator (cp_token
* token
)
8382 switch (token
->type
)
8385 return INDIRECT_REF
;
8391 return UNARY_PLUS_EXPR
;
8397 return TRUTH_NOT_EXPR
;
8400 return BIT_NOT_EXPR
;
8407 /* Parse a new-expression.
8410 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8411 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8413 Returns a representation of the expression. */
8416 cp_parser_new_expression (cp_parser
* parser
)
8418 bool global_scope_p
;
8419 vec
<tree
, va_gc
> *placement
;
8421 vec
<tree
, va_gc
> *initializer
;
8422 tree nelts
= NULL_TREE
;
8425 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8427 /* Look for the optional `::' operator. */
8429 = (cp_parser_global_scope_opt (parser
,
8430 /*current_scope_valid_p=*/false)
8432 /* Look for the `new' operator. */
8433 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8434 /* There's no easy way to tell a new-placement from the
8435 `( type-id )' construct. */
8436 cp_parser_parse_tentatively (parser
);
8437 /* Look for a new-placement. */
8438 placement
= cp_parser_new_placement (parser
);
8439 /* If that didn't work out, there's no new-placement. */
8440 if (!cp_parser_parse_definitely (parser
))
8442 if (placement
!= NULL
)
8443 release_tree_vector (placement
);
8447 /* If the next token is a `(', then we have a parenthesized
8449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8452 const char *saved_message
= parser
->type_definition_forbidden_message
;
8454 /* Consume the `('. */
8455 matching_parens parens
;
8456 parens
.consume_open (parser
);
8458 /* Parse the type-id. */
8459 parser
->type_definition_forbidden_message
8460 = G_("types may not be defined in a new-expression");
8462 type_id_in_expr_sentinel
s (parser
);
8463 type
= cp_parser_type_id (parser
);
8465 parser
->type_definition_forbidden_message
= saved_message
;
8467 /* Look for the closing `)'. */
8468 parens
.require_close (parser
);
8469 token
= cp_lexer_peek_token (parser
->lexer
);
8470 /* There should not be a direct-new-declarator in this production,
8471 but GCC used to allowed this, so we check and emit a sensible error
8472 message for this case. */
8473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8475 error_at (token
->location
,
8476 "array bound forbidden after parenthesized type-id");
8477 inform (token
->location
,
8478 "try removing the parentheses around the type-id");
8479 cp_parser_direct_new_declarator (parser
);
8482 /* Otherwise, there must be a new-type-id. */
8484 type
= cp_parser_new_type_id (parser
, &nelts
);
8486 /* If the next token is a `(' or '{', then we have a new-initializer. */
8487 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8488 if (token
->type
== CPP_OPEN_PAREN
8489 || token
->type
== CPP_OPEN_BRACE
)
8490 initializer
= cp_parser_new_initializer (parser
);
8494 /* A new-expression may not appear in an integral constant
8496 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8497 ret
= error_mark_node
;
8498 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8499 of a new-type-id or type-id of a new-expression, the new-expression shall
8500 contain a new-initializer of the form ( assignment-expression )".
8501 Additionally, consistently with the spirit of DR 1467, we want to accept
8502 'new auto { 2 }' too. */
8503 else if ((ret
= type_uses_auto (type
))
8504 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8505 && (vec_safe_length (initializer
) != 1
8506 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8507 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8509 error_at (token
->location
,
8510 "initialization of new-expression for type %<auto%> "
8511 "requires exactly one element");
8512 ret
= error_mark_node
;
8516 /* Construct a location e.g.:
8519 with caret == start at the start of the "new" token, and the end
8520 at the end of the final token we consumed. */
8521 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8522 location_t end_loc
= get_finish (end_tok
->location
);
8523 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8525 /* Create a representation of the new-expression. */
8526 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8527 tf_warning_or_error
);
8528 protected_set_expr_location (ret
, combined_loc
);
8531 if (placement
!= NULL
)
8532 release_tree_vector (placement
);
8533 if (initializer
!= NULL
)
8534 release_tree_vector (initializer
);
8539 /* Parse a new-placement.
8544 Returns the same representation as for an expression-list. */
8546 static vec
<tree
, va_gc
> *
8547 cp_parser_new_placement (cp_parser
* parser
)
8549 vec
<tree
, va_gc
> *expression_list
;
8551 /* Parse the expression-list. */
8552 expression_list
= (cp_parser_parenthesized_expression_list
8553 (parser
, non_attr
, /*cast_p=*/false,
8554 /*allow_expansion_p=*/true,
8555 /*non_constant_p=*/NULL
));
8557 if (expression_list
&& expression_list
->is_empty ())
8558 error ("expected expression-list or type-id");
8560 return expression_list
;
8563 /* Parse a new-type-id.
8566 type-specifier-seq new-declarator [opt]
8568 Returns the TYPE allocated. If the new-type-id indicates an array
8569 type, *NELTS is set to the number of elements in the last array
8570 bound; the TYPE will not include the last array bound. */
8573 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8575 cp_decl_specifier_seq type_specifier_seq
;
8576 cp_declarator
*new_declarator
;
8577 cp_declarator
*declarator
;
8578 cp_declarator
*outer_declarator
;
8579 const char *saved_message
;
8581 /* The type-specifier sequence must not contain type definitions.
8582 (It cannot contain declarations of new types either, but if they
8583 are not definitions we will catch that because they are not
8585 saved_message
= parser
->type_definition_forbidden_message
;
8586 parser
->type_definition_forbidden_message
8587 = G_("types may not be defined in a new-type-id");
8588 /* Parse the type-specifier-seq. */
8589 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8590 /*is_trailing_return=*/false,
8591 &type_specifier_seq
);
8592 /* Restore the old message. */
8593 parser
->type_definition_forbidden_message
= saved_message
;
8595 if (type_specifier_seq
.type
== error_mark_node
)
8596 return error_mark_node
;
8598 /* Parse the new-declarator. */
8599 new_declarator
= cp_parser_new_declarator_opt (parser
);
8601 /* Determine the number of elements in the last array dimension, if
8604 /* Skip down to the last array dimension. */
8605 declarator
= new_declarator
;
8606 outer_declarator
= NULL
;
8607 while (declarator
&& (declarator
->kind
== cdk_pointer
8608 || declarator
->kind
== cdk_ptrmem
))
8610 outer_declarator
= declarator
;
8611 declarator
= declarator
->declarator
;
8614 && declarator
->kind
== cdk_array
8615 && declarator
->declarator
8616 && declarator
->declarator
->kind
== cdk_array
)
8618 outer_declarator
= declarator
;
8619 declarator
= declarator
->declarator
;
8622 if (declarator
&& declarator
->kind
== cdk_array
)
8624 *nelts
= declarator
->u
.array
.bounds
;
8625 if (*nelts
== error_mark_node
)
8626 *nelts
= integer_one_node
;
8628 if (outer_declarator
)
8629 outer_declarator
->declarator
= declarator
->declarator
;
8631 new_declarator
= NULL
;
8634 return groktypename (&type_specifier_seq
, new_declarator
, false);
8637 /* Parse an (optional) new-declarator.
8640 ptr-operator new-declarator [opt]
8641 direct-new-declarator
8643 Returns the declarator. */
8645 static cp_declarator
*
8646 cp_parser_new_declarator_opt (cp_parser
* parser
)
8648 enum tree_code code
;
8649 tree type
, std_attributes
= NULL_TREE
;
8650 cp_cv_quals cv_quals
;
8652 /* We don't know if there's a ptr-operator next, or not. */
8653 cp_parser_parse_tentatively (parser
);
8654 /* Look for a ptr-operator. */
8655 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8656 /* If that worked, look for more new-declarators. */
8657 if (cp_parser_parse_definitely (parser
))
8659 cp_declarator
*declarator
;
8661 /* Parse another optional declarator. */
8662 declarator
= cp_parser_new_declarator_opt (parser
);
8664 declarator
= cp_parser_make_indirect_declarator
8665 (code
, type
, cv_quals
, declarator
, std_attributes
);
8670 /* If the next token is a `[', there is a direct-new-declarator. */
8671 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8672 return cp_parser_direct_new_declarator (parser
);
8677 /* Parse a direct-new-declarator.
8679 direct-new-declarator:
8681 direct-new-declarator [constant-expression]
8685 static cp_declarator
*
8686 cp_parser_direct_new_declarator (cp_parser
* parser
)
8688 cp_declarator
*declarator
= NULL
;
8695 /* Look for the opening `['. */
8696 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8698 token
= cp_lexer_peek_token (parser
->lexer
);
8699 expression
= cp_parser_expression (parser
);
8700 /* The standard requires that the expression have integral
8701 type. DR 74 adds enumeration types. We believe that the
8702 real intent is that these expressions be handled like the
8703 expression in a `switch' condition, which also allows
8704 classes with a single conversion to integral or
8705 enumeration type. */
8706 if (!processing_template_decl
)
8709 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8714 error_at (token
->location
,
8715 "expression in new-declarator must have integral "
8716 "or enumeration type");
8717 expression
= error_mark_node
;
8721 /* Look for the closing `]'. */
8722 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8724 /* Add this bound to the declarator. */
8725 declarator
= make_array_declarator (declarator
, expression
);
8727 /* If the next token is not a `[', then there are no more
8729 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8736 /* Parse a new-initializer.
8739 ( expression-list [opt] )
8742 Returns a representation of the expression-list. */
8744 static vec
<tree
, va_gc
> *
8745 cp_parser_new_initializer (cp_parser
* parser
)
8747 vec
<tree
, va_gc
> *expression_list
;
8749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8752 bool expr_non_constant_p
;
8753 cp_lexer_set_source_position (parser
->lexer
);
8754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8755 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8756 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8757 expression_list
= make_tree_vector_single (t
);
8760 expression_list
= (cp_parser_parenthesized_expression_list
8761 (parser
, non_attr
, /*cast_p=*/false,
8762 /*allow_expansion_p=*/true,
8763 /*non_constant_p=*/NULL
));
8765 return expression_list
;
8768 /* Parse a delete-expression.
8771 :: [opt] delete cast-expression
8772 :: [opt] delete [ ] cast-expression
8774 Returns a representation of the expression. */
8777 cp_parser_delete_expression (cp_parser
* parser
)
8779 bool global_scope_p
;
8783 /* Look for the optional `::' operator. */
8785 = (cp_parser_global_scope_opt (parser
,
8786 /*current_scope_valid_p=*/false)
8788 /* Look for the `delete' keyword. */
8789 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8790 /* See if the array syntax is in use. */
8791 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8793 /* Consume the `[' token. */
8794 cp_lexer_consume_token (parser
->lexer
);
8795 /* Look for the `]' token. */
8796 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8797 /* Remember that this is the `[]' construct. */
8803 /* Parse the cast-expression. */
8804 expression
= cp_parser_simple_cast_expression (parser
);
8806 /* A delete-expression may not appear in an integral constant
8808 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8809 return error_mark_node
;
8811 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8812 tf_warning_or_error
);
8815 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8816 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8820 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8822 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8823 switch (token
->type
)
8829 case CPP_CLOSE_SQUARE
:
8830 case CPP_CLOSE_PAREN
:
8831 case CPP_CLOSE_BRACE
:
8832 case CPP_OPEN_BRACE
:
8836 case CPP_DEREF_STAR
:
8844 case CPP_GREATER_EQ
:
8865 case CPP_OPEN_PAREN
:
8866 /* In ((type ()) () the last () isn't a valid cast-expression,
8867 so the whole must be parsed as postfix-expression. */
8868 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8871 case CPP_OPEN_SQUARE
:
8872 /* '[' may start a primary-expression in obj-c++ and in C++11,
8873 as a lambda-expression, eg, '(void)[]{}'. */
8874 if (cxx_dialect
>= cxx11
)
8876 return c_dialect_objc ();
8879 case CPP_MINUS_MINUS
:
8880 /* '++' and '--' may or may not start a cast-expression:
8882 struct T { void operator++(int); };
8883 void f() { (T())++; }
8896 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8897 in the order: const_cast, static_cast, reinterpret_cast.
8899 Don't suggest dynamic_cast.
8901 Return the first legal cast kind found, or NULL otherwise. */
8904 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8908 /* Reuse the parser logic by attempting to build the various kinds of
8909 cast, with "complain" disabled.
8910 Identify the first such cast that is valid. */
8912 /* Don't attempt to run such logic within template processing. */
8913 if (processing_template_decl
)
8916 /* First try const_cast. */
8917 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8918 if (trial
!= error_mark_node
)
8919 return "const_cast";
8921 /* If that fails, try static_cast. */
8922 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8923 if (trial
!= error_mark_node
)
8924 return "static_cast";
8926 /* Finally, try reinterpret_cast. */
8927 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8928 if (trial
!= error_mark_node
)
8929 return "reinterpret_cast";
8931 /* No such cast possible. */
8935 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8936 suggesting how to convert a C-style cast of the form:
8940 to a C++-style cast.
8942 The primary range of RICHLOC is asssumed to be that of the original
8943 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8944 of the parens in the C-style cast. */
8947 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8948 location_t close_paren_loc
, tree orig_expr
,
8951 /* This function is non-trivial, so bail out now if the warning isn't
8952 going to be emitted. */
8953 if (!warn_old_style_cast
)
8956 /* Try to find a legal C++ cast, trying them in order:
8957 const_cast, static_cast, reinterpret_cast. */
8958 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8959 if (!cast_suggestion
)
8962 /* Replace the open paren with "CAST_SUGGESTION<". */
8964 pp_printf (&pp
, "%s<", cast_suggestion
);
8965 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8967 /* Replace the close paren with "> (". */
8968 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8970 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8971 rich_loc
->add_fixit_insert_after (")");
8975 /* Parse a cast-expression.
8979 ( type-id ) cast-expression
8981 ADDRESS_P is true iff the unary-expression is appearing as the
8982 operand of the `&' operator. CAST_P is true if this expression is
8983 the target of a cast.
8985 Returns a representation of the expression. */
8988 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8989 bool decltype_p
, cp_id_kind
* pidk
)
8991 /* If it's a `(', then we might be looking at a cast. */
8992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8994 tree type
= NULL_TREE
;
8995 cp_expr
expr (NULL_TREE
);
8996 int cast_expression
= 0;
8997 const char *saved_message
;
8999 /* There's no way to know yet whether or not this is a cast.
9000 For example, `(int (3))' is a unary-expression, while `(int)
9001 3' is a cast. So, we resort to parsing tentatively. */
9002 cp_parser_parse_tentatively (parser
);
9003 /* Types may not be defined in a cast. */
9004 saved_message
= parser
->type_definition_forbidden_message
;
9005 parser
->type_definition_forbidden_message
9006 = G_("types may not be defined in casts");
9007 /* Consume the `('. */
9008 matching_parens parens
;
9009 cp_token
*open_paren
= parens
.consume_open (parser
);
9010 location_t open_paren_loc
= open_paren
->location
;
9011 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9013 /* A very tricky bit is that `(struct S) { 3 }' is a
9014 compound-literal (which we permit in C++ as an extension).
9015 But, that construct is not a cast-expression -- it is a
9016 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9017 is legal; if the compound-literal were a cast-expression,
9018 you'd need an extra set of parentheses.) But, if we parse
9019 the type-id, and it happens to be a class-specifier, then we
9020 will commit to the parse at that point, because we cannot
9021 undo the action that is done when creating a new class. So,
9022 then we cannot back up and do a postfix-expression.
9024 Another tricky case is the following (c++/29234):
9026 struct S { void operator () (); };
9033 As a type-id we parse the parenthesized S()() as a function
9034 returning a function, groktypename complains and we cannot
9035 back up in this case either.
9037 Therefore, we scan ahead to the closing `)', and check to see
9038 if the tokens after the `)' can start a cast-expression. Otherwise
9039 we are dealing with an unary-expression, a postfix-expression
9042 Yet another tricky case, in C++11, is the following (c++/54891):
9046 The issue is that usually, besides the case of lambda-expressions,
9047 the parenthesized type-id cannot be followed by '[', and, eg, we
9048 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9049 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9050 we don't commit, we try a cast-expression, then an unary-expression.
9052 Save tokens so that we can put them back. */
9053 cp_lexer_save_tokens (parser
->lexer
);
9055 /* We may be looking at a cast-expression. */
9056 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9057 /*consume_paren=*/true))
9059 = cp_parser_tokens_start_cast_expression (parser
);
9061 /* Roll back the tokens we skipped. */
9062 cp_lexer_rollback_tokens (parser
->lexer
);
9063 /* If we aren't looking at a cast-expression, simulate an error so
9064 that the call to cp_parser_error_occurred below returns true. */
9065 if (!cast_expression
)
9066 cp_parser_simulate_error (parser
);
9069 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9070 parser
->in_type_id_in_expr_p
= true;
9071 /* Look for the type-id. */
9072 type
= cp_parser_type_id (parser
);
9073 /* Look for the closing `)'. */
9074 cp_token
*close_paren
= parens
.require_close (parser
);
9076 close_paren_loc
= close_paren
->location
;
9077 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9080 /* Restore the saved message. */
9081 parser
->type_definition_forbidden_message
= saved_message
;
9083 /* At this point this can only be either a cast or a
9084 parenthesized ctor such as `(T ())' that looks like a cast to
9085 function returning T. */
9086 if (!cp_parser_error_occurred (parser
))
9088 /* Only commit if the cast-expression doesn't start with
9089 '++', '--', or '[' in C++11. */
9090 if (cast_expression
> 0)
9091 cp_parser_commit_to_topmost_tentative_parse (parser
);
9093 expr
= cp_parser_cast_expression (parser
,
9094 /*address_p=*/false,
9096 /*decltype_p=*/false,
9099 if (cp_parser_parse_definitely (parser
))
9101 /* Warn about old-style casts, if so requested. */
9102 if (warn_old_style_cast
9103 && !in_system_header_at (input_location
)
9104 && !VOID_TYPE_P (type
)
9105 && current_lang_name
!= lang_name_c
)
9107 gcc_rich_location
rich_loc (input_location
);
9108 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9110 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9111 "use of old-style cast to %q#T", type
);
9114 /* Only type conversions to integral or enumeration types
9115 can be used in constant-expressions. */
9116 if (!cast_valid_in_integral_constant_expression_p (type
)
9117 && cp_parser_non_integral_constant_expression (parser
,
9119 return error_mark_node
;
9121 /* Perform the cast. */
9125 with start==caret at the open paren, extending to the
9127 location_t cast_loc
= make_location (open_paren_loc
,
9129 expr
.get_finish ());
9130 expr
= build_c_cast (cast_loc
, type
, expr
);
9135 cp_parser_abort_tentative_parse (parser
);
9138 /* If we get here, then it's not a cast, so it must be a
9139 unary-expression. */
9140 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9141 cast_p
, decltype_p
);
9144 /* Parse a binary expression of the general form:
9148 pm-expression .* cast-expression
9149 pm-expression ->* cast-expression
9151 multiplicative-expression:
9153 multiplicative-expression * pm-expression
9154 multiplicative-expression / pm-expression
9155 multiplicative-expression % pm-expression
9157 additive-expression:
9158 multiplicative-expression
9159 additive-expression + multiplicative-expression
9160 additive-expression - multiplicative-expression
9164 shift-expression << additive-expression
9165 shift-expression >> additive-expression
9167 relational-expression:
9169 relational-expression < shift-expression
9170 relational-expression > shift-expression
9171 relational-expression <= shift-expression
9172 relational-expression >= shift-expression
9176 relational-expression:
9177 relational-expression <? shift-expression
9178 relational-expression >? shift-expression
9180 equality-expression:
9181 relational-expression
9182 equality-expression == relational-expression
9183 equality-expression != relational-expression
9187 and-expression & equality-expression
9189 exclusive-or-expression:
9191 exclusive-or-expression ^ and-expression
9193 inclusive-or-expression:
9194 exclusive-or-expression
9195 inclusive-or-expression | exclusive-or-expression
9197 logical-and-expression:
9198 inclusive-or-expression
9199 logical-and-expression && inclusive-or-expression
9201 logical-or-expression:
9202 logical-and-expression
9203 logical-or-expression || logical-and-expression
9205 All these are implemented with a single function like:
9208 simple-cast-expression
9209 binary-expression <token> binary-expression
9211 CAST_P is true if this expression is the target of a cast.
9213 The binops_by_token map is used to get the tree codes for each <token> type.
9214 binary-expressions are associated according to a precedence table. */
9216 #define TOKEN_PRECEDENCE(token) \
9217 (((token->type == CPP_GREATER \
9218 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9219 && !parser->greater_than_is_operator_p) \
9220 ? PREC_NOT_OPERATOR \
9221 : binops_by_token[token->type].prec)
9224 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9225 bool no_toplevel_fold_p
,
9227 enum cp_parser_prec prec
,
9230 cp_parser_expression_stack stack
;
9231 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9232 cp_parser_expression_stack_entry current
;
9235 enum tree_code rhs_type
;
9236 enum cp_parser_prec new_prec
, lookahead_prec
;
9239 /* Parse the first expression. */
9240 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9241 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9242 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9243 cast_p
, decltype_p
, pidk
);
9244 current
.prec
= prec
;
9246 if (cp_parser_error_occurred (parser
))
9247 return error_mark_node
;
9251 /* Get an operator token. */
9252 token
= cp_lexer_peek_token (parser
->lexer
);
9254 if (warn_cxx11_compat
9255 && token
->type
== CPP_RSHIFT
9256 && !parser
->greater_than_is_operator_p
)
9258 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9259 "%<>>%> operator is treated"
9260 " as two right angle brackets in C++11"))
9261 inform (token
->location
,
9262 "suggest parentheses around %<>>%> expression");
9265 new_prec
= TOKEN_PRECEDENCE (token
);
9266 if (new_prec
!= PREC_NOT_OPERATOR
9267 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9268 /* This is a fold-expression; handle it later. */
9269 new_prec
= PREC_NOT_OPERATOR
;
9271 /* Popping an entry off the stack means we completed a subexpression:
9272 - either we found a token which is not an operator (`>' where it is not
9273 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9274 will happen repeatedly;
9275 - or, we found an operator which has lower priority. This is the case
9276 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9278 if (new_prec
<= current
.prec
)
9287 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9288 current
.loc
= token
->location
;
9290 /* We used the operator token. */
9291 cp_lexer_consume_token (parser
->lexer
);
9293 /* For "false && x" or "true || x", x will never be executed;
9294 disable warnings while evaluating it. */
9295 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9296 c_inhibit_evaluation_warnings
+=
9297 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9298 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9299 c_inhibit_evaluation_warnings
+=
9300 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9302 /* Extract another operand. It may be the RHS of this expression
9303 or the LHS of a new, higher priority expression. */
9304 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9305 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9306 rhs
= cp_parser_simple_cast_expression (parser
);
9308 /* Get another operator token. Look up its precedence to avoid
9309 building a useless (immediately popped) stack entry for common
9310 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9311 token
= cp_lexer_peek_token (parser
->lexer
);
9312 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9313 if (lookahead_prec
!= PREC_NOT_OPERATOR
9314 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9315 lookahead_prec
= PREC_NOT_OPERATOR
;
9316 if (lookahead_prec
> new_prec
)
9318 /* ... and prepare to parse the RHS of the new, higher priority
9319 expression. Since precedence levels on the stack are
9320 monotonically increasing, we do not have to care about
9325 current
.lhs_type
= rhs_type
;
9326 current
.prec
= new_prec
;
9327 new_prec
= lookahead_prec
;
9331 lookahead_prec
= new_prec
;
9332 /* If the stack is not empty, we have parsed into LHS the right side
9333 (`4' in the example above) of an expression we had suspended.
9334 We can use the information on the stack to recover the LHS (`3')
9335 from the stack together with the tree code (`MULT_EXPR'), and
9336 the precedence of the higher level subexpression
9337 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9338 which will be used to actually build the additive expression. */
9340 rhs_type
= current
.lhs_type
;
9345 /* Undo the disabling of warnings done above. */
9346 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9347 c_inhibit_evaluation_warnings
-=
9348 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9349 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9350 c_inhibit_evaluation_warnings
-=
9351 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9353 if (warn_logical_not_paren
9354 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9355 && current
.lhs_type
== TRUTH_NOT_EXPR
9356 /* Avoid warning for !!x == y. */
9357 && (TREE_CODE (current
.lhs
) != NE_EXPR
9358 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9359 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9360 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9361 /* Avoid warning for !b == y where b is boolean. */
9362 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9363 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9365 /* Avoid warning for !!b == y where b is boolean. */
9366 && (!DECL_P (current
.lhs
)
9367 || TREE_TYPE (current
.lhs
) == NULL_TREE
9368 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9369 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9370 current
.lhs
, maybe_constant_value (rhs
));
9374 location_t combined_loc
= make_location (current
.loc
,
9375 current
.lhs
.get_start (),
9378 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9379 ERROR_MARK for everything that is not a binary expression.
9380 This makes warn_about_parentheses miss some warnings that
9381 involve unary operators. For unary expressions we should
9382 pass the correct tree_code unless the unary expression was
9383 surrounded by parentheses.
9385 if (no_toplevel_fold_p
9386 && lookahead_prec
<= current
.prec
9389 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9390 current
.lhs
= error_mark_node
;
9394 = build_min (current
.tree_type
,
9395 TREE_CODE_CLASS (current
.tree_type
)
9397 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9398 current
.lhs
.get_value (), rhs
.get_value ());
9399 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9404 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9405 current
.lhs
, current
.lhs_type
,
9406 rhs
, rhs_type
, &overload
,
9407 complain_flags (decltype_p
));
9408 /* TODO: build_x_binary_op doesn't always honor the location. */
9409 current
.lhs
.set_location (combined_loc
);
9411 current
.lhs_type
= current
.tree_type
;
9413 /* If the binary operator required the use of an overloaded operator,
9414 then this expression cannot be an integral constant-expression.
9415 An overloaded operator can be used even if both operands are
9416 otherwise permissible in an integral constant-expression if at
9417 least one of the operands is of enumeration type. */
9420 && cp_parser_non_integral_constant_expression (parser
,
9422 return error_mark_node
;
9429 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9430 bool no_toplevel_fold_p
,
9431 enum cp_parser_prec prec
,
9434 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9435 /*decltype*/false, prec
, pidk
);
9438 /* Parse the `? expression : assignment-expression' part of a
9439 conditional-expression. The LOGICAL_OR_EXPR is the
9440 logical-or-expression that started the conditional-expression.
9441 Returns a representation of the entire conditional-expression.
9443 This routine is used by cp_parser_assignment_expression.
9445 ? expression : assignment-expression
9449 ? : assignment-expression */
9452 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9454 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9455 cp_expr assignment_expr
;
9456 struct cp_token
*token
;
9457 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9459 /* Consume the `?' token. */
9460 cp_lexer_consume_token (parser
->lexer
);
9461 token
= cp_lexer_peek_token (parser
->lexer
);
9462 if (cp_parser_allow_gnu_extensions_p (parser
)
9463 && token
->type
== CPP_COLON
)
9465 pedwarn (token
->location
, OPT_Wpedantic
,
9466 "ISO C++ does not allow ?: with omitted middle operand");
9467 /* Implicit true clause. */
9469 c_inhibit_evaluation_warnings
+=
9470 folded_logical_or_expr
== truthvalue_true_node
;
9471 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9475 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9476 parser
->colon_corrects_to_scope_p
= false;
9477 /* Parse the expression. */
9478 c_inhibit_evaluation_warnings
+=
9479 folded_logical_or_expr
== truthvalue_false_node
;
9480 expr
= cp_parser_expression (parser
);
9481 c_inhibit_evaluation_warnings
+=
9482 ((folded_logical_or_expr
== truthvalue_true_node
)
9483 - (folded_logical_or_expr
== truthvalue_false_node
));
9484 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9487 /* The next token should be a `:'. */
9488 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9489 /* Parse the assignment-expression. */
9490 assignment_expr
= cp_parser_assignment_expression (parser
);
9491 c_inhibit_evaluation_warnings
-=
9492 folded_logical_or_expr
== truthvalue_true_node
;
9495 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9496 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9497 with the caret at the "?", ranging from the start of
9498 the logical_or_expr to the end of the assignment_expr. */
9499 loc
= make_location (loc
,
9500 logical_or_expr
.get_start (),
9501 assignment_expr
.get_finish ());
9503 /* Build the conditional-expression. */
9504 return build_x_conditional_expr (loc
, logical_or_expr
,
9507 tf_warning_or_error
);
9510 /* Parse an assignment-expression.
9512 assignment-expression:
9513 conditional-expression
9514 logical-or-expression assignment-operator assignment_expression
9517 CAST_P is true if this expression is the target of a cast.
9518 DECLTYPE_P is true if this expression is the operand of decltype.
9520 Returns a representation for the expression. */
9523 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9524 bool cast_p
, bool decltype_p
)
9528 /* If the next token is the `throw' keyword, then we're looking at
9529 a throw-expression. */
9530 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9531 expr
= cp_parser_throw_expression (parser
);
9532 /* Otherwise, it must be that we are looking at a
9533 logical-or-expression. */
9536 /* Parse the binary expressions (logical-or-expression). */
9537 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9539 PREC_NOT_OPERATOR
, pidk
);
9540 /* If the next token is a `?' then we're actually looking at a
9541 conditional-expression. */
9542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9543 return cp_parser_question_colon_clause (parser
, expr
);
9546 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9548 /* If it's an assignment-operator, we're using the second
9550 enum tree_code assignment_operator
9551 = cp_parser_assignment_operator_opt (parser
);
9552 if (assignment_operator
!= ERROR_MARK
)
9554 bool non_constant_p
;
9556 /* Parse the right-hand side of the assignment. */
9557 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9560 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9561 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9563 /* An assignment may not appear in a
9564 constant-expression. */
9565 if (cp_parser_non_integral_constant_expression (parser
,
9567 return error_mark_node
;
9568 /* Build the assignment expression. Its default
9572 is the location of the '=' token as the
9573 caret, ranging from the start of the lhs to the
9575 loc
= make_location (loc
,
9578 expr
= build_x_modify_expr (loc
, expr
,
9579 assignment_operator
,
9581 complain_flags (decltype_p
));
9582 /* TODO: build_x_modify_expr doesn't honor the location,
9583 so we must set it here. */
9584 expr
.set_location (loc
);
9592 /* Parse an (optional) assignment-operator.
9594 assignment-operator: one of
9595 = *= /= %= += -= >>= <<= &= ^= |=
9599 assignment-operator: one of
9602 If the next token is an assignment operator, the corresponding tree
9603 code is returned, and the token is consumed. For example, for
9604 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9605 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9606 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9607 operator, ERROR_MARK is returned. */
9609 static enum tree_code
9610 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9615 /* Peek at the next token. */
9616 token
= cp_lexer_peek_token (parser
->lexer
);
9618 switch (token
->type
)
9629 op
= TRUNC_DIV_EXPR
;
9633 op
= TRUNC_MOD_EXPR
;
9665 /* Nothing else is an assignment operator. */
9669 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9670 if (op
!= ERROR_MARK
9671 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9674 /* If it was an assignment operator, consume it. */
9675 if (op
!= ERROR_MARK
)
9676 cp_lexer_consume_token (parser
->lexer
);
9681 /* Parse an expression.
9684 assignment-expression
9685 expression , assignment-expression
9687 CAST_P is true if this expression is the target of a cast.
9688 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9689 except possibly parenthesized or on the RHS of a comma (N3276).
9691 Returns a representation of the expression. */
9694 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9695 bool cast_p
, bool decltype_p
)
9697 cp_expr expression
= NULL_TREE
;
9698 location_t loc
= UNKNOWN_LOCATION
;
9702 cp_expr assignment_expression
;
9704 /* Parse the next assignment-expression. */
9705 assignment_expression
9706 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9708 /* We don't create a temporary for a call that is the immediate operand
9709 of decltype or on the RHS of a comma. But when we see a comma, we
9710 need to create a temporary for a call on the LHS. */
9711 if (decltype_p
&& !processing_template_decl
9712 && TREE_CODE (assignment_expression
) == CALL_EXPR
9713 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9714 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9715 assignment_expression
9716 = build_cplus_new (TREE_TYPE (assignment_expression
),
9717 assignment_expression
, tf_warning_or_error
);
9719 /* If this is the first assignment-expression, we can just
9722 expression
= assignment_expression
;
9725 /* Create a location with caret at the comma, ranging
9726 from the start of the LHS to the end of the RHS. */
9727 loc
= make_location (loc
,
9728 expression
.get_start (),
9729 assignment_expression
.get_finish ());
9730 expression
= build_x_compound_expr (loc
, expression
,
9731 assignment_expression
,
9732 complain_flags (decltype_p
));
9733 expression
.set_location (loc
);
9735 /* If the next token is not a comma, or we're in a fold-expression, then
9736 we are done with the expression. */
9737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9738 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9740 /* Consume the `,'. */
9741 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9742 cp_lexer_consume_token (parser
->lexer
);
9743 /* A comma operator cannot appear in a constant-expression. */
9744 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9745 expression
= error_mark_node
;
9751 /* Parse a constant-expression.
9753 constant-expression:
9754 conditional-expression
9756 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9757 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9758 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9759 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9760 only parse a conditional-expression, otherwise parse an
9761 assignment-expression. See below for rationale. */
9764 cp_parser_constant_expression (cp_parser
* parser
,
9765 bool allow_non_constant_p
,
9766 bool *non_constant_p
,
9769 bool saved_integral_constant_expression_p
;
9770 bool saved_allow_non_integral_constant_expression_p
;
9771 bool saved_non_integral_constant_expression_p
;
9774 /* It might seem that we could simply parse the
9775 conditional-expression, and then check to see if it were
9776 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9777 one that the compiler can figure out is constant, possibly after
9778 doing some simplifications or optimizations. The standard has a
9779 precise definition of constant-expression, and we must honor
9780 that, even though it is somewhat more restrictive.
9786 is not a legal declaration, because `(2, 3)' is not a
9787 constant-expression. The `,' operator is forbidden in a
9788 constant-expression. However, GCC's constant-folding machinery
9789 will fold this operation to an INTEGER_CST for `3'. */
9791 /* Save the old settings. */
9792 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9793 saved_allow_non_integral_constant_expression_p
9794 = parser
->allow_non_integral_constant_expression_p
;
9795 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9796 /* We are now parsing a constant-expression. */
9797 parser
->integral_constant_expression_p
= true;
9798 parser
->allow_non_integral_constant_expression_p
9799 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9800 parser
->non_integral_constant_expression_p
= false;
9801 /* Although the grammar says "conditional-expression", when not STRICT_P,
9802 we parse an "assignment-expression", which also permits
9803 "throw-expression" and the use of assignment operators. In the case
9804 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9805 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9806 actually essential that we look for an assignment-expression.
9807 For example, cp_parser_initializer_clauses uses this function to
9808 determine whether a particular assignment-expression is in fact
9812 /* Parse the binary expressions (logical-or-expression). */
9813 expression
= cp_parser_binary_expression (parser
, false, false, false,
9814 PREC_NOT_OPERATOR
, NULL
);
9815 /* If the next token is a `?' then we're actually looking at
9816 a conditional-expression; otherwise we're done. */
9817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9818 expression
= cp_parser_question_colon_clause (parser
, expression
);
9821 expression
= cp_parser_assignment_expression (parser
);
9822 /* Restore the old settings. */
9823 parser
->integral_constant_expression_p
9824 = saved_integral_constant_expression_p
;
9825 parser
->allow_non_integral_constant_expression_p
9826 = saved_allow_non_integral_constant_expression_p
;
9827 if (cxx_dialect
>= cxx11
)
9829 /* Require an rvalue constant expression here; that's what our
9830 callers expect. Reference constant expressions are handled
9831 separately in e.g. cp_parser_template_argument. */
9832 tree decay
= expression
;
9833 if (TREE_TYPE (expression
)
9834 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9835 decay
= build_address (expression
);
9836 bool is_const
= potential_rvalue_constant_expression (decay
);
9837 parser
->non_integral_constant_expression_p
= !is_const
;
9838 if (!is_const
&& !allow_non_constant_p
)
9839 require_potential_rvalue_constant_expression (decay
);
9841 if (allow_non_constant_p
)
9842 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9843 parser
->non_integral_constant_expression_p
9844 = saved_non_integral_constant_expression_p
;
9849 /* Parse __builtin_offsetof.
9851 offsetof-expression:
9852 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9854 offsetof-member-designator:
9856 | offsetof-member-designator "." id-expression
9857 | offsetof-member-designator "[" expression "]"
9858 | offsetof-member-designator "->" id-expression */
9861 cp_parser_builtin_offsetof (cp_parser
*parser
)
9863 int save_ice_p
, save_non_ice_p
;
9868 location_t finish_loc
;
9870 /* We're about to accept non-integral-constant things, but will
9871 definitely yield an integral constant expression. Save and
9872 restore these values around our local parsing. */
9873 save_ice_p
= parser
->integral_constant_expression_p
;
9874 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9876 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9878 /* Consume the "__builtin_offsetof" token. */
9879 cp_lexer_consume_token (parser
->lexer
);
9880 /* Consume the opening `('. */
9881 matching_parens parens
;
9882 parens
.require_open (parser
);
9883 /* Parse the type-id. */
9884 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9886 const char *saved_message
= parser
->type_definition_forbidden_message
;
9887 parser
->type_definition_forbidden_message
9888 = G_("types may not be defined within __builtin_offsetof");
9889 type
= cp_parser_type_id (parser
);
9890 parser
->type_definition_forbidden_message
= saved_message
;
9892 /* Look for the `,'. */
9893 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9894 token
= cp_lexer_peek_token (parser
->lexer
);
9896 /* Build the (type *)null that begins the traditional offsetof macro. */
9898 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9899 tf_warning_or_error
);
9901 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9902 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9903 true, &dummy
, token
->location
);
9906 token
= cp_lexer_peek_token (parser
->lexer
);
9907 switch (token
->type
)
9909 case CPP_OPEN_SQUARE
:
9910 /* offsetof-member-designator "[" expression "]" */
9911 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9916 /* offsetof-member-designator "->" identifier */
9917 expr
= grok_array_decl (token
->location
, expr
,
9918 integer_zero_node
, false);
9922 /* offsetof-member-designator "." identifier */
9923 cp_lexer_consume_token (parser
->lexer
);
9924 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9929 case CPP_CLOSE_PAREN
:
9930 /* Consume the ")" token. */
9931 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9932 cp_lexer_consume_token (parser
->lexer
);
9936 /* Error. We know the following require will fail, but
9937 that gives the proper error message. */
9938 parens
.require_close (parser
);
9939 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9940 expr
= error_mark_node
;
9946 /* Make a location of the form:
9947 __builtin_offsetof (struct s, f)
9948 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9949 with caret at the type-id, ranging from the start of the
9950 "_builtin_offsetof" token to the close paren. */
9951 loc
= make_location (loc
, start_loc
, finish_loc
);
9952 /* The result will be an INTEGER_CST, so we need to explicitly
9953 preserve the location. */
9954 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9957 parser
->integral_constant_expression_p
= save_ice_p
;
9958 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9960 expr
= expr
.maybe_add_location_wrapper ();
9964 /* Parse a trait expression.
9966 Returns a representation of the expression, the underlying type
9967 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9970 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9973 tree type1
, type2
= NULL_TREE
;
9974 bool binary
= false;
9975 bool variadic
= false;
9979 case RID_HAS_NOTHROW_ASSIGN
:
9980 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9982 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9983 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9985 case RID_HAS_NOTHROW_COPY
:
9986 kind
= CPTK_HAS_NOTHROW_COPY
;
9988 case RID_HAS_TRIVIAL_ASSIGN
:
9989 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9991 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9992 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9994 case RID_HAS_TRIVIAL_COPY
:
9995 kind
= CPTK_HAS_TRIVIAL_COPY
;
9997 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9998 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10000 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10001 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10003 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10004 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10006 case RID_IS_ABSTRACT
:
10007 kind
= CPTK_IS_ABSTRACT
;
10009 case RID_IS_AGGREGATE
:
10010 kind
= CPTK_IS_AGGREGATE
;
10012 case RID_IS_BASE_OF
:
10013 kind
= CPTK_IS_BASE_OF
;
10017 kind
= CPTK_IS_CLASS
;
10020 kind
= CPTK_IS_EMPTY
;
10023 kind
= CPTK_IS_ENUM
;
10026 kind
= CPTK_IS_FINAL
;
10028 case RID_IS_LITERAL_TYPE
:
10029 kind
= CPTK_IS_LITERAL_TYPE
;
10032 kind
= CPTK_IS_POD
;
10034 case RID_IS_POLYMORPHIC
:
10035 kind
= CPTK_IS_POLYMORPHIC
;
10037 case RID_IS_SAME_AS
:
10038 kind
= CPTK_IS_SAME_AS
;
10041 case RID_IS_STD_LAYOUT
:
10042 kind
= CPTK_IS_STD_LAYOUT
;
10044 case RID_IS_TRIVIAL
:
10045 kind
= CPTK_IS_TRIVIAL
;
10047 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10048 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10051 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10052 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10055 case RID_IS_TRIVIALLY_COPYABLE
:
10056 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10059 kind
= CPTK_IS_UNION
;
10061 case RID_UNDERLYING_TYPE
:
10062 kind
= CPTK_UNDERLYING_TYPE
;
10067 case RID_DIRECT_BASES
:
10068 kind
= CPTK_DIRECT_BASES
;
10070 case RID_IS_ASSIGNABLE
:
10071 kind
= CPTK_IS_ASSIGNABLE
;
10074 case RID_IS_CONSTRUCTIBLE
:
10075 kind
= CPTK_IS_CONSTRUCTIBLE
;
10079 gcc_unreachable ();
10082 /* Get location of initial token. */
10083 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10085 /* Consume the token. */
10086 cp_lexer_consume_token (parser
->lexer
);
10088 matching_parens parens
;
10089 parens
.require_open (parser
);
10092 type_id_in_expr_sentinel
s (parser
);
10093 type1
= cp_parser_type_id (parser
);
10096 if (type1
== error_mark_node
)
10097 return error_mark_node
;
10101 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10104 type_id_in_expr_sentinel
s (parser
);
10105 type2
= cp_parser_type_id (parser
);
10108 if (type2
== error_mark_node
)
10109 return error_mark_node
;
10113 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10115 cp_lexer_consume_token (parser
->lexer
);
10116 tree elt
= cp_parser_type_id (parser
);
10117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10119 cp_lexer_consume_token (parser
->lexer
);
10120 elt
= make_pack_expansion (elt
);
10122 if (elt
== error_mark_node
)
10123 return error_mark_node
;
10124 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10128 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10129 parens
.require_close (parser
);
10131 /* Construct a location of the form:
10132 __is_trivially_copyable(_Tp)
10133 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10134 with start == caret, finishing at the close-paren. */
10135 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10137 /* Complete the trait expression, which may mean either processing
10138 the trait expr now or saving it for template instantiation. */
10141 case CPTK_UNDERLYING_TYPE
:
10142 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10144 return cp_expr (finish_bases (type1
, false), trait_loc
);
10145 case CPTK_DIRECT_BASES
:
10146 return cp_expr (finish_bases (type1
, true), trait_loc
);
10148 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10152 /* Parse a lambda expression.
10155 lambda-introducer lambda-declarator [opt] compound-statement
10157 Returns a representation of the expression. */
10160 cp_parser_lambda_expression (cp_parser
* parser
)
10162 tree lambda_expr
= build_lambda_expr ();
10165 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10166 cp_token_position start
= 0;
10168 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10170 if (cp_unevaluated_operand
)
10172 if (!token
->error_reported
)
10174 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10175 "lambda-expression in unevaluated context");
10176 token
->error_reported
= true;
10180 else if (parser
->in_template_argument_list_p
)
10182 if (!token
->error_reported
)
10184 error_at (token
->location
, "lambda-expression in template-argument");
10185 token
->error_reported
= true;
10190 /* We may be in the middle of deferred access check. Disable
10192 push_deferring_access_checks (dk_no_deferred
);
10194 cp_parser_lambda_introducer (parser
, lambda_expr
);
10196 type
= begin_lambda_type (lambda_expr
);
10197 if (type
== error_mark_node
)
10198 return error_mark_node
;
10200 record_lambda_scope (lambda_expr
);
10202 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10203 determine_visibility (TYPE_NAME (type
));
10205 /* Now that we've started the type, add the capture fields for any
10206 explicit captures. */
10207 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10210 /* Inside the class, surrounding template-parameter-lists do not apply. */
10211 unsigned int saved_num_template_parameter_lists
10212 = parser
->num_template_parameter_lists
;
10213 unsigned char in_statement
= parser
->in_statement
;
10214 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10215 bool fully_implicit_function_template_p
10216 = parser
->fully_implicit_function_template_p
;
10217 tree implicit_template_parms
= parser
->implicit_template_parms
;
10218 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10219 bool auto_is_implicit_function_template_parm_p
10220 = parser
->auto_is_implicit_function_template_parm_p
;
10222 parser
->num_template_parameter_lists
= 0;
10223 parser
->in_statement
= 0;
10224 parser
->in_switch_statement_p
= false;
10225 parser
->fully_implicit_function_template_p
= false;
10226 parser
->implicit_template_parms
= 0;
10227 parser
->implicit_template_scope
= 0;
10228 parser
->auto_is_implicit_function_template_parm_p
= false;
10230 /* By virtue of defining a local class, a lambda expression has access to
10231 the private variables of enclosing classes. */
10233 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10235 if (ok
&& cp_parser_error_occurred (parser
))
10240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10241 && cp_parser_start_tentative_firewall (parser
))
10243 cp_parser_lambda_body (parser
, lambda_expr
);
10245 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10247 if (cp_parser_skip_to_closing_brace (parser
))
10248 cp_lexer_consume_token (parser
->lexer
);
10251 /* The capture list was built up in reverse order; fix that now. */
10252 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10253 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10256 maybe_add_lambda_conv_op (type
);
10258 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10260 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10261 parser
->in_statement
= in_statement
;
10262 parser
->in_switch_statement_p
= in_switch_statement_p
;
10263 parser
->fully_implicit_function_template_p
10264 = fully_implicit_function_template_p
;
10265 parser
->implicit_template_parms
= implicit_template_parms
;
10266 parser
->implicit_template_scope
= implicit_template_scope
;
10267 parser
->auto_is_implicit_function_template_parm_p
10268 = auto_is_implicit_function_template_parm_p
;
10271 /* This field is only used during parsing of the lambda. */
10272 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10274 /* This lambda shouldn't have any proxies left at this point. */
10275 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10276 /* And now that we're done, push proxies for an enclosing lambda. */
10277 insert_pending_capture_proxies ();
10279 /* Update the lambda expression to a range. */
10280 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10281 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10283 end_tok
->location
);
10286 lambda_expr
= build_lambda_object (lambda_expr
);
10288 lambda_expr
= error_mark_node
;
10290 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10292 pop_deferring_access_checks ();
10294 return lambda_expr
;
10297 /* Parse the beginning of a lambda expression.
10300 [ lambda-capture [opt] ]
10302 LAMBDA_EXPR is the current representation of the lambda expression. */
10305 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10307 /* Need commas after the first capture. */
10310 /* Eat the leading `['. */
10311 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10313 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10315 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10316 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10317 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10318 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10320 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10322 cp_lexer_consume_token (parser
->lexer
);
10325 if (!(at_function_scope_p () || parsing_nsdmi ()))
10326 error ("non-local lambda expression cannot have a capture-default");
10329 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10331 cp_token
* capture_token
;
10333 tree capture_init_expr
;
10334 cp_id_kind idk
= CP_ID_KIND_NONE
;
10335 bool explicit_init_p
= false;
10337 enum capture_kind_type
10342 enum capture_kind_type capture_kind
= BY_COPY
;
10344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10346 error ("expected end of capture-list");
10353 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10355 /* Possibly capture `this'. */
10356 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10358 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10359 if (cxx_dialect
< cxx2a
10360 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10361 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10362 "with by-copy capture default");
10363 cp_lexer_consume_token (parser
->lexer
);
10364 add_capture (lambda_expr
,
10365 /*id=*/this_identifier
,
10366 /*initializer=*/finish_this_expr (),
10367 /*by_reference_p=*/true,
10372 /* Possibly capture `*this'. */
10373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10374 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10376 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10377 if (cxx_dialect
< cxx17
)
10378 pedwarn (loc
, 0, "%<*this%> capture only available with "
10379 "-std=c++17 or -std=gnu++17");
10380 cp_lexer_consume_token (parser
->lexer
);
10381 cp_lexer_consume_token (parser
->lexer
);
10382 add_capture (lambda_expr
,
10383 /*id=*/this_identifier
,
10384 /*initializer=*/finish_this_expr (),
10385 /*by_reference_p=*/false,
10390 /* Remember whether we want to capture as a reference or not. */
10391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10393 capture_kind
= BY_REFERENCE
;
10394 cp_lexer_consume_token (parser
->lexer
);
10397 /* Get the identifier. */
10398 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10399 capture_id
= cp_parser_identifier (parser
);
10401 if (capture_id
== error_mark_node
)
10402 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10403 delimiters, but I modified this to stop on unnested ']' as well. It
10404 was already changed to stop on unnested '}', so the
10405 "closing_parenthesis" name is no more misleading with my change. */
10407 cp_parser_skip_to_closing_parenthesis (parser
,
10408 /*recovering=*/true,
10410 /*consume_paren=*/true);
10414 /* Find the initializer for this capture. */
10415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10416 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10417 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10419 bool direct
, non_constant
;
10420 /* An explicit initializer exists. */
10421 if (cxx_dialect
< cxx14
)
10422 pedwarn (input_location
, 0,
10423 "lambda capture initializers "
10424 "only available with -std=c++14 or -std=gnu++14");
10425 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10426 &non_constant
, true);
10427 explicit_init_p
= true;
10428 if (capture_init_expr
== NULL_TREE
)
10430 error ("empty initializer for lambda init-capture");
10431 capture_init_expr
= error_mark_node
;
10436 const char* error_msg
;
10438 /* Turn the identifier into an id-expression. */
10440 = cp_parser_lookup_name_simple (parser
, capture_id
,
10441 capture_token
->location
);
10443 if (capture_init_expr
== error_mark_node
)
10445 unqualified_name_lookup_error (capture_id
);
10448 else if (!VAR_P (capture_init_expr
)
10449 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10451 error_at (capture_token
->location
,
10452 "capture of non-variable %qE",
10453 capture_init_expr
);
10454 if (DECL_P (capture_init_expr
))
10455 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10456 "%q#D declared here", capture_init_expr
);
10459 if (VAR_P (capture_init_expr
)
10460 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10462 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10463 "%qD with non-automatic storage duration",
10464 capture_init_expr
))
10465 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10466 "%q#D declared here", capture_init_expr
);
10471 = finish_id_expression
10476 /*integral_constant_expression_p=*/false,
10477 /*allow_non_integral_constant_expression_p=*/false,
10478 /*non_integral_constant_expression_p=*/NULL
,
10479 /*template_p=*/false,
10481 /*address_p=*/false,
10482 /*template_arg_p=*/false,
10484 capture_token
->location
);
10486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10488 cp_lexer_consume_token (parser
->lexer
);
10489 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10493 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10494 && !explicit_init_p
)
10496 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10497 && capture_kind
== BY_COPY
)
10498 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10499 "of %qD redundant with by-copy capture default",
10501 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10502 && capture_kind
== BY_REFERENCE
)
10503 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10504 "capture of %qD redundant with by-reference capture "
10505 "default", capture_id
);
10508 add_capture (lambda_expr
,
10511 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10514 /* If there is any qualification still in effect, clear it
10515 now; we will be starting fresh with the next capture. */
10516 parser
->scope
= NULL_TREE
;
10517 parser
->qualifying_scope
= NULL_TREE
;
10518 parser
->object_scope
= NULL_TREE
;
10521 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10524 /* Parse the (optional) middle of a lambda expression.
10527 < template-parameter-list [opt] >
10528 ( parameter-declaration-clause [opt] )
10529 attribute-specifier [opt]
10530 decl-specifier-seq [opt]
10531 exception-specification [opt]
10532 lambda-return-type-clause [opt]
10534 LAMBDA_EXPR is the current representation of the lambda expression. */
10537 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10539 /* 5.1.1.4 of the standard says:
10540 If a lambda-expression does not include a lambda-declarator, it is as if
10541 the lambda-declarator were ().
10542 This means an empty parameter list, no attributes, and no exception
10544 tree param_list
= void_list_node
;
10545 tree attributes
= NULL_TREE
;
10546 tree exception_spec
= NULL_TREE
;
10547 tree template_param_list
= NULL_TREE
;
10548 tree tx_qual
= NULL_TREE
;
10549 tree return_type
= NULL_TREE
;
10550 cp_decl_specifier_seq lambda_specs
;
10551 clear_decl_specs (&lambda_specs
);
10553 /* The template-parameter-list is optional, but must begin with
10554 an opening angle if present. */
10555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10557 if (cxx_dialect
< cxx14
)
10558 pedwarn (parser
->lexer
->next_token
->location
, 0,
10559 "lambda templates are only available with "
10560 "-std=c++14 or -std=gnu++14");
10561 else if (cxx_dialect
< cxx2a
)
10562 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10563 "lambda templates are only available with "
10564 "-std=c++2a or -std=gnu++2a");
10566 cp_lexer_consume_token (parser
->lexer
);
10568 template_param_list
= cp_parser_template_parameter_list (parser
);
10570 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10572 /* We just processed one more parameter list. */
10573 ++parser
->num_template_parameter_lists
;
10576 /* The parameter-declaration-clause is optional (unless
10577 template-parameter-list was given), but must begin with an
10578 opening parenthesis if present. */
10579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10581 matching_parens parens
;
10582 parens
.consume_open (parser
);
10584 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10586 /* Parse parameters. */
10587 param_list
= cp_parser_parameter_declaration_clause (parser
);
10589 /* Default arguments shall not be specified in the
10590 parameter-declaration-clause of a lambda-declarator. */
10591 if (cxx_dialect
< cxx14
)
10592 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10593 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10594 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10595 "default argument specified for lambda parameter");
10597 parens
.require_close (parser
);
10599 /* In the decl-specifier-seq of the lambda-declarator, each
10600 decl-specifier shall either be mutable or constexpr. */
10601 int declares_class_or_enum
;
10602 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10603 cp_parser_decl_specifier_seq (parser
,
10604 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10605 &lambda_specs
, &declares_class_or_enum
);
10606 if (lambda_specs
.storage_class
== sc_mutable
)
10608 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10609 if (lambda_specs
.conflicting_specifiers_p
)
10610 error_at (lambda_specs
.locations
[ds_storage_class
],
10611 "duplicate %<mutable%>");
10614 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10616 /* Parse optional exception specification. */
10617 exception_spec
= cp_parser_exception_specification_opt (parser
);
10619 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10621 /* Parse optional trailing return type. */
10622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10624 cp_lexer_consume_token (parser
->lexer
);
10625 return_type
= cp_parser_trailing_type_id (parser
);
10628 /* The function parameters must be in scope all the way until after the
10629 trailing-return-type in case of decltype. */
10630 pop_bindings_and_leave_scope ();
10632 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10633 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10635 /* Create the function call operator.
10637 Messing with declarators like this is no uglier than building up the
10638 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10641 cp_decl_specifier_seq return_type_specs
;
10642 cp_declarator
* declarator
;
10647 clear_decl_specs (&return_type_specs
);
10648 return_type_specs
.type
= make_auto ();
10650 if (lambda_specs
.locations
[ds_constexpr
])
10652 if (cxx_dialect
>= cxx17
)
10653 return_type_specs
.locations
[ds_constexpr
]
10654 = lambda_specs
.locations
[ds_constexpr
];
10656 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10657 "lambda only available with -std=c++17 or -std=gnu++17");
10660 p
= obstack_alloc (&declarator_obstack
, 0);
10662 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10664 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10665 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10666 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10667 VIRT_SPEC_UNSPECIFIED
,
10672 /*requires_clause*/NULL_TREE
);
10673 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10674 declarator
->std_attributes
= attributes
;
10676 fco
= grokmethod (&return_type_specs
,
10679 if (fco
!= error_mark_node
)
10681 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10682 DECL_ARTIFICIAL (fco
) = 1;
10683 /* Give the object parameter a different name. */
10684 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10685 DECL_LAMBDA_FUNCTION (fco
) = 1;
10687 if (template_param_list
)
10689 fco
= finish_member_template_decl (fco
);
10690 finish_template_decl (template_param_list
);
10691 --parser
->num_template_parameter_lists
;
10693 else if (parser
->fully_implicit_function_template_p
)
10694 fco
= finish_fully_implicit_template (parser
, fco
);
10696 finish_member_declaration (fco
);
10698 obstack_free (&declarator_obstack
, p
);
10700 return (fco
!= error_mark_node
);
10704 /* Parse the body of a lambda expression, which is simply
10708 but which requires special handling.
10709 LAMBDA_EXPR is the current representation of the lambda expression. */
10712 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10714 bool nested
= (current_function_decl
!= NULL_TREE
);
10715 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10716 bool in_function_body
= parser
->in_function_body
;
10719 push_function_context ();
10721 /* Still increment function_depth so that we don't GC in the
10722 middle of an expression. */
10725 vec
<tree
> omp_privatization_save
;
10726 save_omp_privatization_clauses (omp_privatization_save
);
10727 /* Clear this in case we're in the middle of a default argument. */
10728 parser
->local_variables_forbidden_p
= false;
10729 parser
->in_function_body
= true;
10732 local_specialization_stack
s (lss_copy
);
10733 tree fco
= lambda_function (lambda_expr
);
10734 tree body
= start_lambda_function (fco
, lambda_expr
);
10735 matching_braces braces
;
10737 if (braces
.require_open (parser
))
10739 tree compound_stmt
= begin_compound_stmt (0);
10741 /* Originally C++11 required us to peek for 'return expr'; and
10742 process it specially here to deduce the return type. N3638
10743 removed the need for that. */
10745 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10746 cp_parser_label_declaration (parser
);
10747 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10748 braces
.require_close (parser
);
10750 finish_compound_stmt (compound_stmt
);
10753 finish_lambda_function (body
);
10756 restore_omp_privatization_clauses (omp_privatization_save
);
10757 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10758 parser
->in_function_body
= in_function_body
;
10760 pop_function_context();
10765 /* Statements [gram.stmt.stmt] */
10767 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10770 add_debug_begin_stmt (location_t loc
)
10772 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10774 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10775 /* A concept is never expanded normally. */
10778 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10779 SET_EXPR_LOCATION (stmt
, loc
);
10783 /* Parse a statement.
10787 expression-statement
10789 selection-statement
10790 iteration-statement
10792 declaration-statement
10799 attribute-specifier-seq (opt) expression-statement
10800 attribute-specifier-seq (opt) compound-statement
10801 attribute-specifier-seq (opt) selection-statement
10802 attribute-specifier-seq (opt) iteration-statement
10803 attribute-specifier-seq (opt) jump-statement
10804 declaration-statement
10805 attribute-specifier-seq (opt) try-block
10808 expression-statement
10816 IN_COMPOUND is true when the statement is nested inside a
10817 cp_parser_compound_statement; this matters for certain pragmas.
10819 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10820 is a (possibly labeled) if statement which is not enclosed in braces
10821 and has an else clause. This is used to implement -Wparentheses.
10823 CHAIN is a vector of if-else-if conditions. */
10826 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10827 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10828 location_t
*loc_after_labels
)
10830 tree statement
, std_attrs
= NULL_TREE
;
10832 location_t statement_location
, attrs_location
;
10837 /* There is no statement yet. */
10838 statement
= NULL_TREE
;
10840 saved_token_sentinel
saved_tokens (parser
->lexer
);
10841 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10842 if (c_dialect_objc ())
10843 /* In obj-c++, seeing '[[' might be the either the beginning of
10844 c++11 attributes, or a nested objc-message-expression. So
10845 let's parse the c++11 attributes tentatively. */
10846 cp_parser_parse_tentatively (parser
);
10847 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10848 if (c_dialect_objc ())
10850 if (!cp_parser_parse_definitely (parser
))
10851 std_attrs
= NULL_TREE
;
10854 /* Peek at the next token. */
10855 token
= cp_lexer_peek_token (parser
->lexer
);
10856 /* Remember the location of the first token in the statement. */
10857 statement_location
= token
->location
;
10858 add_debug_begin_stmt (statement_location
);
10859 /* If this is a keyword, then that will often determine what kind of
10860 statement we have. */
10861 if (token
->type
== CPP_KEYWORD
)
10863 enum rid keyword
= token
->keyword
;
10869 /* Looks like a labeled-statement with a case label.
10870 Parse the label, and then use tail recursion to parse
10872 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10873 in_compound
= false;
10878 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10884 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10891 statement
= cp_parser_jump_statement (parser
);
10894 /* Objective-C++ exception-handling constructs. */
10897 case RID_AT_FINALLY
:
10898 case RID_AT_SYNCHRONIZED
:
10900 statement
= cp_parser_objc_statement (parser
);
10904 statement
= cp_parser_try_block (parser
);
10907 case RID_NAMESPACE
:
10908 /* This must be a namespace alias definition. */
10909 cp_parser_declaration_statement (parser
);
10912 case RID_TRANSACTION_ATOMIC
:
10913 case RID_TRANSACTION_RELAXED
:
10914 case RID_SYNCHRONIZED
:
10915 case RID_ATOMIC_NOEXCEPT
:
10916 case RID_ATOMIC_CANCEL
:
10917 statement
= cp_parser_transaction (parser
, token
);
10919 case RID_TRANSACTION_CANCEL
:
10920 statement
= cp_parser_transaction_cancel (parser
);
10924 /* It might be a keyword like `int' that can start a
10925 declaration-statement. */
10929 else if (token
->type
== CPP_NAME
)
10931 /* If the next token is a `:', then we are looking at a
10932 labeled-statement. */
10933 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10934 if (token
->type
== CPP_COLON
)
10936 /* Looks like a labeled-statement with an ordinary label.
10937 Parse the label, and then use tail recursion to parse
10940 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10941 in_compound
= false;
10945 /* Anything that starts with a `{' must be a compound-statement. */
10946 else if (token
->type
== CPP_OPEN_BRACE
)
10947 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10948 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10949 a statement all its own. */
10950 else if (token
->type
== CPP_PRAGMA
)
10952 /* Only certain OpenMP pragmas are attached to statements, and thus
10953 are considered statements themselves. All others are not. In
10954 the context of a compound, accept the pragma as a "statement" and
10955 return so that we can check for a close brace. Otherwise we
10956 require a real statement and must go back and read one. */
10958 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10959 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10963 else if (token
->type
== CPP_EOF
)
10965 cp_parser_error (parser
, "expected statement");
10969 /* Everything else must be a declaration-statement or an
10970 expression-statement. Try for the declaration-statement
10971 first, unless we are looking at a `;', in which case we know that
10972 we have an expression-statement. */
10975 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10977 if (std_attrs
!= NULL_TREE
)
10979 /* Attributes should be parsed as part of the the
10980 declaration, so let's un-parse them. */
10981 saved_tokens
.rollback();
10982 std_attrs
= NULL_TREE
;
10985 cp_parser_parse_tentatively (parser
);
10986 /* Try to parse the declaration-statement. */
10987 cp_parser_declaration_statement (parser
);
10988 /* If that worked, we're done. */
10989 if (cp_parser_parse_definitely (parser
))
10992 /* All preceding labels have been parsed at this point. */
10993 if (loc_after_labels
!= NULL
)
10994 *loc_after_labels
= statement_location
;
10996 /* Look for an expression-statement instead. */
10997 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10999 /* Handle [[fallthrough]];. */
11000 if (attribute_fallthrough_p (std_attrs
))
11002 /* The next token after the fallthrough attribute is ';'. */
11003 if (statement
== NULL_TREE
)
11005 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11006 statement
= build_call_expr_internal_loc (statement_location
,
11008 void_type_node
, 0);
11009 finish_expr_stmt (statement
);
11012 warning_at (statement_location
, OPT_Wattributes
,
11013 "%<fallthrough%> attribute not followed by %<;%>");
11014 std_attrs
= NULL_TREE
;
11018 /* Set the line number for the statement. */
11019 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11020 SET_EXPR_LOCATION (statement
, statement_location
);
11022 /* Allow "[[fallthrough]];", but warn otherwise. */
11023 if (std_attrs
!= NULL_TREE
)
11024 warning_at (attrs_location
,
11026 "attributes at the beginning of statement are ignored");
11029 /* Append ATTR to attribute list ATTRS. */
11032 attr_chainon (tree attrs
, tree attr
)
11034 if (attrs
== error_mark_node
)
11035 return error_mark_node
;
11036 if (attr
== error_mark_node
)
11037 return error_mark_node
;
11038 return chainon (attrs
, attr
);
11041 /* Parse the label for a labeled-statement, i.e.
11044 case constant-expression :
11048 case constant-expression ... constant-expression : statement
11050 When a label is parsed without errors, the label is added to the
11051 parse tree by the finish_* functions, so this function doesn't
11052 have to return the label. */
11055 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11058 tree label
= NULL_TREE
;
11059 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11061 /* The next token should be an identifier. */
11062 token
= cp_lexer_peek_token (parser
->lexer
);
11063 if (token
->type
!= CPP_NAME
11064 && token
->type
!= CPP_KEYWORD
)
11066 cp_parser_error (parser
, "expected labeled-statement");
11070 /* Remember whether this case or a user-defined label is allowed to fall
11072 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11074 parser
->colon_corrects_to_scope_p
= false;
11075 switch (token
->keyword
)
11079 tree expr
, expr_hi
;
11080 cp_token
*ellipsis
;
11082 /* Consume the `case' token. */
11083 cp_lexer_consume_token (parser
->lexer
);
11084 /* Parse the constant-expression. */
11085 expr
= cp_parser_constant_expression (parser
);
11086 if (check_for_bare_parameter_packs (expr
))
11087 expr
= error_mark_node
;
11089 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11090 if (ellipsis
->type
== CPP_ELLIPSIS
)
11092 /* Consume the `...' token. */
11093 cp_lexer_consume_token (parser
->lexer
);
11094 expr_hi
= cp_parser_constant_expression (parser
);
11095 if (check_for_bare_parameter_packs (expr_hi
))
11096 expr_hi
= error_mark_node
;
11098 /* We don't need to emit warnings here, as the common code
11099 will do this for us. */
11102 expr_hi
= NULL_TREE
;
11104 if (parser
->in_switch_statement_p
)
11106 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11107 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11108 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11111 error_at (token
->location
,
11112 "case label %qE not within a switch statement",
11118 /* Consume the `default' token. */
11119 cp_lexer_consume_token (parser
->lexer
);
11121 if (parser
->in_switch_statement_p
)
11123 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11124 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11125 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11128 error_at (token
->location
, "case label not within a switch statement");
11132 /* Anything else must be an ordinary label. */
11133 label
= finish_label_stmt (cp_parser_identifier (parser
));
11134 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11135 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11139 /* Require the `:' token. */
11140 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11142 /* An ordinary label may optionally be followed by attributes.
11143 However, this is only permitted if the attributes are then
11144 followed by a semicolon. This is because, for backward
11145 compatibility, when parsing
11146 lab: __attribute__ ((unused)) int i;
11147 we want the attribute to attach to "i", not "lab". */
11148 if (label
!= NULL_TREE
11149 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11152 cp_parser_parse_tentatively (parser
);
11153 attrs
= cp_parser_gnu_attributes_opt (parser
);
11154 if (attrs
== NULL_TREE
11155 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11156 cp_parser_abort_tentative_parse (parser
);
11157 else if (!cp_parser_parse_definitely (parser
))
11160 attributes
= attr_chainon (attributes
, attrs
);
11163 if (attributes
!= NULL_TREE
)
11164 cplus_decl_attributes (&label
, attributes
, 0);
11166 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11169 /* Parse an expression-statement.
11171 expression-statement:
11174 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11175 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11176 indicates whether this expression-statement is part of an
11177 expression statement. */
11180 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11182 tree statement
= NULL_TREE
;
11183 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11184 location_t loc
= token
->location
;
11186 /* There might be attribute fallthrough. */
11187 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11189 /* If the next token is a ';', then there is no expression
11191 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11193 statement
= cp_parser_expression (parser
);
11194 if (statement
== error_mark_node
11195 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11197 cp_parser_skip_to_end_of_block_or_statement (parser
);
11198 return error_mark_node
;
11202 /* Handle [[fallthrough]];. */
11203 if (attribute_fallthrough_p (attr
))
11205 /* The next token after the fallthrough attribute is ';'. */
11206 if (statement
== NULL_TREE
)
11207 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11208 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11209 void_type_node
, 0);
11211 warning_at (loc
, OPT_Wattributes
,
11212 "%<fallthrough%> attribute not followed by %<;%>");
11216 /* Allow "[[fallthrough]];", but warn otherwise. */
11217 if (attr
!= NULL_TREE
)
11218 warning_at (loc
, OPT_Wattributes
,
11219 "attributes at the beginning of statement are ignored");
11221 /* Give a helpful message for "A<T>::type t;" and the like. */
11222 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11223 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11225 if (TREE_CODE (statement
) == SCOPE_REF
)
11226 error_at (token
->location
, "need %<typename%> before %qE because "
11227 "%qT is a dependent scope",
11228 statement
, TREE_OPERAND (statement
, 0));
11229 else if (is_overloaded_fn (statement
)
11230 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11233 tree fn
= get_first_fn (statement
);
11234 error_at (token
->location
,
11235 "%<%T::%D%> names the constructor, not the type",
11236 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11240 /* Consume the final `;'. */
11241 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11243 if (in_statement_expr
11244 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11245 /* This is the final expression statement of a statement
11247 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11248 else if (statement
)
11249 statement
= finish_expr_stmt (statement
);
11254 /* Parse a compound-statement.
11256 compound-statement:
11257 { statement-seq [opt] }
11261 compound-statement:
11262 { label-declaration-seq [opt] statement-seq [opt] }
11264 label-declaration-seq:
11266 label-declaration-seq label-declaration
11268 Returns a tree representing the statement. */
11271 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11272 int bcs_flags
, bool function_body
)
11274 tree compound_stmt
;
11275 matching_braces braces
;
11277 /* Consume the `{'. */
11278 if (!braces
.require_open (parser
))
11279 return error_mark_node
;
11280 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11281 && !function_body
&& cxx_dialect
< cxx14
)
11282 pedwarn (input_location
, OPT_Wpedantic
,
11283 "compound-statement in %<constexpr%> function");
11284 /* Begin the compound-statement. */
11285 compound_stmt
= begin_compound_stmt (bcs_flags
);
11286 /* If the next keyword is `__label__' we have a label declaration. */
11287 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11288 cp_parser_label_declaration (parser
);
11289 /* Parse an (optional) statement-seq. */
11290 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11291 /* Finish the compound-statement. */
11292 finish_compound_stmt (compound_stmt
);
11293 /* Consume the `}'. */
11294 braces
.require_close (parser
);
11296 return compound_stmt
;
11299 /* Parse an (optional) statement-seq.
11303 statement-seq [opt] statement */
11306 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11308 /* Scan statements until there aren't any more. */
11311 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11313 /* If we are looking at a `}', then we have run out of
11314 statements; the same is true if we have reached the end
11315 of file, or have stumbled upon a stray '@end'. */
11316 if (token
->type
== CPP_CLOSE_BRACE
11317 || token
->type
== CPP_EOF
11318 || token
->type
== CPP_PRAGMA_EOL
11319 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11322 /* If we are in a compound statement and find 'else' then
11323 something went wrong. */
11324 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11326 if (parser
->in_statement
& IN_IF_STMT
)
11330 token
= cp_lexer_consume_token (parser
->lexer
);
11331 error_at (token
->location
, "%<else%> without a previous %<if%>");
11335 /* Parse the statement. */
11336 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11340 /* Return true if this is the C++20 version of range-based-for with
11344 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11348 /* Save tokens so that we can put them back. */
11349 cp_lexer_save_tokens (parser
->lexer
);
11351 /* There has to be an unnested ; followed by an unnested :. */
11352 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11353 /*recovering=*/false,
11355 /*consume_paren=*/false) != -1)
11358 /* We found the semicolon, eat it now. */
11359 cp_lexer_consume_token (parser
->lexer
);
11361 /* Now look for ':' that is not nested in () or {}. */
11362 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11363 /*recovering=*/false,
11365 /*consume_paren=*/false) == -1);
11368 /* Roll back the tokens we skipped. */
11369 cp_lexer_rollback_tokens (parser
->lexer
);
11374 /* Return true if we're looking at (init; cond), false otherwise. */
11377 cp_parser_init_statement_p (cp_parser
*parser
)
11379 /* Save tokens so that we can put them back. */
11380 cp_lexer_save_tokens (parser
->lexer
);
11382 /* Look for ';' that is not nested in () or {}. */
11383 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11384 /*recovering=*/false,
11386 /*consume_paren=*/false);
11388 /* Roll back the tokens we skipped. */
11389 cp_lexer_rollback_tokens (parser
->lexer
);
11394 /* Parse a selection-statement.
11396 selection-statement:
11397 if ( init-statement [opt] condition ) statement
11398 if ( init-statement [opt] condition ) statement else statement
11399 switch ( init-statement [opt] condition ) statement
11401 Returns the new IF_STMT or SWITCH_STMT.
11403 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11404 is a (possibly labeled) if statement which is not enclosed in
11405 braces and has an else clause. This is used to implement
11408 CHAIN is a vector of if-else-if conditions. This is used to implement
11409 -Wduplicated-cond. */
11412 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11417 token_indent_info guard_tinfo
;
11422 /* Peek at the next token. */
11423 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11424 guard_tinfo
= get_token_indent_info (token
);
11426 /* See what kind of keyword it is. */
11427 keyword
= token
->keyword
;
11437 if (keyword
== RID_IF
11438 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11442 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11443 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11444 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11445 "with -std=c++17 or -std=gnu++17");
11448 /* Look for the `('. */
11449 matching_parens parens
;
11450 if (!parens
.require_open (parser
))
11452 cp_parser_skip_to_end_of_statement (parser
);
11453 return error_mark_node
;
11456 /* Begin the selection-statement. */
11457 if (keyword
== RID_IF
)
11459 statement
= begin_if_stmt ();
11460 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11463 statement
= begin_switch_stmt ();
11465 /* Parse the optional init-statement. */
11466 if (cp_parser_init_statement_p (parser
))
11469 if (cxx_dialect
< cxx17
)
11470 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11471 "init-statement in selection statements only available "
11472 "with -std=c++17 or -std=gnu++17");
11473 cp_parser_init_statement (parser
, &decl
);
11476 /* Parse the condition. */
11477 condition
= cp_parser_condition (parser
);
11478 /* Look for the `)'. */
11479 if (!parens
.require_close (parser
))
11480 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11481 /*consume_paren=*/true);
11483 if (keyword
== RID_IF
)
11486 unsigned char in_statement
;
11488 /* Add the condition. */
11489 condition
= finish_if_stmt_cond (condition
, statement
);
11491 if (warn_duplicated_cond
)
11492 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11495 /* Parse the then-clause. */
11496 in_statement
= parser
->in_statement
;
11497 parser
->in_statement
|= IN_IF_STMT
;
11499 /* Outside a template, the non-selected branch of a constexpr
11500 if is a 'discarded statement', i.e. unevaluated. */
11501 bool was_discarded
= in_discarded_stmt
;
11502 bool discard_then
= (cx
&& !processing_template_decl
11503 && integer_zerop (condition
));
11506 in_discarded_stmt
= true;
11507 ++c_inhibit_evaluation_warnings
;
11510 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11513 parser
->in_statement
= in_statement
;
11515 finish_then_clause (statement
);
11519 THEN_CLAUSE (statement
) = NULL_TREE
;
11520 in_discarded_stmt
= was_discarded
;
11521 --c_inhibit_evaluation_warnings
;
11524 /* If the next token is `else', parse the else-clause. */
11525 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11528 bool discard_else
= (cx
&& !processing_template_decl
11529 && integer_nonzerop (condition
));
11532 in_discarded_stmt
= true;
11533 ++c_inhibit_evaluation_warnings
;
11537 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11538 /* Consume the `else' keyword. */
11539 cp_lexer_consume_token (parser
->lexer
);
11540 if (warn_duplicated_cond
)
11542 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11546 /* We've got "if (COND) else if (COND2)". Start
11547 the condition chain and add COND as the first
11549 chain
= new vec
<tree
> ();
11550 if (!CONSTANT_CLASS_P (condition
)
11551 && !TREE_SIDE_EFFECTS (condition
))
11553 /* Wrap it in a NOP_EXPR so that we can set the
11554 location of the condition. */
11555 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11557 SET_EXPR_LOCATION (e
, token
->location
);
11558 chain
->safe_push (e
);
11561 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11564 /* This is if-else without subsequent if. Zap the
11565 condition chain; we would have already warned at
11571 begin_else_clause (statement
);
11572 /* Parse the else-clause. */
11573 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11574 guard_tinfo
, chain
);
11576 finish_else_clause (statement
);
11578 /* If we are currently parsing a then-clause, then
11579 IF_P will not be NULL. We set it to true to
11580 indicate that this if statement has an else clause.
11581 This may trigger the Wparentheses warning below
11582 when we get back up to the parent if statement. */
11588 ELSE_CLAUSE (statement
) = NULL_TREE
;
11589 in_discarded_stmt
= was_discarded
;
11590 --c_inhibit_evaluation_warnings
;
11595 /* This if statement does not have an else clause. If
11596 NESTED_IF is true, then the then-clause has an if
11597 statement which does have an else clause. We warn
11598 about the potential ambiguity. */
11600 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11601 "suggest explicit braces to avoid ambiguous"
11603 if (warn_duplicated_cond
)
11605 /* We don't need the condition chain anymore. */
11611 /* Now we're all done with the if-statement. */
11612 finish_if_stmt (statement
);
11616 bool in_switch_statement_p
;
11617 unsigned char in_statement
;
11619 /* Add the condition. */
11620 finish_switch_cond (condition
, statement
);
11622 /* Parse the body of the switch-statement. */
11623 in_switch_statement_p
= parser
->in_switch_statement_p
;
11624 in_statement
= parser
->in_statement
;
11625 parser
->in_switch_statement_p
= true;
11626 parser
->in_statement
|= IN_SWITCH_STMT
;
11627 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11629 parser
->in_switch_statement_p
= in_switch_statement_p
;
11630 parser
->in_statement
= in_statement
;
11632 /* Now we're all done with the switch-statement. */
11633 finish_switch_stmt (statement
);
11641 cp_parser_error (parser
, "expected selection-statement");
11642 return error_mark_node
;
11646 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11647 If we have seen at least one decl-specifier, and the next token
11648 is not a parenthesis, then we must be looking at a declaration.
11649 (After "int (" we might be looking at a functional cast.) */
11652 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11653 bool any_specifiers_p
)
11655 if (any_specifiers_p
11656 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11657 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11658 && !cp_parser_error_occurred (parser
))
11659 cp_parser_commit_to_tentative_parse (parser
);
11662 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11663 The declarator shall not specify a function or an array. Returns
11664 TRUE if the declarator is valid, FALSE otherwise. */
11667 cp_parser_check_condition_declarator (cp_parser
* parser
,
11668 cp_declarator
*declarator
,
11671 if (declarator
== cp_error_declarator
11672 || function_declarator_p (declarator
)
11673 || declarator
->kind
== cdk_array
)
11675 if (declarator
== cp_error_declarator
)
11676 /* Already complained. */;
11677 else if (declarator
->kind
== cdk_array
)
11678 error_at (loc
, "condition declares an array");
11680 error_at (loc
, "condition declares a function");
11681 if (parser
->fully_implicit_function_template_p
)
11682 abort_fully_implicit_template (parser
);
11683 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11684 /*or_comma=*/false,
11685 /*consume_paren=*/false);
11692 /* Parse a condition.
11696 type-specifier-seq declarator = initializer-clause
11697 type-specifier-seq declarator braced-init-list
11702 type-specifier-seq declarator asm-specification [opt]
11703 attributes [opt] = assignment-expression
11705 Returns the expression that should be tested. */
11708 cp_parser_condition (cp_parser
* parser
)
11710 cp_decl_specifier_seq type_specifiers
;
11711 const char *saved_message
;
11712 int declares_class_or_enum
;
11714 /* Try the declaration first. */
11715 cp_parser_parse_tentatively (parser
);
11716 /* New types are not allowed in the type-specifier-seq for a
11718 saved_message
= parser
->type_definition_forbidden_message
;
11719 parser
->type_definition_forbidden_message
11720 = G_("types may not be defined in conditions");
11721 /* Parse the type-specifier-seq. */
11722 cp_parser_decl_specifier_seq (parser
,
11723 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11725 &declares_class_or_enum
);
11726 /* Restore the saved message. */
11727 parser
->type_definition_forbidden_message
= saved_message
;
11729 cp_parser_maybe_commit_to_declaration (parser
,
11730 type_specifiers
.any_specifiers_p
);
11732 /* If all is well, we might be looking at a declaration. */
11733 if (!cp_parser_error_occurred (parser
))
11736 tree asm_specification
;
11738 cp_declarator
*declarator
;
11739 tree initializer
= NULL_TREE
;
11740 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11742 /* Parse the declarator. */
11743 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11744 /*ctor_dtor_or_conv_p=*/NULL
,
11745 /*parenthesized_p=*/NULL
,
11746 /*member_p=*/false,
11747 /*friend_p=*/false);
11748 /* Parse the attributes. */
11749 attributes
= cp_parser_attributes_opt (parser
);
11750 /* Parse the asm-specification. */
11751 asm_specification
= cp_parser_asm_specification_opt (parser
);
11752 /* If the next token is not an `=' or '{', then we might still be
11753 looking at an expression. For example:
11757 looks like a decl-specifier-seq and a declarator -- but then
11758 there is no `=', so this is an expression. */
11759 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11760 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11761 cp_parser_simulate_error (parser
);
11763 /* If we did see an `=' or '{', then we are looking at a declaration
11765 if (cp_parser_parse_definitely (parser
))
11768 bool non_constant_p
= false;
11769 int flags
= LOOKUP_ONLYCONVERTING
;
11771 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11772 return error_mark_node
;
11774 /* Create the declaration. */
11775 decl
= start_decl (declarator
, &type_specifiers
,
11776 /*initialized_p=*/true,
11777 attributes
, /*prefix_attributes=*/NULL_TREE
,
11780 /* Parse the initializer. */
11781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11783 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11784 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11787 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11789 /* Consume the `='. */
11790 cp_lexer_consume_token (parser
->lexer
);
11791 initializer
= cp_parser_initializer_clause (parser
,
11796 cp_parser_error (parser
, "expected initializer");
11797 initializer
= error_mark_node
;
11799 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11800 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11802 /* Process the initializer. */
11803 cp_finish_decl (decl
,
11804 initializer
, !non_constant_p
,
11809 pop_scope (pushed_scope
);
11811 return convert_from_reference (decl
);
11814 /* If we didn't even get past the declarator successfully, we are
11815 definitely not looking at a declaration. */
11817 cp_parser_abort_tentative_parse (parser
);
11819 /* Otherwise, we are looking at an expression. */
11820 return cp_parser_expression (parser
);
11823 /* Parses a for-statement or range-for-statement until the closing ')',
11827 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11829 tree init
, scope
, decl
;
11832 /* Begin the for-statement. */
11833 scope
= begin_for_scope (&init
);
11835 /* Parse the initialization. */
11836 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11839 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11841 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11845 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11846 unsigned short unroll
)
11848 /* Normal for loop */
11849 tree condition
= NULL_TREE
;
11850 tree expression
= NULL_TREE
;
11853 stmt
= begin_for_stmt (scope
, init
);
11854 /* The init-statement has already been parsed in
11855 cp_parser_init_statement, so no work is needed here. */
11856 finish_init_stmt (stmt
);
11858 /* If there's a condition, process it. */
11859 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11860 condition
= cp_parser_condition (parser
);
11863 cp_parser_error (parser
, "missing loop condition in loop with "
11864 "%<GCC ivdep%> pragma");
11865 condition
= error_mark_node
;
11869 cp_parser_error (parser
, "missing loop condition in loop with "
11870 "%<GCC unroll%> pragma");
11871 condition
= error_mark_node
;
11873 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11874 /* Look for the `;'. */
11875 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11877 /* If there's an expression, process it. */
11878 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11879 expression
= cp_parser_expression (parser
);
11880 finish_for_expr (expression
, stmt
);
11885 /* Tries to parse a range-based for-statement:
11888 decl-specifier-seq declarator : expression
11890 The decl-specifier-seq declarator and the `:' are already parsed by
11891 cp_parser_init_statement. If processing_template_decl it returns a
11892 newly created RANGE_FOR_STMT; if not, it is converted to a
11893 regular FOR_STMT. */
11896 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11897 bool ivdep
, unsigned short unroll
)
11899 tree stmt
, range_expr
;
11900 auto_vec
<cxx_binding
*, 16> bindings
;
11901 auto_vec
<tree
, 16> names
;
11902 tree decomp_first_name
= NULL_TREE
;
11903 unsigned int decomp_cnt
= 0;
11905 /* Get the range declaration momentarily out of the way so that
11906 the range expression doesn't clash with it. */
11907 if (range_decl
!= error_mark_node
)
11909 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11911 tree v
= DECL_VALUE_EXPR (range_decl
);
11912 /* For decomposition declaration get all of the corresponding
11913 declarations out of the way. */
11914 if (TREE_CODE (v
) == ARRAY_REF
11915 && VAR_P (TREE_OPERAND (v
, 0))
11916 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11918 tree d
= range_decl
;
11919 range_decl
= TREE_OPERAND (v
, 0);
11920 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11921 decomp_first_name
= d
;
11922 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11924 tree name
= DECL_NAME (d
);
11925 names
.safe_push (name
);
11926 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11927 IDENTIFIER_BINDING (name
)
11928 = IDENTIFIER_BINDING (name
)->previous
;
11932 if (names
.is_empty ())
11934 tree name
= DECL_NAME (range_decl
);
11935 names
.safe_push (name
);
11936 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11937 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11943 bool expr_non_constant_p
;
11944 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11947 range_expr
= cp_parser_expression (parser
);
11949 /* Put the range declaration(s) back into scope. */
11950 for (unsigned int i
= 0; i
< names
.length (); i
++)
11952 cxx_binding
*binding
= bindings
[i
];
11953 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11954 IDENTIFIER_BINDING (names
[i
]) = binding
;
11957 /* If in template, STMT is converted to a normal for-statement
11958 at instantiation. If not, it is done just ahead. */
11959 if (processing_template_decl
)
11961 if (check_for_bare_parameter_packs (range_expr
))
11962 range_expr
= error_mark_node
;
11963 stmt
= begin_range_for_stmt (scope
, init
);
11965 RANGE_FOR_IVDEP (stmt
) = 1;
11967 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11968 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11969 if (!type_dependent_expression_p (range_expr
)
11970 /* do_auto_deduction doesn't mess with template init-lists. */
11971 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11972 do_range_for_auto_deduction (range_decl
, range_expr
);
11976 stmt
= begin_for_stmt (scope
, init
);
11977 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11978 decomp_first_name
, decomp_cnt
, ivdep
,
11984 /* Subroutine of cp_convert_range_for: given the initializer expression,
11985 builds up the range temporary. */
11988 build_range_temp (tree range_expr
)
11990 tree range_type
, range_temp
;
11992 /* Find out the type deduced by the declaration
11993 `auto &&__range = range_expr'. */
11994 range_type
= cp_build_reference_type (make_auto (), true);
11995 range_type
= do_auto_deduction (range_type
, range_expr
,
11996 type_uses_auto (range_type
));
11998 /* Create the __range variable. */
11999 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12001 TREE_USED (range_temp
) = 1;
12002 DECL_ARTIFICIAL (range_temp
) = 1;
12007 /* Used by cp_parser_range_for in template context: we aren't going to
12008 do a full conversion yet, but we still need to resolve auto in the
12009 type of the for-range-declaration if present. This is basically
12010 a shortcut version of cp_convert_range_for. */
12013 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12015 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12018 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12019 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12020 iter_type
= (cp_parser_perform_range_for_lookup
12021 (range_temp
, &begin_dummy
, &end_dummy
));
12024 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12026 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12028 tf_warning_or_error
);
12029 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12030 iter_decl
, auto_node
);
12035 /* Converts a range-based for-statement into a normal
12036 for-statement, as per the definition.
12038 for (RANGE_DECL : RANGE_EXPR)
12041 should be equivalent to:
12044 auto &&__range = RANGE_EXPR;
12045 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12049 RANGE_DECL = *__begin;
12054 If RANGE_EXPR is an array:
12055 BEGIN_EXPR = __range
12056 END_EXPR = __range + ARRAY_SIZE(__range)
12057 Else if RANGE_EXPR has a member 'begin' or 'end':
12058 BEGIN_EXPR = __range.begin()
12059 END_EXPR = __range.end()
12061 BEGIN_EXPR = begin(__range)
12062 END_EXPR = end(__range);
12064 If __range has a member 'begin' but not 'end', or vice versa, we must
12065 still use the second alternative (it will surely fail, however).
12066 When calling begin()/end() in the third alternative we must use
12067 argument dependent lookup, but always considering 'std' as an associated
12071 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12072 tree decomp_first_name
, unsigned int decomp_cnt
,
12073 bool ivdep
, unsigned short unroll
)
12076 tree iter_type
, begin_expr
, end_expr
;
12077 tree condition
, expression
;
12079 range_expr
= mark_lvalue_use (range_expr
);
12081 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12082 /* If an error happened previously do nothing or else a lot of
12083 unhelpful errors would be issued. */
12084 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12089 if (VAR_P (range_expr
)
12090 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12091 /* Can't bind a reference to an array of runtime bound. */
12092 range_temp
= range_expr
;
12095 range_temp
= build_range_temp (range_expr
);
12096 pushdecl (range_temp
);
12097 cp_finish_decl (range_temp
, range_expr
,
12098 /*is_constant_init*/false, NULL_TREE
,
12099 LOOKUP_ONLYCONVERTING
);
12100 range_temp
= convert_from_reference (range_temp
);
12102 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12103 &begin_expr
, &end_expr
);
12106 /* The new for initialization statement. */
12107 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12109 TREE_USED (begin
) = 1;
12110 DECL_ARTIFICIAL (begin
) = 1;
12112 cp_finish_decl (begin
, begin_expr
,
12113 /*is_constant_init*/false, NULL_TREE
,
12114 LOOKUP_ONLYCONVERTING
);
12116 if (cxx_dialect
>= cxx17
)
12117 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12118 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12119 TREE_USED (end
) = 1;
12120 DECL_ARTIFICIAL (end
) = 1;
12122 cp_finish_decl (end
, end_expr
,
12123 /*is_constant_init*/false, NULL_TREE
,
12124 LOOKUP_ONLYCONVERTING
);
12126 finish_init_stmt (statement
);
12128 /* The new for condition. */
12129 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12132 NULL
, tf_warning_or_error
);
12133 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12135 /* The new increment expression. */
12136 expression
= finish_unary_op_expr (input_location
,
12137 PREINCREMENT_EXPR
, begin
,
12138 tf_warning_or_error
);
12139 finish_for_expr (expression
, statement
);
12141 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12142 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12144 /* The declaration is initialized with *__begin inside the loop body. */
12145 cp_finish_decl (range_decl
,
12146 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12147 tf_warning_or_error
),
12148 /*is_constant_init*/false, NULL_TREE
,
12149 LOOKUP_ONLYCONVERTING
);
12150 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12151 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12156 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12157 We need to solve both at the same time because the method used
12158 depends on the existence of members begin or end.
12159 Returns the type deduced for the iterator expression. */
12162 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12164 if (error_operand_p (range
))
12166 *begin
= *end
= error_mark_node
;
12167 return error_mark_node
;
12170 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12172 error ("range-based %<for%> expression of type %qT "
12173 "has incomplete type", TREE_TYPE (range
));
12174 *begin
= *end
= error_mark_node
;
12175 return error_mark_node
;
12177 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12179 /* If RANGE is an array, we will use pointer arithmetic. */
12180 *begin
= decay_conversion (range
, tf_warning_or_error
);
12181 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12183 array_type_nelts_top (TREE_TYPE (range
)),
12185 return TREE_TYPE (*begin
);
12189 /* If it is not an array, we must do a bit of magic. */
12190 tree id_begin
, id_end
;
12191 tree member_begin
, member_end
;
12193 *begin
= *end
= error_mark_node
;
12195 id_begin
= get_identifier ("begin");
12196 id_end
= get_identifier ("end");
12197 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12198 /*protect=*/2, /*want_type=*/false,
12199 tf_warning_or_error
);
12200 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12201 /*protect=*/2, /*want_type=*/false,
12202 tf_warning_or_error
);
12204 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12206 /* Use the member functions. */
12207 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12208 *end
= cp_parser_range_for_member_function (range
, id_end
);
12212 /* Use global functions with ADL. */
12213 vec
<tree
, va_gc
> *vec
;
12214 vec
= make_tree_vector ();
12216 vec_safe_push (vec
, range
);
12218 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12219 tf_warning_or_error
);
12220 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12221 tf_warning_or_error
);
12222 member_end
= perform_koenig_lookup (id_end
, vec
,
12223 tf_warning_or_error
);
12224 *end
= finish_call_expr (member_end
, &vec
, false, true,
12225 tf_warning_or_error
);
12227 release_tree_vector (vec
);
12230 /* Last common checks. */
12231 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12233 /* If one of the expressions is an error do no more checks. */
12234 *begin
= *end
= error_mark_node
;
12235 return error_mark_node
;
12237 else if (type_dependent_expression_p (*begin
)
12238 || type_dependent_expression_p (*end
))
12239 /* Can happen, when, eg, in a template context, Koenig lookup
12240 can't resolve begin/end (c++/58503). */
12244 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12245 /* The unqualified type of the __begin and __end temporaries should
12246 be the same, as required by the multiple auto declaration. */
12247 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12249 if (cxx_dialect
>= cxx17
12250 && (build_x_binary_op (input_location
, NE_EXPR
,
12251 *begin
, ERROR_MARK
,
12254 != error_mark_node
))
12255 /* P0184R0 allows __begin and __end to have different types,
12256 but make sure they are comparable so we can give a better
12259 error ("inconsistent begin/end types in range-based %<for%> "
12260 "statement: %qT and %qT",
12261 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12268 /* Helper function for cp_parser_perform_range_for_lookup.
12269 Builds a tree for RANGE.IDENTIFIER(). */
12272 cp_parser_range_for_member_function (tree range
, tree identifier
)
12275 vec
<tree
, va_gc
> *vec
;
12277 member
= finish_class_member_access_expr (range
, identifier
,
12278 false, tf_warning_or_error
);
12279 if (member
== error_mark_node
)
12280 return error_mark_node
;
12282 vec
= make_tree_vector ();
12283 res
= finish_call_expr (member
, &vec
,
12284 /*disallow_virtual=*/false,
12285 /*koenig_p=*/false,
12286 tf_warning_or_error
);
12287 release_tree_vector (vec
);
12291 /* Parse an iteration-statement.
12293 iteration-statement:
12294 while ( condition ) statement
12295 do statement while ( expression ) ;
12296 for ( init-statement condition [opt] ; expression [opt] )
12299 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12302 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12303 unsigned short unroll
)
12308 unsigned char in_statement
;
12309 token_indent_info guard_tinfo
;
12311 /* Peek at the next token. */
12312 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12314 return error_mark_node
;
12316 guard_tinfo
= get_token_indent_info (token
);
12318 /* Remember whether or not we are already within an iteration
12320 in_statement
= parser
->in_statement
;
12322 /* See what kind of keyword it is. */
12323 keyword
= token
->keyword
;
12330 /* Begin the while-statement. */
12331 statement
= begin_while_stmt ();
12332 /* Look for the `('. */
12333 matching_parens parens
;
12334 parens
.require_open (parser
);
12335 /* Parse the condition. */
12336 condition
= cp_parser_condition (parser
);
12337 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12338 /* Look for the `)'. */
12339 parens
.require_close (parser
);
12340 /* Parse the dependent statement. */
12341 parser
->in_statement
= IN_ITERATION_STMT
;
12342 bool prev
= note_iteration_stmt_body_start ();
12343 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12344 note_iteration_stmt_body_end (prev
);
12345 parser
->in_statement
= in_statement
;
12346 /* We're done with the while-statement. */
12347 finish_while_stmt (statement
);
12355 /* Begin the do-statement. */
12356 statement
= begin_do_stmt ();
12357 /* Parse the body of the do-statement. */
12358 parser
->in_statement
= IN_ITERATION_STMT
;
12359 bool prev
= note_iteration_stmt_body_start ();
12360 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12361 note_iteration_stmt_body_end (prev
);
12362 parser
->in_statement
= in_statement
;
12363 finish_do_body (statement
);
12364 /* Look for the `while' keyword. */
12365 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12366 /* Look for the `('. */
12367 matching_parens parens
;
12368 parens
.require_open (parser
);
12369 /* Parse the expression. */
12370 expression
= cp_parser_expression (parser
);
12371 /* We're done with the do-statement. */
12372 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12373 /* Look for the `)'. */
12374 parens
.require_close (parser
);
12375 /* Look for the `;'. */
12376 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12382 /* Look for the `('. */
12383 matching_parens parens
;
12384 parens
.require_open (parser
);
12386 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12388 /* Look for the `)'. */
12389 parens
.require_close (parser
);
12391 /* Parse the body of the for-statement. */
12392 parser
->in_statement
= IN_ITERATION_STMT
;
12393 bool prev
= note_iteration_stmt_body_start ();
12394 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12395 note_iteration_stmt_body_end (prev
);
12396 parser
->in_statement
= in_statement
;
12398 /* We're done with the for-statement. */
12399 finish_for_stmt (statement
);
12404 cp_parser_error (parser
, "expected iteration-statement");
12405 statement
= error_mark_node
;
12412 /* Parse a init-statement or the declarator of a range-based-for.
12413 Returns true if a range-based-for declaration is seen.
12416 expression-statement
12417 simple-declaration */
12420 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12422 /* If the next token is a `;', then we have an empty
12423 expression-statement. Grammatically, this is also a
12424 simple-declaration, but an invalid one, because it does not
12425 declare anything. Therefore, if we did not handle this case
12426 specially, we would issue an error message about an invalid
12428 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12430 bool is_range_for
= false;
12431 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12433 /* Try to parse the init-statement. */
12434 if (cp_parser_range_based_for_with_init_p (parser
))
12437 cp_parser_parse_tentatively (parser
);
12438 /* Parse the declaration. */
12439 cp_parser_simple_declaration (parser
,
12440 /*function_definition_allowed_p=*/false,
12442 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12443 if (!cp_parser_parse_definitely (parser
))
12444 /* That didn't work, try to parse it as an expression-statement. */
12445 cp_parser_expression_statement (parser
, NULL_TREE
);
12447 if (cxx_dialect
< cxx2a
)
12449 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12450 "range-based %<for%> loops with initializer only "
12451 "available with -std=c++2a or -std=gnu++2a");
12452 *decl
= error_mark_node
;
12456 /* A colon is used in range-based for. */
12457 parser
->colon_corrects_to_scope_p
= false;
12459 /* We're going to speculatively look for a declaration, falling back
12460 to an expression, if necessary. */
12461 cp_parser_parse_tentatively (parser
);
12462 /* Parse the declaration. */
12463 cp_parser_simple_declaration (parser
,
12464 /*function_definition_allowed_p=*/false,
12466 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12469 /* It is a range-for, consume the ':'. */
12470 cp_lexer_consume_token (parser
->lexer
);
12471 is_range_for
= true;
12472 if (cxx_dialect
< cxx11
)
12473 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12474 "range-based %<for%> loops only available with "
12475 "-std=c++11 or -std=gnu++11");
12478 /* The ';' is not consumed yet because we told
12479 cp_parser_simple_declaration not to. */
12480 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12482 if (cp_parser_parse_definitely (parser
))
12483 return is_range_for
;
12484 /* If the tentative parse failed, then we shall need to look for an
12485 expression-statement. */
12487 /* If we are here, it is an expression-statement. */
12488 cp_parser_expression_statement (parser
, NULL_TREE
);
12492 /* Parse a jump-statement.
12497 return expression [opt] ;
12498 return braced-init-list ;
12504 goto * expression ;
12506 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12509 cp_parser_jump_statement (cp_parser
* parser
)
12511 tree statement
= error_mark_node
;
12514 unsigned char in_statement
;
12516 /* Peek at the next token. */
12517 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12519 return error_mark_node
;
12521 /* See what kind of keyword it is. */
12522 keyword
= token
->keyword
;
12526 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12527 switch (in_statement
)
12530 error_at (token
->location
, "break statement not within loop or switch");
12533 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12534 || in_statement
== IN_ITERATION_STMT
);
12535 statement
= finish_break_stmt ();
12536 if (in_statement
== IN_ITERATION_STMT
)
12537 break_maybe_infinite_loop ();
12540 error_at (token
->location
, "invalid exit from OpenMP structured block");
12543 error_at (token
->location
, "break statement used with OpenMP for loop");
12546 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12550 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12553 error_at (token
->location
, "continue statement not within a loop");
12555 /* Fall through. */
12556 case IN_ITERATION_STMT
:
12558 statement
= finish_continue_stmt ();
12561 error_at (token
->location
, "invalid exit from OpenMP structured block");
12564 gcc_unreachable ();
12566 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12572 bool expr_non_constant_p
;
12574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12576 cp_lexer_set_source_position (parser
->lexer
);
12577 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12578 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12580 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12581 expr
= cp_parser_expression (parser
);
12583 /* If the next token is a `;', then there is no
12586 /* Build the return-statement. */
12587 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12588 /* Don't deduce from a discarded return statement. */;
12590 statement
= finish_return_stmt (expr
);
12591 /* Look for the final `;'. */
12592 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12597 if (parser
->in_function_body
12598 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12600 error ("%<goto%> in %<constexpr%> function");
12601 cp_function_chain
->invalid_constexpr
= true;
12604 /* Create the goto-statement. */
12605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12607 /* Issue a warning about this use of a GNU extension. */
12608 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12609 /* Consume the '*' token. */
12610 cp_lexer_consume_token (parser
->lexer
);
12611 /* Parse the dependent expression. */
12612 finish_goto_stmt (cp_parser_expression (parser
));
12615 finish_goto_stmt (cp_parser_identifier (parser
));
12616 /* Look for the final `;'. */
12617 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12621 cp_parser_error (parser
, "expected jump-statement");
12628 /* Parse a declaration-statement.
12630 declaration-statement:
12631 block-declaration */
12634 cp_parser_declaration_statement (cp_parser
* parser
)
12638 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12639 p
= obstack_alloc (&declarator_obstack
, 0);
12641 /* Parse the block-declaration. */
12642 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12644 /* Free any declarators allocated. */
12645 obstack_free (&declarator_obstack
, p
);
12648 /* Some dependent statements (like `if (cond) statement'), are
12649 implicitly in their own scope. In other words, if the statement is
12650 a single statement (as opposed to a compound-statement), it is
12651 none-the-less treated as if it were enclosed in braces. Any
12652 declarations appearing in the dependent statement are out of scope
12653 after control passes that point. This function parses a statement,
12654 but ensures that is in its own scope, even if it is not a
12655 compound-statement.
12657 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12658 is a (possibly labeled) if statement which is not enclosed in
12659 braces and has an else clause. This is used to implement
12662 CHAIN is a vector of if-else-if conditions. This is used to implement
12665 Returns the new statement. */
12668 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12669 const token_indent_info
&guard_tinfo
,
12673 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12674 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12675 token_indent_info body_tinfo
12676 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12681 /* Mark if () ; with a special NOP_EXPR. */
12682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12684 cp_lexer_consume_token (parser
->lexer
);
12685 statement
= add_stmt (build_empty_stmt (body_loc
));
12687 if (guard_tinfo
.keyword
== RID_IF
12688 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12689 warning_at (body_loc
, OPT_Wempty_body
,
12690 "suggest braces around empty body in an %<if%> statement");
12691 else if (guard_tinfo
.keyword
== RID_ELSE
)
12692 warning_at (body_loc
, OPT_Wempty_body
,
12693 "suggest braces around empty body in an %<else%> statement");
12695 /* if a compound is opened, we simply parse the statement directly. */
12696 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12697 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12698 /* If the token is not a `{', then we must take special action. */
12701 /* Create a compound-statement. */
12702 statement
= begin_compound_stmt (0);
12703 /* Parse the dependent-statement. */
12704 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12705 &body_loc_after_labels
);
12706 /* Finish the dummy compound-statement. */
12707 finish_compound_stmt (statement
);
12710 token_indent_info next_tinfo
12711 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12712 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12714 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12715 && next_tinfo
.type
!= CPP_SEMICOLON
)
12716 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12717 guard_tinfo
.location
, guard_tinfo
.keyword
);
12719 /* Return the statement. */
12723 /* For some dependent statements (like `while (cond) statement'), we
12724 have already created a scope. Therefore, even if the dependent
12725 statement is a compound-statement, we do not want to create another
12729 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12730 const token_indent_info
&guard_tinfo
)
12732 /* If the token is a `{', then we must take special action. */
12733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12735 token_indent_info body_tinfo
12736 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12737 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12739 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12740 &loc_after_labels
);
12741 token_indent_info next_tinfo
12742 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12743 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12745 if (loc_after_labels
!= UNKNOWN_LOCATION
12746 && next_tinfo
.type
!= CPP_SEMICOLON
)
12747 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12748 guard_tinfo
.location
,
12749 guard_tinfo
.keyword
);
12753 /* Avoid calling cp_parser_compound_statement, so that we
12754 don't create a new scope. Do everything else by hand. */
12755 matching_braces braces
;
12756 braces
.require_open (parser
);
12757 /* If the next keyword is `__label__' we have a label declaration. */
12758 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12759 cp_parser_label_declaration (parser
);
12760 /* Parse an (optional) statement-seq. */
12761 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12762 braces
.require_close (parser
);
12766 /* Declarations [gram.dcl.dcl] */
12768 /* Parse an optional declaration-sequence.
12772 declaration-seq declaration */
12775 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12779 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12781 if (token
->type
== CPP_CLOSE_BRACE
12782 || token
->type
== CPP_EOF
)
12785 cp_parser_toplevel_declaration (parser
);
12789 /* Parse a declaration.
12793 function-definition
12794 template-declaration
12795 explicit-instantiation
12796 explicit-specialization
12797 linkage-specification
12798 namespace-definition
12806 __extension__ declaration */
12809 cp_parser_declaration (cp_parser
* parser
)
12813 int saved_pedantic
;
12815 tree attributes
= NULL_TREE
;
12817 /* Check for the `__extension__' keyword. */
12818 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12820 /* Parse the qualified declaration. */
12821 cp_parser_declaration (parser
);
12822 /* Restore the PEDANTIC flag. */
12823 pedantic
= saved_pedantic
;
12828 /* Try to figure out what kind of declaration is present. */
12829 token1
= *cp_lexer_peek_token (parser
->lexer
);
12831 if (token1
.type
!= CPP_EOF
)
12832 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12835 token2
.type
= CPP_EOF
;
12836 token2
.keyword
= RID_MAX
;
12839 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12840 p
= obstack_alloc (&declarator_obstack
, 0);
12842 /* If the next token is `extern' and the following token is a string
12843 literal, then we have a linkage specification. */
12844 if (token1
.keyword
== RID_EXTERN
12845 && cp_parser_is_pure_string_literal (&token2
))
12846 cp_parser_linkage_specification (parser
);
12847 /* If the next token is `template', then we have either a template
12848 declaration, an explicit instantiation, or an explicit
12850 else if (token1
.keyword
== RID_TEMPLATE
)
12852 /* `template <>' indicates a template specialization. */
12853 if (token2
.type
== CPP_LESS
12854 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12855 cp_parser_explicit_specialization (parser
);
12856 /* `template <' indicates a template declaration. */
12857 else if (token2
.type
== CPP_LESS
)
12858 cp_parser_template_declaration (parser
, /*member_p=*/false);
12859 /* Anything else must be an explicit instantiation. */
12861 cp_parser_explicit_instantiation (parser
);
12863 /* If the next token is `export', then we have a template
12865 else if (token1
.keyword
== RID_EXPORT
)
12866 cp_parser_template_declaration (parser
, /*member_p=*/false);
12867 /* If the next token is `extern', 'static' or 'inline' and the one
12868 after that is `template', we have a GNU extended explicit
12869 instantiation directive. */
12870 else if (cp_parser_allow_gnu_extensions_p (parser
)
12871 && (token1
.keyword
== RID_EXTERN
12872 || token1
.keyword
== RID_STATIC
12873 || token1
.keyword
== RID_INLINE
)
12874 && token2
.keyword
== RID_TEMPLATE
)
12875 cp_parser_explicit_instantiation (parser
);
12876 /* If the next token is `namespace', check for a named or unnamed
12877 namespace definition. */
12878 else if (token1
.keyword
== RID_NAMESPACE
12879 && (/* A named namespace definition. */
12880 (token2
.type
== CPP_NAME
12881 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12883 || (token2
.type
== CPP_OPEN_SQUARE
12884 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12885 == CPP_OPEN_SQUARE
)
12886 /* An unnamed namespace definition. */
12887 || token2
.type
== CPP_OPEN_BRACE
12888 || token2
.keyword
== RID_ATTRIBUTE
))
12889 cp_parser_namespace_definition (parser
);
12890 /* An inline (associated) namespace definition. */
12891 else if (token1
.keyword
== RID_INLINE
12892 && token2
.keyword
== RID_NAMESPACE
)
12893 cp_parser_namespace_definition (parser
);
12894 /* Objective-C++ declaration/definition. */
12895 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12896 cp_parser_objc_declaration (parser
, NULL_TREE
);
12897 else if (c_dialect_objc ()
12898 && token1
.keyword
== RID_ATTRIBUTE
12899 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12900 cp_parser_objc_declaration (parser
, attributes
);
12901 /* At this point we may have a template declared by a concept
12903 else if (flag_concepts
12904 && cp_parser_template_declaration_after_export (parser
,
12905 /*member_p=*/false))
12908 /* Try to parse a block-declaration, or a function-definition. */
12909 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12911 /* Free any declarators allocated. */
12912 obstack_free (&declarator_obstack
, p
);
12915 /* Parse a namespace-scope declaration. */
12918 cp_parser_toplevel_declaration (cp_parser
* parser
)
12920 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12922 if (token
->type
== CPP_PRAGMA
)
12923 /* A top-level declaration can consist solely of a #pragma. A
12924 nested declaration cannot, so this is done here and not in
12925 cp_parser_declaration. (A #pragma at block scope is
12926 handled in cp_parser_statement.) */
12927 cp_parser_pragma (parser
, pragma_external
, NULL
);
12928 else if (token
->type
== CPP_SEMICOLON
)
12930 /* A declaration consisting of a single semicolon is
12931 invalid. Allow it unless we're being pedantic. */
12932 cp_lexer_consume_token (parser
->lexer
);
12933 if (!in_system_header_at (input_location
))
12934 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12937 /* Parse the declaration itself. */
12938 cp_parser_declaration (parser
);
12941 /* Parse a block-declaration.
12946 namespace-alias-definition
12953 __extension__ block-declaration
12958 static_assert-declaration
12960 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12961 part of a declaration-statement. */
12964 cp_parser_block_declaration (cp_parser
*parser
,
12968 int saved_pedantic
;
12970 /* Check for the `__extension__' keyword. */
12971 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12973 /* Parse the qualified declaration. */
12974 cp_parser_block_declaration (parser
, statement_p
);
12975 /* Restore the PEDANTIC flag. */
12976 pedantic
= saved_pedantic
;
12981 /* Peek at the next token to figure out which kind of declaration is
12983 token1
= cp_lexer_peek_token (parser
->lexer
);
12985 /* If the next keyword is `asm', we have an asm-definition. */
12986 if (token1
->keyword
== RID_ASM
)
12989 cp_parser_commit_to_tentative_parse (parser
);
12990 cp_parser_asm_definition (parser
);
12992 /* If the next keyword is `namespace', we have a
12993 namespace-alias-definition. */
12994 else if (token1
->keyword
== RID_NAMESPACE
)
12995 cp_parser_namespace_alias_definition (parser
);
12996 /* If the next keyword is `using', we have a
12997 using-declaration, a using-directive, or an alias-declaration. */
12998 else if (token1
->keyword
== RID_USING
)
13003 cp_parser_commit_to_tentative_parse (parser
);
13004 /* If the token after `using' is `namespace', then we have a
13005 using-directive. */
13006 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13007 if (token2
->keyword
== RID_NAMESPACE
)
13008 cp_parser_using_directive (parser
);
13009 /* If the second token after 'using' is '=', then we have an
13010 alias-declaration. */
13011 else if (cxx_dialect
>= cxx11
13012 && token2
->type
== CPP_NAME
13013 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13014 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13015 cp_parser_alias_declaration (parser
);
13016 /* Otherwise, it's a using-declaration. */
13018 cp_parser_using_declaration (parser
,
13019 /*access_declaration_p=*/false);
13021 /* If the next keyword is `__label__' we have a misplaced label
13023 else if (token1
->keyword
== RID_LABEL
)
13025 cp_lexer_consume_token (parser
->lexer
);
13026 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13027 cp_parser_skip_to_end_of_statement (parser
);
13028 /* If the next token is now a `;', consume it. */
13029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13030 cp_lexer_consume_token (parser
->lexer
);
13032 /* If the next token is `static_assert' we have a static assertion. */
13033 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13034 cp_parser_static_assert (parser
, /*member_p=*/false);
13035 /* Anything else must be a simple-declaration. */
13037 cp_parser_simple_declaration (parser
, !statement_p
,
13038 /*maybe_range_for_decl*/NULL
);
13041 /* Parse a simple-declaration.
13043 simple-declaration:
13044 decl-specifier-seq [opt] init-declarator-list [opt] ;
13045 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13046 brace-or-equal-initializer ;
13048 init-declarator-list:
13050 init-declarator-list , init-declarator
13052 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13053 function-definition as a simple-declaration.
13055 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13056 parsed declaration if it is an uninitialized single declarator not followed
13057 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13058 if present, will not be consumed. */
13061 cp_parser_simple_declaration (cp_parser
* parser
,
13062 bool function_definition_allowed_p
,
13063 tree
*maybe_range_for_decl
)
13065 cp_decl_specifier_seq decl_specifiers
;
13066 int declares_class_or_enum
;
13067 bool saw_declarator
;
13068 location_t comma_loc
= UNKNOWN_LOCATION
;
13069 location_t init_loc
= UNKNOWN_LOCATION
;
13071 if (maybe_range_for_decl
)
13072 *maybe_range_for_decl
= NULL_TREE
;
13074 /* Defer access checks until we know what is being declared; the
13075 checks for names appearing in the decl-specifier-seq should be
13076 done as if we were in the scope of the thing being declared. */
13077 push_deferring_access_checks (dk_deferred
);
13079 /* Parse the decl-specifier-seq. We have to keep track of whether
13080 or not the decl-specifier-seq declares a named class or
13081 enumeration type, since that is the only case in which the
13082 init-declarator-list is allowed to be empty.
13086 In a simple-declaration, the optional init-declarator-list can be
13087 omitted only when declaring a class or enumeration, that is when
13088 the decl-specifier-seq contains either a class-specifier, an
13089 elaborated-type-specifier, or an enum-specifier. */
13090 cp_parser_decl_specifier_seq (parser
,
13091 CP_PARSER_FLAGS_OPTIONAL
,
13093 &declares_class_or_enum
);
13094 /* We no longer need to defer access checks. */
13095 stop_deferring_access_checks ();
13097 /* In a block scope, a valid declaration must always have a
13098 decl-specifier-seq. By not trying to parse declarators, we can
13099 resolve the declaration/expression ambiguity more quickly. */
13100 if (!function_definition_allowed_p
13101 && !decl_specifiers
.any_specifiers_p
)
13103 cp_parser_error (parser
, "expected declaration");
13107 /* If the next two tokens are both identifiers, the code is
13108 erroneous. The usual cause of this situation is code like:
13112 where "T" should name a type -- but does not. */
13113 if (!decl_specifiers
.any_type_specifiers_p
13114 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13116 /* If parsing tentatively, we should commit; we really are
13117 looking at a declaration. */
13118 cp_parser_commit_to_tentative_parse (parser
);
13123 cp_parser_maybe_commit_to_declaration (parser
,
13124 decl_specifiers
.any_specifiers_p
);
13126 /* Look for C++17 decomposition declaration. */
13127 for (size_t n
= 1; ; n
++)
13128 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13129 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13131 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13132 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13133 && decl_specifiers
.any_specifiers_p
)
13136 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13137 maybe_range_for_decl
,
13140 /* The next token should be either a `,' or a `;'. */
13141 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13142 /* If it's a `;', we are done. */
13143 if (token
->type
== CPP_SEMICOLON
)
13145 else if (maybe_range_for_decl
)
13147 if (*maybe_range_for_decl
== NULL_TREE
)
13148 *maybe_range_for_decl
= error_mark_node
;
13151 /* Anything else is an error. */
13154 /* If we have already issued an error message we don't need
13155 to issue another one. */
13156 if ((decl
!= error_mark_node
13157 && DECL_INITIAL (decl
) != error_mark_node
)
13158 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13159 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13160 /* Skip tokens until we reach the end of the statement. */
13161 cp_parser_skip_to_end_of_statement (parser
);
13162 /* If the next token is now a `;', consume it. */
13163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13164 cp_lexer_consume_token (parser
->lexer
);
13172 bool auto_specifier_p
;
13173 /* NULL_TREE if both variable and function declaration are allowed,
13174 error_mark_node if function declaration are not allowed and
13175 a FUNCTION_DECL that should be diagnosed if it is followed by
13176 variable declarations. */
13177 tree auto_function_declaration
;
13179 last_type
= NULL_TREE
;
13181 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13182 auto_function_declaration
= NULL_TREE
;
13184 /* Keep going until we hit the `;' at the end of the simple
13186 saw_declarator
= false;
13187 while (cp_lexer_next_token_is_not (parser
->lexer
,
13191 bool function_definition_p
;
13193 tree auto_result
= NULL_TREE
;
13195 if (saw_declarator
)
13197 /* If we are processing next declarator, comma is expected */
13198 token
= cp_lexer_peek_token (parser
->lexer
);
13199 gcc_assert (token
->type
== CPP_COMMA
);
13200 cp_lexer_consume_token (parser
->lexer
);
13201 if (maybe_range_for_decl
)
13203 *maybe_range_for_decl
= error_mark_node
;
13204 if (comma_loc
== UNKNOWN_LOCATION
)
13205 comma_loc
= token
->location
;
13209 saw_declarator
= true;
13211 /* Parse the init-declarator. */
13212 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13214 function_definition_allowed_p
,
13215 /*member_p=*/false,
13216 declares_class_or_enum
,
13217 &function_definition_p
,
13218 maybe_range_for_decl
,
13221 /* If an error occurred while parsing tentatively, exit quickly.
13222 (That usually happens when in the body of a function; each
13223 statement is treated as a declaration-statement until proven
13225 if (cp_parser_error_occurred (parser
))
13228 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13230 /* If the init-declarator-list contains more than one
13231 init-declarator, they shall all form declarations of
13233 if (auto_function_declaration
== NULL_TREE
)
13234 auto_function_declaration
13235 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13236 else if (TREE_CODE (decl
) == FUNCTION_DECL
13237 || auto_function_declaration
!= error_mark_node
)
13239 error_at (decl_specifiers
.locations
[ds_type_spec
],
13240 "non-variable %qD in declaration with more than one "
13241 "declarator with placeholder type",
13242 TREE_CODE (decl
) == FUNCTION_DECL
13243 ? decl
: auto_function_declaration
);
13244 auto_function_declaration
= error_mark_node
;
13249 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13252 && last_type
!= error_mark_node
13253 && !same_type_p (auto_result
, last_type
))
13255 /* If the list of declarators contains more than one declarator,
13256 the type of each declared variable is determined as described
13257 above. If the type deduced for the template parameter U is not
13258 the same in each deduction, the program is ill-formed. */
13259 error_at (decl_specifiers
.locations
[ds_type_spec
],
13260 "inconsistent deduction for %qT: %qT and then %qT",
13261 decl_specifiers
.type
, last_type
, auto_result
);
13262 last_type
= error_mark_node
;
13265 last_type
= auto_result
;
13268 /* Handle function definitions specially. */
13269 if (function_definition_p
)
13271 /* If the next token is a `,', then we are probably
13272 processing something like:
13276 which is erroneous. */
13277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13279 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13280 error_at (token
->location
,
13282 " declarations and function-definitions is forbidden");
13284 /* Otherwise, we're done with the list of declarators. */
13287 pop_deferring_access_checks ();
13291 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13292 *maybe_range_for_decl
= decl
;
13293 /* The next token should be either a `,' or a `;'. */
13294 token
= cp_lexer_peek_token (parser
->lexer
);
13295 /* If it's a `,', there are more declarators to come. */
13296 if (token
->type
== CPP_COMMA
)
13297 /* will be consumed next time around */;
13298 /* If it's a `;', we are done. */
13299 else if (token
->type
== CPP_SEMICOLON
)
13301 else if (maybe_range_for_decl
)
13303 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13304 permerror (decl_specifiers
.locations
[ds_type_spec
],
13305 "types may not be defined in a for-range-declaration");
13308 /* Anything else is an error. */
13311 /* If we have already issued an error message we don't need
13312 to issue another one. */
13313 if ((decl
!= error_mark_node
13314 && DECL_INITIAL (decl
) != error_mark_node
)
13315 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13316 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13317 /* Skip tokens until we reach the end of the statement. */
13318 cp_parser_skip_to_end_of_statement (parser
);
13319 /* If the next token is now a `;', consume it. */
13320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13321 cp_lexer_consume_token (parser
->lexer
);
13324 /* After the first time around, a function-definition is not
13325 allowed -- even if it was OK at first. For example:
13330 function_definition_allowed_p
= false;
13333 /* Issue an error message if no declarators are present, and the
13334 decl-specifier-seq does not itself declare a class or
13335 enumeration: [dcl.dcl]/3. */
13336 if (!saw_declarator
)
13338 if (cp_parser_declares_only_class_p (parser
))
13340 if (!declares_class_or_enum
13341 && decl_specifiers
.type
13342 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13343 /* Ensure an error is issued anyway when finish_decltype_type,
13344 called via cp_parser_decl_specifier_seq, returns a class or
13345 an enumeration (c++/51786). */
13346 decl_specifiers
.type
= NULL_TREE
;
13347 shadow_tag (&decl_specifiers
);
13349 /* Perform any deferred access checks. */
13350 perform_deferred_access_checks (tf_warning_or_error
);
13353 /* Consume the `;'. */
13355 if (!maybe_range_for_decl
)
13356 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13357 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13359 if (init_loc
!= UNKNOWN_LOCATION
)
13360 error_at (init_loc
, "initializer in range-based %<for%> loop");
13361 if (comma_loc
!= UNKNOWN_LOCATION
)
13362 error_at (comma_loc
,
13363 "multiple declarations in range-based %<for%> loop");
13367 pop_deferring_access_checks ();
13370 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13371 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13375 cp_parser_decomposition_declaration (cp_parser
*parser
,
13376 cp_decl_specifier_seq
*decl_specifiers
,
13377 tree
*maybe_range_for_decl
,
13378 location_t
*init_loc
)
13380 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13381 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13382 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13384 /* Parse the identifier-list. */
13385 auto_vec
<cp_expr
, 10> v
;
13386 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13389 cp_expr e
= cp_parser_identifier (parser
);
13390 if (e
.get_value () == error_mark_node
)
13393 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13395 cp_lexer_consume_token (parser
->lexer
);
13398 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13399 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13401 end_loc
= UNKNOWN_LOCATION
;
13402 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13405 cp_lexer_consume_token (parser
->lexer
);
13408 cp_parser_skip_to_end_of_statement (parser
);
13409 return error_mark_node
;
13413 if (cxx_dialect
< cxx17
)
13414 pedwarn (loc
, 0, "structured bindings only available with "
13415 "-std=c++17 or -std=gnu++17");
13418 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13419 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13420 declarator
->id_loc
= loc
;
13421 if (ref_qual
!= REF_QUAL_NONE
)
13422 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13423 ref_qual
== REF_QUAL_RVALUE
,
13425 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13426 NULL_TREE
, decl_specifiers
->attributes
,
13428 tree orig_decl
= decl
;
13432 cp_decl_specifier_seq decl_specs
;
13433 clear_decl_specs (&decl_specs
);
13434 decl_specs
.type
= make_auto ();
13436 FOR_EACH_VEC_ELT (v
, i
, e
)
13439 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13441 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13442 declarator
->id_loc
= e
.get_location ();
13443 tree elt_pushed_scope
;
13444 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13445 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13446 if (decl2
== error_mark_node
)
13447 decl
= error_mark_node
;
13448 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13450 /* Ensure we've diagnosed redeclaration if we aren't creating
13452 gcc_assert (errorcount
);
13453 decl
= error_mark_node
;
13457 if (elt_pushed_scope
)
13458 pop_scope (elt_pushed_scope
);
13463 error_at (loc
, "empty structured binding declaration");
13464 decl
= error_mark_node
;
13467 if (maybe_range_for_decl
== NULL
13468 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13470 bool non_constant_p
= false, is_direct_init
= false;
13471 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13472 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13474 if (initializer
== NULL_TREE
13475 || (TREE_CODE (initializer
) == TREE_LIST
13476 && TREE_CHAIN (initializer
))
13478 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13479 && CONSTRUCTOR_NELTS (initializer
) != 1))
13481 error_at (loc
, "invalid initializer for structured binding "
13483 initializer
= error_mark_node
;
13486 if (decl
!= error_mark_node
)
13488 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13489 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13490 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13491 cp_finish_decomp (decl
, prev
, v
.length ());
13494 else if (decl
!= error_mark_node
)
13496 *maybe_range_for_decl
= prev
;
13497 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13498 the underlying DECL. */
13499 cp_finish_decomp (decl
, prev
, v
.length ());
13503 pop_scope (pushed_scope
);
13505 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13507 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13508 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13514 /* Parse a decl-specifier-seq.
13516 decl-specifier-seq:
13517 decl-specifier-seq [opt] decl-specifier
13518 decl-specifier attribute-specifier-seq [opt] (C++11)
13521 storage-class-specifier
13532 Concepts Extension:
13537 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13539 The parser flags FLAGS is used to control type-specifier parsing.
13541 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13544 1: one of the decl-specifiers is an elaborated-type-specifier
13545 (i.e., a type declaration)
13546 2: one of the decl-specifiers is an enum-specifier or a
13547 class-specifier (i.e., a type definition)
13552 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13553 cp_parser_flags flags
,
13554 cp_decl_specifier_seq
*decl_specs
,
13555 int* declares_class_or_enum
)
13557 bool constructor_possible_p
= !parser
->in_declarator_p
;
13558 bool found_decl_spec
= false;
13559 cp_token
*start_token
= NULL
;
13562 /* Clear DECL_SPECS. */
13563 clear_decl_specs (decl_specs
);
13565 /* Assume no class or enumeration type is declared. */
13566 *declares_class_or_enum
= 0;
13568 /* Keep reading specifiers until there are no more to read. */
13571 bool constructor_p
;
13575 /* Peek at the next token. */
13576 token
= cp_lexer_peek_token (parser
->lexer
);
13578 /* Save the first token of the decl spec list for error
13581 start_token
= token
;
13582 /* Handle attributes. */
13583 if (cp_next_tokens_can_be_attribute_p (parser
))
13585 /* Parse the attributes. */
13586 tree attrs
= cp_parser_attributes_opt (parser
);
13588 /* In a sequence of declaration specifiers, c++11 attributes
13589 appertain to the type that precede them. In that case
13592 The attribute-specifier-seq affects the type only for
13593 the declaration it appears in, not other declarations
13594 involving the same type.
13596 But for now let's force the user to position the
13597 attribute either at the beginning of the declaration or
13598 after the declarator-id, which would clearly mean that it
13599 applies to the declarator. */
13600 if (cxx11_attribute_p (attrs
))
13602 if (!found_decl_spec
)
13603 /* The c++11 attribute is at the beginning of the
13604 declaration. It appertains to the entity being
13608 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13610 /* This is an attribute following a
13611 class-specifier. */
13612 if (decl_specs
->type_definition_p
)
13613 warn_misplaced_attr_for_class_type (token
->location
,
13619 decl_specs
->std_attributes
13620 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13621 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13622 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13628 decl_specs
->attributes
13629 = attr_chainon (decl_specs
->attributes
, attrs
);
13630 if (decl_specs
->locations
[ds_attribute
] == 0)
13631 decl_specs
->locations
[ds_attribute
] = token
->location
;
13634 /* Assume we will find a decl-specifier keyword. */
13635 found_decl_spec
= true;
13636 /* If the next token is an appropriate keyword, we can simply
13637 add it to the list. */
13638 switch (token
->keyword
)
13644 if (!at_class_scope_p ())
13646 gcc_rich_location
richloc (token
->location
);
13647 richloc
.add_fixit_remove ();
13648 error_at (&richloc
, "%<friend%> used outside of class");
13649 cp_lexer_purge_token (parser
->lexer
);
13654 /* Consume the token. */
13655 cp_lexer_consume_token (parser
->lexer
);
13659 case RID_CONSTEXPR
:
13661 cp_lexer_consume_token (parser
->lexer
);
13666 cp_lexer_consume_token (parser
->lexer
);
13669 /* function-specifier:
13676 cp_parser_function_specifier_opt (parser
, decl_specs
);
13683 /* Consume the token. */
13684 cp_lexer_consume_token (parser
->lexer
);
13685 /* A constructor declarator cannot appear in a typedef. */
13686 constructor_possible_p
= false;
13687 /* The "typedef" keyword can only occur in a declaration; we
13688 may as well commit at this point. */
13689 cp_parser_commit_to_tentative_parse (parser
);
13691 if (decl_specs
->storage_class
!= sc_none
)
13692 decl_specs
->conflicting_specifiers_p
= true;
13695 /* storage-class-specifier:
13705 if (cxx_dialect
== cxx98
)
13707 /* Consume the token. */
13708 cp_lexer_consume_token (parser
->lexer
);
13710 /* Complain about `auto' as a storage specifier, if
13711 we're complaining about C++0x compatibility. */
13712 gcc_rich_location
richloc (token
->location
);
13713 richloc
.add_fixit_remove ();
13714 warning_at (&richloc
, OPT_Wc__11_compat
,
13715 "%<auto%> changes meaning in C++11; "
13716 "please remove it");
13718 /* Set the storage class anyway. */
13719 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13723 /* C++0x auto type-specifier. */
13724 found_decl_spec
= false;
13731 /* Consume the token. */
13732 cp_lexer_consume_token (parser
->lexer
);
13733 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13737 /* Consume the token. */
13739 cp_lexer_consume_token (parser
->lexer
);
13743 /* We did not yet find a decl-specifier yet. */
13744 found_decl_spec
= false;
13748 if (found_decl_spec
13749 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13750 && token
->keyword
!= RID_CONSTEXPR
)
13751 error ("decl-specifier invalid in condition");
13753 if (found_decl_spec
13754 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13755 && token
->keyword
!= RID_MUTABLE
13756 && token
->keyword
!= RID_CONSTEXPR
)
13757 error_at (token
->location
, "%qD invalid in lambda",
13758 ridpointers
[token
->keyword
]);
13761 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13763 /* Constructors are a special case. The `S' in `S()' is not a
13764 decl-specifier; it is the beginning of the declarator. */
13766 = (!found_decl_spec
13767 && constructor_possible_p
13768 && (cp_parser_constructor_declarator_p
13769 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13771 /* If we don't have a DECL_SPEC yet, then we must be looking at
13772 a type-specifier. */
13773 if (!found_decl_spec
&& !constructor_p
)
13775 int decl_spec_declares_class_or_enum
;
13776 bool is_cv_qualifier
;
13780 = cp_parser_type_specifier (parser
, flags
,
13782 /*is_declaration=*/true,
13783 &decl_spec_declares_class_or_enum
,
13785 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13787 /* If this type-specifier referenced a user-defined type
13788 (a typedef, class-name, etc.), then we can't allow any
13789 more such type-specifiers henceforth.
13793 The longest sequence of decl-specifiers that could
13794 possibly be a type name is taken as the
13795 decl-specifier-seq of a declaration. The sequence shall
13796 be self-consistent as described below.
13800 As a general rule, at most one type-specifier is allowed
13801 in the complete decl-specifier-seq of a declaration. The
13802 only exceptions are the following:
13804 -- const or volatile can be combined with any other
13807 -- signed or unsigned can be combined with char, long,
13815 void g (const int Pc);
13817 Here, Pc is *not* part of the decl-specifier seq; it's
13818 the declarator. Therefore, once we see a type-specifier
13819 (other than a cv-qualifier), we forbid any additional
13820 user-defined types. We *do* still allow things like `int
13821 int' to be considered a decl-specifier-seq, and issue the
13822 error message later. */
13823 if (type_spec
&& !is_cv_qualifier
)
13824 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13825 /* A constructor declarator cannot follow a type-specifier. */
13828 constructor_possible_p
= false;
13829 found_decl_spec
= true;
13830 if (!is_cv_qualifier
)
13831 decl_specs
->any_type_specifiers_p
= true;
13833 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
13834 error_at (token
->location
, "type-specifier invalid in lambda");
13838 /* If we still do not have a DECL_SPEC, then there are no more
13839 decl-specifiers. */
13840 if (!found_decl_spec
)
13843 decl_specs
->any_specifiers_p
= true;
13844 /* After we see one decl-specifier, further decl-specifiers are
13845 always optional. */
13846 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13849 /* Don't allow a friend specifier with a class definition. */
13850 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13851 && (*declares_class_or_enum
& 2))
13852 error_at (decl_specs
->locations
[ds_friend
],
13853 "class definition may not be declared a friend");
13856 /* Parse an (optional) storage-class-specifier.
13858 storage-class-specifier:
13867 storage-class-specifier:
13870 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13873 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13875 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13878 if (cxx_dialect
!= cxx98
)
13880 /* Fall through for C++98. */
13881 gcc_fallthrough ();
13888 /* Consume the token. */
13889 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13896 /* Parse an (optional) function-specifier.
13898 function-specifier:
13904 explicit(constant-expression)
13906 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13907 Updates DECL_SPECS, if it is non-NULL. */
13910 cp_parser_function_specifier_opt (cp_parser
* parser
,
13911 cp_decl_specifier_seq
*decl_specs
)
13913 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13914 switch (token
->keyword
)
13917 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13921 /* 14.5.2.3 [temp.mem]
13923 A member function template shall not be virtual. */
13924 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13925 && current_class_type
)
13926 error_at (token
->location
, "templates may not be %<virtual%>");
13928 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13933 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13934 /* If we see '(', it's C++20 explicit(bool). */
13936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13938 matching_parens parens
;
13939 parens
.consume_open (parser
);
13941 /* New types are not allowed in an explicit-specifier. */
13942 const char *saved_message
13943 = parser
->type_definition_forbidden_message
;
13944 parser
->type_definition_forbidden_message
13945 = G_("types may not be defined in explicit-specifier");
13947 if (cxx_dialect
< cxx2a
)
13948 pedwarn (token
->location
, 0,
13949 "%<explicit(bool)%> only available with -std=c++2a "
13950 "or -std=gnu++2a");
13952 /* Parse the constant-expression. */
13953 expr
= cp_parser_constant_expression (parser
);
13955 /* Restore the saved message. */
13956 parser
->type_definition_forbidden_message
= saved_message
;
13957 parens
.require_close (parser
);
13960 /* The explicit-specifier explicit without a constant-expression is
13961 equivalent to the explicit-specifier explicit(true). */
13962 expr
= boolean_true_node
;
13965 "the constant-expression, if supplied, shall be a contextually
13966 converted constant expression of type bool." */
13967 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
13968 /* We could evaluate it -- mark the decl as appropriate. */
13969 if (expr
== boolean_true_node
)
13970 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13971 else if (expr
== boolean_false_node
)
13972 /* Don't mark the decl as explicit. */;
13973 else if (decl_specs
)
13974 /* The expression was value-dependent. Remember it so that we can
13975 substitute it later. */
13976 decl_specs
->explicit_specifier
= expr
;
13984 /* Consume the token. */
13985 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13988 /* Parse a linkage-specification.
13990 linkage-specification:
13991 extern string-literal { declaration-seq [opt] }
13992 extern string-literal declaration */
13995 cp_parser_linkage_specification (cp_parser
* parser
)
13999 /* Look for the `extern' keyword. */
14000 cp_token
*extern_token
14001 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14003 /* Look for the string-literal. */
14004 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14005 linkage
= cp_parser_string_literal (parser
, false, false);
14007 /* Transform the literal into an identifier. If the literal is a
14008 wide-character string, or contains embedded NULs, then we can't
14009 handle it as the user wants. */
14010 if (strlen (TREE_STRING_POINTER (linkage
))
14011 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14013 cp_parser_error (parser
, "invalid linkage-specification");
14014 /* Assume C++ linkage. */
14015 linkage
= lang_name_cplusplus
;
14018 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14020 /* We're now using the new linkage. */
14021 push_lang_context (linkage
);
14023 /* Preserve the location of the the innermost linkage specification,
14024 tracking the locations of nested specifications via a local. */
14025 location_t saved_location
14026 = parser
->innermost_linkage_specification_location
;
14027 /* Construct a location ranging from the start of the "extern" to
14028 the end of the string-literal, with the caret at the start, e.g.:
14032 parser
->innermost_linkage_specification_location
14033 = make_location (extern_token
->location
,
14034 extern_token
->location
,
14035 get_finish (string_token
->location
));
14037 /* If the next token is a `{', then we're using the first
14039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14041 cp_ensure_no_omp_declare_simd (parser
);
14042 cp_ensure_no_oacc_routine (parser
);
14044 /* Consume the `{' token. */
14045 matching_braces braces
;
14046 braces
.consume_open (parser
)->location
;
14047 /* Parse the declarations. */
14048 cp_parser_declaration_seq_opt (parser
);
14049 /* Look for the closing `}'. */
14050 braces
.require_close (parser
);
14052 /* Otherwise, there's just one declaration. */
14055 bool saved_in_unbraced_linkage_specification_p
;
14057 saved_in_unbraced_linkage_specification_p
14058 = parser
->in_unbraced_linkage_specification_p
;
14059 parser
->in_unbraced_linkage_specification_p
= true;
14060 cp_parser_declaration (parser
);
14061 parser
->in_unbraced_linkage_specification_p
14062 = saved_in_unbraced_linkage_specification_p
;
14065 /* We're done with the linkage-specification. */
14066 pop_lang_context ();
14068 /* Restore location of parent linkage specification, if any. */
14069 parser
->innermost_linkage_specification_location
= saved_location
;
14072 /* Parse a static_assert-declaration.
14074 static_assert-declaration:
14075 static_assert ( constant-expression , string-literal ) ;
14076 static_assert ( constant-expression ) ; (C++17)
14078 If MEMBER_P, this static_assert is a class member. */
14081 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14084 location_t token_loc
;
14088 /* Peek at the `static_assert' token so we can keep track of exactly
14089 where the static assertion started. */
14090 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14092 /* Look for the `static_assert' keyword. */
14093 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14097 /* We know we are in a static assertion; commit to any tentative
14099 if (cp_parser_parsing_tentatively (parser
))
14100 cp_parser_commit_to_tentative_parse (parser
);
14102 /* Parse the `(' starting the static assertion condition. */
14103 matching_parens parens
;
14104 parens
.require_open (parser
);
14106 /* Parse the constant-expression. Allow a non-constant expression
14107 here in order to give better diagnostics in finish_static_assert. */
14109 cp_parser_constant_expression (parser
,
14110 /*allow_non_constant_p=*/true,
14111 /*non_constant_p=*/&dummy
);
14113 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14115 if (cxx_dialect
< cxx17
)
14116 pedwarn (input_location
, OPT_Wpedantic
,
14117 "static_assert without a message "
14118 "only available with -std=c++17 or -std=gnu++17");
14120 cp_lexer_consume_token (parser
->lexer
);
14121 message
= build_string (1, "");
14122 TREE_TYPE (message
) = char_array_type_node
;
14123 fix_string_type (message
);
14127 /* Parse the separating `,'. */
14128 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14130 /* Parse the string-literal message. */
14131 message
= cp_parser_string_literal (parser
,
14132 /*translate=*/false,
14135 /* A `)' completes the static assertion. */
14136 if (!parens
.require_close (parser
))
14137 cp_parser_skip_to_closing_parenthesis (parser
,
14138 /*recovering=*/true,
14139 /*or_comma=*/false,
14140 /*consume_paren=*/true);
14143 /* A semicolon terminates the declaration. */
14144 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14146 /* Get the location for the static assertion. Use that of the
14147 condition if available, otherwise, use that of the "static_assert"
14149 location_t assert_loc
= condition
.get_location ();
14150 if (assert_loc
== UNKNOWN_LOCATION
)
14151 assert_loc
= token_loc
;
14153 /* Complete the static assertion, which may mean either processing
14154 the static assert now or saving it for template instantiation. */
14155 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14158 /* Parse the expression in decltype ( expression ). */
14161 cp_parser_decltype_expr (cp_parser
*parser
,
14162 bool &id_expression_or_member_access_p
)
14164 cp_token
*id_expr_start_token
;
14167 /* Since we're going to preserve any side-effects from this parse, set up a
14168 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14169 in the expression. */
14170 tentative_firewall
firewall (parser
);
14172 /* First, try parsing an id-expression. */
14173 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14174 cp_parser_parse_tentatively (parser
);
14175 expr
= cp_parser_id_expression (parser
,
14176 /*template_keyword_p=*/false,
14177 /*check_dependency_p=*/true,
14178 /*template_p=*/NULL
,
14179 /*declarator_p=*/false,
14180 /*optional_p=*/false);
14182 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14184 bool non_integral_constant_expression_p
= false;
14185 tree id_expression
= expr
;
14187 const char *error_msg
;
14189 if (identifier_p (expr
))
14190 /* Lookup the name we got back from the id-expression. */
14191 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14192 id_expr_start_token
->location
);
14194 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14195 /* A template without args is not a complete id-expression. */
14196 expr
= error_mark_node
;
14199 && expr
!= error_mark_node
14200 && TREE_CODE (expr
) != TYPE_DECL
14201 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14202 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14203 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14205 /* Complete lookup of the id-expression. */
14206 expr
= (finish_id_expression
14207 (id_expression
, expr
, parser
->scope
, &idk
,
14208 /*integral_constant_expression_p=*/false,
14209 /*allow_non_integral_constant_expression_p=*/true,
14210 &non_integral_constant_expression_p
,
14211 /*template_p=*/false,
14213 /*address_p=*/false,
14214 /*template_arg_p=*/false,
14216 id_expr_start_token
->location
));
14218 if (expr
== error_mark_node
)
14219 /* We found an id-expression, but it was something that we
14220 should not have found. This is an error, not something
14221 we can recover from, so note that we found an
14222 id-expression and we'll recover as gracefully as
14224 id_expression_or_member_access_p
= true;
14228 && expr
!= error_mark_node
14229 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14230 /* We have an id-expression. */
14231 id_expression_or_member_access_p
= true;
14234 if (!id_expression_or_member_access_p
)
14236 /* Abort the id-expression parse. */
14237 cp_parser_abort_tentative_parse (parser
);
14239 /* Parsing tentatively, again. */
14240 cp_parser_parse_tentatively (parser
);
14242 /* Parse a class member access. */
14243 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14244 /*cast_p=*/false, /*decltype*/true,
14245 /*member_access_only_p=*/true, NULL
);
14248 && expr
!= error_mark_node
14249 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14250 /* We have an id-expression. */
14251 id_expression_or_member_access_p
= true;
14254 if (id_expression_or_member_access_p
)
14255 /* We have parsed the complete id-expression or member access. */
14256 cp_parser_parse_definitely (parser
);
14259 /* Abort our attempt to parse an id-expression or member access
14261 cp_parser_abort_tentative_parse (parser
);
14263 /* Commit to the tentative_firewall so we get syntax errors. */
14264 cp_parser_commit_to_tentative_parse (parser
);
14266 /* Parse a full expression. */
14267 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14268 /*decltype_p=*/true);
14274 /* Parse a `decltype' type. Returns the type.
14276 simple-type-specifier:
14277 decltype ( expression )
14279 decltype ( auto ) */
14282 cp_parser_decltype (cp_parser
*parser
)
14284 bool id_expression_or_member_access_p
= false;
14285 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14287 if (start_token
->type
== CPP_DECLTYPE
)
14289 /* Already parsed. */
14290 cp_lexer_consume_token (parser
->lexer
);
14291 return saved_checks_value (start_token
->u
.tree_check_value
);
14294 /* Look for the `decltype' token. */
14295 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14296 return error_mark_node
;
14298 /* Parse the opening `('. */
14299 matching_parens parens
;
14300 if (!parens
.require_open (parser
))
14301 return error_mark_node
;
14303 push_deferring_access_checks (dk_deferred
);
14305 tree expr
= NULL_TREE
;
14307 if (cxx_dialect
>= cxx14
14308 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14309 /* decltype (auto) */
14310 cp_lexer_consume_token (parser
->lexer
);
14313 /* decltype (expression) */
14315 /* Types cannot be defined in a `decltype' expression. Save away the
14316 old message and set the new one. */
14317 const char *saved_message
= parser
->type_definition_forbidden_message
;
14318 parser
->type_definition_forbidden_message
14319 = G_("types may not be defined in %<decltype%> expressions");
14321 /* The restrictions on constant-expressions do not apply inside
14322 decltype expressions. */
14323 bool saved_integral_constant_expression_p
14324 = parser
->integral_constant_expression_p
;
14325 bool saved_non_integral_constant_expression_p
14326 = parser
->non_integral_constant_expression_p
;
14327 parser
->integral_constant_expression_p
= false;
14329 /* Within a parenthesized expression, a `>' token is always
14330 the greater-than operator. */
14331 bool saved_greater_than_is_operator_p
14332 = parser
->greater_than_is_operator_p
;
14333 parser
->greater_than_is_operator_p
= true;
14335 /* Do not actually evaluate the expression. */
14336 ++cp_unevaluated_operand
;
14338 /* Do not warn about problems with the expression. */
14339 ++c_inhibit_evaluation_warnings
;
14341 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14343 /* Go back to evaluating expressions. */
14344 --cp_unevaluated_operand
;
14345 --c_inhibit_evaluation_warnings
;
14347 /* The `>' token might be the end of a template-id or
14348 template-parameter-list now. */
14349 parser
->greater_than_is_operator_p
14350 = saved_greater_than_is_operator_p
;
14352 /* Restore the old message and the integral constant expression
14354 parser
->type_definition_forbidden_message
= saved_message
;
14355 parser
->integral_constant_expression_p
14356 = saved_integral_constant_expression_p
;
14357 parser
->non_integral_constant_expression_p
14358 = saved_non_integral_constant_expression_p
;
14361 /* Parse to the closing `)'. */
14362 if (!parens
.require_close (parser
))
14364 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14365 /*consume_paren=*/true);
14366 pop_deferring_access_checks ();
14367 return error_mark_node
;
14373 expr
= make_decltype_auto ();
14374 AUTO_IS_DECLTYPE (expr
) = true;
14377 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14378 tf_warning_or_error
);
14380 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14382 start_token
->type
= CPP_DECLTYPE
;
14383 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14384 start_token
->u
.tree_check_value
->value
= expr
;
14385 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14386 start_token
->keyword
= RID_MAX
;
14387 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14389 pop_to_parent_deferring_access_checks ();
14394 /* Special member functions [gram.special] */
14396 /* Parse a conversion-function-id.
14398 conversion-function-id:
14399 operator conversion-type-id
14401 Returns an IDENTIFIER_NODE representing the operator. */
14404 cp_parser_conversion_function_id (cp_parser
* parser
)
14408 tree saved_qualifying_scope
;
14409 tree saved_object_scope
;
14410 tree pushed_scope
= NULL_TREE
;
14412 /* Look for the `operator' token. */
14413 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14414 return error_mark_node
;
14415 /* When we parse the conversion-type-id, the current scope will be
14416 reset. However, we need that information in able to look up the
14417 conversion function later, so we save it here. */
14418 saved_scope
= parser
->scope
;
14419 saved_qualifying_scope
= parser
->qualifying_scope
;
14420 saved_object_scope
= parser
->object_scope
;
14421 /* We must enter the scope of the class so that the names of
14422 entities declared within the class are available in the
14423 conversion-type-id. For example, consider:
14430 S::operator I() { ... }
14432 In order to see that `I' is a type-name in the definition, we
14433 must be in the scope of `S'. */
14435 pushed_scope
= push_scope (saved_scope
);
14436 /* Parse the conversion-type-id. */
14437 type
= cp_parser_conversion_type_id (parser
);
14438 /* Leave the scope of the class, if any. */
14440 pop_scope (pushed_scope
);
14441 /* Restore the saved scope. */
14442 parser
->scope
= saved_scope
;
14443 parser
->qualifying_scope
= saved_qualifying_scope
;
14444 parser
->object_scope
= saved_object_scope
;
14445 /* If the TYPE is invalid, indicate failure. */
14446 if (type
== error_mark_node
)
14447 return error_mark_node
;
14448 return make_conv_op_name (type
);
14451 /* Parse a conversion-type-id:
14453 conversion-type-id:
14454 type-specifier-seq conversion-declarator [opt]
14456 Returns the TYPE specified. */
14459 cp_parser_conversion_type_id (cp_parser
* parser
)
14462 cp_decl_specifier_seq type_specifiers
;
14463 cp_declarator
*declarator
;
14464 tree type_specified
;
14465 const char *saved_message
;
14467 /* Parse the attributes. */
14468 attributes
= cp_parser_attributes_opt (parser
);
14470 saved_message
= parser
->type_definition_forbidden_message
;
14471 parser
->type_definition_forbidden_message
14472 = G_("types may not be defined in a conversion-type-id");
14474 /* Parse the type-specifiers. */
14475 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14476 /*is_trailing_return=*/false,
14479 parser
->type_definition_forbidden_message
= saved_message
;
14481 /* If that didn't work, stop. */
14482 if (type_specifiers
.type
== error_mark_node
)
14483 return error_mark_node
;
14484 /* Parse the conversion-declarator. */
14485 declarator
= cp_parser_conversion_declarator_opt (parser
);
14487 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14488 /*initialized=*/0, &attributes
);
14490 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14492 /* Don't give this error when parsing tentatively. This happens to
14493 work because we always parse this definitively once. */
14494 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14495 && type_uses_auto (type_specified
))
14497 if (cxx_dialect
< cxx14
)
14499 error ("invalid use of %<auto%> in conversion operator");
14500 return error_mark_node
;
14502 else if (template_parm_scope_p ())
14503 warning (0, "use of %<auto%> in member template "
14504 "conversion operator can never be deduced");
14507 return type_specified
;
14510 /* Parse an (optional) conversion-declarator.
14512 conversion-declarator:
14513 ptr-operator conversion-declarator [opt]
14517 static cp_declarator
*
14518 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14520 enum tree_code code
;
14521 tree class_type
, std_attributes
= NULL_TREE
;
14522 cp_cv_quals cv_quals
;
14524 /* We don't know if there's a ptr-operator next, or not. */
14525 cp_parser_parse_tentatively (parser
);
14526 /* Try the ptr-operator. */
14527 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14529 /* If it worked, look for more conversion-declarators. */
14530 if (cp_parser_parse_definitely (parser
))
14532 cp_declarator
*declarator
;
14534 /* Parse another optional declarator. */
14535 declarator
= cp_parser_conversion_declarator_opt (parser
);
14537 declarator
= cp_parser_make_indirect_declarator
14538 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14546 /* Parse an (optional) ctor-initializer.
14549 : mem-initializer-list */
14552 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14554 /* If the next token is not a `:', then there is no
14555 ctor-initializer. */
14556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14558 /* Do default initialization of any bases and members. */
14559 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14560 finish_mem_initializers (NULL_TREE
);
14564 /* Consume the `:' token. */
14565 cp_lexer_consume_token (parser
->lexer
);
14566 /* And the mem-initializer-list. */
14567 cp_parser_mem_initializer_list (parser
);
14570 /* Parse a mem-initializer-list.
14572 mem-initializer-list:
14573 mem-initializer ... [opt]
14574 mem-initializer ... [opt] , mem-initializer-list */
14577 cp_parser_mem_initializer_list (cp_parser
* parser
)
14579 tree mem_initializer_list
= NULL_TREE
;
14580 tree target_ctor
= error_mark_node
;
14581 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14583 /* Let the semantic analysis code know that we are starting the
14584 mem-initializer-list. */
14585 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14586 error_at (token
->location
,
14587 "only constructors take member initializers");
14589 /* Loop through the list. */
14592 tree mem_initializer
;
14594 token
= cp_lexer_peek_token (parser
->lexer
);
14595 /* Parse the mem-initializer. */
14596 mem_initializer
= cp_parser_mem_initializer (parser
);
14597 /* If the next token is a `...', we're expanding member initializers. */
14598 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14600 || (mem_initializer
!= error_mark_node
14601 && check_for_bare_parameter_packs (TREE_PURPOSE
14602 (mem_initializer
))))
14604 /* Consume the `...'. */
14606 cp_lexer_consume_token (parser
->lexer
);
14608 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14609 can be expanded but members cannot. */
14610 if (mem_initializer
!= error_mark_node
14611 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14613 error_at (token
->location
,
14614 "cannot expand initializer for member %qD",
14615 TREE_PURPOSE (mem_initializer
));
14616 mem_initializer
= error_mark_node
;
14619 /* Construct the pack expansion type. */
14620 if (mem_initializer
!= error_mark_node
)
14621 mem_initializer
= make_pack_expansion (mem_initializer
);
14623 if (target_ctor
!= error_mark_node
14624 && mem_initializer
!= error_mark_node
)
14626 error ("mem-initializer for %qD follows constructor delegation",
14627 TREE_PURPOSE (mem_initializer
));
14628 mem_initializer
= error_mark_node
;
14630 /* Look for a target constructor. */
14631 if (mem_initializer
!= error_mark_node
14632 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14633 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14635 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14636 if (mem_initializer_list
)
14638 error ("constructor delegation follows mem-initializer for %qD",
14639 TREE_PURPOSE (mem_initializer_list
));
14640 mem_initializer
= error_mark_node
;
14642 target_ctor
= mem_initializer
;
14644 /* Add it to the list, unless it was erroneous. */
14645 if (mem_initializer
!= error_mark_node
)
14647 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14648 mem_initializer_list
= mem_initializer
;
14650 /* If the next token is not a `,', we're done. */
14651 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14653 /* Consume the `,' token. */
14654 cp_lexer_consume_token (parser
->lexer
);
14657 /* Perform semantic analysis. */
14658 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14659 finish_mem_initializers (mem_initializer_list
);
14662 /* Parse a mem-initializer.
14665 mem-initializer-id ( expression-list [opt] )
14666 mem-initializer-id braced-init-list
14671 ( expression-list [opt] )
14673 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14674 class) or FIELD_DECL (for a non-static data member) to initialize;
14675 the TREE_VALUE is the expression-list. An empty initialization
14676 list is represented by void_list_node. */
14679 cp_parser_mem_initializer (cp_parser
* parser
)
14681 tree mem_initializer_id
;
14682 tree expression_list
;
14684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14686 /* Find out what is being initialized. */
14687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14689 permerror (token
->location
,
14690 "anachronistic old-style base class initializer");
14691 mem_initializer_id
= NULL_TREE
;
14695 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14696 if (mem_initializer_id
== error_mark_node
)
14697 return mem_initializer_id
;
14699 member
= expand_member_init (mem_initializer_id
);
14700 if (member
&& !DECL_P (member
))
14701 in_base_initializer
= 1;
14703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14705 bool expr_non_constant_p
;
14706 cp_lexer_set_source_position (parser
->lexer
);
14707 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14708 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14709 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14710 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14714 vec
<tree
, va_gc
> *vec
;
14715 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14717 /*allow_expansion_p=*/true,
14718 /*non_constant_p=*/NULL
);
14720 return error_mark_node
;
14721 expression_list
= build_tree_list_vec (vec
);
14722 release_tree_vector (vec
);
14725 if (expression_list
== error_mark_node
)
14726 return error_mark_node
;
14727 if (!expression_list
)
14728 expression_list
= void_type_node
;
14730 in_base_initializer
= 0;
14732 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14735 /* Parse a mem-initializer-id.
14737 mem-initializer-id:
14738 :: [opt] nested-name-specifier [opt] class-name
14739 decltype-specifier (C++11)
14742 Returns a TYPE indicating the class to be initialized for the first
14743 production (and the second in C++11). Returns an IDENTIFIER_NODE
14744 indicating the data member to be initialized for the last production. */
14747 cp_parser_mem_initializer_id (cp_parser
* parser
)
14749 bool global_scope_p
;
14750 bool nested_name_specifier_p
;
14751 bool template_p
= false;
14754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14756 /* `typename' is not allowed in this context ([temp.res]). */
14757 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14759 error_at (token
->location
,
14760 "keyword %<typename%> not allowed in this context (a qualified "
14761 "member initializer is implicitly a type)");
14762 cp_lexer_consume_token (parser
->lexer
);
14764 /* Look for the optional `::' operator. */
14766 = (cp_parser_global_scope_opt (parser
,
14767 /*current_scope_valid_p=*/false)
14769 /* Look for the optional nested-name-specifier. The simplest way to
14774 The keyword `typename' is not permitted in a base-specifier or
14775 mem-initializer; in these contexts a qualified name that
14776 depends on a template-parameter is implicitly assumed to be a
14779 is to assume that we have seen the `typename' keyword at this
14781 nested_name_specifier_p
14782 = (cp_parser_nested_name_specifier_opt (parser
,
14783 /*typename_keyword_p=*/true,
14784 /*check_dependency_p=*/true,
14786 /*is_declaration=*/true)
14788 if (nested_name_specifier_p
)
14789 template_p
= cp_parser_optional_template_keyword (parser
);
14790 /* If there is a `::' operator or a nested-name-specifier, then we
14791 are definitely looking for a class-name. */
14792 if (global_scope_p
|| nested_name_specifier_p
)
14793 return cp_parser_class_name (parser
,
14794 /*typename_keyword_p=*/true,
14795 /*template_keyword_p=*/template_p
,
14797 /*check_dependency_p=*/true,
14798 /*class_head_p=*/false,
14799 /*is_declaration=*/true);
14800 /* Otherwise, we could also be looking for an ordinary identifier. */
14801 cp_parser_parse_tentatively (parser
);
14802 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14803 /* Try a decltype-specifier. */
14804 id
= cp_parser_decltype (parser
);
14806 /* Otherwise, try a class-name. */
14807 id
= cp_parser_class_name (parser
,
14808 /*typename_keyword_p=*/true,
14809 /*template_keyword_p=*/false,
14811 /*check_dependency_p=*/true,
14812 /*class_head_p=*/false,
14813 /*is_declaration=*/true);
14814 /* If we found one, we're done. */
14815 if (cp_parser_parse_definitely (parser
))
14817 /* Otherwise, look for an ordinary identifier. */
14818 return cp_parser_identifier (parser
);
14821 /* Overloading [gram.over] */
14823 /* Parse an operator-function-id.
14825 operator-function-id:
14828 Returns an IDENTIFIER_NODE for the operator which is a
14829 human-readable spelling of the identifier, e.g., `operator +'. */
14832 cp_parser_operator_function_id (cp_parser
* parser
)
14834 /* Look for the `operator' keyword. */
14835 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14836 return error_mark_node
;
14837 /* And then the name of the operator itself. */
14838 return cp_parser_operator (parser
);
14841 /* Return an identifier node for a user-defined literal operator.
14842 The suffix identifier is chained to the operator name identifier. */
14845 cp_literal_operator_id (const char* name
)
14848 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14849 + strlen (name
) + 10);
14850 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14851 identifier
= get_identifier (buffer
);
14856 /* Parse an operator.
14859 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14860 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14861 || ++ -- , ->* -> () []
14868 Returns an IDENTIFIER_NODE for the operator which is a
14869 human-readable spelling of the identifier, e.g., `operator +'. */
14872 cp_parser_operator (cp_parser
* parser
)
14874 tree id
= NULL_TREE
;
14878 /* Peek at the next token. */
14879 token
= cp_lexer_peek_token (parser
->lexer
);
14881 location_t start_loc
= token
->location
;
14883 /* Figure out which operator we have. */
14884 enum tree_code op
= ERROR_MARK
;
14885 bool assop
= false;
14886 bool consumed
= false;
14887 switch (token
->type
)
14891 /* The keyword should be either `new' or `delete'. */
14892 if (token
->keyword
== RID_NEW
)
14894 else if (token
->keyword
== RID_DELETE
)
14899 /* Consume the `new' or `delete' token. */
14900 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14902 /* Peek at the next token. */
14903 token
= cp_lexer_peek_token (parser
->lexer
);
14904 /* If it's a `[' token then this is the array variant of the
14906 if (token
->type
== CPP_OPEN_SQUARE
)
14908 /* Consume the `[' token. */
14909 cp_lexer_consume_token (parser
->lexer
);
14910 /* Look for the `]' token. */
14911 if (cp_token
*close_token
14912 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14913 end_loc
= close_token
->location
;
14914 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14916 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14934 op
= TRUNC_DIV_EXPR
;
14938 op
= TRUNC_MOD_EXPR
;
14958 op
= TRUTH_NOT_EXPR
;
14991 op
= TRUNC_DIV_EXPR
;
14996 op
= TRUNC_MOD_EXPR
;
15022 case CPP_LSHIFT_EQ
:
15027 case CPP_RSHIFT_EQ
:
15044 case CPP_GREATER_EQ
:
15049 op
= TRUTH_ANDIF_EXPR
;
15053 op
= TRUTH_ORIF_EXPR
;
15056 case CPP_PLUS_PLUS
:
15057 op
= POSTINCREMENT_EXPR
;
15060 case CPP_MINUS_MINUS
:
15061 op
= PREDECREMENT_EXPR
;
15065 op
= COMPOUND_EXPR
;
15068 case CPP_DEREF_STAR
:
15073 op
= COMPONENT_REF
;
15076 case CPP_OPEN_PAREN
:
15078 /* Consume the `('. */
15079 matching_parens parens
;
15080 parens
.consume_open (parser
);
15081 /* Look for the matching `)'. */
15082 parens
.require_close (parser
);
15088 case CPP_OPEN_SQUARE
:
15089 /* Consume the `['. */
15090 cp_lexer_consume_token (parser
->lexer
);
15091 /* Look for the matching `]'. */
15092 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15097 case CPP_UTF8STRING
:
15098 case CPP_UTF8STRING_USERDEF
:
15105 case CPP_STRING_USERDEF
:
15106 case CPP_WSTRING_USERDEF
:
15107 case CPP_STRING16_USERDEF
:
15108 case CPP_STRING32_USERDEF
:
15110 tree str
, string_tree
;
15113 if (cxx_dialect
== cxx98
)
15114 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15116 /* Consume the string. */
15117 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15118 /*wide_ok=*/true, /*lookup_udlit=*/false);
15119 if (str
== error_mark_node
)
15120 return error_mark_node
;
15121 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15123 string_tree
= USERDEF_LITERAL_VALUE (str
);
15124 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15129 /* Look for the suffix identifier. */
15130 token
= cp_lexer_peek_token (parser
->lexer
);
15131 if (token
->type
== CPP_NAME
)
15132 id
= cp_parser_identifier (parser
);
15133 else if (token
->type
== CPP_KEYWORD
)
15135 error ("unexpected keyword;"
15136 " remove space between quotes and suffix identifier");
15137 return error_mark_node
;
15141 error ("expected suffix identifier");
15142 return error_mark_node
;
15145 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15146 (TREE_TYPE (TREE_TYPE (string_tree
))));
15147 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15150 error ("expected empty string after %<operator%> keyword");
15151 return error_mark_node
;
15153 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15156 error ("invalid encoding prefix in literal operator");
15157 return error_mark_node
;
15159 if (id
!= error_mark_node
)
15161 const char *name
= IDENTIFIER_POINTER (id
);
15162 id
= cp_literal_operator_id (name
);
15168 /* Anything else is an error. */
15172 /* If we have selected an identifier, we need to consume the
15174 if (op
!= ERROR_MARK
)
15176 id
= ovl_op_identifier (assop
, op
);
15178 cp_lexer_consume_token (parser
->lexer
);
15180 /* Otherwise, no valid operator name was present. */
15183 cp_parser_error (parser
, "expected operator");
15184 id
= error_mark_node
;
15187 return cp_expr (id
, start_loc
);
15190 /* Parse a template-declaration.
15192 template-declaration:
15193 export [opt] template < template-parameter-list > declaration
15195 If MEMBER_P is TRUE, this template-declaration occurs within a
15198 The grammar rule given by the standard isn't correct. What
15199 is really meant is:
15201 template-declaration:
15202 export [opt] template-parameter-list-seq
15203 decl-specifier-seq [opt] init-declarator [opt] ;
15204 export [opt] template-parameter-list-seq
15205 function-definition
15207 template-parameter-list-seq:
15208 template-parameter-list-seq [opt]
15209 template < template-parameter-list >
15211 Concept Extensions:
15213 template-parameter-list-seq:
15214 template < template-parameter-list > requires-clause [opt]
15217 requires logical-or-expression */
15220 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15222 /* Check for `export'. */
15223 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15225 /* Consume the `export' token. */
15226 cp_lexer_consume_token (parser
->lexer
);
15227 /* Warn that we do not support `export'. */
15228 warning (0, "keyword %<export%> not implemented, and will be ignored");
15231 cp_parser_template_declaration_after_export (parser
, member_p
);
15234 /* Parse a template-parameter-list.
15236 template-parameter-list:
15238 template-parameter-list , template-parameter
15240 Returns a TREE_LIST. Each node represents a template parameter.
15241 The nodes are connected via their TREE_CHAINs. */
15244 cp_parser_template_parameter_list (cp_parser
* parser
)
15246 tree parameter_list
= NULL_TREE
;
15248 begin_template_parm_list ();
15250 /* The loop below parses the template parms. We first need to know
15251 the total number of template parms to be able to compute proper
15252 canonical types of each dependent type. So after the loop, when
15253 we know the total number of template parms,
15254 end_template_parm_list computes the proper canonical types and
15255 fixes up the dependent types accordingly. */
15260 bool is_parameter_pack
;
15261 location_t parm_loc
;
15263 /* Parse the template-parameter. */
15264 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15265 parameter
= cp_parser_template_parameter (parser
,
15267 &is_parameter_pack
);
15268 /* Add it to the list. */
15269 if (parameter
!= error_mark_node
)
15270 parameter_list
= process_template_parm (parameter_list
,
15274 is_parameter_pack
);
15277 tree err_parm
= build_tree_list (parameter
, parameter
);
15278 parameter_list
= chainon (parameter_list
, err_parm
);
15281 /* If the next token is not a `,', we're done. */
15282 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15284 /* Otherwise, consume the `,' token. */
15285 cp_lexer_consume_token (parser
->lexer
);
15288 return end_template_parm_list (parameter_list
);
15291 /* Parse a introduction-list.
15294 introduced-parameter
15295 introduction-list , introduced-parameter
15297 introduced-parameter:
15298 ...[opt] identifier
15300 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15301 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15302 WILDCARD_DECL will also have DECL_NAME set and token location in
15303 DECL_SOURCE_LOCATION. */
15306 cp_parser_introduction_list (cp_parser
*parser
)
15308 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15312 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15314 cp_lexer_consume_token (parser
->lexer
);
15316 tree identifier
= cp_parser_identifier (parser
);
15317 if (identifier
== error_mark_node
)
15320 /* Build placeholder. */
15321 tree parm
= build_nt (WILDCARD_DECL
);
15322 DECL_SOURCE_LOCATION (parm
)
15323 = cp_lexer_peek_token (parser
->lexer
)->location
;
15324 DECL_NAME (parm
) = identifier
;
15325 WILDCARD_PACK_P (parm
) = is_pack
;
15326 vec_safe_push (introduction_vec
, parm
);
15328 /* If the next token is not a `,', we're done. */
15329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15331 /* Otherwise, consume the `,' token. */
15332 cp_lexer_consume_token (parser
->lexer
);
15335 /* Convert the vec into a TREE_VEC. */
15336 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15339 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15340 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15342 release_tree_vector (introduction_vec
);
15343 return introduction_list
;
15346 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15347 is an abstract declarator. */
15349 static inline cp_declarator
*
15350 get_id_declarator (cp_declarator
*declarator
)
15352 cp_declarator
*d
= declarator
;
15353 while (d
&& d
->kind
!= cdk_id
)
15358 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15359 is an abstract declarator. */
15362 get_unqualified_id (cp_declarator
*declarator
)
15364 declarator
= get_id_declarator (declarator
);
15366 return declarator
->u
.id
.unqualified_name
;
15371 /* Returns true if DECL represents a constrained-parameter. */
15374 is_constrained_parameter (tree decl
)
15377 && TREE_CODE (decl
) == TYPE_DECL
15378 && CONSTRAINED_PARM_CONCEPT (decl
)
15379 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15382 /* Returns true if PARM declares a constrained-parameter. */
15385 is_constrained_parameter (cp_parameter_declarator
*parm
)
15387 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15390 /* Check that the type parameter is only a declarator-id, and that its
15391 type is not cv-qualified. */
15394 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15395 cp_parameter_declarator
*parm
)
15397 if (!parm
->declarator
)
15400 if (parm
->declarator
->kind
!= cdk_id
)
15402 cp_parser_error (parser
, "invalid constrained type parameter");
15406 /* Don't allow cv-qualified type parameters. */
15407 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15408 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15410 cp_parser_error (parser
, "cv-qualified type parameter");
15417 /* Finish parsing/processing a template type parameter and checking
15418 various restrictions. */
15421 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15423 cp_parameter_declarator
* parmdecl
)
15425 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15426 return finish_template_type_parm (class_type_node
, id
);
15428 return error_mark_node
;
15432 finish_constrained_template_template_parm (tree proto
, tree id
)
15434 /* FIXME: This should probably be copied, and we may need to adjust
15435 the template parameter depths. */
15436 tree saved_parms
= current_template_parms
;
15437 begin_template_parm_list ();
15438 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15439 end_template_parm_list ();
15441 tree parm
= finish_template_template_parm (class_type_node
, id
);
15442 current_template_parms
= saved_parms
;
15447 /* Finish parsing/processing a template template parameter by borrowing
15448 the template parameter list from the prototype parameter. */
15451 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15454 cp_parameter_declarator
*parmdecl
)
15456 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15457 return error_mark_node
;
15458 return finish_constrained_template_template_parm (proto
, id
);
15461 /* Create a new non-type template parameter from the given PARM
15465 constrained_non_type_template_parm (bool *is_non_type
,
15466 cp_parameter_declarator
*parm
)
15468 *is_non_type
= true;
15469 cp_declarator
*decl
= parm
->declarator
;
15470 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15471 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15472 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15475 /* Build a constrained template parameter based on the PARMDECL
15476 declarator. The type of PARMDECL is the constrained type, which
15477 refers to the prototype template parameter that ultimately
15478 specifies the type of the declared parameter. */
15481 finish_constrained_parameter (cp_parser
*parser
,
15482 cp_parameter_declarator
*parmdecl
,
15484 bool *is_parameter_pack
)
15486 tree decl
= parmdecl
->decl_specifiers
.type
;
15487 tree id
= get_unqualified_id (parmdecl
->declarator
);
15488 tree def
= parmdecl
->default_argument
;
15489 tree proto
= DECL_INITIAL (decl
);
15491 /* A template parameter constrained by a variadic concept shall also
15492 be declared as a template parameter pack. */
15493 bool is_variadic
= template_parameter_pack_p (proto
);
15494 if (is_variadic
&& !*is_parameter_pack
)
15495 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15497 /* Build the parameter. Return an error if the declarator was invalid. */
15499 if (TREE_CODE (proto
) == TYPE_DECL
)
15500 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15501 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15502 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15505 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15506 if (parm
== error_mark_node
)
15507 return error_mark_node
;
15509 /* Finish the parameter decl and create a node attaching the
15510 default argument and constraint. */
15511 parm
= build_tree_list (def
, parm
);
15512 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15517 /* Returns true if the parsed type actually represents the declaration
15518 of a type template-parameter. */
15521 declares_constrained_type_template_parameter (tree type
)
15523 return (is_constrained_parameter (type
)
15524 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15528 /* Returns true if the parsed type actually represents the declaration of
15529 a template template-parameter. */
15532 declares_constrained_template_template_parameter (tree type
)
15534 return (is_constrained_parameter (type
)
15535 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15538 /* Parse a default argument for a type template-parameter.
15539 Note that diagnostics are handled in cp_parser_template_parameter. */
15542 cp_parser_default_type_template_argument (cp_parser
*parser
)
15544 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15546 /* Consume the `=' token. */
15547 cp_lexer_consume_token (parser
->lexer
);
15549 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15551 /* Parse the default-argument. */
15552 push_deferring_access_checks (dk_no_deferred
);
15553 tree default_argument
= cp_parser_type_id (parser
);
15554 pop_deferring_access_checks ();
15556 if (flag_concepts
&& type_uses_auto (default_argument
))
15558 error_at (token
->location
,
15559 "invalid use of %<auto%> in default template argument");
15560 return error_mark_node
;
15563 return default_argument
;
15566 /* Parse a default argument for a template template-parameter. */
15569 cp_parser_default_template_template_argument (cp_parser
*parser
)
15571 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15575 /* Consume the `='. */
15576 cp_lexer_consume_token (parser
->lexer
);
15577 /* Parse the id-expression. */
15578 push_deferring_access_checks (dk_no_deferred
);
15579 /* save token before parsing the id-expression, for error
15581 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15582 tree default_argument
15583 = cp_parser_id_expression (parser
,
15584 /*template_keyword_p=*/false,
15585 /*check_dependency_p=*/true,
15586 /*template_p=*/&is_template
,
15587 /*declarator_p=*/false,
15588 /*optional_p=*/false);
15589 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15590 /* If the id-expression was a template-id that refers to
15591 a template-class, we already have the declaration here,
15592 so no further lookup is needed. */
15595 /* Look up the name. */
15597 = cp_parser_lookup_name (parser
, default_argument
,
15599 /*is_template=*/is_template
,
15600 /*is_namespace=*/false,
15601 /*check_dependency=*/true,
15602 /*ambiguous_decls=*/NULL
,
15604 /* See if the default argument is valid. */
15605 default_argument
= check_template_template_default_arg (default_argument
);
15606 pop_deferring_access_checks ();
15607 return default_argument
;
15610 /* Parse a template-parameter.
15612 template-parameter:
15614 parameter-declaration
15616 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15617 the parameter. The TREE_PURPOSE is the default value, if any.
15618 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15619 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15620 set to true iff this parameter is a parameter pack. */
15623 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15624 bool *is_parameter_pack
)
15627 cp_parameter_declarator
*parameter_declarator
;
15630 /* Assume it is a type parameter or a template parameter. */
15631 *is_non_type
= false;
15632 /* Assume it not a parameter pack. */
15633 *is_parameter_pack
= false;
15634 /* Peek at the next token. */
15635 token
= cp_lexer_peek_token (parser
->lexer
);
15636 /* If it is `template', we have a type-parameter. */
15637 if (token
->keyword
== RID_TEMPLATE
)
15638 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15639 /* If it is `class' or `typename' we do not know yet whether it is a
15640 type parameter or a non-type parameter. Consider:
15642 template <typename T, typename T::X X> ...
15646 template <class C, class D*> ...
15648 Here, the first parameter is a type parameter, and the second is
15649 a non-type parameter. We can tell by looking at the token after
15650 the identifier -- if it is a `,', `=', or `>' then we have a type
15652 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15654 /* Peek at the token after `class' or `typename'. */
15655 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15656 /* If it's an ellipsis, we have a template type parameter
15658 if (token
->type
== CPP_ELLIPSIS
)
15659 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15660 /* If it's an identifier, skip it. */
15661 if (token
->type
== CPP_NAME
)
15662 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15663 /* Now, see if the token looks like the end of a template
15665 if (token
->type
== CPP_COMMA
15666 || token
->type
== CPP_EQ
15667 || token
->type
== CPP_GREATER
)
15668 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15671 /* Otherwise, it is a non-type parameter or a constrained parameter.
15675 When parsing a default template-argument for a non-type
15676 template-parameter, the first non-nested `>' is taken as the end
15677 of the template parameter-list rather than a greater-than
15679 parameter_declarator
15680 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15681 /*parenthesized_p=*/NULL
);
15683 if (!parameter_declarator
)
15684 return error_mark_node
;
15686 /* If the parameter declaration is marked as a parameter pack, set
15687 *IS_PARAMETER_PACK to notify the caller. */
15688 if (parameter_declarator
->template_parameter_pack_p
)
15689 *is_parameter_pack
= true;
15691 if (parameter_declarator
->default_argument
)
15693 /* Can happen in some cases of erroneous input (c++/34892). */
15694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15695 /* Consume the `...' for better error recovery. */
15696 cp_lexer_consume_token (parser
->lexer
);
15699 // The parameter may have been constrained.
15700 if (is_constrained_parameter (parameter_declarator
))
15701 return finish_constrained_parameter (parser
,
15702 parameter_declarator
,
15704 is_parameter_pack
);
15706 // Now we're sure that the parameter is a non-type parameter.
15707 *is_non_type
= true;
15709 parm
= grokdeclarator (parameter_declarator
->declarator
,
15710 ¶meter_declarator
->decl_specifiers
,
15711 TPARM
, /*initialized=*/0,
15712 /*attrlist=*/NULL
);
15713 if (parm
== error_mark_node
)
15714 return error_mark_node
;
15716 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15719 /* Parse a type-parameter.
15722 class identifier [opt]
15723 class identifier [opt] = type-id
15724 typename identifier [opt]
15725 typename identifier [opt] = type-id
15726 template < template-parameter-list > class identifier [opt]
15727 template < template-parameter-list > class identifier [opt]
15730 GNU Extension (variadic templates):
15733 class ... identifier [opt]
15734 typename ... identifier [opt]
15736 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15737 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15738 the declaration of the parameter.
15740 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15743 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15748 /* Look for a keyword to tell us what kind of parameter this is. */
15749 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15751 return error_mark_node
;
15753 switch (token
->keyword
)
15759 tree default_argument
;
15761 /* If the next token is an ellipsis, we have a template
15763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15765 /* Consume the `...' token. */
15766 cp_lexer_consume_token (parser
->lexer
);
15767 maybe_warn_variadic_templates ();
15769 *is_parameter_pack
= true;
15772 /* If the next token is an identifier, then it names the
15774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15775 identifier
= cp_parser_identifier (parser
);
15777 identifier
= NULL_TREE
;
15779 /* Create the parameter. */
15780 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15782 /* If the next token is an `=', we have a default argument. */
15783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15786 = cp_parser_default_type_template_argument (parser
);
15788 /* Template parameter packs cannot have default
15790 if (*is_parameter_pack
)
15793 error_at (token
->location
,
15794 "template parameter pack %qD cannot have a "
15795 "default argument", identifier
);
15797 error_at (token
->location
,
15798 "template parameter packs cannot have "
15799 "default arguments");
15800 default_argument
= NULL_TREE
;
15802 else if (check_for_bare_parameter_packs (default_argument
))
15803 default_argument
= error_mark_node
;
15806 default_argument
= NULL_TREE
;
15808 /* Create the combined representation of the parameter and the
15809 default argument. */
15810 parameter
= build_tree_list (default_argument
, parameter
);
15817 tree default_argument
;
15819 /* Look for the `<'. */
15820 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15821 /* Parse the template-parameter-list. */
15822 cp_parser_template_parameter_list (parser
);
15823 /* Look for the `>'. */
15824 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15826 // If template requirements are present, parse them.
15829 tree reqs
= get_shorthand_constraints (current_template_parms
);
15830 if (tree r
= cp_parser_requires_clause_opt (parser
))
15831 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15832 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15835 /* Look for the `class' or 'typename' keywords. */
15836 cp_parser_type_parameter_key (parser
);
15837 /* If the next token is an ellipsis, we have a template
15839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15841 /* Consume the `...' token. */
15842 cp_lexer_consume_token (parser
->lexer
);
15843 maybe_warn_variadic_templates ();
15845 *is_parameter_pack
= true;
15847 /* If the next token is an `=', then there is a
15848 default-argument. If the next token is a `>', we are at
15849 the end of the parameter-list. If the next token is a `,',
15850 then we are at the end of this parameter. */
15851 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15852 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15853 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15855 identifier
= cp_parser_identifier (parser
);
15856 /* Treat invalid names as if the parameter were nameless. */
15857 if (identifier
== error_mark_node
)
15858 identifier
= NULL_TREE
;
15861 identifier
= NULL_TREE
;
15863 /* Create the template parameter. */
15864 parameter
= finish_template_template_parm (class_type_node
,
15867 /* If the next token is an `=', then there is a
15868 default-argument. */
15869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15872 = cp_parser_default_template_template_argument (parser
);
15874 /* Template parameter packs cannot have default
15876 if (*is_parameter_pack
)
15879 error_at (token
->location
,
15880 "template parameter pack %qD cannot "
15881 "have a default argument",
15884 error_at (token
->location
, "template parameter packs cannot "
15885 "have default arguments");
15886 default_argument
= NULL_TREE
;
15890 default_argument
= NULL_TREE
;
15892 /* Create the combined representation of the parameter and the
15893 default argument. */
15894 parameter
= build_tree_list (default_argument
, parameter
);
15899 gcc_unreachable ();
15906 /* Parse a template-id.
15909 template-name < template-argument-list [opt] >
15911 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15912 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15913 returned. Otherwise, if the template-name names a function, or set
15914 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15915 names a class, returns a TYPE_DECL for the specialization.
15917 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15918 uninstantiated templates. */
15921 cp_parser_template_id (cp_parser
*parser
,
15922 bool template_keyword_p
,
15923 bool check_dependency_p
,
15924 enum tag_types tag_type
,
15925 bool is_declaration
)
15930 cp_token_position start_of_id
= 0;
15931 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15932 bool is_identifier
;
15934 /* If the next token corresponds to a template-id, there is no need
15936 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15937 if (token
->type
== CPP_TEMPLATE_ID
)
15939 cp_lexer_consume_token (parser
->lexer
);
15940 return saved_checks_value (token
->u
.tree_check_value
);
15943 /* Avoid performing name lookup if there is no possibility of
15944 finding a template-id. */
15945 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15946 || (token
->type
== CPP_NAME
15947 && !cp_parser_nth_token_starts_template_argument_list_p
15950 cp_parser_error (parser
, "expected template-id");
15951 return error_mark_node
;
15954 /* Remember where the template-id starts. */
15955 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15956 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15958 push_deferring_access_checks (dk_deferred
);
15960 /* Parse the template-name. */
15961 is_identifier
= false;
15962 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15963 check_dependency_p
,
15967 if (templ
== error_mark_node
|| is_identifier
)
15969 pop_deferring_access_checks ();
15973 /* Since we're going to preserve any side-effects from this parse, set up a
15974 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15975 in the template arguments. */
15976 tentative_firewall
firewall (parser
);
15978 /* If we find the sequence `[:' after a template-name, it's probably
15979 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15980 parse correctly the argument list. */
15981 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15982 == CPP_OPEN_SQUARE
)
15983 && next_token
->flags
& DIGRAPH
15984 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15986 && !(next_token_2
->flags
& PREV_WHITE
))
15988 cp_parser_parse_tentatively (parser
);
15989 /* Change `:' into `::'. */
15990 next_token_2
->type
= CPP_SCOPE
;
15991 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15993 cp_lexer_consume_token (parser
->lexer
);
15995 /* Parse the arguments. */
15996 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15997 if (!cp_parser_parse_definitely (parser
))
15999 /* If we couldn't parse an argument list, then we revert our changes
16000 and return simply an error. Maybe this is not a template-id
16002 next_token_2
->type
= CPP_COLON
;
16003 cp_parser_error (parser
, "expected %<<%>");
16004 pop_deferring_access_checks ();
16005 return error_mark_node
;
16007 /* Otherwise, emit an error about the invalid digraph, but continue
16008 parsing because we got our argument list. */
16009 if (permerror (next_token
->location
,
16010 "%<<::%> cannot begin a template-argument list"))
16012 static bool hint
= false;
16013 inform (next_token
->location
,
16014 "%<<:%> is an alternate spelling for %<[%>."
16015 " Insert whitespace between %<<%> and %<::%>");
16016 if (!hint
&& !flag_permissive
)
16018 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16019 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16020 "accept your code)");
16027 /* Look for the `<' that starts the template-argument-list. */
16028 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16030 pop_deferring_access_checks ();
16031 return error_mark_node
;
16033 /* Parse the arguments. */
16034 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16036 if ((cxx_dialect
> cxx17
)
16037 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16038 && !template_keyword_p
16039 && (cp_parser_error_occurred (parser
)
16040 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16042 /* This didn't go well. */
16043 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16045 /* C++2A says that "function-name < a;" is now ill-formed. */
16046 if (cp_parser_error_occurred (parser
))
16048 error_at (token
->location
, "invalid template-argument-list");
16049 inform (token
->location
, "function name as the left hand "
16050 "operand of %<<%> is ill-formed in C++2a; wrap the "
16051 "function name in %<()%>");
16054 /* We expect "f<targs>" to be followed by "(args)". */
16055 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16056 "expected %<(%> after template-argument-list");
16058 /* Purge all subsequent tokens. */
16059 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16062 cp_parser_simulate_error (parser
);
16063 pop_deferring_access_checks ();
16064 return error_mark_node
;
16068 /* Set the location to be of the form:
16069 template-name < template-argument-list [opt] >
16070 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16071 with caret == start at the start of the template-name,
16072 ranging until the closing '>'. */
16073 location_t finish_loc
16074 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16075 location_t combined_loc
16076 = make_location (token
->location
, token
->location
, finish_loc
);
16078 /* Check for concepts autos where they don't belong. We could
16079 identify types in some cases of idnetifier TEMPL, looking ahead
16080 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16081 types. We reject them in functions, but if what we have is an
16082 identifier, even with none_type we can't conclude it's NOT a
16083 type, we have to wait for template substitution. */
16084 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16085 template_id
= error_mark_node
;
16086 /* Build a representation of the specialization. */
16087 else if (identifier_p (templ
))
16088 template_id
= build_min_nt_loc (combined_loc
,
16091 else if (DECL_TYPE_TEMPLATE_P (templ
)
16092 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16094 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16095 template (rather than some instantiation thereof) only if
16096 is not nested within some other construct. For example, in
16097 "template <typename T> void f(T) { A<T>::", A<T> is just an
16098 instantiation of A. */
16099 bool entering_scope
16100 = (template_parm_scope_p ()
16101 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16103 = finish_template_type (templ
, arguments
, entering_scope
);
16105 /* A template-like identifier may be a partial concept id. */
16106 else if (flag_concepts
16107 && (template_id
= (cp_parser_maybe_partial_concept_id
16108 (parser
, templ
, arguments
))))
16109 return template_id
;
16110 else if (variable_template_p (templ
))
16112 template_id
= lookup_template_variable (templ
, arguments
);
16113 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16114 SET_EXPR_LOCATION (template_id
, combined_loc
);
16118 /* If it's not a class-template or a template-template, it should be
16119 a function-template. */
16120 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16121 || TREE_CODE (templ
) == OVERLOAD
16122 || TREE_CODE (templ
) == FUNCTION_DECL
16123 || BASELINK_P (templ
)));
16125 template_id
= lookup_template_function (templ
, arguments
);
16126 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16127 SET_EXPR_LOCATION (template_id
, combined_loc
);
16130 /* If parsing tentatively, replace the sequence of tokens that makes
16131 up the template-id with a CPP_TEMPLATE_ID token. That way,
16132 should we re-parse the token stream, we will not have to repeat
16133 the effort required to do the parse, nor will we issue duplicate
16134 error messages about problems during instantiation of the
16137 /* Don't do this if we had a parse error in a declarator; re-parsing
16138 might succeed if a name changes meaning (60361). */
16139 && !(cp_parser_error_occurred (parser
)
16140 && cp_parser_parsing_tentatively (parser
)
16141 && parser
->in_declarator_p
))
16143 /* Reset the contents of the START_OF_ID token. */
16144 token
->type
= CPP_TEMPLATE_ID
;
16145 token
->location
= combined_loc
;
16147 /* Retrieve any deferred checks. Do not pop this access checks yet
16148 so the memory will not be reclaimed during token replacing below. */
16149 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16150 token
->u
.tree_check_value
->value
= template_id
;
16151 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16152 token
->keyword
= RID_MAX
;
16154 /* Purge all subsequent tokens. */
16155 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16157 /* ??? Can we actually assume that, if template_id ==
16158 error_mark_node, we will have issued a diagnostic to the
16159 user, as opposed to simply marking the tentative parse as
16161 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16162 error_at (token
->location
, "parse error in template argument list");
16165 pop_to_parent_deferring_access_checks ();
16166 return template_id
;
16169 /* Parse a template-name.
16174 The standard should actually say:
16178 operator-function-id
16180 A defect report has been filed about this issue.
16182 A conversion-function-id cannot be a template name because they cannot
16183 be part of a template-id. In fact, looking at this code:
16185 a.operator K<int>()
16187 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16188 It is impossible to call a templated conversion-function-id with an
16189 explicit argument list, since the only allowed template parameter is
16190 the type to which it is converting.
16192 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16193 `template' keyword, in a construction like:
16197 In that case `f' is taken to be a template-name, even though there
16198 is no way of knowing for sure.
16200 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16201 name refers to a set of overloaded functions, at least one of which
16202 is a template, or an IDENTIFIER_NODE with the name of the template,
16203 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16204 names are looked up inside uninstantiated templates. */
16207 cp_parser_template_name (cp_parser
* parser
,
16208 bool template_keyword_p
,
16209 bool check_dependency_p
,
16210 bool is_declaration
,
16211 enum tag_types tag_type
,
16212 bool *is_identifier
)
16216 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16218 /* If the next token is `operator', then we have either an
16219 operator-function-id or a conversion-function-id. */
16220 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16222 /* We don't know whether we're looking at an
16223 operator-function-id or a conversion-function-id. */
16224 cp_parser_parse_tentatively (parser
);
16225 /* Try an operator-function-id. */
16226 identifier
= cp_parser_operator_function_id (parser
);
16227 /* If that didn't work, try a conversion-function-id. */
16228 if (!cp_parser_parse_definitely (parser
))
16230 cp_parser_error (parser
, "expected template-name");
16231 return error_mark_node
;
16234 /* Look for the identifier. */
16236 identifier
= cp_parser_identifier (parser
);
16238 /* If we didn't find an identifier, we don't have a template-id. */
16239 if (identifier
== error_mark_node
)
16240 return error_mark_node
;
16242 /* If the name immediately followed the `template' keyword, then it
16243 is a template-name. However, if the next token is not `<', then
16244 we do not treat it as a template-name, since it is not being used
16245 as part of a template-id. This enables us to handle constructs
16248 template <typename T> struct S { S(); };
16249 template <typename T> S<T>::S();
16251 correctly. We would treat `S' as a template -- if it were `S<T>'
16252 -- but we do not if there is no `<'. */
16254 if (processing_template_decl
16255 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16257 /* In a declaration, in a dependent context, we pretend that the
16258 "template" keyword was present in order to improve error
16259 recovery. For example, given:
16261 template <typename T> void f(T::X<int>);
16263 we want to treat "X<int>" as a template-id. */
16265 && !template_keyword_p
16266 && parser
->scope
&& TYPE_P (parser
->scope
)
16267 && check_dependency_p
16268 && dependent_scope_p (parser
->scope
)
16269 /* Do not do this for dtors (or ctors), since they never
16270 need the template keyword before their name. */
16271 && !constructor_name_p (identifier
, parser
->scope
))
16273 cp_token_position start
= 0;
16275 /* Explain what went wrong. */
16276 error_at (token
->location
, "non-template %qD used as template",
16278 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16279 parser
->scope
, identifier
);
16280 /* If parsing tentatively, find the location of the "<" token. */
16281 if (cp_parser_simulate_error (parser
))
16282 start
= cp_lexer_token_position (parser
->lexer
, true);
16283 /* Parse the template arguments so that we can issue error
16284 messages about them. */
16285 cp_lexer_consume_token (parser
->lexer
);
16286 cp_parser_enclosed_template_argument_list (parser
);
16287 /* Skip tokens until we find a good place from which to
16288 continue parsing. */
16289 cp_parser_skip_to_closing_parenthesis (parser
,
16290 /*recovering=*/true,
16292 /*consume_paren=*/false);
16293 /* If parsing tentatively, permanently remove the
16294 template argument list. That will prevent duplicate
16295 error messages from being issued about the missing
16296 "template" keyword. */
16298 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16300 *is_identifier
= true;
16301 parser
->context
->object_type
= NULL_TREE
;
16305 /* If the "template" keyword is present, then there is generally
16306 no point in doing name-lookup, so we just return IDENTIFIER.
16307 But, if the qualifying scope is non-dependent then we can
16308 (and must) do name-lookup normally. */
16309 if (template_keyword_p
)
16311 tree scope
= (parser
->scope
? parser
->scope
16312 : parser
->context
->object_type
);
16313 if (scope
&& TYPE_P (scope
)
16314 && (!CLASS_TYPE_P (scope
)
16315 || (check_dependency_p
&& dependent_type_p (scope
))))
16317 /* We're optimizing away the call to cp_parser_lookup_name, but
16318 we still need to do this. */
16319 parser
->context
->object_type
= NULL_TREE
;
16325 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16326 const bool scoped_p
= ((parser
->scope
? parser
->scope
16327 : parser
->context
->object_type
) != NULL_TREE
);
16329 /* Look up the name. */
16330 decl
= cp_parser_lookup_name (parser
, identifier
,
16332 /*is_template=*/true,
16333 /*is_namespace=*/false,
16334 check_dependency_p
,
16335 /*ambiguous_decls=*/NULL
,
16338 decl
= strip_using_decl (decl
);
16340 /* If DECL is a template, then the name was a template-name. */
16341 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16343 if (TREE_DEPRECATED (decl
)
16344 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16345 warn_deprecated_use (decl
, NULL_TREE
);
16349 /* The standard does not explicitly indicate whether a name that
16350 names a set of overloaded declarations, some of which are
16351 templates, is a template-name. However, such a name should
16352 be a template-name; otherwise, there is no way to form a
16353 template-id for the overloaded templates. */
16354 bool found
= false;
16356 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16357 !found
&& iter
; ++iter
)
16358 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16362 && (cxx_dialect
> cxx17
)
16364 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
16366 /* [temp.names] says "A name is also considered to refer to a template
16367 if it is an unqualified-id followed by a < and name lookup finds
16368 either one or more functions or finds nothing." */
16370 /* The "more functions" case. Just use the OVERLOAD as normally.
16371 We don't use is_overloaded_fn here to avoid considering
16373 if (TREE_CODE (decl
) == OVERLOAD
16374 /* Name lookup found one function. */
16375 || TREE_CODE (decl
) == FUNCTION_DECL
)
16377 /* Name lookup found nothing. */
16378 else if (decl
== error_mark_node
)
16384 /* The name does not name a template. */
16385 cp_parser_error (parser
, "expected template-name");
16386 return error_mark_node
;
16393 /* Parse a template-argument-list.
16395 template-argument-list:
16396 template-argument ... [opt]
16397 template-argument-list , template-argument ... [opt]
16399 Returns a TREE_VEC containing the arguments. */
16402 cp_parser_template_argument_list (cp_parser
* parser
)
16404 tree fixed_args
[10];
16405 unsigned n_args
= 0;
16406 unsigned alloced
= 10;
16407 tree
*arg_ary
= fixed_args
;
16409 bool saved_in_template_argument_list_p
;
16411 bool saved_non_ice_p
;
16413 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16414 parser
->in_template_argument_list_p
= true;
16415 /* Even if the template-id appears in an integral
16416 constant-expression, the contents of the argument list do
16418 saved_ice_p
= parser
->integral_constant_expression_p
;
16419 parser
->integral_constant_expression_p
= false;
16420 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16421 parser
->non_integral_constant_expression_p
= false;
16423 /* Parse the arguments. */
16429 /* Consume the comma. */
16430 cp_lexer_consume_token (parser
->lexer
);
16432 /* Parse the template-argument. */
16433 argument
= cp_parser_template_argument (parser
);
16435 /* If the next token is an ellipsis, we're expanding a template
16437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16439 if (argument
== error_mark_node
)
16441 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16442 error_at (token
->location
,
16443 "expected parameter pack before %<...%>");
16445 /* Consume the `...' token. */
16446 cp_lexer_consume_token (parser
->lexer
);
16448 /* Make the argument into a TYPE_PACK_EXPANSION or
16449 EXPR_PACK_EXPANSION. */
16450 argument
= make_pack_expansion (argument
);
16453 if (n_args
== alloced
)
16457 if (arg_ary
== fixed_args
)
16459 arg_ary
= XNEWVEC (tree
, alloced
);
16460 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16463 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16465 arg_ary
[n_args
++] = argument
;
16467 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16469 vec
= make_tree_vec (n_args
);
16472 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16474 if (arg_ary
!= fixed_args
)
16476 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16477 parser
->integral_constant_expression_p
= saved_ice_p
;
16478 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16480 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16484 /* Parse a template-argument.
16487 assignment-expression
16491 The representation is that of an assignment-expression, type-id, or
16492 id-expression -- except that the qualified id-expression is
16493 evaluated, so that the value returned is either a DECL or an
16496 Although the standard says "assignment-expression", it forbids
16497 throw-expressions or assignments in the template argument.
16498 Therefore, we use "conditional-expression" instead. */
16501 cp_parser_template_argument (cp_parser
* parser
)
16506 bool maybe_type_id
= false;
16507 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16508 location_t loc
= 0;
16511 /* There's really no way to know what we're looking at, so we just
16512 try each alternative in order.
16516 In a template-argument, an ambiguity between a type-id and an
16517 expression is resolved to a type-id, regardless of the form of
16518 the corresponding template-parameter.
16520 Therefore, we try a type-id first. */
16521 cp_parser_parse_tentatively (parser
);
16522 argument
= cp_parser_template_type_arg (parser
);
16523 /* If there was no error parsing the type-id but the next token is a
16524 '>>', our behavior depends on which dialect of C++ we're
16525 parsing. In C++98, we probably found a typo for '> >'. But there
16526 are type-id which are also valid expressions. For instance:
16528 struct X { int operator >> (int); };
16529 template <int V> struct Foo {};
16532 Here 'X()' is a valid type-id of a function type, but the user just
16533 wanted to write the expression "X() >> 5". Thus, we remember that we
16534 found a valid type-id, but we still try to parse the argument as an
16535 expression to see what happens.
16537 In C++0x, the '>>' will be considered two separate '>'
16539 if (!cp_parser_error_occurred (parser
)
16540 && cxx_dialect
== cxx98
16541 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16543 maybe_type_id
= true;
16544 cp_parser_abort_tentative_parse (parser
);
16548 /* If the next token isn't a `,' or a `>', then this argument wasn't
16549 really finished. This means that the argument is not a valid
16551 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16552 cp_parser_error (parser
, "expected template-argument");
16553 /* If that worked, we're done. */
16554 if (cp_parser_parse_definitely (parser
))
16557 /* We're still not sure what the argument will be. */
16558 cp_parser_parse_tentatively (parser
);
16559 /* Try a template. */
16560 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16561 argument
= cp_parser_id_expression (parser
,
16562 /*template_keyword_p=*/false,
16563 /*check_dependency_p=*/true,
16565 /*declarator_p=*/false,
16566 /*optional_p=*/false);
16567 /* If the next token isn't a `,' or a `>', then this argument wasn't
16568 really finished. */
16569 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16570 cp_parser_error (parser
, "expected template-argument");
16571 if (!cp_parser_error_occurred (parser
))
16573 /* Figure out what is being referred to. If the id-expression
16574 was for a class template specialization, then we will have a
16575 TYPE_DECL at this point. There is no need to do name lookup
16576 at this point in that case. */
16577 if (TREE_CODE (argument
) != TYPE_DECL
)
16578 argument
= cp_parser_lookup_name (parser
, argument
,
16580 /*is_template=*/template_p
,
16581 /*is_namespace=*/false,
16582 /*check_dependency=*/true,
16583 /*ambiguous_decls=*/NULL
,
16584 argument_start_token
->location
);
16585 /* Handle a constrained-type-specifier for a non-type template
16587 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16589 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16590 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16591 cp_parser_error (parser
, "expected template-name");
16593 if (cp_parser_parse_definitely (parser
))
16595 if (TREE_DEPRECATED (argument
))
16596 warn_deprecated_use (argument
, NULL_TREE
);
16599 /* It must be a non-type argument. In C++17 any constant-expression is
16601 if (cxx_dialect
> cxx14
)
16604 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16606 -- an integral constant-expression of integral or enumeration
16609 -- the name of a non-type template-parameter; or
16611 -- the name of an object or function with external linkage...
16613 -- the address of an object or function with external linkage...
16615 -- a pointer to member... */
16616 /* Look for a non-type template parameter. */
16617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16619 cp_parser_parse_tentatively (parser
);
16620 argument
= cp_parser_primary_expression (parser
,
16621 /*address_p=*/false,
16623 /*template_arg_p=*/true,
16625 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16626 || !cp_parser_next_token_ends_template_argument_p (parser
))
16627 cp_parser_simulate_error (parser
);
16628 if (cp_parser_parse_definitely (parser
))
16632 /* If the next token is "&", the argument must be the address of an
16633 object or function with external linkage. */
16634 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16637 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16638 cp_lexer_consume_token (parser
->lexer
);
16640 /* See if we might have an id-expression. */
16641 token
= cp_lexer_peek_token (parser
->lexer
);
16642 if (token
->type
== CPP_NAME
16643 || token
->keyword
== RID_OPERATOR
16644 || token
->type
== CPP_SCOPE
16645 || token
->type
== CPP_TEMPLATE_ID
16646 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16648 cp_parser_parse_tentatively (parser
);
16649 argument
= cp_parser_primary_expression (parser
,
16652 /*template_arg_p=*/true,
16654 if (cp_parser_error_occurred (parser
)
16655 || !cp_parser_next_token_ends_template_argument_p (parser
))
16656 cp_parser_abort_tentative_parse (parser
);
16661 if (INDIRECT_REF_P (argument
))
16663 /* Strip the dereference temporarily. */
16664 gcc_assert (REFERENCE_REF_P (argument
));
16665 argument
= TREE_OPERAND (argument
, 0);
16668 /* If we're in a template, we represent a qualified-id referring
16669 to a static data member as a SCOPE_REF even if the scope isn't
16670 dependent so that we can check access control later. */
16672 if (TREE_CODE (probe
) == SCOPE_REF
)
16673 probe
= TREE_OPERAND (probe
, 1);
16676 /* A variable without external linkage might still be a
16677 valid constant-expression, so no error is issued here
16678 if the external-linkage check fails. */
16679 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16680 cp_parser_simulate_error (parser
);
16682 else if (is_overloaded_fn (argument
))
16683 /* All overloaded functions are allowed; if the external
16684 linkage test does not pass, an error will be issued
16688 && (TREE_CODE (argument
) == OFFSET_REF
16689 || TREE_CODE (argument
) == SCOPE_REF
))
16690 /* A pointer-to-member. */
16692 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16695 cp_parser_simulate_error (parser
);
16697 if (cp_parser_parse_definitely (parser
))
16700 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16701 tf_warning_or_error
);
16703 argument
= convert_from_reference (argument
);
16708 /* If the argument started with "&", there are no other valid
16709 alternatives at this point. */
16712 cp_parser_error (parser
, "invalid non-type template argument");
16713 return error_mark_node
;
16717 /* If the argument wasn't successfully parsed as a type-id followed
16718 by '>>', the argument can only be a constant expression now.
16719 Otherwise, we try parsing the constant-expression tentatively,
16720 because the argument could really be a type-id. */
16722 cp_parser_parse_tentatively (parser
);
16724 if (cxx_dialect
<= cxx14
)
16725 argument
= cp_parser_constant_expression (parser
);
16728 /* With C++17 generalized non-type template arguments we need to handle
16729 lvalue constant expressions, too. */
16730 argument
= cp_parser_assignment_expression (parser
);
16731 require_potential_constant_expression (argument
);
16734 if (!maybe_type_id
)
16736 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16737 cp_parser_error (parser
, "expected template-argument");
16738 if (cp_parser_parse_definitely (parser
))
16740 /* We did our best to parse the argument as a non type-id, but that
16741 was the only alternative that matched (albeit with a '>' after
16742 it). We can assume it's just a typo from the user, and a
16743 diagnostic will then be issued. */
16744 return cp_parser_template_type_arg (parser
);
16747 /* Parse an explicit-instantiation.
16749 explicit-instantiation:
16750 template declaration
16752 Although the standard says `declaration', what it really means is:
16754 explicit-instantiation:
16755 template decl-specifier-seq [opt] declarator [opt] ;
16757 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16758 supposed to be allowed. A defect report has been filed about this
16763 explicit-instantiation:
16764 storage-class-specifier template
16765 decl-specifier-seq [opt] declarator [opt] ;
16766 function-specifier template
16767 decl-specifier-seq [opt] declarator [opt] ; */
16770 cp_parser_explicit_instantiation (cp_parser
* parser
)
16772 int declares_class_or_enum
;
16773 cp_decl_specifier_seq decl_specifiers
;
16774 tree extension_specifier
= NULL_TREE
;
16776 timevar_push (TV_TEMPLATE_INST
);
16778 /* Look for an (optional) storage-class-specifier or
16779 function-specifier. */
16780 if (cp_parser_allow_gnu_extensions_p (parser
))
16782 extension_specifier
16783 = cp_parser_storage_class_specifier_opt (parser
);
16784 if (!extension_specifier
)
16785 extension_specifier
16786 = cp_parser_function_specifier_opt (parser
,
16787 /*decl_specs=*/NULL
);
16790 /* Look for the `template' keyword. */
16791 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16792 /* Let the front end know that we are processing an explicit
16794 begin_explicit_instantiation ();
16795 /* [temp.explicit] says that we are supposed to ignore access
16796 control while processing explicit instantiation directives. */
16797 push_deferring_access_checks (dk_no_check
);
16798 /* Parse a decl-specifier-seq. */
16799 cp_parser_decl_specifier_seq (parser
,
16800 CP_PARSER_FLAGS_OPTIONAL
,
16802 &declares_class_or_enum
);
16803 /* If there was exactly one decl-specifier, and it declared a class,
16804 and there's no declarator, then we have an explicit type
16806 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16810 type
= check_tag_decl (&decl_specifiers
,
16811 /*explicit_type_instantiation_p=*/true);
16812 /* Turn access control back on for names used during
16813 template instantiation. */
16814 pop_deferring_access_checks ();
16816 do_type_instantiation (type
, extension_specifier
,
16817 /*complain=*/tf_error
);
16821 cp_declarator
*declarator
;
16824 /* Parse the declarator. */
16826 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16827 /*ctor_dtor_or_conv_p=*/NULL
,
16828 /*parenthesized_p=*/NULL
,
16829 /*member_p=*/false,
16830 /*friend_p=*/false);
16831 if (declares_class_or_enum
& 2)
16832 cp_parser_check_for_definition_in_return_type (declarator
,
16833 decl_specifiers
.type
,
16834 decl_specifiers
.locations
[ds_type_spec
]);
16835 if (declarator
!= cp_error_declarator
)
16837 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16838 permerror (decl_specifiers
.locations
[ds_inline
],
16839 "explicit instantiation shall not use"
16840 " %<inline%> specifier");
16841 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16842 permerror (decl_specifiers
.locations
[ds_constexpr
],
16843 "explicit instantiation shall not use"
16844 " %<constexpr%> specifier");
16846 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16847 NORMAL
, 0, &decl_specifiers
.attributes
);
16848 /* Turn access control back on for names used during
16849 template instantiation. */
16850 pop_deferring_access_checks ();
16851 /* Do the explicit instantiation. */
16852 do_decl_instantiation (decl
, extension_specifier
);
16856 pop_deferring_access_checks ();
16857 /* Skip the body of the explicit instantiation. */
16858 cp_parser_skip_to_end_of_statement (parser
);
16861 /* We're done with the instantiation. */
16862 end_explicit_instantiation ();
16864 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16866 timevar_pop (TV_TEMPLATE_INST
);
16869 /* Parse an explicit-specialization.
16871 explicit-specialization:
16872 template < > declaration
16874 Although the standard says `declaration', what it really means is:
16876 explicit-specialization:
16877 template <> decl-specifier [opt] init-declarator [opt] ;
16878 template <> function-definition
16879 template <> explicit-specialization
16880 template <> template-declaration */
16883 cp_parser_explicit_specialization (cp_parser
* parser
)
16885 bool need_lang_pop
;
16886 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16888 /* Look for the `template' keyword. */
16889 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16890 /* Look for the `<'. */
16891 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16892 /* Look for the `>'. */
16893 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16894 /* We have processed another parameter list. */
16895 ++parser
->num_template_parameter_lists
;
16898 A template ... explicit specialization ... shall not have C
16900 if (current_lang_name
== lang_name_c
)
16902 error_at (token
->location
, "template specialization with C linkage");
16903 maybe_show_extern_c_location ();
16904 /* Give it C++ linkage to avoid confusing other parts of the
16906 push_lang_context (lang_name_cplusplus
);
16907 need_lang_pop
= true;
16910 need_lang_pop
= false;
16911 /* Let the front end know that we are beginning a specialization. */
16912 if (!begin_specialization ())
16914 end_specialization ();
16918 /* If the next keyword is `template', we need to figure out whether
16919 or not we're looking a template-declaration. */
16920 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16922 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16923 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16924 cp_parser_template_declaration_after_export (parser
,
16925 /*member_p=*/false);
16927 cp_parser_explicit_specialization (parser
);
16930 /* Parse the dependent declaration. */
16931 cp_parser_single_declaration (parser
,
16933 /*member_p=*/false,
16934 /*explicit_specialization_p=*/true,
16935 /*friend_p=*/NULL
);
16936 /* We're done with the specialization. */
16937 end_specialization ();
16938 /* For the erroneous case of a template with C linkage, we pushed an
16939 implicit C++ linkage scope; exit that scope now. */
16941 pop_lang_context ();
16942 /* We're done with this parameter list. */
16943 --parser
->num_template_parameter_lists
;
16946 /* Parse a type-specifier.
16949 simple-type-specifier
16952 elaborated-type-specifier
16960 Returns a representation of the type-specifier. For a
16961 class-specifier, enum-specifier, or elaborated-type-specifier, a
16962 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16964 The parser flags FLAGS is used to control type-specifier parsing.
16966 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16967 in a decl-specifier-seq.
16969 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16970 class-specifier, enum-specifier, or elaborated-type-specifier, then
16971 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16972 if a type is declared; 2 if it is defined. Otherwise, it is set to
16975 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16976 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16977 is set to FALSE. */
16980 cp_parser_type_specifier (cp_parser
* parser
,
16981 cp_parser_flags flags
,
16982 cp_decl_specifier_seq
*decl_specs
,
16983 bool is_declaration
,
16984 int* declares_class_or_enum
,
16985 bool* is_cv_qualifier
)
16987 tree type_spec
= NULL_TREE
;
16990 cp_decl_spec ds
= ds_last
;
16992 /* Assume this type-specifier does not declare a new type. */
16993 if (declares_class_or_enum
)
16994 *declares_class_or_enum
= 0;
16995 /* And that it does not specify a cv-qualifier. */
16996 if (is_cv_qualifier
)
16997 *is_cv_qualifier
= false;
16998 /* Peek at the next token. */
16999 token
= cp_lexer_peek_token (parser
->lexer
);
17001 /* If we're looking at a keyword, we can use that to guide the
17002 production we choose. */
17003 keyword
= token
->keyword
;
17007 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17008 goto elaborated_type_specifier
;
17010 /* Look for the enum-specifier. */
17011 type_spec
= cp_parser_enum_specifier (parser
);
17012 /* If that worked, we're done. */
17015 if (declares_class_or_enum
)
17016 *declares_class_or_enum
= 2;
17018 cp_parser_set_decl_spec_type (decl_specs
,
17021 /*type_definition_p=*/true);
17025 goto elaborated_type_specifier
;
17027 /* Any of these indicate either a class-specifier, or an
17028 elaborated-type-specifier. */
17032 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17033 goto elaborated_type_specifier
;
17035 /* Parse tentatively so that we can back up if we don't find a
17036 class-specifier. */
17037 cp_parser_parse_tentatively (parser
);
17038 /* Look for the class-specifier. */
17039 type_spec
= cp_parser_class_specifier (parser
);
17040 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17041 /* If that worked, we're done. */
17042 if (cp_parser_parse_definitely (parser
))
17044 if (declares_class_or_enum
)
17045 *declares_class_or_enum
= 2;
17047 cp_parser_set_decl_spec_type (decl_specs
,
17050 /*type_definition_p=*/true);
17054 /* Fall through. */
17055 elaborated_type_specifier
:
17056 /* We're declaring (not defining) a class or enum. */
17057 if (declares_class_or_enum
)
17058 *declares_class_or_enum
= 1;
17060 /* Fall through. */
17062 /* Look for an elaborated-type-specifier. */
17064 = (cp_parser_elaborated_type_specifier
17066 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17069 cp_parser_set_decl_spec_type (decl_specs
,
17072 /*type_definition_p=*/false);
17077 if (is_cv_qualifier
)
17078 *is_cv_qualifier
= true;
17083 if (is_cv_qualifier
)
17084 *is_cv_qualifier
= true;
17089 if (is_cv_qualifier
)
17090 *is_cv_qualifier
= true;
17094 /* The `__complex__' keyword is a GNU extension. */
17102 /* Handle simple keywords. */
17107 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17108 decl_specs
->any_specifiers_p
= true;
17110 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17113 /* If we do not already have a type-specifier, assume we are looking
17114 at a simple-type-specifier. */
17115 type_spec
= cp_parser_simple_type_specifier (parser
,
17119 /* If we didn't find a type-specifier, and a type-specifier was not
17120 optional in this context, issue an error message. */
17121 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17123 cp_parser_error (parser
, "expected type specifier");
17124 return error_mark_node
;
17130 /* Parse a simple-type-specifier.
17132 simple-type-specifier:
17133 :: [opt] nested-name-specifier [opt] type-name
17134 :: [opt] nested-name-specifier template template-id
17149 simple-type-specifier:
17151 decltype ( expression )
17154 __underlying_type ( type-id )
17158 nested-name-specifier(opt) template-name
17162 simple-type-specifier:
17164 __typeof__ unary-expression
17165 __typeof__ ( type-id )
17166 __typeof__ ( type-id ) { initializer-list , [opt] }
17168 Concepts Extension:
17170 simple-type-specifier:
17171 constrained-type-specifier
17173 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17174 appropriately updated. */
17177 cp_parser_simple_type_specifier (cp_parser
* parser
,
17178 cp_decl_specifier_seq
*decl_specs
,
17179 cp_parser_flags flags
)
17181 tree type
= NULL_TREE
;
17185 /* Peek at the next token. */
17186 token
= cp_lexer_peek_token (parser
->lexer
);
17188 /* If we're looking at a keyword, things are easy. */
17189 switch (token
->keyword
)
17193 decl_specs
->explicit_char_p
= true;
17194 type
= char_type_node
;
17197 type
= char16_type_node
;
17200 type
= char32_type_node
;
17203 type
= wchar_type_node
;
17206 type
= boolean_type_node
;
17209 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17210 type
= short_integer_type_node
;
17214 decl_specs
->explicit_int_p
= true;
17215 type
= integer_type_node
;
17221 idx
= token
->keyword
- RID_INT_N_0
;
17222 if (! int_n_enabled_p
[idx
])
17226 decl_specs
->explicit_intN_p
= true;
17227 decl_specs
->int_n_idx
= idx
;
17229 type
= int_n_trees
[idx
].signed_type
;
17233 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17234 type
= long_integer_type_node
;
17237 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17238 type
= integer_type_node
;
17241 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17242 type
= unsigned_type_node
;
17245 type
= float_type_node
;
17248 type
= double_type_node
;
17251 type
= void_type_node
;
17255 maybe_warn_cpp0x (CPP0X_AUTO
);
17256 if (parser
->auto_is_implicit_function_template_parm_p
)
17258 /* The 'auto' might be the placeholder return type for a function decl
17259 with trailing return type. */
17260 bool have_trailing_return_fn_decl
= false;
17262 cp_parser_parse_tentatively (parser
);
17263 cp_lexer_consume_token (parser
->lexer
);
17264 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17265 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17266 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17267 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17269 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17271 cp_lexer_consume_token (parser
->lexer
);
17272 cp_parser_skip_to_closing_parenthesis (parser
,
17273 /*recovering*/false,
17275 /*consume_paren*/true);
17279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17281 have_trailing_return_fn_decl
= true;
17285 cp_lexer_consume_token (parser
->lexer
);
17287 cp_parser_abort_tentative_parse (parser
);
17289 if (have_trailing_return_fn_decl
)
17291 type
= make_auto ();
17295 if (cxx_dialect
>= cxx14
)
17297 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17298 type
= TREE_TYPE (type
);
17301 type
= error_mark_node
;
17303 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17305 if (cxx_dialect
< cxx14
)
17306 error_at (token
->location
,
17307 "use of %<auto%> in lambda parameter declaration "
17308 "only available with "
17309 "-std=c++14 or -std=gnu++14");
17311 else if (cxx_dialect
< cxx14
)
17312 error_at (token
->location
,
17313 "use of %<auto%> in parameter declaration "
17314 "only available with "
17315 "-std=c++14 or -std=gnu++14");
17316 else if (!flag_concepts
)
17317 pedwarn (token
->location
, 0,
17318 "use of %<auto%> in parameter declaration "
17319 "only available with -fconcepts");
17322 type
= make_auto ();
17326 /* Since DR 743, decltype can either be a simple-type-specifier by
17327 itself or begin a nested-name-specifier. Parsing it will replace
17328 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17329 handling below decide what to do. */
17330 cp_parser_decltype (parser
);
17331 cp_lexer_set_token_position (parser
->lexer
, token
);
17335 /* Consume the `typeof' token. */
17336 cp_lexer_consume_token (parser
->lexer
);
17337 /* Parse the operand to `typeof'. */
17338 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17339 /* If it is not already a TYPE, take its type. */
17340 if (!TYPE_P (type
))
17341 type
= finish_typeof (type
);
17344 cp_parser_set_decl_spec_type (decl_specs
, type
,
17346 /*type_definition_p=*/false);
17350 case RID_UNDERLYING_TYPE
:
17351 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17353 cp_parser_set_decl_spec_type (decl_specs
, type
,
17355 /*type_definition_p=*/false);
17360 case RID_DIRECT_BASES
:
17361 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17363 cp_parser_set_decl_spec_type (decl_specs
, type
,
17365 /*type_definition_p=*/false);
17371 /* If token is an already-parsed decltype not followed by ::,
17372 it's a simple-type-specifier. */
17373 if (token
->type
== CPP_DECLTYPE
17374 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17376 type
= saved_checks_value (token
->u
.tree_check_value
);
17379 cp_parser_set_decl_spec_type (decl_specs
, type
,
17381 /*type_definition_p=*/false);
17382 /* Remember that we are handling a decltype in order to
17383 implement the resolution of DR 1510 when the argument
17384 isn't instantiation dependent. */
17385 decl_specs
->decltype_p
= true;
17387 cp_lexer_consume_token (parser
->lexer
);
17391 /* If the type-specifier was for a built-in type, we're done. */
17394 /* Record the type. */
17396 && (token
->keyword
!= RID_SIGNED
17397 && token
->keyword
!= RID_UNSIGNED
17398 && token
->keyword
!= RID_SHORT
17399 && token
->keyword
!= RID_LONG
))
17400 cp_parser_set_decl_spec_type (decl_specs
,
17403 /*type_definition_p=*/false);
17405 decl_specs
->any_specifiers_p
= true;
17407 /* Consume the token. */
17408 cp_lexer_consume_token (parser
->lexer
);
17410 if (type
== error_mark_node
)
17411 return error_mark_node
;
17413 /* There is no valid C++ program where a non-template type is
17414 followed by a "<". That usually indicates that the user thought
17415 that the type was a template. */
17416 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17419 return TYPE_NAME (type
);
17422 /* The type-specifier must be a user-defined type. */
17423 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17428 /* Don't gobble tokens or issue error messages if this is an
17429 optional type-specifier. */
17430 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17431 cp_parser_parse_tentatively (parser
);
17433 token
= cp_lexer_peek_token (parser
->lexer
);
17435 /* Look for the optional `::' operator. */
17437 = (cp_parser_global_scope_opt (parser
,
17438 /*current_scope_valid_p=*/false)
17440 /* Look for the nested-name specifier. */
17442 = (cp_parser_nested_name_specifier_opt (parser
,
17443 /*typename_keyword_p=*/false,
17444 /*check_dependency_p=*/true,
17446 /*is_declaration=*/false)
17448 /* If we have seen a nested-name-specifier, and the next token
17449 is `template', then we are using the template-id production. */
17451 && cp_parser_optional_template_keyword (parser
))
17453 /* Look for the template-id. */
17454 type
= cp_parser_template_id (parser
,
17455 /*template_keyword_p=*/true,
17456 /*check_dependency_p=*/true,
17458 /*is_declaration=*/false);
17459 /* If the template-id did not name a type, we are out of
17461 if (TREE_CODE (type
) != TYPE_DECL
)
17463 cp_parser_error (parser
, "expected template-id for type");
17467 /* Otherwise, look for a type-name. */
17469 type
= cp_parser_type_name (parser
);
17470 /* Keep track of all name-lookups performed in class scopes. */
17474 && TREE_CODE (type
) == TYPE_DECL
17475 && identifier_p (DECL_NAME (type
)))
17476 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17477 /* If it didn't work out, we don't have a TYPE. */
17478 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17479 && !cp_parser_parse_definitely (parser
))
17481 if (!type
&& cxx_dialect
>= cxx17
)
17483 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17484 cp_parser_parse_tentatively (parser
);
17486 cp_parser_global_scope_opt (parser
,
17487 /*current_scope_valid_p=*/false);
17488 cp_parser_nested_name_specifier_opt (parser
,
17489 /*typename_keyword_p=*/false,
17490 /*check_dependency_p=*/true,
17492 /*is_declaration=*/false);
17493 tree name
= cp_parser_identifier (parser
);
17494 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17495 && parser
->scope
!= error_mark_node
)
17497 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17499 /*is_template=*/false,
17500 /*is_namespace=*/false,
17501 /*check_dependency=*/true,
17502 /*ambiguous_decls=*/NULL
,
17504 if (tmpl
&& tmpl
!= error_mark_node
17505 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17506 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17507 type
= make_template_placeholder (tmpl
);
17510 type
= error_mark_node
;
17511 if (!cp_parser_simulate_error (parser
))
17512 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17513 NLE_TYPE
, token
->location
);
17517 type
= error_mark_node
;
17519 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17520 && !cp_parser_parse_definitely (parser
))
17523 if (type
&& decl_specs
)
17524 cp_parser_set_decl_spec_type (decl_specs
, type
,
17526 /*type_definition_p=*/false);
17529 /* If we didn't get a type-name, issue an error message. */
17530 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17532 cp_parser_error (parser
, "expected type-name");
17533 return error_mark_node
;
17536 if (type
&& type
!= error_mark_node
)
17538 /* See if TYPE is an Objective-C type, and if so, parse and
17539 accept any protocol references following it. Do this before
17540 the cp_parser_check_for_invalid_template_id() call, because
17541 Objective-C types can be followed by '<...>' which would
17542 enclose protocol names rather than template arguments, and so
17543 everything is fine. */
17544 if (c_dialect_objc () && !parser
->scope
17545 && (objc_is_id (type
) || objc_is_class_name (type
)))
17547 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17548 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17550 /* Clobber the "unqualified" type previously entered into
17551 DECL_SPECS with the new, improved protocol-qualified version. */
17553 decl_specs
->type
= qual_type
;
17558 /* There is no valid C++ program where a non-template type is
17559 followed by a "<". That usually indicates that the user
17560 thought that the type was a template. */
17561 cp_parser_check_for_invalid_template_id (parser
, type
,
17569 /* Parse a type-name.
17575 simple-template-id [in c++0x]
17592 Returns a TYPE_DECL for the type. */
17595 cp_parser_type_name (cp_parser
* parser
)
17597 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17602 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17606 /* We can't know yet whether it is a class-name or not. */
17607 cp_parser_parse_tentatively (parser
);
17608 /* Try a class-name. */
17609 type_decl
= cp_parser_class_name (parser
,
17610 typename_keyword_p
,
17611 /*template_keyword_p=*/false,
17613 /*check_dependency_p=*/true,
17614 /*class_head_p=*/false,
17615 /*is_declaration=*/false);
17616 /* If it's not a class-name, keep looking. */
17617 if (!cp_parser_parse_definitely (parser
))
17619 if (cxx_dialect
< cxx11
)
17620 /* It must be a typedef-name or an enum-name. */
17621 return cp_parser_nonclass_name (parser
);
17623 cp_parser_parse_tentatively (parser
);
17624 /* It is either a simple-template-id representing an
17625 instantiation of an alias template... */
17626 type_decl
= cp_parser_template_id (parser
,
17627 /*template_keyword_p=*/false,
17628 /*check_dependency_p=*/true,
17630 /*is_declaration=*/false);
17631 /* Note that this must be an instantiation of an alias template
17632 because [temp.names]/6 says:
17634 A template-id that names an alias template specialization
17637 Whereas [temp.names]/7 says:
17639 A simple-template-id that names a class template
17640 specialization is a class-name.
17642 With concepts, this could also be a partial-concept-id that
17643 declares a non-type template parameter. */
17644 if (type_decl
!= NULL_TREE
17645 && TREE_CODE (type_decl
) == TYPE_DECL
17646 && TYPE_DECL_ALIAS_P (type_decl
))
17647 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17648 else if (is_constrained_parameter (type_decl
))
17649 /* Don't do anything. */ ;
17651 cp_parser_simulate_error (parser
);
17653 if (!cp_parser_parse_definitely (parser
))
17654 /* ... Or a typedef-name or an enum-name. */
17655 return cp_parser_nonclass_name (parser
);
17661 /* Check if DECL and ARGS can form a constrained-type-specifier.
17662 If ARGS is non-null, we try to form a concept check of the
17663 form DECL<?, ARGS> where ? is a wildcard that matches any
17664 kind of template argument. If ARGS is NULL, then we try to
17665 form a concept check of the form DECL<?>. */
17668 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17669 tree decl
, tree args
)
17671 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17673 /* If we a constrained-type-specifier cannot be deduced. */
17674 if (parser
->prevent_constrained_type_specifiers
)
17677 /* A constrained type specifier can only be found in an
17678 overload set or as a reference to a template declaration.
17680 FIXME: This might be masking a bug. It's possible that
17681 that the deduction below is causing template specializations
17682 to be formed with the wildcard as an argument. */
17683 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17686 /* Try to build a call expression that evaluates the
17687 concept. This can fail if the overload set refers
17688 only to non-templates. */
17689 tree placeholder
= build_nt (WILDCARD_DECL
);
17690 tree check
= build_concept_check (decl
, placeholder
, args
);
17691 if (check
== error_mark_node
)
17694 /* Deduce the checked constraint and the prototype parameter.
17696 FIXME: In certain cases, failure to deduce should be a
17697 diagnosable error. */
17700 if (!deduce_constrained_parameter (check
, conc
, proto
))
17703 /* In template parameter scope, this results in a constrained
17704 parameter. Return a descriptor of that parm. */
17705 if (processing_template_parmlist
)
17706 return build_constrained_parameter (conc
, proto
, args
);
17708 /* In a parameter-declaration-clause, constrained-type
17709 specifiers result in invented template parameters. */
17710 if (parser
->auto_is_implicit_function_template_parm_p
)
17712 tree x
= build_constrained_parameter (conc
, proto
, args
);
17713 return synthesize_implicit_template_parm (parser
, x
);
17717 /* Otherwise, we're in a context where the constrained
17718 type name is deduced and the constraint applies
17719 after deduction. */
17720 return make_constrained_auto (conc
, args
);
17726 /* If DECL refers to a concept, return a TYPE_DECL representing
17727 the result of using the constrained type specifier in the
17728 current context. DECL refers to a concept if
17730 - it is an overload set containing a function concept taking a single
17733 - it is a variable concept taking a single type argument. */
17736 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17739 && (TREE_CODE (decl
) == OVERLOAD
17740 || BASELINK_P (decl
)
17741 || variable_concept_p (decl
)))
17742 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17747 /* Check if DECL and ARGS form a partial-concept-id. If so,
17748 assign ID to the resulting constrained placeholder.
17750 Returns true if the partial-concept-id designates a placeholder
17751 and false otherwise. Note that *id is set to NULL_TREE in
17755 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17757 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17760 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17772 Returns a TYPE_DECL for the type. */
17775 cp_parser_nonclass_name (cp_parser
* parser
)
17780 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17781 identifier
= cp_parser_identifier (parser
);
17782 if (identifier
== error_mark_node
)
17783 return error_mark_node
;
17785 /* Look up the type-name. */
17786 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17788 type_decl
= strip_using_decl (type_decl
);
17790 /* If we found an overload set, then it may refer to a concept-name. */
17791 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17794 if (TREE_CODE (type_decl
) != TYPE_DECL
17795 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17797 /* See if this is an Objective-C type. */
17798 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17799 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17801 type_decl
= TYPE_NAME (type
);
17804 /* Issue an error if we did not find a type-name. */
17805 if (TREE_CODE (type_decl
) != TYPE_DECL
17806 /* In Objective-C, we have the complication that class names are
17807 normally type names and start declarations (eg, the
17808 "NSObject" in "NSObject *object;"), but can be used in an
17809 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17810 is an expression. So, a classname followed by a dot is not a
17811 valid type-name. */
17812 || (objc_is_class_name (TREE_TYPE (type_decl
))
17813 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17815 if (!cp_parser_simulate_error (parser
))
17816 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17817 NLE_TYPE
, token
->location
);
17818 return error_mark_node
;
17820 /* Remember that the name was used in the definition of the
17821 current class so that we can check later to see if the
17822 meaning would have been different after the class was
17823 entirely defined. */
17824 else if (type_decl
!= error_mark_node
17826 maybe_note_name_used_in_class (identifier
, type_decl
);
17831 /* Parse an elaborated-type-specifier. Note that the grammar given
17832 here incorporates the resolution to DR68.
17834 elaborated-type-specifier:
17835 class-key :: [opt] nested-name-specifier [opt] identifier
17836 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17837 enum-key :: [opt] nested-name-specifier [opt] identifier
17838 typename :: [opt] nested-name-specifier identifier
17839 typename :: [opt] nested-name-specifier template [opt]
17844 elaborated-type-specifier:
17845 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17846 class-key attributes :: [opt] nested-name-specifier [opt]
17847 template [opt] template-id
17848 enum attributes :: [opt] nested-name-specifier [opt] identifier
17850 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17851 declared `friend'. If IS_DECLARATION is TRUE, then this
17852 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17853 something is being declared.
17855 Returns the TYPE specified. */
17858 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17860 bool is_declaration
)
17862 enum tag_types tag_type
;
17864 tree type
= NULL_TREE
;
17865 tree attributes
= NULL_TREE
;
17867 cp_token
*token
= NULL
;
17869 /* See if we're looking at the `enum' keyword. */
17870 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17872 /* Consume the `enum' token. */
17873 cp_lexer_consume_token (parser
->lexer
);
17874 /* Remember that it's an enumeration type. */
17875 tag_type
= enum_type
;
17876 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17877 enums) is used here. */
17878 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17879 if (cp_parser_is_keyword (token
, RID_CLASS
)
17880 || cp_parser_is_keyword (token
, RID_STRUCT
))
17882 gcc_rich_location
richloc (token
->location
);
17883 richloc
.add_range (input_location
);
17884 richloc
.add_fixit_remove ();
17885 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17886 "a scoped enum must not use the %qD keyword",
17888 /* Consume the `struct' or `class' and parse it anyway. */
17889 cp_lexer_consume_token (parser
->lexer
);
17891 /* Parse the attributes. */
17892 attributes
= cp_parser_attributes_opt (parser
);
17894 /* Or, it might be `typename'. */
17895 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17898 /* Consume the `typename' token. */
17899 cp_lexer_consume_token (parser
->lexer
);
17900 /* Remember that it's a `typename' type. */
17901 tag_type
= typename_type
;
17903 /* Otherwise it must be a class-key. */
17906 tag_type
= cp_parser_class_key (parser
);
17907 if (tag_type
== none_type
)
17908 return error_mark_node
;
17909 /* Parse the attributes. */
17910 attributes
= cp_parser_attributes_opt (parser
);
17913 /* Look for the `::' operator. */
17914 globalscope
= cp_parser_global_scope_opt (parser
,
17915 /*current_scope_valid_p=*/false);
17916 /* Look for the nested-name-specifier. */
17917 tree nested_name_specifier
;
17918 if (tag_type
== typename_type
&& !globalscope
)
17920 nested_name_specifier
17921 = cp_parser_nested_name_specifier (parser
,
17922 /*typename_keyword_p=*/true,
17923 /*check_dependency_p=*/true,
17926 if (!nested_name_specifier
)
17927 return error_mark_node
;
17930 /* Even though `typename' is not present, the proposed resolution
17931 to Core Issue 180 says that in `class A<T>::B', `B' should be
17932 considered a type-name, even if `A<T>' is dependent. */
17933 nested_name_specifier
17934 = cp_parser_nested_name_specifier_opt (parser
,
17935 /*typename_keyword_p=*/true,
17936 /*check_dependency_p=*/true,
17939 /* For everything but enumeration types, consider a template-id.
17940 For an enumeration type, consider only a plain identifier. */
17941 if (tag_type
!= enum_type
)
17943 bool template_p
= false;
17946 /* Allow the `template' keyword. */
17947 template_p
= cp_parser_optional_template_keyword (parser
);
17948 /* If we didn't see `template', we don't know if there's a
17949 template-id or not. */
17951 cp_parser_parse_tentatively (parser
);
17952 /* Parse the template-id. */
17953 token
= cp_lexer_peek_token (parser
->lexer
);
17954 decl
= cp_parser_template_id (parser
, template_p
,
17955 /*check_dependency_p=*/true,
17958 /* If we didn't find a template-id, look for an ordinary
17960 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17962 /* We can get here when cp_parser_template_id, called by
17963 cp_parser_class_name with tag_type == none_type, succeeds
17964 and caches a BASELINK. Then, when called again here,
17965 instead of failing and returning an error_mark_node
17966 returns it (see template/typename17.C in C++11).
17967 ??? Could we diagnose this earlier? */
17968 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17970 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17971 type
= error_mark_node
;
17973 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17974 in effect, then we must assume that, upon instantiation, the
17975 template will correspond to a class. */
17976 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17977 && tag_type
== typename_type
)
17978 type
= make_typename_type (parser
->scope
, decl
,
17980 /*complain=*/tf_error
);
17981 /* If the `typename' keyword is in effect and DECL is not a type
17982 decl, then type is non existent. */
17983 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17985 else if (TREE_CODE (decl
) == TYPE_DECL
)
17987 type
= check_elaborated_type_specifier (tag_type
, decl
,
17988 /*allow_template_p=*/true);
17990 /* If the next token is a semicolon, this must be a specialization,
17991 instantiation, or friend declaration. Check the scope while we
17992 still know whether or not we had a nested-name-specifier. */
17993 if (type
!= error_mark_node
17994 && !nested_name_specifier
&& !is_friend
17995 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17996 check_unqualified_spec_or_inst (type
, token
->location
);
17998 else if (decl
== error_mark_node
)
17999 type
= error_mark_node
;
18004 token
= cp_lexer_peek_token (parser
->lexer
);
18005 identifier
= cp_parser_identifier (parser
);
18007 if (identifier
== error_mark_node
)
18009 parser
->scope
= NULL_TREE
;
18010 return error_mark_node
;
18013 /* For a `typename', we needn't call xref_tag. */
18014 if (tag_type
== typename_type
18015 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18016 return cp_parser_make_typename_type (parser
, identifier
,
18019 /* Template parameter lists apply only if we are not within a
18020 function parameter list. */
18021 bool template_parm_lists_apply
18022 = parser
->num_template_parameter_lists
;
18023 if (template_parm_lists_apply
)
18024 for (cp_binding_level
*s
= current_binding_level
;
18025 s
&& s
->kind
!= sk_template_parms
;
18026 s
= s
->level_chain
)
18027 if (s
->kind
== sk_function_parms
)
18028 template_parm_lists_apply
= false;
18030 /* Look up a qualified name in the usual way. */
18034 tree ambiguous_decls
;
18036 decl
= cp_parser_lookup_name (parser
, identifier
,
18038 /*is_template=*/false,
18039 /*is_namespace=*/false,
18040 /*check_dependency=*/true,
18044 /* If the lookup was ambiguous, an error will already have been
18046 if (ambiguous_decls
)
18047 return error_mark_node
;
18049 /* If we are parsing friend declaration, DECL may be a
18050 TEMPLATE_DECL tree node here. However, we need to check
18051 whether this TEMPLATE_DECL results in valid code. Consider
18052 the following example:
18055 template <class T> class C {};
18058 template <class T> friend class N::C; // #1, valid code
18060 template <class T> class Y {
18061 friend class N::C; // #2, invalid code
18064 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18065 name lookup of `N::C'. We see that friend declaration must
18066 be template for the code to be valid. Note that
18067 processing_template_decl does not work here since it is
18068 always 1 for the above two cases. */
18070 decl
= (cp_parser_maybe_treat_template_as_class
18071 (decl
, /*tag_name_p=*/is_friend
18072 && template_parm_lists_apply
));
18074 if (TREE_CODE (decl
) != TYPE_DECL
)
18076 cp_parser_diagnose_invalid_type_name (parser
,
18079 return error_mark_node
;
18082 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18084 bool allow_template
= (template_parm_lists_apply
18085 || DECL_SELF_REFERENCE_P (decl
));
18086 type
= check_elaborated_type_specifier (tag_type
, decl
,
18089 if (type
== error_mark_node
)
18090 return error_mark_node
;
18093 /* Forward declarations of nested types, such as
18098 are invalid unless all components preceding the final '::'
18099 are complete. If all enclosing types are complete, these
18100 declarations become merely pointless.
18102 Invalid forward declarations of nested types are errors
18103 caught elsewhere in parsing. Those that are pointless arrive
18106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18107 && !is_friend
&& !processing_explicit_instantiation
)
18108 warning (0, "declaration %qD does not declare anything", decl
);
18110 type
= TREE_TYPE (decl
);
18114 /* An elaborated-type-specifier sometimes introduces a new type and
18115 sometimes names an existing type. Normally, the rule is that it
18116 introduces a new type only if there is not an existing type of
18117 the same name already in scope. For example, given:
18120 void f() { struct S s; }
18122 the `struct S' in the body of `f' is the same `struct S' as in
18123 the global scope; the existing definition is used. However, if
18124 there were no global declaration, this would introduce a new
18125 local class named `S'.
18127 An exception to this rule applies to the following code:
18129 namespace N { struct S; }
18131 Here, the elaborated-type-specifier names a new type
18132 unconditionally; even if there is already an `S' in the
18133 containing scope this declaration names a new type.
18134 This exception only applies if the elaborated-type-specifier
18135 forms the complete declaration:
18139 A declaration consisting solely of `class-key identifier ;' is
18140 either a redeclaration of the name in the current scope or a
18141 forward declaration of the identifier as a class name. It
18142 introduces the name into the current scope.
18144 We are in this situation precisely when the next token is a `;'.
18146 An exception to the exception is that a `friend' declaration does
18147 *not* name a new type; i.e., given:
18149 struct S { friend struct T; };
18151 `T' is not a new type in the scope of `S'.
18153 Also, `new struct S' or `sizeof (struct S)' never results in the
18154 definition of a new type; a new type can only be declared in a
18155 declaration context. */
18161 /* Friends have special name lookup rules. */
18162 ts
= ts_within_enclosing_non_class
;
18163 else if (is_declaration
18164 && cp_lexer_next_token_is (parser
->lexer
,
18166 /* This is a `class-key identifier ;' */
18172 (template_parm_lists_apply
18173 && (cp_parser_next_token_starts_class_definition_p (parser
)
18174 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18175 /* An unqualified name was used to reference this type, so
18176 there were no qualifying templates. */
18177 if (template_parm_lists_apply
18178 && !cp_parser_check_template_parameters (parser
,
18179 /*num_templates=*/0,
18180 /*template_id*/false,
18182 /*declarator=*/NULL
))
18183 return error_mark_node
;
18184 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18188 if (type
== error_mark_node
)
18189 return error_mark_node
;
18191 /* Allow attributes on forward declarations of classes. */
18194 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18195 warning (OPT_Wattributes
,
18196 "attributes ignored on uninstantiated type");
18197 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18198 && ! processing_explicit_instantiation
)
18199 warning (OPT_Wattributes
,
18200 "attributes ignored on template instantiation");
18201 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18202 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18204 warning (OPT_Wattributes
,
18205 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18208 if (tag_type
!= enum_type
)
18210 /* Indicate whether this class was declared as a `class' or as a
18212 if (CLASS_TYPE_P (type
))
18213 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18214 cp_parser_check_class_key (tag_type
, type
);
18217 /* A "<" cannot follow an elaborated type specifier. If that
18218 happens, the user was probably trying to form a template-id. */
18219 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18225 /* Parse an enum-specifier.
18228 enum-head { enumerator-list [opt] }
18229 enum-head { enumerator-list , } [C++0x]
18232 enum-key identifier [opt] enum-base [opt]
18233 enum-key nested-name-specifier identifier enum-base [opt]
18238 enum struct [C++0x]
18241 : type-specifier-seq
18243 opaque-enum-specifier:
18244 enum-key identifier enum-base [opt] ;
18247 enum-key attributes[opt] identifier [opt] enum-base [opt]
18248 { enumerator-list [opt] }attributes[opt]
18249 enum-key attributes[opt] identifier [opt] enum-base [opt]
18250 { enumerator-list, }attributes[opt] [C++0x]
18252 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18253 if the token stream isn't an enum-specifier after all. */
18256 cp_parser_enum_specifier (cp_parser
* parser
)
18259 tree type
= NULL_TREE
;
18261 tree nested_name_specifier
= NULL_TREE
;
18263 bool scoped_enum_p
= false;
18264 bool has_underlying_type
= false;
18265 bool nested_being_defined
= false;
18266 bool new_value_list
= false;
18267 bool is_new_type
= false;
18268 bool is_unnamed
= false;
18269 tree underlying_type
= NULL_TREE
;
18270 cp_token
*type_start_token
= NULL
;
18271 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18273 parser
->colon_corrects_to_scope_p
= false;
18275 /* Parse tentatively so that we can back up if we don't find a
18277 cp_parser_parse_tentatively (parser
);
18279 /* Caller guarantees that the current token is 'enum', an identifier
18280 possibly follows, and the token after that is an opening brace.
18281 If we don't have an identifier, fabricate an anonymous name for
18282 the enumeration being defined. */
18283 cp_lexer_consume_token (parser
->lexer
);
18285 /* Parse the "class" or "struct", which indicates a scoped
18286 enumeration type in C++0x. */
18287 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18288 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18290 if (cxx_dialect
< cxx11
)
18291 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18293 /* Consume the `struct' or `class' token. */
18294 cp_lexer_consume_token (parser
->lexer
);
18296 scoped_enum_p
= true;
18299 attributes
= cp_parser_attributes_opt (parser
);
18301 /* Clear the qualification. */
18302 parser
->scope
= NULL_TREE
;
18303 parser
->qualifying_scope
= NULL_TREE
;
18304 parser
->object_scope
= NULL_TREE
;
18306 /* Figure out in what scope the declaration is being placed. */
18307 prev_scope
= current_scope ();
18309 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18311 push_deferring_access_checks (dk_no_check
);
18312 nested_name_specifier
18313 = cp_parser_nested_name_specifier_opt (parser
,
18314 /*typename_keyword_p=*/true,
18315 /*check_dependency_p=*/false,
18317 /*is_declaration=*/false);
18319 if (nested_name_specifier
)
18323 identifier
= cp_parser_identifier (parser
);
18324 name
= cp_parser_lookup_name (parser
, identifier
,
18326 /*is_template=*/false,
18327 /*is_namespace=*/false,
18328 /*check_dependency=*/true,
18329 /*ambiguous_decls=*/NULL
,
18331 if (name
&& name
!= error_mark_node
)
18333 type
= TREE_TYPE (name
);
18334 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18336 /* Are template enums allowed in ISO? */
18337 if (template_parm_scope_p ())
18338 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18339 "%qD is an enumeration template", name
);
18340 /* ignore a typename reference, for it will be solved by name
18345 else if (nested_name_specifier
== error_mark_node
)
18346 /* We already issued an error. */;
18349 error_at (type_start_token
->location
,
18350 "%qD does not name an enumeration in %qT",
18351 identifier
, nested_name_specifier
);
18352 nested_name_specifier
= error_mark_node
;
18357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18358 identifier
= cp_parser_identifier (parser
);
18361 identifier
= make_anon_name ();
18364 error_at (type_start_token
->location
,
18365 "unnamed scoped enum is not allowed");
18368 pop_deferring_access_checks ();
18370 /* Check for the `:' that denotes a specified underlying type in C++0x.
18371 Note that a ':' could also indicate a bitfield width, however. */
18372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18374 cp_decl_specifier_seq type_specifiers
;
18376 /* Consume the `:'. */
18377 cp_lexer_consume_token (parser
->lexer
);
18379 /* Parse the type-specifier-seq. */
18380 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18381 /*is_trailing_return=*/false,
18384 /* At this point this is surely not elaborated type specifier. */
18385 if (!cp_parser_parse_definitely (parser
))
18388 if (cxx_dialect
< cxx11
)
18389 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18391 has_underlying_type
= true;
18393 /* If that didn't work, stop. */
18394 if (type_specifiers
.type
!= error_mark_node
)
18396 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18397 /*initialized=*/0, NULL
);
18398 if (underlying_type
== error_mark_node
18399 || check_for_bare_parameter_packs (underlying_type
))
18400 underlying_type
= NULL_TREE
;
18404 /* Look for the `{' but don't consume it yet. */
18405 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18407 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18409 cp_parser_error (parser
, "expected %<{%>");
18410 if (has_underlying_type
)
18416 /* An opaque-enum-specifier must have a ';' here. */
18417 if ((scoped_enum_p
|| underlying_type
)
18418 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18420 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18421 if (has_underlying_type
)
18429 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18432 if (nested_name_specifier
)
18434 if (CLASS_TYPE_P (nested_name_specifier
))
18436 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18437 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18438 push_scope (nested_name_specifier
);
18440 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18442 push_nested_namespace (nested_name_specifier
);
18446 /* Issue an error message if type-definitions are forbidden here. */
18447 if (!cp_parser_check_type_definition (parser
))
18448 type
= error_mark_node
;
18450 /* Create the new type. We do this before consuming the opening
18451 brace so the enum will be recorded as being on the line of its
18452 tag (or the 'enum' keyword, if there is no tag). */
18453 type
= start_enum (identifier
, type
, underlying_type
,
18454 attributes
, scoped_enum_p
, &is_new_type
);
18456 /* If the next token is not '{' it is an opaque-enum-specifier or an
18457 elaborated-type-specifier. */
18458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18460 timevar_push (TV_PARSE_ENUM
);
18461 if (nested_name_specifier
18462 && nested_name_specifier
!= error_mark_node
)
18464 /* The following catches invalid code such as:
18465 enum class S<int>::E { A, B, C }; */
18466 if (!processing_specialization
18467 && CLASS_TYPE_P (nested_name_specifier
)
18468 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18469 error_at (type_start_token
->location
, "cannot add an enumerator "
18470 "list to a template instantiation");
18472 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18474 error_at (type_start_token
->location
,
18475 "%<%T::%E%> has not been declared",
18476 TYPE_CONTEXT (nested_name_specifier
),
18477 nested_name_specifier
);
18478 type
= error_mark_node
;
18480 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18481 && !CLASS_TYPE_P (nested_name_specifier
))
18483 error_at (type_start_token
->location
, "nested name specifier "
18484 "%qT for enum declaration does not name a class "
18485 "or namespace", nested_name_specifier
);
18486 type
= error_mark_node
;
18488 /* If that scope does not contain the scope in which the
18489 class was originally declared, the program is invalid. */
18490 else if (prev_scope
&& !is_ancestor (prev_scope
,
18491 nested_name_specifier
))
18493 if (at_namespace_scope_p ())
18494 error_at (type_start_token
->location
,
18495 "declaration of %qD in namespace %qD which does not "
18497 type
, prev_scope
, nested_name_specifier
);
18499 error_at (type_start_token
->location
,
18500 "declaration of %qD in %qD which does not "
18502 type
, prev_scope
, nested_name_specifier
);
18503 type
= error_mark_node
;
18505 /* If that scope is the scope where the declaration is being placed
18506 the program is invalid. */
18507 else if (CLASS_TYPE_P (nested_name_specifier
)
18508 && CLASS_TYPE_P (prev_scope
)
18509 && same_type_p (nested_name_specifier
, prev_scope
))
18511 permerror (type_start_token
->location
,
18512 "extra qualification not allowed");
18513 nested_name_specifier
= NULL_TREE
;
18518 begin_scope (sk_scoped_enum
, type
);
18520 /* Consume the opening brace. */
18521 matching_braces braces
;
18522 braces
.consume_open (parser
);
18524 if (type
== error_mark_node
)
18525 ; /* Nothing to add */
18526 else if (OPAQUE_ENUM_P (type
)
18527 || (cxx_dialect
> cxx98
&& processing_specialization
))
18529 new_value_list
= true;
18530 SET_OPAQUE_ENUM_P (type
, false);
18531 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18535 error_at (type_start_token
->location
,
18536 "multiple definition of %q#T", type
);
18537 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18538 "previous definition here");
18539 type
= error_mark_node
;
18542 if (type
== error_mark_node
)
18543 cp_parser_skip_to_end_of_block_or_statement (parser
);
18544 /* If the next token is not '}', then there are some enumerators. */
18545 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18547 if (is_unnamed
&& !scoped_enum_p
)
18548 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18549 "ISO C++ forbids empty unnamed enum");
18552 cp_parser_enumerator_list (parser
, type
);
18554 /* Consume the final '}'. */
18555 braces
.require_close (parser
);
18559 timevar_pop (TV_PARSE_ENUM
);
18563 /* If a ';' follows, then it is an opaque-enum-specifier
18564 and additional restrictions apply. */
18565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18568 error_at (type_start_token
->location
,
18569 "opaque-enum-specifier without name");
18570 else if (nested_name_specifier
)
18571 error_at (type_start_token
->location
,
18572 "opaque-enum-specifier must use a simple identifier");
18576 /* Look for trailing attributes to apply to this enumeration, and
18577 apply them if appropriate. */
18578 if (cp_parser_allow_gnu_extensions_p (parser
))
18580 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18581 cplus_decl_attributes (&type
,
18583 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18586 /* Finish up the enumeration. */
18587 if (type
!= error_mark_node
)
18589 if (new_value_list
)
18590 finish_enum_value_list (type
);
18592 finish_enum (type
);
18595 if (nested_name_specifier
)
18597 if (CLASS_TYPE_P (nested_name_specifier
))
18599 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18600 pop_scope (nested_name_specifier
);
18602 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18604 pop_nested_namespace (nested_name_specifier
);
18608 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18612 /* Parse an enumerator-list. The enumerators all have the indicated
18616 enumerator-definition
18617 enumerator-list , enumerator-definition */
18620 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18624 /* Parse an enumerator-definition. */
18625 cp_parser_enumerator_definition (parser
, type
);
18627 /* If the next token is not a ',', we've reached the end of
18629 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18631 /* Otherwise, consume the `,' and keep going. */
18632 cp_lexer_consume_token (parser
->lexer
);
18633 /* If the next token is a `}', there is a trailing comma. */
18634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18636 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18637 pedwarn (input_location
, OPT_Wpedantic
,
18638 "comma at end of enumerator list");
18644 /* Parse an enumerator-definition. The enumerator has the indicated
18647 enumerator-definition:
18649 enumerator = constant-expression
18656 enumerator-definition:
18657 enumerator attributes [opt]
18658 enumerator attributes [opt] = constant-expression */
18661 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18667 /* Save the input location because we are interested in the location
18668 of the identifier and not the location of the explicit value. */
18669 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18671 /* Look for the identifier. */
18672 identifier
= cp_parser_identifier (parser
);
18673 if (identifier
== error_mark_node
)
18676 /* Parse any specified attributes. */
18677 tree attrs
= cp_parser_attributes_opt (parser
);
18679 /* If the next token is an '=', then there is an explicit value. */
18680 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18682 /* Consume the `=' token. */
18683 cp_lexer_consume_token (parser
->lexer
);
18684 /* Parse the value. */
18685 value
= cp_parser_constant_expression (parser
);
18690 /* If we are processing a template, make sure the initializer of the
18691 enumerator doesn't contain any bare template parameter pack. */
18692 if (check_for_bare_parameter_packs (value
))
18693 value
= error_mark_node
;
18695 /* Create the enumerator. */
18696 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18699 /* Parse a namespace-name.
18702 original-namespace-name
18705 Returns the NAMESPACE_DECL for the namespace. */
18708 cp_parser_namespace_name (cp_parser
* parser
)
18711 tree namespace_decl
;
18713 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18715 /* Get the name of the namespace. */
18716 identifier
= cp_parser_identifier (parser
);
18717 if (identifier
== error_mark_node
)
18718 return error_mark_node
;
18720 /* Look up the identifier in the currently active scope. Look only
18721 for namespaces, due to:
18723 [basic.lookup.udir]
18725 When looking up a namespace-name in a using-directive or alias
18726 definition, only namespace names are considered.
18730 [basic.lookup.qual]
18732 During the lookup of a name preceding the :: scope resolution
18733 operator, object, function, and enumerator names are ignored.
18735 (Note that cp_parser_qualifying_entity only calls this
18736 function if the token after the name is the scope resolution
18738 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18740 /*is_template=*/false,
18741 /*is_namespace=*/true,
18742 /*check_dependency=*/true,
18743 /*ambiguous_decls=*/NULL
,
18745 /* If it's not a namespace, issue an error. */
18746 if (namespace_decl
== error_mark_node
18747 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18749 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18751 auto_diagnostic_group d
;
18753 if (namespace_decl
== error_mark_node
18754 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18755 hint
= suggest_alternative_in_explicit_scope (token
->location
,
18758 if (const char *suggestion
= hint
.suggestion ())
18760 gcc_rich_location
richloc (token
->location
);
18761 richloc
.add_fixit_replace (suggestion
);
18762 error_at (&richloc
,
18763 "%qD is not a namespace-name; did you mean %qs?",
18764 identifier
, suggestion
);
18767 error_at (token
->location
, "%qD is not a namespace-name",
18771 cp_parser_error (parser
, "expected namespace-name");
18772 namespace_decl
= error_mark_node
;
18775 return namespace_decl
;
18778 /* Parse a namespace-definition.
18780 namespace-definition:
18781 named-namespace-definition
18782 unnamed-namespace-definition
18784 named-namespace-definition:
18785 original-namespace-definition
18786 extension-namespace-definition
18788 original-namespace-definition:
18789 namespace identifier { namespace-body }
18791 extension-namespace-definition:
18792 namespace original-namespace-name { namespace-body }
18794 unnamed-namespace-definition:
18795 namespace { namespace-body } */
18798 cp_parser_namespace_definition (cp_parser
* parser
)
18801 int nested_definition_count
= 0;
18803 cp_ensure_no_omp_declare_simd (parser
);
18804 cp_ensure_no_oacc_routine (parser
);
18806 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18810 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18811 cp_lexer_consume_token (parser
->lexer
);
18814 /* Look for the `namespace' keyword. */
18816 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18818 /* Parse any specified attributes before the identifier. */
18819 tree attribs
= cp_parser_attributes_opt (parser
);
18823 identifier
= NULL_TREE
;
18825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18827 identifier
= cp_parser_identifier (parser
);
18829 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18830 pedwarn (input_location
, OPT_Wpedantic
,
18831 "standard attributes on namespaces must precede "
18832 "the namespace name");
18834 /* Parse any attributes specified after the identifier. */
18835 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18838 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18841 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18842 pedwarn (input_location
, OPT_Wpedantic
,
18843 "nested namespace definitions only available with "
18844 "-std=c++17 or -std=gnu++17");
18846 /* Nested namespace names can create new namespaces (unlike
18847 other qualified-ids). */
18848 if (int count
= identifier
? push_namespace (identifier
) : 0)
18849 nested_definition_count
+= count
;
18851 cp_parser_error (parser
, "nested namespace name required");
18852 cp_lexer_consume_token (parser
->lexer
);
18855 if (nested_definition_count
&& !identifier
)
18856 cp_parser_error (parser
, "namespace name required");
18858 if (nested_definition_count
&& attribs
)
18859 error_at (token
->location
,
18860 "a nested namespace definition cannot have attributes");
18861 if (nested_definition_count
&& is_inline
)
18862 error_at (token
->location
,
18863 "a nested namespace definition cannot be inline");
18865 /* Start the namespace. */
18866 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18868 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18870 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18872 /* Look for the `{' to validate starting the namespace. */
18873 matching_braces braces
;
18874 if (braces
.require_open (parser
))
18876 /* Parse the body of the namespace. */
18877 cp_parser_namespace_body (parser
);
18879 /* Look for the final `}'. */
18880 braces
.require_close (parser
);
18883 if (has_visibility
)
18884 pop_visibility (1);
18886 /* Pop the nested namespace definitions. */
18887 while (nested_definition_count
--)
18891 /* Parse a namespace-body.
18894 declaration-seq [opt] */
18897 cp_parser_namespace_body (cp_parser
* parser
)
18899 cp_parser_declaration_seq_opt (parser
);
18902 /* Parse a namespace-alias-definition.
18904 namespace-alias-definition:
18905 namespace identifier = qualified-namespace-specifier ; */
18908 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18911 tree namespace_specifier
;
18913 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18915 /* Look for the `namespace' keyword. */
18916 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18917 /* Look for the identifier. */
18918 identifier
= cp_parser_identifier (parser
);
18919 if (identifier
== error_mark_node
)
18921 /* Look for the `=' token. */
18922 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18923 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18925 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18926 /* Skip the definition. */
18927 cp_lexer_consume_token (parser
->lexer
);
18928 if (cp_parser_skip_to_closing_brace (parser
))
18929 cp_lexer_consume_token (parser
->lexer
);
18932 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18933 /* Look for the qualified-namespace-specifier. */
18934 namespace_specifier
18935 = cp_parser_qualified_namespace_specifier (parser
);
18936 /* Look for the `;' token. */
18937 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18939 /* Register the alias in the symbol table. */
18940 do_namespace_alias (identifier
, namespace_specifier
);
18943 /* Parse a qualified-namespace-specifier.
18945 qualified-namespace-specifier:
18946 :: [opt] nested-name-specifier [opt] namespace-name
18948 Returns a NAMESPACE_DECL corresponding to the specified
18952 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18954 /* Look for the optional `::'. */
18955 cp_parser_global_scope_opt (parser
,
18956 /*current_scope_valid_p=*/false);
18958 /* Look for the optional nested-name-specifier. */
18959 cp_parser_nested_name_specifier_opt (parser
,
18960 /*typename_keyword_p=*/false,
18961 /*check_dependency_p=*/true,
18963 /*is_declaration=*/true);
18965 return cp_parser_namespace_name (parser
);
18968 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18969 access declaration.
18972 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18973 using :: unqualified-id ;
18975 access-declaration:
18981 cp_parser_using_declaration (cp_parser
* parser
,
18982 bool access_declaration_p
)
18985 bool typename_p
= false;
18986 bool global_scope_p
;
18990 int oldcount
= errorcount
;
18991 cp_token
*diag_token
= NULL
;
18993 if (access_declaration_p
)
18995 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18996 cp_parser_parse_tentatively (parser
);
19000 /* Look for the `using' keyword. */
19001 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19004 /* Peek at the next token. */
19005 token
= cp_lexer_peek_token (parser
->lexer
);
19006 /* See if it's `typename'. */
19007 if (token
->keyword
== RID_TYPENAME
)
19009 /* Remember that we've seen it. */
19011 /* Consume the `typename' token. */
19012 cp_lexer_consume_token (parser
->lexer
);
19016 /* Look for the optional global scope qualification. */
19018 = (cp_parser_global_scope_opt (parser
,
19019 /*current_scope_valid_p=*/false)
19022 /* If we saw `typename', or didn't see `::', then there must be a
19023 nested-name-specifier present. */
19024 if (typename_p
|| !global_scope_p
)
19026 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19027 /*check_dependency_p=*/true,
19029 /*is_declaration=*/true);
19030 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19032 cp_parser_skip_to_end_of_block_or_statement (parser
);
19036 /* Otherwise, we could be in either of the two productions. In that
19037 case, treat the nested-name-specifier as optional. */
19039 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19040 /*typename_keyword_p=*/false,
19041 /*check_dependency_p=*/true,
19043 /*is_declaration=*/true);
19045 qscope
= global_namespace
;
19046 else if (UNSCOPED_ENUM_P (qscope
))
19047 qscope
= CP_TYPE_CONTEXT (qscope
);
19049 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19050 /* Something has already gone wrong; there's no need to parse
19051 further. Since an error has occurred, the return value of
19052 cp_parser_parse_definitely will be false, as required. */
19053 return cp_parser_parse_definitely (parser
);
19055 token
= cp_lexer_peek_token (parser
->lexer
);
19056 /* Parse the unqualified-id. */
19057 identifier
= cp_parser_unqualified_id (parser
,
19058 /*template_keyword_p=*/false,
19059 /*check_dependency_p=*/true,
19060 /*declarator_p=*/true,
19061 /*optional_p=*/false);
19063 if (access_declaration_p
)
19065 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19066 cp_parser_simulate_error (parser
);
19067 if (!cp_parser_parse_definitely (parser
))
19070 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19072 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19073 if (cxx_dialect
< cxx17
19074 && !in_system_header_at (ell
->location
))
19075 pedwarn (ell
->location
, 0,
19076 "pack expansion in using-declaration only available "
19077 "with -std=c++17 or -std=gnu++17");
19078 qscope
= make_pack_expansion (qscope
);
19081 /* The function we call to handle a using-declaration is different
19082 depending on what scope we are in. */
19083 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19085 else if (!identifier_p (identifier
)
19086 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19087 /* [namespace.udecl]
19089 A using declaration shall not name a template-id. */
19090 error_at (token
->location
,
19091 "a template-id may not appear in a using-declaration");
19094 if (at_class_scope_p ())
19096 /* Create the USING_DECL. */
19097 decl
= do_class_using_decl (qscope
, identifier
);
19099 if (decl
&& typename_p
)
19100 USING_DECL_TYPENAME_P (decl
) = 1;
19102 if (check_for_bare_parameter_packs (decl
))
19104 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19108 /* Add it to the list of members in this class. */
19109 finish_member_declaration (decl
);
19113 decl
= cp_parser_lookup_name_simple (parser
,
19116 if (decl
== error_mark_node
)
19117 cp_parser_name_lookup_error (parser
, identifier
,
19120 else if (check_for_bare_parameter_packs (decl
))
19122 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19125 else if (!at_namespace_scope_p ())
19126 finish_local_using_decl (decl
, qscope
, identifier
);
19128 finish_namespace_using_decl (decl
, qscope
, identifier
);
19132 if (!access_declaration_p
19133 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19135 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19136 if (cxx_dialect
< cxx17
)
19137 pedwarn (comma
->location
, 0,
19138 "comma-separated list in using-declaration only available "
19139 "with -std=c++17 or -std=gnu++17");
19143 /* Look for the final `;'. */
19144 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19146 if (access_declaration_p
&& errorcount
== oldcount
)
19147 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19148 "access declarations are deprecated "
19149 "in favour of using-declarations; "
19150 "suggestion: add the %<using%> keyword");
19155 /* Parse an alias-declaration.
19158 using identifier attribute-specifier-seq [opt] = type-id */
19161 cp_parser_alias_declaration (cp_parser
* parser
)
19163 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19164 location_t id_location
, type_location
;
19165 cp_declarator
*declarator
;
19166 cp_decl_specifier_seq decl_specs
;
19168 const char *saved_message
= NULL
;
19170 /* Look for the `using' keyword. */
19171 cp_token
*using_token
19172 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19173 if (using_token
== NULL
)
19174 return error_mark_node
;
19176 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19177 id
= cp_parser_identifier (parser
);
19178 if (id
== error_mark_node
)
19179 return error_mark_node
;
19181 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19182 attributes
= cp_parser_attributes_opt (parser
);
19183 if (attributes
== error_mark_node
)
19184 return error_mark_node
;
19186 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19188 if (cp_parser_error_occurred (parser
))
19189 return error_mark_node
;
19191 cp_parser_commit_to_tentative_parse (parser
);
19193 /* Now we are going to parse the type-id of the declaration. */
19198 "A type-specifier-seq shall not define a class or enumeration
19199 unless it appears in the type-id of an alias-declaration (7.1.3) that
19200 is not the declaration of a template-declaration."
19202 In other words, if we currently are in an alias template, the
19203 type-id should not define a type.
19205 So let's set parser->type_definition_forbidden_message in that
19206 case; cp_parser_check_type_definition (called by
19207 cp_parser_class_specifier) will then emit an error if a type is
19208 defined in the type-id. */
19209 if (parser
->num_template_parameter_lists
)
19211 saved_message
= parser
->type_definition_forbidden_message
;
19212 parser
->type_definition_forbidden_message
=
19213 G_("types may not be defined in alias template declarations");
19216 type
= cp_parser_type_id (parser
, &type_location
);
19218 /* Restore the error message if need be. */
19219 if (parser
->num_template_parameter_lists
)
19220 parser
->type_definition_forbidden_message
= saved_message
;
19222 if (type
== error_mark_node
19223 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19225 cp_parser_skip_to_end_of_block_or_statement (parser
);
19226 return error_mark_node
;
19229 /* A typedef-name can also be introduced by an alias-declaration. The
19230 identifier following the using keyword becomes a typedef-name. It has
19231 the same semantics as if it were introduced by the typedef
19232 specifier. In particular, it does not define a new type and it shall
19233 not appear in the type-id. */
19235 clear_decl_specs (&decl_specs
);
19236 decl_specs
.type
= type
;
19237 if (attributes
!= NULL_TREE
)
19239 decl_specs
.attributes
= attributes
;
19240 set_and_check_decl_spec_loc (&decl_specs
,
19244 set_and_check_decl_spec_loc (&decl_specs
,
19247 set_and_check_decl_spec_loc (&decl_specs
,
19250 decl_specs
.locations
[ds_type_spec
] = type_location
;
19252 if (parser
->num_template_parameter_lists
19253 && !cp_parser_check_template_parameters (parser
,
19254 /*num_templates=*/0,
19255 /*template_id*/false,
19257 /*declarator=*/NULL
))
19258 return error_mark_node
;
19260 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19261 declarator
->id_loc
= id_location
;
19263 member_p
= at_class_scope_p ();
19265 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19266 NULL_TREE
, attributes
);
19268 decl
= start_decl (declarator
, &decl_specs
, 0,
19269 attributes
, NULL_TREE
, &pushed_scope
);
19270 if (decl
== error_mark_node
)
19273 // Attach constraints to the alias declaration.
19274 if (flag_concepts
&& current_template_parms
)
19276 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19277 tree constr
= build_constraints (reqs
, NULL_TREE
);
19278 set_constraints (decl
, constr
);
19281 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19284 pop_scope (pushed_scope
);
19286 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19287 added into the symbol table; otherwise, return the TYPE_DECL. */
19288 if (DECL_LANG_SPECIFIC (decl
)
19289 && DECL_TEMPLATE_INFO (decl
)
19290 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19292 decl
= DECL_TI_TEMPLATE (decl
);
19294 check_member_template (decl
);
19300 /* Parse a using-directive.
19303 using namespace :: [opt] nested-name-specifier [opt]
19304 namespace-name ; */
19307 cp_parser_using_directive (cp_parser
* parser
)
19309 tree namespace_decl
;
19312 /* Look for the `using' keyword. */
19313 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19314 /* And the `namespace' keyword. */
19315 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19316 /* Look for the optional `::' operator. */
19317 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19318 /* And the optional nested-name-specifier. */
19319 cp_parser_nested_name_specifier_opt (parser
,
19320 /*typename_keyword_p=*/false,
19321 /*check_dependency_p=*/true,
19323 /*is_declaration=*/true);
19324 /* Get the namespace being used. */
19325 namespace_decl
= cp_parser_namespace_name (parser
);
19326 /* And any specified attributes. */
19327 attribs
= cp_parser_attributes_opt (parser
);
19329 /* Update the symbol table. */
19330 if (namespace_bindings_p ())
19331 finish_namespace_using_directive (namespace_decl
, attribs
);
19333 finish_local_using_directive (namespace_decl
, attribs
);
19335 /* Look for the final `;'. */
19336 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19339 /* Parse an asm-definition.
19342 asm ( string-literal ) ;
19347 asm volatile [opt] ( string-literal ) ;
19348 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19349 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19350 : asm-operand-list [opt] ) ;
19351 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19352 : asm-operand-list [opt]
19353 : asm-clobber-list [opt] ) ;
19354 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19355 : asm-clobber-list [opt]
19356 : asm-goto-list ) ; */
19359 cp_parser_asm_definition (cp_parser
* parser
)
19362 tree outputs
= NULL_TREE
;
19363 tree inputs
= NULL_TREE
;
19364 tree clobbers
= NULL_TREE
;
19365 tree labels
= NULL_TREE
;
19367 bool volatile_p
= false;
19368 bool extended_p
= false;
19369 bool invalid_inputs_p
= false;
19370 bool invalid_outputs_p
= false;
19371 bool goto_p
= false;
19372 required_token missing
= RT_NONE
;
19374 /* Look for the `asm' keyword. */
19375 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19377 if (parser
->in_function_body
19378 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19380 error ("%<asm%> in %<constexpr%> function");
19381 cp_function_chain
->invalid_constexpr
= true;
19384 /* See if the next token is `volatile'. */
19385 if (cp_parser_allow_gnu_extensions_p (parser
)
19386 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19388 /* Remember that we saw the `volatile' keyword. */
19390 /* Consume the token. */
19391 cp_lexer_consume_token (parser
->lexer
);
19393 if (cp_parser_allow_gnu_extensions_p (parser
)
19394 && parser
->in_function_body
19395 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19397 /* Remember that we saw the `goto' keyword. */
19399 /* Consume the token. */
19400 cp_lexer_consume_token (parser
->lexer
);
19402 /* Look for the opening `('. */
19403 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19405 /* Look for the string. */
19406 string
= cp_parser_string_literal (parser
, false, false);
19407 if (string
== error_mark_node
)
19409 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19410 /*consume_paren=*/true);
19414 /* If we're allowing GNU extensions, check for the extended assembly
19415 syntax. Unfortunately, the `:' tokens need not be separated by
19416 a space in C, and so, for compatibility, we tolerate that here
19417 too. Doing that means that we have to treat the `::' operator as
19419 if (cp_parser_allow_gnu_extensions_p (parser
)
19420 && parser
->in_function_body
19421 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19422 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19424 bool inputs_p
= false;
19425 bool clobbers_p
= false;
19426 bool labels_p
= false;
19428 /* The extended syntax was used. */
19431 /* Look for outputs. */
19432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19434 /* Consume the `:'. */
19435 cp_lexer_consume_token (parser
->lexer
);
19436 /* Parse the output-operands. */
19437 if (cp_lexer_next_token_is_not (parser
->lexer
,
19439 && cp_lexer_next_token_is_not (parser
->lexer
,
19441 && cp_lexer_next_token_is_not (parser
->lexer
,
19445 outputs
= cp_parser_asm_operand_list (parser
);
19446 if (outputs
== error_mark_node
)
19447 invalid_outputs_p
= true;
19450 /* If the next token is `::', there are no outputs, and the
19451 next token is the beginning of the inputs. */
19452 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19453 /* The inputs are coming next. */
19456 /* Look for inputs. */
19458 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19460 /* Consume the `:' or `::'. */
19461 cp_lexer_consume_token (parser
->lexer
);
19462 /* Parse the output-operands. */
19463 if (cp_lexer_next_token_is_not (parser
->lexer
,
19465 && cp_lexer_next_token_is_not (parser
->lexer
,
19467 && cp_lexer_next_token_is_not (parser
->lexer
,
19470 inputs
= cp_parser_asm_operand_list (parser
);
19471 if (inputs
== error_mark_node
)
19472 invalid_inputs_p
= true;
19475 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19476 /* The clobbers are coming next. */
19479 /* Look for clobbers. */
19481 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19484 /* Consume the `:' or `::'. */
19485 cp_lexer_consume_token (parser
->lexer
);
19486 /* Parse the clobbers. */
19487 if (cp_lexer_next_token_is_not (parser
->lexer
,
19489 && cp_lexer_next_token_is_not (parser
->lexer
,
19491 clobbers
= cp_parser_asm_clobber_list (parser
);
19494 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19495 /* The labels are coming next. */
19498 /* Look for labels. */
19500 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19503 /* Consume the `:' or `::'. */
19504 cp_lexer_consume_token (parser
->lexer
);
19505 /* Parse the labels. */
19506 labels
= cp_parser_asm_label_list (parser
);
19509 if (goto_p
&& !labels_p
)
19510 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19513 missing
= RT_COLON_SCOPE
;
19515 /* Look for the closing `)'. */
19516 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19517 missing
? missing
: RT_CLOSE_PAREN
))
19518 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19519 /*consume_paren=*/true);
19520 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19522 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19524 /* Create the ASM_EXPR. */
19525 if (parser
->in_function_body
)
19527 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19528 inputs
, clobbers
, labels
);
19529 /* If the extended syntax was not used, mark the ASM_EXPR. */
19532 tree temp
= asm_stmt
;
19533 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19534 temp
= TREE_OPERAND (temp
, 0);
19536 ASM_INPUT_P (temp
) = 1;
19540 symtab
->finalize_toplevel_asm (string
);
19544 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19545 type that comes from the decl-specifier-seq. */
19548 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19550 for (cp_declarator
*d
= declarator
; d
;)
19560 if (TYPE_PTRMEMFUNC_P (type
))
19561 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19562 type
= TREE_TYPE (type
);
19570 /* Declarators [gram.dcl.decl] */
19572 /* Parse an init-declarator.
19575 declarator initializer [opt]
19580 declarator asm-specification [opt] attributes [opt] initializer [opt]
19582 function-definition:
19583 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19585 decl-specifier-seq [opt] declarator function-try-block
19589 function-definition:
19590 __extension__ function-definition
19594 function-definition:
19595 decl-specifier-seq [opt] declarator function-transaction-block
19597 The DECL_SPECIFIERS apply to this declarator. Returns a
19598 representation of the entity declared. If MEMBER_P is TRUE, then
19599 this declarator appears in a class scope. The new DECL created by
19600 this declarator is returned.
19602 The CHECKS are access checks that should be performed once we know
19603 what entity is being declared (and, therefore, what classes have
19606 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19607 for a function-definition here as well. If the declarator is a
19608 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19609 be TRUE upon return. By that point, the function-definition will
19610 have been completely parsed.
19612 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19615 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19616 parsed declaration if it is an uninitialized single declarator not followed
19617 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19618 if present, will not be consumed. If returned, this declarator will be
19619 created with SD_INITIALIZED but will not call cp_finish_decl.
19621 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19622 and there is an initializer, the pointed location_t is set to the
19623 location of the '=' or `(', or '{' in C++11 token introducing the
19627 cp_parser_init_declarator (cp_parser
* parser
,
19628 cp_decl_specifier_seq
*decl_specifiers
,
19629 vec
<deferred_access_check
, va_gc
> *checks
,
19630 bool function_definition_allowed_p
,
19632 int declares_class_or_enum
,
19633 bool* function_definition_p
,
19634 tree
* maybe_range_for_decl
,
19635 location_t
* init_loc
,
19638 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19639 *attributes_start_token
= NULL
;
19640 cp_declarator
*declarator
;
19641 tree prefix_attributes
;
19642 tree attributes
= NULL
;
19643 tree asm_specification
;
19645 tree decl
= NULL_TREE
;
19647 int is_initialized
;
19648 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19649 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19651 enum cpp_ttype initialization_kind
;
19652 bool is_direct_init
= false;
19653 bool is_non_constant_init
;
19654 int ctor_dtor_or_conv_p
;
19655 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19656 tree pushed_scope
= NULL_TREE
;
19657 bool range_for_decl_p
= false;
19658 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19659 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19661 /* Gather the attributes that were provided with the
19662 decl-specifiers. */
19663 prefix_attributes
= decl_specifiers
->attributes
;
19665 /* Assume that this is not the declarator for a function
19667 if (function_definition_p
)
19668 *function_definition_p
= false;
19670 /* Default arguments are only permitted for function parameters. */
19671 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19672 parser
->default_arg_ok_p
= false;
19674 /* Defer access checks while parsing the declarator; we cannot know
19675 what names are accessible until we know what is being
19677 resume_deferring_access_checks ();
19679 token
= cp_lexer_peek_token (parser
->lexer
);
19681 /* Parse the declarator. */
19683 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19684 &ctor_dtor_or_conv_p
,
19685 /*parenthesized_p=*/NULL
,
19686 member_p
, friend_p
);
19687 /* Gather up the deferred checks. */
19688 stop_deferring_access_checks ();
19690 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19692 /* If the DECLARATOR was erroneous, there's no need to go
19694 if (declarator
== cp_error_declarator
)
19695 return error_mark_node
;
19697 /* Check that the number of template-parameter-lists is OK. */
19698 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19700 return error_mark_node
;
19702 if (declares_class_or_enum
& 2)
19703 cp_parser_check_for_definition_in_return_type (declarator
,
19704 decl_specifiers
->type
,
19705 decl_specifiers
->locations
[ds_type_spec
]);
19707 /* Figure out what scope the entity declared by the DECLARATOR is
19708 located in. `grokdeclarator' sometimes changes the scope, so
19709 we compute it now. */
19710 scope
= get_scope_of_declarator (declarator
);
19712 /* Perform any lookups in the declared type which were thought to be
19713 dependent, but are not in the scope of the declarator. */
19714 decl_specifiers
->type
19715 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19717 /* If we're allowing GNU extensions, look for an
19718 asm-specification. */
19719 if (cp_parser_allow_gnu_extensions_p (parser
))
19721 /* Look for an asm-specification. */
19722 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19723 asm_specification
= cp_parser_asm_specification_opt (parser
);
19726 asm_specification
= NULL_TREE
;
19728 /* Look for attributes. */
19729 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19730 attributes
= cp_parser_attributes_opt (parser
);
19732 /* Peek at the next token. */
19733 token
= cp_lexer_peek_token (parser
->lexer
);
19735 bool bogus_implicit_tmpl
= false;
19737 if (function_declarator_p (declarator
))
19739 /* Handle C++17 deduction guides. */
19740 if (!decl_specifiers
->type
19741 && ctor_dtor_or_conv_p
<= 0
19742 && cxx_dialect
>= cxx17
)
19744 cp_declarator
*id
= get_id_declarator (declarator
);
19745 tree name
= id
->u
.id
.unqualified_name
;
19746 parser
->scope
= id
->u
.id
.qualifying_scope
;
19747 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19749 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19750 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19752 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19753 id
->u
.id
.sfk
= sfk_deduction_guide
;
19754 ctor_dtor_or_conv_p
= 1;
19758 /* Check to see if the token indicates the start of a
19759 function-definition. */
19760 if (cp_parser_token_starts_function_definition_p (token
))
19762 if (!function_definition_allowed_p
)
19764 /* If a function-definition should not appear here, issue an
19766 cp_parser_error (parser
,
19767 "a function-definition is not allowed here");
19768 return error_mark_node
;
19771 location_t func_brace_location
19772 = cp_lexer_peek_token (parser
->lexer
)->location
;
19774 /* Neither attributes nor an asm-specification are allowed
19775 on a function-definition. */
19776 if (asm_specification
)
19777 error_at (asm_spec_start_token
->location
,
19778 "an asm-specification is not allowed "
19779 "on a function-definition");
19781 error_at (attributes_start_token
->location
,
19782 "attributes are not allowed "
19783 "on a function-definition");
19784 /* This is a function-definition. */
19785 *function_definition_p
= true;
19787 /* Parse the function definition. */
19789 decl
= cp_parser_save_member_function_body (parser
,
19792 prefix_attributes
);
19795 (cp_parser_function_definition_from_specifiers_and_declarator
19796 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19798 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19800 /* This is where the prologue starts... */
19801 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19802 = func_brace_location
;
19808 else if (parser
->fully_implicit_function_template_p
)
19810 /* A non-template declaration involving a function parameter list
19811 containing an implicit template parameter will be made into a
19812 template. If the resulting declaration is not going to be an
19813 actual function then finish the template scope here to prevent it.
19814 An error message will be issued once we have a decl to talk about.
19816 FIXME probably we should do type deduction rather than create an
19817 implicit template, but the standard currently doesn't allow it. */
19818 bogus_implicit_tmpl
= true;
19819 finish_fully_implicit_template (parser
, NULL_TREE
);
19824 Only in function declarations for constructors, destructors, type
19825 conversions, and deduction guides can the decl-specifier-seq be omitted.
19827 We explicitly postpone this check past the point where we handle
19828 function-definitions because we tolerate function-definitions
19829 that are missing their return types in some modes. */
19830 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19832 cp_parser_error (parser
,
19833 "expected constructor, destructor, or type conversion");
19834 return error_mark_node
;
19837 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19838 if (token
->type
== CPP_EQ
19839 || token
->type
== CPP_OPEN_PAREN
19840 || token
->type
== CPP_OPEN_BRACE
)
19842 is_initialized
= SD_INITIALIZED
;
19843 initialization_kind
= token
->type
;
19844 if (maybe_range_for_decl
)
19845 *maybe_range_for_decl
= error_mark_node
;
19846 tmp_init_loc
= token
->location
;
19847 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19848 *init_loc
= tmp_init_loc
;
19850 if (token
->type
== CPP_EQ
19851 && function_declarator_p (declarator
))
19853 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19854 if (t2
->keyword
== RID_DEFAULT
)
19855 is_initialized
= SD_DEFAULTED
;
19856 else if (t2
->keyword
== RID_DELETE
)
19857 is_initialized
= SD_DELETED
;
19862 /* If the init-declarator isn't initialized and isn't followed by a
19863 `,' or `;', it's not a valid init-declarator. */
19864 if (token
->type
!= CPP_COMMA
19865 && token
->type
!= CPP_SEMICOLON
)
19867 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19868 range_for_decl_p
= true;
19871 if (!maybe_range_for_decl
)
19872 cp_parser_error (parser
, "expected initializer");
19873 return error_mark_node
;
19876 is_initialized
= SD_UNINITIALIZED
;
19877 initialization_kind
= CPP_EOF
;
19880 /* Because start_decl has side-effects, we should only call it if we
19881 know we're going ahead. By this point, we know that we cannot
19882 possibly be looking at any other construct. */
19883 cp_parser_commit_to_tentative_parse (parser
);
19885 /* Enter the newly declared entry in the symbol table. If we're
19886 processing a declaration in a class-specifier, we wait until
19887 after processing the initializer. */
19890 if (parser
->in_unbraced_linkage_specification_p
)
19891 decl_specifiers
->storage_class
= sc_extern
;
19892 decl
= start_decl (declarator
, decl_specifiers
,
19893 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19894 attributes
, prefix_attributes
, &pushed_scope
);
19895 cp_finalize_omp_declare_simd (parser
, decl
);
19896 cp_finalize_oacc_routine (parser
, decl
, false);
19897 /* Adjust location of decl if declarator->id_loc is more appropriate:
19898 set, and decl wasn't merged with another decl, in which case its
19899 location would be different from input_location, and more accurate. */
19901 && declarator
->id_loc
!= UNKNOWN_LOCATION
19902 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19903 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19906 /* Enter the SCOPE. That way unqualified names appearing in the
19907 initializer will be looked up in SCOPE. */
19908 pushed_scope
= push_scope (scope
);
19910 /* Perform deferred access control checks, now that we know in which
19911 SCOPE the declared entity resides. */
19912 if (!member_p
&& decl
)
19914 tree saved_current_function_decl
= NULL_TREE
;
19916 /* If the entity being declared is a function, pretend that we
19917 are in its scope. If it is a `friend', it may have access to
19918 things that would not otherwise be accessible. */
19919 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19921 saved_current_function_decl
= current_function_decl
;
19922 current_function_decl
= decl
;
19925 /* Perform access checks for template parameters. */
19926 cp_parser_perform_template_parameter_access_checks (checks
);
19928 /* Perform the access control checks for the declarator and the
19929 decl-specifiers. */
19930 perform_deferred_access_checks (tf_warning_or_error
);
19932 /* Restore the saved value. */
19933 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19934 current_function_decl
= saved_current_function_decl
;
19937 /* Parse the initializer. */
19938 initializer
= NULL_TREE
;
19939 is_direct_init
= false;
19940 is_non_constant_init
= true;
19941 if (is_initialized
)
19943 if (function_declarator_p (declarator
))
19945 if (initialization_kind
== CPP_EQ
)
19946 initializer
= cp_parser_pure_specifier (parser
);
19949 /* If the declaration was erroneous, we don't really
19950 know what the user intended, so just silently
19951 consume the initializer. */
19952 if (decl
!= error_mark_node
)
19953 error_at (tmp_init_loc
, "initializer provided for function");
19954 cp_parser_skip_to_closing_parenthesis (parser
,
19955 /*recovering=*/true,
19956 /*or_comma=*/false,
19957 /*consume_paren=*/true);
19962 /* We want to record the extra mangling scope for in-class
19963 initializers of class members and initializers of static data
19964 member templates. The former involves deferring
19965 parsing of the initializer until end of class as with default
19966 arguments. So right here we only handle the latter. */
19967 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19968 start_lambda_scope (decl
);
19969 initializer
= cp_parser_initializer (parser
,
19971 &is_non_constant_init
);
19972 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19973 finish_lambda_scope ();
19974 if (initializer
== error_mark_node
)
19975 cp_parser_skip_to_end_of_statement (parser
);
19979 /* The old parser allows attributes to appear after a parenthesized
19980 initializer. Mark Mitchell proposed removing this functionality
19981 on the GCC mailing lists on 2002-08-13. This parser accepts the
19982 attributes -- but ignores them. Made a permerror in GCC 8. */
19983 if (cp_parser_allow_gnu_extensions_p (parser
)
19984 && initialization_kind
== CPP_OPEN_PAREN
19985 && cp_parser_attributes_opt (parser
)
19986 && permerror (input_location
,
19987 "attributes after parenthesized initializer ignored"))
19990 if (flag_permissive
&& !hint
)
19993 inform (input_location
,
19994 "this flexibility is deprecated and will be removed");
19998 /* And now complain about a non-function implicit template. */
19999 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20000 error_at (DECL_SOURCE_LOCATION (decl
),
20001 "non-function %qD declared as implicit template", decl
);
20003 /* For an in-class declaration, use `grokfield' to create the
20009 pop_scope (pushed_scope
);
20010 pushed_scope
= NULL_TREE
;
20012 decl
= grokfield (declarator
, decl_specifiers
,
20013 initializer
, !is_non_constant_init
,
20014 /*asmspec=*/NULL_TREE
,
20015 attr_chainon (attributes
, prefix_attributes
));
20016 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20017 cp_parser_save_default_args (parser
, decl
);
20018 cp_finalize_omp_declare_simd (parser
, decl
);
20019 cp_finalize_oacc_routine (parser
, decl
, false);
20022 /* Finish processing the declaration. But, skip member
20024 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20026 cp_finish_decl (decl
,
20027 initializer
, !is_non_constant_init
,
20029 /* If the initializer is in parentheses, then this is
20030 a direct-initialization, which means that an
20031 `explicit' constructor is OK. Otherwise, an
20032 `explicit' constructor cannot be used. */
20033 ((is_direct_init
|| !is_initialized
)
20034 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20036 else if ((cxx_dialect
!= cxx98
) && friend_p
20037 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20038 /* Core issue #226 (C++0x only): A default template-argument
20039 shall not be specified in a friend class template
20041 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20042 /*is_partial=*/false, /*is_friend_decl=*/1);
20044 if (!friend_p
&& pushed_scope
)
20045 pop_scope (pushed_scope
);
20047 if (function_declarator_p (declarator
)
20048 && parser
->fully_implicit_function_template_p
)
20051 decl
= finish_fully_implicit_template (parser
, decl
);
20053 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20056 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20057 && type_uses_auto (decl_specifiers
->type
))
20058 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20063 /* Parse a declarator.
20067 ptr-operator declarator
20069 abstract-declarator:
20070 ptr-operator abstract-declarator [opt]
20071 direct-abstract-declarator
20076 attributes [opt] direct-declarator
20077 attributes [opt] ptr-operator declarator
20079 abstract-declarator:
20080 attributes [opt] ptr-operator abstract-declarator [opt]
20081 attributes [opt] direct-abstract-declarator
20083 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20084 detect constructors, destructors, deduction guides, or conversion operators.
20085 It is set to -1 if the declarator is a name, and +1 if it is a
20086 function. Otherwise it is set to zero. Usually you just want to
20087 test for >0, but internally the negative value is used.
20089 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20090 a decl-specifier-seq unless it declares a constructor, destructor,
20091 or conversion. It might seem that we could check this condition in
20092 semantic analysis, rather than parsing, but that makes it difficult
20093 to handle something like `f()'. We want to notice that there are
20094 no decl-specifiers, and therefore realize that this is an
20095 expression, not a declaration.)
20097 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20098 the declarator is a direct-declarator of the form "(...)".
20100 MEMBER_P is true iff this declarator is a member-declarator.
20102 FRIEND_P is true iff this declarator is a friend. */
20104 static cp_declarator
*
20105 cp_parser_declarator (cp_parser
* parser
,
20106 cp_parser_declarator_kind dcl_kind
,
20107 int* ctor_dtor_or_conv_p
,
20108 bool* parenthesized_p
,
20109 bool member_p
, bool friend_p
)
20111 cp_declarator
*declarator
;
20112 enum tree_code code
;
20113 cp_cv_quals cv_quals
;
20115 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20117 /* Assume this is not a constructor, destructor, or type-conversion
20119 if (ctor_dtor_or_conv_p
)
20120 *ctor_dtor_or_conv_p
= 0;
20122 if (cp_parser_allow_gnu_extensions_p (parser
))
20123 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20125 /* Check for the ptr-operator production. */
20126 cp_parser_parse_tentatively (parser
);
20127 /* Parse the ptr-operator. */
20128 code
= cp_parser_ptr_operator (parser
,
20133 /* If that worked, then we have a ptr-operator. */
20134 if (cp_parser_parse_definitely (parser
))
20136 /* If a ptr-operator was found, then this declarator was not
20138 if (parenthesized_p
)
20139 *parenthesized_p
= true;
20140 /* The dependent declarator is optional if we are parsing an
20141 abstract-declarator. */
20142 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20143 cp_parser_parse_tentatively (parser
);
20145 /* Parse the dependent declarator. */
20146 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20147 /*ctor_dtor_or_conv_p=*/NULL
,
20148 /*parenthesized_p=*/NULL
,
20149 /*member_p=*/false,
20152 /* If we are parsing an abstract-declarator, we must handle the
20153 case where the dependent declarator is absent. */
20154 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20155 && !cp_parser_parse_definitely (parser
))
20158 declarator
= cp_parser_make_indirect_declarator
20159 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20161 /* Everything else is a direct-declarator. */
20164 if (parenthesized_p
)
20165 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20167 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20168 ctor_dtor_or_conv_p
,
20169 member_p
, friend_p
);
20172 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20173 declarator
->attributes
= gnu_attributes
;
20177 /* Parse a direct-declarator or direct-abstract-declarator.
20181 direct-declarator ( parameter-declaration-clause )
20182 cv-qualifier-seq [opt]
20183 ref-qualifier [opt]
20184 exception-specification [opt]
20185 direct-declarator [ constant-expression [opt] ]
20188 direct-abstract-declarator:
20189 direct-abstract-declarator [opt]
20190 ( parameter-declaration-clause )
20191 cv-qualifier-seq [opt]
20192 ref-qualifier [opt]
20193 exception-specification [opt]
20194 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20195 ( abstract-declarator )
20197 Returns a representation of the declarator. DCL_KIND is
20198 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20199 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20200 we are parsing a direct-declarator. It is
20201 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20202 of ambiguity we prefer an abstract declarator, as per
20203 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20204 as for cp_parser_declarator. */
20206 static cp_declarator
*
20207 cp_parser_direct_declarator (cp_parser
* parser
,
20208 cp_parser_declarator_kind dcl_kind
,
20209 int* ctor_dtor_or_conv_p
,
20210 bool member_p
, bool friend_p
)
20213 cp_declarator
*declarator
= NULL
;
20214 tree scope
= NULL_TREE
;
20215 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20216 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20218 tree pushed_scope
= NULL_TREE
;
20219 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20223 /* Peek at the next token. */
20224 token
= cp_lexer_peek_token (parser
->lexer
);
20225 if (token
->type
== CPP_OPEN_PAREN
)
20227 /* This is either a parameter-declaration-clause, or a
20228 parenthesized declarator. When we know we are parsing a
20229 named declarator, it must be a parenthesized declarator
20230 if FIRST is true. For instance, `(int)' is a
20231 parameter-declaration-clause, with an omitted
20232 direct-abstract-declarator. But `((*))', is a
20233 parenthesized abstract declarator. Finally, when T is a
20234 template parameter `(T)' is a
20235 parameter-declaration-clause, and not a parenthesized
20238 We first try and parse a parameter-declaration-clause,
20239 and then try a nested declarator (if FIRST is true).
20241 It is not an error for it not to be a
20242 parameter-declaration-clause, even when FIRST is
20248 The first is the declaration of a function while the
20249 second is the definition of a variable, including its
20252 Having seen only the parenthesis, we cannot know which of
20253 these two alternatives should be selected. Even more
20254 complex are examples like:
20259 The former is a function-declaration; the latter is a
20260 variable initialization.
20262 Thus again, we try a parameter-declaration-clause, and if
20263 that fails, we back out and return. */
20265 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20268 bool is_declarator
= false;
20272 /* In a member-declarator, the only valid interpretation
20273 of a parenthesis is the start of a
20274 parameter-declaration-clause. (It is invalid to
20275 initialize a static data member with a parenthesized
20276 initializer; only the "=" form of initialization is
20279 cp_parser_parse_tentatively (parser
);
20281 /* Consume the `('. */
20282 matching_parens parens
;
20283 parens
.consume_open (parser
);
20286 /* If this is going to be an abstract declarator, we're
20287 in a declarator and we can't have default args. */
20288 parser
->default_arg_ok_p
= false;
20289 parser
->in_declarator_p
= true;
20292 begin_scope (sk_function_parms
, NULL_TREE
);
20294 /* Parse the parameter-declaration-clause. */
20295 params
= cp_parser_parameter_declaration_clause (parser
);
20297 /* Consume the `)'. */
20298 parens
.require_close (parser
);
20300 /* If all went well, parse the cv-qualifier-seq,
20301 ref-qualifier and the exception-specification. */
20302 if (member_p
|| cp_parser_parse_definitely (parser
))
20304 cp_cv_quals cv_quals
;
20305 cp_virt_specifiers virt_specifiers
;
20306 cp_ref_qualifier ref_qual
;
20307 tree exception_specification
;
20310 bool memfn
= (member_p
|| (pushed_scope
20311 && CLASS_TYPE_P (pushed_scope
)));
20313 is_declarator
= true;
20315 if (ctor_dtor_or_conv_p
)
20316 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20319 /* Parse the cv-qualifier-seq. */
20320 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20321 /* Parse the ref-qualifier. */
20322 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20323 /* Parse the tx-qualifier. */
20324 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20325 /* And the exception-specification. */
20326 exception_specification
20327 = cp_parser_exception_specification_opt (parser
);
20329 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20331 /* In here, we handle cases where attribute is used after
20332 the function declaration. For example:
20333 void func (int x) __attribute__((vector(..))); */
20334 tree gnu_attrs
= NULL_TREE
;
20335 tree requires_clause
= NULL_TREE
;
20336 late_return
= (cp_parser_late_return_type_opt
20337 (parser
, declarator
, requires_clause
,
20338 memfn
? cv_quals
: -1));
20340 /* Parse the virt-specifier-seq. */
20341 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20343 /* Create the function-declarator. */
20344 declarator
= make_call_declarator (declarator
,
20350 exception_specification
,
20353 declarator
->std_attributes
= attrs
;
20354 declarator
->attributes
= gnu_attrs
;
20355 /* Any subsequent parameter lists are to do with
20356 return type, so are not those of the declared
20358 parser
->default_arg_ok_p
= false;
20361 /* Remove the function parms from scope. */
20362 pop_bindings_and_leave_scope ();
20365 /* Repeat the main loop. */
20369 /* If this is the first, we can try a parenthesized
20373 bool saved_in_type_id_in_expr_p
;
20375 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20376 parser
->in_declarator_p
= saved_in_declarator_p
;
20378 open_paren
= token
;
20379 /* Consume the `('. */
20380 matching_parens parens
;
20381 parens
.consume_open (parser
);
20382 /* Parse the nested declarator. */
20383 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20384 parser
->in_type_id_in_expr_p
= true;
20386 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20387 /*parenthesized_p=*/NULL
,
20388 member_p
, friend_p
);
20389 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20391 /* Expect a `)'. */
20392 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20393 if (!parens
.require_close (parser
))
20394 declarator
= cp_error_declarator
;
20395 if (declarator
== cp_error_declarator
)
20398 goto handle_declarator
;
20400 /* Otherwise, we must be done. */
20404 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20405 && token
->type
== CPP_OPEN_SQUARE
20406 && !cp_next_tokens_can_be_attribute_p (parser
))
20408 /* Parse an array-declarator. */
20409 tree bounds
, attrs
;
20411 if (ctor_dtor_or_conv_p
)
20412 *ctor_dtor_or_conv_p
= 0;
20416 parser
->default_arg_ok_p
= false;
20417 parser
->in_declarator_p
= true;
20418 /* Consume the `['. */
20419 cp_lexer_consume_token (parser
->lexer
);
20420 /* Peek at the next token. */
20421 token
= cp_lexer_peek_token (parser
->lexer
);
20422 /* If the next token is `]', then there is no
20423 constant-expression. */
20424 if (token
->type
!= CPP_CLOSE_SQUARE
)
20426 bool non_constant_p
;
20428 = cp_parser_constant_expression (parser
,
20429 /*allow_non_constant=*/true,
20431 if (!non_constant_p
)
20433 else if (error_operand_p (bounds
))
20434 /* Already gave an error. */;
20435 else if (!parser
->in_function_body
20436 || current_binding_level
->kind
== sk_function_parms
)
20438 /* Normally, the array bound must be an integral constant
20439 expression. However, as an extension, we allow VLAs
20440 in function scopes as long as they aren't part of a
20441 parameter declaration. */
20442 cp_parser_error (parser
,
20443 "array bound is not an integer constant");
20444 bounds
= error_mark_node
;
20446 else if (processing_template_decl
20447 && !type_dependent_expression_p (bounds
))
20449 /* Remember this wasn't a constant-expression. */
20450 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20451 TREE_SIDE_EFFECTS (bounds
) = 1;
20455 bounds
= NULL_TREE
;
20456 /* Look for the closing `]'. */
20457 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20459 declarator
= cp_error_declarator
;
20463 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20464 declarator
= make_array_declarator (declarator
, bounds
);
20465 declarator
->std_attributes
= attrs
;
20467 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20470 tree qualifying_scope
;
20471 tree unqualified_name
;
20473 special_function_kind sfk
;
20475 bool pack_expansion_p
= false;
20476 cp_token
*declarator_id_start_token
;
20478 /* Parse a declarator-id */
20479 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20482 cp_parser_parse_tentatively (parser
);
20484 /* If we see an ellipsis, we should be looking at a
20486 if (token
->type
== CPP_ELLIPSIS
)
20488 /* Consume the `...' */
20489 cp_lexer_consume_token (parser
->lexer
);
20491 pack_expansion_p
= true;
20495 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20497 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20498 qualifying_scope
= parser
->scope
;
20503 if (!unqualified_name
&& pack_expansion_p
)
20505 /* Check whether an error occurred. */
20506 okay
= !cp_parser_error_occurred (parser
);
20508 /* We already consumed the ellipsis to mark a
20509 parameter pack, but we have no way to report it,
20510 so abort the tentative parse. We will be exiting
20511 immediately anyway. */
20512 cp_parser_abort_tentative_parse (parser
);
20515 okay
= cp_parser_parse_definitely (parser
);
20518 unqualified_name
= error_mark_node
;
20519 else if (unqualified_name
20520 && (qualifying_scope
20521 || (!identifier_p (unqualified_name
))))
20523 cp_parser_error (parser
, "expected unqualified-id");
20524 unqualified_name
= error_mark_node
;
20528 if (!unqualified_name
)
20530 if (unqualified_name
== error_mark_node
)
20532 declarator
= cp_error_declarator
;
20533 pack_expansion_p
= false;
20534 declarator
->parameter_pack_p
= false;
20538 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20540 if (qualifying_scope
&& at_namespace_scope_p ()
20541 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20543 /* In the declaration of a member of a template class
20544 outside of the class itself, the SCOPE will sometimes
20545 be a TYPENAME_TYPE. For example, given:
20547 template <typename T>
20548 int S<T>::R::i = 3;
20550 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20551 this context, we must resolve S<T>::R to an ordinary
20552 type, rather than a typename type.
20554 The reason we normally avoid resolving TYPENAME_TYPEs
20555 is that a specialization of `S' might render
20556 `S<T>::R' not a type. However, if `S' is
20557 specialized, then this `i' will not be used, so there
20558 is no harm in resolving the types here. */
20561 /* Resolve the TYPENAME_TYPE. */
20562 type
= resolve_typename_type (qualifying_scope
,
20563 /*only_current_p=*/false);
20564 /* If that failed, the declarator is invalid. */
20565 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20567 if (typedef_variant_p (type
))
20568 error_at (declarator_id_start_token
->location
,
20569 "cannot define member of dependent typedef "
20572 error_at (declarator_id_start_token
->location
,
20573 "%<%T::%E%> is not a type",
20574 TYPE_CONTEXT (qualifying_scope
),
20575 TYPE_IDENTIFIER (qualifying_scope
));
20577 qualifying_scope
= type
;
20582 if (unqualified_name
)
20586 if (qualifying_scope
20587 && CLASS_TYPE_P (qualifying_scope
))
20588 class_type
= qualifying_scope
;
20590 class_type
= current_class_type
;
20592 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20594 tree name_type
= TREE_TYPE (unqualified_name
);
20596 if (!class_type
|| !same_type_p (name_type
, class_type
))
20598 /* We do not attempt to print the declarator
20599 here because we do not have enough
20600 information about its original syntactic
20602 cp_parser_error (parser
, "invalid declarator");
20603 declarator
= cp_error_declarator
;
20606 else if (qualifying_scope
20607 && CLASSTYPE_USE_TEMPLATE (name_type
))
20609 error_at (declarator_id_start_token
->location
,
20610 "invalid use of constructor as a template");
20611 inform (declarator_id_start_token
->location
,
20612 "use %<%T::%D%> instead of %<%T::%D%> to "
20613 "name the constructor in a qualified name",
20615 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20616 class_type
, name_type
);
20617 declarator
= cp_error_declarator
;
20620 unqualified_name
= constructor_name (class_type
);
20625 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20626 sfk
= sfk_destructor
;
20627 else if (identifier_p (unqualified_name
)
20628 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20629 sfk
= sfk_conversion
;
20630 else if (/* There's no way to declare a constructor
20631 for an unnamed type, even if the type
20632 got a name for linkage purposes. */
20633 !TYPE_WAS_UNNAMED (class_type
)
20634 /* Handle correctly (c++/19200):
20648 friend void N::S();
20650 && (!friend_p
|| class_type
== qualifying_scope
)
20651 && constructor_name_p (unqualified_name
,
20653 sfk
= sfk_constructor
;
20654 else if (is_overloaded_fn (unqualified_name
)
20655 && DECL_CONSTRUCTOR_P (get_first_fn
20656 (unqualified_name
)))
20657 sfk
= sfk_constructor
;
20659 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20660 *ctor_dtor_or_conv_p
= -1;
20663 declarator
= make_id_declarator (qualifying_scope
,
20666 declarator
->std_attributes
= attrs
;
20667 declarator
->id_loc
= token
->location
;
20668 declarator
->parameter_pack_p
= pack_expansion_p
;
20670 if (pack_expansion_p
)
20671 maybe_warn_variadic_templates ();
20674 handle_declarator
:;
20675 scope
= get_scope_of_declarator (declarator
);
20678 /* Any names that appear after the declarator-id for a
20679 member are looked up in the containing scope. */
20680 if (at_function_scope_p ())
20682 /* But declarations with qualified-ids can't appear in a
20684 cp_parser_error (parser
, "qualified-id in declaration");
20685 declarator
= cp_error_declarator
;
20688 pushed_scope
= push_scope (scope
);
20690 parser
->in_declarator_p
= true;
20691 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20692 || (declarator
&& declarator
->kind
== cdk_id
))
20693 /* Default args are only allowed on function
20695 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20697 parser
->default_arg_ok_p
= false;
20706 /* For an abstract declarator, we might wind up with nothing at this
20707 point. That's an error; the declarator is not optional. */
20709 cp_parser_error (parser
, "expected declarator");
20710 else if (open_paren
)
20712 /* Record overly parenthesized declarator so we can give a
20713 diagnostic about confusing decl/expr disambiguation. */
20714 if (declarator
->kind
== cdk_array
)
20716 /* If the open and close parens are on different lines, this
20717 is probably a formatting thing, so ignore. */
20718 expanded_location open
= expand_location (open_paren
->location
);
20719 expanded_location close
= expand_location (close_paren
->location
);
20720 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20724 declarator
->parenthesized
= open_paren
->location
;
20727 /* If we entered a scope, we must exit it now. */
20729 pop_scope (pushed_scope
);
20731 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20732 parser
->in_declarator_p
= saved_in_declarator_p
;
20737 /* Parse a ptr-operator.
20740 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20741 * cv-qualifier-seq [opt]
20743 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20744 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20749 & cv-qualifier-seq [opt]
20751 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20752 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20753 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20754 filled in with the TYPE containing the member. *CV_QUALS is
20755 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20756 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20757 Note that the tree codes returned by this function have nothing
20758 to do with the types of trees that will be eventually be created
20759 to represent the pointer or reference type being parsed. They are
20760 just constants with suggestive names. */
20761 static enum tree_code
20762 cp_parser_ptr_operator (cp_parser
* parser
,
20764 cp_cv_quals
*cv_quals
,
20767 enum tree_code code
= ERROR_MARK
;
20769 tree attrs
= NULL_TREE
;
20771 /* Assume that it's not a pointer-to-member. */
20773 /* And that there are no cv-qualifiers. */
20774 *cv_quals
= TYPE_UNQUALIFIED
;
20776 /* Peek at the next token. */
20777 token
= cp_lexer_peek_token (parser
->lexer
);
20779 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20780 if (token
->type
== CPP_MULT
)
20781 code
= INDIRECT_REF
;
20782 else if (token
->type
== CPP_AND
)
20784 else if ((cxx_dialect
!= cxx98
) &&
20785 token
->type
== CPP_AND_AND
) /* C++0x only */
20786 code
= NON_LVALUE_EXPR
;
20788 if (code
!= ERROR_MARK
)
20790 /* Consume the `*', `&' or `&&'. */
20791 cp_lexer_consume_token (parser
->lexer
);
20793 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20794 `&', if we are allowing GNU extensions. (The only qualifier
20795 that can legally appear after `&' is `restrict', but that is
20796 enforced during semantic analysis. */
20797 if (code
== INDIRECT_REF
20798 || cp_parser_allow_gnu_extensions_p (parser
))
20799 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20801 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20802 if (attributes
!= NULL
)
20803 *attributes
= attrs
;
20807 /* Try the pointer-to-member case. */
20808 cp_parser_parse_tentatively (parser
);
20809 /* Look for the optional `::' operator. */
20810 cp_parser_global_scope_opt (parser
,
20811 /*current_scope_valid_p=*/false);
20812 /* Look for the nested-name specifier. */
20813 token
= cp_lexer_peek_token (parser
->lexer
);
20814 cp_parser_nested_name_specifier (parser
,
20815 /*typename_keyword_p=*/false,
20816 /*check_dependency_p=*/true,
20818 /*is_declaration=*/false);
20819 /* If we found it, and the next token is a `*', then we are
20820 indeed looking at a pointer-to-member operator. */
20821 if (!cp_parser_error_occurred (parser
)
20822 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20824 /* Indicate that the `*' operator was used. */
20825 code
= INDIRECT_REF
;
20827 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20828 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20829 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20830 error_at (token
->location
, "cannot form pointer to member of "
20831 "non-class %q#T", parser
->scope
);
20834 /* The type of which the member is a member is given by the
20836 *type
= parser
->scope
;
20837 /* The next name will not be qualified. */
20838 parser
->scope
= NULL_TREE
;
20839 parser
->qualifying_scope
= NULL_TREE
;
20840 parser
->object_scope
= NULL_TREE
;
20841 /* Look for optional c++11 attributes. */
20842 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20843 if (attributes
!= NULL
)
20844 *attributes
= attrs
;
20845 /* Look for the optional cv-qualifier-seq. */
20846 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20849 /* If that didn't work we don't have a ptr-operator. */
20850 if (!cp_parser_parse_definitely (parser
))
20851 cp_parser_error (parser
, "expected ptr-operator");
20857 /* Parse an (optional) cv-qualifier-seq.
20860 cv-qualifier cv-qualifier-seq [opt]
20871 Returns a bitmask representing the cv-qualifiers. */
20874 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20876 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20881 cp_cv_quals cv_qualifier
;
20883 /* Peek at the next token. */
20884 token
= cp_lexer_peek_token (parser
->lexer
);
20885 /* See if it's a cv-qualifier. */
20886 switch (token
->keyword
)
20889 cv_qualifier
= TYPE_QUAL_CONST
;
20893 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20897 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20901 cv_qualifier
= TYPE_UNQUALIFIED
;
20908 if (cv_quals
& cv_qualifier
)
20910 gcc_rich_location
richloc (token
->location
);
20911 richloc
.add_fixit_remove ();
20912 error_at (&richloc
, "duplicate cv-qualifier");
20913 cp_lexer_purge_token (parser
->lexer
);
20917 cp_lexer_consume_token (parser
->lexer
);
20918 cv_quals
|= cv_qualifier
;
20925 /* Parse an (optional) ref-qualifier
20931 Returns cp_ref_qualifier representing ref-qualifier. */
20933 static cp_ref_qualifier
20934 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20936 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20938 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20939 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20944 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20945 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20947 switch (token
->type
)
20950 curr_ref_qual
= REF_QUAL_LVALUE
;
20954 curr_ref_qual
= REF_QUAL_RVALUE
;
20958 curr_ref_qual
= REF_QUAL_NONE
;
20962 if (!curr_ref_qual
)
20966 error_at (token
->location
, "multiple ref-qualifiers");
20967 cp_lexer_purge_token (parser
->lexer
);
20971 ref_qual
= curr_ref_qual
;
20972 cp_lexer_consume_token (parser
->lexer
);
20979 /* Parse an optional tx-qualifier.
20983 transaction_safe_dynamic */
20986 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20988 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20989 if (token
->type
== CPP_NAME
)
20991 tree name
= token
->u
.value
;
20992 const char *p
= IDENTIFIER_POINTER (name
);
20993 const int len
= strlen ("transaction_safe");
20994 if (!strncmp (p
, "transaction_safe", len
))
20998 || !strcmp (p
, "_dynamic"))
21000 cp_lexer_consume_token (parser
->lexer
);
21003 error ("%qE requires %<-fgnu-tm%>", name
);
21014 /* Parse an (optional) virt-specifier-seq.
21016 virt-specifier-seq:
21017 virt-specifier virt-specifier-seq [opt]
21023 Returns a bitmask representing the virt-specifiers. */
21025 static cp_virt_specifiers
21026 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21028 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21033 cp_virt_specifiers virt_specifier
;
21035 /* Peek at the next token. */
21036 token
= cp_lexer_peek_token (parser
->lexer
);
21037 /* See if it's a virt-specifier-qualifier. */
21038 if (token
->type
!= CPP_NAME
)
21040 if (id_equal (token
->u
.value
, "override"))
21042 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21043 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21045 else if (id_equal (token
->u
.value
, "final"))
21047 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21048 virt_specifier
= VIRT_SPEC_FINAL
;
21050 else if (id_equal (token
->u
.value
, "__final"))
21052 virt_specifier
= VIRT_SPEC_FINAL
;
21057 if (virt_specifiers
& virt_specifier
)
21059 gcc_rich_location
richloc (token
->location
);
21060 richloc
.add_fixit_remove ();
21061 error_at (&richloc
, "duplicate virt-specifier");
21062 cp_lexer_purge_token (parser
->lexer
);
21066 cp_lexer_consume_token (parser
->lexer
);
21067 virt_specifiers
|= virt_specifier
;
21070 return virt_specifiers
;
21073 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21074 is in scope even though it isn't real. */
21077 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21081 if (current_class_ptr
)
21083 /* We don't clear this between NSDMIs. Is it already what we want? */
21084 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21085 if (DECL_P (current_class_ptr
)
21086 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21087 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21088 && cp_type_quals (type
) == quals
)
21092 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21093 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21094 current_class_ptr
= NULL_TREE
;
21096 = cp_build_fold_indirect_ref (this_parm
);
21097 current_class_ptr
= this_parm
;
21100 /* Return true iff our current scope is a non-static data member
21104 parsing_nsdmi (void)
21106 /* We recognize NSDMI context by the context-less 'this' pointer set up
21107 by the function above. */
21108 if (current_class_ptr
21109 && TREE_CODE (current_class_ptr
) == PARM_DECL
21110 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21115 /* Parse a late-specified return type, if any. This is not a separate
21116 non-terminal, but part of a function declarator, which looks like
21118 -> trailing-type-specifier-seq abstract-declarator(opt)
21120 Returns the type indicated by the type-id.
21122 In addition to this, parse any queued up #pragma omp declare simd
21123 clauses, and #pragma acc routine clauses.
21125 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21129 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21130 tree
& requires_clause
, cp_cv_quals quals
)
21133 tree type
= NULL_TREE
;
21134 bool declare_simd_p
= (parser
->omp_declare_simd
21136 && declarator
->kind
== cdk_id
);
21138 bool oacc_routine_p
= (parser
->oacc_routine
21140 && declarator
->kind
== cdk_id
);
21142 /* Peek at the next token. */
21143 token
= cp_lexer_peek_token (parser
->lexer
);
21144 /* A late-specified return type is indicated by an initial '->'. */
21145 if (token
->type
!= CPP_DEREF
21146 && token
->keyword
!= RID_REQUIRES
21147 && !(token
->type
== CPP_NAME
21148 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21149 && !(declare_simd_p
|| oacc_routine_p
))
21152 tree save_ccp
= current_class_ptr
;
21153 tree save_ccr
= current_class_ref
;
21156 /* DR 1207: 'this' is in scope in the trailing return type. */
21157 inject_this_parameter (current_class_type
, quals
);
21160 if (token
->type
== CPP_DEREF
)
21162 /* Consume the ->. */
21163 cp_lexer_consume_token (parser
->lexer
);
21165 type
= cp_parser_trailing_type_id (parser
);
21168 /* Function declarations may be followed by a trailing
21169 requires-clause. */
21170 requires_clause
= cp_parser_requires_clause_opt (parser
);
21172 if (declare_simd_p
)
21173 declarator
->attributes
21174 = cp_parser_late_parsing_omp_declare_simd (parser
,
21175 declarator
->attributes
);
21176 if (oacc_routine_p
)
21177 declarator
->attributes
21178 = cp_parser_late_parsing_oacc_routine (parser
,
21179 declarator
->attributes
);
21183 current_class_ptr
= save_ccp
;
21184 current_class_ref
= save_ccr
;
21190 /* Parse a declarator-id.
21194 :: [opt] nested-name-specifier [opt] type-name
21196 In the `id-expression' case, the value returned is as for
21197 cp_parser_id_expression if the id-expression was an unqualified-id.
21198 If the id-expression was a qualified-id, then a SCOPE_REF is
21199 returned. The first operand is the scope (either a NAMESPACE_DECL
21200 or TREE_TYPE), but the second is still just a representation of an
21204 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21207 /* The expression must be an id-expression. Assume that qualified
21208 names are the names of types so that:
21211 int S<T>::R::i = 3;
21213 will work; we must treat `S<T>::R' as the name of a type.
21214 Similarly, assume that qualified names are templates, where
21218 int S<T>::R<T>::i = 3;
21221 id
= cp_parser_id_expression (parser
,
21222 /*template_keyword_p=*/false,
21223 /*check_dependency_p=*/false,
21224 /*template_p=*/NULL
,
21225 /*declarator_p=*/true,
21227 if (id
&& BASELINK_P (id
))
21228 id
= BASELINK_FUNCTIONS (id
);
21232 /* Parse a type-id.
21235 type-specifier-seq abstract-declarator [opt]
21237 Returns the TYPE specified. */
21240 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21241 bool is_trailing_return
, location_t
* type_location
)
21243 cp_decl_specifier_seq type_specifier_seq
;
21244 cp_declarator
*abstract_declarator
;
21246 /* Parse the type-specifier-seq. */
21247 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21248 is_trailing_return
,
21249 &type_specifier_seq
);
21251 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21253 if (is_template_arg
&& type_specifier_seq
.type
21254 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21255 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21256 /* A bare template name as a template argument is a template template
21257 argument, not a placeholder, so fail parsing it as a type argument. */
21259 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21260 cp_parser_simulate_error (parser
);
21261 return error_mark_node
;
21263 if (type_specifier_seq
.type
== error_mark_node
)
21264 return error_mark_node
;
21266 /* There might or might not be an abstract declarator. */
21267 cp_parser_parse_tentatively (parser
);
21268 /* Look for the declarator. */
21269 abstract_declarator
21270 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21271 /*parenthesized_p=*/NULL
,
21272 /*member_p=*/false,
21273 /*friend_p=*/false);
21274 /* Check to see if there really was a declarator. */
21275 if (!cp_parser_parse_definitely (parser
))
21276 abstract_declarator
= NULL
;
21278 if (type_specifier_seq
.type
21279 /* The concepts TS allows 'auto' as a type-id. */
21280 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21281 /* None of the valid uses of 'auto' in C++14 involve the type-id
21282 nonterminal, but it is valid in a trailing-return-type. */
21283 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21284 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21286 /* A type-id with type 'auto' is only ok if the abstract declarator
21287 is a function declarator with a late-specified return type.
21289 A type-id with 'auto' is also valid in a trailing-return-type
21290 in a compound-requirement. */
21291 if (abstract_declarator
21292 && abstract_declarator
->kind
== cdk_function
21293 && abstract_declarator
->u
.function
.late_return_type
)
21295 else if (parser
->in_result_type_constraint_p
)
21299 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21300 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21302 error_at (loc
, "missing template arguments after %qT",
21304 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21308 error_at (loc
, "invalid use of %qT", auto_node
);
21309 return error_mark_node
;
21313 return groktypename (&type_specifier_seq
, abstract_declarator
,
21318 cp_parser_type_id (cp_parser
*parser
, location_t
* type_location
)
21320 return cp_parser_type_id_1 (parser
, false, false, type_location
);
21324 cp_parser_template_type_arg (cp_parser
*parser
)
21327 const char *saved_message
= parser
->type_definition_forbidden_message
;
21328 parser
->type_definition_forbidden_message
21329 = G_("types may not be defined in template arguments");
21330 r
= cp_parser_type_id_1 (parser
, true, false, NULL
);
21331 parser
->type_definition_forbidden_message
= saved_message
;
21332 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21334 error ("invalid use of %<auto%> in template argument");
21335 r
= error_mark_node
;
21341 cp_parser_trailing_type_id (cp_parser
*parser
)
21343 return cp_parser_type_id_1 (parser
, false, true, NULL
);
21346 /* Parse a type-specifier-seq.
21348 type-specifier-seq:
21349 type-specifier type-specifier-seq [opt]
21353 type-specifier-seq:
21354 attributes type-specifier-seq [opt]
21356 If IS_DECLARATION is true, we are at the start of a "condition" or
21357 exception-declaration, so we might be followed by a declarator-id.
21359 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21360 i.e. we've just seen "->".
21362 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21365 cp_parser_type_specifier_seq (cp_parser
* parser
,
21366 bool is_declaration
,
21367 bool is_trailing_return
,
21368 cp_decl_specifier_seq
*type_specifier_seq
)
21370 bool seen_type_specifier
= false;
21371 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21372 cp_token
*start_token
= NULL
;
21374 /* Clear the TYPE_SPECIFIER_SEQ. */
21375 clear_decl_specs (type_specifier_seq
);
21377 /* In the context of a trailing return type, enum E { } is an
21378 elaborated-type-specifier followed by a function-body, not an
21380 if (is_trailing_return
)
21381 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21383 /* Parse the type-specifiers and attributes. */
21386 tree type_specifier
;
21387 bool is_cv_qualifier
;
21389 /* Check for attributes first. */
21390 if (cp_next_tokens_can_be_attribute_p (parser
))
21392 type_specifier_seq
->attributes
21393 = attr_chainon (type_specifier_seq
->attributes
,
21394 cp_parser_attributes_opt (parser
));
21398 /* record the token of the beginning of the type specifier seq,
21399 for error reporting purposes*/
21401 start_token
= cp_lexer_peek_token (parser
->lexer
);
21403 /* Look for the type-specifier. */
21404 type_specifier
= cp_parser_type_specifier (parser
,
21406 type_specifier_seq
,
21407 /*is_declaration=*/false,
21410 if (!type_specifier
)
21412 /* If the first type-specifier could not be found, this is not a
21413 type-specifier-seq at all. */
21414 if (!seen_type_specifier
)
21416 /* Set in_declarator_p to avoid skipping to the semicolon. */
21417 int in_decl
= parser
->in_declarator_p
;
21418 parser
->in_declarator_p
= true;
21420 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21421 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21422 cp_parser_error (parser
, "expected type-specifier");
21424 parser
->in_declarator_p
= in_decl
;
21426 type_specifier_seq
->type
= error_mark_node
;
21429 /* If subsequent type-specifiers could not be found, the
21430 type-specifier-seq is complete. */
21434 seen_type_specifier
= true;
21435 /* The standard says that a condition can be:
21437 type-specifier-seq declarator = assignment-expression
21444 we should treat the "S" as a declarator, not as a
21445 type-specifier. The standard doesn't say that explicitly for
21446 type-specifier-seq, but it does say that for
21447 decl-specifier-seq in an ordinary declaration. Perhaps it
21448 would be clearer just to allow a decl-specifier-seq here, and
21449 then add a semantic restriction that if any decl-specifiers
21450 that are not type-specifiers appear, the program is invalid. */
21451 if (is_declaration
&& !is_cv_qualifier
)
21452 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21456 /* Return whether the function currently being declared has an associated
21457 template parameter list. */
21460 function_being_declared_is_template_p (cp_parser
* parser
)
21462 if (!current_template_parms
|| processing_template_parmlist
)
21465 if (parser
->implicit_template_scope
)
21468 if (at_class_scope_p ()
21469 && TYPE_BEING_DEFINED (current_class_type
))
21470 return parser
->num_template_parameter_lists
!= 0;
21472 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21473 (current_class_type
));
21476 /* Parse a parameter-declaration-clause.
21478 parameter-declaration-clause:
21479 parameter-declaration-list [opt] ... [opt]
21480 parameter-declaration-list , ...
21482 Returns a representation for the parameter declarations. A return
21483 value of NULL indicates a parameter-declaration-clause consisting
21484 only of an ellipsis. */
21487 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21493 temp_override
<bool> cleanup
21494 (parser
->auto_is_implicit_function_template_parm_p
);
21496 if (!processing_specialization
21497 && !processing_template_parmlist
21498 && !processing_explicit_instantiation
21499 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21500 actual function or a random abstract declarator. */
21501 && parser
->default_arg_ok_p
)
21502 if (!current_function_decl
21503 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21504 parser
->auto_is_implicit_function_template_parm_p
= true;
21506 /* Peek at the next token. */
21507 token
= cp_lexer_peek_token (parser
->lexer
);
21508 /* Check for trivial parameter-declaration-clauses. */
21509 if (token
->type
== CPP_ELLIPSIS
)
21511 /* Consume the `...' token. */
21512 cp_lexer_consume_token (parser
->lexer
);
21515 else if (token
->type
== CPP_CLOSE_PAREN
)
21516 /* There are no parameters. */
21517 return void_list_node
;
21518 /* Check for `(void)', too, which is a special case. */
21519 else if (token
->keyword
== RID_VOID
21520 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21521 == CPP_CLOSE_PAREN
))
21523 /* Consume the `void' token. */
21524 cp_lexer_consume_token (parser
->lexer
);
21525 /* There are no parameters. */
21526 return void_list_node
;
21529 /* Parse the parameter-declaration-list. */
21530 parameters
= cp_parser_parameter_declaration_list (parser
);
21531 /* If a parse error occurred while parsing the
21532 parameter-declaration-list, then the entire
21533 parameter-declaration-clause is erroneous. */
21534 if (parameters
== error_mark_node
)
21537 /* Peek at the next token. */
21538 token
= cp_lexer_peek_token (parser
->lexer
);
21539 /* If it's a `,', the clause should terminate with an ellipsis. */
21540 if (token
->type
== CPP_COMMA
)
21542 /* Consume the `,'. */
21543 cp_lexer_consume_token (parser
->lexer
);
21544 /* Expect an ellipsis. */
21546 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21548 /* It might also be `...' if the optional trailing `,' was
21550 else if (token
->type
== CPP_ELLIPSIS
)
21552 /* Consume the `...' token. */
21553 cp_lexer_consume_token (parser
->lexer
);
21554 /* And remember that we saw it. */
21558 ellipsis_p
= false;
21560 /* Finish the parameter list. */
21562 parameters
= chainon (parameters
, void_list_node
);
21567 /* Parse a parameter-declaration-list.
21569 parameter-declaration-list:
21570 parameter-declaration
21571 parameter-declaration-list , parameter-declaration
21573 Returns a representation of the parameter-declaration-list, as for
21574 cp_parser_parameter_declaration_clause. However, the
21575 `void_list_node' is never appended to the list. */
21578 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21580 tree parameters
= NULL_TREE
;
21581 tree
*tail
= ¶meters
;
21582 bool saved_in_unbraced_linkage_specification_p
;
21585 /* The special considerations that apply to a function within an
21586 unbraced linkage specifications do not apply to the parameters
21587 to the function. */
21588 saved_in_unbraced_linkage_specification_p
21589 = parser
->in_unbraced_linkage_specification_p
;
21590 parser
->in_unbraced_linkage_specification_p
= false;
21592 /* Look for more parameters. */
21595 cp_parameter_declarator
*parameter
;
21596 tree decl
= error_mark_node
;
21597 bool parenthesized_p
= false;
21599 /* Parse the parameter. */
21601 = cp_parser_parameter_declaration (parser
,
21602 /*template_parm_p=*/false,
21605 /* We don't know yet if the enclosing context is deprecated, so wait
21606 and warn in grokparms if appropriate. */
21607 deprecated_state
= DEPRECATED_SUPPRESS
;
21611 decl
= grokdeclarator (parameter
->declarator
,
21612 ¶meter
->decl_specifiers
,
21614 parameter
->default_argument
!= NULL_TREE
,
21615 ¶meter
->decl_specifiers
.attributes
);
21616 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21617 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21620 deprecated_state
= DEPRECATED_NORMAL
;
21622 /* If a parse error occurred parsing the parameter declaration,
21623 then the entire parameter-declaration-list is erroneous. */
21624 if (decl
== error_mark_node
)
21626 parameters
= error_mark_node
;
21630 if (parameter
->decl_specifiers
.attributes
)
21631 cplus_decl_attributes (&decl
,
21632 parameter
->decl_specifiers
.attributes
,
21634 if (DECL_NAME (decl
))
21635 decl
= pushdecl (decl
);
21637 if (decl
!= error_mark_node
)
21639 retrofit_lang_decl (decl
);
21640 DECL_PARM_INDEX (decl
) = ++index
;
21641 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21644 /* Add the new parameter to the list. */
21645 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21646 tail
= &TREE_CHAIN (*tail
);
21648 /* Peek at the next token. */
21649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21650 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21651 /* These are for Objective-C++ */
21652 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21653 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21654 /* The parameter-declaration-list is complete. */
21656 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21660 /* Peek at the next token. */
21661 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21662 /* If it's an ellipsis, then the list is complete. */
21663 if (token
->type
== CPP_ELLIPSIS
)
21665 /* Otherwise, there must be more parameters. Consume the
21667 cp_lexer_consume_token (parser
->lexer
);
21668 /* When parsing something like:
21670 int i(float f, double d)
21672 we can tell after seeing the declaration for "f" that we
21673 are not looking at an initialization of a variable "i",
21674 but rather at the declaration of a function "i".
21676 Due to the fact that the parsing of template arguments
21677 (as specified to a template-id) requires backtracking we
21678 cannot use this technique when inside a template argument
21680 if (!parser
->in_template_argument_list_p
21681 && !parser
->in_type_id_in_expr_p
21682 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21683 /* However, a parameter-declaration of the form
21684 "float(f)" (which is a valid declaration of a
21685 parameter "f") can also be interpreted as an
21686 expression (the conversion of "f" to "float"). */
21687 && !parenthesized_p
)
21688 cp_parser_commit_to_tentative_parse (parser
);
21692 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21693 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21694 cp_parser_skip_to_closing_parenthesis (parser
,
21695 /*recovering=*/true,
21696 /*or_comma=*/false,
21697 /*consume_paren=*/false);
21702 parser
->in_unbraced_linkage_specification_p
21703 = saved_in_unbraced_linkage_specification_p
;
21705 /* Reset implicit_template_scope if we are about to leave the function
21706 parameter list that introduced it. Note that for out-of-line member
21707 definitions, there will be one or more class scopes before we get to
21708 the template parameter scope. */
21710 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21711 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21713 while (maybe_its
->kind
== sk_class
)
21714 maybe_its
= maybe_its
->level_chain
;
21715 if (maybe_its
== its
)
21717 parser
->implicit_template_parms
= 0;
21718 parser
->implicit_template_scope
= 0;
21725 /* Parse a parameter declaration.
21727 parameter-declaration:
21728 decl-specifier-seq ... [opt] declarator
21729 decl-specifier-seq declarator = assignment-expression
21730 decl-specifier-seq ... [opt] abstract-declarator [opt]
21731 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21733 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21734 declares a template parameter. (In that case, a non-nested `>'
21735 token encountered during the parsing of the assignment-expression
21736 is not interpreted as a greater-than operator.)
21738 Returns a representation of the parameter, or NULL if an error
21739 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21740 true iff the declarator is of the form "(p)". */
21742 static cp_parameter_declarator
*
21743 cp_parser_parameter_declaration (cp_parser
*parser
,
21744 bool template_parm_p
,
21745 bool *parenthesized_p
)
21747 int declares_class_or_enum
;
21748 cp_decl_specifier_seq decl_specifiers
;
21749 cp_declarator
*declarator
;
21750 tree default_argument
;
21751 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21752 const char *saved_message
;
21753 bool template_parameter_pack_p
= false;
21755 /* In a template parameter, `>' is not an operator.
21759 When parsing a default template-argument for a non-type
21760 template-parameter, the first non-nested `>' is taken as the end
21761 of the template parameter-list rather than a greater-than
21764 /* Type definitions may not appear in parameter types. */
21765 saved_message
= parser
->type_definition_forbidden_message
;
21766 parser
->type_definition_forbidden_message
21767 = G_("types may not be defined in parameter types");
21769 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21770 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21771 (current_template_parms
)) : 0);
21773 /* Parse the declaration-specifiers. */
21774 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21775 cp_parser_decl_specifier_seq (parser
,
21776 CP_PARSER_FLAGS_NONE
,
21778 &declares_class_or_enum
);
21780 /* Complain about missing 'typename' or other invalid type names. */
21781 if (!decl_specifiers
.any_type_specifiers_p
21782 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21783 decl_specifiers
.type
= error_mark_node
;
21785 /* If an error occurred, there's no reason to attempt to parse the
21786 rest of the declaration. */
21787 if (cp_parser_error_occurred (parser
))
21789 parser
->type_definition_forbidden_message
= saved_message
;
21793 /* Peek at the next token. */
21794 token
= cp_lexer_peek_token (parser
->lexer
);
21796 /* If the next token is a `)', `,', `=', `>', or `...', then there
21797 is no declarator. However, when variadic templates are enabled,
21798 there may be a declarator following `...'. */
21799 if (token
->type
== CPP_CLOSE_PAREN
21800 || token
->type
== CPP_COMMA
21801 || token
->type
== CPP_EQ
21802 || token
->type
== CPP_GREATER
)
21805 if (parenthesized_p
)
21806 *parenthesized_p
= false;
21808 /* Otherwise, there should be a declarator. */
21811 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21812 parser
->default_arg_ok_p
= false;
21814 /* After seeing a decl-specifier-seq, if the next token is not a
21815 "(", there is no possibility that the code is a valid
21816 expression. Therefore, if parsing tentatively, we commit at
21818 if (!parser
->in_template_argument_list_p
21819 /* In an expression context, having seen:
21823 we cannot be sure whether we are looking at a
21824 function-type (taking a "char" as a parameter) or a cast
21825 of some object of type "char" to "int". */
21826 && !parser
->in_type_id_in_expr_p
21827 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21828 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21829 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21830 cp_parser_commit_to_tentative_parse (parser
);
21831 /* Parse the declarator. */
21832 declarator_token_start
= token
;
21833 declarator
= cp_parser_declarator (parser
,
21834 CP_PARSER_DECLARATOR_EITHER
,
21835 /*ctor_dtor_or_conv_p=*/NULL
,
21837 /*member_p=*/false,
21838 /*friend_p=*/false);
21839 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21840 /* After the declarator, allow more attributes. */
21841 decl_specifiers
.attributes
21842 = attr_chainon (decl_specifiers
.attributes
,
21843 cp_parser_attributes_opt (parser
));
21845 /* If the declarator is a template parameter pack, remember that and
21846 clear the flag in the declarator itself so we don't get errors
21847 from grokdeclarator. */
21848 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21850 declarator
->parameter_pack_p
= false;
21851 template_parameter_pack_p
= true;
21855 /* If the next token is an ellipsis, and we have not seen a declarator
21856 name, and if either the type of the declarator contains parameter
21857 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21858 for, eg, abbreviated integral type names), then we actually have a
21859 parameter pack expansion expression. Otherwise, leave the ellipsis
21860 for a C-style variadic function. */
21861 token
= cp_lexer_peek_token (parser
->lexer
);
21863 /* If a function parameter pack was specified and an implicit template
21864 parameter was introduced during cp_parser_parameter_declaration,
21865 change any implicit parameters introduced into packs. */
21866 if (parser
->implicit_template_parms
21867 && ((token
->type
== CPP_ELLIPSIS
21868 && declarator_can_be_parameter_pack (declarator
))
21869 || (declarator
&& declarator
->parameter_pack_p
)))
21871 int latest_template_parm_idx
= TREE_VEC_LENGTH
21872 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21874 if (latest_template_parm_idx
!= template_parm_idx
)
21875 decl_specifiers
.type
= convert_generic_types_to_packs
21876 (decl_specifiers
.type
,
21877 template_parm_idx
, latest_template_parm_idx
);
21880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21882 tree type
= decl_specifiers
.type
;
21884 if (type
&& DECL_P (type
))
21885 type
= TREE_TYPE (type
);
21888 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21889 && (template_parm_p
|| uses_parameter_packs (type
)))
21890 || (!type
&& template_parm_p
))
21891 && declarator_can_be_parameter_pack (declarator
))
21893 /* Consume the `...'. */
21894 cp_lexer_consume_token (parser
->lexer
);
21895 maybe_warn_variadic_templates ();
21897 /* Build a pack expansion type */
21898 if (template_parm_p
)
21899 template_parameter_pack_p
= true;
21900 else if (declarator
)
21901 declarator
->parameter_pack_p
= true;
21903 decl_specifiers
.type
= make_pack_expansion (type
);
21907 /* The restriction on defining new types applies only to the type
21908 of the parameter, not to the default argument. */
21909 parser
->type_definition_forbidden_message
= saved_message
;
21911 /* If the next token is `=', then process a default argument. */
21912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21914 tree type
= decl_specifiers
.type
;
21915 token
= cp_lexer_peek_token (parser
->lexer
);
21916 /* If we are defining a class, then the tokens that make up the
21917 default argument must be saved and processed later. */
21918 if (!template_parm_p
&& at_class_scope_p ()
21919 && TYPE_BEING_DEFINED (current_class_type
)
21920 && !LAMBDA_TYPE_P (current_class_type
))
21921 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21923 // A constrained-type-specifier may declare a type template-parameter.
21924 else if (declares_constrained_type_template_parameter (type
))
21926 = cp_parser_default_type_template_argument (parser
);
21928 // A constrained-type-specifier may declare a template-template-parameter.
21929 else if (declares_constrained_template_template_parameter (type
))
21931 = cp_parser_default_template_template_argument (parser
);
21933 /* Outside of a class definition, we can just parse the
21934 assignment-expression. */
21937 = cp_parser_default_argument (parser
, template_parm_p
);
21939 if (!parser
->default_arg_ok_p
)
21941 permerror (token
->location
,
21942 "default arguments are only "
21943 "permitted for function parameters");
21945 else if ((declarator
&& declarator
->parameter_pack_p
)
21946 || template_parameter_pack_p
21947 || (decl_specifiers
.type
21948 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21950 /* Find the name of the parameter pack. */
21951 cp_declarator
*id_declarator
= declarator
;
21952 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21953 id_declarator
= id_declarator
->declarator
;
21955 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21956 error_at (declarator_token_start
->location
,
21958 ? G_("template parameter pack %qD "
21959 "cannot have a default argument")
21960 : G_("parameter pack %qD cannot have "
21961 "a default argument"),
21962 id_declarator
->u
.id
.unqualified_name
);
21964 error_at (declarator_token_start
->location
,
21966 ? G_("template parameter pack cannot have "
21967 "a default argument")
21968 : G_("parameter pack cannot have a "
21969 "default argument"));
21971 default_argument
= NULL_TREE
;
21975 default_argument
= NULL_TREE
;
21977 /* Generate a location for the parameter, ranging from the start of the
21978 initial token to the end of the final token (using input_location for
21979 the latter, set up by cp_lexer_set_source_position_from_token when
21982 If we have a identifier, then use it for the caret location, e.g.
21984 extern int callee (int one, int (*two)(int, int), float three);
21985 ~~~~~~^~~~~~~~~~~~~~
21987 otherwise, reuse the start location for the caret location e.g.:
21989 extern int callee (int one, int (*)(int, int), float three);
21993 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21994 ? declarator
->id_loc
21995 : decl_spec_token_start
->location
);
21996 location_t param_loc
= make_location (caret_loc
,
21997 decl_spec_token_start
->location
,
22000 return make_parameter_declarator (&decl_specifiers
,
22004 template_parameter_pack_p
);
22007 /* Parse a default argument and return it.
22009 TEMPLATE_PARM_P is true if this is a default argument for a
22010 non-type template parameter. */
22012 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22014 tree default_argument
= NULL_TREE
;
22015 bool saved_greater_than_is_operator_p
;
22016 bool saved_local_variables_forbidden_p
;
22017 bool non_constant_p
, is_direct_init
;
22019 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22021 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22022 parser
->greater_than_is_operator_p
= !template_parm_p
;
22023 /* Local variable names (and the `this' keyword) may not
22024 appear in a default argument. */
22025 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22026 parser
->local_variables_forbidden_p
= true;
22027 /* Parse the assignment-expression. */
22028 if (template_parm_p
)
22029 push_deferring_access_checks (dk_no_deferred
);
22030 tree saved_class_ptr
= NULL_TREE
;
22031 tree saved_class_ref
= NULL_TREE
;
22032 /* The "this" pointer is not valid in a default argument. */
22035 saved_class_ptr
= current_class_ptr
;
22036 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22037 saved_class_ref
= current_class_ref
;
22038 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22041 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22042 /* Restore the "this" pointer. */
22045 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22046 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22048 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22049 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22050 if (template_parm_p
)
22051 pop_deferring_access_checks ();
22052 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22053 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22055 return default_argument
;
22058 /* Parse a function-body.
22061 compound_statement */
22064 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22066 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22067 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22071 /* Parse a ctor-initializer-opt followed by a function-body. Return
22072 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22073 is true we are parsing a function-try-block. */
22076 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22077 bool in_function_try_block
)
22080 const bool check_body_p
=
22081 DECL_CONSTRUCTOR_P (current_function_decl
)
22082 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22085 /* Begin the function body. */
22086 body
= begin_function_body ();
22087 /* Parse the optional ctor-initializer. */
22088 cp_parser_ctor_initializer_opt (parser
);
22090 /* If we're parsing a constexpr constructor definition, we need
22091 to check that the constructor body is indeed empty. However,
22092 before we get to cp_parser_function_body lot of junk has been
22093 generated, so we can't just check that we have an empty block.
22094 Rather we take a snapshot of the outermost block, and check whether
22095 cp_parser_function_body changed its state. */
22098 list
= cur_stmt_list
;
22099 if (STATEMENT_LIST_TAIL (list
))
22100 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22102 /* Parse the function-body. */
22103 cp_parser_function_body (parser
, in_function_try_block
);
22105 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22106 /* Finish the function body. */
22107 finish_function_body (body
);
22110 /* Parse an initializer.
22113 = initializer-clause
22114 ( expression-list )
22116 Returns an expression representing the initializer. If no
22117 initializer is present, NULL_TREE is returned.
22119 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22120 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22121 set to TRUE if there is no initializer present. If there is an
22122 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22123 is set to true; otherwise it is set to false. */
22126 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22127 bool* non_constant_p
, bool subexpression_p
)
22132 /* Peek at the next token. */
22133 token
= cp_lexer_peek_token (parser
->lexer
);
22135 /* Let our caller know whether or not this initializer was
22137 *is_direct_init
= (token
->type
!= CPP_EQ
);
22138 /* Assume that the initializer is constant. */
22139 *non_constant_p
= false;
22141 if (token
->type
== CPP_EQ
)
22143 /* Consume the `='. */
22144 cp_lexer_consume_token (parser
->lexer
);
22145 /* Parse the initializer-clause. */
22146 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22148 else if (token
->type
== CPP_OPEN_PAREN
)
22150 vec
<tree
, va_gc
> *vec
;
22151 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22153 /*allow_expansion_p=*/true,
22156 return error_mark_node
;
22157 init
= build_tree_list_vec (vec
);
22158 release_tree_vector (vec
);
22160 else if (token
->type
== CPP_OPEN_BRACE
)
22162 cp_lexer_set_source_position (parser
->lexer
);
22163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22164 init
= cp_parser_braced_list (parser
, non_constant_p
);
22165 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22169 /* Anything else is an error. */
22170 cp_parser_error (parser
, "expected initializer");
22171 init
= error_mark_node
;
22174 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22175 init
= error_mark_node
;
22180 /* Parse an initializer-clause.
22182 initializer-clause:
22183 assignment-expression
22186 Returns an expression representing the initializer.
22188 If the `assignment-expression' production is used the value
22189 returned is simply a representation for the expression.
22191 Otherwise, calls cp_parser_braced_list. */
22194 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22196 cp_expr initializer
;
22198 /* Assume the expression is constant. */
22199 *non_constant_p
= false;
22201 /* If it is not a `{', then we are looking at an
22202 assignment-expression. */
22203 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22206 = cp_parser_constant_expression (parser
,
22207 /*allow_non_constant_p=*/true,
22211 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22213 return initializer
;
22216 /* Parse a brace-enclosed initializer list.
22219 { initializer-list , [opt] }
22220 { designated-initializer-list , [opt] }
22223 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22224 the elements of the initializer-list (or NULL, if the last
22225 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22226 NULL_TREE. There is no way to detect whether or not the optional
22227 trailing `,' was provided. NON_CONSTANT_P is as for
22228 cp_parser_initializer. */
22231 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22234 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22236 /* Consume the `{' token. */
22237 matching_braces braces
;
22238 braces
.require_open (parser
);
22239 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22240 initializer
= make_node (CONSTRUCTOR
);
22241 /* If it's not a `}', then there is a non-trivial initializer. */
22242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22244 /* Parse the initializer list. */
22245 CONSTRUCTOR_ELTS (initializer
)
22246 = cp_parser_initializer_list (parser
, non_constant_p
);
22247 /* A trailing `,' token is allowed. */
22248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22249 cp_lexer_consume_token (parser
->lexer
);
22252 *non_constant_p
= false;
22253 /* Now, there should be a trailing `}'. */
22254 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22255 braces
.require_close (parser
);
22256 TREE_TYPE (initializer
) = init_list_type_node
;
22258 cp_expr
result (initializer
);
22259 /* Build a location of the form:
22262 with caret==start at the open brace, finish at the close brace. */
22263 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22264 result
.set_location (combined_loc
);
22268 /* Consume tokens up to, and including, the next non-nested closing `]'.
22269 Returns true iff we found a closing `]'. */
22272 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22274 unsigned square_depth
= 0;
22278 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22280 switch (token
->type
)
22283 case CPP_PRAGMA_EOL
:
22284 /* If we've run out of tokens, then there is no closing `]'. */
22287 case CPP_OPEN_SQUARE
:
22291 case CPP_CLOSE_SQUARE
:
22292 if (!square_depth
--)
22294 cp_lexer_consume_token (parser
->lexer
);
22303 /* Consume the token. */
22304 cp_lexer_consume_token (parser
->lexer
);
22308 /* Return true if we are looking at an array-designator, false otherwise. */
22311 cp_parser_array_designator_p (cp_parser
*parser
)
22313 /* Consume the `['. */
22314 cp_lexer_consume_token (parser
->lexer
);
22316 cp_lexer_save_tokens (parser
->lexer
);
22318 /* Skip tokens until the next token is a closing square bracket.
22319 If we find the closing `]', and the next token is a `=', then
22320 we are looking at an array designator. */
22321 bool array_designator_p
22322 = (cp_parser_skip_to_closing_square_bracket (parser
)
22323 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22325 /* Roll back the tokens we skipped. */
22326 cp_lexer_rollback_tokens (parser
->lexer
);
22328 return array_designator_p
;
22331 /* Parse an initializer-list.
22334 initializer-clause ... [opt]
22335 initializer-list , initializer-clause ... [opt]
22339 designated-initializer-list:
22340 designated-initializer-clause
22341 designated-initializer-list , designated-initializer-clause
22343 designated-initializer-clause:
22344 designator brace-or-equal-initializer
22352 designation initializer-clause ...[opt]
22353 initializer-list , designation initializer-clause ...[opt]
22358 [ constant-expression ] =
22360 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22361 for the initializer. If the INDEX of the elt is non-NULL, it is the
22362 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22363 as for cp_parser_initializer. */
22365 static vec
<constructor_elt
, va_gc
> *
22366 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22368 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22369 bool first_p
= true;
22370 tree first_designator
= NULL_TREE
;
22372 /* Assume all of the expressions are constant. */
22373 *non_constant_p
= false;
22375 /* Parse the rest of the list. */
22381 bool clause_non_constant_p
;
22382 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22384 /* Handle the C++2A syntax, '. id ='. */
22385 if ((cxx_dialect
>= cxx2a
22386 || cp_parser_allow_gnu_extensions_p (parser
))
22387 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22388 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22389 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22390 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22391 == CPP_OPEN_BRACE
)))
22393 if (cxx_dialect
< cxx2a
)
22394 pedwarn (loc
, OPT_Wpedantic
,
22395 "C++ designated initializers only available with "
22396 "-std=c++2a or -std=gnu++2a");
22397 /* Consume the `.'. */
22398 cp_lexer_consume_token (parser
->lexer
);
22399 /* Consume the identifier. */
22400 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22402 /* Consume the `='. */
22403 cp_lexer_consume_token (parser
->lexer
);
22405 /* Also, if the next token is an identifier and the following one is a
22406 colon, we are looking at the GNU designated-initializer
22408 else if (cp_parser_allow_gnu_extensions_p (parser
)
22409 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22410 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22413 /* Warn the user that they are using an extension. */
22414 pedwarn (loc
, OPT_Wpedantic
,
22415 "ISO C++ does not allow GNU designated initializers");
22416 /* Consume the identifier. */
22417 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22418 /* Consume the `:'. */
22419 cp_lexer_consume_token (parser
->lexer
);
22421 /* Also handle C99 array designators, '[ const ] ='. */
22422 else if (cp_parser_allow_gnu_extensions_p (parser
)
22423 && !c_dialect_objc ()
22424 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22426 /* In C++11, [ could start a lambda-introducer. */
22427 bool non_const
= false;
22429 cp_parser_parse_tentatively (parser
);
22431 if (!cp_parser_array_designator_p (parser
))
22433 cp_parser_simulate_error (parser
);
22434 designator
= NULL_TREE
;
22438 designator
= cp_parser_constant_expression (parser
, true,
22440 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22441 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22444 if (!cp_parser_parse_definitely (parser
))
22445 designator
= NULL_TREE
;
22447 && (!require_potential_rvalue_constant_expression
22449 designator
= NULL_TREE
;
22451 /* Warn the user that they are using an extension. */
22452 pedwarn (loc
, OPT_Wpedantic
,
22453 "ISO C++ does not allow C99 designated initializers");
22456 designator
= NULL_TREE
;
22460 first_designator
= designator
;
22463 else if (cxx_dialect
>= cxx2a
22464 && first_designator
!= error_mark_node
22465 && (!first_designator
!= !designator
))
22467 error_at (loc
, "either all initializer clauses should be designated "
22468 "or none of them should be");
22469 first_designator
= error_mark_node
;
22471 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22472 first_designator
= designator
;
22474 /* Parse the initializer. */
22475 initializer
= cp_parser_initializer_clause (parser
,
22476 &clause_non_constant_p
);
22477 /* If any clause is non-constant, so is the entire initializer. */
22478 if (clause_non_constant_p
)
22479 *non_constant_p
= true;
22481 /* If we have an ellipsis, this is an initializer pack
22483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22485 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22487 /* Consume the `...'. */
22488 cp_lexer_consume_token (parser
->lexer
);
22490 if (designator
&& cxx_dialect
>= cxx2a
)
22492 "%<...%> not allowed in designated initializer list");
22494 /* Turn the initializer into an initializer expansion. */
22495 initializer
= make_pack_expansion (initializer
);
22498 /* Add it to the vector. */
22499 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22501 /* If the next token is not a comma, we have reached the end of
22503 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22506 /* Peek at the next token. */
22507 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22508 /* If the next token is a `}', then we're still done. An
22509 initializer-clause can have a trailing `,' after the
22510 initializer-list and before the closing `}'. */
22511 if (token
->type
== CPP_CLOSE_BRACE
)
22514 /* Consume the `,' token. */
22515 cp_lexer_consume_token (parser
->lexer
);
22518 /* The same identifier shall not appear in multiple designators
22519 of a designated-initializer-list. */
22520 if (first_designator
)
22523 tree designator
, val
;
22524 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22525 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22527 if (IDENTIFIER_MARKED (designator
))
22529 error_at (cp_expr_loc_or_loc (val
, input_location
),
22530 "%<.%s%> designator used multiple times in "
22531 "the same initializer list",
22532 IDENTIFIER_POINTER (designator
));
22533 (*v
)[i
].index
= error_mark_node
;
22536 IDENTIFIER_MARKED (designator
) = 1;
22538 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22539 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22540 IDENTIFIER_MARKED (designator
) = 0;
22546 /* Classes [gram.class] */
22548 /* Parse a class-name.
22554 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22555 to indicate that names looked up in dependent types should be
22556 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22557 keyword has been used to indicate that the name that appears next
22558 is a template. TAG_TYPE indicates the explicit tag given before
22559 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22560 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22561 is the class being defined in a class-head. If ENUM_OK is TRUE,
22562 enum-names are also accepted.
22564 Returns the TYPE_DECL representing the class. */
22567 cp_parser_class_name (cp_parser
*parser
,
22568 bool typename_keyword_p
,
22569 bool template_keyword_p
,
22570 enum tag_types tag_type
,
22571 bool check_dependency_p
,
22573 bool is_declaration
,
22580 tree identifier
= NULL_TREE
;
22582 /* All class-names start with an identifier. */
22583 token
= cp_lexer_peek_token (parser
->lexer
);
22584 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22586 cp_parser_error (parser
, "expected class-name");
22587 return error_mark_node
;
22590 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22591 to a template-id, so we save it here. */
22592 scope
= parser
->scope
;
22593 if (scope
== error_mark_node
)
22594 return error_mark_node
;
22596 /* Any name names a type if we're following the `typename' keyword
22597 in a qualified name where the enclosing scope is type-dependent. */
22598 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22599 && dependent_type_p (scope
));
22600 /* Handle the common case (an identifier, but not a template-id)
22602 if (token
->type
== CPP_NAME
22603 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22605 cp_token
*identifier_token
;
22608 /* Look for the identifier. */
22609 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22610 ambiguous_p
= identifier_token
->error_reported
;
22611 identifier
= cp_parser_identifier (parser
);
22612 /* If the next token isn't an identifier, we are certainly not
22613 looking at a class-name. */
22614 if (identifier
== error_mark_node
)
22615 decl
= error_mark_node
;
22616 /* If we know this is a type-name, there's no need to look it
22618 else if (typename_p
)
22622 tree ambiguous_decls
;
22623 /* If we already know that this lookup is ambiguous, then
22624 we've already issued an error message; there's no reason
22628 cp_parser_simulate_error (parser
);
22629 return error_mark_node
;
22631 /* If the next token is a `::', then the name must be a type
22634 [basic.lookup.qual]
22636 During the lookup for a name preceding the :: scope
22637 resolution operator, object, function, and enumerator
22638 names are ignored. */
22639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22640 tag_type
= scope_type
;
22641 /* Look up the name. */
22642 decl
= cp_parser_lookup_name (parser
, identifier
,
22644 /*is_template=*/false,
22645 /*is_namespace=*/false,
22646 check_dependency_p
,
22648 identifier_token
->location
);
22649 if (ambiguous_decls
)
22651 if (cp_parser_parsing_tentatively (parser
))
22652 cp_parser_simulate_error (parser
);
22653 return error_mark_node
;
22659 /* Try a template-id. */
22660 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22661 check_dependency_p
,
22664 if (decl
== error_mark_node
)
22665 return error_mark_node
;
22668 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22670 /* If this is a typename, create a TYPENAME_TYPE. */
22671 if (typename_p
&& decl
!= error_mark_node
)
22673 decl
= make_typename_type (scope
, decl
, typename_type
,
22674 /*complain=*/tf_error
);
22675 if (decl
!= error_mark_node
)
22676 decl
= TYPE_NAME (decl
);
22679 decl
= strip_using_decl (decl
);
22681 /* Check to see that it is really the name of a class. */
22682 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22683 && identifier_p (TREE_OPERAND (decl
, 0))
22684 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22685 /* Situations like this:
22687 template <typename T> struct A {
22688 typename T::template X<int>::I i;
22691 are problematic. Is `T::template X<int>' a class-name? The
22692 standard does not seem to be definitive, but there is no other
22693 valid interpretation of the following `::'. Therefore, those
22694 names are considered class-names. */
22696 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22697 if (decl
!= error_mark_node
)
22698 decl
= TYPE_NAME (decl
);
22700 else if (TREE_CODE (decl
) != TYPE_DECL
22701 || TREE_TYPE (decl
) == error_mark_node
22702 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22703 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22704 /* In Objective-C 2.0, a classname followed by '.' starts a
22705 dot-syntax expression, and it's not a type-name. */
22706 || (c_dialect_objc ()
22707 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22708 && objc_is_class_name (decl
)))
22709 decl
= error_mark_node
;
22711 if (decl
== error_mark_node
)
22712 cp_parser_error (parser
, "expected class-name");
22713 else if (identifier
&& !parser
->scope
)
22714 maybe_note_name_used_in_class (identifier
, decl
);
22719 /* Parse a class-specifier.
22722 class-head { member-specification [opt] }
22724 Returns the TREE_TYPE representing the class. */
22727 cp_parser_class_specifier_1 (cp_parser
* parser
)
22730 tree attributes
= NULL_TREE
;
22731 bool nested_name_specifier_p
;
22732 unsigned saved_num_template_parameter_lists
;
22733 bool saved_in_function_body
;
22734 unsigned char in_statement
;
22735 bool in_switch_statement_p
;
22736 bool saved_in_unbraced_linkage_specification_p
;
22737 tree old_scope
= NULL_TREE
;
22738 tree scope
= NULL_TREE
;
22739 cp_token
*closing_brace
;
22741 push_deferring_access_checks (dk_no_deferred
);
22743 /* Parse the class-head. */
22744 type
= cp_parser_class_head (parser
,
22745 &nested_name_specifier_p
);
22746 /* If the class-head was a semantic disaster, skip the entire body
22750 cp_parser_skip_to_end_of_block_or_statement (parser
);
22751 pop_deferring_access_checks ();
22752 return error_mark_node
;
22755 /* Look for the `{'. */
22756 matching_braces braces
;
22757 if (!braces
.require_open (parser
))
22759 pop_deferring_access_checks ();
22760 return error_mark_node
;
22763 cp_ensure_no_omp_declare_simd (parser
);
22764 cp_ensure_no_oacc_routine (parser
);
22766 /* Issue an error message if type-definitions are forbidden here. */
22767 cp_parser_check_type_definition (parser
);
22768 /* Remember that we are defining one more class. */
22769 ++parser
->num_classes_being_defined
;
22770 /* Inside the class, surrounding template-parameter-lists do not
22772 saved_num_template_parameter_lists
22773 = parser
->num_template_parameter_lists
;
22774 parser
->num_template_parameter_lists
= 0;
22775 /* We are not in a function body. */
22776 saved_in_function_body
= parser
->in_function_body
;
22777 parser
->in_function_body
= false;
22778 /* Or in a loop. */
22779 in_statement
= parser
->in_statement
;
22780 parser
->in_statement
= 0;
22781 /* Or in a switch. */
22782 in_switch_statement_p
= parser
->in_switch_statement_p
;
22783 parser
->in_switch_statement_p
= false;
22784 /* We are not immediately inside an extern "lang" block. */
22785 saved_in_unbraced_linkage_specification_p
22786 = parser
->in_unbraced_linkage_specification_p
;
22787 parser
->in_unbraced_linkage_specification_p
= false;
22789 // Associate constraints with the type.
22791 type
= associate_classtype_constraints (type
);
22793 /* Start the class. */
22794 if (nested_name_specifier_p
)
22796 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22797 old_scope
= push_inner_scope (scope
);
22799 type
= begin_class_definition (type
);
22801 if (type
== error_mark_node
)
22802 /* If the type is erroneous, skip the entire body of the class. */
22803 cp_parser_skip_to_closing_brace (parser
);
22805 /* Parse the member-specification. */
22806 cp_parser_member_specification_opt (parser
);
22808 /* Look for the trailing `}'. */
22809 closing_brace
= braces
.require_close (parser
);
22810 /* Look for trailing attributes to apply to this class. */
22811 if (cp_parser_allow_gnu_extensions_p (parser
))
22812 attributes
= cp_parser_gnu_attributes_opt (parser
);
22813 if (type
!= error_mark_node
)
22814 type
= finish_struct (type
, attributes
);
22815 if (nested_name_specifier_p
)
22816 pop_inner_scope (old_scope
, scope
);
22818 /* We've finished a type definition. Check for the common syntax
22819 error of forgetting a semicolon after the definition. We need to
22820 be careful, as we can't just check for not-a-semicolon and be done
22821 with it; the user might have typed:
22823 class X { } c = ...;
22824 class X { } *p = ...;
22826 and so forth. Instead, enumerate all the possible tokens that
22827 might follow this production; if we don't see one of them, then
22828 complain and silently insert the semicolon. */
22830 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22831 bool want_semicolon
= true;
22833 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22834 /* Don't try to parse c++11 attributes here. As per the
22835 grammar, that should be a task for
22836 cp_parser_decl_specifier_seq. */
22837 want_semicolon
= false;
22839 switch (token
->type
)
22842 case CPP_SEMICOLON
:
22845 case CPP_OPEN_PAREN
:
22846 case CPP_CLOSE_PAREN
:
22848 want_semicolon
= false;
22851 /* While it's legal for type qualifiers and storage class
22852 specifiers to follow type definitions in the grammar, only
22853 compiler testsuites contain code like that. Assume that if
22854 we see such code, then what we're really seeing is a case
22858 const <type> var = ...;
22863 static <type> func (...) ...
22865 i.e. the qualifier or specifier applies to the next
22866 declaration. To do so, however, we need to look ahead one
22867 more token to see if *that* token is a type specifier.
22869 This code could be improved to handle:
22872 static const <type> var = ...; */
22874 if (keyword_is_decl_specifier (token
->keyword
))
22876 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22878 /* Handling user-defined types here would be nice, but very
22881 = (lookahead
->type
== CPP_KEYWORD
22882 && keyword_begins_type_specifier (lookahead
->keyword
));
22889 /* If we don't have a type, then something is very wrong and we
22890 shouldn't try to do anything clever. Likewise for not seeing the
22892 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22894 /* Locate the closing brace. */
22895 cp_token_position prev
22896 = cp_lexer_previous_token_position (parser
->lexer
);
22897 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22898 location_t loc
= prev_token
->location
;
22900 /* We want to suggest insertion of a ';' immediately *after* the
22901 closing brace, so, if we can, offset the location by 1 column. */
22902 location_t next_loc
= loc
;
22903 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22904 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22906 rich_location
richloc (line_table
, next_loc
);
22908 /* If we successfully offset the location, suggest the fix-it. */
22909 if (next_loc
!= loc
)
22910 richloc
.add_fixit_insert_before (next_loc
, ";");
22912 if (CLASSTYPE_DECLARED_CLASS (type
))
22913 error_at (&richloc
,
22914 "expected %<;%> after class definition");
22915 else if (TREE_CODE (type
) == RECORD_TYPE
)
22916 error_at (&richloc
,
22917 "expected %<;%> after struct definition");
22918 else if (TREE_CODE (type
) == UNION_TYPE
)
22919 error_at (&richloc
,
22920 "expected %<;%> after union definition");
22922 gcc_unreachable ();
22924 /* Unget one token and smash it to look as though we encountered
22925 a semicolon in the input stream. */
22926 cp_lexer_set_token_position (parser
->lexer
, prev
);
22927 token
= cp_lexer_peek_token (parser
->lexer
);
22928 token
->type
= CPP_SEMICOLON
;
22929 token
->keyword
= RID_MAX
;
22933 /* If this class is not itself within the scope of another class,
22934 then we need to parse the bodies of all of the queued function
22935 definitions. Note that the queued functions defined in a class
22936 are not always processed immediately following the
22937 class-specifier for that class. Consider:
22940 struct B { void f() { sizeof (A); } };
22943 If `f' were processed before the processing of `A' were
22944 completed, there would be no way to compute the size of `A'.
22945 Note that the nesting we are interested in here is lexical --
22946 not the semantic nesting given by TYPE_CONTEXT. In particular,
22949 struct A { struct B; };
22950 struct A::B { void f() { } };
22952 there is no need to delay the parsing of `A::B::f'. */
22953 if (--parser
->num_classes_being_defined
== 0)
22956 tree class_type
= NULL_TREE
;
22957 tree pushed_scope
= NULL_TREE
;
22959 cp_default_arg_entry
*e
;
22960 tree save_ccp
, save_ccr
;
22962 if (any_erroneous_template_args_p (type
))
22964 /* Skip default arguments, NSDMIs, etc, in order to improve
22965 error recovery (c++/71169, c++/71832). */
22966 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22967 vec_safe_truncate (unparsed_nsdmis
, 0);
22968 vec_safe_truncate (unparsed_classes
, 0);
22969 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22972 /* In a first pass, parse default arguments to the functions.
22973 Then, in a second pass, parse the bodies of the functions.
22974 This two-phased approach handles cases like:
22982 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22985 /* If there are default arguments that have not yet been processed,
22986 take care of them now. */
22987 if (class_type
!= e
->class_type
)
22990 pop_scope (pushed_scope
);
22991 class_type
= e
->class_type
;
22992 pushed_scope
= push_scope (class_type
);
22994 /* Make sure that any template parameters are in scope. */
22995 maybe_begin_member_template_processing (decl
);
22996 /* Parse the default argument expressions. */
22997 cp_parser_late_parsing_default_args (parser
, decl
);
22998 /* Remove any template parameters from the symbol table. */
22999 maybe_end_member_template_processing ();
23001 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23002 /* Now parse any NSDMIs. */
23003 save_ccp
= current_class_ptr
;
23004 save_ccr
= current_class_ref
;
23005 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23007 if (class_type
!= DECL_CONTEXT (decl
))
23010 pop_scope (pushed_scope
);
23011 class_type
= DECL_CONTEXT (decl
);
23012 pushed_scope
= push_scope (class_type
);
23014 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23015 cp_parser_late_parsing_nsdmi (parser
, decl
);
23017 vec_safe_truncate (unparsed_nsdmis
, 0);
23018 current_class_ptr
= save_ccp
;
23019 current_class_ref
= save_ccr
;
23021 pop_scope (pushed_scope
);
23023 /* Now do some post-NSDMI bookkeeping. */
23024 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23025 after_nsdmi_defaulted_late_checks (class_type
);
23026 vec_safe_truncate (unparsed_classes
, 0);
23027 after_nsdmi_defaulted_late_checks (type
);
23029 /* Now parse the body of the functions. */
23032 /* OpenMP UDRs need to be parsed before all other functions. */
23033 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23034 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23035 cp_parser_late_parsing_for_member (parser
, decl
);
23036 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23037 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23038 cp_parser_late_parsing_for_member (parser
, decl
);
23041 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23042 cp_parser_late_parsing_for_member (parser
, decl
);
23043 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23046 vec_safe_push (unparsed_classes
, type
);
23048 /* Put back any saved access checks. */
23049 pop_deferring_access_checks ();
23051 /* Restore saved state. */
23052 parser
->in_switch_statement_p
= in_switch_statement_p
;
23053 parser
->in_statement
= in_statement
;
23054 parser
->in_function_body
= saved_in_function_body
;
23055 parser
->num_template_parameter_lists
23056 = saved_num_template_parameter_lists
;
23057 parser
->in_unbraced_linkage_specification_p
23058 = saved_in_unbraced_linkage_specification_p
;
23064 cp_parser_class_specifier (cp_parser
* parser
)
23067 timevar_push (TV_PARSE_STRUCT
);
23068 ret
= cp_parser_class_specifier_1 (parser
);
23069 timevar_pop (TV_PARSE_STRUCT
);
23073 /* Parse a class-head.
23076 class-key identifier [opt] base-clause [opt]
23077 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23078 class-key nested-name-specifier [opt] template-id
23081 class-virt-specifier:
23085 class-key attributes identifier [opt] base-clause [opt]
23086 class-key attributes nested-name-specifier identifier base-clause [opt]
23087 class-key attributes nested-name-specifier [opt] template-id
23090 Upon return BASES is initialized to the list of base classes (or
23091 NULL, if there are none) in the same form returned by
23092 cp_parser_base_clause.
23094 Returns the TYPE of the indicated class. Sets
23095 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23096 involving a nested-name-specifier was used, and FALSE otherwise.
23098 Returns error_mark_node if this is not a class-head.
23100 Returns NULL_TREE if the class-head is syntactically valid, but
23101 semantically invalid in a way that means we should skip the entire
23102 body of the class. */
23105 cp_parser_class_head (cp_parser
* parser
,
23106 bool* nested_name_specifier_p
)
23108 tree nested_name_specifier
;
23109 enum tag_types class_key
;
23110 tree id
= NULL_TREE
;
23111 tree type
= NULL_TREE
;
23114 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23115 bool template_id_p
= false;
23116 bool qualified_p
= false;
23117 bool invalid_nested_name_p
= false;
23118 bool invalid_explicit_specialization_p
= false;
23119 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23120 tree pushed_scope
= NULL_TREE
;
23121 unsigned num_templates
;
23122 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23123 /* Assume no nested-name-specifier will be present. */
23124 *nested_name_specifier_p
= false;
23125 /* Assume no template parameter lists will be used in defining the
23128 parser
->colon_corrects_to_scope_p
= false;
23130 /* Look for the class-key. */
23131 class_key
= cp_parser_class_key (parser
);
23132 if (class_key
== none_type
)
23133 return error_mark_node
;
23135 location_t class_head_start_location
= input_location
;
23137 /* Parse the attributes. */
23138 attributes
= cp_parser_attributes_opt (parser
);
23140 /* If the next token is `::', that is invalid -- but sometimes
23141 people do try to write:
23145 Handle this gracefully by accepting the extra qualifier, and then
23146 issuing an error about it later if this really is a
23147 class-head. If it turns out just to be an elaborated type
23148 specifier, remain silent. */
23149 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23150 qualified_p
= true;
23152 push_deferring_access_checks (dk_no_check
);
23154 /* Determine the name of the class. Begin by looking for an
23155 optional nested-name-specifier. */
23156 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23157 nested_name_specifier
23158 = cp_parser_nested_name_specifier_opt (parser
,
23159 /*typename_keyword_p=*/false,
23160 /*check_dependency_p=*/false,
23162 /*is_declaration=*/false);
23163 /* If there was a nested-name-specifier, then there *must* be an
23166 cp_token
*bad_template_keyword
= NULL
;
23168 if (nested_name_specifier
)
23170 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23171 /* Although the grammar says `identifier', it really means
23172 `class-name' or `template-name'. You are only allowed to
23173 define a class that has already been declared with this
23176 The proposed resolution for Core Issue 180 says that wherever
23177 you see `class T::X' you should treat `X' as a type-name.
23179 It is OK to define an inaccessible class; for example:
23181 class A { class B; };
23184 We do not know if we will see a class-name, or a
23185 template-name. We look for a class-name first, in case the
23186 class-name is a template-id; if we looked for the
23187 template-name first we would stop after the template-name. */
23188 cp_parser_parse_tentatively (parser
);
23189 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23190 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23191 type
= cp_parser_class_name (parser
,
23192 /*typename_keyword_p=*/false,
23193 /*template_keyword_p=*/false,
23195 /*check_dependency_p=*/false,
23196 /*class_head_p=*/true,
23197 /*is_declaration=*/false);
23198 /* If that didn't work, ignore the nested-name-specifier. */
23199 if (!cp_parser_parse_definitely (parser
))
23201 invalid_nested_name_p
= true;
23202 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23203 id
= cp_parser_identifier (parser
);
23204 if (id
== error_mark_node
)
23207 /* If we could not find a corresponding TYPE, treat this
23208 declaration like an unqualified declaration. */
23209 if (type
== error_mark_node
)
23210 nested_name_specifier
= NULL_TREE
;
23211 /* Otherwise, count the number of templates used in TYPE and its
23212 containing scopes. */
23214 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23216 /* Otherwise, the identifier is optional. */
23219 /* We don't know whether what comes next is a template-id,
23220 an identifier, or nothing at all. */
23221 cp_parser_parse_tentatively (parser
);
23222 /* Check for a template-id. */
23223 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23224 id
= cp_parser_template_id (parser
,
23225 /*template_keyword_p=*/false,
23226 /*check_dependency_p=*/true,
23228 /*is_declaration=*/true);
23229 /* If that didn't work, it could still be an identifier. */
23230 if (!cp_parser_parse_definitely (parser
))
23232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23234 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23235 id
= cp_parser_identifier (parser
);
23242 template_id_p
= true;
23247 pop_deferring_access_checks ();
23251 cp_parser_check_for_invalid_template_id (parser
, id
,
23253 type_start_token
->location
);
23255 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23257 /* If it's not a `:' or a `{' then we can't really be looking at a
23258 class-head, since a class-head only appears as part of a
23259 class-specifier. We have to detect this situation before calling
23260 xref_tag, since that has irreversible side-effects. */
23261 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23263 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23264 type
= error_mark_node
;
23268 /* At this point, we're going ahead with the class-specifier, even
23269 if some other problem occurs. */
23270 cp_parser_commit_to_tentative_parse (parser
);
23271 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23273 cp_parser_error (parser
,
23274 "cannot specify %<override%> for a class");
23275 type
= error_mark_node
;
23278 /* Issue the error about the overly-qualified name now. */
23281 cp_parser_error (parser
,
23282 "global qualification of class name is invalid");
23283 type
= error_mark_node
;
23286 else if (invalid_nested_name_p
)
23288 cp_parser_error (parser
,
23289 "qualified name does not name a class");
23290 type
= error_mark_node
;
23293 else if (nested_name_specifier
)
23297 if (bad_template_keyword
)
23298 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23299 keyword template shall not appear at the top level. */
23300 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23301 "keyword %<template%> not allowed in class-head-name");
23303 /* Reject typedef-names in class heads. */
23304 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23306 error_at (type_start_token
->location
,
23307 "invalid class name in declaration of %qD",
23313 /* Figure out in what scope the declaration is being placed. */
23314 scope
= current_scope ();
23315 /* If that scope does not contain the scope in which the
23316 class was originally declared, the program is invalid. */
23317 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23319 if (at_namespace_scope_p ())
23320 error_at (type_start_token
->location
,
23321 "declaration of %qD in namespace %qD which does not "
23323 type
, scope
, nested_name_specifier
);
23325 error_at (type_start_token
->location
,
23326 "declaration of %qD in %qD which does not enclose %qD",
23327 type
, scope
, nested_name_specifier
);
23333 A declarator-id shall not be qualified except for the
23334 definition of a ... nested class outside of its class
23335 ... [or] the definition or explicit instantiation of a
23336 class member of a namespace outside of its namespace. */
23337 if (scope
== nested_name_specifier
)
23339 permerror (nested_name_specifier_token_start
->location
,
23340 "extra qualification not allowed");
23341 nested_name_specifier
= NULL_TREE
;
23345 /* An explicit-specialization must be preceded by "template <>". If
23346 it is not, try to recover gracefully. */
23347 if (at_namespace_scope_p ()
23348 && parser
->num_template_parameter_lists
== 0
23349 && !processing_template_parmlist
23352 /* Build a location of this form:
23353 struct typename <ARGS>
23354 ^~~~~~~~~~~~~~~~~~~~~~
23355 with caret==start at the start token, and
23356 finishing at the end of the type. */
23357 location_t reported_loc
23358 = make_location (class_head_start_location
,
23359 class_head_start_location
,
23360 get_finish (type_start_token
->location
));
23361 rich_location
richloc (line_table
, reported_loc
);
23362 richloc
.add_fixit_insert_before (class_head_start_location
,
23364 error_at (&richloc
,
23365 "an explicit specialization must be preceded by"
23366 " %<template <>%>");
23367 invalid_explicit_specialization_p
= true;
23368 /* Take the same action that would have been taken by
23369 cp_parser_explicit_specialization. */
23370 ++parser
->num_template_parameter_lists
;
23371 begin_specialization ();
23373 /* There must be no "return" statements between this point and the
23374 end of this function; set "type "to the correct return value and
23375 use "goto done;" to return. */
23376 /* Make sure that the right number of template parameters were
23378 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23380 type_start_token
->location
,
23381 /*declarator=*/NULL
))
23383 /* If something went wrong, there is no point in even trying to
23384 process the class-definition. */
23389 /* Look up the type. */
23392 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23393 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23394 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23396 error_at (type_start_token
->location
,
23397 "function template %qD redeclared as a class template", id
);
23398 type
= error_mark_node
;
23402 type
= TREE_TYPE (id
);
23403 type
= maybe_process_partial_specialization (type
);
23405 /* Check the scope while we still know whether or not we had a
23406 nested-name-specifier. */
23407 if (type
!= error_mark_node
)
23408 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23410 if (nested_name_specifier
)
23411 pushed_scope
= push_scope (nested_name_specifier
);
23413 else if (nested_name_specifier
)
23419 template <typename T> struct S { struct T };
23420 template <typename T> struct S<T>::T { };
23422 we will get a TYPENAME_TYPE when processing the definition of
23423 `S::T'. We need to resolve it to the actual type before we
23424 try to define it. */
23425 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23427 class_type
= resolve_typename_type (TREE_TYPE (type
),
23428 /*only_current_p=*/false);
23429 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23430 type
= TYPE_NAME (class_type
);
23433 cp_parser_error (parser
, "could not resolve typename type");
23434 type
= error_mark_node
;
23438 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23439 == error_mark_node
)
23445 class_type
= current_class_type
;
23446 /* Enter the scope indicated by the nested-name-specifier. */
23447 pushed_scope
= push_scope (nested_name_specifier
);
23448 /* Get the canonical version of this type. */
23449 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23450 /* Call push_template_decl if it seems like we should be defining a
23451 template either from the template headers or the type we're
23452 defining, so that we diagnose both extra and missing headers. */
23453 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23454 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23455 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23457 type
= push_template_decl (type
);
23458 if (type
== error_mark_node
)
23465 type
= TREE_TYPE (type
);
23466 *nested_name_specifier_p
= true;
23468 else /* The name is not a nested name. */
23470 /* If the class was unnamed, create a dummy name. */
23472 id
= make_anon_name ();
23473 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23474 ? ts_within_enclosing_non_class
23476 type
= xref_tag (class_key
, id
, tag_scope
,
23477 parser
->num_template_parameter_lists
);
23480 /* Indicate whether this class was declared as a `class' or as a
23482 if (TREE_CODE (type
) == RECORD_TYPE
)
23483 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23484 cp_parser_check_class_key (class_key
, type
);
23486 /* If this type was already complete, and we see another definition,
23487 that's an error. */
23488 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23490 error_at (type_start_token
->location
, "redefinition of %q#T",
23492 inform (location_of (type
), "previous definition of %q#T",
23497 else if (type
== error_mark_node
)
23502 /* Apply attributes now, before any use of the class as a template
23503 argument in its base list. */
23504 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23505 fixup_attribute_variants (type
);
23508 /* We will have entered the scope containing the class; the names of
23509 base classes should be looked up in that context. For example:
23511 struct A { struct B {}; struct C; };
23512 struct A::C : B {};
23516 /* Get the list of base-classes, if there is one. */
23517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23519 /* PR59482: enter the class scope so that base-specifiers are looked
23523 bases
= cp_parser_base_clause (parser
);
23524 /* PR59482: get out of the previously pushed class scope so that the
23525 subsequent pops pop the right thing. */
23532 /* If we're really defining a class, process the base classes.
23533 If they're invalid, fail. */
23534 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23535 xref_basetypes (type
, bases
);
23538 /* Leave the scope given by the nested-name-specifier. We will
23539 enter the class scope itself while processing the members. */
23541 pop_scope (pushed_scope
);
23543 if (invalid_explicit_specialization_p
)
23545 end_specialization ();
23546 --parser
->num_template_parameter_lists
;
23550 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23551 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23552 CLASSTYPE_FINAL (type
) = 1;
23554 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23558 /* Parse a class-key.
23565 Returns the kind of class-key specified, or none_type to indicate
23568 static enum tag_types
23569 cp_parser_class_key (cp_parser
* parser
)
23572 enum tag_types tag_type
;
23574 /* Look for the class-key. */
23575 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23579 /* Check to see if the TOKEN is a class-key. */
23580 tag_type
= cp_parser_token_is_class_key (token
);
23582 cp_parser_error (parser
, "expected class-key");
23586 /* Parse a type-parameter-key.
23588 type-parameter-key:
23594 cp_parser_type_parameter_key (cp_parser
* parser
)
23596 /* Look for the type-parameter-key. */
23597 enum tag_types tag_type
= none_type
;
23598 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23599 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23601 cp_lexer_consume_token (parser
->lexer
);
23602 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23603 /* typename is not allowed in a template template parameter
23604 by the standard until C++17. */
23605 pedwarn (token
->location
, OPT_Wpedantic
,
23606 "ISO C++ forbids typename key in template template parameter;"
23607 " use -std=c++17 or -std=gnu++17");
23610 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23615 /* Parse an (optional) member-specification.
23617 member-specification:
23618 member-declaration member-specification [opt]
23619 access-specifier : member-specification [opt] */
23622 cp_parser_member_specification_opt (cp_parser
* parser
)
23629 /* Peek at the next token. */
23630 token
= cp_lexer_peek_token (parser
->lexer
);
23631 /* If it's a `}', or EOF then we've seen all the members. */
23632 if (token
->type
== CPP_CLOSE_BRACE
23633 || token
->type
== CPP_EOF
23634 || token
->type
== CPP_PRAGMA_EOL
)
23637 /* See if this token is a keyword. */
23638 keyword
= token
->keyword
;
23642 case RID_PROTECTED
:
23644 /* Consume the access-specifier. */
23645 cp_lexer_consume_token (parser
->lexer
);
23646 /* Remember which access-specifier is active. */
23647 current_access_specifier
= token
->u
.value
;
23648 /* Look for the `:'. */
23649 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23653 /* Accept #pragmas at class scope. */
23654 if (token
->type
== CPP_PRAGMA
)
23656 cp_parser_pragma (parser
, pragma_member
, NULL
);
23660 /* Otherwise, the next construction must be a
23661 member-declaration. */
23662 cp_parser_member_declaration (parser
);
23667 /* Parse a member-declaration.
23669 member-declaration:
23670 decl-specifier-seq [opt] member-declarator-list [opt] ;
23671 function-definition ; [opt]
23672 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23674 template-declaration
23677 member-declarator-list:
23679 member-declarator-list , member-declarator
23682 declarator pure-specifier [opt]
23683 declarator constant-initializer [opt]
23684 identifier [opt] : constant-expression
23688 member-declaration:
23689 __extension__ member-declaration
23692 declarator attributes [opt] pure-specifier [opt]
23693 declarator attributes [opt] constant-initializer [opt]
23694 identifier [opt] attributes [opt] : constant-expression
23698 member-declaration:
23699 static_assert-declaration */
23702 cp_parser_member_declaration (cp_parser
* parser
)
23704 cp_decl_specifier_seq decl_specifiers
;
23705 tree prefix_attributes
;
23707 int declares_class_or_enum
;
23709 cp_token
*token
= NULL
;
23710 cp_token
*decl_spec_token_start
= NULL
;
23711 cp_token
*initializer_token_start
= NULL
;
23712 int saved_pedantic
;
23713 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23715 /* Check for the `__extension__' keyword. */
23716 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23719 cp_parser_member_declaration (parser
);
23720 /* Restore the old value of the PEDANTIC flag. */
23721 pedantic
= saved_pedantic
;
23726 /* Check for a template-declaration. */
23727 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23729 /* An explicit specialization here is an error condition, and we
23730 expect the specialization handler to detect and report this. */
23731 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23732 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23733 cp_parser_explicit_specialization (parser
);
23735 cp_parser_template_declaration (parser
, /*member_p=*/true);
23739 /* Check for a template introduction. */
23740 else if (cp_parser_template_declaration_after_export (parser
, true))
23743 /* Check for a using-declaration. */
23744 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23746 if (cxx_dialect
< cxx11
)
23748 /* Parse the using-declaration. */
23749 cp_parser_using_declaration (parser
,
23750 /*access_declaration_p=*/false);
23756 bool alias_decl_expected
;
23757 cp_parser_parse_tentatively (parser
);
23758 decl
= cp_parser_alias_declaration (parser
);
23759 /* Note that if we actually see the '=' token after the
23760 identifier, cp_parser_alias_declaration commits the
23761 tentative parse. In that case, we really expect an
23762 alias-declaration. Otherwise, we expect a using
23764 alias_decl_expected
=
23765 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23766 cp_parser_parse_definitely (parser
);
23768 if (alias_decl_expected
)
23769 finish_member_declaration (decl
);
23771 cp_parser_using_declaration (parser
,
23772 /*access_declaration_p=*/false);
23777 /* Check for @defs. */
23778 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23781 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23782 ivar
= ivar_chains
;
23786 ivar
= TREE_CHAIN (member
);
23787 TREE_CHAIN (member
) = NULL_TREE
;
23788 finish_member_declaration (member
);
23793 /* If the next token is `static_assert' we have a static assertion. */
23794 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23796 cp_parser_static_assert (parser
, /*member_p=*/true);
23800 parser
->colon_corrects_to_scope_p
= false;
23802 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23805 /* Parse the decl-specifier-seq. */
23806 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23807 cp_parser_decl_specifier_seq (parser
,
23808 CP_PARSER_FLAGS_OPTIONAL
,
23810 &declares_class_or_enum
);
23811 /* Check for an invalid type-name. */
23812 if (!decl_specifiers
.any_type_specifiers_p
23813 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23815 /* If there is no declarator, then the decl-specifier-seq should
23817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23819 /* If there was no decl-specifier-seq, and the next token is a
23820 `;', then we have something like:
23826 Each member-declaration shall declare at least one member
23827 name of the class. */
23828 if (!decl_specifiers
.any_specifiers_p
)
23830 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23831 if (!in_system_header_at (token
->location
))
23833 gcc_rich_location
richloc (token
->location
);
23834 richloc
.add_fixit_remove ();
23835 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23842 /* See if this declaration is a friend. */
23843 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23844 /* If there were decl-specifiers, check to see if there was
23845 a class-declaration. */
23846 type
= check_tag_decl (&decl_specifiers
,
23847 /*explicit_type_instantiation_p=*/false);
23848 /* Nested classes have already been added to the class, but
23849 a `friend' needs to be explicitly registered. */
23852 /* If the `friend' keyword was present, the friend must
23853 be introduced with a class-key. */
23854 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23855 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23856 "in C++03 a class-key must be used "
23857 "when declaring a friend");
23860 template <typename T> struct A {
23861 friend struct A<T>::B;
23864 A<T>::B will be represented by a TYPENAME_TYPE, and
23865 therefore not recognized by check_tag_decl. */
23868 type
= decl_specifiers
.type
;
23869 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23870 type
= TREE_TYPE (type
);
23872 if (!type
|| !TYPE_P (type
))
23873 error_at (decl_spec_token_start
->location
,
23874 "friend declaration does not name a class or "
23877 make_friend_class (current_class_type
, type
,
23878 /*complain=*/true);
23880 /* If there is no TYPE, an error message will already have
23882 else if (!type
|| type
== error_mark_node
)
23884 /* An anonymous aggregate has to be handled specially; such
23885 a declaration really declares a data member (with a
23886 particular type), as opposed to a nested class. */
23887 else if (ANON_AGGR_TYPE_P (type
))
23890 if (decl_specifiers
.storage_class
!= sc_none
)
23891 error_at (decl_spec_token_start
->location
,
23892 "a storage class on an anonymous aggregate "
23893 "in class scope is not allowed");
23895 /* Remove constructors and such from TYPE, now that we
23896 know it is an anonymous aggregate. */
23897 fixup_anonymous_aggr (type
);
23898 /* And make the corresponding data member. */
23899 decl
= build_decl (decl_spec_token_start
->location
,
23900 FIELD_DECL
, NULL_TREE
, type
);
23901 /* Add it to the class. */
23902 finish_member_declaration (decl
);
23905 cp_parser_check_access_in_redeclaration
23907 decl_spec_token_start
->location
);
23912 bool assume_semicolon
= false;
23914 /* Clear attributes from the decl_specifiers but keep them
23915 around as prefix attributes that apply them to the entity
23917 prefix_attributes
= decl_specifiers
.attributes
;
23918 decl_specifiers
.attributes
= NULL_TREE
;
23920 /* See if these declarations will be friends. */
23921 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23923 /* Keep going until we hit the `;' at the end of the
23925 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23927 tree attributes
= NULL_TREE
;
23928 tree first_attribute
;
23930 bool named_bitfld
= false;
23932 /* Peek at the next token. */
23933 token
= cp_lexer_peek_token (parser
->lexer
);
23935 /* The following code wants to know early if it is a bit-field
23936 or some other declaration. Attributes can appear before
23937 the `:' token. Skip over them without consuming any tokens
23938 to peek if they are followed by `:'. */
23939 if (cp_next_tokens_can_be_attribute_p (parser
)
23940 || (token
->type
== CPP_NAME
23941 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23942 && (named_bitfld
= true)))
23945 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23946 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23949 /* Check for a bitfield declaration. */
23950 if (token
->type
== CPP_COLON
23951 || (token
->type
== CPP_NAME
23952 && token
== cp_lexer_peek_token (parser
->lexer
)
23953 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23954 && (named_bitfld
= true)))
23958 tree late_attributes
= NULL_TREE
;
23961 identifier
= cp_parser_identifier (parser
);
23963 identifier
= NULL_TREE
;
23965 /* Look for attributes that apply to the bitfield. */
23966 attributes
= cp_parser_attributes_opt (parser
);
23968 /* Consume the `:' token. */
23969 cp_lexer_consume_token (parser
->lexer
);
23971 /* Get the width of the bitfield. */
23972 width
= cp_parser_constant_expression (parser
, false, NULL
,
23973 cxx_dialect
>= cxx11
);
23975 /* In C++2A and as extension for C++11 and above we allow
23976 default member initializers for bit-fields. */
23977 initializer
= NULL_TREE
;
23978 if (cxx_dialect
>= cxx11
23979 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23980 || cp_lexer_next_token_is (parser
->lexer
,
23984 = cp_lexer_peek_token (parser
->lexer
)->location
;
23985 if (cxx_dialect
< cxx2a
23986 && !in_system_header_at (loc
)
23987 && identifier
!= NULL_TREE
)
23989 "default member initializers for bit-fields "
23990 "only available with -std=c++2a or "
23993 initializer
= cp_parser_save_nsdmi (parser
);
23994 if (identifier
== NULL_TREE
)
23996 error_at (loc
, "default member initializer for "
23997 "unnamed bit-field");
23998 initializer
= NULL_TREE
;
24003 /* Look for attributes that apply to the bitfield after
24004 the `:' token and width. This is where GCC used to
24005 parse attributes in the past, pedwarn if there is
24006 a std attribute. */
24007 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24008 pedwarn (input_location
, OPT_Wpedantic
,
24009 "ISO C++ allows bit-field attributes only "
24010 "before the %<:%> token");
24012 late_attributes
= cp_parser_attributes_opt (parser
);
24015 attributes
= attr_chainon (attributes
, late_attributes
);
24017 /* Remember which attributes are prefix attributes and
24019 first_attribute
= attributes
;
24020 /* Combine the attributes. */
24021 attributes
= attr_chainon (prefix_attributes
, attributes
);
24023 /* Create the bitfield declaration. */
24024 decl
= grokbitfield (identifier
24025 ? make_id_declarator (NULL_TREE
,
24030 width
, initializer
,
24035 cp_declarator
*declarator
;
24036 tree asm_specification
;
24037 int ctor_dtor_or_conv_p
;
24039 /* Parse the declarator. */
24041 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24042 &ctor_dtor_or_conv_p
,
24043 /*parenthesized_p=*/NULL
,
24047 /* If something went wrong parsing the declarator, make sure
24048 that we at least consume some tokens. */
24049 if (declarator
== cp_error_declarator
)
24051 /* Skip to the end of the statement. */
24052 cp_parser_skip_to_end_of_statement (parser
);
24053 /* If the next token is not a semicolon, that is
24054 probably because we just skipped over the body of
24055 a function. So, we consume a semicolon if
24056 present, but do not issue an error message if it
24058 if (cp_lexer_next_token_is (parser
->lexer
,
24060 cp_lexer_consume_token (parser
->lexer
);
24064 if (declares_class_or_enum
& 2)
24065 cp_parser_check_for_definition_in_return_type
24066 (declarator
, decl_specifiers
.type
,
24067 decl_specifiers
.locations
[ds_type_spec
]);
24069 /* Look for an asm-specification. */
24070 asm_specification
= cp_parser_asm_specification_opt (parser
);
24071 /* Look for attributes that apply to the declaration. */
24072 attributes
= cp_parser_attributes_opt (parser
);
24073 /* Remember which attributes are prefix attributes and
24075 first_attribute
= attributes
;
24076 /* Combine the attributes. */
24077 attributes
= attr_chainon (prefix_attributes
, attributes
);
24079 /* If it's an `=', then we have a constant-initializer or a
24080 pure-specifier. It is not correct to parse the
24081 initializer before registering the member declaration
24082 since the member declaration should be in scope while
24083 its initializer is processed. However, the rest of the
24084 front end does not yet provide an interface that allows
24085 us to handle this correctly. */
24086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24090 A pure-specifier shall be used only in the declaration of
24091 a virtual function.
24093 A member-declarator can contain a constant-initializer
24094 only if it declares a static member of integral or
24097 Therefore, if the DECLARATOR is for a function, we look
24098 for a pure-specifier; otherwise, we look for a
24099 constant-initializer. When we call `grokfield', it will
24100 perform more stringent semantics checks. */
24101 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24102 if (function_declarator_p (declarator
)
24103 || (decl_specifiers
.type
24104 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24105 && declarator
->kind
== cdk_id
24106 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24107 == FUNCTION_TYPE
)))
24108 initializer
= cp_parser_pure_specifier (parser
);
24109 else if (decl_specifiers
.storage_class
!= sc_static
)
24110 initializer
= cp_parser_save_nsdmi (parser
);
24111 else if (cxx_dialect
>= cxx11
)
24114 /* Don't require a constant rvalue in C++11, since we
24115 might want a reference constant. We'll enforce
24116 constancy later. */
24117 cp_lexer_consume_token (parser
->lexer
);
24118 /* Parse the initializer. */
24119 initializer
= cp_parser_initializer_clause (parser
,
24123 /* Parse the initializer. */
24124 initializer
= cp_parser_constant_initializer (parser
);
24126 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24127 && !function_declarator_p (declarator
))
24130 if (decl_specifiers
.storage_class
!= sc_static
)
24131 initializer
= cp_parser_save_nsdmi (parser
);
24133 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24135 /* Otherwise, there is no initializer. */
24137 initializer
= NULL_TREE
;
24139 /* See if we are probably looking at a function
24140 definition. We are certainly not looking at a
24141 member-declarator. Calling `grokfield' has
24142 side-effects, so we must not do it unless we are sure
24143 that we are looking at a member-declarator. */
24144 if (cp_parser_token_starts_function_definition_p
24145 (cp_lexer_peek_token (parser
->lexer
)))
24147 /* The grammar does not allow a pure-specifier to be
24148 used when a member function is defined. (It is
24149 possible that this fact is an oversight in the
24150 standard, since a pure function may be defined
24151 outside of the class-specifier. */
24152 if (initializer
&& initializer_token_start
)
24153 error_at (initializer_token_start
->location
,
24154 "pure-specifier on function-definition");
24155 decl
= cp_parser_save_member_function_body (parser
,
24159 if (parser
->fully_implicit_function_template_p
)
24160 decl
= finish_fully_implicit_template (parser
, decl
);
24161 /* If the member was not a friend, declare it here. */
24163 finish_member_declaration (decl
);
24164 /* Peek at the next token. */
24165 token
= cp_lexer_peek_token (parser
->lexer
);
24166 /* If the next token is a semicolon, consume it. */
24167 if (token
->type
== CPP_SEMICOLON
)
24169 location_t semicolon_loc
24170 = cp_lexer_consume_token (parser
->lexer
)->location
;
24171 gcc_rich_location
richloc (semicolon_loc
);
24172 richloc
.add_fixit_remove ();
24173 warning_at (&richloc
, OPT_Wextra_semi
,
24174 "extra %<;%> after in-class "
24175 "function definition");
24180 if (declarator
->kind
== cdk_function
)
24181 declarator
->id_loc
= token
->location
;
24182 /* Create the declaration. */
24183 decl
= grokfield (declarator
, &decl_specifiers
,
24184 initializer
, /*init_const_expr_p=*/true,
24185 asm_specification
, attributes
);
24186 if (parser
->fully_implicit_function_template_p
)
24189 finish_fully_implicit_template (parser
, 0);
24191 decl
= finish_fully_implicit_template (parser
, decl
);
24195 cp_finalize_omp_declare_simd (parser
, decl
);
24196 cp_finalize_oacc_routine (parser
, decl
, false);
24198 /* Reset PREFIX_ATTRIBUTES. */
24199 if (attributes
!= error_mark_node
)
24201 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24202 attributes
= TREE_CHAIN (attributes
);
24204 TREE_CHAIN (attributes
) = NULL_TREE
;
24207 /* If there is any qualification still in effect, clear it
24208 now; we will be starting fresh with the next declarator. */
24209 parser
->scope
= NULL_TREE
;
24210 parser
->qualifying_scope
= NULL_TREE
;
24211 parser
->object_scope
= NULL_TREE
;
24212 /* If it's a `,', then there are more declarators. */
24213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24215 cp_lexer_consume_token (parser
->lexer
);
24216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24218 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24219 gcc_rich_location
richloc (token
->location
);
24220 richloc
.add_fixit_remove ();
24221 error_at (&richloc
, "stray %<,%> at end of "
24222 "member declaration");
24225 /* If the next token isn't a `;', then we have a parse error. */
24226 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24229 /* The next token might be a ways away from where the
24230 actual semicolon is missing. Find the previous token
24231 and use that for our error position. */
24232 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24233 gcc_rich_location
richloc (token
->location
);
24234 richloc
.add_fixit_insert_after (";");
24235 error_at (&richloc
, "expected %<;%> at end of "
24236 "member declaration");
24238 /* Assume that the user meant to provide a semicolon. If
24239 we were to cp_parser_skip_to_end_of_statement, we might
24240 skip to a semicolon inside a member function definition
24241 and issue nonsensical error messages. */
24242 assume_semicolon
= true;
24247 /* Add DECL to the list of members. */
24249 /* Explicitly include, eg, NSDMIs, for better error
24250 recovery (c++/58650). */
24251 || !DECL_DECLARES_FUNCTION_P (decl
))
24252 finish_member_declaration (decl
);
24254 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24255 cp_parser_save_default_args (parser
, decl
);
24256 else if (TREE_CODE (decl
) == FIELD_DECL
24257 && DECL_INITIAL (decl
))
24258 /* Add DECL to the queue of NSDMI to be parsed later. */
24259 vec_safe_push (unparsed_nsdmis
, decl
);
24262 if (assume_semicolon
)
24267 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24269 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24272 /* Parse a pure-specifier.
24277 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24278 Otherwise, ERROR_MARK_NODE is returned. */
24281 cp_parser_pure_specifier (cp_parser
* parser
)
24285 /* Look for the `=' token. */
24286 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24287 return error_mark_node
;
24288 /* Look for the `0' token. */
24289 token
= cp_lexer_peek_token (parser
->lexer
);
24291 if (token
->type
== CPP_EOF
24292 || token
->type
== CPP_PRAGMA_EOL
)
24293 return error_mark_node
;
24295 cp_lexer_consume_token (parser
->lexer
);
24297 /* Accept = default or = delete in c++0x mode. */
24298 if (token
->keyword
== RID_DEFAULT
24299 || token
->keyword
== RID_DELETE
)
24301 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24302 return token
->u
.value
;
24305 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24306 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24308 cp_parser_error (parser
,
24309 "invalid pure specifier (only %<= 0%> is allowed)");
24310 cp_parser_skip_to_end_of_statement (parser
);
24311 return error_mark_node
;
24313 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24315 error_at (token
->location
, "templates may not be %<virtual%>");
24316 return error_mark_node
;
24319 return integer_zero_node
;
24322 /* Parse a constant-initializer.
24324 constant-initializer:
24325 = constant-expression
24327 Returns a representation of the constant-expression. */
24330 cp_parser_constant_initializer (cp_parser
* parser
)
24332 /* Look for the `=' token. */
24333 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24334 return error_mark_node
;
24336 /* It is invalid to write:
24338 struct S { static const int i = { 7 }; };
24341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24343 cp_parser_error (parser
,
24344 "a brace-enclosed initializer is not allowed here");
24345 /* Consume the opening brace. */
24346 matching_braces braces
;
24347 braces
.consume_open (parser
);
24348 /* Skip the initializer. */
24349 cp_parser_skip_to_closing_brace (parser
);
24350 /* Look for the trailing `}'. */
24351 braces
.require_close (parser
);
24353 return error_mark_node
;
24356 return cp_parser_constant_expression (parser
);
24359 /* Derived classes [gram.class.derived] */
24361 /* Parse a base-clause.
24364 : base-specifier-list
24366 base-specifier-list:
24367 base-specifier ... [opt]
24368 base-specifier-list , base-specifier ... [opt]
24370 Returns a TREE_LIST representing the base-classes, in the order in
24371 which they were declared. The representation of each node is as
24372 described by cp_parser_base_specifier.
24374 In the case that no bases are specified, this function will return
24375 NULL_TREE, not ERROR_MARK_NODE. */
24378 cp_parser_base_clause (cp_parser
* parser
)
24380 tree bases
= NULL_TREE
;
24382 /* Look for the `:' that begins the list. */
24383 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24385 /* Scan the base-specifier-list. */
24390 bool pack_expansion_p
= false;
24392 /* Look for the base-specifier. */
24393 base
= cp_parser_base_specifier (parser
);
24394 /* Look for the (optional) ellipsis. */
24395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24397 /* Consume the `...'. */
24398 cp_lexer_consume_token (parser
->lexer
);
24400 pack_expansion_p
= true;
24403 /* Add BASE to the front of the list. */
24404 if (base
&& base
!= error_mark_node
)
24406 if (pack_expansion_p
)
24407 /* Make this a pack expansion type. */
24408 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24410 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24412 TREE_CHAIN (base
) = bases
;
24416 /* Peek at the next token. */
24417 token
= cp_lexer_peek_token (parser
->lexer
);
24418 /* If it's not a comma, then the list is complete. */
24419 if (token
->type
!= CPP_COMMA
)
24421 /* Consume the `,'. */
24422 cp_lexer_consume_token (parser
->lexer
);
24425 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24426 base class had a qualified name. However, the next name that
24427 appears is certainly not qualified. */
24428 parser
->scope
= NULL_TREE
;
24429 parser
->qualifying_scope
= NULL_TREE
;
24430 parser
->object_scope
= NULL_TREE
;
24432 return nreverse (bases
);
24435 /* Parse a base-specifier.
24438 :: [opt] nested-name-specifier [opt] class-name
24439 virtual access-specifier [opt] :: [opt] nested-name-specifier
24441 access-specifier virtual [opt] :: [opt] nested-name-specifier
24444 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24445 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24446 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24447 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24450 cp_parser_base_specifier (cp_parser
* parser
)
24454 bool virtual_p
= false;
24455 bool duplicate_virtual_error_issued_p
= false;
24456 bool duplicate_access_error_issued_p
= false;
24457 bool class_scope_p
, template_p
;
24458 tree access
= access_default_node
;
24461 /* Process the optional `virtual' and `access-specifier'. */
24464 /* Peek at the next token. */
24465 token
= cp_lexer_peek_token (parser
->lexer
);
24466 /* Process `virtual'. */
24467 switch (token
->keyword
)
24470 /* If `virtual' appears more than once, issue an error. */
24471 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24473 cp_parser_error (parser
,
24474 "%<virtual%> specified more than once in base-specifier");
24475 duplicate_virtual_error_issued_p
= true;
24480 /* Consume the `virtual' token. */
24481 cp_lexer_consume_token (parser
->lexer
);
24486 case RID_PROTECTED
:
24488 /* If more than one access specifier appears, issue an
24490 if (access
!= access_default_node
24491 && !duplicate_access_error_issued_p
)
24493 cp_parser_error (parser
,
24494 "more than one access specifier in base-specifier");
24495 duplicate_access_error_issued_p
= true;
24498 access
= ridpointers
[(int) token
->keyword
];
24500 /* Consume the access-specifier. */
24501 cp_lexer_consume_token (parser
->lexer
);
24510 /* It is not uncommon to see programs mechanically, erroneously, use
24511 the 'typename' keyword to denote (dependent) qualified types
24512 as base classes. */
24513 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24515 token
= cp_lexer_peek_token (parser
->lexer
);
24516 if (!processing_template_decl
)
24517 error_at (token
->location
,
24518 "keyword %<typename%> not allowed outside of templates");
24520 error_at (token
->location
,
24521 "keyword %<typename%> not allowed in this context "
24522 "(the base class is implicitly a type)");
24523 cp_lexer_consume_token (parser
->lexer
);
24526 /* Look for the optional `::' operator. */
24527 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24528 /* Look for the nested-name-specifier. The simplest way to
24533 The keyword `typename' is not permitted in a base-specifier or
24534 mem-initializer; in these contexts a qualified name that
24535 depends on a template-parameter is implicitly assumed to be a
24538 is to pretend that we have seen the `typename' keyword at this
24540 cp_parser_nested_name_specifier_opt (parser
,
24541 /*typename_keyword_p=*/true,
24542 /*check_dependency_p=*/true,
24544 /*is_declaration=*/true);
24545 /* If the base class is given by a qualified name, assume that names
24546 we see are type names or templates, as appropriate. */
24547 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24548 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24551 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24552 /* DR 950 allows decltype as a base-specifier. */
24553 type
= cp_parser_decltype (parser
);
24556 /* Otherwise, look for the class-name. */
24557 type
= cp_parser_class_name (parser
,
24561 /*check_dependency_p=*/true,
24562 /*class_head_p=*/false,
24563 /*is_declaration=*/true);
24564 type
= TREE_TYPE (type
);
24567 if (type
== error_mark_node
)
24568 return error_mark_node
;
24570 return finish_base_specifier (type
, access
, virtual_p
);
24573 /* Exception handling [gram.exception] */
24575 /* Parse an (optional) noexcept-specification.
24577 noexcept-specification:
24578 noexcept ( constant-expression ) [opt]
24580 If no noexcept-specification is present, returns NULL_TREE.
24581 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24582 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24583 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24584 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24585 in which case a boolean condition is returned instead. */
24588 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24589 bool require_constexpr
,
24590 bool* consumed_expr
,
24594 const char *saved_message
;
24596 /* Peek at the next token. */
24597 token
= cp_lexer_peek_token (parser
->lexer
);
24599 /* Is it a noexcept-specification? */
24600 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24603 cp_lexer_consume_token (parser
->lexer
);
24605 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24607 matching_parens parens
;
24608 parens
.consume_open (parser
);
24610 if (require_constexpr
)
24612 /* Types may not be defined in an exception-specification. */
24613 saved_message
= parser
->type_definition_forbidden_message
;
24614 parser
->type_definition_forbidden_message
24615 = G_("types may not be defined in an exception-specification");
24617 expr
= cp_parser_constant_expression (parser
);
24619 /* Restore the saved message. */
24620 parser
->type_definition_forbidden_message
= saved_message
;
24624 expr
= cp_parser_expression (parser
);
24625 *consumed_expr
= true;
24628 parens
.require_close (parser
);
24632 expr
= boolean_true_node
;
24633 if (!require_constexpr
)
24634 *consumed_expr
= false;
24637 /* We cannot build a noexcept-spec right away because this will check
24638 that expr is a constexpr. */
24640 return build_noexcept_spec (expr
, tf_warning_or_error
);
24648 /* Parse an (optional) exception-specification.
24650 exception-specification:
24651 throw ( type-id-list [opt] )
24653 Returns a TREE_LIST representing the exception-specification. The
24654 TREE_VALUE of each node is a type. */
24657 cp_parser_exception_specification_opt (cp_parser
* parser
)
24661 const char *saved_message
;
24663 /* Peek at the next token. */
24664 token
= cp_lexer_peek_token (parser
->lexer
);
24666 /* Is it a noexcept-specification? */
24667 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24669 if (type_id_list
!= NULL_TREE
)
24670 return type_id_list
;
24672 /* If it's not `throw', then there's no exception-specification. */
24673 if (!cp_parser_is_keyword (token
, RID_THROW
))
24676 location_t loc
= token
->location
;
24678 /* Consume the `throw'. */
24679 cp_lexer_consume_token (parser
->lexer
);
24681 /* Look for the `('. */
24682 matching_parens parens
;
24683 parens
.require_open (parser
);
24685 /* Peek at the next token. */
24686 token
= cp_lexer_peek_token (parser
->lexer
);
24687 /* If it's not a `)', then there is a type-id-list. */
24688 if (token
->type
!= CPP_CLOSE_PAREN
)
24690 /* Types may not be defined in an exception-specification. */
24691 saved_message
= parser
->type_definition_forbidden_message
;
24692 parser
->type_definition_forbidden_message
24693 = G_("types may not be defined in an exception-specification");
24694 /* Parse the type-id-list. */
24695 type_id_list
= cp_parser_type_id_list (parser
);
24696 /* Restore the saved message. */
24697 parser
->type_definition_forbidden_message
= saved_message
;
24699 if (cxx_dialect
>= cxx17
)
24701 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24703 type_id_list
= NULL_TREE
;
24705 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24706 warning_at (loc
, OPT_Wdeprecated
,
24707 "dynamic exception specifications are deprecated in "
24710 /* In C++17, throw() is equivalent to noexcept (true). throw()
24711 is deprecated in C++11 and above as well, but is still widely used,
24712 so don't warn about it yet. */
24713 else if (cxx_dialect
>= cxx17
)
24714 type_id_list
= noexcept_true_spec
;
24716 type_id_list
= empty_except_spec
;
24718 /* Look for the `)'. */
24719 parens
.require_close (parser
);
24721 return type_id_list
;
24724 /* Parse an (optional) type-id-list.
24728 type-id-list , type-id ... [opt]
24730 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24731 in the order that the types were presented. */
24734 cp_parser_type_id_list (cp_parser
* parser
)
24736 tree types
= NULL_TREE
;
24743 token
= cp_lexer_peek_token (parser
->lexer
);
24745 /* Get the next type-id. */
24746 type
= cp_parser_type_id (parser
);
24747 /* Check for invalid 'auto'. */
24748 if (flag_concepts
&& type_uses_auto (type
))
24750 error_at (token
->location
,
24751 "invalid use of %<auto%> in exception-specification");
24752 type
= error_mark_node
;
24754 /* Parse the optional ellipsis. */
24755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24757 /* Consume the `...'. */
24758 cp_lexer_consume_token (parser
->lexer
);
24760 /* Turn the type into a pack expansion expression. */
24761 type
= make_pack_expansion (type
);
24763 /* Add it to the list. */
24764 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24765 /* Peek at the next token. */
24766 token
= cp_lexer_peek_token (parser
->lexer
);
24767 /* If it is not a `,', we are done. */
24768 if (token
->type
!= CPP_COMMA
)
24770 /* Consume the `,'. */
24771 cp_lexer_consume_token (parser
->lexer
);
24774 return nreverse (types
);
24777 /* Parse a try-block.
24780 try compound-statement handler-seq */
24783 cp_parser_try_block (cp_parser
* parser
)
24787 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24788 if (parser
->in_function_body
24789 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24790 error ("%<try%> in %<constexpr%> function");
24792 try_block
= begin_try_block ();
24793 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24794 finish_try_block (try_block
);
24795 cp_parser_handler_seq (parser
);
24796 finish_handler_sequence (try_block
);
24801 /* Parse a function-try-block.
24803 function-try-block:
24804 try ctor-initializer [opt] function-body handler-seq */
24807 cp_parser_function_try_block (cp_parser
* parser
)
24809 tree compound_stmt
;
24812 /* Look for the `try' keyword. */
24813 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24815 /* Let the rest of the front end know where we are. */
24816 try_block
= begin_function_try_block (&compound_stmt
);
24817 /* Parse the function-body. */
24818 cp_parser_ctor_initializer_opt_and_function_body
24819 (parser
, /*in_function_try_block=*/true);
24820 /* We're done with the `try' part. */
24821 finish_function_try_block (try_block
);
24822 /* Parse the handlers. */
24823 cp_parser_handler_seq (parser
);
24824 /* We're done with the handlers. */
24825 finish_function_handler_sequence (try_block
, compound_stmt
);
24828 /* Parse a handler-seq.
24831 handler handler-seq [opt] */
24834 cp_parser_handler_seq (cp_parser
* parser
)
24840 /* Parse the handler. */
24841 cp_parser_handler (parser
);
24842 /* Peek at the next token. */
24843 token
= cp_lexer_peek_token (parser
->lexer
);
24844 /* If it's not `catch' then there are no more handlers. */
24845 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24850 /* Parse a handler.
24853 catch ( exception-declaration ) compound-statement */
24856 cp_parser_handler (cp_parser
* parser
)
24861 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24862 handler
= begin_handler ();
24863 matching_parens parens
;
24864 parens
.require_open (parser
);
24865 declaration
= cp_parser_exception_declaration (parser
);
24866 finish_handler_parms (declaration
, handler
);
24867 parens
.require_close (parser
);
24868 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24869 finish_handler (handler
);
24872 /* Parse an exception-declaration.
24874 exception-declaration:
24875 type-specifier-seq declarator
24876 type-specifier-seq abstract-declarator
24880 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24881 ellipsis variant is used. */
24884 cp_parser_exception_declaration (cp_parser
* parser
)
24886 cp_decl_specifier_seq type_specifiers
;
24887 cp_declarator
*declarator
;
24888 const char *saved_message
;
24890 /* If it's an ellipsis, it's easy to handle. */
24891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24893 /* Consume the `...' token. */
24894 cp_lexer_consume_token (parser
->lexer
);
24898 /* Types may not be defined in exception-declarations. */
24899 saved_message
= parser
->type_definition_forbidden_message
;
24900 parser
->type_definition_forbidden_message
24901 = G_("types may not be defined in exception-declarations");
24903 /* Parse the type-specifier-seq. */
24904 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24905 /*is_trailing_return=*/false,
24907 /* If it's a `)', then there is no declarator. */
24908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24911 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24912 /*ctor_dtor_or_conv_p=*/NULL
,
24913 /*parenthesized_p=*/NULL
,
24914 /*member_p=*/false,
24915 /*friend_p=*/false);
24917 /* Restore the saved message. */
24918 parser
->type_definition_forbidden_message
= saved_message
;
24920 if (!type_specifiers
.any_specifiers_p
)
24921 return error_mark_node
;
24923 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24926 /* Parse a throw-expression.
24929 throw assignment-expression [opt]
24931 Returns a THROW_EXPR representing the throw-expression. */
24934 cp_parser_throw_expression (cp_parser
* parser
)
24939 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24940 token
= cp_lexer_peek_token (parser
->lexer
);
24941 /* Figure out whether or not there is an assignment-expression
24942 following the "throw" keyword. */
24943 if (token
->type
== CPP_COMMA
24944 || token
->type
== CPP_SEMICOLON
24945 || token
->type
== CPP_CLOSE_PAREN
24946 || token
->type
== CPP_CLOSE_SQUARE
24947 || token
->type
== CPP_CLOSE_BRACE
24948 || token
->type
== CPP_COLON
)
24949 expression
= NULL_TREE
;
24951 expression
= cp_parser_assignment_expression (parser
);
24953 return build_throw (expression
);
24956 /* GNU Extensions */
24958 /* Parse an (optional) asm-specification.
24961 asm ( string-literal )
24963 If the asm-specification is present, returns a STRING_CST
24964 corresponding to the string-literal. Otherwise, returns
24968 cp_parser_asm_specification_opt (cp_parser
* parser
)
24971 tree asm_specification
;
24973 /* Peek at the next token. */
24974 token
= cp_lexer_peek_token (parser
->lexer
);
24975 /* If the next token isn't the `asm' keyword, then there's no
24976 asm-specification. */
24977 if (!cp_parser_is_keyword (token
, RID_ASM
))
24980 /* Consume the `asm' token. */
24981 cp_lexer_consume_token (parser
->lexer
);
24982 /* Look for the `('. */
24983 matching_parens parens
;
24984 parens
.require_open (parser
);
24986 /* Look for the string-literal. */
24987 asm_specification
= cp_parser_string_literal (parser
, false, false);
24989 /* Look for the `)'. */
24990 parens
.require_close (parser
);
24992 return asm_specification
;
24995 /* Parse an asm-operand-list.
24999 asm-operand-list , asm-operand
25002 string-literal ( expression )
25003 [ string-literal ] string-literal ( expression )
25005 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25006 each node is the expression. The TREE_PURPOSE is itself a
25007 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25008 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25009 is a STRING_CST for the string literal before the parenthesis. Returns
25010 ERROR_MARK_NODE if any of the operands are invalid. */
25013 cp_parser_asm_operand_list (cp_parser
* parser
)
25015 tree asm_operands
= NULL_TREE
;
25016 bool invalid_operands
= false;
25020 tree string_literal
;
25024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25026 /* Consume the `[' token. */
25027 cp_lexer_consume_token (parser
->lexer
);
25028 /* Read the operand name. */
25029 name
= cp_parser_identifier (parser
);
25030 if (name
!= error_mark_node
)
25031 name
= build_string (IDENTIFIER_LENGTH (name
),
25032 IDENTIFIER_POINTER (name
));
25033 /* Look for the closing `]'. */
25034 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25038 /* Look for the string-literal. */
25039 string_literal
= cp_parser_string_literal (parser
, false, false);
25041 /* Look for the `('. */
25042 matching_parens parens
;
25043 parens
.require_open (parser
);
25044 /* Parse the expression. */
25045 expression
= cp_parser_expression (parser
);
25046 /* Look for the `)'. */
25047 parens
.require_close (parser
);
25049 if (name
== error_mark_node
25050 || string_literal
== error_mark_node
25051 || expression
== error_mark_node
)
25052 invalid_operands
= true;
25054 /* Add this operand to the list. */
25055 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25058 /* If the next token is not a `,', there are no more
25060 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25062 /* Consume the `,'. */
25063 cp_lexer_consume_token (parser
->lexer
);
25066 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25069 /* Parse an asm-clobber-list.
25073 asm-clobber-list , string-literal
25075 Returns a TREE_LIST, indicating the clobbers in the order that they
25076 appeared. The TREE_VALUE of each node is a STRING_CST. */
25079 cp_parser_asm_clobber_list (cp_parser
* parser
)
25081 tree clobbers
= NULL_TREE
;
25085 tree string_literal
;
25087 /* Look for the string literal. */
25088 string_literal
= cp_parser_string_literal (parser
, false, false);
25089 /* Add it to the list. */
25090 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25091 /* If the next token is not a `,', then the list is
25093 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25095 /* Consume the `,' token. */
25096 cp_lexer_consume_token (parser
->lexer
);
25102 /* Parse an asm-label-list.
25106 asm-label-list , identifier
25108 Returns a TREE_LIST, indicating the labels in the order that they
25109 appeared. The TREE_VALUE of each node is a label. */
25112 cp_parser_asm_label_list (cp_parser
* parser
)
25114 tree labels
= NULL_TREE
;
25118 tree identifier
, label
, name
;
25120 /* Look for the identifier. */
25121 identifier
= cp_parser_identifier (parser
);
25122 if (!error_operand_p (identifier
))
25124 label
= lookup_label (identifier
);
25125 if (TREE_CODE (label
) == LABEL_DECL
)
25127 TREE_USED (label
) = 1;
25128 check_goto (label
);
25129 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25130 IDENTIFIER_POINTER (identifier
));
25131 labels
= tree_cons (name
, label
, labels
);
25134 /* If the next token is not a `,', then the list is
25136 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25138 /* Consume the `,' token. */
25139 cp_lexer_consume_token (parser
->lexer
);
25142 return nreverse (labels
);
25145 /* Return TRUE iff the next tokens in the stream are possibly the
25146 beginning of a GNU extension attribute. */
25149 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25151 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25154 /* Return TRUE iff the next tokens in the stream are possibly the
25155 beginning of a standard C++-11 attribute specifier. */
25158 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25160 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25163 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25164 beginning of a standard C++-11 attribute specifier. */
25167 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25169 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25171 return (cxx_dialect
>= cxx11
25172 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25173 || (token
->type
== CPP_OPEN_SQUARE
25174 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25175 && token
->type
== CPP_OPEN_SQUARE
)));
25178 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25179 beginning of a GNU extension attribute. */
25182 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25184 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25186 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25189 /* Return true iff the next tokens can be the beginning of either a
25190 GNU attribute list, or a standard C++11 attribute sequence. */
25193 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25195 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25196 || cp_next_tokens_can_be_std_attribute_p (parser
));
25199 /* Return true iff the next Nth tokens can be the beginning of either
25200 a GNU attribute list, or a standard C++11 attribute sequence. */
25203 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25205 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25206 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25209 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25210 of GNU attributes, or return NULL. */
25213 cp_parser_attributes_opt (cp_parser
*parser
)
25215 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25216 return cp_parser_gnu_attributes_opt (parser
);
25217 return cp_parser_std_attribute_spec_seq (parser
);
25220 /* Parse an (optional) series of attributes.
25223 attributes attribute
25226 __attribute__ (( attribute-list [opt] ))
25228 The return value is as for cp_parser_gnu_attribute_list. */
25231 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25233 tree attributes
= NULL_TREE
;
25235 temp_override
<bool> cleanup
25236 (parser
->auto_is_implicit_function_template_parm_p
, false);
25241 tree attribute_list
;
25244 /* Peek at the next token. */
25245 token
= cp_lexer_peek_token (parser
->lexer
);
25246 /* If it's not `__attribute__', then we're done. */
25247 if (token
->keyword
!= RID_ATTRIBUTE
)
25250 /* Consume the `__attribute__' keyword. */
25251 cp_lexer_consume_token (parser
->lexer
);
25252 /* Look for the two `(' tokens. */
25253 matching_parens outer_parens
;
25254 outer_parens
.require_open (parser
);
25255 matching_parens inner_parens
;
25256 inner_parens
.require_open (parser
);
25258 /* Peek at the next token. */
25259 token
= cp_lexer_peek_token (parser
->lexer
);
25260 if (token
->type
!= CPP_CLOSE_PAREN
)
25261 /* Parse the attribute-list. */
25262 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25264 /* If the next token is a `)', then there is no attribute
25266 attribute_list
= NULL
;
25268 /* Look for the two `)' tokens. */
25269 if (!inner_parens
.require_close (parser
))
25271 if (!outer_parens
.require_close (parser
))
25274 cp_parser_skip_to_end_of_statement (parser
);
25276 /* Add these new attributes to the list. */
25277 attributes
= attr_chainon (attributes
, attribute_list
);
25283 /* Parse a GNU attribute-list.
25287 attribute-list , attribute
25291 identifier ( identifier )
25292 identifier ( identifier , expression-list )
25293 identifier ( expression-list )
25295 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25296 to an attribute. The TREE_PURPOSE of each node is the identifier
25297 indicating which attribute is in use. The TREE_VALUE represents
25298 the arguments, if any. */
25301 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25303 tree attribute_list
= NULL_TREE
;
25304 bool save_translate_strings_p
= parser
->translate_strings_p
;
25306 parser
->translate_strings_p
= false;
25313 /* Look for the identifier. We also allow keywords here; for
25314 example `__attribute__ ((const))' is legal. */
25315 token
= cp_lexer_peek_token (parser
->lexer
);
25316 if (token
->type
== CPP_NAME
25317 || token
->type
== CPP_KEYWORD
)
25319 tree arguments
= NULL_TREE
;
25321 /* Consume the token, but save it since we need it for the
25322 SIMD enabled function parsing. */
25323 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25325 /* Save away the identifier that indicates which attribute
25327 identifier
= (token
->type
== CPP_KEYWORD
)
25328 /* For keywords, use the canonical spelling, not the
25329 parsed identifier. */
25330 ? ridpointers
[(int) token
->keyword
]
25331 : id_token
->u
.value
;
25333 identifier
= canonicalize_attr_name (identifier
);
25334 attribute
= build_tree_list (identifier
, NULL_TREE
);
25336 /* Peek at the next token. */
25337 token
= cp_lexer_peek_token (parser
->lexer
);
25338 /* If it's an `(', then parse the attribute arguments. */
25339 if (token
->type
== CPP_OPEN_PAREN
)
25341 vec
<tree
, va_gc
> *vec
;
25342 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25343 ? id_attr
: normal_attr
);
25344 vec
= cp_parser_parenthesized_expression_list
25345 (parser
, attr_flag
, /*cast_p=*/false,
25346 /*allow_expansion_p=*/false,
25347 /*non_constant_p=*/NULL
);
25349 arguments
= error_mark_node
;
25352 arguments
= build_tree_list_vec (vec
);
25353 release_tree_vector (vec
);
25355 /* Save the arguments away. */
25356 TREE_VALUE (attribute
) = arguments
;
25359 if (arguments
!= error_mark_node
)
25361 /* Add this attribute to the list. */
25362 TREE_CHAIN (attribute
) = attribute_list
;
25363 attribute_list
= attribute
;
25366 token
= cp_lexer_peek_token (parser
->lexer
);
25368 /* Now, look for more attributes. If the next token isn't a
25369 `,', we're done. */
25370 if (token
->type
!= CPP_COMMA
)
25373 /* Consume the comma and keep going. */
25374 cp_lexer_consume_token (parser
->lexer
);
25376 parser
->translate_strings_p
= save_translate_strings_p
;
25378 /* We built up the list in reverse order. */
25379 return nreverse (attribute_list
);
25382 /* Parse a standard C++11 attribute.
25384 The returned representation is a TREE_LIST which TREE_PURPOSE is
25385 the scoped name of the attribute, and the TREE_VALUE is its
25388 Note that the scoped name of the attribute is itself a TREE_LIST
25389 which TREE_PURPOSE is the namespace of the attribute, and
25390 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25391 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25392 and which TREE_PURPOSE is directly the attribute name.
25394 Clients of the attribute code should use get_attribute_namespace
25395 and get_attribute_name to get the actual namespace and name of
25396 attributes, regardless of their being GNU or C++11 attributes.
25399 attribute-token attribute-argument-clause [opt]
25403 attribute-scoped-token
25405 attribute-scoped-token:
25406 attribute-namespace :: identifier
25408 attribute-namespace:
25411 attribute-argument-clause:
25412 ( balanced-token-seq )
25414 balanced-token-seq:
25415 balanced-token [opt]
25416 balanced-token-seq balanced-token
25419 ( balanced-token-seq )
25420 [ balanced-token-seq ]
25421 { balanced-token-seq }. */
25424 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25426 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25429 temp_override
<bool> cleanup
25430 (parser
->auto_is_implicit_function_template_parm_p
, false);
25432 /* First, parse name of the attribute, a.k.a attribute-token. */
25434 token
= cp_lexer_peek_token (parser
->lexer
);
25435 if (token
->type
== CPP_NAME
)
25436 attr_id
= token
->u
.value
;
25437 else if (token
->type
== CPP_KEYWORD
)
25438 attr_id
= ridpointers
[(int) token
->keyword
];
25439 else if (token
->flags
& NAMED_OP
)
25440 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25442 if (attr_id
== NULL_TREE
)
25445 cp_lexer_consume_token (parser
->lexer
);
25447 token
= cp_lexer_peek_token (parser
->lexer
);
25448 if (token
->type
== CPP_SCOPE
)
25450 /* We are seeing a scoped attribute token. */
25452 cp_lexer_consume_token (parser
->lexer
);
25454 error_at (token
->location
, "attribute using prefix used together "
25455 "with scoped attribute token");
25458 token
= cp_lexer_consume_token (parser
->lexer
);
25459 if (token
->type
== CPP_NAME
)
25460 attr_id
= token
->u
.value
;
25461 else if (token
->type
== CPP_KEYWORD
)
25462 attr_id
= ridpointers
[(int) token
->keyword
];
25463 else if (token
->flags
& NAMED_OP
)
25464 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25467 error_at (token
->location
,
25468 "expected an identifier for the attribute name");
25469 return error_mark_node
;
25472 attr_ns
= canonicalize_attr_name (attr_ns
);
25473 attr_id
= canonicalize_attr_name (attr_id
);
25474 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25476 token
= cp_lexer_peek_token (parser
->lexer
);
25480 attr_ns
= canonicalize_attr_name (attr_ns
);
25481 attr_id
= canonicalize_attr_name (attr_id
);
25482 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25487 attr_id
= canonicalize_attr_name (attr_id
);
25488 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25490 /* C++11 noreturn attribute is equivalent to GNU's. */
25491 if (is_attribute_p ("noreturn", attr_id
))
25492 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25493 /* C++14 deprecated attribute is equivalent to GNU's. */
25494 else if (is_attribute_p ("deprecated", attr_id
))
25495 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25496 /* C++17 fallthrough attribute is equivalent to GNU's. */
25497 else if (is_attribute_p ("fallthrough", attr_id
))
25498 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25499 /* Transactional Memory TS optimize_for_synchronized attribute is
25500 equivalent to GNU transaction_callable. */
25501 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25502 TREE_PURPOSE (attribute
)
25503 = get_identifier ("transaction_callable");
25504 /* Transactional Memory attributes are GNU attributes. */
25505 else if (tm_attr_to_mask (attr_id
))
25506 TREE_PURPOSE (attribute
) = attr_id
;
25509 /* Now parse the optional argument clause of the attribute. */
25511 if (token
->type
!= CPP_OPEN_PAREN
)
25515 vec
<tree
, va_gc
> *vec
;
25516 int attr_flag
= normal_attr
;
25518 if (attr_ns
== gnu_identifier
25519 && attribute_takes_identifier_p (attr_id
))
25520 /* A GNU attribute that takes an identifier in parameter. */
25521 attr_flag
= id_attr
;
25523 vec
= cp_parser_parenthesized_expression_list
25524 (parser
, attr_flag
, /*cast_p=*/false,
25525 /*allow_expansion_p=*/true,
25526 /*non_constant_p=*/NULL
);
25528 arguments
= error_mark_node
;
25531 arguments
= build_tree_list_vec (vec
);
25532 release_tree_vector (vec
);
25535 if (arguments
== error_mark_node
)
25536 attribute
= error_mark_node
;
25538 TREE_VALUE (attribute
) = arguments
;
25544 /* Check that the attribute ATTRIBUTE appears at most once in the
25545 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25546 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25547 isn't implemented yet in GCC. */
25550 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25554 tree name
= get_attribute_name (attribute
);
25555 if (is_attribute_p ("noreturn", name
)
25556 && lookup_attribute ("noreturn", attributes
))
25557 error ("attribute %<noreturn%> can appear at most once "
25558 "in an attribute-list");
25559 else if (is_attribute_p ("deprecated", name
)
25560 && lookup_attribute ("deprecated", attributes
))
25561 error ("attribute %<deprecated%> can appear at most once "
25562 "in an attribute-list");
25566 /* Parse a list of standard C++-11 attributes.
25570 attribute-list , attribute[opt]
25572 attribute-list , attribute ...
25576 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25578 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25579 cp_token
*token
= NULL
;
25583 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25584 if (attribute
== error_mark_node
)
25586 if (attribute
!= NULL_TREE
)
25588 cp_parser_check_std_attribute (attributes
, attribute
);
25589 TREE_CHAIN (attribute
) = attributes
;
25590 attributes
= attribute
;
25592 token
= cp_lexer_peek_token (parser
->lexer
);
25593 if (token
->type
== CPP_ELLIPSIS
)
25595 cp_lexer_consume_token (parser
->lexer
);
25596 if (attribute
== NULL_TREE
)
25597 error_at (token
->location
,
25598 "expected attribute before %<...%>");
25601 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25602 if (pack
== error_mark_node
)
25603 return error_mark_node
;
25604 TREE_VALUE (attribute
) = pack
;
25606 token
= cp_lexer_peek_token (parser
->lexer
);
25608 if (token
->type
!= CPP_COMMA
)
25610 cp_lexer_consume_token (parser
->lexer
);
25612 attributes
= nreverse (attributes
);
25616 /* Parse a standard C++-11 attribute specifier.
25618 attribute-specifier:
25619 [ [ attribute-using-prefix [opt] attribute-list ] ]
25620 alignment-specifier
25622 attribute-using-prefix:
25623 using attribute-namespace :
25625 alignment-specifier:
25626 alignas ( type-id ... [opt] )
25627 alignas ( alignment-expression ... [opt] ). */
25630 cp_parser_std_attribute_spec (cp_parser
*parser
)
25632 tree attributes
= NULL_TREE
;
25633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25635 if (token
->type
== CPP_OPEN_SQUARE
25636 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25638 tree attr_ns
= NULL_TREE
;
25640 cp_lexer_consume_token (parser
->lexer
);
25641 cp_lexer_consume_token (parser
->lexer
);
25643 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25645 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25646 if (token
->type
== CPP_NAME
)
25647 attr_ns
= token
->u
.value
;
25648 else if (token
->type
== CPP_KEYWORD
)
25649 attr_ns
= ridpointers
[(int) token
->keyword
];
25650 else if (token
->flags
& NAMED_OP
)
25651 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25654 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25656 if (cxx_dialect
< cxx17
25657 && !in_system_header_at (input_location
))
25658 pedwarn (input_location
, 0,
25659 "attribute using prefix only available "
25660 "with -std=c++17 or -std=gnu++17");
25662 cp_lexer_consume_token (parser
->lexer
);
25663 cp_lexer_consume_token (parser
->lexer
);
25664 cp_lexer_consume_token (parser
->lexer
);
25667 attr_ns
= NULL_TREE
;
25670 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25672 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25673 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25674 cp_parser_skip_to_end_of_statement (parser
);
25676 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25677 when we are sure that we have actually parsed them. */
25678 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25684 /* Look for an alignment-specifier. */
25686 token
= cp_lexer_peek_token (parser
->lexer
);
25688 if (token
->type
!= CPP_KEYWORD
25689 || token
->keyword
!= RID_ALIGNAS
)
25692 cp_lexer_consume_token (parser
->lexer
);
25693 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25695 matching_parens parens
;
25696 if (!parens
.require_open (parser
))
25697 return error_mark_node
;
25699 cp_parser_parse_tentatively (parser
);
25700 alignas_expr
= cp_parser_type_id (parser
);
25702 if (!cp_parser_parse_definitely (parser
))
25704 alignas_expr
= cp_parser_assignment_expression (parser
);
25705 if (alignas_expr
== error_mark_node
)
25706 cp_parser_skip_to_end_of_statement (parser
);
25707 if (alignas_expr
== NULL_TREE
25708 || alignas_expr
== error_mark_node
)
25709 return alignas_expr
;
25712 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25713 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25715 /* Handle alignas (pack...). */
25716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25718 cp_lexer_consume_token (parser
->lexer
);
25719 alignas_expr
= make_pack_expansion (alignas_expr
);
25722 /* Something went wrong, so don't build the attribute. */
25723 if (alignas_expr
== error_mark_node
)
25724 return error_mark_node
;
25726 if (!parens
.require_close (parser
))
25727 return error_mark_node
;
25729 /* Build the C++-11 representation of an 'aligned'
25732 = build_tree_list (build_tree_list (gnu_identifier
,
25733 aligned_identifier
), alignas_expr
);
25739 /* Parse a standard C++-11 attribute-specifier-seq.
25741 attribute-specifier-seq:
25742 attribute-specifier-seq [opt] attribute-specifier
25746 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25748 tree attr_specs
= NULL_TREE
;
25749 tree attr_last
= NULL_TREE
;
25753 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25754 if (attr_spec
== NULL_TREE
)
25756 if (attr_spec
== error_mark_node
)
25757 return error_mark_node
;
25760 TREE_CHAIN (attr_last
) = attr_spec
;
25762 attr_specs
= attr_last
= attr_spec
;
25763 attr_last
= tree_last (attr_last
);
25769 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25770 return index of the first token after balanced-token, or N on failure. */
25773 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25776 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25778 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25781 case CPP_PRAGMA_EOL
:
25782 /* Ran out of tokens. */
25784 case CPP_OPEN_PAREN
:
25787 case CPP_OPEN_BRACE
:
25790 case CPP_OPEN_SQUARE
:
25793 case CPP_CLOSE_PAREN
:
25796 case CPP_CLOSE_BRACE
:
25799 case CPP_CLOSE_SQUARE
:
25805 while (nparens
|| nbraces
|| nsquares
);
25809 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25810 return index of the first token after the GNU attribute tokens, or N on
25814 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25818 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25819 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25820 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25823 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25826 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25833 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25834 next token), return index of the first token after the standard C++11
25835 attribute tokens, or N on failure. */
25838 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25842 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25843 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25845 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25848 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25852 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25853 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25855 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25866 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25867 as the next token), return index of the first token after the attribute
25868 tokens, or N on failure. */
25871 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25873 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25874 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25875 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25878 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25879 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25880 current value of the PEDANTIC flag, regardless of whether or not
25881 the `__extension__' keyword is present. The caller is responsible
25882 for restoring the value of the PEDANTIC flag. */
25885 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25887 /* Save the old value of the PEDANTIC flag. */
25888 *saved_pedantic
= pedantic
;
25890 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25892 /* Consume the `__extension__' token. */
25893 cp_lexer_consume_token (parser
->lexer
);
25894 /* We're not being pedantic while the `__extension__' keyword is
25904 /* Parse a label declaration.
25907 __label__ label-declarator-seq ;
25909 label-declarator-seq:
25910 identifier , label-declarator-seq
25914 cp_parser_label_declaration (cp_parser
* parser
)
25916 /* Look for the `__label__' keyword. */
25917 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25923 /* Look for an identifier. */
25924 identifier
= cp_parser_identifier (parser
);
25925 /* If we failed, stop. */
25926 if (identifier
== error_mark_node
)
25928 /* Declare it as a label. */
25929 finish_label_decl (identifier
);
25930 /* If the next token is a `;', stop. */
25931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25933 /* Look for the `,' separating the label declarations. */
25934 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25937 /* Look for the final `;'. */
25938 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25941 // -------------------------------------------------------------------------- //
25944 // Parse a requires clause.
25946 // requires-clause:
25947 // 'requires' logical-or-expression
25949 // The required logical-or-expression must be a constant expression. Note
25950 // that we don't check that the expression is constepxr here. We defer until
25951 // we analyze constraints and then, we only check atomic constraints.
25953 cp_parser_requires_clause (cp_parser
*parser
)
25955 // Parse the requires clause so that it is not automatically folded.
25956 ++processing_template_decl
;
25957 tree expr
= cp_parser_binary_expression (parser
, false, false,
25958 PREC_NOT_OPERATOR
, NULL
);
25959 if (check_for_bare_parameter_packs (expr
))
25960 expr
= error_mark_node
;
25961 --processing_template_decl
;
25965 // Optionally parse a requires clause:
25967 cp_parser_requires_clause_opt (cp_parser
*parser
)
25969 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25970 if (tok
->keyword
!= RID_REQUIRES
)
25972 if (!flag_concepts
&& tok
->type
== CPP_NAME
25973 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25975 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25976 "%<requires%> only available with -fconcepts");
25977 /* Parse and discard the requires-clause. */
25978 cp_lexer_consume_token (parser
->lexer
);
25979 cp_parser_requires_clause (parser
);
25983 cp_lexer_consume_token (parser
->lexer
);
25984 return cp_parser_requires_clause (parser
);
25988 /*---------------------------------------------------------------------------
25989 Requires expressions
25990 ---------------------------------------------------------------------------*/
25992 /* Parse a requires expression
25994 requirement-expression:
25995 'requires' requirement-parameter-list [opt] requirement-body */
25997 cp_parser_requires_expression (cp_parser
*parser
)
25999 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26000 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26002 /* A requires-expression shall appear only within a concept
26003 definition or a requires-clause.
26005 TODO: Implement this diagnostic correctly. */
26006 if (!processing_template_decl
)
26008 error_at (loc
, "a requires expression cannot appear outside a template");
26009 cp_parser_skip_to_end_of_statement (parser
);
26010 return error_mark_node
;
26015 /* Local parameters are delared as variables within the scope
26016 of the expression. They are not visible past the end of
26017 the expression. Expressions within the requires-expression
26018 are unevaluated. */
26019 struct scope_sentinel
26023 ++cp_unevaluated_operand
;
26024 begin_scope (sk_block
, NULL_TREE
);
26029 pop_bindings_and_leave_scope ();
26030 --cp_unevaluated_operand
;
26034 /* Parse the optional parameter list. */
26035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26037 parms
= cp_parser_requirement_parameter_list (parser
);
26038 if (parms
== error_mark_node
)
26039 return error_mark_node
;
26044 /* Parse the requirement body. */
26045 reqs
= cp_parser_requirement_body (parser
);
26046 if (reqs
== error_mark_node
)
26047 return error_mark_node
;
26050 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26052 grokparms (parms
, &parms
);
26053 return finish_requires_expr (parms
, reqs
);
26056 /* Parse a parameterized requirement.
26058 requirement-parameter-list:
26059 '(' parameter-declaration-clause ')' */
26061 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26063 matching_parens parens
;
26064 if (!parens
.require_open (parser
))
26065 return error_mark_node
;
26067 tree parms
= cp_parser_parameter_declaration_clause (parser
);
26069 if (!parens
.require_close (parser
))
26070 return error_mark_node
;
26075 /* Parse the body of a requirement.
26078 '{' requirement-list '}' */
26080 cp_parser_requirement_body (cp_parser
*parser
)
26082 matching_braces braces
;
26083 if (!braces
.require_open (parser
))
26084 return error_mark_node
;
26086 tree reqs
= cp_parser_requirement_list (parser
);
26088 if (!braces
.require_close (parser
))
26089 return error_mark_node
;
26094 /* Parse a list of requirements.
26098 requirement-list ';' requirement[opt] */
26100 cp_parser_requirement_list (cp_parser
*parser
)
26102 tree result
= NULL_TREE
;
26105 tree req
= cp_parser_requirement (parser
);
26106 if (req
== error_mark_node
)
26107 return error_mark_node
;
26109 result
= tree_cons (NULL_TREE
, req
, result
);
26111 /* If we see a semi-colon, consume it. */
26112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26113 cp_lexer_consume_token (parser
->lexer
);
26115 /* Stop processing at the end of the list. */
26116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26120 /* Reverse the order of requirements so they are analyzed in
26121 declaration order. */
26122 return nreverse (result
);
26125 /* Parse a syntactic requirement or type requirement.
26129 compound-requirement
26131 nested-requirement */
26133 cp_parser_requirement (cp_parser
*parser
)
26135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26136 return cp_parser_compound_requirement (parser
);
26137 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26138 return cp_parser_type_requirement (parser
);
26139 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26140 return cp_parser_nested_requirement (parser
);
26142 return cp_parser_simple_requirement (parser
);
26145 /* Parse a simple requirement.
26147 simple-requirement:
26150 cp_parser_simple_requirement (cp_parser
*parser
)
26152 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26153 if (!expr
|| expr
== error_mark_node
)
26154 return error_mark_node
;
26156 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26157 return error_mark_node
;
26159 return finish_simple_requirement (expr
);
26162 /* Parse a type requirement
26165 nested-name-specifier [opt] required-type-name ';'
26167 required-type-name:
26169 'template' [opt] simple-template-id */
26171 cp_parser_type_requirement (cp_parser
*parser
)
26173 cp_lexer_consume_token (parser
->lexer
);
26175 // Save the scope before parsing name specifiers.
26176 tree saved_scope
= parser
->scope
;
26177 tree saved_object_scope
= parser
->object_scope
;
26178 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26179 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26180 cp_parser_nested_name_specifier_opt (parser
,
26181 /*typename_keyword_p=*/true,
26182 /*check_dependency_p=*/false,
26184 /*is_declaration=*/false);
26187 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26189 cp_lexer_consume_token (parser
->lexer
);
26190 type
= cp_parser_template_id (parser
,
26191 /*template_keyword_p=*/true,
26192 /*check_dependency=*/false,
26193 /*tag_type=*/none_type
,
26194 /*is_declaration=*/false);
26195 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26196 /*complain=*/tf_error
);
26199 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26201 if (TREE_CODE (type
) == TYPE_DECL
)
26202 type
= TREE_TYPE (type
);
26204 parser
->scope
= saved_scope
;
26205 parser
->object_scope
= saved_object_scope
;
26206 parser
->qualifying_scope
= saved_qualifying_scope
;
26208 if (type
== error_mark_node
)
26209 cp_parser_skip_to_end_of_statement (parser
);
26211 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26212 return error_mark_node
;
26213 if (type
== error_mark_node
)
26214 return error_mark_node
;
26216 return finish_type_requirement (type
);
26219 /* Parse a compound requirement
26221 compound-requirement:
26222 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26224 cp_parser_compound_requirement (cp_parser
*parser
)
26226 /* Parse an expression enclosed in '{ }'s. */
26227 matching_braces braces
;
26228 if (!braces
.require_open (parser
))
26229 return error_mark_node
;
26231 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26232 if (!expr
|| expr
== error_mark_node
)
26233 return error_mark_node
;
26235 if (!braces
.require_close (parser
))
26236 return error_mark_node
;
26238 /* Parse the optional noexcept. */
26239 bool noexcept_p
= false;
26240 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26242 cp_lexer_consume_token (parser
->lexer
);
26246 /* Parse the optional trailing return type. */
26247 tree type
= NULL_TREE
;
26248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26250 cp_lexer_consume_token (parser
->lexer
);
26251 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26252 parser
->in_result_type_constraint_p
= true;
26253 type
= cp_parser_trailing_type_id (parser
);
26254 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26255 if (type
== error_mark_node
)
26256 return error_mark_node
;
26259 return finish_compound_requirement (expr
, type
, noexcept_p
);
26262 /* Parse a nested requirement. This is the same as a requires clause.
26264 nested-requirement:
26267 cp_parser_nested_requirement (cp_parser
*parser
)
26269 cp_lexer_consume_token (parser
->lexer
);
26270 tree req
= cp_parser_requires_clause (parser
);
26271 if (req
== error_mark_node
)
26272 return error_mark_node
;
26273 return finish_nested_requirement (req
);
26276 /* Support Functions */
26278 /* Return the appropriate prefer_type argument for lookup_name_real based on
26279 tag_type and template_mem_access. */
26282 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26284 /* DR 141: When looking in the current enclosing context for a template-name
26285 after -> or ., only consider class templates. */
26286 if (template_mem_access
)
26290 case none_type
: return 0; // No preference.
26291 case scope_type
: return 1; // Type or namespace.
26292 default: return 2; // Type only.
26296 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26297 NAME should have one of the representations used for an
26298 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26299 is returned. If PARSER->SCOPE is a dependent type, then a
26300 SCOPE_REF is returned.
26302 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26303 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26304 was formed. Abstractly, such entities should not be passed to this
26305 function, because they do not need to be looked up, but it is
26306 simpler to check for this special case here, rather than at the
26309 In cases not explicitly covered above, this function returns a
26310 DECL, OVERLOAD, or baselink representing the result of the lookup.
26311 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26314 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26315 (e.g., "struct") that was used. In that case bindings that do not
26316 refer to types are ignored.
26318 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26321 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26324 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26327 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26328 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26329 NULL_TREE otherwise. */
26332 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26333 enum tag_types tag_type
,
26336 bool check_dependency
,
26337 tree
*ambiguous_decls
,
26338 location_t name_location
)
26341 tree object_type
= parser
->context
->object_type
;
26343 /* Assume that the lookup will be unambiguous. */
26344 if (ambiguous_decls
)
26345 *ambiguous_decls
= NULL_TREE
;
26347 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26348 no longer valid. Note that if we are parsing tentatively, and
26349 the parse fails, OBJECT_TYPE will be automatically restored. */
26350 parser
->context
->object_type
= NULL_TREE
;
26352 if (name
== error_mark_node
)
26353 return error_mark_node
;
26355 /* A template-id has already been resolved; there is no lookup to
26357 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26359 if (BASELINK_P (name
))
26361 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26362 == TEMPLATE_ID_EXPR
);
26366 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26367 it should already have been checked to make sure that the name
26368 used matches the type being destroyed. */
26369 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26373 /* Figure out to which type this destructor applies. */
26375 type
= parser
->scope
;
26376 else if (object_type
)
26377 type
= object_type
;
26379 type
= current_class_type
;
26380 /* If that's not a class type, there is no destructor. */
26381 if (!type
|| !CLASS_TYPE_P (type
))
26382 return error_mark_node
;
26384 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26385 lazily_declare_fn (sfk_destructor
, type
);
26387 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26390 return error_mark_node
;
26393 /* By this point, the NAME should be an ordinary identifier. If
26394 the id-expression was a qualified name, the qualifying scope is
26395 stored in PARSER->SCOPE at this point. */
26396 gcc_assert (identifier_p (name
));
26398 /* Perform the lookup. */
26403 if (parser
->scope
== error_mark_node
)
26404 return error_mark_node
;
26406 /* If the SCOPE is dependent, the lookup must be deferred until
26407 the template is instantiated -- unless we are explicitly
26408 looking up names in uninstantiated templates. Even then, we
26409 cannot look up the name if the scope is not a class type; it
26410 might, for example, be a template type parameter. */
26411 dependent_p
= (TYPE_P (parser
->scope
)
26412 && dependent_scope_p (parser
->scope
));
26413 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26415 /* Defer lookup. */
26416 decl
= error_mark_node
;
26419 tree pushed_scope
= NULL_TREE
;
26421 /* If PARSER->SCOPE is a dependent type, then it must be a
26422 class type, and we must not be checking dependencies;
26423 otherwise, we would have processed this lookup above. So
26424 that PARSER->SCOPE is not considered a dependent base by
26425 lookup_member, we must enter the scope here. */
26427 pushed_scope
= push_scope (parser
->scope
);
26429 /* If the PARSER->SCOPE is a template specialization, it
26430 may be instantiated during name lookup. In that case,
26431 errors may be issued. Even if we rollback the current
26432 tentative parse, those errors are valid. */
26433 decl
= lookup_qualified_name (parser
->scope
, name
,
26434 prefer_type_arg (tag_type
),
26435 /*complain=*/true);
26437 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26438 lookup result and the nested-name-specifier nominates a class C:
26439 * if the name specified after the nested-name-specifier, when
26440 looked up in C, is the injected-class-name of C (Clause 9), or
26441 * if the name specified after the nested-name-specifier is the
26442 same as the identifier or the simple-template-id's template-
26443 name in the last component of the nested-name-specifier,
26444 the name is instead considered to name the constructor of
26445 class C. [ Note: for example, the constructor is not an
26446 acceptable lookup result in an elaborated-type-specifier so
26447 the constructor would not be used in place of the
26448 injected-class-name. --end note ] Such a constructor name
26449 shall be used only in the declarator-id of a declaration that
26450 names a constructor or in a using-declaration. */
26451 if (tag_type
== none_type
26452 && DECL_SELF_REFERENCE_P (decl
)
26453 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26454 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26455 prefer_type_arg (tag_type
),
26456 /*complain=*/true);
26458 /* If we have a single function from a using decl, pull it out. */
26459 if (TREE_CODE (decl
) == OVERLOAD
26460 && !really_overloaded_fn (decl
))
26461 decl
= OVL_FUNCTION (decl
);
26464 pop_scope (pushed_scope
);
26467 /* If the scope is a dependent type and either we deferred lookup or
26468 we did lookup but didn't find the name, rememeber the name. */
26469 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26470 && dependent_type_p (parser
->scope
))
26476 /* The resolution to Core Issue 180 says that `struct
26477 A::B' should be considered a type-name, even if `A'
26479 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26480 /*complain=*/tf_error
);
26481 if (type
!= error_mark_node
)
26482 decl
= TYPE_NAME (type
);
26484 else if (is_template
26485 && (cp_parser_next_token_ends_template_argument_p (parser
)
26486 || cp_lexer_next_token_is (parser
->lexer
,
26488 decl
= make_unbound_class_template (parser
->scope
,
26490 /*complain=*/tf_error
);
26492 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26493 parser
->scope
, name
,
26496 parser
->qualifying_scope
= parser
->scope
;
26497 parser
->object_scope
= NULL_TREE
;
26499 else if (object_type
)
26501 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26502 OBJECT_TYPE is not a class. */
26503 if (CLASS_TYPE_P (object_type
))
26504 /* If the OBJECT_TYPE is a template specialization, it may
26505 be instantiated during name lookup. In that case, errors
26506 may be issued. Even if we rollback the current tentative
26507 parse, those errors are valid. */
26508 decl
= lookup_member (object_type
,
26511 prefer_type_arg (tag_type
),
26512 tf_warning_or_error
);
26517 /* Look it up in the enclosing context. DR 141: When looking for a
26518 template-name after -> or ., only consider class templates. */
26519 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26521 /*block_p=*/true, is_namespace
, 0);
26522 if (object_type
== unknown_type_node
)
26523 /* The object is type-dependent, so we can't look anything up; we used
26524 this to get the DR 141 behavior. */
26525 object_type
= NULL_TREE
;
26526 parser
->object_scope
= object_type
;
26527 parser
->qualifying_scope
= NULL_TREE
;
26531 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26533 /*block_p=*/true, is_namespace
, 0);
26534 parser
->qualifying_scope
= NULL_TREE
;
26535 parser
->object_scope
= NULL_TREE
;
26538 /* If the lookup failed, let our caller know. */
26539 if (!decl
|| decl
== error_mark_node
)
26540 return error_mark_node
;
26542 /* Pull out the template from an injected-class-name (or multiple). */
26544 decl
= maybe_get_template_decl_from_type_decl (decl
);
26546 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26547 if (TREE_CODE (decl
) == TREE_LIST
)
26549 if (ambiguous_decls
)
26550 *ambiguous_decls
= decl
;
26551 /* The error message we have to print is too complicated for
26552 cp_parser_error, so we incorporate its actions directly. */
26553 if (!cp_parser_simulate_error (parser
))
26555 error_at (name_location
, "reference to %qD is ambiguous",
26557 print_candidates (decl
);
26559 return error_mark_node
;
26562 gcc_assert (DECL_P (decl
)
26563 || TREE_CODE (decl
) == OVERLOAD
26564 || TREE_CODE (decl
) == SCOPE_REF
26565 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26566 || BASELINK_P (decl
));
26568 /* If we have resolved the name of a member declaration, check to
26569 see if the declaration is accessible. When the name resolves to
26570 set of overloaded functions, accessibility is checked when
26571 overload resolution is done.
26573 During an explicit instantiation, access is not checked at all,
26574 as per [temp.explicit]. */
26576 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26578 maybe_record_typedef_use (decl
);
26580 return cp_expr (decl
, name_location
);
26583 /* Like cp_parser_lookup_name, but for use in the typical case where
26584 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26585 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26588 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26590 return cp_parser_lookup_name (parser
, name
,
26592 /*is_template=*/false,
26593 /*is_namespace=*/false,
26594 /*check_dependency=*/true,
26595 /*ambiguous_decls=*/NULL
,
26599 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26600 the current context, return the TYPE_DECL. If TAG_NAME_P is
26601 true, the DECL indicates the class being defined in a class-head,
26602 or declared in an elaborated-type-specifier.
26604 Otherwise, return DECL. */
26607 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26609 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26610 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26613 template <typename T> struct B;
26616 template <typename T> struct A::B {};
26618 Similarly, in an elaborated-type-specifier:
26620 namespace N { struct X{}; }
26623 template <typename T> friend struct N::X;
26626 However, if the DECL refers to a class type, and we are in
26627 the scope of the class, then the name lookup automatically
26628 finds the TYPE_DECL created by build_self_reference rather
26629 than a TEMPLATE_DECL. For example, in:
26631 template <class T> struct S {
26635 there is no need to handle such case. */
26637 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26638 return DECL_TEMPLATE_RESULT (decl
);
26643 /* If too many, or too few, template-parameter lists apply to the
26644 declarator, issue an error message. Returns TRUE if all went well,
26645 and FALSE otherwise. */
26648 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26649 cp_declarator
*declarator
,
26650 location_t declarator_location
)
26652 switch (declarator
->kind
)
26656 unsigned num_templates
= 0;
26657 tree scope
= declarator
->u
.id
.qualifying_scope
;
26658 bool template_id_p
= false;
26661 num_templates
= num_template_headers_for_class (scope
);
26662 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26663 == TEMPLATE_ID_EXPR
)
26665 /* If the DECLARATOR has the form `X<y>' then it uses one
26666 additional level of template parameters. */
26668 template_id_p
= true;
26671 return cp_parser_check_template_parameters
26672 (parser
, num_templates
, template_id_p
, declarator_location
,
26679 case cdk_reference
:
26681 return (cp_parser_check_declarator_template_parameters
26682 (parser
, declarator
->declarator
, declarator_location
));
26689 gcc_unreachable ();
26694 /* NUM_TEMPLATES were used in the current declaration. If that is
26695 invalid, return FALSE and issue an error messages. Otherwise,
26696 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26697 declarator and we can print more accurate diagnostics. */
26700 cp_parser_check_template_parameters (cp_parser
* parser
,
26701 unsigned num_templates
,
26702 bool template_id_p
,
26703 location_t location
,
26704 cp_declarator
*declarator
)
26706 /* If there are the same number of template classes and parameter
26707 lists, that's OK. */
26708 if (parser
->num_template_parameter_lists
== num_templates
)
26710 /* If there are more, but only one more, and the name ends in an identifier,
26711 then we are declaring a primary template. That's OK too. */
26713 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26715 /* If there are more template classes than parameter lists, we have
26718 template <class T> void S<T>::R<T>::f (); */
26719 if (parser
->num_template_parameter_lists
< num_templates
)
26721 if (declarator
&& !current_function_decl
)
26722 error_at (location
, "specializing member %<%T::%E%> "
26723 "requires %<template<>%> syntax",
26724 declarator
->u
.id
.qualifying_scope
,
26725 declarator
->u
.id
.unqualified_name
);
26726 else if (declarator
)
26727 error_at (location
, "invalid declaration of %<%T::%E%>",
26728 declarator
->u
.id
.qualifying_scope
,
26729 declarator
->u
.id
.unqualified_name
);
26731 error_at (location
, "too few template-parameter-lists");
26734 /* Otherwise, there are too many template parameter lists. We have
26737 template <class T> template <class U> void S::f(); */
26738 error_at (location
, "too many template-parameter-lists");
26742 /* Parse an optional `::' token indicating that the following name is
26743 from the global namespace. If so, PARSER->SCOPE is set to the
26744 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26745 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26746 Returns the new value of PARSER->SCOPE, if the `::' token is
26747 present, and NULL_TREE otherwise. */
26750 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26754 /* Peek at the next token. */
26755 token
= cp_lexer_peek_token (parser
->lexer
);
26756 /* If we're looking at a `::' token then we're starting from the
26757 global namespace, not our current location. */
26758 if (token
->type
== CPP_SCOPE
)
26760 /* Consume the `::' token. */
26761 cp_lexer_consume_token (parser
->lexer
);
26762 /* Set the SCOPE so that we know where to start the lookup. */
26763 parser
->scope
= global_namespace
;
26764 parser
->qualifying_scope
= global_namespace
;
26765 parser
->object_scope
= NULL_TREE
;
26767 return parser
->scope
;
26769 else if (!current_scope_valid_p
)
26771 parser
->scope
= NULL_TREE
;
26772 parser
->qualifying_scope
= NULL_TREE
;
26773 parser
->object_scope
= NULL_TREE
;
26779 /* Returns TRUE if the upcoming token sequence is the start of a
26780 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26781 declarator is preceded by the `friend' specifier. */
26784 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26786 bool constructor_p
;
26787 bool outside_class_specifier_p
;
26788 tree nested_name_specifier
;
26789 cp_token
*next_token
;
26791 /* The common case is that this is not a constructor declarator, so
26792 try to avoid doing lots of work if at all possible. It's not
26793 valid declare a constructor at function scope. */
26794 if (parser
->in_function_body
)
26796 /* And only certain tokens can begin a constructor declarator. */
26797 next_token
= cp_lexer_peek_token (parser
->lexer
);
26798 if (next_token
->type
!= CPP_NAME
26799 && next_token
->type
!= CPP_SCOPE
26800 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26801 && next_token
->type
!= CPP_TEMPLATE_ID
)
26804 /* Parse tentatively; we are going to roll back all of the tokens
26806 cp_parser_parse_tentatively (parser
);
26807 /* Assume that we are looking at a constructor declarator. */
26808 constructor_p
= true;
26810 /* Look for the optional `::' operator. */
26811 cp_parser_global_scope_opt (parser
,
26812 /*current_scope_valid_p=*/false);
26813 /* Look for the nested-name-specifier. */
26814 nested_name_specifier
26815 = (cp_parser_nested_name_specifier_opt (parser
,
26816 /*typename_keyword_p=*/false,
26817 /*check_dependency_p=*/false,
26819 /*is_declaration=*/false));
26821 outside_class_specifier_p
= (!at_class_scope_p ()
26822 || !TYPE_BEING_DEFINED (current_class_type
)
26825 /* Outside of a class-specifier, there must be a
26826 nested-name-specifier. Except in C++17 mode, where we
26827 might be declaring a guiding declaration. */
26828 if (!nested_name_specifier
&& outside_class_specifier_p
26829 && cxx_dialect
< cxx17
)
26830 constructor_p
= false;
26831 else if (nested_name_specifier
== error_mark_node
)
26832 constructor_p
= false;
26834 /* If we have a class scope, this is easy; DR 147 says that S::S always
26835 names the constructor, and no other qualified name could. */
26836 if (constructor_p
&& nested_name_specifier
26837 && CLASS_TYPE_P (nested_name_specifier
))
26839 tree id
= cp_parser_unqualified_id (parser
,
26840 /*template_keyword_p=*/false,
26841 /*check_dependency_p=*/false,
26842 /*declarator_p=*/true,
26843 /*optional_p=*/false);
26844 if (is_overloaded_fn (id
))
26845 id
= DECL_NAME (get_first_fn (id
));
26846 if (!constructor_name_p (id
, nested_name_specifier
))
26847 constructor_p
= false;
26849 /* If we still think that this might be a constructor-declarator,
26850 look for a class-name. */
26851 else if (constructor_p
)
26855 template <typename T> struct S {
26859 we must recognize that the nested `S' names a class. */
26860 if (cxx_dialect
>= cxx17
)
26861 cp_parser_parse_tentatively (parser
);
26864 type_decl
= cp_parser_class_name (parser
,
26865 /*typename_keyword_p=*/false,
26866 /*template_keyword_p=*/false,
26868 /*check_dependency_p=*/false,
26869 /*class_head_p=*/false,
26870 /*is_declaration=*/false);
26872 if (cxx_dialect
>= cxx17
26873 && !cp_parser_parse_definitely (parser
))
26875 type_decl
= NULL_TREE
;
26876 tree tmpl
= cp_parser_template_name (parser
,
26877 /*template_keyword*/false,
26878 /*check_dependency_p*/false,
26879 /*is_declaration*/false,
26881 /*is_identifier*/NULL
);
26882 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26883 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26884 /* It's a deduction guide, return true. */;
26886 cp_parser_simulate_error (parser
);
26889 /* If there was no class-name, then this is not a constructor.
26890 Otherwise, if we are in a class-specifier and we aren't
26891 handling a friend declaration, check that its type matches
26892 current_class_type (c++/38313). Note: error_mark_node
26893 is left alone for error recovery purposes. */
26894 constructor_p
= (!cp_parser_error_occurred (parser
)
26895 && (outside_class_specifier_p
26896 || type_decl
== NULL_TREE
26897 || type_decl
== error_mark_node
26898 || same_type_p (current_class_type
,
26899 TREE_TYPE (type_decl
))));
26901 /* If we're still considering a constructor, we have to see a `(',
26902 to begin the parameter-declaration-clause, followed by either a
26903 `)', an `...', or a decl-specifier. We need to check for a
26904 type-specifier to avoid being fooled into thinking that:
26908 is a constructor. (It is actually a function named `f' that
26909 takes one parameter (of type `int') and returns a value of type
26912 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26913 constructor_p
= false;
26916 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26917 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26918 /* A parameter declaration begins with a decl-specifier,
26919 which is either the "attribute" keyword, a storage class
26920 specifier, or (usually) a type-specifier. */
26921 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26924 tree pushed_scope
= NULL_TREE
;
26925 unsigned saved_num_template_parameter_lists
;
26927 /* Names appearing in the type-specifier should be looked up
26928 in the scope of the class. */
26929 if (current_class_type
)
26931 else if (type_decl
)
26933 type
= TREE_TYPE (type_decl
);
26934 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26936 type
= resolve_typename_type (type
,
26937 /*only_current_p=*/false);
26938 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26940 cp_parser_abort_tentative_parse (parser
);
26944 pushed_scope
= push_scope (type
);
26947 /* Inside the constructor parameter list, surrounding
26948 template-parameter-lists do not apply. */
26949 saved_num_template_parameter_lists
26950 = parser
->num_template_parameter_lists
;
26951 parser
->num_template_parameter_lists
= 0;
26953 /* Look for the type-specifier. */
26954 cp_parser_type_specifier (parser
,
26955 CP_PARSER_FLAGS_NONE
,
26956 /*decl_specs=*/NULL
,
26957 /*is_declarator=*/true,
26958 /*declares_class_or_enum=*/NULL
,
26959 /*is_cv_qualifier=*/NULL
);
26961 parser
->num_template_parameter_lists
26962 = saved_num_template_parameter_lists
;
26964 /* Leave the scope of the class. */
26966 pop_scope (pushed_scope
);
26968 constructor_p
= !cp_parser_error_occurred (parser
);
26972 /* We did not really want to consume any tokens. */
26973 cp_parser_abort_tentative_parse (parser
);
26975 return constructor_p
;
26978 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26979 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26980 they must be performed once we are in the scope of the function.
26982 Returns the function defined. */
26985 cp_parser_function_definition_from_specifiers_and_declarator
26986 (cp_parser
* parser
,
26987 cp_decl_specifier_seq
*decl_specifiers
,
26989 const cp_declarator
*declarator
)
26994 /* Begin the function-definition. */
26995 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26997 /* The things we're about to see are not directly qualified by any
26998 template headers we've seen thus far. */
26999 reset_specialization ();
27001 /* If there were names looked up in the decl-specifier-seq that we
27002 did not check, check them now. We must wait until we are in the
27003 scope of the function to perform the checks, since the function
27004 might be a friend. */
27005 perform_deferred_access_checks (tf_warning_or_error
);
27009 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27010 parser
->omp_declare_simd
= NULL
;
27011 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27012 parser
->oacc_routine
= NULL
;
27017 /* Skip the entire function. */
27018 cp_parser_skip_to_end_of_block_or_statement (parser
);
27019 fn
= error_mark_node
;
27021 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27023 /* Seen already, skip it. An error message has already been output. */
27024 cp_parser_skip_to_end_of_block_or_statement (parser
);
27025 fn
= current_function_decl
;
27026 current_function_decl
= NULL_TREE
;
27027 /* If this is a function from a class, pop the nested class. */
27028 if (current_class_name
)
27029 pop_nested_class ();
27034 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27035 tv
= TV_PARSE_INLINE
;
27037 tv
= TV_PARSE_FUNC
;
27039 fn
= cp_parser_function_definition_after_declarator (parser
,
27040 /*inline_p=*/false);
27047 /* Parse the part of a function-definition that follows the
27048 declarator. INLINE_P is TRUE iff this function is an inline
27049 function defined within a class-specifier.
27051 Returns the function defined. */
27054 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27058 bool saved_in_unbraced_linkage_specification_p
;
27059 bool saved_in_function_body
;
27060 unsigned saved_num_template_parameter_lists
;
27062 bool fully_implicit_function_template_p
27063 = parser
->fully_implicit_function_template_p
;
27064 parser
->fully_implicit_function_template_p
= false;
27065 tree implicit_template_parms
27066 = parser
->implicit_template_parms
;
27067 parser
->implicit_template_parms
= 0;
27068 cp_binding_level
* implicit_template_scope
27069 = parser
->implicit_template_scope
;
27070 parser
->implicit_template_scope
= 0;
27072 saved_in_function_body
= parser
->in_function_body
;
27073 parser
->in_function_body
= true;
27074 /* If the next token is `return', then the code may be trying to
27075 make use of the "named return value" extension that G++ used to
27077 token
= cp_lexer_peek_token (parser
->lexer
);
27078 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27080 /* Consume the `return' keyword. */
27081 cp_lexer_consume_token (parser
->lexer
);
27082 /* Look for the identifier that indicates what value is to be
27084 cp_parser_identifier (parser
);
27085 /* Issue an error message. */
27086 error_at (token
->location
,
27087 "named return values are no longer supported");
27088 /* Skip tokens until we reach the start of the function body. */
27091 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27092 if (token
->type
== CPP_OPEN_BRACE
27093 || token
->type
== CPP_EOF
27094 || token
->type
== CPP_PRAGMA_EOL
)
27096 cp_lexer_consume_token (parser
->lexer
);
27099 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27100 anything declared inside `f'. */
27101 saved_in_unbraced_linkage_specification_p
27102 = parser
->in_unbraced_linkage_specification_p
;
27103 parser
->in_unbraced_linkage_specification_p
= false;
27104 /* Inside the function, surrounding template-parameter-lists do not
27106 saved_num_template_parameter_lists
27107 = parser
->num_template_parameter_lists
;
27108 parser
->num_template_parameter_lists
= 0;
27110 /* If the next token is `try', `__transaction_atomic', or
27111 `__transaction_relaxed`, then we are looking at either function-try-block
27112 or function-transaction-block. Note that all of these include the
27114 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27115 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27116 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27117 RID_TRANSACTION_RELAXED
))
27118 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27119 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27120 cp_parser_function_try_block (parser
);
27122 cp_parser_ctor_initializer_opt_and_function_body
27123 (parser
, /*in_function_try_block=*/false);
27125 /* Finish the function. */
27126 fn
= finish_function (inline_p
);
27127 /* Generate code for it, if necessary. */
27128 expand_or_defer_fn (fn
);
27129 /* Restore the saved values. */
27130 parser
->in_unbraced_linkage_specification_p
27131 = saved_in_unbraced_linkage_specification_p
;
27132 parser
->num_template_parameter_lists
27133 = saved_num_template_parameter_lists
;
27134 parser
->in_function_body
= saved_in_function_body
;
27136 parser
->fully_implicit_function_template_p
27137 = fully_implicit_function_template_p
;
27138 parser
->implicit_template_parms
27139 = implicit_template_parms
;
27140 parser
->implicit_template_scope
27141 = implicit_template_scope
;
27143 if (parser
->fully_implicit_function_template_p
)
27144 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27149 /* Parse a template-declaration body (following argument list). */
27152 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27153 tree parameter_list
,
27156 tree decl
= NULL_TREE
;
27157 bool friend_p
= false;
27159 /* We just processed one more parameter list. */
27160 ++parser
->num_template_parameter_lists
;
27162 /* Get the deferred access checks from the parameter list. These
27163 will be checked once we know what is being declared, as for a
27164 member template the checks must be performed in the scope of the
27165 class containing the member. */
27166 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27168 /* Tentatively parse for a new template parameter list, which can either be
27169 the template keyword or a template introduction. */
27170 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27172 else if (cxx_dialect
>= cxx11
27173 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27174 decl
= cp_parser_alias_declaration (parser
);
27177 /* There are no access checks when parsing a template, as we do not
27178 know if a specialization will be a friend. */
27179 push_deferring_access_checks (dk_no_check
);
27180 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27181 decl
= cp_parser_single_declaration (parser
,
27184 /*explicit_specialization_p=*/false,
27186 pop_deferring_access_checks ();
27188 /* If this is a member template declaration, let the front
27190 if (member_p
&& !friend_p
&& decl
)
27192 if (TREE_CODE (decl
) == TYPE_DECL
)
27193 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27195 decl
= finish_member_template_decl (decl
);
27197 else if (friend_p
&& decl
27198 && DECL_DECLARES_TYPE_P (decl
))
27199 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27200 /*complain=*/true);
27202 /* We are done with the current parameter list. */
27203 --parser
->num_template_parameter_lists
;
27205 pop_deferring_access_checks ();
27208 finish_template_decl (parameter_list
);
27210 /* Check the template arguments for a literal operator template. */
27212 && DECL_DECLARES_FUNCTION_P (decl
)
27213 && UDLIT_OPER_P (DECL_NAME (decl
)))
27216 if (parameter_list
== NULL_TREE
)
27220 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27221 if (num_parms
== 1)
27223 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27224 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27225 if (TREE_TYPE (parm
) != char_type_node
27226 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27229 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27231 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27232 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27233 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27234 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27235 if (parm
== error_mark_node
27236 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27237 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27245 if (cxx_dialect
>= cxx14
)
27246 error ("literal operator template %qD has invalid parameter list."
27247 " Expected non-type template argument pack <char...>"
27248 " or <typename CharT, CharT...>",
27251 error ("literal operator template %qD has invalid parameter list."
27252 " Expected non-type template argument pack <char...>",
27257 /* Register member declarations. */
27258 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27259 finish_member_declaration (decl
);
27260 /* If DECL is a function template, we must return to parse it later.
27261 (Even though there is no definition, there might be default
27262 arguments that need handling.) */
27263 if (member_p
&& decl
27264 && DECL_DECLARES_FUNCTION_P (decl
))
27265 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27268 /* Parse a template introduction header for a template-declaration. Returns
27269 false if tentative parse fails. */
27272 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27274 cp_parser_parse_tentatively (parser
);
27276 tree saved_scope
= parser
->scope
;
27277 tree saved_object_scope
= parser
->object_scope
;
27278 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27280 /* Look for the optional `::' operator. */
27281 cp_parser_global_scope_opt (parser
,
27282 /*current_scope_valid_p=*/false);
27283 /* Look for the nested-name-specifier. */
27284 cp_parser_nested_name_specifier_opt (parser
,
27285 /*typename_keyword_p=*/false,
27286 /*check_dependency_p=*/true,
27288 /*is_declaration=*/false);
27290 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27291 tree concept_name
= cp_parser_identifier (parser
);
27293 /* Look up the concept for which we will be matching
27294 template parameters. */
27295 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27297 parser
->scope
= saved_scope
;
27298 parser
->object_scope
= saved_object_scope
;
27299 parser
->qualifying_scope
= saved_qualifying_scope
;
27301 if (concept_name
== error_mark_node
)
27302 cp_parser_simulate_error (parser
);
27304 /* Look for opening brace for introduction. */
27305 matching_braces braces
;
27306 braces
.require_open (parser
);
27308 if (!cp_parser_parse_definitely (parser
))
27311 push_deferring_access_checks (dk_deferred
);
27313 /* Build vector of placeholder parameters and grab
27314 matching identifiers. */
27315 tree introduction_list
= cp_parser_introduction_list (parser
);
27317 /* Look for closing brace for introduction. */
27318 if (!braces
.require_close (parser
))
27321 /* The introduction-list shall not be empty. */
27322 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27325 /* In cp_parser_introduction_list we have already issued an error. */
27329 if (tmpl_decl
== error_mark_node
)
27331 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27336 /* Build and associate the constraint. */
27337 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27338 if (parms
&& parms
!= error_mark_node
)
27340 cp_parser_template_declaration_after_parameters (parser
, parms
,
27345 error_at (token
->location
, "no matching concept for template-introduction");
27349 /* Parse a normal template-declaration following the template keyword. */
27352 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27354 tree parameter_list
;
27355 bool need_lang_pop
;
27356 location_t location
= input_location
;
27358 /* Look for the `<' token. */
27359 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27361 if (at_class_scope_p () && current_function_decl
)
27363 /* 14.5.2.2 [temp.mem]
27365 A local class shall not have member templates. */
27366 error_at (location
,
27367 "invalid declaration of member template in local class");
27368 cp_parser_skip_to_end_of_block_or_statement (parser
);
27373 A template ... shall not have C linkage. */
27374 if (current_lang_name
== lang_name_c
)
27376 error_at (location
, "template with C linkage");
27377 maybe_show_extern_c_location ();
27378 /* Give it C++ linkage to avoid confusing other parts of the
27380 push_lang_context (lang_name_cplusplus
);
27381 need_lang_pop
= true;
27384 need_lang_pop
= false;
27386 /* We cannot perform access checks on the template parameter
27387 declarations until we know what is being declared, just as we
27388 cannot check the decl-specifier list. */
27389 push_deferring_access_checks (dk_deferred
);
27391 /* If the next token is `>', then we have an invalid
27392 specialization. Rather than complain about an invalid template
27393 parameter, issue an error message here. */
27394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27396 cp_parser_error (parser
, "invalid explicit specialization");
27397 begin_specialization ();
27398 parameter_list
= NULL_TREE
;
27402 /* Parse the template parameters. */
27403 parameter_list
= cp_parser_template_parameter_list (parser
);
27406 /* Look for the `>'. */
27407 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27409 /* Manage template requirements */
27412 tree reqs
= get_shorthand_constraints (current_template_parms
);
27413 if (tree r
= cp_parser_requires_clause_opt (parser
))
27414 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27415 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27418 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27421 /* For the erroneous case of a template with C linkage, we pushed an
27422 implicit C++ linkage scope; exit that scope now. */
27424 pop_lang_context ();
27427 /* Parse a template-declaration, assuming that the `export' (and
27428 `extern') keywords, if present, has already been scanned. MEMBER_P
27429 is as for cp_parser_template_declaration. */
27432 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27434 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27436 cp_lexer_consume_token (parser
->lexer
);
27437 cp_parser_explicit_template_declaration (parser
, member_p
);
27440 else if (flag_concepts
)
27441 return cp_parser_template_introduction (parser
, member_p
);
27446 /* Perform the deferred access checks from a template-parameter-list.
27447 CHECKS is a TREE_LIST of access checks, as returned by
27448 get_deferred_access_checks. */
27451 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27453 ++processing_template_parmlist
;
27454 perform_access_checks (checks
, tf_warning_or_error
);
27455 --processing_template_parmlist
;
27458 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27459 `function-definition' sequence that follows a template header.
27460 If MEMBER_P is true, this declaration appears in a class scope.
27462 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27463 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27466 cp_parser_single_declaration (cp_parser
* parser
,
27467 vec
<deferred_access_check
, va_gc
> *checks
,
27469 bool explicit_specialization_p
,
27472 int declares_class_or_enum
;
27473 tree decl
= NULL_TREE
;
27474 cp_decl_specifier_seq decl_specifiers
;
27475 bool function_definition_p
= false;
27476 cp_token
*decl_spec_token_start
;
27478 /* This function is only used when processing a template
27480 gcc_assert (innermost_scope_kind () == sk_template_parms
27481 || innermost_scope_kind () == sk_template_spec
);
27483 /* Defer access checks until we know what is being declared. */
27484 push_deferring_access_checks (dk_deferred
);
27486 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27488 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27489 cp_parser_decl_specifier_seq (parser
,
27490 CP_PARSER_FLAGS_OPTIONAL
,
27492 &declares_class_or_enum
);
27494 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27496 /* There are no template typedefs. */
27497 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27499 error_at (decl_spec_token_start
->location
,
27500 "template declaration of %<typedef%>");
27501 decl
= error_mark_node
;
27504 /* Gather up the access checks that occurred the
27505 decl-specifier-seq. */
27506 stop_deferring_access_checks ();
27508 /* Check for the declaration of a template class. */
27509 if (declares_class_or_enum
)
27511 if (cp_parser_declares_only_class_p (parser
)
27512 || (declares_class_or_enum
& 2))
27514 // If this is a declaration, but not a definition, associate
27515 // any constraints with the type declaration. Constraints
27516 // are associated with definitions in cp_parser_class_specifier.
27517 if (declares_class_or_enum
== 1)
27518 associate_classtype_constraints (decl_specifiers
.type
);
27520 decl
= shadow_tag (&decl_specifiers
);
27525 friend template <typename T> struct A<T>::B;
27528 A<T>::B will be represented by a TYPENAME_TYPE, and
27529 therefore not recognized by shadow_tag. */
27530 if (friend_p
&& *friend_p
27532 && decl_specifiers
.type
27533 && TYPE_P (decl_specifiers
.type
))
27534 decl
= decl_specifiers
.type
;
27536 if (decl
&& decl
!= error_mark_node
)
27537 decl
= TYPE_NAME (decl
);
27539 decl
= error_mark_node
;
27541 /* Perform access checks for template parameters. */
27542 cp_parser_perform_template_parameter_access_checks (checks
);
27544 /* Give a helpful diagnostic for
27545 template <class T> struct A { } a;
27546 if we aren't already recovering from an error. */
27547 if (!cp_parser_declares_only_class_p (parser
)
27550 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27551 "a class template declaration must not declare "
27553 cp_parser_skip_to_end_of_block_or_statement (parser
);
27559 /* Complain about missing 'typename' or other invalid type names. */
27560 if (!decl_specifiers
.any_type_specifiers_p
27561 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27563 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27564 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27565 the rest of this declaration. */
27566 decl
= error_mark_node
;
27570 /* If it's not a template class, try for a template function. If
27571 the next token is a `;', then this declaration does not declare
27572 anything. But, if there were errors in the decl-specifiers, then
27573 the error might well have come from an attempted class-specifier.
27574 In that case, there's no need to warn about a missing declarator. */
27576 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27577 || decl_specifiers
.type
!= error_mark_node
))
27579 decl
= cp_parser_init_declarator (parser
,
27582 /*function_definition_allowed_p=*/true,
27584 declares_class_or_enum
,
27585 &function_definition_p
,
27588 /* 7.1.1-1 [dcl.stc]
27590 A storage-class-specifier shall not be specified in an explicit
27591 specialization... */
27593 && explicit_specialization_p
27594 && decl_specifiers
.storage_class
!= sc_none
)
27596 error_at (decl_spec_token_start
->location
,
27597 "explicit template specialization cannot have a storage class");
27598 decl
= error_mark_node
;
27601 if (decl
&& VAR_P (decl
))
27602 check_template_variable (decl
);
27605 /* Look for a trailing `;' after the declaration. */
27606 if (!function_definition_p
27607 && (decl
== error_mark_node
27608 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27609 cp_parser_skip_to_end_of_block_or_statement (parser
);
27612 pop_deferring_access_checks ();
27614 /* Clear any current qualification; whatever comes next is the start
27615 of something new. */
27616 parser
->scope
= NULL_TREE
;
27617 parser
->qualifying_scope
= NULL_TREE
;
27618 parser
->object_scope
= NULL_TREE
;
27623 /* Parse a cast-expression that is not the operand of a unary "&". */
27626 cp_parser_simple_cast_expression (cp_parser
*parser
)
27628 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27629 /*cast_p=*/false, /*decltype*/false, NULL
);
27632 /* Parse a functional cast to TYPE. Returns an expression
27633 representing the cast. */
27636 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27638 vec
<tree
, va_gc
> *vec
;
27639 tree expression_list
;
27643 location_t start_loc
= input_location
;
27646 type
= error_mark_node
;
27648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27650 cp_lexer_set_source_position (parser
->lexer
);
27651 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27652 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27653 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27654 if (TREE_CODE (type
) == TYPE_DECL
)
27655 type
= TREE_TYPE (type
);
27657 cast
= finish_compound_literal (type
, expression_list
,
27658 tf_warning_or_error
, fcl_functional
);
27659 /* Create a location of the form:
27662 with caret == start at the start of the type name,
27663 finishing at the closing brace. */
27664 location_t finish_loc
27665 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27666 location_t combined_loc
= make_location (start_loc
, start_loc
,
27668 cast
.set_location (combined_loc
);
27673 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27675 /*allow_expansion_p=*/true,
27676 /*non_constant_p=*/NULL
);
27678 expression_list
= error_mark_node
;
27681 expression_list
= build_tree_list_vec (vec
);
27682 release_tree_vector (vec
);
27685 cast
= build_functional_cast (type
, expression_list
,
27686 tf_warning_or_error
);
27687 /* [expr.const]/1: In an integral constant expression "only type
27688 conversions to integral or enumeration type can be used". */
27689 if (TREE_CODE (type
) == TYPE_DECL
)
27690 type
= TREE_TYPE (type
);
27691 if (cast
!= error_mark_node
27692 && !cast_valid_in_integral_constant_expression_p (type
)
27693 && cp_parser_non_integral_constant_expression (parser
,
27695 return error_mark_node
;
27697 /* Create a location of the form:
27700 with caret == start at the start of the type name,
27701 finishing at the closing paren. */
27702 location_t finish_loc
27703 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27704 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27705 cast
.set_location (combined_loc
);
27709 /* Save the tokens that make up the body of a member function defined
27710 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27711 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27712 specifiers applied to the declaration. Returns the FUNCTION_DECL
27713 for the member function. */
27716 cp_parser_save_member_function_body (cp_parser
* parser
,
27717 cp_decl_specifier_seq
*decl_specifiers
,
27718 cp_declarator
*declarator
,
27724 bool function_try_block
= false;
27726 /* Create the FUNCTION_DECL. */
27727 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27728 cp_finalize_omp_declare_simd (parser
, fn
);
27729 cp_finalize_oacc_routine (parser
, fn
, true);
27730 /* If something went badly wrong, bail out now. */
27731 if (fn
== error_mark_node
)
27733 /* If there's a function-body, skip it. */
27734 if (cp_parser_token_starts_function_definition_p
27735 (cp_lexer_peek_token (parser
->lexer
)))
27736 cp_parser_skip_to_end_of_block_or_statement (parser
);
27737 return error_mark_node
;
27740 /* Remember it, if there default args to post process. */
27741 cp_parser_save_default_args (parser
, fn
);
27743 /* Save away the tokens that make up the body of the
27745 first
= parser
->lexer
->next_token
;
27747 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27748 cp_lexer_consume_token (parser
->lexer
);
27749 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27750 RID_TRANSACTION_ATOMIC
))
27752 cp_lexer_consume_token (parser
->lexer
);
27753 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27755 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27756 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27757 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27758 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27759 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27761 cp_lexer_consume_token (parser
->lexer
);
27762 cp_lexer_consume_token (parser
->lexer
);
27763 cp_lexer_consume_token (parser
->lexer
);
27764 cp_lexer_consume_token (parser
->lexer
);
27765 cp_lexer_consume_token (parser
->lexer
);
27768 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27769 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27771 cp_lexer_consume_token (parser
->lexer
);
27772 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27777 /* Handle function try blocks. */
27778 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27780 cp_lexer_consume_token (parser
->lexer
);
27781 function_try_block
= true;
27783 /* We can have braced-init-list mem-initializers before the fn body. */
27784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27786 cp_lexer_consume_token (parser
->lexer
);
27787 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27789 /* cache_group will stop after an un-nested { } pair, too. */
27790 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27793 /* variadic mem-inits have ... after the ')'. */
27794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27795 cp_lexer_consume_token (parser
->lexer
);
27798 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27799 /* Handle function try blocks. */
27800 if (function_try_block
)
27801 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27802 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27803 last
= parser
->lexer
->next_token
;
27805 /* Save away the inline definition; we will process it when the
27806 class is complete. */
27807 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27808 DECL_PENDING_INLINE_P (fn
) = 1;
27810 /* We need to know that this was defined in the class, so that
27811 friend templates are handled correctly. */
27812 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27814 /* Add FN to the queue of functions to be parsed later. */
27815 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27820 /* Save the tokens that make up the in-class initializer for a non-static
27821 data member. Returns a DEFAULT_ARG. */
27824 cp_parser_save_nsdmi (cp_parser
* parser
)
27826 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27829 /* Parse a template-argument-list, as well as the trailing ">" (but
27830 not the opening "<"). See cp_parser_template_argument_list for the
27834 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27838 tree saved_qualifying_scope
;
27839 tree saved_object_scope
;
27840 bool saved_greater_than_is_operator_p
;
27841 int saved_unevaluated_operand
;
27842 int saved_inhibit_evaluation_warnings
;
27846 When parsing a template-id, the first non-nested `>' is taken as
27847 the end of the template-argument-list rather than a greater-than
27849 saved_greater_than_is_operator_p
27850 = parser
->greater_than_is_operator_p
;
27851 parser
->greater_than_is_operator_p
= false;
27852 /* Parsing the argument list may modify SCOPE, so we save it
27854 saved_scope
= parser
->scope
;
27855 saved_qualifying_scope
= parser
->qualifying_scope
;
27856 saved_object_scope
= parser
->object_scope
;
27857 /* We need to evaluate the template arguments, even though this
27858 template-id may be nested within a "sizeof". */
27859 saved_unevaluated_operand
= cp_unevaluated_operand
;
27860 cp_unevaluated_operand
= 0;
27861 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27862 c_inhibit_evaluation_warnings
= 0;
27863 /* Parse the template-argument-list itself. */
27864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27865 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27866 arguments
= NULL_TREE
;
27868 arguments
= cp_parser_template_argument_list (parser
);
27869 /* Look for the `>' that ends the template-argument-list. If we find
27870 a '>>' instead, it's probably just a typo. */
27871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27873 if (cxx_dialect
!= cxx98
)
27875 /* In C++0x, a `>>' in a template argument list or cast
27876 expression is considered to be two separate `>'
27877 tokens. So, change the current token to a `>', but don't
27878 consume it: it will be consumed later when the outer
27879 template argument list (or cast expression) is parsed.
27880 Note that this replacement of `>' for `>>' is necessary
27881 even if we are parsing tentatively: in the tentative
27882 case, after calling
27883 cp_parser_enclosed_template_argument_list we will always
27884 throw away all of the template arguments and the first
27885 closing `>', either because the template argument list
27886 was erroneous or because we are replacing those tokens
27887 with a CPP_TEMPLATE_ID token. The second `>' (which will
27888 not have been thrown away) is needed either to close an
27889 outer template argument list or to complete a new-style
27891 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27892 token
->type
= CPP_GREATER
;
27894 else if (!saved_greater_than_is_operator_p
)
27896 /* If we're in a nested template argument list, the '>>' has
27897 to be a typo for '> >'. We emit the error message, but we
27898 continue parsing and we push a '>' as next token, so that
27899 the argument list will be parsed correctly. Note that the
27900 global source location is still on the token before the
27901 '>>', so we need to say explicitly where we want it. */
27902 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27903 gcc_rich_location
richloc (token
->location
);
27904 richloc
.add_fixit_replace ("> >");
27905 error_at (&richloc
, "%<>>%> should be %<> >%> "
27906 "within a nested template argument list");
27908 token
->type
= CPP_GREATER
;
27912 /* If this is not a nested template argument list, the '>>'
27913 is a typo for '>'. Emit an error message and continue.
27914 Same deal about the token location, but here we can get it
27915 right by consuming the '>>' before issuing the diagnostic. */
27916 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27917 error_at (token
->location
,
27918 "spurious %<>>%>, use %<>%> to terminate "
27919 "a template argument list");
27923 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27924 /* The `>' token might be a greater-than operator again now. */
27925 parser
->greater_than_is_operator_p
27926 = saved_greater_than_is_operator_p
;
27927 /* Restore the SAVED_SCOPE. */
27928 parser
->scope
= saved_scope
;
27929 parser
->qualifying_scope
= saved_qualifying_scope
;
27930 parser
->object_scope
= saved_object_scope
;
27931 cp_unevaluated_operand
= saved_unevaluated_operand
;
27932 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27937 /* MEMBER_FUNCTION is a member function, or a friend. If default
27938 arguments, or the body of the function have not yet been parsed,
27942 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27944 timevar_push (TV_PARSE_INMETH
);
27945 /* If this member is a template, get the underlying
27947 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27948 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27950 /* There should not be any class definitions in progress at this
27951 point; the bodies of members are only parsed outside of all class
27953 gcc_assert (parser
->num_classes_being_defined
== 0);
27954 /* While we're parsing the member functions we might encounter more
27955 classes. We want to handle them right away, but we don't want
27956 them getting mixed up with functions that are currently in the
27958 push_unparsed_function_queues (parser
);
27960 /* Make sure that any template parameters are in scope. */
27961 maybe_begin_member_template_processing (member_function
);
27963 /* If the body of the function has not yet been parsed, parse it
27965 if (DECL_PENDING_INLINE_P (member_function
))
27967 tree function_scope
;
27968 cp_token_cache
*tokens
;
27970 /* The function is no longer pending; we are processing it. */
27971 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27972 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27973 DECL_PENDING_INLINE_P (member_function
) = 0;
27975 /* If this is a local class, enter the scope of the containing
27977 function_scope
= current_function_decl
;
27978 if (function_scope
)
27979 push_function_context ();
27981 /* Push the body of the function onto the lexer stack. */
27982 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27984 /* Let the front end know that we going to be defining this
27986 start_preparsed_function (member_function
, NULL_TREE
,
27987 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27989 /* Don't do access checking if it is a templated function. */
27990 if (processing_template_decl
)
27991 push_deferring_access_checks (dk_no_check
);
27993 /* #pragma omp declare reduction needs special parsing. */
27994 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27996 parser
->lexer
->in_pragma
= true;
27997 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27998 finish_function (/*inline_p=*/true);
27999 cp_check_omp_declare_reduction (member_function
);
28002 /* Now, parse the body of the function. */
28003 cp_parser_function_definition_after_declarator (parser
,
28004 /*inline_p=*/true);
28006 if (processing_template_decl
)
28007 pop_deferring_access_checks ();
28009 /* Leave the scope of the containing function. */
28010 if (function_scope
)
28011 pop_function_context ();
28012 cp_parser_pop_lexer (parser
);
28015 /* Remove any template parameters from the symbol table. */
28016 maybe_end_member_template_processing ();
28018 /* Restore the queue. */
28019 pop_unparsed_function_queues (parser
);
28020 timevar_pop (TV_PARSE_INMETH
);
28023 /* If DECL contains any default args, remember it on the unparsed
28024 functions queue. */
28027 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28031 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28033 probe
= TREE_CHAIN (probe
))
28034 if (TREE_PURPOSE (probe
))
28036 cp_default_arg_entry entry
= {current_class_type
, decl
};
28037 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28042 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28043 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28044 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28045 from the parameter-type-list. */
28048 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28049 tree default_arg
, tree parmtype
)
28051 cp_token_cache
*tokens
;
28055 if (default_arg
== error_mark_node
)
28056 return error_mark_node
;
28058 /* Push the saved tokens for the default argument onto the parser's
28060 tokens
= DEFARG_TOKENS (default_arg
);
28061 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28063 start_lambda_scope (decl
);
28065 /* Parse the default argument. */
28066 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28067 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28068 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28070 finish_lambda_scope ();
28072 if (parsed_arg
== error_mark_node
)
28073 cp_parser_skip_to_end_of_statement (parser
);
28075 if (!processing_template_decl
)
28077 /* In a non-template class, check conversions now. In a template,
28078 we'll wait and instantiate these as needed. */
28079 if (TREE_CODE (decl
) == PARM_DECL
)
28080 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28081 tf_warning_or_error
);
28082 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28083 parsed_arg
= error_mark_node
;
28085 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28088 /* If the token stream has not been completely used up, then
28089 there was extra junk after the end of the default
28091 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28093 if (TREE_CODE (decl
) == PARM_DECL
)
28094 cp_parser_error (parser
, "expected %<,%>");
28096 cp_parser_error (parser
, "expected %<;%>");
28099 /* Revert to the main lexer. */
28100 cp_parser_pop_lexer (parser
);
28105 /* FIELD is a non-static data member with an initializer which we saved for
28106 later; parse it now. */
28109 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28113 maybe_begin_member_template_processing (field
);
28115 push_unparsed_function_queues (parser
);
28116 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28117 DECL_INITIAL (field
),
28119 pop_unparsed_function_queues (parser
);
28121 maybe_end_member_template_processing ();
28123 DECL_INITIAL (field
) = def
;
28126 /* FN is a FUNCTION_DECL which may contains a parameter with an
28127 unparsed DEFAULT_ARG. Parse the default args now. This function
28128 assumes that the current scope is the scope in which the default
28129 argument should be processed. */
28132 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28134 bool saved_local_variables_forbidden_p
;
28135 tree parm
, parmdecl
;
28137 /* While we're parsing the default args, we might (due to the
28138 statement expression extension) encounter more classes. We want
28139 to handle them right away, but we don't want them getting mixed
28140 up with default args that are currently in the queue. */
28141 push_unparsed_function_queues (parser
);
28143 /* Local variable names (and the `this' keyword) may not appear
28144 in a default argument. */
28145 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28146 parser
->local_variables_forbidden_p
= true;
28148 push_defarg_context (fn
);
28150 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28151 parmdecl
= DECL_ARGUMENTS (fn
);
28152 parm
&& parm
!= void_list_node
;
28153 parm
= TREE_CHAIN (parm
),
28154 parmdecl
= DECL_CHAIN (parmdecl
))
28156 tree default_arg
= TREE_PURPOSE (parm
);
28158 vec
<tree
, va_gc
> *insts
;
28165 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28166 /* This can happen for a friend declaration for a function
28167 already declared with default arguments. */
28171 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28173 TREE_VALUE (parm
));
28174 TREE_PURPOSE (parm
) = parsed_arg
;
28176 /* Update any instantiations we've already created. */
28177 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28178 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28179 TREE_PURPOSE (copy
) = parsed_arg
;
28182 pop_defarg_context ();
28184 /* Make sure no default arg is missing. */
28185 check_default_args (fn
);
28187 /* Restore the state of local_variables_forbidden_p. */
28188 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28190 /* Restore the queue. */
28191 pop_unparsed_function_queues (parser
);
28194 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28196 sizeof ... ( identifier )
28198 where the 'sizeof' token has already been consumed. */
28201 cp_parser_sizeof_pack (cp_parser
*parser
)
28203 /* Consume the `...'. */
28204 cp_lexer_consume_token (parser
->lexer
);
28205 maybe_warn_variadic_templates ();
28207 matching_parens parens
;
28208 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28210 parens
.consume_open (parser
);
28212 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28213 "%<sizeof...%> argument must be surrounded by parentheses");
28215 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28216 tree name
= cp_parser_identifier (parser
);
28217 if (name
== error_mark_node
)
28218 return error_mark_node
;
28219 /* The name is not qualified. */
28220 parser
->scope
= NULL_TREE
;
28221 parser
->qualifying_scope
= NULL_TREE
;
28222 parser
->object_scope
= NULL_TREE
;
28223 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28224 if (expr
== error_mark_node
)
28225 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28227 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28228 expr
= TREE_TYPE (expr
);
28229 else if (TREE_CODE (expr
) == CONST_DECL
)
28230 expr
= DECL_INITIAL (expr
);
28231 expr
= make_pack_expansion (expr
);
28232 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28235 parens
.require_close (parser
);
28240 /* Parse the operand of `sizeof' (or a similar operator). Returns
28241 either a TYPE or an expression, depending on the form of the
28242 input. The KEYWORD indicates which kind of expression we have
28246 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28248 tree expr
= NULL_TREE
;
28249 const char *saved_message
;
28251 bool saved_integral_constant_expression_p
;
28252 bool saved_non_integral_constant_expression_p
;
28254 /* If it's a `...', then we are computing the length of a parameter
28256 if (keyword
== RID_SIZEOF
28257 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28258 return cp_parser_sizeof_pack (parser
);
28260 /* Types cannot be defined in a `sizeof' expression. Save away the
28262 saved_message
= parser
->type_definition_forbidden_message
;
28263 /* And create the new one. */
28264 tmp
= concat ("types may not be defined in %<",
28265 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28266 "%> expressions", NULL
);
28267 parser
->type_definition_forbidden_message
= tmp
;
28269 /* The restrictions on constant-expressions do not apply inside
28270 sizeof expressions. */
28271 saved_integral_constant_expression_p
28272 = parser
->integral_constant_expression_p
;
28273 saved_non_integral_constant_expression_p
28274 = parser
->non_integral_constant_expression_p
;
28275 parser
->integral_constant_expression_p
= false;
28277 /* Do not actually evaluate the expression. */
28278 ++cp_unevaluated_operand
;
28279 ++c_inhibit_evaluation_warnings
;
28280 /* If it's a `(', then we might be looking at the type-id
28282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28284 tree type
= NULL_TREE
;
28286 /* We can't be sure yet whether we're looking at a type-id or an
28288 cp_parser_parse_tentatively (parser
);
28290 matching_parens parens
;
28291 parens
.consume_open (parser
);
28293 /* Note: as a GNU Extension, compound literals are considered
28294 postfix-expressions as they are in C99, so they are valid
28295 arguments to sizeof. See comment in cp_parser_cast_expression
28297 if (cp_parser_compound_literal_p (parser
))
28298 cp_parser_simulate_error (parser
);
28301 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28302 parser
->in_type_id_in_expr_p
= true;
28303 /* Look for the type-id. */
28304 type
= cp_parser_type_id (parser
);
28305 /* Look for the closing `)'. */
28306 parens
.require_close (parser
);
28307 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28310 /* If all went well, then we're done. */
28311 if (cp_parser_parse_definitely (parser
))
28315 /* If the type-id production did not work out, then we must be
28316 looking at the unary-expression production. */
28318 expr
= cp_parser_unary_expression (parser
);
28320 /* Go back to evaluating expressions. */
28321 --cp_unevaluated_operand
;
28322 --c_inhibit_evaluation_warnings
;
28324 /* Free the message we created. */
28326 /* And restore the old one. */
28327 parser
->type_definition_forbidden_message
= saved_message
;
28328 parser
->integral_constant_expression_p
28329 = saved_integral_constant_expression_p
;
28330 parser
->non_integral_constant_expression_p
28331 = saved_non_integral_constant_expression_p
;
28336 /* If the current declaration has no declarator, return true. */
28339 cp_parser_declares_only_class_p (cp_parser
*parser
)
28341 /* If the next token is a `;' or a `,' then there is no
28343 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28344 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28347 /* Update the DECL_SPECS to reflect the storage class indicated by
28351 cp_parser_set_storage_class (cp_parser
*parser
,
28352 cp_decl_specifier_seq
*decl_specs
,
28356 cp_storage_class storage_class
;
28358 if (parser
->in_unbraced_linkage_specification_p
)
28360 error_at (token
->location
, "invalid use of %qD in linkage specification",
28361 ridpointers
[keyword
]);
28364 else if (decl_specs
->storage_class
!= sc_none
)
28366 decl_specs
->conflicting_specifiers_p
= true;
28370 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28371 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28372 && decl_specs
->gnu_thread_keyword_p
)
28374 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28375 "%<__thread%> before %qD", ridpointers
[keyword
]);
28381 storage_class
= sc_auto
;
28384 storage_class
= sc_register
;
28387 storage_class
= sc_static
;
28390 storage_class
= sc_extern
;
28393 storage_class
= sc_mutable
;
28396 gcc_unreachable ();
28398 decl_specs
->storage_class
= storage_class
;
28399 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28401 /* A storage class specifier cannot be applied alongside a typedef
28402 specifier. If there is a typedef specifier present then set
28403 conflicting_specifiers_p which will trigger an error later
28404 on in grokdeclarator. */
28405 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28406 decl_specs
->conflicting_specifiers_p
= true;
28409 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28410 is true, the type is a class or enum definition. */
28413 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28416 bool type_definition_p
)
28418 decl_specs
->any_specifiers_p
= true;
28420 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28421 (with, for example, in "typedef int wchar_t;") we remember that
28422 this is what happened. In system headers, we ignore these
28423 declarations so that G++ can work with system headers that are not
28425 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28426 && !type_definition_p
28427 && (type_spec
== boolean_type_node
28428 || type_spec
== char16_type_node
28429 || type_spec
== char32_type_node
28430 || type_spec
== wchar_type_node
)
28431 && (decl_specs
->type
28432 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28433 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28434 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28435 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28437 decl_specs
->redefined_builtin_type
= type_spec
;
28438 set_and_check_decl_spec_loc (decl_specs
,
28439 ds_redefined_builtin_type_spec
,
28441 if (!decl_specs
->type
)
28443 decl_specs
->type
= type_spec
;
28444 decl_specs
->type_definition_p
= false;
28445 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28448 else if (decl_specs
->type
)
28449 decl_specs
->multiple_types_p
= true;
28452 decl_specs
->type
= type_spec
;
28453 decl_specs
->type_definition_p
= type_definition_p
;
28454 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28455 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28459 /* True iff TOKEN is the GNU keyword __thread. */
28462 token_is__thread (cp_token
*token
)
28464 gcc_assert (token
->keyword
== RID_THREAD
);
28465 return id_equal (token
->u
.value
, "__thread");
28468 /* Set the location for a declarator specifier and check if it is
28471 DECL_SPECS is the sequence of declarator specifiers onto which to
28474 DS is the single declarator specifier to set which location is to
28475 be set onto the existing sequence of declarators.
28477 LOCATION is the location for the declarator specifier to
28481 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28482 cp_decl_spec ds
, cp_token
*token
)
28484 gcc_assert (ds
< ds_last
);
28486 if (decl_specs
== NULL
)
28489 source_location location
= token
->location
;
28491 if (decl_specs
->locations
[ds
] == 0)
28493 decl_specs
->locations
[ds
] = location
;
28494 if (ds
== ds_thread
)
28495 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28501 if (decl_specs
->locations
[ds_long_long
] != 0)
28502 error_at (location
,
28503 "%<long long long%> is too long for GCC");
28506 decl_specs
->locations
[ds_long_long
] = location
;
28507 pedwarn_cxx98 (location
,
28509 "ISO C++ 1998 does not support %<long long%>");
28512 else if (ds
== ds_thread
)
28514 bool gnu
= token_is__thread (token
);
28515 gcc_rich_location
richloc (location
);
28516 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28518 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28519 error_at (&richloc
,
28520 "both %<__thread%> and %<thread_local%> specified");
28524 richloc
.add_fixit_remove ();
28525 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28530 static const char *const decl_spec_names
[] = {
28547 gcc_rich_location
richloc (location
);
28548 richloc
.add_fixit_remove ();
28549 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28554 /* Return true iff the declarator specifier DS is present in the
28555 sequence of declarator specifiers DECL_SPECS. */
28558 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28561 gcc_assert (ds
< ds_last
);
28563 if (decl_specs
== NULL
)
28566 return decl_specs
->locations
[ds
] != 0;
28569 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28570 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28573 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28575 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28578 /* Issue an error message indicating that TOKEN_DESC was expected.
28579 If KEYWORD is true, it indicated this function is called by
28580 cp_parser_require_keword and the required token can only be
28581 a indicated keyword.
28583 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28584 within any error as the location of an "opening" token matching
28585 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28586 RT_CLOSE_PAREN). */
28589 cp_parser_required_error (cp_parser
*parser
,
28590 required_token token_desc
,
28592 location_t matching_location
)
28594 if (cp_parser_simulate_error (parser
))
28597 const char *gmsgid
= NULL
;
28598 switch (token_desc
)
28601 gmsgid
= G_("expected %<new%>");
28604 gmsgid
= G_("expected %<delete%>");
28607 gmsgid
= G_("expected %<return%>");
28610 gmsgid
= G_("expected %<while%>");
28613 gmsgid
= G_("expected %<extern%>");
28615 case RT_STATIC_ASSERT
:
28616 gmsgid
= G_("expected %<static_assert%>");
28619 gmsgid
= G_("expected %<decltype%>");
28622 gmsgid
= G_("expected %<operator%>");
28625 gmsgid
= G_("expected %<class%>");
28628 gmsgid
= G_("expected %<template%>");
28631 gmsgid
= G_("expected %<namespace%>");
28634 gmsgid
= G_("expected %<using%>");
28637 gmsgid
= G_("expected %<asm%>");
28640 gmsgid
= G_("expected %<try%>");
28643 gmsgid
= G_("expected %<catch%>");
28646 gmsgid
= G_("expected %<throw%>");
28649 gmsgid
= G_("expected %<__label__%>");
28652 gmsgid
= G_("expected %<@try%>");
28654 case RT_AT_SYNCHRONIZED
:
28655 gmsgid
= G_("expected %<@synchronized%>");
28658 gmsgid
= G_("expected %<@throw%>");
28660 case RT_TRANSACTION_ATOMIC
:
28661 gmsgid
= G_("expected %<__transaction_atomic%>");
28663 case RT_TRANSACTION_RELAXED
:
28664 gmsgid
= G_("expected %<__transaction_relaxed%>");
28670 if (!gmsgid
&& !keyword
)
28672 switch (token_desc
)
28675 gmsgid
= G_("expected %<;%>");
28677 case RT_OPEN_PAREN
:
28678 gmsgid
= G_("expected %<(%>");
28680 case RT_CLOSE_BRACE
:
28681 gmsgid
= G_("expected %<}%>");
28683 case RT_OPEN_BRACE
:
28684 gmsgid
= G_("expected %<{%>");
28686 case RT_CLOSE_SQUARE
:
28687 gmsgid
= G_("expected %<]%>");
28689 case RT_OPEN_SQUARE
:
28690 gmsgid
= G_("expected %<[%>");
28693 gmsgid
= G_("expected %<,%>");
28696 gmsgid
= G_("expected %<::%>");
28699 gmsgid
= G_("expected %<<%>");
28702 gmsgid
= G_("expected %<>%>");
28705 gmsgid
= G_("expected %<=%>");
28708 gmsgid
= G_("expected %<...%>");
28711 gmsgid
= G_("expected %<*%>");
28714 gmsgid
= G_("expected %<~%>");
28717 gmsgid
= G_("expected %<:%>");
28719 case RT_COLON_SCOPE
:
28720 gmsgid
= G_("expected %<:%> or %<::%>");
28722 case RT_CLOSE_PAREN
:
28723 gmsgid
= G_("expected %<)%>");
28725 case RT_COMMA_CLOSE_PAREN
:
28726 gmsgid
= G_("expected %<,%> or %<)%>");
28728 case RT_PRAGMA_EOL
:
28729 gmsgid
= G_("expected end of line");
28732 gmsgid
= G_("expected identifier");
28735 gmsgid
= G_("expected selection-statement");
28738 gmsgid
= G_("expected iteration-statement");
28741 gmsgid
= G_("expected jump-statement");
28744 gmsgid
= G_("expected class-key");
28746 case RT_CLASS_TYPENAME_TEMPLATE
:
28747 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28750 gcc_unreachable ();
28755 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28759 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28760 issue an error message indicating that TOKEN_DESC was expected.
28762 Returns the token consumed, if the token had the appropriate type.
28763 Otherwise, returns NULL.
28765 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28766 within any error as the location of an "opening" token matching
28767 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28768 RT_CLOSE_PAREN). */
28771 cp_parser_require (cp_parser
* parser
,
28772 enum cpp_ttype type
,
28773 required_token token_desc
,
28774 location_t matching_location
)
28776 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28777 return cp_lexer_consume_token (parser
->lexer
);
28780 /* Output the MESSAGE -- unless we're parsing tentatively. */
28781 if (!cp_parser_simulate_error (parser
))
28782 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28783 matching_location
);
28788 /* An error message is produced if the next token is not '>'.
28789 All further tokens are skipped until the desired token is
28790 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28793 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28795 /* Current level of '< ... >'. */
28796 unsigned level
= 0;
28797 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28798 unsigned nesting_depth
= 0;
28800 /* Are we ready, yet? If not, issue error message. */
28801 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28804 /* Skip tokens until the desired token is found. */
28807 /* Peek at the next token. */
28808 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28811 if (!nesting_depth
)
28816 if (cxx_dialect
== cxx98
)
28817 /* C++0x views the `>>' operator as two `>' tokens, but
28820 else if (!nesting_depth
&& level
-- == 0)
28822 /* We've hit a `>>' where the first `>' closes the
28823 template argument list, and the second `>' is
28824 spurious. Just consume the `>>' and stop; we've
28825 already produced at least one error. */
28826 cp_lexer_consume_token (parser
->lexer
);
28829 /* Fall through for C++0x, so we handle the second `>' in
28831 gcc_fallthrough ();
28834 if (!nesting_depth
&& level
-- == 0)
28836 /* We've reached the token we want, consume it and stop. */
28837 cp_lexer_consume_token (parser
->lexer
);
28842 case CPP_OPEN_PAREN
:
28843 case CPP_OPEN_SQUARE
:
28847 case CPP_CLOSE_PAREN
:
28848 case CPP_CLOSE_SQUARE
:
28849 if (nesting_depth
-- == 0)
28854 case CPP_PRAGMA_EOL
:
28855 case CPP_SEMICOLON
:
28856 case CPP_OPEN_BRACE
:
28857 case CPP_CLOSE_BRACE
:
28858 /* The '>' was probably forgotten, don't look further. */
28865 /* Consume this token. */
28866 cp_lexer_consume_token (parser
->lexer
);
28870 /* If the next token is the indicated keyword, consume it. Otherwise,
28871 issue an error message indicating that TOKEN_DESC was expected.
28873 Returns the token consumed, if the token had the appropriate type.
28874 Otherwise, returns NULL. */
28877 cp_parser_require_keyword (cp_parser
* parser
,
28879 required_token token_desc
)
28881 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28883 if (token
&& token
->keyword
!= keyword
)
28885 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28893 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28894 function-definition. */
28897 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28899 return (/* An ordinary function-body begins with an `{'. */
28900 token
->type
== CPP_OPEN_BRACE
28901 /* A ctor-initializer begins with a `:'. */
28902 || token
->type
== CPP_COLON
28903 /* A function-try-block begins with `try'. */
28904 || token
->keyword
== RID_TRY
28905 /* A function-transaction-block begins with `__transaction_atomic'
28906 or `__transaction_relaxed'. */
28907 || token
->keyword
== RID_TRANSACTION_ATOMIC
28908 || token
->keyword
== RID_TRANSACTION_RELAXED
28909 /* The named return value extension begins with `return'. */
28910 || token
->keyword
== RID_RETURN
);
28913 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28917 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28921 token
= cp_lexer_peek_token (parser
->lexer
);
28922 return (token
->type
== CPP_OPEN_BRACE
28923 || (token
->type
== CPP_COLON
28924 && !parser
->colon_doesnt_start_class_def_p
));
28927 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28928 C++0x) ending a template-argument. */
28931 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28935 token
= cp_lexer_peek_token (parser
->lexer
);
28936 return (token
->type
== CPP_COMMA
28937 || token
->type
== CPP_GREATER
28938 || token
->type
== CPP_ELLIPSIS
28939 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28942 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28943 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28946 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28951 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28952 if (token
->type
== CPP_LESS
)
28954 /* Check for the sequence `<::' in the original code. It would be lexed as
28955 `[:', where `[' is a digraph, and there is no whitespace before
28957 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28960 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28961 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28967 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28968 or none_type otherwise. */
28970 static enum tag_types
28971 cp_parser_token_is_class_key (cp_token
* token
)
28973 switch (token
->keyword
)
28978 return record_type
;
28987 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28988 or none_type otherwise or if the token is null. */
28990 static enum tag_types
28991 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28996 switch (token
->keyword
)
29001 return typename_type
;
29008 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29011 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29013 if (type
== error_mark_node
)
29015 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29017 if (permerror (input_location
, "%qs tag used in naming %q#T",
29018 class_key
== union_type
? "union"
29019 : class_key
== record_type
? "struct" : "class",
29021 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29022 "%q#T was previously declared here", type
);
29026 /* Issue an error message if DECL is redeclared with different
29027 access than its original declaration [class.access.spec/3].
29028 This applies to nested classes, nested class templates and
29029 enumerations [class.mem/1]. */
29032 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29035 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29036 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29039 if ((TREE_PRIVATE (decl
)
29040 != (current_access_specifier
== access_private_node
))
29041 || (TREE_PROTECTED (decl
)
29042 != (current_access_specifier
== access_protected_node
)))
29043 error_at (location
, "%qD redeclared with different access", decl
);
29046 /* Look for the `template' keyword, as a syntactic disambiguator.
29047 Return TRUE iff it is present, in which case it will be
29051 cp_parser_optional_template_keyword (cp_parser
*parser
)
29053 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29055 /* In C++98 the `template' keyword can only be used within templates;
29056 outside templates the parser can always figure out what is a
29057 template and what is not. In C++11, per the resolution of DR 468,
29058 `template' is allowed in cases where it is not strictly necessary. */
29059 if (!processing_template_decl
29060 && pedantic
&& cxx_dialect
== cxx98
)
29062 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29063 pedwarn (token
->location
, OPT_Wpedantic
,
29064 "in C++98 %<template%> (as a disambiguator) is only "
29065 "allowed within templates");
29066 /* If this part of the token stream is rescanned, the same
29067 error message would be generated. So, we purge the token
29068 from the stream. */
29069 cp_lexer_purge_token (parser
->lexer
);
29074 /* Consume the `template' keyword. */
29075 cp_lexer_consume_token (parser
->lexer
);
29082 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29083 set PARSER->SCOPE, and perform other related actions. */
29086 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29088 struct tree_check
*check_value
;
29090 /* Get the stored value. */
29091 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29092 /* Set the scope from the stored value. */
29093 parser
->scope
= saved_checks_value (check_value
);
29094 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29095 parser
->object_scope
= NULL_TREE
;
29098 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29099 encounter the end of a block before what we were looking for. */
29102 cp_parser_cache_group (cp_parser
*parser
,
29103 enum cpp_ttype end
,
29108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29110 /* Abort a parenthesized expression if we encounter a semicolon. */
29111 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29112 && token
->type
== CPP_SEMICOLON
)
29114 /* If we've reached the end of the file, stop. */
29115 if (token
->type
== CPP_EOF
29116 || (end
!= CPP_PRAGMA_EOL
29117 && token
->type
== CPP_PRAGMA_EOL
))
29119 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29120 /* We've hit the end of an enclosing block, so there's been some
29121 kind of syntax error. */
29124 /* Consume the token. */
29125 cp_lexer_consume_token (parser
->lexer
);
29126 /* See if it starts a new group. */
29127 if (token
->type
== CPP_OPEN_BRACE
)
29129 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29130 /* In theory this should probably check end == '}', but
29131 cp_parser_save_member_function_body needs it to exit
29132 after either '}' or ')' when called with ')'. */
29136 else if (token
->type
== CPP_OPEN_PAREN
)
29138 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29139 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29142 else if (token
->type
== CPP_PRAGMA
)
29143 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29144 else if (token
->type
== end
)
29149 /* Like above, for caching a default argument or NSDMI. Both of these are
29150 terminated by a non-nested comma, but it can be unclear whether or not a
29151 comma is nested in a template argument list unless we do more parsing.
29152 In order to handle this ambiguity, when we encounter a ',' after a '<'
29153 we try to parse what follows as a parameter-declaration-list (in the
29154 case of a default argument) or a member-declarator (in the case of an
29155 NSDMI). If that succeeds, then we stop caching. */
29158 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29160 unsigned depth
= 0;
29161 int maybe_template_id
= 0;
29162 cp_token
*first_token
;
29164 tree default_argument
;
29166 /* Add tokens until we have processed the entire default
29167 argument. We add the range [first_token, token). */
29168 first_token
= cp_lexer_peek_token (parser
->lexer
);
29169 if (first_token
->type
== CPP_OPEN_BRACE
)
29171 /* For list-initialization, this is straightforward. */
29172 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29173 token
= cp_lexer_peek_token (parser
->lexer
);
29179 /* Peek at the next token. */
29180 token
= cp_lexer_peek_token (parser
->lexer
);
29181 /* What we do depends on what token we have. */
29182 switch (token
->type
)
29184 /* In valid code, a default argument must be
29185 immediately followed by a `,' `)', or `...'. */
29187 if (depth
== 0 && maybe_template_id
)
29189 /* If we've seen a '<', we might be in a
29190 template-argument-list. Until Core issue 325 is
29191 resolved, we don't know how this situation ought
29192 to be handled, so try to DTRT. We check whether
29193 what comes after the comma is a valid parameter
29194 declaration list. If it is, then the comma ends
29195 the default argument; otherwise the default
29196 argument continues. */
29197 bool error
= false;
29200 /* Set ITALP so cp_parser_parameter_declaration_list
29201 doesn't decide to commit to this parse. */
29202 bool saved_italp
= parser
->in_template_argument_list_p
;
29203 parser
->in_template_argument_list_p
= true;
29205 cp_parser_parse_tentatively (parser
);
29209 /* Parse declarators until we reach a non-comma or
29210 somthing that cannot be an initializer.
29211 Just checking whether we're looking at a single
29212 declarator is insufficient. Consider:
29213 int var = tuple<T,U>::x;
29214 The template parameter 'U' looks exactly like a
29218 int ctor_dtor_or_conv_p
;
29219 cp_lexer_consume_token (parser
->lexer
);
29220 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29221 &ctor_dtor_or_conv_p
,
29222 /*parenthesized_p=*/NULL
,
29224 /*friend_p=*/false);
29225 peek
= cp_lexer_peek_token (parser
->lexer
);
29226 if (cp_parser_error_occurred (parser
))
29229 while (peek
->type
== CPP_COMMA
);
29230 /* If we met an '=' or ';' then the original comma
29231 was the end of the NSDMI. Otherwise assume
29232 we're still in the NSDMI. */
29233 error
= (peek
->type
!= CPP_EQ
29234 && peek
->type
!= CPP_SEMICOLON
);
29238 cp_lexer_consume_token (parser
->lexer
);
29239 begin_scope (sk_function_parms
, NULL_TREE
);
29240 if (cp_parser_parameter_declaration_list (parser
)
29241 == error_mark_node
)
29243 pop_bindings_and_leave_scope ();
29245 if (!cp_parser_error_occurred (parser
) && !error
)
29247 cp_parser_abort_tentative_parse (parser
);
29249 parser
->in_template_argument_list_p
= saved_italp
;
29253 case CPP_CLOSE_PAREN
:
29255 /* If we run into a non-nested `;', `}', or `]',
29256 then the code is invalid -- but the default
29257 argument is certainly over. */
29258 case CPP_SEMICOLON
:
29259 case CPP_CLOSE_BRACE
:
29260 case CPP_CLOSE_SQUARE
:
29262 /* Handle correctly int n = sizeof ... ( p ); */
29263 && token
->type
!= CPP_ELLIPSIS
)
29265 /* Update DEPTH, if necessary. */
29266 else if (token
->type
== CPP_CLOSE_PAREN
29267 || token
->type
== CPP_CLOSE_BRACE
29268 || token
->type
== CPP_CLOSE_SQUARE
)
29272 case CPP_OPEN_PAREN
:
29273 case CPP_OPEN_SQUARE
:
29274 case CPP_OPEN_BRACE
:
29280 /* This might be the comparison operator, or it might
29281 start a template argument list. */
29282 ++maybe_template_id
;
29286 if (cxx_dialect
== cxx98
)
29288 /* Fall through for C++0x, which treats the `>>'
29289 operator like two `>' tokens in certain
29291 gcc_fallthrough ();
29296 /* This might be an operator, or it might close a
29297 template argument list. But if a previous '<'
29298 started a template argument list, this will have
29299 closed it, so we can't be in one anymore. */
29300 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29301 if (maybe_template_id
< 0)
29302 maybe_template_id
= 0;
29306 /* If we run out of tokens, issue an error message. */
29308 case CPP_PRAGMA_EOL
:
29309 error_at (token
->location
, "file ends in default argument");
29310 return error_mark_node
;
29314 /* In these cases, we should look for template-ids.
29315 For example, if the default argument is
29316 `X<int, double>()', we need to do name lookup to
29317 figure out whether or not `X' is a template; if
29318 so, the `,' does not end the default argument.
29320 That is not yet done. */
29327 /* If we've reached the end, stop. */
29331 /* Add the token to the token block. */
29332 token
= cp_lexer_consume_token (parser
->lexer
);
29335 /* Create a DEFAULT_ARG to represent the unparsed default
29337 default_argument
= make_node (DEFAULT_ARG
);
29338 DEFARG_TOKENS (default_argument
)
29339 = cp_token_cache_new (first_token
, token
);
29340 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29342 return default_argument
;
29345 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29348 defarg_location (tree default_argument
)
29350 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29351 location_t start
= tokens
->first
->location
;
29352 location_t end
= tokens
->last
->location
;
29353 return make_location (start
, start
, end
);
29356 /* Begin parsing tentatively. We always save tokens while parsing
29357 tentatively so that if the tentative parsing fails we can restore the
29361 cp_parser_parse_tentatively (cp_parser
* parser
)
29363 /* Enter a new parsing context. */
29364 parser
->context
= cp_parser_context_new (parser
->context
);
29365 /* Begin saving tokens. */
29366 cp_lexer_save_tokens (parser
->lexer
);
29367 /* In order to avoid repetitive access control error messages,
29368 access checks are queued up until we are no longer parsing
29370 push_deferring_access_checks (dk_deferred
);
29373 /* Commit to the currently active tentative parse. */
29376 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29378 cp_parser_context
*context
;
29381 /* Mark all of the levels as committed. */
29382 lexer
= parser
->lexer
;
29383 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29385 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29387 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29388 while (!cp_lexer_saving_tokens (lexer
))
29389 lexer
= lexer
->next
;
29390 cp_lexer_commit_tokens (lexer
);
29394 /* Commit to the topmost currently active tentative parse.
29396 Note that this function shouldn't be called when there are
29397 irreversible side-effects while in a tentative state. For
29398 example, we shouldn't create a permanent entry in the symbol
29399 table, or issue an error message that might not apply if the
29400 tentative parse is aborted. */
29403 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29405 cp_parser_context
*context
= parser
->context
;
29406 cp_lexer
*lexer
= parser
->lexer
;
29410 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29412 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29414 while (!cp_lexer_saving_tokens (lexer
))
29415 lexer
= lexer
->next
;
29416 cp_lexer_commit_tokens (lexer
);
29420 /* Abort the currently active tentative parse. All consumed tokens
29421 will be rolled back, and no diagnostics will be issued. */
29424 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29426 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29427 || errorcount
> 0);
29428 cp_parser_simulate_error (parser
);
29429 /* Now, pretend that we want to see if the construct was
29430 successfully parsed. */
29431 cp_parser_parse_definitely (parser
);
29434 /* Stop parsing tentatively. If a parse error has occurred, restore the
29435 token stream. Otherwise, commit to the tokens we have consumed.
29436 Returns true if no error occurred; false otherwise. */
29439 cp_parser_parse_definitely (cp_parser
* parser
)
29441 bool error_occurred
;
29442 cp_parser_context
*context
;
29444 /* Remember whether or not an error occurred, since we are about to
29445 destroy that information. */
29446 error_occurred
= cp_parser_error_occurred (parser
);
29447 /* Remove the topmost context from the stack. */
29448 context
= parser
->context
;
29449 parser
->context
= context
->next
;
29450 /* If no parse errors occurred, commit to the tentative parse. */
29451 if (!error_occurred
)
29453 /* Commit to the tokens read tentatively, unless that was
29455 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29456 cp_lexer_commit_tokens (parser
->lexer
);
29458 pop_to_parent_deferring_access_checks ();
29460 /* Otherwise, if errors occurred, roll back our state so that things
29461 are just as they were before we began the tentative parse. */
29464 cp_lexer_rollback_tokens (parser
->lexer
);
29465 pop_deferring_access_checks ();
29467 /* Add the context to the front of the free list. */
29468 context
->next
= cp_parser_context_free_list
;
29469 cp_parser_context_free_list
= context
;
29471 return !error_occurred
;
29474 /* Returns true if we are parsing tentatively and are not committed to
29475 this tentative parse. */
29478 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29480 return (cp_parser_parsing_tentatively (parser
)
29481 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29484 /* Returns nonzero iff an error has occurred during the most recent
29485 tentative parse. */
29488 cp_parser_error_occurred (cp_parser
* parser
)
29490 return (cp_parser_parsing_tentatively (parser
)
29491 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29494 /* Returns nonzero if GNU extensions are allowed. */
29497 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29499 return parser
->allow_gnu_extensions_p
;
29502 /* Objective-C++ Productions */
29505 /* Parse an Objective-C expression, which feeds into a primary-expression
29509 objc-message-expression
29510 objc-string-literal
29511 objc-encode-expression
29512 objc-protocol-expression
29513 objc-selector-expression
29515 Returns a tree representation of the expression. */
29518 cp_parser_objc_expression (cp_parser
* parser
)
29520 /* Try to figure out what kind of declaration is present. */
29521 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29525 case CPP_OPEN_SQUARE
:
29526 return cp_parser_objc_message_expression (parser
);
29528 case CPP_OBJC_STRING
:
29529 kwd
= cp_lexer_consume_token (parser
->lexer
);
29530 return objc_build_string_object (kwd
->u
.value
);
29533 switch (kwd
->keyword
)
29535 case RID_AT_ENCODE
:
29536 return cp_parser_objc_encode_expression (parser
);
29538 case RID_AT_PROTOCOL
:
29539 return cp_parser_objc_protocol_expression (parser
);
29541 case RID_AT_SELECTOR
:
29542 return cp_parser_objc_selector_expression (parser
);
29549 error_at (kwd
->location
,
29550 "misplaced %<@%D%> Objective-C++ construct",
29552 cp_parser_skip_to_end_of_block_or_statement (parser
);
29555 return error_mark_node
;
29558 /* Parse an Objective-C message expression.
29560 objc-message-expression:
29561 [ objc-message-receiver objc-message-args ]
29563 Returns a representation of an Objective-C message. */
29566 cp_parser_objc_message_expression (cp_parser
* parser
)
29568 tree receiver
, messageargs
;
29570 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29571 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29572 receiver
= cp_parser_objc_message_receiver (parser
);
29573 messageargs
= cp_parser_objc_message_args (parser
);
29574 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29575 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29577 tree result
= objc_build_message_expr (receiver
, messageargs
);
29579 /* Construct a location e.g.
29582 ranging from the '[' to the ']', with the caret at the start. */
29583 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29584 protected_set_expr_location (result
, combined_loc
);
29589 /* Parse an objc-message-receiver.
29591 objc-message-receiver:
29593 simple-type-specifier
29595 Returns a representation of the type or expression. */
29598 cp_parser_objc_message_receiver (cp_parser
* parser
)
29602 /* An Objective-C message receiver may be either (1) a type
29603 or (2) an expression. */
29604 cp_parser_parse_tentatively (parser
);
29605 rcv
= cp_parser_expression (parser
);
29607 /* If that worked out, fine. */
29608 if (cp_parser_parse_definitely (parser
))
29611 cp_parser_parse_tentatively (parser
);
29612 rcv
= cp_parser_simple_type_specifier (parser
,
29613 /*decl_specs=*/NULL
,
29614 CP_PARSER_FLAGS_NONE
);
29616 if (cp_parser_parse_definitely (parser
))
29617 return objc_get_class_reference (rcv
);
29619 cp_parser_error (parser
, "objective-c++ message receiver expected");
29620 return error_mark_node
;
29623 /* Parse the arguments and selectors comprising an Objective-C message.
29628 objc-selector-args , objc-comma-args
29630 objc-selector-args:
29631 objc-selector [opt] : assignment-expression
29632 objc-selector-args objc-selector [opt] : assignment-expression
29635 assignment-expression
29636 objc-comma-args , assignment-expression
29638 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29639 selector arguments and TREE_VALUE containing a list of comma
29643 cp_parser_objc_message_args (cp_parser
* parser
)
29645 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29646 bool maybe_unary_selector_p
= true;
29647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29649 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29651 tree selector
= NULL_TREE
, arg
;
29653 if (token
->type
!= CPP_COLON
)
29654 selector
= cp_parser_objc_selector (parser
);
29656 /* Detect if we have a unary selector. */
29657 if (maybe_unary_selector_p
29658 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29659 return build_tree_list (selector
, NULL_TREE
);
29661 maybe_unary_selector_p
= false;
29662 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29663 arg
= cp_parser_assignment_expression (parser
);
29666 = chainon (sel_args
,
29667 build_tree_list (selector
, arg
));
29669 token
= cp_lexer_peek_token (parser
->lexer
);
29672 /* Handle non-selector arguments, if any. */
29673 while (token
->type
== CPP_COMMA
)
29677 cp_lexer_consume_token (parser
->lexer
);
29678 arg
= cp_parser_assignment_expression (parser
);
29681 = chainon (addl_args
,
29682 build_tree_list (NULL_TREE
, arg
));
29684 token
= cp_lexer_peek_token (parser
->lexer
);
29687 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29689 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29690 return build_tree_list (error_mark_node
, error_mark_node
);
29693 return build_tree_list (sel_args
, addl_args
);
29696 /* Parse an Objective-C encode expression.
29698 objc-encode-expression:
29699 @encode objc-typename
29701 Returns an encoded representation of the type argument. */
29704 cp_parser_objc_encode_expression (cp_parser
* parser
)
29708 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29710 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29711 matching_parens parens
;
29712 parens
.require_open (parser
);
29713 token
= cp_lexer_peek_token (parser
->lexer
);
29714 type
= complete_type (cp_parser_type_id (parser
));
29715 parens
.require_close (parser
);
29719 error_at (token
->location
,
29720 "%<@encode%> must specify a type as an argument");
29721 return error_mark_node
;
29724 /* This happens if we find @encode(T) (where T is a template
29725 typename or something dependent on a template typename) when
29726 parsing a template. In that case, we can't compile it
29727 immediately, but we rather create an AT_ENCODE_EXPR which will
29728 need to be instantiated when the template is used.
29730 if (dependent_type_p (type
))
29732 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29733 TREE_READONLY (value
) = 1;
29738 /* Build a location of the form:
29741 with caret==start at the @ token, finishing at the close paren. */
29742 location_t combined_loc
29743 = make_location (start_loc
, start_loc
,
29744 cp_lexer_previous_token (parser
->lexer
)->location
);
29746 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29749 /* Parse an Objective-C @defs expression. */
29752 cp_parser_objc_defs_expression (cp_parser
*parser
)
29756 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29757 matching_parens parens
;
29758 parens
.require_open (parser
);
29759 name
= cp_parser_identifier (parser
);
29760 parens
.require_close (parser
);
29762 return objc_get_class_ivars (name
);
29765 /* Parse an Objective-C protocol expression.
29767 objc-protocol-expression:
29768 @protocol ( identifier )
29770 Returns a representation of the protocol expression. */
29773 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29776 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29778 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29779 matching_parens parens
;
29780 parens
.require_open (parser
);
29781 proto
= cp_parser_identifier (parser
);
29782 parens
.require_close (parser
);
29784 /* Build a location of the form:
29787 with caret==start at the @ token, finishing at the close paren. */
29788 location_t combined_loc
29789 = make_location (start_loc
, start_loc
,
29790 cp_lexer_previous_token (parser
->lexer
)->location
);
29791 tree result
= objc_build_protocol_expr (proto
);
29792 protected_set_expr_location (result
, combined_loc
);
29796 /* Parse an Objective-C selector expression.
29798 objc-selector-expression:
29799 @selector ( objc-method-signature )
29801 objc-method-signature:
29807 objc-selector-seq objc-selector :
29809 Returns a representation of the method selector. */
29812 cp_parser_objc_selector_expression (cp_parser
* parser
)
29814 tree sel_seq
= NULL_TREE
;
29815 bool maybe_unary_selector_p
= true;
29817 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29819 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29820 matching_parens parens
;
29821 parens
.require_open (parser
);
29822 token
= cp_lexer_peek_token (parser
->lexer
);
29824 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29825 || token
->type
== CPP_SCOPE
)
29827 tree selector
= NULL_TREE
;
29829 if (token
->type
!= CPP_COLON
29830 || token
->type
== CPP_SCOPE
)
29831 selector
= cp_parser_objc_selector (parser
);
29833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29834 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29836 /* Detect if we have a unary selector. */
29837 if (maybe_unary_selector_p
)
29839 sel_seq
= selector
;
29840 goto finish_selector
;
29844 cp_parser_error (parser
, "expected %<:%>");
29847 maybe_unary_selector_p
= false;
29848 token
= cp_lexer_consume_token (parser
->lexer
);
29850 if (token
->type
== CPP_SCOPE
)
29853 = chainon (sel_seq
,
29854 build_tree_list (selector
, NULL_TREE
));
29856 = chainon (sel_seq
,
29857 build_tree_list (NULL_TREE
, NULL_TREE
));
29861 = chainon (sel_seq
,
29862 build_tree_list (selector
, NULL_TREE
));
29864 token
= cp_lexer_peek_token (parser
->lexer
);
29868 parens
.require_close (parser
);
29871 /* Build a location of the form:
29874 with caret==start at the @ token, finishing at the close paren. */
29875 location_t combined_loc
29876 = make_location (loc
, loc
,
29877 cp_lexer_previous_token (parser
->lexer
)->location
);
29878 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29879 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29880 protected_set_expr_location (result
, combined_loc
);
29884 /* Parse a list of identifiers.
29886 objc-identifier-list:
29888 objc-identifier-list , identifier
29890 Returns a TREE_LIST of identifier nodes. */
29893 cp_parser_objc_identifier_list (cp_parser
* parser
)
29899 identifier
= cp_parser_identifier (parser
);
29900 if (identifier
== error_mark_node
)
29901 return error_mark_node
;
29903 list
= build_tree_list (NULL_TREE
, identifier
);
29904 sep
= cp_lexer_peek_token (parser
->lexer
);
29906 while (sep
->type
== CPP_COMMA
)
29908 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29909 identifier
= cp_parser_identifier (parser
);
29910 if (identifier
== error_mark_node
)
29913 list
= chainon (list
, build_tree_list (NULL_TREE
,
29915 sep
= cp_lexer_peek_token (parser
->lexer
);
29921 /* Parse an Objective-C alias declaration.
29923 objc-alias-declaration:
29924 @compatibility_alias identifier identifier ;
29926 This function registers the alias mapping with the Objective-C front end.
29927 It returns nothing. */
29930 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29934 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29935 alias
= cp_parser_identifier (parser
);
29936 orig
= cp_parser_identifier (parser
);
29937 objc_declare_alias (alias
, orig
);
29938 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29941 /* Parse an Objective-C class forward-declaration.
29943 objc-class-declaration:
29944 @class objc-identifier-list ;
29946 The function registers the forward declarations with the Objective-C
29947 front end. It returns nothing. */
29950 cp_parser_objc_class_declaration (cp_parser
* parser
)
29952 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29957 id
= cp_parser_identifier (parser
);
29958 if (id
== error_mark_node
)
29961 objc_declare_class (id
);
29963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29964 cp_lexer_consume_token (parser
->lexer
);
29968 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29971 /* Parse a list of Objective-C protocol references.
29973 objc-protocol-refs-opt:
29974 objc-protocol-refs [opt]
29976 objc-protocol-refs:
29977 < objc-identifier-list >
29979 Returns a TREE_LIST of identifiers, if any. */
29982 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29984 tree protorefs
= NULL_TREE
;
29986 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29988 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29989 protorefs
= cp_parser_objc_identifier_list (parser
);
29990 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29996 /* Parse a Objective-C visibility specification. */
29999 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30001 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30003 switch (vis
->keyword
)
30005 case RID_AT_PRIVATE
:
30006 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30008 case RID_AT_PROTECTED
:
30009 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30011 case RID_AT_PUBLIC
:
30012 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30014 case RID_AT_PACKAGE
:
30015 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30021 /* Eat '@private'/'@protected'/'@public'. */
30022 cp_lexer_consume_token (parser
->lexer
);
30025 /* Parse an Objective-C method type. Return 'true' if it is a class
30026 (+) method, and 'false' if it is an instance (-) method. */
30029 cp_parser_objc_method_type (cp_parser
* parser
)
30031 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30037 /* Parse an Objective-C protocol qualifier. */
30040 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30042 tree quals
= NULL_TREE
, node
;
30043 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30045 node
= token
->u
.value
;
30047 while (node
&& identifier_p (node
)
30048 && (node
== ridpointers
[(int) RID_IN
]
30049 || node
== ridpointers
[(int) RID_OUT
]
30050 || node
== ridpointers
[(int) RID_INOUT
]
30051 || node
== ridpointers
[(int) RID_BYCOPY
]
30052 || node
== ridpointers
[(int) RID_BYREF
]
30053 || node
== ridpointers
[(int) RID_ONEWAY
]))
30055 quals
= tree_cons (NULL_TREE
, node
, quals
);
30056 cp_lexer_consume_token (parser
->lexer
);
30057 token
= cp_lexer_peek_token (parser
->lexer
);
30058 node
= token
->u
.value
;
30064 /* Parse an Objective-C typename. */
30067 cp_parser_objc_typename (cp_parser
* parser
)
30069 tree type_name
= NULL_TREE
;
30071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30073 tree proto_quals
, cp_type
= NULL_TREE
;
30075 matching_parens parens
;
30076 parens
.consume_open (parser
); /* Eat '('. */
30077 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30079 /* An ObjC type name may consist of just protocol qualifiers, in which
30080 case the type shall default to 'id'. */
30081 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30083 cp_type
= cp_parser_type_id (parser
);
30085 /* If the type could not be parsed, an error has already
30086 been produced. For error recovery, behave as if it had
30087 not been specified, which will use the default type
30089 if (cp_type
== error_mark_node
)
30091 cp_type
= NULL_TREE
;
30092 /* We need to skip to the closing parenthesis as
30093 cp_parser_type_id() does not seem to do it for
30095 cp_parser_skip_to_closing_parenthesis (parser
,
30096 /*recovering=*/true,
30097 /*or_comma=*/false,
30098 /*consume_paren=*/false);
30102 parens
.require_close (parser
);
30103 type_name
= build_tree_list (proto_quals
, cp_type
);
30109 /* Check to see if TYPE refers to an Objective-C selector name. */
30112 cp_parser_objc_selector_p (enum cpp_ttype type
)
30114 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30115 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30116 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30117 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30118 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30121 /* Parse an Objective-C selector. */
30124 cp_parser_objc_selector (cp_parser
* parser
)
30126 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30128 if (!cp_parser_objc_selector_p (token
->type
))
30130 error_at (token
->location
, "invalid Objective-C++ selector name");
30131 return error_mark_node
;
30134 /* C++ operator names are allowed to appear in ObjC selectors. */
30135 switch (token
->type
)
30137 case CPP_AND_AND
: return get_identifier ("and");
30138 case CPP_AND_EQ
: return get_identifier ("and_eq");
30139 case CPP_AND
: return get_identifier ("bitand");
30140 case CPP_OR
: return get_identifier ("bitor");
30141 case CPP_COMPL
: return get_identifier ("compl");
30142 case CPP_NOT
: return get_identifier ("not");
30143 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30144 case CPP_OR_OR
: return get_identifier ("or");
30145 case CPP_OR_EQ
: return get_identifier ("or_eq");
30146 case CPP_XOR
: return get_identifier ("xor");
30147 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30148 default: return token
->u
.value
;
30152 /* Parse an Objective-C params list. */
30155 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30157 tree params
= NULL_TREE
;
30158 bool maybe_unary_selector_p
= true;
30159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30161 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30163 tree selector
= NULL_TREE
, type_name
, identifier
;
30164 tree parm_attr
= NULL_TREE
;
30166 if (token
->keyword
== RID_ATTRIBUTE
)
30169 if (token
->type
!= CPP_COLON
)
30170 selector
= cp_parser_objc_selector (parser
);
30172 /* Detect if we have a unary selector. */
30173 if (maybe_unary_selector_p
30174 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30176 params
= selector
; /* Might be followed by attributes. */
30180 maybe_unary_selector_p
= false;
30181 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30183 /* Something went quite wrong. There should be a colon
30184 here, but there is not. Stop parsing parameters. */
30187 type_name
= cp_parser_objc_typename (parser
);
30188 /* New ObjC allows attributes on parameters too. */
30189 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30190 parm_attr
= cp_parser_attributes_opt (parser
);
30191 identifier
= cp_parser_identifier (parser
);
30195 objc_build_keyword_decl (selector
,
30200 token
= cp_lexer_peek_token (parser
->lexer
);
30203 if (params
== NULL_TREE
)
30205 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30206 return error_mark_node
;
30209 /* We allow tail attributes for the method. */
30210 if (token
->keyword
== RID_ATTRIBUTE
)
30212 *attributes
= cp_parser_attributes_opt (parser
);
30213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30214 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30216 cp_parser_error (parser
,
30217 "method attributes must be specified at the end");
30218 return error_mark_node
;
30221 if (params
== NULL_TREE
)
30223 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30224 return error_mark_node
;
30229 /* Parse the non-keyword Objective-C params. */
30232 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30235 tree params
= make_node (TREE_LIST
);
30236 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30237 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30239 while (token
->type
== CPP_COMMA
)
30241 cp_parameter_declarator
*parmdecl
;
30244 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30245 token
= cp_lexer_peek_token (parser
->lexer
);
30247 if (token
->type
== CPP_ELLIPSIS
)
30249 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30251 token
= cp_lexer_peek_token (parser
->lexer
);
30255 /* TODO: parse attributes for tail parameters. */
30256 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30257 parm
= grokdeclarator (parmdecl
->declarator
,
30258 &parmdecl
->decl_specifiers
,
30259 PARM
, /*initialized=*/0,
30260 /*attrlist=*/NULL
);
30262 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30263 token
= cp_lexer_peek_token (parser
->lexer
);
30266 /* We allow tail attributes for the method. */
30267 if (token
->keyword
== RID_ATTRIBUTE
)
30269 if (*attributes
== NULL_TREE
)
30271 *attributes
= cp_parser_attributes_opt (parser
);
30272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30273 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30277 /* We have an error, but parse the attributes, so that we can
30279 *attributes
= cp_parser_attributes_opt (parser
);
30281 cp_parser_error (parser
,
30282 "method attributes must be specified at the end");
30283 return error_mark_node
;
30289 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30292 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30296 /* If the next token is `extern' and the following token is a string
30297 literal, then we have a linkage specification. */
30298 if (token
->keyword
== RID_EXTERN
30299 && cp_parser_is_pure_string_literal
30300 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30301 cp_parser_linkage_specification (parser
);
30302 /* Handle #pragma, if any. */
30303 else if (token
->type
== CPP_PRAGMA
)
30304 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30305 /* Allow stray semicolons. */
30306 else if (token
->type
== CPP_SEMICOLON
)
30307 cp_lexer_consume_token (parser
->lexer
);
30308 /* Mark methods as optional or required, when building protocols. */
30309 else if (token
->keyword
== RID_AT_OPTIONAL
)
30311 cp_lexer_consume_token (parser
->lexer
);
30312 objc_set_method_opt (true);
30314 else if (token
->keyword
== RID_AT_REQUIRED
)
30316 cp_lexer_consume_token (parser
->lexer
);
30317 objc_set_method_opt (false);
30319 else if (token
->keyword
== RID_NAMESPACE
)
30320 cp_parser_namespace_definition (parser
);
30321 /* Other stray characters must generate errors. */
30322 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30324 cp_lexer_consume_token (parser
->lexer
);
30325 error ("stray %qs between Objective-C++ methods",
30326 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30328 /* Finally, try to parse a block-declaration, or a function-definition. */
30330 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30333 /* Parse a method signature. */
30336 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30338 tree rettype
, kwdparms
, optparms
;
30339 bool ellipsis
= false;
30340 bool is_class_method
;
30342 is_class_method
= cp_parser_objc_method_type (parser
);
30343 rettype
= cp_parser_objc_typename (parser
);
30344 *attributes
= NULL_TREE
;
30345 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30346 if (kwdparms
== error_mark_node
)
30347 return error_mark_node
;
30348 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30349 if (optparms
== error_mark_node
)
30350 return error_mark_node
;
30352 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30356 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30359 cp_lexer_save_tokens (parser
->lexer
);
30360 tattr
= cp_parser_attributes_opt (parser
);
30361 gcc_assert (tattr
) ;
30363 /* If the attributes are followed by a method introducer, this is not allowed.
30364 Dump the attributes and flag the situation. */
30365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30366 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30369 /* Otherwise, the attributes introduce some interstitial code, possibly so
30370 rewind to allow that check. */
30371 cp_lexer_rollback_tokens (parser
->lexer
);
30375 /* Parse an Objective-C method prototype list. */
30378 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30380 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30382 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30384 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30386 tree attributes
, sig
;
30387 bool is_class_method
;
30388 if (token
->type
== CPP_PLUS
)
30389 is_class_method
= true;
30391 is_class_method
= false;
30392 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30393 if (sig
== error_mark_node
)
30395 cp_parser_skip_to_end_of_block_or_statement (parser
);
30396 token
= cp_lexer_peek_token (parser
->lexer
);
30399 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30400 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30402 else if (token
->keyword
== RID_AT_PROPERTY
)
30403 cp_parser_objc_at_property_declaration (parser
);
30404 else if (token
->keyword
== RID_ATTRIBUTE
30405 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30406 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30408 "prefix attributes are ignored for methods");
30410 /* Allow for interspersed non-ObjC++ code. */
30411 cp_parser_objc_interstitial_code (parser
);
30413 token
= cp_lexer_peek_token (parser
->lexer
);
30416 if (token
->type
!= CPP_EOF
)
30417 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30419 cp_parser_error (parser
, "expected %<@end%>");
30421 objc_finish_interface ();
30424 /* Parse an Objective-C method definition list. */
30427 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30429 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30431 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30435 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30438 tree sig
, attribute
;
30439 bool is_class_method
;
30440 if (token
->type
== CPP_PLUS
)
30441 is_class_method
= true;
30443 is_class_method
= false;
30444 push_deferring_access_checks (dk_deferred
);
30445 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30446 if (sig
== error_mark_node
)
30448 cp_parser_skip_to_end_of_block_or_statement (parser
);
30449 token
= cp_lexer_peek_token (parser
->lexer
);
30452 objc_start_method_definition (is_class_method
, sig
, attribute
,
30455 /* For historical reasons, we accept an optional semicolon. */
30456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30457 cp_lexer_consume_token (parser
->lexer
);
30459 ptk
= cp_lexer_peek_token (parser
->lexer
);
30460 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30461 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30463 perform_deferred_access_checks (tf_warning_or_error
);
30464 stop_deferring_access_checks ();
30465 meth
= cp_parser_function_definition_after_declarator (parser
,
30467 pop_deferring_access_checks ();
30468 objc_finish_method_definition (meth
);
30471 /* The following case will be removed once @synthesize is
30472 completely implemented. */
30473 else if (token
->keyword
== RID_AT_PROPERTY
)
30474 cp_parser_objc_at_property_declaration (parser
);
30475 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30476 cp_parser_objc_at_synthesize_declaration (parser
);
30477 else if (token
->keyword
== RID_AT_DYNAMIC
)
30478 cp_parser_objc_at_dynamic_declaration (parser
);
30479 else if (token
->keyword
== RID_ATTRIBUTE
30480 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30481 warning_at (token
->location
, OPT_Wattributes
,
30482 "prefix attributes are ignored for methods");
30484 /* Allow for interspersed non-ObjC++ code. */
30485 cp_parser_objc_interstitial_code (parser
);
30487 token
= cp_lexer_peek_token (parser
->lexer
);
30490 if (token
->type
!= CPP_EOF
)
30491 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30493 cp_parser_error (parser
, "expected %<@end%>");
30495 objc_finish_implementation ();
30498 /* Parse Objective-C ivars. */
30501 cp_parser_objc_class_ivars (cp_parser
* parser
)
30503 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30505 if (token
->type
!= CPP_OPEN_BRACE
)
30506 return; /* No ivars specified. */
30508 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30509 token
= cp_lexer_peek_token (parser
->lexer
);
30511 while (token
->type
!= CPP_CLOSE_BRACE
30512 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30514 cp_decl_specifier_seq declspecs
;
30515 int decl_class_or_enum_p
;
30516 tree prefix_attributes
;
30518 cp_parser_objc_visibility_spec (parser
);
30520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30523 cp_parser_decl_specifier_seq (parser
,
30524 CP_PARSER_FLAGS_OPTIONAL
,
30526 &decl_class_or_enum_p
);
30528 /* auto, register, static, extern, mutable. */
30529 if (declspecs
.storage_class
!= sc_none
)
30531 cp_parser_error (parser
, "invalid type for instance variable");
30532 declspecs
.storage_class
= sc_none
;
30535 /* thread_local. */
30536 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30538 cp_parser_error (parser
, "invalid type for instance variable");
30539 declspecs
.locations
[ds_thread
] = 0;
30543 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30545 cp_parser_error (parser
, "invalid type for instance variable");
30546 declspecs
.locations
[ds_typedef
] = 0;
30549 prefix_attributes
= declspecs
.attributes
;
30550 declspecs
.attributes
= NULL_TREE
;
30552 /* Keep going until we hit the `;' at the end of the
30554 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30556 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30557 cp_declarator
*declarator
= NULL
;
30558 int ctor_dtor_or_conv_p
;
30560 /* Check for a (possibly unnamed) bitfield declaration. */
30561 token
= cp_lexer_peek_token (parser
->lexer
);
30562 if (token
->type
== CPP_COLON
)
30565 if (token
->type
== CPP_NAME
30566 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30569 /* Get the name of the bitfield. */
30570 declarator
= make_id_declarator (NULL_TREE
,
30571 cp_parser_identifier (parser
),
30575 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30576 /* Get the width of the bitfield. */
30578 = cp_parser_constant_expression (parser
);
30582 /* Parse the declarator. */
30584 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30585 &ctor_dtor_or_conv_p
,
30586 /*parenthesized_p=*/NULL
,
30587 /*member_p=*/false,
30588 /*friend_p=*/false);
30591 /* Look for attributes that apply to the ivar. */
30592 attributes
= cp_parser_attributes_opt (parser
);
30593 /* Remember which attributes are prefix attributes and
30595 first_attribute
= attributes
;
30596 /* Combine the attributes. */
30597 attributes
= attr_chainon (prefix_attributes
, attributes
);
30600 /* Create the bitfield declaration. */
30601 decl
= grokbitfield (declarator
, &declspecs
,
30602 width
, NULL_TREE
, attributes
);
30604 decl
= grokfield (declarator
, &declspecs
,
30605 NULL_TREE
, /*init_const_expr_p=*/false,
30606 NULL_TREE
, attributes
);
30608 /* Add the instance variable. */
30609 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30610 objc_add_instance_variable (decl
);
30612 /* Reset PREFIX_ATTRIBUTES. */
30613 if (attributes
!= error_mark_node
)
30615 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30616 attributes
= TREE_CHAIN (attributes
);
30618 TREE_CHAIN (attributes
) = NULL_TREE
;
30621 token
= cp_lexer_peek_token (parser
->lexer
);
30623 if (token
->type
== CPP_COMMA
)
30625 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30631 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30632 token
= cp_lexer_peek_token (parser
->lexer
);
30635 if (token
->keyword
== RID_AT_END
)
30636 cp_parser_error (parser
, "expected %<}%>");
30638 /* Do not consume the RID_AT_END, so it will be read again as terminating
30639 the @interface of @implementation. */
30640 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30641 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30643 /* For historical reasons, we accept an optional semicolon. */
30644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30645 cp_lexer_consume_token (parser
->lexer
);
30648 /* Parse an Objective-C protocol declaration. */
30651 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30653 tree proto
, protorefs
;
30656 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30657 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30659 tok
= cp_lexer_peek_token (parser
->lexer
);
30660 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30661 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30665 /* See if we have a forward declaration or a definition. */
30666 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30668 /* Try a forward declaration first. */
30669 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30675 id
= cp_parser_identifier (parser
);
30676 if (id
== error_mark_node
)
30679 objc_declare_protocol (id
, attributes
);
30681 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30682 cp_lexer_consume_token (parser
->lexer
);
30686 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30689 /* Ok, we got a full-fledged definition (or at least should). */
30692 proto
= cp_parser_identifier (parser
);
30693 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30694 objc_start_protocol (proto
, protorefs
, attributes
);
30695 cp_parser_objc_method_prototype_list (parser
);
30699 /* Parse an Objective-C superclass or category. */
30702 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30705 tree
*categ
, bool *is_class_extension
)
30707 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30709 *super
= *categ
= NULL_TREE
;
30710 *is_class_extension
= false;
30711 if (next
->type
== CPP_COLON
)
30713 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30714 *super
= cp_parser_identifier (parser
);
30716 else if (next
->type
== CPP_OPEN_PAREN
)
30718 matching_parens parens
;
30719 parens
.consume_open (parser
); /* Eat '('. */
30721 /* If there is no category name, and this is an @interface, we
30722 have a class extension. */
30723 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30725 *categ
= NULL_TREE
;
30726 *is_class_extension
= true;
30729 *categ
= cp_parser_identifier (parser
);
30731 parens
.require_close (parser
);
30735 /* Parse an Objective-C class interface. */
30738 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30740 tree name
, super
, categ
, protos
;
30741 bool is_class_extension
;
30743 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30744 name
= cp_parser_identifier (parser
);
30745 if (name
== error_mark_node
)
30747 /* It's hard to recover because even if valid @interface stuff
30748 is to follow, we can't compile it (or validate it) if we
30749 don't even know which class it refers to. Let's assume this
30750 was a stray '@interface' token in the stream and skip it.
30754 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30755 &is_class_extension
);
30756 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30758 /* We have either a class or a category on our hands. */
30759 if (categ
|| is_class_extension
)
30760 objc_start_category_interface (name
, categ
, protos
, attributes
);
30763 objc_start_class_interface (name
, super
, protos
, attributes
);
30764 /* Handle instance variable declarations, if any. */
30765 cp_parser_objc_class_ivars (parser
);
30766 objc_continue_interface ();
30769 cp_parser_objc_method_prototype_list (parser
);
30772 /* Parse an Objective-C class implementation. */
30775 cp_parser_objc_class_implementation (cp_parser
* parser
)
30777 tree name
, super
, categ
;
30778 bool is_class_extension
;
30780 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30781 name
= cp_parser_identifier (parser
);
30782 if (name
== error_mark_node
)
30784 /* It's hard to recover because even if valid @implementation
30785 stuff is to follow, we can't compile it (or validate it) if
30786 we don't even know which class it refers to. Let's assume
30787 this was a stray '@implementation' token in the stream and
30792 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30793 &is_class_extension
);
30795 /* We have either a class or a category on our hands. */
30797 objc_start_category_implementation (name
, categ
);
30800 objc_start_class_implementation (name
, super
);
30801 /* Handle instance variable declarations, if any. */
30802 cp_parser_objc_class_ivars (parser
);
30803 objc_continue_implementation ();
30806 cp_parser_objc_method_definition_list (parser
);
30809 /* Consume the @end token and finish off the implementation. */
30812 cp_parser_objc_end_implementation (cp_parser
* parser
)
30814 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30815 objc_finish_implementation ();
30818 /* Parse an Objective-C declaration. */
30821 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30823 /* Try to figure out what kind of declaration is present. */
30824 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30827 switch (kwd
->keyword
)
30832 error_at (kwd
->location
, "attributes may not be specified before"
30833 " the %<@%D%> Objective-C++ keyword",
30837 case RID_AT_IMPLEMENTATION
:
30838 warning_at (kwd
->location
, OPT_Wattributes
,
30839 "prefix attributes are ignored before %<@%D%>",
30846 switch (kwd
->keyword
)
30849 cp_parser_objc_alias_declaration (parser
);
30852 cp_parser_objc_class_declaration (parser
);
30854 case RID_AT_PROTOCOL
:
30855 cp_parser_objc_protocol_declaration (parser
, attributes
);
30857 case RID_AT_INTERFACE
:
30858 cp_parser_objc_class_interface (parser
, attributes
);
30860 case RID_AT_IMPLEMENTATION
:
30861 cp_parser_objc_class_implementation (parser
);
30864 cp_parser_objc_end_implementation (parser
);
30867 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30869 cp_parser_skip_to_end_of_block_or_statement (parser
);
30873 /* Parse an Objective-C try-catch-finally statement.
30875 objc-try-catch-finally-stmt:
30876 @try compound-statement objc-catch-clause-seq [opt]
30877 objc-finally-clause [opt]
30879 objc-catch-clause-seq:
30880 objc-catch-clause objc-catch-clause-seq [opt]
30883 @catch ( objc-exception-declaration ) compound-statement
30885 objc-finally-clause:
30886 @finally compound-statement
30888 objc-exception-declaration:
30889 parameter-declaration
30892 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30896 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30897 for C. Keep them in sync. */
30900 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30902 location_t location
;
30905 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30906 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30907 objc_maybe_warn_exceptions (location
);
30908 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30909 node, lest it get absorbed into the surrounding block. */
30910 stmt
= push_stmt_list ();
30911 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30912 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30914 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30916 cp_parameter_declarator
*parm
;
30917 tree parameter_declaration
= error_mark_node
;
30918 bool seen_open_paren
= false;
30919 matching_parens parens
;
30921 cp_lexer_consume_token (parser
->lexer
);
30922 if (parens
.require_open (parser
))
30923 seen_open_paren
= true;
30924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30926 /* We have "@catch (...)" (where the '...' are literally
30927 what is in the code). Skip the '...'.
30928 parameter_declaration is set to NULL_TREE, and
30929 objc_being_catch_clauses() knows that that means
30931 cp_lexer_consume_token (parser
->lexer
);
30932 parameter_declaration
= NULL_TREE
;
30936 /* We have "@catch (NSException *exception)" or something
30937 like that. Parse the parameter declaration. */
30938 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30940 parameter_declaration
= error_mark_node
;
30942 parameter_declaration
= grokdeclarator (parm
->declarator
,
30943 &parm
->decl_specifiers
,
30944 PARM
, /*initialized=*/0,
30945 /*attrlist=*/NULL
);
30947 if (seen_open_paren
)
30948 parens
.require_close (parser
);
30951 /* If there was no open parenthesis, we are recovering from
30952 an error, and we are trying to figure out what mistake
30953 the user has made. */
30955 /* If there is an immediate closing parenthesis, the user
30956 probably forgot the opening one (ie, they typed "@catch
30957 NSException *e)". Parse the closing parenthesis and keep
30959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30960 cp_lexer_consume_token (parser
->lexer
);
30962 /* If these is no immediate closing parenthesis, the user
30963 probably doesn't know that parenthesis are required at
30964 all (ie, they typed "@catch NSException *e"). So, just
30965 forget about the closing parenthesis and keep going. */
30967 objc_begin_catch_clause (parameter_declaration
);
30968 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30969 objc_finish_catch_clause ();
30971 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30973 cp_lexer_consume_token (parser
->lexer
);
30974 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30975 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30976 node, lest it get absorbed into the surrounding block. */
30977 stmt
= push_stmt_list ();
30978 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30979 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30982 return objc_finish_try_stmt ();
30985 /* Parse an Objective-C synchronized statement.
30987 objc-synchronized-stmt:
30988 @synchronized ( expression ) compound-statement
30990 Returns NULL_TREE. */
30993 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30995 location_t location
;
30998 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31000 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31001 objc_maybe_warn_exceptions (location
);
31002 matching_parens parens
;
31003 parens
.require_open (parser
);
31004 lock
= cp_parser_expression (parser
);
31005 parens
.require_close (parser
);
31007 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31008 node, lest it get absorbed into the surrounding block. */
31009 stmt
= push_stmt_list ();
31010 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31012 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31015 /* Parse an Objective-C throw statement.
31018 @throw assignment-expression [opt] ;
31020 Returns a constructed '@throw' statement. */
31023 cp_parser_objc_throw_statement (cp_parser
*parser
)
31025 tree expr
= NULL_TREE
;
31026 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31028 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31030 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31031 expr
= cp_parser_expression (parser
);
31033 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31035 return objc_build_throw_stmt (loc
, expr
);
31038 /* Parse an Objective-C statement. */
31041 cp_parser_objc_statement (cp_parser
* parser
)
31043 /* Try to figure out what kind of declaration is present. */
31044 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31046 switch (kwd
->keyword
)
31049 return cp_parser_objc_try_catch_finally_statement (parser
);
31050 case RID_AT_SYNCHRONIZED
:
31051 return cp_parser_objc_synchronized_statement (parser
);
31053 return cp_parser_objc_throw_statement (parser
);
31055 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31057 cp_parser_skip_to_end_of_block_or_statement (parser
);
31060 return error_mark_node
;
31063 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31064 look ahead to see if an objc keyword follows the attributes. This
31065 is to detect the use of prefix attributes on ObjC @interface and
31069 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31071 cp_lexer_save_tokens (parser
->lexer
);
31072 *attrib
= cp_parser_attributes_opt (parser
);
31073 gcc_assert (*attrib
);
31074 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31076 cp_lexer_commit_tokens (parser
->lexer
);
31079 cp_lexer_rollback_tokens (parser
->lexer
);
31083 /* This routine is a minimal replacement for
31084 c_parser_struct_declaration () used when parsing the list of
31085 types/names or ObjC++ properties. For example, when parsing the
31088 @property (readonly) int a, b, c;
31090 this function is responsible for parsing "int a, int b, int c" and
31091 returning the declarations as CHAIN of DECLs.
31093 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31094 similar parsing. */
31096 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31098 tree decls
= NULL_TREE
;
31099 cp_decl_specifier_seq declspecs
;
31100 int decl_class_or_enum_p
;
31101 tree prefix_attributes
;
31103 cp_parser_decl_specifier_seq (parser
,
31104 CP_PARSER_FLAGS_NONE
,
31106 &decl_class_or_enum_p
);
31108 if (declspecs
.type
== error_mark_node
)
31109 return error_mark_node
;
31111 /* auto, register, static, extern, mutable. */
31112 if (declspecs
.storage_class
!= sc_none
)
31114 cp_parser_error (parser
, "invalid type for property");
31115 declspecs
.storage_class
= sc_none
;
31118 /* thread_local. */
31119 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31121 cp_parser_error (parser
, "invalid type for property");
31122 declspecs
.locations
[ds_thread
] = 0;
31126 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31128 cp_parser_error (parser
, "invalid type for property");
31129 declspecs
.locations
[ds_typedef
] = 0;
31132 prefix_attributes
= declspecs
.attributes
;
31133 declspecs
.attributes
= NULL_TREE
;
31135 /* Keep going until we hit the `;' at the end of the declaration. */
31136 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31138 tree attributes
, first_attribute
, decl
;
31139 cp_declarator
*declarator
;
31142 /* Parse the declarator. */
31143 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31144 NULL
, NULL
, false, false);
31146 /* Look for attributes that apply to the ivar. */
31147 attributes
= cp_parser_attributes_opt (parser
);
31148 /* Remember which attributes are prefix attributes and
31150 first_attribute
= attributes
;
31151 /* Combine the attributes. */
31152 attributes
= attr_chainon (prefix_attributes
, attributes
);
31154 decl
= grokfield (declarator
, &declspecs
,
31155 NULL_TREE
, /*init_const_expr_p=*/false,
31156 NULL_TREE
, attributes
);
31158 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31159 return error_mark_node
;
31161 /* Reset PREFIX_ATTRIBUTES. */
31162 if (attributes
!= error_mark_node
)
31164 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31165 attributes
= TREE_CHAIN (attributes
);
31167 TREE_CHAIN (attributes
) = NULL_TREE
;
31170 DECL_CHAIN (decl
) = decls
;
31173 token
= cp_lexer_peek_token (parser
->lexer
);
31174 if (token
->type
== CPP_COMMA
)
31176 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31185 /* Parse an Objective-C @property declaration. The syntax is:
31187 objc-property-declaration:
31188 '@property' objc-property-attributes[opt] struct-declaration ;
31190 objc-property-attributes:
31191 '(' objc-property-attribute-list ')'
31193 objc-property-attribute-list:
31194 objc-property-attribute
31195 objc-property-attribute-list, objc-property-attribute
31197 objc-property-attribute
31198 'getter' = identifier
31199 'setter' = identifier
31208 @property NSString *name;
31209 @property (readonly) id object;
31210 @property (retain, nonatomic, getter=getTheName) id name;
31211 @property int a, b, c;
31213 PS: This function is identical to
31214 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31216 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31218 /* The following variables hold the attributes of the properties as
31219 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31220 seen. When we see an attribute, we set them to 'true' (if they
31221 are boolean properties) or to the identifier (if they have an
31222 argument, ie, for getter and setter). Note that here we only
31223 parse the list of attributes, check the syntax and accumulate the
31224 attributes that we find. objc_add_property_declaration() will
31225 then process the information. */
31226 bool property_assign
= false;
31227 bool property_copy
= false;
31228 tree property_getter_ident
= NULL_TREE
;
31229 bool property_nonatomic
= false;
31230 bool property_readonly
= false;
31231 bool property_readwrite
= false;
31232 bool property_retain
= false;
31233 tree property_setter_ident
= NULL_TREE
;
31235 /* 'properties' is the list of properties that we read. Usually a
31236 single one, but maybe more (eg, in "@property int a, b, c;" there
31241 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31243 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31245 /* Parse the optional attribute list... */
31246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31249 matching_parens parens
;
31250 parens
.consume_open (parser
);
31254 bool syntax_error
= false;
31255 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31258 if (token
->type
!= CPP_NAME
)
31260 cp_parser_error (parser
, "expected identifier");
31263 keyword
= C_RID_CODE (token
->u
.value
);
31264 cp_lexer_consume_token (parser
->lexer
);
31267 case RID_ASSIGN
: property_assign
= true; break;
31268 case RID_COPY
: property_copy
= true; break;
31269 case RID_NONATOMIC
: property_nonatomic
= true; break;
31270 case RID_READONLY
: property_readonly
= true; break;
31271 case RID_READWRITE
: property_readwrite
= true; break;
31272 case RID_RETAIN
: property_retain
= true; break;
31276 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31278 if (keyword
== RID_GETTER
)
31279 cp_parser_error (parser
,
31280 "missing %<=%> (after %<getter%> attribute)");
31282 cp_parser_error (parser
,
31283 "missing %<=%> (after %<setter%> attribute)");
31284 syntax_error
= true;
31287 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31288 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31290 cp_parser_error (parser
, "expected identifier");
31291 syntax_error
= true;
31294 if (keyword
== RID_SETTER
)
31296 if (property_setter_ident
!= NULL_TREE
)
31298 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31299 cp_lexer_consume_token (parser
->lexer
);
31302 property_setter_ident
= cp_parser_objc_selector (parser
);
31303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31304 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31306 cp_lexer_consume_token (parser
->lexer
);
31310 if (property_getter_ident
!= NULL_TREE
)
31312 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31313 cp_lexer_consume_token (parser
->lexer
);
31316 property_getter_ident
= cp_parser_objc_selector (parser
);
31320 cp_parser_error (parser
, "unknown property attribute");
31321 syntax_error
= true;
31328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31329 cp_lexer_consume_token (parser
->lexer
);
31334 /* FIXME: "@property (setter, assign);" will generate a spurious
31335 "error: expected ‘)’ before ‘,’ token". This is because
31336 cp_parser_require, unlike the C counterpart, will produce an
31337 error even if we are in error recovery. */
31338 if (!parens
.require_close (parser
))
31340 cp_parser_skip_to_closing_parenthesis (parser
,
31341 /*recovering=*/true,
31342 /*or_comma=*/false,
31343 /*consume_paren=*/true);
31347 /* ... and the property declaration(s). */
31348 properties
= cp_parser_objc_struct_declaration (parser
);
31350 if (properties
== error_mark_node
)
31352 cp_parser_skip_to_end_of_statement (parser
);
31353 /* If the next token is now a `;', consume it. */
31354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31355 cp_lexer_consume_token (parser
->lexer
);
31359 if (properties
== NULL_TREE
)
31360 cp_parser_error (parser
, "expected identifier");
31363 /* Comma-separated properties are chained together in
31364 reverse order; add them one by one. */
31365 properties
= nreverse (properties
);
31367 for (; properties
; properties
= TREE_CHAIN (properties
))
31368 objc_add_property_declaration (loc
, copy_node (properties
),
31369 property_readonly
, property_readwrite
,
31370 property_assign
, property_retain
,
31371 property_copy
, property_nonatomic
,
31372 property_getter_ident
, property_setter_ident
);
31375 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31378 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31380 objc-synthesize-declaration:
31381 @synthesize objc-synthesize-identifier-list ;
31383 objc-synthesize-identifier-list:
31384 objc-synthesize-identifier
31385 objc-synthesize-identifier-list, objc-synthesize-identifier
31387 objc-synthesize-identifier
31389 identifier = identifier
31392 @synthesize MyProperty;
31393 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31395 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31396 for C. Keep them in sync.
31399 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31401 tree list
= NULL_TREE
;
31403 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31405 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31408 tree property
, ivar
;
31409 property
= cp_parser_identifier (parser
);
31410 if (property
== error_mark_node
)
31412 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31417 cp_lexer_consume_token (parser
->lexer
);
31418 ivar
= cp_parser_identifier (parser
);
31419 if (ivar
== error_mark_node
)
31421 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31427 list
= chainon (list
, build_tree_list (ivar
, property
));
31428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31429 cp_lexer_consume_token (parser
->lexer
);
31433 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31434 objc_add_synthesize_declaration (loc
, list
);
31437 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31439 objc-dynamic-declaration:
31440 @dynamic identifier-list ;
31443 @dynamic MyProperty;
31444 @dynamic MyProperty, AnotherProperty;
31446 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31447 for C. Keep them in sync.
31450 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31452 tree list
= NULL_TREE
;
31454 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31456 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31460 property
= cp_parser_identifier (parser
);
31461 if (property
== error_mark_node
)
31463 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31466 list
= chainon (list
, build_tree_list (NULL
, property
));
31467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31468 cp_lexer_consume_token (parser
->lexer
);
31472 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31473 objc_add_dynamic_declaration (loc
, list
);
31477 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31479 /* Returns name of the next clause.
31480 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31481 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31482 returned and the token is consumed. */
31484 static pragma_omp_clause
31485 cp_parser_omp_clause_name (cp_parser
*parser
)
31487 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31489 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31490 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31491 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31492 result
= PRAGMA_OMP_CLAUSE_IF
;
31493 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31494 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31495 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31496 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31497 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31498 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31499 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31500 result
= PRAGMA_OMP_CLAUSE_FOR
;
31501 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31503 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31504 const char *p
= IDENTIFIER_POINTER (id
);
31509 if (!strcmp ("aligned", p
))
31510 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31511 else if (!strcmp ("async", p
))
31512 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31515 if (!strcmp ("collapse", p
))
31516 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31517 else if (!strcmp ("copy", p
))
31518 result
= PRAGMA_OACC_CLAUSE_COPY
;
31519 else if (!strcmp ("copyin", p
))
31520 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31521 else if (!strcmp ("copyout", p
))
31522 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31523 else if (!strcmp ("copyprivate", p
))
31524 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31525 else if (!strcmp ("create", p
))
31526 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31529 if (!strcmp ("defaultmap", p
))
31530 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31531 else if (!strcmp ("depend", p
))
31532 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31533 else if (!strcmp ("device", p
))
31534 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31535 else if (!strcmp ("deviceptr", p
))
31536 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31537 else if (!strcmp ("device_resident", p
))
31538 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31539 else if (!strcmp ("dist_schedule", p
))
31540 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31543 if (!strcmp ("final", p
))
31544 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31545 else if (!strcmp ("finalize", p
))
31546 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31547 else if (!strcmp ("firstprivate", p
))
31548 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31549 else if (!strcmp ("from", p
))
31550 result
= PRAGMA_OMP_CLAUSE_FROM
;
31553 if (!strcmp ("gang", p
))
31554 result
= PRAGMA_OACC_CLAUSE_GANG
;
31555 else if (!strcmp ("grainsize", p
))
31556 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31559 if (!strcmp ("hint", p
))
31560 result
= PRAGMA_OMP_CLAUSE_HINT
;
31561 else if (!strcmp ("host", p
))
31562 result
= PRAGMA_OACC_CLAUSE_HOST
;
31565 if (!strcmp ("if_present", p
))
31566 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31567 else if (!strcmp ("inbranch", p
))
31568 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31569 else if (!strcmp ("independent", p
))
31570 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31571 else if (!strcmp ("is_device_ptr", p
))
31572 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31575 if (!strcmp ("lastprivate", p
))
31576 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31577 else if (!strcmp ("linear", p
))
31578 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31579 else if (!strcmp ("link", p
))
31580 result
= PRAGMA_OMP_CLAUSE_LINK
;
31583 if (!strcmp ("map", p
))
31584 result
= PRAGMA_OMP_CLAUSE_MAP
;
31585 else if (!strcmp ("mergeable", p
))
31586 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31589 if (!strcmp ("nogroup", p
))
31590 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31591 else if (!strcmp ("notinbranch", p
))
31592 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31593 else if (!strcmp ("nowait", p
))
31594 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31595 else if (!strcmp ("num_gangs", p
))
31596 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31597 else if (!strcmp ("num_tasks", p
))
31598 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31599 else if (!strcmp ("num_teams", p
))
31600 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31601 else if (!strcmp ("num_threads", p
))
31602 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31603 else if (!strcmp ("num_workers", p
))
31604 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31607 if (!strcmp ("ordered", p
))
31608 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31611 if (!strcmp ("parallel", p
))
31612 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31613 else if (!strcmp ("present", p
))
31614 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31615 else if (!strcmp ("present_or_copy", p
)
31616 || !strcmp ("pcopy", p
))
31617 result
= PRAGMA_OACC_CLAUSE_COPY
;
31618 else if (!strcmp ("present_or_copyin", p
)
31619 || !strcmp ("pcopyin", p
))
31620 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31621 else if (!strcmp ("present_or_copyout", p
)
31622 || !strcmp ("pcopyout", p
))
31623 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31624 else if (!strcmp ("present_or_create", p
)
31625 || !strcmp ("pcreate", p
))
31626 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31627 else if (!strcmp ("priority", p
))
31628 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31629 else if (!strcmp ("proc_bind", p
))
31630 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31633 if (!strcmp ("reduction", p
))
31634 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31637 if (!strcmp ("safelen", p
))
31638 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31639 else if (!strcmp ("schedule", p
))
31640 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31641 else if (!strcmp ("sections", p
))
31642 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31643 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31644 result
= PRAGMA_OACC_CLAUSE_HOST
;
31645 else if (!strcmp ("seq", p
))
31646 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31647 else if (!strcmp ("shared", p
))
31648 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31649 else if (!strcmp ("simd", p
))
31650 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31651 else if (!strcmp ("simdlen", p
))
31652 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31655 if (!strcmp ("taskgroup", p
))
31656 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31657 else if (!strcmp ("thread_limit", p
))
31658 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31659 else if (!strcmp ("threads", p
))
31660 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31661 else if (!strcmp ("tile", p
))
31662 result
= PRAGMA_OACC_CLAUSE_TILE
;
31663 else if (!strcmp ("to", p
))
31664 result
= PRAGMA_OMP_CLAUSE_TO
;
31667 if (!strcmp ("uniform", p
))
31668 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31669 else if (!strcmp ("untied", p
))
31670 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31671 else if (!strcmp ("use_device", p
))
31672 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31673 else if (!strcmp ("use_device_ptr", p
))
31674 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31677 if (!strcmp ("vector", p
))
31678 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31679 else if (!strcmp ("vector_length", p
))
31680 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31683 if (!strcmp ("wait", p
))
31684 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31685 else if (!strcmp ("worker", p
))
31686 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31691 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31692 cp_lexer_consume_token (parser
->lexer
);
31697 /* Validate that a clause of the given type does not already exist. */
31700 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31701 const char *name
, location_t location
)
31705 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31706 if (OMP_CLAUSE_CODE (c
) == code
)
31708 error_at (location
, "too many %qs clauses", name
);
31716 variable-list , identifier
31718 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31719 colon). An opening parenthesis will have been consumed by the caller.
31721 If KIND is nonzero, create the appropriate node and install the decl
31722 in OMP_CLAUSE_DECL and add the node to the head of the list.
31724 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31725 return the list created.
31727 COLON can be NULL if only closing parenthesis should end the list,
31728 or pointer to bool which will receive false if the list is terminated
31729 by closing parenthesis or true if the list is terminated by colon. */
31732 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31733 tree list
, bool *colon
)
31736 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31739 parser
->colon_corrects_to_scope_p
= false;
31746 token
= cp_lexer_peek_token (parser
->lexer
);
31748 && current_class_ptr
31749 && cp_parser_is_keyword (token
, RID_THIS
))
31751 decl
= finish_this_expr ();
31752 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31753 || CONVERT_EXPR_P (decl
))
31754 decl
= TREE_OPERAND (decl
, 0);
31755 cp_lexer_consume_token (parser
->lexer
);
31759 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31760 /*check_dependency_p=*/true,
31761 /*template_p=*/NULL
,
31762 /*declarator_p=*/false,
31763 /*optional_p=*/false);
31764 if (name
== error_mark_node
)
31767 if (identifier_p (name
))
31768 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31771 if (decl
== error_mark_node
)
31772 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31775 if (decl
== error_mark_node
)
31777 else if (kind
!= 0)
31781 case OMP_CLAUSE__CACHE_
:
31782 /* The OpenACC cache directive explicitly only allows "array
31783 elements or subarrays". */
31784 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31786 error_at (token
->location
, "expected %<[%>");
31787 decl
= error_mark_node
;
31791 case OMP_CLAUSE_MAP
:
31792 case OMP_CLAUSE_FROM
:
31793 case OMP_CLAUSE_TO
:
31794 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31797 = cp_lexer_peek_token (parser
->lexer
)->location
;
31798 cp_id_kind idk
= CP_ID_KIND_NONE
;
31799 cp_lexer_consume_token (parser
->lexer
);
31800 decl
= convert_from_reference (decl
);
31802 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31807 case OMP_CLAUSE_DEPEND
:
31808 case OMP_CLAUSE_REDUCTION
:
31809 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31811 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31813 parser
->colon_corrects_to_scope_p
= false;
31814 cp_lexer_consume_token (parser
->lexer
);
31815 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31816 low_bound
= cp_parser_expression (parser
);
31818 parser
->colon_corrects_to_scope_p
31819 = saved_colon_corrects_to_scope_p
;
31820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31821 length
= integer_one_node
;
31824 /* Look for `:'. */
31825 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31827 if (!cp_lexer_next_token_is (parser
->lexer
,
31829 length
= cp_parser_expression (parser
);
31831 /* Look for the closing `]'. */
31832 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31836 decl
= tree_cons (low_bound
, length
, decl
);
31843 tree u
= build_omp_clause (token
->location
, kind
);
31844 OMP_CLAUSE_DECL (u
) = decl
;
31845 OMP_CLAUSE_CHAIN (u
) = list
;
31849 list
= tree_cons (decl
, NULL_TREE
, list
);
31852 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31854 cp_lexer_consume_token (parser
->lexer
);
31858 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31860 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31863 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31867 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31871 /* Try to resync to an unnested comma. Copied from
31872 cp_parser_parenthesized_expression_list. */
31875 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31876 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31877 /*recovering=*/true,
31879 /*consume_paren=*/true);
31887 /* Similarly, but expect leading and trailing parenthesis. This is a very
31888 common case for omp clauses. */
31891 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31893 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31894 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31899 copy ( variable-list )
31900 copyin ( variable-list )
31901 copyout ( variable-list )
31902 create ( variable-list )
31903 delete ( variable-list )
31904 present ( variable-list ) */
31907 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31910 enum gomp_map_kind kind
;
31913 case PRAGMA_OACC_CLAUSE_COPY
:
31914 kind
= GOMP_MAP_TOFROM
;
31916 case PRAGMA_OACC_CLAUSE_COPYIN
:
31917 kind
= GOMP_MAP_TO
;
31919 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31920 kind
= GOMP_MAP_FROM
;
31922 case PRAGMA_OACC_CLAUSE_CREATE
:
31923 kind
= GOMP_MAP_ALLOC
;
31925 case PRAGMA_OACC_CLAUSE_DELETE
:
31926 kind
= GOMP_MAP_RELEASE
;
31928 case PRAGMA_OACC_CLAUSE_DEVICE
:
31929 kind
= GOMP_MAP_FORCE_TO
;
31931 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31932 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31934 case PRAGMA_OACC_CLAUSE_HOST
:
31935 kind
= GOMP_MAP_FORCE_FROM
;
31937 case PRAGMA_OACC_CLAUSE_LINK
:
31938 kind
= GOMP_MAP_LINK
;
31940 case PRAGMA_OACC_CLAUSE_PRESENT
:
31941 kind
= GOMP_MAP_FORCE_PRESENT
;
31944 gcc_unreachable ();
31947 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31949 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31950 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31956 deviceptr ( variable-list ) */
31959 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31961 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31964 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31965 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31966 variable-list must only allow for pointer variables. */
31967 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31968 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31970 tree v
= TREE_PURPOSE (t
);
31971 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31972 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31973 OMP_CLAUSE_DECL (u
) = v
;
31974 OMP_CLAUSE_CHAIN (u
) = list
;
31989 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31990 enum omp_clause_code code
,
31991 tree list
, location_t location
)
31993 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31994 tree c
= build_omp_clause (location
, code
);
31995 OMP_CLAUSE_CHAIN (c
) = list
;
32000 num_gangs ( expression )
32001 num_workers ( expression )
32002 vector_length ( expression ) */
32005 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32006 const char *str
, tree list
)
32008 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32010 matching_parens parens
;
32011 if (!parens
.require_open (parser
))
32014 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32016 if (t
== error_mark_node
32017 || !parens
.require_close (parser
))
32019 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32020 /*or_comma=*/false,
32021 /*consume_paren=*/true);
32025 check_no_duplicate_clause (list
, code
, str
, loc
);
32027 tree c
= build_omp_clause (loc
, code
);
32028 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32029 OMP_CLAUSE_CHAIN (c
) = list
;
32035 gang [( gang-arg-list )]
32036 worker [( [num:] int-expr )]
32037 vector [( [length:] int-expr )]
32039 where gang-arg is one of:
32044 and size-expr may be:
32051 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32052 const char *str
, tree list
)
32054 const char *id
= "num";
32055 cp_lexer
*lexer
= parser
->lexer
;
32056 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32057 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32059 if (kind
== OMP_CLAUSE_VECTOR
)
32062 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32064 matching_parens parens
;
32065 parens
.consume_open (parser
);
32069 cp_token
*next
= cp_lexer_peek_token (lexer
);
32072 /* Gang static argument. */
32073 if (kind
== OMP_CLAUSE_GANG
32074 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32076 cp_lexer_consume_token (lexer
);
32078 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32079 goto cleanup_error
;
32082 if (ops
[idx
] != NULL
)
32084 cp_parser_error (parser
, "too many %<static%> arguments");
32085 goto cleanup_error
;
32088 /* Check for the '*' argument. */
32089 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32090 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32091 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32094 cp_lexer_consume_token (lexer
);
32095 ops
[idx
] = integer_minus_one_node
;
32097 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32099 cp_lexer_consume_token (lexer
);
32105 /* Worker num: argument and vector length: arguments. */
32106 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32107 && id_equal (next
->u
.value
, id
)
32108 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32110 cp_lexer_consume_token (lexer
); /* id */
32111 cp_lexer_consume_token (lexer
); /* ':' */
32114 /* Now collect the actual argument. */
32115 if (ops
[idx
] != NULL_TREE
)
32117 cp_parser_error (parser
, "unexpected argument");
32118 goto cleanup_error
;
32121 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32123 if (expr
== error_mark_node
)
32124 goto cleanup_error
;
32126 mark_exp_read (expr
);
32129 if (kind
== OMP_CLAUSE_GANG
32130 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32132 cp_lexer_consume_token (lexer
);
32139 if (!parens
.require_close (parser
))
32140 goto cleanup_error
;
32143 check_no_duplicate_clause (list
, kind
, str
, loc
);
32145 c
= build_omp_clause (loc
, kind
);
32148 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32150 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32151 OMP_CLAUSE_CHAIN (c
) = list
;
32156 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32161 tile ( size-expr-list ) */
32164 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32166 tree c
, expr
= error_mark_node
;
32167 tree tile
= NULL_TREE
;
32169 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32170 so, but the spec authors never considered such a case and have
32171 differing opinions on what it might mean, including 'not
32173 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32174 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32177 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32182 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32186 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32187 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32189 cp_lexer_consume_token (parser
->lexer
);
32190 expr
= integer_zero_node
;
32193 expr
= cp_parser_constant_expression (parser
);
32195 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32197 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32199 /* Consume the trailing ')'. */
32200 cp_lexer_consume_token (parser
->lexer
);
32202 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32203 tile
= nreverse (tile
);
32204 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32205 OMP_CLAUSE_CHAIN (c
) = list
;
32210 Parse wait clause or directive parameters. */
32213 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32215 vec
<tree
, va_gc
> *args
;
32218 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32220 /*allow_expansion_p=*/true,
32221 /*non_constant_p=*/NULL
);
32223 if (args
== NULL
|| args
->length () == 0)
32225 cp_parser_error (parser
, "expected integer expression before ')'");
32227 release_tree_vector (args
);
32231 args_tree
= build_tree_list_vec (args
);
32233 release_tree_vector (args
);
32235 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32237 tree targ
= TREE_VALUE (t
);
32239 if (targ
!= error_mark_node
)
32241 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32242 error ("%<wait%> expression must be integral");
32245 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32247 targ
= mark_rvalue_use (targ
);
32248 OMP_CLAUSE_DECL (c
) = targ
;
32249 OMP_CLAUSE_CHAIN (c
) = list
;
32259 wait ( int-expr-list ) */
32262 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32264 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32266 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32269 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32275 collapse ( constant-expression ) */
32278 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32284 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32285 matching_parens parens
;
32286 if (!parens
.require_open (parser
))
32289 num
= cp_parser_constant_expression (parser
);
32291 if (!parens
.require_close (parser
))
32292 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32293 /*or_comma=*/false,
32294 /*consume_paren=*/true);
32296 if (num
== error_mark_node
)
32298 num
= fold_non_dependent_expr (num
);
32299 if (!tree_fits_shwi_p (num
)
32300 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32301 || (n
= tree_to_shwi (num
)) <= 0
32304 error_at (loc
, "collapse argument needs positive constant integer expression");
32308 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32309 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32310 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32311 OMP_CLAUSE_CHAIN (c
) = list
;
32312 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32318 default ( none | shared )
32321 default ( none | present ) */
32324 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32325 location_t location
, bool is_oacc
)
32327 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32330 matching_parens parens
;
32331 if (!parens
.require_open (parser
))
32333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32335 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32336 const char *p
= IDENTIFIER_POINTER (id
);
32341 if (strcmp ("none", p
) != 0)
32343 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32347 if (strcmp ("present", p
) != 0 || !is_oacc
)
32349 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32353 if (strcmp ("shared", p
) != 0 || is_oacc
)
32355 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32362 cp_lexer_consume_token (parser
->lexer
);
32368 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32370 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32373 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32374 || !parens
.require_close (parser
))
32375 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32376 /*or_comma=*/false,
32377 /*consume_paren=*/true);
32379 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32382 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32383 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32384 OMP_CLAUSE_CHAIN (c
) = list
;
32385 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32391 final ( expression ) */
32394 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32398 matching_parens parens
;
32399 if (!parens
.require_open (parser
))
32402 t
= cp_parser_condition (parser
);
32404 if (t
== error_mark_node
32405 || !parens
.require_close (parser
))
32406 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32407 /*or_comma=*/false,
32408 /*consume_paren=*/true);
32410 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32412 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32413 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32414 OMP_CLAUSE_CHAIN (c
) = list
;
32423 if ( directive-name-modifier : expression )
32425 directive-name-modifier:
32426 parallel | task | taskloop | target data | target | target update
32427 | target enter data | target exit data */
32430 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32434 enum tree_code if_modifier
= ERROR_MARK
;
32436 matching_parens parens
;
32437 if (!parens
.require_open (parser
))
32440 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32442 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32443 const char *p
= IDENTIFIER_POINTER (id
);
32446 if (strcmp ("parallel", p
) == 0)
32447 if_modifier
= OMP_PARALLEL
;
32448 else if (strcmp ("task", p
) == 0)
32449 if_modifier
= OMP_TASK
;
32450 else if (strcmp ("taskloop", p
) == 0)
32451 if_modifier
= OMP_TASKLOOP
;
32452 else if (strcmp ("target", p
) == 0)
32454 if_modifier
= OMP_TARGET
;
32455 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32457 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32458 p
= IDENTIFIER_POINTER (id
);
32459 if (strcmp ("data", p
) == 0)
32460 if_modifier
= OMP_TARGET_DATA
;
32461 else if (strcmp ("update", p
) == 0)
32462 if_modifier
= OMP_TARGET_UPDATE
;
32463 else if (strcmp ("enter", p
) == 0)
32464 if_modifier
= OMP_TARGET_ENTER_DATA
;
32465 else if (strcmp ("exit", p
) == 0)
32466 if_modifier
= OMP_TARGET_EXIT_DATA
;
32467 if (if_modifier
!= OMP_TARGET
)
32472 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32473 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32475 if_modifier
= ERROR_MARK
;
32477 if (if_modifier
== OMP_TARGET_ENTER_DATA
32478 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32480 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32482 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32483 p
= IDENTIFIER_POINTER (id
);
32484 if (strcmp ("data", p
) == 0)
32490 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32491 error_at (loc
, "expected %<data%>");
32492 if_modifier
= ERROR_MARK
;
32497 if (if_modifier
!= ERROR_MARK
)
32499 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32502 cp_lexer_consume_token (parser
->lexer
);
32509 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32510 error_at (loc
, "expected %<:%>");
32512 if_modifier
= ERROR_MARK
;
32517 t
= cp_parser_condition (parser
);
32519 if (t
== error_mark_node
32520 || !parens
.require_close (parser
))
32521 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32522 /*or_comma=*/false,
32523 /*consume_paren=*/true);
32525 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32526 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32528 if (if_modifier
!= ERROR_MARK
32529 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32531 const char *p
= NULL
;
32532 switch (if_modifier
)
32534 case OMP_PARALLEL
: p
= "parallel"; break;
32535 case OMP_TASK
: p
= "task"; break;
32536 case OMP_TASKLOOP
: p
= "taskloop"; break;
32537 case OMP_TARGET_DATA
: p
= "target data"; break;
32538 case OMP_TARGET
: p
= "target"; break;
32539 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32540 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32541 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32542 default: gcc_unreachable ();
32544 error_at (location
, "too many %<if%> clauses with %qs modifier",
32548 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32551 error_at (location
, "too many %<if%> clauses");
32553 error_at (location
, "too many %<if%> clauses without modifier");
32556 else if (if_modifier
== ERROR_MARK
32557 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32559 error_at (location
, "if any %<if%> clause has modifier, then all "
32560 "%<if%> clauses have to use modifier");
32565 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32566 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32567 OMP_CLAUSE_IF_EXPR (c
) = t
;
32568 OMP_CLAUSE_CHAIN (c
) = list
;
32577 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32578 tree list
, location_t location
)
32582 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32585 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32586 OMP_CLAUSE_CHAIN (c
) = list
;
32594 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32595 tree list
, location_t location
)
32599 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32601 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32602 OMP_CLAUSE_CHAIN (c
) = list
;
32607 num_threads ( expression ) */
32610 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32611 location_t location
)
32615 matching_parens parens
;
32616 if (!parens
.require_open (parser
))
32619 t
= cp_parser_expression (parser
);
32621 if (t
== error_mark_node
32622 || !parens
.require_close (parser
))
32623 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32624 /*or_comma=*/false,
32625 /*consume_paren=*/true);
32627 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32628 "num_threads", location
);
32630 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32631 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32632 OMP_CLAUSE_CHAIN (c
) = list
;
32638 num_tasks ( expression ) */
32641 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32642 location_t location
)
32646 matching_parens parens
;
32647 if (!parens
.require_open (parser
))
32650 t
= cp_parser_expression (parser
);
32652 if (t
== error_mark_node
32653 || !parens
.require_close (parser
))
32654 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32655 /*or_comma=*/false,
32656 /*consume_paren=*/true);
32658 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32659 "num_tasks", location
);
32661 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32662 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32663 OMP_CLAUSE_CHAIN (c
) = list
;
32669 grainsize ( expression ) */
32672 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32673 location_t location
)
32677 matching_parens parens
;
32678 if (!parens
.require_open (parser
))
32681 t
= cp_parser_expression (parser
);
32683 if (t
== error_mark_node
32684 || !parens
.require_close (parser
))
32685 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32686 /*or_comma=*/false,
32687 /*consume_paren=*/true);
32689 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32690 "grainsize", location
);
32692 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32693 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32694 OMP_CLAUSE_CHAIN (c
) = list
;
32700 priority ( expression ) */
32703 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32704 location_t location
)
32708 matching_parens parens
;
32709 if (!parens
.require_open (parser
))
32712 t
= cp_parser_expression (parser
);
32714 if (t
== error_mark_node
32715 || !parens
.require_close (parser
))
32716 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32717 /*or_comma=*/false,
32718 /*consume_paren=*/true);
32720 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32721 "priority", location
);
32723 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32724 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32725 OMP_CLAUSE_CHAIN (c
) = list
;
32731 hint ( expression ) */
32734 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32735 location_t location
)
32739 matching_parens parens
;
32740 if (!parens
.require_open (parser
))
32743 t
= cp_parser_expression (parser
);
32745 if (t
== error_mark_node
32746 || !parens
.require_close (parser
))
32747 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32748 /*or_comma=*/false,
32749 /*consume_paren=*/true);
32751 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32753 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32754 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32755 OMP_CLAUSE_CHAIN (c
) = list
;
32761 defaultmap ( tofrom : scalar ) */
32764 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32765 location_t location
)
32770 matching_parens parens
;
32771 if (!parens
.require_open (parser
))
32774 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32776 cp_parser_error (parser
, "expected %<tofrom%>");
32779 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32780 p
= IDENTIFIER_POINTER (id
);
32781 if (strcmp (p
, "tofrom") != 0)
32783 cp_parser_error (parser
, "expected %<tofrom%>");
32786 cp_lexer_consume_token (parser
->lexer
);
32787 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32790 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32792 cp_parser_error (parser
, "expected %<scalar%>");
32795 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32796 p
= IDENTIFIER_POINTER (id
);
32797 if (strcmp (p
, "scalar") != 0)
32799 cp_parser_error (parser
, "expected %<scalar%>");
32802 cp_lexer_consume_token (parser
->lexer
);
32803 if (!parens
.require_close (parser
))
32806 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32809 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32810 OMP_CLAUSE_CHAIN (c
) = list
;
32814 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32815 /*or_comma=*/false,
32816 /*consume_paren=*/true);
32824 ordered ( constant-expression ) */
32827 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32828 tree list
, location_t location
)
32830 tree c
, num
= NULL_TREE
;
32833 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32834 "ordered", location
);
32836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32838 matching_parens parens
;
32839 parens
.consume_open (parser
);
32841 num
= cp_parser_constant_expression (parser
);
32843 if (!parens
.require_close (parser
))
32844 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32845 /*or_comma=*/false,
32846 /*consume_paren=*/true);
32848 if (num
== error_mark_node
)
32850 num
= fold_non_dependent_expr (num
);
32851 if (!tree_fits_shwi_p (num
)
32852 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32853 || (n
= tree_to_shwi (num
)) <= 0
32856 error_at (location
,
32857 "ordered argument needs positive constant integer "
32863 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32864 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32865 OMP_CLAUSE_CHAIN (c
) = list
;
32870 reduction ( reduction-operator : variable-list )
32872 reduction-operator:
32873 One of: + * - & ^ | && ||
32877 reduction-operator:
32878 One of: + * - & ^ | && || min max
32882 reduction-operator:
32883 One of: + * - & ^ | && ||
32887 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32889 enum tree_code code
= ERROR_MARK
;
32890 tree nlist
, c
, id
= NULL_TREE
;
32892 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32895 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32897 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32898 case CPP_MULT
: code
= MULT_EXPR
; break;
32899 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32900 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32901 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32902 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32903 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32904 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32908 if (code
!= ERROR_MARK
)
32909 cp_lexer_consume_token (parser
->lexer
);
32912 bool saved_colon_corrects_to_scope_p
;
32913 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32914 parser
->colon_corrects_to_scope_p
= false;
32915 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32916 /*check_dependency_p=*/true,
32917 /*template_p=*/NULL
,
32918 /*declarator_p=*/false,
32919 /*optional_p=*/false);
32920 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32921 if (identifier_p (id
))
32923 const char *p
= IDENTIFIER_POINTER (id
);
32925 if (strcmp (p
, "min") == 0)
32927 else if (strcmp (p
, "max") == 0)
32929 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32931 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32933 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32935 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32936 code
= BIT_AND_EXPR
;
32937 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32938 code
= BIT_IOR_EXPR
;
32939 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32940 code
= BIT_XOR_EXPR
;
32941 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32942 code
= TRUTH_ANDIF_EXPR
;
32943 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32944 code
= TRUTH_ORIF_EXPR
;
32945 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32946 tree scope
= parser
->scope
;
32948 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32949 parser
->scope
= NULL_TREE
;
32950 parser
->qualifying_scope
= NULL_TREE
;
32951 parser
->object_scope
= NULL_TREE
;
32955 error ("invalid reduction-identifier");
32957 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32958 /*or_comma=*/false,
32959 /*consume_paren=*/true);
32964 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32967 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32969 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32971 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32972 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32979 schedule ( schedule-kind )
32980 schedule ( schedule-kind , expression )
32983 static | dynamic | guided | runtime | auto
32986 schedule ( schedule-modifier : schedule-kind )
32987 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32995 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32998 int modifiers
= 0, nmodifiers
= 0;
33000 matching_parens parens
;
33001 if (!parens
.require_open (parser
))
33004 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33006 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33008 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33009 const char *p
= IDENTIFIER_POINTER (id
);
33010 if (strcmp ("simd", p
) == 0)
33011 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33012 else if (strcmp ("monotonic", p
) == 0)
33013 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33014 else if (strcmp ("nonmonotonic", p
) == 0)
33015 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33018 cp_lexer_consume_token (parser
->lexer
);
33019 if (nmodifiers
++ == 0
33020 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33021 cp_lexer_consume_token (parser
->lexer
);
33024 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33031 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33032 const char *p
= IDENTIFIER_POINTER (id
);
33037 if (strcmp ("dynamic", p
) != 0)
33039 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33043 if (strcmp ("guided", p
) != 0)
33045 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33049 if (strcmp ("runtime", p
) != 0)
33051 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33058 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33059 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33060 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33061 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33064 cp_lexer_consume_token (parser
->lexer
);
33066 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33067 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33068 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33069 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33071 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33079 cp_lexer_consume_token (parser
->lexer
);
33081 token
= cp_lexer_peek_token (parser
->lexer
);
33082 t
= cp_parser_assignment_expression (parser
);
33084 if (t
== error_mark_node
)
33086 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33087 error_at (token
->location
, "schedule %<runtime%> does not take "
33088 "a %<chunk_size%> parameter");
33089 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33090 error_at (token
->location
, "schedule %<auto%> does not take "
33091 "a %<chunk_size%> parameter");
33093 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33095 if (!parens
.require_close (parser
))
33098 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33101 OMP_CLAUSE_SCHEDULE_KIND (c
)
33102 = (enum omp_clause_schedule_kind
)
33103 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33105 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33106 OMP_CLAUSE_CHAIN (c
) = list
;
33110 cp_parser_error (parser
, "invalid schedule kind");
33112 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33113 /*or_comma=*/false,
33114 /*consume_paren=*/true);
33122 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33123 tree list
, location_t location
)
33127 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33129 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33130 OMP_CLAUSE_CHAIN (c
) = list
;
33139 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33140 tree list
, location_t location
)
33142 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33143 tree c
= build_omp_clause (location
, code
);
33144 OMP_CLAUSE_CHAIN (c
) = list
;
33155 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33156 enum omp_clause_code code
,
33157 tree list
, location_t location
)
33159 tree c
= build_omp_clause (location
, code
);
33160 OMP_CLAUSE_CHAIN (c
) = list
;
33168 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33169 tree list
, location_t location
)
33171 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33172 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33173 OMP_CLAUSE_CHAIN (c
) = list
;
33182 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33183 enum omp_clause_code code
,
33184 tree list
, location_t location
)
33186 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33187 tree c
= build_omp_clause (location
, code
);
33188 OMP_CLAUSE_CHAIN (c
) = list
;
33193 num_teams ( expression ) */
33196 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33197 location_t location
)
33201 matching_parens parens
;
33202 if (!parens
.require_open (parser
))
33205 t
= cp_parser_expression (parser
);
33207 if (t
== error_mark_node
33208 || !parens
.require_close (parser
))
33209 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33210 /*or_comma=*/false,
33211 /*consume_paren=*/true);
33213 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33214 "num_teams", location
);
33216 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33217 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33218 OMP_CLAUSE_CHAIN (c
) = list
;
33224 thread_limit ( expression ) */
33227 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33228 location_t location
)
33232 matching_parens parens
;
33233 if (!parens
.require_open (parser
))
33236 t
= cp_parser_expression (parser
);
33238 if (t
== error_mark_node
33239 || !parens
.require_close (parser
))
33240 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33241 /*or_comma=*/false,
33242 /*consume_paren=*/true);
33244 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33245 "thread_limit", location
);
33247 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33248 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33249 OMP_CLAUSE_CHAIN (c
) = list
;
33255 aligned ( variable-list )
33256 aligned ( variable-list : constant-expression ) */
33259 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33261 tree nlist
, c
, alignment
= NULL_TREE
;
33264 matching_parens parens
;
33265 if (!parens
.require_open (parser
))
33268 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33273 alignment
= cp_parser_constant_expression (parser
);
33275 if (!parens
.require_close (parser
))
33276 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33280 if (alignment
== error_mark_node
)
33281 alignment
= NULL_TREE
;
33284 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33285 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33291 linear ( variable-list )
33292 linear ( variable-list : expression )
33295 linear ( modifier ( variable-list ) )
33296 linear ( modifier ( variable-list ) : expression ) */
33299 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33302 tree nlist
, c
, step
= integer_one_node
;
33304 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33306 matching_parens parens
;
33307 if (!parens
.require_open (parser
))
33310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33312 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33313 const char *p
= IDENTIFIER_POINTER (id
);
33315 if (strcmp ("ref", p
) == 0)
33316 kind
= OMP_CLAUSE_LINEAR_REF
;
33317 else if (strcmp ("val", p
) == 0)
33318 kind
= OMP_CLAUSE_LINEAR_VAL
;
33319 else if (strcmp ("uval", p
) == 0)
33320 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33321 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33322 cp_lexer_consume_token (parser
->lexer
);
33324 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33327 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33328 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33332 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33333 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33335 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33336 else if (!parens
.require_close (parser
))
33337 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33338 /*or_comma=*/false,
33339 /*consume_paren=*/true);
33346 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33347 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33349 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33350 cp_parser_parse_tentatively (parser
);
33351 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33352 /*check_dependency_p=*/true,
33353 /*template_p=*/NULL
,
33354 /*declarator_p=*/false,
33355 /*optional_p=*/false);
33356 if (step
!= error_mark_node
)
33357 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33358 if (step
== error_mark_node
)
33361 cp_parser_abort_tentative_parse (parser
);
33363 else if (!cp_parser_parse_definitely (parser
))
33367 step
= cp_parser_expression (parser
);
33369 if (!parens
.require_close (parser
))
33370 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33371 /*or_comma=*/false,
33372 /*consume_paren=*/true);
33374 if (step
== error_mark_node
)
33378 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33380 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33381 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33388 safelen ( constant-expression ) */
33391 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33392 location_t location
)
33396 matching_parens parens
;
33397 if (!parens
.require_open (parser
))
33400 t
= cp_parser_constant_expression (parser
);
33402 if (t
== error_mark_node
33403 || !parens
.require_close (parser
))
33404 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33405 /*or_comma=*/false,
33406 /*consume_paren=*/true);
33408 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33410 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33411 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33412 OMP_CLAUSE_CHAIN (c
) = list
;
33418 simdlen ( constant-expression ) */
33421 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33422 location_t location
)
33426 matching_parens parens
;
33427 if (!parens
.require_open (parser
))
33430 t
= cp_parser_constant_expression (parser
);
33432 if (t
== error_mark_node
33433 || !parens
.require_close (parser
))
33434 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33435 /*or_comma=*/false,
33436 /*consume_paren=*/true);
33438 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33440 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33441 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33442 OMP_CLAUSE_CHAIN (c
) = list
;
33449 identifier [+/- integer]
33450 vec , identifier [+/- integer]
33454 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33459 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33461 cp_parser_error (parser
, "expected identifier");
33465 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33467 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33468 tree t
, identifier
= cp_parser_identifier (parser
);
33469 tree addend
= NULL
;
33471 if (identifier
== error_mark_node
)
33472 t
= error_mark_node
;
33475 t
= cp_parser_lookup_name_simple
33476 (parser
, identifier
,
33477 cp_lexer_peek_token (parser
->lexer
)->location
);
33478 if (t
== error_mark_node
)
33479 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33486 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33488 addend
= integer_zero_node
;
33489 goto add_to_vector
;
33491 cp_lexer_consume_token (parser
->lexer
);
33493 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33495 cp_parser_error (parser
, "expected integer");
33499 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33500 if (TREE_CODE (addend
) != INTEGER_CST
)
33502 cp_parser_error (parser
, "expected integer");
33505 cp_lexer_consume_token (parser
->lexer
);
33508 if (t
!= error_mark_node
)
33510 vec
= tree_cons (addend
, t
, vec
);
33512 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33515 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33518 cp_lexer_consume_token (parser
->lexer
);
33521 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33523 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33524 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33525 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33526 OMP_CLAUSE_CHAIN (u
) = list
;
33533 depend ( depend-kind : variable-list )
33541 depend ( sink : vec ) */
33544 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33547 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33549 matching_parens parens
;
33550 if (!parens
.require_open (parser
))
33553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33555 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33556 const char *p
= IDENTIFIER_POINTER (id
);
33558 if (strcmp ("in", p
) == 0)
33559 kind
= OMP_CLAUSE_DEPEND_IN
;
33560 else if (strcmp ("inout", p
) == 0)
33561 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33562 else if (strcmp ("out", p
) == 0)
33563 kind
= OMP_CLAUSE_DEPEND_OUT
;
33564 else if (strcmp ("source", p
) == 0)
33565 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33566 else if (strcmp ("sink", p
) == 0)
33567 kind
= OMP_CLAUSE_DEPEND_SINK
;
33574 cp_lexer_consume_token (parser
->lexer
);
33576 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33578 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33579 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33580 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33581 OMP_CLAUSE_CHAIN (c
) = list
;
33582 if (!parens
.require_close (parser
))
33583 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33584 /*or_comma=*/false,
33585 /*consume_paren=*/true);
33589 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33592 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33593 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33596 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33599 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33600 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33605 cp_parser_error (parser
, "invalid depend kind");
33607 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33608 /*or_comma=*/false,
33609 /*consume_paren=*/true);
33614 map ( map-kind : variable-list )
33615 map ( variable-list )
33618 alloc | to | from | tofrom
33622 alloc | to | from | tofrom | release | delete
33624 map ( always [,] map-kind: variable-list ) */
33627 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33630 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33631 bool always
= false;
33633 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33638 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33639 const char *p
= IDENTIFIER_POINTER (id
);
33641 if (strcmp ("always", p
) == 0)
33644 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33646 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33647 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33649 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33653 cp_lexer_consume_token (parser
->lexer
);
33655 cp_lexer_consume_token (parser
->lexer
);
33660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33661 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33663 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33664 const char *p
= IDENTIFIER_POINTER (id
);
33666 if (strcmp ("alloc", p
) == 0)
33667 kind
= GOMP_MAP_ALLOC
;
33668 else if (strcmp ("to", p
) == 0)
33669 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33670 else if (strcmp ("from", p
) == 0)
33671 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33672 else if (strcmp ("tofrom", p
) == 0)
33673 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33674 else if (strcmp ("release", p
) == 0)
33675 kind
= GOMP_MAP_RELEASE
;
33678 cp_parser_error (parser
, "invalid map kind");
33679 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33680 /*or_comma=*/false,
33681 /*consume_paren=*/true);
33684 cp_lexer_consume_token (parser
->lexer
);
33685 cp_lexer_consume_token (parser
->lexer
);
33687 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33688 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33690 kind
= GOMP_MAP_DELETE
;
33691 cp_lexer_consume_token (parser
->lexer
);
33692 cp_lexer_consume_token (parser
->lexer
);
33695 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33698 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33699 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33705 device ( expression ) */
33708 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33709 location_t location
)
33713 matching_parens parens
;
33714 if (!parens
.require_open (parser
))
33717 t
= cp_parser_expression (parser
);
33719 if (t
== error_mark_node
33720 || !parens
.require_close (parser
))
33721 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33722 /*or_comma=*/false,
33723 /*consume_paren=*/true);
33725 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33726 "device", location
);
33728 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33729 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33730 OMP_CLAUSE_CHAIN (c
) = list
;
33736 dist_schedule ( static )
33737 dist_schedule ( static , expression ) */
33740 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33741 location_t location
)
33745 matching_parens parens
;
33746 if (!parens
.require_open (parser
))
33749 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33751 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33753 cp_lexer_consume_token (parser
->lexer
);
33755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33757 cp_lexer_consume_token (parser
->lexer
);
33759 t
= cp_parser_assignment_expression (parser
);
33761 if (t
== error_mark_node
)
33763 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33765 if (!parens
.require_close (parser
))
33768 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33771 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33773 OMP_CLAUSE_CHAIN (c
) = list
;
33777 cp_parser_error (parser
, "invalid dist_schedule kind");
33779 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33780 /*or_comma=*/false,
33781 /*consume_paren=*/true);
33786 proc_bind ( proc-bind-kind )
33789 master | close | spread */
33792 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33793 location_t location
)
33796 enum omp_clause_proc_bind_kind kind
;
33798 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33803 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33804 const char *p
= IDENTIFIER_POINTER (id
);
33806 if (strcmp ("master", p
) == 0)
33807 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33808 else if (strcmp ("close", p
) == 0)
33809 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33810 else if (strcmp ("spread", p
) == 0)
33811 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33818 cp_lexer_consume_token (parser
->lexer
);
33819 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33822 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33823 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33825 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33826 OMP_CLAUSE_CHAIN (c
) = list
;
33830 cp_parser_error (parser
, "invalid depend kind");
33832 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33833 /*or_comma=*/false,
33834 /*consume_paren=*/true);
33839 async [( int-expr )] */
33842 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33845 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33847 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33849 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33851 matching_parens parens
;
33852 parens
.consume_open (parser
);
33854 t
= cp_parser_expression (parser
);
33855 if (t
== error_mark_node
33856 || !parens
.require_close (parser
))
33857 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33858 /*or_comma=*/false,
33859 /*consume_paren=*/true);
33862 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33864 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33865 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33866 OMP_CLAUSE_CHAIN (c
) = list
;
33872 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33873 is a bitmask in MASK. Return the list of clauses found. */
33876 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33877 const char *where
, cp_token
*pragma_tok
,
33878 bool finish_p
= true)
33880 tree clauses
= NULL
;
33883 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33886 pragma_omp_clause c_kind
;
33887 omp_clause_code code
;
33888 const char *c_name
;
33889 tree prev
= clauses
;
33891 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33892 cp_lexer_consume_token (parser
->lexer
);
33894 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33895 c_kind
= cp_parser_omp_clause_name (parser
);
33899 case PRAGMA_OACC_CLAUSE_ASYNC
:
33900 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33903 case PRAGMA_OACC_CLAUSE_AUTO
:
33904 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33908 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33909 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33910 c_name
= "collapse";
33912 case PRAGMA_OACC_CLAUSE_COPY
:
33913 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33916 case PRAGMA_OACC_CLAUSE_COPYIN
:
33917 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33920 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33921 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33922 c_name
= "copyout";
33924 case PRAGMA_OACC_CLAUSE_CREATE
:
33925 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33928 case PRAGMA_OACC_CLAUSE_DELETE
:
33929 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33932 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33933 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33934 c_name
= "default";
33936 case PRAGMA_OACC_CLAUSE_DEVICE
:
33937 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33940 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33941 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33942 c_name
= "deviceptr";
33944 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33945 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33946 c_name
= "device_resident";
33948 case PRAGMA_OACC_CLAUSE_FINALIZE
:
33949 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
33951 c_name
= "finalize";
33953 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33954 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33956 c_name
= "firstprivate";
33958 case PRAGMA_OACC_CLAUSE_GANG
:
33960 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33963 case PRAGMA_OACC_CLAUSE_HOST
:
33964 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33967 case PRAGMA_OACC_CLAUSE_IF
:
33968 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33971 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
33972 clauses
= cp_parser_oacc_simple_clause (parser
,
33973 OMP_CLAUSE_IF_PRESENT
,
33975 c_name
= "if_present";
33977 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33978 clauses
= cp_parser_oacc_simple_clause (parser
,
33979 OMP_CLAUSE_INDEPENDENT
,
33981 c_name
= "independent";
33983 case PRAGMA_OACC_CLAUSE_LINK
:
33984 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33987 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33988 code
= OMP_CLAUSE_NUM_GANGS
;
33989 c_name
= "num_gangs";
33990 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33993 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33994 c_name
= "num_workers";
33995 code
= OMP_CLAUSE_NUM_WORKERS
;
33996 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33999 case PRAGMA_OACC_CLAUSE_PRESENT
:
34000 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34001 c_name
= "present";
34003 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34004 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34006 c_name
= "private";
34008 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34009 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34010 c_name
= "reduction";
34012 case PRAGMA_OACC_CLAUSE_SEQ
:
34013 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34017 case PRAGMA_OACC_CLAUSE_TILE
:
34018 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34021 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34022 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34024 c_name
= "use_device";
34026 case PRAGMA_OACC_CLAUSE_VECTOR
:
34028 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34031 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34032 c_name
= "vector_length";
34033 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34034 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34037 case PRAGMA_OACC_CLAUSE_WAIT
:
34038 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34041 case PRAGMA_OACC_CLAUSE_WORKER
:
34043 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34047 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34053 if (((mask
>> c_kind
) & 1) == 0)
34055 /* Remove the invalid clause(s) from the list to avoid
34056 confusing the rest of the compiler. */
34058 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34063 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34066 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34071 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34072 is a bitmask in MASK. Return the list of clauses found; the result
34073 of clause default goes in *pdefault. */
34076 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34077 const char *where
, cp_token
*pragma_tok
,
34078 bool finish_p
= true)
34080 tree clauses
= NULL
;
34082 cp_token
*token
= NULL
;
34084 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34086 pragma_omp_clause c_kind
;
34087 const char *c_name
;
34088 tree prev
= clauses
;
34090 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34091 cp_lexer_consume_token (parser
->lexer
);
34093 token
= cp_lexer_peek_token (parser
->lexer
);
34094 c_kind
= cp_parser_omp_clause_name (parser
);
34098 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
34099 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
34101 c_name
= "collapse";
34103 case PRAGMA_OMP_CLAUSE_COPYIN
:
34104 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
34107 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
34108 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
34110 c_name
= "copyprivate";
34112 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34113 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
34114 token
->location
, false);
34115 c_name
= "default";
34117 case PRAGMA_OMP_CLAUSE_FINAL
:
34118 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
34121 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
34122 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34124 c_name
= "firstprivate";
34126 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34127 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34129 c_name
= "grainsize";
34131 case PRAGMA_OMP_CLAUSE_HINT
:
34132 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34136 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34137 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34139 c_name
= "defaultmap";
34141 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34142 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34144 c_name
= "use_device_ptr";
34146 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34147 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34149 c_name
= "is_device_ptr";
34151 case PRAGMA_OMP_CLAUSE_IF
:
34152 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34156 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34157 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34159 c_name
= "lastprivate";
34161 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34162 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34164 c_name
= "mergeable";
34166 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34167 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34170 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34171 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34173 c_name
= "num_tasks";
34175 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34176 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34178 c_name
= "num_threads";
34180 case PRAGMA_OMP_CLAUSE_ORDERED
:
34181 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34183 c_name
= "ordered";
34185 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34186 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34188 c_name
= "priority";
34190 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34191 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34193 c_name
= "private";
34195 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34196 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34197 c_name
= "reduction";
34199 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34200 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34202 c_name
= "schedule";
34204 case PRAGMA_OMP_CLAUSE_SHARED
:
34205 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34209 case PRAGMA_OMP_CLAUSE_UNTIED
:
34210 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34214 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34215 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34216 clauses
, token
->location
);
34217 c_name
= "inbranch";
34219 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34220 clauses
= cp_parser_omp_clause_branch (parser
,
34221 OMP_CLAUSE_NOTINBRANCH
,
34222 clauses
, token
->location
);
34223 c_name
= "notinbranch";
34225 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34226 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34227 clauses
, token
->location
);
34228 c_name
= "parallel";
34232 error_at (token
->location
, "%qs must be the first clause of %qs",
34237 case PRAGMA_OMP_CLAUSE_FOR
:
34238 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34239 clauses
, token
->location
);
34242 goto clause_not_first
;
34244 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34245 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34246 clauses
, token
->location
);
34247 c_name
= "sections";
34249 goto clause_not_first
;
34251 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34252 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34253 clauses
, token
->location
);
34254 c_name
= "taskgroup";
34256 goto clause_not_first
;
34258 case PRAGMA_OMP_CLAUSE_LINK
:
34259 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34262 case PRAGMA_OMP_CLAUSE_TO
:
34263 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34264 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34267 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34270 case PRAGMA_OMP_CLAUSE_FROM
:
34271 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34274 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34275 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34277 c_name
= "uniform";
34279 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34280 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34282 c_name
= "num_teams";
34284 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34285 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34287 c_name
= "thread_limit";
34289 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34290 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34291 c_name
= "aligned";
34293 case PRAGMA_OMP_CLAUSE_LINEAR
:
34295 bool declare_simd
= false;
34296 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34297 declare_simd
= true;
34298 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34302 case PRAGMA_OMP_CLAUSE_DEPEND
:
34303 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34307 case PRAGMA_OMP_CLAUSE_MAP
:
34308 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34311 case PRAGMA_OMP_CLAUSE_DEVICE
:
34312 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34316 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34317 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34319 c_name
= "dist_schedule";
34321 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34322 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34324 c_name
= "proc_bind";
34326 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34327 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34329 c_name
= "safelen";
34331 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34332 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34334 c_name
= "simdlen";
34336 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34337 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34339 c_name
= "nogroup";
34341 case PRAGMA_OMP_CLAUSE_THREADS
:
34343 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34344 clauses
, token
->location
);
34345 c_name
= "threads";
34347 case PRAGMA_OMP_CLAUSE_SIMD
:
34349 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34350 clauses
, token
->location
);
34354 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34360 if (((mask
>> c_kind
) & 1) == 0)
34362 /* Remove the invalid clause(s) from the list to avoid
34363 confusing the rest of the compiler. */
34365 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34369 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34372 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34373 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34375 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34384 In practice, we're also interested in adding the statement to an
34385 outer node. So it is convenient if we work around the fact that
34386 cp_parser_statement calls add_stmt. */
34389 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34391 unsigned save
= parser
->in_statement
;
34393 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34394 This preserves the "not within loop or switch" style error messages
34395 for nonsense cases like
34401 if (parser
->in_statement
)
34402 parser
->in_statement
= IN_OMP_BLOCK
;
34408 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34410 parser
->in_statement
= save
;
34414 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34416 tree stmt
= begin_omp_structured_block ();
34417 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34419 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34421 cp_parser_end_omp_structured_block (parser
, save
);
34422 return finish_omp_structured_block (stmt
);
34426 # pragma omp atomic new-line
34430 x binop= expr | x++ | ++x | x-- | --x
34432 +, *, -, /, &, ^, |, <<, >>
34434 where x is an lvalue expression with scalar type.
34437 # pragma omp atomic new-line
34440 # pragma omp atomic read new-line
34443 # pragma omp atomic write new-line
34446 # pragma omp atomic update new-line
34449 # pragma omp atomic capture new-line
34452 # pragma omp atomic capture new-line
34460 expression-stmt | x = x binop expr
34462 v = expression-stmt
34464 { v = x; update-stmt; } | { update-stmt; v = x; }
34468 expression-stmt | x = x binop expr | x = expr binop x
34472 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34474 where x and v are lvalue expressions with scalar type. */
34477 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34479 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34480 tree rhs1
= NULL_TREE
, orig_lhs
;
34481 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34482 bool structured_block
= false;
34483 bool seq_cst
= false;
34485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34487 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34488 const char *p
= IDENTIFIER_POINTER (id
);
34490 if (!strcmp (p
, "seq_cst"))
34493 cp_lexer_consume_token (parser
->lexer
);
34494 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34495 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34496 cp_lexer_consume_token (parser
->lexer
);
34499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34501 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34502 const char *p
= IDENTIFIER_POINTER (id
);
34504 if (!strcmp (p
, "read"))
34505 code
= OMP_ATOMIC_READ
;
34506 else if (!strcmp (p
, "write"))
34508 else if (!strcmp (p
, "update"))
34510 else if (!strcmp (p
, "capture"))
34511 code
= OMP_ATOMIC_CAPTURE_NEW
;
34515 cp_lexer_consume_token (parser
->lexer
);
34519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34520 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34521 cp_lexer_consume_token (parser
->lexer
);
34523 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34525 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34526 const char *p
= IDENTIFIER_POINTER (id
);
34528 if (!strcmp (p
, "seq_cst"))
34531 cp_lexer_consume_token (parser
->lexer
);
34535 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34539 case OMP_ATOMIC_READ
:
34540 case NOP_EXPR
: /* atomic write */
34541 v
= cp_parser_unary_expression (parser
);
34542 if (v
== error_mark_node
)
34544 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34546 if (code
== NOP_EXPR
)
34547 lhs
= cp_parser_expression (parser
);
34549 lhs
= cp_parser_unary_expression (parser
);
34550 if (lhs
== error_mark_node
)
34552 if (code
== NOP_EXPR
)
34554 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34562 case OMP_ATOMIC_CAPTURE_NEW
:
34563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34565 cp_lexer_consume_token (parser
->lexer
);
34566 structured_block
= true;
34570 v
= cp_parser_unary_expression (parser
);
34571 if (v
== error_mark_node
)
34573 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34581 lhs
= cp_parser_unary_expression (parser
);
34583 switch (TREE_CODE (lhs
))
34588 case POSTINCREMENT_EXPR
:
34589 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34590 code
= OMP_ATOMIC_CAPTURE_OLD
;
34592 case PREINCREMENT_EXPR
:
34593 lhs
= TREE_OPERAND (lhs
, 0);
34594 opcode
= PLUS_EXPR
;
34595 rhs
= integer_one_node
;
34598 case POSTDECREMENT_EXPR
:
34599 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34600 code
= OMP_ATOMIC_CAPTURE_OLD
;
34602 case PREDECREMENT_EXPR
:
34603 lhs
= TREE_OPERAND (lhs
, 0);
34604 opcode
= MINUS_EXPR
;
34605 rhs
= integer_one_node
;
34608 case COMPOUND_EXPR
:
34609 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34610 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34611 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34612 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34613 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34614 (TREE_OPERAND (lhs
, 1), 0), 0)))
34616 /* Undo effects of boolean_increment for post {in,de}crement. */
34617 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34620 if (TREE_CODE (lhs
) == MODIFY_EXPR
34621 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34623 /* Undo effects of boolean_increment. */
34624 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34626 /* This is pre or post increment. */
34627 rhs
= TREE_OPERAND (lhs
, 1);
34628 lhs
= TREE_OPERAND (lhs
, 0);
34630 if (code
== OMP_ATOMIC_CAPTURE_NEW
34631 && !structured_block
34632 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34633 code
= OMP_ATOMIC_CAPTURE_OLD
;
34639 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34642 opcode
= MULT_EXPR
;
34645 opcode
= TRUNC_DIV_EXPR
;
34648 opcode
= PLUS_EXPR
;
34651 opcode
= MINUS_EXPR
;
34653 case CPP_LSHIFT_EQ
:
34654 opcode
= LSHIFT_EXPR
;
34656 case CPP_RSHIFT_EQ
:
34657 opcode
= RSHIFT_EXPR
;
34660 opcode
= BIT_AND_EXPR
;
34663 opcode
= BIT_IOR_EXPR
;
34666 opcode
= BIT_XOR_EXPR
;
34669 enum cp_parser_prec oprec
;
34671 cp_lexer_consume_token (parser
->lexer
);
34672 cp_parser_parse_tentatively (parser
);
34673 rhs1
= cp_parser_simple_cast_expression (parser
);
34674 if (rhs1
== error_mark_node
)
34676 cp_parser_abort_tentative_parse (parser
);
34677 cp_parser_simple_cast_expression (parser
);
34680 token
= cp_lexer_peek_token (parser
->lexer
);
34681 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34683 cp_parser_abort_tentative_parse (parser
);
34684 cp_parser_parse_tentatively (parser
);
34685 rhs
= cp_parser_binary_expression (parser
, false, true,
34686 PREC_NOT_OPERATOR
, NULL
);
34687 if (rhs
== error_mark_node
)
34689 cp_parser_abort_tentative_parse (parser
);
34690 cp_parser_binary_expression (parser
, false, true,
34691 PREC_NOT_OPERATOR
, NULL
);
34694 switch (TREE_CODE (rhs
))
34697 case TRUNC_DIV_EXPR
:
34706 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34708 if (cp_parser_parse_definitely (parser
))
34710 opcode
= TREE_CODE (rhs
);
34711 rhs1
= TREE_OPERAND (rhs
, 0);
34712 rhs
= TREE_OPERAND (rhs
, 1);
34722 cp_parser_abort_tentative_parse (parser
);
34723 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34725 rhs
= cp_parser_expression (parser
);
34726 if (rhs
== error_mark_node
)
34732 cp_parser_error (parser
,
34733 "invalid form of %<#pragma omp atomic%>");
34736 if (!cp_parser_parse_definitely (parser
))
34738 switch (token
->type
)
34740 case CPP_SEMICOLON
:
34741 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34743 code
= OMP_ATOMIC_CAPTURE_OLD
;
34748 cp_lexer_consume_token (parser
->lexer
);
34751 else if (structured_block
)
34758 cp_parser_error (parser
,
34759 "invalid form of %<#pragma omp atomic%>");
34762 opcode
= MULT_EXPR
;
34765 opcode
= TRUNC_DIV_EXPR
;
34768 opcode
= PLUS_EXPR
;
34771 opcode
= MINUS_EXPR
;
34774 opcode
= LSHIFT_EXPR
;
34777 opcode
= RSHIFT_EXPR
;
34780 opcode
= BIT_AND_EXPR
;
34783 opcode
= BIT_IOR_EXPR
;
34786 opcode
= BIT_XOR_EXPR
;
34789 cp_parser_error (parser
,
34790 "invalid operator for %<#pragma omp atomic%>");
34793 oprec
= TOKEN_PRECEDENCE (token
);
34794 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34795 if (commutative_tree_code (opcode
))
34796 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34797 cp_lexer_consume_token (parser
->lexer
);
34798 rhs
= cp_parser_binary_expression (parser
, false, false,
34800 if (rhs
== error_mark_node
)
34805 cp_parser_error (parser
,
34806 "invalid operator for %<#pragma omp atomic%>");
34809 cp_lexer_consume_token (parser
->lexer
);
34811 rhs
= cp_parser_expression (parser
);
34812 if (rhs
== error_mark_node
)
34817 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34819 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34821 v
= cp_parser_unary_expression (parser
);
34822 if (v
== error_mark_node
)
34824 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34826 lhs1
= cp_parser_unary_expression (parser
);
34827 if (lhs1
== error_mark_node
)
34830 if (structured_block
)
34832 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34833 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34836 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34837 if (!structured_block
)
34838 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34842 cp_parser_skip_to_end_of_block_or_statement (parser
);
34843 if (structured_block
)
34845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34846 cp_lexer_consume_token (parser
->lexer
);
34847 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34849 cp_parser_skip_to_end_of_block_or_statement (parser
);
34850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34851 cp_lexer_consume_token (parser
->lexer
);
34858 # pragma omp barrier new-line */
34861 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34863 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34864 finish_omp_barrier ();
34868 # pragma omp critical [(name)] new-line
34872 # pragma omp critical [(name) [hint(expression)]] new-line
34873 structured-block */
34875 #define OMP_CRITICAL_CLAUSE_MASK \
34876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34879 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34881 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34883 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34885 matching_parens parens
;
34886 parens
.consume_open (parser
);
34888 name
= cp_parser_identifier (parser
);
34890 if (name
== error_mark_node
34891 || !parens
.require_close (parser
))
34892 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34893 /*or_comma=*/false,
34894 /*consume_paren=*/true);
34895 if (name
== error_mark_node
)
34898 clauses
= cp_parser_omp_all_clauses (parser
,
34899 OMP_CRITICAL_CLAUSE_MASK
,
34900 "#pragma omp critical", pragma_tok
);
34903 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34905 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34906 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34910 # pragma omp flush flush-vars[opt] new-line
34913 ( variable-list ) */
34916 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34919 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34920 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34922 finish_omp_flush ();
34925 /* Helper function, to parse omp for increment expression. */
34928 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34930 tree cond
= cp_parser_binary_expression (parser
, false, true,
34931 PREC_NOT_OPERATOR
, NULL
);
34932 if (cond
== error_mark_node
34933 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34935 cp_parser_skip_to_end_of_statement (parser
);
34936 return error_mark_node
;
34939 switch (TREE_CODE (cond
))
34947 /* Fall through: OpenMP disallows NE_EXPR. */
34948 gcc_fallthrough ();
34950 return error_mark_node
;
34953 /* If decl is an iterator, preserve LHS and RHS of the relational
34954 expr until finish_omp_for. */
34956 && (type_dependent_expression_p (decl
)
34957 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34960 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34962 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34963 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34964 /*overload=*/NULL
, tf_warning_or_error
);
34967 /* Helper function, to parse omp for increment expression. */
34970 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34972 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34978 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34980 op
= (token
->type
== CPP_PLUS_PLUS
34981 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34982 cp_lexer_consume_token (parser
->lexer
);
34983 lhs
= cp_parser_simple_cast_expression (parser
);
34985 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34986 return error_mark_node
;
34987 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34990 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34992 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34993 return error_mark_node
;
34995 token
= cp_lexer_peek_token (parser
->lexer
);
34996 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34998 op
= (token
->type
== CPP_PLUS_PLUS
34999 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
35000 cp_lexer_consume_token (parser
->lexer
);
35001 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
35004 op
= cp_parser_assignment_operator_opt (parser
);
35005 if (op
== ERROR_MARK
)
35006 return error_mark_node
;
35008 if (op
!= NOP_EXPR
)
35010 rhs
= cp_parser_assignment_expression (parser
);
35011 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
35012 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35015 lhs
= cp_parser_binary_expression (parser
, false, false,
35016 PREC_ADDITIVE_EXPRESSION
, NULL
);
35017 token
= cp_lexer_peek_token (parser
->lexer
);
35018 decl_first
= (lhs
== decl
35019 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
35022 if (token
->type
!= CPP_PLUS
35023 && token
->type
!= CPP_MINUS
)
35024 return error_mark_node
;
35028 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
35029 cp_lexer_consume_token (parser
->lexer
);
35030 rhs
= cp_parser_binary_expression (parser
, false, false,
35031 PREC_ADDITIVE_EXPRESSION
, NULL
);
35032 token
= cp_lexer_peek_token (parser
->lexer
);
35033 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
35035 if (lhs
== NULL_TREE
)
35037 if (op
== PLUS_EXPR
)
35040 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
35041 tf_warning_or_error
);
35044 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
35045 ERROR_MARK
, NULL
, tf_warning_or_error
);
35048 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
35053 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
35054 || op
== MINUS_EXPR
)
35055 return error_mark_node
;
35056 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
35059 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
35061 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
35064 /* Parse the initialization statement of an OpenMP for loop.
35066 Return true if the resulting construct should have an
35067 OMP_CLAUSE_PRIVATE added to it. */
35070 cp_parser_omp_for_loop_init (cp_parser
*parser
,
35071 tree
&this_pre_body
,
35072 vec
<tree
, va_gc
> *&for_block
,
35078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35081 tree add_private_clause
= NULL_TREE
;
35083 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35087 integer-type var = lb
35088 random-access-iterator-type var = lb
35089 pointer-type var = lb
35091 cp_decl_specifier_seq type_specifiers
;
35093 /* First, try to parse as an initialized declaration. See
35094 cp_parser_condition, from whence the bulk of this is copied. */
35096 cp_parser_parse_tentatively (parser
);
35097 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
35098 /*is_trailing_return=*/false,
35100 if (cp_parser_parse_definitely (parser
))
35102 /* If parsing a type specifier seq succeeded, then this
35103 MUST be a initialized declaration. */
35104 tree asm_specification
, attributes
;
35105 cp_declarator
*declarator
;
35107 declarator
= cp_parser_declarator (parser
,
35108 CP_PARSER_DECLARATOR_NAMED
,
35109 /*ctor_dtor_or_conv_p=*/NULL
,
35110 /*parenthesized_p=*/NULL
,
35111 /*member_p=*/false,
35112 /*friend_p=*/false);
35113 attributes
= cp_parser_attributes_opt (parser
);
35114 asm_specification
= cp_parser_asm_specification_opt (parser
);
35116 if (declarator
== cp_error_declarator
)
35117 cp_parser_skip_to_end_of_statement (parser
);
35121 tree pushed_scope
, auto_node
;
35123 decl
= start_decl (declarator
, &type_specifiers
,
35124 SD_INITIALIZED
, attributes
,
35125 /*prefix_attributes=*/NULL_TREE
,
35128 auto_node
= type_uses_auto (TREE_TYPE (decl
));
35129 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
35131 if (cp_lexer_next_token_is (parser
->lexer
,
35133 error ("parenthesized initialization is not allowed in "
35134 "OpenMP %<for%> loop");
35136 /* Trigger an error. */
35137 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35139 init
= error_mark_node
;
35140 cp_parser_skip_to_end_of_statement (parser
);
35142 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35143 || type_dependent_expression_p (decl
)
35146 bool is_direct_init
, is_non_constant_init
;
35148 init
= cp_parser_initializer (parser
,
35150 &is_non_constant_init
);
35155 = do_auto_deduction (TREE_TYPE (decl
), init
,
35158 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35159 && !type_dependent_expression_p (decl
))
35163 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35165 LOOKUP_ONLYCONVERTING
);
35167 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35169 vec_safe_push (for_block
, this_pre_body
);
35174 init
= pop_stmt_list (this_pre_body
);
35175 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35177 tree_stmt_iterator i
= tsi_start (init
);
35178 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35179 while (!tsi_end_p (i
))
35181 tree t
= tsi_stmt (i
);
35182 if (TREE_CODE (t
) == DECL_EXPR
35183 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35186 vec_safe_push (for_block
, t
);
35191 if (tsi_one_before_end_p (i
))
35193 tree t
= tsi_stmt (i
);
35195 free_stmt_list (init
);
35200 this_pre_body
= NULL_TREE
;
35205 cp_lexer_consume_token (parser
->lexer
);
35206 init
= cp_parser_assignment_expression (parser
);
35209 if (TYPE_REF_P (TREE_TYPE (decl
)))
35210 init
= error_mark_node
;
35212 cp_finish_decl (decl
, NULL_TREE
,
35213 /*init_const_expr_p=*/false,
35215 LOOKUP_ONLYCONVERTING
);
35219 pop_scope (pushed_scope
);
35225 /* If parsing a type specifier sequence failed, then
35226 this MUST be a simple expression. */
35227 cp_parser_parse_tentatively (parser
);
35228 decl
= cp_parser_primary_expression (parser
, false, false,
35230 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35231 if (!cp_parser_error_occurred (parser
)
35233 && (TREE_CODE (decl
) == COMPONENT_REF
35234 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35236 cp_parser_abort_tentative_parse (parser
);
35237 cp_parser_parse_tentatively (parser
);
35238 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35239 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35240 /*check_dependency_p=*/true,
35241 /*template_p=*/NULL
,
35242 /*declarator_p=*/false,
35243 /*optional_p=*/false);
35244 if (name
!= error_mark_node
35245 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35247 decl
= cp_parser_lookup_name_simple (parser
, name
,
35249 if (TREE_CODE (decl
) == FIELD_DECL
)
35250 add_private_clause
= omp_privatize_field (decl
, false);
35252 cp_parser_abort_tentative_parse (parser
);
35253 cp_parser_parse_tentatively (parser
);
35254 decl
= cp_parser_primary_expression (parser
, false, false,
35257 if (!cp_parser_error_occurred (parser
)
35260 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35264 cp_parser_parse_definitely (parser
);
35265 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35266 rhs
= cp_parser_assignment_expression (parser
);
35268 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35271 tf_warning_or_error
));
35272 if (!add_private_clause
)
35273 add_private_clause
= decl
;
35278 cp_parser_abort_tentative_parse (parser
);
35279 init
= cp_parser_expression (parser
);
35282 if (TREE_CODE (init
) == MODIFY_EXPR
35283 || TREE_CODE (init
) == MODOP_EXPR
)
35284 real_decl
= TREE_OPERAND (init
, 0);
35288 return add_private_clause
;
35291 /* Parse the restricted form of the for statement allowed by OpenMP. */
35294 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35295 tree
*cclauses
, bool *if_p
)
35297 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35298 tree real_decl
, initv
, condv
, incrv
, declv
;
35299 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35300 location_t loc_first
;
35301 bool collapse_err
= false;
35302 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35303 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35304 auto_vec
<tree
, 4> orig_inits
;
35305 bool tiling
= false;
35307 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35308 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35309 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35310 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35313 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35315 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35316 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35319 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35322 if (ordered
&& ordered
< collapse
)
35324 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35325 "%<ordered%> clause parameter is less than %<collapse%>");
35326 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35327 = build_int_cst (NULL_TREE
, collapse
);
35328 ordered
= collapse
;
35332 for (tree
*pc
= &clauses
; *pc
; )
35333 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35335 error_at (OMP_CLAUSE_LOCATION (*pc
),
35336 "%<linear%> clause may not be specified together "
35337 "with %<ordered%> clause with a parameter");
35338 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35341 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35344 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35345 count
= ordered
? ordered
: collapse
;
35347 declv
= make_tree_vec (count
);
35348 initv
= make_tree_vec (count
);
35349 condv
= make_tree_vec (count
);
35350 incrv
= make_tree_vec (count
);
35352 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35354 for (i
= 0; i
< count
; i
++)
35356 int bracecount
= 0;
35357 tree add_private_clause
= NULL_TREE
;
35360 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35363 cp_parser_error (parser
, "for statement expected");
35366 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35368 matching_parens parens
;
35369 if (!parens
.require_open (parser
))
35372 init
= orig_init
= decl
= real_decl
= NULL
;
35373 this_pre_body
= push_stmt_list ();
35376 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35377 init
, orig_init
, decl
, real_decl
);
35379 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35382 this_pre_body
= pop_stmt_list (this_pre_body
);
35386 pre_body
= push_stmt_list ();
35388 add_stmt (this_pre_body
);
35389 pre_body
= pop_stmt_list (pre_body
);
35392 pre_body
= this_pre_body
;
35397 if (cclauses
!= NULL
35398 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35399 && real_decl
!= NULL_TREE
)
35402 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35403 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35404 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35406 error_at (loc
, "iteration variable %qD"
35407 " should not be firstprivate", real_decl
);
35408 *c
= OMP_CLAUSE_CHAIN (*c
);
35410 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35411 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35413 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35415 *c
= OMP_CLAUSE_CHAIN (*c
);
35416 if (code
== OMP_SIMD
)
35418 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35419 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35423 OMP_CLAUSE_CHAIN (l
) = clauses
;
35426 add_private_clause
= NULL_TREE
;
35430 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35431 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35432 add_private_clause
= NULL_TREE
;
35433 c
= &OMP_CLAUSE_CHAIN (*c
);
35437 if (add_private_clause
)
35440 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35442 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35443 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35444 && OMP_CLAUSE_DECL (c
) == decl
)
35446 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35447 && OMP_CLAUSE_DECL (c
) == decl
)
35448 error_at (loc
, "iteration variable %qD "
35449 "should not be firstprivate",
35451 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35452 && OMP_CLAUSE_DECL (c
) == decl
)
35453 error_at (loc
, "iteration variable %qD should not be reduction",
35458 if (code
!= OMP_SIMD
)
35459 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35460 else if (collapse
== 1)
35461 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35463 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35464 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35465 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35468 OMP_CLAUSE_CHAIN (c
) = clauses
;
35470 /* For linear, signal that we need to fill up
35471 the so far unknown linear step. */
35472 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35473 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35479 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35480 cond
= cp_parser_omp_for_cond (parser
, decl
);
35481 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35486 /* If decl is an iterator, preserve the operator on decl
35487 until finish_omp_for. */
35489 && ((processing_template_decl
35490 && (TREE_TYPE (real_decl
) == NULL_TREE
35491 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35492 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35493 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35495 incr
= cp_parser_expression (parser
);
35496 if (!EXPR_HAS_LOCATION (incr
))
35497 protected_set_expr_location (incr
, input_location
);
35500 if (!parens
.require_close (parser
))
35501 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35502 /*or_comma=*/false,
35503 /*consume_paren=*/true);
35505 TREE_VEC_ELT (declv
, i
) = decl
;
35506 TREE_VEC_ELT (initv
, i
) = init
;
35507 TREE_VEC_ELT (condv
, i
) = cond
;
35508 TREE_VEC_ELT (incrv
, i
) = incr
;
35511 orig_inits
.safe_grow_cleared (i
+ 1);
35512 orig_inits
[i
] = orig_init
;
35515 if (i
== count
- 1)
35518 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35519 in between the collapsed for loops to be still considered perfectly
35520 nested. Hopefully the final version clarifies this.
35521 For now handle (multiple) {'s and empty statements. */
35522 cp_parser_parse_tentatively (parser
);
35525 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35527 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35529 cp_lexer_consume_token (parser
->lexer
);
35532 else if (bracecount
35533 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35534 cp_lexer_consume_token (parser
->lexer
);
35537 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35538 error_at (loc
, "not enough for loops to collapse");
35539 collapse_err
= true;
35540 cp_parser_abort_tentative_parse (parser
);
35548 cp_parser_parse_definitely (parser
);
35549 nbraces
+= bracecount
;
35556 /* Note that we saved the original contents of this flag when we entered
35557 the structured block, and so we don't need to re-save it here. */
35558 parser
->in_statement
= IN_OMP_FOR
;
35560 /* Note that the grammar doesn't call for a structured block here,
35561 though the loop as a whole is a structured block. */
35562 body
= push_stmt_list ();
35563 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35564 body
= pop_stmt_list (body
);
35566 if (declv
== NULL_TREE
)
35569 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35570 body
, pre_body
, &orig_inits
, clauses
);
35574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35576 cp_lexer_consume_token (parser
->lexer
);
35579 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35580 cp_lexer_consume_token (parser
->lexer
);
35585 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35586 "collapsed loops not perfectly nested");
35588 collapse_err
= true;
35589 cp_parser_statement_seq_opt (parser
, NULL
);
35590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35595 while (!for_block
->is_empty ())
35597 tree t
= for_block
->pop ();
35598 if (TREE_CODE (t
) == STATEMENT_LIST
)
35599 add_stmt (pop_stmt_list (t
));
35603 release_tree_vector (for_block
);
35608 /* Helper function for OpenMP parsing, split clauses and call
35609 finish_omp_clauses on each of the set of clauses afterwards. */
35612 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35613 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35616 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35617 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35619 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35623 #pragma omp simd simd-clause[optseq] new-line
35626 #define OMP_SIMD_CLAUSE_MASK \
35627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35637 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35638 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35641 tree clauses
, sb
, ret
;
35643 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35645 strcat (p_name
, " simd");
35646 mask
|= OMP_SIMD_CLAUSE_MASK
;
35648 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35652 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35653 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35654 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35655 OMP_CLAUSE_ORDERED
);
35656 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35658 error_at (OMP_CLAUSE_LOCATION (c
),
35659 "%<ordered%> clause with parameter may not be specified "
35660 "on %qs construct", p_name
);
35661 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35665 sb
= begin_omp_structured_block ();
35666 save
= cp_parser_begin_omp_structured_block (parser
);
35668 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35670 cp_parser_end_omp_structured_block (parser
, save
);
35671 add_stmt (finish_omp_structured_block (sb
));
35677 #pragma omp for for-clause[optseq] new-line
35681 #pragma omp for simd for-simd-clause[optseq] new-line
35684 #define OMP_FOR_CLAUSE_MASK \
35685 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35696 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35697 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35700 tree clauses
, sb
, ret
;
35702 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35704 strcat (p_name
, " for");
35705 mask
|= OMP_FOR_CLAUSE_MASK
;
35706 /* parallel for{, simd} disallows nowait clause, but for
35707 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35708 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35709 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35710 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35711 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35712 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35714 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35716 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35717 const char *p
= IDENTIFIER_POINTER (id
);
35719 if (strcmp (p
, "simd") == 0)
35721 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35722 if (cclauses
== NULL
)
35723 cclauses
= cclauses_buf
;
35725 cp_lexer_consume_token (parser
->lexer
);
35726 if (!flag_openmp
) /* flag_openmp_simd */
35727 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35729 sb
= begin_omp_structured_block ();
35730 save
= cp_parser_begin_omp_structured_block (parser
);
35731 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35733 cp_parser_end_omp_structured_block (parser
, save
);
35734 tree body
= finish_omp_structured_block (sb
);
35737 ret
= make_node (OMP_FOR
);
35738 TREE_TYPE (ret
) = void_type_node
;
35739 OMP_FOR_BODY (ret
) = body
;
35740 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35741 SET_EXPR_LOCATION (ret
, loc
);
35746 if (!flag_openmp
) /* flag_openmp_simd */
35748 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35752 /* Composite distribute parallel for disallows linear clause. */
35753 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35754 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35756 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35760 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35761 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35764 sb
= begin_omp_structured_block ();
35765 save
= cp_parser_begin_omp_structured_block (parser
);
35767 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35769 cp_parser_end_omp_structured_block (parser
, save
);
35770 add_stmt (finish_omp_structured_block (sb
));
35776 # pragma omp master new-line
35777 structured-block */
35780 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35782 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35783 return c_finish_omp_master (input_location
,
35784 cp_parser_omp_structured_block (parser
, if_p
));
35788 # pragma omp ordered new-line
35792 # pragma omp ordered ordered-clauses new-line
35793 structured-block */
35795 #define OMP_ORDERED_CLAUSE_MASK \
35796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35799 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35800 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35803 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35804 enum pragma_context context
, bool *if_p
)
35806 location_t loc
= pragma_tok
->location
;
35808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35810 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35811 const char *p
= IDENTIFIER_POINTER (id
);
35813 if (strcmp (p
, "depend") == 0)
35815 if (!flag_openmp
) /* flag_openmp_simd */
35817 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35820 if (context
== pragma_stmt
)
35822 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35823 "%<depend%> clause may only be used in compound "
35825 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35829 = cp_parser_omp_all_clauses (parser
,
35830 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35831 "#pragma omp ordered", pragma_tok
);
35832 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35838 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35839 "#pragma omp ordered", pragma_tok
);
35841 if (!flag_openmp
/* flag_openmp_simd */
35842 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35845 c_finish_omp_ordered (loc
, clauses
,
35846 cp_parser_omp_structured_block (parser
, if_p
));
35853 { section-sequence }
35856 section-directive[opt] structured-block
35857 section-sequence section-directive structured-block */
35860 cp_parser_omp_sections_scope (cp_parser
*parser
)
35862 tree stmt
, substmt
;
35863 bool error_suppress
= false;
35866 matching_braces braces
;
35867 if (!braces
.require_open (parser
))
35870 stmt
= push_stmt_list ();
35872 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35873 != PRAGMA_OMP_SECTION
)
35875 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35876 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35877 add_stmt (substmt
);
35882 tok
= cp_lexer_peek_token (parser
->lexer
);
35883 if (tok
->type
== CPP_CLOSE_BRACE
)
35885 if (tok
->type
== CPP_EOF
)
35888 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35890 cp_lexer_consume_token (parser
->lexer
);
35891 cp_parser_require_pragma_eol (parser
, tok
);
35892 error_suppress
= false;
35894 else if (!error_suppress
)
35896 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35897 error_suppress
= true;
35900 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35901 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35902 add_stmt (substmt
);
35904 braces
.require_close (parser
);
35906 substmt
= pop_stmt_list (stmt
);
35908 stmt
= make_node (OMP_SECTIONS
);
35909 TREE_TYPE (stmt
) = void_type_node
;
35910 OMP_SECTIONS_BODY (stmt
) = substmt
;
35917 # pragma omp sections sections-clause[optseq] newline
35920 #define OMP_SECTIONS_CLAUSE_MASK \
35921 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35928 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35929 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35932 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35934 strcat (p_name
, " sections");
35935 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35937 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35939 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35943 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35944 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35947 ret
= cp_parser_omp_sections_scope (parser
);
35949 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35955 # pragma omp parallel parallel-clause[optseq] new-line
35957 # pragma omp parallel for parallel-for-clause[optseq] new-line
35959 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35963 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35964 structured-block */
35966 #define OMP_PARALLEL_CLAUSE_MASK \
35967 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35978 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35979 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35982 tree stmt
, clauses
, block
;
35984 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35986 strcat (p_name
, " parallel");
35987 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35988 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35989 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35990 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35991 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35993 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35995 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35996 if (cclauses
== NULL
)
35997 cclauses
= cclauses_buf
;
35999 cp_lexer_consume_token (parser
->lexer
);
36000 if (!flag_openmp
) /* flag_openmp_simd */
36001 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
36003 block
= begin_omp_parallel ();
36004 save
= cp_parser_begin_omp_structured_block (parser
);
36005 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
36007 cp_parser_end_omp_structured_block (parser
, save
);
36008 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
36010 if (ret
== NULL_TREE
)
36012 OMP_PARALLEL_COMBINED (stmt
) = 1;
36015 /* When combined with distribute, parallel has to be followed by for.
36016 #pragma omp target parallel is allowed though. */
36018 && (mask
& (OMP_CLAUSE_MASK_1
36019 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
36021 error_at (loc
, "expected %<for%> after %qs", p_name
);
36022 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36025 else if (!flag_openmp
) /* flag_openmp_simd */
36027 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36030 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36032 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36033 const char *p
= IDENTIFIER_POINTER (id
);
36034 if (strcmp (p
, "sections") == 0)
36036 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36037 cclauses
= cclauses_buf
;
36039 cp_lexer_consume_token (parser
->lexer
);
36040 block
= begin_omp_parallel ();
36041 save
= cp_parser_begin_omp_structured_block (parser
);
36042 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
36043 cp_parser_end_omp_structured_block (parser
, save
);
36044 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
36046 OMP_PARALLEL_COMBINED (stmt
) = 1;
36051 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36055 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
36056 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
36059 block
= begin_omp_parallel ();
36060 save
= cp_parser_begin_omp_structured_block (parser
);
36061 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36062 cp_parser_end_omp_structured_block (parser
, save
);
36063 stmt
= finish_omp_parallel (clauses
, block
);
36068 # pragma omp single single-clause[optseq] new-line
36069 structured-block */
36071 #define OMP_SINGLE_CLAUSE_MASK \
36072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
36075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36078 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36080 tree stmt
= make_node (OMP_SINGLE
);
36081 TREE_TYPE (stmt
) = void_type_node
;
36083 OMP_SINGLE_CLAUSES (stmt
)
36084 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
36085 "#pragma omp single", pragma_tok
);
36086 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36088 return add_stmt (stmt
);
36092 # pragma omp task task-clause[optseq] new-line
36093 structured-block */
36095 #define OMP_TASK_CLAUSE_MASK \
36096 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36108 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36110 tree clauses
, block
;
36113 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
36114 "#pragma omp task", pragma_tok
);
36115 block
= begin_omp_task ();
36116 save
= cp_parser_begin_omp_structured_block (parser
);
36117 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36118 cp_parser_end_omp_structured_block (parser
, save
);
36119 return finish_omp_task (clauses
, block
);
36123 # pragma omp taskwait new-line */
36126 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
36128 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36129 finish_omp_taskwait ();
36133 # pragma omp taskyield new-line */
36136 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
36138 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36139 finish_omp_taskyield ();
36143 # pragma omp taskgroup new-line
36144 structured-block */
36147 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36149 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36150 return c_finish_omp_taskgroup (input_location
,
36151 cp_parser_omp_structured_block (parser
,
36157 # pragma omp threadprivate (variable-list) */
36160 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36164 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36165 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36167 finish_omp_threadprivate (vars
);
36171 # pragma omp cancel cancel-clause[optseq] new-line */
36173 #define OMP_CANCEL_CLAUSE_MASK \
36174 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36181 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36183 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36184 "#pragma omp cancel", pragma_tok
);
36185 finish_omp_cancel (clauses
);
36189 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36191 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36198 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36199 enum pragma_context context
)
36202 bool point_seen
= false;
36204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36206 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36207 const char *p
= IDENTIFIER_POINTER (id
);
36209 if (strcmp (p
, "point") == 0)
36211 cp_lexer_consume_token (parser
->lexer
);
36217 cp_parser_error (parser
, "expected %<point%>");
36218 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36222 if (context
!= pragma_compound
)
36224 if (context
== pragma_stmt
)
36225 error_at (pragma_tok
->location
,
36226 "%<#pragma %s%> may only be used in compound statements",
36227 "omp cancellation point");
36229 cp_parser_error (parser
, "expected declaration specifiers");
36230 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36234 clauses
= cp_parser_omp_all_clauses (parser
,
36235 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36236 "#pragma omp cancellation point",
36238 finish_omp_cancellation_point (clauses
);
36242 #pragma omp distribute distribute-clause[optseq] new-line
36245 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36253 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36254 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36257 tree clauses
, sb
, ret
;
36259 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36261 strcat (p_name
, " distribute");
36262 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36266 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36267 const char *p
= IDENTIFIER_POINTER (id
);
36269 bool parallel
= false;
36271 if (strcmp (p
, "simd") == 0)
36274 parallel
= strcmp (p
, "parallel") == 0;
36275 if (parallel
|| simd
)
36277 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36278 if (cclauses
== NULL
)
36279 cclauses
= cclauses_buf
;
36280 cp_lexer_consume_token (parser
->lexer
);
36281 if (!flag_openmp
) /* flag_openmp_simd */
36284 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36287 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36290 sb
= begin_omp_structured_block ();
36291 save
= cp_parser_begin_omp_structured_block (parser
);
36293 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36296 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36298 cp_parser_end_omp_structured_block (parser
, save
);
36299 tree body
= finish_omp_structured_block (sb
);
36302 ret
= make_node (OMP_DISTRIBUTE
);
36303 TREE_TYPE (ret
) = void_type_node
;
36304 OMP_FOR_BODY (ret
) = body
;
36305 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36306 SET_EXPR_LOCATION (ret
, loc
);
36311 if (!flag_openmp
) /* flag_openmp_simd */
36313 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36317 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36321 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36322 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36325 sb
= begin_omp_structured_block ();
36326 save
= cp_parser_begin_omp_structured_block (parser
);
36328 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36330 cp_parser_end_omp_structured_block (parser
, save
);
36331 add_stmt (finish_omp_structured_block (sb
));
36337 # pragma omp teams teams-clause[optseq] new-line
36338 structured-block */
36340 #define OMP_TEAMS_CLAUSE_MASK \
36341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36350 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36351 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36354 tree clauses
, sb
, ret
;
36356 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36358 strcat (p_name
, " teams");
36359 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36363 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36364 const char *p
= IDENTIFIER_POINTER (id
);
36365 if (strcmp (p
, "distribute") == 0)
36367 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36368 if (cclauses
== NULL
)
36369 cclauses
= cclauses_buf
;
36371 cp_lexer_consume_token (parser
->lexer
);
36372 if (!flag_openmp
) /* flag_openmp_simd */
36373 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36375 sb
= begin_omp_structured_block ();
36376 save
= cp_parser_begin_omp_structured_block (parser
);
36377 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36379 cp_parser_end_omp_structured_block (parser
, save
);
36380 tree body
= finish_omp_structured_block (sb
);
36383 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36384 ret
= make_node (OMP_TEAMS
);
36385 TREE_TYPE (ret
) = void_type_node
;
36386 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36387 OMP_TEAMS_BODY (ret
) = body
;
36388 OMP_TEAMS_COMBINED (ret
) = 1;
36389 SET_EXPR_LOCATION (ret
, loc
);
36390 return add_stmt (ret
);
36393 if (!flag_openmp
) /* flag_openmp_simd */
36395 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36399 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36403 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36404 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36407 tree stmt
= make_node (OMP_TEAMS
);
36408 TREE_TYPE (stmt
) = void_type_node
;
36409 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36410 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36411 SET_EXPR_LOCATION (stmt
, loc
);
36413 return add_stmt (stmt
);
36417 # pragma omp target data target-data-clause[optseq] new-line
36418 structured-block */
36420 #define OMP_TARGET_DATA_CLAUSE_MASK \
36421 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36427 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36430 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36431 "#pragma omp target data", pragma_tok
);
36433 for (tree
*pc
= &clauses
; *pc
;)
36435 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36436 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36439 case GOMP_MAP_ALWAYS_TO
:
36440 case GOMP_MAP_FROM
:
36441 case GOMP_MAP_ALWAYS_FROM
:
36442 case GOMP_MAP_TOFROM
:
36443 case GOMP_MAP_ALWAYS_TOFROM
:
36444 case GOMP_MAP_ALLOC
:
36447 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36448 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36449 case GOMP_MAP_ALWAYS_POINTER
:
36453 error_at (OMP_CLAUSE_LOCATION (*pc
),
36454 "%<#pragma omp target data%> with map-type other "
36455 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36456 "on %<map%> clause");
36457 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36460 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36466 error_at (pragma_tok
->location
,
36467 "%<#pragma omp target data%> must contain at least "
36468 "one %<map%> clause");
36472 tree stmt
= make_node (OMP_TARGET_DATA
);
36473 TREE_TYPE (stmt
) = void_type_node
;
36474 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36476 keep_next_level (true);
36477 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36479 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36480 return add_stmt (stmt
);
36484 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36485 structured-block */
36487 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36495 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36496 enum pragma_context context
)
36498 bool data_seen
= false;
36499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36501 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36502 const char *p
= IDENTIFIER_POINTER (id
);
36504 if (strcmp (p
, "data") == 0)
36506 cp_lexer_consume_token (parser
->lexer
);
36512 cp_parser_error (parser
, "expected %<data%>");
36513 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36517 if (context
== pragma_stmt
)
36519 error_at (pragma_tok
->location
,
36520 "%<#pragma %s%> may only be used in compound statements",
36521 "omp target enter data");
36522 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36527 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36528 "#pragma omp target enter data", pragma_tok
);
36530 for (tree
*pc
= &clauses
; *pc
;)
36532 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36533 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36536 case GOMP_MAP_ALWAYS_TO
:
36537 case GOMP_MAP_ALLOC
:
36540 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36541 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36542 case GOMP_MAP_ALWAYS_POINTER
:
36546 error_at (OMP_CLAUSE_LOCATION (*pc
),
36547 "%<#pragma omp target enter data%> with map-type other "
36548 "than %<to%> or %<alloc%> on %<map%> clause");
36549 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36552 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36558 error_at (pragma_tok
->location
,
36559 "%<#pragma omp target enter data%> must contain at least "
36560 "one %<map%> clause");
36564 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36565 TREE_TYPE (stmt
) = void_type_node
;
36566 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36567 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36568 return add_stmt (stmt
);
36572 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36573 structured-block */
36575 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36583 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36584 enum pragma_context context
)
36586 bool data_seen
= false;
36587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36589 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36590 const char *p
= IDENTIFIER_POINTER (id
);
36592 if (strcmp (p
, "data") == 0)
36594 cp_lexer_consume_token (parser
->lexer
);
36600 cp_parser_error (parser
, "expected %<data%>");
36601 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36605 if (context
== pragma_stmt
)
36607 error_at (pragma_tok
->location
,
36608 "%<#pragma %s%> may only be used in compound statements",
36609 "omp target exit data");
36610 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36615 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36616 "#pragma omp target exit data", pragma_tok
);
36618 for (tree
*pc
= &clauses
; *pc
;)
36620 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36621 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36623 case GOMP_MAP_FROM
:
36624 case GOMP_MAP_ALWAYS_FROM
:
36625 case GOMP_MAP_RELEASE
:
36626 case GOMP_MAP_DELETE
:
36629 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36630 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36631 case GOMP_MAP_ALWAYS_POINTER
:
36635 error_at (OMP_CLAUSE_LOCATION (*pc
),
36636 "%<#pragma omp target exit data%> with map-type other "
36637 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36639 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36642 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36648 error_at (pragma_tok
->location
,
36649 "%<#pragma omp target exit data%> must contain at least "
36650 "one %<map%> clause");
36654 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36655 TREE_TYPE (stmt
) = void_type_node
;
36656 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36657 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36658 return add_stmt (stmt
);
36662 # pragma omp target update target-update-clause[optseq] new-line */
36664 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36673 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36674 enum pragma_context context
)
36676 if (context
== pragma_stmt
)
36678 error_at (pragma_tok
->location
,
36679 "%<#pragma %s%> may only be used in compound statements",
36680 "omp target update");
36681 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36686 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36687 "#pragma omp target update", pragma_tok
);
36688 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36689 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36691 error_at (pragma_tok
->location
,
36692 "%<#pragma omp target update%> must contain at least one "
36693 "%<from%> or %<to%> clauses");
36697 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36698 TREE_TYPE (stmt
) = void_type_node
;
36699 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36700 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36706 # pragma omp target target-clause[optseq] new-line
36707 structured-block */
36709 #define OMP_TARGET_CLAUSE_MASK \
36710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36721 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36722 enum pragma_context context
, bool *if_p
)
36724 tree
*pc
= NULL
, stmt
;
36726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36728 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36729 const char *p
= IDENTIFIER_POINTER (id
);
36730 enum tree_code ccode
= ERROR_MARK
;
36732 if (strcmp (p
, "teams") == 0)
36734 else if (strcmp (p
, "parallel") == 0)
36735 ccode
= OMP_PARALLEL
;
36736 else if (strcmp (p
, "simd") == 0)
36738 if (ccode
!= ERROR_MARK
)
36740 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36741 char p_name
[sizeof ("#pragma omp target teams distribute "
36742 "parallel for simd")];
36744 cp_lexer_consume_token (parser
->lexer
);
36745 strcpy (p_name
, "#pragma omp target");
36746 if (!flag_openmp
) /* flag_openmp_simd */
36752 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36753 OMP_TARGET_CLAUSE_MASK
,
36757 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36758 OMP_TARGET_CLAUSE_MASK
,
36762 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36763 OMP_TARGET_CLAUSE_MASK
,
36767 gcc_unreachable ();
36769 return stmt
!= NULL_TREE
;
36771 keep_next_level (true);
36772 tree sb
= begin_omp_structured_block (), ret
;
36773 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36777 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36778 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36782 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36783 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36787 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36788 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36792 gcc_unreachable ();
36794 cp_parser_end_omp_structured_block (parser
, save
);
36795 tree body
= finish_omp_structured_block (sb
);
36796 if (ret
== NULL_TREE
)
36798 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36800 /* For combined target teams, ensure the num_teams and
36801 thread_limit clause expressions are evaluated on the host,
36802 before entering the target construct. */
36804 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36805 c
; c
= OMP_CLAUSE_CHAIN (c
))
36806 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36807 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36808 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36810 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36811 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36812 if (expr
== error_mark_node
)
36814 tree tmp
= TARGET_EXPR_SLOT (expr
);
36816 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36817 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36818 OMP_CLAUSE_FIRSTPRIVATE
);
36819 OMP_CLAUSE_DECL (tc
) = tmp
;
36820 OMP_CLAUSE_CHAIN (tc
)
36821 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36822 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36825 tree stmt
= make_node (OMP_TARGET
);
36826 TREE_TYPE (stmt
) = void_type_node
;
36827 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36828 OMP_TARGET_BODY (stmt
) = body
;
36829 OMP_TARGET_COMBINED (stmt
) = 1;
36830 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36832 pc
= &OMP_TARGET_CLAUSES (stmt
);
36833 goto check_clauses
;
36835 else if (!flag_openmp
) /* flag_openmp_simd */
36837 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36840 else if (strcmp (p
, "data") == 0)
36842 cp_lexer_consume_token (parser
->lexer
);
36843 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36846 else if (strcmp (p
, "enter") == 0)
36848 cp_lexer_consume_token (parser
->lexer
);
36849 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36852 else if (strcmp (p
, "exit") == 0)
36854 cp_lexer_consume_token (parser
->lexer
);
36855 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36858 else if (strcmp (p
, "update") == 0)
36860 cp_lexer_consume_token (parser
->lexer
);
36861 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36864 if (!flag_openmp
) /* flag_openmp_simd */
36866 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36870 stmt
= make_node (OMP_TARGET
);
36871 TREE_TYPE (stmt
) = void_type_node
;
36873 OMP_TARGET_CLAUSES (stmt
)
36874 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36875 "#pragma omp target", pragma_tok
);
36876 pc
= &OMP_TARGET_CLAUSES (stmt
);
36877 keep_next_level (true);
36878 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36880 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36886 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36887 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36890 case GOMP_MAP_ALWAYS_TO
:
36891 case GOMP_MAP_FROM
:
36892 case GOMP_MAP_ALWAYS_FROM
:
36893 case GOMP_MAP_TOFROM
:
36894 case GOMP_MAP_ALWAYS_TOFROM
:
36895 case GOMP_MAP_ALLOC
:
36896 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36897 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36898 case GOMP_MAP_ALWAYS_POINTER
:
36901 error_at (OMP_CLAUSE_LOCATION (*pc
),
36902 "%<#pragma omp target%> with map-type other "
36903 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36904 "on %<map%> clause");
36905 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36908 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36914 # pragma acc cache (variable-list) new-line
36918 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36920 tree stmt
, clauses
;
36922 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36923 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36925 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36927 stmt
= make_node (OACC_CACHE
);
36928 TREE_TYPE (stmt
) = void_type_node
;
36929 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36930 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36937 # pragma acc data oacc-data-clause[optseq] new-line
36938 structured-block */
36940 #define OACC_DATA_CLAUSE_MASK \
36941 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36950 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36952 tree stmt
, clauses
, block
;
36955 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36956 "#pragma acc data", pragma_tok
);
36958 block
= begin_omp_parallel ();
36959 save
= cp_parser_begin_omp_structured_block (parser
);
36960 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36961 cp_parser_end_omp_structured_block (parser
, save
);
36962 stmt
= finish_oacc_data (clauses
, block
);
36967 # pragma acc host_data <clauses> new-line
36968 structured-block */
36970 #define OACC_HOST_DATA_CLAUSE_MASK \
36971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36974 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36976 tree stmt
, clauses
, block
;
36979 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36980 "#pragma acc host_data", pragma_tok
);
36982 block
= begin_omp_parallel ();
36983 save
= cp_parser_begin_omp_structured_block (parser
);
36984 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36985 cp_parser_end_omp_structured_block (parser
, save
);
36986 stmt
= finish_oacc_host_data (clauses
, block
);
36991 # pragma acc declare oacc-data-clause[optseq] new-line
36994 #define OACC_DECLARE_CLAUSE_MASK \
36995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
37005 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
37007 tree clauses
, stmt
;
37008 bool error
= false;
37010 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
37011 "#pragma acc declare", pragma_tok
, true);
37014 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37016 error_at (pragma_tok
->location
,
37017 "no valid clauses specified in %<#pragma acc declare%>");
37021 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
37023 location_t loc
= OMP_CLAUSE_LOCATION (t
);
37024 tree decl
= OMP_CLAUSE_DECL (t
);
37025 if (!DECL_P (decl
))
37027 error_at (loc
, "array section in %<#pragma acc declare%>");
37031 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
37032 switch (OMP_CLAUSE_MAP_KIND (t
))
37034 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
37035 case GOMP_MAP_ALLOC
:
37037 case GOMP_MAP_FORCE_DEVICEPTR
:
37038 case GOMP_MAP_DEVICE_RESIDENT
:
37041 case GOMP_MAP_LINK
:
37042 if (!global_bindings_p ()
37043 && (TREE_STATIC (decl
)
37044 || !DECL_EXTERNAL (decl
)))
37047 "%qD must be a global variable in "
37048 "%<#pragma acc declare link%>",
37056 if (global_bindings_p ())
37058 error_at (loc
, "invalid OpenACC clause at file scope");
37062 if (DECL_EXTERNAL (decl
))
37065 "invalid use of %<extern%> variable %qD "
37066 "in %<#pragma acc declare%>", decl
);
37070 else if (TREE_PUBLIC (decl
))
37073 "invalid use of %<global%> variable %qD "
37074 "in %<#pragma acc declare%>", decl
);
37081 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
37082 || lookup_attribute ("omp declare target link",
37083 DECL_ATTRIBUTES (decl
)))
37085 error_at (loc
, "variable %qD used more than once with "
37086 "%<#pragma acc declare%>", decl
);
37095 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
37096 id
= get_identifier ("omp declare target link");
37098 id
= get_identifier ("omp declare target");
37100 DECL_ATTRIBUTES (decl
)
37101 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
37102 if (global_bindings_p ())
37104 symtab_node
*node
= symtab_node::get (decl
);
37107 node
->offloadable
= 1;
37108 if (ENABLE_OFFLOADING
)
37110 g
->have_offload
= true;
37111 if (is_a
<varpool_node
*> (node
))
37112 vec_safe_push (offload_vars
, decl
);
37119 if (error
|| global_bindings_p ())
37122 stmt
= make_node (OACC_DECLARE
);
37123 TREE_TYPE (stmt
) = void_type_node
;
37124 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
37125 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37133 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37137 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37139 LOC is the location of the #pragma token.
37142 #define OACC_ENTER_DATA_CLAUSE_MASK \
37143 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37149 #define OACC_EXIT_DATA_CLAUSE_MASK \
37150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37158 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37161 location_t loc
= pragma_tok
->location
;
37162 tree stmt
, clauses
;
37163 const char *p
= "";
37165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37166 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37168 if (strcmp (p
, "data") != 0)
37170 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37171 enter
? "enter" : "exit");
37172 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37176 cp_lexer_consume_token (parser
->lexer
);
37179 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37180 "#pragma acc enter data", pragma_tok
);
37182 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37183 "#pragma acc exit data", pragma_tok
);
37185 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37187 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37188 enter
? "enter" : "exit");
37192 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37193 TREE_TYPE (stmt
) = void_type_node
;
37194 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37195 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37201 # pragma acc loop oacc-loop-clause[optseq] new-line
37202 structured-block */
37204 #define OACC_LOOP_CLAUSE_MASK \
37205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37217 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37218 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37220 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37222 strcat (p_name
, " loop");
37223 mask
|= OACC_LOOP_CLAUSE_MASK
;
37225 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37229 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37231 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37233 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37236 tree block
= begin_omp_structured_block ();
37237 int save
= cp_parser_begin_omp_structured_block (parser
);
37238 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37239 cp_parser_end_omp_structured_block (parser
, save
);
37240 add_stmt (finish_omp_structured_block (block
));
37246 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37251 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37255 #define OACC_KERNELS_CLAUSE_MASK \
37256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37270 #define OACC_PARALLEL_CLAUSE_MASK \
37271 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37289 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37290 char *p_name
, bool *if_p
)
37292 omp_clause_mask mask
;
37293 enum tree_code code
;
37294 switch (cp_parser_pragma_kind (pragma_tok
))
37296 case PRAGMA_OACC_KERNELS
:
37297 strcat (p_name
, " kernels");
37298 mask
= OACC_KERNELS_CLAUSE_MASK
;
37299 code
= OACC_KERNELS
;
37301 case PRAGMA_OACC_PARALLEL
:
37302 strcat (p_name
, " parallel");
37303 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37304 code
= OACC_PARALLEL
;
37307 gcc_unreachable ();
37310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37313 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37314 if (strcmp (p
, "loop") == 0)
37316 cp_lexer_consume_token (parser
->lexer
);
37317 tree block
= begin_omp_parallel ();
37319 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37321 return finish_omp_construct (code
, block
, clauses
);
37325 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37327 tree block
= begin_omp_parallel ();
37328 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37329 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37330 cp_parser_end_omp_structured_block (parser
, save
);
37331 return finish_omp_construct (code
, block
, clauses
);
37335 # pragma acc update oacc-update-clause[optseq] new-line
37338 #define OACC_UPDATE_CLAUSE_MASK \
37339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37347 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37349 tree stmt
, clauses
;
37351 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37352 "#pragma acc update", pragma_tok
);
37354 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37356 error_at (pragma_tok
->location
,
37357 "%<#pragma acc update%> must contain at least one "
37358 "%<device%> or %<host%> or %<self%> clause");
37362 stmt
= make_node (OACC_UPDATE
);
37363 TREE_TYPE (stmt
) = void_type_node
;
37364 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37365 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37371 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37373 LOC is the location of the #pragma token.
37376 #define OACC_WAIT_CLAUSE_MASK \
37377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37380 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37382 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37383 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37385 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37386 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37388 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37389 "#pragma acc wait", pragma_tok
);
37391 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37392 stmt
= finish_expr_stmt (stmt
);
37398 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37400 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37401 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37409 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37410 enum pragma_context context
)
37412 bool first_p
= parser
->omp_declare_simd
== NULL
;
37413 cp_omp_declare_simd_data data
;
37416 data
.error_seen
= false;
37417 data
.fndecl_seen
= false;
37418 data
.tokens
= vNULL
;
37419 data
.clauses
= NULL_TREE
;
37420 /* It is safe to take the address of a local variable; it will only be
37421 used while this scope is live. */
37422 parser
->omp_declare_simd
= &data
;
37425 /* Store away all pragma tokens. */
37426 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37427 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37428 cp_lexer_consume_token (parser
->lexer
);
37429 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37430 parser
->omp_declare_simd
->error_seen
= true;
37431 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37432 struct cp_token_cache
*cp
37433 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37434 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37438 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37439 cp_parser_pragma (parser
, context
, NULL
);
37442 case pragma_external
:
37443 cp_parser_declaration (parser
);
37445 case pragma_member
:
37446 cp_parser_member_declaration (parser
);
37448 case pragma_objc_icode
:
37449 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37452 cp_parser_declaration_statement (parser
);
37455 if (parser
->omp_declare_simd
37456 && !parser
->omp_declare_simd
->error_seen
37457 && !parser
->omp_declare_simd
->fndecl_seen
)
37458 error_at (pragma_tok
->location
,
37459 "%<#pragma omp declare simd%> not immediately followed by "
37460 "function declaration or definition");
37461 data
.tokens
.release ();
37462 parser
->omp_declare_simd
= NULL
;
37466 /* Finalize #pragma omp declare simd clauses after direct declarator has
37467 been parsed, and put that into "omp declare simd" attribute. */
37470 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37472 struct cp_token_cache
*ce
;
37473 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37476 if (!data
->error_seen
&& data
->fndecl_seen
)
37478 error ("%<#pragma omp declare simd%> not immediately followed by "
37479 "a single function declaration or definition");
37480 data
->error_seen
= true;
37482 if (data
->error_seen
)
37485 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37489 cp_parser_push_lexer_for_tokens (parser
, ce
);
37490 parser
->lexer
->in_pragma
= true;
37491 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37492 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37493 cp_lexer_consume_token (parser
->lexer
);
37494 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37495 "#pragma omp declare simd", pragma_tok
);
37496 cp_parser_pop_lexer (parser
);
37498 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37499 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37500 TREE_CHAIN (c
) = attrs
;
37501 if (processing_template_decl
)
37502 ATTR_IS_DEPENDENT (c
) = 1;
37506 data
->fndecl_seen
= true;
37512 # pragma omp declare target new-line
37513 declarations and definitions
37514 # pragma omp end declare target new-line
37517 # pragma omp declare target ( extended-list ) new-line
37519 # pragma omp declare target declare-target-clauses[seq] new-line */
37521 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37526 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37528 tree clauses
= NULL_TREE
;
37529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37531 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37532 "#pragma omp declare target", pragma_tok
);
37533 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37535 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37537 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37538 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37542 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37543 scope_chain
->omp_declare_target_attribute
++;
37546 if (scope_chain
->omp_declare_target_attribute
)
37547 error_at (pragma_tok
->location
,
37548 "%<#pragma omp declare target%> with clauses in between "
37549 "%<#pragma omp declare target%> without clauses and "
37550 "%<#pragma omp end declare target%>");
37551 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37553 tree t
= OMP_CLAUSE_DECL (c
), id
;
37554 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37555 tree at2
= lookup_attribute ("omp declare target link",
37556 DECL_ATTRIBUTES (t
));
37557 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37559 id
= get_identifier ("omp declare target link");
37560 std::swap (at1
, at2
);
37563 id
= get_identifier ("omp declare target");
37566 error_at (OMP_CLAUSE_LOCATION (c
),
37567 "%qD specified both in declare target %<link%> and %<to%>"
37573 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37574 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37577 symtab_node
*node
= symtab_node::get (t
);
37580 node
->offloadable
= 1;
37581 if (ENABLE_OFFLOADING
)
37583 g
->have_offload
= true;
37584 if (is_a
<varpool_node
*> (node
))
37585 vec_safe_push (offload_vars
, t
);
37593 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37595 const char *p
= "";
37596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37598 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37599 p
= IDENTIFIER_POINTER (id
);
37601 if (strcmp (p
, "declare") == 0)
37603 cp_lexer_consume_token (parser
->lexer
);
37605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37607 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37608 p
= IDENTIFIER_POINTER (id
);
37610 if (strcmp (p
, "target") == 0)
37611 cp_lexer_consume_token (parser
->lexer
);
37614 cp_parser_error (parser
, "expected %<target%>");
37615 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37621 cp_parser_error (parser
, "expected %<declare%>");
37622 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37625 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37626 if (!scope_chain
->omp_declare_target_attribute
)
37627 error_at (pragma_tok
->location
,
37628 "%<#pragma omp end declare target%> without corresponding "
37629 "%<#pragma omp declare target%>");
37631 scope_chain
->omp_declare_target_attribute
--;
37634 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37635 expression and optional initializer clause of
37636 #pragma omp declare reduction. We store the expression(s) as
37637 either 3, 6 or 7 special statements inside of the artificial function's
37638 body. The first two statements are DECL_EXPRs for the artificial
37639 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37640 expression that uses those variables.
37641 If there was any INITIALIZER clause, this is followed by further statements,
37642 the fourth and fifth statements are DECL_EXPRs for the artificial
37643 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37644 constructor variant (first token after open paren is not omp_priv),
37645 then the sixth statement is a statement with the function call expression
37646 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37647 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37648 to initialize the OMP_PRIV artificial variable and there is seventh
37649 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37652 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37654 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37655 gcc_assert (TYPE_REF_P (type
));
37656 type
= TREE_TYPE (type
);
37657 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37658 DECL_ARTIFICIAL (omp_out
) = 1;
37659 pushdecl (omp_out
);
37660 add_decl_expr (omp_out
);
37661 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37662 DECL_ARTIFICIAL (omp_in
) = 1;
37664 add_decl_expr (omp_in
);
37666 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37668 keep_next_level (true);
37669 tree block
= begin_omp_structured_block ();
37670 combiner
= cp_parser_expression (parser
);
37671 finish_expr_stmt (combiner
);
37672 block
= finish_omp_structured_block (block
);
37675 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37678 const char *p
= "";
37679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37681 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37682 p
= IDENTIFIER_POINTER (id
);
37685 if (strcmp (p
, "initializer") == 0)
37687 cp_lexer_consume_token (parser
->lexer
);
37688 matching_parens parens
;
37689 if (!parens
.require_open (parser
))
37693 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37695 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37696 p
= IDENTIFIER_POINTER (id
);
37699 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37700 DECL_ARTIFICIAL (omp_priv
) = 1;
37701 pushdecl (omp_priv
);
37702 add_decl_expr (omp_priv
);
37703 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37704 DECL_ARTIFICIAL (omp_orig
) = 1;
37705 pushdecl (omp_orig
);
37706 add_decl_expr (omp_orig
);
37708 keep_next_level (true);
37709 block
= begin_omp_structured_block ();
37712 if (strcmp (p
, "omp_priv") == 0)
37714 bool is_direct_init
, is_non_constant_init
;
37716 cp_lexer_consume_token (parser
->lexer
);
37717 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37719 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37720 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37722 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37723 == CPP_CLOSE_PAREN
))
37725 finish_omp_structured_block (block
);
37726 error ("invalid initializer clause");
37729 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37730 &is_non_constant_init
);
37731 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37732 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37736 cp_parser_parse_tentatively (parser
);
37737 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37738 /*check_dependency_p=*/true,
37739 /*template_p=*/NULL
,
37740 /*declarator_p=*/false,
37741 /*optional_p=*/false);
37742 vec
<tree
, va_gc
> *args
;
37743 if (fn_name
== error_mark_node
37744 || cp_parser_error_occurred (parser
)
37745 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37746 || ((args
= cp_parser_parenthesized_expression_list
37747 (parser
, non_attr
, /*cast_p=*/false,
37748 /*allow_expansion_p=*/true,
37749 /*non_constant_p=*/NULL
)),
37750 cp_parser_error_occurred (parser
)))
37752 finish_omp_structured_block (block
);
37753 cp_parser_abort_tentative_parse (parser
);
37754 cp_parser_error (parser
, "expected id-expression (arguments)");
37759 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37760 if (arg
== omp_priv
37761 || (TREE_CODE (arg
) == ADDR_EXPR
37762 && TREE_OPERAND (arg
, 0) == omp_priv
))
37764 cp_parser_abort_tentative_parse (parser
);
37765 if (arg
== NULL_TREE
)
37766 error ("one of the initializer call arguments should be %<omp_priv%>"
37767 " or %<&omp_priv%>");
37768 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37770 finish_expr_stmt (initializer
);
37773 block
= finish_omp_structured_block (block
);
37774 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37778 add_decl_expr (omp_orig
);
37780 if (!parens
.require_close (parser
))
37784 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37785 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37792 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37793 initializer-clause[opt] new-line
37795 initializer-clause:
37796 initializer (omp_priv initializer)
37797 initializer (function-name (argument-list)) */
37800 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37801 enum pragma_context
)
37803 auto_vec
<tree
> types
;
37804 enum tree_code reduc_code
= ERROR_MARK
;
37805 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37807 cp_token
*first_token
;
37808 cp_token_cache
*cp
;
37812 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37813 p
= obstack_alloc (&declarator_obstack
, 0);
37815 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37818 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37821 reduc_code
= PLUS_EXPR
;
37824 reduc_code
= MULT_EXPR
;
37827 reduc_code
= MINUS_EXPR
;
37830 reduc_code
= BIT_AND_EXPR
;
37833 reduc_code
= BIT_XOR_EXPR
;
37836 reduc_code
= BIT_IOR_EXPR
;
37839 reduc_code
= TRUTH_ANDIF_EXPR
;
37842 reduc_code
= TRUTH_ORIF_EXPR
;
37845 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37848 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37849 "%<|%>, %<&&%>, %<||%> or identifier");
37853 if (reduc_code
!= ERROR_MARK
)
37854 cp_lexer_consume_token (parser
->lexer
);
37856 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37857 if (reduc_id
== error_mark_node
)
37860 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37863 /* Types may not be defined in declare reduction type list. */
37864 const char *saved_message
;
37865 saved_message
= parser
->type_definition_forbidden_message
;
37866 parser
->type_definition_forbidden_message
37867 = G_("types may not be defined in declare reduction type list");
37868 bool saved_colon_corrects_to_scope_p
;
37869 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37870 parser
->colon_corrects_to_scope_p
= false;
37871 bool saved_colon_doesnt_start_class_def_p
;
37872 saved_colon_doesnt_start_class_def_p
37873 = parser
->colon_doesnt_start_class_def_p
;
37874 parser
->colon_doesnt_start_class_def_p
= true;
37878 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37879 type
= cp_parser_type_id (parser
);
37880 if (type
== error_mark_node
)
37882 else if (ARITHMETIC_TYPE_P (type
)
37883 && (orig_reduc_id
== NULL_TREE
37884 || (TREE_CODE (type
) != COMPLEX_TYPE
37885 && (id_equal (orig_reduc_id
, "min")
37886 || id_equal (orig_reduc_id
, "max")))))
37887 error_at (loc
, "predeclared arithmetic type %qT in "
37888 "%<#pragma omp declare reduction%>", type
);
37889 else if (TREE_CODE (type
) == FUNCTION_TYPE
37890 || TREE_CODE (type
) == METHOD_TYPE
37891 || TREE_CODE (type
) == ARRAY_TYPE
)
37892 error_at (loc
, "function or array type %qT in "
37893 "%<#pragma omp declare reduction%>", type
);
37894 else if (TYPE_REF_P (type
))
37895 error_at (loc
, "reference type %qT in "
37896 "%<#pragma omp declare reduction%>", type
);
37897 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37898 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37899 "%<#pragma omp declare reduction%>", type
);
37901 types
.safe_push (type
);
37903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37904 cp_lexer_consume_token (parser
->lexer
);
37909 /* Restore the saved message. */
37910 parser
->type_definition_forbidden_message
= saved_message
;
37911 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37912 parser
->colon_doesnt_start_class_def_p
37913 = saved_colon_doesnt_start_class_def_p
;
37915 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37916 || types
.is_empty ())
37919 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37923 first_token
= cp_lexer_peek_token (parser
->lexer
);
37926 FOR_EACH_VEC_ELT (types
, i
, type
)
37929 = build_function_type_list (void_type_node
,
37930 cp_build_reference_type (type
, false),
37932 tree this_reduc_id
= reduc_id
;
37933 if (!dependent_type_p (type
))
37934 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37935 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37936 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37937 DECL_ARTIFICIAL (fndecl
) = 1;
37938 DECL_EXTERNAL (fndecl
) = 1;
37939 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37940 DECL_IGNORED_P (fndecl
) = 1;
37941 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37942 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37943 DECL_ATTRIBUTES (fndecl
)
37944 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37945 DECL_ATTRIBUTES (fndecl
));
37946 if (processing_template_decl
)
37947 fndecl
= push_template_decl (fndecl
);
37948 bool block_scope
= false;
37949 tree block
= NULL_TREE
;
37950 if (current_function_decl
)
37952 block_scope
= true;
37953 DECL_CONTEXT (fndecl
) = global_namespace
;
37954 if (!processing_template_decl
)
37957 else if (current_class_type
)
37961 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37962 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37963 cp_lexer_consume_token (parser
->lexer
);
37964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37966 cp
= cp_token_cache_new (first_token
,
37967 cp_lexer_peek_nth_token (parser
->lexer
,
37970 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37971 finish_member_declaration (fndecl
);
37972 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37973 DECL_PENDING_INLINE_P (fndecl
) = 1;
37974 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37979 DECL_CONTEXT (fndecl
) = current_namespace
;
37983 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37985 block
= begin_omp_structured_block ();
37988 cp_parser_push_lexer_for_tokens (parser
, cp
);
37989 parser
->lexer
->in_pragma
= true;
37991 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37994 finish_function (/*inline_p=*/false);
37996 DECL_CONTEXT (fndecl
) = current_function_decl
;
37998 cp_parser_pop_lexer (parser
);
38002 cp_parser_pop_lexer (parser
);
38004 finish_function (/*inline_p=*/false);
38007 DECL_CONTEXT (fndecl
) = current_function_decl
;
38008 block
= finish_omp_structured_block (block
);
38009 if (TREE_CODE (block
) == BIND_EXPR
)
38010 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
38011 else if (TREE_CODE (block
) == STATEMENT_LIST
)
38012 DECL_SAVED_TREE (fndecl
) = block
;
38013 if (processing_template_decl
)
38014 add_decl_expr (fndecl
);
38016 cp_check_omp_declare_reduction (fndecl
);
38017 if (cp
== NULL
&& types
.length () > 1)
38018 cp
= cp_token_cache_new (first_token
,
38019 cp_lexer_peek_nth_token (parser
->lexer
, 2));
38020 if (errs
!= errorcount
)
38024 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38027 /* Free any declarators allocated. */
38028 obstack_free (&declarator_obstack
, p
);
38032 #pragma omp declare simd declare-simd-clauses[optseq] new-line
38033 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38034 initializer-clause[opt] new-line
38035 #pragma omp declare target new-line */
38038 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
38039 enum pragma_context context
)
38041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38043 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38044 const char *p
= IDENTIFIER_POINTER (id
);
38046 if (strcmp (p
, "simd") == 0)
38048 cp_lexer_consume_token (parser
->lexer
);
38049 cp_parser_omp_declare_simd (parser
, pragma_tok
,
38053 cp_ensure_no_omp_declare_simd (parser
);
38054 if (strcmp (p
, "reduction") == 0)
38056 cp_lexer_consume_token (parser
->lexer
);
38057 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
38061 if (!flag_openmp
) /* flag_openmp_simd */
38063 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38066 if (strcmp (p
, "target") == 0)
38068 cp_lexer_consume_token (parser
->lexer
);
38069 cp_parser_omp_declare_target (parser
, pragma_tok
);
38073 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
38075 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38080 #pragma omp taskloop taskloop-clause[optseq] new-line
38083 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
38086 #define OMP_TASKLOOP_CLAUSE_MASK \
38087 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
38094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
38096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
38098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
38099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
38100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
38103 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
38104 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38107 tree clauses
, sb
, ret
;
38109 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38111 strcat (p_name
, " taskloop");
38112 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
38114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38116 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38117 const char *p
= IDENTIFIER_POINTER (id
);
38119 if (strcmp (p
, "simd") == 0)
38121 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38122 if (cclauses
== NULL
)
38123 cclauses
= cclauses_buf
;
38125 cp_lexer_consume_token (parser
->lexer
);
38126 if (!flag_openmp
) /* flag_openmp_simd */
38127 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38129 sb
= begin_omp_structured_block ();
38130 save
= cp_parser_begin_omp_structured_block (parser
);
38131 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38133 cp_parser_end_omp_structured_block (parser
, save
);
38134 tree body
= finish_omp_structured_block (sb
);
38137 ret
= make_node (OMP_TASKLOOP
);
38138 TREE_TYPE (ret
) = void_type_node
;
38139 OMP_FOR_BODY (ret
) = body
;
38140 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38141 SET_EXPR_LOCATION (ret
, loc
);
38146 if (!flag_openmp
) /* flag_openmp_simd */
38148 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38152 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38156 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38157 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38160 sb
= begin_omp_structured_block ();
38161 save
= cp_parser_begin_omp_structured_block (parser
);
38163 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38166 cp_parser_end_omp_structured_block (parser
, save
);
38167 add_stmt (finish_omp_structured_block (sb
));
38174 # pragma acc routine oacc-routine-clause[optseq] new-line
38175 function-definition
38177 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38180 #define OACC_ROUTINE_CLAUSE_MASK \
38181 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38187 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38188 component, which must resolve to a declared namespace-scope
38189 function. The clauses are either processed directly (for a named
38190 function), or defered until the immediatley following declaration
38194 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38195 enum pragma_context context
)
38197 gcc_checking_assert (context
== pragma_external
);
38198 /* The checking for "another pragma following this one" in the "no optional
38199 '( name )'" case makes sure that we dont re-enter. */
38200 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38202 cp_oacc_routine_data data
;
38203 data
.error_seen
= false;
38204 data
.fndecl_seen
= false;
38205 data
.tokens
= vNULL
;
38206 data
.clauses
= NULL_TREE
;
38207 data
.loc
= pragma_tok
->location
;
38208 /* It is safe to take the address of a local variable; it will only be
38209 used while this scope is live. */
38210 parser
->oacc_routine
= &data
;
38212 /* Look for optional '( name )'. */
38213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38215 matching_parens parens
;
38216 parens
.consume_open (parser
); /* '(' */
38218 /* We parse the name as an id-expression. If it resolves to
38219 anything other than a non-overloaded function at namespace
38220 scope, it's an error. */
38221 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38222 tree name
= cp_parser_id_expression (parser
,
38223 /*template_keyword_p=*/false,
38224 /*check_dependency_p=*/false,
38225 /*template_p=*/NULL
,
38226 /*declarator_p=*/false,
38227 /*optional_p=*/false);
38228 tree decl
= (identifier_p (name
)
38229 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38231 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38232 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38234 if (decl
== error_mark_node
38235 || !parens
.require_close (parser
))
38237 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38238 parser
->oacc_routine
= NULL
;
38243 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38244 "#pragma acc routine",
38245 cp_lexer_peek_token (parser
->lexer
));
38247 if (decl
&& is_overloaded_fn (decl
)
38248 && (TREE_CODE (decl
) != FUNCTION_DECL
38249 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38251 error_at (name_loc
,
38252 "%<#pragma acc routine%> names a set of overloads");
38253 parser
->oacc_routine
= NULL
;
38257 /* Perhaps we should use the same rule as declarations in different
38259 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38261 error_at (name_loc
,
38262 "%qD does not refer to a namespace scope function", decl
);
38263 parser
->oacc_routine
= NULL
;
38267 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38269 error_at (name_loc
, "%qD does not refer to a function", decl
);
38270 parser
->oacc_routine
= NULL
;
38274 cp_finalize_oacc_routine (parser
, decl
, false);
38275 parser
->oacc_routine
= NULL
;
38277 else /* No optional '( name )'. */
38279 /* Store away all pragma tokens. */
38280 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38281 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38282 cp_lexer_consume_token (parser
->lexer
);
38283 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38284 parser
->oacc_routine
->error_seen
= true;
38285 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38286 struct cp_token_cache
*cp
38287 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38288 parser
->oacc_routine
->tokens
.safe_push (cp
);
38290 /* Emit a helpful diagnostic if there's another pragma following this
38292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38294 cp_ensure_no_oacc_routine (parser
);
38295 data
.tokens
.release ();
38296 /* ..., and then just keep going. */
38300 /* We only have to consider the pragma_external case here. */
38301 cp_parser_declaration (parser
);
38302 if (parser
->oacc_routine
38303 && !parser
->oacc_routine
->fndecl_seen
)
38304 cp_ensure_no_oacc_routine (parser
);
38306 parser
->oacc_routine
= NULL
;
38307 data
.tokens
.release ();
38311 /* Finalize #pragma acc routine clauses after direct declarator has
38315 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38317 struct cp_token_cache
*ce
;
38318 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38320 if (!data
->error_seen
&& data
->fndecl_seen
)
38322 error_at (data
->loc
,
38323 "%<#pragma acc routine%> not immediately followed by "
38324 "a single function declaration or definition");
38325 data
->error_seen
= true;
38327 if (data
->error_seen
)
38330 gcc_checking_assert (data
->tokens
.length () == 1);
38331 ce
= data
->tokens
[0];
38333 cp_parser_push_lexer_for_tokens (parser
, ce
);
38334 parser
->lexer
->in_pragma
= true;
38335 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38337 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38338 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38339 parser
->oacc_routine
->clauses
38340 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38341 "#pragma acc routine", pragma_tok
);
38342 cp_parser_pop_lexer (parser
);
38343 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38349 /* Apply any saved OpenACC routine clauses to a just-parsed
38353 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38355 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38357 /* Keep going if we're in error reporting mode. */
38358 if (parser
->oacc_routine
->error_seen
38359 || fndecl
== error_mark_node
)
38362 if (parser
->oacc_routine
->fndecl_seen
)
38364 error_at (parser
->oacc_routine
->loc
,
38365 "%<#pragma acc routine%> not immediately followed by"
38366 " a single function declaration or definition");
38367 parser
->oacc_routine
= NULL
;
38370 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38372 cp_ensure_no_oacc_routine (parser
);
38376 if (oacc_get_fn_attrib (fndecl
))
38378 error_at (parser
->oacc_routine
->loc
,
38379 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38380 parser
->oacc_routine
= NULL
;
38384 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38386 error_at (parser
->oacc_routine
->loc
,
38388 ? G_("%<#pragma acc routine%> must be applied before use")
38389 : G_("%<#pragma acc routine%> must be applied before "
38391 parser
->oacc_routine
= NULL
;
38395 /* Process the routine's dimension clauses. */
38396 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38397 oacc_replace_fn_attrib (fndecl
, dims
);
38399 /* Add an "omp declare target" attribute. */
38400 DECL_ATTRIBUTES (fndecl
)
38401 = tree_cons (get_identifier ("omp declare target"),
38402 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38404 /* Don't unset parser->oacc_routine here: we may still need it to
38405 diagnose wrong usage. But, remember that we've used this "#pragma acc
38407 parser
->oacc_routine
->fndecl_seen
= true;
38411 /* Main entry point to OpenMP statement pragmas. */
38414 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38417 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38418 omp_clause_mask
mask (0);
38420 switch (cp_parser_pragma_kind (pragma_tok
))
38422 case PRAGMA_OACC_ATOMIC
:
38423 cp_parser_omp_atomic (parser
, pragma_tok
);
38425 case PRAGMA_OACC_CACHE
:
38426 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38428 case PRAGMA_OACC_DATA
:
38429 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38431 case PRAGMA_OACC_ENTER_DATA
:
38432 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38434 case PRAGMA_OACC_EXIT_DATA
:
38435 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38437 case PRAGMA_OACC_HOST_DATA
:
38438 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38440 case PRAGMA_OACC_KERNELS
:
38441 case PRAGMA_OACC_PARALLEL
:
38442 strcpy (p_name
, "#pragma acc");
38443 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38446 case PRAGMA_OACC_LOOP
:
38447 strcpy (p_name
, "#pragma acc");
38448 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38451 case PRAGMA_OACC_UPDATE
:
38452 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38454 case PRAGMA_OACC_WAIT
:
38455 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38457 case PRAGMA_OMP_ATOMIC
:
38458 cp_parser_omp_atomic (parser
, pragma_tok
);
38460 case PRAGMA_OMP_CRITICAL
:
38461 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38463 case PRAGMA_OMP_DISTRIBUTE
:
38464 strcpy (p_name
, "#pragma omp");
38465 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38468 case PRAGMA_OMP_FOR
:
38469 strcpy (p_name
, "#pragma omp");
38470 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38473 case PRAGMA_OMP_MASTER
:
38474 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38476 case PRAGMA_OMP_PARALLEL
:
38477 strcpy (p_name
, "#pragma omp");
38478 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38481 case PRAGMA_OMP_SECTIONS
:
38482 strcpy (p_name
, "#pragma omp");
38483 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38485 case PRAGMA_OMP_SIMD
:
38486 strcpy (p_name
, "#pragma omp");
38487 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38490 case PRAGMA_OMP_SINGLE
:
38491 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38493 case PRAGMA_OMP_TASK
:
38494 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38496 case PRAGMA_OMP_TASKGROUP
:
38497 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38499 case PRAGMA_OMP_TASKLOOP
:
38500 strcpy (p_name
, "#pragma omp");
38501 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38504 case PRAGMA_OMP_TEAMS
:
38505 strcpy (p_name
, "#pragma omp");
38506 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38510 gcc_unreachable ();
38513 protected_set_expr_location (stmt
, pragma_tok
->location
);
38516 /* Transactional Memory parsing routines. */
38518 /* Parse a transaction attribute.
38524 We use this instead of cp_parser_attributes_opt for transactions to avoid
38525 the pedwarn in C++98 mode. */
38528 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38531 tree attr_name
, attr
= NULL
;
38533 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38534 return cp_parser_attributes_opt (parser
);
38536 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38538 cp_lexer_consume_token (parser
->lexer
);
38539 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38542 token
= cp_lexer_peek_token (parser
->lexer
);
38543 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38545 token
= cp_lexer_consume_token (parser
->lexer
);
38547 attr_name
= (token
->type
== CPP_KEYWORD
38548 /* For keywords, use the canonical spelling,
38549 not the parsed identifier. */
38550 ? ridpointers
[(int) token
->keyword
]
38552 attr
= build_tree_list (attr_name
, NULL_TREE
);
38555 cp_parser_error (parser
, "expected identifier");
38557 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38559 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38563 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38565 transaction-statement:
38566 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38568 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38572 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38574 unsigned char old_in
= parser
->in_transaction
;
38575 unsigned char this_in
= 1, new_in
;
38576 enum rid keyword
= token
->keyword
;
38577 tree stmt
, attrs
, noex
;
38579 cp_lexer_consume_token (parser
->lexer
);
38581 if (keyword
== RID_TRANSACTION_RELAXED
38582 || keyword
== RID_SYNCHRONIZED
)
38583 this_in
|= TM_STMT_ATTR_RELAXED
;
38586 attrs
= cp_parser_txn_attribute_opt (parser
);
38588 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38591 /* Parse a noexcept specification. */
38592 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38593 noex
= boolean_true_node
;
38594 else if (keyword
== RID_ATOMIC_CANCEL
)
38596 /* cancel-and-throw is unimplemented. */
38597 sorry ("atomic_cancel");
38601 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38603 /* Keep track if we're in the lexical scope of an outer transaction. */
38604 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38606 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38608 parser
->in_transaction
= new_in
;
38609 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38610 parser
->in_transaction
= old_in
;
38612 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38617 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38619 transaction-expression:
38620 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38621 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38625 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38627 unsigned char old_in
= parser
->in_transaction
;
38628 unsigned char this_in
= 1;
38632 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38634 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38635 || keyword
== RID_TRANSACTION_RELAXED
);
38639 keyword
== RID_TRANSACTION_RELAXED
38640 ? G_("%<__transaction_relaxed%> without transactional memory "
38642 : G_("%<__transaction_atomic%> without transactional memory "
38643 "support enabled"));
38645 token
= cp_parser_require_keyword (parser
, keyword
,
38646 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38647 : RT_TRANSACTION_RELAXED
));
38648 gcc_assert (token
!= NULL
);
38650 if (keyword
== RID_TRANSACTION_RELAXED
)
38651 this_in
|= TM_STMT_ATTR_RELAXED
;
38653 /* Set this early. This might mean that we allow transaction_cancel in
38654 an expression that we find out later actually has to be a constexpr.
38655 However, we expect that cxx_constant_value will be able to deal with
38656 this; also, if the noexcept has no constexpr, then what we parse next
38657 really is a transaction's body. */
38658 parser
->in_transaction
= this_in
;
38660 /* Parse a noexcept specification. */
38661 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38664 if (!noex
|| !noex_expr
38665 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38667 matching_parens parens
;
38668 parens
.require_open (parser
);
38670 expr
= cp_parser_expression (parser
);
38671 expr
= finish_parenthesized_expr (expr
);
38673 parens
.require_close (parser
);
38677 /* The only expression that is available got parsed for the noexcept
38678 already. noexcept is true then. */
38680 noex
= boolean_true_node
;
38683 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38684 parser
->in_transaction
= old_in
;
38686 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38687 return error_mark_node
;
38689 return (flag_tm
? expr
: error_mark_node
);
38692 /* Parse a function-transaction-block.
38694 function-transaction-block:
38695 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38697 __transaction_atomic txn-attribute[opt] function-try-block
38698 __transaction_relaxed ctor-initializer[opt] function-body
38699 __transaction_relaxed function-try-block
38703 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38705 unsigned char old_in
= parser
->in_transaction
;
38706 unsigned char new_in
= 1;
38707 tree compound_stmt
, stmt
, attrs
;
38710 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38711 || keyword
== RID_TRANSACTION_RELAXED
);
38712 token
= cp_parser_require_keyword (parser
, keyword
,
38713 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38714 : RT_TRANSACTION_RELAXED
));
38715 gcc_assert (token
!= NULL
);
38717 if (keyword
== RID_TRANSACTION_RELAXED
)
38718 new_in
|= TM_STMT_ATTR_RELAXED
;
38721 attrs
= cp_parser_txn_attribute_opt (parser
);
38723 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38726 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38728 parser
->in_transaction
= new_in
;
38730 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38731 cp_parser_function_try_block (parser
);
38733 cp_parser_ctor_initializer_opt_and_function_body
38734 (parser
, /*in_function_try_block=*/false);
38736 parser
->in_transaction
= old_in
;
38738 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38741 /* Parse a __transaction_cancel statement.
38744 __transaction_cancel txn-attribute[opt] ;
38745 __transaction_cancel txn-attribute[opt] throw-expression ;
38747 ??? Cancel and throw is not yet implemented. */
38750 cp_parser_transaction_cancel (cp_parser
*parser
)
38753 bool is_outer
= false;
38756 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38757 RT_TRANSACTION_CANCEL
);
38758 gcc_assert (token
!= NULL
);
38760 attrs
= cp_parser_txn_attribute_opt (parser
);
38762 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38764 /* ??? Parse cancel-and-throw here. */
38766 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38770 error_at (token
->location
, "%<__transaction_cancel%> without "
38771 "transactional memory support enabled");
38772 return error_mark_node
;
38774 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38776 error_at (token
->location
, "%<__transaction_cancel%> within a "
38777 "%<__transaction_relaxed%>");
38778 return error_mark_node
;
38782 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38783 && !is_tm_may_cancel_outer (current_function_decl
))
38785 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38786 "within outer %<__transaction_atomic%>");
38787 error_at (token
->location
,
38788 " or a %<transaction_may_cancel_outer%> function");
38789 return error_mark_node
;
38792 else if (parser
->in_transaction
== 0)
38794 error_at (token
->location
, "%<__transaction_cancel%> not within "
38795 "%<__transaction_atomic%>");
38796 return error_mark_node
;
38799 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38807 static GTY (()) cp_parser
*the_parser
;
38810 /* Special handling for the first token or line in the file. The first
38811 thing in the file might be #pragma GCC pch_preprocess, which loads a
38812 PCH file, which is a GC collection point. So we need to handle this
38813 first pragma without benefit of an existing lexer structure.
38815 Always returns one token to the caller in *FIRST_TOKEN. This is
38816 either the true first token of the file, or the first token after
38817 the initial pragma. */
38820 cp_parser_initial_pragma (cp_token
*first_token
)
38824 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38825 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38828 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38829 if (first_token
->type
== CPP_STRING
)
38831 name
= first_token
->u
.value
;
38833 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38834 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38835 error_at (first_token
->location
,
38836 "junk at end of %<#pragma GCC pch_preprocess%>");
38839 error_at (first_token
->location
, "expected string literal");
38841 /* Skip to the end of the pragma. */
38842 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38843 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38845 /* Now actually load the PCH file. */
38847 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38849 /* Read one more token to return to our caller. We have to do this
38850 after reading the PCH file in, since its pointers have to be
38852 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38855 /* Parse a pragma GCC ivdep. */
38858 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38860 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38864 /* Parse a pragma GCC unroll. */
38866 static unsigned short
38867 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38869 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38870 tree expr
= cp_parser_constant_expression (parser
);
38871 unsigned short unroll
;
38872 expr
= maybe_constant_value (expr
);
38873 HOST_WIDE_INT lunroll
= 0;
38874 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38875 || TREE_CODE (expr
) != INTEGER_CST
38876 || (lunroll
= tree_to_shwi (expr
)) < 0
38877 || lunroll
>= USHRT_MAX
)
38879 error_at (location
, "%<#pragma GCC unroll%> requires an"
38880 " assignment-expression that evaluates to a non-negative"
38881 " integral constant less than %u", USHRT_MAX
);
38886 unroll
= (unsigned short)lunroll
;
38890 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38894 /* Normal parsing of a pragma token. Here we can (and must) use the
38898 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38900 cp_token
*pragma_tok
;
38905 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38906 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38907 parser
->lexer
->in_pragma
= true;
38909 id
= cp_parser_pragma_kind (pragma_tok
);
38910 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38911 cp_ensure_no_omp_declare_simd (parser
);
38914 case PRAGMA_GCC_PCH_PREPROCESS
:
38915 error_at (pragma_tok
->location
,
38916 "%<#pragma GCC pch_preprocess%> must be first");
38919 case PRAGMA_OMP_BARRIER
:
38922 case pragma_compound
:
38923 cp_parser_omp_barrier (parser
, pragma_tok
);
38926 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38927 "used in compound statements", "omp barrier");
38934 case PRAGMA_OMP_FLUSH
:
38937 case pragma_compound
:
38938 cp_parser_omp_flush (parser
, pragma_tok
);
38941 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38942 "used in compound statements", "omp flush");
38949 case PRAGMA_OMP_TASKWAIT
:
38952 case pragma_compound
:
38953 cp_parser_omp_taskwait (parser
, pragma_tok
);
38956 error_at (pragma_tok
->location
,
38957 "%<#pragma %s%> may only be used in compound statements",
38965 case PRAGMA_OMP_TASKYIELD
:
38968 case pragma_compound
:
38969 cp_parser_omp_taskyield (parser
, pragma_tok
);
38972 error_at (pragma_tok
->location
,
38973 "%<#pragma %s%> may only be used in compound statements",
38981 case PRAGMA_OMP_CANCEL
:
38984 case pragma_compound
:
38985 cp_parser_omp_cancel (parser
, pragma_tok
);
38988 error_at (pragma_tok
->location
,
38989 "%<#pragma %s%> may only be used in compound statements",
38997 case PRAGMA_OMP_CANCELLATION_POINT
:
38998 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
39001 case PRAGMA_OMP_THREADPRIVATE
:
39002 cp_parser_omp_threadprivate (parser
, pragma_tok
);
39005 case PRAGMA_OMP_DECLARE
:
39006 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
39008 case PRAGMA_OACC_DECLARE
:
39009 cp_parser_oacc_declare (parser
, pragma_tok
);
39012 case PRAGMA_OACC_ENTER_DATA
:
39013 if (context
== pragma_stmt
)
39015 error_at (pragma_tok
->location
,
39016 "%<#pragma %s%> may only be used in compound statements",
39020 else if (context
!= pragma_compound
)
39022 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
39025 case PRAGMA_OACC_EXIT_DATA
:
39026 if (context
== pragma_stmt
)
39028 error_at (pragma_tok
->location
,
39029 "%<#pragma %s%> may only be used in compound statements",
39033 else if (context
!= pragma_compound
)
39035 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
39038 case PRAGMA_OACC_ROUTINE
:
39039 if (context
!= pragma_external
)
39041 error_at (pragma_tok
->location
,
39042 "%<#pragma acc routine%> must be at file scope");
39045 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
39048 case PRAGMA_OACC_UPDATE
:
39049 if (context
== pragma_stmt
)
39051 error_at (pragma_tok
->location
,
39052 "%<#pragma %s%> may only be used in compound statements",
39056 else if (context
!= pragma_compound
)
39058 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
39061 case PRAGMA_OACC_WAIT
:
39062 if (context
== pragma_stmt
)
39064 error_at (pragma_tok
->location
,
39065 "%<#pragma %s%> may only be used in compound statements",
39069 else if (context
!= pragma_compound
)
39071 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
39074 case PRAGMA_OACC_ATOMIC
:
39075 case PRAGMA_OACC_CACHE
:
39076 case PRAGMA_OACC_DATA
:
39077 case PRAGMA_OACC_HOST_DATA
:
39078 case PRAGMA_OACC_KERNELS
:
39079 case PRAGMA_OACC_PARALLEL
:
39080 case PRAGMA_OACC_LOOP
:
39081 case PRAGMA_OMP_ATOMIC
:
39082 case PRAGMA_OMP_CRITICAL
:
39083 case PRAGMA_OMP_DISTRIBUTE
:
39084 case PRAGMA_OMP_FOR
:
39085 case PRAGMA_OMP_MASTER
:
39086 case PRAGMA_OMP_PARALLEL
:
39087 case PRAGMA_OMP_SECTIONS
:
39088 case PRAGMA_OMP_SIMD
:
39089 case PRAGMA_OMP_SINGLE
:
39090 case PRAGMA_OMP_TASK
:
39091 case PRAGMA_OMP_TASKGROUP
:
39092 case PRAGMA_OMP_TASKLOOP
:
39093 case PRAGMA_OMP_TEAMS
:
39094 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
39096 stmt
= push_omp_privatization_clauses (false);
39097 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
39098 pop_omp_privatization_clauses (stmt
);
39101 case PRAGMA_OMP_ORDERED
:
39102 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
39104 stmt
= push_omp_privatization_clauses (false);
39105 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
39106 pop_omp_privatization_clauses (stmt
);
39109 case PRAGMA_OMP_TARGET
:
39110 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
39112 stmt
= push_omp_privatization_clauses (false);
39113 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
39114 pop_omp_privatization_clauses (stmt
);
39117 case PRAGMA_OMP_END_DECLARE_TARGET
:
39118 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
39121 case PRAGMA_OMP_SECTION
:
39122 error_at (pragma_tok
->location
,
39123 "%<#pragma omp section%> may only be used in "
39124 "%<#pragma omp sections%> construct");
39129 if (context
== pragma_external
)
39131 error_at (pragma_tok
->location
,
39132 "%<#pragma GCC ivdep%> must be inside a function");
39135 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39136 unsigned short unroll
;
39137 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39138 if (tok
->type
== CPP_PRAGMA
39139 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39141 tok
= cp_lexer_consume_token (parser
->lexer
);
39142 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39143 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39147 if (tok
->type
!= CPP_KEYWORD
39148 || (tok
->keyword
!= RID_FOR
39149 && tok
->keyword
!= RID_WHILE
39150 && tok
->keyword
!= RID_DO
))
39152 cp_parser_error (parser
, "for, while or do statement expected");
39155 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39159 case PRAGMA_UNROLL
:
39161 if (context
== pragma_external
)
39163 error_at (pragma_tok
->location
,
39164 "%<#pragma GCC unroll%> must be inside a function");
39167 const unsigned short unroll
39168 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39170 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39171 if (tok
->type
== CPP_PRAGMA
39172 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39174 tok
= cp_lexer_consume_token (parser
->lexer
);
39175 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39176 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39180 if (tok
->type
!= CPP_KEYWORD
39181 || (tok
->keyword
!= RID_FOR
39182 && tok
->keyword
!= RID_WHILE
39183 && tok
->keyword
!= RID_DO
))
39185 cp_parser_error (parser
, "for, while or do statement expected");
39188 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39193 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39194 c_invoke_pragma_handler (id
);
39198 cp_parser_error (parser
, "expected declaration specifiers");
39202 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39206 /* The interface the pragma parsers have to the lexer. */
39209 pragma_lex (tree
*value
, location_t
*loc
)
39211 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39212 enum cpp_ttype ret
= tok
->type
;
39214 *value
= tok
->u
.value
;
39216 *loc
= tok
->location
;
39218 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39220 else if (ret
== CPP_STRING
)
39221 *value
= cp_parser_string_literal (the_parser
, false, false);
39224 if (ret
== CPP_KEYWORD
)
39226 cp_lexer_consume_token (the_parser
->lexer
);
39233 /* External interface. */
39235 /* Parse one entire translation unit. */
39238 c_parse_file (void)
39240 static bool already_called
= false;
39242 if (already_called
)
39243 fatal_error (input_location
,
39244 "inter-module optimizations not implemented for C++");
39245 already_called
= true;
39247 the_parser
= cp_parser_new ();
39248 push_deferring_access_checks (flag_access_control
39249 ? dk_no_deferred
: dk_no_check
);
39250 cp_parser_translation_unit (the_parser
);
39253 finish_translation_unit ();
39256 /* Create an identifier for a generic parameter type (a synthesized
39257 template parameter implied by `auto' or a concept identifier). */
39259 static GTY(()) int generic_parm_count
;
39261 make_generic_type_name ()
39264 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39265 return get_identifier (buf
);
39268 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39269 (creating a new template parameter list if necessary). Returns the newly
39270 created template type parm. */
39273 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39275 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39277 /* Before committing to modifying any scope, if we're in an
39278 implicit template scope, and we're trying to synthesize a
39279 constrained parameter, try to find a previous parameter with
39280 the same name. This is the same-type rule for abbreviated
39281 function templates.
39283 NOTE: We can generate implicit parameters when tentatively
39284 parsing a nested name specifier, only to reject that parse
39285 later. However, matching the same template-id as part of a
39286 direct-declarator should generate an identical template
39287 parameter, so this rule will merge them. */
39288 if (parser
->implicit_template_scope
&& constr
)
39290 tree t
= parser
->implicit_template_parms
;
39293 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39295 tree d
= TREE_VALUE (t
);
39296 if (TREE_CODE (d
) == PARM_DECL
)
39297 /* Return the TEMPLATE_PARM_INDEX. */
39298 d
= DECL_INITIAL (d
);
39301 t
= TREE_CHAIN (t
);
39305 /* We are either continuing a function template that already contains implicit
39306 template parameters, creating a new fully-implicit function template, or
39307 extending an existing explicit function template with implicit template
39310 cp_binding_level
*const entry_scope
= current_binding_level
;
39312 bool become_template
= false;
39313 cp_binding_level
*parent_scope
= 0;
39315 if (parser
->implicit_template_scope
)
39317 gcc_assert (parser
->implicit_template_parms
);
39319 current_binding_level
= parser
->implicit_template_scope
;
39323 /* Roll back to the existing template parameter scope (in the case of
39324 extending an explicit function template) or introduce a new template
39325 parameter scope ahead of the function parameter scope (or class scope
39326 in the case of out-of-line member definitions). The function scope is
39327 added back after template parameter synthesis below. */
39329 cp_binding_level
*scope
= entry_scope
;
39331 while (scope
->kind
== sk_function_parms
)
39333 parent_scope
= scope
;
39334 scope
= scope
->level_chain
;
39336 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39338 /* If not defining a class, then any class scope is a scope level in
39339 an out-of-line member definition. In this case simply wind back
39340 beyond the first such scope to inject the template parameter list.
39341 Otherwise wind back to the class being defined. The latter can
39342 occur in class member friend declarations such as:
39348 friend void A::foo (auto);
39351 The template parameter list synthesized for the friend declaration
39352 must be injected in the scope of 'B'. This can also occur in
39353 erroneous cases such as:
39359 void B::foo (auto) {}
39362 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39363 but, nevertheless, the template parameter list synthesized for the
39364 declarator should be injected into the scope of 'A' as if the
39365 ill-formed template was specified explicitly. */
39367 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39369 parent_scope
= scope
;
39370 scope
= scope
->level_chain
;
39374 current_binding_level
= scope
;
39376 if (scope
->kind
!= sk_template_parms
39377 || !function_being_declared_is_template_p (parser
))
39379 /* Introduce a new template parameter list for implicit template
39382 become_template
= true;
39384 parser
->implicit_template_scope
39385 = begin_scope (sk_template_parms
, NULL
);
39387 ++processing_template_decl
;
39389 parser
->fully_implicit_function_template_p
= true;
39390 ++parser
->num_template_parameter_lists
;
39394 /* Synthesize implicit template parameters at the end of the explicit
39395 template parameter list. */
39397 gcc_assert (current_template_parms
);
39399 parser
->implicit_template_scope
= scope
;
39401 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39402 parser
->implicit_template_parms
39403 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39407 /* Synthesize a new template parameter and track the current template
39408 parameter chain with implicit_template_parms. */
39410 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39411 tree synth_id
= make_generic_type_name ();
39412 tree synth_tmpl_parm
;
39413 bool non_type
= false;
39415 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39417 = finish_template_type_parm (class_type_node
, synth_id
);
39418 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39420 = finish_constrained_template_template_parm (proto
, synth_id
);
39423 synth_tmpl_parm
= copy_decl (proto
);
39424 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39428 // Attach the constraint to the parm before processing.
39429 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39430 TREE_TYPE (node
) = constr
;
39432 = process_template_parm (parser
->implicit_template_parms
,
39435 /*non_type=*/non_type
,
39436 /*param_pack=*/false);
39438 // Chain the new parameter to the list of implicit parameters.
39439 if (parser
->implicit_template_parms
)
39440 parser
->implicit_template_parms
39441 = TREE_CHAIN (parser
->implicit_template_parms
);
39443 parser
->implicit_template_parms
= new_parm
;
39445 tree new_decl
= get_local_decls ();
39447 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39448 new_decl
= DECL_INITIAL (new_decl
);
39450 /* If creating a fully implicit function template, start the new implicit
39451 template parameter list with this synthesized type, otherwise grow the
39452 current template parameter list. */
39454 if (become_template
)
39456 parent_scope
->level_chain
= current_binding_level
;
39458 tree new_parms
= make_tree_vec (1);
39459 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39460 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39461 new_parms
, current_template_parms
);
39465 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39466 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39467 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39468 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39471 // If the new parameter was constrained, we need to add that to the
39472 // constraints in the template parameter list.
39473 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39475 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39476 reqs
= conjoin_constraints (reqs
, req
);
39477 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39480 current_binding_level
= entry_scope
;
39485 /* Finish the declaration of a fully implicit function template. Such a
39486 template has no explicit template parameter list so has not been through the
39487 normal template head and tail processing. synthesize_implicit_template_parm
39488 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39489 provided if the declaration is a class member such that its template
39490 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39491 form is returned. Otherwise NULL_TREE is returned. */
39494 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39496 gcc_assert (parser
->fully_implicit_function_template_p
);
39498 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39499 && DECL_VIRTUAL_P (member_decl_opt
))
39501 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39502 "implicit templates may not be %<virtual%>");
39503 DECL_VIRTUAL_P (member_decl_opt
) = false;
39506 if (member_decl_opt
)
39507 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39508 end_template_decl ();
39510 parser
->fully_implicit_function_template_p
= false;
39511 parser
->implicit_template_parms
= 0;
39512 parser
->implicit_template_scope
= 0;
39513 --parser
->num_template_parameter_lists
;
39515 return member_decl_opt
;
39518 /* Like finish_fully_implicit_template, but to be used in error
39519 recovery, rearranging scopes so that we restore the state we had
39520 before synthesize_implicit_template_parm inserted the implement
39521 template parms scope. */
39524 abort_fully_implicit_template (cp_parser
*parser
)
39526 cp_binding_level
*return_to_scope
= current_binding_level
;
39528 if (parser
->implicit_template_scope
39529 && return_to_scope
!= parser
->implicit_template_scope
)
39531 cp_binding_level
*child
= return_to_scope
;
39532 for (cp_binding_level
*scope
= child
->level_chain
;
39533 scope
!= parser
->implicit_template_scope
;
39534 scope
= child
->level_chain
)
39536 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39537 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39538 current_binding_level
= parser
->implicit_template_scope
;
39541 return_to_scope
= return_to_scope
->level_chain
;
39543 finish_fully_implicit_template (parser
, NULL
);
39545 gcc_assert (current_binding_level
== return_to_scope
);
39548 /* Helper function for diagnostics that have complained about things
39549 being used with 'extern "C"' linkage.
39551 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39554 maybe_show_extern_c_location (void)
39556 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39557 inform (the_parser
->innermost_linkage_specification_location
,
39558 "%<extern \"C\"%> linkage started here");
39561 #include "gt-cp-parser.h"